Skip to content

Commit

Permalink
Use ES6 setter for StreamController state + comment in Event
Browse files Browse the repository at this point in the history
  • Loading branch information
tchakabam committed May 11, 2016
1 parent 6ef0def commit f0d575f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
73 changes: 38 additions & 35 deletions src/controller/stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class StreamController extends EventHandler {
this.timer = null;
}
EventHandler.prototype.destroy.call(this);
this.updateState(State.STOPPED);
this.state = State.STOPPED;
}

startLoad(startPosition=0) {
Expand All @@ -77,16 +77,16 @@ class StreamController extends EventHandler {
logger.log('resuming video');
media.play();
}
this.updateState(State.IDLE);
this.state = State.IDLE;
} else {
this.lastCurrentTime = this.startPosition ? this.startPosition : startPosition;
this.updateState(State.STARTING);
this.state = State.STARTING;
}
this.nextLoadPosition = this.startPosition = this.lastCurrentTime;
this.tick();
} else {
logger.warn('cannot start loading as manifest not parsed yet');
this.updateState(State.STOPPED);
this.state = State.STOPPED;
}
}

Expand All @@ -103,7 +103,7 @@ class StreamController extends EventHandler {
this.demuxer.destroy();
this.demuxer = null;
}
this.updateState(State.STOPPED);
this.state = State.STOPPED;
}

