Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent adding duplicate log listeners #402

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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