forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 90
/
subtitle-stream-controller.js
213 lines (185 loc) · 6.04 KB
/
subtitle-stream-controller.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
/*
* Subtitle Stream Controller
*/
import Event from '../events';
import { logger } from '../utils/logger';
import Decrypter from '../crypt/decrypter';
import TaskLoop from '../task-loop';
const { performance } = window;
const State = {
STOPPED: 'STOPPED',
IDLE: 'IDLE',
KEY_LOADING: 'KEY_LOADING',
FRAG_LOADING: 'FRAG_LOADING'
};
class SubtitleStreamController extends TaskLoop {
constructor (hls) {
super(hls,
Event.MEDIA_ATTACHED,
Event.ERROR,
Event.KEY_LOADED,
Event.FRAG_LOADED,
Event.SUBTITLE_TRACKS_UPDATED,
Event.SUBTITLE_TRACK_SWITCH,
Event.SUBTITLE_TRACK_LOADED,
Event.SUBTITLE_FRAG_PROCESSED);
this.config = hls.config;
this.vttFragSNsProcessed = {};
this.vttFragQueues = undefined;
this.currentlyProcessing = null;
this.state = State.STOPPED;
this.currentTrackId = -1;
this.decrypter = new Decrypter(hls.observer, hls.config);
}
onHandlerDestroyed () {
this.state = State.STOPPED;
}
// Remove all queued items and create a new, empty queue for each track.
clearVttFragQueues () {
this.vttFragQueues = {};
this.tracks.forEach(track => {
this.vttFragQueues[track.id] = [];
});
}
// If no frag is being processed and queue isn't empty, initiate processing of next frag in line.
nextFrag () {
if (this.currentlyProcessing === null && this.currentTrackId > -1 && this.vttFragQueues[this.currentTrackId].length) {
let frag = this.currentlyProcessing = this.vttFragQueues[this.currentTrackId].shift();
this.fragCurrent = frag;
this.hls.trigger(Event.FRAG_LOADING, { frag: frag });
this.state = State.FRAG_LOADING;
}
}
// When fragment has finished processing, add sn to list of completed if successful.
onSubtitleFragProcessed (data) {
if (data.success) {
this.vttFragSNsProcessed[data.frag.trackId].push(data.frag.sn);
}
this.currentlyProcessing = null;
this.state = State.IDLE;
this.nextFrag();
}
onMediaAttached () {
this.state = State.IDLE;
}
// If something goes wrong, procede to next frag, if we were processing one.
onError (data) {
let frag = data.frag;
// don't handle frag error not related to subtitle fragment
if (frag && frag.type !== 'subtitle') {
return;
}
if (this.currentlyProcessing) {
this.currentlyProcessing = null;
this.nextFrag();
}
}
doTick () {
switch (this.state) {
case State.IDLE:
const tracks = this.tracks;
let trackId = this.currentTrackId;
const processedFragSNs = this.vttFragSNsProcessed[trackId],
fragQueue = this.vttFragQueues[trackId],
currentFragSN = this.currentlyProcessing ? this.currentlyProcessing.sn : -1;
const alreadyProcessed = function (frag) {
return processedFragSNs.indexOf(frag.sn) > -1;
};
const alreadyInQueue = function (frag) {
return fragQueue.some(fragInQueue => {
return fragInQueue.sn === frag.sn;
});
};
// exit if tracks don't exist
if (!tracks) {
break;
}
var trackDetails;
if (trackId < tracks.length) {
trackDetails = tracks[trackId].details;
}
if (typeof trackDetails === 'undefined') {
break;
}
// Add all fragments that haven't been, aren't currently being and aren't waiting to be processed, to queue.
trackDetails.fragments.forEach(frag => {
if (!(alreadyProcessed(frag) || frag.sn === currentFragSN || alreadyInQueue(frag))) {
// Load key if subtitles are encrypted
if (frag.encrypted) {
logger.log(`Loading key for ${frag.sn}`);
this.state = State.KEY_LOADING;
this.hls.trigger(Event.KEY_LOADING, { frag: frag });
} else {
// Frags don't know their subtitle track ID, so let's just add that...
frag.trackId = trackId;
fragQueue.push(frag);
this.nextFrag();
}
}
});
}
}
// Got all new subtitle tracks.
onSubtitleTracksUpdated (data) {
logger.log('subtitle tracks updated');
this.tracks = data.subtitleTracks;
this.clearVttFragQueues();
this.vttFragSNsProcessed = {};
this.tracks.forEach(track => {
this.vttFragSNsProcessed[track.id] = [];
});
}
onSubtitleTrackSwitch (data) {
this.currentTrackId = data.id;
if (!this.tracks || this.currentTrackId === -1) {
return;
}
// Check if track was already loaded and if so make sure we finish
// downloading its frags, if not all have been downloaded yet
const currentTrack = this.tracks[this.currentTrackId];
if (currentTrack && currentTrack.details) {
this.tick();
}
}
// Got a new set of subtitle fragments.
onSubtitleTrackLoaded () {
this.tick();
}
onKeyLoaded () {
if (this.state === State.KEY_LOADING) {
this.state = State.IDLE;
this.tick();
}
}
onFragLoaded (data) {
let fragCurrent = this.fragCurrent,
decryptData = data.frag.decryptdata;
let fragLoaded = data.frag,
hls = this.hls;
if (this.state === State.FRAG_LOADING &&
fragCurrent &&
data.frag.type === 'subtitle' &&
fragCurrent.sn === data.frag.sn) {
// check to see if the payload needs to be decrypted
if ((data.payload.byteLength > 0) && (decryptData != null) && (decryptData.key != null) && (decryptData.method === 'AES-128')) {
let startTime;
try {
startTime = performance.now();
} catch (error) {
startTime = Date.now();
}
// decrypt the subtitles
this.decrypter.decrypt(data.payload, decryptData.key.buffer, decryptData.iv.buffer, function (decryptedData) {
let endTime;
try {
endTime = performance.now();
} catch (error) {
endTime = Date.now();
}
hls.trigger(Event.FRAG_DECRYPTED, { frag: fragLoaded, payload: decryptedData, stats: { tstart: startTime, tdecrypt: endTime } });
});
}
}
}
}
export default SubtitleStreamController;