Skip to content

Commit

Permalink
Attempt to adjust m2ts sample times to fix gaps in appended media
Browse files Browse the repository at this point in the history
Resolves aspects of #5631
  • Loading branch information
robwalch committed Sep 8, 2023
1 parent b122a39 commit 767b697
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/remux/mp4-remuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,17 @@ export default class MP4Remuxer implements Remuxer {
const cts =
inputSamples[0].pts -
normalizePts(inputSamples[0].dts, inputSamples[0].pts);
// if not contiguous, let's use target timeOffset
nextAvcDts = pts - cts;
if (
chromeVersion &&
nextAvcDts !== null &&
Math.abs(pts - cts - nextAvcDts) < 15000
) {
// treat as contigous to adjust samples that would otherwise produce video buffer gaps in Chrome
contiguous = true;
} else {
// if not contiguous, let's use target timeOffset
nextAvcDts = pts - cts;
}
}

// PTS is coded on 33bits, and can loop from -2^32 to 2^32
Expand Down Expand Up @@ -575,10 +584,12 @@ export default class MP4Remuxer implements Remuxer {
sample.length = sampleLen;

// ensure sample monotonic DTS
if (dtsStep < sample.dts) {
if (sample.dts < dtsStep) {
sample.dts = dtsStep;
dtsStep += (averageSampleDuration / 4) | 0 || 1;
} else {
dtsStep = sample.dts;
}
sample.dts = sample.dts < dtsStep ? dtsStep++ : sample.dts;

minPTS = Math.min(sample.pts, minPTS);
maxPTS = Math.max(sample.pts, maxPTS);
Expand Down

0 comments on commit 767b697

Please sign in to comment.