Skip to content

Commit

Permalink
feat(FEC-8209): handle MEDIA_ERR_DECODE errors (#60)
Browse files Browse the repository at this point in the history
Add error handling for html5 video media error MEDIA_ERR_DECODE
  • Loading branch information
OrenMe authored Jun 14, 2018
1 parent df02d4b commit 9cc68bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/hls-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
* @private
*/
_onLoadedMetadataCallback: ?Function;
/**
* Reference to _onVideoError function
* @member {?Function} - _onVideoErrorCallback
* @type {?Function}
* @private
*/
_onVideoErrorCallback: ?Function;

/**
* Reference to _onRecoveredCallback function
Expand Down Expand Up @@ -210,6 +217,24 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
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();
this._onVideoErrorCallback = (e) => this._onVideoError(e);
this._videoElement.addEventListener(EventType.ERROR, this._onVideoErrorCallback);
}

/**
* video error event handler.
* @param {Event} event - the media error event
* @private
* @returns {void}
*/
_onVideoError(event: Event): void {
if ((event.currentTarget instanceof HTMLMediaElement) && (event.currentTarget.error instanceof MediaError)) {
const mediaError = event.currentTarget.error;
if (mediaError.code === mediaError.MEDIA_ERR_DECODE) {
HlsAdapter._logger.debug("The video playback was aborted due to a corruption problem or because the video used features your browser did not support.", mediaError.message);
this._handleMediaError();
}
}
}

/**
Expand Down Expand Up @@ -274,6 +299,18 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
this._resolveLoad({tracks: this._playerTracks});
}

/**
* Remove the error listener
* @private
* @returns {void}
*/
_removeVideoErrorListener(): void {
if (this._onVideoErrorCallback) {
this._videoElement.removeEventListener(EventType.ERROR, this._onVideoErrorCallback);
this._onVideoErrorCallback = null;
}
}

/**
* Remove the loadedmetadata listener
* @private
Expand Down Expand Up @@ -759,6 +796,8 @@ export default class HlsAdapter extends BaseMediaSourceAdapter {
this._hls.off(Hlsjs.Events.LEVEL_SWITCHED, this._onLevelSwitched);
this._hls.off(Hlsjs.Events.AUDIO_TRACK_SWITCHED, this._onAudioTrackSwitched);
this._removeRecoveredCallbackListener();
this._removeVideoErrorListener();
this._removeLoadedMetadataListener();
}

/**
Expand Down
9 changes: 3 additions & 6 deletions test/src/hls-adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ describe('HlsAdapter Instance - Unit', function () {
it('should parse all hls tracks into player tracks', function () {
hlsAdapterInstance._videoElement = {
textTracks: hls_tracks.subtitles,
removeEventListener: function () {
}
removeEventListener: () => {}
};
hlsAdapterInstance._hls = {
audioTracks: hls_tracks.audioTracks,
Expand All @@ -214,8 +213,7 @@ describe('HlsAdapter Instance - Unit', function () {
it('should disable all text tracks', function () {
hlsAdapterInstance._videoElement = {
textTracks: hls_tracks.subtitles,
removeEventListener: function () {
}
removeEventListener: () => {}
};
hlsAdapterInstance._disableAllTextTracks();
for (let i = 0; i < hlsAdapterInstance._videoElement.textTracks.length; i++) {
Expand All @@ -226,8 +224,7 @@ describe('HlsAdapter Instance - Unit', function () {
it('should hide the active text track', function () {
hlsAdapterInstance._videoElement = {
textTracks: hls_tracks.subtitles,
removeEventListener: function () {
}
removeEventListener: () => {}
};
hlsAdapterInstance._videoElement.textTracks[0].mode = 'showing';
hlsAdapterInstance.hideTextTrack();
Expand Down

0 comments on commit 9cc68bd

Please sign in to comment.