23bf150f by Garrett Committed by Jon-Carlos Rivera

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
1 parent f8af9a26
......@@ -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
......
......@@ -468,11 +468,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,
'expired content returns negative one'
......@@ -483,6 +478,11 @@ function() {
'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,
'calculates the earliest available position'
......@@ -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'
);
});
......