Skip to content

Commit

Permalink
fix(HLS): Fix presentation delay for small live playlists (eg: 3-4 se…
Browse files Browse the repository at this point in the history
…gments) (#5687)
  • Loading branch information
avelad authored and joeyparrish committed Oct 4, 2023
1 parent ac8bb85 commit 10b03e7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ shaka.hls.HlsParser = class {
// enough info here to determine the presentation type and duration.
if (playlist.type == shaka.hls.PlaylistType.MEDIA) {
if (this.isLive_()) {
this.changePresentationTimelineToLive_();
this.changePresentationTimelineToLive_(playlist);
const delay = this.getUpdatePlaylistDelay_();
this.updatePlaylistTimer_.tickAfter(/* seconds= */ delay);
}
Expand Down Expand Up @@ -2054,7 +2054,7 @@ shaka.hls.HlsParser = class {
if (this.isLive_() && !wasLive) {
// Now that we know that the presentation is live, convert the timeline
// to live.
this.changePresentationTimelineToLive_();
this.changePresentationTimelineToLive_(playlist);
}

// Copy values from the real stream info to our initial one.
Expand Down Expand Up @@ -2770,9 +2770,10 @@ shaka.hls.HlsParser = class {


/**
* @param {!shaka.hls.Playlist} playlist
* @private
*/
changePresentationTimelineToLive_() {
changePresentationTimelineToLive_(playlist) {
// The live edge will be calculated from segments, so we don't need to
// set a presentation start time. We will assert later that this is
// working as expected.
Expand All @@ -2795,8 +2796,12 @@ shaka.hls.HlsParser = class {
} else if (this.presentationDelay_) {
presentationDelay = this.presentationDelay_;
} else {
const numberOfSegments = this.config_.hls.liveSegmentsDelay;
presentationDelay = this.maxTargetDuration_ * numberOfSegments;
const playlistSegments = playlist.segments.length;
let delaySegments = this.config_.hls.liveSegmentsDelay;
if (delaySegments > (playlistSegments - 2)) {
delaySegments = Math.max(1, playlistSegments - 2);
}
presentationDelay = this.maxTargetDuration_ * delaySegments;
}

this.presentationTimeline_.setPresentationStartTime(0);
Expand Down
33 changes: 33 additions & 0 deletions test/hls/hls_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,43 @@ describe('HlsParser live', () => {

it('sets 3 times target duration as presentation delay if not configured',
async () => {
const media = [
'#EXTM3U\n',
'#EXT-X-TARGETDURATION:5\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXT-X-MEDIA-SEQUENCE:0\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXTINF:2,\n',
'main.mp4\n',
].join('');
const manifest = await testInitialManifest(master, media);
expect(manifest.presentationTimeline.getDelay()).toBe(15);
});

it('sets 1 times target duration as presentation delay if there are not enough segments', async () => { // eslint-disable-line max-len
const media = [
'#EXTM3U\n',
'#EXT-X-TARGETDURATION:5\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXT-X-MEDIA-SEQUENCE:0\n',
'#EXTINF:2,\n',
'main.mp4\n',
'#EXTINF:2,\n',
'main.mp4\n',
].join('');
const manifest = await testInitialManifest(master, media);
expect(manifest.presentationTimeline.getDelay()).toBe(5);
});

it('sets presentation delay if defined', async () => {
const media = [
'#EXTM3U\n',
Expand Down

0 comments on commit 10b03e7

Please sign in to comment.