Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(FEC-9523): add attach detach implemention #21

Merged
merged 15 commits into from
Dec 30, 2019
59 changes: 45 additions & 14 deletions src/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Flash extends FakeEventTarget implements IEngine {
* @type {Object}
* @private
*/
_config: ?Object = null;
_config: Object;

/**
* Promise when load finished
Expand Down Expand Up @@ -75,7 +75,20 @@ class Flash extends FakeEventTarget implements IEngine {
*/
_eventManager: EventManager = null;

_srcToLoad: ?string = null;
_source: PKMediaSourceObject;
/**
* The last time detach occurred
* @type {number}
* @private
*/
_lastTimeDetach: ?number = NaN;
Yuvalke marked this conversation as resolved.
Show resolved Hide resolved

/**
* The start time after attach
* @type {number}
* @private
*/
_startTimeAttach: ?number = NaN;

/**
* The state of player mute
Expand Down Expand Up @@ -182,12 +195,22 @@ class Flash extends FakeEventTarget implements IEngine {
constructor(source: PKMediaSourceObject, config: Object) {
super();
this._el = Utils.Dom.createElement('div');
this._init(source, config);
this.init(source, config);
}

attachMediaSource(): void {}
attachMediaSource(): void {
this.init(this._source, this._config);
this._startTimeAttach = this._lastTimeDetach;
Yuvalke marked this conversation as resolved.
Show resolved Hide resolved
this._lastTimeDetach = null;
}

detachMediaSource(): void {}
detachMediaSource(): void {
this._lastTimeDetach = this.currentTime;
const {_source, _config} = this;
this.destroy();
Utils.Object.mergeDeep(this, {_source, _config});
this._loadPromise = null;
}

hideTextTrack(): void {}

Expand All @@ -197,26 +220,29 @@ class Flash extends FakeEventTarget implements IEngine {

exitPictureInPicture(): void {}

_init(source: PKMediaSourceObject, config: Object): void {
init(source: PKMediaSourceObject, config: Object): void {
this._eventManager = new EventManager();
this._config = config;
this._source = source;
if (this._el) {
this._api = new FlashHLSAdapter(source, config, this._el);
this._api.attach();
this._addBindings();
this._srcToLoad = source.url;
}
}

reset(): void {
if (this._api) {
this._api.reset();
}
if (this._eventManager) {
this._eventManager.removeAll();
}

this._src = null;
this._config = null;
this._volume = null;
this._volumeBeforeMute = null;
this._srcToLoad = null;
this._muted = this.defaultMuted;
Yuvalke marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -227,7 +253,7 @@ class Flash extends FakeEventTarget implements IEngine {
*/
restore(source: PKMediaSourceObject, config: Object): void {
this.destroy();
this._init(source, config);
this.init(source, config);
}

/**
Expand Down Expand Up @@ -282,6 +308,7 @@ class Flash extends FakeEventTarget implements IEngine {
this._api.destroy();
this._eventManager.destroy();
this.reset();
this._api = null;
}
}

Expand All @@ -302,8 +329,11 @@ class Flash extends FakeEventTarget implements IEngine {
EventType.SEEKING,
EventType.SEEKED,
EventType.ENDED,
EventType.TEXT_CUE_CHANGED,
EventType.VIDEO_TRACK_CHANGED,
EventType.AUDIO_TRACK_CHANGED,
EventType.ABORT,
EventType.EMPTIED,
EventType.DURATION_CHANGE
];
events.forEach(eventName => {
Expand Down Expand Up @@ -392,15 +422,15 @@ class Flash extends FakeEventTarget implements IEngine {
* @public
*/
get src(): string {
if (this._src) {
if (this._loadPromise && this._src) {
return this._src;
}
return '';
}

/**
* Load media.
* @param {number} startTime - Optional time to start the video from.
* @param {?number} startTime - Optional time to start the video from.
* @public
* @returns {Promise<Object>} - The loaded data
*/
Expand All @@ -409,8 +439,9 @@ class Flash extends FakeEventTarget implements IEngine {
Flash._logger.warn('Missing API - Flash is not ready');
return Promise.reject('Flash is not ready');
}
this._src = this._srcToLoad;
this._loadPromise = this._api.load(startTime);
this._src = this._source ? this._source.url : null;
const playbackStartTime = this._startTimeAttach || startTime || 0;
Yuvalke marked this conversation as resolved.
Show resolved Hide resolved
this._loadPromise = this._api.load(playbackStartTime);
return this._loadPromise;
}

Expand Down
16 changes: 14 additions & 2 deletions src/flashhls-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class FlashHLSAdapter extends FakeEventTarget {
_el: HTMLDivElement;
_api: FlashAPI;
_src: PKMediaSourceObject;
_startTime: number;
_startTime: ?number;
_firstPlay: boolean = true;
_initialVolume: number;
_initialVolume: ?number;
_loadReported: boolean = false;
paused: boolean = true;
ended: boolean = false;
Expand Down Expand Up @@ -92,6 +92,13 @@ class FlashHLSAdapter extends FakeEventTarget {
if (this._el && this._el.parentNode) {
this._el.innerHTML = '';
}
//simulate the event sequence like video tag
Yuvalke marked this conversation as resolved.
Show resolved Hide resolved
this._trigger(EventType.ABORT);
this._trigger(EventType.EMPTIED);
//to hide the text tracks simulate event like happened in hls.js
this._trigger(EventType.TEXT_CUE_CHANGED, {cues: []});
this._trigger(EventType.TIME_UPDATE);
this.reset();
}

attach(): HTMLDivElement {
Expand Down Expand Up @@ -333,8 +340,13 @@ class FlashHLSAdapter extends FakeEventTarget {
this.ended = false;
this.seeking = false;
this.duration = null;
this.currentTime = null;
this.buffer = null;
this.watched = null;
this._startTime = null;
this._firstPlay = true;
this._initialVolume = null;
this._loadReported = false;
}
}

Expand Down