Skip to content

Commit

Permalink
fix: prevent adding duplicate log listeners on every push after a flu…
Browse files Browse the repository at this point in the history
…sh (#402)
  • Loading branch information
brandonocasey authored Oct 14, 2021
1 parent 8a58d6e commit eb332c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
39 changes: 25 additions & 14 deletions lib/mp4/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ var VIDEO_PROPERTIES = require('../constants/video-properties.js');
// object types
var VideoSegmentStream, AudioSegmentStream, Transmuxer, CoalesceStream;

var retriggerForStream = function(key, event) {
event.stream = key;
this.trigger('log', event);
};

var addPipelineLogRetriggers = function(transmuxer, pipeline) {
var keys = Object.keys(pipeline);

for (var i = 0; i < keys.length; i++) {
var key = keys[i];

// skip non-stream keys and headOfPipeline
// which is just a duplicate
if (key === 'headOfPipeline' || !pipeline[key].on) {
continue;
}

pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
}
};

/**
* Compare two arrays (even typed) for same-ness
*/
Expand Down Expand Up @@ -972,6 +993,8 @@ Transmuxer = function(options) {
pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
// Let the consumer know we have finished flushing the entire pipeline
pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));

addPipelineLogRetriggers(this, pipeline);
};

this.setupTsPipeline = function() {
Expand Down Expand Up @@ -1107,6 +1130,8 @@ Transmuxer = function(options) {
pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
// Let the consumer know we have finished flushing the entire pipeline
pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));

addPipelineLogRetriggers(this, pipeline);
};

// hook up the segment streams once track metadata is delivered
Expand Down Expand Up @@ -1181,20 +1206,6 @@ Transmuxer = function(options) {
this.setupTsPipeline();
}

if (this.transmuxPipeline_) {
var keys = Object.keys(this.transmuxPipeline_);

for (var i = 0; i < keys.length; i++) {
var key = keys[i];

// skip non-stream keys and headOfPipeline
// which is just a duplicate
if (key === 'headOfPipeline' || !this.transmuxPipeline_[key].on) {
continue;
}
this.transmuxPipeline_[key].on('log', this.getLogTrigger_(key));
}
}
hasFlushed = false;
}
this.transmuxPipeline_.headOfPipeline.push(data);
Expand Down
8 changes: 8 additions & 0 deletions test/transmuxer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4034,9 +4034,17 @@ QUnit.test('pipeline retriggers log events', function(assert) {
})));
transmuxer.push(packetize(timedMetadataPes([0x03])));
transmuxer.flush();
transmuxer.push(packetize(PAT));
transmuxer.push(packetize(generatePMT({
hasAudio: true
})));
transmuxer.push(packetize(timedMetadataPes([0x03])));
transmuxer.flush();
assert.equal(transmuxer.transmuxPipeline_.type, 'ts', 'detected TS file data');
checkLogs();

transmuxer.push(new Uint8Array(id3.id3Tag(id3.id3Frame('PRIV', 0x00, 0x01)).concat([0xFF, 0xF1])));
transmuxer.flush();
transmuxer.push(new Uint8Array(id3.id3Tag(id3.id3Frame('PRIV', 0x00, 0x01)).concat([0xFF, 0xF1])));
transmuxer.flush();
assert.equal(transmuxer.transmuxPipeline_.type, 'aac', 'detected AAC file data');
Expand Down

0 comments on commit eb332c1

Please sign in to comment.