diff --git a/src/js/live-tracker.js b/src/js/live-tracker.js index 5eb3b75d2..cc2f3e9b4 100644 --- a/src/js/live-tracker.js +++ b/src/js/live-tracker.js @@ -110,16 +110,23 @@ class LiveTracker extends Component { return; } + // If we haven't seen a timeupdate, we need to check whether playback + // began before this component started tracking. This can happen commonly + // when using autoplay. + if (!this.timeupdateSeen_) { + this.timeupdateSeen_ = this.player_.hasStarted(); + } + this.trackingInterval_ = this.setInterval(this.trackLive_, 30); this.trackLive_(); this.on(this.player_, 'play', this.trackLive_); this.on(this.player_, 'pause', this.trackLive_); - this.one(this.player_, 'play', this.handlePlay); // this is to prevent showing that we are not live // before a video starts to play if (!this.timeupdateSeen_) { + this.one(this.player_, 'play', this.handlePlay); this.handleTimeupdate = () => { this.timeupdateSeen_ = true; this.handleTimeupdate = null; diff --git a/test/unit/live-tracker.test.js b/test/unit/live-tracker.test.js index 8375edb82..ac69e437b 100644 --- a/test/unit/live-tracker.test.js +++ b/test/unit/live-tracker.test.js @@ -153,6 +153,44 @@ QUnit.module('LiveTracker', () => { assert.equal(playCalls, 0, 'should not have called play'); }); + QUnit.test('seeks to live edge on the first timeupdate after playback is requested', function(assert) { + sinon.spy(this.liveTracker, 'seekToLiveEdge'); + + // Begin live tracking. + this.player.duration(Infinity); + assert.ok(this.liveTracker.seekToLiveEdge.notCalled, 'seekToLiveEdge was not called yet'); + + this.player.trigger('play'); + this.player.trigger('playing'); + assert.ok(this.liveTracker.seekToLiveEdge.notCalled, 'seekToLiveEdge was not called yet'); + + this.player.trigger('timeupdate'); + assert.ok(this.liveTracker.seekToLiveEdge.calledOnce, 'seekToLiveEdge was called'); + + this.player.trigger('timeupdate'); + assert.ok(this.liveTracker.seekToLiveEdge.calledOnce, 'seekToLiveEdge was not called on subsequent timeupdate'); + }); + + QUnit.test('does not seek to live edge on the first timeupdate after playback is requested if playback already began', function(assert) { + sinon.spy(this.liveTracker, 'seekToLiveEdge'); + + // Begin live tracking. + this.liveTracker.stopTracking(); + this.player.hasStarted = () => true; + this.liveTracker.startTracking(); + assert.ok(this.liveTracker.seekToLiveEdge.notCalled, 'seekToLiveEdge was not called'); + + this.player.trigger('play'); + this.player.trigger('playing'); + assert.ok(this.liveTracker.seekToLiveEdge.notCalled, 'seekToLiveEdge was not called'); + + this.player.trigger('timeupdate'); + assert.ok(this.liveTracker.seekToLiveEdge.notCalled, 'seekToLiveEdge was not called'); + + this.player.trigger('timeupdate'); + assert.ok(this.liveTracker.seekToLiveEdge.notCalled, 'seekToLiveEdge was not called'); + }); + QUnit.test('single seekable, helpers should be correct', function(assert) { // simple this.player.seekable = () => createTimeRanges(10, 50);