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: add feature-flag to insert fake encryption in init segments on broken platforms #5561

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions demo/common/message_ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ shakaDemo.MessageIds = {
FAST_HALF_LIFE: 'DEMO_FAST_HALF_LIFE',
FORCE_HTTPS: 'DEMO_FORCE_HTTPS',
FORCE_TRANSMUX: 'DEMO_FORCE_TRANSMUX',
INSERT_FAKE_ENCRYPTION_IN_INIT: 'DEMO_INSERT_FAKE_ENCRYPTION_IN_INIT',
FUZZ_FACTOR: 'DEMO_FUZZ_FACTOR',
GAP_DETECTION_THRESHOLD: 'DEMO_GAP_DETECTION_THRESHOLD',
GAP_JUMP_TIMER_TIME: 'DEMO_GAP_JUMP_TIMER_TIME',
Expand Down
4 changes: 3 additions & 1 deletion demo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,9 @@ shakaDemo.Config = class {
.addTextInput_(MessageIds.SOURCE_BUFFER_EXTRA_FEATURES,
'mediaSource.sourceBufferExtraFeatures')
.addBoolInput_(MessageIds.FORCE_TRANSMUX,
'mediaSource.forceTransmux');
'mediaSource.forceTransmux')
.addBoolInput_(MessageIds.INSERT_FAKE_ENCRYPTION_IN_INIT,
'mediaSource.insertFakeEncryptionInInit');
}

