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(SUP-37490): Align captions with clipTo/SeekFrom Values in PlayManifest #798

Merged
merged 3 commits into from
Nov 11, 2024
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
8 changes: 8 additions & 0 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2917,4 +2917,12 @@ export default class Player extends FakeEventTarget {
this._cachedUrls = [];
}
}

public setSeekFrom(seekFrom: number): void {
this._externalCaptionsHandler.seekFrom = seekFrom;
}

public setClipTo(clipTo: number): void {
this._externalCaptionsHandler.clipTo = clipTo;
}
}
45 changes: 45 additions & 0 deletions src/track/external-captions-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ class ExternalCaptionsHandler extends FakeEventTarget {
* @private
*/
private _lastTimeUpdate: number = 0;
/**
* when normalizing cues, this will be the new cue time range start time
*/
private _seekFrom = -1;
/**
* when normalizing cues, this will be the new cue time range end time
*/
private _clipTo = -1;

/**
* constructor
Expand Down Expand Up @@ -289,6 +297,8 @@ class ExternalCaptionsHandler extends FakeEventTarget {
* @returns {void}
*/
public reset(): void {
this._seekFrom = -1;
this._clipTo = -1;
this._resetCurrentTrack();
this._textTrackModel = {} as VTTCue;
this._resetExternalNativeTextTrack();
Expand Down Expand Up @@ -393,6 +403,7 @@ class ExternalCaptionsHandler extends FakeEventTarget {
return new Promise((resolve, reject) => {
this._getCuesString(textTrack)
.then(vttString => this._parseCues(vttString))
.then((cuesArray) => this._filterAndShiftCues(cuesArray))
.then(cuesArray => {
this._textTrackModel[textTrack.language].cues = cuesArray;
resolve();
Expand Down Expand Up @@ -602,6 +613,40 @@ class ExternalCaptionsHandler extends FakeEventTarget {
this._eventManager.listen(this._player, Html5EventType.TIME_UPDATE, () => this._handleCaptionOnTimeUpdate(textTrack));
}
}

private _filterCuePointsOutOfVideoRange(cuePoints: any[], seekFrom: number, clipTo: number | undefined): any[] {
return cuePoints.filter((cp: any) => cp.startTime >= seekFrom && (!clipTo || cp.startTime < clipTo));
}

public set seekFrom(seekFrom: number) {
this._seekFrom = seekFrom;
}

public set clipTo(clipTo: number) {
this._clipTo = clipTo;
}

/**
* Translate the cues between [seekFrom, clipTo] into a time range that starts from 0.
* @param {TextTrack} textTrack - text track to be set
* @returns {void}
* @private
*/
private _filterAndShiftCues(cuesArray: any[]): any[] {
if (!cuesArray || !cuesArray.length) return [];
if (this._seekFrom === -1 && this._clipTo === -1) return cuesArray;

const seekFrom = this._seekFrom === -1 ? 0 : this._seekFrom;
const clipTo = this._clipTo === -1 ? undefined : this._clipTo;

const filteredCues = this._filterCuePointsOutOfVideoRange(cuesArray, seekFrom, clipTo);
filteredCues.forEach((cp: any) => {
cp.startTime = cp.startTime - seekFrom;
cp.endTime = cp.endTime - seekFrom;
});

return filteredCues;
}
}

export {ExternalCaptionsHandler};
Loading