Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | 3x 3x 17x 17x 17x 17x 17x 17x 17x 17x 15x 15x 25x 25x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 1x 1x 2x 2x 1x 1x 7x 7x 7x 7x 4x 4x 4x 4x 13x 2x 2x 2x 2x 2x 6x 6x 6x 1x 1x 1x 1x 1x 4x 4x 4x 3x 3x 3x 3x 9x 3x 2x 2x 2x 2x 2x 6x 6x 6x 2x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 6x 1x 1x 1x 1x 6x 6x 1x 7x 7x 2x 2x 15x 15x 3x 3x 1x | /**
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2017, Dash Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import Constants from '../constants/Constants.js';
import FactoryMaker from '../../core/FactoryMaker.js';
import ThroughputModel from '../models/ThroughputModel.js';
import MetricsConstants from '../constants/MetricsConstants.js';
import {HTTPRequest} from '../vo/metrics/HTTPRequest.js';
import MediaPlayerEvents from '../MediaPlayerEvents.js';
import EventBus from '../../core/EventBus.js';
/**
* @constructor
* @ignore
*/
function ThroughputController() {
const context = this.context;
const eventBus = EventBus(context).getInstance();
let throughputModel,
playbackController,
settings;
function initialize() {
throughputModel = ThroughputModel(context).create({
settings
});
_registerEvents();
}
function setConfig(config) {
if (config.settings) {
settings = config.settings;
}
if (config.playbackController) {
playbackController = config.playbackController;
}
}
function _registerEvents() {
eventBus.on(MediaPlayerEvents.METRIC_ADDED, _onMetricAdded, instance);
window.performance?.addEventListener?.(
'resourcetimingbufferfull',
_onResourceTimingBufferFull,
);
}
function _resetEvents() {
eventBus.off(MediaPlayerEvents.METRIC_ADDED, _onMetricAdded, instance);
window.performance?.removeEventListener?.(
'resourcetimingbufferfull',
_onResourceTimingBufferFull,
);
}
function _onResourceTimingBufferFull() {
performance.clearResourceTimings();
}
/**
* Push new values to the throughput model once an HTTP request completed
* @param {object} e
* @private
*/
function _onMetricAdded(e) {
if (e.metric === MetricsConstants.HTTP_REQUEST && e.value && (e.value.type === HTTPRequest.MPD_TYPE || (e.value.type === HTTPRequest.MEDIA_SEGMENT_TYPE && (e.mediaType === Constants.AUDIO || e.mediaType === Constants.VIDEO)))) {
throughputModel.addEntry(e.mediaType, e.value);
}
}
/**
* Get average value
* @param {string} throughputType
* @param {string} mediaType
* @param {string|null} calculationMode
* @param {number} sampleSize
* @return {number}
* @private
*/
function _getAverage(throughputType, mediaType, calculationMode = null, sampleSize = NaN) {
let dict = null;
let ewmaHalfLife = throughputModel.getEwmaHalfLife();
let halfLife = null;
let useMin = true;
Iif (!calculationMode) {
calculationMode = settings.get().streaming.abr.throughput.averageCalculationMode;
}
switch (throughputType) {
// Set the parameters for the standard bandwidth calculation based on throughput values for a media type
case Constants.THROUGHPUT_TYPES.BANDWIDTH:
dict = calculationMode === Constants.THROUGHPUT_CALCULATION_MODES.EWMA ? throughputModel.getEwmaThroughputDict(mediaType) : throughputModel.getThroughputDict(mediaType);
halfLife = ewmaHalfLife.bandwidthHalfLife;
useMin = true;
sampleSize = !isNaN(sampleSize) ? sampleSize : playbackController.getIsDynamic() ? settings.get().streaming.abr.throughput.sampleSettings.live : settings.get().streaming.abr.throughput.sampleSettings.vod;
break;
// Set the parameters for the standard latency calculation based on throughput values for a media type
case Constants.THROUGHPUT_TYPES.LATENCY:
dict = calculationMode === Constants.THROUGHPUT_CALCULATION_MODES.EWMA ? throughputModel.getEwmaLatencyDict(mediaType) : throughputModel.getLatencyDict(mediaType);
halfLife = ewmaHalfLife.latencyHalfLife;
useMin = false;
sampleSize = !isNaN(sampleSize) ? sampleSize : settings.get().streaming.abr.throughput.sampleSettings.averageLatencySampleAmount;
break;
}
Iif (!dict || dict.length === 0) {
return NaN;
}
let adjustedSampleSize;
switch (calculationMode) {
case Constants.THROUGHPUT_CALCULATION_MODES.ARITHMETIC_MEAN:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getArithmeticMean(dict, adjustedSampleSize);
case Constants.THROUGHPUT_CALCULATION_MODES.BYTE_SIZE_WEIGHTED_ARITHMETIC_MEAN:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getByteSizeWeightedArithmeticMean(dict, adjustedSampleSize);
case Constants.THROUGHPUT_CALCULATION_MODES.DATE_WEIGHTED_ARITHMETIC_MEAN:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getDateWeightedArithmeticMean(dict, adjustedSampleSize);
case Constants.THROUGHPUT_CALCULATION_MODES.HARMONIC_MEAN:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getHarmonicMean(dict, adjustedSampleSize);
case Constants.THROUGHPUT_CALCULATION_MODES.BYTE_SIZE_WEIGHTED_HARMONIC_MEAN:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getByteSizeWeightedHarmonicMean(dict, adjustedSampleSize);
case Constants.THROUGHPUT_CALCULATION_MODES.DATE_WEIGHTED_HARMONIC_MEAN:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getDateWeightedHarmonicMean(dict, adjustedSampleSize);
case Constants.THROUGHPUT_CALCULATION_MODES.EWMA:
return getEwma(dict, halfLife, useMin);
case Constants.THROUGHPUT_CALCULATION_MODES.ZLEMA:
adjustedSampleSize = _getAdjustedSampleSize(dict, sampleSize, throughputType);
return getZlema(dict, adjustedSampleSize);
}
}
/**
* @param {array} dict
* @param {number} sampleSize
* @param {string} type
* @return {number}
* @private
*/
function _getAdjustedSampleSize(dict, sampleSize, type) {
Iif (!dict) {
sampleSize = 0;
} else if (sampleSize >= dict.length) {
sampleSize = dict.length;
} else Eif (type === Constants.THROUGHPUT_TYPES.BANDWIDTH && settings.get().streaming.abr.throughput.sampleSettings.enableSampleSizeAdjustment) {
// if throughput samples vary a lot, average over a wider sample
for (let i = 1; i < sampleSize; ++i) {
const ratio = dict[dict.length - i].value / dict[dict.length - i - 1].value;
if (ratio >= settings.get().streaming.abr.throughput.sampleSettings.increaseScale || ratio <= settings.get().streaming.abr.throughput.sampleSettings.decreaseScale) {
sampleSize += 1;
if (sampleSize === dict.length) { // cannot increase sampleSize beyond arr.length
break;
}
}
}
}
return sampleSize;
}
/**
* Calculate the arithmetic mean of the values provided via the dict
* @param {array} dict
* @param {number} sampleSize
* @return {number|*}
* @private
*/
function getArithmeticMean(dict, sampleSize) {
let arr = dict;
Iif (sampleSize === 0 || !arr || arr.length === 0) {
return NaN;
}
// Extract the last n elements
arr = arr.slice(-sampleSize);
return arr.reduce((total, entry) => {
return total + entry.value
}, 0) / arr.length;
}
/**
* Calculates the byte size weighted arithmetic mean of the values provided via the dict
* @param {array} dict
* @param {number} sampleSize
* @return {number|*}
* @private
*/
function getByteSizeWeightedArithmeticMean(dict, sampleSize) {
let arr = dict;
Iif (sampleSize === 0 || !arr || arr.length === 0) {
return NaN;
}
// Extract the last n elements
arr = arr.slice(-sampleSize);
let divideBy = 0;
return arr.reduce((total, entry) => {
let weight = Math.sqrt(entry.downloadedBytes);
divideBy += weight;
return total + entry.value * weight
}, 0) / divideBy;
}
/**
* Calculates the time weighted arithmetic mean of the values provided via the dict
* @param {array} dict
* @param {number} sampleSize
* @return {number|*}
* @private
*/
function getDateWeightedArithmeticMean(dict, sampleSize) {
let arr = dict;
Iif (sampleSize === 0 || !arr || arr.length === 0) {
return NaN;
}
// Extract the last n elements
arr = arr.slice(-sampleSize);
let divideBy = 0;
return arr.reduce((total, entry, index) => {
let weight = index + 1;
divideBy += weight;
return total + entry.value * weight
}, 0) / divideBy;
}
/**
* Calculate the harmonic mean of the values provided via the dict
* @param {array} dict
* @param {number} sampleSize
* @return {number|*}
* @private
*/
function getHarmonicMean(dict, sampleSize) {
let arr = dict;
Iif (sampleSize === 0 || !arr || arr.length === 0) {
return NaN;
}
// Extract the last n elements
arr = arr.slice(-sampleSize);
const value = arr.reduce((total, entry) => {
return total + 1 / entry.value
}, 0);
return arr.length / value
}
/**
* Calculate the harmonic mean of the values provided via the dict
* @param {array} dict
* @param {number} sampleSize
* @return {number|*}
* @private
*/
function getByteSizeWeightedHarmonicMean(dict, sampleSize) {
let arr = dict;
Iif (sampleSize === 0 || !arr || arr.length === 0) {
return NaN;
}
// Extract the last n elements
arr = arr.slice(-sampleSize);
let dividend = 0;
const value = arr.reduce((total, entry) => {
let weight = Math.sqrt(entry.downloadedBytes);
dividend += weight;
return total + (1 / entry.value) * weight
}, 0);
return dividend / value
}
/**
* Calculates the time weighted harmonic mean of the values provided via the dict
* @param {array} dict
* @param {number} sampleSize
* @return {number|*}
* @private
*/
function getDateWeightedHarmonicMean(dict, sampleSize) {
let arr = dict;
Iif (sampleSize === 0 || !arr || arr.length === 0) {
return NaN;
}
// Extract the last n elements
arr = arr.slice(-sampleSize);
let dividend = 0;
const value = arr.reduce((total, entry, index) => {
let weight = index + 1;
dividend += weight;
return total + (1 / entry.value) * weight
}, 0);
return dividend / value
}
/**
* Calculated the exponential weighted moving average for the values provided via the dict
* @param {object} dict
* @param {object} halfLife
* @param {boolean} useMin - Whether to apply Math.min of the fastEstimate and the slowEstimate
* @return {number}
* @private
*/
function getEwma(dict, halfLife, useMin = true) {
if (!dict || dict.totalWeight <= 0) {
return NaN;
}
// to correct for startup, divide by zero factor = 1 - Math.pow(0.5, ewmaObj.totalWeight / halfLife)
const fastEstimate = dict.fastEstimate / (1 - Math.pow(0.5, dict.totalWeight / halfLife.fast));
const slowEstimate = dict.slowEstimate / (1 - Math.pow(0.5, dict.totalWeight / halfLife.slow));
return useMin ? Math.min(fastEstimate, slowEstimate) : Math.max(fastEstimate, slowEstimate);
}
/**
* Calculates the Zero-Lag Exponential Moving Average
* @param {array} dict
* @param {number} sampleSize
* @returns {number}
*/
function getZlema(dict, sampleSize) {
Iif (sampleSize === 0 || !dict || dict.length === 0) {
return NaN;
}
// Extract the last n elements
let values = dict.slice(-sampleSize).map((entry) => {
return entry.value;
})
let alpha = 2 / (values.length + 1);
let ema = values[values.length - 1];
let zlema = values[values.length - 1];
for (let i = 0; i < values.length; i++) {
ema = alpha * values[i] + (1 - alpha) * ema;
zlema = alpha * ema + (1 - alpha) * zlema;
}
return zlema;
}
/**
* Returns the average throughput based on the provided calculation mode. The returned value is depicted in kbit/s
* @param {string} mediaType
* @param {string | null} calculationMode
* @param {number | NaN} sampleSize
* @return {number}
*/
function getAverageThroughput(mediaType, calculationMode = null, sampleSize = NaN) {
const value = _getAverage(Constants.THROUGHPUT_TYPES.BANDWIDTH, mediaType, calculationMode, sampleSize);
return Math.round(value);
}
/**
* Returns the average throughout applying the bandwidth safety factor provided in the settings. The returned value is depicted in kbit/s
* @param {string} mediaType
* @param {string | null} calculationMode
* @param {number | NaN} sampleSize
* @return {number}
*/
function getSafeAverageThroughput(mediaType, calculationMode = null, sampleSize = NaN) {
let average = getAverageThroughput(mediaType, calculationMode, sampleSize);
if (!isNaN(average)) {
average *= settings.get().streaming.abr.throughput.bandwidthSafetyFactor;
}
return average;
}
/**
* Returns the average latency based on the provided calculation mode
* @param {string} mediaType
* @param {string | null} calculationMode
* @param {number | NaN} sampleSize
* @return {number}
*/
function getAverageLatency(mediaType, calculationMode = null, sampleSize = NaN) {
const value = _getAverage(Constants.THROUGHPUT_TYPES.LATENCY, mediaType, calculationMode, sampleSize);
return Math.round(value);
}
/**
* Returns the raw throughput measurements without calculating the average
* @param mediaType
* @returns {*}
*/
function getRawThroughputData(mediaType) {
Iif (!mediaType) {
return []
}
return throughputModel.getThroughputDict(mediaType);
}
function reset() {
throughputModel.reset();
_resetEvents();
}
const instance = {
getArithmeticMean,
getAverageLatency,
getAverageThroughput,
getByteSizeWeightedArithmeticMean,
getByteSizeWeightedHarmonicMean,
getDateWeightedArithmeticMean,
getDateWeightedHarmonicMean,
getEwma,
getHarmonicMean,
getRawThroughputData,
getSafeAverageThroughput,
getZlema,
initialize,
reset,
setConfig
};
return instance;
}
ThroughputController.__dashjs_factory_name = 'ThroughputController';
export default FactoryMaker.getSingletonFactory(ThroughputController);
|