Skip to content

Commit

Permalink
fix: Manually order key for decodingInfo cache (#4795)
Browse files Browse the repository at this point in the history
  • Loading branch information
theodab authored Dec 7, 2022
1 parent 36db83d commit 806a9a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
46 changes: 42 additions & 4 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,40 @@ shaka.util.StreamUtils = class {
}


/**
* Constructs a string out of an object, similar to the JSON.stringify method.
* Unlike that method, this guarantees that the order of the keys is
* alphabetical, so it can be used as a way to reliably compare two objects.
*
* @param {!Object} obj
* @return {string}
* @private
*/
static alphabeticalKeyOrderStringify_(obj) {
const keys = [];
for (const key in obj) {
keys.push(key);
}
// Alphabetically sort the keys, so they will be in a reliable order.
keys.sort();

const terms = [];
for (const key of keys) {
const escapedKey = JSON.stringify(key);
const value = obj[key];
if (value instanceof Object) {
const stringifiedValue =
shaka.util.StreamUtils.alphabeticalKeyOrderStringify_(value);
terms.push(escapedKey + ':' + stringifiedValue);
} else {
const escapedValue = JSON.stringify(value);
terms.push(escapedKey + ':' + escapedValue);
}
}
return '{' + terms.join(',') + '}';
}


/**
* Queries mediaCapabilities for the decoding info for that decoding config,
* and assigns it to the given variant.
Expand All @@ -510,10 +544,14 @@ shaka.util.StreamUtils = class {
* @private
*/
static async getDecodingInfosForVariant_(variant, decodingConfig) {
const cacheKey =
shaka.util.StreamUtils.alphabeticalKeyOrderStringify_(decodingConfig);

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 +1639,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 806a9a8

Please sign in to comment.