diff --git a/lib/util/stream_utils.js b/lib/util/stream_utils.js index 908e48b6df8..68f0ae5bc5c 100644 --- a/lib/util/stream_utils.js +++ b/lib/util/stream_utils.js @@ -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.} + * @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 @@ -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.', @@ -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;