Skip to content

Commit

Permalink
feat(CaptionStream): add flag to turn off 708 captions (#365)
Browse files Browse the repository at this point in the history
Pass `parse708captions` to transmuxer or CaptionStream to turn off parsing for 708 captions.
  • Loading branch information
gkatsev committed Jan 20, 2021
1 parent fa920a6 commit 8a7cdb6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/flv/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ Transmuxer = function(options) {
.pipe(this.metadataStream)
.pipe(coalesceStream);
// if CEA-708 parsing is available, hook up a caption stream
captionStream = new m2ts.CaptionStream();
captionStream = new m2ts.CaptionStream(options);
h264Stream.pipe(captionStream)
.pipe(coalesceStream);

Expand Down
24 changes: 18 additions & 6 deletions lib/m2ts/caption-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
var Stream = require('../utils/stream');
var cea708Parser = require('../tools/caption-packet-parser');

var CaptionStream = function() {
var CaptionStream = function(options) {
options = options || {};

CaptionStream.prototype.init.call(this);

// parse708captions flag, default to true
this.parse708captions_ = typeof options.parse708captions === 'boolean' ?
options.parse708captions :
true;

this.captionPackets_ = [];

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

this.cc708Stream_ = new Cea708Stream(); // eslint-disable-line no-use-before-define
if (this.parse708captions_) {
this.cc708Stream_ = new Cea708Stream(); // eslint-disable-line no-use-before-define
}

this.reset();

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

this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
if (this.parse708captions_) {
this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
}

};

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

CaptionStream.prototype.dispatchCea708Packet = function(packet) {
this.cc708Stream_.push(packet);
if (this.parse708captions_) {
this.cc708Stream_.push(packet);
}
};


Expand Down
2 changes: 1 addition & 1 deletion lib/mp4/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ Transmuxer = function(options) {
pipeline.timestampRolloverStream = new m2ts.TimestampRolloverStream();
pipeline.adtsStream = new AdtsStream();
pipeline.h264Stream = new H264Stream();
pipeline.captionStream = new m2ts.CaptionStream();
pipeline.captionStream = new m2ts.CaptionStream(options);
pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
pipeline.headOfPipeline = pipeline.packetStream;

Expand Down
2 changes: 1 addition & 1 deletion lib/partial/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var tsPipeline = function(options) {
timestampRollover: new m2ts.TimestampRolloverStream(),
adts: new codecs.Adts(),
h264: new codecs.h264.H264Stream(),
captionStream: new m2ts.CaptionStream(),
captionStream: new m2ts.CaptionStream(options),
metadataStream: new m2ts.MetadataStream()
};

Expand Down
44 changes: 43 additions & 1 deletion test/caption-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ QUnit.test('clears buffer and drops data until first command that sets activeCha
assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
});

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

QUnit.test("both 608 and 708 captions are available by default", function(assert) {
var cc608 = [];
var cc708 = [];
captionStream.on('data', function(caption) {
if (caption.stream === 'CC1') {
cc608.push(caption);
} else {
cc708.push(caption);
}
});

var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();

assert.equal(cc608.length, 3, 'parsed three 608 cues');
assert.equal(cc708.length, 3, 'parsed three 708 cues');
});

QUnit.test("708 parsing can be turned off", function(assert) {
captionStream.reset();
captionStream = new m2ts.CaptionStream({
parse708captions: false
});
var cc608 = [];
var cc708 = [];
captionStream.on('data', function(caption) {
if (caption.stream === 'CC1') {
cc608.push(caption);
} else {
cc708.push(caption);
}
});

var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();

assert.equal(cc608.length, 3, 'parsed three 608 cues');
assert.equal(cc708.length, 0, 'did not parse any 708 cues');
});

QUnit.test('ignores XDS and Text packets', function(assert) {
var captions = [];

Expand Down

0 comments on commit 8a7cdb6

Please sign in to comment.