Skip to content

Commit 8a7cdb6

Browse files
authored
feat(CaptionStream): add flag to turn off 708 captions (#365)
Pass `parse708captions` to transmuxer or CaptionStream to turn off parsing for 708 captions.
1 parent fa920a6 commit 8a7cdb6

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

lib/flv/transmuxer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Transmuxer = function(options) {
373373
.pipe(this.metadataStream)
374374
.pipe(coalesceStream);
375375
// if CEA-708 parsing is available, hook up a caption stream
376-
captionStream = new m2ts.CaptionStream();
376+
captionStream = new m2ts.CaptionStream(options);
377377
h264Stream.pipe(captionStream)
378378
.pipe(coalesceStream);
379379

lib/m2ts/caption-stream.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
var Stream = require('../utils/stream');
2121
var cea708Parser = require('../tools/caption-packet-parser');
2222

23-
var CaptionStream = function() {
23+
var CaptionStream = function(options) {
24+
options = options || {};
2425

2526
CaptionStream.prototype.init.call(this);
2627

28+
// parse708captions flag, default to true
29+
this.parse708captions_ = typeof options.parse708captions === 'boolean' ?
30+
options.parse708captions :
31+
true;
32+
2733
this.captionPackets_ = [];
2834

2935
this.ccStreams_ = [
@@ -33,7 +39,9 @@ var CaptionStream = function() {
3339
new Cea608Stream(1, 1) // eslint-disable-line no-use-before-define
3440
];
3541

36-
this.cc708Stream_ = new Cea708Stream(); // eslint-disable-line no-use-before-define
42+
if (this.parse708captions_) {
43+
this.cc708Stream_ = new Cea708Stream(); // eslint-disable-line no-use-before-define
44+
}
3745

3846
this.reset();
3947

@@ -44,9 +52,11 @@ var CaptionStream = function() {
4452
cc.on('done', this.trigger.bind(this, 'done'));
4553
}, this);
4654

47-
this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
48-
this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
49-
this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
55+
if (this.parse708captions_) {
56+
this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
57+
this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
58+
this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
59+
}
5060

5161
};
5262

@@ -213,7 +223,9 @@ CaptionStream.prototype.setsTextOrXDSActive = function(packet) {
213223
};
214224

215225
CaptionStream.prototype.dispatchCea708Packet = function(packet) {
216-
this.cc708Stream_.push(packet);
226+
if (this.parse708captions_) {
227+
this.cc708Stream_.push(packet);
228+
}
217229
};
218230

219231

lib/mp4/transmuxer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ Transmuxer = function(options) {
982982
pipeline.timestampRolloverStream = new m2ts.TimestampRolloverStream();
983983
pipeline.adtsStream = new AdtsStream();
984984
pipeline.h264Stream = new H264Stream();
985-
pipeline.captionStream = new m2ts.CaptionStream();
985+
pipeline.captionStream = new m2ts.CaptionStream(options);
986986
pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
987987
pipeline.headOfPipeline = pipeline.packetStream;
988988

lib/partial/transmuxer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var tsPipeline = function(options) {
3030
timestampRollover: new m2ts.TimestampRolloverStream(),
3131
adts: new codecs.Adts(),
3232
h264: new codecs.h264.H264Stream(),
33-
captionStream: new m2ts.CaptionStream(),
33+
captionStream: new m2ts.CaptionStream(options),
3434
metadataStream: new m2ts.MetadataStream()
3535
};
3636

test/caption-stream.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ QUnit.test('clears buffer and drops data until first command that sets activeCha
883883
assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
884884
});
885885

886-
QUnit.test('ignores CEA708 captions', function(assert) {
886+
QUnit.test("don't mess up 608 captions when 708 are present", function(assert) {
887887
var captions = [];
888888
captionStream.ccStreams_.forEach(function(cc) {
889889
cc.on('data', function(caption) {
@@ -902,6 +902,48 @@ QUnit.test('ignores CEA708 captions', function(assert) {
902902
assert.equal(captions[2].text, 'WE TRY NOT TO PUT AN ANIMAL DOWN\nIF WE DON\'T HAVE TO.', 'parsed third caption correctly');
903903
});
904904

905+
QUnit.test("both 608 and 708 captions are available by default", function(assert) {
906+
var cc608 = [];
907+
var cc708 = [];
908+
captionStream.on('data', function(caption) {
909+
if (caption.stream === 'CC1') {
910+
cc608.push(caption);
911+
} else {
912+
cc708.push(caption);
913+
}
914+
});
915+
916+
var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
917+
seiNals.forEach(captionStream.push, captionStream);
918+
captionStream.flush();
919+
920+
assert.equal(cc608.length, 3, 'parsed three 608 cues');
921+
assert.equal(cc708.length, 3, 'parsed three 708 cues');
922+
});
923+
924+
QUnit.test("708 parsing can be turned off", function(assert) {
925+
captionStream.reset();
926+
captionStream = new m2ts.CaptionStream({
927+
parse708captions: false
928+
});
929+
var cc608 = [];
930+
var cc708 = [];
931+
captionStream.on('data', function(caption) {
932+
if (caption.stream === 'CC1') {
933+
cc608.push(caption);
934+
} else {
935+
cc708.push(caption);
936+
}
937+
});
938+
939+
var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
940+
seiNals.forEach(captionStream.push, captionStream);
941+
captionStream.flush();
942+
943+
assert.equal(cc608.length, 3, 'parsed three 608 cues');
944+
assert.equal(cc708.length, 0, 'did not parse any 708 cues');
945+
});
946+
905947
QUnit.test('ignores XDS and Text packets', function(assert) {
906948
var captions = [];
907949

0 commit comments

Comments
 (0)