Skip to content

Commit

Permalink
fix: Fix HLS lazy-loading with DRM (shaka-project#4646)
Browse files Browse the repository at this point in the history
Now that DRM info is only parsed after playback has begun, the manifest
parser needs to communicate back to the other components that new DRM
info is available after lazy-loading a playlist.

Closes shaka-project#4622
  • Loading branch information
joeyparrish authored Nov 4, 2022
1 parent 941ed4e commit a7f0be7
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 15 deletions.
6 changes: 5 additions & 1 deletion externs/shaka/manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ shaka.extern.ManifestParser = class {
* isLowLatencyMode: function():boolean,
* isAutoLowLatencyMode: function():boolean,
* enableLowLatencyMode: function(),
* updateDuration: function()
* updateDuration: function(),
* newDrmInfo: function(shaka.extern.Stream)
* }}
*
* @description
Expand Down Expand Up @@ -150,6 +151,9 @@ shaka.extern.ManifestParser = class {
* Enable low latency streaming mode.
* @property {function()} updateDuration
* Update the presentation duration based on PresentationTimeline.
* @property {function(shaka.extern.Stream)} newDrmInfo
* Inform the player of new DRM info that needs to be processed for the given
* stream.
* @exportDoc
*/
shaka.extern.ManifestParser.PlayerInterface;
Expand Down
6 changes: 6 additions & 0 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,12 @@ shaka.hls.HlsParser = class {
stream.roles = realStream.roles;
stream.mimeType = realStream.mimeType;

// Since we lazy-loaded this content, the player may need to create new
// sessions for the DRM info in this stream.
if (stream.drmInfos.length) {
this.playerInterface_.newDrmInfo(stream);
}

const ContentType = shaka.util.ManifestParserUtils.ContentType;
if (type == ContentType.VIDEO || type == ContentType.AUDIO) {
for (const otherStreamInfo of this.uriToStreamInfosMap_.values()) {
Expand Down
1 change: 1 addition & 0 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ shaka.offline.Storage = class {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};

parser.configure(config.manifest);
Expand Down
49 changes: 35 additions & 14 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,15 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.streamingEngine_.updateDuration();
}
},
newDrmInfo: (stream) => {
// We may need to create new sessions for any new init data.
const currentDrmInfo =
this.drmEngine_ ? this.drmEngine_.getDrmInfo() : null;
// DrmEngine.newInitData() requires mediaKeys to be available.
if (currentDrmInfo && this.drmEngine_.getMediaKeys()) {
this.processDrmInfos_(currentDrmInfo.keySystem, stream);
}
},
};

const startTime = Date.now() / 1000;
Expand Down Expand Up @@ -5410,27 +5419,39 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}

// We may need to create new sessions for any new init data.
const curDrmInfo = this.drmEngine_ ? this.drmEngine_.getDrmInfo() : null;
const currentDrmInfo =
this.drmEngine_ ? this.drmEngine_.getDrmInfo() : null;
// DrmEngine.newInitData() requires mediaKeys to be available.
if (curDrmInfo && this.drmEngine_.getMediaKeys()) {
if (currentDrmInfo && this.drmEngine_.getMediaKeys()) {
for (const variant of manifest.variants) {
const videoDrmInfos = variant.video ? variant.video.drmInfos : [];
const audioDrmInfos = variant.audio ? variant.audio.drmInfos : [];
const drmInfos = videoDrmInfos.concat(audioDrmInfos);
for (const drmInfo of drmInfos) {
// Ignore any data for different key systems.
if (drmInfo.keySystem == curDrmInfo.keySystem) {
for (const initData of (drmInfo.initData || [])) {
this.drmEngine_.newInitData(
initData.initDataType, initData.initData);
}
}
}
this.processDrmInfos_(currentDrmInfo.keySystem, variant.video);
this.processDrmInfos_(currentDrmInfo.keySystem, variant.audio);
}
}
this.checkRestrictedVariants_(manifest);
}

