Home Reference Source

src/utils/ewma-bandwidth-estimator.ts

  1. /*
  2. * EWMA Bandwidth Estimator
  3. * - heavily inspired from shaka-player
  4. * Tracks bandwidth samples and estimates available bandwidth.
  5. * Based on the minimum of two exponentially-weighted moving averages with
  6. * different half-lives.
  7. */
  8.  
  9. import EWMA from '../utils/ewma';
  10.  
  11. class EwmaBandWidthEstimator {
  12. private defaultEstimate_: number;
  13. private minWeight_: number;
  14. private minDelayMs_: number;
  15. private slow_: EWMA;
  16. private fast_: EWMA;
  17.  
  18. constructor(slow: number, fast: number, defaultEstimate: number) {
  19. this.defaultEstimate_ = defaultEstimate;
  20. this.minWeight_ = 0.001;
  21. this.minDelayMs_ = 50;
  22. this.slow_ = new EWMA(slow);
  23. this.fast_ = new EWMA(fast);
  24. }
  25.  
  26. update(slow: number, fast: number) {
  27. const { slow_, fast_ } = this;
  28. if (this.slow_.halfLife !== slow) {
  29. this.slow_ = new EWMA(slow, slow_.getEstimate(), slow_.getTotalWeight());
  30. }
  31. if (this.fast_.halfLife !== fast) {
  32. this.fast_ = new EWMA(fast, fast_.getEstimate(), fast_.getTotalWeight());
  33. }
  34. }
  35.  
  36. sample(durationMs: number, numBytes: number) {
  37. durationMs = Math.max(durationMs, this.minDelayMs_);
  38. const numBits = 8 * numBytes;
  39. // weight is duration in seconds
  40. const durationS = durationMs / 1000;
  41. // value is bandwidth in bits/s
  42. const bandwidthInBps = numBits / durationS;
  43. this.fast_.sample(durationS, bandwidthInBps);
  44. this.slow_.sample(durationS, bandwidthInBps);
  45. }
  46.  
  47. canEstimate(): boolean {
  48. const fast = this.fast_;
  49. return fast && fast.getTotalWeight() >= this.minWeight_;
  50. }
  51.  
  52. getEstimate(): number {
  53. if (this.canEstimate()) {
  54. // console.log('slow estimate:'+ Math.round(this.slow_.getEstimate()));
  55. // console.log('fast estimate:'+ Math.round(this.fast_.getEstimate()));
  56. // Take the minimum of these two estimates. This should have the effect of
  57. // adapting down quickly, but up more slowly.
  58. return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
  59. } else {
  60. return this.defaultEstimate_;
  61. }
  62. }
  63.  
  64. destroy() {}
  65. }
  66. export default EwmaBandWidthEstimator;