Skip to content

Commit

Permalink
fix(videojs-dash): currentTime with DVR live stream
Browse files Browse the repository at this point in the history
  • Loading branch information
amtins committed May 17, 2023
1 parent 9f19888 commit 61497c6
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/js/videojs-dash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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_);
Expand All @@ -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
*
Expand Down

0 comments on commit 61497c6

Please sign in to comment.