text-tracks.js
8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* @file text-tracks.js
*/
import window from 'global/window';
import videojs from 'video.js';
/**
* Create captions text tracks on video.js if they do not exist
*
* @param {Object} inbandTextTracks a reference to current inbandTextTracks
* @param {Object} tech the video.js tech
* @param {Object} captionStream the caption stream to create
* @private
*/
export const createCaptionsTrackIfNotExists = function(inbandTextTracks, tech, captionStream) {
if (!inbandTextTracks[captionStream]) {
tech.trigger({type: 'usage', name: 'vhs-608'});
tech.trigger({type: 'usage', name: 'hls-608'});
let instreamId = captionStream;
// we need to translate SERVICEn for 708 to how mux.js currently labels them
if (/^cc708_/.test(captionStream)) {
instreamId = 'SERVICE' + captionStream.split('_')[1];
}
const track = tech.textTracks().getTrackById(instreamId);
if (track) {
// Resuse an existing track with a CC# id because this was
// very likely created by videojs-contrib-hls from information
// in the m3u8 for us to use
inbandTextTracks[captionStream] = track;
} else {
// This section gets called when we have caption services that aren't specified in the manifest.
// Manifest level caption services are handled in media-groups.js under CLOSED-CAPTIONS.
const captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
let label = captionStream;
let language = captionStream;
let def = false;
const captionService = captionServices[instreamId];
if (captionService) {
label = captionService.label;
language = captionService.language;
def = captionService.default;
}
// Otherwise, create a track with the default `CC#` label and
// without a language
inbandTextTracks[captionStream] = tech.addRemoteTextTrack({
kind: 'captions',
id: instreamId,
// TODO: investigate why this doesn't seem to turn the caption on by default
default: def,
label,
language
}, false).track;
}
}
};
/**
* Add caption text track data to a source handler given an array of captions
*
* @param {Object}
* @param {Object} inbandTextTracks the inband text tracks
* @param {number} timestampOffset the timestamp offset of the source buffer
* @param {Array} captionArray an array of caption data
* @private
*/
export const addCaptionData = function({
inbandTextTracks,
captionArray,
timestampOffset
}) {
if (!captionArray) {
return;
}
const Cue = window.WebKitDataCue || window.VTTCue;
captionArray.forEach((caption) => {
const track = caption.stream;
inbandTextTracks[track].addCue(new Cue(
caption.startTime + timestampOffset,
caption.endTime + timestampOffset,
caption.text
));
});
};
/**
* Define properties on a cue for backwards compatability,
* but warn the user that the way that they are using it
* is depricated and will be removed at a later date.
*
* @param {Cue} cue the cue to add the properties on
* @private
*/
const deprecateOldCue = function(cue) {
Object.defineProperties(cue.frame, {
id: {
get() {
videojs.log.warn('cue.frame.id is deprecated. Use cue.value.key instead.');
return cue.value.key;
}
},
value: {
get() {
videojs.log.warn('cue.frame.value is deprecated. Use cue.value.data instead.');
return cue.value.data;
}
},
privateData: {
get() {
videojs.log.warn('cue.frame.privateData is deprecated. Use cue.value.data instead.');
return cue.value.data;
}
}
});
};
/**
* Add metadata text track data to a source handler given an array of metadata
*
* @param {Object}
* @param {Object} inbandTextTracks the inband text tracks
* @param {Array} metadataArray an array of meta data
* @param {number} timestampOffset the timestamp offset of the source buffer
* @param {number} videoDuration the duration of the video
* @private
*/
export const addMetadata = ({
inbandTextTracks,
metadataArray,
timestampOffset,
videoDuration
}) => {
if (!metadataArray) {
return;
}
const Cue = window.WebKitDataCue || window.VTTCue;
const metadataTrack = inbandTextTracks.metadataTrack_;
if (!metadataTrack) {
return;
}
metadataArray.forEach((metadata) => {
const time = metadata.cueTime + timestampOffset;
// if time isn't a finite number between 0 and Infinity, like NaN,
// ignore this bit of metadata.
// This likely occurs when you have an non-timed ID3 tag like TIT2,
// which is the "Title/Songname/Content description" frame
if (typeof time !== 'number' || window.isNaN(time) || time < 0 || !(time < Infinity)) {
return;
}
metadata.frames.forEach((frame) => {
const cue = new Cue(
time,
time,
frame.value || frame.url || frame.data || ''
);
cue.frame = frame;
cue.value = frame;
deprecateOldCue(cue);
metadataTrack.addCue(cue);
});
});
if (!metadataTrack.cues || !metadataTrack.cues.length) {
return;
}
// Updating the metadeta cues so that
// the endTime of each cue is the startTime of the next cue
// the endTime of last cue is the duration of the video
const cues = metadataTrack.cues;
const cuesArray = [];
// Create a copy of the TextTrackCueList...
// ...disregarding cues with a falsey value
for (let i = 0; i < cues.length; i++) {
if (cues[i]) {
cuesArray.push(cues[i]);
}
}
// Group cues by their startTime value
const cuesGroupedByStartTime = cuesArray.reduce((obj, cue) => {
const timeSlot = obj[cue.startTime] || [];
timeSlot.push(cue);
obj[cue.startTime] = timeSlot;
return obj;
}, {});
// Sort startTimes by ascending order
const sortedStartTimes = Object.keys(cuesGroupedByStartTime)
.sort((a, b) => Number(a) - Number(b));
// Map each cue group's endTime to the next group's startTime
sortedStartTimes.forEach((startTime, idx) => {
const cueGroup = cuesGroupedByStartTime[startTime];
const nextTime = Number(sortedStartTimes[idx + 1]) || videoDuration;
// Map each cue's endTime the next group's startTime
cueGroup.forEach((cue) => {
cue.endTime = nextTime;
});
});
};
/**
* Create metadata text track on video.js if it does not exist
*
* @param {Object} inbandTextTracks a reference to current inbandTextTracks
* @param {string} dispatchType the inband metadata track dispatch type
* @param {Object} tech the video.js tech
* @private
*/
export const createMetadataTrackIfNotExists = (inbandTextTracks, dispatchType, tech) => {
if (inbandTextTracks.metadataTrack_) {
return;
}
inbandTextTracks.metadataTrack_ = tech.addRemoteTextTrack({
kind: 'metadata',
label: 'Timed Metadata'
}, false).track;
inbandTextTracks.metadataTrack_.inBandMetadataTrackDispatchType = dispatchType;
};
/**
* Remove cues from a track on video.js.
*
* @param {Double} start start of where we should remove the cue
* @param {Double} end end of where the we should remove the cue
* @param {Object} track the text track to remove the cues from
* @private
*/
export const removeCuesFromTrack = function(start, end, track) {
let i;
let cue;
if (!track) {
return;
}
if (!track.cues) {
return;
}
i = track.cues.length;
while (i--) {
cue = track.cues[i];
// Remove any cue within the provided start and end time
if (cue.startTime >= start && cue.endTime <= end) {
track.removeCue(cue);
}
}
};
/**
* Remove duplicate cues from a track on video.js (a cue is considered a
* duplicate if it has the same time interval and text as another)
*
* @param {Object} track the text track to remove the duplicate cues from
* @private
*/
export const removeDuplicateCuesFromTrack = function(track) {
const cues = track.cues;
if (!cues) {
return;
}
for (let i = 0; i < cues.length; i++) {
const duplicates = [];
let occurrences = 0;
for (let j = 0; j < cues.length; j++) {
if (
cues[i].startTime === cues[j].startTime &&
cues[i].endTime === cues[j].endTime &&
cues[i].text === cues[j].text
) {
occurrences++;
if (occurrences > 1) {
duplicates.push(cues[j]);
}
}
}
if (duplicates.length) {
duplicates.forEach(dupe => track.removeCue(dupe));
}
}
};