Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Commit

Permalink
use 3*targetDuration for safe live point instead of 3 segments
Browse files Browse the repository at this point in the history
  • Loading branch information
mjneil committed Oct 5, 2017
1 parent d4abc21 commit bad769e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,33 @@ export const sumDurations = function(playlist, startIndex, endIndex) {
return durations;
};

/**
* Determines the media index of the segment corresponding to the safe edge of the live
* window which is 3 times target duration.
*
* @param {Object} playlist
* a media playlist object;
* @return {Number}
* The media index of the segment
* @function safeLiveIndex
*/
export const safeLiveIndex = function(playlist) {
const safeDistance = playlist.targetDuration * Playlist.UNSAFE_LIVE_SEGMENTS;

let i = playlist.segments.length;
let distanceFromEnd = 0;

while (i--) {
distanceFromEnd += playlist.segments[i].duration;

if (distanceFromEnd >= safeDistance) {
break;
}
}

return Math.max(0, i);
};

/**
* Calculates the playlist end time
*
Expand Down Expand Up @@ -246,9 +273,7 @@ export const playlistEnd = function(playlist, expired, useSafeLiveEnd) {

expired = expired || 0;

let endSequence = useSafeLiveEnd ?
Math.max(0, playlist.segments.length - Playlist.UNSAFE_LIVE_SEGMENTS) :
Math.max(0, playlist.segments.length);
let endSequence = useSafeLiveEnd ? safeLiveIndex(playlist) : playlist.segments.length;

return intervalDuration(playlist,
playlist.mediaSequence + endSequence,
Expand Down Expand Up @@ -485,6 +510,7 @@ export const estimateSegmentRequestTime = function(segmentDuration,

Playlist.duration = duration;
Playlist.seekable = seekable;
Playlist.safeLiveIndex = safeLiveIndex;
Playlist.getMediaInfoForTime = getMediaInfoForTime;
Playlist.isEnabled = isEnabled;
Playlist.isBlacklisted = isBlacklisted;
Expand Down
85 changes: 83 additions & 2 deletions test/playlist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,92 @@ function(assert) {

assert.equal(seekable.start(0), 0, 'starts at the earliest available segment');
assert.equal(seekable.end(0),
9 - (2 + 2 + 1),
'allows seeking no further than three segments from the end');
9 - (2 + 2 + 1 + 2),
'allows seeking no further than three times target duration from the end');
assert.equal(playlistEnd, 9, 'playlist end at the last segment');
});

QUnit.test('safeLiveIndex returns the correct media index for the safe live point',
function(assert) {
const playlist = {
targetDuration: 6,
mediaSequence: 10,
syncInfo: {
time: 0,
mediaSequence: 10
},
segments: [
{
duration: 6
},
{
duration: 6
},
{
duration: 6
},
{
duration: 6
},
{
duration: 6
},
{
duration: 6
}
]
};

let expected = 3;
let actual = Playlist.safeLiveIndex(playlist);

assert.equal(actual, expected, 'correct media index for standard durations');

playlist.segments = [
{
duration: 6
},
{
duration: 4
},
{
duration: 5
},
{
duration: 6
},
{
duration: 3
},
{
duration: 4
},
{
duration: 3
}
];

expected = 2;
actual = Playlist.safeLiveIndex(playlist);
assert.equal(actual, expected, 'correct media index for variable segment durations');

playlist.segments = [
{
duration: 6
},
{
duration: 6
},
{
duration: 3
}
];
expected = 0;
actual = Playlist.safeLiveIndex(playlist);
assert.equal(actual, expected,
'returns media index 0 when playlist has no safe live point');
});

QUnit.test(
'seekable end and playlist end account for non-zero starting VOD media sequence',
function(assert) {
Expand Down
1 change: 1 addition & 0 deletions test/videojs-contrib-hls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ QUnit.test('translates seekable by the starting time for live playlists', functi
this.requests.shift().respond(200, null,
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:15\n' +
'#EXT-X-TARGETDURATION:10\n' +
'#EXTINF:10,\n' +
'0.ts\n' +
'#EXTINF:10,\n' +
Expand Down

0 comments on commit bad769e

Please sign in to comment.