Skip to content

Commit

Permalink
Pass all Variants to prepareMediaKeyConfigsForVariant_
Browse files Browse the repository at this point in the history
Rather than try to build the config by key system map across
multiple calls to prepareMediaKeyConfigsForVariant_, pass all the
variants to it and build it in one call.

Issue #1567

Change-Id: Ibb7314117895a70dc421465cfcae1695108bba7e
  • Loading branch information
vaage committed Oct 1, 2018
1 parent 86a4f30 commit ed295ce
Showing 1 changed file with 75 additions and 62 deletions.
137 changes: 75 additions & 62 deletions lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ goog.require('shaka.util.FakeEvent');
goog.require('shaka.util.Functional');
goog.require('shaka.util.IDestroyable');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.MapUtils');
goog.require('shaka.util.MimeUtils');
goog.require('shaka.util.PublicPromise');
Expand Down Expand Up @@ -372,11 +371,7 @@ shaka.media.DrmEngine.prototype.init_ = function(variants) {
}

/** @type {!Map.<string, MediaKeySystemConfiguration>} */
let configsByKeySystem = new Map();

for (const variant of variants) {
this.prepareMediaKeyConfigsForVariant_(variant, configsByKeySystem);
}
const configsByKeySystem = this.prepareMediaKeyConfigsForVariants_(variants);

// TODO(vaage): Find an explanation for the difference between this
// "unencrypted" form and the "no drm info unencrypted form" and express
Expand Down Expand Up @@ -641,19 +636,24 @@ shaka.media.DrmEngine.prototype.getKeyStatuses = function() {


/**
* @param {!shaka.extern.Variant} variant
* @param {!Map.<string, MediaKeySystemConfiguration>} configsByKeySystem
* (Output parameter.) A dictionary of configs, indexed by key system.
* @param {!Array.<shaka.extern.Variant>} variants
* @see https://bit.ly/EmeConfig for MediaKeySystemConfiguration spec
* @return {!Map.<string, MediaKeySystemConfiguration>}
* @private
*/
shaka.media.DrmEngine.prototype.prepareMediaKeyConfigsForVariant_ =
function(variant, configsByKeySystem) {
// Get all the streams in the variant.
const streams = shaka.util.StreamUtils.getVariantStreams(variant);
shaka.media.DrmEngine.prototype.prepareMediaKeyConfigsForVariants_ = function(
variants) {
// Get all the drm info so that we can avoid using nested loops when we just
// need the drm info.
const allDrmInfo = new Set();
for (const variant of variants) {
for (const info of variant.drmInfos) {
allDrmInfo.add(info);
}
}

// Make sure all the drm infos are valid and filled in correctly.
variant.drmInfos.forEach((info) => {
for (const info of allDrmInfo) {
this.fillInDrmInfoDefaults_(info);

// Chromecast has a variant of PlayReady that uses a different key
Expand All @@ -668,19 +668,17 @@ shaka.media.DrmEngine.prototype.prepareMediaKeyConfigsForVariant_ =
info.keySystem = 'com.chromecast.playready';
}
}
});
}

// Fill in any missing config entries.
variant.drmInfos.forEach((info) => {
if (configsByKeySystem.has(info.keySystem)) {
return;
}
const persistentState =
this.usePersistentLicenses_ ? 'required' : 'optional';
const sessionTypes =
this.usePersistentLicenses_ ? ['persistent-license'] : ['temporary'];

const persistentState =
this.usePersistentLicenses_ ? 'required' : 'optional';
const sessionTypes =
this.usePersistentLicenses_ ? ['persistent-license'] : ['temporary'];
const configs = new Map();

// Create a config entry for each key system.
for (const info of allDrmInfo) {
const config = {
// Ignore initDataTypes.
audioCapabilities: [],
Expand All @@ -694,52 +692,67 @@ shaka.media.DrmEngine.prototype.prepareMediaKeyConfigsForVariant_ =

// Multiple calls to |set| will still respect the insertion order of the
// first call to |set| for a given key.
configsByKeySystem.set(info.keySystem, config);
});

// Add the last bit of information to each config;
variant.drmInfos.forEach((info) => {
const config = configsByKeySystem.get(info.keySystem);
goog.asserts.assert(
config,
'Any missing configs should have be filled in before.');

config.drmInfos.push(info);

if (info.distinctiveIdentifierRequired) {
config.distinctiveIdentifier = 'required';
}
configs.set(info.keySystem, config);
}

if (info.persistentStateRequired) {
config.persistentState = 'required';
}
// Connect each key system with each stream using it.
for (const variant of variants) {
/** @type {?shaka.extern.Stream} */
const audio = variant.audio;
/** @type {?shaka.extern.Stream} */
const video = variant.video;

/** @type {string} */
const audioMimeType =
audio ?
shaka.util.MimeUtils.getFullType(audio.mimeType, audio.codecs) :
'';
/** @type {string} */
const videoMimeType =
video ?
shaka.util.MimeUtils.getFullType(video.mimeType, video.codecs) :
'';

// Add the last bit of information to each config;
for (const info of variant.drmInfos) {
const config = configs.get(info.keySystem);
goog.asserts.assert(
config,
'Any missing configs should have be filled in before.');

config.drmInfos.push(info);

if (info.distinctiveIdentifierRequired) {
config.distinctiveIdentifier = 'required';
}

streams.forEach((stream) => {
const ContentType = shaka.util.ManifestParserUtils.ContentType;
let isVideo = stream.type == ContentType.VIDEO;
if (info.persistentStateRequired) {
config.persistentState = 'required';
}

/** @type {?string} */
let robustness = isVideo ?
info.videoRobustness :
info.audioRobustness;
if (audio) {
/** @type {MediaKeySystemMediaCapability} */
const capability = {
robustness: info.audioRobustness || '',
contentType: audioMimeType,
};

/** @type {string} */
let fullMimeType = shaka.util.MimeUtils.getFullType(
stream.mimeType, stream.codecs);
config.audioCapabilities.push(capability);
}

/** @type {MediaKeySystemMediaCapability} */
let capability = {
robustness: robustness || '',
contentType: fullMimeType,
};
if (video) {
/** @type {MediaKeySystemMediaCapability} */
const capability = {
robustness: info.videoRobustness || '',
contentType: videoMimeType,
};

if (isVideo) {
config.videoCapabilities.push(capability);
} else {
config.audioCapabilities.push(capability);
}
});
});
}
}

return configs;
};


Expand Down

0 comments on commit ed295ce

Please sign in to comment.