From 3fd87fec09671e45affea864db0551d6f45fd2a6 Mon Sep 17 00:00:00 2001 From: jony89 Date: Sat, 18 Jan 2020 12:05:38 +0200 Subject: [PATCH] fix: computeLivePosition minimum value of media.currentTime --- src/controller/base-stream-controller.js | 3 ++- src/controller/stream-controller.js | 4 ++-- tests/unit/controller/stream-controller.js | 26 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/controller/base-stream-controller.js b/src/controller/base-stream-controller.js index ae3348d7cd1..ab3b066da18 100644 --- a/src/controller/base-stream-controller.js +++ b/src/controller/base-stream-controller.js @@ -128,6 +128,7 @@ export default class BaseStreamController extends TaskLoop { computeLivePosition (sliding, levelDetails) { let targetLatency = this.config.liveSyncDuration !== undefined ? this.config.liveSyncDuration : this.config.liveSyncDurationCount * levelDetails.targetduration; - return sliding + Math.max(0, levelDetails.totalduration - targetLatency); + const currentTime = this.media ? this.media.currentTime : null; + return Math.max(sliding + Math.max(0, levelDetails.totalduration - targetLatency), currentTime); } } diff --git a/src/controller/stream-controller.js b/src/controller/stream-controller.js index 701acc58b2f..d0869e372d0 100644 --- a/src/controller/stream-controller.js +++ b/src/controller/stream-controller.js @@ -280,9 +280,9 @@ class StreamController extends BaseStreamController { if (bufferEnd < Math.max(start - config.maxFragLookUpTolerance, end - maxLatency)) { let liveSyncPosition = this.liveSyncPosition = this.computeLivePosition(start, levelDetails); - logger.log(`buffer end: ${bufferEnd.toFixed(3)} is located too far from the end of live sliding playlist, reset currentTime to : ${liveSyncPosition.toFixed(3)}`); bufferEnd = liveSyncPosition; - if (media && !media.paused && media.readyState && media.duration > liveSyncPosition) { + if (media && !media.paused && media.readyState && media.duration > liveSyncPosition && liveSyncPosition > media.currentTime) { + logger.log(`buffer end: ${bufferEnd.toFixed(3)} is located too far from the end of live sliding playlist, reset currentTime to : ${liveSyncPosition.toFixed(3)}`); media.currentTime = liveSyncPosition; } diff --git a/tests/unit/controller/stream-controller.js b/tests/unit/controller/stream-controller.js index 3df11e27ca1..428b25a4bef 100644 --- a/tests/unit/controller/stream-controller.js +++ b/tests/unit/controller/stream-controller.js @@ -305,4 +305,30 @@ describe('StreamController', function () { }); }); }); + + describe('computeLivePosition', function () { + it('should return the current live sync position according to liveSyncDuration', function () { + streamController.config.liveSyncDuration = 2; + const levelDetails = { totalduration: 20 }; + const liveSyncPosition = streamController.computeLivePosition(8, levelDetails); + expect(liveSyncPosition).to.equal(26); + }); + + it('should return the current live sync position according to liveSyncDurationCount', function () { + streamController.config.liveSyncDurationCount = 2; + const levelDetails = { totalduration: 20, targetduration: 0.5 }; + const liveSyncPosition = streamController.computeLivePosition(8, levelDetails); + expect(liveSyncPosition).to.equal(27); + }); + + it('should not return value that is less than the video current time', function () { + streamController.media = { + currentTime: 25 + }; + streamController.config.liveSyncDuration = 1; + const levelDetails = { totalduration: 14 }; + const liveSyncPosition = streamController.computeLivePosition(8, levelDetails); + expect(liveSyncPosition).to.equal(25); + }); + }); });