1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import window from 'global/window';
|
12 | import * as Ranges from './ranges';
|
13 | import logger from './util/logger';
|
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 |
|
34 | constructor(options) {
|
35 | this.masterPlaylistController_ = options.masterPlaylistController;
|
36 | this.tech_ = options.tech;
|
37 | this.seekable = options.seekable;
|
38 | this.allowSeeksWithinUnsafeLiveWindow = options.allowSeeksWithinUnsafeLiveWindow;
|
39 | this.liveRangeSafeTimeDelta = options.liveRangeSafeTimeDelta;
|
40 | this.media = options.media;
|
41 |
|
42 | this.consecutiveUpdates = 0;
|
43 | this.lastRecordedTime = null;
|
44 | this.timer_ = null;
|
45 | this.checkCurrentTimeTimeout_ = null;
|
46 | this.logger_ = logger('PlaybackWatcher');
|
47 |
|
48 | this.logger_('initialize');
|
49 |
|
50 | const playHandler = () => this.monitorCurrentTime_();
|
51 | const canPlayHandler = () => this.monitorCurrentTime_();
|
52 | const waitingHandler = () => this.techWaiting_();
|
53 | const cancelTimerHandler = () => this.cancelTimer_();
|
54 |
|
55 | const mpc = this.masterPlaylistController_;
|
56 |
|
57 | const loaderTypes = ['main', 'subtitle', 'audio'];
|
58 | const loaderChecks = {};
|
59 |
|
60 | loaderTypes.forEach((type) => {
|
61 | loaderChecks[type] = {
|
62 | reset: () => this.resetSegmentDownloads_(type),
|
63 | updateend: () => this.checkSegmentDownloads_(type)
|
64 | };
|
65 |
|
66 | mpc[`${type}SegmentLoader_`].on('appendsdone', loaderChecks[type].updateend);
|
67 |
|
68 |
|
69 |
|
70 | mpc[`${type}SegmentLoader_`].on('playlistupdate', loaderChecks[type].reset);
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | this.tech_.on(['seeked', 'seeking'], loaderChecks[type].reset);
|
76 | });
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | const setSeekingHandlers = (fn) => {
|
87 | ['main', 'audio'].forEach((type) => {
|
88 | mpc[`${type}SegmentLoader_`][fn]('appended', this.seekingAppendCheck_);
|
89 | });
|
90 | };
|
91 |
|
92 | this.seekingAppendCheck_ = () => {
|
93 | if (this.fixesBadSeeks_()) {
|
94 | this.consecutiveUpdates = 0;
|
95 | this.lastRecordedTime = this.tech_.currentTime();
|
96 | setSeekingHandlers('off');
|
97 | }
|
98 | };
|
99 |
|
100 | this.clearSeekingAppendCheck_ = () => setSeekingHandlers('off');
|
101 |
|
102 | this.watchForBadSeeking_ = () => {
|
103 | this.clearSeekingAppendCheck_();
|
104 | setSeekingHandlers('on');
|
105 | };
|
106 |
|
107 | this.tech_.on('seeked', this.clearSeekingAppendCheck_);
|
108 | this.tech_.on('seeking', this.watchForBadSeeking_);
|
109 |
|
110 | this.tech_.on('waiting', waitingHandler);
|
111 | this.tech_.on(timerCancelEvents, cancelTimerHandler);
|
112 | this.tech_.on('canplay', canPlayHandler);
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | this.tech_.one('play', playHandler);
|
126 |
|
127 |
|
128 | this.dispose = () => {
|
129 | this.clearSeekingAppendCheck_();
|
130 | this.logger_('dispose');
|
131 | this.tech_.off('waiting', waitingHandler);
|
132 | this.tech_.off(timerCancelEvents, cancelTimerHandler);
|
133 | this.tech_.off('canplay', canPlayHandler);
|
134 | this.tech_.off('play', playHandler);
|
135 | this.tech_.off('seeking', this.watchForBadSeeking_);
|
136 | this.tech_.off('seeked', this.clearSeekingAppendCheck_);
|
137 |
|
138 | loaderTypes.forEach((type) => {
|
139 | mpc[`${type}SegmentLoader_`].off('appendsdone', loaderChecks[type].updateend);
|
140 | mpc[`${type}SegmentLoader_`].off('playlistupdate', loaderChecks[type].reset);
|
141 | this.tech_.off(['seeked', 'seeking'], loaderChecks[type].reset);
|
142 | });
|
143 | if (this.checkCurrentTimeTimeout_) {
|
144 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
145 | }
|
146 | this.cancelTimer_();
|
147 | };
|
148 | }
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 | monitorCurrentTime_() {
|
156 | this.checkCurrentTime_();
|
157 |
|
158 | if (this.checkCurrentTimeTimeout_) {
|
159 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
160 | }
|
161 |
|
162 |
|
163 | this.checkCurrentTimeTimeout_ =
|
164 | window.setTimeout(this.monitorCurrentTime_.bind(this), 250);
|
165 | }
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | resetSegmentDownloads_(type) {
|
178 | const loader = this.masterPlaylistController_[`${type}SegmentLoader_`];
|
179 |
|
180 | if (this[`${type}StalledDownloads_`] > 0) {
|
181 | this.logger_(`resetting possible stalled download count for ${type} loader`);
|
182 | }
|
183 | this[`${type}StalledDownloads_`] = 0;
|
184 | this[`${type}Buffered_`] = loader.buffered_();
|
185 | }
|
186 |
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | checkSegmentDownloads_(type) {
|
198 | const mpc = this.masterPlaylistController_;
|
199 | const loader = mpc[`${type}SegmentLoader_`];
|
200 | const buffered = loader.buffered_();
|
201 | const isBufferedDifferent = Ranges.isRangeDifferent(this[`${type}Buffered_`], buffered);
|
202 |
|
203 | this[`${type}Buffered_`] = buffered;
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | if (isBufferedDifferent) {
|
209 | this.resetSegmentDownloads_(type);
|
210 | return;
|
211 | }
|
212 |
|
213 | this[`${type}StalledDownloads_`]++;
|
214 |
|
215 | this.logger_(`found #${this[`${type}StalledDownloads_`]} ${type} appends that did not increase buffer (possible stalled download)`, {
|
216 | playlistId: loader.playlist_ && loader.playlist_.id,
|
217 | buffered: Ranges.timeRangesToArray(buffered)
|
218 |
|
219 | });
|
220 |
|
221 |
|
222 | if (this[`${type}StalledDownloads_`] < 10) {
|
223 | return;
|
224 | }
|
225 |
|
226 | this.logger_(`${type} loader stalled download exclusion`);
|
227 | this.resetSegmentDownloads_(type);
|
228 | this.tech_.trigger({type: 'usage', name: `vhs-${type}-download-exclusion`});
|
229 |
|
230 | if (type === 'subtitle') {
|
231 | return;
|
232 | }
|
233 |
|
234 |
|
235 |
|
236 | mpc.blacklistCurrentPlaylist({
|
237 | message: `Excessive ${type} segment downloading detected.`
|
238 | }, Infinity);
|
239 | }
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | checkCurrentTime_() {
|
249 | if (this.tech_.paused() || this.tech_.seeking()) {
|
250 | return;
|
251 | }
|
252 |
|
253 | const currentTime = this.tech_.currentTime();
|
254 | const buffered = this.tech_.buffered();
|
255 |
|
256 | if (this.lastRecordedTime === currentTime &&
|
257 | (!buffered.length ||
|
258 | currentTime + Ranges.SAFE_TIME_DELTA >= buffered.end(buffered.length - 1))) {
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | return this.techWaiting_();
|
265 | }
|
266 |
|
267 | if (this.consecutiveUpdates >= 5 &&
|
268 | currentTime === this.lastRecordedTime) {
|
269 | this.consecutiveUpdates++;
|
270 | this.waiting_();
|
271 | } else if (currentTime === this.lastRecordedTime) {
|
272 | this.consecutiveUpdates++;
|
273 | } else {
|
274 | this.consecutiveUpdates = 0;
|
275 | this.lastRecordedTime = currentTime;
|
276 | }
|
277 | }
|
278 |
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | cancelTimer_() {
|
286 | this.consecutiveUpdates = 0;
|
287 |
|
288 | if (this.timer_) {
|
289 | this.logger_('cancelTimer_');
|
290 | clearTimeout(this.timer_);
|
291 | }
|
292 |
|
293 | this.timer_ = null;
|
294 | }
|
295 |
|
296 | |
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | fixesBadSeeks_() {
|
303 | const seeking = this.tech_.seeking();
|
304 |
|
305 | if (!seeking) {
|
306 | return false;
|
307 | }
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | const seekable = this.seekable();
|
314 | const currentTime = this.tech_.currentTime();
|
315 | const isAfterSeekableRange = this.afterSeekableWindow_(
|
316 | seekable,
|
317 | currentTime,
|
318 | this.media(),
|
319 | this.allowSeeksWithinUnsafeLiveWindow
|
320 | );
|
321 | let seekTo;
|
322 |
|
323 | if (isAfterSeekableRange) {
|
324 | const seekableEnd = seekable.end(seekable.length - 1);
|
325 |
|
326 |
|
327 | seekTo = seekableEnd;
|
328 | }
|
329 |
|
330 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
331 | const seekableStart = seekable.start(0);
|
332 |
|
333 |
|
334 |
|
335 | seekTo = seekableStart +
|
336 |
|
337 |
|
338 | (seekableStart === seekable.end(0) ? 0 : Ranges.SAFE_TIME_DELTA);
|
339 | }
|
340 |
|
341 | if (typeof seekTo !== 'undefined') {
|
342 | this.logger_(`Trying to seek outside of seekable at time ${currentTime} with ` +
|
343 | `seekable range ${Ranges.printableRange(seekable)}. Seeking to ` +
|
344 | `${seekTo}.`);
|
345 |
|
346 | this.tech_.setCurrentTime(seekTo);
|
347 | return true;
|
348 | }
|
349 |
|
350 | const sourceUpdater = this.masterPlaylistController_.sourceUpdater_;
|
351 | const buffered = this.tech_.buffered();
|
352 | const audioBuffered = sourceUpdater.audioBuffer ? sourceUpdater.audioBuffered() : null;
|
353 | const videoBuffered = sourceUpdater.videoBuffer ? sourceUpdater.videoBuffered() : null;
|
354 | const media = this.media();
|
355 |
|
356 |
|
357 |
|
358 | const minAppendedDuration = media.partTargetDuration ? media.partTargetDuration :
|
359 | (media.targetDuration - Ranges.TIME_FUDGE_FACTOR) * 2;
|
360 |
|
361 |
|
362 |
|
363 | const bufferedToCheck = [audioBuffered, videoBuffered];
|
364 |
|
365 | for (let i = 0; i < bufferedToCheck.length; i++) {
|
366 |
|
367 | if (!bufferedToCheck[i]) {
|
368 | continue;
|
369 | }
|
370 |
|
371 | const timeAhead = Ranges.timeAheadOf(bufferedToCheck[i], currentTime);
|
372 |
|
373 |
|
374 |
|
375 | if (timeAhead < minAppendedDuration) {
|
376 | return false;
|
377 | }
|
378 | }
|
379 |
|
380 | const nextRange = Ranges.findNextRange(buffered, currentTime);
|
381 |
|
382 |
|
383 |
|
384 | if (nextRange.length === 0) {
|
385 | return false;
|
386 | }
|
387 |
|
388 | seekTo = nextRange.start(0) + Ranges.SAFE_TIME_DELTA;
|
389 |
|
390 | this.logger_(`Buffered region starts (${nextRange.start(0)}) ` +
|
391 | ` just beyond seek point (${currentTime}). Seeking to ${seekTo}.`);
|
392 |
|
393 | this.tech_.setCurrentTime(seekTo);
|
394 |
|
395 | return true;
|
396 | }
|
397 |
|
398 | |
399 |
|
400 |
|
401 |
|
402 |
|
403 | waiting_() {
|
404 | if (this.techWaiting_()) {
|
405 | return;
|
406 | }
|
407 |
|
408 |
|
409 | const currentTime = this.tech_.currentTime();
|
410 | const buffered = this.tech_.buffered();
|
411 | const currentRange = Ranges.findRange(buffered, currentTime);
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
|
422 | this.cancelTimer_();
|
423 | this.tech_.setCurrentTime(currentTime);
|
424 |
|
425 | this.logger_(`Stopped at ${currentTime} while inside a buffered region ` +
|
426 | `[${currentRange.start(0)} -> ${currentRange.end(0)}]. Attempting to resume ` +
|
427 | 'playback by seeking to the current time.');
|
428 |
|
429 |
|
430 | this.tech_.trigger({type: 'usage', name: 'vhs-unknown-waiting'});
|
431 | this.tech_.trigger({type: 'usage', name: 'hls-unknown-waiting'});
|
432 | return;
|
433 | }
|
434 | }
|
435 |
|
436 | |
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 | techWaiting_() {
|
445 | const seekable = this.seekable();
|
446 | const currentTime = this.tech_.currentTime();
|
447 |
|
448 | if (this.tech_.seeking() || this.timer_ !== null) {
|
449 |
|
450 | return true;
|
451 | }
|
452 |
|
453 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
454 | const livePoint = seekable.end(seekable.length - 1);
|
455 |
|
456 | this.logger_(`Fell out of live window at time ${currentTime}. Seeking to ` +
|
457 | `live point (seekable end) ${livePoint}`);
|
458 | this.cancelTimer_();
|
459 | this.tech_.setCurrentTime(livePoint);
|
460 |
|
461 |
|
462 | this.tech_.trigger({type: 'usage', name: 'vhs-live-resync'});
|
463 | this.tech_.trigger({type: 'usage', name: 'hls-live-resync'});
|
464 | return true;
|
465 | }
|
466 |
|
467 | const sourceUpdater = this.tech_.vhs.masterPlaylistController_.sourceUpdater_;
|
468 | const buffered = this.tech_.buffered();
|
469 | const videoUnderflow = this.videoUnderflow_({
|
470 | audioBuffered: sourceUpdater.audioBuffered(),
|
471 | videoBuffered: sourceUpdater.videoBuffered(),
|
472 | currentTime
|
473 | });
|
474 |
|
475 | if (videoUnderflow) {
|
476 |
|
477 |
|
478 |
|
479 |
|
480 | this.cancelTimer_();
|
481 | this.tech_.setCurrentTime(currentTime);
|
482 |
|
483 |
|
484 | this.tech_.trigger({type: 'usage', name: 'vhs-video-underflow'});
|
485 | this.tech_.trigger({type: 'usage', name: 'hls-video-underflow'});
|
486 | return true;
|
487 | }
|
488 | const nextRange = Ranges.findNextRange(buffered, currentTime);
|
489 |
|
490 |
|
491 | if (nextRange.length > 0) {
|
492 | const difference = nextRange.start(0) - currentTime;
|
493 |
|
494 | this.logger_(`Stopped at ${currentTime}, setting timer for ${difference}, seeking ` +
|
495 | `to ${nextRange.start(0)}`);
|
496 |
|
497 | this.cancelTimer_();
|
498 |
|
499 | this.timer_ = setTimeout(
|
500 | this.skipTheGap_.bind(this),
|
501 | difference * 1000,
|
502 | currentTime
|
503 | );
|
504 | return true;
|
505 | }
|
506 |
|
507 |
|
508 | return false;
|
509 | }
|
510 |
|
511 | afterSeekableWindow_(seekable, currentTime, playlist, allowSeeksWithinUnsafeLiveWindow = false) {
|
512 | if (!seekable.length) {
|
513 |
|
514 | return false;
|
515 | }
|
516 |
|
517 | let allowedEnd = seekable.end(seekable.length - 1) + Ranges.SAFE_TIME_DELTA;
|
518 | const isLive = !playlist.endList;
|
519 |
|
520 | if (isLive && allowSeeksWithinUnsafeLiveWindow) {
|
521 | allowedEnd = seekable.end(seekable.length - 1) + (playlist.targetDuration * 3);
|
522 | }
|
523 |
|
524 | if (currentTime > allowedEnd) {
|
525 | return true;
|
526 | }
|
527 |
|
528 | return false;
|
529 | }
|
530 |
|
531 | beforeSeekableWindow_(seekable, currentTime) {
|
532 | if (seekable.length &&
|
533 |
|
534 | seekable.start(0) > 0 &&
|
535 | currentTime < seekable.start(0) - this.liveRangeSafeTimeDelta) {
|
536 | return true;
|
537 | }
|
538 |
|
539 | return false;
|
540 | }
|
541 |
|
542 | videoUnderflow_({videoBuffered, audioBuffered, currentTime}) {
|
543 |
|
544 | if (!videoBuffered) {
|
545 | return;
|
546 | }
|
547 | let gap;
|
548 |
|
549 |
|
550 | if (videoBuffered.length && audioBuffered.length) {
|
551 |
|
552 |
|
553 |
|
554 | const lastVideoRange = Ranges.findRange(videoBuffered, currentTime - 3);
|
555 | const videoRange = Ranges.findRange(videoBuffered, currentTime);
|
556 | const audioRange = Ranges.findRange(audioBuffered, currentTime);
|
557 |
|
558 | if (audioRange.length && !videoRange.length && lastVideoRange.length) {
|
559 | gap = {start: lastVideoRange.end(0), end: audioRange.end(0)};
|
560 | }
|
561 |
|
562 |
|
563 | } else {
|
564 | const nextRange = Ranges.findNextRange(videoBuffered, currentTime);
|
565 |
|
566 |
|
567 |
|
568 | if (!nextRange.length) {
|
569 | gap = this.gapFromVideoUnderflow_(videoBuffered, currentTime);
|
570 | }
|
571 | }
|
572 |
|
573 | if (gap) {
|
574 | this.logger_(`Encountered a gap in video from ${gap.start} to ${gap.end}. ` +
|
575 | `Seeking to current time ${currentTime}`);
|
576 |
|
577 | return true;
|
578 | }
|
579 |
|
580 | return false;
|
581 | }
|
582 |
|
583 | |
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 | skipTheGap_(scheduledCurrentTime) {
|
590 | const buffered = this.tech_.buffered();
|
591 | const currentTime = this.tech_.currentTime();
|
592 | const nextRange = Ranges.findNextRange(buffered, currentTime);
|
593 |
|
594 | this.cancelTimer_();
|
595 |
|
596 | if (nextRange.length === 0 ||
|
597 | currentTime !== scheduledCurrentTime) {
|
598 | return;
|
599 | }
|
600 |
|
601 | this.logger_(
|
602 | 'skipTheGap_:',
|
603 | 'currentTime:', currentTime,
|
604 | 'scheduled currentTime:', scheduledCurrentTime,
|
605 | 'nextRange start:', nextRange.start(0)
|
606 | );
|
607 |
|
608 |
|
609 | this.tech_.setCurrentTime(nextRange.start(0) + Ranges.TIME_FUDGE_FACTOR);
|
610 |
|
611 | this.tech_.trigger({type: 'usage', name: 'vhs-gap-skip'});
|
612 | this.tech_.trigger({type: 'usage', name: 'hls-gap-skip'});
|
613 | }
|
614 |
|
615 | gapFromVideoUnderflow_(buffered, currentTime) {
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 | const gaps = Ranges.findGaps(buffered);
|
639 |
|
640 | for (let i = 0; i < gaps.length; i++) {
|
641 | const start = gaps.start(i);
|
642 | const end = gaps.end(i);
|
643 |
|
644 |
|
645 | if (currentTime - start < 4 && currentTime - start > 2) {
|
646 | return {
|
647 | start,
|
648 | end
|
649 | };
|
650 | }
|
651 | }
|
652 |
|
653 | return null;
|
654 | }
|
655 | }
|