Files
shaka-player/lib/util/multi_map.js
T
Denis Seleznev b75cd4c38d fix: Fix codec choice when resolutions differ (#3072)
Before this change, our codec choice only worked when all codecs had the same resolutions available.

When one codec had a different set of resolutions available, we might end up choosing the wrong one.  This is because our choice was based on the average bandwidth of all resolutions for that codec.  So a "better" codec with higher resolutions available would also end up having a higher average bitrate, and would be avoided.

This fixes the issue by only considering the resolutions that all codecs have in common, then taking the average bitrate among those to choose a codec.
2021-02-24 10:43:48 -08:00

101 lines
1.8 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.MultiMap');
/**
* @summary A simple multimap template.
* @template T
*/
shaka.util.MultiMap = class {
/** */
constructor() {
/** @private {!Object.<string, !Array.<T>>} */
this.map_ = {};
}
/**
* Add a key, value pair to the map.
* @param {string} key
* @param {T} value
*/
push(key, value) {
// eslint-disable-next-line no-prototype-builtins
if (this.map_.hasOwnProperty(key)) {
this.map_[key].push(value);
} else {
this.map_[key] = [value];
}
}
/**
* Get a list of values by key.
* @param {string} key
* @return {Array.<T>} or null if no such key exists.
*/
get(key) {
const list = this.map_[key];
// slice() clones the list so that it and the map can each be modified
// without affecting the other.
return list ? list.slice() : null;
}
/**
* Get a list of all values.
* @return {!Array.<T>}
*/
getAll() {
const list = [];
for (const key in this.map_) {
list.push(...this.map_[key]);
}
return list;
}
/**
* Remove a specific value, if it exists.
* @param {string} key
* @param {T} value
*/
remove(key, value) {
if (!(key in this.map_)) {
return;
}
this.map_[key] = this.map_[key].filter((i) => i != value);
}
/**
* Clear all keys and values from the multimap.
*/
clear() {
this.map_ = {};
}
/**
* @param {function(string, !Array.<T>)} callback
*/
forEach(callback) {
for (const key in this.map_) {
callback(key, this.map_[key]);
}
}
/**
* Returns the number of elements in the multimap.
* @return {number}
*/
size() {
return Object.keys(this.map_).length;
}
};