Skip to content

Commit

Permalink
ABR controller isofunctional code readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tchakabam committed Apr 23, 2018
1 parent b747aab commit c3a1e22
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/controller/abr-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ class AbrController extends EventHandler {
}

onFragLoading (data) {
let frag = data.frag;
const hls = this.hls;
const config = this.hls.config;
const frag = data.frag;

let ewmaFast, ewmaSlow;

if (frag.type === 'main') {
if (!this.timer) {
this.timer = setInterval(this.onCheck, 100);
Expand All @@ -40,12 +45,9 @@ class AbrController extends EventHandler {
// lazy init of bw Estimator, rationale is that we use different params for Live/VoD
// so we need to wait for stream manifest / playlist type to instantiate it.
if (!this._bwEstimator) {
let hls = this.hls,
level = data.frag.level,
isLive = hls.levels[level].details.live,
config = hls.config,
ewmaFast, ewmaSlow;
let level = data.frag.level;

const isLive = hls.levels[level].details.live;
if (isLive) {
ewmaFast = config.abrEwmaFastLive;
ewmaSlow = config.abrEwmaSlowLive;
Expand All @@ -55,8 +57,9 @@ class AbrController extends EventHandler {
}
this._bwEstimator = new EwmaBandWidthEstimator(hls, ewmaSlow, ewmaFast, config.abrEwmaDefaultEstimate);
}
this.fragCurrent = frag;
this._bwEstimator = new EwmaBandWidthEstimator(hls, ewmaSlow, ewmaFast, config.abrEwmaDefaultEstimate);
}
this.fragCurrent = frag;
}

_abandonRulesCheck () {
Expand All @@ -65,7 +68,16 @@ class AbrController extends EventHandler {
we compute expected time of arrival of the complete fragment.
we compare it to expected time of buffer starvation
*/
let hls = this.hls, v = hls.media, frag = this.fragCurrent, loader = frag.loader, minAutoLevel = hls.minAutoLevel;
const hls = this.hls;
const video = hls.media;
const frag = this.fragCurrent;

if (!frag) {
return;
}

const loader = frag.loader;
const minAutoLevel = hls.minAutoLevel;

// if loader has been destroyed or loading has been aborted, stop timer and return
if (!loader || (loader.stats && loader.stats.aborted)) {
Expand All @@ -78,9 +90,9 @@ class AbrController extends EventHandler {
let stats = loader.stats;
/* only monitor frag retrieval time if
(video not paused OR first fragment being loaded(ready state === HAVE_NOTHING = 0)) AND autoswitching enabled AND not lowest level (=> means that we have several levels) */
if (v && stats && ((!v.paused && (v.playbackRate !== 0)) || !v.readyState) && frag.autoLevel && frag.level) {
if (video && stats && ((!video.paused && (video.playbackRate !== 0)) || !video.readyState) && frag.autoLevel && frag.level) {
let requestDelay = performance.now() - stats.trequest,
playbackRate = Math.abs(v.playbackRate);
playbackRate = Math.abs(video.playbackRate);
// monitor fragment load progress after half of expected fragment duration,to stabilize bitrate
if (requestDelay > (500 * frag.duration / playbackRate)) {
let levels = hls.levels,
Expand All @@ -89,9 +101,9 @@ class AbrController extends EventHandler {
level = levels[frag.level],
levelBitrate = level.realBitrate ? Math.max(level.realBitrate, level.bitrate) : level.bitrate,
expectedLen = stats.total ? stats.total : Math.max(stats.loaded, Math.round(frag.duration * levelBitrate / 8)),
pos = v.currentTime,
pos = video.currentTime,
fragLoadedDelay = (expectedLen - stats.loaded) / loadRate,
bufferStarvationDelay = (BufferHelper.bufferInfo(v, pos, hls.config.maxBufferHole).end - pos) / playbackRate;
bufferStarvationDelay = (BufferHelper.bufferInfo(video, pos, hls.config.maxBufferHole).end - pos) / playbackRate;
// consider emergency switch down only if we have less than 2 frag buffered AND
// time to finish loading current fragment is bigger than buffer starvation delay
// ie if we risk buffer starvation if bw does not increase quickly
Expand Down Expand Up @@ -216,16 +228,16 @@ class AbrController extends EventHandler {
}
get _nextABRAutoLevel () {
let hls = this.hls, maxAutoLevel = hls.maxAutoLevel, levels = hls.levels, config = hls.config, minAutoLevel = hls.minAutoLevel;
const v = hls.media,
const video = hls.media,
currentLevel = this.lastLoadedFragLevel,
currentFragDuration = this.fragCurrent ? this.fragCurrent.duration : 0,
pos = (v ? v.currentTime : 0),
// playbackRate is the absolute value of the playback rate; if v.playbackRate is 0, we use 1 to load as
pos = (video ? video.currentTime : 0),
// playbackRate is the absolute value of the playback rate; if video.playbackRate is 0, we use 1 to load as
// if we're playing back at the normal rate.
playbackRate = ((v && (v.playbackRate !== 0)) ? Math.abs(v.playbackRate) : 1.0),
playbackRate = ((video && (video.playbackRate !== 0)) ? Math.abs(video.playbackRate) : 1.0),
avgbw = this._bwEstimator ? this._bwEstimator.getEstimate() : config.abrEwmaDefaultEstimate,
// bufferStarvationDelay is the wall-clock time left until the playback buffer is exhausted.
bufferStarvationDelay = (BufferHelper.bufferInfo(v, pos, config.maxBufferHole).end - pos) / playbackRate;
bufferStarvationDelay = (BufferHelper.bufferInfo(video, pos, config.maxBufferHole).end - pos) / playbackRate;

// First, look to see if we can find a level matching with our avg bandwidth AND that could also guarantee no rebuffering at all
let bestLevel = this._findBestLevel(currentLevel, currentFragDuration, avgbw, minAutoLevel, maxAutoLevel, bufferStarvationDelay, config.abrBandWidthFactor, config.abrBandWidthUpFactor, levels);
Expand Down

0 comments on commit c3a1e22

Please sign in to comment.