Skip to content

Commit

Permalink
mp4-remuxer / remuxAudio() : recompute mdatSize / dont rely on audioT…
Browse files Browse the repository at this point in the history
…rack.len (video-dev#2096)

* mp4-remuxer / remuxAudio() : dont rely on track.len to compute mdatSize

recompute it by looping through all samples and summing their individual size

get rid of track.len as it should now be useless

related to video-dev#2063
  • Loading branch information
mangui authored and johnBartos committed Feb 24, 2019
1 parent 29ad34d commit e8a4085
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/demux/adts.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ export function appendFrame (track, data, offset, pts, frameIndex) {
};

track.samples.push(aacSample);
track.len += frameLength;

return { sample: aacSample, length: frameLength + headerLength };
}

Expand Down
1 change: 0 additions & 1 deletion src/demux/mpegaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const MpegAudio = {
track.channelCount = header.channelCount;
track.samplerate = header.sampleRate;
track.samples.push(sample);
track.len += header.frameLength;

return { sample, length: header.frameLength };
}
Expand Down
1 change: 0 additions & 1 deletion src/demux/tsdemuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class TSDemuxer {
inputTimeScale: 90000,
sequenceNumber: 0,
samples: [],
len: 0,
dropped: type === 'video' ? 0 : undefined,
isAAC: type === 'audio' ? true : undefined,
duration: type === 'audio' ? duration : undefined
Expand Down
29 changes: 14 additions & 15 deletions src/remux/mp4-remuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ class MP4Remuxer {
// next AVC sample DTS should be equal to last sample DTS + last sample duration (in PES timescale)
this.nextAvcDts = lastDTS + mp4SampleDuration;
let dropped = track.dropped;
track.len = 0;
track.nbNalu = 0;
track.dropped = 0;
if (outputSamples.length && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
Expand Down Expand Up @@ -434,11 +433,11 @@ class MP4Remuxer {
const initPTS = this._initPTS;
const rawMPEG = !track.isAAC && this.typeSupported.mpeg;

let offset,
mp4Sample,
let mp4Sample,
fillFrame,
mdat, moof,
firstPTS, lastPTS,
offset = (rawMPEG ? 0 : 8),
inputSamples = track.samples,
outputSamples = [],
nextAudioPts = this.nextAudioPts;
Expand Down Expand Up @@ -503,7 +502,6 @@ class MP4Remuxer {
if (delta <= -maxAudioFramesDrift * inputSampleDuration) {
logger.warn(`Dropping 1 audio frame @ ${(nextPts / inputTimeScale).toFixed(3)}s due to ${Math.round(duration)} ms overlap.`);
inputSamples.splice(i, 1);
track.len -= sample.unit.length;
// Don't touch nextPtsNorm or i
} // eslint-disable-line brace-style

Expand All @@ -522,7 +520,6 @@ class MP4Remuxer {
fillFrame = sample.unit.subarray();
}
inputSamples.splice(i, 0, { unit: fillFrame, pts: newStamp, dts: newStamp });
track.len += fillFrame.length;
nextPts += inputSampleDuration;
i++;
}
Expand All @@ -543,6 +540,13 @@ class MP4Remuxer {
}
}

// compute mdat size, as we eventually filtered/added some samples
let nbSamples = inputSamples.length;
let mdatSize = 0;
while (nbSamples--) {
mdatSize += inputSamples[nbSamples].unit.byteLength;
}

for (let j = 0, nbSamples = inputSamples.length; j < nbSamples; j++) {
let audioSample = inputSamples[j];
let unit = audioSample.unit;
Expand All @@ -568,13 +572,13 @@ class MP4Remuxer {
fillFrame = unit.subarray();
}

track.len += numMissingFrames * fillFrame.length;
mdatSize += numMissingFrames * fillFrame.length;
}
// if we have frame overlap, overlapping for more than half a frame duraion
} else if (delta < -12) {
// drop overlapping audio frames... browser will deal with it
logger.log(`drop overlapping AAC sample, expected/parsed/delta:${(nextAudioPts / inputTimeScale).toFixed(3)}s/${(pts / inputTimeScale).toFixed(3)}s/${(-delta)}ms`);
track.len -= unit.byteLength;
mdatSize -= unit.byteLength;
continue;
}
// set PTS/DTS to expected PTS/DTS
Expand All @@ -583,11 +587,8 @@ class MP4Remuxer {
}
// remember first PTS of our audioSamples
firstPTS = pts;
if (track.len > 0) {
/* concatenate the audio data and construct the mdat in place
(need 8 more bytes to fill length and mdat type) */
let mdatSize = rawMPEG ? track.len : track.len + 8;
offset = rawMPEG ? 0 : 8;
if (mdatSize > 0) {
mdatSize += offset;
try {
mdat = new Uint8Array(mdatSize);
} catch (err) {
Expand Down Expand Up @@ -646,7 +647,7 @@ class MP4Remuxer {
lastPTS = pts;
}
let lastSampleDuration = 0;
let nbSamples = outputSamples.length;
nbSamples = outputSamples.length;
// set last sample duration as being identical to previous sample
if (nbSamples >= 2) {
lastSampleDuration = outputSamples[nbSamples - 2].duration;
Expand All @@ -656,7 +657,6 @@ class MP4Remuxer {
// next audio sample PTS should be equal to last sample PTS + duration
this.nextAudioPts = nextAudioPts = lastPTS + scaleFactor * lastSampleDuration;
// logger.log('Audio/PTS/PTSend:' + audioSample.pts.toFixed(0) + '/' + this.nextAacDts.toFixed(0));
track.len = 0;
track.samples = outputSamples;
if (rawMPEG) {
moof = new Uint8Array();
Expand Down Expand Up @@ -715,7 +715,6 @@ class MP4Remuxer {
for (let i = 0; i < nbSamples; i++) {
let stamp = startDTS + i * frameDuration;
samples.push({ unit: silentFrame, pts: stamp, dts: stamp });
track.len += silentFrame.length;
}
track.samples = samples;

Expand Down
2 changes: 0 additions & 2 deletions tests/unit/demuxer/adts.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ describe('appendFrame', function () {
length: 16
});
expect(track.samples.length).to.equal(1);
expect(track.len).to.equal(7);
});

it('should not append sample if `parseFrameHeader` fails', function () {
Expand All @@ -410,6 +409,5 @@ describe('appendFrame', function () {

expect(appendFrame(track, data, 0, 0, 0)).to.be.undefined;
expect(track.samples.length).to.equal(0);
expect(track.len).to.equal(0);
});
});

0 comments on commit e8a4085

Please sign in to comment.