Skip to content

Commit

Permalink
feat: Cache mediaCapabilities.decodingInfo results
Browse files Browse the repository at this point in the history
  • Loading branch information
theodab committed Dec 5, 2022
1 parent 6915a97 commit 1a5dfb3
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,26 @@ shaka.util.StreamUtils = class {
}


/**
* Queries mediaCapabilities for the decoding info for that decoding config.
* If that query has been done before, instead return a cached result.
* @param {!MediaDecodingConfiguration} decodingConfig
* @return {!Promise.<!MediaCapabilitiesDecodingInfo>}
* @private
*/
static async getDecodingInfo_(decodingConfig) {
const cacheKey = JSON.stringify(decodingConfig);
const cache = shaka.util.StreamUtils.decodingConfigCache_;
if (cache[cacheKey]) {
return cache[cacheKey];
}
const result =
await navigator.mediaCapabilities.decodingInfo(decodingConfig);
cache[cacheKey] = result;
return result;
}


/**
* Get the decodingInfo results of the variants via MediaCapabilities.
* This should be called after the DrmEngine is created and configured, and
Expand All @@ -518,11 +538,10 @@ shaka.util.StreamUtils = class {
return;
}

const mediaCapabilities = navigator.mediaCapabilities;

const getVariantDecodingInfos = (async (variant, decodingConfig) => {
try {
const result = await mediaCapabilities.decodingInfo(decodingConfig);
const result =
await shaka.util.StreamUtils.getDecodingInfo_(decodingConfig);
variant.decodingInfos.push(result);
} catch (e) {
shaka.log.info('MediaCapabilities.decodingInfo() failed.',
Expand Down Expand Up @@ -1578,6 +1597,16 @@ shaka.util.StreamUtils = class {
};


/**
* A cache of results from mediaCapabilities.decodingInfo, indexed by the
* (stringified) decodingConfig.
*
* @type {Object.<(!string), (!MediaCapabilitiesDecodingInfo)>}
* @private
*/
shaka.util.StreamUtils.decodingConfigCache_ = {};


/** @private {number} */
shaka.util.StreamUtils.nextTrackId_ = 0;

Expand Down

0 comments on commit 1a5dfb3

Please sign in to comment.