Source: lib/abr/ewma_bandwidth_estimator.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.abr.EwmaBandwidthEstimator');
  7. goog.require('shaka.abr.Ewma');
  8. /**
  9. * @summary
  10. * This class tracks bandwidth samples and estimates available bandwidth.
  11. * Based on the minimum of two exponentially-weighted moving averages with
  12. * different half-lives.
  13. *
  14. */
  15. shaka.abr.EwmaBandwidthEstimator = class {
  16. constructor() {
  17. /**
  18. * A fast-moving average.
  19. * Half of the estimate is based on the last 2 seconds of sample history.
  20. * @private {!shaka.abr.Ewma}
  21. */
  22. this.fast_ = new shaka.abr.Ewma(2);
  23. /**
  24. * A slow-moving average.
  25. * Half of the estimate is based on the last 5 seconds of sample history.
  26. * @private {!shaka.abr.Ewma}
  27. */
  28. this.slow_ = new shaka.abr.Ewma(5);
  29. /**
  30. * Number of bytes sampled.
  31. * @private {number}
  32. */
  33. this.bytesSampled_ = 0;
  34. /**
  35. * Minimum number of bytes sampled before we trust the estimate. If we have
  36. * not sampled much data, our estimate may not be accurate enough to trust.
  37. * If bytesSampled_ is less than minTotalBytes_, we use defaultEstimate_.
  38. * This specific value is based on experimentation.
  39. *
  40. * @private {number}
  41. */
  42. this.minTotalBytes_ = 128e3; // 128kB
  43. /**
  44. * Minimum number of bytes, under which samples are discarded. Our models
  45. * do not include latency information, so connection startup time (time to
  46. * first byte) is considered part of the download time. Because of this, we
  47. * should ignore very small downloads which would cause our estimate to be
  48. * too low.
  49. * This specific value is based on experimentation.
  50. *
  51. * @private {number}
  52. */
  53. this.minBytes_ = 16e3; // 16kB
  54. }
  55. /**
  56. * Called by the Player to provide an updated configuration any time it
  57. * changes.
  58. * Must be called at least once before init().
  59. *
  60. * @param {shaka.extern.AdvancedAbrConfiguration} config
  61. */
  62. configure(config) {
  63. this.minTotalBytes_ = config.minTotalBytes;
  64. this.minBytes_ = config.minBytes;
  65. this.fast_.updateAlpha(config.fastHalfLife);
  66. this.slow_.updateAlpha(config.slowHalfLife);
  67. }
  68. /**
  69. * Takes a bandwidth sample.
  70. *
  71. * @param {number} durationMs The amount of time, in milliseconds, for a
  72. * particular request.
  73. * @param {number} numBytes The total number of bytes transferred in that
  74. * request.
  75. */
  76. sample(
  77. durationMs, numBytes) {
  78. if (numBytes < this.minBytes_) {
  79. return;
  80. }
  81. const bandwidth = 8000 * numBytes / durationMs;
  82. const weight = durationMs / 1000;
  83. this.bytesSampled_ += numBytes;
  84. this.fast_.sample(weight, bandwidth);
  85. this.slow_.sample(weight, bandwidth);
  86. }
  87. /**
  88. * Gets the current bandwidth estimate.
  89. *
  90. * @param {number} defaultEstimate
  91. * @return {number} The bandwidth estimate in bits per second.
  92. */
  93. getBandwidthEstimate(defaultEstimate) {
  94. if (this.bytesSampled_ < this.minTotalBytes_) {
  95. return defaultEstimate;
  96. }
  97. // Take the minimum of these two estimates. This should have the effect
  98. // of adapting down quickly, but up more slowly.
  99. return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
  100. }
  101. /**
  102. * @return {boolean} True if there is enough data to produce a meaningful
  103. * estimate.
  104. */
  105. hasGoodEstimate() {
  106. return this.bytesSampled_ >= this.minTotalBytes_;
  107. }
  108. };