2016-06-01 14:04:22 -04:00
|
|
|
/*
|
|
|
|
* EWMA Bandwidth Estimator
|
|
|
|
* - heavily inspired from shaka-player
|
|
|
|
* Tracks bandwidth samples and estimates available bandwidth.
|
|
|
|
* Based on the minimum of two exponentially-weighted moving averages with
|
|
|
|
* different half-lives.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EWMA from '../utils/ewma';
|
|
|
|
|
|
|
|
|
|
|
|
class EwmaBandWidthEstimator {
|
|
|
|
|
2016-06-25 02:04:53 -04:00
|
|
|
constructor(hls,slow,fast) {
|
2016-06-01 14:04:22 -04:00
|
|
|
this.hls = hls;
|
|
|
|
this.defaultEstimate_ = 5e5; // 500kbps
|
|
|
|
this.minWeight_ = 0.001;
|
|
|
|
this.minDelayMs_ = 50;
|
2016-06-25 02:04:53 -04:00
|
|
|
this.slow_ = new EWMA(slow);
|
|
|
|
this.fast_ = new EWMA(fast);
|
2016-06-01 14:04:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sample(durationMs,numBytes) {
|
|
|
|
durationMs = Math.max(durationMs, this.minDelayMs_);
|
2016-06-25 02:04:53 -04:00
|
|
|
var bandwidth = 8000* numBytes / durationMs,
|
2016-06-01 14:04:22 -04:00
|
|
|
//console.log('instant bw:'+ Math.round(bandwidth));
|
|
|
|
// we weight sample using loading duration....
|
2016-06-25 02:04:53 -04:00
|
|
|
weight = durationMs / 1000;
|
|
|
|
this.fast_.sample(weight,bandwidth);
|
|
|
|
this.slow_.sample(weight,bandwidth);
|
2016-06-01 14:04:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getEstimate() {
|
2016-06-17 09:11:34 -04:00
|
|
|
if (!this.fast_ || this.fast_.getTotalWeight() < this.minWeight_) {
|
2016-06-01 14:04:22 -04:00
|
|
|
return this.defaultEstimate_;
|
|
|
|
}
|
|
|
|
//console.log('slow estimate:'+ Math.round(this.slow_.getEstimate()));
|
|
|
|
//console.log('fast estimate:'+ Math.round(this.fast_.getEstimate()));
|
|
|
|
// Take the minimum of these two estimates. This should have the effect of
|
|
|
|
// adapting down quickly, but up more slowly.
|
|
|
|
return Math.min(this.fast_.getEstimate(),this.slow_.getEstimate());
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default EwmaBandWidthEstimator;
|
|
|
|
|