forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cues.js
56 lines (48 loc) · 1.62 KB
/
cues.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
import { fixLineBreaks } from './vttparser';
export function newCue (track, startTime, endTime, captionScreen) {
let row;
let cue;
let indenting;
let indent;
let text;
let VTTCue = window.VTTCue || window.TextTrackCue;
for (let r = 0; r < captionScreen.rows.length; r++) {
row = captionScreen.rows[r];
indenting = true;
indent = 0;
text = '';
if (!row.isEmpty()) {
for (let c = 0; c < row.chars.length; c++) {
if (row.chars[c].uchar.match(/\s/) && indenting) {
indent++;
} else {
text += row.chars[c].uchar;
indenting = false;
}
}
// To be used for cleaning-up orphaned roll-up captions
row.cueStartTime = startTime;
// Give a slight bump to the endTime if it's equal to startTime to avoid a SyntaxError in IE
if (startTime === endTime) {
endTime += 0.0001;
}
cue = new VTTCue(startTime, endTime, fixLineBreaks(text.trim()));
if (indent >= 16) {
indent--;
} else {
indent++;
}
// VTTCue.line get's flakey when using controls, so let's now include line 13&14
// also, drop line 1 since it's to close to the top
if (navigator.userAgent.match(/Firefox\//)) {
cue.line = r + 1;
} else {
cue.line = (r > 7 ? r - 2 : r + 1);
}
cue.align = 'left';
// Clamp the position between 0 and 100 - if out of these bounds, Firefox throws an exception and captions break
cue.position = Math.max(0, Math.min(100, 100 * (indent / 32) + (navigator.userAgent.match(/Firefox\//) ? 50 : 0)));
track.addCue(cue);
}
}
}