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. */
  43. this.minTotalBytes_ = 128e3; // 128kB
  44. /**
  45. * Minimum number of bytes, under which samples are discarded. Our models
  46. * do not include latency information, so connection startup time (time to
  47. * first byte) is considered part of the download time. Because of this, we
  48. * should ignore very small downloads which would cause our estimate to be
  49. * too low.
  50. * This specific value is based on experimentation.
  51. *
  52. * @private {number}
  53. */
  54. this.minBytes_ = 16e3; // 16kB
  55. }
  56. /**
  57. * Called by the Player to provide an updated configuration any time it
  58. * changes.
  59. * Must be called at least once before init().
  60. *
  61. * @param {shaka.extern.AdvancedAbrConfiguration} config
  62. */
  63. configure(config) {
  64. this.minTotalBytes_ = config.minTotalBytes;
  65. this.minBytes_ = config.minBytes;
  66. this.fast_.updateAlpha(config.fastHalfLife);
  67. this.slow_.updateAlpha(config.slowHalfLife);
  68. }
  69. /**
  70. * Takes a bandwidth sample.
  71. *
  72. * @param {number} durationMs The amount of time, in milliseconds, for a
  73. * particular request.
  74. * @param {number} numBytes The total number of bytes transferred in that
  75. * request.
  76. */
  77. sample(
  78. durationMs, numBytes) {
  79. if (numBytes < this.minBytes_) {
  80. return;
  81. }
  82. const bandwidth = 8000 * numBytes / durationMs;
  83. const weight = durationMs / 1000;
  84. this.bytesSampled_ += numBytes;
  85. this.fast_.sample(weight, bandwidth);
  86. this.slow_.sample(weight, bandwidth);
  87. }
  88. /**
  89. * Gets the current bandwidth estimate.
  90. *
  91. * @param {number} defaultEstimate
  92. * @return {number} The bandwidth estimate in bits per second.
  93. */
  94. getBandwidthEstimate(defaultEstimate) {
  95. if (this.bytesSampled_ < this.minTotalBytes_) {
  96. return defaultEstimate;
  97. }
  98. // Take the minimum of these two estimates. This should have the effect
  99. // of adapting down quickly, but up more slowly.
  100. return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
  101. }
  102. /**
  103. * @return {boolean} True if there is enough data to produce a meaningful
  104. * estimate.
  105. */
  106. hasGoodEstimate() {
  107. return this.bytesSampled_ >= this.minTotalBytes_;
  108. }
  109. };