From c1f9ca01d5275fc8ac190cf3e398aab8a6db74f3 Mon Sep 17 00:00:00 2001 From: SivanA-Kaltura <88330203+SivanA-Kaltura@users.noreply.github.com> Date: Sun, 10 Nov 2024 16:59:45 +0200 Subject: [PATCH 1/3] filter and shift cues --- src/player.ts | 8 +++++ src/track/external-captions-handler.ts | 45 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/player.ts b/src/player.ts index 13697590..5adcbd2a 100644 --- a/src/player.ts +++ b/src/player.ts @@ -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); + } } diff --git a/src/track/external-captions-handler.ts b/src/track/external-captions-handler.ts index 53f25764..7c434846 100644 --- a/src/track/external-captions-handler.ts +++ b/src/track/external-captions-handler.ts @@ -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 @@ -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(); @@ -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(); @@ -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}; From e962019fff25ff2957699e5fd2529b9a6d6a4971 Mon Sep 17 00:00:00 2001 From: SivanA-Kaltura <88330203+SivanA-Kaltura@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:58:45 +0200 Subject: [PATCH 2/3] rename --- src/player.ts | 8 +++--- src/track/external-captions-handler.ts | 34 +++++++++++++------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/player.ts b/src/player.ts index 5adcbd2a..bedf033e 100644 --- a/src/player.ts +++ b/src/player.ts @@ -2918,11 +2918,11 @@ export default class Player extends FakeEventTarget { } } - public setCuesTimeRangeStart(cueRangeStart: number): void { - this._externalCaptionsHandler?.setCuesTimeRangeStart(cueRangeStart); + public setSeekFrom(seekFrom: number): void { + this._externalCaptionsHandler?.setSeekFrom(seekFrom); } - public setCuesTimeRangeEnd(cueRangeEnd: number): void { - this._externalCaptionsHandler?.setCuesTimeRangeEnd(cueRangeEnd); + public setClipTo(clipTo: number): void { + this._externalCaptionsHandler?.setClipTo(clipTo); } } diff --git a/src/track/external-captions-handler.ts b/src/track/external-captions-handler.ts index 7c434846..c50b9c72 100644 --- a/src/track/external-captions-handler.ts +++ b/src/track/external-captions-handler.ts @@ -86,13 +86,13 @@ class ExternalCaptionsHandler extends FakeEventTarget { */ private _lastTimeUpdate: number = 0; /** - * when normalizing cues, this will be the new start time + * when normalizing cues, this will be the new cue time range start time */ - private _cuesTimeRangeStart = -1; + private _seekFrom = -1; /** - * when normalizing cues, this will be the new end time + * when normalizing cues, this will be the new cue time range end time */ - private _cuesTimeRangeEnd = -1; + private _clipTo = -1; /** * constructor @@ -297,8 +297,8 @@ class ExternalCaptionsHandler extends FakeEventTarget { * @returns {void} */ public reset(): void { - this._cuesTimeRangeStart = -1; - this._cuesTimeRangeEnd = -1; + this._seekFrom = -1; + this._clipTo = -1; this._resetCurrentTrack(); this._textTrackModel = {} as VTTCue; this._resetExternalNativeTextTrack(); @@ -618,31 +618,31 @@ class ExternalCaptionsHandler extends FakeEventTarget { return cuePoints.filter((cp: any) => cp.startTime >= seekFrom && (!clipTo || cp.startTime < clipTo)); } - public setCuesTimeRangeStart(cuesTimeRangeStart: number): void { - this._cuesTimeRangeStart = cuesTimeRangeStart; + public setSeekFrom(seekFrom: number): void { + this._seekFrom = seekFrom; } - public setCuesTimeRangeEnd(cuesTimeRangeEnd: number): void { - this._cuesTimeRangeEnd = cuesTimeRangeEnd; + public setClipTo(clipTo: number): void { + this._clipTo = clipTo; } /** - * Translate the cues between [cue seek range start, cue seek range end] into a time range that starts from 0. + * 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._cuesTimeRangeStart === -1 && this._cuesTimeRangeEnd === -1) return cuesArray; + if (this._seekFrom === -1 && this._clipTo === -1) return cuesArray; - const cuesTimeRangeStart = this._cuesTimeRangeStart === -1 ? 0 : this._cuesTimeRangeStart; - const cuesTimeRangeEnd = this._cuesTimeRangeEnd === -1 ? undefined : this._cuesTimeRangeEnd; + const seekFrom = this._seekFrom === -1 ? 0 : this._seekFrom; + const clipTo = this._clipTo === -1 ? undefined : this._clipTo; - const filteredCues = this._filterCuePointsOutOfVideoRange(cuesArray, cuesTimeRangeStart, cuesTimeRangeEnd); + const filteredCues = this._filterCuePointsOutOfVideoRange(cuesArray, seekFrom, clipTo); filteredCues.forEach((cp: any) => { - cp.startTime = cp.startTime - cuesTimeRangeStart; - cp.endTime = cp.endTime - cuesTimeRangeStart; + cp.startTime = cp.startTime - seekFrom; + cp.endTime = cp.endTime - seekFrom; }); return filteredCues; From cad0346bff5917eddb936f8d537b03c4bbe6b30f Mon Sep 17 00:00:00 2001 From: SivanA-Kaltura <88330203+SivanA-Kaltura@users.noreply.github.com> Date: Mon, 11 Nov 2024 11:36:16 +0200 Subject: [PATCH 3/3] review fix --- src/player.ts | 4 ++-- src/track/external-captions-handler.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/player.ts b/src/player.ts index bedf033e..25743cef 100644 --- a/src/player.ts +++ b/src/player.ts @@ -2919,10 +2919,10 @@ export default class Player extends FakeEventTarget { } public setSeekFrom(seekFrom: number): void { - this._externalCaptionsHandler?.setSeekFrom(seekFrom); + this._externalCaptionsHandler.seekFrom = seekFrom; } public setClipTo(clipTo: number): void { - this._externalCaptionsHandler?.setClipTo(clipTo); + this._externalCaptionsHandler.clipTo = clipTo; } } diff --git a/src/track/external-captions-handler.ts b/src/track/external-captions-handler.ts index c50b9c72..1f70abdd 100644 --- a/src/track/external-captions-handler.ts +++ b/src/track/external-captions-handler.ts @@ -618,11 +618,11 @@ class ExternalCaptionsHandler extends FakeEventTarget { return cuePoints.filter((cp: any) => cp.startTime >= seekFrom && (!clipTo || cp.startTime < clipTo)); } - public setSeekFrom(seekFrom: number): void { + public set seekFrom(seekFrom: number) { this._seekFrom = seekFrom; } - public setClipTo(clipTo: number): void { + public set clipTo(clipTo: number) { this._clipTo = clipTo; }