Skip to content

Commit

Permalink
* replace redundant logic in stream-ctrls by task-loop class
Browse files Browse the repository at this point in the history
* fix destroying of event-handlers
  • Loading branch information
tchakabam committed Nov 28, 2017
1 parent a510136 commit 4cf1d33
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 82 deletions.
38 changes: 10 additions & 28 deletions src/controller/audio-stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as LevelHelper from '../helper/level-helper';import TimeRanges from '..
import {ErrorTypes, ErrorDetails} from '../errors';
import {logger} from '../utils/logger';
import { findFragWithCC } from '../utils/discontinuities';
import TaskLoop from '../task-loop';

const State = {
STOPPED : 'STOPPED',
Expand All @@ -29,7 +30,7 @@ const State = {
WAITING_INIT_PTS : 'WAITING_INIT_PTS'
};

class AudioStreamController extends EventHandler {
class AudioStreamController extends TaskLoop {

constructor(hls) {
super(hls,
Expand All @@ -52,21 +53,17 @@ class AudioStreamController extends EventHandler {

this.config = hls.config;
this.audioCodecSwap = false;
this.ticks = 0;
this._state = State.STOPPED;
this.ontick = this.tick.bind(this);
this.initPTS=[];
this.waitingFragment=null;
this.initPTS = [];
this.waitingFragment = null;
this.videoTrackCC = null;
}

destroy() {
_onDestroying() {
this.stopLoad();
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
EventHandler.prototype.destroy.call(this);
}

_onDestroyed() {
this.state = State.STOPPED;
}

Expand All @@ -92,9 +89,7 @@ class AudioStreamController extends EventHandler {
if (this.tracks) {
var lastCurrentTime = this.lastCurrentTime;
this.stopLoad();
if (!this.timer) {
this.timer = setInterval(this.ontick, 100);
}
this.setInterval(100);
this.fragLoadError = 0;
if (lastCurrentTime > 0 && startPosition === -1) {
logger.log(`audio:override startPosition with lastCurrentTime @${lastCurrentTime.toFixed(3)}`);
Expand Down Expand Up @@ -139,17 +134,6 @@ class AudioStreamController extends EventHandler {
return this._state;
}

tick() {
this.ticks++;
if (this.ticks === 1) {
this.doTick();
if (this.ticks > 1) {
setTimeout(this.tick, 1);
}
this.ticks = 0;
}
}

doTick() {
var pos, track, trackDetails, hls = this.hls, config = hls.config;
//logger.log('audioStream:' + this.state);
Expand Down Expand Up @@ -503,9 +487,7 @@ class AudioStreamController extends EventHandler {
}
} else {
// switching to audio track, start timer if not already started
if (!this.timer) {
this.timer = setInterval(this.ontick, 100);
}
this.setInterval(100);
}

//should we switch tracks ?
Expand Down
6 changes: 3 additions & 3 deletions src/controller/level-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LevelController extends EventHandler {
this.timer = null;
}

destroy() {
_onDestroying() {
this.cleanTimer();
this._manualLevel = -1;
}
Expand Down Expand Up @@ -318,15 +318,15 @@ class LevelController extends EventHandler {

const {sidxInfo} = data;
if (sidxInfo) {

console.log('Level controller received SIDX info:', sidxInfo);

sidxInfo.references.forEach((segmentRef, index) => {

const segRefInfo = segmentRef.info;

const frag = this.levels[this.level].details.fragments[index];

if(frag.byteRange.length === 0) {
frag.rawByteRange = String(segRefInfo.end - segRefInfo.start) + '@' + String(segRefInfo.start);
}
Expand Down
33 changes: 8 additions & 25 deletions src/controller/stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as LevelHelper from '../helper/level-helper';import TimeRanges from '..
import {ErrorTypes, ErrorDetails} from '../errors';
import {logger} from '../utils/logger';
import { alignDiscontinuities } from '../utils/discontinuities';
import TaskLoop from '../task-loop';

const State = {
STOPPED : 'STOPPED',
Expand All @@ -26,7 +27,7 @@ const State = {
ERROR : 'ERROR'
};

class StreamController extends EventHandler {
class StreamController extends TaskLoop {

constructor(hls) {
super(hls,
Expand All @@ -50,28 +51,22 @@ class StreamController extends EventHandler {

this.config = hls.config;
this.audioCodecSwap = false;
this.ticks = 0;
this._state = State.STOPPED;
this.ontick = this.tick.bind(this);
}

destroy() {
_onDestroying() {
this.stopLoad();
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
EventHandler.prototype.destroy.call(this);
}

_onDestroyed() {
this.state = State.STOPPED;
}

startLoad(startPosition) {
if (this.levels) {
let lastCurrentTime = this.lastCurrentTime, hls = this.hls;
this.stopLoad();
if (!this.timer) {
this.timer = setInterval(this.ontick, 100);
}
this.setInterval(100);
this.level = -1;
this.fragLoadError = 0;
if (!this.startFragRequested) {
Expand Down Expand Up @@ -118,19 +113,8 @@ class StreamController extends EventHandler {
this.forceStartLoad = false;
}

tick() {
const MAX_TICK_RE_ENTRY = 0;
if (this.ticks > MAX_TICKS_RE_ENTRY) {
this.ticks++;
this.doTick();
this.ticks--;
} else {
this.ticks = 0;
setTimeout(this.tick.bind(this), 0);
}
}

doTick() {

switch(this.state) {
case State.ERROR:
//don't do anything in error state to avoid breaking further ...
Expand Down Expand Up @@ -182,7 +166,6 @@ class StreamController extends EventHandler {
config = hls.config,
media = this.media;


// if start level not parsed yet OR
// if video not attached AND start fragment already requested OR start frag prefetch disable
// exit loop, as we either need more info (level not parsed) or we need media to be attached to load new fragment
Expand Down
19 changes: 4 additions & 15 deletions src/controller/subtitle-stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Event from '../events';
import EventHandler from '../event-handler';
import {logger} from '../utils/logger';
import Decrypter from '../crypt/decrypter';
import TaskLoop from '../task-loop';

const State = {
STOPPED : 'STOPPED',
Expand All @@ -14,7 +15,7 @@ const State = {
FRAG_LOADING : 'FRAG_LOADING'
};

class SubtitleStreamController extends EventHandler {
class SubtitleStreamController extends TaskLoop {

constructor(hls) {
super(hls,
Expand All @@ -26,18 +27,17 @@ class SubtitleStreamController extends EventHandler {
Event.SUBTITLE_TRACK_SWITCH,
Event.SUBTITLE_TRACK_LOADED,
Event.SUBTITLE_FRAG_PROCESSED);

this.config = hls.config;
this.vttFragSNsProcessed = {};
this.vttFragQueues = undefined;
this.currentlyProcessing = null;
this.state = State.STOPPED;
this.currentTrackId = -1;
this.ticks = 0;
this.decrypter = new Decrypter(hls.observer, hls.config);
}

destroy() {
EventHandler.prototype.destroy.call(this);
_onDestroyed() {
this.state = State.STOPPED;
}

Expand Down Expand Up @@ -86,17 +86,6 @@ class SubtitleStreamController extends EventHandler {
}
}

tick() {
this.ticks++;
if (this.ticks === 1) {
this.doTick();
if (this.ticks > 1) {
setTimeout(() => { this.tick(); }, 1);
}
this.ticks = 0;
}
}

doTick() {
switch(this.state) {
case State.IDLE:
Expand Down
5 changes: 5 additions & 0 deletions src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class EventHandler {
}

destroy() {
this._onDestroying();
this.unregisterListeners();
this._onDestroyed();
}

_onDestroying() {}
_onDestroyed() {}

isEventHandler() {
return typeof this.handledEvents === 'object' && this.handledEvents.length && typeof this.onEvent === 'function';
}
Expand Down
29 changes: 18 additions & 11 deletions src/task-loop.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import EventHandler from './event-handler';

const MAX_TICK_RE_ENTRY = 0;
const MAX_TICKS_RE_ENTRY = 0;

export class TaskLoop extends EventHandler {
export default class TaskLoop extends EventHandler {

constructor(hls, ...events) {
super(hls, ...events);

this._tickInterval = null;
this._ticks = 0;
}

/**
* @override
*/
destroy() {
this.clearInterval();

EventHandler.prototype.destroy.call(this);
super.destroy();
}

/**
Expand All @@ -26,14 +26,21 @@ export class TaskLoop extends EventHandler {
return !isNaN(this._tickInterval);
}

setInterval() {
/**
* @param {number} millis Interval time (ms)
* @returns {boolean} True when interval has been scheduled, false when already scheduled (no effect)
*/
setInterval(millis) {
if (!this._tickInterval) {
this._tickInterval = setInterval(this.tick.bind(this));
this._tickInterval = setInterval(this.tick.bind(this, false), millis);
return true;
}
return false;
}

/**
* @returns {boolean} True when interval was cleared, false when none was set (no effect)
*/
clearInterval() {
if (this._tickInterval) {
clearInterval(this._tickInterval);
Expand All @@ -47,14 +54,14 @@ export class TaskLoop extends EventHandler {
* @param {Wether to force async} forceAsync
* @returns {boolean} True when async, false when sync
*/
tick(forceAsync = false) {
if (this.ticks > MAX_TICKS_RE_ENTRY && !forceAsync) {
this.ticks++;
tick(forceAsync = true) {
if (this._ticks > MAX_TICKS_RE_ENTRY || !forceAsync) {
this._ticks++;
this.doTick();
this.ticks--;
this._ticks--;
return false;
} else {
this.ticks = 0;
this._ticks = 0;
setTimeout(this.tick.bind(this), 0);
return true;
}
Expand Down

0 comments on commit 4cf1d33

Please sign in to comment.