diff --git a/src/util/media-sequence-sync.js b/src/util/media-sequence-sync.js index c596c96f1..5c9959276 100644 --- a/src/util/media-sequence-sync.js +++ b/src/util/media-sequence-sync.js @@ -273,7 +273,7 @@ export class DependantMediaSequenceSync extends MediaSequenceSync { this.parent_ = parent; } - calculateBaseTime_(mediaSequence, fallback) { + calculateBaseTime_(mediaSequence, segments, fallback) { if (!this.storage_.size) { const info = this.parent_.getSyncInfoForMediaSequence(mediaSequence); @@ -284,6 +284,6 @@ export class DependantMediaSequenceSync extends MediaSequenceSync { return 0; } - return super.calculateBaseTime_(mediaSequence, fallback); + return super.calculateBaseTime_(mediaSequence, segments, fallback); } } diff --git a/test/util/media-sequence-sync.test.js b/test/util/media-sequence-sync.test.js new file mode 100644 index 000000000..5e5354eca --- /dev/null +++ b/test/util/media-sequence-sync.test.js @@ -0,0 +1,109 @@ +import QUnit from 'qunit'; +import { MediaSequenceSync } from '../../src/util/media-sequence-sync'; + +QUnit.module('MediaSequenceSync: update', function(hooks) { + let mediaSequenceSync; + + hooks.beforeEach(function() { + mediaSequenceSync = new MediaSequenceSync(); + }); + + QUnit.test('update calculates correct base time based on mediaSequence of new playlist', function(assert) { + const initialMediaSequence = 10; + const initialSegments = [ + // Segment 10 with duration 5 + { duration: 5 }, + // Segment 11 with duration 6 + { duration: 6 }, + // Segment 12 with duration 7 + { duration: 7 } + ]; + + // Initial update with starting playlist + mediaSequenceSync.update( + { + mediaSequence: initialMediaSequence, + segments: initialSegments + }, + // Current time, value is used for fallback and not significant here + 20 + ); + + // Confirm that the initial update set the correct start and end times + assert.strictEqual( + mediaSequenceSync.start, + 0, + 'The start time is set to the initial value of 0.' + ); + + // Confirm the end time is the correct sum of the segment durations + // = 18 + const expectedInitialEndTime = 0 + 5 + 6 + 7; + + assert.strictEqual( + mediaSequenceSync.end, + expectedInitialEndTime, + 'The end time is calculated correctly after the initial update.' + ); + + // New playlist with higher mediaSequence + let newMediaSequence = 11; + let newSegments = [ + // Segment 11 with duration 4 + { duration: 4 }, + // Segment 12 with duration 5 + { duration: 5 }, + // Segment 13 with duration 6 + { duration: 6 } + ]; + + // Update with the new playlist + mediaSequenceSync.update( + { + mediaSequence: newMediaSequence, + segments: newSegments + }, + 30 + ); + + // Segment 10 with duration 5 has fallen off the start of the playlist + let expectedStartTime = 5; + + assert.strictEqual( + mediaSequenceSync.start, + expectedStartTime, + 'The base time is calculated correctly when a new playlist with a higher mediaSequence is loaded.' + ); + + // New playlist with lower mediaSequence + newMediaSequence = 10; + newSegments = [ + // Segment 10 with duration 5 + { duration: 5 }, + // Segment 11 with duration 6 + { duration: 6 }, + // Segment 12 with duration 7 + { duration: 7 } + ]; + + // Update with the new playlist + mediaSequenceSync.update( + { + mediaSequence: newMediaSequence, + segments: newSegments + }, + 40 + ); + + // Expected base time is calculated by extrapolating backwards: + // Segment 11 start time: 5 + // Segment 10 start time: Segment 11 start time (5) - Segment 10 duration (5) = 0 + expectedStartTime = 0; + + assert.strictEqual( + mediaSequenceSync.start, + expectedStartTime, + 'The base time is calculated correctly when a new playlist with a lower mediaSequence is loaded.' + ); + }); +});