Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
Support for multiple CEA608 tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
squarebracket committed Apr 11, 2017
1 parent b59b111 commit 2f99900
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 42 deletions.
5 changes: 4 additions & 1 deletion src/add-text-track-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ const addTextTrackData = function(sourceHandler, captionArray, metadataArray) {

if (captionArray) {
captionArray.forEach(function(caption) {
this.inbandTextTrack_.addCue(
// Support mux.js which only supports CC1
let track = caption.track || 1;

this.inbandTextTracks_[track].addCue(
new Cue(
caption.startTime + this.timestampOffset,
caption.endTime + this.timestampOffset,
Expand Down
23 changes: 16 additions & 7 deletions src/create-text-tracks-if-necessary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ const createTextTracksIfNecessary = function(sourceBuffer, mediaSource, segment)

// create an in-band caption track if one is present in the segment
if (segment.captions &&
segment.captions.length &&
!sourceBuffer.inbandTextTrack_) {
removeExistingTrack(player, 'captions', 'cc1');
sourceBuffer.inbandTextTrack_ = player.addRemoteTextTrack({
kind: 'captions',
label: 'cc1'
}, false).track;
segment.captions.length) {
if (!sourceBuffer.inbandTextTracks_) {
sourceBuffer.inbandTextTracks_ = {};
}
segment.captions.forEach(function(caption) {
// Support mux.js which only supports CC1
let track = caption.track || '1';

if (!sourceBuffer.inbandTextTracks_[track]) {
removeExistingTrack(player, 'captions', 'cc' + track);
sourceBuffer.inbandTextTracks_[track] = player.addRemoteTextTrack({
kind: 'captions',
label: 'cc' + track
}, false).track;
}
});
}

if (segment.metadata &&
Expand Down
8 changes: 6 additions & 2 deletions src/flash-source-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export default class FlashSourceBuffer extends videojs.EventTarget {
// of a buffered-range and everything else is reset on seek
this.mediaSource_.player_.on('seeked', () => {
removeCuesFromTrack(0, Infinity, this.metadataTrack_);
removeCuesFromTrack(0, Infinity, this.inbandTextTrack_);
for (let track in this.inbandTextTracks_) {
removeCuesFromTrack(0, Infinity, this.inbandTextTracks_[track]);
}
});

this.mediaSource_.player_.tech_.hls.on('dispose', () => {
Expand Down Expand Up @@ -245,7 +247,9 @@ export default class FlashSourceBuffer extends videojs.EventTarget {
*/
remove(start, end) {
removeCuesFromTrack(start, end, this.metadataTrack_);
removeCuesFromTrack(start, end, this.inbandTextTrack_);
for (let track in this.inbandTextTracks_) {
removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
}
this.trigger({ type: 'update' });
this.trigger({ type: 'updateend' });
}
Expand Down
6 changes: 5 additions & 1 deletion src/virtual-source-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ export default class VirtualSourceBuffer extends videojs.EventTarget {
removeCuesFromTrack(start, end, this.metadataTrack_);

// Remove Any Captions
removeCuesFromTrack(start, end, this.inbandTextTrack_);
if (this.inbandTextTracks_) {
for (let track in this.inbandTextTracks_) {
removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
}
}
}

/**
Expand Down
44 changes: 39 additions & 5 deletions test/add-text-track-data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class MockTextTrack {
module('Text Track Data', {
beforeEach() {
this.sourceHandler = {
inbandTextTrack_: new MockTextTrack(),
inbandTextTracks_: {
1: new MockTextTrack(),
2: new MockTextTrack(),
3: new MockTextTrack(),
4: new MockTextTrack()
},
metadataTrack_: new MockTextTrack(),
mediaSource_: {
duration: NaN
Expand All @@ -27,17 +32,46 @@ module('Text Track Data', {

test('does nothing if no cues are specified', function() {
addTextTrackData(this.sourceHandler, [], []);
equal(this.sourceHandler.inbandTextTrack_.cues.length, 0, 'added no 608 cues');
equal(this.sourceHandler.inbandTextTracks_[1].cues.length, 0, 'added no 608 cues');
equal(this.sourceHandler.metadataTrack_.cues.length, 0, 'added no metadata cues');
});

test('creates cues for 608 captions', function() {
test('creates cues for 608 captions with no track property in cc1', function() {
addTextTrackData(this.sourceHandler, [{
startTime: 0,
endTime: 1,
text: 'caption text'
}], []);
equal(this.sourceHandler.inbandTextTrack_.cues.length, 1, 'added one 608 cues');
equal(this.sourceHandler.inbandTextTracks_[1].cues.length, 1, 'added one 608 cues to cc1');
equal(this.sourceHandler.metadataTrack_.cues.length, 0, 'added no metadata cues');
});

test('creates cues for 608 captions with track property in ccX', function() {
addTextTrackData(this.sourceHandler, [{
startTime: 0,
endTime: 1,
text: 'cc1 text',
track: 1
}, {
startTime: 0,
endTime: 1,
text: 'cc2 text',
track: 2
}, {
startTime: 0,
endTime: 1,
text: 'cc3 text',
track: 3
}, {
startTime: 0,
endTime: 1,
text: 'cc4 text',
track: 4
}], []);
equal(this.sourceHandler.inbandTextTracks_[1].cues.length, 1, 'added one 608 cues to cc1');
equal(this.sourceHandler.inbandTextTracks_[2].cues.length, 1, 'added one 608 cues to cc2');
equal(this.sourceHandler.inbandTextTracks_[3].cues.length, 1, 'added one 608 cues to cc3');
equal(this.sourceHandler.inbandTextTracks_[4].cues.length, 1, 'added one 608 cues to cc4');
equal(this.sourceHandler.metadataTrack_.cues.length, 0, 'added no metadata cues');
});

Expand All @@ -46,6 +80,6 @@ test('creates cues for timed metadata', function() {
cueTime: 1,
frames: [{}]
}]);
equal(this.sourceHandler.inbandTextTrack_.cues.length, 0, 'added no 608 cues');
equal(this.sourceHandler.inbandTextTracks_[1].cues.length, 0, 'added no 608 cues');
equal(this.sourceHandler.metadataTrack_.cues.length, 1, 'added one metadata cues');
});
58 changes: 32 additions & 26 deletions test/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,17 @@ function() {

initializeNativeSourceBuffers(sourceBuffer);

sourceBuffer.inbandTextTrack_ = {
removeCue(cue) {
removedCue.push(cue);
this.cues.splice(this.cues.indexOf(cue), 1);
},
cues: [
{startTime: 10, endTime: 20, text: 'delete me'},
{startTime: 0, endTime: 2, text: 'save me'}
]
sourceBuffer.inbandTextTracks_ = {
1: {
removeCue(cue) {
removedCue.push(cue);
this.cues.splice(this.cues.indexOf(cue), 1);
},
cues: [
{startTime: 10, endTime: 20, text: 'delete me'},
{startTime: 0, endTime: 2, text: 'save me'}
]
}
};
mediaSource.videoBuffer_.remove = function(start, end) {
if (start === 3 && end === 10) {
Expand All @@ -244,7 +246,7 @@ function() {

QUnit.equal(removes, 2, 'called remove on both sourceBuffers');
QUnit.equal(
sourceBuffer.inbandTextTrack_.cues.length,
sourceBuffer.inbandTextTracks_[1].cues.length,
1,
'one cue remains after remove'
);
Expand All @@ -263,8 +265,10 @@ function() {

initializeNativeSourceBuffers(sourceBuffer);

sourceBuffer.inbandTextTrack_ = {
cues: null
sourceBuffer.inbandTextTracks_ = {
1: {
cues: null
}
};

mediaSource.videoBuffer_.remove = function(start, end) {
Expand All @@ -278,7 +282,7 @@ function() {
sourceBuffer.remove(3, 10);

QUnit.equal(
sourceBuffer.inbandTextTrack_.cues,
sourceBuffer.inbandTextTracks_[1].cues,
null,
'cues are still null'
);
Expand All @@ -294,15 +298,17 @@ QUnit.test('removing works even with audio disabled', function() {

initializeNativeSourceBuffers(muxedBuffer);

muxedBuffer.inbandTextTrack_ = {
removeCue(cue) {
removedCue.push(cue);
this.cues.splice(this.cues.indexOf(cue), 1);
},
cues: [
{startTime: 10, endTime: 20, text: 'delete me'},
{startTime: 0, endTime: 2, text: 'save me'}
]
muxedBuffer.inbandTextTracks_ = {
1: {
removeCue(cue) {
removedCue.push(cue);
this.cues.splice(this.cues.indexOf(cue), 1);
},
cues: [
{startTime: 10, endTime: 20, text: 'delete me'},
{startTime: 0, endTime: 2, text: 'save me'}
]
}
};
mediaSource.videoBuffer_.remove = function(start, end) {
if (start === 3 && end === 10) {
Expand All @@ -318,7 +324,7 @@ QUnit.test('removing works even with audio disabled', function() {
muxedBuffer.remove(3, 10);

QUnit.equal(removes, 2, 'called remove on both muxedBuffers');
QUnit.equal(muxedBuffer.inbandTextTrack_.cues.length,
QUnit.equal(muxedBuffer.inbandTextTracks_[1].cues.length,
1,
'one cue remains after remove');
QUnit.equal(removedCue[0].text,
Expand Down Expand Up @@ -1057,16 +1063,16 @@ QUnit.test('translates caption events into WebVTT cues', function() {
captions: [{
startTime: 1,
endTime: 3,
text: 'This is an in-band caption'
text: 'This is an in-band caption in cc1'
}]
}));
sourceBuffer.transmuxer_.onmessage(doneMessage);
let cues = sourceBuffer.inbandTextTrack_.cues;
let cues = sourceBuffer.inbandTextTracks_[1].cues;

QUnit.equal(types.length, 1, 'created one text track');
QUnit.equal(types[0], 'captions', 'the type was captions');
QUnit.equal(cues.length, 1, 'created one cue');
QUnit.equal(cues[0].text, 'This is an in-band caption', 'included the text');
QUnit.equal(cues[0].text, 'This is an in-band caption in cc1', 'included the text');
QUnit.equal(cues[0].startTime, 11, 'started at eleven');
QUnit.equal(cues[0].endTime, 13, 'ended at thirteen');
});
Expand Down

0 comments on commit 2f99900

Please sign in to comment.