playable
Version:
Video player based on HTML5Video
100 lines • 4.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var constants_1 = require("../../constants");
var SEEK_BY_UI_EVENTS = [
constants_1.UIEvent.GO_FORWARD_WITH_KEYBOARD,
constants_1.UIEvent.GO_BACKWARD_WITH_KEYBOARD,
constants_1.UIEvent.PROGRESS_CHANGE,
];
var LiveStateEngine = /** @class */ (function () {
function LiveStateEngine(_a) {
var eventEmitter = _a.eventEmitter, engine = _a.engine;
this._eventEmitter = eventEmitter;
this._engine = engine;
this._state = constants_1.LiveState.NONE;
this._isSeekedByUIWhilePlaying = null;
this._bindEvents();
}
Object.defineProperty(LiveStateEngine.prototype, "state", {
get: function () {
return this._state;
},
enumerable: false,
configurable: true
});
LiveStateEngine.prototype._bindEvents = function () {
var _this = this;
this._unbindEvents = this._eventEmitter.bindEvents((0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([
[constants_1.VideoEvent.STATE_CHANGED, this._processStateChange]
], SEEK_BY_UI_EVENTS.map(function (eventName) { return [eventName, _this._processSeekByUI]; }), true), [
[constants_1.VideoEvent.DYNAMIC_CONTENT_ENDED, this._onDynamicContentEnded],
], false), this);
};
LiveStateEngine.prototype._processStateChange = function (_a) {
var prevState = _a.prevState, nextState = _a.nextState;
if (nextState === constants_1.EngineState.SRC_SET) {
this._setState(constants_1.LiveState.NONE);
return;
}
if (!this._engine.isDynamicContent || this._engine.isDynamicContentEnded) {
return;
}
switch (nextState) {
case constants_1.EngineState.METADATA_LOADED:
this._setState(constants_1.LiveState.INITIAL);
break;
case constants_1.EngineState.PLAY_REQUESTED:
if (this._state === constants_1.LiveState.INITIAL) {
this._engine.syncWithLive();
}
break;
case constants_1.EngineState.PLAYING:
// NOTE: skip PLAYING event after events like `WAITING` and other not important events.
if (this._state === constants_1.LiveState.INITIAL ||
this._state === constants_1.LiveState.NOT_SYNC ||
this._isSeekedByUIWhilePlaying) {
this._setState(this._engine.isSyncWithLive ? constants_1.LiveState.SYNC : constants_1.LiveState.NOT_SYNC);
this._isSeekedByUIWhilePlaying = false;
}
break;
case constants_1.EngineState.PAUSED:
// NOTE: process `PAUSED` event only `PLAYING`, to be sure its not related with `WAITING` events
if (prevState === constants_1.EngineState.PLAYING) {
this._setState(constants_1.LiveState.NOT_SYNC);
}
break;
default:
break;
}
};
LiveStateEngine.prototype._processSeekByUI = function () {
if (this._engine.isDynamicContent &&
this._engine.getCurrentState() === constants_1.EngineState.PLAYING) {
// NOTE: flag should be handled on `PLAYING` state in `_processStateChange`
this._isSeekedByUIWhilePlaying = true;
}
};
LiveStateEngine.prototype._onDynamicContentEnded = function () {
this._setState(constants_1.LiveState.ENDED);
};
LiveStateEngine.prototype._setState = function (state) {
if (this._state !== state) {
var prevState = this._state;
var nextState = state;
this._state = state;
this._eventEmitter.emitAsync(constants_1.VideoEvent.LIVE_STATE_CHANGED, {
prevState: prevState,
nextState: nextState,
});
}
};
LiveStateEngine.prototype.destroy = function () {
this._unbindEvents();
};
LiveStateEngine.moduleName = 'liveStateEngine';
LiveStateEngine.dependencies = ['eventEmitter', 'engine'];
return LiveStateEngine;
}());
exports.default = LiveStateEngine;
//# sourceMappingURL=live-state-engine.js.map