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