Skip to content

Commit

Permalink
fix: Manually order key for decodingInfo cache
Browse files Browse the repository at this point in the history
  • Loading branch information
theodab committed Dec 6, 2022
1 parent 68f10a1 commit b446633
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
43 changes: 39 additions & 4 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,45 @@ shaka.util.StreamUtils = class {
* @private
*/
static async getDecodingInfosForVariant_(variant, decodingConfig) {
// Construct a key for that decoding config. By using this instead of plain
// JSON.stringify, we ensure that the keys are always in a consistent order.
const cacheKeyAr = [decodingConfig.type];
if (decodingConfig.video) {
cacheKeyAr.push(decodingConfig.video.contentType);
cacheKeyAr.push(decodingConfig.video.width);
cacheKeyAr.push(decodingConfig.video.height);
cacheKeyAr.push(decodingConfig.video.bitrate);
cacheKeyAr.push(decodingConfig.video.framerate);
cacheKeyAr.push(decodingConfig.video.transferFunction);
}
if (decodingConfig.audio) {
cacheKeyAr.push(decodingConfig.audio.contentType);
cacheKeyAr.push(decodingConfig.audio.channels);
cacheKeyAr.push(decodingConfig.audio.bitrate);
cacheKeyAr.push(decodingConfig.audio.samplerate);
cacheKeyAr.push(decodingConfig.audio.spatialRendering);
}
if (decodingConfig.keySystemConfiguration) {
const keyConfig = decodingConfig.keySystemConfiguration;
cacheKeyAr.push(keyConfig.keySystem);
cacheKeyAr.push(keyConfig.initDataType);
cacheKeyAr.push(keyConfig.persistentState);
cacheKeyAr.push(keyConfig.distinctiveIdentifier);
cacheKeyAr.push(keyConfig.sessionTypes.join(','));
if (keyConfig.audio) {
cacheKeyAr.push(keyConfig.audio.robustness);
}
if (keyConfig.video) {
cacheKeyAr.push(keyConfig.video.robustness);
}
}
const cacheKey = cacheKeyAr.map((term) => `{${term}}`).join('');

try {
const cacheKey = JSON.stringify(decodingConfig);
const cache = shaka.util.StreamUtils.decodingConfigCache;
const cache = shaka.util.StreamUtils.decodingConfigCache_;
if (cache[cacheKey]) {
shaka.log.v2('Using cached results of mediaCapabilities.decodingInfo',
'for key', cacheKey);
variant.decodingInfos.push(cache[cacheKey]);
} else {
const result =
Expand Down Expand Up @@ -1601,9 +1636,9 @@ shaka.util.StreamUtils = class {
* (stringified) decodingConfig.
*
* @type {Object.<(!string), (!MediaCapabilitiesDecodingInfo)>}
* @export
* @private
*/
shaka.util.StreamUtils.decodingConfigCache = {};
shaka.util.StreamUtils.decodingConfigCache_ = {};


/** @private {number} */
Expand Down
4 changes: 2 additions & 2 deletions test/test/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ function configureJasmineEnvironment() {
}

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

// Code in karma-jasmine's adapter will malform test failures when the
Expand Down

0 comments on commit b446633

Please sign in to comment.