Source: lib/media/buffering_observer.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.media.BufferingObserver');
  7. /**
  8. * The buffering observer watches how much content has been buffered and raises
  9. * events when the state changes (enough => not enough or vice versa).
  10. *
  11. * @final
  12. */
  13. shaka.media.BufferingObserver = class {
  14. /**
  15. * @param {number} thresholdWhenStarving
  16. * @param {number} thresholdWhenSatisfied
  17. */
  18. constructor(thresholdWhenStarving, thresholdWhenSatisfied) {
  19. const State = shaka.media.BufferingObserver.State;
  20. /** @private {shaka.media.BufferingObserver.State} */
  21. this.previousState_ = State.SATISFIED;
  22. /** @private {!Map.<shaka.media.BufferingObserver.State, number>} */
  23. this.thresholds_ = new Map()
  24. .set(State.SATISFIED, thresholdWhenSatisfied)
  25. .set(State.STARVING, thresholdWhenStarving);
  26. /** @private {number} */
  27. this.lastRebufferTime_ = 0;
  28. }
  29. /**
  30. * @param {number} thresholdWhenStarving
  31. * @param {number} thresholdWhenSatisfied
  32. */
  33. setThresholds(thresholdWhenStarving, thresholdWhenSatisfied) {
  34. const State = shaka.media.BufferingObserver.State;
  35. this.thresholds_
  36. .set(State.SATISFIED, thresholdWhenSatisfied)
  37. .set(State.STARVING, thresholdWhenStarving);
  38. }
  39. /**
  40. * Update the observer by telling it how much content has been buffered (in
  41. * seconds) and if we are buffered to the end of the presentation. If the
  42. * controller believes the state has changed, it will return |true|.
  43. *
  44. * @param {number} bufferLead
  45. * @param {boolean} bufferedToEnd
  46. * @return {boolean}
  47. */
  48. update(bufferLead, bufferedToEnd) {
  49. const State = shaka.media.BufferingObserver.State;
  50. /**
  51. * Our threshold for how much we need before we declare ourselves as
  52. * starving is based on whether or not we were just starving. If we
  53. * were just starving, we are more likely to starve again, so we require
  54. * more content to be buffered than if we were not just starving.
  55. *
  56. * @type {number}
  57. */
  58. const threshold = this.thresholds_.get(this.previousState_);
  59. const oldState = this.previousState_;
  60. const newState = (bufferedToEnd || bufferLead >= threshold) ?
  61. (State.SATISFIED) :
  62. (State.STARVING);
  63. // Save the new state now so that calls to |getState| from any callbacks
  64. // will be accurate.
  65. this.previousState_ = newState;
  66. // Return |true| only when the state has changed.
  67. const stateChanged = oldState != newState;
  68. if (stateChanged && newState === State.SATISFIED) {
  69. this.lastRebufferTime_ = Date.now();
  70. }
  71. return stateChanged;
  72. }
  73. /**
  74. * Set which state that the observer should think playback was in.
  75. *
  76. * @param {shaka.media.BufferingObserver.State} state
  77. */
  78. setState(state) {
  79. this.previousState_ = state;
  80. }
  81. /**
  82. * Get the state that the observer last thought playback was in.
  83. *
  84. * @return {shaka.media.BufferingObserver.State}
  85. */
  86. getState() {
  87. return this.previousState_;
  88. }
  89. /**
  90. * Return the last time that the state went from |STARVING| to |SATISFIED|.
  91. * @return {number}
  92. */
  93. getLastRebufferTime() {
  94. return this.lastRebufferTime_;
  95. }
  96. /**
  97. * Reset the last rebuffer time to zero.
  98. */
  99. resetLastRebufferTime() {
  100. this.lastRebufferTime_ = 0;
  101. }
  102. };
  103. /**
  104. * Rather than using booleans to communicate what state we are in, we have this
  105. * enum.
  106. *
  107. * @enum {number}
  108. */
  109. shaka.media.BufferingObserver.State = {
  110. STARVING: 0,
  111. SATISFIED: 1,
  112. };