Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix position stops updating after seek on HLS streams #165

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions src/js/tech/ChromecastTech.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var ChromecastSessionManager = require('../chromecast/ChromecastSessionManager'),
ChromecastTechUI = require('./ChromecastTechUI');
ChromecastTechUI = require('./ChromecastTechUI'),
Throttler = require('../throttler');


/**
* Registers the ChromecastTech Tech with Video.js. Calls {@link
Expand Down Expand Up @@ -80,6 +82,14 @@ module.exports = function(videojs) {
};
// See `currentTime` function
this._initialStartTime = options.startTime || 0;
this._isScrubbing = false;
this._isSeeking = false;

this.seekThrottler = new Throttler((time) => {
this._remotePlayer.currentTime = time;
this._remotePlayerController.seek();
this._triggerTimeUpdateEvent();
});

this._playSource(options.source, this._initialStartTime);
this.ready(function() {
Expand Down Expand Up @@ -243,19 +253,58 @@ module.exports = function(videojs) {
* @see {@link http://docs.videojs.com/Tech.html#setCurrentTime}
*/
setCurrentTime(time) {
var duration = this.duration();
const duration = this.duration();

if (time > duration || !this._remotePlayer.canSeek) {
return;
}
// Seeking to any place within (approximately) 1 second of the end of the item
// causes the Video.js player to get stuck in a BUFFERING state. To work around
// this, we only allow seeking to within 1 second of the end of an item.
this._remotePlayer.currentTime = Math.min(duration - 1, time);
this._remotePlayerController.seek();

if (this.scrubbing()) {
this._isSeeking = true;
// We need to throttle the actual seeking, because when you
// scrub, videojs does pause() -> setCurrentTime() -> play()
// and that triggers a weird bug where the chromecast stops sending
// time_changed events.
this.seekThrottler.throttle(time);
return false;
}

// We need to delay the actual seeking, because when you
// scrub, videojs does pause() -> setCurrentTime() -> play()
// and that triggers a weird bug where the chromecast stops sending
// time_changed events.
this.player().one('play', () => {
if (this.seeking()) {
this.seekThrottler.executeRemaining();
} else {
this._remotePlayer.currentTime = time;
this._remotePlayerController.seek();
}
this._isSeeking = false;
});

this._triggerTimeUpdateEvent();
}

seeking() {
return this._isSeeking;
}

scrubbing() {
return this._isScrubbing;
}

setScrubbing(newValue) {
if (newValue === true) {
this._isScrubbing = true;
return;
}

if (newValue === false) {
this._isScrubbing = false;
}
}

/**
* Returns the current playback time position.
*
Expand All @@ -274,6 +323,7 @@ module.exports = function(videojs) {
if (!this._hasPlayedAnyItem) {
return this._initialStartTime;
}

return this._remotePlayer.currentTime;
}

Expand Down
26 changes: 26 additions & 0 deletions src/js/throttler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = class Throttler {
timerId = null;
latestArgs;

constructor(mainFunction) {
this.mainFunction = mainFunction;
}

throttle(...args) {
this.latestArgs = args;

if (this.timerId === null) {
this.mainFunction(...this.latestArgs);
this.timerId = setTimeout(() => {
this.timerId = null;
}, 300);
}
}

executeRemaining() {
if (this.timerId) {
this.mainFunction(...this.latestArgs);
clearTimeout(this.timerId);
}
}
};
Loading