Skip to content

Commit

Permalink
fix(FEC-8233): handle media & error recovering (#61)
Browse files Browse the repository at this point in the history
If the media recovered successfully, trigger media recovered event
  • Loading branch information
odedhutzler authored and OrenMe committed Jun 14, 2018
1 parent 3aa9feb commit df02d4b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
37 changes: 35 additions & 2 deletions src/hls-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
*/
_onLoadedMetadataCallback: ?Function;

/**
* Reference to _onRecoveredCallback function
* @member {?Function} - _onRecoveredCallback
* @type {?Function}
* @private
*/
_onRecoveredCallback: ?Function;

/**
* Factory method to create media source adapter.
* @function createAdapter
Expand Down Expand Up @@ -191,7 +199,7 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
}

/**
* Adds the required bindings with hls.js.
* Adds the required bindings locally and with hls.js.
* @function _addBindings
* @private
* @returns {void}
Expand All @@ -201,6 +209,7 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
this._hls.on(Hlsjs.Events.MANIFEST_LOADED, this._onManifestLoaded.bind(this));
this._hls.on(Hlsjs.Events.LEVEL_SWITCHED, this._onLevelSwitched.bind(this));
this._hls.on(Hlsjs.Events.AUDIO_TRACK_SWITCHED, this._onAudioTrackSwitched.bind(this));
this._onRecoveredCallback = () => this._onRecovered();
}

/**
Expand Down Expand Up @@ -277,6 +286,18 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
}
}

/**
* Remove the loadedmetadata listener, when recovering from media error.
* @private
* @returns {void}
*/
_removeRecoveredCallbackListener(): void {
if (this._onRecoveredCallback) {
this._videoElement.removeEventListener(EventType.LOADED_METADATA, this._onRecoveredCallback);
this._onRecoveredCallback = null;
}
}

/**
* Destroys the hls adapter.
* @function destroy
Expand Down Expand Up @@ -669,9 +690,11 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
const now: number = performance.now();
let recover = true;
if (this._checkTimeDeltaHasPassed(now, this._recoverDecodingErrorDate, this._config.recoverDecodingErrorDelay)) {
this._videoElement.addEventListener(EventType.LOADED_METADATA, this._onRecoveredCallback);
this._recoverDecodingError();
} else {
if (this._checkTimeDeltaHasPassed(now, this._recoverSwapAudioCodecDate, this._config.recoverSwapAudioCodecDelay)) {
this._videoElement.addEventListener(EventType.LOADED_METADATA, this._onRecoveredCallback);
this._recoverSwapAudioCodec();
} else {
recover = false;
Expand All @@ -681,6 +704,16 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
return recover;
}

/**
* trigger mediarecovered event if metadata is loaded (means the recovery succeeded)
* @returns {void}
* @private
*/
_onRecovered(): void {
this._trigger(EventType.MEDIA_RECOVERED);
this._videoElement.removeEventListener(EventType.LOADED_METADATA, this._onRecoveredCallback);
}

/**
* Check if time ahs passed a certain delta
* @param {number} now - current time
Expand Down Expand Up @@ -725,7 +758,7 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
this._hls.off(Hlsjs.Events.ERROR, this._onError);
this._hls.off(Hlsjs.Events.LEVEL_SWITCHED, this._onLevelSwitched);
this._hls.off(Hlsjs.Events.AUDIO_TRACK_SWITCHED, this._onAudioTrackSwitched);
this._removeLoadedMetadataListener();
this._removeRecoveredCallbackListener();
}

/**
Expand Down
12 changes: 9 additions & 3 deletions test/src/hls-adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ describe('HlsAdapter Instance - Unit', function () {

it('should parse all hls tracks into player tracks', function () {
hlsAdapterInstance._videoElement = {
textTracks: hls_tracks.subtitles
textTracks: hls_tracks.subtitles,
removeEventListener: function () {
}
};
hlsAdapterInstance._hls = {
audioTracks: hls_tracks.audioTracks,
Expand All @@ -211,7 +213,9 @@ describe('HlsAdapter Instance - Unit', function () {

it('should disable all text tracks', function () {
hlsAdapterInstance._videoElement = {
textTracks: hls_tracks.subtitles
textTracks: hls_tracks.subtitles,
removeEventListener: function () {
}
};
hlsAdapterInstance._disableAllTextTracks();
for (let i = 0; i < hlsAdapterInstance._videoElement.textTracks.length; i++) {
Expand All @@ -221,7 +225,9 @@ describe('HlsAdapter Instance - Unit', function () {

it('should hide the active text track', function () {
hlsAdapterInstance._videoElement = {
textTracks: hls_tracks.subtitles
textTracks: hls_tracks.subtitles,
removeEventListener: function () {
}
};
hlsAdapterInstance._videoElement.textTracks[0].mode = 'showing';
hlsAdapterInstance.hideTextTrack();
Expand Down
8 changes: 4 additions & 4 deletions test/src/json/player_tracks.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
"_bandwidth": 2962000,
"_active": false,
"_label": "Main",
"_label": "720p",
"_language": "",
"_index": 0,
"_height": 720,
Expand All @@ -35,7 +35,7 @@
{
"_bandwidth": 1427000,
"_active": true,
"_label": "Main",
"_label": "432p",
"_language": "",
"_index": 1,
"_height": 432,
Expand All @@ -44,7 +44,7 @@
{
"_bandwidth": 688000,
"_active": false,
"_label": "Main",
"_label": "252p",
"_language": "",
"_index": 2,
"_height": 252,
Expand All @@ -53,7 +53,7 @@
{
"_bandwidth": 331000,
"_active": false,
"_label": "Main",
"_label": "160p",
"_language": "",
"_index": 3,
"_height": 160,
Expand Down

0 comments on commit df02d4b

Please sign in to comment.