forked from shaka-project/shaka-player
/
text_engine.js
469 lines (412 loc) · 14.3 KB
/
text_engine.js
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.text.TextEngine');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.text.Cue');
goog.require('shaka.util.BufferUtils');
goog.require('shaka.util.Functional');
goog.require('shaka.util.IDestroyable');
goog.require('shaka.util.MimeUtils');
goog.requireType('shaka.cea.ICaptionDecoder');
// TODO: revisit this when Closure Compiler supports partially-exported classes.
/**
* @summary Manages text parsers and cues.
* @implements {shaka.util.IDestroyable}
* @export
*/
shaka.text.TextEngine = class {
/** @param {shaka.extern.TextDisplayer} displayer */
constructor(displayer) {
/** @private {?shaka.extern.TextParser} */
this.parser_ = null;
/** @private {shaka.extern.TextDisplayer} */
this.displayer_ = displayer;
/** @private {boolean} */
this.segmentRelativeVttTiming_ = false;
/** @private {number} */
this.timestampOffset_ = 0;
/** @private {number} */
this.appendWindowStart_ = 0;
/** @private {number} */
this.appendWindowEnd_ = Infinity;
/** @private {?number} */
this.bufferStart_ = null;
/** @private {?number} */
this.bufferEnd_ = null;
/** @private {string} */
this.selectedClosedCaptionId_ = '';
/**
* The closed captions map stores the CEA closed captions by closed captions
* id and start and end time.
* It's used as the buffer of closed caption text streams, to show captions
* when we start displaying captions or switch caption tracks, we need to be
* able to get the cues for the other language and display them without
* re-fetching the video segments they were embedded in.
* Structure of closed caption map:
* closed caption id -> {start and end time -> cues}
* @private {!Map.<string, !Map.<string, !Array.<shaka.text.Cue>>>} */
this.closedCaptionsMap_ = new Map();
}
/**
* @param {string} mimeType
* @param {!shaka.extern.TextParserPlugin} plugin
* @export
*/
static registerParser(mimeType, plugin) {
shaka.text.TextEngine.parserMap_[mimeType] = plugin;
}
/**
* @param {string} mimeType
* @export
*/
static unregisterParser(mimeType) {
delete shaka.text.TextEngine.parserMap_[mimeType];
}
/**
* @return {?shaka.extern.TextParserPlugin}
* @export
*/
static findParser(mimeType) {
return shaka.text.TextEngine.parserMap_[mimeType];
}
/**
* @param {string} mimeType
* @return {boolean}
*/
static isTypeSupported(mimeType) {
if (shaka.text.TextEngine.parserMap_[mimeType]) {
// An actual parser is available.
return true;
}
if (mimeType == shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE ||
mimeType == shaka.util.MimeUtils.CEA708_CLOSED_CAPTION_MIMETYPE ) {
// Closed captions.
return true;
}
return false;
}
// TODO: revisit this when the compiler supports partially-exported classes.
/**
* @override
* @export
*/
destroy() {
this.parser_ = null;
this.displayer_ = null;
this.closedCaptionsMap_.clear();
return Promise.resolve();
}
/**
* @param {!shaka.extern.TextDisplayer} displayer
*/
setDisplayer(displayer) {
this.displayer_ = displayer;
}
/**
* Initialize the parser. This can be called multiple times, but must be
* called at least once before appendBuffer.
*
* @param {string} mimeType
* @param {boolean} sequenceMode
* @param {boolean} segmentRelativeVttTiming
*/
initParser(mimeType, sequenceMode, segmentRelativeVttTiming) {
// No parser for CEA, which is extracted from video and side-loaded
// into TextEngine and TextDisplayer.
if (mimeType == shaka.util.MimeUtils.CEA608_CLOSED_CAPTION_MIMETYPE ||
mimeType == shaka.util.MimeUtils.CEA708_CLOSED_CAPTION_MIMETYPE) {
return;
}
const factory = shaka.text.TextEngine.parserMap_[mimeType];
goog.asserts.assert(
factory, 'Text type negotiation should have happened already');
this.parser_ = shaka.util.Functional.callFactory(factory);
if (this.parser_.setSequenceMode) {
this.parser_.setSequenceMode(sequenceMode);
} else {
shaka.log.alwaysWarn(
'Text parsers should have a "setSequenceMode" method!');
}
this.segmentRelativeVttTiming_ = segmentRelativeVttTiming;
}
/**
* @param {BufferSource} buffer
* @param {?number} startTime relative to the start of the presentation
* @param {?number} endTime relative to the start of the presentation
* @return {!Promise}
*/
async appendBuffer(buffer, startTime, endTime) {
goog.asserts.assert(
this.parser_, 'The parser should already be initialized');
// Start the operation asynchronously to avoid blocking the caller.
await Promise.resolve();
// Check that TextEngine hasn't been destroyed.
if (!this.parser_ || !this.displayer_) {
return;
}
if (startTime == null || endTime == null) {
this.parser_.parseInit(shaka.util.BufferUtils.toUint8(buffer));
return;
}
const vttOffset = this.segmentRelativeVttTiming_ ?
startTime : this.timestampOffset_;
/** @type {shaka.extern.TextParser.TimeContext} **/
const time = {
periodStart: this.timestampOffset_,
segmentStart: startTime,
segmentEnd: endTime,
vttOffset: vttOffset,
};
// Parse the buffer and add the new cues.
const allCues = this.parser_.parseMedia(
shaka.util.BufferUtils.toUint8(buffer), time);
const cuesToAppend = allCues.filter((cue) => {
return cue.startTime >= this.appendWindowStart_ &&
cue.startTime < this.appendWindowEnd_;
});
this.displayer_.append(cuesToAppend);
// NOTE: We update the buffered range from the start and end times
// passed down from the segment reference, not with the start and end
// times of the parsed cues. This is important because some segments
// may contain no cues, but we must still consider those ranges
// buffered.
if (this.bufferStart_ == null) {
this.bufferStart_ = Math.max(startTime, this.appendWindowStart_);
} else {
// We already had something in buffer, and we assume we are extending
// the range from the end.
goog.asserts.assert(
this.bufferEnd_ != null,
'There should already be a buffered range end.');
goog.asserts.assert(
(startTime - this.bufferEnd_) <= 1,
'There should not be a gap in text references >1s');
}
this.bufferEnd_ = Math.min(endTime, this.appendWindowEnd_);
}
/**
* @param {number} startTime relative to the start of the presentation
* @param {number} endTime relative to the start of the presentation
* @return {!Promise}
*/
async remove(startTime, endTime) {
// Start the operation asynchronously to avoid blocking the caller.
await Promise.resolve();
if (this.displayer_ && this.displayer_.remove(startTime, endTime)) {
if (this.bufferStart_ == null) {
goog.asserts.assert(
this.bufferEnd_ == null, 'end must be null if startTime is null');
} else {
goog.asserts.assert(
this.bufferEnd_ != null,
'end must be non-null if startTime is non-null');
// Update buffered range.
if (endTime <= this.bufferStart_ || startTime >= this.bufferEnd_) {
// No intersection. Nothing was removed.
} else if (startTime <= this.bufferStart_ &&
endTime >= this.bufferEnd_) {
// We wiped out everything.
this.bufferStart_ = this.bufferEnd_ = null;
} else if (startTime <= this.bufferStart_ &&
endTime < this.bufferEnd_) {
// We removed from the beginning of the range.
this.bufferStart_ = endTime;
} else if (startTime > this.bufferStart_ &&
endTime >= this.bufferEnd_) {
// We removed from the end of the range.
this.bufferEnd_ = startTime;
} else {
// We removed from the middle? StreamingEngine isn't supposed to.
goog.asserts.assert(
false, 'removal from the middle is not supported by TextEngine');
}
}
}
}
/** @param {number} timestampOffset */
setTimestampOffset(timestampOffset) {
this.timestampOffset_ = timestampOffset;
}
/**
* @param {number} appendWindowStart
* @param {number} appendWindowEnd
*/
setAppendWindow(appendWindowStart, appendWindowEnd) {
this.appendWindowStart_ = appendWindowStart;
this.appendWindowEnd_ = appendWindowEnd;
}
/**
* @return {?number} Time in seconds of the beginning of the buffered range,
* or null if nothing is buffered.
*/
bufferStart() {
return this.bufferStart_;
}
/**
* @return {?number} Time in seconds of the end of the buffered range,
* or null if nothing is buffered.
*/
bufferEnd() {
return this.bufferEnd_;
}
/**
* @param {number} t A timestamp
* @return {boolean}
*/
isBuffered(t) {
if (this.bufferStart_ == null || this.bufferEnd_ == null) {
return false;
}
return t >= this.bufferStart_ && t < this.bufferEnd_;
}
/**
* @param {number} t A timestamp
* @return {number} Number of seconds ahead of 't' we have buffered
*/
bufferedAheadOf(t) {
if (this.bufferEnd_ == null || this.bufferEnd_ < t) {
return 0;
}
goog.asserts.assert(
this.bufferStart_ != null,
'start should not be null if end is not null');
return this.bufferEnd_ - Math.max(t, this.bufferStart_);
}
/**
* Set the selected closed captions id.
* Append the cues stored in the closed captions map until buffer end time.
* This is to fill the gap between buffered and unbuffered captions, and to
* avoid duplicates that would be caused by any future video segments parsed
* for captions.
*
* @param {string} id
* @param {number} bufferEndTime Load any stored cues up to this time.
*/
setSelectedClosedCaptionId(id, bufferEndTime) {
this.selectedClosedCaptionId_ = id;
const captionsMap = this.closedCaptionsMap_.get(id);
if (captionsMap) {
for (const startAndEndTime of captionsMap.keys()) {
/** @type {Array.<!shaka.text.Cue>} */
const cues = captionsMap.get(startAndEndTime)
.filter((c) => c.endTime <= bufferEndTime);
if (cues) {
this.displayer_.append(cues);
}
}
}
}
/**
* @param {!Array<muxjs.mp4.ClosedCaption>} closedCaptions
* @return {!Array<!shaka.cea.ICaptionDecoder.ClosedCaption>}
*/
convertMuxjsCaptionsToShakaCaptions(closedCaptions) {
const cues = [];
for (const caption of closedCaptions) {
const cue = new shaka.text.Cue(
caption.startTime, caption.endTime, caption.text);
cues.push({
stream: caption.stream,
cue,
});
}
return cues;
}
/**
* @param {!shaka.text.Cue} cue the cue to apply the timestamp to recursively
* @param {number} videoTimestampOffset the timestamp offset of the video
* @private
*/
applyVideoTimestampOffsetRecursive_(cue, videoTimestampOffset) {
cue.startTime += videoTimestampOffset;
cue.endTime += videoTimestampOffset;
for (const nested of cue.nestedCues) {
this.applyVideoTimestampOffsetRecursive_(nested, videoTimestampOffset);
}
}
/**
* Store the closed captions in the text engine, and append the cues to the
* text displayer. This is a side-channel used for embedded text only.
*
* @param {!Array.<!shaka.cea.ICaptionDecoder.ClosedCaption>} closedCaptions
* @param {?number} startTime relative to the start of the presentation
* @param {?number} endTime relative to the start of the presentation
* @param {number} videoTimestampOffset the timestamp offset of the video
* stream in which these captions were embedded
*/
storeAndAppendClosedCaptions(
closedCaptions, startTime, endTime, videoTimestampOffset) {
const startAndEndTime = startTime + ' ' + endTime;
/** @type {!Map.<string, !Map.<string, !Array.<!shaka.text.Cue>>>} */
const captionsMap = new Map();
for (const caption of closedCaptions) {
const id = caption.stream;
const cue = caption.cue;
if (!captionsMap.has(id)) {
captionsMap.set(id, new Map());
}
if (!captionsMap.get(id).has(startAndEndTime)) {
captionsMap.get(id).set(startAndEndTime, []);
}
// Adjust CEA captions with respect to the timestamp offset of the video
// stream in which they were embedded.
this.applyVideoTimestampOffsetRecursive_(cue, videoTimestampOffset);
const keepThisCue =
cue.startTime >= this.appendWindowStart_ &&
cue.startTime < this.appendWindowEnd_;
if (!keepThisCue) {
continue;
}
captionsMap.get(id).get(startAndEndTime).push(cue);
if (id == this.selectedClosedCaptionId_) {
this.displayer_.append([cue]);
}
}
for (const id of captionsMap.keys()) {
if (!this.closedCaptionsMap_.has(id)) {
this.closedCaptionsMap_.set(id, new Map());
}
for (const startAndEndTime of captionsMap.get(id).keys()) {
const cues = captionsMap.get(id).get(startAndEndTime);
this.closedCaptionsMap_.get(id).set(startAndEndTime, cues);
}
}
if (this.bufferStart_ == null) {
this.bufferStart_ = Math.max(startTime, this.appendWindowStart_);
} else {
this.bufferStart_ = Math.min(
this.bufferStart_, Math.max(startTime, this.appendWindowStart_));
}
this.bufferEnd_ = Math.max(
this.bufferEnd_, Math.min(endTime, this.appendWindowEnd_));
}
/**
* Get the number of closed caption channels.
*
* This function is for TESTING ONLY. DO NOT USE in the library.
*
* @return {number}
*/
getNumberOfClosedCaptionChannels() {
return this.closedCaptionsMap_.size;
}
/**
* Get the number of closed caption cues for a given channel. If there is
* no channel for the given channel id, this will return 0.
*
* This function is for TESTING ONLY. DO NOT USE in the library.
*
* @param {string} channelId
* @return {number}
*/
getNumberOfClosedCaptionsInChannel(channelId) {
const channel = this.closedCaptionsMap_.get(channelId);
return channel ? channel.size : 0;
}
};
/** @private {!Object.<string, !shaka.extern.TextParserPlugin>} */
shaka.text.TextEngine.parserMap_ = {};