Skip to content

Commit

Permalink
mp4-remuxer: remove audio frame PTS sorting to avoid audio glitch on …
Browse files Browse the repository at this point in the history
…broken audio streams

BUT always fix audio frame duration (no matter if the timeOffset provided is accurate or not)

related to video-dev#1453 video-dev#1334
  • Loading branch information
mangui committed Nov 30, 2017
1 parent 5fd8c78 commit 716fbd5
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/remux/mp4-remuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,30 +457,29 @@ class MP4Remuxer {
Math.abs((inputSamples[0].pts-nextAudioPts-initDTS)) < 20*inputSampleDuration)
);

if (!contiguous) {
// if fragments are not contiguous, let's use timeOffset to compute next Audio PTS
nextAudioPts = timeOffset*inputTimeScale;
}

// compute normalized PTS
inputSamples.forEach(function(sample) {
sample.pts = sample.dts = ptsNormalize(sample.pts - initDTS, nextAudioPts);
sample.pts = sample.dts = ptsNormalize(sample.pts - initDTS, timeOffset*inputTimeScale);
});

// sort based on normalized PTS (this is to avoid sorting issues in case timestamp
// reloop in the middle of our samples array)
inputSamples.sort(function(a, b) {
return a.pts - b.pts;
});
if (!contiguous) {
if (!accurateTimeOffset) {
// if frag are mot contiguous and if we cant trust time offset, let's use first sample PTS as next audio PTS
nextAudioPts = inputSamples[0].pts;
} else {
// if timeOffset is accurate, let's use it as predicted next audio PTS
nextAudioPts = timeOffset*inputTimeScale;
}
}

// If the audio track is missing samples, the frames seem to get "left-shifted" within the
// resulting mp4 segment, causing sync issues and leaving gaps at the end of the audio segment.
// In an effort to prevent this from happening, we inject frames here where there are gaps.
// When possible, we inject a silent frame; when that's not possible, we duplicate the last
// frame.

// only inject/drop audio frames in case time offset is accurate
if (accurateTimeOffset && track.isAAC) {
if ( track.isAAC) {
const maxAudioFramesDrift = this.config.maxAudioFramesDrift;
for (let i = 0, nextPts = nextAudioPts; i < inputSamples.length; ) {
// First, let's see how far off this frame is from where we expect it to be
Expand Down

0 comments on commit 716fbd5

Please sign in to comment.