Skip to content

Commit eb332c1

Browse files
fix: prevent adding duplicate log listeners on every push after a flush (#402)
1 parent 8a58d6e commit eb332c1

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lib/mp4/transmuxer.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ var VIDEO_PROPERTIES = require('../constants/video-properties.js');
2828
// object types
2929
var VideoSegmentStream, AudioSegmentStream, Transmuxer, CoalesceStream;
3030

31+
var retriggerForStream = function(key, event) {
32+
event.stream = key;
33+
this.trigger('log', event);
34+
};
35+
36+
var addPipelineLogRetriggers = function(transmuxer, pipeline) {
37+
var keys = Object.keys(pipeline);
38+
39+
for (var i = 0; i < keys.length; i++) {
40+
var key = keys[i];
41+
42+
// skip non-stream keys and headOfPipeline
43+
// which is just a duplicate
44+
if (key === 'headOfPipeline' || !pipeline[key].on) {
45+
continue;
46+
}
47+
48+
pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
49+
}
50+
};
51+
3152
/**
3253
* Compare two arrays (even typed) for same-ness
3354
*/
@@ -972,6 +993,8 @@ Transmuxer = function(options) {
972993
pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
973994
// Let the consumer know we have finished flushing the entire pipeline
974995
pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
996+
997+
addPipelineLogRetriggers(this, pipeline);
975998
};
976999

9771000
this.setupTsPipeline = function() {
@@ -1107,6 +1130,8 @@ Transmuxer = function(options) {
11071130
pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
11081131
// Let the consumer know we have finished flushing the entire pipeline
11091132
pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
1133+
1134+
addPipelineLogRetriggers(this, pipeline);
11101135
};
11111136

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

1184-
if (this.transmuxPipeline_) {
1185-
var keys = Object.keys(this.transmuxPipeline_);
1186-
1187-
for (var i = 0; i < keys.length; i++) {
1188-
var key = keys[i];
1189-
1190-
// skip non-stream keys and headOfPipeline
1191-
// which is just a duplicate
1192-
if (key === 'headOfPipeline' || !this.transmuxPipeline_[key].on) {
1193-
continue;
1194-
}
1195-
this.transmuxPipeline_[key].on('log', this.getLogTrigger_(key));
1196-
}
1197-
}
11981209
hasFlushed = false;
11991210
}
12001211
this.transmuxPipeline_.headOfPipeline.push(data);

test/transmuxer.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,9 +4034,17 @@ QUnit.test('pipeline retriggers log events', function(assert) {
40344034
})));
40354035
transmuxer.push(packetize(timedMetadataPes([0x03])));
40364036
transmuxer.flush();
4037+
transmuxer.push(packetize(PAT));
4038+
transmuxer.push(packetize(generatePMT({
4039+
hasAudio: true
4040+
})));
4041+
transmuxer.push(packetize(timedMetadataPes([0x03])));
4042+
transmuxer.flush();
40374043
assert.equal(transmuxer.transmuxPipeline_.type, 'ts', 'detected TS file data');
40384044
checkLogs();
40394045

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

0 commit comments

Comments
 (0)