Skip to content

Commit

Permalink
Support for SAMPLE-AES key rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
varvaruc committed Oct 11, 2022
1 parent b2f279d commit 010d936
Showing 1 changed file with 73 additions and 49 deletions.
122 changes: 73 additions & 49 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ shaka.hls.HlsParser = class {

const mediaSequenceToStartTime =
this.getMediaSequenceToStartTimeFor_(streamInfo);
const {keyIds, drmInfos} = this.parseDrmInfo_(playlist, stream.mimeType);

const keysAreEqual =
(a, b) => a.size === b.size && [...a].every((value) => b.has(value));

if (!keysAreEqual(stream.keyIds, keyIds)) {
stream.keyIds = keyIds;
stream.drmInfos = drmInfos;
if (this.manifest_) {
this.playerInterface_.filter(this.manifest_);
}
}

const segments = this.createSegments_(
streamInfo.verbatimMediaPlaylistUri, playlist, stream.type,
stream.mimeType, mediaSequenceToStartTime, mediaVariables);
Expand Down Expand Up @@ -1770,55 +1783,8 @@ shaka.hls.HlsParser = class {
mediaVariables);
}

/** @type {!Array.<!shaka.hls.Tag>} */
const drmTags = [];
if (playlist.segments) {
for (const segment of playlist.segments) {
const segmentKeyTags = shaka.hls.Utils.filterTagsByName(segment.tags,
'EXT-X-KEY');
drmTags.push(...segmentKeyTags);
}
}

let encrypted = false;
let aesEncrypted = false;

/** @type {!Array.<shaka.extern.DrmInfo>}*/
const drmInfos = [];
const keyIds = new Set();

// TODO: May still need changes to support key rotation.
for (const drmTag of drmTags) {
const method = drmTag.getRequiredAttrValue('METHOD');
if (method != 'NONE') {
encrypted = true;

if (method == 'AES-128') {
// These keys are handled separately.
aesEncrypted = true;
} else {
// According to the HLS spec, KEYFORMAT is optional and implicitly
// defaults to "identity".
// https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis-11#section-4.4.4.4
const keyFormat =
drmTag.getAttributeValue('KEYFORMAT') || 'identity';
const drmParser =
shaka.hls.HlsParser.KEYFORMATS_TO_DRM_PARSERS_[keyFormat];

const drmInfo = drmParser ? drmParser(drmTag, mimeType) : null;
if (drmInfo) {
if (drmInfo.keyIds) {
for (const keyId of drmInfo.keyIds) {
keyIds.add(keyId);
}
}
drmInfos.push(drmInfo);
} else {
shaka.log.warning('Unsupported HLS KEYFORMAT', keyFormat);
}
}
}
}
const {drmInfos, keyIds, encrypted, aesEncrypted} =
this.parseDrmInfo_(playlist, mimeType);

if (encrypted && !drmInfos.length && !aesEncrypted) {
throw new shaka.util.Error(
Expand Down Expand Up @@ -1934,6 +1900,64 @@ shaka.hls.HlsParser = class {
};
}

/**
* @param {!shaka.hls.Playlist} playlist
* @param {string} mimeType
* @private
*/
parseDrmInfo_(playlist, mimeType) {
/** @type {!Array.<!shaka.hls.Tag>} */
const drmTags = [];
if (playlist.segments) {
for (const segment of playlist.segments) {
const segmentKeyTags = shaka.hls.Utils.filterTagsByName(segment.tags,
'EXT-X-KEY');
drmTags.push(...segmentKeyTags);
}
}

let encrypted = false;
let aesEncrypted = false;

/** @type {!Array.<shaka.extern.DrmInfo>}*/
const drmInfos = [];
const keyIds = new Set();

// TODO: May still need changes to support key rotation.
for (const drmTag of drmTags) {
const method = drmTag.getRequiredAttrValue('METHOD');
if (method != 'NONE') {
encrypted = true;

if (method == 'AES-128') {
// These keys are handled separately.
aesEncrypted = true;
} else {
// According to the HLS spec, KEYFORMAT is optional and implicitly
// defaults to "identity".
// https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis-11#section-4.4.4.4
const keyFormat =
drmTag.getAttributeValue('KEYFORMAT') || 'identity';
const drmParser =
shaka.hls.HlsParser.KEYFORMATS_TO_DRM_PARSERS_[keyFormat];

const drmInfo = drmParser ? drmParser(drmTag, mimeType) : null;
if (drmInfo) {
if (drmInfo.keyIds) {
for (const keyId of drmInfo.keyIds) {
keyIds.add(keyId);
}
}
drmInfos.push(drmInfo);
} else {
shaka.log.warning('Unsupported HLS KEYFORMAT', keyFormat);
}
}
}
}

return {drmInfos, keyIds, encrypted, aesEncrypted};
}

/**
* @param {!shaka.hls.Tag} drmTag
Expand Down

0 comments on commit 010d936

Please sign in to comment.