Skip to content

Commit

Permalink
filter and shift cues
Browse files Browse the repository at this point in the history
  • Loading branch information
SivanA-Kaltura committed Nov 10, 2024
1 parent 3c2e365 commit c1f9ca0
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 setCuesTimeRangeStart(cueRangeStart: number): void {
this._externalCaptionsHandler?.setCuesTimeRangeStart(cueRangeStart);
}

public setCuesTimeRangeEnd(cueRangeEnd: number): void {
this._externalCaptionsHandler?.setCuesTimeRangeEnd(cueRangeEnd);
}
}
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 start time
*/
private _cuesTimeRangeStart = -1;
/**
* when normalizing cues, this will be the new end time
*/
private _cuesTimeRangeEnd = -1;

/**
* constructor
Expand Down Expand Up @@ -289,6 +297,8 @@ class ExternalCaptionsHandler extends FakeEventTarget {
* @returns {void}
*/
public reset(): void {
this._cuesTimeRangeStart = -1;
this._cuesTimeRangeEnd = -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 setCuesTimeRangeStart(cuesTimeRangeStart: number): void {
this._cuesTimeRangeStart = cuesTimeRangeStart;
}

public setCuesTimeRangeEnd(cuesTimeRangeEnd: number): void {
this._cuesTimeRangeEnd = cuesTimeRangeEnd;
}

/**
* Translate the cues between [cue seek range start, cue seek range end] 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._cuesTimeRangeStart === -1 && this._cuesTimeRangeEnd === -1) return cuesArray;

const cuesTimeRangeStart = this._cuesTimeRangeStart === -1 ? 0 : this._cuesTimeRangeStart;
const cuesTimeRangeEnd = this._cuesTimeRangeEnd === -1 ? undefined : this._cuesTimeRangeEnd;

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

return filteredCues;
}
}

export {ExternalCaptionsHandler};

0 comments on commit c1f9ca0

Please sign in to comment.