Skip to content

Commit

Permalink
feat: Cache mediaCapabilities.decodingInfo results (shaka-project#4789)
Browse files Browse the repository at this point in the history
  • Loading branch information
theodab committed Dec 6, 2022
1 parent 6915a97 commit b7781f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
52 changes: 39 additions & 13 deletions lib/util/stream_utils.js
Expand Up @@ -499,6 +499,33 @@ shaka.util.StreamUtils = class {
}


/**
* Queries mediaCapabilities for the decoding info for that decoding config,
* and assigns it to the given variant.
* If that query has been done before, instead return a cached result.
* @param {!shaka.extern.Variant} variant
* @param {!MediaDecodingConfiguration} decodingConfig
* @private
*/
static async getDecodingInfosForVariant_(variant, decodingConfig) {
try {
const cacheKey = JSON.stringify(decodingConfig);
const cache = shaka.util.StreamUtils.decodingConfigCache;
if (cache[cacheKey]) {
variant.decodingInfos.push(cache[cacheKey]);
} else {
const result =
await navigator.mediaCapabilities.decodingInfo(decodingConfig);
cache[cacheKey] = result;
variant.decodingInfos.push(result);
}
} catch (e) {
shaka.log.info('MediaCapabilities.decodingInfo() failed.',
JSON.stringify(decodingConfig), e);
}
}


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

const mediaCapabilities = navigator.mediaCapabilities;

const getVariantDecodingInfos = (async (variant, decodingConfig) => {
try {
const result = await mediaCapabilities.decodingInfo(decodingConfig);
variant.decodingInfos.push(result);
} catch (e) {
shaka.log.info('MediaCapabilities.decodingInfo() failed.',
JSON.stringify(decodingConfig), e);
}
});

for (const variant of variants) {
/** @type {!Array.<!MediaDecodingConfiguration>} */
const decodingConfigs = shaka.util.StreamUtils.getDecodingConfigs_(
Expand All @@ -540,7 +555,8 @@ shaka.util.StreamUtils = class {
// https://github.com/shaka-project/shaka-player/pull/4708#discussion_r1022581178
for (const config of decodingConfigs) {
// eslint-disable-next-line no-await-in-loop
await getVariantDecodingInfos(variant, config);
await shaka.util.StreamUtils.getDecodingInfosForVariant_(
variant, config);
}
}
}
Expand Down Expand Up @@ -1578,6 +1594,16 @@ shaka.util.StreamUtils = class {
};


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


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

Expand Down
5 changes: 5 additions & 0 deletions test/test/boot.js
Expand Up @@ -331,6 +331,11 @@ function configureJasmineEnvironment() {
});
}

// Reset decoding config cache after each test.
afterEach(() => {
shaka.util.StreamUtils.decodingConfigCache = {};
});

// Code in karma-jasmine's adapter will malform test failures when the
// expectation message contains a stack trace, losing the failure message and
// mixing up the stack trace of the failure. To avoid this, we modify
Expand Down

0 comments on commit b7781f0

Please sign in to comment.