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

live stream can switch from clear content to encrypted content #1094 #1217

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Rostislav Hejduk <[email protected]>
Sam Dutton <[email protected]>
Sanborn Hilland <[email protected]>
Sandra Lokshina <[email protected]>
Semih Gokceoglu <[email protected]>
Seth Madison <[email protected]>
Theodore Abshire <[email protected]>
Thomas Stephens <[email protected]>
Expand Down
52 changes: 52 additions & 0 deletions lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ shaka.media.DrmEngine.prototype.getDrmInfo = function() {
shaka.media.DrmEngine.prototype.prepareMediaKeyConfigs_ =
function(manifest, offline, configsByKeySystem, keySystemsInOrder) {
var clearKeyDrmInfo = this.configureClearKey_();
var configDrmInfos = this.getDrmInfosByConfig_(manifest);

manifest.periods.forEach(function(period) {
period.variants.forEach(function(variant) {
Expand All @@ -474,6 +475,14 @@ shaka.media.DrmEngine.prototype.prepareMediaKeyConfigs_ =
variant.drmInfos = [clearKeyDrmInfo];
}

// If initial manifest contains unencrypted content,
// drm configuration overrides DrmInfo so drmEngine can be activated.
// Thus, player can play encrypted content if live stream switches from
// unencrypted content to encrypted content during live stream.
if (configDrmInfos) {
variant.drmInfos = configDrmInfos;
}

variant.drmInfos.forEach(function(drmInfo) {
this.fillInDrmInfoDefaults_(drmInfo);

Expand Down Expand Up @@ -774,6 +783,49 @@ shaka.media.DrmEngine.prototype.configureClearKey_ = function() {
};


/**
* Returns the DrmInfo that is generated by drm configation.
* It activates DrmEngine if drm configs have keySystems.
* @param {!shakaExtern.Manifest} manifest
* @return {Array.<{shakaExtern.DrmInfo}>}
* @private
*/
shaka.media.DrmEngine.prototype.getDrmInfosByConfig_ = function(manifest) {
var config = this.config_;
var serverKeys = Object.keys(config.servers);

if (!serverKeys.length) {
return null;
}

var isEncryptedContent = manifest.periods.some(function(period) {
return period.variants.some(function(variant) {
return variant.drmInfos.length;
});
});

// We only want to create phony DrmInfos
// when none are provided by the manifest.
if (isEncryptedContent) {
return null;
}

return serverKeys.map(function(keySystem) {
return {
keySystem: keySystem,
licenseServerUri: config.servers[keySystem],
distinctiveIdentifierRequired: false,
persistentStateRequired: false,
audioRobustness: '',
videoRobustness: '',
serverCertificate: null,
initData: [],
keyIds: []
};
});
};


/**
* Creates a DrmInfo object describing the settings used to initialize the
* engine.
Expand Down
25 changes: 24 additions & 1 deletion test/media/drm_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,37 @@ describe('DrmEngine', function() {
}).then(done);
});

it('makes no queries for clear content', function(done) {
it('makes no queries for clear content if no key config', function(done) {
requestMediaKeySystemAccessSpy.and.callFake(
fakeRequestMediaKeySystemAccess.bind(null, []));
manifest.periods[0].variants[0].drmInfos = [];
config.servers = {};
config.advanced = {};

drmEngine.configure(config);
drmEngine.init(manifest, /* offline */ false).then(function() {
expect(drmEngine.initialized()).toBe(true);
expect(drmEngine.keySystem()).toBe('');
expect(requestMediaKeySystemAccessSpy.calls.count()).toBe(0);
}).catch(fail).then(done);
});

it('makes queries for clear content if key is configured', function(done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thank you for writing tests!

requestMediaKeySystemAccessSpy.and.callFake(
fakeRequestMediaKeySystemAccess.bind(null, ['drm.abc']));
manifest.periods[0].variants[0].drmInfos = [];
config.servers = {
'drm.abc': 'http://abc.drm/license'
};

drmEngine.configure(config);
drmEngine.init(manifest, /* offline */ false).then(function() {
expect(drmEngine.initialized()).toBe(true);
expect(drmEngine.keySystem()).toBe('drm.abc');
expect(requestMediaKeySystemAccessSpy.calls.count()).toBe(1);
}).then(done);
});

it('uses advanced config to override DrmInfo fields', function(done) {
// Leave only one drmInfo
manifest = new shaka.test.ManifestGenerator()
Expand Down Expand Up @@ -508,6 +527,8 @@ describe('DrmEngine', function() {
requestMediaKeySystemAccessSpy.and.callFake(
fakeRequestMediaKeySystemAccess.bind(null, []));
manifest.periods[0].variants[0].drmInfos = [];
config.servers = {};
config.advanced = {};

initAndAttach().then(function() {
expect(mockVideo.setMediaKeys).not.toHaveBeenCalled();
Expand Down Expand Up @@ -775,6 +796,8 @@ describe('DrmEngine', function() {

it('dispatches an error if manifest says unencrypted', function(done) {
manifest.periods[0].variants[0].drmInfos = [];
config.servers = {};
config.advanced = {};

onErrorSpy.and.stub();

Expand Down