1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import window from 'global/window';
|
12 | import Ranges from './ranges';
|
13 | import videojs from 'video.js';
|
14 |
|
15 |
|
16 | const timerCancelEvents = [
|
17 | 'seeking',
|
18 | 'seeked',
|
19 | 'pause',
|
20 | 'playing',
|
21 | 'error'
|
22 | ];
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export default class PlaybackWatcher {
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 | constructor(options) {
|
34 | this.tech_ = options.tech;
|
35 | this.seekable = options.seekable;
|
36 |
|
37 | this.consecutiveUpdates = 0;
|
38 | this.lastRecordedTime = null;
|
39 | this.timer_ = null;
|
40 | this.checkCurrentTimeTimeout_ = null;
|
41 |
|
42 | if (options.debug) {
|
43 | this.logger_ = videojs.log.bind(videojs, 'playback-watcher ->');
|
44 | }
|
45 | this.logger_('initialize');
|
46 |
|
47 | let canPlayHandler = () => this.monitorCurrentTime_();
|
48 | let waitingHandler = () => this.techWaiting_();
|
49 | let cancelTimerHandler = () => this.cancelTimer_();
|
50 | let fixesBadSeeksHandler = () => this.fixesBadSeeks_();
|
51 |
|
52 | this.tech_.on('seekablechanged', fixesBadSeeksHandler);
|
53 | this.tech_.on('waiting', waitingHandler);
|
54 | this.tech_.on(timerCancelEvents, cancelTimerHandler);
|
55 | this.tech_.on('canplay', canPlayHandler);
|
56 |
|
57 |
|
58 | this.dispose = () => {
|
59 | this.logger_('dispose');
|
60 | this.tech_.off('seekablechanged', fixesBadSeeksHandler);
|
61 | this.tech_.off('waiting', waitingHandler);
|
62 | this.tech_.off(timerCancelEvents, cancelTimerHandler);
|
63 | this.tech_.off('canplay', canPlayHandler);
|
64 | if (this.checkCurrentTimeTimeout_) {
|
65 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
66 | }
|
67 | this.cancelTimer_();
|
68 | };
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | monitorCurrentTime_() {
|
77 | this.checkCurrentTime_();
|
78 |
|
79 | if (this.checkCurrentTimeTimeout_) {
|
80 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
81 | }
|
82 |
|
83 |
|
84 | this.checkCurrentTimeTimeout_ =
|
85 | window.setTimeout(this.monitorCurrentTime_.bind(this), 250);
|
86 | }
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | checkCurrentTime_() {
|
96 | if (this.tech_.seeking() && this.fixesBadSeeks_()) {
|
97 | this.consecutiveUpdates = 0;
|
98 | this.lastRecordedTime = this.tech_.currentTime();
|
99 | return;
|
100 | }
|
101 |
|
102 | if (this.tech_.paused() || this.tech_.seeking()) {
|
103 | return;
|
104 | }
|
105 |
|
106 | let currentTime = this.tech_.currentTime();
|
107 | let buffered = this.tech_.buffered();
|
108 |
|
109 | if (this.lastRecordedTime === currentTime &&
|
110 | (!buffered.length ||
|
111 | currentTime + Ranges.SAFE_TIME_DELTA >= buffered.end(buffered.length - 1))) {
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | return this.techWaiting_();
|
120 | }
|
121 |
|
122 | if (this.consecutiveUpdates >= 5 &&
|
123 | currentTime === this.lastRecordedTime) {
|
124 | this.consecutiveUpdates++;
|
125 | this.waiting_();
|
126 | } else if (currentTime === this.lastRecordedTime) {
|
127 | this.consecutiveUpdates++;
|
128 | } else {
|
129 | this.consecutiveUpdates = 0;
|
130 | this.lastRecordedTime = currentTime;
|
131 | }
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | cancelTimer_() {
|
141 | this.consecutiveUpdates = 0;
|
142 |
|
143 | if (this.timer_) {
|
144 | this.logger_('cancelTimer_');
|
145 | clearTimeout(this.timer_);
|
146 | }
|
147 |
|
148 | this.timer_ = null;
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | fixesBadSeeks_() {
|
158 | const seeking = this.tech_.seeking();
|
159 | const seekable = this.seekable();
|
160 | const currentTime = this.tech_.currentTime();
|
161 | let seekTo;
|
162 |
|
163 | if (seeking && this.afterSeekableWindow_(seekable, currentTime)) {
|
164 | const seekableEnd = seekable.end(seekable.length - 1);
|
165 |
|
166 |
|
167 | seekTo = seekableEnd;
|
168 | }
|
169 |
|
170 | if (seeking && this.beforeSeekableWindow_(seekable, currentTime)) {
|
171 | const seekableStart = seekable.start(0);
|
172 |
|
173 |
|
174 |
|
175 | seekTo = seekableStart + Ranges.SAFE_TIME_DELTA;
|
176 | }
|
177 |
|
178 | if (typeof seekTo !== 'undefined') {
|
179 | this.logger_(`Trying to seek outside of seekable at time ${currentTime} with ` +
|
180 | `seekable range ${Ranges.printableRange(seekable)}. Seeking to ` +
|
181 | `${seekTo}.`);
|
182 |
|
183 | this.tech_.setCurrentTime(seekTo);
|
184 | return true;
|
185 | }
|
186 |
|
187 | return false;
|
188 | }
|
189 |
|
190 | |
191 |
|
192 |
|
193 |
|
194 |
|
195 | waiting_() {
|
196 | if (this.techWaiting_()) {
|
197 | return;
|
198 | }
|
199 |
|
200 |
|
201 | let currentTime = this.tech_.currentTime();
|
202 | let buffered = this.tech_.buffered();
|
203 | let currentRange = Ranges.findRange(buffered, currentTime);
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
|
214 | this.cancelTimer_();
|
215 | this.tech_.setCurrentTime(currentTime);
|
216 |
|
217 | this.logger_(`Stopped at ${currentTime} while inside a buffered region ` +
|
218 | `[${currentRange.start(0)} -> ${currentRange.end(0)}]. Attempting to resume ` +
|
219 | `playback by seeking to the current time.`);
|
220 |
|
221 |
|
222 | this.tech_.trigger({type: 'usage', name: 'hls-unknown-waiting'});
|
223 | return;
|
224 | }
|
225 | }
|
226 |
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | techWaiting_() {
|
236 | let seekable = this.seekable();
|
237 | let currentTime = this.tech_.currentTime();
|
238 |
|
239 | if (this.tech_.seeking() && this.fixesBadSeeks_()) {
|
240 |
|
241 | return true;
|
242 | }
|
243 |
|
244 | if (this.tech_.seeking() || this.timer_ !== null) {
|
245 |
|
246 | return true;
|
247 | }
|
248 |
|
249 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
250 | let livePoint = seekable.end(seekable.length - 1);
|
251 |
|
252 | this.logger_(`Fell out of live window at time ${currentTime}. Seeking to ` +
|
253 | `live point (seekable end) ${livePoint}`);
|
254 | this.cancelTimer_();
|
255 | this.tech_.setCurrentTime(livePoint);
|
256 |
|
257 |
|
258 | this.tech_.trigger({type: 'usage', name: 'hls-live-resync'});
|
259 | return true;
|
260 | }
|
261 |
|
262 | let buffered = this.tech_.buffered();
|
263 | let nextRange = Ranges.findNextRange(buffered, currentTime);
|
264 |
|
265 | if (this.videoUnderflow_(nextRange, buffered, currentTime)) {
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | this.cancelTimer_();
|
271 | this.tech_.setCurrentTime(currentTime);
|
272 |
|
273 |
|
274 | this.tech_.trigger({type: 'usage', name: 'hls-video-underflow'});
|
275 | return true;
|
276 | }
|
277 |
|
278 |
|
279 | if (nextRange.length > 0) {
|
280 | let difference = nextRange.start(0) - currentTime;
|
281 |
|
282 | this.logger_(
|
283 | `Stopped at ${currentTime}, setting timer for ${difference}, seeking ` +
|
284 | `to ${nextRange.start(0)}`);
|
285 |
|
286 | this.timer_ = setTimeout(this.skipTheGap_.bind(this),
|
287 | difference * 1000,
|
288 | currentTime);
|
289 | return true;
|
290 | }
|
291 |
|
292 |
|
293 | return false;
|
294 | }
|
295 |
|
296 | afterSeekableWindow_(seekable, currentTime) {
|
297 | if (!seekable.length) {
|
298 |
|
299 | return false;
|
300 | }
|
301 |
|
302 | if (currentTime > seekable.end(seekable.length - 1) + Ranges.SAFE_TIME_DELTA) {
|
303 | return true;
|
304 | }
|
305 |
|
306 | return false;
|
307 | }
|
308 |
|
309 | beforeSeekableWindow_(seekable, currentTime) {
|
310 | if (seekable.length &&
|
311 |
|
312 | seekable.start(0) > 0 &&
|
313 | currentTime < seekable.start(0) - Ranges.SAFE_TIME_DELTA) {
|
314 | return true;
|
315 | }
|
316 |
|
317 | return false;
|
318 | }
|
319 |
|
320 | videoUnderflow_(nextRange, buffered, currentTime) {
|
321 | if (nextRange.length === 0) {
|
322 |
|
323 |
|
324 | let gap = this.gapFromVideoUnderflow_(buffered, currentTime);
|
325 |
|
326 | if (gap) {
|
327 | this.logger_(`Encountered a gap in video from ${gap.start} to ${gap.end}. ` +
|
328 | `Seeking to current time ${currentTime}`);
|
329 |
|
330 | return true;
|
331 | }
|
332 | }
|
333 |
|
334 | return false;
|
335 | }
|
336 |
|
337 | |
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | skipTheGap_(scheduledCurrentTime) {
|
344 | let buffered = this.tech_.buffered();
|
345 | let currentTime = this.tech_.currentTime();
|
346 | let nextRange = Ranges.findNextRange(buffered, currentTime);
|
347 |
|
348 | this.cancelTimer_();
|
349 |
|
350 | if (nextRange.length === 0 ||
|
351 | currentTime !== scheduledCurrentTime) {
|
352 | return;
|
353 | }
|
354 |
|
355 | this.logger_('skipTheGap_:',
|
356 | 'currentTime:', currentTime,
|
357 | 'scheduled currentTime:', scheduledCurrentTime,
|
358 | 'nextRange start:', nextRange.start(0));
|
359 |
|
360 |
|
361 | this.tech_.setCurrentTime(nextRange.start(0) + Ranges.TIME_FUDGE_FACTOR);
|
362 |
|
363 | this.tech_.trigger({type: 'usage', name: 'hls-gap-skip'});
|
364 | }
|
365 |
|
366 | gapFromVideoUnderflow_(buffered, currentTime) {
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | let gaps = Ranges.findGaps(buffered);
|
390 |
|
391 | for (let i = 0; i < gaps.length; i++) {
|
392 | let start = gaps.start(i);
|
393 | let end = gaps.end(i);
|
394 |
|
395 |
|
396 | if (currentTime - start < 4 && currentTime - start > 2) {
|
397 | return {
|
398 | start,
|
399 | end
|
400 | };
|
401 | }
|
402 | }
|
403 |
|
404 | return null;
|
405 | }
|
406 |
|
407 | |
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | logger_() {}
|
414 | }
|