From 236dacb53bfc511ce3144e621b5f572f644e245d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Thu, 8 Jun 2023 22:03:59 +0200 Subject: [PATCH] feat: Improve live latency on load (#5268) With this change the latency is reduced in 50ms approx. Very important for LL streams. --- lib/media/playhead.js | 2 +- lib/media/video_wrapper.js | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/media/playhead.js b/lib/media/playhead.js index 9cad395943..b788dcd095 100644 --- a/lib/media/playhead.js +++ b/lib/media/playhead.js @@ -251,7 +251,7 @@ shaka.media.MediaSourcePlayhead = class { this.videoWrapper_ = new shaka.media.VideoWrapper( mediaElement, () => this.onSeeking_(), - this.getStartTime_(startTime)); + () => this.getStartTime_(startTime)); /** @type {shaka.util.Timer} */ this.checkWindowTimer_ = new shaka.util.Timer(() => { diff --git a/lib/media/video_wrapper.js b/lib/media/video_wrapper.js index 6ddc5e0346..67f49ad6df 100644 --- a/lib/media/video_wrapper.js +++ b/lib/media/video_wrapper.js @@ -27,17 +27,25 @@ shaka.media.VideoWrapper = class { /** * @param {!HTMLMediaElement} video * @param {function()} onSeek Called when the video seeks. - * @param {number} startTime The time to start at. + * @param {function():number} getStartTime Calle to get the time to start at. */ - constructor(video, onSeek, startTime) { + constructor(video, onSeek, getStartTime) { /** @private {HTMLMediaElement} */ this.video_ = video; /** @private {function()} */ this.onSeek_ = onSeek; - /** @private {number} */ - this.startTime_ = startTime; + /** @private {?number} */ + this.startTime_ = null; + + /** @private {function():number} */ + this.getStartTime_ = () => { + if (this.startTime_ == null) { + this.startTime_ = getStartTime(); + } + return this.startTime_; + }; /** @private {boolean} */ this.started_ = false; @@ -58,7 +66,7 @@ shaka.media.VideoWrapper = class { HTMLMediaElement.HAVE_METADATA, this.eventManager_, () => { - this.setStartTime_(this.startTime_); + this.setStartTime_(this.getStartTime_()); }); } @@ -86,7 +94,7 @@ shaka.media.VideoWrapper = class { * @return {number} */ getTime() { - return this.started_ ? this.video_.currentTime : this.startTime_; + return this.started_ ? this.video_.currentTime : this.getStartTime_(); } @@ -103,7 +111,7 @@ shaka.media.VideoWrapper = class { HTMLMediaElement.HAVE_METADATA, this.eventManager_, () => { - this.setStartTime_(this.startTime_); + this.setStartTime_(this.getStartTime_()); }); } }