@svta/common-media-library
Version:
A common library for media playback in JavaScript
50 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EwmaEstimator = void 0;
const getBandwidthBps_js_1 = require("../utils/getBandwidthBps.js");
const Ewma_js_1 = require("./Ewma.js");
/**
* Exponential Weighted Moving Average (EWMA) throughput estimator based on 2 half-lives
*
* @group Throughput
*
* @beta
*/
class EwmaEstimator {
constructor(options) {
this.slowEwma = new Ewma_js_1.Ewma(options.slowHalfLife);
this.fastEwma = new Ewma_js_1.Ewma(options.fastHalfLife);
}
sample(sample) {
// TODO: shaka
// 1. If `sample.encodedBodySize` is less than `this.minBytes_`, don't do anything
// TODO: dash.js
// 1. If `sample.encodedBodySize` is NaN or Infinity, don't do anything
// TODO: hls.js
// 1. If `durationSeconds` is less than `this.minDelayMs_`, make it `this.minDelayMs_` (default is 50ms)
const durationSeconds = sample.duration / 1000;
const bandwidthBps = (0, getBandwidthBps_js_1.getBandwidthBps)(sample);
this.slowEwma.sample(durationSeconds, bandwidthBps);
this.fastEwma.sample(durationSeconds, bandwidthBps);
}
getEstimate() {
// TODO: shaka
// 1. Returns `defaultEstimate` for this.totalBytes < `options.minTotalBytes`
// 1.1. Returns `options.defaultBandwidthEstimate`
// 1.2. or `navigator.connection.downlink * 1e6` if available
// TODO: dash.js
// 1. Returns `NaN` for this.totalDuration <= 0
// 2. `Math.max` for latency calculation
// TODO: hls.js
// 1. Returns `defaultEstimate` for this.totalDuration < `this.minDuration`
// 1.1. Returns `config.abrEwmaDefaultEstimate` (default: 500 kbps)
return Math.min(this.fastEwma.getEstimate(), this.slowEwma.getEstimate());
}
canEstimate() {
// TODO: shaka, `this.totalBytes >= options.minTotalBytes`
// TODO: hls.js, `this.totalDuration >= this.minDuration`
return true;
}
}
exports.EwmaEstimator = EwmaEstimator;
//# sourceMappingURL=EwmaEstimator.js.map