Skip to content

Commit

Permalink
fix(SUP-37490): Align captions with clipTo/SeekFrom Values in PlayMan…
Browse files Browse the repository at this point in the history
…ifest
  • Loading branch information
SivanA-Kaltura authored Nov 11, 2024
1 parent 3c2e365 commit 8f24f03
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
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};

0 comments on commit 8f24f03

Please sign in to comment.