/** @private */
Expand Down
1 change: 1 addition & 0 deletions demo/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"DEMO_FAST_HALF_LIFE": "Fast half life",
"DEMO_FORCE_HTTPS": "Force HTTPS",
"DEMO_FORCE_TRANSMUX": "Force Transmux",
"DEMO_INSERT_FAKE_ENCRYPTION_IN_INIT": "Insert fake encryption in init segments when needed by the platform.",
"DEMO_FRONT_INTRO_DISMISS": "Dismiss",
"DEMO_FRONT_INTRO_ONE": "This is a demo of Google's Shaka Player, a JavaScript library for adaptive video streaming.",
"DEMO_FRONT_INTRO_TWO": "Choose a video to playback; more assets are available via the \"all content\" tab.",
Expand Down
4 changes: 4 additions & 0 deletions demo/locales/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@
"description": "The name of a configuration value.",
"message": "Force Transmux"
},
"DEMO_INSERT_FAKE_ENCRYPTION_IN_INIT": {
"description": "The name of a configuration value.",
"message": "Insert fake encryption in init segments when needed by the platform."
},
"DEMO_FRONT_INTRO_DISMISS": {
"description": "A button allowing users to dismiss the intro message.",
"message": "Dismiss"
Expand Down
14 changes: 13 additions & 1 deletion externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,8 @@ shaka.extern.StreamingConfiguration;
/**
* @typedef {{
* sourceBufferExtraFeatures: string,
* forceTransmux: boolean
* forceTransmux: boolean,
* insertFakeEncryptionInInit: boolean
* }}
*
* @description
Expand All @@ -1240,6 +1241,17 @@ shaka.extern.StreamingConfiguration;
* If this is <code>true</code>, we will transmux AAC and TS content even if
* not strictly necessary for the assets to be played.
* This value defaults to <code>false</code>.
* @property {boolean} insertFakeEncryptionInInit
* If true, will apply a work-around for non-encrypted init segments on
* encrypted content for some platforms.
* <br><br>
* See https://github.com/shaka-project/shaka-player/issues/2759.
* <br><br>
* If you know you don't need this, you canset this value to
* <code>false</code> to gain a few milliseconds on loading time and seek
* time.
* <br><br>
* This value defaults to <code>true</code>.
* @exportDoc
*/
shaka.extern.MediaSourceConfiguration;
Expand Down
12 changes: 7 additions & 5 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1483,15 +1483,17 @@ shaka.media.MediaSourceEngine = class {
const encryptionExpected = this.expectedEncryption_[contentType];

// If:
// 1. this is an init segment,
// 2. and encryption is expected,
// 3. and the platform requires encryption in all init segments,
// 4. and the content is MP4 (mimeType == "video/mp4" or "audio/mp4"),
// 1. the configuration tells to insert fake encryption,
// 2. and this is an init segment,
// 3. and encryption is expected,
// 4. and the platform requires encryption in all init segments,
// 5. and the content is MP4 (mimeType == "video/mp4" or "audio/mp4"),
// then insert fake encryption metadata for init segments that lack it.
// The MP4 requirement is because we can currently only do this
// transformation on MP4 containers.
// See: https://github.com/shaka-project/shaka-player/issues/2759
if (isInitSegment &&
if (this.config_.insertFakeEncryptionInInit &&
isInitSegment &&
encryptionExpected &&
shaka.util.Platform.requiresEncryptionInfoInAllInitSegments() &&
shaka.util.MimeUtils.getContainerType(
Expand Down
1 change: 1 addition & 0 deletions lib/util/player_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ shaka.util.PlayerConfiguration = class {
const mediaSource = {
sourceBufferExtraFeatures: '',
forceTransmux: false,
insertFakeEncryptionInInit: true,
};

const ads = {
Expand Down
56 changes: 51 additions & 5 deletions test/media/media_source_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ describe('MediaSourceEngine', () => {
const buffer2 = /** @type {!ArrayBuffer} */ (/** @type {?} */ (2));
const buffer3 = /** @type {!ArrayBuffer} */ (/** @type {?} */ (3));

const fakeVideoStream = {mimeType: 'video/foo', drmInfos: []};
const fakeAudioStream = {mimeType: 'audio/foo', drmInfos: []};
const fakeTextStream = {mimeType: 'text/foo', drmInfos: []};
const fakeVideoStream = {mimeType: 'video/mp4', drmInfos: [{}]};
const fakeAudioStream = {mimeType: 'audio/mp4', drmInfos: []};
const fakeTextStream = {mimeType: 'text/mp4', drmInfos: []};
const fakeTransportStream = {mimeType: 'tsMimetype', drmInfos: []};

/** @type {shaka.extern.Stream} */
Expand All @@ -82,6 +82,10 @@ describe('MediaSourceEngine', () => {

/** @type {!jasmine.Spy} */
let createMediaSourceSpy;
/** @type {!jasmine.Spy} */
let requiresEncryptionInfoInAllInitSegmentsSpy;
/** @type {!jasmine.Spy} */
let fakeEncryptionSpy;

/** @type {!shaka.media.MediaSourceEngine} */
let mediaSourceEngine;
Expand Down Expand Up @@ -139,6 +143,12 @@ describe('MediaSourceEngine', () => {
shaka.media.MediaSourceEngine.prototype.createMediaSource =
Util.spyFunc(createMediaSourceSpy);

requiresEncryptionInfoInAllInitSegmentsSpy = spyOn(shaka.util.Platform,
'requiresEncryptionInfoInAllInitSegments').and.returnValue(false);

fakeEncryptionSpy = spyOn(shaka.media.ContentWorkarounds, 'fakeEncryption')
.and.callFake((data) => data + 100);

// MediaSourceEngine uses video to:
// - set src attribute
// - read error codes when operations fail
Expand Down Expand Up @@ -269,8 +279,8 @@ describe('MediaSourceEngine', () => {
initObject.set(ContentType.AUDIO, fakeAudioStream);
initObject.set(ContentType.VIDEO, fakeVideoStream);
await mediaSourceEngine.init(initObject, false);
expect(mockMediaSource.addSourceBuffer).toHaveBeenCalledWith('audio/foo');
expect(mockMediaSource.addSourceBuffer).toHaveBeenCalledWith('video/foo');
expect(mockMediaSource.addSourceBuffer).toHaveBeenCalledWith('audio/mp4');
expect(mockMediaSource.addSourceBuffer).toHaveBeenCalledWith('video/mp4');
expect(shaka.text.TextEngine).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -396,6 +406,42 @@ describe('MediaSourceEngine', () => {
await mediaSourceEngine.init(initObject, false);
});

it('should apply fake encryption by default', async () => {
requiresEncryptionInfoInAllInitSegmentsSpy.and.returnValue(true);

const p = mediaSourceEngine.appendBuffer(
ContentType.VIDEO, buffer, null, fakeStream,
/* hasClosedCaptions= */ false);

expect(fakeEncryptionSpy).toHaveBeenCalled();

expect(videoSourceBuffer.appendBuffer)
.toHaveBeenCalledWith((buffer + 100));
videoSourceBuffer.updateend();

await p;
});

it('should not apply fake encryption when config is off', async () => {
requiresEncryptionInfoInAllInitSegmentsSpy.and.returnValue(true);

const config = shaka.util.PlayerConfiguration.createDefault().mediaSource;
config.insertFakeEncryptionInInit = false;

mediaSourceEngine.configure(config);

const p = mediaSourceEngine.appendBuffer(
ContentType.VIDEO, buffer, null, fakeStream,
/* hasClosedCaptions= */ false);

expect(fakeEncryptionSpy).not.toHaveBeenCalled();

expect(videoSourceBuffer.appendBuffer).toHaveBeenCalledWith(buffer);
videoSourceBuffer.updateend();

await p;
});

it('appends the given data', async () => {
const p = mediaSourceEngine.appendBuffer(
ContentType.AUDIO, buffer, null, fakeStream,
Expand Down