Skip to content

Commit

Permalink
mp4-remuxer: use TypedArray.prototype.subarray instead of TypedArray.…
Browse files Browse the repository at this point in the history
…prototype.slice

get rid of polyfill and fix silent audio frame injection on IE11
  • Loading branch information
mangui committed Dec 1, 2016
1 parent 941ba5a commit 258911d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
37 changes: 18 additions & 19 deletions src/remux/mp4-remuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Event from '../events';
import {logger} from '../utils/logger';
import MP4 from '../remux/mp4-generator';
import {ErrorTypes, ErrorDetails} from '../errors';
import '../utils/polyfill';

class MP4Remuxer {
constructor(observer, id, config, typeSupported) {
Expand Down Expand Up @@ -175,7 +174,7 @@ class MP4Remuxer {
lastPTS, lastDTS,
inputSamples = track.samples,
outputSamples = [],
PTSNormalize = this._PTSNormalize,
ptsNormalize = this._PTSNormalize,
initDTS = this._initDTS;

// for (let i = 0; i < track.samples.length; i++) {
Expand Down Expand Up @@ -206,7 +205,7 @@ class MP4Remuxer {
}

// PTS is coded on 33bits, and can loop from -2^32 to 2^32
// PTSNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value
// ptsNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value
let nextAvcDts;
// contiguous fragments are consecutive fragments from same quality level (same level, new SN = old SN + 1)
if (contiguous) {
Expand All @@ -219,8 +218,8 @@ class MP4Remuxer {

// compute first DTS and last DTS, normalize them against reference value
let sample = inputSamples[0];
firstDTS = Math.max(PTSNormalize(sample.dts - initDTS,nextAvcDts),0);
firstPTS = Math.max(PTSNormalize(sample.pts - initDTS,nextAvcDts),0);
firstDTS = Math.max(ptsNormalize(sample.dts - initDTS,nextAvcDts),0);
firstPTS = Math.max(ptsNormalize(sample.pts - initDTS,nextAvcDts),0);

// check timestamp continuity accross consecutive fragments (this is to remove inter-fragment gap/hole)
let delta = Math.round((firstDTS - nextAvcDts) / 90);
Expand All @@ -245,8 +244,8 @@ class MP4Remuxer {

// compute lastPTS/lastDTS
sample = inputSamples[inputSamples.length-1];
lastDTS = Math.max(PTSNormalize(sample.dts - initDTS,nextAvcDts) ,0);
lastPTS = Math.max(PTSNormalize(sample.pts - initDTS,nextAvcDts) ,0);
lastDTS = Math.max(ptsNormalize(sample.dts - initDTS,nextAvcDts) ,0);
lastPTS = Math.max(ptsNormalize(sample.pts - initDTS,nextAvcDts) ,0);
lastPTS = Math.max(lastPTS, lastDTS);

let vendor = navigator.vendor, userAgent = navigator.userAgent,
Expand All @@ -267,13 +266,13 @@ class MP4Remuxer {
sample.dts = firstDTS + i*pes2mp4ScaleFactor*mp4SampleDuration;
} else {
// ensure sample monotonic DTS
sample.dts = Math.max(PTSNormalize(sample.dts - initDTS, nextAvcDts),firstDTS);
sample.dts = Math.max(ptsNormalize(sample.dts - initDTS, nextAvcDts),firstDTS);
// ensure dts is a multiple of scale factor to avoid rounding issues
sample.dts = Math.round(sample.dts/pes2mp4ScaleFactor)*pes2mp4ScaleFactor;
}
// we normalize PTS against nextAvcDts, we also substract initDTS (some streams don't start @ PTS O)
// and we ensure that computed value is greater or equal than sample DTS
sample.pts = Math.max(PTSNormalize(sample.pts - initDTS,nextAvcDts) , sample.dts);
sample.pts = Math.max(ptsNormalize(sample.pts - initDTS,nextAvcDts) , sample.dts);
// ensure pts is a multiple of scale factor to avoid rounding issues
sample.pts = Math.round(sample.pts/pes2mp4ScaleFactor)*pes2mp4ScaleFactor;
}
Expand Down Expand Up @@ -395,7 +394,7 @@ class MP4Remuxer {
pes2mp4ScaleFactor = pesTimeScale/mp4timeScale,
expectedSampleDuration = track.timescale * (track.isAAC ? 1024 : 1152) / track.audiosamplerate,
pesFrameDuration = expectedSampleDuration * pes2mp4ScaleFactor,
PTSNormalize = this._PTSNormalize,
ptsNormalize = this._PTSNormalize,
initDTS = this._initDTS;
var view,
offset = 8,
Expand Down Expand Up @@ -444,7 +443,7 @@ class MP4Remuxer {
for (let i = 0, nextPtsNorm = nextAacPts; i < samples0.length; ) {
// First, let's see how far off this frame is from where we expect it to be
var sample = samples0[i],
ptsNorm = PTSNormalize(sample.pts - initDTS, nextAacPts),
ptsNorm = ptsNormalize(sample.pts - initDTS, nextAacPts),
delta = ptsNorm - nextPtsNorm;

// If we're overlapping by more than a duration, drop this sample
Expand All @@ -464,7 +463,7 @@ class MP4Remuxer {
fillFrame = AAC.getSilentFrame(track.channelCount);
if (!fillFrame) {
logger.log('Unable to get silent frame for given audio codec; duplicating last frame instead.');
fillFrame = sample.unit.slice(0);
fillFrame = sample.unit.subarray();
}
samples0.splice(i, 0, {unit: fillFrame, pts: newStamp, dts: newStamp});
track.len += fillFrame.length;
Expand Down Expand Up @@ -502,12 +501,12 @@ class MP4Remuxer {
//logger.log(`Audio/PTS:${Math.round(pts/90)}`);
// if not first sample
if (lastDTS !== undefined) {
ptsnorm = PTSNormalize(pts, lastDTS);
dtsnorm = PTSNormalize(dts, lastDTS);
ptsnorm = ptsNormalize(pts, lastDTS);
dtsnorm = ptsNormalize(dts, lastDTS);
mp4Sample.duration = Math.round((dtsnorm - lastDTS) / pes2mp4ScaleFactor);
} else {
ptsnorm = PTSNormalize(pts, nextAacPts);
dtsnorm = PTSNormalize(dts, nextAacPts);
ptsnorm = ptsNormalize(pts, nextAacPts);
dtsnorm = ptsNormalize(dts, nextAacPts);
let delta = Math.round(1000 * (ptsnorm - nextAacPts) / pesTimeScale),
numMissingFrames = 0;
// if fragment are contiguous, detect hole/overlapping between fragments
Expand All @@ -521,7 +520,7 @@ class MP4Remuxer {
if (numMissingFrames > 0) {
fillFrame = AAC.getSilentFrame(track.channelCount);
if (!fillFrame) {
fillFrame = unit.slice(0);
fillFrame = unit.subarray();
}
track.len += numMissingFrames * fillFrame.length;
}
Expand Down Expand Up @@ -555,7 +554,7 @@ class MP4Remuxer {
fillFrame = AAC.getSilentFrame(track.channelCount);
if (!fillFrame) {
logger.log('Unable to get silent frame for given audio codec; duplicating this frame instead.');
fillFrame = unit.slice(0);
fillFrame = unit.subarray();
}
mdat.set(fillFrame, offset);
offset += fillFrame.byteLength;
Expand Down Expand Up @@ -655,7 +654,7 @@ class MP4Remuxer {
let samples = [];
for(var i = 0; i < nbSamples; i++) {
var stamp = startDTS + i * frameDuration;
samples.push({unit: silentFrame.slice(0), pts: stamp, dts: stamp});
samples.push({unit: silentFrame.subarray(), pts: stamp, dts: stamp});
track.len += silentFrame.length;
}
track.samples = samples;
Expand Down
14 changes: 0 additions & 14 deletions src/utils/polyfill.js

This file was deleted.

0 comments on commit 258911d

Please sign in to comment.