Skip to content

Commit

Permalink
fix: Even better method for generating keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
theodab committed Dec 7, 2022
1 parent b446633 commit 8805169
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 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,39 +544,8 @@ 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('');
const cacheKey =
shaka.util.StreamUtils.alphabeticalKeyOrderStringify_(decodingConfig);

try {
const cache = shaka.util.StreamUtils.decodingConfigCache_;
Expand Down

0 comments on commit 8805169

Please sign in to comment.