All files / src/utils ewma-bandwidth-estimator.js

100% Statements 16/16
100% Branches 4/4
80% Functions 4/5
100% Lines 16/16

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                        5x 5x 5x 5x 5x 5x       9x 9x     9x 9x 9x       10x 10x       10x         9x   1x                
/*
 * EWMA Bandwidth Estimator
 *  - heavily inspired from shaka-player
 * Tracks bandwidth samples and estimates available bandwidth.
 * Based on the minimum of two exponentially-weighted moving averages with
 * different half-lives.
 */
 
import EWMA from '../utils/ewma';
 
class EwmaBandWidthEstimator {
  constructor (hls, slow, fast, defaultEstimate) {
    this.hls = hls;
    this.defaultEstimate_ = defaultEstimate;
    this.minWeight_ = 0.001;
    this.minDelayMs_ = 50;
    this.slow_ = new EWMA(slow);
    this.fast_ = new EWMA(fast);
  }
 
  sample (durationMs, numBytes) {
    durationMs = Math.max(durationMs, this.minDelayMs_);
    let bandwidth = 8000 * numBytes / durationMs,
      // console.log('instant bw:'+ Math.round(bandwidth));
      // we weight sample using loading duration....
      weight = durationMs / 1000;
    this.fast_.sample(weight, bandwidth);
    this.slow_.sample(weight, bandwidth);
  }
 
  canEstimate () {
    let fast = this.fast_;
    return (fast && fast.getTotalWeight() >= this.minWeight_);
  }
 
  getEstimate () {
    if (this.canEstimate()) {
      // console.log('slow estimate:'+ Math.round(this.slow_.getEstimate()));
      // console.log('fast estimate:'+ Math.round(this.fast_.getEstimate()));
      // Take the minimum of these two estimates.  This should have the effect of
      // adapting down quickly, but up more slowly.
      return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
    } else {
      return this.defaultEstimate_;
    }
  }
 
  destroy () {
  }
}
export default EwmaBandWidthEstimator;