From 61497c65047981c6be262714e186b1313e5153f1 Mon Sep 17 00:00:00 2001 From: amtins Date: Tue, 11 Jun 2019 11:12:29 +0200 Subject: [PATCH] fix(videojs-dash): currentTime with DVR live stream --- src/js/videojs-dash.js | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/js/videojs-dash.js b/src/js/videojs-dash.js index 3160b7e9..07321016 100644 --- a/src/js/videojs-dash.js +++ b/src/js/videojs-dash.js @@ -254,6 +254,11 @@ class Html5DashJS { // Setup text tracks setupTextTracks.call(null, this.player, tech, options); + // Allows to use the currentTime function with a value expressed in seconds instead of a timestamp in DVR live streams + this.tech_.seekable = this.seekable.bind(this); + this.tech_.setCurrentTime = this.setCurrentTime.bind(this); + this.tech_.currentTime = this.currentTime.bind(this); + // Attach the source with any protection data this.mediaPlayer_.setProtectionData(this.keySystemOptions_); this.mediaPlayer_.attachSource(manifestSource); @@ -289,6 +294,15 @@ class Html5DashJS { return output; } + currentTime() { + // Livestream + if (this.mediaPlayer_.isDynamic()) { + return this.mediaPlayer_.time() + this.timeDiffFromStart(this.startTime()) + 10; + } + + return this.mediaPlayer_.time(); + } + dispose() { if (this.mediaPlayer_) { this.mediaPlayer_.off(dashjs.MediaPlayer.events.ERROR, this.retriggerError_); @@ -314,6 +328,64 @@ class Html5DashJS { } + seekable() { + const duration = this.duration(); + + if (!this.mediaPlayer_.isReady() || duration === 0) { + return videojs.createTimeRange(); + } + + // Livestream + if (this.mediaPlayer_.isDynamic()) { + const dvrWindowSize = this.mediaPlayer_.getDVRWindowSize(); + const start = this.timeDiffFromStart(this.startTime()); + const end = start + dvrWindowSize; + + return videojs.createTimeRange( + start, + end + ); + } + + // VOD + return videojs.createTimeRange(0, duration); + } + + setCurrentTime(time) { + const seekable = this.tech_.seekable(); + + // Livestream + if (seekable.length && this.mediaPlayer_.isDynamic()) { + this.mediaPlayer_.seek(time - this.timeDiffFromStart(this.startTime())); + } else { + this.mediaPlayer_.seek(time); + } + } + + /** + * Timestamp in seconds. Used to create the time range + * + * @return {number} + */ + startTime() { + if (!this.startTime_) { + this.startTime_ = (Date.now() / 1000) | 0; + } + + return this.startTime_; + } + + /** + * Diff between now and startTime + * + * @param {number} startTime timestamp in seconds + * + * @return {number} + */ + timeDiffFromStart(startTime) { + return (Date.now() / 1000 - startTime) | 0; + } + /** * Get a list of hooks for a specific lifecycle *