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 b0adf1a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 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
18 changes: 13 additions & 5 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 @@ -53,14 +60,14 @@ export const syncPointStrategies = [

// 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
};
}
}
Expand All @@ -79,7 +86,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,7 +103,7 @@ export const syncPointStrategies = [
lastDistance = distance;
syncPoint = {
time: segment.start,
segmentIndex: i
segmentIndex
};
}

Expand Down

0 comments on commit b0adf1a

Please sign in to comment.