Skip to content

Commit

Permalink
feat: Turn remux on an off dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Jun 24, 2019
1 parent a3fcf47 commit ca7e1c3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
28 changes: 24 additions & 4 deletions lib/mp4/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ var AdtsStream = require('../codecs/adts.js');
var H264Stream = require('../codecs/h264').H264Stream;
var AacStream = require('../aac');
var isLikelyAacData = require('../aac/utils').isLikelyAacData;
var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;

var ONE_SECOND_IN_TS = 90000; // 90kHz clock

// constants
var AUDIO_PROPERTIES = [
Expand Down Expand Up @@ -257,8 +257,7 @@ VideoSegmentStream = function(track, options) {
}, this);
}

if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' &&
!pps) {
if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
pps = nalUnit.data;
track.pps = [nalUnit.data];
}
Expand Down Expand Up @@ -709,14 +708,21 @@ CoalesceStream = function(options, metadataStream) {
// important information required for the construction of
// the final segment
this.pendingTracks.push(output.track);
this.pendingBoxes.push(output.boxes);
this.pendingBytes += output.boxes.byteLength;

// TODO: is there an issue for this against chrome?
// We unshift audio and push video because
// as of Chrome 75 when switching from
// one init segment to another if the video
// mdat does not appear after the audio mdat
// only audio will play for the duration of our transmux.
if (output.track.type === 'video') {
this.videoTrack = output.track;
this.pendingBoxes.push(output.boxes);
}
if (output.track.type === 'audio') {
this.audioTrack = output.track;
this.pendingBoxes.unshift(output.boxes);
}
};
};
Expand Down Expand Up @@ -867,6 +873,10 @@ CoalesceStream.prototype.flush = function(flushSource) {
this.emittedTracks = 0;
}
};

CoalesceStream.prototype.setRemux = function(val) {
this.remuxTracks = val;
};
/**
* A Stream that expects MP2T binary data as input and produces
* corresponding media segments, suitable for use with Media Source
Expand Down Expand Up @@ -1119,6 +1129,16 @@ Transmuxer = function(options) {
}
};

this.setRemux = function(val) {
var pipeline = this.transmuxPipeline_;

options.remux = val;

if (pipeline && pipeline.coalesceStream) {
pipeline.coalesceStream.setRemux(val);
}
};

this.alignGopsWith = function(gopsToAlignWith) {
if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
Expand Down
3 changes: 1 addition & 2 deletions lib/partial/audio-segment-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Stream = require('../utils/stream.js');
var mp4 = require('../mp4/mp4-generator.js');
var audioFrameUtils = require('../mp4/audio-frame-utils');
var trackInfo = require('../mp4/track-decode-info.js');
var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;

// constants
var AUDIO_PROPERTIES = [
Expand All @@ -14,8 +15,6 @@ var AUDIO_PROPERTIES = [
'samplesize'
];

var ONE_SECOND_IN_TS = 90000; // 90kHz clock

/**
* Constructs a single-track, ISO BMFF media segment from AAC data
* events. The output of this stream can be fed to a SourceBuffer
Expand Down
9 changes: 9 additions & 0 deletions lib/partial/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ var Transmuxer = function(options) {
}
};

this.setRemux = function(val) {
options.remux = val;

if (pipeline && pipeline.coalesceStream) {
pipeline.coalesceStream.setRemux(val);
}
};


this.setAudioAppendStart = function(audioAppendStart) {
if (!pipeline || !pipeline.tracks.audio || !pipeline.audioSegmentStream) {
return;
Expand Down
7 changes: 4 additions & 3 deletions test/transmuxer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3738,7 +3738,7 @@ validateTrackFragment = function(track, segment, metadata, type) {
QUnit.test('parses an example mp2t file and generates combined media segments', function() {
var
segments = [],
i, j, boxes, mfhd, trackType = 'video', trackId = 256, baseOffset = 0, initSegment;
i, j, boxes, mfhd, trackType = 'audio', trackId = 257, baseOffset = 0, initSegment;

transmuxer.on('data', function(segment) {
if (segment.type === 'combined') {
Expand Down Expand Up @@ -3780,15 +3780,16 @@ QUnit.test('parses an example mp2t file and generates combined media segments',
}

validateTrackFragment(boxes[i].boxes[1], segments[0].data, {
trackId: trackId++,
trackId: trackId,
width: 388,
height: 300,
baseOffset: baseOffset,
mdatOffset: boxes[i].size
}, trackType);

trackId--;
baseOffset = 0;
trackType = 'audio';
trackType = 'video';
}
}
});
Expand Down

0 comments on commit ca7e1c3

Please sign in to comment.