Skip to content

Commit

Permalink
fix(MSS): Add parsing of PIFF Sample Encryption Box
Browse files Browse the repository at this point in the history
  • Loading branch information
unext-wendong committed Jul 24, 2023
1 parent c378e10 commit d2a772b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions externs/isoboxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var ISOBoxerUtils;

/**
* @typedef {{
* _parsing: boolean,
* type: string,
* size: number,
* _parent: ISOBox,
Expand Down Expand Up @@ -123,6 +124,7 @@ var ISOBoxerUtils;
* sample_info_size: Array.<number>,
* data_offset: number
* }}
* @property {boolean} _parsing
* @property {string} type
* @property {number} size
* @property {ISOBox} _parent
Expand Down
14 changes: 12 additions & 2 deletions lib/mss/content_protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,22 @@ shaka.mss.ContentProtection = class {
* @private
*/
static getKID_(xml) {
const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
// KID element is optional and no more than one is
// allowed inside the DATA element.
for (const elem of xml.getElementsByTagName('DATA')) {
const kid = shaka.util.XmlUtils.findChild(elem, 'KID');
if (kid) {
return kid.textContent;
// GUID: [DWORD, WORD, WORD, 8-BYTE]
const guidBytes = Uint8ArrayUtils.fromBase64(kid.textContent);
// Reverse byte order from little-endian to big-endian
const kidBytes = new Uint8Array([
guidBytes[3], guidBytes[2], guidBytes[1], guidBytes[0],
guidBytes[5], guidBytes[4],
guidBytes[7], guidBytes[6],
...guidBytes.slice(8),
]);
return Uint8ArrayUtils.toHex(kidBytes);
}
}

Expand Down Expand Up @@ -274,7 +284,7 @@ shaka.mss.ContentProtection = class {

for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const systemID = element.getAttribute('SystemID');
const systemID = element.getAttribute('SystemID').toLowerCase();
const keySystem = keySystemsBySystemId[systemID];
if (keySystem) {
const KID = ContentProtection.getPlayReadyKID_(element);
Expand Down
36 changes: 34 additions & 2 deletions lib/transmuxer/mss_transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ shaka.transmuxer.MssTransmuxer = class {
}
});
// eslint-disable-next-line no-restricted-syntax
this.isoBoxer_.addBoxProcessor('senc', function() {
const sencProcessor = function() {
// eslint-disable-next-line no-invalid-this
const box = /** @type {!ISOBox} */(this);
box._procFullBox();
box._procField('sample_count', 'uint', 32);
if (box.flags & 1) {
box._procField('AlgorithmID', 'uint', 24);
box._procField('IV_size', 'uint', 8);
box._procFieldArray('KID', 16, 'uint', 8);
}
box._procField('sample_count', 'uint', 32);
// eslint-disable-next-line no-restricted-syntax
box._procEntries('entry', box.sample_count, function(entry) {
// eslint-disable-next-line no-invalid-this
Expand All @@ -125,6 +127,28 @@ shaka.transmuxer.MssTransmuxer = class {
});
}
});
};
this.isoBoxer_.addBoxProcessor('senc', sencProcessor);
// eslint-disable-next-line no-restricted-syntax
this.isoBoxer_.addBoxProcessor('uuid', function() {
const MssTransmuxer = shaka.transmuxer.MssTransmuxer;
// eslint-disable-next-line no-invalid-this
const box = /** @type {!ISOBox} */(this);
let isSENC = true;
for (let i = 0; i < 16; i++) {
if (box.usertype[i] !== MssTransmuxer.UUID_SENC_[i]) {
isSENC = false;
}
// Add support for other user types here
}

if (isSENC) {
if (box._parsing) {
box.type = 'sepiff'; // Rename it to be recognized later
}
// eslint-disable-next-line no-restricted-syntax, no-invalid-this
sencProcessor.call(/** @type {!ISOBox} */(this));
}
});
}

Expand Down Expand Up @@ -345,6 +369,14 @@ shaka.transmuxer.MssTransmuxer = class {
}
};

/**
* @private {!Uint8Array}
*/
shaka.transmuxer.MssTransmuxer.UUID_SENC_ = new Uint8Array([
0xA2, 0x39, 0x4F, 0x52, 0x5A, 0x9B, 0x4F, 0x14,
0xA2, 0x44, 0x6C, 0x42, 0x7C, 0x64, 0x8D, 0xF4,
]);

shaka.transmuxer.TransmuxerEngine.registerTransmuxer(
'mss/audio/mp4',
() => new shaka.transmuxer.MssTransmuxer('mss/audio/mp4'),
Expand Down

0 comments on commit d2a772b

Please sign in to comment.