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

feat(dash): Construct ClearKey PSSH based on MPD ContentProtection #4104

Merged
merged 3 commits into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 54 additions & 4 deletions lib/dash/content_protection.js
Expand Up @@ -68,7 +68,7 @@ shaka.dash.ContentProtection = class {

if (parsedNonCenc.length) {
drmInfos = ContentProtection.convertElements_(
defaultInit, parsedNonCenc, keySystemsByURI);
defaultInit, parsedNonCenc, keySystemsByURI, keyIds);

// If there are no drmInfos after parsing, then add a dummy entry.
// This may be removed in parseKeyIds.
Expand Down Expand Up @@ -356,7 +356,41 @@ shaka.dash.ContentProtection = class {
0x9a, 0x04, 0xf0, 0x79, 0x98, 0x40, 0x42, 0x86,
0xab, 0x92, 0xe6, 0x5b, 0xe0, 0x88, 0x5f, 0x95,
]);
const pssh = shaka.util.Pssh.createPssh(data, systemId);
const keyIds = new Set();
const psshVersion = 0;
const pssh =
shaka.util.Pssh.createPssh(data, systemId, keyIds, psshVersion);
return [
{
initData: pssh,
initDataType: 'cenc',
keyId: element.keyId,
},
];
}

/**
* Creates ClearKey initData from Default_KID value retrieved from previously
* parsed ContentProtection tag.
* @param {shaka.dash.ContentProtection.Element} element
* @param {!Set.<string>} keyIds
* @return {?Array.<shaka.extern.InitDataOverride>}
* @private
*/
static getInitDataClearKey_(element, keyIds) {
if (keyIds.size == 0) {
return null;
}

const systemId = new Uint8Array([
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02,
0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b,
]);
const data = new Uint8Array([]);
const psshVersion = 1;
const pssh =
shaka.util.Pssh.createPssh(data, systemId, keyIds, psshVersion);

return [
{
initData: pssh,
Expand All @@ -372,10 +406,11 @@ shaka.dash.ContentProtection = class {
* @param {Array.<shaka.extern.InitDataOverride>} defaultInit
* @param {!Array.<shaka.dash.ContentProtection.Element>} elements
* @param {!Object.<string, string>} keySystemsByURI
* @param {!Set.<string>} keyIds
* @return {!Array.<shaka.extern.DrmInfo>}
* @private
*/
static convertElements_(defaultInit, elements, keySystemsByURI) {
static convertElements_(defaultInit, elements, keySystemsByURI, keyIds) {
const ContentProtection = shaka.dash.ContentProtection;
const ManifestParserUtils = shaka.util.ManifestParserUtils;
const licenseUrlParsers = ContentProtection.licenseUrlParsers_;
Expand All @@ -391,7 +426,14 @@ shaka.dash.ContentProtection = class {
'Init data must be null or non-empty.');

const proInitData = ContentProtection.getInitDataFromPro_(element);
const initData = element.init || defaultInit || proInitData;
let clearKeyInitData = null;
if (element.schemeUri ===
shaka.dash.ContentProtection.ClearKeySchemeUri_) {
clearKeyInitData =
ContentProtection.getInitDataClearKey_(element, keyIds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be indented 4, not 2, if I'm not mistaken.

}
const initData = element.init || defaultInit || proInitData ||
clearKeyInitData;
const info = ManifestParserUtils.createDrmInfo(keySystem, initData);
const licenseParser = licenseUrlParsers.get(keySystem);
if (licenseParser) {
Expand Down Expand Up @@ -607,3 +649,11 @@ shaka.dash.ContentProtection.CencNamespaceUri_ = 'urn:mpeg:cenc:2013';
*/
shaka.dash.ContentProtection.ClearKeyNamespaceUri_ =
'http://dashif.org/guidelines/clearKey';


/**
* @const {string}
* @private
*/
shaka.dash.ContentProtection.ClearKeySchemeUri_ =
'urn:uuid:e2719d58-a985-b3c9-781a-b030af78d30e';
5 changes: 4 additions & 1 deletion lib/hls/hls_parser.js
Expand Up @@ -2529,7 +2529,10 @@ shaka.hls.HlsParser = class {
0x9a, 0x04, 0xf0, 0x79, 0x98, 0x40, 0x42, 0x86,
0xab, 0x92, 0xe6, 0x5b, 0xe0, 0x88, 0x5f, 0x95,
]);
const pssh = shaka.util.Pssh.createPssh(data, systemId);
const keyIds = new Set();
const psshVersion = 0;
const pssh =
shaka.util.Pssh.createPssh(data, systemId, keyIds, psshVersion);
const drmInfo = shaka.util.ManifestParserUtils.createDrmInfo(
'com.microsoft.playready', [
{initDataType: 'cenc', initData: pssh},
Expand Down
29 changes: 24 additions & 5 deletions lib/util/pssh.js
Expand Up @@ -90,16 +90,22 @@ shaka.util.Pssh = class {
}

/**
* Creates a pssh blob from the given system ID and data.
* Creates a pssh blob from the given system ID, data, keyIds and version.
*
* @param {!Uint8Array} data
* @param {!Uint8Array} systemId
* @param {!Set.<string>} keyIds
* @param {number} version
* @return {!Uint8Array}
*/
static createPssh(data, systemId) {
static createPssh(data,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the parameters should go into one line. There is no need to have two lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Will make the change.
Thought there was a line length limit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line limit is 80 characters. :-)

systemId, keyIds, version) {
goog.asserts.assert(systemId.byteLength == 16, 'Invalid system ID length');
const dataLength = data.length;
const psshSize = 0x4 + 0x4 + 0x4 + systemId.length + 0x4 + dataLength;
let psshSize = 0x4 + 0x4 + 0x4 + systemId.length + 0x4 + dataLength;
if (version > 0) {
psshSize += 0x4 + (16 * keyIds.size);
}

/** @type {!Uint8Array} */
const psshBox = new Uint8Array(psshSize);
Expand All @@ -111,10 +117,24 @@ shaka.util.Pssh = class {
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0x70737368); // 'pssh'
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0); // flags
(version < 1) ? psshData.setUint32(byteCursor, 0) :
psshData.setUint32(byteCursor, 0x01000000); // version + flags
byteCursor += 0x4;
psshBox.set(systemId, byteCursor);
byteCursor += systemId.length;

// if version > 0, add KID count and kid values.
if (version > 0) {
psshData.setUint32(byteCursor, keyIds.size); // KID_count
byteCursor += 0x4;
const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
for (const keyId of keyIds) {
const KID = Uint8ArrayUtils.fromHex(keyId);
psshBox.set(KID, byteCursor);
byteCursor += KID.length;
}
}

psshData.setUint32(byteCursor, dataLength);
byteCursor += 0x4;
psshBox.set(data, byteCursor);
Expand All @@ -124,7 +144,6 @@ shaka.util.Pssh = class {
return psshBox;
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the line.

/**
* Normalise the initData array. This is to apply browser specific
* work-arounds, e.g. removing duplicates which appears to occur
Expand Down