Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HLS): Support AES-128 in init segment according the RFC #5677

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ shaka.hls.HlsParser = class {

let segmentUris = [firstSegment.absoluteUri];
const initSegmentRef = this.getInitSegmentReference_(
playlist.absoluteUri, firstSegment.tags, new Map());
playlist, firstSegment.tags, new Map());
if (initSegmentRef) {
segmentUris = initSegmentRef.getUris();
}
Expand Down Expand Up @@ -2811,13 +2811,13 @@ shaka.hls.HlsParser = class {

/**
* Get the InitSegmentReference for a segment if it has a EXT-X-MAP tag.
* @param {string} playlistUri The absolute uri of the media playlist.
* @param {!shaka.hls.Playlist} playlist
* @param {!Array.<!shaka.hls.Tag>} tags Segment tags
* @param {!Map.<string, string>} variables
* @return {shaka.media.InitSegmentReference}
* @private
*/
getInitSegmentReference_(playlistUri, tags, variables) {
getInitSegmentReference_(playlist, tags, variables) {
/** @type {?shaka.hls.Tag} */
const mapTag = shaka.hls.Utils.getFirstTagWithName(tags, 'EXT-X-MAP');

Expand All @@ -2828,16 +2828,26 @@ shaka.hls.HlsParser = class {
const verbatimInitSegmentUri = mapTag.getRequiredAttrValue('URI');
const absoluteInitSegmentUri = this.variableSubstitution_(
shaka.hls.Utils.constructAbsoluteUri(
playlistUri, verbatimInitSegmentUri),
playlist.absoluteUri, verbatimInitSegmentUri),
variables);

const mapTagKey = [
absoluteInitSegmentUri,
mapTag.getAttributeValue('BYTERANGE', ''),
].join('-');
if (!this.mapTagToInitSegmentRefMap_.has(mapTagKey)) {
/** @type {shaka.extern.aes128Key|undefined} */
let aes128Key = undefined;
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);
}
}
}
const initSegmentRef = this.createInitSegmentReference_(
absoluteInitSegmentUri, mapTag);
absoluteInitSegmentUri, mapTag, aes128Key);
this.mapTagToInitSegmentRefMap_.set(mapTagKey, initSegmentRef);
}
return this.mapTagToInitSegmentRefMap_.get(mapTagKey);
Expand All @@ -2848,10 +2858,11 @@ shaka.hls.HlsParser = class {
* playlist.
* @param {string} absoluteInitSegmentUri
* @param {!shaka.hls.Tag} mapTag EXT-X-MAP
* @param {shaka.extern.aes128Key=} aes128Key
* @return {!shaka.media.InitSegmentReference}
* @private
*/
createInitSegmentReference_(absoluteInitSegmentUri, mapTag) {
createInitSegmentReference_(absoluteInitSegmentUri, mapTag, aes128Key) {
let startByte = 0;
let endByte = null;
const byterange = mapTag.getAttributeValue('BYTERANGE');
Expand All @@ -2862,12 +2873,26 @@ shaka.hls.HlsParser = class {
const byteLength = Number(blocks[0]);
startByte = Number(blocks[1]);
endByte = startByte + byteLength - 1;

if (aes128Key) {
// MAP segment encrypted with method 'AES-128', when served with
// HTTP Range, has the unencrypted size specified in the range.
// See: https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-08#section-6.3.6
const length = (endByte + 1) - startByte;
if (length % 16) {
endByte += (16 - (length % 16));
}
}
}

const initSegmentRef = new shaka.media.InitSegmentReference(
() => [absoluteInitSegmentUri],
startByte,
endByte);
endByte,
/* mediaQuality= */ null,
/* timescale= */ null,
/* segmentData= */ null,
aes128Key);
return initSegmentRef;
}

Expand Down Expand Up @@ -3262,7 +3287,7 @@ shaka.hls.HlsParser = class {

mediaSequenceToStartTime.set(position, startTime);

initSegmentRef = this.getInitSegmentReference_(playlist.absoluteUri,
initSegmentRef = this.getInitSegmentReference_(playlist,
item.tags, variables);

// If the stream is low latency and the user has not configured the
Expand Down
2 changes: 2 additions & 0 deletions test/hls/hls_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3126,8 +3126,10 @@ describe('HlsParser', () => {

const firstMp4Segment = mp4AesEncryptionVideo.segmentIndex.get(0);
expect(firstMp4Segment.aes128Key).toBeDefined();
expect(firstMp4Segment.initSegmentReference.aes128Key).toBeDefined();
const secondMp4Segment = mp4AesEncryptionVideo.segmentIndex.get(1);
expect(secondMp4Segment.aes128Key).toBeNull();
expect(secondMp4Segment.initSegmentReference.aes128Key).toBeDefined();

const tsAesEncryptionVideo = actual.variants[2].video;
await tsAesEncryptionVideo.createSegmentIndex();
Expand Down
Loading