Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AV desync regression in v1.4.0 with misaligned mp4 audio #5471

Merged
merged 1 commit into from
May 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/remux/passthrough-remuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,25 @@ class PassThroughRemuxer implements Remuxer {
this.emitInitSegment = false;
}

const duration = getDuration(data, initData);
const startDTS = getStartDTS(initData, data);
const decodeTime = startDTS === null ? timeOffset : startDTS;
if (
isInvalidInitPts(initPTS, decodeTime, timeOffset) ||
isInvalidInitPts(initPTS, decodeTime, timeOffset, duration) ||
(initSegment.timescale !== initPTS.timescale && accurateTimeOffset)
) {
initSegment.initPTS = decodeTime - timeOffset;
if (initPTS && initPTS.timescale === 1) {
logger.warn(
`Adjusting initPTS by ${initSegment.initPTS - initPTS.baseTime}`
);
}
this.initPTS = initPTS = {
baseTime: initSegment.initPTS,
timescale: 1,
};
}

const duration = getDuration(data, initData);
const startTime = audioTrack
? decodeTime - initPTS.baseTime / initPTS.timescale
: (lastEndTime as number);
Expand Down Expand Up @@ -244,14 +249,16 @@ class PassThroughRemuxer implements Remuxer {
function isInvalidInitPts(
initPTS: RationalTimestamp | null,
startDTS: number,
timeOffset: number
timeOffset: number,
duration: number
): initPTS is null {
if (initPTS === null) {
return true;
}
// InitPTS is invalid when it would cause start time to be negative, or distance from time offset to be more than 1 second
// InitPTS is invalid when distance from program would be more than segment duration or a minimum of one second
const minDuration = Math.max(duration, 1);
const startTime = startDTS - initPTS.baseTime / initPTS.timescale;
return startTime < 0 && Math.abs(startTime - timeOffset) > 1;
return Math.abs(startTime - timeOffset) > minDuration;
}

function getParsedTrackCodec(
Expand Down