Skip to content

Commit 2b1eca7

Browse files
committed
fix(videojs-dash): currentTime with DVR live stream
1 parent 9f19888 commit 2b1eca7

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/js/videojs-dash.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ class Html5DashJS {
254254
// Setup text tracks
255255
setupTextTracks.call(null, this.player, tech, options);
256256

257+
// Allows to use the currentTime function with a value expressed in seconds instead of a timestamp in DVR live streams
258+
this.tech_.seekable = this.seekable.bind(this);
259+
this.tech_.setCurrentTime = this.setCurrentTime.bind(this);
260+
this.tech_.currentTime = this.currentTime.bind(this);
261+
257262
// Attach the source with any protection data
258263
this.mediaPlayer_.setProtectionData(this.keySystemOptions_);
259264
this.mediaPlayer_.attachSource(manifestSource);
@@ -289,6 +294,15 @@ class Html5DashJS {
289294
return output;
290295
}
291296

297+
currentTime() {
298+
// Livestream
299+
if (this.mediaPlayer_.isDynamic()) {
300+
return this.mediaPlayer_.time() + this.timeDiffFromStart(this.startTime());
301+
}
302+
303+
return this.mediaPlayer_.time();
304+
}
305+
292306
dispose() {
293307
if (this.mediaPlayer_) {
294308
this.mediaPlayer_.off(dashjs.MediaPlayer.events.ERROR, this.retriggerError_);
@@ -314,6 +328,64 @@ class Html5DashJS {
314328

315329
}
316330

331+
seekable() {
332+
const duration = this.duration();
333+
334+
if (!this.mediaPlayer_.isReady() || duration === 0) {
335+
return videojs.createTimeRange();
336+
}
337+
338+
// Livestream
339+
if (this.mediaPlayer_.isDynamic()) {
340+
const dvrWindowSize = this.mediaPlayer_.getDVRWindowSize();
341+
const start = this.timeDiffFromStart(this.startTime());
342+
const end = start + dvrWindowSize;
343+
344+
return videojs.createTimeRange(
345+
start,
346+
end
347+
);
348+
}
349+
350+
// VOD
351+
return videojs.createTimeRange(0, duration);
352+
}
353+
354+
setCurrentTime(time) {
355+
const seekable = this.tech_.seekable();
356+
357+
// Livestream
358+
if (seekable.length && this.mediaPlayer_.isDynamic()) {
359+
this.mediaPlayer_.seek(time - this.timeDiffFromStart(this.startTime()));
360+
} else {
361+
this.mediaPlayer_.seek(time);
362+
}
363+
}
364+
365+
/**
366+
* Timestamp in seconds. Used to create the time range
367+
*
368+
* @return {number}
369+
*/
370+
startTime() {
371+
if (!this.startTime_) {
372+
this.startTime_ = (Date.now() / 1000) | 0;
373+
}
374+
375+
return this.startTime_;
376+
}
377+
378+
/**
379+
* Diff between now and startTime
380+
*
381+
* @param {number} startTime timestamp in seconds
382+
*
383+
* @return {number}
384+
*/
385+
timeDiffFromStart(startTime) {
386+
return (Date.now() / 1000 - startTime) | 0;
387+
}
388+
317389
/**
318390
* Get a list of hooks for a specific lifecycle
319391
*

0 commit comments

Comments
 (0)