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: CEA708 subtitle doesn't get correct index on native #514

Merged
merged 8 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/engines/html5/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ export default class Html5 extends FakeEventTarget implements IEngine {
this._handleVideoError();
});
this._handleMetadataTrackEvents();
this._eventManager.listen(this._el.textTracks, 'addtrack', (event: any) => {
if (event.track.kind === 'captions' || event.track.kind === 'subtitles') {
this.dispatchEvent(new FakeEvent(CustomEventType.TEXT_TRACK_ADDED, {track: event.track}));
}
});
let mediaSourceAdapter = this._mediaSourceAdapter;
if (mediaSourceAdapter) {
this._eventManager.listen(mediaSourceAdapter, CustomEventType.VIDEO_TRACK_CHANGED, (event: FakeEvent) => this.dispatchEvent(event));
Expand Down
4 changes: 4 additions & 0 deletions src/event/event-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ const CustomEventType: PKEventTypes = {
* Fires when the active text track has been changed.
*/
TEXT_TRACK_CHANGED: 'texttrackchanged',
/**
* Fires when the text track added to native
*/
TEXT_TRACK_ADDED: 'texttrackadded',
/**
* Fires when the active text track cue has changed.
*/
Expand Down
45 changes: 24 additions & 21 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,27 @@ 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to avoid accessing the native video element as much as possible, for example, this will fail for flash and youtube or any other external non HTML5 engine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OrenMe why? get video element will be null and then nothing happens on the next check, otherwise, text tracks will be empty and if both are not empty for other engines it's ok that the logic will work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but this logic was not needed till now for non - native text track, so what has changed?
We should strive to get away from this and not add more use cases.

Copy link
Contributor Author

@Yuvalke Yuvalke Dec 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flash and youtube won't raise this event anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's for native text track only, listener added only for useNativeTextTrack=true

const trackIndex = videoElement
Yuvalke marked this conversation as resolved.
Show resolved Hide resolved
? 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 @@ -2113,33 +2134,15 @@ export default class Player extends FakeEventTarget {
*/
_updateTracks(tracks: Array<Track>): void {
Player._logger.debug('Tracks changed', tracks);
if (this.config.playback.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();
this._maybeAdjustTextTracksIndexes();
this._setDefaultTracks();
}

/**
* If we added external tracks to the video element, we might need to adjust the text tracks indexes between the video
* element and the players tracks list
* @returns {void}
* @private
*/
_maybeAdjustTextTracksIndexes(): void {
if (this._config.playback.useNativeTextTrack) {
const getNativeLanguageTrackIndex = (textTrack: TextTrack): number => {
const videoElement = this.getVideoElement();
return videoElement ? Array.from(videoElement.textTracks).findIndex(track => (track ? track.language === textTrack.language : false)) : -1;
};
const textTracks = this._getTextTracks();
let externalTrackIndex = textTracks.length;
textTracks.forEach(track => {
track.index = track.external ? externalTrackIndex++ : getNativeLanguageTrackIndex(track);
});
}
}

/**
* Returns the tracks according to a type.
* @function _getTextTracks
Expand Down