mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-26 17:46:26 +03:00
1284dd484b
Before the buffering observer was a playhead observer, but with supporting src=, changing how we interact with the buffering observer was made easier if it was handled as a top-level component. This meant moving it off the playhead observer interface and create its own timer in player. Coming off the playehead observer interface, the buffering observer did not need to use callbacks, which made it easier to use. This will also allow us to use it as the single source of "is buffering" in a later change. Change-Id: I8cad9bfde3309de7c2b8301b90aa8c40b6e4d247
108 lines
3.2 KiB
JavaScript
108 lines
3.2 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
};
|