Skip to content

Commit

Permalink
fix: computeLivePosition minimum value of media.currentTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jony89 committed Jan 18, 2020
1 parent 12f2e16 commit 3fd87fe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/controller/base-stream-controller.js
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/controller/stream-controller.js
Expand Up @@ -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;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/unit/controller/stream-controller.js
Expand Up @@ -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);
});
});
});

0 comments on commit 3fd87fe

Please sign in to comment.