Skip to content

Commit

Permalink
fix(HLS): Fix init segment when EXT-X-MAP is preceded by EXT-X-BYTERA…
Browse files Browse the repository at this point in the history
…NGE (#5732)
  • Loading branch information
avelad authored Oct 5, 2023
1 parent bdbaae5 commit 24e5de3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2859,16 +2859,19 @@ shaka.hls.HlsParser = class {
if (!this.mapTagToInitSegmentRefMap_.has(mapTagKey)) {
/** @type {shaka.extern.aes128Key|undefined} */
let aes128Key = undefined;
let byteRangeTag = null;
for (const tag of tags) {
if (tag.name == 'EXT-X-KEY') {
if (tag.getRequiredAttrValue('METHOD') == 'AES-128' &&
tag.id < mapTag.id) {
aes128Key = this.parseAES128DrmTag_(tag, playlist);
}
} else if (tag.name == 'EXT-X-BYTERANGE' && tag.id < mapTag.id) {
byteRangeTag = tag;
}
}
const initSegmentRef = this.createInitSegmentReference_(
absoluteInitSegmentUri, mapTag, aes128Key);
absoluteInitSegmentUri, mapTag, byteRangeTag, aes128Key);
this.mapTagToInitSegmentRefMap_.set(mapTagKey, initSegmentRef);
}
return this.mapTagToInitSegmentRefMap_.get(mapTagKey);
Expand All @@ -2879,14 +2882,19 @@ shaka.hls.HlsParser = class {
* playlist.
* @param {string} absoluteInitSegmentUri
* @param {!shaka.hls.Tag} mapTag EXT-X-MAP
* @param {shaka.hls.Tag=} byteRangeTag EXT-X-BYTERANGE
* @param {shaka.extern.aes128Key=} aes128Key
* @return {!shaka.media.InitSegmentReference}
* @private
*/
createInitSegmentReference_(absoluteInitSegmentUri, mapTag, aes128Key) {
createInitSegmentReference_(absoluteInitSegmentUri, mapTag, byteRangeTag,
aes128Key) {
let startByte = 0;
let endByte = null;
const byterange = mapTag.getAttributeValue('BYTERANGE');
let byterange = mapTag.getAttributeValue('BYTERANGE');
if (!byterange && byteRangeTag) {
byterange = byteRangeTag.value;
}
// If a BYTERANGE attribute is not specified, the segment consists
// of the entire resource.
if (byterange) {
Expand Down
14 changes: 11 additions & 3 deletions test/hls/hls_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2976,7 +2976,8 @@ describe('HlsParser', () => {
const media = [
'#EXTM3U\n',
'#EXT-X-PLAYLIST-TYPE:VOD\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXT-X-BYTERANGE:616@0\n',
'#EXT-X-MAP:URI="init.mp4"\n',
'#EXTINF:5,\n',
'main.mp4\n',
'#EXT-X-MAP:URI="init2.mp4",BYTERANGE="616@0"\n',
Expand Down Expand Up @@ -3004,8 +3005,15 @@ describe('HlsParser', () => {
// uri.
const initSegments = Array.from(actualVideo.segmentIndex).map(
(seg) => seg.initSegmentReference);
expect(initSegments[0].getUris()[0]).toBe('test:/init.mp4');
expect(initSegments[1].getUris()[0]).toBe('test:/init2.mp4');
expect(initSegments.length).toBe(2);
const firstInitSegment = initSegments[0];
expect(firstInitSegment.getUris()[0]).toBe('test:/init.mp4');
expect(firstInitSegment.startByte).toBe(0);
expect(firstInitSegment.endByte).toBe(615);
const secondInitSegment = initSegments[1];
expect(secondInitSegment.getUris()[0]).toBe('test:/init2.mp4');
expect(secondInitSegment.startByte).toBe(0);
expect(secondInitSegment.endByte).toBe(615);
});

it('parses variants encrypted with AES-128', async () => {
Expand Down

0 comments on commit 24e5de3

Please sign in to comment.