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