Skip to content

Commit

Permalink
Add method to find start pts
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Walch authored and alangdm committed Sep 15, 2020
1 parent 63b7fed commit 5deccde
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/remux/mp4-remuxer.js
Expand Up @@ -34,6 +34,25 @@ class MP4Remuxer {
this.ISGenerated = false;
}

getVideoStartPts (videoSamples) {
let rolloverDetected = false;
const startPTS = videoSamples.reduce((minPTS, sample) => {
const delta = sample.pts - minPTS;
if (delta < -4294967296) { // 2^32, see PTSNormalize for reasoning, but we're hitting a rollover here, and we don't want that to impact the timeOffset calculation
rolloverDetected = true;
return minPTS;
} else if (delta > 0) {
return minPTS;
} else {
return sample.pts;
}
}, videoSamples[0].pts);
if (rolloverDetected) {
logger.debug('PTS rollover detected');
}
return startPTS;
}

remux (audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset) {
// generate Init Segment if needed
if (!this.ISGenerated) {
Expand All @@ -50,21 +69,7 @@ class MP4Remuxer {
// if first audio DTS is not aligned with first video DTS then we need to take that into account
// when providing timeOffset to remuxAudio / remuxVideo. if we don't do that, there might be a permanent / small
// drift between audio and video streams
let rolloverDetected = false;
const startPTS = videoTrack.samples.reduce((minPTS, sample) => {
const delta = sample.pts - minPTS;
if (delta < -4294967296) { // 2^32, see PTSNormalize for reasoning, but we're hitting a rollover here, and we don't want that to impact the timeOffset calculation
rolloverDetected = true;
return minPTS;
} else if (delta > 0) {
return minPTS;
} else {
return sample.pts;
}
}, videoTrack.samples[0].pts);
if (rolloverDetected) {
logger.debug('PTS rollover detected');
}
const startPTS = this.getVideoStartPts(videoTrack.samples);
const tsDelta = audioTrack.samples[0].pts - startPTS;
const audiovideoTimestampDelta = tsDelta / videoTrack.inputTimeScale;
audioTimeOffset += Math.max(0, audiovideoTimestampDelta);
Expand Down Expand Up @@ -177,7 +182,7 @@ class MP4Remuxer {
}
};
if (computePTSDTS) {
const startPTS = videoSamples.reduce((minPTS, sample) => Math.min(minPTS, sample.pts), videoSamples[0].pts);
const startPTS = this.getVideoStartPts(videoSamples);
const startOffset = Math.round(inputTimeScale * timeOffset);
initDTS = Math.min(initDTS, videoSamples[0].dts - startOffset);
initPTS = Math.min(initPTS, startPTS - startOffset);
Expand Down

0 comments on commit 5deccde

Please sign in to comment.