d9afdb77 by Brandon Bay Committed by David LaPalomento

getMediaIndexByTime was returning -1 at playlist boundaries

It should correctly return the first or last segment in the playlist.
1 parent c6c973f6
......@@ -1115,6 +1115,10 @@ videojs.Hls.translateMediaIndex = function(mediaIndex, original, update) {
videojs.Hls.getMediaIndexByTime = function(playlist, time) {
var index, counter, timeRanges, currentSegmentRange;
if (time === 0) {
return 0;
}
timeRanges = [];
for (index = 0; index < playlist.segments.length; index++) {
currentSegmentRange = {};
......@@ -1123,6 +1127,10 @@ videojs.Hls.getMediaIndexByTime = function(playlist, time) {
timeRanges.push(currentSegmentRange);
}
if (time >= timeRanges[timeRanges.length - 1].end) {
return (playlist.segments.length - 1);
}
for (counter = 0; counter < timeRanges.length; counter++) {
if (time >= timeRanges[counter].start && time < timeRanges[counter].end) {
return counter;
......
......@@ -1728,6 +1728,24 @@ test('mediaIndex is zero before the first segment loads', function() {
strictEqual(player.hls.mediaIndex, 0, 'mediaIndex is zero');
});
test('mediaIndex returns correctly at playlist boundaries', function() {
player.src({
src: 'http://example.com/master.m3u8',
type: 'application/vnd.apple.mpegurl'
});
openMediaSource(player);
standardXHRResponse(requests.shift()); // master
standardXHRResponse(requests.shift()); // media
strictEqual(player.hls.mediaIndex, 0, 'mediaIndex is zero at first segment');
// seek to end
player.currentTime(40);
strictEqual(player.hls.mediaIndex, 3, 'mediaIndex is 3 at last segment');
});
test('reloads out-of-date live playlists when switching variants', function() {
player.src({
src: 'http://example.com/master.m3u8',
......