Skip to content

Commit

Permalink
fix(FEC-10879): Safari ignores explicit text track (#528)
Browse files Browse the repository at this point in the history
Issue: for the first selection safari doesn't fire the addTrack event yet to handle the change in the indexes(timing issue).
Solution: add native container to handle the text track by index decided once it parsed. (same as we did on hls adapter)
  • Loading branch information
Yuvalke authored Jan 7, 2021
1 parent 37bfb4f commit 8c9d503
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 32 deletions.
16 changes: 8 additions & 8 deletions src/engines/html5/media-source/adapters/native-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export default class NativeAdapter extends BaseMediaSourceAdapter {
* @private
*/
_startTimeAttach: number = NaN;
_nativeTextTracksMap = [];

/**
* Checks if NativeAdapter can play a given mime type.
Expand Down Expand Up @@ -530,6 +531,7 @@ export default class NativeAdapter extends BaseMediaSourceAdapter {
this._waitingEventTriggered = false;
this._progressiveSources = [];
this._loadPromise = null;
this._nativeTextTracksMap = [];
this._loadPromiseReject = null;
this._liveEdge = 0;
this._lastTimeUpdate = 0;
Expand Down Expand Up @@ -673,10 +675,12 @@ export default class NativeAdapter extends BaseMediaSourceAdapter {
};
if (settings.kind === 'subtitles') {
parsedTracks.push(new PKTextTrack(settings));
this._nativeTextTracksMap[settings.index] = textTracks[i];
} else if (settings.kind === 'captions' && this._config.enableCEA708Captions) {
settings.label = settings.label || captionsTextTrackLabels.shift();
settings.language = settings.language || captionsTextTrackLanguageCodes.shift();
parsedTracks.push(new PKTextTrack(settings));
this._nativeTextTracksMap[settings.index] = textTracks[i];
}
}
}
Expand Down Expand Up @@ -842,12 +846,9 @@ export default class NativeAdapter extends BaseMediaSourceAdapter {
* @public
*/
selectTextTrack(textTrack: PKTextTrack): void {
const textTracks = this._videoElement.textTracks;
if (textTrack instanceof PKTextTrack && (textTrack.kind === 'subtitles' || textTrack.kind === 'captions') && textTracks) {
if (textTrack instanceof PKTextTrack && (textTrack.kind === 'subtitles' || textTrack.kind === 'captions')) {
this._removeNativeTextTrackChangeListener();
const selectedTrack = Array.from(textTracks).find(
(track, index) => textTrack.index === index && track && (track.kind === 'subtitles' || track.kind === 'captions')
);
const selectedTrack = this._nativeTextTracksMap[textTrack.index];
if (selectedTrack) {
this._disableTextTracks();
selectedTrack.mode = this._getDisplayTextTrackModeString();
Expand Down Expand Up @@ -898,8 +899,8 @@ export default class NativeAdapter extends BaseMediaSourceAdapter {
const pkTextTracks = this._getPKTextTracks();
const pkOffTrack = pkTextTracks.find(track => track.language === 'off');
const getActiveVidTextTrackIndex = () => {
for (let i = 0; i < this._videoElement.textTracks.length; i++) {
const textTrack = this._videoElement.textTracks[i];
for (let i = 0; i < this._nativeTextTracksMap.length; i++) {
const textTrack = this._nativeTextTracksMap[i];
if (this._getDisplayTextTrackModeString() === textTrack.mode) {
return i;
}
Expand All @@ -910,7 +911,6 @@ export default class NativeAdapter extends BaseMediaSourceAdapter {
const vidIndex = getActiveVidTextTrackIndex();
const activePKtextTrack = this._getActivePKTextTrack();
const pkIndex = activePKtextTrack ? activePKtextTrack.index : -1;

if (vidIndex !== pkIndex) {
// In case no text track with 'showing' mode
// we need to set the off track
Expand Down
24 changes: 0 additions & 24 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,27 +1781,6 @@ export default class Player extends FakeEventTarget {
}
}

/**
* The text track changed event object
* @param {FakeEvent} event - payload with text track
* @returns {void}
* @private
*/
_onTextTrackAdded(event: FakeEvent): void {
const videoElement = this.getVideoElement();
const trackIndex = videoElement
? Array.from(videoElement.textTracks).findIndex(track => track && track.language === event.payload.track.language)
: -1;
if (trackIndex === 0) {
const textTracks = this._getTextTracks();
// new native track added to start or end of text track list so if it added to start we should fix our indexes
// by increasing the index by 1
textTracks.forEach(track => {
track.index = ++track.index;
});
}
}

/**
* The text track changed event object
* @param {FakeEvent} event - payload with text track
Expand Down Expand Up @@ -2182,9 +2161,6 @@ export default class Player extends FakeEventTarget {
*/
_updateTracks(tracks: Array<Track>): void {
Player._logger.debug('Tracks changed', tracks);
if (this.config.text.useNativeTextTrack) {
this._eventManager.listen(this._engine, CustomEventType.TEXT_TRACK_ADDED, (event: FakeEvent) => this._onTextTrackAdded(event));
}
this._tracks = tracks.concat(this._externalCaptionsHandler.getExternalTracks(tracks));
this._addTextTrackOffOption();
this._maybeSetTracksLabels();
Expand Down

0 comments on commit 8c9d503

Please sign in to comment.