/**
* @param {string} keySystem
* @param {?shaka.extern.Stream} stream
* @private
*/
processDrmInfos_(keySystem, stream) {
if (!stream) {
return;
}

for (const drmInfo of stream.drmInfos) {
// Ignore any data for different key systems.
if (drmInfo.keySystem == keySystem) {
for (const initData of (drmInfo.initData || [])) {
this.drmEngine_.newInitData(
initData.initDataType, initData.initData);
}
}
}
}

/**
* @private
*/
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_content_protection_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('DashParser ContentProtection', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};

const actual = await dashParser.start(
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('DashParser Live', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
});

Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_manifest_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('DashParser Manifest', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
});

Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_segment_base_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('DashParser SegmentBase', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
});

Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_segment_list_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ describe('DashParser SegmentList', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
const manifest = await dashParser.start('dummy://foo', playerInterface);
const stream = manifest.variants[0].video;
Expand Down
1 change: 1 addition & 0 deletions test/dash/dash_parser_segment_template_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('DashParser SegmentTemplate', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
});

Expand Down
1 change: 1 addition & 0 deletions test/hls/hls_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('HlsParser live', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};

parser = new shaka.hls.HlsParser();
Expand Down
9 changes: 9 additions & 0 deletions test/hls/hls_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('HlsParser', () => {
let parser;
/** @type {!jasmine.Spy} */
let onEventSpy;
/** @type {!jasmine.Spy} */
let newDrmInfoSpy;
/** @type {shaka.extern.ManifestParser.PlayerInterface} */
let playerInterface;
/** @type {shaka.extern.ManifestConfiguration} */
Expand Down Expand Up @@ -86,6 +88,7 @@ describe('HlsParser', () => {

config = shaka.util.PlayerConfiguration.createDefault().manifest;
onEventSpy = jasmine.createSpy('onEvent');
newDrmInfoSpy = jasmine.createSpy('newDrmInfo');
playerInterface = {
modifyManifestRequest: (request, manifestInfo) => {},
modifySegmentRequest: (request, segmentInfo) => {},
Expand All @@ -99,6 +102,7 @@ describe('HlsParser', () => {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: shaka.test.Util.spyFunc(newDrmInfoSpy),
};

parser = new shaka.hls.HlsParser();
Expand Down Expand Up @@ -2763,6 +2767,7 @@ describe('HlsParser', () => {
});

await testHlsParser(master, media, manifest);
expect(newDrmInfoSpy).toHaveBeenCalled();
});

it('constructs DrmInfo for PlayReady', async () => {
Expand Down Expand Up @@ -2803,6 +2808,7 @@ describe('HlsParser', () => {
});

await testHlsParser(master, media, manifest);
expect(newDrmInfoSpy).toHaveBeenCalled();
});

it('constructs DrmInfo for FairPlay', async () => {
Expand Down Expand Up @@ -2840,6 +2846,7 @@ describe('HlsParser', () => {
});

await testHlsParser(master, media, manifest);
expect(newDrmInfoSpy).toHaveBeenCalled();
});

it('constructs DrmInfo for ClearKey with explicit KEYFORMAT', async () => {
Expand Down Expand Up @@ -2874,6 +2881,7 @@ describe('HlsParser', () => {
});

await testHlsParser(master, media, manifest);
expect(newDrmInfoSpy).toHaveBeenCalled();
});

it('constructs DrmInfo for ClearKey without explicit KEYFORMAT', async () => {
Expand Down Expand Up @@ -2907,6 +2915,7 @@ describe('HlsParser', () => {
});

await testHlsParser(master, media, manifest);
expect(newDrmInfoSpy).toHaveBeenCalled();
});

it('falls back to mp4 if HEAD request fails', async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/test/util/dash_parser_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ shaka.test.Dash = class {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
const manifest = await dashParser.start('dummy://foo', playerInterface);
const stream = manifest.variants[0].video;
Expand Down Expand Up @@ -79,6 +80,7 @@ shaka.test.Dash = class {
isAutoLowLatencyMode: () => false,
enableLowLatencyMode: () => {},
updateDuration: () => {},
newDrmInfo: (stream) => {},
};
const p = dashParser.start('dummy://foo', playerInterface);
await expectAsync(p).toBeRejectedWith(
Expand Down

0 comments on commit a7f0be7

Please sign in to comment.