1 |
|
2 |
|
3 |
|
4 | import Playlist from './playlist';
|
5 | import videojs from 'video.js';
|
6 | import Config from './config';
|
7 | import window from 'global/window';
|
8 | import { initSegmentId, segmentKeyId } from './bin-utils';
|
9 | import { mediaSegmentRequest, REQUEST_ERRORS } from './media-segment-request';
|
10 | import segmentTransmuxer from './segment-transmuxer';
|
11 | import { TIME_FUDGE_FACTOR, timeUntilRebuffer as timeUntilRebuffer_ } from './ranges';
|
12 | import { minRebufferMaxBandwidthSelector } from './playlist-selectors';
|
13 | import logger from './util/logger';
|
14 | import { concatSegments } from './util/segment';
|
15 | import {
|
16 | createCaptionsTrackIfNotExists,
|
17 | createMetadataTrackIfNotExists,
|
18 | addMetadata,
|
19 | addCaptionData,
|
20 | removeCuesFromTrack
|
21 | } from './util/text-tracks';
|
22 | import { gopsSafeToAlignWith, removeGopBuffer, updateGopBuffer } from './util/gops';
|
23 | import shallowEqual from './util/shallow-equal.js';
|
24 | import { QUOTA_EXCEEDED_ERR } from './error-codes';
|
25 | import {timeRangesToArray, lastBufferedEnd, timeAheadOf} from './ranges.js';
|
26 | import {getKnownPartCount} from './playlist.js';
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export const getSyncSegmentCandidate = function(currentTimeline, segments, targetTime) {
|
38 | segments = segments || [];
|
39 | const timelineSegments = [];
|
40 | let time = 0;
|
41 |
|
42 | for (let i = 0; i < segments.length; i++) {
|
43 | const segment = segments[i];
|
44 |
|
45 | if (currentTimeline === segment.timeline) {
|
46 | timelineSegments.push(i);
|
47 | time += segment.duration;
|
48 |
|
49 | if (time > targetTime) {
|
50 | return i;
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 | if (timelineSegments.length === 0) {
|
56 | return 0;
|
57 | }
|
58 |
|
59 |
|
60 | return timelineSegments[timelineSegments.length - 1];
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const MIN_BACK_BUFFER = 1;
|
68 |
|
69 |
|
70 | const CHECK_BUFFER_DELAY = 500;
|
71 | const finite = (num) => typeof num === 'number' && isFinite(num);
|
72 |
|
73 |
|
74 |
|
75 | const MIN_SEGMENT_DURATION_TO_SAVE_STATS = 1 / 60;
|
76 |
|
77 | export const illegalMediaSwitch = (loaderType, startingMedia, trackInfo) => {
|
78 |
|
79 |
|
80 | if (loaderType !== 'main' || !startingMedia || !trackInfo) {
|
81 | return null;
|
82 | }
|
83 |
|
84 | if (!trackInfo.hasAudio && !trackInfo.hasVideo) {
|
85 | return 'Neither audio nor video found in segment.';
|
86 | }
|
87 |
|
88 | if (startingMedia.hasVideo && !trackInfo.hasVideo) {
|
89 | return 'Only audio found in segment when we expected video.' +
|
90 | ' We can\'t switch to audio only from a stream that had video.' +
|
91 | ' To get rid of this message, please add codec information to the manifest.';
|
92 | }
|
93 |
|
94 | if (!startingMedia.hasVideo && trackInfo.hasVideo) {
|
95 | return 'Video found in segment when we expected only audio.' +
|
96 | ' We can\'t switch to a stream with video from an audio only stream.' +
|
97 | ' To get rid of this message, please add codec information to the manifest.';
|
98 | }
|
99 |
|
100 | return null;
|
101 | };
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | export const safeBackBufferTrimTime = (seekable, currentTime, targetDuration) => {
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | let trimTime = currentTime - Config.BACK_BUFFER_LENGTH;
|
123 |
|
124 | if (seekable.length) {
|
125 |
|
126 |
|
127 | trimTime = Math.max(trimTime, seekable.start(0));
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 | const maxTrimTime = currentTime - targetDuration;
|
133 |
|
134 | return Math.min(maxTrimTime, trimTime);
|
135 | };
|
136 |
|
137 | export const segmentInfoString = (segmentInfo) => {
|
138 | const {
|
139 | startOfSegment,
|
140 | duration,
|
141 | segment,
|
142 | part,
|
143 | playlist: {
|
144 | mediaSequence: seq,
|
145 | id,
|
146 | segments = []
|
147 | },
|
148 | mediaIndex: index,
|
149 | partIndex,
|
150 | timeline
|
151 | } = segmentInfo;
|
152 |
|
153 | const segmentLen = segments.length - 1;
|
154 | let selection = 'mediaIndex/partIndex increment';
|
155 |
|
156 | if (segmentInfo.getMediaInfoForTime) {
|
157 | selection = `getMediaInfoForTime (${segmentInfo.getMediaInfoForTime})`;
|
158 | } else if (segmentInfo.isSyncRequest) {
|
159 | selection = 'getSyncSegmentCandidate (isSyncRequest)';
|
160 | }
|
161 |
|
162 | if (segmentInfo.independent) {
|
163 | selection += ` with independent ${segmentInfo.independent}`;
|
164 | }
|
165 |
|
166 | const hasPartIndex = typeof partIndex === 'number';
|
167 | const name = segmentInfo.segment.uri ? 'segment' : 'pre-segment';
|
168 | const zeroBasedPartCount = hasPartIndex ? getKnownPartCount({preloadSegment: segment}) - 1 : 0;
|
169 |
|
170 | return `${name} [${seq + index}/${seq + segmentLen}]` +
|
171 | (hasPartIndex ? ` part [${partIndex}/${zeroBasedPartCount}]` : '') +
|
172 | ` segment start/end [${segment.start} => ${segment.end}]` +
|
173 | (hasPartIndex ? ` part start/end [${part.start} => ${part.end}]` : '') +
|
174 | ` startOfSegment [${startOfSegment}]` +
|
175 | ` duration [${duration}]` +
|
176 | ` timeline [${timeline}]` +
|
177 | ` selected by [${selection}]` +
|
178 | ` playlist [${id}]`;
|
179 | };
|
180 |
|
181 | const timingInfoPropertyForMedia = (mediaType) => `${mediaType}TimingInfo`;
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | export const timestampOffsetForSegment = ({
|
203 | segmentTimeline,
|
204 | currentTimeline,
|
205 | startOfSegment,
|
206 | buffered,
|
207 | overrideCheck
|
208 | }) => {
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | if (!overrideCheck && segmentTimeline === currentTimeline) {
|
216 | return null;
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | if (segmentTimeline < currentTimeline) {
|
244 | return startOfSegment;
|
245 | }
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | return buffered.length ? buffered.end(buffered.length - 1) : startOfSegment;
|
253 | };
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | export const shouldWaitForTimelineChange = ({
|
351 | timelineChangeController,
|
352 | currentTimeline,
|
353 | segmentTimeline,
|
354 | loaderType,
|
355 | audioDisabled
|
356 | }) => {
|
357 | if (currentTimeline === segmentTimeline) {
|
358 | return false;
|
359 | }
|
360 |
|
361 | if (loaderType === 'audio') {
|
362 | const lastMainTimelineChange = timelineChangeController.lastTimelineChange({
|
363 | type: 'main'
|
364 | });
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 | return !lastMainTimelineChange || lastMainTimelineChange.to !== segmentTimeline;
|
371 | }
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | if (loaderType === 'main' && audioDisabled) {
|
378 | const pendingAudioTimelineChange = timelineChangeController.pendingTimelineChange({
|
379 | type: 'audio'
|
380 | });
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | if (pendingAudioTimelineChange && pendingAudioTimelineChange.to === segmentTimeline) {
|
401 | return false;
|
402 | }
|
403 |
|
404 | return true;
|
405 | }
|
406 |
|
407 | return false;
|
408 | };
|
409 |
|
410 | export const mediaDuration = (timingInfos) => {
|
411 | let maxDuration = 0;
|
412 |
|
413 | ['video', 'audio'].forEach(function(type) {
|
414 | const typeTimingInfo = timingInfos[`${type}TimingInfo`];
|
415 |
|
416 | if (!typeTimingInfo) {
|
417 | return;
|
418 | }
|
419 | const {start, end} = typeTimingInfo;
|
420 | let duration;
|
421 |
|
422 | if (typeof start === 'bigint' || typeof end === 'bigint') {
|
423 | duration = window.BigInt(end) - window.BigInt(start);
|
424 | } else if (typeof start === 'number' && typeof end === 'number') {
|
425 | duration = end - start;
|
426 | }
|
427 |
|
428 | if (typeof duration !== 'undefined' && duration > maxDuration) {
|
429 | maxDuration = duration;
|
430 | }
|
431 | });
|
432 |
|
433 |
|
434 |
|
435 | if (typeof maxDuration === 'bigint' && maxDuration < Number.MAX_SAFE_INTEGER) {
|
436 | maxDuration = Number(maxDuration);
|
437 | }
|
438 |
|
439 | return maxDuration;
|
440 | };
|
441 |
|
442 | export const segmentTooLong = ({ segmentDuration, maxDuration }) => {
|
443 |
|
444 |
|
445 | if (!segmentDuration) {
|
446 | return false;
|
447 | }
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 | return Math.round(segmentDuration) > maxDuration + TIME_FUDGE_FACTOR;
|
461 | };
|
462 |
|
463 | export const getTroublesomeSegmentDurationMessage = (segmentInfo, sourceType) => {
|
464 |
|
465 |
|
466 | if (sourceType !== 'hls') {
|
467 | return null;
|
468 | }
|
469 |
|
470 | const segmentDuration = mediaDuration({
|
471 | audioTimingInfo: segmentInfo.audioTimingInfo,
|
472 | videoTimingInfo: segmentInfo.videoTimingInfo
|
473 | });
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | if (!segmentDuration) {
|
480 | return null;
|
481 | }
|
482 |
|
483 | const targetDuration = segmentInfo.playlist.targetDuration;
|
484 |
|
485 | const isSegmentWayTooLong = segmentTooLong({
|
486 | segmentDuration,
|
487 | maxDuration: targetDuration * 2
|
488 | });
|
489 | const isSegmentSlightlyTooLong = segmentTooLong({
|
490 | segmentDuration,
|
491 | maxDuration: targetDuration
|
492 | });
|
493 |
|
494 | const segmentTooLongMessage = `Segment with index ${segmentInfo.mediaIndex} ` +
|
495 | `from playlist ${segmentInfo.playlist.id} ` +
|
496 | `has a duration of ${segmentDuration} ` +
|
497 | `when the reported duration is ${segmentInfo.duration} ` +
|
498 | `and the target duration is ${targetDuration}. ` +
|
499 | 'For HLS content, a duration in excess of the target duration may result in ' +
|
500 | 'playback issues. See the HLS specification section on EXT-X-TARGETDURATION for ' +
|
501 | 'more details: ' +
|
502 | 'https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.3.1';
|
503 |
|
504 | if (isSegmentWayTooLong || isSegmentSlightlyTooLong) {
|
505 | return {
|
506 | severity: isSegmentWayTooLong ? 'warn' : 'info',
|
507 | message: segmentTooLongMessage
|
508 | };
|
509 | }
|
510 |
|
511 | return null;
|
512 | };
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | export default class SegmentLoader extends videojs.EventTarget {
|
522 | constructor(settings, options = {}) {
|
523 | super();
|
524 |
|
525 | if (!settings) {
|
526 | throw new TypeError('Initialization settings are required');
|
527 | }
|
528 | if (typeof settings.currentTime !== 'function') {
|
529 | throw new TypeError('No currentTime getter specified');
|
530 | }
|
531 | if (!settings.mediaSource) {
|
532 | throw new TypeError('No MediaSource specified');
|
533 | }
|
534 |
|
535 | this.bandwidth = settings.bandwidth;
|
536 | this.throughput = {rate: 0, count: 0};
|
537 | this.roundTrip = NaN;
|
538 | this.resetStats_();
|
539 | this.mediaIndex = null;
|
540 | this.partIndex = null;
|
541 |
|
542 |
|
543 | this.hasPlayed_ = settings.hasPlayed;
|
544 | this.currentTime_ = settings.currentTime;
|
545 | this.seekable_ = settings.seekable;
|
546 | this.seeking_ = settings.seeking;
|
547 | this.duration_ = settings.duration;
|
548 | this.mediaSource_ = settings.mediaSource;
|
549 | this.vhs_ = settings.vhs;
|
550 | this.loaderType_ = settings.loaderType;
|
551 | this.currentMediaInfo_ = void 0;
|
552 | this.startingMediaInfo_ = void 0;
|
553 | this.segmentMetadataTrack_ = settings.segmentMetadataTrack;
|
554 | this.goalBufferLength_ = settings.goalBufferLength;
|
555 | this.sourceType_ = settings.sourceType;
|
556 | this.sourceUpdater_ = settings.sourceUpdater;
|
557 | this.inbandTextTracks_ = settings.inbandTextTracks;
|
558 | this.state_ = 'INIT';
|
559 | this.timelineChangeController_ = settings.timelineChangeController;
|
560 | this.shouldSaveSegmentTimingInfo_ = true;
|
561 | this.parse708captions_ = settings.parse708captions;
|
562 | this.useDtsForTimestampOffset_ = settings.useDtsForTimestampOffset;
|
563 | this.captionServices_ = settings.captionServices;
|
564 | this.experimentalExactManifestTimings = settings.experimentalExactManifestTimings;
|
565 |
|
566 |
|
567 | this.checkBufferTimeout_ = null;
|
568 | this.error_ = void 0;
|
569 | this.currentTimeline_ = -1;
|
570 | this.pendingSegment_ = null;
|
571 | this.xhrOptions_ = null;
|
572 | this.pendingSegments_ = [];
|
573 | this.audioDisabled_ = false;
|
574 | this.isPendingTimestampOffset_ = false;
|
575 |
|
576 | this.gopBuffer_ = [];
|
577 | this.timeMapping_ = 0;
|
578 | this.safeAppend_ = videojs.browser.IE_VERSION >= 11;
|
579 | this.appendInitSegment_ = {
|
580 | audio: true,
|
581 | video: true
|
582 | };
|
583 | this.playlistOfLastInitSegment_ = {
|
584 | audio: null,
|
585 | video: null
|
586 | };
|
587 | this.callQueue_ = [];
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 | this.loadQueue_ = [];
|
594 | this.metadataQueue_ = {
|
595 | id3: [],
|
596 | caption: []
|
597 | };
|
598 | this.waitingOnRemove_ = false;
|
599 | this.quotaExceededErrorRetryTimeout_ = null;
|
600 |
|
601 |
|
602 | this.activeInitSegmentId_ = null;
|
603 | this.initSegments_ = {};
|
604 |
|
605 |
|
606 | this.cacheEncryptionKeys_ = settings.cacheEncryptionKeys;
|
607 | this.keyCache_ = {};
|
608 |
|
609 | this.decrypter_ = settings.decrypter;
|
610 |
|
611 |
|
612 |
|
613 |
|
614 | this.syncController_ = settings.syncController;
|
615 | this.syncPoint_ = {
|
616 | segmentIndex: 0,
|
617 | time: 0
|
618 | };
|
619 |
|
620 | this.transmuxer_ = this.createTransmuxer_();
|
621 | this.triggerSyncInfoUpdate_ = () => this.trigger('syncinfoupdate');
|
622 | this.syncController_.on('syncinfoupdate', this.triggerSyncInfoUpdate_);
|
623 |
|
624 | this.mediaSource_.addEventListener('sourceopen', () => {
|
625 | if (!this.isEndOfStream_()) {
|
626 | this.ended_ = false;
|
627 | }
|
628 | });
|
629 |
|
630 |
|
631 | this.fetchAtBuffer_ = false;
|
632 |
|
633 | this.logger_ = logger(`SegmentLoader[${this.loaderType_}]`);
|
634 |
|
635 | Object.defineProperty(this, 'state', {
|
636 | get() {
|
637 | return this.state_;
|
638 | },
|
639 | set(newState) {
|
640 | if (newState !== this.state_) {
|
641 | this.logger_(`${this.state_} -> ${newState}`);
|
642 | this.state_ = newState;
|
643 | this.trigger('statechange');
|
644 | }
|
645 | }
|
646 | });
|
647 |
|
648 | this.sourceUpdater_.on('ready', () => {
|
649 | if (this.hasEnoughInfoToAppend_()) {
|
650 | this.processCallQueue_();
|
651 | }
|
652 | });
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 | if (this.loaderType_ === 'main') {
|
659 | this.timelineChangeController_.on('pendingtimelinechange', () => {
|
660 | if (this.hasEnoughInfoToAppend_()) {
|
661 | this.processCallQueue_();
|
662 | }
|
663 | });
|
664 | }
|
665 |
|
666 |
|
667 |
|
668 | if (this.loaderType_ === 'audio') {
|
669 | this.timelineChangeController_.on('timelinechange', () => {
|
670 | if (this.hasEnoughInfoToLoad_()) {
|
671 | this.processLoadQueue_();
|
672 | }
|
673 | if (this.hasEnoughInfoToAppend_()) {
|
674 | this.processCallQueue_();
|
675 | }
|
676 | });
|
677 | }
|
678 | }
|
679 |
|
680 | createTransmuxer_() {
|
681 | return segmentTransmuxer.createTransmuxer({
|
682 | remux: false,
|
683 | alignGopsAtEnd: this.safeAppend_,
|
684 | keepOriginalTimestamps: true,
|
685 | parse708captions: this.parse708captions_,
|
686 | captionServices: this.captionServices_
|
687 | });
|
688 | }
|
689 |
|
690 | |
691 |
|
692 |
|
693 |
|
694 |
|
695 | resetStats_() {
|
696 | this.mediaBytesTransferred = 0;
|
697 | this.mediaRequests = 0;
|
698 | this.mediaRequestsAborted = 0;
|
699 | this.mediaRequestsTimedout = 0;
|
700 | this.mediaRequestsErrored = 0;
|
701 | this.mediaTransferDuration = 0;
|
702 | this.mediaSecondsLoaded = 0;
|
703 | this.mediaAppends = 0;
|
704 | }
|
705 |
|
706 | |
707 |
|
708 |
|
709 | dispose() {
|
710 | this.trigger('dispose');
|
711 | this.state = 'DISPOSED';
|
712 | this.pause();
|
713 | this.abort_();
|
714 | if (this.transmuxer_) {
|
715 | this.transmuxer_.terminate();
|
716 | }
|
717 | this.resetStats_();
|
718 |
|
719 | if (this.checkBufferTimeout_) {
|
720 | window.clearTimeout(this.checkBufferTimeout_);
|
721 | }
|
722 |
|
723 | if (this.syncController_ && this.triggerSyncInfoUpdate_) {
|
724 | this.syncController_.off('syncinfoupdate', this.triggerSyncInfoUpdate_);
|
725 | }
|
726 |
|
727 | this.off();
|
728 | }
|
729 |
|
730 | setAudio(enable) {
|
731 | this.audioDisabled_ = !enable;
|
732 | if (enable) {
|
733 | this.appendInitSegment_.audio = true;
|
734 | } else {
|
735 |
|
736 | this.sourceUpdater_.removeAudio(0, this.duration_());
|
737 | }
|
738 | }
|
739 |
|
740 | |
741 |
|
742 |
|
743 |
|
744 | abort() {
|
745 | if (this.state !== 'WAITING') {
|
746 | if (this.pendingSegment_) {
|
747 | this.pendingSegment_ = null;
|
748 | }
|
749 | return;
|
750 | }
|
751 |
|
752 | this.abort_();
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 | this.state = 'READY';
|
759 |
|
760 |
|
761 |
|
762 | if (!this.paused()) {
|
763 | this.monitorBuffer_();
|
764 | }
|
765 | }
|
766 |
|
767 | |
768 |
|
769 |
|
770 |
|
771 |
|
772 | abort_() {
|
773 | if (this.pendingSegment_ && this.pendingSegment_.abortRequests) {
|
774 | this.pendingSegment_.abortRequests();
|
775 | }
|
776 |
|
777 |
|
778 | this.pendingSegment_ = null;
|
779 | this.callQueue_ = [];
|
780 | this.loadQueue_ = [];
|
781 | this.metadataQueue_.id3 = [];
|
782 | this.metadataQueue_.caption = [];
|
783 | this.timelineChangeController_.clearPendingTimelineChange(this.loaderType_);
|
784 | this.waitingOnRemove_ = false;
|
785 | window.clearTimeout(this.quotaExceededErrorRetryTimeout_);
|
786 | this.quotaExceededErrorRetryTimeout_ = null;
|
787 | }
|
788 |
|
789 | checkForAbort_(requestId) {
|
790 |
|
791 |
|
792 | if (this.state === 'APPENDING' && !this.pendingSegment_) {
|
793 | this.state = 'READY';
|
794 | return true;
|
795 | }
|
796 |
|
797 | if (!this.pendingSegment_ || this.pendingSegment_.requestId !== requestId) {
|
798 | return true;
|
799 | }
|
800 |
|
801 | return false;
|
802 | }
|
803 |
|
804 | |
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 | error(error) {
|
811 | if (typeof error !== 'undefined') {
|
812 | this.logger_('error occurred:', error);
|
813 | this.error_ = error;
|
814 | }
|
815 |
|
816 | this.pendingSegment_ = null;
|
817 | return this.error_;
|
818 | }
|
819 |
|
820 | endOfStream() {
|
821 | this.ended_ = true;
|
822 | if (this.transmuxer_) {
|
823 |
|
824 | segmentTransmuxer.reset(this.transmuxer_);
|
825 | }
|
826 | this.gopBuffer_.length = 0;
|
827 | this.pause();
|
828 | this.trigger('ended');
|
829 | }
|
830 |
|
831 | |
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 | buffered_() {
|
838 | const trackInfo = this.getMediaInfo_();
|
839 |
|
840 | if (!this.sourceUpdater_ || !trackInfo) {
|
841 | return videojs.createTimeRanges();
|
842 | }
|
843 |
|
844 | if (this.loaderType_ === 'main') {
|
845 | const { hasAudio, hasVideo, isMuxed } = trackInfo;
|
846 |
|
847 | if (hasVideo && hasAudio && !this.audioDisabled_ && !isMuxed) {
|
848 | return this.sourceUpdater_.buffered();
|
849 | }
|
850 |
|
851 | if (hasVideo) {
|
852 | return this.sourceUpdater_.videoBuffered();
|
853 | }
|
854 | }
|
855 |
|
856 |
|
857 |
|
858 | return this.sourceUpdater_.audioBuffered();
|
859 | }
|
860 |
|
861 | |
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 | initSegmentForMap(map, set = false) {
|
872 | if (!map) {
|
873 | return null;
|
874 | }
|
875 |
|
876 | const id = initSegmentId(map);
|
877 | let storedMap = this.initSegments_[id];
|
878 |
|
879 | if (set && !storedMap && map.bytes) {
|
880 | this.initSegments_[id] = storedMap = {
|
881 | resolvedUri: map.resolvedUri,
|
882 | byterange: map.byterange,
|
883 | bytes: map.bytes,
|
884 | tracks: map.tracks,
|
885 | timescales: map.timescales
|
886 | };
|
887 | }
|
888 |
|
889 | return storedMap || map;
|
890 | }
|
891 |
|
892 | |
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 | segmentKey(key, set = false) {
|
903 | if (!key) {
|
904 | return null;
|
905 | }
|
906 |
|
907 | const id = segmentKeyId(key);
|
908 | let storedKey = this.keyCache_[id];
|
909 |
|
910 |
|
911 |
|
912 | if (this.cacheEncryptionKeys_ && set && !storedKey && key.bytes) {
|
913 | this.keyCache_[id] = storedKey = {
|
914 | resolvedUri: key.resolvedUri,
|
915 | bytes: key.bytes
|
916 | };
|
917 | }
|
918 |
|
919 | const result = {
|
920 | resolvedUri: (storedKey || key).resolvedUri
|
921 | };
|
922 |
|
923 | if (storedKey) {
|
924 | result.bytes = storedKey.bytes;
|
925 | }
|
926 |
|
927 | return result;
|
928 | }
|
929 |
|
930 | |
931 |
|
932 |
|
933 |
|
934 |
|
935 |
|
936 | couldBeginLoading_() {
|
937 | return this.playlist_ && !this.paused();
|
938 | }
|
939 |
|
940 | |
941 |
|
942 |
|
943 | load() {
|
944 |
|
945 | this.monitorBuffer_();
|
946 |
|
947 |
|
948 |
|
949 | if (!this.playlist_) {
|
950 | return;
|
951 | }
|
952 |
|
953 |
|
954 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
955 | return this.init_();
|
956 | }
|
957 |
|
958 |
|
959 |
|
960 | if (!this.couldBeginLoading_() ||
|
961 | (this.state !== 'READY' &&
|
962 | this.state !== 'INIT')) {
|
963 | return;
|
964 | }
|
965 |
|
966 | this.state = 'READY';
|
967 | }
|
968 |
|
969 | |
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 | init_() {
|
977 | this.state = 'READY';
|
978 |
|
979 |
|
980 | this.resetEverything();
|
981 | return this.monitorBuffer_();
|
982 | }
|
983 |
|
984 | |
985 |
|
986 |
|
987 |
|
988 |
|
989 | playlist(newPlaylist, options = {}) {
|
990 | if (!newPlaylist) {
|
991 | return;
|
992 | }
|
993 | const oldPlaylist = this.playlist_;
|
994 | const segmentInfo = this.pendingSegment_;
|
995 |
|
996 | this.playlist_ = newPlaylist;
|
997 | this.xhrOptions_ = options;
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 | if (this.state === 'INIT') {
|
1007 | newPlaylist.syncInfo = {
|
1008 | mediaSequence: newPlaylist.mediaSequence,
|
1009 | time: 0
|
1010 | };
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 | if (this.loaderType_ === 'main') {
|
1020 | this.syncController_.setDateTimeMappingForStart(newPlaylist);
|
1021 | }
|
1022 | }
|
1023 |
|
1024 | let oldId = null;
|
1025 |
|
1026 | if (oldPlaylist) {
|
1027 | if (oldPlaylist.id) {
|
1028 | oldId = oldPlaylist.id;
|
1029 | } else if (oldPlaylist.uri) {
|
1030 | oldId = oldPlaylist.uri;
|
1031 | }
|
1032 | }
|
1033 |
|
1034 | this.logger_(`playlist update [${oldId} => ${newPlaylist.id || newPlaylist.uri}]`);
|
1035 |
|
1036 |
|
1037 |
|
1038 | this.trigger('syncinfoupdate');
|
1039 |
|
1040 |
|
1041 |
|
1042 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
1043 | return this.init_();
|
1044 | }
|
1045 |
|
1046 | if (!oldPlaylist || oldPlaylist.uri !== newPlaylist.uri) {
|
1047 | if (this.mediaIndex !== null) {
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 | if (!newPlaylist.endList) {
|
1058 | this.resetLoader();
|
1059 | } else {
|
1060 | this.resyncLoader();
|
1061 | }
|
1062 | }
|
1063 | this.currentMediaInfo_ = void 0;
|
1064 | this.trigger('playlistupdate');
|
1065 |
|
1066 |
|
1067 | return;
|
1068 | }
|
1069 |
|
1070 |
|
1071 |
|
1072 | const mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
1073 |
|
1074 | this.logger_(`live window shift [${mediaSequenceDiff}]`);
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 | if (this.mediaIndex !== null) {
|
1080 | this.mediaIndex -= mediaSequenceDiff;
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 | if (this.mediaIndex < 0) {
|
1086 | this.mediaIndex = null;
|
1087 | this.partIndex = null;
|
1088 | } else {
|
1089 | const segment = this.playlist_.segments[this.mediaIndex];
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 | if (this.partIndex && (!segment.parts || !segment.parts.length || !segment.parts[this.partIndex])) {
|
1095 | const mediaIndex = this.mediaIndex;
|
1096 |
|
1097 | this.logger_(`currently processing part (index ${this.partIndex}) no longer exists.`);
|
1098 | this.resetLoader();
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 | this.mediaIndex = mediaIndex;
|
1104 | }
|
1105 | }
|
1106 | }
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 | if (segmentInfo) {
|
1112 | segmentInfo.mediaIndex -= mediaSequenceDiff;
|
1113 |
|
1114 | if (segmentInfo.mediaIndex < 0) {
|
1115 | segmentInfo.mediaIndex = null;
|
1116 | segmentInfo.partIndex = null;
|
1117 | } else {
|
1118 |
|
1119 |
|
1120 |
|
1121 | if (segmentInfo.mediaIndex >= 0) {
|
1122 | segmentInfo.segment = newPlaylist.segments[segmentInfo.mediaIndex];
|
1123 | }
|
1124 |
|
1125 | if (segmentInfo.partIndex >= 0 && segmentInfo.segment.parts) {
|
1126 | segmentInfo.part = segmentInfo.segment.parts[segmentInfo.partIndex];
|
1127 | }
|
1128 | }
|
1129 | }
|
1130 |
|
1131 | this.syncController_.saveExpiredSegmentInfo(oldPlaylist, newPlaylist);
|
1132 | }
|
1133 |
|
1134 | |
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 | pause() {
|
1141 | if (this.checkBufferTimeout_) {
|
1142 | window.clearTimeout(this.checkBufferTimeout_);
|
1143 |
|
1144 | this.checkBufferTimeout_ = null;
|
1145 | }
|
1146 | }
|
1147 |
|
1148 | |
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 | paused() {
|
1154 | return this.checkBufferTimeout_ === null;
|
1155 | }
|
1156 |
|
1157 | |
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 | resetEverything(done) {
|
1164 | this.ended_ = false;
|
1165 | this.appendInitSegment_ = {
|
1166 | audio: true,
|
1167 | video: true
|
1168 | };
|
1169 | this.resetLoader();
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 | this.remove(0, Infinity, done);
|
1175 |
|
1176 |
|
1177 | if (this.transmuxer_) {
|
1178 | this.transmuxer_.postMessage({
|
1179 | action: 'clearAllMp4Captions'
|
1180 | });
|
1181 |
|
1182 |
|
1183 | this.transmuxer_.postMessage({
|
1184 | action: 'reset'
|
1185 | });
|
1186 | }
|
1187 | }
|
1188 |
|
1189 | |
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 | resetLoader() {
|
1196 | this.fetchAtBuffer_ = false;
|
1197 | this.resyncLoader();
|
1198 | }
|
1199 |
|
1200 | |
1201 |
|
1202 |
|
1203 |
|
1204 | resyncLoader() {
|
1205 | if (this.transmuxer_) {
|
1206 |
|
1207 | segmentTransmuxer.reset(this.transmuxer_);
|
1208 | }
|
1209 | this.mediaIndex = null;
|
1210 | this.partIndex = null;
|
1211 | this.syncPoint_ = null;
|
1212 | this.isPendingTimestampOffset_ = false;
|
1213 | this.callQueue_ = [];
|
1214 | this.loadQueue_ = [];
|
1215 | this.metadataQueue_.id3 = [];
|
1216 | this.metadataQueue_.caption = [];
|
1217 | this.abort();
|
1218 |
|
1219 | if (this.transmuxer_) {
|
1220 | this.transmuxer_.postMessage({
|
1221 | action: 'clearParsedMp4Captions'
|
1222 | });
|
1223 | }
|
1224 | }
|
1225 |
|
1226 | |
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 |
|
1233 |
|
1234 |
|
1235 | remove(start, end, done = () => {}, force = false) {
|
1236 |
|
1237 |
|
1238 |
|
1239 | if (end === Infinity) {
|
1240 | end = this.duration_();
|
1241 | }
|
1242 |
|
1243 |
|
1244 |
|
1245 |
|
1246 | if (end <= start) {
|
1247 | this.logger_('skipping remove because end ${end} is <= start ${start}');
|
1248 | return;
|
1249 | }
|
1250 |
|
1251 | if (!this.sourceUpdater_ || !this.getMediaInfo_()) {
|
1252 | this.logger_('skipping remove because no source updater or starting media info');
|
1253 |
|
1254 | return;
|
1255 | }
|
1256 |
|
1257 |
|
1258 | let removesRemaining = 1;
|
1259 | const removeFinished = () => {
|
1260 | removesRemaining--;
|
1261 | if (removesRemaining === 0) {
|
1262 | done();
|
1263 | }
|
1264 | };
|
1265 |
|
1266 | if (force || !this.audioDisabled_) {
|
1267 | removesRemaining++;
|
1268 | this.sourceUpdater_.removeAudio(start, end, removeFinished);
|
1269 | }
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 | if (force || this.loaderType_ === 'main') {
|
1280 | this.gopBuffer_ = removeGopBuffer(this.gopBuffer_, start, end, this.timeMapping_);
|
1281 | removesRemaining++;
|
1282 | this.sourceUpdater_.removeVideo(start, end, removeFinished);
|
1283 | }
|
1284 |
|
1285 |
|
1286 | for (const track in this.inbandTextTracks_) {
|
1287 | removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
|
1288 | }
|
1289 |
|
1290 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
1291 |
|
1292 |
|
1293 | removeFinished();
|
1294 | }
|
1295 |
|
1296 | |
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 | monitorBuffer_() {
|
1302 | if (this.checkBufferTimeout_) {
|
1303 | window.clearTimeout(this.checkBufferTimeout_);
|
1304 | }
|
1305 |
|
1306 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this), 1);
|
1307 | }
|
1308 |
|
1309 | |
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 | monitorBufferTick_() {
|
1316 | if (this.state === 'READY') {
|
1317 | this.fillBuffer_();
|
1318 | }
|
1319 |
|
1320 | if (this.checkBufferTimeout_) {
|
1321 | window.clearTimeout(this.checkBufferTimeout_);
|
1322 | }
|
1323 |
|
1324 | this.checkBufferTimeout_ = window.setTimeout(
|
1325 | this.monitorBufferTick_.bind(this),
|
1326 | CHECK_BUFFER_DELAY
|
1327 | );
|
1328 | }
|
1329 |
|
1330 | |
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 | fillBuffer_() {
|
1340 |
|
1341 |
|
1342 | if (this.sourceUpdater_.updating()) {
|
1343 | return;
|
1344 | }
|
1345 |
|
1346 |
|
1347 | const segmentInfo = this.chooseNextRequest_();
|
1348 |
|
1349 | if (!segmentInfo) {
|
1350 | return;
|
1351 | }
|
1352 |
|
1353 | if (typeof segmentInfo.timestampOffset === 'number') {
|
1354 | this.isPendingTimestampOffset_ = false;
|
1355 | this.timelineChangeController_.pendingTimelineChange({
|
1356 | type: this.loaderType_,
|
1357 | from: this.currentTimeline_,
|
1358 | to: segmentInfo.timeline
|
1359 | });
|
1360 | }
|
1361 |
|
1362 | this.loadSegment_(segmentInfo);
|
1363 | }
|
1364 |
|
1365 | |
1366 |
|
1367 |
|
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 | isEndOfStream_(mediaIndex = this.mediaIndex, playlist = this.playlist_, partIndex = this.partIndex) {
|
1375 | if (!playlist || !this.mediaSource_) {
|
1376 | return false;
|
1377 | }
|
1378 |
|
1379 | const segment = typeof mediaIndex === 'number' && playlist.segments[mediaIndex];
|
1380 |
|
1381 |
|
1382 | const appendedLastSegment = (mediaIndex + 1) === playlist.segments.length;
|
1383 |
|
1384 | const appendedLastPart = !segment || !segment.parts || (partIndex + 1) === segment.parts.length;
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 | return playlist.endList &&
|
1390 | this.mediaSource_.readyState === 'open' &&
|
1391 | appendedLastSegment &&
|
1392 | appendedLastPart;
|
1393 | }
|
1394 |
|
1395 | |
1396 |
|
1397 |
|
1398 |
|
1399 |
|
1400 | chooseNextRequest_() {
|
1401 | const buffered = this.buffered_();
|
1402 | const bufferedEnd = lastBufferedEnd(buffered) || 0;
|
1403 | const bufferedTime = timeAheadOf(buffered, this.currentTime_());
|
1404 | const preloaded = !this.hasPlayed_() && bufferedTime >= 1;
|
1405 | const haveEnoughBuffer = bufferedTime >= this.goalBufferLength_();
|
1406 | const segments = this.playlist_.segments;
|
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 | if (!segments.length || preloaded || haveEnoughBuffer) {
|
1413 | return null;
|
1414 | }
|
1415 |
|
1416 | this.syncPoint_ = this.syncPoint_ || this.syncController_.getSyncPoint(
|
1417 | this.playlist_,
|
1418 | this.duration_(),
|
1419 | this.currentTimeline_,
|
1420 | this.currentTime_()
|
1421 | );
|
1422 |
|
1423 | const next = {
|
1424 | partIndex: null,
|
1425 | mediaIndex: null,
|
1426 | startOfSegment: null,
|
1427 | playlist: this.playlist_,
|
1428 | isSyncRequest: Boolean(!this.syncPoint_)
|
1429 | };
|
1430 |
|
1431 | if (next.isSyncRequest) {
|
1432 | next.mediaIndex = getSyncSegmentCandidate(this.currentTimeline_, segments, bufferedEnd);
|
1433 | } else if (this.mediaIndex !== null) {
|
1434 | const segment = segments[this.mediaIndex];
|
1435 | const partIndex = typeof this.partIndex === 'number' ? this.partIndex : -1;
|
1436 |
|
1437 | next.startOfSegment = segment.end ? segment.end : bufferedEnd;
|
1438 |
|
1439 | if (segment.parts && segment.parts[partIndex + 1]) {
|
1440 | next.mediaIndex = this.mediaIndex;
|
1441 | next.partIndex = partIndex + 1;
|
1442 | } else {
|
1443 | next.mediaIndex = this.mediaIndex + 1;
|
1444 | }
|
1445 | } else {
|
1446 |
|
1447 | const {segmentIndex, startTime, partIndex} = Playlist.getMediaInfoForTime({
|
1448 | experimentalExactManifestTimings: this.experimentalExactManifestTimings,
|
1449 | playlist: this.playlist_,
|
1450 | currentTime: this.fetchAtBuffer_ ? bufferedEnd : this.currentTime_(),
|
1451 | startingPartIndex: this.syncPoint_.partIndex,
|
1452 | startingSegmentIndex: this.syncPoint_.segmentIndex,
|
1453 | startTime: this.syncPoint_.time
|
1454 | });
|
1455 |
|
1456 | next.getMediaInfoForTime = this.fetchAtBuffer_ ?
|
1457 | `bufferedEnd ${bufferedEnd}` : `currentTime ${this.currentTime_()}`;
|
1458 | next.mediaIndex = segmentIndex;
|
1459 | next.startOfSegment = startTime;
|
1460 | next.partIndex = partIndex;
|
1461 | }
|
1462 |
|
1463 | const nextSegment = segments[next.mediaIndex];
|
1464 | let nextPart = nextSegment &&
|
1465 | typeof next.partIndex === 'number' &&
|
1466 | nextSegment.parts &&
|
1467 | nextSegment.parts[next.partIndex];
|
1468 |
|
1469 |
|
1470 |
|
1471 | if (!nextSegment || (typeof next.partIndex === 'number' && !nextPart)) {
|
1472 | return null;
|
1473 | }
|
1474 |
|
1475 |
|
1476 |
|
1477 | if (typeof next.partIndex !== 'number' && nextSegment.parts) {
|
1478 | next.partIndex = 0;
|
1479 | nextPart = nextSegment.parts[0];
|
1480 | }
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 | if (!bufferedTime && nextPart && !nextPart.independent) {
|
1487 |
|
1488 | if (next.partIndex === 0) {
|
1489 | const lastSegment = segments[next.mediaIndex - 1];
|
1490 | const lastSegmentLastPart = lastSegment.parts && lastSegment.parts.length && lastSegment.parts[lastSegment.parts.length - 1];
|
1491 |
|
1492 | if (lastSegmentLastPart && lastSegmentLastPart.independent) {
|
1493 | next.mediaIndex -= 1;
|
1494 | next.partIndex = lastSegment.parts.length - 1;
|
1495 | next.independent = 'previous segment';
|
1496 | }
|
1497 | } else if (nextSegment.parts[next.partIndex - 1].independent) {
|
1498 | next.partIndex -= 1;
|
1499 | next.independent = 'previous part';
|
1500 | }
|
1501 | }
|
1502 |
|
1503 | const ended = this.mediaSource_ && this.mediaSource_.readyState === 'ended';
|
1504 |
|
1505 |
|
1506 |
|
1507 |
|
1508 |
|
1509 | if (next.mediaIndex >= (segments.length - 1) && ended && !this.seeking_()) {
|
1510 | return null;
|
1511 | }
|
1512 |
|
1513 | return this.generateSegmentInfo_(next);
|
1514 | }
|
1515 |
|
1516 | generateSegmentInfo_(options) {
|
1517 | const {
|
1518 | independent,
|
1519 | playlist,
|
1520 | mediaIndex,
|
1521 | startOfSegment,
|
1522 | isSyncRequest,
|
1523 | partIndex,
|
1524 | forceTimestampOffset,
|
1525 | getMediaInfoForTime
|
1526 | } = options;
|
1527 | const segment = playlist.segments[mediaIndex];
|
1528 | const part = typeof partIndex === 'number' && segment.parts[partIndex];
|
1529 | const segmentInfo = {
|
1530 | requestId: 'segment-loader-' + Math.random(),
|
1531 |
|
1532 | uri: part && part.resolvedUri || segment.resolvedUri,
|
1533 |
|
1534 | mediaIndex,
|
1535 | partIndex: part ? partIndex : null,
|
1536 |
|
1537 |
|
1538 | isSyncRequest,
|
1539 | startOfSegment,
|
1540 |
|
1541 | playlist,
|
1542 |
|
1543 | bytes: null,
|
1544 |
|
1545 | encryptedBytes: null,
|
1546 |
|
1547 |
|
1548 | timestampOffset: null,
|
1549 |
|
1550 | timeline: segment.timeline,
|
1551 |
|
1552 | duration: part && part.duration || segment.duration,
|
1553 |
|
1554 | segment,
|
1555 | part,
|
1556 | byteLength: 0,
|
1557 | transmuxer: this.transmuxer_,
|
1558 |
|
1559 | getMediaInfoForTime,
|
1560 | independent
|
1561 | };
|
1562 |
|
1563 | const overrideCheck =
|
1564 | typeof forceTimestampOffset !== 'undefined' ? forceTimestampOffset : this.isPendingTimestampOffset_;
|
1565 |
|
1566 | segmentInfo.timestampOffset = this.timestampOffsetForSegment_({
|
1567 | segmentTimeline: segment.timeline,
|
1568 | currentTimeline: this.currentTimeline_,
|
1569 | startOfSegment,
|
1570 | buffered: this.buffered_(),
|
1571 | overrideCheck
|
1572 | });
|
1573 |
|
1574 | const audioBufferedEnd = lastBufferedEnd(this.sourceUpdater_.audioBuffered());
|
1575 |
|
1576 | if (typeof audioBufferedEnd === 'number') {
|
1577 |
|
1578 |
|
1579 | segmentInfo.audioAppendStart = audioBufferedEnd - this.sourceUpdater_.audioTimestampOffset();
|
1580 | }
|
1581 |
|
1582 | if (this.sourceUpdater_.videoBuffered().length) {
|
1583 | segmentInfo.gopsToAlignWith = gopsSafeToAlignWith(
|
1584 | this.gopBuffer_,
|
1585 |
|
1586 |
|
1587 | this.currentTime_() - this.sourceUpdater_.videoTimestampOffset(),
|
1588 | this.timeMapping_
|
1589 | );
|
1590 | }
|
1591 |
|
1592 | return segmentInfo;
|
1593 | }
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 | timestampOffsetForSegment_(options) {
|
1599 | return timestampOffsetForSegment(options);
|
1600 | }
|
1601 | |
1602 |
|
1603 |
|
1604 |
|
1605 |
|
1606 |
|
1607 |
|
1608 |
|
1609 |
|
1610 | earlyAbortWhenNeeded_(stats) {
|
1611 | if (this.vhs_.tech_.paused() ||
|
1612 |
|
1613 |
|
1614 |
|
1615 | !this.xhrOptions_.timeout ||
|
1616 |
|
1617 | !(this.playlist_.attributes.BANDWIDTH)) {
|
1618 | return;
|
1619 | }
|
1620 |
|
1621 |
|
1622 |
|
1623 |
|
1624 | if (Date.now() - (stats.firstBytesReceivedAt || Date.now()) < 1000) {
|
1625 | return;
|
1626 | }
|
1627 |
|
1628 | const currentTime = this.currentTime_();
|
1629 | const measuredBandwidth = stats.bandwidth;
|
1630 | const segmentDuration = this.pendingSegment_.duration;
|
1631 |
|
1632 | const requestTimeRemaining =
|
1633 | Playlist.estimateSegmentRequestTime(
|
1634 | segmentDuration,
|
1635 | measuredBandwidth,
|
1636 | this.playlist_,
|
1637 | stats.bytesReceived
|
1638 | );
|
1639 |
|
1640 |
|
1641 |
|
1642 |
|
1643 | const timeUntilRebuffer = timeUntilRebuffer_(
|
1644 | this.buffered_(),
|
1645 | currentTime,
|
1646 | this.vhs_.tech_.playbackRate()
|
1647 | ) - 1;
|
1648 |
|
1649 |
|
1650 |
|
1651 | if (requestTimeRemaining <= timeUntilRebuffer) {
|
1652 | return;
|
1653 | }
|
1654 |
|
1655 | const switchCandidate = minRebufferMaxBandwidthSelector({
|
1656 | master: this.vhs_.playlists.master,
|
1657 | currentTime,
|
1658 | bandwidth: measuredBandwidth,
|
1659 | duration: this.duration_(),
|
1660 | segmentDuration,
|
1661 | timeUntilRebuffer,
|
1662 | currentTimeline: this.currentTimeline_,
|
1663 | syncController: this.syncController_
|
1664 | });
|
1665 |
|
1666 | if (!switchCandidate) {
|
1667 | return;
|
1668 | }
|
1669 |
|
1670 | const rebufferingImpact = requestTimeRemaining - timeUntilRebuffer;
|
1671 |
|
1672 | const timeSavedBySwitching = rebufferingImpact - switchCandidate.rebufferingImpact;
|
1673 |
|
1674 | let minimumTimeSaving = 0.5;
|
1675 |
|
1676 |
|
1677 |
|
1678 |
|
1679 | if (timeUntilRebuffer <= TIME_FUDGE_FACTOR) {
|
1680 | minimumTimeSaving = 1;
|
1681 | }
|
1682 |
|
1683 | if (!switchCandidate.playlist ||
|
1684 | switchCandidate.playlist.uri === this.playlist_.uri ||
|
1685 | timeSavedBySwitching < minimumTimeSaving) {
|
1686 | return;
|
1687 | }
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 | this.bandwidth =
|
1693 | switchCandidate.playlist.attributes.BANDWIDTH * Config.BANDWIDTH_VARIANCE + 1;
|
1694 | this.trigger('earlyabort');
|
1695 | }
|
1696 |
|
1697 | handleAbort_(segmentInfo) {
|
1698 | this.logger_(`Aborting ${segmentInfoString(segmentInfo)}`);
|
1699 | this.mediaRequestsAborted += 1;
|
1700 | }
|
1701 |
|
1702 | |
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 |
|
1709 |
|
1710 |
|
1711 | handleProgress_(event, simpleSegment) {
|
1712 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
1713 |
|
1714 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
1715 | return;
|
1716 | }
|
1717 |
|
1718 | this.trigger('progress');
|
1719 | }
|
1720 |
|
1721 | handleTrackInfo_(simpleSegment, trackInfo) {
|
1722 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
1723 |
|
1724 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
1725 | return;
|
1726 | }
|
1727 |
|
1728 | if (this.checkForIllegalMediaSwitch(trackInfo)) {
|
1729 | return;
|
1730 | }
|
1731 |
|
1732 | trackInfo = trackInfo || {};
|
1733 |
|
1734 |
|
1735 |
|
1736 |
|
1737 | if (!shallowEqual(this.currentMediaInfo_, trackInfo)) {
|
1738 | this.appendInitSegment_ = {
|
1739 | audio: true,
|
1740 | video: true
|
1741 | };
|
1742 |
|
1743 | this.startingMediaInfo_ = trackInfo;
|
1744 | this.currentMediaInfo_ = trackInfo;
|
1745 | this.logger_('trackinfo update', trackInfo);
|
1746 | this.trigger('trackinfo');
|
1747 | }
|
1748 |
|
1749 |
|
1750 |
|
1751 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
1752 | return;
|
1753 | }
|
1754 |
|
1755 |
|
1756 |
|
1757 | this.pendingSegment_.trackInfo = trackInfo;
|
1758 |
|
1759 |
|
1760 | if (this.hasEnoughInfoToAppend_()) {
|
1761 | this.processCallQueue_();
|
1762 | }
|
1763 | }
|
1764 |
|
1765 | handleTimingInfo_(simpleSegment, mediaType, timeType, time) {
|
1766 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
1767 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
1768 | return;
|
1769 | }
|
1770 |
|
1771 | const segmentInfo = this.pendingSegment_;
|
1772 | const timingInfoProperty = timingInfoPropertyForMedia(mediaType);
|
1773 |
|
1774 | segmentInfo[timingInfoProperty] = segmentInfo[timingInfoProperty] || {};
|
1775 | segmentInfo[timingInfoProperty][timeType] = time;
|
1776 |
|
1777 | this.logger_(`timinginfo: ${mediaType} - ${timeType} - ${time}`);
|
1778 |
|
1779 |
|
1780 | if (this.hasEnoughInfoToAppend_()) {
|
1781 | this.processCallQueue_();
|
1782 | }
|
1783 | }
|
1784 |
|
1785 | handleCaptions_(simpleSegment, captionData) {
|
1786 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
1787 |
|
1788 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
1789 | return;
|
1790 | }
|
1791 |
|
1792 |
|
1793 |
|
1794 | if (captionData.length === 0) {
|
1795 | this.logger_('SegmentLoader received no captions from a caption event');
|
1796 | return;
|
1797 | }
|
1798 |
|
1799 | const segmentInfo = this.pendingSegment_;
|
1800 |
|
1801 |
|
1802 |
|
1803 | if (!segmentInfo.hasAppendedData_) {
|
1804 | this.metadataQueue_.caption.push(this.handleCaptions_.bind(this, simpleSegment, captionData));
|
1805 | return;
|
1806 | }
|
1807 |
|
1808 | const timestampOffset = this.sourceUpdater_.videoTimestampOffset() === null ?
|
1809 | this.sourceUpdater_.audioTimestampOffset() :
|
1810 | this.sourceUpdater_.videoTimestampOffset();
|
1811 |
|
1812 | const captionTracks = {};
|
1813 |
|
1814 |
|
1815 | captionData.forEach((caption) => {
|
1816 |
|
1817 |
|
1818 | captionTracks[caption.stream] = captionTracks[caption.stream] || {
|
1819 |
|
1820 | startTime: Infinity,
|
1821 | captions: [],
|
1822 |
|
1823 | endTime: 0
|
1824 | };
|
1825 |
|
1826 | const captionTrack = captionTracks[caption.stream];
|
1827 |
|
1828 | captionTrack.startTime = Math.min(captionTrack.startTime, (caption.startTime + timestampOffset));
|
1829 | captionTrack.endTime = Math.max(captionTrack.endTime, (caption.endTime + timestampOffset));
|
1830 | captionTrack.captions.push(caption);
|
1831 | });
|
1832 |
|
1833 | Object.keys(captionTracks).forEach((trackName) => {
|
1834 | const {startTime, endTime, captions} = captionTracks[trackName];
|
1835 | const inbandTextTracks = this.inbandTextTracks_;
|
1836 |
|
1837 | this.logger_(`adding cues from ${startTime} -> ${endTime} for ${trackName}`);
|
1838 |
|
1839 | createCaptionsTrackIfNotExists(inbandTextTracks, this.vhs_.tech_, trackName);
|
1840 |
|
1841 |
|
1842 |
|
1843 |
|
1844 | removeCuesFromTrack(startTime, endTime, inbandTextTracks[trackName]);
|
1845 |
|
1846 | addCaptionData({captionArray: captions, inbandTextTracks, timestampOffset});
|
1847 | });
|
1848 |
|
1849 |
|
1850 |
|
1851 |
|
1852 | if (this.transmuxer_) {
|
1853 | this.transmuxer_.postMessage({
|
1854 | action: 'clearParsedMp4Captions'
|
1855 | });
|
1856 | }
|
1857 | }
|
1858 |
|
1859 | handleId3_(simpleSegment, id3Frames, dispatchType) {
|
1860 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
1861 |
|
1862 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
1863 | return;
|
1864 | }
|
1865 |
|
1866 | const segmentInfo = this.pendingSegment_;
|
1867 |
|
1868 |
|
1869 | if (!segmentInfo.hasAppendedData_) {
|
1870 | this.metadataQueue_.id3.push(this.handleId3_.bind(this, simpleSegment, id3Frames, dispatchType));
|
1871 | return;
|
1872 | }
|
1873 |
|
1874 | const timestampOffset = this.sourceUpdater_.videoTimestampOffset() === null ?
|
1875 | this.sourceUpdater_.audioTimestampOffset() :
|
1876 | this.sourceUpdater_.videoTimestampOffset();
|
1877 |
|
1878 |
|
1879 |
|
1880 |
|
1881 | createMetadataTrackIfNotExists(this.inbandTextTracks_, dispatchType, this.vhs_.tech_);
|
1882 | addMetadata({
|
1883 | inbandTextTracks: this.inbandTextTracks_,
|
1884 | metadataArray: id3Frames,
|
1885 | timestampOffset,
|
1886 | videoDuration: this.duration_()
|
1887 | });
|
1888 | }
|
1889 |
|
1890 | processMetadataQueue_() {
|
1891 | this.metadataQueue_.id3.forEach((fn) => fn());
|
1892 | this.metadataQueue_.caption.forEach((fn) => fn());
|
1893 |
|
1894 | this.metadataQueue_.id3 = [];
|
1895 | this.metadataQueue_.caption = [];
|
1896 | }
|
1897 |
|
1898 | processCallQueue_() {
|
1899 | const callQueue = this.callQueue_;
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 | this.callQueue_ = [];
|
1905 | callQueue.forEach((fun) => fun());
|
1906 | }
|
1907 |
|
1908 | processLoadQueue_() {
|
1909 | const loadQueue = this.loadQueue_;
|
1910 |
|
1911 |
|
1912 |
|
1913 |
|
1914 | this.loadQueue_ = [];
|
1915 | loadQueue.forEach((fun) => fun());
|
1916 | }
|
1917 |
|
1918 | |
1919 |
|
1920 |
|
1921 |
|
1922 |
|
1923 |
|
1924 | hasEnoughInfoToLoad_() {
|
1925 |
|
1926 |
|
1927 | if (this.loaderType_ !== 'audio') {
|
1928 | return true;
|
1929 | }
|
1930 |
|
1931 | const segmentInfo = this.pendingSegment_;
|
1932 |
|
1933 |
|
1934 |
|
1935 | if (!segmentInfo) {
|
1936 | return false;
|
1937 | }
|
1938 |
|
1939 |
|
1940 |
|
1941 |
|
1942 |
|
1943 | if (!this.getCurrentMediaInfo_()) {
|
1944 | return true;
|
1945 | }
|
1946 |
|
1947 | if (
|
1948 |
|
1949 |
|
1950 |
|
1951 |
|
1952 |
|
1953 |
|
1954 |
|
1955 |
|
1956 |
|
1957 |
|
1958 |
|
1959 |
|
1960 |
|
1961 |
|
1962 |
|
1963 | shouldWaitForTimelineChange({
|
1964 | timelineChangeController: this.timelineChangeController_,
|
1965 | currentTimeline: this.currentTimeline_,
|
1966 | segmentTimeline: segmentInfo.timeline,
|
1967 | loaderType: this.loaderType_,
|
1968 | audioDisabled: this.audioDisabled_
|
1969 | })
|
1970 | ) {
|
1971 | return false;
|
1972 | }
|
1973 |
|
1974 | return true;
|
1975 | }
|
1976 |
|
1977 | getCurrentMediaInfo_(segmentInfo = this.pendingSegment_) {
|
1978 | return segmentInfo && segmentInfo.trackInfo || this.currentMediaInfo_;
|
1979 | }
|
1980 |
|
1981 | getMediaInfo_(segmentInfo = this.pendingSegment_) {
|
1982 | return this.getCurrentMediaInfo_(segmentInfo) || this.startingMediaInfo_;
|
1983 | }
|
1984 |
|
1985 | hasEnoughInfoToAppend_() {
|
1986 | if (!this.sourceUpdater_.ready()) {
|
1987 | return false;
|
1988 | }
|
1989 |
|
1990 |
|
1991 |
|
1992 | if (this.waitingOnRemove_ || this.quotaExceededErrorRetryTimeout_) {
|
1993 | return false;
|
1994 | }
|
1995 |
|
1996 | const segmentInfo = this.pendingSegment_;
|
1997 | const trackInfo = this.getCurrentMediaInfo_();
|
1998 |
|
1999 |
|
2000 |
|
2001 |
|
2002 | if (!segmentInfo || !trackInfo) {
|
2003 | return false;
|
2004 | }
|
2005 |
|
2006 | const {hasAudio, hasVideo, isMuxed} = trackInfo;
|
2007 |
|
2008 | if (hasVideo && !segmentInfo.videoTimingInfo) {
|
2009 | return false;
|
2010 | }
|
2011 |
|
2012 |
|
2013 | if (hasAudio && !this.audioDisabled_ && !isMuxed && !segmentInfo.audioTimingInfo) {
|
2014 | return false;
|
2015 | }
|
2016 |
|
2017 | if (
|
2018 | shouldWaitForTimelineChange({
|
2019 | timelineChangeController: this.timelineChangeController_,
|
2020 | currentTimeline: this.currentTimeline_,
|
2021 | segmentTimeline: segmentInfo.timeline,
|
2022 | loaderType: this.loaderType_,
|
2023 | audioDisabled: this.audioDisabled_
|
2024 | })
|
2025 | ) {
|
2026 | return false;
|
2027 | }
|
2028 |
|
2029 | return true;
|
2030 | }
|
2031 |
|
2032 | handleData_(simpleSegment, result) {
|
2033 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
2034 |
|
2035 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
2036 | return;
|
2037 | }
|
2038 |
|
2039 |
|
2040 |
|
2041 | if (this.callQueue_.length || !this.hasEnoughInfoToAppend_()) {
|
2042 | this.callQueue_.push(this.handleData_.bind(this, simpleSegment, result));
|
2043 | return;
|
2044 | }
|
2045 |
|
2046 | const segmentInfo = this.pendingSegment_;
|
2047 |
|
2048 |
|
2049 | this.setTimeMapping_(segmentInfo.timeline);
|
2050 |
|
2051 |
|
2052 | this.updateMediaSecondsLoaded_(segmentInfo.part || segmentInfo.segment);
|
2053 |
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 | if (this.mediaSource_.readyState === 'closed') {
|
2061 | return;
|
2062 | }
|
2063 |
|
2064 |
|
2065 |
|
2066 | if (simpleSegment.map) {
|
2067 | simpleSegment.map = this.initSegmentForMap(simpleSegment.map, true);
|
2068 |
|
2069 | segmentInfo.segment.map = simpleSegment.map;
|
2070 | }
|
2071 |
|
2072 |
|
2073 | if (simpleSegment.key) {
|
2074 | this.segmentKey(simpleSegment.key, true);
|
2075 | }
|
2076 |
|
2077 | segmentInfo.isFmp4 = simpleSegment.isFmp4;
|
2078 | segmentInfo.timingInfo = segmentInfo.timingInfo || {};
|
2079 |
|
2080 | if (segmentInfo.isFmp4) {
|
2081 | this.trigger('fmp4');
|
2082 |
|
2083 | segmentInfo.timingInfo.start =
|
2084 | segmentInfo[timingInfoPropertyForMedia(result.type)].start;
|
2085 | } else {
|
2086 | const trackInfo = this.getCurrentMediaInfo_();
|
2087 | const useVideoTimingInfo =
|
2088 | this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
|
2089 | let firstVideoFrameTimeForData;
|
2090 |
|
2091 | if (useVideoTimingInfo) {
|
2092 | firstVideoFrameTimeForData = segmentInfo.videoTimingInfo.start;
|
2093 | }
|
2094 |
|
2095 |
|
2096 |
|
2097 |
|
2098 | segmentInfo.timingInfo.start = this.trueSegmentStart_({
|
2099 | currentStart: segmentInfo.timingInfo.start,
|
2100 | playlist: segmentInfo.playlist,
|
2101 | mediaIndex: segmentInfo.mediaIndex,
|
2102 | currentVideoTimestampOffset: this.sourceUpdater_.videoTimestampOffset(),
|
2103 | useVideoTimingInfo,
|
2104 | firstVideoFrameTimeForData,
|
2105 | videoTimingInfo: segmentInfo.videoTimingInfo,
|
2106 | audioTimingInfo: segmentInfo.audioTimingInfo
|
2107 | });
|
2108 | }
|
2109 |
|
2110 |
|
2111 |
|
2112 |
|
2113 | this.updateAppendInitSegmentStatus(segmentInfo, result.type);
|
2114 |
|
2115 |
|
2116 |
|
2117 | this.updateSourceBufferTimestampOffset_(segmentInfo);
|
2118 |
|
2119 |
|
2120 |
|
2121 | if (segmentInfo.isSyncRequest) {
|
2122 |
|
2123 |
|
2124 |
|
2125 | this.updateTimingInfoEnd_(segmentInfo);
|
2126 | this.syncController_.saveSegmentTimingInfo({
|
2127 | segmentInfo,
|
2128 | shouldSaveTimelineMapping: this.loaderType_ === 'main'
|
2129 | });
|
2130 |
|
2131 | const next = this.chooseNextRequest_();
|
2132 |
|
2133 |
|
2134 |
|
2135 | if (next.mediaIndex !== segmentInfo.mediaIndex || next.partIndex !== segmentInfo.partIndex) {
|
2136 | this.logger_('sync segment was incorrect, not appending');
|
2137 | return;
|
2138 | }
|
2139 |
|
2140 | this.logger_('sync segment was correct, appending');
|
2141 | }
|
2142 |
|
2143 |
|
2144 |
|
2145 |
|
2146 |
|
2147 | segmentInfo.hasAppendedData_ = true;
|
2148 |
|
2149 | this.processMetadataQueue_();
|
2150 |
|
2151 | this.appendData_(segmentInfo, result);
|
2152 | }
|
2153 |
|
2154 | updateAppendInitSegmentStatus(segmentInfo, type) {
|
2155 |
|
2156 | if (this.loaderType_ === 'main' &&
|
2157 | typeof segmentInfo.timestampOffset === 'number' &&
|
2158 |
|
2159 |
|
2160 | !segmentInfo.changedTimestampOffset) {
|
2161 |
|
2162 |
|
2163 | this.appendInitSegment_ = {
|
2164 | audio: true,
|
2165 | video: true
|
2166 | };
|
2167 | }
|
2168 |
|
2169 | if (this.playlistOfLastInitSegment_[type] !== segmentInfo.playlist) {
|
2170 |
|
2171 |
|
2172 | this.appendInitSegment_[type] = true;
|
2173 | }
|
2174 | }
|
2175 |
|
2176 | getInitSegmentAndUpdateState_({ type, initSegment, map, playlist }) {
|
2177 |
|
2178 |
|
2179 |
|
2180 |
|
2181 |
|
2182 | if (map) {
|
2183 | const id = initSegmentId(map);
|
2184 |
|
2185 | if (this.activeInitSegmentId_ === id) {
|
2186 |
|
2187 | return null;
|
2188 | }
|
2189 |
|
2190 |
|
2191 |
|
2192 |
|
2193 |
|
2194 | initSegment = this.initSegmentForMap(map, true).bytes;
|
2195 | this.activeInitSegmentId_ = id;
|
2196 | }
|
2197 |
|
2198 |
|
2199 |
|
2200 |
|
2201 |
|
2202 |
|
2203 | if (initSegment && this.appendInitSegment_[type]) {
|
2204 |
|
2205 |
|
2206 |
|
2207 | this.playlistOfLastInitSegment_[type] = playlist;
|
2208 |
|
2209 | this.appendInitSegment_[type] = false;
|
2210 |
|
2211 |
|
2212 |
|
2213 | this.activeInitSegmentId_ = null;
|
2214 |
|
2215 | return initSegment;
|
2216 | }
|
2217 |
|
2218 | return null;
|
2219 | }
|
2220 |
|
2221 | handleQuotaExceededError_({segmentInfo, type, bytes}, error) {
|
2222 | const audioBuffered = this.sourceUpdater_.audioBuffered();
|
2223 | const videoBuffered = this.sourceUpdater_.videoBuffered();
|
2224 |
|
2225 |
|
2226 |
|
2227 |
|
2228 | if (audioBuffered.length > 1) {
|
2229 | this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the audio buffer: ' +
|
2230 | timeRangesToArray(audioBuffered).join(', '));
|
2231 | }
|
2232 | if (videoBuffered.length > 1) {
|
2233 | this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the video buffer: ' +
|
2234 | timeRangesToArray(videoBuffered).join(', '));
|
2235 | }
|
2236 |
|
2237 | const audioBufferStart = audioBuffered.length ? audioBuffered.start(0) : 0;
|
2238 | const audioBufferEnd = audioBuffered.length ?
|
2239 | audioBuffered.end(audioBuffered.length - 1) : 0;
|
2240 | const videoBufferStart = videoBuffered.length ? videoBuffered.start(0) : 0;
|
2241 | const videoBufferEnd = videoBuffered.length ?
|
2242 | videoBuffered.end(videoBuffered.length - 1) : 0;
|
2243 |
|
2244 | if (
|
2245 | (audioBufferEnd - audioBufferStart) <= MIN_BACK_BUFFER &&
|
2246 | (videoBufferEnd - videoBufferStart) <= MIN_BACK_BUFFER
|
2247 | ) {
|
2248 |
|
2249 |
|
2250 |
|
2251 |
|
2252 | this.logger_('On QUOTA_EXCEEDED_ERR, single segment too large to append to ' +
|
2253 | 'buffer, triggering an error. ' +
|
2254 | `Appended byte length: ${bytes.byteLength}, ` +
|
2255 | `audio buffer: ${timeRangesToArray(audioBuffered).join(', ')}, ` +
|
2256 | `video buffer: ${timeRangesToArray(videoBuffered).join(', ')}, `);
|
2257 | this.error({
|
2258 | message: 'Quota exceeded error with append of a single segment of content',
|
2259 | excludeUntil: Infinity
|
2260 | });
|
2261 | this.trigger('error');
|
2262 | return;
|
2263 | }
|
2264 |
|
2265 |
|
2266 |
|
2267 |
|
2268 |
|
2269 |
|
2270 |
|
2271 |
|
2272 |
|
2273 |
|
2274 |
|
2275 |
|
2276 |
|
2277 | this.waitingOnRemove_ = true;
|
2278 | this.callQueue_.push(this.appendToSourceBuffer_.bind(this, {segmentInfo, type, bytes}));
|
2279 |
|
2280 | const currentTime = this.currentTime_();
|
2281 |
|
2282 |
|
2283 | const timeToRemoveUntil = currentTime - MIN_BACK_BUFFER;
|
2284 |
|
2285 | this.logger_(`On QUOTA_EXCEEDED_ERR, removing audio/video from 0 to ${timeToRemoveUntil}`);
|
2286 | this.remove(0, timeToRemoveUntil, () => {
|
2287 |
|
2288 | this.logger_(`On QUOTA_EXCEEDED_ERR, retrying append in ${MIN_BACK_BUFFER}s`);
|
2289 | this.waitingOnRemove_ = false;
|
2290 |
|
2291 |
|
2292 | this.quotaExceededErrorRetryTimeout_ = window.setTimeout(() => {
|
2293 | this.logger_('On QUOTA_EXCEEDED_ERR, re-processing call queue');
|
2294 | this.quotaExceededErrorRetryTimeout_ = null;
|
2295 | this.processCallQueue_();
|
2296 | }, MIN_BACK_BUFFER * 1000);
|
2297 | }, true);
|
2298 | }
|
2299 |
|
2300 | handleAppendError_({segmentInfo, type, bytes}, error) {
|
2301 |
|
2302 | if (!error) {
|
2303 | return;
|
2304 | }
|
2305 |
|
2306 | if (error.code === QUOTA_EXCEEDED_ERR) {
|
2307 | this.handleQuotaExceededError_({segmentInfo, type, bytes});
|
2308 |
|
2309 |
|
2310 | return;
|
2311 | }
|
2312 |
|
2313 | this.logger_('Received non QUOTA_EXCEEDED_ERR on append', error);
|
2314 | this.error(`${type} append of ${bytes.length}b failed for segment ` +
|
2315 | `#${segmentInfo.mediaIndex} in playlist ${segmentInfo.playlist.id}`);
|
2316 |
|
2317 |
|
2318 |
|
2319 |
|
2320 |
|
2321 |
|
2322 | this.trigger('appenderror');
|
2323 | }
|
2324 |
|
2325 | appendToSourceBuffer_({ segmentInfo, type, initSegment, data, bytes }) {
|
2326 |
|
2327 | if (!bytes) {
|
2328 | const segments = [data];
|
2329 | let byteLength = data.byteLength;
|
2330 |
|
2331 | if (initSegment) {
|
2332 |
|
2333 |
|
2334 | segments.unshift(initSegment);
|
2335 | byteLength += initSegment.byteLength;
|
2336 | }
|
2337 |
|
2338 |
|
2339 |
|
2340 | bytes = concatSegments({
|
2341 | bytes: byteLength,
|
2342 | segments
|
2343 | });
|
2344 | }
|
2345 |
|
2346 | this.sourceUpdater_.appendBuffer(
|
2347 | {segmentInfo, type, bytes},
|
2348 | this.handleAppendError_.bind(this, {segmentInfo, type, bytes})
|
2349 | );
|
2350 | }
|
2351 |
|
2352 | handleSegmentTimingInfo_(type, requestId, segmentTimingInfo) {
|
2353 | if (!this.pendingSegment_ || requestId !== this.pendingSegment_.requestId) {
|
2354 | return;
|
2355 | }
|
2356 |
|
2357 | const segment = this.pendingSegment_.segment;
|
2358 | const timingInfoProperty = `${type}TimingInfo`;
|
2359 |
|
2360 | if (!segment[timingInfoProperty]) {
|
2361 | segment[timingInfoProperty] = {};
|
2362 | }
|
2363 |
|
2364 | segment[timingInfoProperty].transmuxerPrependedSeconds =
|
2365 | segmentTimingInfo.prependedContentDuration || 0;
|
2366 | segment[timingInfoProperty].transmuxedPresentationStart =
|
2367 | segmentTimingInfo.start.presentation;
|
2368 | segment[timingInfoProperty].transmuxedDecodeStart =
|
2369 | segmentTimingInfo.start.decode;
|
2370 | segment[timingInfoProperty].transmuxedPresentationEnd =
|
2371 | segmentTimingInfo.end.presentation;
|
2372 | segment[timingInfoProperty].transmuxedDecodeEnd =
|
2373 | segmentTimingInfo.end.decode;
|
2374 |
|
2375 | segment[timingInfoProperty].baseMediaDecodeTime =
|
2376 | segmentTimingInfo.baseMediaDecodeTime;
|
2377 | }
|
2378 |
|
2379 | appendData_(segmentInfo, result) {
|
2380 | const {
|
2381 | type,
|
2382 | data
|
2383 | } = result;
|
2384 |
|
2385 | if (!data || !data.byteLength) {
|
2386 | return;
|
2387 | }
|
2388 |
|
2389 | if (type === 'audio' && this.audioDisabled_) {
|
2390 | return;
|
2391 | }
|
2392 |
|
2393 | const initSegment = this.getInitSegmentAndUpdateState_({
|
2394 | type,
|
2395 | initSegment: result.initSegment,
|
2396 | playlist: segmentInfo.playlist,
|
2397 | map: segmentInfo.isFmp4 ? segmentInfo.segment.map : null
|
2398 | });
|
2399 |
|
2400 | this.appendToSourceBuffer_({ segmentInfo, type, initSegment, data });
|
2401 | }
|
2402 |
|
2403 | |
2404 |
|
2405 |
|
2406 |
|
2407 |
|
2408 | loadSegment_(segmentInfo) {
|
2409 | this.state = 'WAITING';
|
2410 | this.pendingSegment_ = segmentInfo;
|
2411 | this.trimBackBuffer_(segmentInfo);
|
2412 |
|
2413 | if (typeof segmentInfo.timestampOffset === 'number') {
|
2414 | if (this.transmuxer_) {
|
2415 | this.transmuxer_.postMessage({
|
2416 | action: 'clearAllMp4Captions'
|
2417 | });
|
2418 | }
|
2419 | }
|
2420 |
|
2421 | if (!this.hasEnoughInfoToLoad_()) {
|
2422 | this.loadQueue_.push(() => {
|
2423 |
|
2424 |
|
2425 | const options = Object.assign(
|
2426 | {},
|
2427 | segmentInfo,
|
2428 | {forceTimestampOffset: true}
|
2429 | );
|
2430 |
|
2431 | Object.assign(segmentInfo, this.generateSegmentInfo_(options));
|
2432 | this.isPendingTimestampOffset_ = false;
|
2433 | this.updateTransmuxerAndRequestSegment_(segmentInfo);
|
2434 | });
|
2435 | return;
|
2436 | }
|
2437 |
|
2438 | this.updateTransmuxerAndRequestSegment_(segmentInfo);
|
2439 | }
|
2440 |
|
2441 | updateTransmuxerAndRequestSegment_(segmentInfo) {
|
2442 |
|
2443 |
|
2444 |
|
2445 |
|
2446 |
|
2447 | if (this.shouldUpdateTransmuxerTimestampOffset_(segmentInfo.timestampOffset)) {
|
2448 | this.gopBuffer_.length = 0;
|
2449 |
|
2450 | segmentInfo.gopsToAlignWith = [];
|
2451 | this.timeMapping_ = 0;
|
2452 |
|
2453 | this.transmuxer_.postMessage({
|
2454 | action: 'reset'
|
2455 | });
|
2456 | this.transmuxer_.postMessage({
|
2457 | action: 'setTimestampOffset',
|
2458 | timestampOffset: segmentInfo.timestampOffset
|
2459 | });
|
2460 | }
|
2461 |
|
2462 | const simpleSegment = this.createSimplifiedSegmentObj_(segmentInfo);
|
2463 | const isEndOfStream = this.isEndOfStream_(
|
2464 | segmentInfo.mediaIndex,
|
2465 | segmentInfo.playlist,
|
2466 | segmentInfo.partIndex
|
2467 | );
|
2468 | const isWalkingForward = this.mediaIndex !== null;
|
2469 | const isDiscontinuity = segmentInfo.timeline !== this.currentTimeline_ &&
|
2470 |
|
2471 |
|
2472 | segmentInfo.timeline > 0;
|
2473 | const isEndOfTimeline = isEndOfStream || (isWalkingForward && isDiscontinuity);
|
2474 |
|
2475 | this.logger_(`Requesting ${segmentInfoString(segmentInfo)}`);
|
2476 |
|
2477 |
|
2478 |
|
2479 |
|
2480 |
|
2481 |
|
2482 | if (simpleSegment.map && !simpleSegment.map.bytes) {
|
2483 | this.logger_('going to request init segment.');
|
2484 | this.appendInitSegment_ = {
|
2485 | video: true,
|
2486 | audio: true
|
2487 | };
|
2488 | }
|
2489 |
|
2490 | segmentInfo.abortRequests = mediaSegmentRequest({
|
2491 | xhr: this.vhs_.xhr,
|
2492 | xhrOptions: this.xhrOptions_,
|
2493 | decryptionWorker: this.decrypter_,
|
2494 | segment: simpleSegment,
|
2495 | abortFn: this.handleAbort_.bind(this, segmentInfo),
|
2496 | progressFn: this.handleProgress_.bind(this),
|
2497 | trackInfoFn: this.handleTrackInfo_.bind(this),
|
2498 | timingInfoFn: this.handleTimingInfo_.bind(this),
|
2499 | videoSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'video', segmentInfo.requestId),
|
2500 | audioSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'audio', segmentInfo.requestId),
|
2501 | captionsFn: this.handleCaptions_.bind(this),
|
2502 | isEndOfTimeline,
|
2503 | endedTimelineFn: () => {
|
2504 | this.logger_('received endedtimeline callback');
|
2505 | },
|
2506 | id3Fn: this.handleId3_.bind(this),
|
2507 |
|
2508 | dataFn: this.handleData_.bind(this),
|
2509 | doneFn: this.segmentRequestFinished_.bind(this),
|
2510 | onTransmuxerLog: ({message, level, stream}) => {
|
2511 | this.logger_(`${segmentInfoString(segmentInfo)} logged from transmuxer stream ${stream} as a ${level}: ${message}`);
|
2512 | }
|
2513 | });
|
2514 | }
|
2515 |
|
2516 | |
2517 |
|
2518 |
|
2519 |
|
2520 |
|
2521 |
|
2522 |
|
2523 |
|
2524 | trimBackBuffer_(segmentInfo) {
|
2525 | const removeToTime = safeBackBufferTrimTime(
|
2526 | this.seekable_(),
|
2527 | this.currentTime_(),
|
2528 | this.playlist_.targetDuration || 10
|
2529 | );
|
2530 |
|
2531 |
|
2532 |
|
2533 |
|
2534 |
|
2535 |
|
2536 |
|
2537 | if (removeToTime > 0) {
|
2538 | this.remove(0, removeToTime);
|
2539 | }
|
2540 | }
|
2541 |
|
2542 | |
2543 |
|
2544 |
|
2545 |
|
2546 |
|
2547 |
|
2548 |
|
2549 |
|
2550 |
|
2551 | createSimplifiedSegmentObj_(segmentInfo) {
|
2552 | const segment = segmentInfo.segment;
|
2553 | const part = segmentInfo.part;
|
2554 |
|
2555 | const simpleSegment = {
|
2556 | resolvedUri: part ? part.resolvedUri : segment.resolvedUri,
|
2557 | byterange: part ? part.byterange : segment.byterange,
|
2558 | requestId: segmentInfo.requestId,
|
2559 | transmuxer: segmentInfo.transmuxer,
|
2560 | audioAppendStart: segmentInfo.audioAppendStart,
|
2561 | gopsToAlignWith: segmentInfo.gopsToAlignWith,
|
2562 | part: segmentInfo.part
|
2563 | };
|
2564 |
|
2565 | const previousSegment = segmentInfo.playlist.segments[segmentInfo.mediaIndex - 1];
|
2566 |
|
2567 | if (previousSegment && previousSegment.timeline === segment.timeline) {
|
2568 |
|
2569 |
|
2570 |
|
2571 |
|
2572 |
|
2573 |
|
2574 |
|
2575 |
|
2576 | if (previousSegment.videoTimingInfo) {
|
2577 | simpleSegment.baseStartTime =
|
2578 | previousSegment.videoTimingInfo.transmuxedDecodeEnd;
|
2579 | } else if (previousSegment.audioTimingInfo) {
|
2580 | simpleSegment.baseStartTime =
|
2581 | previousSegment.audioTimingInfo.transmuxedDecodeEnd;
|
2582 | }
|
2583 | }
|
2584 |
|
2585 | if (segment.key) {
|
2586 |
|
2587 |
|
2588 | const iv = segment.key.iv || new Uint32Array([
|
2589 | 0, 0, 0, segmentInfo.mediaIndex + segmentInfo.playlist.mediaSequence
|
2590 | ]);
|
2591 |
|
2592 | simpleSegment.key = this.segmentKey(segment.key);
|
2593 | simpleSegment.key.iv = iv;
|
2594 | }
|
2595 |
|
2596 | if (segment.map) {
|
2597 | simpleSegment.map = this.initSegmentForMap(segment.map);
|
2598 | }
|
2599 |
|
2600 | return simpleSegment;
|
2601 | }
|
2602 |
|
2603 | saveTransferStats_(stats) {
|
2604 |
|
2605 |
|
2606 | this.mediaRequests += 1;
|
2607 |
|
2608 | if (stats) {
|
2609 | this.mediaBytesTransferred += stats.bytesReceived;
|
2610 | this.mediaTransferDuration += stats.roundTripTime;
|
2611 | }
|
2612 | }
|
2613 |
|
2614 | saveBandwidthRelatedStats_(duration, stats) {
|
2615 |
|
2616 |
|
2617 |
|
2618 | this.pendingSegment_.byteLength = stats.bytesReceived;
|
2619 |
|
2620 | if (duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
|
2621 | this.logger_(`Ignoring segment's bandwidth because its duration of ${duration}` +
|
2622 | ` is less than the min to record ${MIN_SEGMENT_DURATION_TO_SAVE_STATS}`);
|
2623 | return;
|
2624 | }
|
2625 |
|
2626 | this.bandwidth = stats.bandwidth;
|
2627 | this.roundTrip = stats.roundTripTime;
|
2628 | }
|
2629 |
|
2630 | handleTimeout_() {
|
2631 |
|
2632 |
|
2633 | this.mediaRequestsTimedout += 1;
|
2634 | this.bandwidth = 1;
|
2635 | this.roundTrip = NaN;
|
2636 | this.trigger('bandwidthupdate');
|
2637 | this.trigger('timeout');
|
2638 | }
|
2639 |
|
2640 | |
2641 |
|
2642 |
|
2643 |
|
2644 |
|
2645 |
|
2646 | segmentRequestFinished_(error, simpleSegment, result) {
|
2647 |
|
2648 |
|
2649 |
|
2650 |
|
2651 |
|
2652 | if (this.callQueue_.length) {
|
2653 | this.callQueue_.push(this.segmentRequestFinished_.bind(this, error, simpleSegment, result));
|
2654 | return;
|
2655 | }
|
2656 |
|
2657 | this.saveTransferStats_(simpleSegment.stats);
|
2658 |
|
2659 |
|
2660 | if (!this.pendingSegment_) {
|
2661 | return;
|
2662 | }
|
2663 |
|
2664 |
|
2665 |
|
2666 |
|
2667 |
|
2668 | if (simpleSegment.requestId !== this.pendingSegment_.requestId) {
|
2669 | return;
|
2670 | }
|
2671 |
|
2672 |
|
2673 | if (error) {
|
2674 | this.pendingSegment_ = null;
|
2675 | this.state = 'READY';
|
2676 |
|
2677 |
|
2678 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
2679 | return;
|
2680 | }
|
2681 |
|
2682 | this.pause();
|
2683 |
|
2684 |
|
2685 |
|
2686 |
|
2687 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
2688 | this.handleTimeout_();
|
2689 | return;
|
2690 | }
|
2691 |
|
2692 |
|
2693 |
|
2694 | this.mediaRequestsErrored += 1;
|
2695 | this.error(error);
|
2696 | this.trigger('error');
|
2697 | return;
|
2698 | }
|
2699 |
|
2700 | const segmentInfo = this.pendingSegment_;
|
2701 |
|
2702 |
|
2703 |
|
2704 | this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats);
|
2705 |
|
2706 | segmentInfo.endOfAllRequests = simpleSegment.endOfAllRequests;
|
2707 |
|
2708 | if (result.gopInfo) {
|
2709 | this.gopBuffer_ = updateGopBuffer(this.gopBuffer_, result.gopInfo, this.safeAppend_);
|
2710 | }
|
2711 |
|
2712 |
|
2713 |
|
2714 | this.state = 'APPENDING';
|
2715 |
|
2716 |
|
2717 | this.trigger('appending');
|
2718 |
|
2719 | this.waitForAppendsToComplete_(segmentInfo);
|
2720 | }
|
2721 |
|
2722 | setTimeMapping_(timeline) {
|
2723 | const timelineMapping = this.syncController_.mappingForTimeline(timeline);
|
2724 |
|
2725 | if (timelineMapping !== null) {
|
2726 | this.timeMapping_ = timelineMapping;
|
2727 | }
|
2728 | }
|
2729 |
|
2730 | updateMediaSecondsLoaded_(segment) {
|
2731 | if (typeof segment.start === 'number' && typeof segment.end === 'number') {
|
2732 | this.mediaSecondsLoaded += segment.end - segment.start;
|
2733 | } else {
|
2734 | this.mediaSecondsLoaded += segment.duration;
|
2735 | }
|
2736 | }
|
2737 |
|
2738 | shouldUpdateTransmuxerTimestampOffset_(timestampOffset) {
|
2739 | if (timestampOffset === null) {
|
2740 | return false;
|
2741 | }
|
2742 |
|
2743 |
|
2744 |
|
2745 |
|
2746 | if (this.loaderType_ === 'main' &&
|
2747 | timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
|
2748 | return true;
|
2749 | }
|
2750 |
|
2751 | if (!this.audioDisabled_ &&
|
2752 | timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
|
2753 | return true;
|
2754 | }
|
2755 |
|
2756 | return false;
|
2757 | }
|
2758 |
|
2759 | trueSegmentStart_({
|
2760 | currentStart,
|
2761 | playlist,
|
2762 | mediaIndex,
|
2763 | firstVideoFrameTimeForData,
|
2764 | currentVideoTimestampOffset,
|
2765 | useVideoTimingInfo,
|
2766 | videoTimingInfo,
|
2767 | audioTimingInfo
|
2768 | }) {
|
2769 | if (typeof currentStart !== 'undefined') {
|
2770 |
|
2771 | return currentStart;
|
2772 | }
|
2773 |
|
2774 | if (!useVideoTimingInfo) {
|
2775 | return audioTimingInfo.start;
|
2776 | }
|
2777 |
|
2778 | const previousSegment = playlist.segments[mediaIndex - 1];
|
2779 |
|
2780 |
|
2781 |
|
2782 |
|
2783 |
|
2784 |
|
2785 | if (mediaIndex === 0 ||
|
2786 | !previousSegment ||
|
2787 | typeof previousSegment.start === 'undefined' ||
|
2788 | previousSegment.end !==
|
2789 | (firstVideoFrameTimeForData + currentVideoTimestampOffset)) {
|
2790 | return firstVideoFrameTimeForData;
|
2791 | }
|
2792 |
|
2793 | return videoTimingInfo.start;
|
2794 | }
|
2795 |
|
2796 | waitForAppendsToComplete_(segmentInfo) {
|
2797 | const trackInfo = this.getCurrentMediaInfo_(segmentInfo);
|
2798 |
|
2799 | if (!trackInfo) {
|
2800 | this.error({
|
2801 | message: 'No starting media returned, likely due to an unsupported media format.',
|
2802 | blacklistDuration: Infinity
|
2803 | });
|
2804 | this.trigger('error');
|
2805 | return;
|
2806 | }
|
2807 |
|
2808 |
|
2809 |
|
2810 | const {hasAudio, hasVideo, isMuxed} = trackInfo;
|
2811 | const waitForVideo = this.loaderType_ === 'main' && hasVideo;
|
2812 | const waitForAudio = !this.audioDisabled_ && hasAudio && !isMuxed;
|
2813 |
|
2814 | segmentInfo.waitingOnAppends = 0;
|
2815 |
|
2816 |
|
2817 | if (!segmentInfo.hasAppendedData_) {
|
2818 | if (!segmentInfo.timingInfo && typeof segmentInfo.timestampOffset === 'number') {
|
2819 |
|
2820 |
|
2821 |
|
2822 |
|
2823 |
|
2824 |
|
2825 |
|
2826 |
|
2827 | this.isPendingTimestampOffset_ = true;
|
2828 | }
|
2829 |
|
2830 |
|
2831 | segmentInfo.timingInfo = {start: 0};
|
2832 | segmentInfo.waitingOnAppends++;
|
2833 |
|
2834 | if (!this.isPendingTimestampOffset_) {
|
2835 |
|
2836 | this.updateSourceBufferTimestampOffset_(segmentInfo);
|
2837 |
|
2838 |
|
2839 |
|
2840 | this.processMetadataQueue_();
|
2841 | }
|
2842 |
|
2843 |
|
2844 | this.checkAppendsDone_(segmentInfo);
|
2845 | return;
|
2846 | }
|
2847 |
|
2848 |
|
2849 | if (waitForVideo) {
|
2850 | segmentInfo.waitingOnAppends++;
|
2851 | }
|
2852 | if (waitForAudio) {
|
2853 | segmentInfo.waitingOnAppends++;
|
2854 | }
|
2855 |
|
2856 | if (waitForVideo) {
|
2857 | this.sourceUpdater_.videoQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
|
2858 | }
|
2859 | if (waitForAudio) {
|
2860 | this.sourceUpdater_.audioQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
|
2861 | }
|
2862 | }
|
2863 |
|
2864 | checkAppendsDone_(segmentInfo) {
|
2865 | if (this.checkForAbort_(segmentInfo.requestId)) {
|
2866 | return;
|
2867 | }
|
2868 |
|
2869 | segmentInfo.waitingOnAppends--;
|
2870 |
|
2871 | if (segmentInfo.waitingOnAppends === 0) {
|
2872 | this.handleAppendsDone_();
|
2873 | }
|
2874 | }
|
2875 |
|
2876 | checkForIllegalMediaSwitch(trackInfo) {
|
2877 | const illegalMediaSwitchError =
|
2878 | illegalMediaSwitch(this.loaderType_, this.getCurrentMediaInfo_(), trackInfo);
|
2879 |
|
2880 | if (illegalMediaSwitchError) {
|
2881 | this.error({
|
2882 | message: illegalMediaSwitchError,
|
2883 | blacklistDuration: Infinity
|
2884 | });
|
2885 | this.trigger('error');
|
2886 | return true;
|
2887 | }
|
2888 |
|
2889 | return false;
|
2890 | }
|
2891 |
|
2892 | updateSourceBufferTimestampOffset_(segmentInfo) {
|
2893 | if (segmentInfo.timestampOffset === null ||
|
2894 |
|
2895 |
|
2896 | typeof segmentInfo.timingInfo.start !== 'number' ||
|
2897 |
|
2898 | segmentInfo.changedTimestampOffset ||
|
2899 |
|
2900 | this.loaderType_ !== 'main') {
|
2901 | return;
|
2902 | }
|
2903 |
|
2904 | let didChange = false;
|
2905 |
|
2906 |
|
2907 |
|
2908 |
|
2909 |
|
2910 | segmentInfo.timestampOffset -= this.getSegmentStartTimeForTimestampOffsetCalculation_({
|
2911 | videoTimingInfo: segmentInfo.segment.videoTimingInfo,
|
2912 | audioTimingInfo: segmentInfo.segment.audioTimingInfo,
|
2913 | timingInfo: segmentInfo.timingInfo
|
2914 | });
|
2915 |
|
2916 |
|
2917 |
|
2918 | segmentInfo.changedTimestampOffset = true;
|
2919 |
|
2920 | if (segmentInfo.timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
|
2921 | this.sourceUpdater_.videoTimestampOffset(segmentInfo.timestampOffset);
|
2922 | didChange = true;
|
2923 | }
|
2924 |
|
2925 | if (segmentInfo.timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
|
2926 | this.sourceUpdater_.audioTimestampOffset(segmentInfo.timestampOffset);
|
2927 | didChange = true;
|
2928 | }
|
2929 |
|
2930 | if (didChange) {
|
2931 | this.trigger('timestampoffset');
|
2932 | }
|
2933 | }
|
2934 |
|
2935 | getSegmentStartTimeForTimestampOffsetCalculation_({ videoTimingInfo, audioTimingInfo, timingInfo }) {
|
2936 | if (!this.useDtsForTimestampOffset_) {
|
2937 | return timingInfo.start;
|
2938 | }
|
2939 |
|
2940 | if (videoTimingInfo && typeof videoTimingInfo.transmuxedDecodeStart === 'number') {
|
2941 | return videoTimingInfo.transmuxedDecodeStart;
|
2942 | }
|
2943 |
|
2944 |
|
2945 | if (audioTimingInfo && typeof audioTimingInfo.transmuxedDecodeStart === 'number') {
|
2946 | return audioTimingInfo.transmuxedDecodeStart;
|
2947 | }
|
2948 |
|
2949 |
|
2950 | return timingInfo.start;
|
2951 | }
|
2952 |
|
2953 | updateTimingInfoEnd_(segmentInfo) {
|
2954 | segmentInfo.timingInfo = segmentInfo.timingInfo || {};
|
2955 | const trackInfo = this.getMediaInfo_();
|
2956 | const useVideoTimingInfo =
|
2957 | this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
|
2958 | const prioritizedTimingInfo = useVideoTimingInfo && segmentInfo.videoTimingInfo ?
|
2959 | segmentInfo.videoTimingInfo : segmentInfo.audioTimingInfo;
|
2960 |
|
2961 | if (!prioritizedTimingInfo) {
|
2962 | return;
|
2963 | }
|
2964 | segmentInfo.timingInfo.end = typeof prioritizedTimingInfo.end === 'number' ?
|
2965 |
|
2966 |
|
2967 |
|
2968 | prioritizedTimingInfo.end : prioritizedTimingInfo.start + segmentInfo.duration;
|
2969 | }
|
2970 |
|
2971 | |
2972 |
|
2973 |
|
2974 |
|
2975 |
|
2976 |
|
2977 |
|
2978 | handleAppendsDone_() {
|
2979 |
|
2980 | if (this.pendingSegment_) {
|
2981 | this.trigger('appendsdone');
|
2982 | }
|
2983 |
|
2984 | if (!this.pendingSegment_) {
|
2985 | this.state = 'READY';
|
2986 |
|
2987 |
|
2988 | if (!this.paused()) {
|
2989 | this.monitorBuffer_();
|
2990 | }
|
2991 | return;
|
2992 | }
|
2993 |
|
2994 | const segmentInfo = this.pendingSegment_;
|
2995 |
|
2996 |
|
2997 |
|
2998 |
|
2999 | this.updateTimingInfoEnd_(segmentInfo);
|
3000 | if (this.shouldSaveSegmentTimingInfo_) {
|
3001 |
|
3002 |
|
3003 |
|
3004 |
|
3005 |
|
3006 |
|
3007 |
|
3008 |
|
3009 |
|
3010 |
|
3011 |
|
3012 |
|
3013 |
|
3014 |
|
3015 |
|
3016 |
|
3017 |
|
3018 | this.syncController_.saveSegmentTimingInfo({
|
3019 | segmentInfo,
|
3020 | shouldSaveTimelineMapping: this.loaderType_ === 'main'
|
3021 | });
|
3022 | }
|
3023 |
|
3024 | const segmentDurationMessage =
|
3025 | getTroublesomeSegmentDurationMessage(segmentInfo, this.sourceType_);
|
3026 |
|
3027 | if (segmentDurationMessage) {
|
3028 | if (segmentDurationMessage.severity === 'warn') {
|
3029 | videojs.log.warn(segmentDurationMessage.message);
|
3030 | } else {
|
3031 | this.logger_(segmentDurationMessage.message);
|
3032 | }
|
3033 | }
|
3034 |
|
3035 | this.recordThroughput_(segmentInfo);
|
3036 | this.pendingSegment_ = null;
|
3037 | this.state = 'READY';
|
3038 |
|
3039 | if (segmentInfo.isSyncRequest) {
|
3040 | this.trigger('syncinfoupdate');
|
3041 |
|
3042 |
|
3043 |
|
3044 |
|
3045 | if (!segmentInfo.hasAppendedData_) {
|
3046 | this.logger_(`Throwing away un-appended sync request ${segmentInfoString(segmentInfo)}`);
|
3047 | return;
|
3048 | }
|
3049 | }
|
3050 |
|
3051 | this.logger_(`Appended ${segmentInfoString(segmentInfo)}`);
|
3052 |
|
3053 | this.addSegmentMetadataCue_(segmentInfo);
|
3054 | this.fetchAtBuffer_ = true;
|
3055 | if (this.currentTimeline_ !== segmentInfo.timeline) {
|
3056 | this.timelineChangeController_.lastTimelineChange({
|
3057 | type: this.loaderType_,
|
3058 | from: this.currentTimeline_,
|
3059 | to: segmentInfo.timeline
|
3060 | });
|
3061 |
|
3062 |
|
3063 |
|
3064 | if (this.loaderType_ === 'main' && !this.audioDisabled_) {
|
3065 | this.timelineChangeController_.lastTimelineChange({
|
3066 | type: 'audio',
|
3067 | from: this.currentTimeline_,
|
3068 | to: segmentInfo.timeline
|
3069 | });
|
3070 | }
|
3071 | }
|
3072 | this.currentTimeline_ = segmentInfo.timeline;
|
3073 |
|
3074 |
|
3075 |
|
3076 |
|
3077 |
|
3078 | this.trigger('syncinfoupdate');
|
3079 | const segment = segmentInfo.segment;
|
3080 | const part = segmentInfo.part;
|
3081 | const badSegmentGuess = segment.end &&
|
3082 | this.currentTime_() - segment.end > segmentInfo.playlist.targetDuration * 3;
|
3083 | const badPartGuess = part &&
|
3084 | part.end && this.currentTime_() - part.end > segmentInfo.playlist.partTargetDuration * 3;
|
3085 |
|
3086 |
|
3087 |
|
3088 |
|
3089 |
|
3090 | if (badSegmentGuess || badPartGuess) {
|
3091 | this.logger_(`bad ${badSegmentGuess ? 'segment' : 'part'} ${segmentInfoString(segmentInfo)}`);
|
3092 | this.resetEverything();
|
3093 | return;
|
3094 | }
|
3095 |
|
3096 | const isWalkingForward = this.mediaIndex !== null;
|
3097 |
|
3098 |
|
3099 |
|
3100 | if (isWalkingForward) {
|
3101 | this.trigger('bandwidthupdate');
|
3102 | }
|
3103 | this.trigger('progress');
|
3104 |
|
3105 | this.mediaIndex = segmentInfo.mediaIndex;
|
3106 | this.partIndex = segmentInfo.partIndex;
|
3107 |
|
3108 |
|
3109 |
|
3110 |
|
3111 | if (this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex)) {
|
3112 | this.endOfStream();
|
3113 | }
|
3114 |
|
3115 |
|
3116 | this.trigger('appended');
|
3117 |
|
3118 | if (segmentInfo.hasAppendedData_) {
|
3119 | this.mediaAppends++;
|
3120 | }
|
3121 |
|
3122 | if (!this.paused()) {
|
3123 | this.monitorBuffer_();
|
3124 | }
|
3125 | }
|
3126 |
|
3127 | |
3128 |
|
3129 |
|
3130 |
|
3131 |
|
3132 |
|
3133 |
|
3134 |
|
3135 |
|
3136 | recordThroughput_(segmentInfo) {
|
3137 | if (segmentInfo.duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
|
3138 | this.logger_(`Ignoring segment's throughput because its duration of ${segmentInfo.duration}` +
|
3139 | ` is less than the min to record ${MIN_SEGMENT_DURATION_TO_SAVE_STATS}`);
|
3140 | return;
|
3141 | }
|
3142 |
|
3143 | const rate = this.throughput.rate;
|
3144 |
|
3145 |
|
3146 | const segmentProcessingTime =
|
3147 | Date.now() - segmentInfo.endOfAllRequests + 1;
|
3148 |
|
3149 | const segmentProcessingThroughput =
|
3150 | Math.floor((segmentInfo.byteLength / segmentProcessingTime) * 8 * 1000);
|
3151 |
|
3152 |
|
3153 |
|
3154 | this.throughput.rate +=
|
3155 | (segmentProcessingThroughput - rate) / (++this.throughput.count);
|
3156 | }
|
3157 |
|
3158 | |
3159 |
|
3160 |
|
3161 |
|
3162 |
|
3163 |
|
3164 |
|
3165 |
|
3166 |
|
3167 | addSegmentMetadataCue_(segmentInfo) {
|
3168 | if (!this.segmentMetadataTrack_) {
|
3169 | return;
|
3170 | }
|
3171 |
|
3172 | const segment = segmentInfo.segment;
|
3173 | const start = segment.start;
|
3174 | const end = segment.end;
|
3175 |
|
3176 |
|
3177 | if (!finite(start) || !finite(end)) {
|
3178 | return;
|
3179 | }
|
3180 |
|
3181 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
3182 |
|
3183 | const Cue = window.WebKitDataCue || window.VTTCue;
|
3184 | const value = {
|
3185 | custom: segment.custom,
|
3186 | dateTimeObject: segment.dateTimeObject,
|
3187 | dateTimeString: segment.dateTimeString,
|
3188 | bandwidth: segmentInfo.playlist.attributes.BANDWIDTH,
|
3189 | resolution: segmentInfo.playlist.attributes.RESOLUTION,
|
3190 | codecs: segmentInfo.playlist.attributes.CODECS,
|
3191 | byteLength: segmentInfo.byteLength,
|
3192 | uri: segmentInfo.uri,
|
3193 | timeline: segmentInfo.timeline,
|
3194 | playlist: segmentInfo.playlist.id,
|
3195 | start,
|
3196 | end
|
3197 | };
|
3198 | const data = JSON.stringify(value);
|
3199 | const cue = new Cue(start, end, data);
|
3200 |
|
3201 |
|
3202 |
|
3203 | cue.value = value;
|
3204 |
|
3205 | this.segmentMetadataTrack_.addCue(cue);
|
3206 | }
|
3207 | }
|