Files
shaka-player/lib/media/buffering_observer.js
T
Joey Parrish f539147d48 fix: Correct license headers in compiled output
This fixes all the license headers in the main library, which corrects
the appearance of the main license in the compiled output.

It seems that the `!` in the header forces the compiler to keep it in
the output.  I believe older compiler releases did this purely based
on `@license`.

Issue #2638

Change-Id: I7f0e918caad10c9af689c9d07672b7fe9be7b2f3
2020-06-09 16:05:09 -07:00

108 lines
3.0 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.media.BufferingObserver');
/**
* The buffering observer watches how much content has been buffered and raises
* events when the state changes (enough => not enough or vice versa).
*
* @final
*/
shaka.media.BufferingObserver = class {
/**
* @param {number} thresholdWhenStarving
* @param {number} thresholdWhenSatisfied
*/
constructor(thresholdWhenStarving, thresholdWhenSatisfied) {
const State = shaka.media.BufferingObserver.State;
/** @private {shaka.media.BufferingObserver.State} */
this.previousState_ = State.SATISFIED;
/** @private {!Map.<shaka.media.BufferingObserver.State, number>} */
this.thresholds_ = new Map()
.set(State.SATISFIED, thresholdWhenSatisfied)
.set(State.STARVING, thresholdWhenStarving);
}
/**
* @param {number} thresholdWhenStarving
* @param {number} thresholdWhenSatisfied
*/
setThresholds(thresholdWhenStarving, thresholdWhenSatisfied) {
const State = shaka.media.BufferingObserver.State;
this.thresholds_
.set(State.SATISFIED, thresholdWhenSatisfied)
.set(State.STARVING, thresholdWhenStarving);
}
/**
* Update the observer by telling it how much content has been buffered (in
* seconds) and if we are buffered to the end of the presentation. If the
* controller believes the state has changed, it will return |true|.
*
* @param {number} bufferLead
* @param {boolean} bufferedToEnd
* @return {boolean}
*/
update(bufferLead, bufferedToEnd) {
const State = shaka.media.BufferingObserver.State;
/**
* Our threshold for how much we need before we declare ourselves as
* starving is based on whether or not we were just starving. If we
* were just starving, we are more likely to starve again, so we require
* more content to be buffered than if we were not just starving.
*
* @type {number}
*/
const threshold = this.thresholds_.get(this.previousState_);
const oldState = this.previousState_;
const newState = (bufferedToEnd || bufferLead >= threshold) ?
(State.SATISFIED) :
(State.STARVING);
// Save the new state now so that calls to |getState| from any callbacks
// will be accurate.
this.previousState_ = newState;
// Return |true| only when the state has changed.
return oldState != newState;
}
/**
* Set which state that the observer should think playback was in.
*
* @param {shaka.media.BufferingObserver.State} state
*/
setState(state) {
this.previousState_ = state;
}
/**
* Get the state that the observer last thought playback was in.
*
* @return {shaka.media.BufferingObserver.State}
*/
getState() {
return this.previousState_;
}
};
/**
* Rather than using booleans to communicate what state we are in, we have this
* enum.
*
* @enum {number}
*/
shaka.media.BufferingObserver.State = {
STARVING: 0,
SATISFIED: 1,
};