tick() {
Expand All @@ -118,7 +118,6 @@ class StreamController extends EventHandler {
}

doTick() {
//logger.log(this.state);
switch(this.state) {
case State.STARTING:
var hls = this.hls;
Expand All @@ -131,7 +130,7 @@ class StreamController extends EventHandler {
}
// set new level to playlist loader : this will trigger start level load
this.level = hls.nextLoadLevel = this.startLevel;
this.updateState(State.WAITING_LEVEL);
this.state = State.WAITING_LEVEL;
this.loadedmetadata = false;
break;
case State.IDLE:
Expand All @@ -145,7 +144,7 @@ class StreamController extends EventHandler {
var level = this.levels[this.level];
// check if playlist is already loaded
if (level && level.details) {
this.updateState(State.IDLE);
this.state = State.IDLE;
}
break;
case State.FRAG_LOADING_WAITING_RETRY:
Expand All @@ -156,7 +155,7 @@ class StreamController extends EventHandler {
// if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading
if(!retryDate || (now >= retryDate) || isSeeking) {
logger.log(`mediaController: retryDate reached, switch back to IDLE state`);
this.updateState(State.IDLE);
this.state = State.IDLE;
}
break;
case State.ERROR:
Expand Down Expand Up @@ -239,7 +238,7 @@ class StreamController extends EventHandler {
// if live playlist, ensure that new playlist has been refreshed to avoid loading/try to load
// a useless and outdated fragment (that might even introduce load error if it is already out of the live playlist)
if (typeof levelDetails === 'undefined' || levelDetails.live && this.levelLastLoaded !== level) {
this.updateState(State.WAITING_LEVEL);
this.state = State.WAITING_LEVEL;
return true;
}

Expand Down Expand Up @@ -364,7 +363,7 @@ class StreamController extends EventHandler {
// is currently processing a seek command and waiting for new data to resume at another point.
// Going to ended state while media is seeking can spawn an infinite buffering broken state.
if (!this.media.seeking) {
this.updateState(State.ENDED);
this.state = State.ENDED;
}
frag = null;
}
Expand All @@ -375,7 +374,7 @@ class StreamController extends EventHandler {
//logger.log('loading frag ' + i +',pos/bufEnd:' + pos.toFixed(3) + '/' + bufferEnd.toFixed(3));
if ((frag.decryptdata.uri != null) && (frag.decryptdata.key == null)) {
logger.log(`Loading key for ${frag.sn} of [${levelDetails.startSN} ,${levelDetails.endSN}],level ${level}`);
this.updateState(State.KEY_LOADING);
this.state = State.KEY_LOADING;
hls.trigger(Event.KEY_LOADING, {frag: frag});
} else {
logger.log(`Loading ${frag.sn} of [${levelDetails.startSN} ,${levelDetails.endSN}],level ${level}, currentTime:${pos},bufferEnd:${bufferEnd.toFixed(3)}`);
Expand Down Expand Up @@ -405,18 +404,22 @@ class StreamController extends EventHandler {
this.fragCurrent = frag;
this.startFragRequested = true;
hls.trigger(Event.FRAG_LOADING, {frag: frag});
this.updateState(State.FRAG_LOADING);
this.state = State.FRAG_LOADING;
}
}
}

updateState(state) {
this.state = state;
if (this.previousState !== this.state) {
logger.log(`engine state transition from ${this.previousState} to ${this.state}`);
this.hls.trigger(State.STREAM_STATE_TRANSITION, {previousState: this.previousState, state: this.state});
set state(nextState) {
if (this.state !== nextState) {
const previousState = this.state;
this._state = nextState;
logger.log(`engine state transition from ${previousState} to ${nextState}`);
this.hls.trigger(State.STREAM_STATE_TRANSITION, {previousState, nextState});
}
this.previousState = state;
}

get state() {
return this._state;
}

getBufferRange(position) {
Expand Down Expand Up @@ -535,7 +538,7 @@ class StreamController extends EventHandler {
this.fragCurrent = null;
// increase fragment load Index to avoid frag loop loading error after buffer flush
this.fragLoadIdx += 2 * this.config.fragLoadingLoopThreshold;
this.updateState(State.PAUSED);
this.state = State.PAUSED;
// flush everything
this.hls.trigger(Event.BUFFER_FLUSHING, {startOffset: 0, endOffset: Number.POSITIVE_INFINITY});
}
Expand Down Expand Up @@ -572,7 +575,7 @@ class StreamController extends EventHandler {
if (currentRange && currentRange.start > 1) {
// flush buffer preceding current fragment (flush until current fragment start offset)
// minus 1s to avoid video freezing, that could happen if we flush keyframe of current video ...
this.updateState(State.PAUSED);
this.state = State.PAUSED;
this.hls.trigger(Event.BUFFER_FLUSHING, {startOffset: 0, endOffset: currentRange.start - 1});
}
if (!media.paused) {
Expand Down Expand Up @@ -600,7 +603,7 @@ class StreamController extends EventHandler {
}
this.fragCurrent = null;
// flush position is the start position of this new buffer
this.updateState(State.PAUSED);
this.state = State.PAUSED;
this.hls.trigger(Event.BUFFER_FLUSHING, {startOffset: nextRange.start, endOffset: Number.POSITIVE_INFINITY});
}
}
Expand Down Expand Up @@ -667,11 +670,11 @@ class StreamController extends EventHandler {
}
this.fragPrevious = null;
// switch to IDLE state to load new fragment
this.updateState(State.IDLE);
this.state = State.IDLE;
}
} else if (this.state === State.ENDED) {
// switch to IDLE state to check for potential new fragment
this.updateState(State.IDLE);
this.state = State.IDLE;
}
if (this.media) {
this.lastCurrentTime = this.media.currentTime;
Expand Down Expand Up @@ -774,15 +777,15 @@ class StreamController extends EventHandler {
}
// only switch batck to IDLE state if we were waiting for level to start downloading a new fragment
if (this.state === State.WAITING_LEVEL) {
this.updateState(State.IDLE);
this.state = State.IDLE;
}
//trigger handler right now
this.tick();
}

onKeyLoaded() {
if (this.state === State.KEY_LOADING) {
this.updateState(State.IDLE);
this.state = State.IDLE;
this.tick();
}
}
Expand All @@ -795,12 +798,12 @@ class StreamController extends EventHandler {
data.frag.sn === fragCurrent.sn) {
if (this.fragBitrateTest === true) {
// switch back to IDLE state ... we just loaded a fragment to determine adequate start bitrate and initialize autoswitch algo
this.updateState(State.IDLE);
this.state = State.IDLE;
this.fragBitrateTest = false;
data.stats.tparsed = data.stats.tbuffered = performance.now();
this.hls.trigger(Event.FRAG_BUFFERED, {stats: data.stats, frag: fragCurrent});
} else {
this.updateState(State.PARSING);
this.state = State.PARSING;
// transmux the MPEG-TS data to ISO-BMFF segments
this.stats = data.stats;
var currentLevel = this.levels[this.level],
Expand Down Expand Up @@ -946,7 +949,7 @@ class StreamController extends EventHandler {
onFragParsed() {
if (this.state === State.PARSING) {
this.stats.tparsed = performance.now();
this.updateState(State.PARSED);
this.state = State.PARSED;
this._checkAppendedParsed();
}
}
Expand All @@ -973,7 +976,7 @@ class StreamController extends EventHandler {
this.fragLastKbps = Math.round(8 * stats.length / (stats.tbuffered - stats.tfirst));
this.hls.trigger(Event.FRAG_BUFFERED, {stats: stats, frag: frag});
logger.log(`media buffered : ${this.timeRangesToString(this.media.buffered)}`);
this.updateState(State.IDLE);
this.state = State.IDLE;
}
this.tick();
}
Expand All @@ -999,13 +1002,13 @@ class StreamController extends EventHandler {
logger.warn(`mediaController: frag loading failed, retry in ${delay} ms`);
this.retryDate = performance.now() + delay;
// retry loading state
this.updateState(State.FRAG_LOADING_WAITING_RETRY);
this.state = State.FRAG_LOADING_WAITING_RETRY;
} else {
logger.error(`mediaController: ${data.details} reaches max retry, redispatch as fatal ...`);
// redispatch same error but with fatal set to true
data.fatal = true;
this.hls.trigger(Event.ERROR, data);
this.updateState(State.ERROR);
this.state = State.ERROR;
}
}
break;
Expand All @@ -1017,7 +1020,7 @@ class StreamController extends EventHandler {
// when in ERROR state, don't switch back to IDLE state in case a non-fatal error is received
if(this.state !== State.ERROR) {
// if fatal error, stop processing, otherwise move to IDLE to retry loading
this.updateState(data.fatal ? State.ERROR : State.IDLE);
this.state = data.fatal ? State.ERROR : State.IDLE;
logger.warn(`mediaController: ${data.details} while loading frag,switch to ${this.state} state ...`);
}
break;
Expand Down Expand Up @@ -1120,7 +1123,7 @@ _checkBuffer() {
}

onFragLoadEmergencyAborted() {
this.updateState(State.IDLE);
this.state = State.IDLE;
this.tick();
}

Expand All @@ -1143,7 +1146,7 @@ _checkBuffer() {
this.immediateLevelSwitchEnd();
}
// move to IDLE once flush complete. this should trigger new fragment loading
this.updateState(State.IDLE);
this.state = State.IDLE;
// reset reference to frag
this.fragPrevious = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ module.exports = {
KEY_LOADING: 'hlsKeyLoading',
// fired when a decrypt key loading is completed - data: { frag : fragment object, payload : key payload, stats : { trequest, tfirst, tload, length}}
KEY_LOADED: 'hlsKeyLoaded',
// fired upon stream controller state transitions
// fired upon stream controller state transitions - data: {previousState, nextState}
STREAM_STATE_TRANSITION: 'hlsStreamStateTransition'
};

0 comments on commit f0d575f

Please sign in to comment.