From 3ac4c54c1a05ef499d9303e88aba88270d40cfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=CC=81lvaro=20Velad=20Galva=CC=81n?= Date: Thu, 30 Jan 2025 11:31:22 +0100 Subject: [PATCH 1/2] fix: Fixed displaying subtitles in UITextDisplayer at high playback rates --- lib/text/ui_text_displayer.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/text/ui_text_displayer.js b/lib/text/ui_text_displayer.js index b0217d37ed..167414c3c0 100644 --- a/lib/text/ui_text_displayer.js +++ b/lib/text/ui_text_displayer.js @@ -55,6 +55,9 @@ shaka.text.UITextDisplayer = class { /** @private {?number} */ this.aspectRatio_ = null; + /** @private {?shaka.extern.TextDisplayerConfiguration} */ + this.config_ = null; + /** @type {HTMLElement} */ this.textContainer_ = shaka.util.Dom.createHTMLElement('div'); this.textContainer_.classList.add('shaka-text-container'); @@ -77,7 +80,8 @@ shaka.text.UITextDisplayer = class { if (!this.video_.paused) { this.updateCaptions_(); } - }).tickEvery(/* seconds= */ 0.25); + }); + this.configureCaptionsTimer_(); /** * Maps cues to cue elements. Specifically points out the wrapper element of @@ -101,6 +105,10 @@ shaka.text.UITextDisplayer = class { this.updateCaptions_(/* forceUpdate= */ true); }); + this.eventManager_.listen(this.video_, 'ratechange', () => { + this.configureCaptionsTimer_(); + }); + // From: https://html.spec.whatwg.org/multipage/media.html#dom-video-videowidth // Whenever the natural width or natural height of the video changes // (including, for example, because the selected video track was changed), @@ -137,9 +145,8 @@ shaka.text.UITextDisplayer = class { * @export */ configure(config) { - if (this.captionsTimer_) { - this.captionsTimer_.tickEvery(config.captionsUpdatePeriod); - } + this.config_ = config; + this.configureCaptionsTimer_(); } @@ -186,6 +193,7 @@ shaka.text.UITextDisplayer = class { this.cues_ = []; if (this.captionsTimer_) { this.captionsTimer_.stop(); + this.captionsTimer_ = null; } this.currentCuesMap_.clear(); @@ -265,6 +273,19 @@ shaka.text.UITextDisplayer = class { enableTextDisplayer() { } + /** + * @private + */ + configureCaptionsTimer_() { + if (this.captionsTimer_) { + const captionsUpdatePeriod = this.config_ ? + this.config_.captionsUpdatePeriod : 0.25; + const updateTime = + captionsUpdatePeriod / Math.max(1, this.video_.playbackRate); + this.captionsTimer_.tickEvery(updateTime); + } + } + /** * @private */ From 01a8163f4a640f7ec7bcf662daa9e5c9fe07ddd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=CC=81lvaro=20Velad=20Galva=CC=81n?= Date: Thu, 30 Jan 2025 12:11:43 +0100 Subject: [PATCH 2/2] Add Math.abs --- lib/text/ui_text_displayer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/text/ui_text_displayer.js b/lib/text/ui_text_displayer.js index 167414c3c0..1fe4add63c 100644 --- a/lib/text/ui_text_displayer.js +++ b/lib/text/ui_text_displayer.js @@ -280,8 +280,8 @@ shaka.text.UITextDisplayer = class { if (this.captionsTimer_) { const captionsUpdatePeriod = this.config_ ? this.config_.captionsUpdatePeriod : 0.25; - const updateTime = - captionsUpdatePeriod / Math.max(1, this.video_.playbackRate); + const updateTime = captionsUpdatePeriod / + Math.max(1, Math.abs(this.video_.playbackRate)); this.captionsTimer_.tickEvery(updateTime); } }