Skip to content

Commit

Permalink
Refer to 608/708 captions via name instead of number (video-dev#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBartos authored Apr 5, 2018
1 parent b80a20d commit fdc4c97
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
57 changes: 36 additions & 21 deletions src/controller/timeline-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,28 @@ class TimelineController extends EventHandler {
this.unparsedVttFrags = [];
this.initPTS = undefined;
this.cueRanges = [];
this.captionsTracks = {};

this.captionsProperties = {
textTrack1: {
label: this.config.captionsTextTrack1Label,
languageCode: this.config.captionsTextTrack1LanguageCode
},
textTrack2: {
label: this.config.captionsTextTrack2Label,
languageCode: this.config.captionsTextTrack2LanguageCode
}
};

if (this.config.enableCEA708Captions) {
let channel1 = new OutputFilter(this, 1);
let channel2 = new OutputFilter(this, 2);
let channel1 = new OutputFilter(this, 'textTrack1');
let channel2 = new OutputFilter(this, 'textTrack2');

this.cea608Parser = new Cea608Parser(0, channel1, channel2);
}
}

addCues (channel, startTime, endTime, screen) {
addCues (trackName, startTime, endTime, screen) {
// skip cues which overlap more than 50% with previously parsed time ranges
const ranges = this.cueRanges;
let merged = false;
Expand All @@ -66,7 +78,7 @@ class TimelineController extends EventHandler {
if (!merged)
ranges.push([startTime, endTime]);

this.Cues.newCue(this[channel], startTime, endTime, screen);
this.Cues.newCue(this.captionsTracks[trackName], startTime, endTime, screen);
}

// Triggered when an initial PTS is found; used for synchronisation of WebVTT.
Expand All @@ -84,35 +96,35 @@ class TimelineController extends EventHandler {
}
}

getExistingTrack (channelNumber) {
const media = this.media;
getExistingTrack (trackName) {
const { media } = this;
if (media) {
for (let i = 0; i < media.textTracks.length; i++) {
let textTrack = media.textTracks[i];
let propName = 'textTrack' + channelNumber;
if (textTrack[propName] === true)
if (textTrack[trackName])
return textTrack;
}
}
return null;
}

createCaptionsTrack (track) {
let trackVar = 'textTrack' + track;
if (!this[trackVar]) {
createCaptionsTrack (trackName) {
const { label, languageCode } = this.captionsProperties[trackName];
const captionsTracks = this.captionsTracks;
if (!captionsTracks[trackName]) {
// Enable reuse of existing text track.
let existingTrack = this.getExistingTrack(track);
const existingTrack = this.getExistingTrack(trackName);
if (!existingTrack) {
const textTrack = this.createTextTrack('captions', this.config['captionsTextTrack' + track + 'Label'], this.config['captionsTextTrack' + track + 'LanguageCode']);
const textTrack = this.createTextTrack('captions', label, languageCode);
if (textTrack) {
textTrack[trackVar] = true;
this[trackVar] = textTrack;
// Set a special property on the track so we know it's managed by Hls.js
textTrack[trackName] = true;
captionsTracks[trackName] = textTrack;
}
} else {
this[trackVar] = existingTrack;
clearCurrentCues(this[trackVar]);

sendAddTrackEvent(this[trackVar], this.media);
captionsTracks[trackName] = existingTrack;
clearCurrentCues(captionsTracks[trackName]);
sendAddTrackEvent(captionsTracks[trackName], this.media);
}
}
}
Expand All @@ -133,8 +145,11 @@ class TimelineController extends EventHandler {
}

onMediaDetaching () {
clearCurrentCues(this.textTrack1);
clearCurrentCues(this.textTrack2);
const { captionsTracks } = this;
Object.keys(captionsTracks).forEach(trackName => {
clearCurrentCues(captionsTracks[trackName]);
delete captionsTracks[trackName];
});
}

onManifestLoading () {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/output-filter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default class OutputFilter {
constructor (timelineController, track) {
constructor (timelineController, trackName) {
this.timelineController = timelineController;
this.track = track;
this.trackName = trackName;
this.startTime = null;
this.endTime = null;
this.screen = null;
Expand All @@ -11,7 +11,7 @@ export default class OutputFilter {
if (this.startTime === null)
return;

this.timelineController.addCues('textTrack' + this.track, this.startTime, this.endTime, this.screen);
this.timelineController.addCues(this.trackName, this.startTime, this.endTime, this.screen);
this.startTime = null;
}

Expand All @@ -21,6 +21,6 @@ export default class OutputFilter {

this.endTime = endTime;
this.screen = screen;
this.timelineController.createCaptionsTrack(this.track);
this.timelineController.createCaptionsTrack(this.trackName);
}
}

0 comments on commit fdc4c97

Please sign in to comment.