From 23bf150f61b5aae8149c9b411074cbd468b6949b Mon Sep 17 00:00:00 2001 From: Garrett Date: Mon, 25 Jul 2016 16:16:28 -0400 Subject: [PATCH] Guard against negative media index due to manifest vs actual timing differences (#794) * Guard against negative media index due to manifest vs actual timing differences One of the possible reasons for us getting a negative media index is that we've recorded actual segment timing information and it differs enough from the manifest's timing information that we can't properly place the first segment. In the event that we're at time 0 and the video isn't live, we can safely assume that we should get the first segment. * Change getMediaIndexForTime to return first segment when time and expired are 0 --- src/playlist.js | 4 ++++ test/playlist.test.js | 23 ++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/playlist.js b/src/playlist.js index e5e254d13..eedf46f0a 100644 --- a/src/playlist.js +++ b/src/playlist.js @@ -264,6 +264,10 @@ export const getMediaIndexForTime_ = function(playlist, time, expired) { return 0; } + if (time === 0 && !expired) { + return 0; + } + expired = expired || 0; // find segments with known timing information that bound the diff --git a/test/playlist.test.js b/test/playlist.test.js index c849875ff..e652303ac 100644 --- a/test/playlist.test.js +++ b/test/playlist.test.js @@ -467,11 +467,6 @@ function() { media = loader.media(); - QUnit.equal( - Playlist.getMediaIndexForTime_(media, 0), - -1, - 'the lowest returned value is negative one' - ); QUnit.equal( Playlist.getMediaIndexForTime_(media, 45), -1, @@ -482,6 +477,11 @@ function() { -1, 'expired content returns negative one' ); + QUnit.equal( + Playlist.getMediaIndexForTime_(media, 0), + 0, + 'time of 0 with no expired time returns first segment' + ); QUnit.equal( Playlist.getMediaIndexForTime_(media, 50 + 100), 0, @@ -628,3 +628,16 @@ QUnit.test('accounts for expired time when calculating media index', function() 'calculates within the second segment' ); }); + +QUnit.test('returns index 0 when time is 0 and expired is falsy', function() { + QUnit.equal( + Playlist.getMediaIndexForTime_({segments: []}, 0, 0), + 0, + 'returns 0 when time is 0 and expired is 0' + ); + QUnit.equal( + Playlist.getMediaIndexForTime_({segments: []}, 0), + 0, + 'returns 0 when time is 0 and expired is undefined' + ); +});