Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Manually order key for decodingInfo cache #4795

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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