Skip to content

Commit

Permalink
fix(HLS): Calculate the delay based on the sum of the segment lengths (
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Aug 26, 2024
1 parent b119c03 commit abdabb0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
20 changes: 15 additions & 5 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3410,20 +3410,30 @@ shaka.hls.HlsParser = class {
// We accomplish this in our DASH-y model by setting a presentation
// delay of configured value, or 3 segments duration if not configured.
// This will be the "live edge" of the presentation.
let presentationDelay;
let presentationDelay = 0;
if (this.config_.defaultPresentationDelay) {
presentationDelay = this.config_.defaultPresentationDelay;
} else if (this.lowLatencyPresentationDelay_) {
presentationDelay = this.lowLatencyPresentationDelay_;
} else if (this.presentationDelay_) {
presentationDelay = this.presentationDelay_;
} else {
const playlistSegments = playlist.segments.length;
const totalSegments = playlist.segments.length;
let delaySegments = this.config_.hls.liveSegmentsDelay;
if (delaySegments > (playlistSegments - 2)) {
delaySegments = Math.max(1, playlistSegments - 2);
if (delaySegments > (totalSegments - 2)) {
delaySegments = Math.max(1, totalSegments - 2);
}
for (let i = totalSegments - delaySegments; i < totalSegments; i++) {
const extinfTag = shaka.hls.Utils.getFirstTagWithName(
playlist.segments[i].tags, 'EXTINF');
if (extinfTag) {
const extinfValues = extinfTag.value.split(',');
const duration = Number(extinfValues[0]);
presentationDelay += Math.ceil(duration);
} else {
presentationDelay += this.maxTargetDuration_;
}
}
presentationDelay = this.maxTargetDuration_ * delaySegments;
}

if (this.startTime_ && this.startTime_ < 0) {
Expand Down
14 changes: 7 additions & 7 deletions test/hls/hls_live_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe('HlsParser live', () => {
async () => {
const media = [
'#EXTM3U\n',
'#EXT-X-TARGETDURATION:5\n',
'#EXT-X-TARGETDURATION:2\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXT-X-MEDIA-SEQUENCE:0\n',
'#EXTINF:2,\n',
Expand All @@ -568,14 +568,14 @@ describe('HlsParser live', () => {
'main.mp4\n',
].join('');
const manifest = await testInitialManifest(master, media);
expect(manifest.presentationTimeline.getDelay()).toBe(15);
expect(manifest.presentationTimeline.getDelay()).toBe(6);
});

it('sets 3 times target duration as presentation delay if not configured and clamped to the start', async () => { // eslint-disable-line max-len
const media = [
'#EXTM3U\n',
'#EXT-X-TARGETDURATION:5\n',
'#EXT-X-START:TIME-OFFSET=-10\n',
'#EXT-X-TARGETDURATION:2\n',
'#EXT-X-START:TIME-OFFSET=-4\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXT-X-MEDIA-SEQUENCE:0\n',
'#EXTINF:2,\n',
Expand All @@ -592,14 +592,14 @@ describe('HlsParser live', () => {
'main.mp4\n',
].join('');
const manifest = await testInitialManifest(master, media);
expect(manifest.presentationTimeline.getDelay()).toBe(10);
expect(manifest.presentationTimeline.getDelay()).toBe(4);
expect(manifest.startTime).toBe(0);
});

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-TARGETDURATION:2\n',
'#EXT-X-MAP:URI="init.mp4",BYTERANGE="616@0"\n',
'#EXT-X-MEDIA-SEQUENCE:0\n',
'#EXTINF:2,\n',
Expand All @@ -608,7 +608,7 @@ describe('HlsParser live', () => {
'main.mp4\n',
].join('');
const manifest = await testInitialManifest(master, media);
expect(manifest.presentationTimeline.getDelay()).toBe(5);
expect(manifest.presentationTimeline.getDelay()).toBe(2);
});

it('sets presentation delay if defined', async () => {
Expand Down

0 comments on commit abdabb0

Please sign in to comment.