Skip to content

Commit

Permalink
fix: Dedupe DRM init data (shaka-project#3695)
Browse files Browse the repository at this point in the history
Linear streams accumulate init data over time because the init data is pushed to
an array regardless of whether or not it already exists as part of the logic to
combine periods. This PR dedupes the init data based on keyId to help reduce
memory usage over extended playouts.
  • Loading branch information
nick-michael committed Oct 15, 2021
1 parent 077ea2a commit a09d8c9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/media/drm_engine.js
Expand Up @@ -1825,9 +1825,16 @@ shaka.media.DrmEngine = class {
// audio adaptations, so we shouldn't have to worry about checking
// robustness.
if (drm1.keySystem == drm2.keySystem) {
/** @type {Array<shaka.extern.InitDataOverride>} */
let initData = [];
initData = initData.concat(drm1.initData || []);
initData = initData.concat(drm2.initData || []);
initData = initData.filter((d, i) => {
return d.keyId === undefined || i === initData.findIndex((d2) => {
return d2.keyId === d.keyId;
});
});

const keyIds = drm1.keyIds && drm2.keyIds ?
new Set([...drm1.keyIds, ...drm2.keyIds]) :
drm1.keyIds || drm2.keyIds;
Expand Down
44 changes: 44 additions & 0 deletions test/media/drm_engine_unit.js
Expand Up @@ -2152,6 +2152,50 @@ describe('DrmEngine', () => {
[drmInfoAudio]);
expect(returned).toEqual([drmInfoDesired]);
});

it('dedupes the merged init data based on keyId matching', () => {
const serverCert = new Uint8Array(0);
const drmInfoVideo = {
keySystem: 'drm.abc',
licenseServerUri: 'http://abc.drm/license',
distinctiveIdentifierRequired: false,
persistentStateRequired: true,
videoRobustness: 'really_really_ridiculously_good',
serverCertificate: serverCert,
serverCertificateUri: '',
initData: [{keyId: 'v-init'}],
keyIds: new Set(['deadbeefdeadbeefdeadbeefdeadbeef']),
};
const drmInfoAudio = {
keySystem: 'drm.abc',
licenseServerUri: undefined,
distinctiveIdentifierRequired: true,
persistentStateRequired: false,
audioRobustness: 'good',
serverCertificate: undefined,
serverCertificateUri: '',
initData: [{keyId: 'v-init'}, {keyId: 'a-init'}],
keyIds: new Set(['eadbeefdeadbeefdeadbeefdeadbeefd']),
};
const drmInfoDesired = {
keySystem: 'drm.abc',
licenseServerUri: 'http://abc.drm/license',
distinctiveIdentifierRequired: true,
persistentStateRequired: true,
audioRobustness: 'good',
videoRobustness: 'really_really_ridiculously_good',
serverCertificate: serverCert,
serverCertificateUri: '',
initData: [{keyId: 'v-init'}, {keyId: 'a-init'}],
keyIds: new Set([
'deadbeefdeadbeefdeadbeefdeadbeef',
'eadbeefdeadbeefdeadbeefdeadbeefd',
]),
};
const returned = shaka.media.DrmEngine.getCommonDrmInfos([drmInfoVideo],
[drmInfoAudio]);
expect(returned).toEqual([drmInfoDesired]);
});
}); // describe('getCommonDrmInfos')

describe('configure', () => {
Expand Down

0 comments on commit a09d8c9

Please sign in to comment.