Skip to content

Commit

Permalink
feat(dash): [ClearKey] Changes as per comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
sr1990 committed Apr 13, 2022
1 parent c4bea5f commit 41025d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 56 deletions.
17 changes: 10 additions & 7 deletions lib/dash/content_protection.js
Expand Up @@ -356,7 +356,10 @@ 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,
Expand All @@ -367,7 +370,8 @@ shaka.dash.ContentProtection = class {
}

/**
* Creates ClearKey initData from Default_KID value.
* 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>}
Expand All @@ -378,16 +382,15 @@ shaka.dash.ContentProtection = class {
return null;
}

const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
const [first] = keyIds;
const data = Uint8ArrayUtils.fromHex(first);

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);

const pssh = shaka.util.Pssh.createPsshClearKey(data, systemId);
return [
{
initData: pssh,
Expand Down
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
72 changes: 24 additions & 48 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,
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,58 +117,28 @@ 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;
psshData.setUint32(byteCursor, dataLength);
byteCursor += 0x4;
psshBox.set(data, byteCursor);
byteCursor += dataLength;

goog.asserts.assert(byteCursor === psshSize, 'PSSH invalid length.');
return psshBox;
}

/**
* Creates a ClearKey cenc pssh blob from the given system ID and data.
* Data will contain one key id.
* Based on https://www.w3.org/TR/eme-initdata-cenc/
* @param {!Uint8Array} data
* @param {!Uint8Array} systemId
* @return {!Uint8Array}
*/
static createPsshClearKey(data, systemId) {
goog.asserts.assert(systemId.byteLength == 16, 'Invalid system ID length');
const dataLength = data.length;
const psshSize = 0x4 + // pssh size (4 bytes)
0x4 + // 'pssh' (4 bytes)
0x4 + // version (1 byte) + flags (3 bytes)
systemId.length + // system id (16 bytes)
0x4 + // KID count (4 bytes)
dataLength + // KID value (16 bytes)
0x4; // size of data (4 bytes)

/** @type {!Uint8Array} */
const psshBox = new Uint8Array(psshSize);
/** @type {!DataView} */
const psshData = shaka.util.BufferUtils.toDataView(psshBox);
// 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;
}
}

let byteCursor = 0;
psshData.setUint32(byteCursor, psshSize);
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0x70737368); // 'pssh'
byteCursor += 0x4;
psshData.setUint32(byteCursor, 0x01000000); // version = 1
byteCursor += 0x4;
psshBox.set(systemId, byteCursor);
byteCursor += systemId.length;
psshData.setUint32(byteCursor, 0x00000001); // KID_count
psshData.setUint32(byteCursor, dataLength);
byteCursor += 0x4;
psshBox.set(data, byteCursor); // add kid
psshBox.set(data, byteCursor);
byteCursor += dataLength;
psshData.setUint32(byteCursor, 0x00000000);
byteCursor += 0x4;

goog.asserts.assert(byteCursor === psshSize, 'PSSH invalid length.');
return psshBox;
Expand Down

0 comments on commit 41025d8

Please sign in to comment.