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. /** */
  17. constructor() {
  18. /**
  19. * A fast-moving average.
  20. * Half of the estimate is based on the last 2 seconds of sample history.
  21. * @private {!shaka.abr.Ewma}
  22. */
  23. this.fast_ = new shaka.abr.Ewma(2);
  24. /**
  25. * A slow-moving average.
  26. * Half of the estimate is based on the last 5 seconds of sample history.
  27. * @private {!shaka.abr.Ewma}
  28. */
  29. this.slow_ = new shaka.abr.Ewma(5);
  30. /**
  31. * Number of bytes sampled.
  32. * @private {number}
  33. */
  34. this.bytesSampled_ = 0;
  35. /**
  36. * Minimum number of bytes sampled before we trust the estimate. If we have
  37. * not sampled much data, our estimate may not be accurate enough to trust.
  38. * If bytesSampled_ is less than minTotalBytes_, we use defaultEstimate_.
  39. * This specific value is based on experimentation.
  40. *
  41. * @private {number}
  42. * @const
  43. */
  44. this.minTotalBytes_ = 128e3; // 128kB
  45. /**
  46. * Minimum number of bytes, under which samples are discarded. Our models
  47. * do not include latency information, so connection startup time (time to
  48. * first byte) is considered part of the download time. Because of this, we
  49. * should ignore very small downloads which would cause our estimate to be
  50. * too low.
  51. * This specific value is based on experimentation.
  52. *
  53. * @private {number}
  54. * @const
  55. */
  56. this.minBytes_ = 16e3; // 16kB
  57. }
  58. /**
  59. * Takes a bandwidth sample.
  60. *
  61. * @param {number} durationMs The amount of time, in milliseconds, for a
  62. * particular request.
  63. * @param {number} numBytes The total number of bytes transferred in that
  64. * request.
  65. */
  66. sample(
  67. durationMs, numBytes) {
  68. if (numBytes < this.minBytes_) {
  69. return;
  70. }
  71. const bandwidth = 8000 * numBytes / durationMs;
  72. const weight = durationMs / 1000;
  73. this.bytesSampled_ += numBytes;
  74. this.fast_.sample(weight, bandwidth);
  75. this.slow_.sample(weight, bandwidth);
  76. }
  77. /**
  78. * Gets the current bandwidth estimate.
  79. *
  80. * @param {number} defaultEstimate
  81. * @return {number} The bandwidth estimate in bits per second.
  82. */
  83. getBandwidthEstimate(defaultEstimate) {
  84. if (this.bytesSampled_ < this.minTotalBytes_) {
  85. return defaultEstimate;
  86. }
  87. // Take the minimum of these two estimates. This should have the effect
  88. // of adapting down quickly, but up more slowly.
  89. return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
  90. }
  91. /**
  92. * @return {boolean} True if there is enough data to produce a meaningful
  93. * estimate.
  94. */
  95. hasGoodEstimate() {
  96. return this.bytesSampled_ >= this.minTotalBytes_;
  97. }
  98. };