Skip to content

Commit

Permalink
fix: correct playlist end logic for live playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Mar 25, 2021
1 parent b33e109 commit e0d34d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const forwardDuration = function(playlist, endSequence) {
*/
const intervalDuration = function(playlist, endSequence, expired) {
if (typeof endSequence === 'undefined') {
endSequence = playlist.mediaSequence + playlist.segments.length;
endSequence = playlist.mediaSequence + (playlist.segments.length - 1);
}

if (endSequence < playlist.mediaSequence) {
Expand Down Expand Up @@ -293,13 +293,18 @@ export const playlistEnd = function(playlist, expired, useSafeLiveEnd, liveEdgeP

expired = expired || 0;

const endSequence = useSafeLiveEnd ? safeLiveIndex(playlist, liveEdgePadding) : playlist.segments.length;

return intervalDuration(
let lastSegmentTime = intervalDuration(
playlist,
playlist.mediaSequence + endSequence,
playlist.mediaSequence + playlist.segments.length - 1,
expired
);

if (useSafeLiveEnd) {
liveEdgePadding = typeof liveEdgePadding === 'number' ? liveEdgePadding : playlist.targetDuration * 3;
lastSegmentTime -= (playlist.targetDuration * 3);
}

return lastSegmentTime;
};

/**
Expand Down
21 changes: 15 additions & 6 deletions src/sync-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import {sumDurations} from './playlist';
import videojs from 'video.js';
import logger from './util/logger';

const getSegmentIndex = (i, playlist, currentTime = 0) => {
const segments = playlist.segments;

return (playlist.endList || currentTime === 0) ? i : segments.length - (i + 1);
};

export const syncPointStrategies = [
// Stategy "VOD": Handle the VOD-case where the sync-point is *always*
// the equivalence display-time 0 === segment-index 0
Expand Down Expand Up @@ -38,7 +44,8 @@ export const syncPointStrategies = [
currentTime = currentTime || 0;

for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
const segmentIndex = getSegmentIndex(i, playlist, currentTime);
const segment = segments[segmentIndex];
const datetimeMapping =
syncController.timelineToDatetimeMappings[segment.timeline];

Expand All @@ -49,21 +56,23 @@ export const syncPointStrategies = [
if (segment.dateTimeObject) {
const segmentTime = segment.dateTimeObject.getTime() / 1000;
const segmentStart = segmentTime + datetimeMapping;

const distance = Math.abs(currentTime - segmentStart);

// Once the distance begins to increase, or if distance is 0, we have passed
// currentTime and can stop looking for better candidates
if (lastDistance !== null && (distance === 0 || lastDistance < distance)) {
if (lastDistance !== null && lastDistance < distance) {
break;
}

lastDistance = distance;
syncPoint = {
time: segmentStart,
segmentIndex: i
segmentIndex
};
}
}

return syncPoint;
}
},
Expand All @@ -79,7 +88,8 @@ export const syncPointStrategies = [
currentTime = currentTime || 0;

for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
const segmentIndex = getSegmentIndex(i, playlist, currentTime);
const segment = segments[segmentIndex];

if (segment.timeline === currentTimeline &&
typeof segment.start !== 'undefined') {
Expand All @@ -95,10 +105,9 @@ export const syncPointStrategies = [
lastDistance = distance;
syncPoint = {
time: segment.start,
segmentIndex: i
segmentIndex
};
}

}
}
return syncPoint;
Expand Down

0 comments on commit e0d34d4

Please sign in to comment.