1 |
|
2 |
|
3 |
|
4 | import Playlist from './playlist';
|
5 | import videojs from 'video.js';
|
6 | import SourceUpdater from './source-updater';
|
7 | import Config from './config';
|
8 | import window from 'global/window';
|
9 | import removeCuesFromTrack from
|
10 | 'videojs-contrib-media-sources/es5/remove-cues-from-track.js';
|
11 | import { initSegmentId } from './bin-utils';
|
12 | import {mediaSegmentRequest, REQUEST_ERRORS} from './media-segment-request';
|
13 | import { TIME_FUDGE_FACTOR, timeUntilRebuffer as timeUntilRebuffer_ } from './ranges';
|
14 | import { minRebufferMaxBandwidthSelector } from './playlist-selectors';
|
15 |
|
16 |
|
17 | const CHECK_BUFFER_DELAY = 500;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const detectEndOfStream = function(playlist, mediaSource, segmentIndex) {
|
30 | if (!playlist || !mediaSource) {
|
31 | return false;
|
32 | }
|
33 |
|
34 | let segments = playlist.segments;
|
35 |
|
36 |
|
37 |
|
38 | let appendedLastSegment = segmentIndex === segments.length;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | return playlist.endList &&
|
44 | mediaSource.readyState === 'open' &&
|
45 | appendedLastSegment;
|
46 | };
|
47 |
|
48 | const finite = (num) => typeof num === 'number' && isFinite(num);
|
49 |
|
50 | export const illegalMediaSwitch = (loaderType, startingMedia, newSegmentMedia) => {
|
51 |
|
52 |
|
53 | if (loaderType !== 'main' || !startingMedia || !newSegmentMedia) {
|
54 | return null;
|
55 | }
|
56 |
|
57 | if (!newSegmentMedia.containsAudio && !newSegmentMedia.containsVideo) {
|
58 | return 'Neither audio nor video found in segment.';
|
59 | }
|
60 |
|
61 | if (startingMedia.containsVideo && !newSegmentMedia.containsVideo) {
|
62 | return 'Only audio found in segment when we expected video.' +
|
63 | ' We can\'t switch to audio only from a stream that had video.' +
|
64 | ' To get rid of this message, please add codec information to the manifest.';
|
65 | }
|
66 |
|
67 | if (!startingMedia.containsVideo && newSegmentMedia.containsVideo) {
|
68 | return 'Video found in segment when we expected only audio.' +
|
69 | ' We can\'t switch to a stream with video from an audio only stream.' +
|
70 | ' To get rid of this message, please add codec information to the manifest.';
|
71 | }
|
72 |
|
73 | return null;
|
74 | };
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export const safeBackBufferTrimTime = (seekable, currentTime, targetDuration) => {
|
90 | let removeToTime;
|
91 |
|
92 | if (seekable.length &&
|
93 | seekable.start(0) > 0 &&
|
94 | seekable.start(0) < currentTime) {
|
95 |
|
96 | removeToTime = seekable.start(0);
|
97 | } else {
|
98 |
|
99 | removeToTime = currentTime - 30;
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | return Math.min(removeToTime, currentTime - targetDuration);
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | export default class SegmentLoader extends videojs.EventTarget {
|
116 | constructor(settings, options = {}) {
|
117 | super();
|
118 |
|
119 | if (!settings) {
|
120 | throw new TypeError('Initialization settings are required');
|
121 | }
|
122 | if (typeof settings.currentTime !== 'function') {
|
123 | throw new TypeError('No currentTime getter specified');
|
124 | }
|
125 | if (!settings.mediaSource) {
|
126 | throw new TypeError('No MediaSource specified');
|
127 | }
|
128 |
|
129 | this.state = 'INIT';
|
130 | this.bandwidth = settings.bandwidth;
|
131 | this.throughput = {rate: 0, count: 0};
|
132 | this.roundTrip = NaN;
|
133 | this.resetStats_();
|
134 | this.mediaIndex = null;
|
135 |
|
136 |
|
137 | this.hasPlayed_ = settings.hasPlayed;
|
138 | this.currentTime_ = settings.currentTime;
|
139 | this.seekable_ = settings.seekable;
|
140 | this.seeking_ = settings.seeking;
|
141 | this.duration_ = settings.duration;
|
142 | this.mediaSource_ = settings.mediaSource;
|
143 | this.hls_ = settings.hls;
|
144 | this.loaderType_ = settings.loaderType;
|
145 | this.startingMedia_ = void 0;
|
146 | this.segmentMetadataTrack_ = settings.segmentMetadataTrack;
|
147 | this.goalBufferLength_ = settings.goalBufferLength;
|
148 |
|
149 |
|
150 | this.checkBufferTimeout_ = null;
|
151 | this.error_ = void 0;
|
152 | this.currentTimeline_ = -1;
|
153 | this.pendingSegment_ = null;
|
154 | this.mimeType_ = null;
|
155 | this.sourceUpdater_ = null;
|
156 | this.xhrOptions_ = null;
|
157 |
|
158 |
|
159 | this.activeInitSegmentId_ = null;
|
160 | this.initSegments_ = {};
|
161 |
|
162 | this.decrypter_ = settings.decrypter;
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | this.syncController_ = settings.syncController;
|
168 | this.syncPoint_ = {
|
169 | segmentIndex: 0,
|
170 | time: 0
|
171 | };
|
172 |
|
173 | this.syncController_.on('syncinfoupdate', () => this.trigger('syncinfoupdate'));
|
174 |
|
175 | this.mediaSource_.addEventListener('sourceopen', () => this.ended_ = false);
|
176 |
|
177 |
|
178 | this.fetchAtBuffer_ = false;
|
179 |
|
180 | if (options.debug) {
|
181 | this.logger_ = videojs.log.bind(videojs, 'segment-loader', this.loaderType_, '->');
|
182 | }
|
183 | }
|
184 |
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 | resetStats_() {
|
191 | this.mediaBytesTransferred = 0;
|
192 | this.mediaRequests = 0;
|
193 | this.mediaRequestsAborted = 0;
|
194 | this.mediaRequestsTimedout = 0;
|
195 | this.mediaRequestsErrored = 0;
|
196 | this.mediaTransferDuration = 0;
|
197 | this.mediaSecondsLoaded = 0;
|
198 | }
|
199 |
|
200 | |
201 |
|
202 |
|
203 | dispose() {
|
204 | this.state = 'DISPOSED';
|
205 | this.pause();
|
206 | this.abort_();
|
207 | if (this.sourceUpdater_) {
|
208 | this.sourceUpdater_.dispose();
|
209 | }
|
210 | this.resetStats_();
|
211 | }
|
212 |
|
213 | |
214 |
|
215 |
|
216 |
|
217 | abort() {
|
218 | if (this.state !== 'WAITING') {
|
219 | if (this.pendingSegment_) {
|
220 | this.pendingSegment_ = null;
|
221 | }
|
222 | return;
|
223 | }
|
224 |
|
225 | this.abort_();
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | this.state = 'READY';
|
232 |
|
233 |
|
234 |
|
235 | if (!this.paused()) {
|
236 | this.monitorBuffer_();
|
237 | }
|
238 | }
|
239 |
|
240 | |
241 |
|
242 |
|
243 |
|
244 |
|
245 | abort_() {
|
246 | if (this.pendingSegment_) {
|
247 | this.pendingSegment_.abortRequests();
|
248 | }
|
249 |
|
250 |
|
251 | this.pendingSegment_ = null;
|
252 | }
|
253 |
|
254 | |
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | error(error) {
|
261 | if (typeof error !== 'undefined') {
|
262 | this.error_ = error;
|
263 | }
|
264 |
|
265 | this.pendingSegment_ = null;
|
266 | return this.error_;
|
267 | }
|
268 |
|
269 | endOfStream() {
|
270 | this.ended_ = true;
|
271 | this.pause();
|
272 | this.trigger('ended');
|
273 | }
|
274 |
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | buffered_() {
|
282 | if (!this.sourceUpdater_) {
|
283 | return videojs.createTimeRanges();
|
284 | }
|
285 |
|
286 | return this.sourceUpdater_.buffered();
|
287 | }
|
288 |
|
289 | |
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | initSegment(map, set = false) {
|
300 | if (!map) {
|
301 | return null;
|
302 | }
|
303 |
|
304 | const id = initSegmentId(map);
|
305 | let storedMap = this.initSegments_[id];
|
306 |
|
307 | if (set && !storedMap && map.bytes) {
|
308 | this.initSegments_[id] = storedMap = {
|
309 | resolvedUri: map.resolvedUri,
|
310 | byterange: map.byterange,
|
311 | bytes: map.bytes
|
312 | };
|
313 | }
|
314 |
|
315 | return storedMap || map;
|
316 | }
|
317 |
|
318 | |
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | couldBeginLoading_() {
|
325 | return this.playlist_ &&
|
326 |
|
327 |
|
328 |
|
329 | (this.sourceUpdater_ || (this.mimeType_ && this.state === 'INIT')) &&
|
330 | !this.paused();
|
331 | }
|
332 |
|
333 | |
334 |
|
335 |
|
336 | load() {
|
337 |
|
338 | this.monitorBuffer_();
|
339 |
|
340 |
|
341 |
|
342 | if (!this.playlist_) {
|
343 | return;
|
344 | }
|
345 |
|
346 |
|
347 | this.syncController_.setDateTimeMapping(this.playlist_);
|
348 |
|
349 |
|
350 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
351 | return this.init_();
|
352 | }
|
353 |
|
354 |
|
355 |
|
356 | if (!this.couldBeginLoading_() ||
|
357 | (this.state !== 'READY' &&
|
358 | this.state !== 'INIT')) {
|
359 | return;
|
360 | }
|
361 |
|
362 | this.state = 'READY';
|
363 | }
|
364 |
|
365 | |
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | init_() {
|
373 | this.state = 'READY';
|
374 | this.sourceUpdater_ = new SourceUpdater(this.mediaSource_, this.mimeType_);
|
375 | this.resetEverything();
|
376 | return this.monitorBuffer_();
|
377 | }
|
378 |
|
379 | |
380 |
|
381 |
|
382 |
|
383 |
|
384 | playlist(newPlaylist, options = {}) {
|
385 | if (!newPlaylist) {
|
386 | return;
|
387 | }
|
388 |
|
389 | let oldPlaylist = this.playlist_;
|
390 | let segmentInfo = this.pendingSegment_;
|
391 |
|
392 | this.playlist_ = newPlaylist;
|
393 | this.xhrOptions_ = options;
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | if (!this.hasPlayed_()) {
|
399 | newPlaylist.syncInfo = {
|
400 | mediaSequence: newPlaylist.mediaSequence,
|
401 | time: 0
|
402 | };
|
403 | }
|
404 |
|
405 |
|
406 |
|
407 | this.trigger('syncinfoupdate');
|
408 |
|
409 |
|
410 |
|
411 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
412 | return this.init_();
|
413 | }
|
414 |
|
415 | if (!oldPlaylist || oldPlaylist.uri !== newPlaylist.uri) {
|
416 | if (this.mediaIndex !== null) {
|
417 |
|
418 |
|
419 | this.resyncLoader();
|
420 | }
|
421 |
|
422 |
|
423 | return;
|
424 | }
|
425 |
|
426 |
|
427 |
|
428 | let mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
429 |
|
430 | this.logger_('mediaSequenceDiff', mediaSequenceDiff);
|
431 |
|
432 |
|
433 |
|
434 |
|
435 | if (this.mediaIndex !== null) {
|
436 | this.mediaIndex -= mediaSequenceDiff;
|
437 | }
|
438 |
|
439 |
|
440 |
|
441 |
|
442 | if (segmentInfo) {
|
443 | segmentInfo.mediaIndex -= mediaSequenceDiff;
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | if (segmentInfo.mediaIndex >= 0) {
|
449 | segmentInfo.segment = newPlaylist.segments[segmentInfo.mediaIndex];
|
450 | }
|
451 | }
|
452 |
|
453 | this.syncController_.saveExpiredSegmentInfo(oldPlaylist, newPlaylist);
|
454 | }
|
455 |
|
456 | |
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 | pause() {
|
463 | if (this.checkBufferTimeout_) {
|
464 | window.clearTimeout(this.checkBufferTimeout_);
|
465 |
|
466 | this.checkBufferTimeout_ = null;
|
467 | }
|
468 | }
|
469 |
|
470 | |
471 |
|
472 |
|
473 |
|
474 |
|
475 | paused() {
|
476 | return this.checkBufferTimeout_ === null;
|
477 | }
|
478 |
|
479 | |
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 | mimeType(mimeType) {
|
486 | if (this.mimeType_) {
|
487 | return;
|
488 | }
|
489 |
|
490 | this.mimeType_ = mimeType;
|
491 |
|
492 |
|
493 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
494 | this.init_();
|
495 | }
|
496 | }
|
497 |
|
498 | |
499 |
|
500 |
|
501 | resetEverything() {
|
502 | this.ended_ = false;
|
503 | this.resetLoader();
|
504 | this.remove(0, this.duration_());
|
505 | this.trigger('reseteverything');
|
506 | }
|
507 |
|
508 | |
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | resetLoader() {
|
515 | this.fetchAtBuffer_ = false;
|
516 | this.resyncLoader();
|
517 | }
|
518 |
|
519 | |
520 |
|
521 |
|
522 |
|
523 | resyncLoader() {
|
524 | this.mediaIndex = null;
|
525 | this.syncPoint_ = null;
|
526 | this.abort();
|
527 | }
|
528 |
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 | remove(start, end) {
|
535 | if (this.sourceUpdater_) {
|
536 | this.sourceUpdater_.remove(start, end);
|
537 | }
|
538 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
539 | }
|
540 |
|
541 | |
542 |
|
543 |
|
544 |
|
545 |
|
546 | monitorBuffer_() {
|
547 | if (this.checkBufferTimeout_) {
|
548 | window.clearTimeout(this.checkBufferTimeout_);
|
549 | }
|
550 |
|
551 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this), 1);
|
552 | }
|
553 |
|
554 | |
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 | monitorBufferTick_() {
|
561 | if (this.state === 'READY') {
|
562 | this.fillBuffer_();
|
563 | }
|
564 |
|
565 | if (this.checkBufferTimeout_) {
|
566 | window.clearTimeout(this.checkBufferTimeout_);
|
567 | }
|
568 |
|
569 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this),
|
570 | CHECK_BUFFER_DELAY);
|
571 | }
|
572 |
|
573 | |
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 | fillBuffer_() {
|
583 | if (this.sourceUpdater_.updating()) {
|
584 | return;
|
585 | }
|
586 |
|
587 | if (!this.syncPoint_) {
|
588 | this.syncPoint_ = this.syncController_.getSyncPoint(this.playlist_,
|
589 | this.duration_(),
|
590 | this.currentTimeline_,
|
591 | this.currentTime_());
|
592 | }
|
593 |
|
594 |
|
595 | let segmentInfo = this.checkBuffer_(this.buffered_(),
|
596 | this.playlist_,
|
597 | this.mediaIndex,
|
598 | this.hasPlayed_(),
|
599 | this.currentTime_(),
|
600 | this.syncPoint_);
|
601 |
|
602 | if (!segmentInfo) {
|
603 | return;
|
604 | }
|
605 |
|
606 | let isEndOfStream = detectEndOfStream(this.playlist_,
|
607 | this.mediaSource_,
|
608 | segmentInfo.mediaIndex);
|
609 |
|
610 | if (isEndOfStream) {
|
611 | this.endOfStream();
|
612 | return;
|
613 | }
|
614 |
|
615 | if (segmentInfo.mediaIndex === this.playlist_.segments.length - 1 &&
|
616 | this.mediaSource_.readyState === 'ended' &&
|
617 | !this.seeking_()) {
|
618 | return;
|
619 | }
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 | if (segmentInfo.timeline !== this.currentTimeline_ ||
|
628 | ((segmentInfo.startOfSegment !== null) &&
|
629 | segmentInfo.startOfSegment < this.sourceUpdater_.timestampOffset())) {
|
630 | this.syncController_.reset();
|
631 | segmentInfo.timestampOffset = segmentInfo.startOfSegment;
|
632 | }
|
633 |
|
634 | this.loadSegment_(segmentInfo);
|
635 | }
|
636 |
|
637 | |
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 | checkBuffer_(buffered, playlist, mediaIndex, hasPlayed, currentTime, syncPoint) {
|
650 | let lastBufferedEnd = 0;
|
651 | let startOfSegment;
|
652 |
|
653 | if (buffered.length) {
|
654 | lastBufferedEnd = buffered.end(buffered.length - 1);
|
655 | }
|
656 |
|
657 | let bufferedTime = Math.max(0, lastBufferedEnd - currentTime);
|
658 |
|
659 | if (!playlist.segments.length) {
|
660 | return null;
|
661 | }
|
662 |
|
663 |
|
664 |
|
665 | if (bufferedTime >= this.goalBufferLength_()) {
|
666 | return null;
|
667 | }
|
668 |
|
669 |
|
670 |
|
671 | if (!hasPlayed && bufferedTime >= 1) {
|
672 | return null;
|
673 | }
|
674 |
|
675 | this.logger_('checkBuffer_',
|
676 | 'mediaIndex:', mediaIndex,
|
677 | 'hasPlayed:', hasPlayed,
|
678 | 'currentTime:', currentTime,
|
679 | 'syncPoint:', syncPoint,
|
680 | 'fetchAtBuffer:', this.fetchAtBuffer_,
|
681 | 'bufferedTime:', bufferedTime);
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 | if (syncPoint === null) {
|
688 | mediaIndex = this.getSyncSegmentCandidate_(playlist);
|
689 | this.logger_('getSync', 'mediaIndex:', mediaIndex);
|
690 | return this.generateSegmentInfo_(playlist, mediaIndex, null, true);
|
691 | }
|
692 |
|
693 |
|
694 | if (mediaIndex !== null) {
|
695 | this.logger_('walkForward', 'mediaIndex:', mediaIndex + 1);
|
696 | let segment = playlist.segments[mediaIndex];
|
697 |
|
698 | if (segment && segment.end) {
|
699 | startOfSegment = segment.end;
|
700 | } else {
|
701 | startOfSegment = lastBufferedEnd;
|
702 | }
|
703 | return this.generateSegmentInfo_(playlist, mediaIndex + 1, startOfSegment, false);
|
704 | }
|
705 |
|
706 |
|
707 |
|
708 |
|
709 | if (this.fetchAtBuffer_) {
|
710 |
|
711 | let mediaSourceInfo = Playlist.getMediaInfoForTime(playlist,
|
712 | lastBufferedEnd,
|
713 | syncPoint.segmentIndex,
|
714 | syncPoint.time);
|
715 |
|
716 | mediaIndex = mediaSourceInfo.mediaIndex;
|
717 | startOfSegment = mediaSourceInfo.startTime;
|
718 | } else {
|
719 |
|
720 | let mediaSourceInfo = Playlist.getMediaInfoForTime(playlist,
|
721 | currentTime,
|
722 | syncPoint.segmentIndex,
|
723 | syncPoint.time);
|
724 |
|
725 | mediaIndex = mediaSourceInfo.mediaIndex;
|
726 | startOfSegment = mediaSourceInfo.startTime;
|
727 | }
|
728 | this.logger_('getMediaIndexForTime',
|
729 | 'mediaIndex:', mediaIndex,
|
730 | 'startOfSegment:', startOfSegment);
|
731 |
|
732 | return this.generateSegmentInfo_(playlist, mediaIndex, startOfSegment, false);
|
733 | }
|
734 |
|
735 | |
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 | getSyncSegmentCandidate_(playlist) {
|
745 | if (this.currentTimeline_ === -1) {
|
746 | return 0;
|
747 | }
|
748 |
|
749 | let segmentIndexArray = playlist.segments
|
750 | .map((s, i) => {
|
751 | return {
|
752 | timeline: s.timeline,
|
753 | segmentIndex: i
|
754 | };
|
755 | }).filter(s => s.timeline === this.currentTimeline_);
|
756 |
|
757 | if (segmentIndexArray.length) {
|
758 | return segmentIndexArray[Math.min(segmentIndexArray.length - 1, 1)].segmentIndex;
|
759 | }
|
760 |
|
761 | return Math.max(playlist.segments.length - 1, 0);
|
762 | }
|
763 |
|
764 | generateSegmentInfo_(playlist, mediaIndex, startOfSegment, isSyncRequest) {
|
765 | if (mediaIndex < 0 || mediaIndex >= playlist.segments.length) {
|
766 | return null;
|
767 | }
|
768 |
|
769 | let segment = playlist.segments[mediaIndex];
|
770 |
|
771 | return {
|
772 | requestId: 'segment-loader-' + Math.random(),
|
773 |
|
774 | uri: segment.resolvedUri,
|
775 |
|
776 | mediaIndex,
|
777 |
|
778 |
|
779 | isSyncRequest,
|
780 | startOfSegment,
|
781 |
|
782 | playlist,
|
783 |
|
784 | bytes: null,
|
785 |
|
786 | encryptedBytes: null,
|
787 |
|
788 |
|
789 | timestampOffset: null,
|
790 |
|
791 | timeline: segment.timeline,
|
792 |
|
793 | duration: segment.duration,
|
794 |
|
795 | segment
|
796 | };
|
797 | }
|
798 |
|
799 | |
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 | abortRequestEarly_(stats) {
|
810 | if (this.hls_.tech_.paused() ||
|
811 |
|
812 |
|
813 |
|
814 | !this.xhrOptions_.timeout ||
|
815 |
|
816 | !(this.playlist_.attributes.BANDWIDTH)) {
|
817 | return false;
|
818 | }
|
819 |
|
820 |
|
821 |
|
822 |
|
823 | if (Date.now() - (stats.firstBytesReceivedAt || Date.now()) < 1000) {
|
824 | return false;
|
825 | }
|
826 |
|
827 | const currentTime = this.currentTime_();
|
828 | const measuredBandwidth = stats.bandwidth;
|
829 | const segmentDuration = this.pendingSegment_.duration;
|
830 |
|
831 | const requestTimeRemaining =
|
832 | Playlist.estimateSegmentRequestTime(segmentDuration,
|
833 | measuredBandwidth,
|
834 | this.playlist_,
|
835 | stats.bytesReceived);
|
836 |
|
837 |
|
838 |
|
839 |
|
840 | const timeUntilRebuffer = timeUntilRebuffer_(this.buffered_(),
|
841 | currentTime,
|
842 | this.hls_.tech_.playbackRate()) - 1;
|
843 |
|
844 |
|
845 |
|
846 | if (requestTimeRemaining <= timeUntilRebuffer) {
|
847 | return false;
|
848 | }
|
849 |
|
850 | const switchCandidate = minRebufferMaxBandwidthSelector({
|
851 | master: this.hls_.playlists.master,
|
852 | currentTime,
|
853 | bandwidth: measuredBandwidth,
|
854 | duration: this.duration_(),
|
855 | segmentDuration,
|
856 | timeUntilRebuffer,
|
857 | currentTimeline: this.currentTimeline_,
|
858 | syncController: this.syncController_
|
859 | });
|
860 |
|
861 | if (!switchCandidate) {
|
862 | return;
|
863 | }
|
864 |
|
865 | const rebufferingImpact = requestTimeRemaining - timeUntilRebuffer;
|
866 |
|
867 | const timeSavedBySwitching = rebufferingImpact - switchCandidate.rebufferingImpact;
|
868 |
|
869 | let minimumTimeSaving = 0.5;
|
870 |
|
871 |
|
872 |
|
873 |
|
874 | if (timeUntilRebuffer <= TIME_FUDGE_FACTOR) {
|
875 | minimumTimeSaving = 1;
|
876 | }
|
877 |
|
878 | if (!switchCandidate.playlist ||
|
879 | switchCandidate.playlist.uri === this.playlist_.uri ||
|
880 | timeSavedBySwitching < minimumTimeSaving) {
|
881 | return false;
|
882 | }
|
883 |
|
884 |
|
885 |
|
886 |
|
887 | this.bandwidth =
|
888 | switchCandidate.playlist.attributes.BANDWIDTH * Config.BANDWIDTH_VARIANCE + 1;
|
889 | this.abort();
|
890 | this.trigger('earlyabort');
|
891 | return true;
|
892 | }
|
893 |
|
894 | |
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 | handleProgress_(event, simpleSegment) {
|
904 | if (!this.pendingSegment_ ||
|
905 | simpleSegment.requestId !== this.pendingSegment_.requestId ||
|
906 | this.abortRequestEarly_(simpleSegment.stats)) {
|
907 | return;
|
908 | }
|
909 |
|
910 | this.trigger('progress');
|
911 | }
|
912 |
|
913 | |
914 |
|
915 |
|
916 |
|
917 |
|
918 | loadSegment_(segmentInfo) {
|
919 | this.state = 'WAITING';
|
920 | this.pendingSegment_ = segmentInfo;
|
921 | this.trimBackBuffer_(segmentInfo);
|
922 |
|
923 | segmentInfo.abortRequests = mediaSegmentRequest(this.hls_.xhr,
|
924 | this.xhrOptions_,
|
925 | this.decrypter_,
|
926 | this.createSimplifiedSegmentObj_(segmentInfo),
|
927 |
|
928 | this.handleProgress_.bind(this),
|
929 | this.segmentRequestFinished_.bind(this));
|
930 | }
|
931 |
|
932 | |
933 |
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 | trimBackBuffer_(segmentInfo) {
|
941 | const removeToTime = safeBackBufferTrimTime(this.seekable_(),
|
942 | this.currentTime_(),
|
943 | this.playlist_.targetDuration || 10);
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 | if (removeToTime > 0) {
|
952 | this.remove(0, removeToTime);
|
953 | }
|
954 | }
|
955 |
|
956 | |
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 | createSimplifiedSegmentObj_(segmentInfo) {
|
966 | const segment = segmentInfo.segment;
|
967 | const simpleSegment = {
|
968 | resolvedUri: segment.resolvedUri,
|
969 | byterange: segment.byterange,
|
970 | requestId: segmentInfo.requestId
|
971 | };
|
972 |
|
973 | if (segment.key) {
|
974 |
|
975 |
|
976 | const iv = segment.key.iv || new Uint32Array([
|
977 | 0, 0, 0, segmentInfo.mediaIndex + segmentInfo.playlist.mediaSequence
|
978 | ]);
|
979 |
|
980 | simpleSegment.key = {
|
981 | resolvedUri: segment.key.resolvedUri,
|
982 | iv
|
983 | };
|
984 | }
|
985 |
|
986 | if (segment.map) {
|
987 | simpleSegment.map = this.initSegment(segment.map);
|
988 | }
|
989 |
|
990 | return simpleSegment;
|
991 | }
|
992 |
|
993 | |
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 | segmentRequestFinished_(error, simpleSegment) {
|
1000 |
|
1001 |
|
1002 | this.mediaRequests += 1;
|
1003 |
|
1004 | if (simpleSegment.stats) {
|
1005 | this.mediaBytesTransferred += simpleSegment.stats.bytesReceived;
|
1006 | this.mediaTransferDuration += simpleSegment.stats.roundTripTime;
|
1007 | }
|
1008 |
|
1009 |
|
1010 | if (!this.pendingSegment_) {
|
1011 | this.mediaRequestsAborted += 1;
|
1012 | return;
|
1013 | }
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 | if (simpleSegment.requestId !== this.pendingSegment_.requestId) {
|
1020 | return;
|
1021 | }
|
1022 |
|
1023 |
|
1024 | if (error) {
|
1025 | this.pendingSegment_ = null;
|
1026 | this.state = 'READY';
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
1032 | this.mediaRequestsAborted += 1;
|
1033 | return;
|
1034 | }
|
1035 |
|
1036 | this.pause();
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
1042 | this.mediaRequestsTimedout += 1;
|
1043 | this.bandwidth = 1;
|
1044 | this.roundTrip = NaN;
|
1045 | this.trigger('bandwidthupdate');
|
1046 | return;
|
1047 | }
|
1048 |
|
1049 |
|
1050 |
|
1051 | this.mediaRequestsErrored += 1;
|
1052 | this.error(error);
|
1053 | this.trigger('error');
|
1054 | return;
|
1055 | }
|
1056 |
|
1057 |
|
1058 |
|
1059 | this.bandwidth = simpleSegment.stats.bandwidth;
|
1060 | this.roundTrip = simpleSegment.stats.roundTripTime;
|
1061 |
|
1062 |
|
1063 |
|
1064 | if (simpleSegment.map) {
|
1065 | simpleSegment.map = this.initSegment(simpleSegment.map, true);
|
1066 | }
|
1067 |
|
1068 | this.processSegmentResponse_(simpleSegment);
|
1069 | }
|
1070 |
|
1071 | |
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 | processSegmentResponse_(simpleSegment) {
|
1078 | const segmentInfo = this.pendingSegment_;
|
1079 |
|
1080 | segmentInfo.bytes = simpleSegment.bytes;
|
1081 | if (simpleSegment.map) {
|
1082 | segmentInfo.segment.map.bytes = simpleSegment.map.bytes;
|
1083 | }
|
1084 |
|
1085 | segmentInfo.endOfAllRequests = simpleSegment.endOfAllRequests;
|
1086 | this.handleSegment_();
|
1087 | }
|
1088 |
|
1089 | |
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 | handleSegment_() {
|
1095 | if (!this.pendingSegment_) {
|
1096 | this.state = 'READY';
|
1097 | return;
|
1098 | }
|
1099 |
|
1100 | const segmentInfo = this.pendingSegment_;
|
1101 | const segment = segmentInfo.segment;
|
1102 | const timingInfo = this.syncController_.probeSegmentInfo(segmentInfo);
|
1103 |
|
1104 |
|
1105 |
|
1106 |
|
1107 | if (typeof this.startingMedia_ === 'undefined' &&
|
1108 | timingInfo &&
|
1109 |
|
1110 |
|
1111 | (timingInfo.containsAudio || timingInfo.containsVideo)) {
|
1112 | this.startingMedia_ = {
|
1113 | containsAudio: timingInfo.containsAudio,
|
1114 | containsVideo: timingInfo.containsVideo
|
1115 | };
|
1116 | }
|
1117 |
|
1118 | const illegalMediaSwitchError =
|
1119 | illegalMediaSwitch(this.loaderType_, this.startingMedia_, timingInfo);
|
1120 |
|
1121 | if (illegalMediaSwitchError) {
|
1122 | this.error({
|
1123 | message: illegalMediaSwitchError,
|
1124 | blacklistDuration: Infinity
|
1125 | });
|
1126 | this.trigger('error');
|
1127 | return;
|
1128 | }
|
1129 |
|
1130 | if (segmentInfo.isSyncRequest) {
|
1131 | this.trigger('syncinfoupdate');
|
1132 | this.pendingSegment_ = null;
|
1133 | this.state = 'READY';
|
1134 | return;
|
1135 | }
|
1136 |
|
1137 | if (segmentInfo.timestampOffset !== null &&
|
1138 | segmentInfo.timestampOffset !== this.sourceUpdater_.timestampOffset()) {
|
1139 | this.sourceUpdater_.timestampOffset(segmentInfo.timestampOffset);
|
1140 |
|
1141 | this.trigger('timestampoffset');
|
1142 | }
|
1143 |
|
1144 | const timelineMapping = this.syncController_.mappingForTimeline(segmentInfo.timeline);
|
1145 |
|
1146 | if (timelineMapping !== null) {
|
1147 | this.trigger({
|
1148 | type: 'segmenttimemapping',
|
1149 | mapping: timelineMapping
|
1150 | });
|
1151 | }
|
1152 |
|
1153 | this.state = 'APPENDING';
|
1154 |
|
1155 |
|
1156 |
|
1157 | if (segment.map) {
|
1158 | const initId = initSegmentId(segment.map);
|
1159 |
|
1160 | if (!this.activeInitSegmentId_ ||
|
1161 | this.activeInitSegmentId_ !== initId) {
|
1162 | const initSegment = this.initSegment(segment.map);
|
1163 |
|
1164 | this.sourceUpdater_.appendBuffer(initSegment.bytes, () => {
|
1165 | this.activeInitSegmentId_ = initId;
|
1166 | });
|
1167 | }
|
1168 | }
|
1169 |
|
1170 | segmentInfo.byteLength = segmentInfo.bytes.byteLength;
|
1171 | if (typeof segment.start === 'number' && typeof segment.end === 'number') {
|
1172 | this.mediaSecondsLoaded += segment.end - segment.start;
|
1173 | } else {
|
1174 | this.mediaSecondsLoaded += segment.duration;
|
1175 | }
|
1176 |
|
1177 | this.sourceUpdater_.appendBuffer(segmentInfo.bytes,
|
1178 | this.handleUpdateEnd_.bind(this));
|
1179 | }
|
1180 |
|
1181 | |
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 | handleUpdateEnd_() {
|
1189 | this.logger_('handleUpdateEnd_', 'segmentInfo:', this.pendingSegment_);
|
1190 |
|
1191 | if (!this.pendingSegment_) {
|
1192 | this.state = 'READY';
|
1193 | if (!this.paused()) {
|
1194 | this.monitorBuffer_();
|
1195 | }
|
1196 | return;
|
1197 | }
|
1198 |
|
1199 | const segmentInfo = this.pendingSegment_;
|
1200 | const segment = segmentInfo.segment;
|
1201 | const isWalkingForward = this.mediaIndex !== null;
|
1202 |
|
1203 | this.pendingSegment_ = null;
|
1204 | this.recordThroughput_(segmentInfo);
|
1205 | this.addSegmentMetadataCue_(segmentInfo);
|
1206 |
|
1207 | this.state = 'READY';
|
1208 |
|
1209 | this.mediaIndex = segmentInfo.mediaIndex;
|
1210 | this.fetchAtBuffer_ = true;
|
1211 | this.currentTimeline_ = segmentInfo.timeline;
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 | this.trigger('syncinfoupdate');
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 | if (segment.end &&
|
1224 | this.currentTime_() - segment.end > segmentInfo.playlist.targetDuration * 3) {
|
1225 | this.resetEverything();
|
1226 | return;
|
1227 | }
|
1228 |
|
1229 |
|
1230 |
|
1231 | if (isWalkingForward) {
|
1232 | this.trigger('bandwidthupdate');
|
1233 | }
|
1234 | this.trigger('progress');
|
1235 |
|
1236 |
|
1237 |
|
1238 |
|
1239 | const isEndOfStream = detectEndOfStream(segmentInfo.playlist,
|
1240 | this.mediaSource_,
|
1241 | segmentInfo.mediaIndex + 1);
|
1242 |
|
1243 | if (isEndOfStream) {
|
1244 | this.endOfStream();
|
1245 | }
|
1246 |
|
1247 | if (!this.paused()) {
|
1248 | this.monitorBuffer_();
|
1249 | }
|
1250 | }
|
1251 |
|
1252 | |
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 | recordThroughput_(segmentInfo) {
|
1262 | const rate = this.throughput.rate;
|
1263 |
|
1264 |
|
1265 | const segmentProcessingTime =
|
1266 | Date.now() - segmentInfo.endOfAllRequests + 1;
|
1267 |
|
1268 | const segmentProcessingThroughput =
|
1269 | Math.floor((segmentInfo.byteLength / segmentProcessingTime) * 8 * 1000);
|
1270 |
|
1271 |
|
1272 |
|
1273 | this.throughput.rate +=
|
1274 | (segmentProcessingThroughput - rate) / (++this.throughput.count);
|
1275 | }
|
1276 |
|
1277 | |
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 |
|
1283 | logger_() {}
|
1284 |
|
1285 | |
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 | addSegmentMetadataCue_(segmentInfo) {
|
1295 | if (!this.segmentMetadataTrack_) {
|
1296 | return;
|
1297 | }
|
1298 |
|
1299 | const segment = segmentInfo.segment;
|
1300 | const start = segment.start;
|
1301 | const end = segment.end;
|
1302 |
|
1303 |
|
1304 | if (!finite(start) || !finite(end)) {
|
1305 | return;
|
1306 | }
|
1307 |
|
1308 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
1309 |
|
1310 | const Cue = window.WebKitDataCue || window.VTTCue;
|
1311 | const value = {
|
1312 | uri: segmentInfo.uri,
|
1313 | timeline: segmentInfo.timeline,
|
1314 | playlist: segmentInfo.playlist.uri,
|
1315 | start,
|
1316 | end
|
1317 | };
|
1318 | const data = JSON.stringify(value);
|
1319 | const cue = new Cue(start, end, data);
|
1320 |
|
1321 |
|
1322 |
|
1323 | cue.value = value;
|
1324 |
|
1325 | this.segmentMetadataTrack_.addCue(cue);
|
1326 | }
|
1327 | }
|