1 |
|
2 | import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
|
3 | import _inheritsLoose from '@babel/runtime/helpers/inheritsLoose';
|
4 | import document from 'global/document';
|
5 | import window from 'global/window';
|
6 | import _resolveUrl from '@videojs/vhs-utils/es/resolve-url.js';
|
7 | import videojs from 'video.js';
|
8 | import _extends from '@babel/runtime/helpers/extends';
|
9 | import { Parser } from 'm3u8-parser';
|
10 | import { isAudioCodec, translateLegacyCodec, codecsFromDefault, parseCodecs, getMimeForCodec, DEFAULT_VIDEO_CODEC, DEFAULT_AUDIO_CODEC, browserSupportsCodec, muxerSupportsCodec } from '@videojs/vhs-utils/es/codecs.js';
|
11 | import { simpleTypeFromSourceType } from '@videojs/vhs-utils/es/media-types.js';
|
12 | export { simpleTypeFromSourceType } from '@videojs/vhs-utils/es/media-types.js';
|
13 | import { generateSidxKey, parseUTCTiming, parse, addSidxSegmentsToPlaylist } from 'mpd-parser';
|
14 | import parseSidx from 'mux.js/lib/tools/parse-sidx';
|
15 | import { getId3Offset } from '@videojs/vhs-utils/es/id3-helpers';
|
16 | import { detectContainerForBytes, isLikelyFmp4MediaSegment } from '@videojs/vhs-utils/es/containers';
|
17 | import { concatTypedArrays, stringToBytes, toUint8 } from '@videojs/vhs-utils/es/byte-helpers';
|
18 | import { ONE_SECOND_IN_TS } from 'mux.js/lib/utils/clock';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var resolveUrl = _resolveUrl;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var resolveManifestRedirect = function resolveManifestRedirect(handleManifestRedirect, url, req) {
|
37 |
|
38 |
|
39 |
|
40 | if (handleManifestRedirect && req && req.responseURL && url !== req.responseURL) {
|
41 | return req.responseURL;
|
42 | }
|
43 |
|
44 | return url;
|
45 | };
|
46 |
|
47 | var logger = function logger(source) {
|
48 | if (videojs.log.debug) {
|
49 | return videojs.log.debug.bind(videojs, 'VHS:', source + " >");
|
50 | }
|
51 |
|
52 | return function () {};
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | var TIME_FUDGE_FACTOR = 1 / 30;
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | var SAFE_TIME_DELTA = TIME_FUDGE_FACTOR * 3;
|
69 |
|
70 | var filterRanges = function filterRanges(timeRanges, predicate) {
|
71 | var results = [];
|
72 | var i;
|
73 |
|
74 | if (timeRanges && timeRanges.length) {
|
75 |
|
76 | for (i = 0; i < timeRanges.length; i++) {
|
77 | if (predicate(timeRanges.start(i), timeRanges.end(i))) {
|
78 | results.push([timeRanges.start(i), timeRanges.end(i)]);
|
79 | }
|
80 | }
|
81 | }
|
82 |
|
83 | return videojs.createTimeRanges(results);
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | var findRange = function findRange(buffered, time) {
|
96 | return filterRanges(buffered, function (start, end) {
|
97 | return start - SAFE_TIME_DELTA <= time && end + SAFE_TIME_DELTA >= time;
|
98 | });
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | var findNextRange = function findNextRange(timeRanges, time) {
|
109 | return filterRanges(timeRanges, function (start) {
|
110 | return start - TIME_FUDGE_FACTOR >= time;
|
111 | });
|
112 | };
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | var findGaps = function findGaps(buffered) {
|
121 | if (buffered.length < 2) {
|
122 | return videojs.createTimeRanges();
|
123 | }
|
124 |
|
125 | var ranges = [];
|
126 |
|
127 | for (var i = 1; i < buffered.length; i++) {
|
128 | var start = buffered.end(i - 1);
|
129 | var end = buffered.start(i);
|
130 | ranges.push([start, end]);
|
131 | }
|
132 |
|
133 | return videojs.createTimeRanges(ranges);
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | var bufferIntersection = function bufferIntersection(bufferA, bufferB) {
|
144 | var start = null;
|
145 | var end = null;
|
146 | var arity = 0;
|
147 | var extents = [];
|
148 | var ranges = [];
|
149 |
|
150 | if (!bufferA || !bufferA.length || !bufferB || !bufferB.length) {
|
151 | return videojs.createTimeRange();
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 | var count = bufferA.length;
|
157 |
|
158 | while (count--) {
|
159 | extents.push({
|
160 | time: bufferA.start(count),
|
161 | type: 'start'
|
162 | });
|
163 | extents.push({
|
164 | time: bufferA.end(count),
|
165 | type: 'end'
|
166 | });
|
167 | }
|
168 |
|
169 | count = bufferB.length;
|
170 |
|
171 | while (count--) {
|
172 | extents.push({
|
173 | time: bufferB.start(count),
|
174 | type: 'start'
|
175 | });
|
176 | extents.push({
|
177 | time: bufferB.end(count),
|
178 | type: 'end'
|
179 | });
|
180 | }
|
181 |
|
182 |
|
183 | extents.sort(function (a, b) {
|
184 | return a.time - b.time;
|
185 | });
|
186 |
|
187 |
|
188 | for (count = 0; count < extents.length; count++) {
|
189 | if (extents[count].type === 'start') {
|
190 | arity++;
|
191 |
|
192 |
|
193 | if (arity === 2) {
|
194 | start = extents[count].time;
|
195 | }
|
196 | } else if (extents[count].type === 'end') {
|
197 | arity--;
|
198 |
|
199 |
|
200 | if (arity === 1) {
|
201 | end = extents[count].time;
|
202 | }
|
203 | }
|
204 |
|
205 |
|
206 | if (start !== null && end !== null) {
|
207 | ranges.push([start, end]);
|
208 | start = null;
|
209 | end = null;
|
210 | }
|
211 | }
|
212 |
|
213 | return videojs.createTimeRanges(ranges);
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | var printableRange = function printableRange(range) {
|
223 | var strArr = [];
|
224 |
|
225 | if (!range || !range.length) {
|
226 | return '';
|
227 | }
|
228 |
|
229 | for (var i = 0; i < range.length; i++) {
|
230 | strArr.push(range.start(i) + ' => ' + range.end(i));
|
231 | }
|
232 |
|
233 | return strArr.join(', ');
|
234 | };
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | var timeUntilRebuffer = function timeUntilRebuffer(buffered, currentTime, playbackRate) {
|
251 | if (playbackRate === void 0) {
|
252 | playbackRate = 1;
|
253 | }
|
254 |
|
255 | var bufferedEnd = buffered.length ? buffered.end(buffered.length - 1) : 0;
|
256 | return (bufferedEnd - currentTime) / playbackRate;
|
257 | };
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | var timeRangesToArray = function timeRangesToArray(timeRanges) {
|
266 | var timeRangesList = [];
|
267 |
|
268 | for (var i = 0; i < timeRanges.length; i++) {
|
269 | timeRangesList.push({
|
270 | start: timeRanges.start(i),
|
271 | end: timeRanges.end(i)
|
272 | });
|
273 | }
|
274 |
|
275 | return timeRangesList;
|
276 | };
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | var isRangeDifferent = function isRangeDifferent(a, b) {
|
291 |
|
292 | if (a === b) {
|
293 | return false;
|
294 | }
|
295 |
|
296 |
|
297 | if (!a && b || !b && a) {
|
298 | return true;
|
299 | }
|
300 |
|
301 |
|
302 | if (a.length !== b.length) {
|
303 | return true;
|
304 | }
|
305 |
|
306 |
|
307 | for (var i = 0; i < a.length; i++) {
|
308 | if (a.start(i) !== b.start(i) || a.end(i) !== b.end(i)) {
|
309 | return true;
|
310 | }
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 | return false;
|
316 | };
|
317 | var lastBufferedEnd = function lastBufferedEnd(a) {
|
318 | if (!a || !a.length || !a.end) {
|
319 | return;
|
320 | }
|
321 |
|
322 | return a.end(a.length - 1);
|
323 | };
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | var timeAheadOf = function timeAheadOf(range, startTime) {
|
340 | var time = 0;
|
341 |
|
342 | if (!range || !range.length) {
|
343 | return time;
|
344 | }
|
345 |
|
346 | for (var i = 0; i < range.length; i++) {
|
347 | var start = range.start(i);
|
348 | var end = range.end(i);
|
349 |
|
350 | if (startTime > end) {
|
351 | continue;
|
352 | }
|
353 |
|
354 |
|
355 | if (startTime > start && startTime <= end) {
|
356 | time += end - startTime;
|
357 | continue;
|
358 | }
|
359 |
|
360 |
|
361 | time += end - start;
|
362 | }
|
363 |
|
364 | return time;
|
365 | };
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | var createTimeRange = videojs.createTimeRange;
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 | var getPartsAndSegments = function getPartsAndSegments(playlist) {
|
383 | return (playlist.segments || []).reduce(function (acc, segment, si) {
|
384 | if (segment.parts) {
|
385 | segment.parts.forEach(function (part, pi) {
|
386 | acc.push({
|
387 | duration: part.duration,
|
388 | segmentIndex: si,
|
389 | partIndex: pi,
|
390 | part: part,
|
391 | segment: segment
|
392 | });
|
393 | });
|
394 | } else {
|
395 | acc.push({
|
396 | duration: segment.duration,
|
397 | segmentIndex: si,
|
398 | partIndex: null,
|
399 | segment: segment,
|
400 | part: null
|
401 | });
|
402 | }
|
403 |
|
404 | return acc;
|
405 | }, []);
|
406 | };
|
407 | var getLastParts = function getLastParts(media) {
|
408 | var lastSegment = media.segments && media.segments.length && media.segments[media.segments.length - 1];
|
409 | return lastSegment && lastSegment.parts || [];
|
410 | };
|
411 | var getKnownPartCount = function getKnownPartCount(_ref) {
|
412 | var preloadSegment = _ref.preloadSegment;
|
413 |
|
414 | if (!preloadSegment) {
|
415 | return;
|
416 | }
|
417 |
|
418 | var parts = preloadSegment.parts,
|
419 | preloadHints = preloadSegment.preloadHints;
|
420 | var partCount = (preloadHints || []).reduce(function (count, hint) {
|
421 | return count + (hint.type === 'PART' ? 1 : 0);
|
422 | }, 0);
|
423 | partCount += parts && parts.length ? parts.length : 0;
|
424 | return partCount;
|
425 | };
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 | var liveEdgeDelay = function liveEdgeDelay(master, media) {
|
436 | if (media.endList) {
|
437 | return 0;
|
438 | }
|
439 |
|
440 |
|
441 | if (master && master.suggestedPresentationDelay) {
|
442 | return master.suggestedPresentationDelay;
|
443 | }
|
444 |
|
445 | var hasParts = getLastParts(media).length > 0;
|
446 |
|
447 | if (hasParts && media.serverControl && media.serverControl.partHoldBack) {
|
448 | return media.serverControl.partHoldBack;
|
449 | } else if (hasParts && media.partTargetDuration) {
|
450 | return media.partTargetDuration * 3;
|
451 | } else if (media.serverControl && media.serverControl.holdBack) {
|
452 | return media.serverControl.holdBack;
|
453 | } else if (media.targetDuration) {
|
454 | return media.targetDuration * 3;
|
455 | }
|
456 |
|
457 | return 0;
|
458 | };
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 | var backwardDuration = function backwardDuration(playlist, endSequence) {
|
468 | var result = 0;
|
469 | var i = endSequence - playlist.mediaSequence;
|
470 |
|
471 |
|
472 | var segment = playlist.segments[i];
|
473 |
|
474 |
|
475 | if (segment) {
|
476 | if (typeof segment.start !== 'undefined') {
|
477 | return {
|
478 | result: segment.start,
|
479 | precise: true
|
480 | };
|
481 | }
|
482 |
|
483 | if (typeof segment.end !== 'undefined') {
|
484 | return {
|
485 | result: segment.end - segment.duration,
|
486 | precise: true
|
487 | };
|
488 | }
|
489 | }
|
490 |
|
491 | while (i--) {
|
492 | segment = playlist.segments[i];
|
493 |
|
494 | if (typeof segment.end !== 'undefined') {
|
495 | return {
|
496 | result: result + segment.end,
|
497 | precise: true
|
498 | };
|
499 | }
|
500 |
|
501 | result += segment.duration;
|
502 |
|
503 | if (typeof segment.start !== 'undefined') {
|
504 | return {
|
505 | result: result + segment.start,
|
506 | precise: true
|
507 | };
|
508 | }
|
509 | }
|
510 |
|
511 | return {
|
512 | result: result,
|
513 | precise: false
|
514 | };
|
515 | };
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 | var forwardDuration = function forwardDuration(playlist, endSequence) {
|
526 | var result = 0;
|
527 | var segment;
|
528 | var i = endSequence - playlist.mediaSequence;
|
529 |
|
530 |
|
531 | for (; i < playlist.segments.length; i++) {
|
532 | segment = playlist.segments[i];
|
533 |
|
534 | if (typeof segment.start !== 'undefined') {
|
535 | return {
|
536 | result: segment.start - result,
|
537 | precise: true
|
538 | };
|
539 | }
|
540 |
|
541 | result += segment.duration;
|
542 |
|
543 | if (typeof segment.end !== 'undefined') {
|
544 | return {
|
545 | result: segment.end - result,
|
546 | precise: true
|
547 | };
|
548 | }
|
549 | }
|
550 |
|
551 |
|
552 | return {
|
553 | result: -1,
|
554 | precise: false
|
555 | };
|
556 | };
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | var intervalDuration = function intervalDuration(playlist, endSequence, expired) {
|
573 | if (typeof endSequence === 'undefined') {
|
574 | endSequence = playlist.mediaSequence + playlist.segments.length;
|
575 | }
|
576 |
|
577 | if (endSequence < playlist.mediaSequence) {
|
578 | return 0;
|
579 | }
|
580 |
|
581 |
|
582 | var backward = backwardDuration(playlist, endSequence);
|
583 |
|
584 | if (backward.precise) {
|
585 |
|
586 |
|
587 |
|
588 | return backward.result;
|
589 | }
|
590 |
|
591 |
|
592 |
|
593 | var forward = forwardDuration(playlist, endSequence);
|
594 |
|
595 | if (forward.precise) {
|
596 |
|
597 |
|
598 | return forward.result;
|
599 | }
|
600 |
|
601 |
|
602 | return backward.result + expired;
|
603 | };
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 | var duration = function duration(playlist, endSequence, expired) {
|
622 | if (!playlist) {
|
623 | return 0;
|
624 | }
|
625 |
|
626 | if (typeof expired !== 'number') {
|
627 | expired = 0;
|
628 | }
|
629 |
|
630 |
|
631 |
|
632 | if (typeof endSequence === 'undefined') {
|
633 |
|
634 | if (playlist.totalDuration) {
|
635 | return playlist.totalDuration;
|
636 | }
|
637 |
|
638 |
|
639 | if (!playlist.endList) {
|
640 | return window.Infinity;
|
641 | }
|
642 | }
|
643 |
|
644 |
|
645 | return intervalDuration(playlist, endSequence, expired);
|
646 | };
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 | var sumDurations = function sumDurations(_ref2) {
|
661 | var defaultDuration = _ref2.defaultDuration,
|
662 | durationList = _ref2.durationList,
|
663 | startIndex = _ref2.startIndex,
|
664 | endIndex = _ref2.endIndex;
|
665 | var durations = 0;
|
666 |
|
667 | if (startIndex > endIndex) {
|
668 | var _ref3 = [endIndex, startIndex];
|
669 | startIndex = _ref3[0];
|
670 | endIndex = _ref3[1];
|
671 | }
|
672 |
|
673 | if (startIndex < 0) {
|
674 | for (var i = startIndex; i < Math.min(0, endIndex); i++) {
|
675 | durations += defaultDuration;
|
676 | }
|
677 |
|
678 | startIndex = 0;
|
679 | }
|
680 |
|
681 | for (var _i = startIndex; _i < endIndex; _i++) {
|
682 | durations += durationList[_i].duration;
|
683 | }
|
684 |
|
685 | return durations;
|
686 | };
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 |
|
707 | var playlistEnd = function playlistEnd(playlist, expired, useSafeLiveEnd, liveEdgePadding) {
|
708 | if (!playlist || !playlist.segments) {
|
709 | return null;
|
710 | }
|
711 |
|
712 | if (playlist.endList) {
|
713 | return duration(playlist);
|
714 | }
|
715 |
|
716 | if (expired === null) {
|
717 | return null;
|
718 | }
|
719 |
|
720 | expired = expired || 0;
|
721 | var lastSegmentTime = intervalDuration(playlist, playlist.mediaSequence + playlist.segments.length, expired);
|
722 |
|
723 | if (useSafeLiveEnd) {
|
724 | liveEdgePadding = typeof liveEdgePadding === 'number' ? liveEdgePadding : liveEdgeDelay(null, playlist);
|
725 | lastSegmentTime -= liveEdgePadding;
|
726 | }
|
727 |
|
728 |
|
729 | return Math.max(0, lastSegmentTime);
|
730 | };
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 | var seekable = function seekable(playlist, expired, liveEdgePadding) {
|
750 | var useSafeLiveEnd = true;
|
751 | var seekableStart = expired || 0;
|
752 | var seekableEnd = playlistEnd(playlist, expired, useSafeLiveEnd, liveEdgePadding);
|
753 |
|
754 | if (seekableEnd === null) {
|
755 | return createTimeRange();
|
756 | }
|
757 |
|
758 | return createTimeRange(seekableStart, seekableEnd);
|
759 | };
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 | var getMediaInfoForTime = function getMediaInfoForTime(_ref4) {
|
775 | var playlist = _ref4.playlist,
|
776 | currentTime = _ref4.currentTime,
|
777 | startingSegmentIndex = _ref4.startingSegmentIndex,
|
778 | startingPartIndex = _ref4.startingPartIndex,
|
779 | startTime = _ref4.startTime,
|
780 | experimentalExactManifestTimings = _ref4.experimentalExactManifestTimings;
|
781 | var time = currentTime - startTime;
|
782 | var partsAndSegments = getPartsAndSegments(playlist);
|
783 | var startIndex = 0;
|
784 |
|
785 | for (var i = 0; i < partsAndSegments.length; i++) {
|
786 | var partAndSegment = partsAndSegments[i];
|
787 |
|
788 | if (startingSegmentIndex !== partAndSegment.segmentIndex) {
|
789 | continue;
|
790 | }
|
791 |
|
792 |
|
793 | if (typeof startingPartIndex === 'number' && typeof partAndSegment.partIndex === 'number' && startingPartIndex !== partAndSegment.partIndex) {
|
794 | continue;
|
795 | }
|
796 |
|
797 | startIndex = i;
|
798 | break;
|
799 | }
|
800 |
|
801 | if (time < 0) {
|
802 |
|
803 |
|
804 | if (startIndex > 0) {
|
805 | for (var _i2 = startIndex - 1; _i2 >= 0; _i2--) {
|
806 | var _partAndSegment = partsAndSegments[_i2];
|
807 | time += _partAndSegment.duration;
|
808 |
|
809 | if (experimentalExactManifestTimings) {
|
810 | if (time < 0) {
|
811 | continue;
|
812 | }
|
813 | } else if (time + TIME_FUDGE_FACTOR <= 0) {
|
814 | continue;
|
815 | }
|
816 |
|
817 | return {
|
818 | partIndex: _partAndSegment.partIndex,
|
819 | segmentIndex: _partAndSegment.segmentIndex,
|
820 | startTime: startTime - sumDurations({
|
821 | defaultDuration: playlist.targetDuration,
|
822 | durationList: partsAndSegments,
|
823 | startIndex: startIndex,
|
824 | endIndex: _i2
|
825 | })
|
826 | };
|
827 | }
|
828 | }
|
829 |
|
830 |
|
831 |
|
832 | return {
|
833 | partIndex: partsAndSegments[0] && partsAndSegments[0].partIndex || null,
|
834 | segmentIndex: partsAndSegments[0] && partsAndSegments[0].segmentIndex || 0,
|
835 | startTime: currentTime
|
836 | };
|
837 | }
|
838 |
|
839 |
|
840 |
|
841 |
|
842 | if (startIndex < 0) {
|
843 | for (var _i3 = startIndex; _i3 < 0; _i3++) {
|
844 | time -= playlist.targetDuration;
|
845 |
|
846 | if (time < 0) {
|
847 | return {
|
848 | partIndex: partsAndSegments[0] && partsAndSegments[0].partIndex || null,
|
849 | segmentIndex: partsAndSegments[0] && partsAndSegments[0].segmentIndex || 0,
|
850 | startTime: currentTime
|
851 | };
|
852 | }
|
853 | }
|
854 |
|
855 | startIndex = 0;
|
856 | }
|
857 |
|
858 |
|
859 |
|
860 | for (var _i4 = startIndex; _i4 < partsAndSegments.length; _i4++) {
|
861 | var _partAndSegment2 = partsAndSegments[_i4];
|
862 | time -= _partAndSegment2.duration;
|
863 |
|
864 | if (experimentalExactManifestTimings) {
|
865 | if (time > 0) {
|
866 | continue;
|
867 | }
|
868 | } else if (time - TIME_FUDGE_FACTOR >= 0) {
|
869 | continue;
|
870 | }
|
871 |
|
872 | return {
|
873 | partIndex: _partAndSegment2.partIndex,
|
874 | segmentIndex: _partAndSegment2.segmentIndex,
|
875 | startTime: startTime + sumDurations({
|
876 | defaultDuration: playlist.targetDuration,
|
877 | durationList: partsAndSegments,
|
878 | startIndex: startIndex,
|
879 | endIndex: _i4
|
880 | })
|
881 | };
|
882 | }
|
883 |
|
884 |
|
885 | return {
|
886 | segmentIndex: partsAndSegments[partsAndSegments.length - 1].segmentIndex,
|
887 | partIndex: partsAndSegments[partsAndSegments.length - 1].partIndex,
|
888 | startTime: currentTime
|
889 | };
|
890 | };
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 | var isBlacklisted = function isBlacklisted(playlist) {
|
900 | return playlist.excludeUntil && playlist.excludeUntil > Date.now();
|
901 | };
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 | var isIncompatible = function isIncompatible(playlist) {
|
912 | return playlist.excludeUntil && playlist.excludeUntil === Infinity;
|
913 | };
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 | var isEnabled = function isEnabled(playlist) {
|
923 | var blacklisted = isBlacklisted(playlist);
|
924 | return !playlist.disabled && !blacklisted;
|
925 | };
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 |
|
933 |
|
934 | var isDisabled = function isDisabled(playlist) {
|
935 | return playlist.disabled;
|
936 | };
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 | var isAes = function isAes(media) {
|
944 | for (var i = 0; i < media.segments.length; i++) {
|
945 | if (media.segments[i].key) {
|
946 | return true;
|
947 | }
|
948 | }
|
949 |
|
950 | return false;
|
951 | };
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 | var hasAttribute = function hasAttribute(attr, playlist) {
|
965 | return playlist.attributes && playlist.attributes[attr];
|
966 | };
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 | var estimateSegmentRequestTime = function estimateSegmentRequestTime(segmentDuration, bandwidth, playlist, bytesReceived) {
|
985 | if (bytesReceived === void 0) {
|
986 | bytesReceived = 0;
|
987 | }
|
988 |
|
989 | if (!hasAttribute('BANDWIDTH', playlist)) {
|
990 | return NaN;
|
991 | }
|
992 |
|
993 | var size = segmentDuration * playlist.attributes.BANDWIDTH;
|
994 | return (size - bytesReceived * 8) / bandwidth;
|
995 | };
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 | var isLowestEnabledRendition = function isLowestEnabledRendition(master, media) {
|
1003 | if (master.playlists.length === 1) {
|
1004 | return true;
|
1005 | }
|
1006 |
|
1007 | var currentBandwidth = media.attributes.BANDWIDTH || Number.MAX_VALUE;
|
1008 | return master.playlists.filter(function (playlist) {
|
1009 | if (!isEnabled(playlist)) {
|
1010 | return false;
|
1011 | }
|
1012 |
|
1013 | return (playlist.attributes.BANDWIDTH || 0) < currentBandwidth;
|
1014 | }).length === 0;
|
1015 | };
|
1016 | var playlistMatch = function playlistMatch(a, b) {
|
1017 |
|
1018 |
|
1019 |
|
1020 | if (!a && !b || !a && b || a && !b) {
|
1021 | return false;
|
1022 | }
|
1023 |
|
1024 |
|
1025 | if (a === b) {
|
1026 | return true;
|
1027 | }
|
1028 |
|
1029 |
|
1030 |
|
1031 | if (a.id && b.id && a.id === b.id) {
|
1032 | return true;
|
1033 | }
|
1034 |
|
1035 |
|
1036 |
|
1037 | if (a.resolvedUri && b.resolvedUri && a.resolvedUri === b.resolvedUri) {
|
1038 | return true;
|
1039 | }
|
1040 |
|
1041 |
|
1042 |
|
1043 | if (a.uri && b.uri && a.uri === b.uri) {
|
1044 | return true;
|
1045 | }
|
1046 |
|
1047 | return false;
|
1048 | };
|
1049 |
|
1050 | var someAudioVariant = function someAudioVariant(master, callback) {
|
1051 | var AUDIO = master && master.mediaGroups && master.mediaGroups.AUDIO || {};
|
1052 | var found = false;
|
1053 |
|
1054 | for (var groupName in AUDIO) {
|
1055 | for (var label in AUDIO[groupName]) {
|
1056 | found = callback(AUDIO[groupName][label]);
|
1057 |
|
1058 | if (found) {
|
1059 | break;
|
1060 | }
|
1061 | }
|
1062 |
|
1063 | if (found) {
|
1064 | break;
|
1065 | }
|
1066 | }
|
1067 |
|
1068 | return !!found;
|
1069 | };
|
1070 |
|
1071 | var isAudioOnly = function isAudioOnly(master) {
|
1072 |
|
1073 |
|
1074 | if (!master || !master.playlists || !master.playlists.length) {
|
1075 |
|
1076 |
|
1077 | var found = someAudioVariant(master, function (variant) {
|
1078 | return variant.playlists && variant.playlists.length || variant.uri;
|
1079 | });
|
1080 | return found;
|
1081 | }
|
1082 |
|
1083 |
|
1084 | var _loop = function _loop(i) {
|
1085 | var playlist = master.playlists[i];
|
1086 | var CODECS = playlist.attributes && playlist.attributes.CODECS;
|
1087 |
|
1088 | if (CODECS && CODECS.split(',').every(function (c) {
|
1089 | return isAudioCodec(c);
|
1090 | })) {
|
1091 | return "continue";
|
1092 | }
|
1093 |
|
1094 |
|
1095 | var found = someAudioVariant(master, function (variant) {
|
1096 | return playlistMatch(playlist, variant);
|
1097 | });
|
1098 |
|
1099 | if (found) {
|
1100 | return "continue";
|
1101 | }
|
1102 |
|
1103 |
|
1104 |
|
1105 | return {
|
1106 | v: false
|
1107 | };
|
1108 | };
|
1109 |
|
1110 | for (var i = 0; i < master.playlists.length; i++) {
|
1111 | var _ret = _loop(i);
|
1112 |
|
1113 | if (_ret === "continue") continue;
|
1114 | if (typeof _ret === "object") return _ret.v;
|
1115 | }
|
1116 |
|
1117 |
|
1118 |
|
1119 | return true;
|
1120 | };
|
1121 |
|
1122 | var Playlist = {
|
1123 | liveEdgeDelay: liveEdgeDelay,
|
1124 | duration: duration,
|
1125 | seekable: seekable,
|
1126 | getMediaInfoForTime: getMediaInfoForTime,
|
1127 | isEnabled: isEnabled,
|
1128 | isDisabled: isDisabled,
|
1129 | isBlacklisted: isBlacklisted,
|
1130 | isIncompatible: isIncompatible,
|
1131 | playlistEnd: playlistEnd,
|
1132 | isAes: isAes,
|
1133 | hasAttribute: hasAttribute,
|
1134 | estimateSegmentRequestTime: estimateSegmentRequestTime,
|
1135 | isLowestEnabledRendition: isLowestEnabledRendition,
|
1136 | isAudioOnly: isAudioOnly,
|
1137 | playlistMatch: playlistMatch
|
1138 | };
|
1139 |
|
1140 | var log = videojs.log;
|
1141 | var createPlaylistID = function createPlaylistID(index, uri) {
|
1142 | return index + "-" + uri;
|
1143 | };
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 | var parseManifest = function parseManifest(_ref) {
|
1164 | var onwarn = _ref.onwarn,
|
1165 | oninfo = _ref.oninfo,
|
1166 | manifestString = _ref.manifestString,
|
1167 | _ref$customTagParsers = _ref.customTagParsers,
|
1168 | customTagParsers = _ref$customTagParsers === void 0 ? [] : _ref$customTagParsers,
|
1169 | _ref$customTagMappers = _ref.customTagMappers,
|
1170 | customTagMappers = _ref$customTagMappers === void 0 ? [] : _ref$customTagMappers,
|
1171 | experimentalLLHLS = _ref.experimentalLLHLS;
|
1172 | var parser = new Parser();
|
1173 |
|
1174 | if (onwarn) {
|
1175 | parser.on('warn', onwarn);
|
1176 | }
|
1177 |
|
1178 | if (oninfo) {
|
1179 | parser.on('info', oninfo);
|
1180 | }
|
1181 |
|
1182 | customTagParsers.forEach(function (customParser) {
|
1183 | return parser.addParser(customParser);
|
1184 | });
|
1185 | customTagMappers.forEach(function (mapper) {
|
1186 | return parser.addTagMapper(mapper);
|
1187 | });
|
1188 | parser.push(manifestString);
|
1189 | parser.end();
|
1190 | var manifest = parser.manifest;
|
1191 |
|
1192 |
|
1193 | if (!experimentalLLHLS) {
|
1194 | ['preloadSegment', 'skip', 'serverControl', 'renditionReports', 'partInf', 'partTargetDuration'].forEach(function (k) {
|
1195 | if (manifest.hasOwnProperty(k)) {
|
1196 | delete manifest[k];
|
1197 | }
|
1198 | });
|
1199 |
|
1200 | if (manifest.segments) {
|
1201 | manifest.segments.forEach(function (segment) {
|
1202 | ['parts', 'preloadHints'].forEach(function (k) {
|
1203 | if (segment.hasOwnProperty(k)) {
|
1204 | delete segment[k];
|
1205 | }
|
1206 | });
|
1207 | });
|
1208 | }
|
1209 | }
|
1210 |
|
1211 | if (!manifest.targetDuration) {
|
1212 | var targetDuration = 10;
|
1213 |
|
1214 | if (manifest.segments && manifest.segments.length) {
|
1215 | targetDuration = manifest.segments.reduce(function (acc, s) {
|
1216 | return Math.max(acc, s.duration);
|
1217 | }, 0);
|
1218 | }
|
1219 |
|
1220 | if (onwarn) {
|
1221 | onwarn("manifest has no targetDuration defaulting to " + targetDuration);
|
1222 | }
|
1223 |
|
1224 | manifest.targetDuration = targetDuration;
|
1225 | }
|
1226 |
|
1227 | var parts = getLastParts(manifest);
|
1228 |
|
1229 | if (parts.length && !manifest.partTargetDuration) {
|
1230 | var partTargetDuration = parts.reduce(function (acc, p) {
|
1231 | return Math.max(acc, p.duration);
|
1232 | }, 0);
|
1233 |
|
1234 | if (onwarn) {
|
1235 | onwarn("manifest has no partTargetDuration defaulting to " + partTargetDuration);
|
1236 | log.error('LL-HLS manifest has parts but lacks required #EXT-X-PART-INF:PART-TARGET value. See https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis-09#section-4.4.3.7. Playback is not guaranteed.');
|
1237 | }
|
1238 |
|
1239 | manifest.partTargetDuration = partTargetDuration;
|
1240 | }
|
1241 |
|
1242 | return manifest;
|
1243 | };
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 |
|
1251 |
|
1252 |
|
1253 |
|
1254 | var forEachMediaGroup = function forEachMediaGroup(master, callback) {
|
1255 | if (!master.mediaGroups) {
|
1256 | return;
|
1257 | }
|
1258 |
|
1259 | ['AUDIO', 'SUBTITLES'].forEach(function (mediaType) {
|
1260 | if (!master.mediaGroups[mediaType]) {
|
1261 | return;
|
1262 | }
|
1263 |
|
1264 | for (var groupKey in master.mediaGroups[mediaType]) {
|
1265 | for (var labelKey in master.mediaGroups[mediaType][groupKey]) {
|
1266 | var mediaProperties = master.mediaGroups[mediaType][groupKey][labelKey];
|
1267 | callback(mediaProperties, mediaType, groupKey, labelKey);
|
1268 | }
|
1269 | }
|
1270 | });
|
1271 | };
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 | var setupMediaPlaylist = function setupMediaPlaylist(_ref2) {
|
1288 | var playlist = _ref2.playlist,
|
1289 | uri = _ref2.uri,
|
1290 | id = _ref2.id;
|
1291 | playlist.id = id;
|
1292 | playlist.playlistErrors_ = 0;
|
1293 |
|
1294 | if (uri) {
|
1295 |
|
1296 |
|
1297 |
|
1298 | playlist.uri = uri;
|
1299 | }
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 | playlist.attributes = playlist.attributes || {};
|
1309 | };
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 | var setupMediaPlaylists = function setupMediaPlaylists(master) {
|
1320 | var i = master.playlists.length;
|
1321 |
|
1322 | while (i--) {
|
1323 | var playlist = master.playlists[i];
|
1324 | setupMediaPlaylist({
|
1325 | playlist: playlist,
|
1326 | id: createPlaylistID(i, playlist.uri)
|
1327 | });
|
1328 | playlist.resolvedUri = resolveUrl(master.uri, playlist.uri);
|
1329 | master.playlists[playlist.id] = playlist;
|
1330 |
|
1331 | master.playlists[playlist.uri] = playlist;
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 | if (!playlist.attributes.BANDWIDTH) {
|
1337 | log.warn('Invalid playlist STREAM-INF detected. Missing BANDWIDTH attribute.');
|
1338 | }
|
1339 | }
|
1340 | };
|
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 | var resolveMediaGroupUris = function resolveMediaGroupUris(master) {
|
1349 | forEachMediaGroup(master, function (properties) {
|
1350 | if (properties.uri) {
|
1351 | properties.resolvedUri = resolveUrl(master.uri, properties.uri);
|
1352 | }
|
1353 | });
|
1354 | };
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 | var masterForMedia = function masterForMedia(media, uri) {
|
1368 | var id = createPlaylistID(0, uri);
|
1369 | var master = {
|
1370 | mediaGroups: {
|
1371 | 'AUDIO': {},
|
1372 | 'VIDEO': {},
|
1373 | 'CLOSED-CAPTIONS': {},
|
1374 | 'SUBTITLES': {}
|
1375 | },
|
1376 | uri: window.location.href,
|
1377 | resolvedUri: window.location.href,
|
1378 | playlists: [{
|
1379 | uri: uri,
|
1380 | id: id,
|
1381 | resolvedUri: uri,
|
1382 |
|
1383 |
|
1384 | attributes: {}
|
1385 | }]
|
1386 | };
|
1387 |
|
1388 | master.playlists[id] = master.playlists[0];
|
1389 |
|
1390 | master.playlists[uri] = master.playlists[0];
|
1391 | return master;
|
1392 | };
|
1393 |
|
1394 |
|
1395 |
|
1396 |
|
1397 |
|
1398 |
|
1399 |
|
1400 |
|
1401 |
|
1402 |
|
1403 | var addPropertiesToMaster = function addPropertiesToMaster(master, uri) {
|
1404 | master.uri = uri;
|
1405 |
|
1406 | for (var i = 0; i < master.playlists.length; i++) {
|
1407 | if (!master.playlists[i].uri) {
|
1408 |
|
1409 |
|
1410 |
|
1411 | var phonyUri = "placeholder-uri-" + i;
|
1412 | master.playlists[i].uri = phonyUri;
|
1413 | }
|
1414 | }
|
1415 |
|
1416 | var audioOnlyMaster = isAudioOnly(master);
|
1417 | forEachMediaGroup(master, function (properties, mediaType, groupKey, labelKey) {
|
1418 | var groupId = "placeholder-uri-" + mediaType + "-" + groupKey + "-" + labelKey;
|
1419 |
|
1420 | if (!properties.playlists || !properties.playlists.length) {
|
1421 |
|
1422 |
|
1423 |
|
1424 | if (audioOnlyMaster && mediaType === 'AUDIO' && !properties.uri) {
|
1425 | for (var _i = 0; _i < master.playlists.length; _i++) {
|
1426 | var p = master.playlists[_i];
|
1427 |
|
1428 | if (p.attributes && p.attributes.AUDIO && p.attributes.AUDIO === groupKey) {
|
1429 | return;
|
1430 | }
|
1431 | }
|
1432 | }
|
1433 |
|
1434 | properties.playlists = [_extends({}, properties)];
|
1435 | }
|
1436 |
|
1437 | properties.playlists.forEach(function (p, i) {
|
1438 | var id = createPlaylistID(i, groupId);
|
1439 |
|
1440 | if (p.uri) {
|
1441 | p.resolvedUri = p.resolvedUri || resolveUrl(master.uri, p.uri);
|
1442 | } else {
|
1443 |
|
1444 |
|
1445 |
|
1446 |
|
1447 | p.uri = i === 0 ? groupId : id;
|
1448 |
|
1449 |
|
1450 | p.resolvedUri = p.uri;
|
1451 | }
|
1452 |
|
1453 | p.id = p.id || id;
|
1454 |
|
1455 |
|
1456 | p.attributes = p.attributes || {};
|
1457 |
|
1458 | master.playlists[p.id] = p;
|
1459 | master.playlists[p.uri] = p;
|
1460 | });
|
1461 | });
|
1462 | setupMediaPlaylists(master);
|
1463 | resolveMediaGroupUris(master);
|
1464 | };
|
1465 |
|
1466 | var mergeOptions$2 = videojs.mergeOptions,
|
1467 | EventTarget$1 = videojs.EventTarget;
|
1468 |
|
1469 | var addLLHLSQueryDirectives = function addLLHLSQueryDirectives(uri, media) {
|
1470 | if (media.endList || !media.serverControl) {
|
1471 | return uri;
|
1472 | }
|
1473 |
|
1474 | var parameters = {};
|
1475 |
|
1476 | if (media.serverControl.canBlockReload) {
|
1477 | var preloadSegment = media.preloadSegment;
|
1478 |
|
1479 | var nextMSN = media.mediaSequence + media.segments.length;
|
1480 |
|
1481 |
|
1482 |
|
1483 | if (preloadSegment) {
|
1484 | var parts = preloadSegment.parts || [];
|
1485 |
|
1486 | var nextPart = getKnownPartCount(media) - 1;
|
1487 |
|
1488 |
|
1489 |
|
1490 | if (nextPart > -1 && nextPart !== parts.length - 1) {
|
1491 |
|
1492 |
|
1493 | parameters._HLS_part = nextPart;
|
1494 | }
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 | if (nextPart > -1 || parts.length) {
|
1506 | nextMSN--;
|
1507 | }
|
1508 | }
|
1509 |
|
1510 |
|
1511 |
|
1512 | parameters._HLS_msn = nextMSN;
|
1513 | }
|
1514 |
|
1515 | if (media.serverControl && media.serverControl.canSkipUntil) {
|
1516 |
|
1517 |
|
1518 | parameters._HLS_skip = media.serverControl.canSkipDateranges ? 'v2' : 'YES';
|
1519 | }
|
1520 |
|
1521 | if (Object.keys(parameters).length) {
|
1522 | var parsedUri = new window.URL(uri);
|
1523 | ['_HLS_skip', '_HLS_msn', '_HLS_part'].forEach(function (name) {
|
1524 | if (!parameters.hasOwnProperty(name)) {
|
1525 | return;
|
1526 | }
|
1527 |
|
1528 | parsedUri.searchParams.set(name, parameters[name]);
|
1529 | });
|
1530 | uri = parsedUri.toString();
|
1531 | }
|
1532 |
|
1533 | return uri;
|
1534 | };
|
1535 |
|
1536 |
|
1537 |
|
1538 |
|
1539 |
|
1540 |
|
1541 |
|
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 | var updateSegment = function updateSegment(a, b) {
|
1547 | if (!a) {
|
1548 | return b;
|
1549 | }
|
1550 |
|
1551 | var result = mergeOptions$2(a, b);
|
1552 |
|
1553 |
|
1554 | if (a.preloadHints && !b.preloadHints) {
|
1555 | delete result.preloadHints;
|
1556 | }
|
1557 |
|
1558 |
|
1559 |
|
1560 | if (a.parts && !b.parts) {
|
1561 | delete result.parts;
|
1562 |
|
1563 |
|
1564 | } else if (a.parts && b.parts) {
|
1565 | for (var i = 0; i < b.parts.length; i++) {
|
1566 | if (a.parts && a.parts[i]) {
|
1567 | result.parts[i] = mergeOptions$2(a.parts[i], b.parts[i]);
|
1568 | }
|
1569 | }
|
1570 | }
|
1571 |
|
1572 |
|
1573 |
|
1574 | if (!a.skipped && b.skipped) {
|
1575 | result.skipped = false;
|
1576 | }
|
1577 |
|
1578 |
|
1579 |
|
1580 | if (a.preload && !b.preload) {
|
1581 | result.preload = false;
|
1582 | }
|
1583 |
|
1584 | return result;
|
1585 | };
|
1586 |
|
1587 |
|
1588 |
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 | var updateSegments = function updateSegments(original, update, offset) {
|
1603 | var oldSegments = original.slice();
|
1604 | var newSegments = update.slice();
|
1605 | offset = offset || 0;
|
1606 | var result = [];
|
1607 | var currentMap;
|
1608 |
|
1609 | for (var newIndex = 0; newIndex < newSegments.length; newIndex++) {
|
1610 | var oldSegment = oldSegments[newIndex + offset];
|
1611 | var newSegment = newSegments[newIndex];
|
1612 |
|
1613 | if (oldSegment) {
|
1614 | currentMap = oldSegment.map || currentMap;
|
1615 | result.push(updateSegment(oldSegment, newSegment));
|
1616 | } else {
|
1617 |
|
1618 | if (currentMap && !newSegment.map) {
|
1619 | newSegment.map = currentMap;
|
1620 | }
|
1621 |
|
1622 | result.push(newSegment);
|
1623 | }
|
1624 | }
|
1625 |
|
1626 | return result;
|
1627 | };
|
1628 | var resolveSegmentUris = function resolveSegmentUris(segment, baseUri) {
|
1629 |
|
1630 |
|
1631 | if (!segment.resolvedUri && segment.uri) {
|
1632 | segment.resolvedUri = resolveUrl(baseUri, segment.uri);
|
1633 | }
|
1634 |
|
1635 | if (segment.key && !segment.key.resolvedUri) {
|
1636 | segment.key.resolvedUri = resolveUrl(baseUri, segment.key.uri);
|
1637 | }
|
1638 |
|
1639 | if (segment.map && !segment.map.resolvedUri) {
|
1640 | segment.map.resolvedUri = resolveUrl(baseUri, segment.map.uri);
|
1641 | }
|
1642 |
|
1643 | if (segment.map && segment.map.key && !segment.map.key.resolvedUri) {
|
1644 | segment.map.key.resolvedUri = resolveUrl(baseUri, segment.map.key.uri);
|
1645 | }
|
1646 |
|
1647 | if (segment.parts && segment.parts.length) {
|
1648 | segment.parts.forEach(function (p) {
|
1649 | if (p.resolvedUri) {
|
1650 | return;
|
1651 | }
|
1652 |
|
1653 | p.resolvedUri = resolveUrl(baseUri, p.uri);
|
1654 | });
|
1655 | }
|
1656 |
|
1657 | if (segment.preloadHints && segment.preloadHints.length) {
|
1658 | segment.preloadHints.forEach(function (p) {
|
1659 | if (p.resolvedUri) {
|
1660 | return;
|
1661 | }
|
1662 |
|
1663 | p.resolvedUri = resolveUrl(baseUri, p.uri);
|
1664 | });
|
1665 | }
|
1666 | };
|
1667 |
|
1668 | var getAllSegments = function getAllSegments(media) {
|
1669 | var segments = media.segments || [];
|
1670 | var preloadSegment = media.preloadSegment;
|
1671 |
|
1672 |
|
1673 |
|
1674 | if (preloadSegment && preloadSegment.parts && preloadSegment.parts.length) {
|
1675 |
|
1676 |
|
1677 |
|
1678 | if (preloadSegment.preloadHints) {
|
1679 | for (var i = 0; i < preloadSegment.preloadHints.length; i++) {
|
1680 | if (preloadSegment.preloadHints[i].type === 'MAP') {
|
1681 | return segments;
|
1682 | }
|
1683 | }
|
1684 | }
|
1685 |
|
1686 |
|
1687 | preloadSegment.duration = media.targetDuration;
|
1688 | preloadSegment.preload = true;
|
1689 | segments.push(preloadSegment);
|
1690 | }
|
1691 |
|
1692 | return segments;
|
1693 | };
|
1694 |
|
1695 |
|
1696 |
|
1697 |
|
1698 | var isPlaylistUnchanged = function isPlaylistUnchanged(a, b) {
|
1699 | return a === b || a.segments && b.segments && a.segments.length === b.segments.length && a.endList === b.endList && a.mediaSequence === b.mediaSequence;
|
1700 | };
|
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 |
|
1709 |
|
1710 |
|
1711 |
|
1712 |
|
1713 |
|
1714 | var updateMaster$1 = function updateMaster(master, newMedia, unchangedCheck) {
|
1715 | if (unchangedCheck === void 0) {
|
1716 | unchangedCheck = isPlaylistUnchanged;
|
1717 | }
|
1718 |
|
1719 | var result = mergeOptions$2(master, {});
|
1720 | var oldMedia = result.playlists[newMedia.id];
|
1721 |
|
1722 | if (!oldMedia) {
|
1723 | return null;
|
1724 | }
|
1725 |
|
1726 | if (unchangedCheck(oldMedia, newMedia)) {
|
1727 | return null;
|
1728 | }
|
1729 |
|
1730 | newMedia.segments = getAllSegments(newMedia);
|
1731 | var mergedPlaylist = mergeOptions$2(oldMedia, newMedia);
|
1732 |
|
1733 | if (mergedPlaylist.preloadSegment && !newMedia.preloadSegment) {
|
1734 | delete mergedPlaylist.preloadSegment;
|
1735 | }
|
1736 |
|
1737 |
|
1738 | if (oldMedia.segments) {
|
1739 | if (newMedia.skip) {
|
1740 | newMedia.segments = newMedia.segments || [];
|
1741 |
|
1742 |
|
1743 | for (var i = 0; i < newMedia.skip.skippedSegments; i++) {
|
1744 | newMedia.segments.unshift({
|
1745 | skipped: true
|
1746 | });
|
1747 | }
|
1748 | }
|
1749 |
|
1750 | mergedPlaylist.segments = updateSegments(oldMedia.segments, newMedia.segments, newMedia.mediaSequence - oldMedia.mediaSequence);
|
1751 | }
|
1752 |
|
1753 |
|
1754 | mergedPlaylist.segments.forEach(function (segment) {
|
1755 | resolveSegmentUris(segment, mergedPlaylist.resolvedUri);
|
1756 | });
|
1757 |
|
1758 |
|
1759 |
|
1760 | for (var _i = 0; _i < result.playlists.length; _i++) {
|
1761 | if (result.playlists[_i].id === newMedia.id) {
|
1762 | result.playlists[_i] = mergedPlaylist;
|
1763 | }
|
1764 | }
|
1765 |
|
1766 | result.playlists[newMedia.id] = mergedPlaylist;
|
1767 |
|
1768 | result.playlists[newMedia.uri] = mergedPlaylist;
|
1769 |
|
1770 | forEachMediaGroup(master, function (properties, mediaType, groupKey, labelKey) {
|
1771 | if (!properties.playlists) {
|
1772 | return;
|
1773 | }
|
1774 |
|
1775 | for (var _i2 = 0; _i2 < properties.playlists.length; _i2++) {
|
1776 | if (newMedia.id === properties.playlists[_i2].id) {
|
1777 | properties.playlists[_i2] = newMedia;
|
1778 | }
|
1779 | }
|
1780 | });
|
1781 | return result;
|
1782 | };
|
1783 |
|
1784 |
|
1785 |
|
1786 |
|
1787 |
|
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 |
|
1793 |
|
1794 | var refreshDelay = function refreshDelay(media, update) {
|
1795 | var segments = media.segments || [];
|
1796 | var lastSegment = segments[segments.length - 1];
|
1797 | var lastPart = lastSegment && lastSegment.parts && lastSegment.parts[lastSegment.parts.length - 1];
|
1798 | var lastDuration = lastPart && lastPart.duration || lastSegment && lastSegment.duration;
|
1799 |
|
1800 | if (update && lastDuration) {
|
1801 | return lastDuration * 1000;
|
1802 | }
|
1803 |
|
1804 |
|
1805 |
|
1806 | return (media.partTargetDuration || media.targetDuration || 10) * 500;
|
1807 | };
|
1808 |
|
1809 |
|
1810 |
|
1811 |
|
1812 |
|
1813 |
|
1814 |
|
1815 |
|
1816 |
|
1817 |
|
1818 | var PlaylistLoader = function (_EventTarget) {
|
1819 | _inheritsLoose(PlaylistLoader, _EventTarget);
|
1820 |
|
1821 | function PlaylistLoader(src, vhs, options) {
|
1822 | var _this;
|
1823 |
|
1824 | if (options === void 0) {
|
1825 | options = {};
|
1826 | }
|
1827 |
|
1828 | _this = _EventTarget.call(this) || this;
|
1829 |
|
1830 | if (!src) {
|
1831 | throw new Error('A non-empty playlist URL or object is required');
|
1832 | }
|
1833 |
|
1834 | _this.logger_ = logger('PlaylistLoader');
|
1835 | var _options = options,
|
1836 | _options$withCredenti = _options.withCredentials,
|
1837 | withCredentials = _options$withCredenti === void 0 ? false : _options$withCredenti,
|
1838 | _options$handleManife = _options.handleManifestRedirects,
|
1839 | handleManifestRedirects = _options$handleManife === void 0 ? false : _options$handleManife;
|
1840 | _this.src = src;
|
1841 | _this.vhs_ = vhs;
|
1842 | _this.withCredentials = withCredentials;
|
1843 | _this.handleManifestRedirects = handleManifestRedirects;
|
1844 | var vhsOptions = vhs.options_;
|
1845 | _this.customTagParsers = vhsOptions && vhsOptions.customTagParsers || [];
|
1846 | _this.customTagMappers = vhsOptions && vhsOptions.customTagMappers || [];
|
1847 | _this.experimentalLLHLS = vhsOptions && vhsOptions.experimentalLLHLS || false;
|
1848 |
|
1849 | if (videojs.browser.IE_VERSION) {
|
1850 | _this.experimentalLLHLS = false;
|
1851 | }
|
1852 |
|
1853 |
|
1854 | _this.state = 'HAVE_NOTHING';
|
1855 |
|
1856 | _this.handleMediaupdatetimeout_ = _this.handleMediaupdatetimeout_.bind(_assertThisInitialized(_this));
|
1857 |
|
1858 | _this.on('mediaupdatetimeout', _this.handleMediaupdatetimeout_);
|
1859 |
|
1860 | return _this;
|
1861 | }
|
1862 |
|
1863 | var _proto = PlaylistLoader.prototype;
|
1864 |
|
1865 | _proto.handleMediaupdatetimeout_ = function handleMediaupdatetimeout_() {
|
1866 | var _this2 = this;
|
1867 |
|
1868 | if (this.state !== 'HAVE_METADATA') {
|
1869 |
|
1870 | return;
|
1871 | }
|
1872 |
|
1873 | var media = this.media();
|
1874 | var uri = resolveUrl(this.master.uri, media.uri);
|
1875 |
|
1876 | if (this.experimentalLLHLS) {
|
1877 | uri = addLLHLSQueryDirectives(uri, media);
|
1878 | }
|
1879 |
|
1880 | this.state = 'HAVE_CURRENT_METADATA';
|
1881 | this.request = this.vhs_.xhr({
|
1882 | uri: uri,
|
1883 | withCredentials: this.withCredentials
|
1884 | }, function (error, req) {
|
1885 |
|
1886 | if (!_this2.request) {
|
1887 | return;
|
1888 | }
|
1889 |
|
1890 | if (error) {
|
1891 | return _this2.playlistRequestError(_this2.request, _this2.media(), 'HAVE_METADATA');
|
1892 | }
|
1893 |
|
1894 | _this2.haveMetadata({
|
1895 | playlistString: _this2.request.responseText,
|
1896 | url: _this2.media().uri,
|
1897 | id: _this2.media().id
|
1898 | });
|
1899 | });
|
1900 | };
|
1901 |
|
1902 | _proto.playlistRequestError = function playlistRequestError(xhr, playlist, startingState) {
|
1903 | var uri = playlist.uri,
|
1904 | id = playlist.id;
|
1905 |
|
1906 | this.request = null;
|
1907 |
|
1908 | if (startingState) {
|
1909 | this.state = startingState;
|
1910 | }
|
1911 |
|
1912 | this.error = {
|
1913 | playlist: this.master.playlists[id],
|
1914 | status: xhr.status,
|
1915 | message: "HLS playlist request error at URL: " + uri + ".",
|
1916 | responseText: xhr.responseText,
|
1917 | code: xhr.status >= 500 ? 4 : 2
|
1918 | };
|
1919 | this.trigger('error');
|
1920 | };
|
1921 |
|
1922 | _proto.parseManifest_ = function parseManifest_(_ref) {
|
1923 | var _this3 = this;
|
1924 |
|
1925 | var url = _ref.url,
|
1926 | manifestString = _ref.manifestString;
|
1927 | return parseManifest({
|
1928 | onwarn: function onwarn(_ref2) {
|
1929 | var message = _ref2.message;
|
1930 | return _this3.logger_("m3u8-parser warn for " + url + ": " + message);
|
1931 | },
|
1932 | oninfo: function oninfo(_ref3) {
|
1933 | var message = _ref3.message;
|
1934 | return _this3.logger_("m3u8-parser info for " + url + ": " + message);
|
1935 | },
|
1936 | manifestString: manifestString,
|
1937 | customTagParsers: this.customTagParsers,
|
1938 | customTagMappers: this.customTagMappers,
|
1939 | experimentalLLHLS: this.experimentalLLHLS
|
1940 | });
|
1941 | }
|
1942 | |
1943 |
|
1944 |
|
1945 |
|
1946 |
|
1947 |
|
1948 |
|
1949 |
|
1950 |
|
1951 |
|
1952 |
|
1953 |
|
1954 | ;
|
1955 |
|
1956 | _proto.haveMetadata = function haveMetadata(_ref4) {
|
1957 | var playlistString = _ref4.playlistString,
|
1958 | playlistObject = _ref4.playlistObject,
|
1959 | url = _ref4.url,
|
1960 | id = _ref4.id;
|
1961 |
|
1962 | this.request = null;
|
1963 | this.state = 'HAVE_METADATA';
|
1964 | var playlist = playlistObject || this.parseManifest_({
|
1965 | url: url,
|
1966 | manifestString: playlistString
|
1967 | });
|
1968 | playlist.lastRequest = Date.now();
|
1969 | setupMediaPlaylist({
|
1970 | playlist: playlist,
|
1971 | uri: url,
|
1972 | id: id
|
1973 | });
|
1974 |
|
1975 | var update = updateMaster$1(this.master, playlist);
|
1976 | this.targetDuration = playlist.partTargetDuration || playlist.targetDuration;
|
1977 |
|
1978 | if (update) {
|
1979 | this.master = update;
|
1980 | this.media_ = this.master.playlists[id];
|
1981 | } else {
|
1982 | this.trigger('playlistunchanged');
|
1983 | }
|
1984 |
|
1985 | this.updateMediaUpdateTimeout_(refreshDelay(this.media(), !!update));
|
1986 | this.trigger('loadedplaylist');
|
1987 | }
|
1988 | |
1989 |
|
1990 |
|
1991 | ;
|
1992 |
|
1993 | _proto.dispose = function dispose() {
|
1994 | this.trigger('dispose');
|
1995 | this.stopRequest();
|
1996 | window.clearTimeout(this.mediaUpdateTimeout);
|
1997 | window.clearTimeout(this.finalRenditionTimeout);
|
1998 | this.off();
|
1999 | };
|
2000 |
|
2001 | _proto.stopRequest = function stopRequest() {
|
2002 | if (this.request) {
|
2003 | var oldRequest = this.request;
|
2004 | this.request = null;
|
2005 | oldRequest.onreadystatechange = null;
|
2006 | oldRequest.abort();
|
2007 | }
|
2008 | }
|
2009 | |
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
2017 |
|
2018 |
|
2019 |
|
2020 |
|
2021 |
|
2022 |
|
2023 | ;
|
2024 |
|
2025 | _proto.media = function media(playlist, shouldDelay) {
|
2026 | var _this4 = this;
|
2027 |
|
2028 |
|
2029 | if (!playlist) {
|
2030 | return this.media_;
|
2031 | }
|
2032 |
|
2033 |
|
2034 | if (this.state === 'HAVE_NOTHING') {
|
2035 | throw new Error('Cannot switch media playlist from ' + this.state);
|
2036 | }
|
2037 |
|
2038 |
|
2039 |
|
2040 | if (typeof playlist === 'string') {
|
2041 | if (!this.master.playlists[playlist]) {
|
2042 | throw new Error('Unknown playlist URI: ' + playlist);
|
2043 | }
|
2044 |
|
2045 | playlist = this.master.playlists[playlist];
|
2046 | }
|
2047 |
|
2048 | window.clearTimeout(this.finalRenditionTimeout);
|
2049 |
|
2050 | if (shouldDelay) {
|
2051 | var delay = (playlist.partTargetDuration || playlist.targetDuration) / 2 * 1000 || 5 * 1000;
|
2052 | this.finalRenditionTimeout = window.setTimeout(this.media.bind(this, playlist, false), delay);
|
2053 | return;
|
2054 | }
|
2055 |
|
2056 | var startingState = this.state;
|
2057 | var mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
2058 | var masterPlaylistRef = this.master.playlists[playlist.id];
|
2059 |
|
2060 | if (masterPlaylistRef && masterPlaylistRef.endList ||
|
2061 |
|
2062 | playlist.endList && playlist.segments.length) {
|
2063 |
|
2064 | if (this.request) {
|
2065 | this.request.onreadystatechange = null;
|
2066 | this.request.abort();
|
2067 | this.request = null;
|
2068 | }
|
2069 |
|
2070 | this.state = 'HAVE_METADATA';
|
2071 | this.media_ = playlist;
|
2072 |
|
2073 | if (mediaChange) {
|
2074 | this.trigger('mediachanging');
|
2075 |
|
2076 | if (startingState === 'HAVE_MASTER') {
|
2077 |
|
2078 |
|
2079 |
|
2080 |
|
2081 |
|
2082 | this.trigger('loadedmetadata');
|
2083 | } else {
|
2084 | this.trigger('mediachange');
|
2085 | }
|
2086 | }
|
2087 |
|
2088 | return;
|
2089 | }
|
2090 |
|
2091 |
|
2092 |
|
2093 |
|
2094 |
|
2095 |
|
2096 | this.updateMediaUpdateTimeout_(refreshDelay(playlist, true));
|
2097 |
|
2098 | if (!mediaChange) {
|
2099 | return;
|
2100 | }
|
2101 |
|
2102 | this.state = 'SWITCHING_MEDIA';
|
2103 |
|
2104 | if (this.request) {
|
2105 | if (playlist.resolvedUri === this.request.url) {
|
2106 |
|
2107 |
|
2108 | return;
|
2109 | }
|
2110 |
|
2111 | this.request.onreadystatechange = null;
|
2112 | this.request.abort();
|
2113 | this.request = null;
|
2114 | }
|
2115 |
|
2116 |
|
2117 | if (this.media_) {
|
2118 | this.trigger('mediachanging');
|
2119 | }
|
2120 |
|
2121 | this.request = this.vhs_.xhr({
|
2122 | uri: playlist.resolvedUri,
|
2123 | withCredentials: this.withCredentials
|
2124 | }, function (error, req) {
|
2125 |
|
2126 | if (!_this4.request) {
|
2127 | return;
|
2128 | }
|
2129 |
|
2130 | playlist.lastRequest = Date.now();
|
2131 | playlist.resolvedUri = resolveManifestRedirect(_this4.handleManifestRedirects, playlist.resolvedUri, req);
|
2132 |
|
2133 | if (error) {
|
2134 | return _this4.playlistRequestError(_this4.request, playlist, startingState);
|
2135 | }
|
2136 |
|
2137 | _this4.haveMetadata({
|
2138 | playlistString: req.responseText,
|
2139 | url: playlist.uri,
|
2140 | id: playlist.id
|
2141 | });
|
2142 |
|
2143 |
|
2144 | if (startingState === 'HAVE_MASTER') {
|
2145 | _this4.trigger('loadedmetadata');
|
2146 | } else {
|
2147 | _this4.trigger('mediachange');
|
2148 | }
|
2149 | });
|
2150 | }
|
2151 | |
2152 |
|
2153 |
|
2154 | ;
|
2155 |
|
2156 | _proto.pause = function pause() {
|
2157 | if (this.mediaUpdateTimeout) {
|
2158 | window.clearTimeout(this.mediaUpdateTimeout);
|
2159 | this.mediaUpdateTimeout = null;
|
2160 | }
|
2161 |
|
2162 | this.stopRequest();
|
2163 |
|
2164 | if (this.state === 'HAVE_NOTHING') {
|
2165 |
|
2166 |
|
2167 | this.started = false;
|
2168 | }
|
2169 |
|
2170 |
|
2171 | if (this.state === 'SWITCHING_MEDIA') {
|
2172 |
|
2173 |
|
2174 |
|
2175 | if (this.media_) {
|
2176 | this.state = 'HAVE_METADATA';
|
2177 | } else {
|
2178 | this.state = 'HAVE_MASTER';
|
2179 | }
|
2180 | } else if (this.state === 'HAVE_CURRENT_METADATA') {
|
2181 | this.state = 'HAVE_METADATA';
|
2182 | }
|
2183 | }
|
2184 | |
2185 |
|
2186 |
|
2187 | ;
|
2188 |
|
2189 | _proto.load = function load(shouldDelay) {
|
2190 | var _this5 = this;
|
2191 |
|
2192 | if (this.mediaUpdateTimeout) {
|
2193 | window.clearTimeout(this.mediaUpdateTimeout);
|
2194 | this.mediaUpdateTimeout = null;
|
2195 | }
|
2196 |
|
2197 | var media = this.media();
|
2198 |
|
2199 | if (shouldDelay) {
|
2200 | var delay = media ? (media.partTargetDuration || media.targetDuration) / 2 * 1000 : 5 * 1000;
|
2201 | this.mediaUpdateTimeout = window.setTimeout(function () {
|
2202 | _this5.mediaUpdateTimeout = null;
|
2203 |
|
2204 | _this5.load();
|
2205 | }, delay);
|
2206 | return;
|
2207 | }
|
2208 |
|
2209 | if (!this.started) {
|
2210 | this.start();
|
2211 | return;
|
2212 | }
|
2213 |
|
2214 | if (media && !media.endList) {
|
2215 | this.trigger('mediaupdatetimeout');
|
2216 | } else {
|
2217 | this.trigger('loadedplaylist');
|
2218 | }
|
2219 | };
|
2220 |
|
2221 | _proto.updateMediaUpdateTimeout_ = function updateMediaUpdateTimeout_(delay) {
|
2222 | var _this6 = this;
|
2223 |
|
2224 | if (this.mediaUpdateTimeout) {
|
2225 | window.clearTimeout(this.mediaUpdateTimeout);
|
2226 | this.mediaUpdateTimeout = null;
|
2227 | }
|
2228 |
|
2229 |
|
2230 | if (!this.media() || this.media().endList) {
|
2231 | return;
|
2232 | }
|
2233 |
|
2234 | this.mediaUpdateTimeout = window.setTimeout(function () {
|
2235 | _this6.mediaUpdateTimeout = null;
|
2236 |
|
2237 | _this6.trigger('mediaupdatetimeout');
|
2238 |
|
2239 | _this6.updateMediaUpdateTimeout_(delay);
|
2240 | }, delay);
|
2241 | }
|
2242 | |
2243 |
|
2244 |
|
2245 | ;
|
2246 |
|
2247 | _proto.start = function start() {
|
2248 | var _this7 = this;
|
2249 |
|
2250 | this.started = true;
|
2251 |
|
2252 | if (typeof this.src === 'object') {
|
2253 |
|
2254 |
|
2255 | if (!this.src.uri) {
|
2256 | this.src.uri = window.location.href;
|
2257 | }
|
2258 |
|
2259 |
|
2260 |
|
2261 | this.src.resolvedUri = this.src.uri;
|
2262 |
|
2263 |
|
2264 |
|
2265 |
|
2266 |
|
2267 |
|
2268 |
|
2269 |
|
2270 |
|
2271 | setTimeout(function () {
|
2272 | _this7.setupInitialPlaylist(_this7.src);
|
2273 | }, 0);
|
2274 | return;
|
2275 | }
|
2276 |
|
2277 |
|
2278 | this.request = this.vhs_.xhr({
|
2279 | uri: this.src,
|
2280 | withCredentials: this.withCredentials
|
2281 | }, function (error, req) {
|
2282 |
|
2283 | if (!_this7.request) {
|
2284 | return;
|
2285 | }
|
2286 |
|
2287 |
|
2288 | _this7.request = null;
|
2289 |
|
2290 | if (error) {
|
2291 | _this7.error = {
|
2292 | status: req.status,
|
2293 | message: "HLS playlist request error at URL: " + _this7.src + ".",
|
2294 | responseText: req.responseText,
|
2295 |
|
2296 | code: 2
|
2297 | };
|
2298 |
|
2299 | if (_this7.state === 'HAVE_NOTHING') {
|
2300 | _this7.started = false;
|
2301 | }
|
2302 |
|
2303 | return _this7.trigger('error');
|
2304 | }
|
2305 |
|
2306 | _this7.src = resolveManifestRedirect(_this7.handleManifestRedirects, _this7.src, req);
|
2307 |
|
2308 | var manifest = _this7.parseManifest_({
|
2309 | manifestString: req.responseText,
|
2310 | url: _this7.src
|
2311 | });
|
2312 |
|
2313 | _this7.setupInitialPlaylist(manifest);
|
2314 | });
|
2315 | };
|
2316 |
|
2317 | _proto.srcUri = function srcUri() {
|
2318 | return typeof this.src === 'string' ? this.src : this.src.uri;
|
2319 | }
|
2320 | |
2321 |
|
2322 |
|
2323 |
|
2324 |
|
2325 |
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 |
|
2331 |
|
2332 |
|
2333 |
|
2334 |
|
2335 |
|
2336 |
|
2337 |
|
2338 | ;
|
2339 |
|
2340 | _proto.setupInitialPlaylist = function setupInitialPlaylist(manifest) {
|
2341 | this.state = 'HAVE_MASTER';
|
2342 |
|
2343 | if (manifest.playlists) {
|
2344 | this.master = manifest;
|
2345 | addPropertiesToMaster(this.master, this.srcUri());
|
2346 |
|
2347 |
|
2348 |
|
2349 | manifest.playlists.forEach(function (playlist) {
|
2350 | playlist.segments = getAllSegments(playlist);
|
2351 | playlist.segments.forEach(function (segment) {
|
2352 | resolveSegmentUris(segment, playlist.resolvedUri);
|
2353 | });
|
2354 | });
|
2355 | this.trigger('loadedplaylist');
|
2356 |
|
2357 | if (!this.request) {
|
2358 |
|
2359 |
|
2360 | this.media(this.master.playlists[0]);
|
2361 | }
|
2362 |
|
2363 | return;
|
2364 | }
|
2365 |
|
2366 |
|
2367 |
|
2368 |
|
2369 | var uri = this.srcUri() || window.location.href;
|
2370 | this.master = masterForMedia(manifest, uri);
|
2371 | this.haveMetadata({
|
2372 | playlistObject: manifest,
|
2373 | url: uri,
|
2374 | id: this.master.playlists[0].id
|
2375 | });
|
2376 | this.trigger('loadedmetadata');
|
2377 | };
|
2378 |
|
2379 | return PlaylistLoader;
|
2380 | }(EventTarget$1);
|
2381 |
|
2382 |
|
2383 |
|
2384 |
|
2385 | var videojsXHR = videojs.xhr,
|
2386 | mergeOptions$1 = videojs.mergeOptions;
|
2387 |
|
2388 | var callbackWrapper = function callbackWrapper(request, error, response, callback) {
|
2389 | var reqResponse = request.responseType === 'arraybuffer' ? request.response : request.responseText;
|
2390 |
|
2391 | if (!error && reqResponse) {
|
2392 | request.responseTime = Date.now();
|
2393 | request.roundTripTime = request.responseTime - request.requestTime;
|
2394 | request.bytesReceived = reqResponse.byteLength || reqResponse.length;
|
2395 |
|
2396 | if (!request.bandwidth) {
|
2397 | request.bandwidth = Math.floor(request.bytesReceived / request.roundTripTime * 8 * 1000);
|
2398 | }
|
2399 | }
|
2400 |
|
2401 | if (response.headers) {
|
2402 | request.responseHeaders = response.headers;
|
2403 | }
|
2404 |
|
2405 |
|
2406 |
|
2407 |
|
2408 | if (error && error.code === 'ETIMEDOUT') {
|
2409 | request.timedout = true;
|
2410 | }
|
2411 |
|
2412 |
|
2413 |
|
2414 |
|
2415 | if (!error && !request.aborted && response.statusCode !== 200 && response.statusCode !== 206 && response.statusCode !== 0) {
|
2416 | error = new Error('XHR Failed with a response of: ' + (request && (reqResponse || request.responseText)));
|
2417 | }
|
2418 |
|
2419 | callback(error, request);
|
2420 | };
|
2421 |
|
2422 | var xhrFactory = function xhrFactory() {
|
2423 | var xhr = function XhrFunction(options, callback) {
|
2424 |
|
2425 | options = mergeOptions$1({
|
2426 | timeout: 45e3
|
2427 | }, options);
|
2428 |
|
2429 |
|
2430 | var beforeRequest = XhrFunction.beforeRequest || videojs.Vhs.xhr.beforeRequest;
|
2431 |
|
2432 | if (beforeRequest && typeof beforeRequest === 'function') {
|
2433 | var newOptions = beforeRequest(options);
|
2434 |
|
2435 | if (newOptions) {
|
2436 | options = newOptions;
|
2437 | }
|
2438 | }
|
2439 |
|
2440 |
|
2441 |
|
2442 | var xhrMethod = videojs.Vhs.xhr.original === true ? videojsXHR : videojs.Vhs.xhr;
|
2443 | var request = xhrMethod(options, function (error, response) {
|
2444 | return callbackWrapper(request, error, response, callback);
|
2445 | });
|
2446 | var originalAbort = request.abort;
|
2447 |
|
2448 | request.abort = function () {
|
2449 | request.aborted = true;
|
2450 | return originalAbort.apply(request, arguments);
|
2451 | };
|
2452 |
|
2453 | request.uri = options.uri;
|
2454 | request.requestTime = Date.now();
|
2455 | return request;
|
2456 | };
|
2457 |
|
2458 | xhr.original = true;
|
2459 | return xhr;
|
2460 | };
|
2461 |
|
2462 |
|
2463 |
|
2464 |
|
2465 |
|
2466 |
|
2467 |
|
2468 |
|
2469 |
|
2470 | var byterangeStr = function byterangeStr(byterange) {
|
2471 |
|
2472 |
|
2473 | var byterangeEnd = byterange.offset + byterange.length - 1;
|
2474 | var byterangeStart = byterange.offset;
|
2475 | return 'bytes=' + byterangeStart + '-' + byterangeEnd;
|
2476 | };
|
2477 |
|
2478 |
|
2479 |
|
2480 |
|
2481 |
|
2482 |
|
2483 |
|
2484 |
|
2485 | var segmentXhrHeaders = function segmentXhrHeaders(segment) {
|
2486 | var headers = {};
|
2487 |
|
2488 | if (segment.byterange) {
|
2489 | headers.Range = byterangeStr(segment.byterange);
|
2490 | }
|
2491 |
|
2492 | return headers;
|
2493 | };
|
2494 |
|
2495 |
|
2496 |
|
2497 |
|
2498 |
|
2499 |
|
2500 |
|
2501 |
|
2502 |
|
2503 |
|
2504 |
|
2505 |
|
2506 | var textRange = function textRange(range, i) {
|
2507 | return range.start(i) + '-' + range.end(i);
|
2508 | };
|
2509 |
|
2510 |
|
2511 |
|
2512 |
|
2513 |
|
2514 |
|
2515 |
|
2516 |
|
2517 |
|
2518 | var formatHexString = function formatHexString(e, i) {
|
2519 | var value = e.toString(16);
|
2520 | return '00'.substring(0, 2 - value.length) + value + (i % 2 ? ' ' : '');
|
2521 | };
|
2522 |
|
2523 | var formatAsciiString = function formatAsciiString(e) {
|
2524 | if (e >= 0x20 && e < 0x7e) {
|
2525 | return String.fromCharCode(e);
|
2526 | }
|
2527 |
|
2528 | return '.';
|
2529 | };
|
2530 |
|
2531 |
|
2532 |
|
2533 |
|
2534 |
|
2535 |
|
2536 |
|
2537 |
|
2538 |
|
2539 |
|
2540 |
|
2541 |
|
2542 | var createTransferableMessage = function createTransferableMessage(message) {
|
2543 | var transferable = {};
|
2544 | Object.keys(message).forEach(function (key) {
|
2545 | var value = message[key];
|
2546 |
|
2547 | if (ArrayBuffer.isView(value)) {
|
2548 | transferable[key] = {
|
2549 | bytes: value.buffer,
|
2550 | byteOffset: value.byteOffset,
|
2551 | byteLength: value.byteLength
|
2552 | };
|
2553 | } else {
|
2554 | transferable[key] = value;
|
2555 | }
|
2556 | });
|
2557 | return transferable;
|
2558 | };
|
2559 |
|
2560 |
|
2561 |
|
2562 |
|
2563 |
|
2564 |
|
2565 |
|
2566 |
|
2567 |
|
2568 |
|
2569 | var initSegmentId = function initSegmentId(initSegment) {
|
2570 | var byterange = initSegment.byterange || {
|
2571 | length: Infinity,
|
2572 | offset: 0
|
2573 | };
|
2574 | return [byterange.length, byterange.offset, initSegment.resolvedUri].join(',');
|
2575 | };
|
2576 |
|
2577 |
|
2578 |
|
2579 |
|
2580 |
|
2581 |
|
2582 |
|
2583 | var segmentKeyId = function segmentKeyId(key) {
|
2584 | return key.resolvedUri;
|
2585 | };
|
2586 |
|
2587 |
|
2588 |
|
2589 |
|
2590 |
|
2591 |
|
2592 |
|
2593 |
|
2594 |
|
2595 | var hexDump = function hexDump(data) {
|
2596 | var bytes = Array.prototype.slice.call(data);
|
2597 | var step = 16;
|
2598 | var result = '';
|
2599 | var hex;
|
2600 | var ascii;
|
2601 |
|
2602 | for (var j = 0; j < bytes.length / step; j++) {
|
2603 | hex = bytes.slice(j * step, j * step + step).map(formatHexString).join('');
|
2604 | ascii = bytes.slice(j * step, j * step + step).map(formatAsciiString).join('');
|
2605 | result += hex + ' ' + ascii + '\n';
|
2606 | }
|
2607 |
|
2608 | return result;
|
2609 | };
|
2610 | var tagDump = function tagDump(_ref) {
|
2611 | var bytes = _ref.bytes;
|
2612 | return hexDump(bytes);
|
2613 | };
|
2614 | var textRanges = function textRanges(ranges) {
|
2615 | var result = '';
|
2616 | var i;
|
2617 |
|
2618 | for (i = 0; i < ranges.length; i++) {
|
2619 | result += textRange(ranges, i) + ' ';
|
2620 | }
|
2621 |
|
2622 | return result;
|
2623 | };
|
2624 |
|
2625 | var utils = Object.freeze({
|
2626 | __proto__: null,
|
2627 | createTransferableMessage: createTransferableMessage,
|
2628 | initSegmentId: initSegmentId,
|
2629 | segmentKeyId: segmentKeyId,
|
2630 | hexDump: hexDump,
|
2631 | tagDump: tagDump,
|
2632 | textRanges: textRanges
|
2633 | });
|
2634 |
|
2635 |
|
2636 |
|
2637 |
|
2638 | var SEGMENT_END_FUDGE_PERCENT = 0.25;
|
2639 |
|
2640 |
|
2641 |
|
2642 |
|
2643 |
|
2644 |
|
2645 |
|
2646 |
|
2647 |
|
2648 |
|
2649 |
|
2650 |
|
2651 |
|
2652 |
|
2653 |
|
2654 |
|
2655 | var playerTimeToProgramTime = function playerTimeToProgramTime(playerTime, segment) {
|
2656 | if (!segment.dateTimeObject) {
|
2657 |
|
2658 |
|
2659 | return null;
|
2660 | }
|
2661 |
|
2662 | var transmuxerPrependedSeconds = segment.videoTimingInfo.transmuxerPrependedSeconds;
|
2663 | var transmuxedStart = segment.videoTimingInfo.transmuxedPresentationStart;
|
2664 |
|
2665 | var startOfSegment = transmuxedStart + transmuxerPrependedSeconds;
|
2666 | var offsetFromSegmentStart = playerTime - startOfSegment;
|
2667 | return new Date(segment.dateTimeObject.getTime() + offsetFromSegmentStart * 1000);
|
2668 | };
|
2669 | var originalSegmentVideoDuration = function originalSegmentVideoDuration(videoTimingInfo) {
|
2670 | return videoTimingInfo.transmuxedPresentationEnd - videoTimingInfo.transmuxedPresentationStart - videoTimingInfo.transmuxerPrependedSeconds;
|
2671 | };
|
2672 |
|
2673 |
|
2674 |
|
2675 |
|
2676 |
|
2677 |
|
2678 |
|
2679 |
|
2680 | var findSegmentForProgramTime = function findSegmentForProgramTime(programTime, playlist) {
|
2681 |
|
2682 |
|
2683 |
|
2684 | var dateTimeObject;
|
2685 |
|
2686 | try {
|
2687 | dateTimeObject = new Date(programTime);
|
2688 | } catch (e) {
|
2689 | return null;
|
2690 | }
|
2691 |
|
2692 | if (!playlist || !playlist.segments || playlist.segments.length === 0) {
|
2693 | return null;
|
2694 | }
|
2695 |
|
2696 | var segment = playlist.segments[0];
|
2697 |
|
2698 | if (dateTimeObject < segment.dateTimeObject) {
|
2699 |
|
2700 | return null;
|
2701 | }
|
2702 |
|
2703 | for (var i = 0; i < playlist.segments.length - 1; i++) {
|
2704 | segment = playlist.segments[i];
|
2705 | var nextSegmentStart = playlist.segments[i + 1].dateTimeObject;
|
2706 |
|
2707 | if (dateTimeObject < nextSegmentStart) {
|
2708 | break;
|
2709 | }
|
2710 | }
|
2711 |
|
2712 | var lastSegment = playlist.segments[playlist.segments.length - 1];
|
2713 | var lastSegmentStart = lastSegment.dateTimeObject;
|
2714 | var lastSegmentDuration = lastSegment.videoTimingInfo ? originalSegmentVideoDuration(lastSegment.videoTimingInfo) : lastSegment.duration + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT;
|
2715 | var lastSegmentEnd = new Date(lastSegmentStart.getTime() + lastSegmentDuration * 1000);
|
2716 |
|
2717 | if (dateTimeObject > lastSegmentEnd) {
|
2718 |
|
2719 | return null;
|
2720 | }
|
2721 |
|
2722 | if (dateTimeObject > lastSegmentStart) {
|
2723 | segment = lastSegment;
|
2724 | }
|
2725 |
|
2726 | return {
|
2727 | segment: segment,
|
2728 | estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : Playlist.duration(playlist, playlist.mediaSequence + playlist.segments.indexOf(segment)),
|
2729 |
|
2730 |
|
2731 |
|
2732 |
|
2733 | type: segment.videoTimingInfo ? 'accurate' : 'estimate'
|
2734 | };
|
2735 | };
|
2736 |
|
2737 |
|
2738 |
|
2739 |
|
2740 |
|
2741 |
|
2742 |
|
2743 | var findSegmentForPlayerTime = function findSegmentForPlayerTime(time, playlist) {
|
2744 |
|
2745 |
|
2746 |
|
2747 |
|
2748 | if (!playlist || !playlist.segments || playlist.segments.length === 0) {
|
2749 | return null;
|
2750 | }
|
2751 |
|
2752 | var segmentEnd = 0;
|
2753 | var segment;
|
2754 |
|
2755 | for (var i = 0; i < playlist.segments.length; i++) {
|
2756 | segment = playlist.segments[i];
|
2757 |
|
2758 |
|
2759 |
|
2760 |
|
2761 |
|
2762 |
|
2763 | segmentEnd = segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationEnd : segmentEnd + segment.duration;
|
2764 |
|
2765 | if (time <= segmentEnd) {
|
2766 | break;
|
2767 | }
|
2768 | }
|
2769 |
|
2770 | var lastSegment = playlist.segments[playlist.segments.length - 1];
|
2771 |
|
2772 | if (lastSegment.videoTimingInfo && lastSegment.videoTimingInfo.transmuxedPresentationEnd < time) {
|
2773 |
|
2774 | return null;
|
2775 | }
|
2776 |
|
2777 | if (time > segmentEnd) {
|
2778 |
|
2779 |
|
2780 |
|
2781 | if (time > segmentEnd + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT) {
|
2782 |
|
2783 |
|
2784 |
|
2785 | return null;
|
2786 | }
|
2787 |
|
2788 | segment = lastSegment;
|
2789 | }
|
2790 |
|
2791 | return {
|
2792 | segment: segment,
|
2793 | estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : segmentEnd - segment.duration,
|
2794 |
|
2795 |
|
2796 | type: segment.videoTimingInfo ? 'accurate' : 'estimate'
|
2797 | };
|
2798 | };
|
2799 |
|
2800 |
|
2801 |
|
2802 |
|
2803 |
|
2804 |
|
2805 |
|
2806 |
|
2807 |
|
2808 |
|
2809 |
|
2810 | var getOffsetFromTimestamp = function getOffsetFromTimestamp(comparisonTimeStamp, programTime) {
|
2811 | var segmentDateTime;
|
2812 | var programDateTime;
|
2813 |
|
2814 | try {
|
2815 | segmentDateTime = new Date(comparisonTimeStamp);
|
2816 | programDateTime = new Date(programTime);
|
2817 | } catch (e) {
|
2818 | }
|
2819 |
|
2820 | var segmentTimeEpoch = segmentDateTime.getTime();
|
2821 | var programTimeEpoch = programDateTime.getTime();
|
2822 | return (programTimeEpoch - segmentTimeEpoch) / 1000;
|
2823 | };
|
2824 |
|
2825 |
|
2826 |
|
2827 |
|
2828 |
|
2829 |
|
2830 | var verifyProgramDateTimeTags = function verifyProgramDateTimeTags(playlist) {
|
2831 | if (!playlist.segments || playlist.segments.length === 0) {
|
2832 | return false;
|
2833 | }
|
2834 |
|
2835 | for (var i = 0; i < playlist.segments.length; i++) {
|
2836 | var segment = playlist.segments[i];
|
2837 |
|
2838 | if (!segment.dateTimeObject) {
|
2839 | return false;
|
2840 | }
|
2841 | }
|
2842 |
|
2843 | return true;
|
2844 | };
|
2845 |
|
2846 |
|
2847 |
|
2848 |
|
2849 |
|
2850 |
|
2851 |
|
2852 |
|
2853 |
|
2854 |
|
2855 |
|
2856 |
|
2857 |
|
2858 |
|
2859 |
|
2860 |
|
2861 | var getProgramTime = function getProgramTime(_ref) {
|
2862 | var playlist = _ref.playlist,
|
2863 | _ref$time = _ref.time,
|
2864 | time = _ref$time === void 0 ? undefined : _ref$time,
|
2865 | callback = _ref.callback;
|
2866 |
|
2867 | if (!callback) {
|
2868 | throw new Error('getProgramTime: callback must be provided');
|
2869 | }
|
2870 |
|
2871 | if (!playlist || time === undefined) {
|
2872 | return callback({
|
2873 | message: 'getProgramTime: playlist and time must be provided'
|
2874 | });
|
2875 | }
|
2876 |
|
2877 | var matchedSegment = findSegmentForPlayerTime(time, playlist);
|
2878 |
|
2879 | if (!matchedSegment) {
|
2880 | return callback({
|
2881 | message: 'valid programTime was not found'
|
2882 | });
|
2883 | }
|
2884 |
|
2885 | if (matchedSegment.type === 'estimate') {
|
2886 | return callback({
|
2887 | message: 'Accurate programTime could not be determined.' + ' Please seek to e.seekTime and try again',
|
2888 | seekTime: matchedSegment.estimatedStart
|
2889 | });
|
2890 | }
|
2891 |
|
2892 | var programTimeObject = {
|
2893 | mediaSeconds: time
|
2894 | };
|
2895 | var programTime = playerTimeToProgramTime(time, matchedSegment.segment);
|
2896 |
|
2897 | if (programTime) {
|
2898 | programTimeObject.programDateTime = programTime.toISOString();
|
2899 | }
|
2900 |
|
2901 | return callback(null, programTimeObject);
|
2902 | };
|
2903 |
|
2904 |
|
2905 |
|
2906 |
|
2907 |
|
2908 |
|
2909 |
|
2910 |
|
2911 |
|
2912 |
|
2913 |
|
2914 |
|
2915 |
|
2916 |
|
2917 |
|
2918 | var seekToProgramTime = function seekToProgramTime(_ref2) {
|
2919 | var programTime = _ref2.programTime,
|
2920 | playlist = _ref2.playlist,
|
2921 | _ref2$retryCount = _ref2.retryCount,
|
2922 | retryCount = _ref2$retryCount === void 0 ? 2 : _ref2$retryCount,
|
2923 | seekTo = _ref2.seekTo,
|
2924 | _ref2$pauseAfterSeek = _ref2.pauseAfterSeek,
|
2925 | pauseAfterSeek = _ref2$pauseAfterSeek === void 0 ? true : _ref2$pauseAfterSeek,
|
2926 | tech = _ref2.tech,
|
2927 | callback = _ref2.callback;
|
2928 |
|
2929 | if (!callback) {
|
2930 | throw new Error('seekToProgramTime: callback must be provided');
|
2931 | }
|
2932 |
|
2933 | if (typeof programTime === 'undefined' || !playlist || !seekTo) {
|
2934 | return callback({
|
2935 | message: 'seekToProgramTime: programTime, seekTo and playlist must be provided'
|
2936 | });
|
2937 | }
|
2938 |
|
2939 | if (!playlist.endList && !tech.hasStarted_) {
|
2940 | return callback({
|
2941 | message: 'player must be playing a live stream to start buffering'
|
2942 | });
|
2943 | }
|
2944 |
|
2945 | if (!verifyProgramDateTimeTags(playlist)) {
|
2946 | return callback({
|
2947 | message: 'programDateTime tags must be provided in the manifest ' + playlist.resolvedUri
|
2948 | });
|
2949 | }
|
2950 |
|
2951 | var matchedSegment = findSegmentForProgramTime(programTime, playlist);
|
2952 |
|
2953 | if (!matchedSegment) {
|
2954 | return callback({
|
2955 | message: programTime + " was not found in the stream"
|
2956 | });
|
2957 | }
|
2958 |
|
2959 | var segment = matchedSegment.segment;
|
2960 | var mediaOffset = getOffsetFromTimestamp(segment.dateTimeObject, programTime);
|
2961 |
|
2962 | if (matchedSegment.type === 'estimate') {
|
2963 |
|
2964 | if (retryCount === 0) {
|
2965 | return callback({
|
2966 | message: programTime + " is not buffered yet. Try again"
|
2967 | });
|
2968 | }
|
2969 |
|
2970 | seekTo(matchedSegment.estimatedStart + mediaOffset);
|
2971 | tech.one('seeked', function () {
|
2972 | seekToProgramTime({
|
2973 | programTime: programTime,
|
2974 | playlist: playlist,
|
2975 | retryCount: retryCount - 1,
|
2976 | seekTo: seekTo,
|
2977 | pauseAfterSeek: pauseAfterSeek,
|
2978 | tech: tech,
|
2979 | callback: callback
|
2980 | });
|
2981 | });
|
2982 | return;
|
2983 | }
|
2984 |
|
2985 |
|
2986 |
|
2987 |
|
2988 | var seekToTime = segment.start + mediaOffset;
|
2989 |
|
2990 | var seekedCallback = function seekedCallback() {
|
2991 | return callback(null, tech.currentTime());
|
2992 | };
|
2993 |
|
2994 |
|
2995 | tech.one('seeked', seekedCallback);
|
2996 |
|
2997 | if (pauseAfterSeek) {
|
2998 | tech.pause();
|
2999 | }
|
3000 |
|
3001 | seekTo(seekToTime);
|
3002 | };
|
3003 |
|
3004 |
|
3005 |
|
3006 | var callbackOnCompleted = function callbackOnCompleted(request, cb) {
|
3007 | if (request.readyState === 4) {
|
3008 | return cb();
|
3009 | }
|
3010 |
|
3011 | return;
|
3012 | };
|
3013 |
|
3014 | var containerRequest = function containerRequest(uri, xhr, cb) {
|
3015 | var bytes = [];
|
3016 | var id3Offset;
|
3017 | var finished = false;
|
3018 |
|
3019 | var endRequestAndCallback = function endRequestAndCallback(err, req, type, _bytes) {
|
3020 | req.abort();
|
3021 | finished = true;
|
3022 | return cb(err, req, type, _bytes);
|
3023 | };
|
3024 |
|
3025 | var progressListener = function progressListener(error, request) {
|
3026 | if (finished) {
|
3027 | return;
|
3028 | }
|
3029 |
|
3030 | if (error) {
|
3031 | return endRequestAndCallback(error, request, '', bytes);
|
3032 | }
|
3033 |
|
3034 |
|
3035 | var newPart = request.responseText.substring(bytes && bytes.byteLength || 0, request.responseText.length);
|
3036 |
|
3037 | bytes = concatTypedArrays(bytes, stringToBytes(newPart, true));
|
3038 | id3Offset = id3Offset || getId3Offset(bytes);
|
3039 |
|
3040 |
|
3041 | if (bytes.length < 10 || id3Offset && bytes.length < id3Offset + 2) {
|
3042 | return callbackOnCompleted(request, function () {
|
3043 | return endRequestAndCallback(error, request, '', bytes);
|
3044 | });
|
3045 | }
|
3046 |
|
3047 | var type = detectContainerForBytes(bytes);
|
3048 |
|
3049 |
|
3050 |
|
3051 | if (type === 'ts' && bytes.length < 188) {
|
3052 | return callbackOnCompleted(request, function () {
|
3053 | return endRequestAndCallback(error, request, '', bytes);
|
3054 | });
|
3055 | }
|
3056 |
|
3057 |
|
3058 |
|
3059 | if (!type && bytes.length < 376) {
|
3060 | return callbackOnCompleted(request, function () {
|
3061 | return endRequestAndCallback(error, request, '', bytes);
|
3062 | });
|
3063 | }
|
3064 |
|
3065 | return endRequestAndCallback(null, request, type, bytes);
|
3066 | };
|
3067 |
|
3068 | var options = {
|
3069 | uri: uri,
|
3070 | beforeSend: function beforeSend(request) {
|
3071 |
|
3072 | request.overrideMimeType('text/plain; charset=x-user-defined');
|
3073 | request.addEventListener('progress', function (_ref) {
|
3074 | _ref.total;
|
3075 | _ref.loaded;
|
3076 | return callbackWrapper(request, null, {
|
3077 | statusCode: request.status
|
3078 | }, progressListener);
|
3079 | });
|
3080 | }
|
3081 | };
|
3082 | var request = xhr(options, function (error, response) {
|
3083 | return callbackWrapper(request, error, response, progressListener);
|
3084 | });
|
3085 | return request;
|
3086 | };
|
3087 |
|
3088 | var EventTarget = videojs.EventTarget,
|
3089 | mergeOptions = videojs.mergeOptions;
|
3090 |
|
3091 | var dashPlaylistUnchanged = function dashPlaylistUnchanged(a, b) {
|
3092 | if (!isPlaylistUnchanged(a, b)) {
|
3093 | return false;
|
3094 | }
|
3095 |
|
3096 |
|
3097 |
|
3098 |
|
3099 |
|
3100 |
|
3101 |
|
3102 | if (a.sidx && b.sidx && (a.sidx.offset !== b.sidx.offset || a.sidx.length !== b.sidx.length)) {
|
3103 | return false;
|
3104 | } else if (!a.sidx && b.sidx || a.sidx && !b.sidx) {
|
3105 | return false;
|
3106 | }
|
3107 |
|
3108 |
|
3109 |
|
3110 | if (a.segments && !b.segments || !a.segments && b.segments) {
|
3111 | return false;
|
3112 | }
|
3113 |
|
3114 |
|
3115 | if (!a.segments && !b.segments) {
|
3116 | return true;
|
3117 | }
|
3118 |
|
3119 |
|
3120 | for (var i = 0; i < a.segments.length; i++) {
|
3121 | var aSegment = a.segments[i];
|
3122 | var bSegment = b.segments[i];
|
3123 |
|
3124 | if (aSegment.uri !== bSegment.uri) {
|
3125 | return false;
|
3126 | }
|
3127 |
|
3128 |
|
3129 | if (!aSegment.byterange && !bSegment.byterange) {
|
3130 | continue;
|
3131 | }
|
3132 |
|
3133 | var aByterange = aSegment.byterange;
|
3134 | var bByterange = bSegment.byterange;
|
3135 |
|
3136 | if (aByterange && !bByterange || !aByterange && bByterange) {
|
3137 | return false;
|
3138 | }
|
3139 |
|
3140 |
|
3141 | if (aByterange.offset !== bByterange.offset || aByterange.length !== bByterange.length) {
|
3142 | return false;
|
3143 | }
|
3144 | }
|
3145 |
|
3146 |
|
3147 | return true;
|
3148 | };
|
3149 |
|
3150 |
|
3151 |
|
3152 |
|
3153 |
|
3154 |
|
3155 |
|
3156 |
|
3157 |
|
3158 |
|
3159 |
|
3160 |
|
3161 |
|
3162 |
|
3163 |
|
3164 |
|
3165 |
|
3166 |
|
3167 | var parseMasterXml = function parseMasterXml(_ref) {
|
3168 | var masterXml = _ref.masterXml,
|
3169 | srcUrl = _ref.srcUrl,
|
3170 | clientOffset = _ref.clientOffset,
|
3171 | sidxMapping = _ref.sidxMapping;
|
3172 | var master = parse(masterXml, {
|
3173 | manifestUri: srcUrl,
|
3174 | clientOffset: clientOffset,
|
3175 | sidxMapping: sidxMapping
|
3176 | });
|
3177 | addPropertiesToMaster(master, srcUrl);
|
3178 | return master;
|
3179 | };
|
3180 |
|
3181 |
|
3182 |
|
3183 |
|
3184 |
|
3185 |
|
3186 |
|
3187 |
|
3188 |
|
3189 |
|
3190 |
|
3191 |
|
3192 |
|
3193 | var updateMaster = function updateMaster(oldMaster, newMaster, sidxMapping) {
|
3194 | var noChanges = true;
|
3195 | var update = mergeOptions(oldMaster, {
|
3196 |
|
3197 | duration: newMaster.duration,
|
3198 | minimumUpdatePeriod: newMaster.minimumUpdatePeriod
|
3199 | });
|
3200 |
|
3201 | for (var i = 0; i < newMaster.playlists.length; i++) {
|
3202 | var playlist = newMaster.playlists[i];
|
3203 |
|
3204 | if (playlist.sidx) {
|
3205 | var sidxKey = generateSidxKey(playlist.sidx);
|
3206 |
|
3207 | if (sidxMapping && sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx) {
|
3208 | addSidxSegmentsToPlaylist(playlist, sidxMapping[sidxKey].sidx, playlist.sidx.resolvedUri);
|
3209 | }
|
3210 | }
|
3211 |
|
3212 | var playlistUpdate = updateMaster$1(update, playlist, dashPlaylistUnchanged);
|
3213 |
|
3214 | if (playlistUpdate) {
|
3215 | update = playlistUpdate;
|
3216 | noChanges = false;
|
3217 | }
|
3218 | }
|
3219 |
|
3220 |
|
3221 | forEachMediaGroup(newMaster, function (properties, type, group, label) {
|
3222 | if (properties.playlists && properties.playlists.length) {
|
3223 | var id = properties.playlists[0].id;
|
3224 |
|
3225 | var _playlistUpdate = updateMaster$1(update, properties.playlists[0], dashPlaylistUnchanged);
|
3226 |
|
3227 | if (_playlistUpdate) {
|
3228 | update = _playlistUpdate;
|
3229 |
|
3230 | update.mediaGroups[type][group][label].playlists[0] = update.playlists[id];
|
3231 | noChanges = false;
|
3232 | }
|
3233 | }
|
3234 | });
|
3235 |
|
3236 | if (newMaster.minimumUpdatePeriod !== oldMaster.minimumUpdatePeriod) {
|
3237 | noChanges = false;
|
3238 | }
|
3239 |
|
3240 | if (noChanges) {
|
3241 | return null;
|
3242 | }
|
3243 |
|
3244 | return update;
|
3245 | };
|
3246 |
|
3247 |
|
3248 |
|
3249 |
|
3250 | var equivalentSidx = function equivalentSidx(a, b) {
|
3251 | var neitherMap = Boolean(!a.map && !b.map);
|
3252 | var equivalentMap = neitherMap || Boolean(a.map && b.map && a.map.byterange.offset === b.map.byterange.offset && a.map.byterange.length === b.map.byterange.length);
|
3253 | return equivalentMap && a.uri === b.uri && a.byterange.offset === b.byterange.offset && a.byterange.length === b.byterange.length;
|
3254 | };
|
3255 |
|
3256 |
|
3257 | var compareSidxEntry = function compareSidxEntry(playlists, oldSidxMapping) {
|
3258 | var newSidxMapping = {};
|
3259 |
|
3260 | for (var id in playlists) {
|
3261 | var playlist = playlists[id];
|
3262 | var currentSidxInfo = playlist.sidx;
|
3263 |
|
3264 | if (currentSidxInfo) {
|
3265 | var key = generateSidxKey(currentSidxInfo);
|
3266 |
|
3267 | if (!oldSidxMapping[key]) {
|
3268 | break;
|
3269 | }
|
3270 |
|
3271 | var savedSidxInfo = oldSidxMapping[key].sidxInfo;
|
3272 |
|
3273 | if (equivalentSidx(savedSidxInfo, currentSidxInfo)) {
|
3274 | newSidxMapping[key] = oldSidxMapping[key];
|
3275 | }
|
3276 | }
|
3277 | }
|
3278 |
|
3279 | return newSidxMapping;
|
3280 | };
|
3281 |
|
3282 |
|
3283 |
|
3284 |
|
3285 |
|
3286 |
|
3287 |
|
3288 |
|
3289 |
|
3290 | var filterChangedSidxMappings = function filterChangedSidxMappings(master, oldSidxMapping) {
|
3291 | var videoSidx = compareSidxEntry(master.playlists, oldSidxMapping);
|
3292 | var mediaGroupSidx = videoSidx;
|
3293 | forEachMediaGroup(master, function (properties, mediaType, groupKey, labelKey) {
|
3294 | if (properties.playlists && properties.playlists.length) {
|
3295 | var playlists = properties.playlists;
|
3296 | mediaGroupSidx = mergeOptions(mediaGroupSidx, compareSidxEntry(playlists, oldSidxMapping));
|
3297 | }
|
3298 | });
|
3299 | return mediaGroupSidx;
|
3300 | };
|
3301 |
|
3302 | var DashPlaylistLoader = function (_EventTarget) {
|
3303 | _inheritsLoose(DashPlaylistLoader, _EventTarget);
|
3304 |
|
3305 |
|
3306 |
|
3307 |
|
3308 | function DashPlaylistLoader(srcUrlOrPlaylist, vhs, options, masterPlaylistLoader) {
|
3309 | var _this;
|
3310 |
|
3311 | if (options === void 0) {
|
3312 | options = {};
|
3313 | }
|
3314 |
|
3315 | _this = _EventTarget.call(this) || this;
|
3316 | _this.masterPlaylistLoader_ = masterPlaylistLoader || _assertThisInitialized(_this);
|
3317 |
|
3318 | if (!masterPlaylistLoader) {
|
3319 | _this.isMaster_ = true;
|
3320 | }
|
3321 |
|
3322 | var _options = options,
|
3323 | _options$withCredenti = _options.withCredentials,
|
3324 | withCredentials = _options$withCredenti === void 0 ? false : _options$withCredenti,
|
3325 | _options$handleManife = _options.handleManifestRedirects,
|
3326 | handleManifestRedirects = _options$handleManife === void 0 ? false : _options$handleManife;
|
3327 | _this.vhs_ = vhs;
|
3328 | _this.withCredentials = withCredentials;
|
3329 | _this.handleManifestRedirects = handleManifestRedirects;
|
3330 |
|
3331 | if (!srcUrlOrPlaylist) {
|
3332 | throw new Error('A non-empty playlist URL or object is required');
|
3333 | }
|
3334 |
|
3335 |
|
3336 | _this.on('minimumUpdatePeriod', function () {
|
3337 | _this.refreshXml_();
|
3338 | });
|
3339 |
|
3340 |
|
3341 | _this.on('mediaupdatetimeout', function () {
|
3342 | _this.refreshMedia_(_this.media().id);
|
3343 | });
|
3344 |
|
3345 | _this.state = 'HAVE_NOTHING';
|
3346 | _this.loadedPlaylists_ = {};
|
3347 | _this.logger_ = logger('DashPlaylistLoader');
|
3348 |
|
3349 |
|
3350 | if (_this.isMaster_) {
|
3351 | _this.masterPlaylistLoader_.srcUrl = srcUrlOrPlaylist;
|
3352 |
|
3353 |
|
3354 | _this.masterPlaylistLoader_.sidxMapping_ = {};
|
3355 | } else {
|
3356 | _this.childPlaylist_ = srcUrlOrPlaylist;
|
3357 | }
|
3358 |
|
3359 | return _this;
|
3360 | }
|
3361 |
|
3362 | var _proto = DashPlaylistLoader.prototype;
|
3363 |
|
3364 | _proto.requestErrored_ = function requestErrored_(err, request, startingState) {
|
3365 |
|
3366 | if (!this.request) {
|
3367 | return true;
|
3368 | }
|
3369 |
|
3370 |
|
3371 | this.request = null;
|
3372 |
|
3373 | if (err) {
|
3374 |
|
3375 |
|
3376 | this.error = typeof err === 'object' && !(err instanceof Error) ? err : {
|
3377 | status: request.status,
|
3378 | message: 'DASH request error at URL: ' + request.uri,
|
3379 | response: request.response,
|
3380 |
|
3381 | code: 2
|
3382 | };
|
3383 |
|
3384 | if (startingState) {
|
3385 | this.state = startingState;
|
3386 | }
|
3387 |
|
3388 | this.trigger('error');
|
3389 | return true;
|
3390 | }
|
3391 | }
|
3392 | |
3393 |
|
3394 |
|
3395 |
|
3396 | ;
|
3397 |
|
3398 | _proto.addSidxSegments_ = function addSidxSegments_(playlist, startingState, cb) {
|
3399 | var _this2 = this;
|
3400 |
|
3401 | var sidxKey = playlist.sidx && generateSidxKey(playlist.sidx);
|
3402 |
|
3403 | if (!playlist.sidx || !sidxKey || this.masterPlaylistLoader_.sidxMapping_[sidxKey]) {
|
3404 |
|
3405 | this.mediaRequest_ = window.setTimeout(function () {
|
3406 | return cb(false);
|
3407 | }, 0);
|
3408 | return;
|
3409 | }
|
3410 |
|
3411 |
|
3412 | var uri = resolveManifestRedirect(this.handleManifestRedirects, playlist.sidx.resolvedUri);
|
3413 |
|
3414 | var fin = function fin(err, request) {
|
3415 | if (_this2.requestErrored_(err, request, startingState)) {
|
3416 | return;
|
3417 | }
|
3418 |
|
3419 | var sidxMapping = _this2.masterPlaylistLoader_.sidxMapping_;
|
3420 | var sidx;
|
3421 |
|
3422 | try {
|
3423 | sidx = parseSidx(toUint8(request.response).subarray(8));
|
3424 | } catch (e) {
|
3425 |
|
3426 | _this2.requestErrored_(e, request, startingState);
|
3427 |
|
3428 | return;
|
3429 | }
|
3430 |
|
3431 | sidxMapping[sidxKey] = {
|
3432 | sidxInfo: playlist.sidx,
|
3433 | sidx: sidx
|
3434 | };
|
3435 | addSidxSegmentsToPlaylist(playlist, sidx, playlist.sidx.resolvedUri);
|
3436 | return cb(true);
|
3437 | };
|
3438 |
|
3439 | this.request = containerRequest(uri, this.vhs_.xhr, function (err, request, container, bytes) {
|
3440 | if (err) {
|
3441 | return fin(err, request);
|
3442 | }
|
3443 |
|
3444 | if (!container || container !== 'mp4') {
|
3445 | return fin({
|
3446 | status: request.status,
|
3447 | message: "Unsupported " + (container || 'unknown') + " container type for sidx segment at URL: " + uri,
|
3448 |
|
3449 |
|
3450 | response: '',
|
3451 | playlist: playlist,
|
3452 | internal: true,
|
3453 | blacklistDuration: Infinity,
|
3454 |
|
3455 | code: 2
|
3456 | }, request);
|
3457 | }
|
3458 |
|
3459 |
|
3460 | var _playlist$sidx$bytera = playlist.sidx.byterange,
|
3461 | offset = _playlist$sidx$bytera.offset,
|
3462 | length = _playlist$sidx$bytera.length;
|
3463 |
|
3464 | if (bytes.length >= length + offset) {
|
3465 | return fin(err, {
|
3466 | response: bytes.subarray(offset, offset + length),
|
3467 | status: request.status,
|
3468 | uri: request.uri
|
3469 | });
|
3470 | }
|
3471 |
|
3472 |
|
3473 | _this2.request = _this2.vhs_.xhr({
|
3474 | uri: uri,
|
3475 | responseType: 'arraybuffer',
|
3476 | headers: segmentXhrHeaders({
|
3477 | byterange: playlist.sidx.byterange
|
3478 | })
|
3479 | }, fin);
|
3480 | });
|
3481 | };
|
3482 |
|
3483 | _proto.dispose = function dispose() {
|
3484 | this.trigger('dispose');
|
3485 | this.stopRequest();
|
3486 | this.loadedPlaylists_ = {};
|
3487 | window.clearTimeout(this.minimumUpdatePeriodTimeout_);
|
3488 | window.clearTimeout(this.mediaRequest_);
|
3489 | window.clearTimeout(this.mediaUpdateTimeout);
|
3490 | this.mediaUpdateTimeout = null;
|
3491 | this.mediaRequest_ = null;
|
3492 | this.minimumUpdatePeriodTimeout_ = null;
|
3493 |
|
3494 | if (this.masterPlaylistLoader_.createMupOnMedia_) {
|
3495 | this.off('loadedmetadata', this.masterPlaylistLoader_.createMupOnMedia_);
|
3496 | this.masterPlaylistLoader_.createMupOnMedia_ = null;
|
3497 | }
|
3498 |
|
3499 | this.off();
|
3500 | };
|
3501 |
|
3502 | _proto.hasPendingRequest = function hasPendingRequest() {
|
3503 | return this.request || this.mediaRequest_;
|
3504 | };
|
3505 |
|
3506 | _proto.stopRequest = function stopRequest() {
|
3507 | if (this.request) {
|
3508 | var oldRequest = this.request;
|
3509 | this.request = null;
|
3510 | oldRequest.onreadystatechange = null;
|
3511 | oldRequest.abort();
|
3512 | }
|
3513 | };
|
3514 |
|
3515 | _proto.media = function media(playlist) {
|
3516 | var _this3 = this;
|
3517 |
|
3518 |
|
3519 | if (!playlist) {
|
3520 | return this.media_;
|
3521 | }
|
3522 |
|
3523 |
|
3524 | if (this.state === 'HAVE_NOTHING') {
|
3525 | throw new Error('Cannot switch media playlist from ' + this.state);
|
3526 | }
|
3527 |
|
3528 | var startingState = this.state;
|
3529 |
|
3530 | if (typeof playlist === 'string') {
|
3531 | if (!this.masterPlaylistLoader_.master.playlists[playlist]) {
|
3532 | throw new Error('Unknown playlist URI: ' + playlist);
|
3533 | }
|
3534 |
|
3535 | playlist = this.masterPlaylistLoader_.master.playlists[playlist];
|
3536 | }
|
3537 |
|
3538 | var mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
3539 |
|
3540 | if (mediaChange && this.loadedPlaylists_[playlist.id] && this.loadedPlaylists_[playlist.id].endList) {
|
3541 | this.state = 'HAVE_METADATA';
|
3542 | this.media_ = playlist;
|
3543 |
|
3544 | if (mediaChange) {
|
3545 | this.trigger('mediachanging');
|
3546 | this.trigger('mediachange');
|
3547 | }
|
3548 |
|
3549 | return;
|
3550 | }
|
3551 |
|
3552 |
|
3553 | if (!mediaChange) {
|
3554 | return;
|
3555 | }
|
3556 |
|
3557 |
|
3558 | if (this.media_) {
|
3559 | this.trigger('mediachanging');
|
3560 | }
|
3561 |
|
3562 | this.addSidxSegments_(playlist, startingState, function (sidxChanged) {
|
3563 |
|
3564 | _this3.haveMetadata({
|
3565 | startingState: startingState,
|
3566 | playlist: playlist
|
3567 | });
|
3568 | });
|
3569 | };
|
3570 |
|
3571 | _proto.haveMetadata = function haveMetadata(_ref2) {
|
3572 | var startingState = _ref2.startingState,
|
3573 | playlist = _ref2.playlist;
|
3574 | this.state = 'HAVE_METADATA';
|
3575 | this.loadedPlaylists_[playlist.id] = playlist;
|
3576 | this.mediaRequest_ = null;
|
3577 |
|
3578 | this.refreshMedia_(playlist.id);
|
3579 |
|
3580 |
|
3581 | if (startingState === 'HAVE_MASTER') {
|
3582 | this.trigger('loadedmetadata');
|
3583 | } else {
|
3584 |
|
3585 | this.trigger('mediachange');
|
3586 | }
|
3587 | };
|
3588 |
|
3589 | _proto.pause = function pause() {
|
3590 | if (this.masterPlaylistLoader_.createMupOnMedia_) {
|
3591 | this.off('loadedmetadata', this.masterPlaylistLoader_.createMupOnMedia_);
|
3592 | this.masterPlaylistLoader_.createMupOnMedia_ = null;
|
3593 | }
|
3594 |
|
3595 | this.stopRequest();
|
3596 | window.clearTimeout(this.mediaUpdateTimeout);
|
3597 | this.mediaUpdateTimeout = null;
|
3598 |
|
3599 | if (this.isMaster_) {
|
3600 | window.clearTimeout(this.masterPlaylistLoader_.minimumUpdatePeriodTimeout_);
|
3601 | this.masterPlaylistLoader_.minimumUpdatePeriodTimeout_ = null;
|
3602 | }
|
3603 |
|
3604 | if (this.state === 'HAVE_NOTHING') {
|
3605 |
|
3606 |
|
3607 | this.started = false;
|
3608 | }
|
3609 | };
|
3610 |
|
3611 | _proto.load = function load(isFinalRendition) {
|
3612 | var _this4 = this;
|
3613 |
|
3614 | window.clearTimeout(this.mediaUpdateTimeout);
|
3615 | this.mediaUpdateTimeout = null;
|
3616 | var media = this.media();
|
3617 |
|
3618 | if (isFinalRendition) {
|
3619 | var delay = media ? media.targetDuration / 2 * 1000 : 5 * 1000;
|
3620 | this.mediaUpdateTimeout = window.setTimeout(function () {
|
3621 | return _this4.load();
|
3622 | }, delay);
|
3623 | return;
|
3624 | }
|
3625 |
|
3626 |
|
3627 |
|
3628 | if (!this.started) {
|
3629 | this.start();
|
3630 | return;
|
3631 | }
|
3632 |
|
3633 | if (media && !media.endList) {
|
3634 |
|
3635 |
|
3636 |
|
3637 | if (this.isMaster_ && !this.minimumUpdatePeriodTimeout_) {
|
3638 |
|
3639 | this.trigger('minimumUpdatePeriod');
|
3640 |
|
3641 | this.updateMinimumUpdatePeriodTimeout_();
|
3642 | }
|
3643 |
|
3644 | this.trigger('mediaupdatetimeout');
|
3645 | } else {
|
3646 | this.trigger('loadedplaylist');
|
3647 | }
|
3648 | };
|
3649 |
|
3650 | _proto.start = function start() {
|
3651 | var _this5 = this;
|
3652 |
|
3653 | this.started = true;
|
3654 |
|
3655 |
|
3656 | if (!this.isMaster_) {
|
3657 | this.mediaRequest_ = window.setTimeout(function () {
|
3658 | return _this5.haveMaster_();
|
3659 | }, 0);
|
3660 | return;
|
3661 | }
|
3662 |
|
3663 | this.requestMaster_(function (req, masterChanged) {
|
3664 | _this5.haveMaster_();
|
3665 |
|
3666 | if (!_this5.hasPendingRequest() && !_this5.media_) {
|
3667 | _this5.media(_this5.masterPlaylistLoader_.master.playlists[0]);
|
3668 | }
|
3669 | });
|
3670 | };
|
3671 |
|
3672 | _proto.requestMaster_ = function requestMaster_(cb) {
|
3673 | var _this6 = this;
|
3674 |
|
3675 | this.request = this.vhs_.xhr({
|
3676 | uri: this.masterPlaylistLoader_.srcUrl,
|
3677 | withCredentials: this.withCredentials
|
3678 | }, function (error, req) {
|
3679 | if (_this6.requestErrored_(error, req)) {
|
3680 | if (_this6.state === 'HAVE_NOTHING') {
|
3681 | _this6.started = false;
|
3682 | }
|
3683 |
|
3684 | return;
|
3685 | }
|
3686 |
|
3687 | var masterChanged = req.responseText !== _this6.masterPlaylistLoader_.masterXml_;
|
3688 | _this6.masterPlaylistLoader_.masterXml_ = req.responseText;
|
3689 |
|
3690 | if (req.responseHeaders && req.responseHeaders.date) {
|
3691 | _this6.masterLoaded_ = Date.parse(req.responseHeaders.date);
|
3692 | } else {
|
3693 | _this6.masterLoaded_ = Date.now();
|
3694 | }
|
3695 |
|
3696 | _this6.masterPlaylistLoader_.srcUrl = resolveManifestRedirect(_this6.handleManifestRedirects, _this6.masterPlaylistLoader_.srcUrl, req);
|
3697 |
|
3698 | if (masterChanged) {
|
3699 | _this6.handleMaster_();
|
3700 |
|
3701 | _this6.syncClientServerClock_(function () {
|
3702 | return cb(req, masterChanged);
|
3703 | });
|
3704 |
|
3705 | return;
|
3706 | }
|
3707 |
|
3708 | return cb(req, masterChanged);
|
3709 | });
|
3710 | }
|
3711 | |
3712 |
|
3713 |
|
3714 |
|
3715 |
|
3716 |
|
3717 |
|
3718 | ;
|
3719 |
|
3720 | _proto.syncClientServerClock_ = function syncClientServerClock_(done) {
|
3721 | var _this7 = this;
|
3722 |
|
3723 | var utcTiming = parseUTCTiming(this.masterPlaylistLoader_.masterXml_);
|
3724 |
|
3725 |
|
3726 | if (utcTiming === null) {
|
3727 | this.masterPlaylistLoader_.clientOffset_ = this.masterLoaded_ - Date.now();
|
3728 | return done();
|
3729 | }
|
3730 |
|
3731 | if (utcTiming.method === 'DIRECT') {
|
3732 | this.masterPlaylistLoader_.clientOffset_ = utcTiming.value - Date.now();
|
3733 | return done();
|
3734 | }
|
3735 |
|
3736 | this.request = this.vhs_.xhr({
|
3737 | uri: resolveUrl(this.masterPlaylistLoader_.srcUrl, utcTiming.value),
|
3738 | method: utcTiming.method,
|
3739 | withCredentials: this.withCredentials
|
3740 | }, function (error, req) {
|
3741 |
|
3742 | if (!_this7.request) {
|
3743 | return;
|
3744 | }
|
3745 |
|
3746 | if (error) {
|
3747 |
|
3748 |
|
3749 | _this7.masterPlaylistLoader_.clientOffset_ = _this7.masterLoaded_ - Date.now();
|
3750 | return done();
|
3751 | }
|
3752 |
|
3753 | var serverTime;
|
3754 |
|
3755 | if (utcTiming.method === 'HEAD') {
|
3756 | if (!req.responseHeaders || !req.responseHeaders.date) {
|
3757 |
|
3758 |
|
3759 | serverTime = _this7.masterLoaded_;
|
3760 | } else {
|
3761 | serverTime = Date.parse(req.responseHeaders.date);
|
3762 | }
|
3763 | } else {
|
3764 | serverTime = Date.parse(req.responseText);
|
3765 | }
|
3766 |
|
3767 | _this7.masterPlaylistLoader_.clientOffset_ = serverTime - Date.now();
|
3768 | done();
|
3769 | });
|
3770 | };
|
3771 |
|
3772 | _proto.haveMaster_ = function haveMaster_() {
|
3773 | this.state = 'HAVE_MASTER';
|
3774 |
|
3775 | if (this.isMaster_) {
|
3776 |
|
3777 |
|
3778 |
|
3779 | this.trigger('loadedplaylist');
|
3780 | } else if (!this.media_) {
|
3781 |
|
3782 |
|
3783 | this.media(this.childPlaylist_);
|
3784 | }
|
3785 | };
|
3786 |
|
3787 | _proto.handleMaster_ = function handleMaster_() {
|
3788 |
|
3789 | this.mediaRequest_ = null;
|
3790 | var newMaster = parseMasterXml({
|
3791 | masterXml: this.masterPlaylistLoader_.masterXml_,
|
3792 | srcUrl: this.masterPlaylistLoader_.srcUrl,
|
3793 | clientOffset: this.masterPlaylistLoader_.clientOffset_,
|
3794 | sidxMapping: this.masterPlaylistLoader_.sidxMapping_
|
3795 | });
|
3796 | var oldMaster = this.masterPlaylistLoader_.master;
|
3797 |
|
3798 | if (oldMaster) {
|
3799 | newMaster = updateMaster(oldMaster, newMaster, this.masterPlaylistLoader_.sidxMapping_);
|
3800 | }
|
3801 |
|
3802 |
|
3803 | this.masterPlaylistLoader_.master = newMaster ? newMaster : oldMaster;
|
3804 | var location = this.masterPlaylistLoader_.master.locations && this.masterPlaylistLoader_.master.locations[0];
|
3805 |
|
3806 | if (location && location !== this.masterPlaylistLoader_.srcUrl) {
|
3807 | this.masterPlaylistLoader_.srcUrl = location;
|
3808 | }
|
3809 |
|
3810 | if (!oldMaster || newMaster && newMaster.minimumUpdatePeriod !== oldMaster.minimumUpdatePeriod) {
|
3811 | this.updateMinimumUpdatePeriodTimeout_();
|
3812 | }
|
3813 |
|
3814 | return Boolean(newMaster);
|
3815 | };
|
3816 |
|
3817 | _proto.updateMinimumUpdatePeriodTimeout_ = function updateMinimumUpdatePeriodTimeout_() {
|
3818 | var mpl = this.masterPlaylistLoader_;
|
3819 |
|
3820 |
|
3821 | if (mpl.createMupOnMedia_) {
|
3822 | mpl.off('loadedmetadata', mpl.createMupOnMedia_);
|
3823 | mpl.createMupOnMedia_ = null;
|
3824 | }
|
3825 |
|
3826 |
|
3827 | if (mpl.minimumUpdatePeriodTimeout_) {
|
3828 | window.clearTimeout(mpl.minimumUpdatePeriodTimeout_);
|
3829 | mpl.minimumUpdatePeriodTimeout_ = null;
|
3830 | }
|
3831 |
|
3832 | var mup = mpl.master && mpl.master.minimumUpdatePeriod;
|
3833 |
|
3834 |
|
3835 |
|
3836 |
|
3837 | if (mup === 0) {
|
3838 | if (mpl.media()) {
|
3839 | mup = mpl.media().targetDuration * 1000;
|
3840 | } else {
|
3841 | mpl.createMupOnMedia_ = mpl.updateMinimumUpdatePeriodTimeout_;
|
3842 | mpl.one('loadedmetadata', mpl.createMupOnMedia_);
|
3843 | }
|
3844 | }
|
3845 |
|
3846 |
|
3847 |
|
3848 |
|
3849 | if (typeof mup !== 'number' || mup <= 0) {
|
3850 | if (mup < 0) {
|
3851 | this.logger_("found invalid minimumUpdatePeriod of " + mup + ", not setting a timeout");
|
3852 | }
|
3853 |
|
3854 | return;
|
3855 | }
|
3856 |
|
3857 | this.createMUPTimeout_(mup);
|
3858 | };
|
3859 |
|
3860 | _proto.createMUPTimeout_ = function createMUPTimeout_(mup) {
|
3861 | var mpl = this.masterPlaylistLoader_;
|
3862 | mpl.minimumUpdatePeriodTimeout_ = window.setTimeout(function () {
|
3863 | mpl.minimumUpdatePeriodTimeout_ = null;
|
3864 | mpl.trigger('minimumUpdatePeriod');
|
3865 | mpl.createMUPTimeout_(mup);
|
3866 | }, mup);
|
3867 | }
|
3868 | |
3869 |
|
3870 |
|
3871 | ;
|
3872 |
|
3873 | _proto.refreshXml_ = function refreshXml_() {
|
3874 | var _this8 = this;
|
3875 |
|
3876 | this.requestMaster_(function (req, masterChanged) {
|
3877 | if (!masterChanged) {
|
3878 | return;
|
3879 | }
|
3880 |
|
3881 | if (_this8.media_) {
|
3882 | _this8.media_ = _this8.masterPlaylistLoader_.master.playlists[_this8.media_.id];
|
3883 | }
|
3884 |
|
3885 |
|
3886 | _this8.masterPlaylistLoader_.sidxMapping_ = filterChangedSidxMappings(_this8.masterPlaylistLoader_.master, _this8.masterPlaylistLoader_.sidxMapping_);
|
3887 |
|
3888 | _this8.addSidxSegments_(_this8.media(), _this8.state, function (sidxChanged) {
|
3889 |
|
3890 | _this8.refreshMedia_(_this8.media().id);
|
3891 | });
|
3892 | });
|
3893 | }
|
3894 | |
3895 |
|
3896 |
|
3897 |
|
3898 |
|
3899 | ;
|
3900 |
|
3901 | _proto.refreshMedia_ = function refreshMedia_(mediaID) {
|
3902 | var _this9 = this;
|
3903 |
|
3904 | if (!mediaID) {
|
3905 | throw new Error('refreshMedia_ must take a media id');
|
3906 | }
|
3907 |
|
3908 |
|
3909 |
|
3910 |
|
3911 |
|
3912 |
|
3913 | if (this.media_ && this.isMaster_) {
|
3914 | this.handleMaster_();
|
3915 | }
|
3916 |
|
3917 | var playlists = this.masterPlaylistLoader_.master.playlists;
|
3918 | var mediaChanged = !this.media_ || this.media_ !== playlists[mediaID];
|
3919 |
|
3920 | if (mediaChanged) {
|
3921 | this.media_ = playlists[mediaID];
|
3922 | } else {
|
3923 | this.trigger('playlistunchanged');
|
3924 | }
|
3925 |
|
3926 | if (!this.mediaUpdateTimeout) {
|
3927 | var createMediaUpdateTimeout = function createMediaUpdateTimeout() {
|
3928 | if (_this9.media().endList) {
|
3929 | return;
|
3930 | }
|
3931 |
|
3932 | _this9.mediaUpdateTimeout = window.setTimeout(function () {
|
3933 | _this9.trigger('mediaupdatetimeout');
|
3934 |
|
3935 | createMediaUpdateTimeout();
|
3936 | }, refreshDelay(_this9.media(), Boolean(mediaChanged)));
|
3937 | };
|
3938 |
|
3939 | createMediaUpdateTimeout();
|
3940 | }
|
3941 |
|
3942 | this.trigger('loadedplaylist');
|
3943 | };
|
3944 |
|
3945 | return DashPlaylistLoader;
|
3946 | }(EventTarget);
|
3947 |
|
3948 | var Config = {
|
3949 | GOAL_BUFFER_LENGTH: 30,
|
3950 | MAX_GOAL_BUFFER_LENGTH: 60,
|
3951 | BACK_BUFFER_LENGTH: 30,
|
3952 | GOAL_BUFFER_LENGTH_RATE: 1,
|
3953 |
|
3954 | INITIAL_BANDWIDTH: 4194304,
|
3955 |
|
3956 |
|
3957 | BANDWIDTH_VARIANCE: 1.2,
|
3958 |
|
3959 | BUFFER_LOW_WATER_LINE: 0,
|
3960 | MAX_BUFFER_LOW_WATER_LINE: 30,
|
3961 |
|
3962 | EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE: 16,
|
3963 | BUFFER_LOW_WATER_LINE_RATE: 1,
|
3964 |
|
3965 | BUFFER_HIGH_WATER_LINE: 30
|
3966 | };
|
3967 |
|
3968 | var stringToArrayBuffer = function stringToArrayBuffer(string) {
|
3969 | var view = new Uint8Array(new ArrayBuffer(string.length));
|
3970 |
|
3971 | for (var i = 0; i < string.length; i++) {
|
3972 | view[i] = string.charCodeAt(i);
|
3973 | }
|
3974 |
|
3975 | return view.buffer;
|
3976 | };
|
3977 |
|
3978 |
|
3979 |
|
3980 | var browserWorkerPolyFill = function browserWorkerPolyFill(workerObj) {
|
3981 |
|
3982 | workerObj.on = workerObj.addEventListener;
|
3983 | workerObj.off = workerObj.removeEventListener;
|
3984 | return workerObj;
|
3985 | };
|
3986 |
|
3987 | var createObjectURL = function createObjectURL(str) {
|
3988 | try {
|
3989 | return URL.createObjectURL(new Blob([str], {
|
3990 | type: 'application/javascript'
|
3991 | }));
|
3992 | } catch (e) {
|
3993 | var blob = new BlobBuilder();
|
3994 | blob.append(str);
|
3995 | return URL.createObjectURL(blob.getBlob());
|
3996 | }
|
3997 | };
|
3998 |
|
3999 | var factory = function factory(code) {
|
4000 | return function () {
|
4001 | var objectUrl = createObjectURL(code);
|
4002 | var worker = browserWorkerPolyFill(new Worker(objectUrl));
|
4003 | worker.objURL = objectUrl;
|
4004 | var terminate = worker.terminate;
|
4005 | worker.on = worker.addEventListener;
|
4006 | worker.off = worker.removeEventListener;
|
4007 |
|
4008 | worker.terminate = function () {
|
4009 | URL.revokeObjectURL(objectUrl);
|
4010 | return terminate.call(this);
|
4011 | };
|
4012 |
|
4013 | return worker;
|
4014 | };
|
4015 | };
|
4016 | var transform = function transform(code) {
|
4017 | return "var browserWorkerPolyFill = " + browserWorkerPolyFill.toString() + ";\n" + 'browserWorkerPolyFill(self);\n' + code;
|
4018 | };
|
4019 |
|
4020 | var getWorkerString = function getWorkerString(fn) {
|
4021 | return fn.toString().replace(/^function.+?{/, '').slice(0, -1);
|
4022 | };
|
4023 |
|
4024 |
|
4025 | var workerCode$1 = transform(getWorkerString(function () {
|
4026 | |
4027 |
|
4028 |
|
4029 |
|
4030 |
|
4031 |
|
4032 |
|
4033 |
|
4034 |
|
4035 |
|
4036 | var Stream = function Stream() {
|
4037 | this.init = function () {
|
4038 | var listeners = {};
|
4039 | |
4040 |
|
4041 |
|
4042 |
|
4043 |
|
4044 |
|
4045 |
|
4046 | this.on = function (type, listener) {
|
4047 | if (!listeners[type]) {
|
4048 | listeners[type] = [];
|
4049 | }
|
4050 |
|
4051 | listeners[type] = listeners[type].concat(listener);
|
4052 | };
|
4053 | |
4054 |
|
4055 |
|
4056 |
|
4057 |
|
4058 |
|
4059 |
|
4060 |
|
4061 | this.off = function (type, listener) {
|
4062 | var index;
|
4063 |
|
4064 | if (!listeners[type]) {
|
4065 | return false;
|
4066 | }
|
4067 |
|
4068 | index = listeners[type].indexOf(listener);
|
4069 | listeners[type] = listeners[type].slice();
|
4070 | listeners[type].splice(index, 1);
|
4071 | return index > -1;
|
4072 | };
|
4073 | |
4074 |
|
4075 |
|
4076 |
|
4077 |
|
4078 |
|
4079 |
|
4080 | this.trigger = function (type) {
|
4081 | var callbacks, i, length, args;
|
4082 | callbacks = listeners[type];
|
4083 |
|
4084 | if (!callbacks) {
|
4085 | return;
|
4086 | }
|
4087 |
|
4088 |
|
4089 |
|
4090 |
|
4091 |
|
4092 | if (arguments.length === 2) {
|
4093 | length = callbacks.length;
|
4094 |
|
4095 | for (i = 0; i < length; ++i) {
|
4096 | callbacks[i].call(this, arguments[1]);
|
4097 | }
|
4098 | } else {
|
4099 | args = [];
|
4100 | i = arguments.length;
|
4101 |
|
4102 | for (i = 1; i < arguments.length; ++i) {
|
4103 | args.push(arguments[i]);
|
4104 | }
|
4105 |
|
4106 | length = callbacks.length;
|
4107 |
|
4108 | for (i = 0; i < length; ++i) {
|
4109 | callbacks[i].apply(this, args);
|
4110 | }
|
4111 | }
|
4112 | };
|
4113 | |
4114 |
|
4115 |
|
4116 |
|
4117 |
|
4118 | this.dispose = function () {
|
4119 | listeners = {};
|
4120 | };
|
4121 | };
|
4122 | };
|
4123 | |
4124 |
|
4125 |
|
4126 |
|
4127 |
|
4128 |
|
4129 |
|
4130 |
|
4131 |
|
4132 |
|
4133 |
|
4134 | Stream.prototype.pipe = function (destination) {
|
4135 | this.on('data', function (data) {
|
4136 | destination.push(data);
|
4137 | });
|
4138 | this.on('done', function (flushSource) {
|
4139 | destination.flush(flushSource);
|
4140 | });
|
4141 | this.on('partialdone', function (flushSource) {
|
4142 | destination.partialFlush(flushSource);
|
4143 | });
|
4144 | this.on('endedtimeline', function (flushSource) {
|
4145 | destination.endTimeline(flushSource);
|
4146 | });
|
4147 | this.on('reset', function (flushSource) {
|
4148 | destination.reset(flushSource);
|
4149 | });
|
4150 | return destination;
|
4151 | };
|
4152 |
|
4153 |
|
4154 |
|
4155 |
|
4156 |
|
4157 | Stream.prototype.push = function (data) {
|
4158 | this.trigger('data', data);
|
4159 | };
|
4160 |
|
4161 | Stream.prototype.flush = function (flushSource) {
|
4162 | this.trigger('done', flushSource);
|
4163 | };
|
4164 |
|
4165 | Stream.prototype.partialFlush = function (flushSource) {
|
4166 | this.trigger('partialdone', flushSource);
|
4167 | };
|
4168 |
|
4169 | Stream.prototype.endTimeline = function (flushSource) {
|
4170 | this.trigger('endedtimeline', flushSource);
|
4171 | };
|
4172 |
|
4173 | Stream.prototype.reset = function (flushSource) {
|
4174 | this.trigger('reset', flushSource);
|
4175 | };
|
4176 |
|
4177 | var stream = Stream;
|
4178 | |
4179 |
|
4180 |
|
4181 |
|
4182 |
|
4183 |
|
4184 |
|
4185 |
|
4186 |
|
4187 |
|
4188 | var UINT32_MAX = Math.pow(2, 32) - 1;
|
4189 | var box, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, sdtp, stbl, stsd, traf, trex, trun$1, types, MAJOR_BRAND, MINOR_VERSION, AVC1_BRAND, VIDEO_HDLR, AUDIO_HDLR, HDLR_TYPES, VMHD, SMHD, DREF, STCO, STSC, STSZ, STTS;
|
4190 |
|
4191 | (function () {
|
4192 | var i;
|
4193 | types = {
|
4194 | avc1: [],
|
4195 |
|
4196 | avcC: [],
|
4197 | btrt: [],
|
4198 | dinf: [],
|
4199 | dref: [],
|
4200 | esds: [],
|
4201 | ftyp: [],
|
4202 | hdlr: [],
|
4203 | mdat: [],
|
4204 | mdhd: [],
|
4205 | mdia: [],
|
4206 | mfhd: [],
|
4207 | minf: [],
|
4208 | moof: [],
|
4209 | moov: [],
|
4210 | mp4a: [],
|
4211 |
|
4212 | mvex: [],
|
4213 | mvhd: [],
|
4214 | pasp: [],
|
4215 | sdtp: [],
|
4216 | smhd: [],
|
4217 | stbl: [],
|
4218 | stco: [],
|
4219 | stsc: [],
|
4220 | stsd: [],
|
4221 | stsz: [],
|
4222 | stts: [],
|
4223 | styp: [],
|
4224 | tfdt: [],
|
4225 | tfhd: [],
|
4226 | traf: [],
|
4227 | trak: [],
|
4228 | trun: [],
|
4229 | trex: [],
|
4230 | tkhd: [],
|
4231 | vmhd: []
|
4232 | };
|
4233 |
|
4234 |
|
4235 | if (typeof Uint8Array === 'undefined') {
|
4236 | return;
|
4237 | }
|
4238 |
|
4239 | for (i in types) {
|
4240 | if (types.hasOwnProperty(i)) {
|
4241 | types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
|
4242 | }
|
4243 | }
|
4244 |
|
4245 | MAJOR_BRAND = new Uint8Array(['i'.charCodeAt(0), 's'.charCodeAt(0), 'o'.charCodeAt(0), 'm'.charCodeAt(0)]);
|
4246 | AVC1_BRAND = new Uint8Array(['a'.charCodeAt(0), 'v'.charCodeAt(0), 'c'.charCodeAt(0), '1'.charCodeAt(0)]);
|
4247 | MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
|
4248 | VIDEO_HDLR = new Uint8Array([0x00,
|
4249 | 0x00, 0x00, 0x00,
|
4250 | 0x00, 0x00, 0x00, 0x00,
|
4251 | 0x76, 0x69, 0x64, 0x65,
|
4252 | 0x00, 0x00, 0x00, 0x00,
|
4253 | 0x00, 0x00, 0x00, 0x00,
|
4254 | 0x00, 0x00, 0x00, 0x00,
|
4255 | 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00
|
4256 | ]);
|
4257 | AUDIO_HDLR = new Uint8Array([0x00,
|
4258 | 0x00, 0x00, 0x00,
|
4259 | 0x00, 0x00, 0x00, 0x00,
|
4260 | 0x73, 0x6f, 0x75, 0x6e,
|
4261 | 0x00, 0x00, 0x00, 0x00,
|
4262 | 0x00, 0x00, 0x00, 0x00,
|
4263 | 0x00, 0x00, 0x00, 0x00,
|
4264 | 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00
|
4265 | ]);
|
4266 | HDLR_TYPES = {
|
4267 | video: VIDEO_HDLR,
|
4268 | audio: AUDIO_HDLR
|
4269 | };
|
4270 | DREF = new Uint8Array([0x00,
|
4271 | 0x00, 0x00, 0x00,
|
4272 | 0x00, 0x00, 0x00, 0x01,
|
4273 | 0x00, 0x00, 0x00, 0x0c,
|
4274 | 0x75, 0x72, 0x6c, 0x20,
|
4275 | 0x00,
|
4276 | 0x00, 0x00, 0x01
|
4277 | ]);
|
4278 | SMHD = new Uint8Array([0x00,
|
4279 | 0x00, 0x00, 0x00,
|
4280 | 0x00, 0x00,
|
4281 | 0x00, 0x00
|
4282 | ]);
|
4283 | STCO = new Uint8Array([0x00,
|
4284 | 0x00, 0x00, 0x00,
|
4285 | 0x00, 0x00, 0x00, 0x00
|
4286 | ]);
|
4287 | STSC = STCO;
|
4288 | STSZ = new Uint8Array([0x00,
|
4289 | 0x00, 0x00, 0x00,
|
4290 | 0x00, 0x00, 0x00, 0x00,
|
4291 | 0x00, 0x00, 0x00, 0x00
|
4292 | ]);
|
4293 | STTS = STCO;
|
4294 | VMHD = new Uint8Array([0x00,
|
4295 | 0x00, 0x00, 0x01,
|
4296 | 0x00, 0x00,
|
4297 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
4298 | ]);
|
4299 | })();
|
4300 |
|
4301 | box = function box(type) {
|
4302 | var payload = [],
|
4303 | size = 0,
|
4304 | i,
|
4305 | result,
|
4306 | view;
|
4307 |
|
4308 | for (i = 1; i < arguments.length; i++) {
|
4309 | payload.push(arguments[i]);
|
4310 | }
|
4311 |
|
4312 | i = payload.length;
|
4313 |
|
4314 | while (i--) {
|
4315 | size += payload[i].byteLength;
|
4316 | }
|
4317 |
|
4318 | result = new Uint8Array(size + 8);
|
4319 | view = new DataView(result.buffer, result.byteOffset, result.byteLength);
|
4320 | view.setUint32(0, result.byteLength);
|
4321 | result.set(type, 4);
|
4322 |
|
4323 | for (i = 0, size = 8; i < payload.length; i++) {
|
4324 | result.set(payload[i], size);
|
4325 | size += payload[i].byteLength;
|
4326 | }
|
4327 |
|
4328 | return result;
|
4329 | };
|
4330 |
|
4331 | dinf = function dinf() {
|
4332 | return box(types.dinf, box(types.dref, DREF));
|
4333 | };
|
4334 |
|
4335 | esds = function esds(track) {
|
4336 | return box(types.esds, new Uint8Array([0x00,
|
4337 | 0x00, 0x00, 0x00,
|
4338 |
|
4339 | 0x03,
|
4340 | 0x19,
|
4341 | 0x00, 0x00,
|
4342 | 0x00,
|
4343 |
|
4344 | 0x04,
|
4345 | 0x11,
|
4346 | 0x40,
|
4347 | 0x15,
|
4348 | 0x00, 0x06, 0x00,
|
4349 | 0x00, 0x00, 0xda, 0xc0,
|
4350 | 0x00, 0x00, 0xda, 0xc0,
|
4351 |
|
4352 | 0x05,
|
4353 | 0x02,
|
4354 |
|
4355 |
|
4356 | track.audioobjecttype << 3 | track.samplingfrequencyindex >>> 1, track.samplingfrequencyindex << 7 | track.channelcount << 3, 0x06, 0x01, 0x02
|
4357 | ]));
|
4358 | };
|
4359 |
|
4360 | ftyp = function ftyp() {
|
4361 | return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
|
4362 | };
|
4363 |
|
4364 | hdlr = function hdlr(type) {
|
4365 | return box(types.hdlr, HDLR_TYPES[type]);
|
4366 | };
|
4367 |
|
4368 | mdat = function mdat(data) {
|
4369 | return box(types.mdat, data);
|
4370 | };
|
4371 |
|
4372 | mdhd = function mdhd(track) {
|
4373 | var result = new Uint8Array([0x00,
|
4374 | 0x00, 0x00, 0x00,
|
4375 | 0x00, 0x00, 0x00, 0x02,
|
4376 | 0x00, 0x00, 0x00, 0x03,
|
4377 | 0x00, 0x01, 0x5f, 0x90,
|
4378 | track.duration >>> 24 & 0xFF, track.duration >>> 16 & 0xFF, track.duration >>> 8 & 0xFF, track.duration & 0xFF,
|
4379 | 0x55, 0xc4,
|
4380 | 0x00, 0x00]);
|
4381 |
|
4382 |
|
4383 |
|
4384 | if (track.samplerate) {
|
4385 | result[12] = track.samplerate >>> 24 & 0xFF;
|
4386 | result[13] = track.samplerate >>> 16 & 0xFF;
|
4387 | result[14] = track.samplerate >>> 8 & 0xFF;
|
4388 | result[15] = track.samplerate & 0xFF;
|
4389 | }
|
4390 |
|
4391 | return box(types.mdhd, result);
|
4392 | };
|
4393 |
|
4394 | mdia = function mdia(track) {
|
4395 | return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
|
4396 | };
|
4397 |
|
4398 | mfhd = function mfhd(sequenceNumber) {
|
4399 | return box(types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
|
4400 | (sequenceNumber & 0xFF000000) >> 24, (sequenceNumber & 0xFF0000) >> 16, (sequenceNumber & 0xFF00) >> 8, sequenceNumber & 0xFF
|
4401 | ]));
|
4402 | };
|
4403 |
|
4404 | minf = function minf(track) {
|
4405 | return box(types.minf, track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD), dinf(), stbl(track));
|
4406 | };
|
4407 |
|
4408 | moof = function moof(sequenceNumber, tracks) {
|
4409 | var trackFragments = [],
|
4410 | i = tracks.length;
|
4411 |
|
4412 | while (i--) {
|
4413 | trackFragments[i] = traf(tracks[i]);
|
4414 | }
|
4415 |
|
4416 | return box.apply(null, [types.moof, mfhd(sequenceNumber)].concat(trackFragments));
|
4417 | };
|
4418 | |
4419 |
|
4420 |
|
4421 |
|
4422 |
|
4423 |
|
4424 |
|
4425 | moov = function moov(tracks) {
|
4426 | var i = tracks.length,
|
4427 | boxes = [];
|
4428 |
|
4429 | while (i--) {
|
4430 | boxes[i] = trak(tracks[i]);
|
4431 | }
|
4432 |
|
4433 | return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
|
4434 | };
|
4435 |
|
4436 | mvex = function mvex(tracks) {
|
4437 | var i = tracks.length,
|
4438 | boxes = [];
|
4439 |
|
4440 | while (i--) {
|
4441 | boxes[i] = trex(tracks[i]);
|
4442 | }
|
4443 |
|
4444 | return box.apply(null, [types.mvex].concat(boxes));
|
4445 | };
|
4446 |
|
4447 | mvhd = function mvhd(duration) {
|
4448 | var bytes = new Uint8Array([0x00,
|
4449 | 0x00, 0x00, 0x00,
|
4450 | 0x00, 0x00, 0x00, 0x01,
|
4451 | 0x00, 0x00, 0x00, 0x02,
|
4452 | 0x00, 0x01, 0x5f, 0x90,
|
4453 | (duration & 0xFF000000) >> 24, (duration & 0xFF0000) >> 16, (duration & 0xFF00) >> 8, duration & 0xFF,
|
4454 | 0x00, 0x01, 0x00, 0x00,
|
4455 | 0x01, 0x00,
|
4456 | 0x00, 0x00,
|
4457 | 0x00, 0x00, 0x00, 0x00,
|
4458 | 0x00, 0x00, 0x00, 0x00,
|
4459 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
4460 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
4461 | 0xff, 0xff, 0xff, 0xff
|
4462 | ]);
|
4463 | return box(types.mvhd, bytes);
|
4464 | };
|
4465 |
|
4466 | sdtp = function sdtp(track) {
|
4467 | var samples = track.samples || [],
|
4468 | bytes = new Uint8Array(4 + samples.length),
|
4469 | flags,
|
4470 | i;
|
4471 |
|
4472 |
|
4473 | for (i = 0; i < samples.length; i++) {
|
4474 | flags = samples[i].flags;
|
4475 | bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
|
4476 | }
|
4477 |
|
4478 | return box(types.sdtp, bytes);
|
4479 | };
|
4480 |
|
4481 | stbl = function stbl(track) {
|
4482 | return box(types.stbl, stsd(track), box(types.stts, STTS), box(types.stsc, STSC), box(types.stsz, STSZ), box(types.stco, STCO));
|
4483 | };
|
4484 |
|
4485 | (function () {
|
4486 | var videoSample, audioSample;
|
4487 |
|
4488 | stsd = function stsd(track) {
|
4489 | return box(types.stsd, new Uint8Array([0x00,
|
4490 | 0x00, 0x00, 0x00,
|
4491 | 0x00, 0x00, 0x00, 0x01]), track.type === 'video' ? videoSample(track) : audioSample(track));
|
4492 | };
|
4493 |
|
4494 | videoSample = function videoSample(track) {
|
4495 | var sps = track.sps || [],
|
4496 | pps = track.pps || [],
|
4497 | sequenceParameterSets = [],
|
4498 | pictureParameterSets = [],
|
4499 | i,
|
4500 | avc1Box;
|
4501 |
|
4502 | for (i = 0; i < sps.length; i++) {
|
4503 | sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
|
4504 | sequenceParameterSets.push(sps[i].byteLength & 0xFF);
|
4505 |
|
4506 | sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i]));
|
4507 | }
|
4508 |
|
4509 |
|
4510 | for (i = 0; i < pps.length; i++) {
|
4511 | pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
|
4512 | pictureParameterSets.push(pps[i].byteLength & 0xFF);
|
4513 | pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
|
4514 | }
|
4515 |
|
4516 | avc1Box = [types.avc1, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
4517 | 0x00, 0x01,
|
4518 | 0x00, 0x00,
|
4519 | 0x00, 0x00,
|
4520 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
4521 | (track.width & 0xff00) >> 8, track.width & 0xff,
|
4522 | (track.height & 0xff00) >> 8, track.height & 0xff,
|
4523 | 0x00, 0x48, 0x00, 0x00,
|
4524 | 0x00, 0x48, 0x00, 0x00,
|
4525 | 0x00, 0x00, 0x00, 0x00,
|
4526 | 0x00, 0x01,
|
4527 | 0x13, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x6a, 0x73, 0x2d, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x69, 0x62, 0x2d, 0x68, 0x6c, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
4528 | 0x00, 0x18,
|
4529 | 0x11, 0x11
|
4530 | ]), box(types.avcC, new Uint8Array([0x01,
|
4531 | track.profileIdc,
|
4532 | track.profileCompatibility,
|
4533 | track.levelIdc,
|
4534 | 0xff
|
4535 | ].concat([sps.length],
|
4536 | sequenceParameterSets,
|
4537 | [pps.length],
|
4538 | pictureParameterSets
|
4539 | ))), box(types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
|
4540 | 0x00, 0x2d, 0xc6, 0xc0,
|
4541 | 0x00, 0x2d, 0xc6, 0xc0
|
4542 | ]))];
|
4543 |
|
4544 | if (track.sarRatio) {
|
4545 | var hSpacing = track.sarRatio[0],
|
4546 | vSpacing = track.sarRatio[1];
|
4547 | avc1Box.push(box(types.pasp, new Uint8Array([(hSpacing & 0xFF000000) >> 24, (hSpacing & 0xFF0000) >> 16, (hSpacing & 0xFF00) >> 8, hSpacing & 0xFF, (vSpacing & 0xFF000000) >> 24, (vSpacing & 0xFF0000) >> 16, (vSpacing & 0xFF00) >> 8, vSpacing & 0xFF])));
|
4548 | }
|
4549 |
|
4550 | return box.apply(null, avc1Box);
|
4551 | };
|
4552 |
|
4553 | audioSample = function audioSample(track) {
|
4554 | return box(types.mp4a, new Uint8Array([
|
4555 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
4556 | 0x00, 0x01,
|
4557 |
|
4558 | 0x00, 0x00, 0x00, 0x00,
|
4559 | 0x00, 0x00, 0x00, 0x00,
|
4560 | (track.channelcount & 0xff00) >> 8, track.channelcount & 0xff,
|
4561 | (track.samplesize & 0xff00) >> 8, track.samplesize & 0xff,
|
4562 | 0x00, 0x00,
|
4563 | 0x00, 0x00,
|
4564 | (track.samplerate & 0xff00) >> 8, track.samplerate & 0xff, 0x00, 0x00
|
4565 |
|
4566 | ]), esds(track));
|
4567 | };
|
4568 | })();
|
4569 |
|
4570 | tkhd = function tkhd(track) {
|
4571 | var result = new Uint8Array([0x00,
|
4572 | 0x00, 0x00, 0x07,
|
4573 | 0x00, 0x00, 0x00, 0x00,
|
4574 | 0x00, 0x00, 0x00, 0x00,
|
4575 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
4576 | 0x00, 0x00, 0x00, 0x00,
|
4577 | (track.duration & 0xFF000000) >> 24, (track.duration & 0xFF0000) >> 16, (track.duration & 0xFF00) >> 8, track.duration & 0xFF,
|
4578 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
4579 | 0x00, 0x00,
|
4580 | 0x00, 0x00,
|
4581 | 0x01, 0x00,
|
4582 | 0x00, 0x00,
|
4583 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
4584 | (track.width & 0xFF00) >> 8, track.width & 0xFF, 0x00, 0x00,
|
4585 | (track.height & 0xFF00) >> 8, track.height & 0xFF, 0x00, 0x00
|
4586 | ]);
|
4587 | return box(types.tkhd, result);
|
4588 | };
|
4589 | |
4590 |
|
4591 |
|
4592 |
|
4593 |
|
4594 |
|
4595 | traf = function traf(track) {
|
4596 | var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable, dataOffset, upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
|
4597 | trackFragmentHeader = box(types.tfhd, new Uint8Array([0x00,
|
4598 | 0x00, 0x00, 0x3a,
|
4599 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
4600 | 0x00, 0x00, 0x00, 0x01,
|
4601 | 0x00, 0x00, 0x00, 0x00,
|
4602 | 0x00, 0x00, 0x00, 0x00,
|
4603 | 0x00, 0x00, 0x00, 0x00
|
4604 | ]));
|
4605 | upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / (UINT32_MAX + 1));
|
4606 | lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % (UINT32_MAX + 1));
|
4607 | trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([0x01,
|
4608 | 0x00, 0x00, 0x00,
|
4609 |
|
4610 | upperWordBaseMediaDecodeTime >>> 24 & 0xFF, upperWordBaseMediaDecodeTime >>> 16 & 0xFF, upperWordBaseMediaDecodeTime >>> 8 & 0xFF, upperWordBaseMediaDecodeTime & 0xFF, lowerWordBaseMediaDecodeTime >>> 24 & 0xFF, lowerWordBaseMediaDecodeTime >>> 16 & 0xFF, lowerWordBaseMediaDecodeTime >>> 8 & 0xFF, lowerWordBaseMediaDecodeTime & 0xFF]));
|
4611 |
|
4612 |
|
4613 |
|
4614 | dataOffset = 32 +
|
4615 | 20 +
|
4616 | 8 +
|
4617 | 16 +
|
4618 | 8 +
|
4619 | 8;
|
4620 |
|
4621 |
|
4622 | if (track.type === 'audio') {
|
4623 | trackFragmentRun = trun$1(track, dataOffset);
|
4624 | return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun);
|
4625 | }
|
4626 |
|
4627 |
|
4628 |
|
4629 |
|
4630 | sampleDependencyTable = sdtp(track);
|
4631 | trackFragmentRun = trun$1(track, sampleDependencyTable.length + dataOffset);
|
4632 | return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable);
|
4633 | };
|
4634 | |
4635 |
|
4636 |
|
4637 |
|
4638 |
|
4639 |
|
4640 |
|
4641 | trak = function trak(track) {
|
4642 | track.duration = track.duration || 0xffffffff;
|
4643 | return box(types.trak, tkhd(track), mdia(track));
|
4644 | };
|
4645 |
|
4646 | trex = function trex(track) {
|
4647 | var result = new Uint8Array([0x00,
|
4648 | 0x00, 0x00, 0x00,
|
4649 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
4650 | 0x00, 0x00, 0x00, 0x01,
|
4651 | 0x00, 0x00, 0x00, 0x00,
|
4652 | 0x00, 0x00, 0x00, 0x00,
|
4653 | 0x00, 0x01, 0x00, 0x01
|
4654 | ]);
|
4655 |
|
4656 |
|
4657 |
|
4658 |
|
4659 | if (track.type !== 'video') {
|
4660 | result[result.length - 1] = 0x00;
|
4661 | }
|
4662 |
|
4663 | return box(types.trex, result);
|
4664 | };
|
4665 |
|
4666 | (function () {
|
4667 | var audioTrun, videoTrun, trunHeader;
|
4668 |
|
4669 |
|
4670 |
|
4671 |
|
4672 | trunHeader = function trunHeader(samples, offset) {
|
4673 | var durationPresent = 0,
|
4674 | sizePresent = 0,
|
4675 | flagsPresent = 0,
|
4676 | compositionTimeOffset = 0;
|
4677 |
|
4678 | if (samples.length) {
|
4679 | if (samples[0].duration !== undefined) {
|
4680 | durationPresent = 0x1;
|
4681 | }
|
4682 |
|
4683 | if (samples[0].size !== undefined) {
|
4684 | sizePresent = 0x2;
|
4685 | }
|
4686 |
|
4687 | if (samples[0].flags !== undefined) {
|
4688 | flagsPresent = 0x4;
|
4689 | }
|
4690 |
|
4691 | if (samples[0].compositionTimeOffset !== undefined) {
|
4692 | compositionTimeOffset = 0x8;
|
4693 | }
|
4694 | }
|
4695 |
|
4696 | return [0x00,
|
4697 | 0x00, durationPresent | sizePresent | flagsPresent | compositionTimeOffset, 0x01,
|
4698 | (samples.length & 0xFF000000) >>> 24, (samples.length & 0xFF0000) >>> 16, (samples.length & 0xFF00) >>> 8, samples.length & 0xFF,
|
4699 | (offset & 0xFF000000) >>> 24, (offset & 0xFF0000) >>> 16, (offset & 0xFF00) >>> 8, offset & 0xFF
|
4700 | ];
|
4701 | };
|
4702 |
|
4703 | videoTrun = function videoTrun(track, offset) {
|
4704 | var bytesOffest, bytes, header, samples, sample, i;
|
4705 | samples = track.samples || [];
|
4706 | offset += 8 + 12 + 16 * samples.length;
|
4707 | header = trunHeader(samples, offset);
|
4708 | bytes = new Uint8Array(header.length + samples.length * 16);
|
4709 | bytes.set(header);
|
4710 | bytesOffest = header.length;
|
4711 |
|
4712 | for (i = 0; i < samples.length; i++) {
|
4713 | sample = samples[i];
|
4714 | bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
|
4715 | bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
|
4716 | bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
|
4717 | bytes[bytesOffest++] = sample.duration & 0xFF;
|
4718 |
|
4719 | bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
|
4720 | bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
|
4721 | bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
|
4722 | bytes[bytesOffest++] = sample.size & 0xFF;
|
4723 |
|
4724 | bytes[bytesOffest++] = sample.flags.isLeading << 2 | sample.flags.dependsOn;
|
4725 | bytes[bytesOffest++] = sample.flags.isDependedOn << 6 | sample.flags.hasRedundancy << 4 | sample.flags.paddingValue << 1 | sample.flags.isNonSyncSample;
|
4726 | bytes[bytesOffest++] = sample.flags.degradationPriority & 0xF0 << 8;
|
4727 | bytes[bytesOffest++] = sample.flags.degradationPriority & 0x0F;
|
4728 |
|
4729 | bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF000000) >>> 24;
|
4730 | bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF0000) >>> 16;
|
4731 | bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF00) >>> 8;
|
4732 | bytes[bytesOffest++] = sample.compositionTimeOffset & 0xFF;
|
4733 | }
|
4734 |
|
4735 | return box(types.trun, bytes);
|
4736 | };
|
4737 |
|
4738 | audioTrun = function audioTrun(track, offset) {
|
4739 | var bytes, bytesOffest, header, samples, sample, i;
|
4740 | samples = track.samples || [];
|
4741 | offset += 8 + 12 + 8 * samples.length;
|
4742 | header = trunHeader(samples, offset);
|
4743 | bytes = new Uint8Array(header.length + samples.length * 8);
|
4744 | bytes.set(header);
|
4745 | bytesOffest = header.length;
|
4746 |
|
4747 | for (i = 0; i < samples.length; i++) {
|
4748 | sample = samples[i];
|
4749 | bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
|
4750 | bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
|
4751 | bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
|
4752 | bytes[bytesOffest++] = sample.duration & 0xFF;
|
4753 |
|
4754 | bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
|
4755 | bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
|
4756 | bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
|
4757 | bytes[bytesOffest++] = sample.size & 0xFF;
|
4758 | }
|
4759 |
|
4760 | return box(types.trun, bytes);
|
4761 | };
|
4762 |
|
4763 | trun$1 = function trun(track, offset) {
|
4764 | if (track.type === 'audio') {
|
4765 | return audioTrun(track, offset);
|
4766 | }
|
4767 |
|
4768 | return videoTrun(track, offset);
|
4769 | };
|
4770 | })();
|
4771 |
|
4772 | var mp4Generator = {
|
4773 | ftyp: ftyp,
|
4774 | mdat: mdat,
|
4775 | moof: moof,
|
4776 | moov: moov,
|
4777 | initSegment: function initSegment(tracks) {
|
4778 | var fileType = ftyp(),
|
4779 | movie = moov(tracks),
|
4780 | result;
|
4781 | result = new Uint8Array(fileType.byteLength + movie.byteLength);
|
4782 | result.set(fileType);
|
4783 | result.set(movie, fileType.byteLength);
|
4784 | return result;
|
4785 | }
|
4786 | };
|
4787 | |
4788 |
|
4789 |
|
4790 |
|
4791 |
|
4792 |
|
4793 |
|
4794 |
|
4795 |
|
4796 |
|
4797 |
|
4798 | var groupNalsIntoFrames = function groupNalsIntoFrames(nalUnits) {
|
4799 | var i,
|
4800 | currentNal,
|
4801 | currentFrame = [],
|
4802 | frames = [];
|
4803 |
|
4804 | frames.byteLength = 0;
|
4805 | frames.nalCount = 0;
|
4806 | frames.duration = 0;
|
4807 | currentFrame.byteLength = 0;
|
4808 |
|
4809 | for (i = 0; i < nalUnits.length; i++) {
|
4810 | currentNal = nalUnits[i];
|
4811 |
|
4812 | if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
|
4813 |
|
4814 |
|
4815 | if (currentFrame.length) {
|
4816 | currentFrame.duration = currentNal.dts - currentFrame.dts;
|
4817 |
|
4818 | frames.byteLength += currentFrame.byteLength;
|
4819 | frames.nalCount += currentFrame.length;
|
4820 | frames.duration += currentFrame.duration;
|
4821 | frames.push(currentFrame);
|
4822 | }
|
4823 |
|
4824 | currentFrame = [currentNal];
|
4825 | currentFrame.byteLength = currentNal.data.byteLength;
|
4826 | currentFrame.pts = currentNal.pts;
|
4827 | currentFrame.dts = currentNal.dts;
|
4828 | } else {
|
4829 |
|
4830 | if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
|
4831 | currentFrame.keyFrame = true;
|
4832 | }
|
4833 |
|
4834 | currentFrame.duration = currentNal.dts - currentFrame.dts;
|
4835 | currentFrame.byteLength += currentNal.data.byteLength;
|
4836 | currentFrame.push(currentNal);
|
4837 | }
|
4838 | }
|
4839 |
|
4840 |
|
4841 |
|
4842 | if (frames.length && (!currentFrame.duration || currentFrame.duration <= 0)) {
|
4843 | currentFrame.duration = frames[frames.length - 1].duration;
|
4844 | }
|
4845 |
|
4846 |
|
4847 |
|
4848 | frames.byteLength += currentFrame.byteLength;
|
4849 | frames.nalCount += currentFrame.length;
|
4850 | frames.duration += currentFrame.duration;
|
4851 | frames.push(currentFrame);
|
4852 | return frames;
|
4853 | };
|
4854 |
|
4855 |
|
4856 |
|
4857 |
|
4858 |
|
4859 | var groupFramesIntoGops = function groupFramesIntoGops(frames) {
|
4860 | var i,
|
4861 | currentFrame,
|
4862 | currentGop = [],
|
4863 | gops = [];
|
4864 |
|
4865 |
|
4866 | currentGop.byteLength = 0;
|
4867 | currentGop.nalCount = 0;
|
4868 | currentGop.duration = 0;
|
4869 | currentGop.pts = frames[0].pts;
|
4870 | currentGop.dts = frames[0].dts;
|
4871 |
|
4872 | gops.byteLength = 0;
|
4873 | gops.nalCount = 0;
|
4874 | gops.duration = 0;
|
4875 | gops.pts = frames[0].pts;
|
4876 | gops.dts = frames[0].dts;
|
4877 |
|
4878 | for (i = 0; i < frames.length; i++) {
|
4879 | currentFrame = frames[i];
|
4880 |
|
4881 | if (currentFrame.keyFrame) {
|
4882 |
|
4883 |
|
4884 | if (currentGop.length) {
|
4885 | gops.push(currentGop);
|
4886 | gops.byteLength += currentGop.byteLength;
|
4887 | gops.nalCount += currentGop.nalCount;
|
4888 | gops.duration += currentGop.duration;
|
4889 | }
|
4890 |
|
4891 | currentGop = [currentFrame];
|
4892 | currentGop.nalCount = currentFrame.length;
|
4893 | currentGop.byteLength = currentFrame.byteLength;
|
4894 | currentGop.pts = currentFrame.pts;
|
4895 | currentGop.dts = currentFrame.dts;
|
4896 | currentGop.duration = currentFrame.duration;
|
4897 | } else {
|
4898 | currentGop.duration += currentFrame.duration;
|
4899 | currentGop.nalCount += currentFrame.length;
|
4900 | currentGop.byteLength += currentFrame.byteLength;
|
4901 | currentGop.push(currentFrame);
|
4902 | }
|
4903 | }
|
4904 |
|
4905 | if (gops.length && currentGop.duration <= 0) {
|
4906 | currentGop.duration = gops[gops.length - 1].duration;
|
4907 | }
|
4908 |
|
4909 | gops.byteLength += currentGop.byteLength;
|
4910 | gops.nalCount += currentGop.nalCount;
|
4911 | gops.duration += currentGop.duration;
|
4912 |
|
4913 | gops.push(currentGop);
|
4914 | return gops;
|
4915 | };
|
4916 | |
4917 |
|
4918 |
|
4919 |
|
4920 |
|
4921 |
|
4922 |
|
4923 |
|
4924 |
|
4925 |
|
4926 |
|
4927 | var extendFirstKeyFrame = function extendFirstKeyFrame(gops) {
|
4928 | var currentGop;
|
4929 |
|
4930 | if (!gops[0][0].keyFrame && gops.length > 1) {
|
4931 |
|
4932 | currentGop = gops.shift();
|
4933 | gops.byteLength -= currentGop.byteLength;
|
4934 | gops.nalCount -= currentGop.nalCount;
|
4935 |
|
4936 |
|
4937 |
|
4938 | gops[0][0].dts = currentGop.dts;
|
4939 | gops[0][0].pts = currentGop.pts;
|
4940 | gops[0][0].duration += currentGop.duration;
|
4941 | }
|
4942 |
|
4943 | return gops;
|
4944 | };
|
4945 | |
4946 |
|
4947 |
|
4948 |
|
4949 |
|
4950 |
|
4951 | var createDefaultSample = function createDefaultSample() {
|
4952 | return {
|
4953 | size: 0,
|
4954 | flags: {
|
4955 | isLeading: 0,
|
4956 | dependsOn: 1,
|
4957 | isDependedOn: 0,
|
4958 | hasRedundancy: 0,
|
4959 | degradationPriority: 0,
|
4960 | isNonSyncSample: 1
|
4961 | }
|
4962 | };
|
4963 | };
|
4964 | |
4965 |
|
4966 |
|
4967 |
|
4968 |
|
4969 |
|
4970 |
|
4971 |
|
4972 |
|
4973 |
|
4974 | var sampleForFrame = function sampleForFrame(frame, dataOffset) {
|
4975 | var sample = createDefaultSample();
|
4976 | sample.dataOffset = dataOffset;
|
4977 | sample.compositionTimeOffset = frame.pts - frame.dts;
|
4978 | sample.duration = frame.duration;
|
4979 | sample.size = 4 * frame.length;
|
4980 |
|
4981 | sample.size += frame.byteLength;
|
4982 |
|
4983 | if (frame.keyFrame) {
|
4984 | sample.flags.dependsOn = 2;
|
4985 | sample.flags.isNonSyncSample = 0;
|
4986 | }
|
4987 |
|
4988 | return sample;
|
4989 | };
|
4990 |
|
4991 |
|
4992 | var generateSampleTable$1 = function generateSampleTable(gops, baseDataOffset) {
|
4993 | var h,
|
4994 | i,
|
4995 | sample,
|
4996 | currentGop,
|
4997 | currentFrame,
|
4998 | dataOffset = baseDataOffset || 0,
|
4999 | samples = [];
|
5000 |
|
5001 | for (h = 0; h < gops.length; h++) {
|
5002 | currentGop = gops[h];
|
5003 |
|
5004 | for (i = 0; i < currentGop.length; i++) {
|
5005 | currentFrame = currentGop[i];
|
5006 | sample = sampleForFrame(currentFrame, dataOffset);
|
5007 | dataOffset += sample.size;
|
5008 | samples.push(sample);
|
5009 | }
|
5010 | }
|
5011 |
|
5012 | return samples;
|
5013 | };
|
5014 |
|
5015 |
|
5016 | var concatenateNalData = function concatenateNalData(gops) {
|
5017 | var h,
|
5018 | i,
|
5019 | j,
|
5020 | currentGop,
|
5021 | currentFrame,
|
5022 | currentNal,
|
5023 | dataOffset = 0,
|
5024 | nalsByteLength = gops.byteLength,
|
5025 | numberOfNals = gops.nalCount,
|
5026 | totalByteLength = nalsByteLength + 4 * numberOfNals,
|
5027 | data = new Uint8Array(totalByteLength),
|
5028 | view = new DataView(data.buffer);
|
5029 |
|
5030 | for (h = 0; h < gops.length; h++) {
|
5031 | currentGop = gops[h];
|
5032 |
|
5033 | for (i = 0; i < currentGop.length; i++) {
|
5034 | currentFrame = currentGop[i];
|
5035 |
|
5036 | for (j = 0; j < currentFrame.length; j++) {
|
5037 | currentNal = currentFrame[j];
|
5038 | view.setUint32(dataOffset, currentNal.data.byteLength);
|
5039 | dataOffset += 4;
|
5040 | data.set(currentNal.data, dataOffset);
|
5041 | dataOffset += currentNal.data.byteLength;
|
5042 | }
|
5043 | }
|
5044 | }
|
5045 |
|
5046 | return data;
|
5047 | };
|
5048 |
|
5049 |
|
5050 | var generateSampleTableForFrame = function generateSampleTableForFrame(frame, baseDataOffset) {
|
5051 | var sample,
|
5052 | dataOffset = baseDataOffset || 0,
|
5053 | samples = [];
|
5054 | sample = sampleForFrame(frame, dataOffset);
|
5055 | samples.push(sample);
|
5056 | return samples;
|
5057 | };
|
5058 |
|
5059 |
|
5060 | var concatenateNalDataForFrame = function concatenateNalDataForFrame(frame) {
|
5061 | var i,
|
5062 | currentNal,
|
5063 | dataOffset = 0,
|
5064 | nalsByteLength = frame.byteLength,
|
5065 | numberOfNals = frame.length,
|
5066 | totalByteLength = nalsByteLength + 4 * numberOfNals,
|
5067 | data = new Uint8Array(totalByteLength),
|
5068 | view = new DataView(data.buffer);
|
5069 |
|
5070 | for (i = 0; i < frame.length; i++) {
|
5071 | currentNal = frame[i];
|
5072 | view.setUint32(dataOffset, currentNal.data.byteLength);
|
5073 | dataOffset += 4;
|
5074 | data.set(currentNal.data, dataOffset);
|
5075 | dataOffset += currentNal.data.byteLength;
|
5076 | }
|
5077 |
|
5078 | return data;
|
5079 | };
|
5080 |
|
5081 | var frameUtils = {
|
5082 | groupNalsIntoFrames: groupNalsIntoFrames,
|
5083 | groupFramesIntoGops: groupFramesIntoGops,
|
5084 | extendFirstKeyFrame: extendFirstKeyFrame,
|
5085 | generateSampleTable: generateSampleTable$1,
|
5086 | concatenateNalData: concatenateNalData,
|
5087 | generateSampleTableForFrame: generateSampleTableForFrame,
|
5088 | concatenateNalDataForFrame: concatenateNalDataForFrame
|
5089 | };
|
5090 | |
5091 |
|
5092 |
|
5093 |
|
5094 |
|
5095 |
|
5096 |
|
5097 | var highPrefix = [33, 16, 5, 32, 164, 27];
|
5098 | var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
|
5099 |
|
5100 | var zeroFill = function zeroFill(count) {
|
5101 | var a = [];
|
5102 |
|
5103 | while (count--) {
|
5104 | a.push(0);
|
5105 | }
|
5106 |
|
5107 | return a;
|
5108 | };
|
5109 |
|
5110 | var makeTable = function makeTable(metaTable) {
|
5111 | return Object.keys(metaTable).reduce(function (obj, key) {
|
5112 | obj[key] = new Uint8Array(metaTable[key].reduce(function (arr, part) {
|
5113 | return arr.concat(part);
|
5114 | }, []));
|
5115 | return obj;
|
5116 | }, {});
|
5117 | };
|
5118 |
|
5119 | var silence;
|
5120 |
|
5121 | var silence_1 = function silence_1() {
|
5122 | if (!silence) {
|
5123 |
|
5124 | var coneOfSilence = {
|
5125 | 96000: [highPrefix, [227, 64], zeroFill(154), [56]],
|
5126 | 88200: [highPrefix, [231], zeroFill(170), [56]],
|
5127 | 64000: [highPrefix, [248, 192], zeroFill(240), [56]],
|
5128 | 48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
|
5129 | 44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
|
5130 | 32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
|
5131 | 24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
|
5132 | 16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
|
5133 | 12000: [lowPrefix, zeroFill(268), [3, 127, 248], zeroFill(268), [6, 255, 240], zeroFill(268), [13, 255, 224], zeroFill(268), [27, 253, 128], zeroFill(259), [56]],
|
5134 | 11025: [lowPrefix, zeroFill(268), [3, 127, 248], zeroFill(268), [6, 255, 240], zeroFill(268), [13, 255, 224], zeroFill(268), [27, 255, 192], zeroFill(268), [55, 175, 128], zeroFill(108), [112]],
|
5135 | 8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
|
5136 | };
|
5137 | silence = makeTable(coneOfSilence);
|
5138 | }
|
5139 |
|
5140 | return silence;
|
5141 | };
|
5142 | |
5143 |
|
5144 |
|
5145 |
|
5146 |
|
5147 |
|
5148 |
|
5149 |
|
5150 | var ONE_SECOND_IN_TS$4 = 90000,
|
5151 |
|
5152 | secondsToVideoTs,
|
5153 | secondsToAudioTs,
|
5154 | videoTsToSeconds,
|
5155 | audioTsToSeconds,
|
5156 | audioTsToVideoTs,
|
5157 | videoTsToAudioTs,
|
5158 | metadataTsToSeconds;
|
5159 |
|
5160 | secondsToVideoTs = function secondsToVideoTs(seconds) {
|
5161 | return seconds * ONE_SECOND_IN_TS$4;
|
5162 | };
|
5163 |
|
5164 | secondsToAudioTs = function secondsToAudioTs(seconds, sampleRate) {
|
5165 | return seconds * sampleRate;
|
5166 | };
|
5167 |
|
5168 | videoTsToSeconds = function videoTsToSeconds(timestamp) {
|
5169 | return timestamp / ONE_SECOND_IN_TS$4;
|
5170 | };
|
5171 |
|
5172 | audioTsToSeconds = function audioTsToSeconds(timestamp, sampleRate) {
|
5173 | return timestamp / sampleRate;
|
5174 | };
|
5175 |
|
5176 | audioTsToVideoTs = function audioTsToVideoTs(timestamp, sampleRate) {
|
5177 | return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
|
5178 | };
|
5179 |
|
5180 | videoTsToAudioTs = function videoTsToAudioTs(timestamp, sampleRate) {
|
5181 | return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
|
5182 | };
|
5183 | |
5184 |
|
5185 |
|
5186 |
|
5187 |
|
5188 |
|
5189 | metadataTsToSeconds = function metadataTsToSeconds(timestamp, timelineStartPts, keepOriginalTimestamps) {
|
5190 | return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
|
5191 | };
|
5192 |
|
5193 | var clock = {
|
5194 | ONE_SECOND_IN_TS: ONE_SECOND_IN_TS$4,
|
5195 | secondsToVideoTs: secondsToVideoTs,
|
5196 | secondsToAudioTs: secondsToAudioTs,
|
5197 | videoTsToSeconds: videoTsToSeconds,
|
5198 | audioTsToSeconds: audioTsToSeconds,
|
5199 | audioTsToVideoTs: audioTsToVideoTs,
|
5200 | videoTsToAudioTs: videoTsToAudioTs,
|
5201 | metadataTsToSeconds: metadataTsToSeconds
|
5202 | };
|
5203 | |
5204 |
|
5205 |
|
5206 |
|
5207 |
|
5208 |
|
5209 |
|
5210 | |
5211 |
|
5212 |
|
5213 |
|
5214 | var sumFrameByteLengths = function sumFrameByteLengths(array) {
|
5215 | var i,
|
5216 | currentObj,
|
5217 | sum = 0;
|
5218 |
|
5219 | for (i = 0; i < array.length; i++) {
|
5220 | currentObj = array[i];
|
5221 | sum += currentObj.data.byteLength;
|
5222 | }
|
5223 |
|
5224 | return sum;
|
5225 | };
|
5226 |
|
5227 |
|
5228 |
|
5229 | var prefixWithSilence = function prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime) {
|
5230 | var baseMediaDecodeTimeTs,
|
5231 | frameDuration = 0,
|
5232 | audioGapDuration = 0,
|
5233 | audioFillFrameCount = 0,
|
5234 | audioFillDuration = 0,
|
5235 | silentFrame,
|
5236 | i,
|
5237 | firstFrame;
|
5238 |
|
5239 | if (!frames.length) {
|
5240 | return;
|
5241 | }
|
5242 |
|
5243 | baseMediaDecodeTimeTs = clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate);
|
5244 |
|
5245 | frameDuration = Math.ceil(clock.ONE_SECOND_IN_TS / (track.samplerate / 1024));
|
5246 |
|
5247 | if (audioAppendStartTs && videoBaseMediaDecodeTime) {
|
5248 |
|
5249 | audioGapDuration = baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime);
|
5250 |
|
5251 | audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
|
5252 | audioFillDuration = audioFillFrameCount * frameDuration;
|
5253 | }
|
5254 |
|
5255 |
|
5256 |
|
5257 | if (audioFillFrameCount < 1 || audioFillDuration > clock.ONE_SECOND_IN_TS / 2) {
|
5258 | return;
|
5259 | }
|
5260 |
|
5261 | silentFrame = silence_1()[track.samplerate];
|
5262 |
|
5263 | if (!silentFrame) {
|
5264 |
|
5265 |
|
5266 | silentFrame = frames[0].data;
|
5267 | }
|
5268 |
|
5269 | for (i = 0; i < audioFillFrameCount; i++) {
|
5270 | firstFrame = frames[0];
|
5271 | frames.splice(0, 0, {
|
5272 | data: silentFrame,
|
5273 | dts: firstFrame.dts - frameDuration,
|
5274 | pts: firstFrame.pts - frameDuration
|
5275 | });
|
5276 | }
|
5277 |
|
5278 | track.baseMediaDecodeTime -= Math.floor(clock.videoTsToAudioTs(audioFillDuration, track.samplerate));
|
5279 | return audioFillDuration;
|
5280 | };
|
5281 |
|
5282 |
|
5283 |
|
5284 |
|
5285 |
|
5286 | var trimAdtsFramesByEarliestDts = function trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts) {
|
5287 | if (track.minSegmentDts >= earliestAllowedDts) {
|
5288 | return adtsFrames;
|
5289 | }
|
5290 |
|
5291 |
|
5292 | track.minSegmentDts = Infinity;
|
5293 | return adtsFrames.filter(function (currentFrame) {
|
5294 |
|
5295 | if (currentFrame.dts >= earliestAllowedDts) {
|
5296 | track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
|
5297 | track.minSegmentPts = track.minSegmentDts;
|
5298 | return true;
|
5299 | }
|
5300 |
|
5301 |
|
5302 | return false;
|
5303 | });
|
5304 | };
|
5305 |
|
5306 |
|
5307 | var generateSampleTable = function generateSampleTable(frames) {
|
5308 | var i,
|
5309 | currentFrame,
|
5310 | samples = [];
|
5311 |
|
5312 | for (i = 0; i < frames.length; i++) {
|
5313 | currentFrame = frames[i];
|
5314 | samples.push({
|
5315 | size: currentFrame.data.byteLength,
|
5316 | duration: 1024
|
5317 |
|
5318 | });
|
5319 | }
|
5320 |
|
5321 | return samples;
|
5322 | };
|
5323 |
|
5324 |
|
5325 | var concatenateFrameData = function concatenateFrameData(frames) {
|
5326 | var i,
|
5327 | currentFrame,
|
5328 | dataOffset = 0,
|
5329 | data = new Uint8Array(sumFrameByteLengths(frames));
|
5330 |
|
5331 | for (i = 0; i < frames.length; i++) {
|
5332 | currentFrame = frames[i];
|
5333 | data.set(currentFrame.data, dataOffset);
|
5334 | dataOffset += currentFrame.data.byteLength;
|
5335 | }
|
5336 |
|
5337 | return data;
|
5338 | };
|
5339 |
|
5340 | var audioFrameUtils = {
|
5341 | prefixWithSilence: prefixWithSilence,
|
5342 | trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
|
5343 | generateSampleTable: generateSampleTable,
|
5344 | concatenateFrameData: concatenateFrameData
|
5345 | };
|
5346 | |
5347 |
|
5348 |
|
5349 |
|
5350 |
|
5351 |
|
5352 |
|
5353 | var ONE_SECOND_IN_TS$3 = clock.ONE_SECOND_IN_TS;
|
5354 | |
5355 |
|
5356 |
|
5357 |
|
5358 |
|
5359 |
|
5360 | var collectDtsInfo = function collectDtsInfo(track, data) {
|
5361 | if (typeof data.pts === 'number') {
|
5362 | if (track.timelineStartInfo.pts === undefined) {
|
5363 | track.timelineStartInfo.pts = data.pts;
|
5364 | }
|
5365 |
|
5366 | if (track.minSegmentPts === undefined) {
|
5367 | track.minSegmentPts = data.pts;
|
5368 | } else {
|
5369 | track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
|
5370 | }
|
5371 |
|
5372 | if (track.maxSegmentPts === undefined) {
|
5373 | track.maxSegmentPts = data.pts;
|
5374 | } else {
|
5375 | track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
|
5376 | }
|
5377 | }
|
5378 |
|
5379 | if (typeof data.dts === 'number') {
|
5380 | if (track.timelineStartInfo.dts === undefined) {
|
5381 | track.timelineStartInfo.dts = data.dts;
|
5382 | }
|
5383 |
|
5384 | if (track.minSegmentDts === undefined) {
|
5385 | track.minSegmentDts = data.dts;
|
5386 | } else {
|
5387 | track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
|
5388 | }
|
5389 |
|
5390 | if (track.maxSegmentDts === undefined) {
|
5391 | track.maxSegmentDts = data.dts;
|
5392 | } else {
|
5393 | track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
|
5394 | }
|
5395 | }
|
5396 | };
|
5397 | |
5398 |
|
5399 |
|
5400 |
|
5401 |
|
5402 |
|
5403 | var clearDtsInfo = function clearDtsInfo(track) {
|
5404 | delete track.minSegmentDts;
|
5405 | delete track.maxSegmentDts;
|
5406 | delete track.minSegmentPts;
|
5407 | delete track.maxSegmentPts;
|
5408 | };
|
5409 | |
5410 |
|
5411 |
|
5412 |
|
5413 |
|
5414 |
|
5415 |
|
5416 |
|
5417 |
|
5418 |
|
5419 | var calculateTrackBaseMediaDecodeTime = function calculateTrackBaseMediaDecodeTime(track, keepOriginalTimestamps) {
|
5420 | var baseMediaDecodeTime,
|
5421 | scale,
|
5422 | minSegmentDts = track.minSegmentDts;
|
5423 |
|
5424 | if (!keepOriginalTimestamps) {
|
5425 | minSegmentDts -= track.timelineStartInfo.dts;
|
5426 | }
|
5427 |
|
5428 |
|
5429 |
|
5430 | baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime;
|
5431 |
|
5432 | baseMediaDecodeTime += minSegmentDts;
|
5433 |
|
5434 | baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
|
5435 |
|
5436 | if (track.type === 'audio') {
|
5437 |
|
5438 |
|
5439 | scale = track.samplerate / ONE_SECOND_IN_TS$3;
|
5440 | baseMediaDecodeTime *= scale;
|
5441 | baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
|
5442 | }
|
5443 |
|
5444 | return baseMediaDecodeTime;
|
5445 | };
|
5446 |
|
5447 | var trackDecodeInfo = {
|
5448 | clearDtsInfo: clearDtsInfo,
|
5449 | calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
|
5450 | collectDtsInfo: collectDtsInfo
|
5451 | };
|
5452 | |
5453 |
|
5454 |
|
5455 |
|
5456 |
|
5457 |
|
5458 |
|
5459 |
|
5460 |
|
5461 |
|
5462 |
|
5463 |
|
5464 |
|
5465 |
|
5466 |
|
5467 |
|
5468 | var USER_DATA_REGISTERED_ITU_T_T35 = 4,
|
5469 | RBSP_TRAILING_BITS = 128;
|
5470 | |
5471 |
|
5472 |
|
5473 |
|
5474 |
|
5475 |
|
5476 |
|
5477 |
|
5478 |
|
5479 | var parseSei = function parseSei(bytes) {
|
5480 | var i = 0,
|
5481 | result = {
|
5482 | payloadType: -1,
|
5483 | payloadSize: 0
|
5484 | },
|
5485 | payloadType = 0,
|
5486 | payloadSize = 0;
|
5487 |
|
5488 | while (i < bytes.byteLength) {
|
5489 |
|
5490 | if (bytes[i] === RBSP_TRAILING_BITS) {
|
5491 | break;
|
5492 | }
|
5493 |
|
5494 |
|
5495 | while (bytes[i] === 0xFF) {
|
5496 | payloadType += 255;
|
5497 | i++;
|
5498 | }
|
5499 |
|
5500 | payloadType += bytes[i++];
|
5501 |
|
5502 | while (bytes[i] === 0xFF) {
|
5503 | payloadSize += 255;
|
5504 | i++;
|
5505 | }
|
5506 |
|
5507 | payloadSize += bytes[i++];
|
5508 |
|
5509 |
|
5510 | if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
|
5511 | var userIdentifier = String.fromCharCode(bytes[i + 3], bytes[i + 4], bytes[i + 5], bytes[i + 6]);
|
5512 |
|
5513 | if (userIdentifier === 'GA94') {
|
5514 | result.payloadType = payloadType;
|
5515 | result.payloadSize = payloadSize;
|
5516 | result.payload = bytes.subarray(i, i + payloadSize);
|
5517 | break;
|
5518 | } else {
|
5519 | result.payload = void 0;
|
5520 | }
|
5521 | }
|
5522 |
|
5523 |
|
5524 | i += payloadSize;
|
5525 | payloadType = 0;
|
5526 | payloadSize = 0;
|
5527 | }
|
5528 |
|
5529 | return result;
|
5530 | };
|
5531 |
|
5532 |
|
5533 | var parseUserData = function parseUserData(sei) {
|
5534 |
|
5535 |
|
5536 | if (sei.payload[0] !== 181) {
|
5537 | return null;
|
5538 | }
|
5539 |
|
5540 |
|
5541 | if ((sei.payload[1] << 8 | sei.payload[2]) !== 49) {
|
5542 | return null;
|
5543 | }
|
5544 |
|
5545 |
|
5546 | if (String.fromCharCode(sei.payload[3], sei.payload[4], sei.payload[5], sei.payload[6]) !== 'GA94') {
|
5547 | return null;
|
5548 | }
|
5549 |
|
5550 |
|
5551 | if (sei.payload[7] !== 0x03) {
|
5552 | return null;
|
5553 | }
|
5554 |
|
5555 |
|
5556 |
|
5557 | return sei.payload.subarray(8, sei.payload.length - 1);
|
5558 | };
|
5559 |
|
5560 |
|
5561 | var parseCaptionPackets = function parseCaptionPackets(pts, userData) {
|
5562 | var results = [],
|
5563 | i,
|
5564 | count,
|
5565 | offset,
|
5566 | data;
|
5567 |
|
5568 | if (!(userData[0] & 0x40)) {
|
5569 | return results;
|
5570 | }
|
5571 |
|
5572 |
|
5573 | count = userData[0] & 0x1f;
|
5574 |
|
5575 | for (i = 0; i < count; i++) {
|
5576 | offset = i * 3;
|
5577 | data = {
|
5578 | type: userData[offset + 2] & 0x03,
|
5579 | pts: pts
|
5580 | };
|
5581 |
|
5582 | if (userData[offset + 2] & 0x04) {
|
5583 | data.ccData = userData[offset + 3] << 8 | userData[offset + 4];
|
5584 | results.push(data);
|
5585 | }
|
5586 | }
|
5587 |
|
5588 | return results;
|
5589 | };
|
5590 |
|
5591 | var discardEmulationPreventionBytes$1 = function discardEmulationPreventionBytes(data) {
|
5592 | var length = data.byteLength,
|
5593 | emulationPreventionBytesPositions = [],
|
5594 | i = 1,
|
5595 | newLength,
|
5596 | newData;
|
5597 |
|
5598 | while (i < length - 2) {
|
5599 | if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
|
5600 | emulationPreventionBytesPositions.push(i + 2);
|
5601 | i += 2;
|
5602 | } else {
|
5603 | i++;
|
5604 | }
|
5605 | }
|
5606 |
|
5607 |
|
5608 |
|
5609 | if (emulationPreventionBytesPositions.length === 0) {
|
5610 | return data;
|
5611 | }
|
5612 |
|
5613 |
|
5614 | newLength = length - emulationPreventionBytesPositions.length;
|
5615 | newData = new Uint8Array(newLength);
|
5616 | var sourceIndex = 0;
|
5617 |
|
5618 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
5619 | if (sourceIndex === emulationPreventionBytesPositions[0]) {
|
5620 |
|
5621 | sourceIndex++;
|
5622 |
|
5623 | emulationPreventionBytesPositions.shift();
|
5624 | }
|
5625 |
|
5626 | newData[i] = data[sourceIndex];
|
5627 | }
|
5628 |
|
5629 | return newData;
|
5630 | };
|
5631 |
|
5632 |
|
5633 | var captionPacketParser = {
|
5634 | parseSei: parseSei,
|
5635 | parseUserData: parseUserData,
|
5636 | parseCaptionPackets: parseCaptionPackets,
|
5637 | discardEmulationPreventionBytes: discardEmulationPreventionBytes$1,
|
5638 | USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
|
5639 | };
|
5640 |
|
5641 |
|
5642 | var CaptionStream$1 = function CaptionStream(options) {
|
5643 | options = options || {};
|
5644 | CaptionStream.prototype.init.call(this);
|
5645 |
|
5646 | this.parse708captions_ = typeof options.parse708captions === 'boolean' ? options.parse708captions : true;
|
5647 | this.captionPackets_ = [];
|
5648 | this.ccStreams_ = [new Cea608Stream(0, 0),
|
5649 | new Cea608Stream(0, 1),
|
5650 | new Cea608Stream(1, 0),
|
5651 | new Cea608Stream(1, 1)
|
5652 | ];
|
5653 |
|
5654 | if (this.parse708captions_) {
|
5655 | this.cc708Stream_ = new Cea708Stream({
|
5656 | captionServices: options.captionServices
|
5657 | });
|
5658 | }
|
5659 |
|
5660 | this.reset();
|
5661 |
|
5662 | this.ccStreams_.forEach(function (cc) {
|
5663 | cc.on('data', this.trigger.bind(this, 'data'));
|
5664 | cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
|
5665 | cc.on('done', this.trigger.bind(this, 'done'));
|
5666 | }, this);
|
5667 |
|
5668 | if (this.parse708captions_) {
|
5669 | this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
|
5670 | this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
|
5671 | this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
|
5672 | }
|
5673 | };
|
5674 |
|
5675 | CaptionStream$1.prototype = new stream();
|
5676 |
|
5677 | CaptionStream$1.prototype.push = function (event) {
|
5678 | var sei, userData, newCaptionPackets;
|
5679 |
|
5680 | if (event.nalUnitType !== 'sei_rbsp') {
|
5681 | return;
|
5682 | }
|
5683 |
|
5684 |
|
5685 | sei = captionPacketParser.parseSei(event.escapedRBSP);
|
5686 |
|
5687 | if (!sei.payload) {
|
5688 | return;
|
5689 | }
|
5690 |
|
5691 |
|
5692 | if (sei.payloadType !== captionPacketParser.USER_DATA_REGISTERED_ITU_T_T35) {
|
5693 | return;
|
5694 | }
|
5695 |
|
5696 |
|
5697 | userData = captionPacketParser.parseUserData(sei);
|
5698 |
|
5699 | if (!userData) {
|
5700 | return;
|
5701 | }
|
5702 |
|
5703 |
|
5704 |
|
5705 |
|
5706 |
|
5707 |
|
5708 |
|
5709 |
|
5710 |
|
5711 | if (event.dts < this.latestDts_) {
|
5712 |
|
5713 | this.ignoreNextEqualDts_ = true;
|
5714 | return;
|
5715 | } else if (event.dts === this.latestDts_ && this.ignoreNextEqualDts_) {
|
5716 | this.numSameDts_--;
|
5717 |
|
5718 | if (!this.numSameDts_) {
|
5719 |
|
5720 | this.ignoreNextEqualDts_ = false;
|
5721 | }
|
5722 |
|
5723 | return;
|
5724 | }
|
5725 |
|
5726 |
|
5727 | newCaptionPackets = captionPacketParser.parseCaptionPackets(event.pts, userData);
|
5728 | this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
|
5729 |
|
5730 | if (this.latestDts_ !== event.dts) {
|
5731 | this.numSameDts_ = 0;
|
5732 | }
|
5733 |
|
5734 | this.numSameDts_++;
|
5735 | this.latestDts_ = event.dts;
|
5736 | };
|
5737 |
|
5738 | CaptionStream$1.prototype.flushCCStreams = function (flushType) {
|
5739 | this.ccStreams_.forEach(function (cc) {
|
5740 | return flushType === 'flush' ? cc.flush() : cc.partialFlush();
|
5741 | }, this);
|
5742 | };
|
5743 |
|
5744 | CaptionStream$1.prototype.flushStream = function (flushType) {
|
5745 |
|
5746 | if (!this.captionPackets_.length) {
|
5747 | this.flushCCStreams(flushType);
|
5748 | return;
|
5749 | }
|
5750 |
|
5751 |
|
5752 |
|
5753 | this.captionPackets_.forEach(function (elem, idx) {
|
5754 | elem.presortIndex = idx;
|
5755 | });
|
5756 |
|
5757 | this.captionPackets_.sort(function (a, b) {
|
5758 | if (a.pts === b.pts) {
|
5759 | return a.presortIndex - b.presortIndex;
|
5760 | }
|
5761 |
|
5762 | return a.pts - b.pts;
|
5763 | });
|
5764 | this.captionPackets_.forEach(function (packet) {
|
5765 | if (packet.type < 2) {
|
5766 |
|
5767 | this.dispatchCea608Packet(packet);
|
5768 | } else {
|
5769 |
|
5770 | this.dispatchCea708Packet(packet);
|
5771 | }
|
5772 | }, this);
|
5773 | this.captionPackets_.length = 0;
|
5774 | this.flushCCStreams(flushType);
|
5775 | };
|
5776 |
|
5777 | CaptionStream$1.prototype.flush = function () {
|
5778 | return this.flushStream('flush');
|
5779 | };
|
5780 |
|
5781 |
|
5782 | CaptionStream$1.prototype.partialFlush = function () {
|
5783 | return this.flushStream('partialFlush');
|
5784 | };
|
5785 |
|
5786 | CaptionStream$1.prototype.reset = function () {
|
5787 | this.latestDts_ = null;
|
5788 | this.ignoreNextEqualDts_ = false;
|
5789 | this.numSameDts_ = 0;
|
5790 | this.activeCea608Channel_ = [null, null];
|
5791 | this.ccStreams_.forEach(function (ccStream) {
|
5792 | ccStream.reset();
|
5793 | });
|
5794 | };
|
5795 |
|
5796 | |
5797 |
|
5798 |
|
5799 |
|
5800 |
|
5801 |
|
5802 |
|
5803 |
|
5804 |
|
5805 |
|
5806 |
|
5807 |
|
5808 | CaptionStream$1.prototype.dispatchCea608Packet = function (packet) {
|
5809 |
|
5810 | if (this.setsTextOrXDSActive(packet)) {
|
5811 | this.activeCea608Channel_[packet.type] = null;
|
5812 | } else if (this.setsChannel1Active(packet)) {
|
5813 | this.activeCea608Channel_[packet.type] = 0;
|
5814 | } else if (this.setsChannel2Active(packet)) {
|
5815 | this.activeCea608Channel_[packet.type] = 1;
|
5816 | }
|
5817 |
|
5818 | if (this.activeCea608Channel_[packet.type] === null) {
|
5819 |
|
5820 |
|
5821 |
|
5822 | return;
|
5823 | }
|
5824 |
|
5825 | this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
|
5826 | };
|
5827 |
|
5828 | CaptionStream$1.prototype.setsChannel1Active = function (packet) {
|
5829 | return (packet.ccData & 0x7800) === 0x1000;
|
5830 | };
|
5831 |
|
5832 | CaptionStream$1.prototype.setsChannel2Active = function (packet) {
|
5833 | return (packet.ccData & 0x7800) === 0x1800;
|
5834 | };
|
5835 |
|
5836 | CaptionStream$1.prototype.setsTextOrXDSActive = function (packet) {
|
5837 | return (packet.ccData & 0x7100) === 0x0100 || (packet.ccData & 0x78fe) === 0x102a || (packet.ccData & 0x78fe) === 0x182a;
|
5838 | };
|
5839 |
|
5840 | CaptionStream$1.prototype.dispatchCea708Packet = function (packet) {
|
5841 | if (this.parse708captions_) {
|
5842 | this.cc708Stream_.push(packet);
|
5843 | }
|
5844 | };
|
5845 |
|
5846 |
|
5847 |
|
5848 |
|
5849 |
|
5850 |
|
5851 |
|
5852 |
|
5853 |
|
5854 |
|
5855 |
|
5856 |
|
5857 |
|
5858 |
|
5859 |
|
5860 |
|
5861 |
|
5862 |
|
5863 |
|
5864 | var CHARACTER_TRANSLATION_708 = {
|
5865 | 0x7f: 0x266a,
|
5866 |
|
5867 | 0x1020: 0x20,
|
5868 |
|
5869 | 0x1021: 0xa0,
|
5870 |
|
5871 | 0x1025: 0x2026,
|
5872 |
|
5873 | 0x102a: 0x0160,
|
5874 |
|
5875 | 0x102c: 0x0152,
|
5876 |
|
5877 | 0x1030: 0x2588,
|
5878 |
|
5879 | 0x1031: 0x2018,
|
5880 |
|
5881 | 0x1032: 0x2019,
|
5882 |
|
5883 | 0x1033: 0x201c,
|
5884 |
|
5885 | 0x1034: 0x201d,
|
5886 |
|
5887 | 0x1035: 0x2022,
|
5888 |
|
5889 | 0x1039: 0x2122,
|
5890 |
|
5891 | 0x103a: 0x0161,
|
5892 |
|
5893 | 0x103c: 0x0153,
|
5894 |
|
5895 | 0x103d: 0x2120,
|
5896 |
|
5897 | 0x103f: 0x0178,
|
5898 |
|
5899 | 0x1076: 0x215b,
|
5900 |
|
5901 | 0x1077: 0x215c,
|
5902 |
|
5903 | 0x1078: 0x215d,
|
5904 |
|
5905 | 0x1079: 0x215e,
|
5906 |
|
5907 | 0x107a: 0x23d0,
|
5908 |
|
5909 | 0x107b: 0x23a4,
|
5910 |
|
5911 | 0x107c: 0x23a3,
|
5912 |
|
5913 | 0x107d: 0x23af,
|
5914 |
|
5915 | 0x107e: 0x23a6,
|
5916 |
|
5917 | 0x107f: 0x23a1,
|
5918 |
|
5919 | 0x10a0: 0x3138
|
5920 |
|
5921 | };
|
5922 |
|
5923 | var get708CharFromCode = function get708CharFromCode(code) {
|
5924 | var newCode = CHARACTER_TRANSLATION_708[code] || code;
|
5925 |
|
5926 | if (code & 0x1000 && code === newCode) {
|
5927 |
|
5928 | return '';
|
5929 | }
|
5930 |
|
5931 | return String.fromCharCode(newCode);
|
5932 | };
|
5933 |
|
5934 | var within708TextBlock = function within708TextBlock(b) {
|
5935 | return 0x20 <= b && b <= 0x7f || 0xa0 <= b && b <= 0xff;
|
5936 | };
|
5937 |
|
5938 | var Cea708Window = function Cea708Window(windowNum) {
|
5939 | this.windowNum = windowNum;
|
5940 | this.reset();
|
5941 | };
|
5942 |
|
5943 | Cea708Window.prototype.reset = function () {
|
5944 | this.clearText();
|
5945 | this.pendingNewLine = false;
|
5946 | this.winAttr = {};
|
5947 | this.penAttr = {};
|
5948 | this.penLoc = {};
|
5949 | this.penColor = {};
|
5950 |
|
5951 |
|
5952 | this.visible = 0;
|
5953 | this.rowLock = 0;
|
5954 | this.columnLock = 0;
|
5955 | this.priority = 0;
|
5956 | this.relativePositioning = 0;
|
5957 | this.anchorVertical = 0;
|
5958 | this.anchorHorizontal = 0;
|
5959 | this.anchorPoint = 0;
|
5960 | this.rowCount = 1;
|
5961 | this.virtualRowCount = this.rowCount + 1;
|
5962 | this.columnCount = 41;
|
5963 | this.windowStyle = 0;
|
5964 | this.penStyle = 0;
|
5965 | };
|
5966 |
|
5967 | Cea708Window.prototype.getText = function () {
|
5968 | return this.rows.join('\n');
|
5969 | };
|
5970 |
|
5971 | Cea708Window.prototype.clearText = function () {
|
5972 | this.rows = [''];
|
5973 | this.rowIdx = 0;
|
5974 | };
|
5975 |
|
5976 | Cea708Window.prototype.newLine = function (pts) {
|
5977 | if (this.rows.length >= this.virtualRowCount && typeof this.beforeRowOverflow === 'function') {
|
5978 | this.beforeRowOverflow(pts);
|
5979 | }
|
5980 |
|
5981 | if (this.rows.length > 0) {
|
5982 | this.rows.push('');
|
5983 | this.rowIdx++;
|
5984 | }
|
5985 |
|
5986 |
|
5987 | while (this.rows.length > this.virtualRowCount) {
|
5988 | this.rows.shift();
|
5989 | this.rowIdx--;
|
5990 | }
|
5991 | };
|
5992 |
|
5993 | Cea708Window.prototype.isEmpty = function () {
|
5994 | if (this.rows.length === 0) {
|
5995 | return true;
|
5996 | } else if (this.rows.length === 1) {
|
5997 | return this.rows[0] === '';
|
5998 | }
|
5999 |
|
6000 | return false;
|
6001 | };
|
6002 |
|
6003 | Cea708Window.prototype.addText = function (text) {
|
6004 | this.rows[this.rowIdx] += text;
|
6005 | };
|
6006 |
|
6007 | Cea708Window.prototype.backspace = function () {
|
6008 | if (!this.isEmpty()) {
|
6009 | var row = this.rows[this.rowIdx];
|
6010 | this.rows[this.rowIdx] = row.substr(0, row.length - 1);
|
6011 | }
|
6012 | };
|
6013 |
|
6014 | var Cea708Service = function Cea708Service(serviceNum, encoding, stream) {
|
6015 | this.serviceNum = serviceNum;
|
6016 | this.text = '';
|
6017 | this.currentWindow = new Cea708Window(-1);
|
6018 | this.windows = [];
|
6019 | this.stream = stream;
|
6020 |
|
6021 | if (typeof encoding === 'string') {
|
6022 | this.createTextDecoder(encoding);
|
6023 | }
|
6024 | };
|
6025 | |
6026 |
|
6027 |
|
6028 |
|
6029 |
|
6030 |
|
6031 |
|
6032 |
|
6033 |
|
6034 | Cea708Service.prototype.init = function (pts, beforeRowOverflow) {
|
6035 | this.startPts = pts;
|
6036 |
|
6037 | for (var win = 0; win < 8; win++) {
|
6038 | this.windows[win] = new Cea708Window(win);
|
6039 |
|
6040 | if (typeof beforeRowOverflow === 'function') {
|
6041 | this.windows[win].beforeRowOverflow = beforeRowOverflow;
|
6042 | }
|
6043 | }
|
6044 | };
|
6045 | |
6046 |
|
6047 |
|
6048 |
|
6049 |
|
6050 |
|
6051 |
|
6052 | Cea708Service.prototype.setCurrentWindow = function (windowNum) {
|
6053 | this.currentWindow = this.windows[windowNum];
|
6054 | };
|
6055 | |
6056 |
|
6057 |
|
6058 |
|
6059 |
|
6060 | Cea708Service.prototype.createTextDecoder = function (encoding) {
|
6061 | if (typeof TextDecoder === 'undefined') {
|
6062 | this.stream.trigger('log', {
|
6063 | level: 'warn',
|
6064 | message: 'The `encoding` option is unsupported without TextDecoder support'
|
6065 | });
|
6066 | } else {
|
6067 | try {
|
6068 | this.textDecoder_ = new TextDecoder(encoding);
|
6069 | } catch (error) {
|
6070 | this.stream.trigger('log', {
|
6071 | level: 'warn',
|
6072 | message: 'TextDecoder could not be created with ' + encoding + ' encoding. ' + error
|
6073 | });
|
6074 | }
|
6075 | }
|
6076 | };
|
6077 |
|
6078 | var Cea708Stream = function Cea708Stream(options) {
|
6079 | options = options || {};
|
6080 | Cea708Stream.prototype.init.call(this);
|
6081 | var self = this;
|
6082 | var captionServices = options.captionServices || {};
|
6083 | var captionServiceEncodings = {};
|
6084 | var serviceProps;
|
6085 |
|
6086 | Object.keys(captionServices).forEach(function (serviceName) {
|
6087 | serviceProps = captionServices[serviceName];
|
6088 |
|
6089 | if (/^SERVICE/.test(serviceName)) {
|
6090 | captionServiceEncodings[serviceName] = serviceProps.encoding;
|
6091 | }
|
6092 | });
|
6093 | this.serviceEncodings = captionServiceEncodings;
|
6094 | this.current708Packet = null;
|
6095 | this.services = {};
|
6096 |
|
6097 | this.push = function (packet) {
|
6098 | if (packet.type === 3) {
|
6099 |
|
6100 | self.new708Packet();
|
6101 | self.add708Bytes(packet);
|
6102 | } else {
|
6103 | if (self.current708Packet === null) {
|
6104 |
|
6105 | self.new708Packet();
|
6106 | }
|
6107 |
|
6108 | self.add708Bytes(packet);
|
6109 | }
|
6110 | };
|
6111 | };
|
6112 |
|
6113 | Cea708Stream.prototype = new stream();
|
6114 | |
6115 |
|
6116 |
|
6117 |
|
6118 | Cea708Stream.prototype.new708Packet = function () {
|
6119 | if (this.current708Packet !== null) {
|
6120 | this.push708Packet();
|
6121 | }
|
6122 |
|
6123 | this.current708Packet = {
|
6124 | data: [],
|
6125 | ptsVals: []
|
6126 | };
|
6127 | };
|
6128 | |
6129 |
|
6130 |
|
6131 |
|
6132 |
|
6133 | Cea708Stream.prototype.add708Bytes = function (packet) {
|
6134 | var data = packet.ccData;
|
6135 | var byte0 = data >>> 8;
|
6136 | var byte1 = data & 0xff;
|
6137 |
|
6138 |
|
6139 | this.current708Packet.ptsVals.push(packet.pts);
|
6140 | this.current708Packet.data.push(byte0);
|
6141 | this.current708Packet.data.push(byte1);
|
6142 | };
|
6143 | |
6144 |
|
6145 |
|
6146 |
|
6147 |
|
6148 | Cea708Stream.prototype.push708Packet = function () {
|
6149 | var packet708 = this.current708Packet;
|
6150 | var packetData = packet708.data;
|
6151 | var serviceNum = null;
|
6152 | var blockSize = null;
|
6153 | var i = 0;
|
6154 | var b = packetData[i++];
|
6155 | packet708.seq = b >> 6;
|
6156 | packet708.sizeCode = b & 0x3f;
|
6157 |
|
6158 | for (; i < packetData.length; i++) {
|
6159 | b = packetData[i++];
|
6160 | serviceNum = b >> 5;
|
6161 | blockSize = b & 0x1f;
|
6162 |
|
6163 | if (serviceNum === 7 && blockSize > 0) {
|
6164 |
|
6165 | b = packetData[i++];
|
6166 | serviceNum = b;
|
6167 | }
|
6168 |
|
6169 | this.pushServiceBlock(serviceNum, i, blockSize);
|
6170 |
|
6171 | if (blockSize > 0) {
|
6172 | i += blockSize - 1;
|
6173 | }
|
6174 | }
|
6175 | };
|
6176 | |
6177 |
|
6178 |
|
6179 |
|
6180 |
|
6181 |
|
6182 |
|
6183 |
|
6184 |
|
6185 |
|
6186 |
|
6187 |
|
6188 |
|
6189 |
|
6190 | Cea708Stream.prototype.pushServiceBlock = function (serviceNum, start, size) {
|
6191 | var b;
|
6192 | var i = start;
|
6193 | var packetData = this.current708Packet.data;
|
6194 | var service = this.services[serviceNum];
|
6195 |
|
6196 | if (!service) {
|
6197 | service = this.initService(serviceNum, i);
|
6198 | }
|
6199 |
|
6200 | for (; i < start + size && i < packetData.length; i++) {
|
6201 | b = packetData[i];
|
6202 |
|
6203 | if (within708TextBlock(b)) {
|
6204 | i = this.handleText(i, service);
|
6205 | } else if (b === 0x18) {
|
6206 | i = this.multiByteCharacter(i, service);
|
6207 | } else if (b === 0x10) {
|
6208 | i = this.extendedCommands(i, service);
|
6209 | } else if (0x80 <= b && b <= 0x87) {
|
6210 | i = this.setCurrentWindow(i, service);
|
6211 | } else if (0x98 <= b && b <= 0x9f) {
|
6212 | i = this.defineWindow(i, service);
|
6213 | } else if (b === 0x88) {
|
6214 | i = this.clearWindows(i, service);
|
6215 | } else if (b === 0x8c) {
|
6216 | i = this.deleteWindows(i, service);
|
6217 | } else if (b === 0x89) {
|
6218 | i = this.displayWindows(i, service);
|
6219 | } else if (b === 0x8a) {
|
6220 | i = this.hideWindows(i, service);
|
6221 | } else if (b === 0x8b) {
|
6222 | i = this.toggleWindows(i, service);
|
6223 | } else if (b === 0x97) {
|
6224 | i = this.setWindowAttributes(i, service);
|
6225 | } else if (b === 0x90) {
|
6226 | i = this.setPenAttributes(i, service);
|
6227 | } else if (b === 0x91) {
|
6228 | i = this.setPenColor(i, service);
|
6229 | } else if (b === 0x92) {
|
6230 | i = this.setPenLocation(i, service);
|
6231 | } else if (b === 0x8f) {
|
6232 | service = this.reset(i, service);
|
6233 | } else if (b === 0x08) {
|
6234 |
|
6235 | service.currentWindow.backspace();
|
6236 | } else if (b === 0x0c) {
|
6237 |
|
6238 | service.currentWindow.clearText();
|
6239 | } else if (b === 0x0d) {
|
6240 |
|
6241 | service.currentWindow.pendingNewLine = true;
|
6242 | } else if (b === 0x0e) {
|
6243 |
|
6244 | service.currentWindow.clearText();
|
6245 | } else if (b === 0x8d) {
|
6246 |
|
6247 | i++;
|
6248 | } else ;
|
6249 | }
|
6250 | };
|
6251 | |
6252 |
|
6253 |
|
6254 |
|
6255 |
|
6256 |
|
6257 |
|
6258 |
|
6259 |
|
6260 | Cea708Stream.prototype.extendedCommands = function (i, service) {
|
6261 | var packetData = this.current708Packet.data;
|
6262 | var b = packetData[++i];
|
6263 |
|
6264 | if (within708TextBlock(b)) {
|
6265 | i = this.handleText(i, service, {
|
6266 | isExtended: true
|
6267 | });
|
6268 | }
|
6269 |
|
6270 | return i;
|
6271 | };
|
6272 | |
6273 |
|
6274 |
|
6275 |
|
6276 |
|
6277 |
|
6278 |
|
6279 |
|
6280 | Cea708Stream.prototype.getPts = function (byteIndex) {
|
6281 |
|
6282 | return this.current708Packet.ptsVals[Math.floor(byteIndex / 2)];
|
6283 | };
|
6284 | |
6285 |
|
6286 |
|
6287 |
|
6288 |
|
6289 |
|
6290 |
|
6291 |
|
6292 | Cea708Stream.prototype.initService = function (serviceNum, i) {
|
6293 | var serviceName = 'SERVICE' + serviceNum;
|
6294 | var self = this;
|
6295 | var serviceName;
|
6296 | var encoding;
|
6297 |
|
6298 | if (serviceName in this.serviceEncodings) {
|
6299 | encoding = this.serviceEncodings[serviceName];
|
6300 | }
|
6301 |
|
6302 | this.services[serviceNum] = new Cea708Service(serviceNum, encoding, self);
|
6303 | this.services[serviceNum].init(this.getPts(i), function (pts) {
|
6304 | self.flushDisplayed(pts, self.services[serviceNum]);
|
6305 | });
|
6306 | return this.services[serviceNum];
|
6307 | };
|
6308 | |
6309 |
|
6310 |
|
6311 |
|
6312 |
|
6313 |
|
6314 |
|
6315 |
|
6316 |
|
6317 | Cea708Stream.prototype.handleText = function (i, service, options) {
|
6318 | var isExtended = options && options.isExtended;
|
6319 | var isMultiByte = options && options.isMultiByte;
|
6320 | var packetData = this.current708Packet.data;
|
6321 | var extended = isExtended ? 0x1000 : 0x0000;
|
6322 | var currentByte = packetData[i];
|
6323 | var nextByte = packetData[i + 1];
|
6324 | var win = service.currentWindow;
|
6325 | var char;
|
6326 | var charCodeArray;
|
6327 |
|
6328 | if (service.textDecoder_ && !isExtended) {
|
6329 | if (isMultiByte) {
|
6330 | charCodeArray = [currentByte, nextByte];
|
6331 | i++;
|
6332 | } else {
|
6333 | charCodeArray = [currentByte];
|
6334 | }
|
6335 |
|
6336 | char = service.textDecoder_.decode(new Uint8Array(charCodeArray));
|
6337 | } else {
|
6338 | char = get708CharFromCode(extended | currentByte);
|
6339 | }
|
6340 |
|
6341 | if (win.pendingNewLine && !win.isEmpty()) {
|
6342 | win.newLine(this.getPts(i));
|
6343 | }
|
6344 |
|
6345 | win.pendingNewLine = false;
|
6346 | win.addText(char);
|
6347 | return i;
|
6348 | };
|
6349 | |
6350 |
|
6351 |
|
6352 |
|
6353 |
|
6354 |
|
6355 |
|
6356 |
|
6357 |
|
6358 | Cea708Stream.prototype.multiByteCharacter = function (i, service) {
|
6359 | var packetData = this.current708Packet.data;
|
6360 | var firstByte = packetData[i + 1];
|
6361 | var secondByte = packetData[i + 2];
|
6362 |
|
6363 | if (within708TextBlock(firstByte) && within708TextBlock(secondByte)) {
|
6364 | i = this.handleText(++i, service, {
|
6365 | isMultiByte: true
|
6366 | });
|
6367 | }
|
6368 |
|
6369 | return i;
|
6370 | };
|
6371 | |
6372 |
|
6373 |
|
6374 |
|
6375 |
|
6376 |
|
6377 |
|
6378 |
|
6379 |
|
6380 |
|
6381 |
|
6382 | Cea708Stream.prototype.setCurrentWindow = function (i, service) {
|
6383 | var packetData = this.current708Packet.data;
|
6384 | var b = packetData[i];
|
6385 | var windowNum = b & 0x07;
|
6386 | service.setCurrentWindow(windowNum);
|
6387 | return i;
|
6388 | };
|
6389 | |
6390 |
|
6391 |
|
6392 |
|
6393 |
|
6394 |
|
6395 |
|
6396 |
|
6397 |
|
6398 |
|
6399 |
|
6400 | Cea708Stream.prototype.defineWindow = function (i, service) {
|
6401 | var packetData = this.current708Packet.data;
|
6402 | var b = packetData[i];
|
6403 | var windowNum = b & 0x07;
|
6404 | service.setCurrentWindow(windowNum);
|
6405 | var win = service.currentWindow;
|
6406 | b = packetData[++i];
|
6407 | win.visible = (b & 0x20) >> 5;
|
6408 |
|
6409 | win.rowLock = (b & 0x10) >> 4;
|
6410 |
|
6411 | win.columnLock = (b & 0x08) >> 3;
|
6412 |
|
6413 | win.priority = b & 0x07;
|
6414 |
|
6415 | b = packetData[++i];
|
6416 | win.relativePositioning = (b & 0x80) >> 7;
|
6417 |
|
6418 | win.anchorVertical = b & 0x7f;
|
6419 |
|
6420 | b = packetData[++i];
|
6421 | win.anchorHorizontal = b;
|
6422 |
|
6423 | b = packetData[++i];
|
6424 | win.anchorPoint = (b & 0xf0) >> 4;
|
6425 |
|
6426 | win.rowCount = b & 0x0f;
|
6427 |
|
6428 | b = packetData[++i];
|
6429 | win.columnCount = b & 0x3f;
|
6430 |
|
6431 | b = packetData[++i];
|
6432 | win.windowStyle = (b & 0x38) >> 3;
|
6433 |
|
6434 | win.penStyle = b & 0x07;
|
6435 |
|
6436 |
|
6437 | win.virtualRowCount = win.rowCount + 1;
|
6438 | return i;
|
6439 | };
|
6440 | |
6441 |
|
6442 |
|
6443 |
|
6444 |
|
6445 |
|
6446 |
|
6447 |
|
6448 |
|
6449 |
|
6450 |
|
6451 | Cea708Stream.prototype.setWindowAttributes = function (i, service) {
|
6452 | var packetData = this.current708Packet.data;
|
6453 | var b = packetData[i];
|
6454 | var winAttr = service.currentWindow.winAttr;
|
6455 | b = packetData[++i];
|
6456 | winAttr.fillOpacity = (b & 0xc0) >> 6;
|
6457 |
|
6458 | winAttr.fillRed = (b & 0x30) >> 4;
|
6459 |
|
6460 | winAttr.fillGreen = (b & 0x0c) >> 2;
|
6461 |
|
6462 | winAttr.fillBlue = b & 0x03;
|
6463 |
|
6464 | b = packetData[++i];
|
6465 | winAttr.borderType = (b & 0xc0) >> 6;
|
6466 |
|
6467 | winAttr.borderRed = (b & 0x30) >> 4;
|
6468 |
|
6469 | winAttr.borderGreen = (b & 0x0c) >> 2;
|
6470 |
|
6471 | winAttr.borderBlue = b & 0x03;
|
6472 |
|
6473 | b = packetData[++i];
|
6474 | winAttr.borderType += (b & 0x80) >> 5;
|
6475 |
|
6476 | winAttr.wordWrap = (b & 0x40) >> 6;
|
6477 |
|
6478 | winAttr.printDirection = (b & 0x30) >> 4;
|
6479 |
|
6480 | winAttr.scrollDirection = (b & 0x0c) >> 2;
|
6481 |
|
6482 | winAttr.justify = b & 0x03;
|
6483 |
|
6484 | b = packetData[++i];
|
6485 | winAttr.effectSpeed = (b & 0xf0) >> 4;
|
6486 |
|
6487 | winAttr.effectDirection = (b & 0x0c) >> 2;
|
6488 |
|
6489 | winAttr.displayEffect = b & 0x03;
|
6490 |
|
6491 | return i;
|
6492 | };
|
6493 | |
6494 |
|
6495 |
|
6496 |
|
6497 |
|
6498 |
|
6499 |
|
6500 |
|
6501 | Cea708Stream.prototype.flushDisplayed = function (pts, service) {
|
6502 | var displayedText = [];
|
6503 |
|
6504 |
|
6505 | for (var winId = 0; winId < 8; winId++) {
|
6506 | if (service.windows[winId].visible && !service.windows[winId].isEmpty()) {
|
6507 | displayedText.push(service.windows[winId].getText());
|
6508 | }
|
6509 | }
|
6510 |
|
6511 | service.endPts = pts;
|
6512 | service.text = displayedText.join('\n\n');
|
6513 | this.pushCaption(service);
|
6514 | service.startPts = pts;
|
6515 | };
|
6516 | |
6517 |
|
6518 |
|
6519 |
|
6520 |
|
6521 |
|
6522 |
|
6523 | Cea708Stream.prototype.pushCaption = function (service) {
|
6524 | if (service.text !== '') {
|
6525 | this.trigger('data', {
|
6526 | startPts: service.startPts,
|
6527 | endPts: service.endPts,
|
6528 | text: service.text,
|
6529 | stream: 'cc708_' + service.serviceNum
|
6530 | });
|
6531 | service.text = '';
|
6532 | service.startPts = service.endPts;
|
6533 | }
|
6534 | };
|
6535 | |
6536 |
|
6537 |
|
6538 |
|
6539 |
|
6540 |
|
6541 |
|
6542 |
|
6543 |
|
6544 |
|
6545 |
|
6546 | Cea708Stream.prototype.displayWindows = function (i, service) {
|
6547 | var packetData = this.current708Packet.data;
|
6548 | var b = packetData[++i];
|
6549 | var pts = this.getPts(i);
|
6550 | this.flushDisplayed(pts, service);
|
6551 |
|
6552 | for (var winId = 0; winId < 8; winId++) {
|
6553 | if (b & 0x01 << winId) {
|
6554 | service.windows[winId].visible = 1;
|
6555 | }
|
6556 | }
|
6557 |
|
6558 | return i;
|
6559 | };
|
6560 | |
6561 |
|
6562 |
|
6563 |
|
6564 |
|
6565 |
|
6566 |
|
6567 |
|
6568 |
|
6569 |
|
6570 |
|
6571 | Cea708Stream.prototype.hideWindows = function (i, service) {
|
6572 | var packetData = this.current708Packet.data;
|
6573 | var b = packetData[++i];
|
6574 | var pts = this.getPts(i);
|
6575 | this.flushDisplayed(pts, service);
|
6576 |
|
6577 | for (var winId = 0; winId < 8; winId++) {
|
6578 | if (b & 0x01 << winId) {
|
6579 | service.windows[winId].visible = 0;
|
6580 | }
|
6581 | }
|
6582 |
|
6583 | return i;
|
6584 | };
|
6585 | |
6586 |
|
6587 |
|
6588 |
|
6589 |
|
6590 |
|
6591 |
|
6592 |
|
6593 |
|
6594 |
|
6595 |
|
6596 | Cea708Stream.prototype.toggleWindows = function (i, service) {
|
6597 | var packetData = this.current708Packet.data;
|
6598 | var b = packetData[++i];
|
6599 | var pts = this.getPts(i);
|
6600 | this.flushDisplayed(pts, service);
|
6601 |
|
6602 | for (var winId = 0; winId < 8; winId++) {
|
6603 | if (b & 0x01 << winId) {
|
6604 | service.windows[winId].visible ^= 1;
|
6605 | }
|
6606 | }
|
6607 |
|
6608 | return i;
|
6609 | };
|
6610 | |
6611 |
|
6612 |
|
6613 |
|
6614 |
|
6615 |
|
6616 |
|
6617 |
|
6618 |
|
6619 |
|
6620 |
|
6621 | Cea708Stream.prototype.clearWindows = function (i, service) {
|
6622 | var packetData = this.current708Packet.data;
|
6623 | var b = packetData[++i];
|
6624 | var pts = this.getPts(i);
|
6625 | this.flushDisplayed(pts, service);
|
6626 |
|
6627 | for (var winId = 0; winId < 8; winId++) {
|
6628 | if (b & 0x01 << winId) {
|
6629 | service.windows[winId].clearText();
|
6630 | }
|
6631 | }
|
6632 |
|
6633 | return i;
|
6634 | };
|
6635 | |
6636 |
|
6637 |
|
6638 |
|
6639 |
|
6640 |
|
6641 |
|
6642 |
|
6643 |
|
6644 |
|
6645 |
|
6646 | Cea708Stream.prototype.deleteWindows = function (i, service) {
|
6647 | var packetData = this.current708Packet.data;
|
6648 | var b = packetData[++i];
|
6649 | var pts = this.getPts(i);
|
6650 | this.flushDisplayed(pts, service);
|
6651 |
|
6652 | for (var winId = 0; winId < 8; winId++) {
|
6653 | if (b & 0x01 << winId) {
|
6654 | service.windows[winId].reset();
|
6655 | }
|
6656 | }
|
6657 |
|
6658 | return i;
|
6659 | };
|
6660 | |
6661 |
|
6662 |
|
6663 |
|
6664 |
|
6665 |
|
6666 |
|
6667 |
|
6668 |
|
6669 |
|
6670 |
|
6671 | Cea708Stream.prototype.setPenAttributes = function (i, service) {
|
6672 | var packetData = this.current708Packet.data;
|
6673 | var b = packetData[i];
|
6674 | var penAttr = service.currentWindow.penAttr;
|
6675 | b = packetData[++i];
|
6676 | penAttr.textTag = (b & 0xf0) >> 4;
|
6677 |
|
6678 | penAttr.offset = (b & 0x0c) >> 2;
|
6679 |
|
6680 | penAttr.penSize = b & 0x03;
|
6681 |
|
6682 | b = packetData[++i];
|
6683 | penAttr.italics = (b & 0x80) >> 7;
|
6684 |
|
6685 | penAttr.underline = (b & 0x40) >> 6;
|
6686 |
|
6687 | penAttr.edgeType = (b & 0x38) >> 3;
|
6688 |
|
6689 | penAttr.fontStyle = b & 0x07;
|
6690 |
|
6691 | return i;
|
6692 | };
|
6693 | |
6694 |
|
6695 |
|
6696 |
|
6697 |
|
6698 |
|
6699 |
|
6700 |
|
6701 |
|
6702 |
|
6703 |
|
6704 | Cea708Stream.prototype.setPenColor = function (i, service) {
|
6705 | var packetData = this.current708Packet.data;
|
6706 | var b = packetData[i];
|
6707 | var penColor = service.currentWindow.penColor;
|
6708 | b = packetData[++i];
|
6709 | penColor.fgOpacity = (b & 0xc0) >> 6;
|
6710 |
|
6711 | penColor.fgRed = (b & 0x30) >> 4;
|
6712 |
|
6713 | penColor.fgGreen = (b & 0x0c) >> 2;
|
6714 |
|
6715 | penColor.fgBlue = b & 0x03;
|
6716 |
|
6717 | b = packetData[++i];
|
6718 | penColor.bgOpacity = (b & 0xc0) >> 6;
|
6719 |
|
6720 | penColor.bgRed = (b & 0x30) >> 4;
|
6721 |
|
6722 | penColor.bgGreen = (b & 0x0c) >> 2;
|
6723 |
|
6724 | penColor.bgBlue = b & 0x03;
|
6725 |
|
6726 | b = packetData[++i];
|
6727 | penColor.edgeRed = (b & 0x30) >> 4;
|
6728 |
|
6729 | penColor.edgeGreen = (b & 0x0c) >> 2;
|
6730 |
|
6731 | penColor.edgeBlue = b & 0x03;
|
6732 |
|
6733 | return i;
|
6734 | };
|
6735 | |
6736 |
|
6737 |
|
6738 |
|
6739 |
|
6740 |
|
6741 |
|
6742 |
|
6743 |
|
6744 |
|
6745 |
|
6746 | Cea708Stream.prototype.setPenLocation = function (i, service) {
|
6747 | var packetData = this.current708Packet.data;
|
6748 | var b = packetData[i];
|
6749 | var penLoc = service.currentWindow.penLoc;
|
6750 |
|
6751 | service.currentWindow.pendingNewLine = true;
|
6752 | b = packetData[++i];
|
6753 | penLoc.row = b & 0x0f;
|
6754 |
|
6755 | b = packetData[++i];
|
6756 | penLoc.column = b & 0x3f;
|
6757 |
|
6758 | return i;
|
6759 | };
|
6760 | |
6761 |
|
6762 |
|
6763 |
|
6764 |
|
6765 |
|
6766 |
|
6767 |
|
6768 |
|
6769 |
|
6770 |
|
6771 | Cea708Stream.prototype.reset = function (i, service) {
|
6772 | var pts = this.getPts(i);
|
6773 | this.flushDisplayed(pts, service);
|
6774 | return this.initService(service.serviceNum, i);
|
6775 | };
|
6776 |
|
6777 |
|
6778 |
|
6779 |
|
6780 |
|
6781 |
|
6782 |
|
6783 |
|
6784 | var CHARACTER_TRANSLATION = {
|
6785 | 0x2a: 0xe1,
|
6786 |
|
6787 | 0x5c: 0xe9,
|
6788 |
|
6789 | 0x5e: 0xed,
|
6790 |
|
6791 | 0x5f: 0xf3,
|
6792 |
|
6793 | 0x60: 0xfa,
|
6794 |
|
6795 | 0x7b: 0xe7,
|
6796 |
|
6797 | 0x7c: 0xf7,
|
6798 |
|
6799 | 0x7d: 0xd1,
|
6800 |
|
6801 | 0x7e: 0xf1,
|
6802 |
|
6803 | 0x7f: 0x2588,
|
6804 |
|
6805 | 0x0130: 0xae,
|
6806 |
|
6807 | 0x0131: 0xb0,
|
6808 |
|
6809 | 0x0132: 0xbd,
|
6810 |
|
6811 | 0x0133: 0xbf,
|
6812 |
|
6813 | 0x0134: 0x2122,
|
6814 |
|
6815 | 0x0135: 0xa2,
|
6816 |
|
6817 | 0x0136: 0xa3,
|
6818 |
|
6819 | 0x0137: 0x266a,
|
6820 |
|
6821 | 0x0138: 0xe0,
|
6822 |
|
6823 | 0x0139: 0xa0,
|
6824 |
|
6825 | 0x013a: 0xe8,
|
6826 |
|
6827 | 0x013b: 0xe2,
|
6828 |
|
6829 | 0x013c: 0xea,
|
6830 |
|
6831 | 0x013d: 0xee,
|
6832 |
|
6833 | 0x013e: 0xf4,
|
6834 |
|
6835 | 0x013f: 0xfb,
|
6836 |
|
6837 | 0x0220: 0xc1,
|
6838 |
|
6839 | 0x0221: 0xc9,
|
6840 |
|
6841 | 0x0222: 0xd3,
|
6842 |
|
6843 | 0x0223: 0xda,
|
6844 |
|
6845 | 0x0224: 0xdc,
|
6846 |
|
6847 | 0x0225: 0xfc,
|
6848 |
|
6849 | 0x0226: 0x2018,
|
6850 |
|
6851 | 0x0227: 0xa1,
|
6852 |
|
6853 | 0x0228: 0x2a,
|
6854 |
|
6855 | 0x0229: 0x27,
|
6856 |
|
6857 | 0x022a: 0x2014,
|
6858 |
|
6859 | 0x022b: 0xa9,
|
6860 |
|
6861 | 0x022c: 0x2120,
|
6862 |
|
6863 | 0x022d: 0x2022,
|
6864 |
|
6865 | 0x022e: 0x201c,
|
6866 |
|
6867 | 0x022f: 0x201d,
|
6868 |
|
6869 | 0x0230: 0xc0,
|
6870 |
|
6871 | 0x0231: 0xc2,
|
6872 |
|
6873 | 0x0232: 0xc7,
|
6874 |
|
6875 | 0x0233: 0xc8,
|
6876 |
|
6877 | 0x0234: 0xca,
|
6878 |
|
6879 | 0x0235: 0xcb,
|
6880 |
|
6881 | 0x0236: 0xeb,
|
6882 |
|
6883 | 0x0237: 0xce,
|
6884 |
|
6885 | 0x0238: 0xcf,
|
6886 |
|
6887 | 0x0239: 0xef,
|
6888 |
|
6889 | 0x023a: 0xd4,
|
6890 |
|
6891 | 0x023b: 0xd9,
|
6892 |
|
6893 | 0x023c: 0xf9,
|
6894 |
|
6895 | 0x023d: 0xdb,
|
6896 |
|
6897 | 0x023e: 0xab,
|
6898 |
|
6899 | 0x023f: 0xbb,
|
6900 |
|
6901 | 0x0320: 0xc3,
|
6902 |
|
6903 | 0x0321: 0xe3,
|
6904 |
|
6905 | 0x0322: 0xcd,
|
6906 |
|
6907 | 0x0323: 0xcc,
|
6908 |
|
6909 | 0x0324: 0xec,
|
6910 |
|
6911 | 0x0325: 0xd2,
|
6912 |
|
6913 | 0x0326: 0xf2,
|
6914 |
|
6915 | 0x0327: 0xd5,
|
6916 |
|
6917 | 0x0328: 0xf5,
|
6918 |
|
6919 | 0x0329: 0x7b,
|
6920 |
|
6921 | 0x032a: 0x7d,
|
6922 |
|
6923 | 0x032b: 0x5c,
|
6924 |
|
6925 | 0x032c: 0x5e,
|
6926 |
|
6927 | 0x032d: 0x5f,
|
6928 |
|
6929 | 0x032e: 0x7c,
|
6930 |
|
6931 | 0x032f: 0x7e,
|
6932 |
|
6933 | 0x0330: 0xc4,
|
6934 |
|
6935 | 0x0331: 0xe4,
|
6936 |
|
6937 | 0x0332: 0xd6,
|
6938 |
|
6939 | 0x0333: 0xf6,
|
6940 |
|
6941 | 0x0334: 0xdf,
|
6942 |
|
6943 | 0x0335: 0xa5,
|
6944 |
|
6945 | 0x0336: 0xa4,
|
6946 |
|
6947 | 0x0337: 0x2502,
|
6948 |
|
6949 | 0x0338: 0xc5,
|
6950 |
|
6951 | 0x0339: 0xe5,
|
6952 |
|
6953 | 0x033a: 0xd8,
|
6954 |
|
6955 | 0x033b: 0xf8,
|
6956 |
|
6957 | 0x033c: 0x250c,
|
6958 |
|
6959 | 0x033d: 0x2510,
|
6960 |
|
6961 | 0x033e: 0x2514,
|
6962 |
|
6963 | 0x033f: 0x2518
|
6964 |
|
6965 | };
|
6966 |
|
6967 | var getCharFromCode = function getCharFromCode(code) {
|
6968 | if (code === null) {
|
6969 | return '';
|
6970 | }
|
6971 |
|
6972 | code = CHARACTER_TRANSLATION[code] || code;
|
6973 | return String.fromCharCode(code);
|
6974 | };
|
6975 |
|
6976 |
|
6977 | var BOTTOM_ROW = 14;
|
6978 |
|
6979 |
|
6980 | var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620, 0x1700, 0x1720, 0x1000, 0x1300, 0x1320, 0x1400, 0x1420];
|
6981 |
|
6982 |
|
6983 | var createDisplayBuffer = function createDisplayBuffer() {
|
6984 | var result = [],
|
6985 | i = BOTTOM_ROW + 1;
|
6986 |
|
6987 | while (i--) {
|
6988 | result.push('');
|
6989 | }
|
6990 |
|
6991 | return result;
|
6992 | };
|
6993 |
|
6994 | var Cea608Stream = function Cea608Stream(field, dataChannel) {
|
6995 | Cea608Stream.prototype.init.call(this);
|
6996 | this.field_ = field || 0;
|
6997 | this.dataChannel_ = dataChannel || 0;
|
6998 | this.name_ = 'CC' + ((this.field_ << 1 | this.dataChannel_) + 1);
|
6999 | this.setConstants();
|
7000 | this.reset();
|
7001 |
|
7002 | this.push = function (packet) {
|
7003 | var data, swap, char0, char1, text;
|
7004 |
|
7005 | data = packet.ccData & 0x7f7f;
|
7006 |
|
7007 | if (data === this.lastControlCode_) {
|
7008 | this.lastControlCode_ = null;
|
7009 | return;
|
7010 | }
|
7011 |
|
7012 |
|
7013 | if ((data & 0xf000) === 0x1000) {
|
7014 | this.lastControlCode_ = data;
|
7015 | } else if (data !== this.PADDING_) {
|
7016 | this.lastControlCode_ = null;
|
7017 | }
|
7018 |
|
7019 | char0 = data >>> 8;
|
7020 | char1 = data & 0xff;
|
7021 |
|
7022 | if (data === this.PADDING_) {
|
7023 | return;
|
7024 | } else if (data === this.RESUME_CAPTION_LOADING_) {
|
7025 | this.mode_ = 'popOn';
|
7026 | } else if (data === this.END_OF_CAPTION_) {
|
7027 |
|
7028 |
|
7029 |
|
7030 |
|
7031 | this.mode_ = 'popOn';
|
7032 | this.clearFormatting(packet.pts);
|
7033 |
|
7034 | this.flushDisplayed(packet.pts);
|
7035 |
|
7036 | swap = this.displayed_;
|
7037 | this.displayed_ = this.nonDisplayed_;
|
7038 | this.nonDisplayed_ = swap;
|
7039 |
|
7040 | this.startPts_ = packet.pts;
|
7041 | } else if (data === this.ROLL_UP_2_ROWS_) {
|
7042 | this.rollUpRows_ = 2;
|
7043 | this.setRollUp(packet.pts);
|
7044 | } else if (data === this.ROLL_UP_3_ROWS_) {
|
7045 | this.rollUpRows_ = 3;
|
7046 | this.setRollUp(packet.pts);
|
7047 | } else if (data === this.ROLL_UP_4_ROWS_) {
|
7048 | this.rollUpRows_ = 4;
|
7049 | this.setRollUp(packet.pts);
|
7050 | } else if (data === this.CARRIAGE_RETURN_) {
|
7051 | this.clearFormatting(packet.pts);
|
7052 | this.flushDisplayed(packet.pts);
|
7053 | this.shiftRowsUp_();
|
7054 | this.startPts_ = packet.pts;
|
7055 | } else if (data === this.BACKSPACE_) {
|
7056 | if (this.mode_ === 'popOn') {
|
7057 | this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
|
7058 | } else {
|
7059 | this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
|
7060 | }
|
7061 | } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
|
7062 | this.flushDisplayed(packet.pts);
|
7063 | this.displayed_ = createDisplayBuffer();
|
7064 | } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
|
7065 | this.nonDisplayed_ = createDisplayBuffer();
|
7066 | } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
|
7067 | if (this.mode_ !== 'paintOn') {
|
7068 |
|
7069 |
|
7070 | this.flushDisplayed(packet.pts);
|
7071 | this.displayed_ = createDisplayBuffer();
|
7072 | }
|
7073 |
|
7074 | this.mode_ = 'paintOn';
|
7075 | this.startPts_ = packet.pts;
|
7076 | } else if (this.isSpecialCharacter(char0, char1)) {
|
7077 |
|
7078 |
|
7079 |
|
7080 |
|
7081 | char0 = (char0 & 0x03) << 8;
|
7082 | text = getCharFromCode(char0 | char1);
|
7083 | this[this.mode_](packet.pts, text);
|
7084 | this.column_++;
|
7085 | } else if (this.isExtCharacter(char0, char1)) {
|
7086 |
|
7087 |
|
7088 |
|
7089 |
|
7090 |
|
7091 | if (this.mode_ === 'popOn') {
|
7092 | this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
|
7093 | } else {
|
7094 | this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
|
7095 | }
|
7096 |
|
7097 |
|
7098 |
|
7099 |
|
7100 |
|
7101 | char0 = (char0 & 0x03) << 8;
|
7102 | text = getCharFromCode(char0 | char1);
|
7103 | this[this.mode_](packet.pts, text);
|
7104 | this.column_++;
|
7105 | } else if (this.isMidRowCode(char0, char1)) {
|
7106 |
|
7107 | this.clearFormatting(packet.pts);
|
7108 |
|
7109 |
|
7110 | this[this.mode_](packet.pts, ' ');
|
7111 | this.column_++;
|
7112 |
|
7113 | if ((char1 & 0xe) === 0xe) {
|
7114 | this.addFormatting(packet.pts, ['i']);
|
7115 | }
|
7116 |
|
7117 | if ((char1 & 0x1) === 0x1) {
|
7118 | this.addFormatting(packet.pts, ['u']);
|
7119 | }
|
7120 |
|
7121 | } else if (this.isOffsetControlCode(char0, char1)) {
|
7122 |
|
7123 |
|
7124 |
|
7125 |
|
7126 | this.column_ += char1 & 0x03;
|
7127 | } else if (this.isPAC(char0, char1)) {
|
7128 |
|
7129 |
|
7130 | var row = ROWS.indexOf(data & 0x1f20);
|
7131 |
|
7132 | if (this.mode_ === 'rollUp') {
|
7133 |
|
7134 |
|
7135 |
|
7136 | if (row - this.rollUpRows_ + 1 < 0) {
|
7137 | row = this.rollUpRows_ - 1;
|
7138 | }
|
7139 |
|
7140 | this.setRollUp(packet.pts, row);
|
7141 | }
|
7142 |
|
7143 | if (row !== this.row_) {
|
7144 |
|
7145 | this.clearFormatting(packet.pts);
|
7146 | this.row_ = row;
|
7147 | }
|
7148 |
|
7149 |
|
7150 |
|
7151 | if (char1 & 0x1 && this.formatting_.indexOf('u') === -1) {
|
7152 | this.addFormatting(packet.pts, ['u']);
|
7153 | }
|
7154 |
|
7155 | if ((data & 0x10) === 0x10) {
|
7156 |
|
7157 |
|
7158 |
|
7159 |
|
7160 | this.column_ = ((data & 0xe) >> 1) * 4;
|
7161 | }
|
7162 |
|
7163 | if (this.isColorPAC(char1)) {
|
7164 |
|
7165 |
|
7166 |
|
7167 |
|
7168 | if ((char1 & 0xe) === 0xe) {
|
7169 | this.addFormatting(packet.pts, ['i']);
|
7170 | }
|
7171 | }
|
7172 |
|
7173 | } else if (this.isNormalChar(char0)) {
|
7174 | if (char1 === 0x00) {
|
7175 | char1 = null;
|
7176 | }
|
7177 |
|
7178 | text = getCharFromCode(char0);
|
7179 | text += getCharFromCode(char1);
|
7180 | this[this.mode_](packet.pts, text);
|
7181 | this.column_ += text.length;
|
7182 | }
|
7183 |
|
7184 | };
|
7185 | };
|
7186 |
|
7187 | Cea608Stream.prototype = new stream();
|
7188 |
|
7189 |
|
7190 | Cea608Stream.prototype.flushDisplayed = function (pts) {
|
7191 | var content = this.displayed_
|
7192 | .map(function (row, index) {
|
7193 | try {
|
7194 | return row.trim();
|
7195 | } catch (e) {
|
7196 |
|
7197 |
|
7198 |
|
7199 | this.trigger('log', {
|
7200 | level: 'warn',
|
7201 | message: 'Skipping a malformed 608 caption at index ' + index + '.'
|
7202 | });
|
7203 | return '';
|
7204 | }
|
7205 | }, this)
|
7206 | .join('\n')
|
7207 | .replace(/^\n+|\n+$/g, '');
|
7208 |
|
7209 | if (content.length) {
|
7210 | this.trigger('data', {
|
7211 | startPts: this.startPts_,
|
7212 | endPts: pts,
|
7213 | text: content,
|
7214 | stream: this.name_
|
7215 | });
|
7216 | }
|
7217 | };
|
7218 | |
7219 |
|
7220 |
|
7221 |
|
7222 |
|
7223 | Cea608Stream.prototype.reset = function () {
|
7224 | this.mode_ = 'popOn';
|
7225 |
|
7226 |
|
7227 |
|
7228 |
|
7229 | this.topRow_ = 0;
|
7230 | this.startPts_ = 0;
|
7231 | this.displayed_ = createDisplayBuffer();
|
7232 | this.nonDisplayed_ = createDisplayBuffer();
|
7233 | this.lastControlCode_ = null;
|
7234 |
|
7235 | this.column_ = 0;
|
7236 | this.row_ = BOTTOM_ROW;
|
7237 | this.rollUpRows_ = 2;
|
7238 |
|
7239 | this.formatting_ = [];
|
7240 | };
|
7241 | |
7242 |
|
7243 |
|
7244 |
|
7245 |
|
7246 | Cea608Stream.prototype.setConstants = function () {
|
7247 |
|
7248 |
|
7249 |
|
7250 |
|
7251 |
|
7252 |
|
7253 |
|
7254 |
|
7255 |
|
7256 |
|
7257 |
|
7258 |
|
7259 | if (this.dataChannel_ === 0) {
|
7260 | this.BASE_ = 0x10;
|
7261 | this.EXT_ = 0x11;
|
7262 | this.CONTROL_ = (0x14 | this.field_) << 8;
|
7263 | this.OFFSET_ = 0x17;
|
7264 | } else if (this.dataChannel_ === 1) {
|
7265 | this.BASE_ = 0x18;
|
7266 | this.EXT_ = 0x19;
|
7267 | this.CONTROL_ = (0x1c | this.field_) << 8;
|
7268 | this.OFFSET_ = 0x1f;
|
7269 | }
|
7270 |
|
7271 |
|
7272 |
|
7273 |
|
7274 |
|
7275 | this.PADDING_ = 0x0000;
|
7276 |
|
7277 | this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
|
7278 | this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f;
|
7279 |
|
7280 | this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
|
7281 | this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
|
7282 | this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
|
7283 | this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d;
|
7284 |
|
7285 | this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29;
|
7286 |
|
7287 | this.BACKSPACE_ = this.CONTROL_ | 0x21;
|
7288 | this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
|
7289 | this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
|
7290 | };
|
7291 | |
7292 |
|
7293 |
|
7294 |
|
7295 |
|
7296 |
|
7297 |
|
7298 |
|
7299 |
|
7300 |
|
7301 |
|
7302 |
|
7303 |
|
7304 | Cea608Stream.prototype.isSpecialCharacter = function (char0, char1) {
|
7305 | return char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f;
|
7306 | };
|
7307 | |
7308 |
|
7309 |
|
7310 |
|
7311 |
|
7312 |
|
7313 |
|
7314 |
|
7315 |
|
7316 |
|
7317 |
|
7318 |
|
7319 |
|
7320 | Cea608Stream.prototype.isExtCharacter = function (char0, char1) {
|
7321 | return (char0 === this.EXT_ + 1 || char0 === this.EXT_ + 2) && char1 >= 0x20 && char1 <= 0x3f;
|
7322 | };
|
7323 | |
7324 |
|
7325 |
|
7326 |
|
7327 |
|
7328 |
|
7329 |
|
7330 |
|
7331 |
|
7332 |
|
7333 |
|
7334 |
|
7335 |
|
7336 | Cea608Stream.prototype.isMidRowCode = function (char0, char1) {
|
7337 | return char0 === this.EXT_ && char1 >= 0x20 && char1 <= 0x2f;
|
7338 | };
|
7339 | |
7340 |
|
7341 |
|
7342 |
|
7343 |
|
7344 |
|
7345 |
|
7346 |
|
7347 |
|
7348 |
|
7349 |
|
7350 |
|
7351 |
|
7352 | Cea608Stream.prototype.isOffsetControlCode = function (char0, char1) {
|
7353 | return char0 === this.OFFSET_ && char1 >= 0x21 && char1 <= 0x23;
|
7354 | };
|
7355 | |
7356 |
|
7357 |
|
7358 |
|
7359 |
|
7360 |
|
7361 |
|
7362 |
|
7363 |
|
7364 |
|
7365 |
|
7366 |
|
7367 |
|
7368 | Cea608Stream.prototype.isPAC = function (char0, char1) {
|
7369 | return char0 >= this.BASE_ && char0 < this.BASE_ + 8 && char1 >= 0x40 && char1 <= 0x7f;
|
7370 | };
|
7371 | |
7372 |
|
7373 |
|
7374 |
|
7375 |
|
7376 |
|
7377 |
|
7378 |
|
7379 |
|
7380 |
|
7381 |
|
7382 | Cea608Stream.prototype.isColorPAC = function (char1) {
|
7383 | return char1 >= 0x40 && char1 <= 0x4f || char1 >= 0x60 && char1 <= 0x7f;
|
7384 | };
|
7385 | |
7386 |
|
7387 |
|
7388 |
|
7389 |
|
7390 |
|
7391 |
|
7392 |
|
7393 |
|
7394 |
|
7395 | Cea608Stream.prototype.isNormalChar = function (char) {
|
7396 | return char >= 0x20 && char <= 0x7f;
|
7397 | };
|
7398 | |
7399 |
|
7400 |
|
7401 |
|
7402 |
|
7403 |
|
7404 |
|
7405 |
|
7406 |
|
7407 | Cea608Stream.prototype.setRollUp = function (pts, newBaseRow) {
|
7408 |
|
7409 | if (this.mode_ !== 'rollUp') {
|
7410 | this.row_ = BOTTOM_ROW;
|
7411 | this.mode_ = 'rollUp';
|
7412 |
|
7413 | this.flushDisplayed(pts);
|
7414 | this.nonDisplayed_ = createDisplayBuffer();
|
7415 | this.displayed_ = createDisplayBuffer();
|
7416 | }
|
7417 |
|
7418 | if (newBaseRow !== undefined && newBaseRow !== this.row_) {
|
7419 |
|
7420 | for (var i = 0; i < this.rollUpRows_; i++) {
|
7421 | this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
|
7422 | this.displayed_[this.row_ - i] = '';
|
7423 | }
|
7424 | }
|
7425 |
|
7426 | if (newBaseRow === undefined) {
|
7427 | newBaseRow = this.row_;
|
7428 | }
|
7429 |
|
7430 | this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
|
7431 | };
|
7432 |
|
7433 |
|
7434 |
|
7435 | Cea608Stream.prototype.addFormatting = function (pts, format) {
|
7436 | this.formatting_ = this.formatting_.concat(format);
|
7437 | var text = format.reduce(function (text, format) {
|
7438 | return text + '<' + format + '>';
|
7439 | }, '');
|
7440 | this[this.mode_](pts, text);
|
7441 | };
|
7442 |
|
7443 |
|
7444 |
|
7445 | Cea608Stream.prototype.clearFormatting = function (pts) {
|
7446 | if (!this.formatting_.length) {
|
7447 | return;
|
7448 | }
|
7449 |
|
7450 | var text = this.formatting_.reverse().reduce(function (text, format) {
|
7451 | return text + '</' + format + '>';
|
7452 | }, '');
|
7453 | this.formatting_ = [];
|
7454 | this[this.mode_](pts, text);
|
7455 | };
|
7456 |
|
7457 |
|
7458 | Cea608Stream.prototype.popOn = function (pts, text) {
|
7459 | var baseRow = this.nonDisplayed_[this.row_];
|
7460 |
|
7461 | baseRow += text;
|
7462 | this.nonDisplayed_[this.row_] = baseRow;
|
7463 | };
|
7464 |
|
7465 | Cea608Stream.prototype.rollUp = function (pts, text) {
|
7466 | var baseRow = this.displayed_[this.row_];
|
7467 | baseRow += text;
|
7468 | this.displayed_[this.row_] = baseRow;
|
7469 | };
|
7470 |
|
7471 | Cea608Stream.prototype.shiftRowsUp_ = function () {
|
7472 | var i;
|
7473 |
|
7474 | for (i = 0; i < this.topRow_; i++) {
|
7475 | this.displayed_[i] = '';
|
7476 | }
|
7477 |
|
7478 | for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
|
7479 | this.displayed_[i] = '';
|
7480 | }
|
7481 |
|
7482 |
|
7483 | for (i = this.topRow_; i < this.row_; i++) {
|
7484 | this.displayed_[i] = this.displayed_[i + 1];
|
7485 | }
|
7486 |
|
7487 |
|
7488 | this.displayed_[this.row_] = '';
|
7489 | };
|
7490 |
|
7491 | Cea608Stream.prototype.paintOn = function (pts, text) {
|
7492 | var baseRow = this.displayed_[this.row_];
|
7493 | baseRow += text;
|
7494 | this.displayed_[this.row_] = baseRow;
|
7495 | };
|
7496 |
|
7497 |
|
7498 | var captionStream = {
|
7499 | CaptionStream: CaptionStream$1,
|
7500 | Cea608Stream: Cea608Stream,
|
7501 | Cea708Stream: Cea708Stream
|
7502 | };
|
7503 | |
7504 |
|
7505 |
|
7506 |
|
7507 |
|
7508 |
|
7509 |
|
7510 | var streamTypes = {
|
7511 | H264_STREAM_TYPE: 0x1B,
|
7512 | ADTS_STREAM_TYPE: 0x0F,
|
7513 | METADATA_STREAM_TYPE: 0x15
|
7514 | };
|
7515 | var MAX_TS = 8589934592;
|
7516 | var RO_THRESH = 4294967296;
|
7517 | var TYPE_SHARED = 'shared';
|
7518 |
|
7519 | var handleRollover$1 = function handleRollover(value, reference) {
|
7520 | var direction = 1;
|
7521 |
|
7522 | if (value > reference) {
|
7523 |
|
7524 |
|
7525 |
|
7526 |
|
7527 |
|
7528 |
|
7529 |
|
7530 | direction = -1;
|
7531 | }
|
7532 |
|
7533 |
|
7534 |
|
7535 | while (Math.abs(reference - value) > RO_THRESH) {
|
7536 | value += direction * MAX_TS;
|
7537 | }
|
7538 |
|
7539 | return value;
|
7540 | };
|
7541 |
|
7542 | var TimestampRolloverStream$1 = function TimestampRolloverStream(type) {
|
7543 | var lastDTS, referenceDTS;
|
7544 | TimestampRolloverStream.prototype.init.call(this);
|
7545 |
|
7546 |
|
7547 |
|
7548 | this.type_ = type || TYPE_SHARED;
|
7549 |
|
7550 | this.push = function (data) {
|
7551 |
|
7552 |
|
7553 | if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
|
7554 | return;
|
7555 | }
|
7556 |
|
7557 | if (referenceDTS === undefined) {
|
7558 | referenceDTS = data.dts;
|
7559 | }
|
7560 |
|
7561 | data.dts = handleRollover$1(data.dts, referenceDTS);
|
7562 | data.pts = handleRollover$1(data.pts, referenceDTS);
|
7563 | lastDTS = data.dts;
|
7564 | this.trigger('data', data);
|
7565 | };
|
7566 |
|
7567 | this.flush = function () {
|
7568 | referenceDTS = lastDTS;
|
7569 | this.trigger('done');
|
7570 | };
|
7571 |
|
7572 | this.endTimeline = function () {
|
7573 | this.flush();
|
7574 | this.trigger('endedtimeline');
|
7575 | };
|
7576 |
|
7577 | this.discontinuity = function () {
|
7578 | referenceDTS = void 0;
|
7579 | lastDTS = void 0;
|
7580 | };
|
7581 |
|
7582 | this.reset = function () {
|
7583 | this.discontinuity();
|
7584 | this.trigger('reset');
|
7585 | };
|
7586 | };
|
7587 |
|
7588 | TimestampRolloverStream$1.prototype = new stream();
|
7589 | var timestampRolloverStream = {
|
7590 | TimestampRolloverStream: TimestampRolloverStream$1,
|
7591 | handleRollover: handleRollover$1
|
7592 | };
|
7593 |
|
7594 | var percentEncode$1 = function percentEncode(bytes, start, end) {
|
7595 | var i,
|
7596 | result = '';
|
7597 |
|
7598 | for (i = start; i < end; i++) {
|
7599 | result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
|
7600 | }
|
7601 |
|
7602 | return result;
|
7603 | },
|
7604 |
|
7605 |
|
7606 | parseUtf8 = function parseUtf8(bytes, start, end) {
|
7607 | return decodeURIComponent(percentEncode$1(bytes, start, end));
|
7608 | },
|
7609 |
|
7610 |
|
7611 | parseIso88591$1 = function parseIso88591(bytes, start, end) {
|
7612 | return unescape(percentEncode$1(bytes, start, end));
|
7613 | },
|
7614 | parseSyncSafeInteger$1 = function parseSyncSafeInteger(data) {
|
7615 | return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
|
7616 | },
|
7617 | tagParsers = {
|
7618 | TXXX: function TXXX(tag) {
|
7619 | var i;
|
7620 |
|
7621 | if (tag.data[0] !== 3) {
|
7622 |
|
7623 | return;
|
7624 | }
|
7625 |
|
7626 | for (i = 1; i < tag.data.length; i++) {
|
7627 | if (tag.data[i] === 0) {
|
7628 |
|
7629 | tag.description = parseUtf8(tag.data, 1, i);
|
7630 |
|
7631 | tag.value = parseUtf8(tag.data, i + 1, tag.data.length).replace(/\0*$/, '');
|
7632 | break;
|
7633 | }
|
7634 | }
|
7635 |
|
7636 | tag.data = tag.value;
|
7637 | },
|
7638 | WXXX: function WXXX(tag) {
|
7639 | var i;
|
7640 |
|
7641 | if (tag.data[0] !== 3) {
|
7642 |
|
7643 | return;
|
7644 | }
|
7645 |
|
7646 | for (i = 1; i < tag.data.length; i++) {
|
7647 | if (tag.data[i] === 0) {
|
7648 |
|
7649 | tag.description = parseUtf8(tag.data, 1, i);
|
7650 | tag.url = parseUtf8(tag.data, i + 1, tag.data.length);
|
7651 | break;
|
7652 | }
|
7653 | }
|
7654 | },
|
7655 | PRIV: function PRIV(tag) {
|
7656 | var i;
|
7657 |
|
7658 | for (i = 0; i < tag.data.length; i++) {
|
7659 | if (tag.data[i] === 0) {
|
7660 |
|
7661 | tag.owner = parseIso88591$1(tag.data, 0, i);
|
7662 | break;
|
7663 | }
|
7664 | }
|
7665 |
|
7666 | tag.privateData = tag.data.subarray(i + 1);
|
7667 | tag.data = tag.privateData;
|
7668 | }
|
7669 | },
|
7670 | _MetadataStream;
|
7671 |
|
7672 | _MetadataStream = function MetadataStream(options) {
|
7673 | var settings = {
|
7674 |
|
7675 |
|
7676 |
|
7677 | descriptor: options && options.descriptor
|
7678 | },
|
7679 |
|
7680 | tagSize = 0,
|
7681 |
|
7682 | buffer = [],
|
7683 |
|
7684 | bufferSize = 0,
|
7685 | i;
|
7686 |
|
7687 | _MetadataStream.prototype.init.call(this);
|
7688 |
|
7689 |
|
7690 |
|
7691 | this.dispatchType = streamTypes.METADATA_STREAM_TYPE.toString(16);
|
7692 |
|
7693 | if (settings.descriptor) {
|
7694 | for (i = 0; i < settings.descriptor.length; i++) {
|
7695 | this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
|
7696 | }
|
7697 | }
|
7698 |
|
7699 | this.push = function (chunk) {
|
7700 | var tag, frameStart, frameSize, frame, i, frameHeader;
|
7701 |
|
7702 | if (chunk.type !== 'timed-metadata') {
|
7703 | return;
|
7704 | }
|
7705 |
|
7706 |
|
7707 |
|
7708 |
|
7709 | if (chunk.dataAlignmentIndicator) {
|
7710 | bufferSize = 0;
|
7711 | buffer.length = 0;
|
7712 | }
|
7713 |
|
7714 |
|
7715 | if (buffer.length === 0 && (chunk.data.length < 10 || chunk.data[0] !== 'I'.charCodeAt(0) || chunk.data[1] !== 'D'.charCodeAt(0) || chunk.data[2] !== '3'.charCodeAt(0))) {
|
7716 | this.trigger('log', {
|
7717 | level: 'warn',
|
7718 | message: 'Skipping unrecognized metadata packet'
|
7719 | });
|
7720 | return;
|
7721 | }
|
7722 |
|
7723 |
|
7724 | buffer.push(chunk);
|
7725 | bufferSize += chunk.data.byteLength;
|
7726 |
|
7727 | if (buffer.length === 1) {
|
7728 |
|
7729 |
|
7730 |
|
7731 |
|
7732 | tagSize = parseSyncSafeInteger$1(chunk.data.subarray(6, 10));
|
7733 |
|
7734 |
|
7735 | tagSize += 10;
|
7736 | }
|
7737 |
|
7738 |
|
7739 | if (bufferSize < tagSize) {
|
7740 | return;
|
7741 | }
|
7742 |
|
7743 |
|
7744 | tag = {
|
7745 | data: new Uint8Array(tagSize),
|
7746 | frames: [],
|
7747 | pts: buffer[0].pts,
|
7748 | dts: buffer[0].dts
|
7749 | };
|
7750 |
|
7751 | for (i = 0; i < tagSize;) {
|
7752 | tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
|
7753 | i += buffer[0].data.byteLength;
|
7754 | bufferSize -= buffer[0].data.byteLength;
|
7755 | buffer.shift();
|
7756 | }
|
7757 |
|
7758 |
|
7759 | frameStart = 10;
|
7760 |
|
7761 | if (tag.data[5] & 0x40) {
|
7762 |
|
7763 | frameStart += 4;
|
7764 |
|
7765 | frameStart += parseSyncSafeInteger$1(tag.data.subarray(10, 14));
|
7766 |
|
7767 | tagSize -= parseSyncSafeInteger$1(tag.data.subarray(16, 20));
|
7768 | }
|
7769 |
|
7770 |
|
7771 |
|
7772 | do {
|
7773 |
|
7774 | frameSize = parseSyncSafeInteger$1(tag.data.subarray(frameStart + 4, frameStart + 8));
|
7775 |
|
7776 | if (frameSize < 1) {
|
7777 | this.trigger('log', {
|
7778 | level: 'warn',
|
7779 | message: 'Malformed ID3 frame encountered. Skipping metadata parsing.'
|
7780 | });
|
7781 | return;
|
7782 | }
|
7783 |
|
7784 | frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
|
7785 | frame = {
|
7786 | id: frameHeader,
|
7787 | data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
|
7788 | };
|
7789 | frame.key = frame.id;
|
7790 |
|
7791 | if (tagParsers[frame.id]) {
|
7792 | tagParsers[frame.id](frame);
|
7793 |
|
7794 |
|
7795 | if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
|
7796 | var d = frame.data,
|
7797 | size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
|
7798 | size *= 4;
|
7799 | size += d[7] & 0x03;
|
7800 | frame.timeStamp = size;
|
7801 |
|
7802 |
|
7803 |
|
7804 |
|
7805 | if (tag.pts === undefined && tag.dts === undefined) {
|
7806 | tag.pts = frame.timeStamp;
|
7807 | tag.dts = frame.timeStamp;
|
7808 | }
|
7809 |
|
7810 | this.trigger('timestamp', frame);
|
7811 | }
|
7812 | }
|
7813 |
|
7814 | tag.frames.push(frame);
|
7815 | frameStart += 10;
|
7816 |
|
7817 | frameStart += frameSize;
|
7818 | } while (frameStart < tagSize);
|
7819 |
|
7820 | this.trigger('data', tag);
|
7821 | };
|
7822 | };
|
7823 |
|
7824 | _MetadataStream.prototype = new stream();
|
7825 | var metadataStream = _MetadataStream;
|
7826 | var TimestampRolloverStream = timestampRolloverStream.TimestampRolloverStream;
|
7827 |
|
7828 | var _TransportPacketStream, _TransportParseStream, _ElementaryStream;
|
7829 |
|
7830 |
|
7831 | var MP2T_PACKET_LENGTH$1 = 188,
|
7832 |
|
7833 | SYNC_BYTE$1 = 0x47;
|
7834 | |
7835 |
|
7836 |
|
7837 |
|
7838 |
|
7839 | _TransportPacketStream = function TransportPacketStream() {
|
7840 | var buffer = new Uint8Array(MP2T_PACKET_LENGTH$1),
|
7841 | bytesInBuffer = 0;
|
7842 |
|
7843 | _TransportPacketStream.prototype.init.call(this);
|
7844 |
|
7845 | |
7846 |
|
7847 |
|
7848 |
|
7849 |
|
7850 | this.push = function (bytes) {
|
7851 | var startIndex = 0,
|
7852 | endIndex = MP2T_PACKET_LENGTH$1,
|
7853 | everything;
|
7854 |
|
7855 |
|
7856 | if (bytesInBuffer) {
|
7857 | everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
|
7858 | everything.set(buffer.subarray(0, bytesInBuffer));
|
7859 | everything.set(bytes, bytesInBuffer);
|
7860 | bytesInBuffer = 0;
|
7861 | } else {
|
7862 | everything = bytes;
|
7863 | }
|
7864 |
|
7865 |
|
7866 | while (endIndex < everything.byteLength) {
|
7867 |
|
7868 | if (everything[startIndex] === SYNC_BYTE$1 && everything[endIndex] === SYNC_BYTE$1) {
|
7869 |
|
7870 |
|
7871 | this.trigger('data', everything.subarray(startIndex, endIndex));
|
7872 | startIndex += MP2T_PACKET_LENGTH$1;
|
7873 | endIndex += MP2T_PACKET_LENGTH$1;
|
7874 | continue;
|
7875 | }
|
7876 |
|
7877 |
|
7878 |
|
7879 |
|
7880 | startIndex++;
|
7881 | endIndex++;
|
7882 | }
|
7883 |
|
7884 |
|
7885 |
|
7886 |
|
7887 | if (startIndex < everything.byteLength) {
|
7888 | buffer.set(everything.subarray(startIndex), 0);
|
7889 | bytesInBuffer = everything.byteLength - startIndex;
|
7890 | }
|
7891 | };
|
7892 | |
7893 |
|
7894 |
|
7895 |
|
7896 |
|
7897 | this.flush = function () {
|
7898 |
|
7899 |
|
7900 |
|
7901 | if (bytesInBuffer === MP2T_PACKET_LENGTH$1 && buffer[0] === SYNC_BYTE$1) {
|
7902 | this.trigger('data', buffer);
|
7903 | bytesInBuffer = 0;
|
7904 | }
|
7905 |
|
7906 | this.trigger('done');
|
7907 | };
|
7908 |
|
7909 | this.endTimeline = function () {
|
7910 | this.flush();
|
7911 | this.trigger('endedtimeline');
|
7912 | };
|
7913 |
|
7914 | this.reset = function () {
|
7915 | bytesInBuffer = 0;
|
7916 | this.trigger('reset');
|
7917 | };
|
7918 | };
|
7919 |
|
7920 | _TransportPacketStream.prototype = new stream();
|
7921 | |
7922 |
|
7923 |
|
7924 |
|
7925 |
|
7926 | _TransportParseStream = function TransportParseStream() {
|
7927 | var parsePsi, parsePat, parsePmt, self;
|
7928 |
|
7929 | _TransportParseStream.prototype.init.call(this);
|
7930 |
|
7931 | self = this;
|
7932 | this.packetsWaitingForPmt = [];
|
7933 | this.programMapTable = undefined;
|
7934 |
|
7935 | parsePsi = function parsePsi(payload, psi) {
|
7936 | var offset = 0;
|
7937 |
|
7938 |
|
7939 |
|
7940 |
|
7941 |
|
7942 |
|
7943 | if (psi.payloadUnitStartIndicator) {
|
7944 | offset += payload[offset] + 1;
|
7945 | }
|
7946 |
|
7947 | if (psi.type === 'pat') {
|
7948 | parsePat(payload.subarray(offset), psi);
|
7949 | } else {
|
7950 | parsePmt(payload.subarray(offset), psi);
|
7951 | }
|
7952 | };
|
7953 |
|
7954 | parsePat = function parsePat(payload, pat) {
|
7955 | pat.section_number = payload[7];
|
7956 |
|
7957 | pat.last_section_number = payload[8];
|
7958 |
|
7959 |
|
7960 | self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
|
7961 | pat.pmtPid = self.pmtPid;
|
7962 | };
|
7963 | |
7964 |
|
7965 |
|
7966 |
|
7967 |
|
7968 |
|
7969 |
|
7970 |
|
7971 |
|
7972 |
|
7973 | parsePmt = function parsePmt(payload, pmt) {
|
7974 | var sectionLength, tableEnd, programInfoLength, offset;
|
7975 |
|
7976 |
|
7977 |
|
7978 |
|
7979 |
|
7980 | if (!(payload[5] & 0x01)) {
|
7981 | return;
|
7982 | }
|
7983 |
|
7984 |
|
7985 | self.programMapTable = {
|
7986 | video: null,
|
7987 | audio: null,
|
7988 | 'timed-metadata': {}
|
7989 | };
|
7990 |
|
7991 | sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
|
7992 | tableEnd = 3 + sectionLength - 4;
|
7993 |
|
7994 |
|
7995 | programInfoLength = (payload[10] & 0x0f) << 8 | payload[11];
|
7996 |
|
7997 | offset = 12 + programInfoLength;
|
7998 |
|
7999 | while (offset < tableEnd) {
|
8000 | var streamType = payload[offset];
|
8001 | var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2];
|
8002 |
|
8003 |
|
8004 |
|
8005 | if (streamType === streamTypes.H264_STREAM_TYPE && self.programMapTable.video === null) {
|
8006 | self.programMapTable.video = pid;
|
8007 | } else if (streamType === streamTypes.ADTS_STREAM_TYPE && self.programMapTable.audio === null) {
|
8008 | self.programMapTable.audio = pid;
|
8009 | } else if (streamType === streamTypes.METADATA_STREAM_TYPE) {
|
8010 |
|
8011 | self.programMapTable['timed-metadata'][pid] = streamType;
|
8012 | }
|
8013 |
|
8014 |
|
8015 |
|
8016 | offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
|
8017 | }
|
8018 |
|
8019 |
|
8020 | pmt.programMapTable = self.programMapTable;
|
8021 | };
|
8022 | |
8023 |
|
8024 |
|
8025 |
|
8026 |
|
8027 | this.push = function (packet) {
|
8028 | var result = {},
|
8029 | offset = 4;
|
8030 | result.payloadUnitStartIndicator = !!(packet[1] & 0x40);
|
8031 |
|
8032 | result.pid = packet[1] & 0x1f;
|
8033 | result.pid <<= 8;
|
8034 | result.pid |= packet[2];
|
8035 |
|
8036 |
|
8037 |
|
8038 |
|
8039 |
|
8040 | if ((packet[3] & 0x30) >>> 4 > 0x01) {
|
8041 | offset += packet[offset] + 1;
|
8042 | }
|
8043 |
|
8044 |
|
8045 | if (result.pid === 0) {
|
8046 | result.type = 'pat';
|
8047 | parsePsi(packet.subarray(offset), result);
|
8048 | this.trigger('data', result);
|
8049 | } else if (result.pid === this.pmtPid) {
|
8050 | result.type = 'pmt';
|
8051 | parsePsi(packet.subarray(offset), result);
|
8052 | this.trigger('data', result);
|
8053 |
|
8054 | while (this.packetsWaitingForPmt.length) {
|
8055 | this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
|
8056 | }
|
8057 | } else if (this.programMapTable === undefined) {
|
8058 |
|
8059 |
|
8060 | this.packetsWaitingForPmt.push([packet, offset, result]);
|
8061 | } else {
|
8062 | this.processPes_(packet, offset, result);
|
8063 | }
|
8064 | };
|
8065 |
|
8066 | this.processPes_ = function (packet, offset, result) {
|
8067 |
|
8068 | if (result.pid === this.programMapTable.video) {
|
8069 | result.streamType = streamTypes.H264_STREAM_TYPE;
|
8070 | } else if (result.pid === this.programMapTable.audio) {
|
8071 | result.streamType = streamTypes.ADTS_STREAM_TYPE;
|
8072 | } else {
|
8073 |
|
8074 |
|
8075 | result.streamType = this.programMapTable['timed-metadata'][result.pid];
|
8076 | }
|
8077 |
|
8078 | result.type = 'pes';
|
8079 | result.data = packet.subarray(offset);
|
8080 | this.trigger('data', result);
|
8081 | };
|
8082 | };
|
8083 |
|
8084 | _TransportParseStream.prototype = new stream();
|
8085 | _TransportParseStream.STREAM_TYPES = {
|
8086 | h264: 0x1b,
|
8087 | adts: 0x0f
|
8088 | };
|
8089 | |
8090 |
|
8091 |
|
8092 |
|
8093 |
|
8094 |
|
8095 |
|
8096 |
|
8097 |
|
8098 | _ElementaryStream = function ElementaryStream() {
|
8099 | var self = this,
|
8100 | segmentHadPmt = false,
|
8101 |
|
8102 | video = {
|
8103 | data: [],
|
8104 | size: 0
|
8105 | },
|
8106 | audio = {
|
8107 | data: [],
|
8108 | size: 0
|
8109 | },
|
8110 | timedMetadata = {
|
8111 | data: [],
|
8112 | size: 0
|
8113 | },
|
8114 | programMapTable,
|
8115 | parsePes = function parsePes(payload, pes) {
|
8116 | var ptsDtsFlags;
|
8117 | var startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2];
|
8118 |
|
8119 | pes.data = new Uint8Array();
|
8120 |
|
8121 |
|
8122 |
|
8123 | if (startPrefix !== 1) {
|
8124 | return;
|
8125 | }
|
8126 |
|
8127 |
|
8128 | pes.packetLength = 6 + (payload[4] << 8 | payload[5]);
|
8129 |
|
8130 | pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0;
|
8131 |
|
8132 |
|
8133 |
|
8134 | ptsDtsFlags = payload[7];
|
8135 |
|
8136 |
|
8137 |
|
8138 |
|
8139 |
|
8140 |
|
8141 |
|
8142 |
|
8143 | if (ptsDtsFlags & 0xC0) {
|
8144 |
|
8145 |
|
8146 |
|
8147 | pes.pts = (payload[9] & 0x0E) << 27 | (payload[10] & 0xFF) << 20 | (payload[11] & 0xFE) << 12 | (payload[12] & 0xFF) << 5 | (payload[13] & 0xFE) >>> 3;
|
8148 | pes.pts *= 4;
|
8149 |
|
8150 | pes.pts += (payload[13] & 0x06) >>> 1;
|
8151 |
|
8152 | pes.dts = pes.pts;
|
8153 |
|
8154 | if (ptsDtsFlags & 0x40) {
|
8155 | pes.dts = (payload[14] & 0x0E) << 27 | (payload[15] & 0xFF) << 20 | (payload[16] & 0xFE) << 12 | (payload[17] & 0xFF) << 5 | (payload[18] & 0xFE) >>> 3;
|
8156 | pes.dts *= 4;
|
8157 |
|
8158 | pes.dts += (payload[18] & 0x06) >>> 1;
|
8159 | }
|
8160 | }
|
8161 |
|
8162 |
|
8163 |
|
8164 |
|
8165 | pes.data = payload.subarray(9 + payload[8]);
|
8166 | },
|
8167 |
|
8168 | |
8169 |
|
8170 |
|
8171 | flushStream = function flushStream(stream, type, forceFlush) {
|
8172 | var packetData = new Uint8Array(stream.size),
|
8173 | event = {
|
8174 | type: type
|
8175 | },
|
8176 | i = 0,
|
8177 | offset = 0,
|
8178 | packetFlushable = false,
|
8179 | fragment;
|
8180 |
|
8181 |
|
8182 | if (!stream.data.length || stream.size < 9) {
|
8183 | return;
|
8184 | }
|
8185 |
|
8186 | event.trackId = stream.data[0].pid;
|
8187 |
|
8188 | for (i = 0; i < stream.data.length; i++) {
|
8189 | fragment = stream.data[i];
|
8190 | packetData.set(fragment.data, offset);
|
8191 | offset += fragment.data.byteLength;
|
8192 | }
|
8193 |
|
8194 |
|
8195 | parsePes(packetData, event);
|
8196 |
|
8197 |
|
8198 | packetFlushable = type === 'video' || event.packetLength <= stream.size;
|
8199 |
|
8200 | if (forceFlush || packetFlushable) {
|
8201 | stream.size = 0;
|
8202 | stream.data.length = 0;
|
8203 | }
|
8204 |
|
8205 |
|
8206 |
|
8207 | if (packetFlushable) {
|
8208 | self.trigger('data', event);
|
8209 | }
|
8210 | };
|
8211 |
|
8212 | _ElementaryStream.prototype.init.call(this);
|
8213 | |
8214 |
|
8215 |
|
8216 |
|
8217 |
|
8218 |
|
8219 | this.push = function (data) {
|
8220 | ({
|
8221 | pat: function pat() {
|
8222 |
|
8223 | },
|
8224 | pes: function pes() {
|
8225 | var stream, streamType;
|
8226 |
|
8227 | switch (data.streamType) {
|
8228 | case streamTypes.H264_STREAM_TYPE:
|
8229 | stream = video;
|
8230 | streamType = 'video';
|
8231 | break;
|
8232 |
|
8233 | case streamTypes.ADTS_STREAM_TYPE:
|
8234 | stream = audio;
|
8235 | streamType = 'audio';
|
8236 | break;
|
8237 |
|
8238 | case streamTypes.METADATA_STREAM_TYPE:
|
8239 | stream = timedMetadata;
|
8240 | streamType = 'timed-metadata';
|
8241 | break;
|
8242 |
|
8243 | default:
|
8244 |
|
8245 | return;
|
8246 | }
|
8247 |
|
8248 |
|
8249 |
|
8250 | if (data.payloadUnitStartIndicator) {
|
8251 | flushStream(stream, streamType, true);
|
8252 | }
|
8253 |
|
8254 |
|
8255 |
|
8256 | stream.data.push(data);
|
8257 | stream.size += data.data.byteLength;
|
8258 | },
|
8259 | pmt: function pmt() {
|
8260 | var event = {
|
8261 | type: 'metadata',
|
8262 | tracks: []
|
8263 | };
|
8264 | programMapTable = data.programMapTable;
|
8265 |
|
8266 | if (programMapTable.video !== null) {
|
8267 | event.tracks.push({
|
8268 | timelineStartInfo: {
|
8269 | baseMediaDecodeTime: 0
|
8270 | },
|
8271 | id: +programMapTable.video,
|
8272 | codec: 'avc',
|
8273 | type: 'video'
|
8274 | });
|
8275 | }
|
8276 |
|
8277 | if (programMapTable.audio !== null) {
|
8278 | event.tracks.push({
|
8279 | timelineStartInfo: {
|
8280 | baseMediaDecodeTime: 0
|
8281 | },
|
8282 | id: +programMapTable.audio,
|
8283 | codec: 'adts',
|
8284 | type: 'audio'
|
8285 | });
|
8286 | }
|
8287 |
|
8288 | segmentHadPmt = true;
|
8289 | self.trigger('data', event);
|
8290 | }
|
8291 | })[data.type]();
|
8292 | };
|
8293 |
|
8294 | this.reset = function () {
|
8295 | video.size = 0;
|
8296 | video.data.length = 0;
|
8297 | audio.size = 0;
|
8298 | audio.data.length = 0;
|
8299 | this.trigger('reset');
|
8300 | };
|
8301 | |
8302 |
|
8303 |
|
8304 |
|
8305 |
|
8306 |
|
8307 |
|
8308 |
|
8309 |
|
8310 |
|
8311 |
|
8312 | this.flushStreams_ = function () {
|
8313 |
|
8314 |
|
8315 | flushStream(video, 'video');
|
8316 | flushStream(audio, 'audio');
|
8317 | flushStream(timedMetadata, 'timed-metadata');
|
8318 | };
|
8319 |
|
8320 | this.flush = function () {
|
8321 |
|
8322 |
|
8323 |
|
8324 | if (!segmentHadPmt && programMapTable) {
|
8325 | var pmt = {
|
8326 | type: 'metadata',
|
8327 | tracks: []
|
8328 | };
|
8329 |
|
8330 | if (programMapTable.video !== null) {
|
8331 | pmt.tracks.push({
|
8332 | timelineStartInfo: {
|
8333 | baseMediaDecodeTime: 0
|
8334 | },
|
8335 | id: +programMapTable.video,
|
8336 | codec: 'avc',
|
8337 | type: 'video'
|
8338 | });
|
8339 | }
|
8340 |
|
8341 | if (programMapTable.audio !== null) {
|
8342 | pmt.tracks.push({
|
8343 | timelineStartInfo: {
|
8344 | baseMediaDecodeTime: 0
|
8345 | },
|
8346 | id: +programMapTable.audio,
|
8347 | codec: 'adts',
|
8348 | type: 'audio'
|
8349 | });
|
8350 | }
|
8351 |
|
8352 | self.trigger('data', pmt);
|
8353 | }
|
8354 |
|
8355 | segmentHadPmt = false;
|
8356 | this.flushStreams_();
|
8357 | this.trigger('done');
|
8358 | };
|
8359 | };
|
8360 |
|
8361 | _ElementaryStream.prototype = new stream();
|
8362 | var m2ts = {
|
8363 | PAT_PID: 0x0000,
|
8364 | MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH$1,
|
8365 | TransportPacketStream: _TransportPacketStream,
|
8366 | TransportParseStream: _TransportParseStream,
|
8367 | ElementaryStream: _ElementaryStream,
|
8368 | TimestampRolloverStream: TimestampRolloverStream,
|
8369 | CaptionStream: captionStream.CaptionStream,
|
8370 | Cea608Stream: captionStream.Cea608Stream,
|
8371 | Cea708Stream: captionStream.Cea708Stream,
|
8372 | MetadataStream: metadataStream
|
8373 | };
|
8374 |
|
8375 | for (var type in streamTypes) {
|
8376 | if (streamTypes.hasOwnProperty(type)) {
|
8377 | m2ts[type] = streamTypes[type];
|
8378 | }
|
8379 | }
|
8380 |
|
8381 | var m2ts_1 = m2ts;
|
8382 | var ONE_SECOND_IN_TS$2 = clock.ONE_SECOND_IN_TS;
|
8383 |
|
8384 | var _AdtsStream;
|
8385 |
|
8386 | var ADTS_SAMPLING_FREQUENCIES$1 = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
8387 | |
8388 |
|
8389 |
|
8390 |
|
8391 |
|
8392 |
|
8393 |
|
8394 |
|
8395 |
|
8396 | _AdtsStream = function AdtsStream(handlePartialSegments) {
|
8397 | var buffer,
|
8398 | frameNum = 0;
|
8399 |
|
8400 | _AdtsStream.prototype.init.call(this);
|
8401 |
|
8402 | this.skipWarn_ = function (start, end) {
|
8403 | this.trigger('log', {
|
8404 | level: 'warn',
|
8405 | message: "adts skiping bytes " + start + " to " + end + " in frame " + frameNum + " outside syncword"
|
8406 | });
|
8407 | };
|
8408 |
|
8409 | this.push = function (packet) {
|
8410 | var i = 0,
|
8411 | frameLength,
|
8412 | protectionSkipBytes,
|
8413 | oldBuffer,
|
8414 | sampleCount,
|
8415 | adtsFrameDuration;
|
8416 |
|
8417 | if (!handlePartialSegments) {
|
8418 | frameNum = 0;
|
8419 | }
|
8420 |
|
8421 | if (packet.type !== 'audio') {
|
8422 |
|
8423 | return;
|
8424 | }
|
8425 |
|
8426 |
|
8427 |
|
8428 | if (buffer && buffer.length) {
|
8429 | oldBuffer = buffer;
|
8430 | buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
|
8431 | buffer.set(oldBuffer);
|
8432 | buffer.set(packet.data, oldBuffer.byteLength);
|
8433 | } else {
|
8434 | buffer = packet.data;
|
8435 | }
|
8436 |
|
8437 |
|
8438 |
|
8439 | var skip;
|
8440 |
|
8441 |
|
8442 | while (i + 7 < buffer.length) {
|
8443 |
|
8444 | if (buffer[i] !== 0xFF || (buffer[i + 1] & 0xF6) !== 0xF0) {
|
8445 | if (typeof skip !== 'number') {
|
8446 | skip = i;
|
8447 | }
|
8448 |
|
8449 |
|
8450 |
|
8451 | i++;
|
8452 | continue;
|
8453 | }
|
8454 |
|
8455 | if (typeof skip === 'number') {
|
8456 | this.skipWarn_(skip, i);
|
8457 | skip = null;
|
8458 | }
|
8459 |
|
8460 |
|
8461 |
|
8462 | protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2;
|
8463 |
|
8464 |
|
8465 |
|
8466 | frameLength = (buffer[i + 3] & 0x03) << 11 | buffer[i + 4] << 3 | (buffer[i + 5] & 0xe0) >> 5;
|
8467 | sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
|
8468 | adtsFrameDuration = sampleCount * ONE_SECOND_IN_TS$2 / ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2];
|
8469 |
|
8470 |
|
8471 | if (buffer.byteLength - i < frameLength) {
|
8472 | break;
|
8473 | }
|
8474 |
|
8475 |
|
8476 | this.trigger('data', {
|
8477 | pts: packet.pts + frameNum * adtsFrameDuration,
|
8478 | dts: packet.dts + frameNum * adtsFrameDuration,
|
8479 | sampleCount: sampleCount,
|
8480 | audioobjecttype: (buffer[i + 2] >>> 6 & 0x03) + 1,
|
8481 | channelcount: (buffer[i + 2] & 1) << 2 | (buffer[i + 3] & 0xc0) >>> 6,
|
8482 | samplerate: ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2],
|
8483 | samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
|
8484 |
|
8485 | samplesize: 16,
|
8486 |
|
8487 | data: buffer.subarray(i + 7 + protectionSkipBytes, i + frameLength)
|
8488 | });
|
8489 | frameNum++;
|
8490 | i += frameLength;
|
8491 | }
|
8492 |
|
8493 | if (typeof skip === 'number') {
|
8494 | this.skipWarn_(skip, i);
|
8495 | skip = null;
|
8496 | }
|
8497 |
|
8498 |
|
8499 | buffer = buffer.subarray(i);
|
8500 | };
|
8501 |
|
8502 | this.flush = function () {
|
8503 | frameNum = 0;
|
8504 | this.trigger('done');
|
8505 | };
|
8506 |
|
8507 | this.reset = function () {
|
8508 | buffer = void 0;
|
8509 | this.trigger('reset');
|
8510 | };
|
8511 |
|
8512 | this.endTimeline = function () {
|
8513 | buffer = void 0;
|
8514 | this.trigger('endedtimeline');
|
8515 | };
|
8516 | };
|
8517 |
|
8518 | _AdtsStream.prototype = new stream();
|
8519 | var adts = _AdtsStream;
|
8520 | |
8521 |
|
8522 |
|
8523 |
|
8524 |
|
8525 |
|
8526 |
|
8527 | var ExpGolomb;
|
8528 | |
8529 |
|
8530 |
|
8531 |
|
8532 |
|
8533 | ExpGolomb = function ExpGolomb(workingData) {
|
8534 | var
|
8535 | workingBytesAvailable = workingData.byteLength,
|
8536 |
|
8537 | workingWord = 0,
|
8538 |
|
8539 |
|
8540 | workingBitsAvailable = 0;
|
8541 |
|
8542 |
|
8543 | this.length = function () {
|
8544 | return 8 * workingBytesAvailable;
|
8545 | };
|
8546 |
|
8547 |
|
8548 | this.bitsAvailable = function () {
|
8549 | return 8 * workingBytesAvailable + workingBitsAvailable;
|
8550 | };
|
8551 |
|
8552 |
|
8553 | this.loadWord = function () {
|
8554 | var position = workingData.byteLength - workingBytesAvailable,
|
8555 | workingBytes = new Uint8Array(4),
|
8556 | availableBytes = Math.min(4, workingBytesAvailable);
|
8557 |
|
8558 | if (availableBytes === 0) {
|
8559 | throw new Error('no bytes available');
|
8560 | }
|
8561 |
|
8562 | workingBytes.set(workingData.subarray(position, position + availableBytes));
|
8563 | workingWord = new DataView(workingBytes.buffer).getUint32(0);
|
8564 |
|
8565 | workingBitsAvailable = availableBytes * 8;
|
8566 | workingBytesAvailable -= availableBytes;
|
8567 | };
|
8568 |
|
8569 |
|
8570 | this.skipBits = function (count) {
|
8571 | var skipBytes;
|
8572 |
|
8573 | if (workingBitsAvailable > count) {
|
8574 | workingWord <<= count;
|
8575 | workingBitsAvailable -= count;
|
8576 | } else {
|
8577 | count -= workingBitsAvailable;
|
8578 | skipBytes = Math.floor(count / 8);
|
8579 | count -= skipBytes * 8;
|
8580 | workingBytesAvailable -= skipBytes;
|
8581 | this.loadWord();
|
8582 | workingWord <<= count;
|
8583 | workingBitsAvailable -= count;
|
8584 | }
|
8585 | };
|
8586 |
|
8587 |
|
8588 | this.readBits = function (size) {
|
8589 | var bits = Math.min(workingBitsAvailable, size),
|
8590 |
|
8591 | valu = workingWord >>> 32 - bits;
|
8592 |
|
8593 |
|
8594 | workingBitsAvailable -= bits;
|
8595 |
|
8596 | if (workingBitsAvailable > 0) {
|
8597 | workingWord <<= bits;
|
8598 | } else if (workingBytesAvailable > 0) {
|
8599 | this.loadWord();
|
8600 | }
|
8601 |
|
8602 | bits = size - bits;
|
8603 |
|
8604 | if (bits > 0) {
|
8605 | return valu << bits | this.readBits(bits);
|
8606 | }
|
8607 |
|
8608 | return valu;
|
8609 | };
|
8610 |
|
8611 |
|
8612 | this.skipLeadingZeros = function () {
|
8613 | var leadingZeroCount;
|
8614 |
|
8615 | for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
|
8616 | if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
|
8617 |
|
8618 | workingWord <<= leadingZeroCount;
|
8619 | workingBitsAvailable -= leadingZeroCount;
|
8620 | return leadingZeroCount;
|
8621 | }
|
8622 | }
|
8623 |
|
8624 |
|
8625 | this.loadWord();
|
8626 | return leadingZeroCount + this.skipLeadingZeros();
|
8627 | };
|
8628 |
|
8629 |
|
8630 | this.skipUnsignedExpGolomb = function () {
|
8631 | this.skipBits(1 + this.skipLeadingZeros());
|
8632 | };
|
8633 |
|
8634 |
|
8635 | this.skipExpGolomb = function () {
|
8636 | this.skipBits(1 + this.skipLeadingZeros());
|
8637 | };
|
8638 |
|
8639 |
|
8640 | this.readUnsignedExpGolomb = function () {
|
8641 | var clz = this.skipLeadingZeros();
|
8642 |
|
8643 | return this.readBits(clz + 1) - 1;
|
8644 | };
|
8645 |
|
8646 |
|
8647 | this.readExpGolomb = function () {
|
8648 | var valu = this.readUnsignedExpGolomb();
|
8649 |
|
8650 | if (0x01 & valu) {
|
8651 |
|
8652 | return 1 + valu >>> 1;
|
8653 | }
|
8654 |
|
8655 | return -1 * (valu >>> 1);
|
8656 | };
|
8657 |
|
8658 |
|
8659 |
|
8660 | this.readBoolean = function () {
|
8661 | return this.readBits(1) === 1;
|
8662 | };
|
8663 |
|
8664 |
|
8665 | this.readUnsignedByte = function () {
|
8666 | return this.readBits(8);
|
8667 | };
|
8668 |
|
8669 | this.loadWord();
|
8670 | };
|
8671 |
|
8672 | var expGolomb = ExpGolomb;
|
8673 |
|
8674 | var _H264Stream, _NalByteStream;
|
8675 |
|
8676 | var PROFILES_WITH_OPTIONAL_SPS_DATA;
|
8677 | |
8678 |
|
8679 |
|
8680 |
|
8681 | _NalByteStream = function NalByteStream() {
|
8682 | var syncPoint = 0,
|
8683 | i,
|
8684 | buffer;
|
8685 |
|
8686 | _NalByteStream.prototype.init.call(this);
|
8687 | |
8688 |
|
8689 |
|
8690 |
|
8691 |
|
8692 |
|
8693 |
|
8694 |
|
8695 |
|
8696 | this.push = function (data) {
|
8697 | var swapBuffer;
|
8698 |
|
8699 | if (!buffer) {
|
8700 | buffer = data.data;
|
8701 | } else {
|
8702 | swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
|
8703 | swapBuffer.set(buffer);
|
8704 | swapBuffer.set(data.data, buffer.byteLength);
|
8705 | buffer = swapBuffer;
|
8706 | }
|
8707 |
|
8708 | var len = buffer.byteLength;
|
8709 |
|
8710 |
|
8711 |
|
8712 |
|
8713 |
|
8714 |
|
8715 |
|
8716 |
|
8717 |
|
8718 | for (; syncPoint < len - 3; syncPoint++) {
|
8719 | if (buffer[syncPoint + 2] === 1) {
|
8720 |
|
8721 | i = syncPoint + 5;
|
8722 | break;
|
8723 | }
|
8724 | }
|
8725 |
|
8726 | while (i < len) {
|
8727 |
|
8728 |
|
8729 | switch (buffer[i]) {
|
8730 | case 0:
|
8731 |
|
8732 | if (buffer[i - 1] !== 0) {
|
8733 | i += 2;
|
8734 | break;
|
8735 | } else if (buffer[i - 2] !== 0) {
|
8736 | i++;
|
8737 | break;
|
8738 | }
|
8739 |
|
8740 |
|
8741 | if (syncPoint + 3 !== i - 2) {
|
8742 | this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
|
8743 | }
|
8744 |
|
8745 |
|
8746 | do {
|
8747 | i++;
|
8748 | } while (buffer[i] !== 1 && i < len);
|
8749 |
|
8750 | syncPoint = i - 2;
|
8751 | i += 3;
|
8752 | break;
|
8753 |
|
8754 | case 1:
|
8755 |
|
8756 | if (buffer[i - 1] !== 0 || buffer[i - 2] !== 0) {
|
8757 | i += 3;
|
8758 | break;
|
8759 | }
|
8760 |
|
8761 |
|
8762 | this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
|
8763 | syncPoint = i - 2;
|
8764 | i += 3;
|
8765 | break;
|
8766 |
|
8767 | default:
|
8768 |
|
8769 |
|
8770 | i += 3;
|
8771 | break;
|
8772 | }
|
8773 | }
|
8774 |
|
8775 |
|
8776 | buffer = buffer.subarray(syncPoint);
|
8777 | i -= syncPoint;
|
8778 | syncPoint = 0;
|
8779 | };
|
8780 |
|
8781 | this.reset = function () {
|
8782 | buffer = null;
|
8783 | syncPoint = 0;
|
8784 | this.trigger('reset');
|
8785 | };
|
8786 |
|
8787 | this.flush = function () {
|
8788 |
|
8789 | if (buffer && buffer.byteLength > 3) {
|
8790 | this.trigger('data', buffer.subarray(syncPoint + 3));
|
8791 | }
|
8792 |
|
8793 |
|
8794 | buffer = null;
|
8795 | syncPoint = 0;
|
8796 | this.trigger('done');
|
8797 | };
|
8798 |
|
8799 | this.endTimeline = function () {
|
8800 | this.flush();
|
8801 | this.trigger('endedtimeline');
|
8802 | };
|
8803 | };
|
8804 |
|
8805 | _NalByteStream.prototype = new stream();
|
8806 |
|
8807 |
|
8808 |
|
8809 | PROFILES_WITH_OPTIONAL_SPS_DATA = {
|
8810 | 100: true,
|
8811 | 110: true,
|
8812 | 122: true,
|
8813 | 244: true,
|
8814 | 44: true,
|
8815 | 83: true,
|
8816 | 86: true,
|
8817 | 118: true,
|
8818 | 128: true,
|
8819 |
|
8820 |
|
8821 | 138: true,
|
8822 | 139: true,
|
8823 | 134: true
|
8824 | };
|
8825 | |
8826 |
|
8827 |
|
8828 |
|
8829 |
|
8830 | _H264Stream = function H264Stream() {
|
8831 | var nalByteStream = new _NalByteStream(),
|
8832 | self,
|
8833 | trackId,
|
8834 | currentPts,
|
8835 | currentDts,
|
8836 | discardEmulationPreventionBytes,
|
8837 | readSequenceParameterSet,
|
8838 | skipScalingList;
|
8839 |
|
8840 | _H264Stream.prototype.init.call(this);
|
8841 |
|
8842 | self = this;
|
8843 | |
8844 |
|
8845 |
|
8846 |
|
8847 |
|
8848 |
|
8849 |
|
8850 |
|
8851 |
|
8852 |
|
8853 |
|
8854 |
|
8855 | this.push = function (packet) {
|
8856 | if (packet.type !== 'video') {
|
8857 | return;
|
8858 | }
|
8859 |
|
8860 | trackId = packet.trackId;
|
8861 | currentPts = packet.pts;
|
8862 | currentDts = packet.dts;
|
8863 | nalByteStream.push(packet);
|
8864 | };
|
8865 | |
8866 |
|
8867 |
|
8868 |
|
8869 |
|
8870 |
|
8871 |
|
8872 |
|
8873 |
|
8874 |
|
8875 | nalByteStream.on('data', function (data) {
|
8876 | var event = {
|
8877 | trackId: trackId,
|
8878 | pts: currentPts,
|
8879 | dts: currentDts,
|
8880 | data: data,
|
8881 | nalUnitTypeCode: data[0] & 0x1f
|
8882 | };
|
8883 |
|
8884 | switch (event.nalUnitTypeCode) {
|
8885 | case 0x05:
|
8886 | event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
|
8887 | break;
|
8888 |
|
8889 | case 0x06:
|
8890 | event.nalUnitType = 'sei_rbsp';
|
8891 | event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
|
8892 | break;
|
8893 |
|
8894 | case 0x07:
|
8895 | event.nalUnitType = 'seq_parameter_set_rbsp';
|
8896 | event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
|
8897 | event.config = readSequenceParameterSet(event.escapedRBSP);
|
8898 | break;
|
8899 |
|
8900 | case 0x08:
|
8901 | event.nalUnitType = 'pic_parameter_set_rbsp';
|
8902 | break;
|
8903 |
|
8904 | case 0x09:
|
8905 | event.nalUnitType = 'access_unit_delimiter_rbsp';
|
8906 | break;
|
8907 | }
|
8908 |
|
8909 |
|
8910 | self.trigger('data', event);
|
8911 | });
|
8912 | nalByteStream.on('done', function () {
|
8913 | self.trigger('done');
|
8914 | });
|
8915 | nalByteStream.on('partialdone', function () {
|
8916 | self.trigger('partialdone');
|
8917 | });
|
8918 | nalByteStream.on('reset', function () {
|
8919 | self.trigger('reset');
|
8920 | });
|
8921 | nalByteStream.on('endedtimeline', function () {
|
8922 | self.trigger('endedtimeline');
|
8923 | });
|
8924 |
|
8925 | this.flush = function () {
|
8926 | nalByteStream.flush();
|
8927 | };
|
8928 |
|
8929 | this.partialFlush = function () {
|
8930 | nalByteStream.partialFlush();
|
8931 | };
|
8932 |
|
8933 | this.reset = function () {
|
8934 | nalByteStream.reset();
|
8935 | };
|
8936 |
|
8937 | this.endTimeline = function () {
|
8938 | nalByteStream.endTimeline();
|
8939 | };
|
8940 | |
8941 |
|
8942 |
|
8943 |
|
8944 |
|
8945 |
|
8946 |
|
8947 |
|
8948 |
|
8949 |
|
8950 |
|
8951 | skipScalingList = function skipScalingList(count, expGolombDecoder) {
|
8952 | var lastScale = 8,
|
8953 | nextScale = 8,
|
8954 | j,
|
8955 | deltaScale;
|
8956 |
|
8957 | for (j = 0; j < count; j++) {
|
8958 | if (nextScale !== 0) {
|
8959 | deltaScale = expGolombDecoder.readExpGolomb();
|
8960 | nextScale = (lastScale + deltaScale + 256) % 256;
|
8961 | }
|
8962 |
|
8963 | lastScale = nextScale === 0 ? lastScale : nextScale;
|
8964 | }
|
8965 | };
|
8966 | |
8967 |
|
8968 |
|
8969 |
|
8970 |
|
8971 |
|
8972 |
|
8973 |
|
8974 |
|
8975 |
|
8976 | discardEmulationPreventionBytes = function discardEmulationPreventionBytes(data) {
|
8977 | var length = data.byteLength,
|
8978 | emulationPreventionBytesPositions = [],
|
8979 | i = 1,
|
8980 | newLength,
|
8981 | newData;
|
8982 |
|
8983 | while (i < length - 2) {
|
8984 | if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
|
8985 | emulationPreventionBytesPositions.push(i + 2);
|
8986 | i += 2;
|
8987 | } else {
|
8988 | i++;
|
8989 | }
|
8990 | }
|
8991 |
|
8992 |
|
8993 |
|
8994 | if (emulationPreventionBytesPositions.length === 0) {
|
8995 | return data;
|
8996 | }
|
8997 |
|
8998 |
|
8999 | newLength = length - emulationPreventionBytesPositions.length;
|
9000 | newData = new Uint8Array(newLength);
|
9001 | var sourceIndex = 0;
|
9002 |
|
9003 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
9004 | if (sourceIndex === emulationPreventionBytesPositions[0]) {
|
9005 |
|
9006 | sourceIndex++;
|
9007 |
|
9008 | emulationPreventionBytesPositions.shift();
|
9009 | }
|
9010 |
|
9011 | newData[i] = data[sourceIndex];
|
9012 | }
|
9013 |
|
9014 | return newData;
|
9015 | };
|
9016 | |
9017 |
|
9018 |
|
9019 |
|
9020 |
|
9021 |
|
9022 |
|
9023 |
|
9024 |
|
9025 |
|
9026 |
|
9027 | readSequenceParameterSet = function readSequenceParameterSet(data) {
|
9028 | var frameCropLeftOffset = 0,
|
9029 | frameCropRightOffset = 0,
|
9030 | frameCropTopOffset = 0,
|
9031 | frameCropBottomOffset = 0,
|
9032 | expGolombDecoder,
|
9033 | profileIdc,
|
9034 | levelIdc,
|
9035 | profileCompatibility,
|
9036 | chromaFormatIdc,
|
9037 | picOrderCntType,
|
9038 | numRefFramesInPicOrderCntCycle,
|
9039 | picWidthInMbsMinus1,
|
9040 | picHeightInMapUnitsMinus1,
|
9041 | frameMbsOnlyFlag,
|
9042 | scalingListCount,
|
9043 | sarRatio = [1, 1],
|
9044 | aspectRatioIdc,
|
9045 | i;
|
9046 | expGolombDecoder = new expGolomb(data);
|
9047 | profileIdc = expGolombDecoder.readUnsignedByte();
|
9048 |
|
9049 | profileCompatibility = expGolombDecoder.readUnsignedByte();
|
9050 |
|
9051 | levelIdc = expGolombDecoder.readUnsignedByte();
|
9052 |
|
9053 | expGolombDecoder.skipUnsignedExpGolomb();
|
9054 |
|
9055 |
|
9056 | if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
|
9057 | chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
|
9058 |
|
9059 | if (chromaFormatIdc === 3) {
|
9060 | expGolombDecoder.skipBits(1);
|
9061 | }
|
9062 |
|
9063 | expGolombDecoder.skipUnsignedExpGolomb();
|
9064 |
|
9065 | expGolombDecoder.skipUnsignedExpGolomb();
|
9066 |
|
9067 | expGolombDecoder.skipBits(1);
|
9068 |
|
9069 | if (expGolombDecoder.readBoolean()) {
|
9070 |
|
9071 | scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
|
9072 |
|
9073 | for (i = 0; i < scalingListCount; i++) {
|
9074 | if (expGolombDecoder.readBoolean()) {
|
9075 |
|
9076 | if (i < 6) {
|
9077 | skipScalingList(16, expGolombDecoder);
|
9078 | } else {
|
9079 | skipScalingList(64, expGolombDecoder);
|
9080 | }
|
9081 | }
|
9082 | }
|
9083 | }
|
9084 | }
|
9085 |
|
9086 | expGolombDecoder.skipUnsignedExpGolomb();
|
9087 |
|
9088 | picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
|
9089 |
|
9090 | if (picOrderCntType === 0) {
|
9091 | expGolombDecoder.readUnsignedExpGolomb();
|
9092 | } else if (picOrderCntType === 1) {
|
9093 | expGolombDecoder.skipBits(1);
|
9094 |
|
9095 | expGolombDecoder.skipExpGolomb();
|
9096 |
|
9097 | expGolombDecoder.skipExpGolomb();
|
9098 |
|
9099 | numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
|
9100 |
|
9101 | for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
|
9102 | expGolombDecoder.skipExpGolomb();
|
9103 | }
|
9104 | }
|
9105 |
|
9106 | expGolombDecoder.skipUnsignedExpGolomb();
|
9107 |
|
9108 | expGolombDecoder.skipBits(1);
|
9109 |
|
9110 | picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
|
9111 | picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
|
9112 | frameMbsOnlyFlag = expGolombDecoder.readBits(1);
|
9113 |
|
9114 | if (frameMbsOnlyFlag === 0) {
|
9115 | expGolombDecoder.skipBits(1);
|
9116 | }
|
9117 |
|
9118 | expGolombDecoder.skipBits(1);
|
9119 |
|
9120 | if (expGolombDecoder.readBoolean()) {
|
9121 |
|
9122 | frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
|
9123 | frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
|
9124 | frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
|
9125 | frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
|
9126 | }
|
9127 |
|
9128 | if (expGolombDecoder.readBoolean()) {
|
9129 |
|
9130 | if (expGolombDecoder.readBoolean()) {
|
9131 |
|
9132 | aspectRatioIdc = expGolombDecoder.readUnsignedByte();
|
9133 |
|
9134 | switch (aspectRatioIdc) {
|
9135 | case 1:
|
9136 | sarRatio = [1, 1];
|
9137 | break;
|
9138 |
|
9139 | case 2:
|
9140 | sarRatio = [12, 11];
|
9141 | break;
|
9142 |
|
9143 | case 3:
|
9144 | sarRatio = [10, 11];
|
9145 | break;
|
9146 |
|
9147 | case 4:
|
9148 | sarRatio = [16, 11];
|
9149 | break;
|
9150 |
|
9151 | case 5:
|
9152 | sarRatio = [40, 33];
|
9153 | break;
|
9154 |
|
9155 | case 6:
|
9156 | sarRatio = [24, 11];
|
9157 | break;
|
9158 |
|
9159 | case 7:
|
9160 | sarRatio = [20, 11];
|
9161 | break;
|
9162 |
|
9163 | case 8:
|
9164 | sarRatio = [32, 11];
|
9165 | break;
|
9166 |
|
9167 | case 9:
|
9168 | sarRatio = [80, 33];
|
9169 | break;
|
9170 |
|
9171 | case 10:
|
9172 | sarRatio = [18, 11];
|
9173 | break;
|
9174 |
|
9175 | case 11:
|
9176 | sarRatio = [15, 11];
|
9177 | break;
|
9178 |
|
9179 | case 12:
|
9180 | sarRatio = [64, 33];
|
9181 | break;
|
9182 |
|
9183 | case 13:
|
9184 | sarRatio = [160, 99];
|
9185 | break;
|
9186 |
|
9187 | case 14:
|
9188 | sarRatio = [4, 3];
|
9189 | break;
|
9190 |
|
9191 | case 15:
|
9192 | sarRatio = [3, 2];
|
9193 | break;
|
9194 |
|
9195 | case 16:
|
9196 | sarRatio = [2, 1];
|
9197 | break;
|
9198 |
|
9199 | case 255:
|
9200 | {
|
9201 | sarRatio = [expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte(), expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte()];
|
9202 | break;
|
9203 | }
|
9204 | }
|
9205 |
|
9206 | if (sarRatio) {
|
9207 | sarRatio[0] / sarRatio[1];
|
9208 | }
|
9209 | }
|
9210 | }
|
9211 |
|
9212 | return {
|
9213 | profileIdc: profileIdc,
|
9214 | levelIdc: levelIdc,
|
9215 | profileCompatibility: profileCompatibility,
|
9216 | width: (picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2,
|
9217 | height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2,
|
9218 |
|
9219 | sarRatio: sarRatio
|
9220 | };
|
9221 | };
|
9222 | };
|
9223 |
|
9224 | _H264Stream.prototype = new stream();
|
9225 | var h264 = {
|
9226 | H264Stream: _H264Stream,
|
9227 | NalByteStream: _NalByteStream
|
9228 | };
|
9229 | |
9230 |
|
9231 |
|
9232 |
|
9233 |
|
9234 |
|
9235 |
|
9236 |
|
9237 |
|
9238 | var ADTS_SAMPLING_FREQUENCIES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
9239 |
|
9240 | var parseId3TagSize = function parseId3TagSize(header, byteIndex) {
|
9241 | var returnSize = header[byteIndex + 6] << 21 | header[byteIndex + 7] << 14 | header[byteIndex + 8] << 7 | header[byteIndex + 9],
|
9242 | flags = header[byteIndex + 5],
|
9243 | footerPresent = (flags & 16) >> 4;
|
9244 |
|
9245 | returnSize = returnSize >= 0 ? returnSize : 0;
|
9246 |
|
9247 | if (footerPresent) {
|
9248 | return returnSize + 20;
|
9249 | }
|
9250 |
|
9251 | return returnSize + 10;
|
9252 | };
|
9253 |
|
9254 | var getId3Offset = function getId3Offset(data, offset) {
|
9255 | if (data.length - offset < 10 || data[offset] !== 'I'.charCodeAt(0) || data[offset + 1] !== 'D'.charCodeAt(0) || data[offset + 2] !== '3'.charCodeAt(0)) {
|
9256 | return offset;
|
9257 | }
|
9258 |
|
9259 | offset += parseId3TagSize(data, offset);
|
9260 | return getId3Offset(data, offset);
|
9261 | };
|
9262 |
|
9263 |
|
9264 | var isLikelyAacData$1 = function isLikelyAacData(data) {
|
9265 | var offset = getId3Offset(data, 0);
|
9266 | return data.length >= offset + 2 && (data[offset] & 0xFF) === 0xFF && (data[offset + 1] & 0xF0) === 0xF0 &&
|
9267 |
|
9268 | (data[offset + 1] & 0x16) === 0x10;
|
9269 | };
|
9270 |
|
9271 | var parseSyncSafeInteger = function parseSyncSafeInteger(data) {
|
9272 | return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
|
9273 | };
|
9274 |
|
9275 |
|
9276 |
|
9277 | var percentEncode = function percentEncode(bytes, start, end) {
|
9278 | var i,
|
9279 | result = '';
|
9280 |
|
9281 | for (i = start; i < end; i++) {
|
9282 | result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
|
9283 | }
|
9284 |
|
9285 | return result;
|
9286 | };
|
9287 |
|
9288 |
|
9289 |
|
9290 | var parseIso88591 = function parseIso88591(bytes, start, end) {
|
9291 | return unescape(percentEncode(bytes, start, end));
|
9292 | };
|
9293 |
|
9294 | var parseAdtsSize = function parseAdtsSize(header, byteIndex) {
|
9295 | var lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
|
9296 | middle = header[byteIndex + 4] << 3,
|
9297 | highTwo = header[byteIndex + 3] & 0x3 << 11;
|
9298 | return highTwo | middle | lowThree;
|
9299 | };
|
9300 |
|
9301 | var parseType$2 = function parseType(header, byteIndex) {
|
9302 | if (header[byteIndex] === 'I'.charCodeAt(0) && header[byteIndex + 1] === 'D'.charCodeAt(0) && header[byteIndex + 2] === '3'.charCodeAt(0)) {
|
9303 | return 'timed-metadata';
|
9304 | } else if (header[byteIndex] & 0xff === 0xff && (header[byteIndex + 1] & 0xf0) === 0xf0) {
|
9305 | return 'audio';
|
9306 | }
|
9307 |
|
9308 | return null;
|
9309 | };
|
9310 |
|
9311 | var parseSampleRate = function parseSampleRate(packet) {
|
9312 | var i = 0;
|
9313 |
|
9314 | while (i + 5 < packet.length) {
|
9315 | if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
|
9316 |
|
9317 |
|
9318 | i++;
|
9319 | continue;
|
9320 | }
|
9321 |
|
9322 | return ADTS_SAMPLING_FREQUENCIES[(packet[i + 2] & 0x3c) >>> 2];
|
9323 | }
|
9324 |
|
9325 | return null;
|
9326 | };
|
9327 |
|
9328 | var parseAacTimestamp = function parseAacTimestamp(packet) {
|
9329 | var frameStart, frameSize, frame, frameHeader;
|
9330 |
|
9331 | frameStart = 10;
|
9332 |
|
9333 | if (packet[5] & 0x40) {
|
9334 |
|
9335 | frameStart += 4;
|
9336 |
|
9337 | frameStart += parseSyncSafeInteger(packet.subarray(10, 14));
|
9338 | }
|
9339 |
|
9340 |
|
9341 |
|
9342 | do {
|
9343 |
|
9344 | frameSize = parseSyncSafeInteger(packet.subarray(frameStart + 4, frameStart + 8));
|
9345 |
|
9346 | if (frameSize < 1) {
|
9347 | return null;
|
9348 | }
|
9349 |
|
9350 | frameHeader = String.fromCharCode(packet[frameStart], packet[frameStart + 1], packet[frameStart + 2], packet[frameStart + 3]);
|
9351 |
|
9352 | if (frameHeader === 'PRIV') {
|
9353 | frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
|
9354 |
|
9355 | for (var i = 0; i < frame.byteLength; i++) {
|
9356 | if (frame[i] === 0) {
|
9357 | var owner = parseIso88591(frame, 0, i);
|
9358 |
|
9359 | if (owner === 'com.apple.streaming.transportStreamTimestamp') {
|
9360 | var d = frame.subarray(i + 1);
|
9361 | var size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
|
9362 | size *= 4;
|
9363 | size += d[7] & 0x03;
|
9364 | return size;
|
9365 | }
|
9366 |
|
9367 | break;
|
9368 | }
|
9369 | }
|
9370 | }
|
9371 |
|
9372 | frameStart += 10;
|
9373 |
|
9374 | frameStart += frameSize;
|
9375 | } while (frameStart < packet.byteLength);
|
9376 |
|
9377 | return null;
|
9378 | };
|
9379 |
|
9380 | var utils = {
|
9381 | isLikelyAacData: isLikelyAacData$1,
|
9382 | parseId3TagSize: parseId3TagSize,
|
9383 | parseAdtsSize: parseAdtsSize,
|
9384 | parseType: parseType$2,
|
9385 | parseSampleRate: parseSampleRate,
|
9386 | parseAacTimestamp: parseAacTimestamp
|
9387 | };
|
9388 |
|
9389 | var _AacStream;
|
9390 | |
9391 |
|
9392 |
|
9393 |
|
9394 |
|
9395 | _AacStream = function AacStream() {
|
9396 | var everything = new Uint8Array(),
|
9397 | timeStamp = 0;
|
9398 |
|
9399 | _AacStream.prototype.init.call(this);
|
9400 |
|
9401 | this.setTimestamp = function (timestamp) {
|
9402 | timeStamp = timestamp;
|
9403 | };
|
9404 |
|
9405 | this.push = function (bytes) {
|
9406 | var frameSize = 0,
|
9407 | byteIndex = 0,
|
9408 | bytesLeft,
|
9409 | chunk,
|
9410 | packet,
|
9411 | tempLength;
|
9412 |
|
9413 |
|
9414 | if (everything.length) {
|
9415 | tempLength = everything.length;
|
9416 | everything = new Uint8Array(bytes.byteLength + tempLength);
|
9417 | everything.set(everything.subarray(0, tempLength));
|
9418 | everything.set(bytes, tempLength);
|
9419 | } else {
|
9420 | everything = bytes;
|
9421 | }
|
9422 |
|
9423 | while (everything.length - byteIndex >= 3) {
|
9424 | if (everything[byteIndex] === 'I'.charCodeAt(0) && everything[byteIndex + 1] === 'D'.charCodeAt(0) && everything[byteIndex + 2] === '3'.charCodeAt(0)) {
|
9425 |
|
9426 |
|
9427 | if (everything.length - byteIndex < 10) {
|
9428 | break;
|
9429 | }
|
9430 |
|
9431 |
|
9432 | frameSize = utils.parseId3TagSize(everything, byteIndex);
|
9433 |
|
9434 |
|
9435 |
|
9436 | if (byteIndex + frameSize > everything.length) {
|
9437 | break;
|
9438 | }
|
9439 |
|
9440 | chunk = {
|
9441 | type: 'timed-metadata',
|
9442 | data: everything.subarray(byteIndex, byteIndex + frameSize)
|
9443 | };
|
9444 | this.trigger('data', chunk);
|
9445 | byteIndex += frameSize;
|
9446 | continue;
|
9447 | } else if ((everything[byteIndex] & 0xff) === 0xff && (everything[byteIndex + 1] & 0xf0) === 0xf0) {
|
9448 |
|
9449 |
|
9450 | if (everything.length - byteIndex < 7) {
|
9451 | break;
|
9452 | }
|
9453 |
|
9454 | frameSize = utils.parseAdtsSize(everything, byteIndex);
|
9455 |
|
9456 |
|
9457 | if (byteIndex + frameSize > everything.length) {
|
9458 | break;
|
9459 | }
|
9460 |
|
9461 | packet = {
|
9462 | type: 'audio',
|
9463 | data: everything.subarray(byteIndex, byteIndex + frameSize),
|
9464 | pts: timeStamp,
|
9465 | dts: timeStamp
|
9466 | };
|
9467 | this.trigger('data', packet);
|
9468 | byteIndex += frameSize;
|
9469 | continue;
|
9470 | }
|
9471 |
|
9472 | byteIndex++;
|
9473 | }
|
9474 |
|
9475 | bytesLeft = everything.length - byteIndex;
|
9476 |
|
9477 | if (bytesLeft > 0) {
|
9478 | everything = everything.subarray(byteIndex);
|
9479 | } else {
|
9480 | everything = new Uint8Array();
|
9481 | }
|
9482 | };
|
9483 |
|
9484 | this.reset = function () {
|
9485 | everything = new Uint8Array();
|
9486 | this.trigger('reset');
|
9487 | };
|
9488 |
|
9489 | this.endTimeline = function () {
|
9490 | everything = new Uint8Array();
|
9491 | this.trigger('endedtimeline');
|
9492 | };
|
9493 | };
|
9494 |
|
9495 | _AacStream.prototype = new stream();
|
9496 | var aac = _AacStream;
|
9497 |
|
9498 | var AUDIO_PROPERTIES = ['audioobjecttype', 'channelcount', 'samplerate', 'samplingfrequencyindex', 'samplesize'];
|
9499 | var audioProperties = AUDIO_PROPERTIES;
|
9500 | var VIDEO_PROPERTIES = ['width', 'height', 'profileIdc', 'levelIdc', 'profileCompatibility', 'sarRatio'];
|
9501 | var videoProperties = VIDEO_PROPERTIES;
|
9502 | var H264Stream = h264.H264Stream;
|
9503 | var isLikelyAacData = utils.isLikelyAacData;
|
9504 | var ONE_SECOND_IN_TS$1 = clock.ONE_SECOND_IN_TS;
|
9505 |
|
9506 | var _VideoSegmentStream, _AudioSegmentStream, _Transmuxer, _CoalesceStream;
|
9507 |
|
9508 | var retriggerForStream = function retriggerForStream(key, event) {
|
9509 | event.stream = key;
|
9510 | this.trigger('log', event);
|
9511 | };
|
9512 |
|
9513 | var addPipelineLogRetriggers = function addPipelineLogRetriggers(transmuxer, pipeline) {
|
9514 | var keys = Object.keys(pipeline);
|
9515 |
|
9516 | for (var i = 0; i < keys.length; i++) {
|
9517 | var key = keys[i];
|
9518 |
|
9519 |
|
9520 | if (key === 'headOfPipeline' || !pipeline[key].on) {
|
9521 | continue;
|
9522 | }
|
9523 |
|
9524 | pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
|
9525 | }
|
9526 | };
|
9527 | |
9528 |
|
9529 |
|
9530 |
|
9531 |
|
9532 | var arrayEquals = function arrayEquals(a, b) {
|
9533 | var i;
|
9534 |
|
9535 | if (a.length !== b.length) {
|
9536 | return false;
|
9537 | }
|
9538 |
|
9539 |
|
9540 | for (i = 0; i < a.length; i++) {
|
9541 | if (a[i] !== b[i]) {
|
9542 | return false;
|
9543 | }
|
9544 | }
|
9545 |
|
9546 | return true;
|
9547 | };
|
9548 |
|
9549 | var generateSegmentTimingInfo = function generateSegmentTimingInfo(baseMediaDecodeTime, startDts, startPts, endDts, endPts, prependedContentDuration) {
|
9550 | var ptsOffsetFromDts = startPts - startDts,
|
9551 | decodeDuration = endDts - startDts,
|
9552 | presentationDuration = endPts - startPts;
|
9553 |
|
9554 |
|
9555 |
|
9556 |
|
9557 | return {
|
9558 | start: {
|
9559 | dts: baseMediaDecodeTime,
|
9560 | pts: baseMediaDecodeTime + ptsOffsetFromDts
|
9561 | },
|
9562 | end: {
|
9563 | dts: baseMediaDecodeTime + decodeDuration,
|
9564 | pts: baseMediaDecodeTime + presentationDuration
|
9565 | },
|
9566 | prependedContentDuration: prependedContentDuration,
|
9567 | baseMediaDecodeTime: baseMediaDecodeTime
|
9568 | };
|
9569 | };
|
9570 | |
9571 |
|
9572 |
|
9573 |
|
9574 |
|
9575 |
|
9576 |
|
9577 |
|
9578 |
|
9579 |
|
9580 |
|
9581 | _AudioSegmentStream = function AudioSegmentStream(track, options) {
|
9582 | var adtsFrames = [],
|
9583 | sequenceNumber,
|
9584 | earliestAllowedDts = 0,
|
9585 | audioAppendStartTs = 0,
|
9586 | videoBaseMediaDecodeTime = Infinity;
|
9587 | options = options || {};
|
9588 | sequenceNumber = options.firstSequenceNumber || 0;
|
9589 |
|
9590 | _AudioSegmentStream.prototype.init.call(this);
|
9591 |
|
9592 | this.push = function (data) {
|
9593 | trackDecodeInfo.collectDtsInfo(track, data);
|
9594 |
|
9595 | if (track) {
|
9596 | audioProperties.forEach(function (prop) {
|
9597 | track[prop] = data[prop];
|
9598 | });
|
9599 | }
|
9600 |
|
9601 |
|
9602 | adtsFrames.push(data);
|
9603 | };
|
9604 |
|
9605 | this.setEarliestDts = function (earliestDts) {
|
9606 | earliestAllowedDts = earliestDts;
|
9607 | };
|
9608 |
|
9609 | this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
|
9610 | videoBaseMediaDecodeTime = baseMediaDecodeTime;
|
9611 | };
|
9612 |
|
9613 | this.setAudioAppendStart = function (timestamp) {
|
9614 | audioAppendStartTs = timestamp;
|
9615 | };
|
9616 |
|
9617 | this.flush = function () {
|
9618 | var frames, moof, mdat, boxes, frameDuration, segmentDuration, videoClockCyclesOfSilencePrefixed;
|
9619 |
|
9620 | if (adtsFrames.length === 0) {
|
9621 | this.trigger('done', 'AudioSegmentStream');
|
9622 | return;
|
9623 | }
|
9624 |
|
9625 | frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
|
9626 | track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
|
9627 |
|
9628 | videoClockCyclesOfSilencePrefixed = audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime);
|
9629 |
|
9630 |
|
9631 | track.samples = audioFrameUtils.generateSampleTable(frames);
|
9632 |
|
9633 | mdat = mp4Generator.mdat(audioFrameUtils.concatenateFrameData(frames));
|
9634 | adtsFrames = [];
|
9635 | moof = mp4Generator.moof(sequenceNumber, [track]);
|
9636 | boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
|
9637 |
|
9638 | sequenceNumber++;
|
9639 | boxes.set(moof);
|
9640 | boxes.set(mdat, moof.byteLength);
|
9641 | trackDecodeInfo.clearDtsInfo(track);
|
9642 | frameDuration = Math.ceil(ONE_SECOND_IN_TS$1 * 1024 / track.samplerate);
|
9643 |
|
9644 |
|
9645 |
|
9646 |
|
9647 | if (frames.length) {
|
9648 | segmentDuration = frames.length * frameDuration;
|
9649 | this.trigger('segmentTimingInfo', generateSegmentTimingInfo(
|
9650 |
|
9651 |
|
9652 | clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate),
|
9653 | frames[0].dts, frames[0].pts, frames[0].dts + segmentDuration, frames[0].pts + segmentDuration, videoClockCyclesOfSilencePrefixed || 0));
|
9654 | this.trigger('timingInfo', {
|
9655 | start: frames[0].pts,
|
9656 | end: frames[0].pts + segmentDuration
|
9657 | });
|
9658 | }
|
9659 |
|
9660 | this.trigger('data', {
|
9661 | track: track,
|
9662 | boxes: boxes
|
9663 | });
|
9664 | this.trigger('done', 'AudioSegmentStream');
|
9665 | };
|
9666 |
|
9667 | this.reset = function () {
|
9668 | trackDecodeInfo.clearDtsInfo(track);
|
9669 | adtsFrames = [];
|
9670 | this.trigger('reset');
|
9671 | };
|
9672 | };
|
9673 |
|
9674 | _AudioSegmentStream.prototype = new stream();
|
9675 | |
9676 |
|
9677 |
|
9678 |
|
9679 |
|
9680 |
|
9681 |
|
9682 |
|
9683 |
|
9684 |
|
9685 |
|
9686 |
|
9687 | _VideoSegmentStream = function VideoSegmentStream(track, options) {
|
9688 | var sequenceNumber,
|
9689 | nalUnits = [],
|
9690 | gopsToAlignWith = [],
|
9691 | config,
|
9692 | pps;
|
9693 | options = options || {};
|
9694 | sequenceNumber = options.firstSequenceNumber || 0;
|
9695 |
|
9696 | _VideoSegmentStream.prototype.init.call(this);
|
9697 |
|
9698 | delete track.minPTS;
|
9699 | this.gopCache_ = [];
|
9700 | |
9701 |
|
9702 |
|
9703 |
|
9704 |
|
9705 |
|
9706 |
|
9707 |
|
9708 |
|
9709 | this.push = function (nalUnit) {
|
9710 | trackDecodeInfo.collectDtsInfo(track, nalUnit);
|
9711 |
|
9712 | if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
|
9713 | config = nalUnit.config;
|
9714 | track.sps = [nalUnit.data];
|
9715 | videoProperties.forEach(function (prop) {
|
9716 | track[prop] = config[prop];
|
9717 | }, this);
|
9718 | }
|
9719 |
|
9720 | if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
|
9721 | pps = nalUnit.data;
|
9722 | track.pps = [nalUnit.data];
|
9723 | }
|
9724 |
|
9725 |
|
9726 | nalUnits.push(nalUnit);
|
9727 | };
|
9728 | |
9729 |
|
9730 |
|
9731 |
|
9732 |
|
9733 |
|
9734 | this.flush = function () {
|
9735 | var frames,
|
9736 | gopForFusion,
|
9737 | gops,
|
9738 | moof,
|
9739 | mdat,
|
9740 | boxes,
|
9741 | prependedContentDuration = 0,
|
9742 | firstGop,
|
9743 | lastGop;
|
9744 |
|
9745 |
|
9746 | while (nalUnits.length) {
|
9747 | if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
|
9748 | break;
|
9749 | }
|
9750 |
|
9751 | nalUnits.shift();
|
9752 | }
|
9753 |
|
9754 |
|
9755 | if (nalUnits.length === 0) {
|
9756 | this.resetStream_();
|
9757 | this.trigger('done', 'VideoSegmentStream');
|
9758 | return;
|
9759 | }
|
9760 |
|
9761 |
|
9762 |
|
9763 |
|
9764 | frames = frameUtils.groupNalsIntoFrames(nalUnits);
|
9765 | gops = frameUtils.groupFramesIntoGops(frames);
|
9766 |
|
9767 |
|
9768 |
|
9769 |
|
9770 |
|
9771 |
|
9772 |
|
9773 |
|
9774 |
|
9775 |
|
9776 |
|
9777 |
|
9778 |
|
9779 |
|
9780 |
|
9781 |
|
9782 |
|
9783 |
|
9784 | if (!gops[0][0].keyFrame) {
|
9785 |
|
9786 | gopForFusion = this.getGopForFusion_(nalUnits[0], track);
|
9787 |
|
9788 | if (gopForFusion) {
|
9789 |
|
9790 |
|
9791 | prependedContentDuration = gopForFusion.duration;
|
9792 | gops.unshift(gopForFusion);
|
9793 |
|
9794 |
|
9795 | gops.byteLength += gopForFusion.byteLength;
|
9796 | gops.nalCount += gopForFusion.nalCount;
|
9797 | gops.pts = gopForFusion.pts;
|
9798 | gops.dts = gopForFusion.dts;
|
9799 | gops.duration += gopForFusion.duration;
|
9800 | } else {
|
9801 |
|
9802 | gops = frameUtils.extendFirstKeyFrame(gops);
|
9803 | }
|
9804 | }
|
9805 |
|
9806 |
|
9807 | if (gopsToAlignWith.length) {
|
9808 | var alignedGops;
|
9809 |
|
9810 | if (options.alignGopsAtEnd) {
|
9811 | alignedGops = this.alignGopsAtEnd_(gops);
|
9812 | } else {
|
9813 | alignedGops = this.alignGopsAtStart_(gops);
|
9814 | }
|
9815 |
|
9816 | if (!alignedGops) {
|
9817 |
|
9818 | this.gopCache_.unshift({
|
9819 | gop: gops.pop(),
|
9820 | pps: track.pps,
|
9821 | sps: track.sps
|
9822 | });
|
9823 |
|
9824 | this.gopCache_.length = Math.min(6, this.gopCache_.length);
|
9825 |
|
9826 | nalUnits = [];
|
9827 |
|
9828 | this.resetStream_();
|
9829 | this.trigger('done', 'VideoSegmentStream');
|
9830 | return;
|
9831 | }
|
9832 |
|
9833 |
|
9834 |
|
9835 | trackDecodeInfo.clearDtsInfo(track);
|
9836 | gops = alignedGops;
|
9837 | }
|
9838 |
|
9839 | trackDecodeInfo.collectDtsInfo(track, gops);
|
9840 |
|
9841 |
|
9842 | track.samples = frameUtils.generateSampleTable(gops);
|
9843 |
|
9844 | mdat = mp4Generator.mdat(frameUtils.concatenateNalData(gops));
|
9845 | track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
|
9846 | this.trigger('processedGopsInfo', gops.map(function (gop) {
|
9847 | return {
|
9848 | pts: gop.pts,
|
9849 | dts: gop.dts,
|
9850 | byteLength: gop.byteLength
|
9851 | };
|
9852 | }));
|
9853 | firstGop = gops[0];
|
9854 | lastGop = gops[gops.length - 1];
|
9855 | this.trigger('segmentTimingInfo', generateSegmentTimingInfo(track.baseMediaDecodeTime, firstGop.dts, firstGop.pts, lastGop.dts + lastGop.duration, lastGop.pts + lastGop.duration, prependedContentDuration));
|
9856 | this.trigger('timingInfo', {
|
9857 | start: gops[0].pts,
|
9858 | end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
|
9859 | });
|
9860 |
|
9861 | this.gopCache_.unshift({
|
9862 | gop: gops.pop(),
|
9863 | pps: track.pps,
|
9864 | sps: track.sps
|
9865 | });
|
9866 |
|
9867 | this.gopCache_.length = Math.min(6, this.gopCache_.length);
|
9868 |
|
9869 | nalUnits = [];
|
9870 | this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
|
9871 | this.trigger('timelineStartInfo', track.timelineStartInfo);
|
9872 | moof = mp4Generator.moof(sequenceNumber, [track]);
|
9873 |
|
9874 |
|
9875 | boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
|
9876 |
|
9877 | sequenceNumber++;
|
9878 | boxes.set(moof);
|
9879 | boxes.set(mdat, moof.byteLength);
|
9880 | this.trigger('data', {
|
9881 | track: track,
|
9882 | boxes: boxes
|
9883 | });
|
9884 | this.resetStream_();
|
9885 |
|
9886 | this.trigger('done', 'VideoSegmentStream');
|
9887 | };
|
9888 |
|
9889 | this.reset = function () {
|
9890 | this.resetStream_();
|
9891 | nalUnits = [];
|
9892 | this.gopCache_.length = 0;
|
9893 | gopsToAlignWith.length = 0;
|
9894 | this.trigger('reset');
|
9895 | };
|
9896 |
|
9897 | this.resetStream_ = function () {
|
9898 | trackDecodeInfo.clearDtsInfo(track);
|
9899 |
|
9900 |
|
9901 | config = undefined;
|
9902 | pps = undefined;
|
9903 | };
|
9904 |
|
9905 |
|
9906 |
|
9907 | this.getGopForFusion_ = function (nalUnit) {
|
9908 | var halfSecond = 45000,
|
9909 |
|
9910 | allowableOverlap = 10000,
|
9911 |
|
9912 | nearestDistance = Infinity,
|
9913 | dtsDistance,
|
9914 | nearestGopObj,
|
9915 | currentGop,
|
9916 | currentGopObj,
|
9917 | i;
|
9918 |
|
9919 | for (i = 0; i < this.gopCache_.length; i++) {
|
9920 | currentGopObj = this.gopCache_[i];
|
9921 | currentGop = currentGopObj.gop;
|
9922 |
|
9923 | if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) || !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
|
9924 | continue;
|
9925 | }
|
9926 |
|
9927 |
|
9928 | if (currentGop.dts < track.timelineStartInfo.dts) {
|
9929 | continue;
|
9930 | }
|
9931 |
|
9932 |
|
9933 | dtsDistance = nalUnit.dts - currentGop.dts - currentGop.duration;
|
9934 |
|
9935 |
|
9936 | if (dtsDistance >= -allowableOverlap && dtsDistance <= halfSecond) {
|
9937 |
|
9938 |
|
9939 | if (!nearestGopObj || nearestDistance > dtsDistance) {
|
9940 | nearestGopObj = currentGopObj;
|
9941 | nearestDistance = dtsDistance;
|
9942 | }
|
9943 | }
|
9944 | }
|
9945 |
|
9946 | if (nearestGopObj) {
|
9947 | return nearestGopObj.gop;
|
9948 | }
|
9949 |
|
9950 | return null;
|
9951 | };
|
9952 |
|
9953 |
|
9954 |
|
9955 | this.alignGopsAtStart_ = function (gops) {
|
9956 | var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
|
9957 | byteLength = gops.byteLength;
|
9958 | nalCount = gops.nalCount;
|
9959 | duration = gops.duration;
|
9960 | alignIndex = gopIndex = 0;
|
9961 |
|
9962 | while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
|
9963 | align = gopsToAlignWith[alignIndex];
|
9964 | gop = gops[gopIndex];
|
9965 |
|
9966 | if (align.pts === gop.pts) {
|
9967 | break;
|
9968 | }
|
9969 |
|
9970 | if (gop.pts > align.pts) {
|
9971 |
|
9972 |
|
9973 | alignIndex++;
|
9974 | continue;
|
9975 | }
|
9976 |
|
9977 |
|
9978 |
|
9979 | gopIndex++;
|
9980 | byteLength -= gop.byteLength;
|
9981 | nalCount -= gop.nalCount;
|
9982 | duration -= gop.duration;
|
9983 | }
|
9984 |
|
9985 | if (gopIndex === 0) {
|
9986 |
|
9987 | return gops;
|
9988 | }
|
9989 |
|
9990 | if (gopIndex === gops.length) {
|
9991 |
|
9992 | return null;
|
9993 | }
|
9994 |
|
9995 | alignedGops = gops.slice(gopIndex);
|
9996 | alignedGops.byteLength = byteLength;
|
9997 | alignedGops.duration = duration;
|
9998 | alignedGops.nalCount = nalCount;
|
9999 | alignedGops.pts = alignedGops[0].pts;
|
10000 | alignedGops.dts = alignedGops[0].dts;
|
10001 | return alignedGops;
|
10002 | };
|
10003 |
|
10004 |
|
10005 |
|
10006 | this.alignGopsAtEnd_ = function (gops) {
|
10007 | var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
|
10008 | alignIndex = gopsToAlignWith.length - 1;
|
10009 | gopIndex = gops.length - 1;
|
10010 | alignEndIndex = null;
|
10011 | matchFound = false;
|
10012 |
|
10013 | while (alignIndex >= 0 && gopIndex >= 0) {
|
10014 | align = gopsToAlignWith[alignIndex];
|
10015 | gop = gops[gopIndex];
|
10016 |
|
10017 | if (align.pts === gop.pts) {
|
10018 | matchFound = true;
|
10019 | break;
|
10020 | }
|
10021 |
|
10022 | if (align.pts > gop.pts) {
|
10023 | alignIndex--;
|
10024 | continue;
|
10025 | }
|
10026 |
|
10027 | if (alignIndex === gopsToAlignWith.length - 1) {
|
10028 |
|
10029 |
|
10030 |
|
10031 | alignEndIndex = gopIndex;
|
10032 | }
|
10033 |
|
10034 | gopIndex--;
|
10035 | }
|
10036 |
|
10037 | if (!matchFound && alignEndIndex === null) {
|
10038 | return null;
|
10039 | }
|
10040 |
|
10041 | var trimIndex;
|
10042 |
|
10043 | if (matchFound) {
|
10044 | trimIndex = gopIndex;
|
10045 | } else {
|
10046 | trimIndex = alignEndIndex;
|
10047 | }
|
10048 |
|
10049 | if (trimIndex === 0) {
|
10050 | return gops;
|
10051 | }
|
10052 |
|
10053 | var alignedGops = gops.slice(trimIndex);
|
10054 | var metadata = alignedGops.reduce(function (total, gop) {
|
10055 | total.byteLength += gop.byteLength;
|
10056 | total.duration += gop.duration;
|
10057 | total.nalCount += gop.nalCount;
|
10058 | return total;
|
10059 | }, {
|
10060 | byteLength: 0,
|
10061 | duration: 0,
|
10062 | nalCount: 0
|
10063 | });
|
10064 | alignedGops.byteLength = metadata.byteLength;
|
10065 | alignedGops.duration = metadata.duration;
|
10066 | alignedGops.nalCount = metadata.nalCount;
|
10067 | alignedGops.pts = alignedGops[0].pts;
|
10068 | alignedGops.dts = alignedGops[0].dts;
|
10069 | return alignedGops;
|
10070 | };
|
10071 |
|
10072 | this.alignGopsWith = function (newGopsToAlignWith) {
|
10073 | gopsToAlignWith = newGopsToAlignWith;
|
10074 | };
|
10075 | };
|
10076 |
|
10077 | _VideoSegmentStream.prototype = new stream();
|
10078 | |
10079 |
|
10080 |
|
10081 |
|
10082 |
|
10083 |
|
10084 |
|
10085 |
|
10086 |
|
10087 | _CoalesceStream = function CoalesceStream(options, metadataStream) {
|
10088 |
|
10089 |
|
10090 |
|
10091 | this.numberOfTracks = 0;
|
10092 | this.metadataStream = metadataStream;
|
10093 | options = options || {};
|
10094 |
|
10095 | if (typeof options.remux !== 'undefined') {
|
10096 | this.remuxTracks = !!options.remux;
|
10097 | } else {
|
10098 | this.remuxTracks = true;
|
10099 | }
|
10100 |
|
10101 | if (typeof options.keepOriginalTimestamps === 'boolean') {
|
10102 | this.keepOriginalTimestamps = options.keepOriginalTimestamps;
|
10103 | } else {
|
10104 | this.keepOriginalTimestamps = false;
|
10105 | }
|
10106 |
|
10107 | this.pendingTracks = [];
|
10108 | this.videoTrack = null;
|
10109 | this.pendingBoxes = [];
|
10110 | this.pendingCaptions = [];
|
10111 | this.pendingMetadata = [];
|
10112 | this.pendingBytes = 0;
|
10113 | this.emittedTracks = 0;
|
10114 |
|
10115 | _CoalesceStream.prototype.init.call(this);
|
10116 |
|
10117 |
|
10118 | this.push = function (output) {
|
10119 |
|
10120 |
|
10121 | if (output.text) {
|
10122 | return this.pendingCaptions.push(output);
|
10123 | }
|
10124 |
|
10125 |
|
10126 | if (output.frames) {
|
10127 | return this.pendingMetadata.push(output);
|
10128 | }
|
10129 |
|
10130 |
|
10131 |
|
10132 |
|
10133 | this.pendingTracks.push(output.track);
|
10134 | this.pendingBytes += output.boxes.byteLength;
|
10135 |
|
10136 |
|
10137 |
|
10138 |
|
10139 |
|
10140 |
|
10141 | if (output.track.type === 'video') {
|
10142 | this.videoTrack = output.track;
|
10143 | this.pendingBoxes.push(output.boxes);
|
10144 | }
|
10145 |
|
10146 | if (output.track.type === 'audio') {
|
10147 | this.audioTrack = output.track;
|
10148 | this.pendingBoxes.unshift(output.boxes);
|
10149 | }
|
10150 | };
|
10151 | };
|
10152 |
|
10153 | _CoalesceStream.prototype = new stream();
|
10154 |
|
10155 | _CoalesceStream.prototype.flush = function (flushSource) {
|
10156 | var offset = 0,
|
10157 | event = {
|
10158 | captions: [],
|
10159 | captionStreams: {},
|
10160 | metadata: [],
|
10161 | info: {}
|
10162 | },
|
10163 | caption,
|
10164 | id3,
|
10165 | initSegment,
|
10166 | timelineStartPts = 0,
|
10167 | i;
|
10168 |
|
10169 | if (this.pendingTracks.length < this.numberOfTracks) {
|
10170 | if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
|
10171 |
|
10172 |
|
10173 |
|
10174 | return;
|
10175 | } else if (this.remuxTracks) {
|
10176 |
|
10177 |
|
10178 | return;
|
10179 | } else if (this.pendingTracks.length === 0) {
|
10180 |
|
10181 |
|
10182 |
|
10183 |
|
10184 |
|
10185 |
|
10186 | this.emittedTracks++;
|
10187 |
|
10188 | if (this.emittedTracks >= this.numberOfTracks) {
|
10189 | this.trigger('done');
|
10190 | this.emittedTracks = 0;
|
10191 | }
|
10192 |
|
10193 | return;
|
10194 | }
|
10195 | }
|
10196 |
|
10197 | if (this.videoTrack) {
|
10198 | timelineStartPts = this.videoTrack.timelineStartInfo.pts;
|
10199 | videoProperties.forEach(function (prop) {
|
10200 | event.info[prop] = this.videoTrack[prop];
|
10201 | }, this);
|
10202 | } else if (this.audioTrack) {
|
10203 | timelineStartPts = this.audioTrack.timelineStartInfo.pts;
|
10204 | audioProperties.forEach(function (prop) {
|
10205 | event.info[prop] = this.audioTrack[prop];
|
10206 | }, this);
|
10207 | }
|
10208 |
|
10209 | if (this.videoTrack || this.audioTrack) {
|
10210 | if (this.pendingTracks.length === 1) {
|
10211 | event.type = this.pendingTracks[0].type;
|
10212 | } else {
|
10213 | event.type = 'combined';
|
10214 | }
|
10215 |
|
10216 | this.emittedTracks += this.pendingTracks.length;
|
10217 | initSegment = mp4Generator.initSegment(this.pendingTracks);
|
10218 |
|
10219 | event.initSegment = new Uint8Array(initSegment.byteLength);
|
10220 |
|
10221 |
|
10222 | event.initSegment.set(initSegment);
|
10223 |
|
10224 | event.data = new Uint8Array(this.pendingBytes);
|
10225 |
|
10226 | for (i = 0; i < this.pendingBoxes.length; i++) {
|
10227 | event.data.set(this.pendingBoxes[i], offset);
|
10228 | offset += this.pendingBoxes[i].byteLength;
|
10229 | }
|
10230 |
|
10231 |
|
10232 |
|
10233 | for (i = 0; i < this.pendingCaptions.length; i++) {
|
10234 | caption = this.pendingCaptions[i];
|
10235 | caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
|
10236 | caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
|
10237 | event.captionStreams[caption.stream] = true;
|
10238 | event.captions.push(caption);
|
10239 | }
|
10240 |
|
10241 |
|
10242 |
|
10243 | for (i = 0; i < this.pendingMetadata.length; i++) {
|
10244 | id3 = this.pendingMetadata[i];
|
10245 | id3.cueTime = clock.metadataTsToSeconds(id3.pts, timelineStartPts, this.keepOriginalTimestamps);
|
10246 | event.metadata.push(id3);
|
10247 | }
|
10248 |
|
10249 |
|
10250 |
|
10251 | event.metadata.dispatchType = this.metadataStream.dispatchType;
|
10252 |
|
10253 | this.pendingTracks.length = 0;
|
10254 | this.videoTrack = null;
|
10255 | this.pendingBoxes.length = 0;
|
10256 | this.pendingCaptions.length = 0;
|
10257 | this.pendingBytes = 0;
|
10258 | this.pendingMetadata.length = 0;
|
10259 |
|
10260 |
|
10261 |
|
10262 | this.trigger('data', event);
|
10263 |
|
10264 |
|
10265 |
|
10266 |
|
10267 | for (i = 0; i < event.captions.length; i++) {
|
10268 | caption = event.captions[i];
|
10269 | this.trigger('caption', caption);
|
10270 | }
|
10271 |
|
10272 |
|
10273 |
|
10274 |
|
10275 |
|
10276 | for (i = 0; i < event.metadata.length; i++) {
|
10277 | id3 = event.metadata[i];
|
10278 | this.trigger('id3Frame', id3);
|
10279 | }
|
10280 | }
|
10281 |
|
10282 |
|
10283 | if (this.emittedTracks >= this.numberOfTracks) {
|
10284 | this.trigger('done');
|
10285 | this.emittedTracks = 0;
|
10286 | }
|
10287 | };
|
10288 |
|
10289 | _CoalesceStream.prototype.setRemux = function (val) {
|
10290 | this.remuxTracks = val;
|
10291 | };
|
10292 | |
10293 |
|
10294 |
|
10295 |
|
10296 |
|
10297 |
|
10298 |
|
10299 |
|
10300 | _Transmuxer = function Transmuxer(options) {
|
10301 | var self = this,
|
10302 | hasFlushed = true,
|
10303 | videoTrack,
|
10304 | audioTrack;
|
10305 |
|
10306 | _Transmuxer.prototype.init.call(this);
|
10307 |
|
10308 | options = options || {};
|
10309 | this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
|
10310 | this.transmuxPipeline_ = {};
|
10311 |
|
10312 | this.setupAacPipeline = function () {
|
10313 | var pipeline = {};
|
10314 | this.transmuxPipeline_ = pipeline;
|
10315 | pipeline.type = 'aac';
|
10316 | pipeline.metadataStream = new m2ts_1.MetadataStream();
|
10317 |
|
10318 | pipeline.aacStream = new aac();
|
10319 | pipeline.audioTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('audio');
|
10320 | pipeline.timedMetadataTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('timed-metadata');
|
10321 | pipeline.adtsStream = new adts();
|
10322 | pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
|
10323 | pipeline.headOfPipeline = pipeline.aacStream;
|
10324 | pipeline.aacStream.pipe(pipeline.audioTimestampRolloverStream).pipe(pipeline.adtsStream);
|
10325 | pipeline.aacStream.pipe(pipeline.timedMetadataTimestampRolloverStream).pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
|
10326 | pipeline.metadataStream.on('timestamp', function (frame) {
|
10327 | pipeline.aacStream.setTimestamp(frame.timeStamp);
|
10328 | });
|
10329 | pipeline.aacStream.on('data', function (data) {
|
10330 | if (data.type !== 'timed-metadata' && data.type !== 'audio' || pipeline.audioSegmentStream) {
|
10331 | return;
|
10332 | }
|
10333 |
|
10334 | audioTrack = audioTrack || {
|
10335 | timelineStartInfo: {
|
10336 | baseMediaDecodeTime: self.baseMediaDecodeTime
|
10337 | },
|
10338 | codec: 'adts',
|
10339 | type: 'audio'
|
10340 | };
|
10341 |
|
10342 | pipeline.coalesceStream.numberOfTracks++;
|
10343 | pipeline.audioSegmentStream = new _AudioSegmentStream(audioTrack, options);
|
10344 | pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
|
10345 | pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
|
10346 |
|
10347 | pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
|
10348 |
|
10349 | self.trigger('trackinfo', {
|
10350 | hasAudio: !!audioTrack,
|
10351 | hasVideo: !!videoTrack
|
10352 | });
|
10353 | });
|
10354 |
|
10355 | pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
|
10356 |
|
10357 | pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
|
10358 | addPipelineLogRetriggers(this, pipeline);
|
10359 | };
|
10360 |
|
10361 | this.setupTsPipeline = function () {
|
10362 | var pipeline = {};
|
10363 | this.transmuxPipeline_ = pipeline;
|
10364 | pipeline.type = 'ts';
|
10365 | pipeline.metadataStream = new m2ts_1.MetadataStream();
|
10366 |
|
10367 | pipeline.packetStream = new m2ts_1.TransportPacketStream();
|
10368 | pipeline.parseStream = new m2ts_1.TransportParseStream();
|
10369 | pipeline.elementaryStream = new m2ts_1.ElementaryStream();
|
10370 | pipeline.timestampRolloverStream = new m2ts_1.TimestampRolloverStream();
|
10371 | pipeline.adtsStream = new adts();
|
10372 | pipeline.h264Stream = new H264Stream();
|
10373 | pipeline.captionStream = new m2ts_1.CaptionStream(options);
|
10374 | pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
|
10375 | pipeline.headOfPipeline = pipeline.packetStream;
|
10376 |
|
10377 | pipeline.packetStream.pipe(pipeline.parseStream).pipe(pipeline.elementaryStream).pipe(pipeline.timestampRolloverStream);
|
10378 |
|
10379 |
|
10380 | pipeline.timestampRolloverStream.pipe(pipeline.h264Stream);
|
10381 | pipeline.timestampRolloverStream.pipe(pipeline.adtsStream);
|
10382 | pipeline.timestampRolloverStream.pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
|
10383 |
|
10384 | pipeline.h264Stream.pipe(pipeline.captionStream).pipe(pipeline.coalesceStream);
|
10385 | pipeline.elementaryStream.on('data', function (data) {
|
10386 | var i;
|
10387 |
|
10388 | if (data.type === 'metadata') {
|
10389 | i = data.tracks.length;
|
10390 |
|
10391 | while (i--) {
|
10392 | if (!videoTrack && data.tracks[i].type === 'video') {
|
10393 | videoTrack = data.tracks[i];
|
10394 | videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
|
10395 | } else if (!audioTrack && data.tracks[i].type === 'audio') {
|
10396 | audioTrack = data.tracks[i];
|
10397 | audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
|
10398 | }
|
10399 | }
|
10400 |
|
10401 |
|
10402 | if (videoTrack && !pipeline.videoSegmentStream) {
|
10403 | pipeline.coalesceStream.numberOfTracks++;
|
10404 | pipeline.videoSegmentStream = new _VideoSegmentStream(videoTrack, options);
|
10405 | pipeline.videoSegmentStream.on('log', self.getLogTrigger_('videoSegmentStream'));
|
10406 | pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
|
10407 |
|
10408 |
|
10409 |
|
10410 |
|
10411 | if (audioTrack && !options.keepOriginalTimestamps) {
|
10412 | audioTrack.timelineStartInfo = timelineStartInfo;
|
10413 |
|
10414 |
|
10415 |
|
10416 |
|
10417 | pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - self.baseMediaDecodeTime);
|
10418 | }
|
10419 | });
|
10420 | pipeline.videoSegmentStream.on('processedGopsInfo', self.trigger.bind(self, 'gopInfo'));
|
10421 | pipeline.videoSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'videoSegmentTimingInfo'));
|
10422 | pipeline.videoSegmentStream.on('baseMediaDecodeTime', function (baseMediaDecodeTime) {
|
10423 | if (audioTrack) {
|
10424 | pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
|
10425 | }
|
10426 | });
|
10427 | pipeline.videoSegmentStream.on('timingInfo', self.trigger.bind(self, 'videoTimingInfo'));
|
10428 |
|
10429 | pipeline.h264Stream.pipe(pipeline.videoSegmentStream).pipe(pipeline.coalesceStream);
|
10430 | }
|
10431 |
|
10432 | if (audioTrack && !pipeline.audioSegmentStream) {
|
10433 |
|
10434 | pipeline.coalesceStream.numberOfTracks++;
|
10435 | pipeline.audioSegmentStream = new _AudioSegmentStream(audioTrack, options);
|
10436 | pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
|
10437 | pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
|
10438 | pipeline.audioSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'audioSegmentTimingInfo'));
|
10439 |
|
10440 | pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
|
10441 | }
|
10442 |
|
10443 |
|
10444 | self.trigger('trackinfo', {
|
10445 | hasAudio: !!audioTrack,
|
10446 | hasVideo: !!videoTrack
|
10447 | });
|
10448 | }
|
10449 | });
|
10450 |
|
10451 | pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
|
10452 | pipeline.coalesceStream.on('id3Frame', function (id3Frame) {
|
10453 | id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
|
10454 | self.trigger('id3Frame', id3Frame);
|
10455 | });
|
10456 | pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
|
10457 |
|
10458 | pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
|
10459 | addPipelineLogRetriggers(this, pipeline);
|
10460 | };
|
10461 |
|
10462 |
|
10463 | this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
|
10464 | var pipeline = this.transmuxPipeline_;
|
10465 |
|
10466 | if (!options.keepOriginalTimestamps) {
|
10467 | this.baseMediaDecodeTime = baseMediaDecodeTime;
|
10468 | }
|
10469 |
|
10470 | if (audioTrack) {
|
10471 | audioTrack.timelineStartInfo.dts = undefined;
|
10472 | audioTrack.timelineStartInfo.pts = undefined;
|
10473 | trackDecodeInfo.clearDtsInfo(audioTrack);
|
10474 |
|
10475 | if (pipeline.audioTimestampRolloverStream) {
|
10476 | pipeline.audioTimestampRolloverStream.discontinuity();
|
10477 | }
|
10478 | }
|
10479 |
|
10480 | if (videoTrack) {
|
10481 | if (pipeline.videoSegmentStream) {
|
10482 | pipeline.videoSegmentStream.gopCache_ = [];
|
10483 | }
|
10484 |
|
10485 | videoTrack.timelineStartInfo.dts = undefined;
|
10486 | videoTrack.timelineStartInfo.pts = undefined;
|
10487 | trackDecodeInfo.clearDtsInfo(videoTrack);
|
10488 | pipeline.captionStream.reset();
|
10489 | }
|
10490 |
|
10491 | if (pipeline.timestampRolloverStream) {
|
10492 | pipeline.timestampRolloverStream.discontinuity();
|
10493 | }
|
10494 | };
|
10495 |
|
10496 | this.setAudioAppendStart = function (timestamp) {
|
10497 | if (audioTrack) {
|
10498 | this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
|
10499 | }
|
10500 | };
|
10501 |
|
10502 | this.setRemux = function (val) {
|
10503 | var pipeline = this.transmuxPipeline_;
|
10504 | options.remux = val;
|
10505 |
|
10506 | if (pipeline && pipeline.coalesceStream) {
|
10507 | pipeline.coalesceStream.setRemux(val);
|
10508 | }
|
10509 | };
|
10510 |
|
10511 | this.alignGopsWith = function (gopsToAlignWith) {
|
10512 | if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
|
10513 | this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
|
10514 | }
|
10515 | };
|
10516 |
|
10517 | this.getLogTrigger_ = function (key) {
|
10518 | var self = this;
|
10519 | return function (event) {
|
10520 | event.stream = key;
|
10521 | self.trigger('log', event);
|
10522 | };
|
10523 | };
|
10524 |
|
10525 |
|
10526 | this.push = function (data) {
|
10527 | if (hasFlushed) {
|
10528 | var isAac = isLikelyAacData(data);
|
10529 |
|
10530 | if (isAac && this.transmuxPipeline_.type !== 'aac') {
|
10531 | this.setupAacPipeline();
|
10532 | } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
|
10533 | this.setupTsPipeline();
|
10534 | }
|
10535 |
|
10536 | hasFlushed = false;
|
10537 | }
|
10538 |
|
10539 | this.transmuxPipeline_.headOfPipeline.push(data);
|
10540 | };
|
10541 |
|
10542 |
|
10543 | this.flush = function () {
|
10544 | hasFlushed = true;
|
10545 |
|
10546 | this.transmuxPipeline_.headOfPipeline.flush();
|
10547 | };
|
10548 |
|
10549 | this.endTimeline = function () {
|
10550 | this.transmuxPipeline_.headOfPipeline.endTimeline();
|
10551 | };
|
10552 |
|
10553 | this.reset = function () {
|
10554 | if (this.transmuxPipeline_.headOfPipeline) {
|
10555 | this.transmuxPipeline_.headOfPipeline.reset();
|
10556 | }
|
10557 | };
|
10558 |
|
10559 |
|
10560 | this.resetCaptions = function () {
|
10561 | if (this.transmuxPipeline_.captionStream) {
|
10562 | this.transmuxPipeline_.captionStream.reset();
|
10563 | }
|
10564 | };
|
10565 | };
|
10566 |
|
10567 | _Transmuxer.prototype = new stream();
|
10568 | var transmuxer = {
|
10569 | Transmuxer: _Transmuxer,
|
10570 | VideoSegmentStream: _VideoSegmentStream,
|
10571 | AudioSegmentStream: _AudioSegmentStream,
|
10572 | AUDIO_PROPERTIES: audioProperties,
|
10573 | VIDEO_PROPERTIES: videoProperties,
|
10574 |
|
10575 | generateSegmentTimingInfo: generateSegmentTimingInfo
|
10576 | };
|
10577 | |
10578 |
|
10579 |
|
10580 |
|
10581 |
|
10582 |
|
10583 |
|
10584 | var toUnsigned$3 = function toUnsigned(value) {
|
10585 | return value >>> 0;
|
10586 | };
|
10587 |
|
10588 | var toHexString$1 = function toHexString(value) {
|
10589 | return ('00' + value.toString(16)).slice(-2);
|
10590 | };
|
10591 |
|
10592 | var bin = {
|
10593 | toUnsigned: toUnsigned$3,
|
10594 | toHexString: toHexString$1
|
10595 | };
|
10596 |
|
10597 | var parseType$1 = function parseType(buffer) {
|
10598 | var result = '';
|
10599 | result += String.fromCharCode(buffer[0]);
|
10600 | result += String.fromCharCode(buffer[1]);
|
10601 | result += String.fromCharCode(buffer[2]);
|
10602 | result += String.fromCharCode(buffer[3]);
|
10603 | return result;
|
10604 | };
|
10605 |
|
10606 | var parseType_1 = parseType$1;
|
10607 | var toUnsigned$2 = bin.toUnsigned;
|
10608 |
|
10609 | var findBox = function findBox(data, path) {
|
10610 | var results = [],
|
10611 | i,
|
10612 | size,
|
10613 | type,
|
10614 | end,
|
10615 | subresults;
|
10616 |
|
10617 | if (!path.length) {
|
10618 |
|
10619 | return null;
|
10620 | }
|
10621 |
|
10622 | for (i = 0; i < data.byteLength;) {
|
10623 | size = toUnsigned$2(data[i] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3]);
|
10624 | type = parseType_1(data.subarray(i + 4, i + 8));
|
10625 | end = size > 1 ? i + size : data.byteLength;
|
10626 |
|
10627 | if (type === path[0]) {
|
10628 | if (path.length === 1) {
|
10629 |
|
10630 |
|
10631 | results.push(data.subarray(i + 8, end));
|
10632 | } else {
|
10633 |
|
10634 | subresults = findBox(data.subarray(i + 8, end), path.slice(1));
|
10635 |
|
10636 | if (subresults.length) {
|
10637 | results = results.concat(subresults);
|
10638 | }
|
10639 | }
|
10640 | }
|
10641 |
|
10642 | i = end;
|
10643 | }
|
10644 |
|
10645 |
|
10646 | return results;
|
10647 | };
|
10648 |
|
10649 | var findBox_1 = findBox;
|
10650 | var toUnsigned$1 = bin.toUnsigned;
|
10651 |
|
10652 | var tfdt = function tfdt(data) {
|
10653 | var result = {
|
10654 | version: data[0],
|
10655 | flags: new Uint8Array(data.subarray(1, 4)),
|
10656 | baseMediaDecodeTime: toUnsigned$1(data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7])
|
10657 | };
|
10658 |
|
10659 | if (result.version === 1) {
|
10660 | result.baseMediaDecodeTime *= Math.pow(2, 32);
|
10661 | result.baseMediaDecodeTime += toUnsigned$1(data[8] << 24 | data[9] << 16 | data[10] << 8 | data[11]);
|
10662 | }
|
10663 |
|
10664 | return result;
|
10665 | };
|
10666 |
|
10667 | var parseTfdt = tfdt;
|
10668 |
|
10669 | var parseSampleFlags = function parseSampleFlags(flags) {
|
10670 | return {
|
10671 | isLeading: (flags[0] & 0x0c) >>> 2,
|
10672 | dependsOn: flags[0] & 0x03,
|
10673 | isDependedOn: (flags[1] & 0xc0) >>> 6,
|
10674 | hasRedundancy: (flags[1] & 0x30) >>> 4,
|
10675 | paddingValue: (flags[1] & 0x0e) >>> 1,
|
10676 | isNonSyncSample: flags[1] & 0x01,
|
10677 | degradationPriority: flags[2] << 8 | flags[3]
|
10678 | };
|
10679 | };
|
10680 |
|
10681 | var parseSampleFlags_1 = parseSampleFlags;
|
10682 |
|
10683 | var trun = function trun(data) {
|
10684 | var result = {
|
10685 | version: data[0],
|
10686 | flags: new Uint8Array(data.subarray(1, 4)),
|
10687 | samples: []
|
10688 | },
|
10689 | view = new DataView(data.buffer, data.byteOffset, data.byteLength),
|
10690 |
|
10691 | dataOffsetPresent = result.flags[2] & 0x01,
|
10692 |
|
10693 | firstSampleFlagsPresent = result.flags[2] & 0x04,
|
10694 |
|
10695 | sampleDurationPresent = result.flags[1] & 0x01,
|
10696 |
|
10697 | sampleSizePresent = result.flags[1] & 0x02,
|
10698 |
|
10699 | sampleFlagsPresent = result.flags[1] & 0x04,
|
10700 |
|
10701 | sampleCompositionTimeOffsetPresent = result.flags[1] & 0x08,
|
10702 |
|
10703 | sampleCount = view.getUint32(4),
|
10704 | offset = 8,
|
10705 | sample;
|
10706 |
|
10707 | if (dataOffsetPresent) {
|
10708 |
|
10709 | result.dataOffset = view.getInt32(offset);
|
10710 | offset += 4;
|
10711 | }
|
10712 |
|
10713 |
|
10714 |
|
10715 | if (firstSampleFlagsPresent && sampleCount) {
|
10716 | sample = {
|
10717 | flags: parseSampleFlags_1(data.subarray(offset, offset + 4))
|
10718 | };
|
10719 | offset += 4;
|
10720 |
|
10721 | if (sampleDurationPresent) {
|
10722 | sample.duration = view.getUint32(offset);
|
10723 | offset += 4;
|
10724 | }
|
10725 |
|
10726 | if (sampleSizePresent) {
|
10727 | sample.size = view.getUint32(offset);
|
10728 | offset += 4;
|
10729 | }
|
10730 |
|
10731 | if (sampleCompositionTimeOffsetPresent) {
|
10732 | if (result.version === 1) {
|
10733 | sample.compositionTimeOffset = view.getInt32(offset);
|
10734 | } else {
|
10735 | sample.compositionTimeOffset = view.getUint32(offset);
|
10736 | }
|
10737 |
|
10738 | offset += 4;
|
10739 | }
|
10740 |
|
10741 | result.samples.push(sample);
|
10742 | sampleCount--;
|
10743 | }
|
10744 |
|
10745 | while (sampleCount--) {
|
10746 | sample = {};
|
10747 |
|
10748 | if (sampleDurationPresent) {
|
10749 | sample.duration = view.getUint32(offset);
|
10750 | offset += 4;
|
10751 | }
|
10752 |
|
10753 | if (sampleSizePresent) {
|
10754 | sample.size = view.getUint32(offset);
|
10755 | offset += 4;
|
10756 | }
|
10757 |
|
10758 | if (sampleFlagsPresent) {
|
10759 | sample.flags = parseSampleFlags_1(data.subarray(offset, offset + 4));
|
10760 | offset += 4;
|
10761 | }
|
10762 |
|
10763 | if (sampleCompositionTimeOffsetPresent) {
|
10764 | if (result.version === 1) {
|
10765 | sample.compositionTimeOffset = view.getInt32(offset);
|
10766 | } else {
|
10767 | sample.compositionTimeOffset = view.getUint32(offset);
|
10768 | }
|
10769 |
|
10770 | offset += 4;
|
10771 | }
|
10772 |
|
10773 | result.samples.push(sample);
|
10774 | }
|
10775 |
|
10776 | return result;
|
10777 | };
|
10778 |
|
10779 | var parseTrun = trun;
|
10780 |
|
10781 | var tfhd = function tfhd(data) {
|
10782 | var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
|
10783 | result = {
|
10784 | version: data[0],
|
10785 | flags: new Uint8Array(data.subarray(1, 4)),
|
10786 | trackId: view.getUint32(4)
|
10787 | },
|
10788 | baseDataOffsetPresent = result.flags[2] & 0x01,
|
10789 | sampleDescriptionIndexPresent = result.flags[2] & 0x02,
|
10790 | defaultSampleDurationPresent = result.flags[2] & 0x08,
|
10791 | defaultSampleSizePresent = result.flags[2] & 0x10,
|
10792 | defaultSampleFlagsPresent = result.flags[2] & 0x20,
|
10793 | durationIsEmpty = result.flags[0] & 0x010000,
|
10794 | defaultBaseIsMoof = result.flags[0] & 0x020000,
|
10795 | i;
|
10796 | i = 8;
|
10797 |
|
10798 | if (baseDataOffsetPresent) {
|
10799 | i += 4;
|
10800 |
|
10801 |
|
10802 | result.baseDataOffset = view.getUint32(12);
|
10803 | i += 4;
|
10804 | }
|
10805 |
|
10806 | if (sampleDescriptionIndexPresent) {
|
10807 | result.sampleDescriptionIndex = view.getUint32(i);
|
10808 | i += 4;
|
10809 | }
|
10810 |
|
10811 | if (defaultSampleDurationPresent) {
|
10812 | result.defaultSampleDuration = view.getUint32(i);
|
10813 | i += 4;
|
10814 | }
|
10815 |
|
10816 | if (defaultSampleSizePresent) {
|
10817 | result.defaultSampleSize = view.getUint32(i);
|
10818 | i += 4;
|
10819 | }
|
10820 |
|
10821 | if (defaultSampleFlagsPresent) {
|
10822 | result.defaultSampleFlags = view.getUint32(i);
|
10823 | }
|
10824 |
|
10825 | if (durationIsEmpty) {
|
10826 | result.durationIsEmpty = true;
|
10827 | }
|
10828 |
|
10829 | if (!baseDataOffsetPresent && defaultBaseIsMoof) {
|
10830 | result.baseDataOffsetIsMoof = true;
|
10831 | }
|
10832 |
|
10833 | return result;
|
10834 | };
|
10835 |
|
10836 | var parseTfhd = tfhd;
|
10837 | var discardEmulationPreventionBytes = captionPacketParser.discardEmulationPreventionBytes;
|
10838 | var CaptionStream = captionStream.CaptionStream;
|
10839 | |
10840 |
|
10841 |
|
10842 |
|
10843 |
|
10844 |
|
10845 |
|
10846 |
|
10847 |
|
10848 |
|
10849 |
|
10850 | var mapToSample = function mapToSample(offset, samples) {
|
10851 | var approximateOffset = offset;
|
10852 |
|
10853 | for (var i = 0; i < samples.length; i++) {
|
10854 | var sample = samples[i];
|
10855 |
|
10856 | if (approximateOffset < sample.size) {
|
10857 | return sample;
|
10858 | }
|
10859 |
|
10860 | approximateOffset -= sample.size;
|
10861 | }
|
10862 |
|
10863 | return null;
|
10864 | };
|
10865 | |
10866 |
|
10867 |
|
10868 |
|
10869 |
|
10870 |
|
10871 |
|
10872 |
|
10873 |
|
10874 |
|
10875 |
|
10876 |
|
10877 |
|
10878 |
|
10879 |
|
10880 |
|
10881 | var findSeiNals = function findSeiNals(avcStream, samples, trackId) {
|
10882 | var avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
|
10883 | result = {
|
10884 | logs: [],
|
10885 | seiNals: []
|
10886 | },
|
10887 | seiNal,
|
10888 | i,
|
10889 | length,
|
10890 | lastMatchedSample;
|
10891 |
|
10892 | for (i = 0; i + 4 < avcStream.length; i += length) {
|
10893 | length = avcView.getUint32(i);
|
10894 | i += 4;
|
10895 |
|
10896 | if (length <= 0) {
|
10897 | continue;
|
10898 | }
|
10899 |
|
10900 | switch (avcStream[i] & 0x1F) {
|
10901 | case 0x06:
|
10902 | var data = avcStream.subarray(i + 1, i + 1 + length);
|
10903 | var matchingSample = mapToSample(i, samples);
|
10904 | seiNal = {
|
10905 | nalUnitType: 'sei_rbsp',
|
10906 | size: length,
|
10907 | data: data,
|
10908 | escapedRBSP: discardEmulationPreventionBytes(data),
|
10909 | trackId: trackId
|
10910 | };
|
10911 |
|
10912 | if (matchingSample) {
|
10913 | seiNal.pts = matchingSample.pts;
|
10914 | seiNal.dts = matchingSample.dts;
|
10915 | lastMatchedSample = matchingSample;
|
10916 | } else if (lastMatchedSample) {
|
10917 |
|
10918 |
|
10919 | seiNal.pts = lastMatchedSample.pts;
|
10920 | seiNal.dts = lastMatchedSample.dts;
|
10921 | } else {
|
10922 | result.logs.push({
|
10923 | level: 'warn',
|
10924 | message: 'We\'ve encountered a nal unit without data at ' + i + ' for trackId ' + trackId + '. See mux.js#223.'
|
10925 | });
|
10926 | break;
|
10927 | }
|
10928 |
|
10929 | result.seiNals.push(seiNal);
|
10930 | break;
|
10931 | }
|
10932 | }
|
10933 |
|
10934 | return result;
|
10935 | };
|
10936 | |
10937 |
|
10938 |
|
10939 |
|
10940 |
|
10941 |
|
10942 |
|
10943 |
|
10944 |
|
10945 |
|
10946 |
|
10947 |
|
10948 |
|
10949 |
|
10950 |
|
10951 | var parseSamples = function parseSamples(truns, baseMediaDecodeTime, tfhd) {
|
10952 | var currentDts = baseMediaDecodeTime;
|
10953 | var defaultSampleDuration = tfhd.defaultSampleDuration || 0;
|
10954 | var defaultSampleSize = tfhd.defaultSampleSize || 0;
|
10955 | var trackId = tfhd.trackId;
|
10956 | var allSamples = [];
|
10957 | truns.forEach(function (trun) {
|
10958 |
|
10959 |
|
10960 |
|
10961 | var trackRun = parseTrun(trun);
|
10962 | var samples = trackRun.samples;
|
10963 | samples.forEach(function (sample) {
|
10964 | if (sample.duration === undefined) {
|
10965 | sample.duration = defaultSampleDuration;
|
10966 | }
|
10967 |
|
10968 | if (sample.size === undefined) {
|
10969 | sample.size = defaultSampleSize;
|
10970 | }
|
10971 |
|
10972 | sample.trackId = trackId;
|
10973 | sample.dts = currentDts;
|
10974 |
|
10975 | if (sample.compositionTimeOffset === undefined) {
|
10976 | sample.compositionTimeOffset = 0;
|
10977 | }
|
10978 |
|
10979 | sample.pts = currentDts + sample.compositionTimeOffset;
|
10980 | currentDts += sample.duration;
|
10981 | });
|
10982 | allSamples = allSamples.concat(samples);
|
10983 | });
|
10984 | return allSamples;
|
10985 | };
|
10986 | |
10987 |
|
10988 |
|
10989 |
|
10990 |
|
10991 |
|
10992 |
|
10993 |
|
10994 |
|
10995 |
|
10996 | var parseCaptionNals = function parseCaptionNals(segment, videoTrackId) {
|
10997 |
|
10998 | var trafs = findBox_1(segment, ['moof', 'traf']);
|
10999 |
|
11000 | var mdats = findBox_1(segment, ['mdat']);
|
11001 | var captionNals = {};
|
11002 | var mdatTrafPairs = [];
|
11003 |
|
11004 | mdats.forEach(function (mdat, index) {
|
11005 | var matchingTraf = trafs[index];
|
11006 | mdatTrafPairs.push({
|
11007 | mdat: mdat,
|
11008 | traf: matchingTraf
|
11009 | });
|
11010 | });
|
11011 | mdatTrafPairs.forEach(function (pair) {
|
11012 | var mdat = pair.mdat;
|
11013 | var traf = pair.traf;
|
11014 | var tfhd = findBox_1(traf, ['tfhd']);
|
11015 |
|
11016 | var headerInfo = parseTfhd(tfhd[0]);
|
11017 | var trackId = headerInfo.trackId;
|
11018 | var tfdt = findBox_1(traf, ['tfdt']);
|
11019 |
|
11020 | var baseMediaDecodeTime = tfdt.length > 0 ? parseTfdt(tfdt[0]).baseMediaDecodeTime : 0;
|
11021 | var truns = findBox_1(traf, ['trun']);
|
11022 | var samples;
|
11023 | var result;
|
11024 |
|
11025 | if (videoTrackId === trackId && truns.length > 0) {
|
11026 | samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);
|
11027 | result = findSeiNals(mdat, samples, trackId);
|
11028 |
|
11029 | if (!captionNals[trackId]) {
|
11030 | captionNals[trackId] = {
|
11031 | seiNals: [],
|
11032 | logs: []
|
11033 | };
|
11034 | }
|
11035 |
|
11036 | captionNals[trackId].seiNals = captionNals[trackId].seiNals.concat(result.seiNals);
|
11037 | captionNals[trackId].logs = captionNals[trackId].logs.concat(result.logs);
|
11038 | }
|
11039 | });
|
11040 | return captionNals;
|
11041 | };
|
11042 | |
11043 |
|
11044 |
|
11045 |
|
11046 |
|
11047 |
|
11048 |
|
11049 |
|
11050 |
|
11051 |
|
11052 |
|
11053 |
|
11054 |
|
11055 |
|
11056 |
|
11057 |
|
11058 |
|
11059 |
|
11060 | var parseEmbeddedCaptions = function parseEmbeddedCaptions(segment, trackId, timescale) {
|
11061 | var captionNals;
|
11062 |
|
11063 | if (trackId === null) {
|
11064 | return null;
|
11065 | }
|
11066 |
|
11067 | captionNals = parseCaptionNals(segment, trackId);
|
11068 | var trackNals = captionNals[trackId] || {};
|
11069 | return {
|
11070 | seiNals: trackNals.seiNals,
|
11071 | logs: trackNals.logs,
|
11072 | timescale: timescale
|
11073 | };
|
11074 | };
|
11075 | |
11076 |
|
11077 |
|
11078 |
|
11079 |
|
11080 | var CaptionParser = function CaptionParser() {
|
11081 | var isInitialized = false;
|
11082 | var captionStream;
|
11083 |
|
11084 | var segmentCache;
|
11085 |
|
11086 | var trackId;
|
11087 |
|
11088 | var timescale;
|
11089 |
|
11090 | var parsedCaptions;
|
11091 |
|
11092 | var parsingPartial;
|
11093 | |
11094 |
|
11095 |
|
11096 |
|
11097 |
|
11098 | this.isInitialized = function () {
|
11099 | return isInitialized;
|
11100 | };
|
11101 | |
11102 |
|
11103 |
|
11104 |
|
11105 |
|
11106 |
|
11107 | this.init = function (options) {
|
11108 | captionStream = new CaptionStream();
|
11109 | isInitialized = true;
|
11110 | parsingPartial = options ? options.isPartial : false;
|
11111 |
|
11112 | captionStream.on('data', function (event) {
|
11113 |
|
11114 | event.startTime = event.startPts / timescale;
|
11115 | event.endTime = event.endPts / timescale;
|
11116 | parsedCaptions.captions.push(event);
|
11117 | parsedCaptions.captionStreams[event.stream] = true;
|
11118 | });
|
11119 | captionStream.on('log', function (log) {
|
11120 | parsedCaptions.logs.push(log);
|
11121 | });
|
11122 | };
|
11123 | |
11124 |
|
11125 |
|
11126 |
|
11127 |
|
11128 |
|
11129 |
|
11130 | this.isNewInit = function (videoTrackIds, timescales) {
|
11131 | if (videoTrackIds && videoTrackIds.length === 0 || timescales && typeof timescales === 'object' && Object.keys(timescales).length === 0) {
|
11132 | return false;
|
11133 | }
|
11134 |
|
11135 | return trackId !== videoTrackIds[0] || timescale !== timescales[trackId];
|
11136 | };
|
11137 | |
11138 |
|
11139 |
|
11140 |
|
11141 |
|
11142 |
|
11143 |
|
11144 |
|
11145 |
|
11146 |
|
11147 |
|
11148 |
|
11149 | this.parse = function (segment, videoTrackIds, timescales) {
|
11150 | var parsedData;
|
11151 |
|
11152 | if (!this.isInitialized()) {
|
11153 | return null;
|
11154 | } else if (!videoTrackIds || !timescales) {
|
11155 | return null;
|
11156 | } else if (this.isNewInit(videoTrackIds, timescales)) {
|
11157 |
|
11158 |
|
11159 | trackId = videoTrackIds[0];
|
11160 | timescale = timescales[trackId];
|
11161 |
|
11162 |
|
11163 | } else if (trackId === null || !timescale) {
|
11164 | segmentCache.push(segment);
|
11165 | return null;
|
11166 | }
|
11167 |
|
11168 |
|
11169 | while (segmentCache.length > 0) {
|
11170 | var cachedSegment = segmentCache.shift();
|
11171 | this.parse(cachedSegment, videoTrackIds, timescales);
|
11172 | }
|
11173 |
|
11174 | parsedData = parseEmbeddedCaptions(segment, trackId, timescale);
|
11175 |
|
11176 | if (parsedData && parsedData.logs) {
|
11177 | parsedCaptions.logs = parsedCaptions.logs.concat(parsedData.logs);
|
11178 | }
|
11179 |
|
11180 | if (parsedData === null || !parsedData.seiNals) {
|
11181 | if (parsedCaptions.logs.length) {
|
11182 | return {
|
11183 | logs: parsedCaptions.logs,
|
11184 | captions: [],
|
11185 | captionStreams: []
|
11186 | };
|
11187 | }
|
11188 |
|
11189 | return null;
|
11190 | }
|
11191 |
|
11192 | this.pushNals(parsedData.seiNals);
|
11193 |
|
11194 | this.flushStream();
|
11195 | return parsedCaptions;
|
11196 | };
|
11197 | |
11198 |
|
11199 |
|
11200 |
|
11201 |
|
11202 |
|
11203 |
|
11204 |
|
11205 | this.pushNals = function (nals) {
|
11206 | if (!this.isInitialized() || !nals || nals.length === 0) {
|
11207 | return null;
|
11208 | }
|
11209 |
|
11210 | nals.forEach(function (nal) {
|
11211 | captionStream.push(nal);
|
11212 | });
|
11213 | };
|
11214 | |
11215 |
|
11216 |
|
11217 |
|
11218 |
|
11219 |
|
11220 | this.flushStream = function () {
|
11221 | if (!this.isInitialized()) {
|
11222 | return null;
|
11223 | }
|
11224 |
|
11225 | if (!parsingPartial) {
|
11226 | captionStream.flush();
|
11227 | } else {
|
11228 | captionStream.partialFlush();
|
11229 | }
|
11230 | };
|
11231 | |
11232 |
|
11233 |
|
11234 |
|
11235 |
|
11236 | this.clearParsedCaptions = function () {
|
11237 | parsedCaptions.captions = [];
|
11238 | parsedCaptions.captionStreams = {};
|
11239 | parsedCaptions.logs = [];
|
11240 | };
|
11241 | |
11242 |
|
11243 |
|
11244 |
|
11245 |
|
11246 |
|
11247 | this.resetCaptionStream = function () {
|
11248 | if (!this.isInitialized()) {
|
11249 | return null;
|
11250 | }
|
11251 |
|
11252 | captionStream.reset();
|
11253 | };
|
11254 | |
11255 |
|
11256 |
|
11257 |
|
11258 |
|
11259 |
|
11260 |
|
11261 | this.clearAllCaptions = function () {
|
11262 | this.clearParsedCaptions();
|
11263 | this.resetCaptionStream();
|
11264 | };
|
11265 | |
11266 |
|
11267 |
|
11268 |
|
11269 |
|
11270 | this.reset = function () {
|
11271 | segmentCache = [];
|
11272 | trackId = null;
|
11273 | timescale = null;
|
11274 |
|
11275 | if (!parsedCaptions) {
|
11276 | parsedCaptions = {
|
11277 | captions: [],
|
11278 |
|
11279 | captionStreams: {},
|
11280 | logs: []
|
11281 | };
|
11282 | } else {
|
11283 | this.clearParsedCaptions();
|
11284 | }
|
11285 |
|
11286 | this.resetCaptionStream();
|
11287 | };
|
11288 |
|
11289 | this.reset();
|
11290 | };
|
11291 |
|
11292 | var captionParser = CaptionParser;
|
11293 | var toUnsigned = bin.toUnsigned;
|
11294 | var toHexString = bin.toHexString;
|
11295 | var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader;
|
11296 | |
11297 |
|
11298 |
|
11299 |
|
11300 |
|
11301 |
|
11302 |
|
11303 |
|
11304 |
|
11305 |
|
11306 |
|
11307 |
|
11308 |
|
11309 |
|
11310 |
|
11311 |
|
11312 |
|
11313 |
|
11314 |
|
11315 | timescale = function timescale(init) {
|
11316 | var result = {},
|
11317 | traks = findBox_1(init, ['moov', 'trak']);
|
11318 |
|
11319 | return traks.reduce(function (result, trak) {
|
11320 | var tkhd, version, index, id, mdhd;
|
11321 | tkhd = findBox_1(trak, ['tkhd'])[0];
|
11322 |
|
11323 | if (!tkhd) {
|
11324 | return null;
|
11325 | }
|
11326 |
|
11327 | version = tkhd[0];
|
11328 | index = version === 0 ? 12 : 20;
|
11329 | id = toUnsigned(tkhd[index] << 24 | tkhd[index + 1] << 16 | tkhd[index + 2] << 8 | tkhd[index + 3]);
|
11330 | mdhd = findBox_1(trak, ['mdia', 'mdhd'])[0];
|
11331 |
|
11332 | if (!mdhd) {
|
11333 | return null;
|
11334 | }
|
11335 |
|
11336 | version = mdhd[0];
|
11337 | index = version === 0 ? 12 : 20;
|
11338 | result[id] = toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
|
11339 | return result;
|
11340 | }, result);
|
11341 | };
|
11342 | |
11343 |
|
11344 |
|
11345 |
|
11346 |
|
11347 |
|
11348 |
|
11349 |
|
11350 |
|
11351 |
|
11352 |
|
11353 |
|
11354 |
|
11355 |
|
11356 |
|
11357 |
|
11358 |
|
11359 |
|
11360 | startTime = function startTime(timescale, fragment) {
|
11361 | var trafs, baseTimes, result;
|
11362 |
|
11363 | trafs = findBox_1(fragment, ['moof', 'traf']);
|
11364 |
|
11365 | baseTimes = [].concat.apply([], trafs.map(function (traf) {
|
11366 | return findBox_1(traf, ['tfhd']).map(function (tfhd) {
|
11367 | var id, scale, baseTime;
|
11368 |
|
11369 | id = toUnsigned(tfhd[4] << 24 | tfhd[5] << 16 | tfhd[6] << 8 | tfhd[7]);
|
11370 |
|
11371 | scale = timescale[id] || 90e3;
|
11372 |
|
11373 | baseTime = findBox_1(traf, ['tfdt']).map(function (tfdt) {
|
11374 | var version, result;
|
11375 | version = tfdt[0];
|
11376 | result = toUnsigned(tfdt[4] << 24 | tfdt[5] << 16 | tfdt[6] << 8 | tfdt[7]);
|
11377 |
|
11378 | if (version === 1) {
|
11379 | result *= Math.pow(2, 32);
|
11380 | result += toUnsigned(tfdt[8] << 24 | tfdt[9] << 16 | tfdt[10] << 8 | tfdt[11]);
|
11381 | }
|
11382 |
|
11383 | return result;
|
11384 | })[0];
|
11385 | baseTime = typeof baseTime === 'number' && !isNaN(baseTime) ? baseTime : Infinity;
|
11386 |
|
11387 | return baseTime / scale;
|
11388 | });
|
11389 | }));
|
11390 |
|
11391 | result = Math.min.apply(null, baseTimes);
|
11392 | return isFinite(result) ? result : 0;
|
11393 | };
|
11394 | |
11395 |
|
11396 |
|
11397 |
|
11398 |
|
11399 |
|
11400 |
|
11401 |
|
11402 |
|
11403 |
|
11404 |
|
11405 |
|
11406 |
|
11407 |
|
11408 |
|
11409 |
|
11410 |
|
11411 |
|
11412 |
|
11413 |
|
11414 |
|
11415 | compositionStartTime = function compositionStartTime(timescales, fragment) {
|
11416 | var trafBoxes = findBox_1(fragment, ['moof', 'traf']);
|
11417 | var baseMediaDecodeTime = 0;
|
11418 | var compositionTimeOffset = 0;
|
11419 | var trackId;
|
11420 |
|
11421 | if (trafBoxes && trafBoxes.length) {
|
11422 |
|
11423 |
|
11424 |
|
11425 | var tfhd = findBox_1(trafBoxes[0], ['tfhd'])[0];
|
11426 | var trun = findBox_1(trafBoxes[0], ['trun'])[0];
|
11427 | var tfdt = findBox_1(trafBoxes[0], ['tfdt'])[0];
|
11428 |
|
11429 | if (tfhd) {
|
11430 | var parsedTfhd = parseTfhd(tfhd);
|
11431 | trackId = parsedTfhd.trackId;
|
11432 | }
|
11433 |
|
11434 | if (tfdt) {
|
11435 | var parsedTfdt = parseTfdt(tfdt);
|
11436 | baseMediaDecodeTime = parsedTfdt.baseMediaDecodeTime;
|
11437 | }
|
11438 |
|
11439 | if (trun) {
|
11440 | var parsedTrun = parseTrun(trun);
|
11441 |
|
11442 | if (parsedTrun.samples && parsedTrun.samples.length) {
|
11443 | compositionTimeOffset = parsedTrun.samples[0].compositionTimeOffset || 0;
|
11444 | }
|
11445 | }
|
11446 | }
|
11447 |
|
11448 |
|
11449 |
|
11450 | var timescale = timescales[trackId] || 90e3;
|
11451 |
|
11452 | return (baseMediaDecodeTime + compositionTimeOffset) / timescale;
|
11453 | };
|
11454 | |
11455 |
|
11456 |
|
11457 |
|
11458 |
|
11459 |
|
11460 |
|
11461 |
|
11462 |
|
11463 |
|
11464 |
|
11465 |
|
11466 |
|
11467 | getVideoTrackIds = function getVideoTrackIds(init) {
|
11468 | var traks = findBox_1(init, ['moov', 'trak']);
|
11469 | var videoTrackIds = [];
|
11470 | traks.forEach(function (trak) {
|
11471 | var hdlrs = findBox_1(trak, ['mdia', 'hdlr']);
|
11472 | var tkhds = findBox_1(trak, ['tkhd']);
|
11473 | hdlrs.forEach(function (hdlr, index) {
|
11474 | var handlerType = parseType_1(hdlr.subarray(8, 12));
|
11475 | var tkhd = tkhds[index];
|
11476 | var view;
|
11477 | var version;
|
11478 | var trackId;
|
11479 |
|
11480 | if (handlerType === 'vide') {
|
11481 | view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
|
11482 | version = view.getUint8(0);
|
11483 | trackId = version === 0 ? view.getUint32(12) : view.getUint32(20);
|
11484 | videoTrackIds.push(trackId);
|
11485 | }
|
11486 | });
|
11487 | });
|
11488 | return videoTrackIds;
|
11489 | };
|
11490 |
|
11491 | getTimescaleFromMediaHeader = function getTimescaleFromMediaHeader(mdhd) {
|
11492 |
|
11493 | var version = mdhd[0];
|
11494 | var index = version === 0 ? 12 : 20;
|
11495 | return toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
|
11496 | };
|
11497 | |
11498 |
|
11499 |
|
11500 |
|
11501 |
|
11502 |
|
11503 | getTracks = function getTracks(init) {
|
11504 | var traks = findBox_1(init, ['moov', 'trak']);
|
11505 | var tracks = [];
|
11506 | traks.forEach(function (trak) {
|
11507 | var track = {};
|
11508 | var tkhd = findBox_1(trak, ['tkhd'])[0];
|
11509 | var view, tkhdVersion;
|
11510 |
|
11511 | if (tkhd) {
|
11512 | view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
|
11513 | tkhdVersion = view.getUint8(0);
|
11514 | track.id = tkhdVersion === 0 ? view.getUint32(12) : view.getUint32(20);
|
11515 | }
|
11516 |
|
11517 | var hdlr = findBox_1(trak, ['mdia', 'hdlr'])[0];
|
11518 |
|
11519 | if (hdlr) {
|
11520 | var type = parseType_1(hdlr.subarray(8, 12));
|
11521 |
|
11522 | if (type === 'vide') {
|
11523 | track.type = 'video';
|
11524 | } else if (type === 'soun') {
|
11525 | track.type = 'audio';
|
11526 | } else {
|
11527 | track.type = type;
|
11528 | }
|
11529 | }
|
11530 |
|
11531 |
|
11532 | var stsd = findBox_1(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
|
11533 |
|
11534 | if (stsd) {
|
11535 | var sampleDescriptions = stsd.subarray(8);
|
11536 |
|
11537 | track.codec = parseType_1(sampleDescriptions.subarray(4, 8));
|
11538 | var codecBox = findBox_1(sampleDescriptions, [track.codec])[0];
|
11539 | var codecConfig, codecConfigType;
|
11540 |
|
11541 | if (codecBox) {
|
11542 |
|
11543 | if (/^[asm]vc[1-9]$/i.test(track.codec)) {
|
11544 |
|
11545 |
|
11546 | codecConfig = codecBox.subarray(78);
|
11547 | codecConfigType = parseType_1(codecConfig.subarray(4, 8));
|
11548 |
|
11549 | if (codecConfigType === 'avcC' && codecConfig.length > 11) {
|
11550 | track.codec += '.';
|
11551 |
|
11552 |
|
11553 | track.codec += toHexString(codecConfig[9]);
|
11554 |
|
11555 | track.codec += toHexString(codecConfig[10]);
|
11556 |
|
11557 | track.codec += toHexString(codecConfig[11]);
|
11558 | } else {
|
11559 |
|
11560 |
|
11561 | track.codec = 'avc1.4d400d';
|
11562 | }
|
11563 | } else if (/^mp4[a,v]$/i.test(track.codec)) {
|
11564 |
|
11565 | codecConfig = codecBox.subarray(28);
|
11566 | codecConfigType = parseType_1(codecConfig.subarray(4, 8));
|
11567 |
|
11568 | if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
|
11569 | track.codec += '.' + toHexString(codecConfig[19]);
|
11570 |
|
11571 | track.codec += '.' + toHexString(codecConfig[20] >>> 2 & 0x3f).replace(/^0/, '');
|
11572 | } else {
|
11573 |
|
11574 |
|
11575 | track.codec = 'mp4a.40.2';
|
11576 | }
|
11577 | } else {
|
11578 |
|
11579 | track.codec = track.codec.toLowerCase();
|
11580 | }
|
11581 | }
|
11582 | }
|
11583 |
|
11584 | var mdhd = findBox_1(trak, ['mdia', 'mdhd'])[0];
|
11585 |
|
11586 | if (mdhd) {
|
11587 | track.timescale = getTimescaleFromMediaHeader(mdhd);
|
11588 | }
|
11589 |
|
11590 | tracks.push(track);
|
11591 | });
|
11592 | return tracks;
|
11593 | };
|
11594 |
|
11595 | var probe$2 = {
|
11596 |
|
11597 | findBox: findBox_1,
|
11598 | parseType: parseType_1,
|
11599 | timescale: timescale,
|
11600 | startTime: startTime,
|
11601 | compositionStartTime: compositionStartTime,
|
11602 | videoTrackIds: getVideoTrackIds,
|
11603 | tracks: getTracks,
|
11604 | getTimescaleFromMediaHeader: getTimescaleFromMediaHeader
|
11605 | };
|
11606 |
|
11607 | var parsePid = function parsePid(packet) {
|
11608 | var pid = packet[1] & 0x1f;
|
11609 | pid <<= 8;
|
11610 | pid |= packet[2];
|
11611 | return pid;
|
11612 | };
|
11613 |
|
11614 | var parsePayloadUnitStartIndicator = function parsePayloadUnitStartIndicator(packet) {
|
11615 | return !!(packet[1] & 0x40);
|
11616 | };
|
11617 |
|
11618 | var parseAdaptionField = function parseAdaptionField(packet) {
|
11619 | var offset = 0;
|
11620 |
|
11621 |
|
11622 |
|
11623 |
|
11624 |
|
11625 | if ((packet[3] & 0x30) >>> 4 > 0x01) {
|
11626 | offset += packet[4] + 1;
|
11627 | }
|
11628 |
|
11629 | return offset;
|
11630 | };
|
11631 |
|
11632 | var parseType = function parseType(packet, pmtPid) {
|
11633 | var pid = parsePid(packet);
|
11634 |
|
11635 | if (pid === 0) {
|
11636 | return 'pat';
|
11637 | } else if (pid === pmtPid) {
|
11638 | return 'pmt';
|
11639 | } else if (pmtPid) {
|
11640 | return 'pes';
|
11641 | }
|
11642 |
|
11643 | return null;
|
11644 | };
|
11645 |
|
11646 | var parsePat = function parsePat(packet) {
|
11647 | var pusi = parsePayloadUnitStartIndicator(packet);
|
11648 | var offset = 4 + parseAdaptionField(packet);
|
11649 |
|
11650 | if (pusi) {
|
11651 | offset += packet[offset] + 1;
|
11652 | }
|
11653 |
|
11654 | return (packet[offset + 10] & 0x1f) << 8 | packet[offset + 11];
|
11655 | };
|
11656 |
|
11657 | var parsePmt = function parsePmt(packet) {
|
11658 | var programMapTable = {};
|
11659 | var pusi = parsePayloadUnitStartIndicator(packet);
|
11660 | var payloadOffset = 4 + parseAdaptionField(packet);
|
11661 |
|
11662 | if (pusi) {
|
11663 | payloadOffset += packet[payloadOffset] + 1;
|
11664 | }
|
11665 |
|
11666 |
|
11667 |
|
11668 |
|
11669 |
|
11670 |
|
11671 | if (!(packet[payloadOffset + 5] & 0x01)) {
|
11672 | return;
|
11673 | }
|
11674 |
|
11675 | var sectionLength, tableEnd, programInfoLength;
|
11676 |
|
11677 | sectionLength = (packet[payloadOffset + 1] & 0x0f) << 8 | packet[payloadOffset + 2];
|
11678 | tableEnd = 3 + sectionLength - 4;
|
11679 |
|
11680 |
|
11681 | programInfoLength = (packet[payloadOffset + 10] & 0x0f) << 8 | packet[payloadOffset + 11];
|
11682 |
|
11683 | var offset = 12 + programInfoLength;
|
11684 |
|
11685 | while (offset < tableEnd) {
|
11686 | var i = payloadOffset + offset;
|
11687 |
|
11688 | programMapTable[(packet[i + 1] & 0x1F) << 8 | packet[i + 2]] = packet[i];
|
11689 |
|
11690 |
|
11691 | offset += ((packet[i + 3] & 0x0F) << 8 | packet[i + 4]) + 5;
|
11692 | }
|
11693 |
|
11694 | return programMapTable;
|
11695 | };
|
11696 |
|
11697 | var parsePesType = function parsePesType(packet, programMapTable) {
|
11698 | var pid = parsePid(packet);
|
11699 | var type = programMapTable[pid];
|
11700 |
|
11701 | switch (type) {
|
11702 | case streamTypes.H264_STREAM_TYPE:
|
11703 | return 'video';
|
11704 |
|
11705 | case streamTypes.ADTS_STREAM_TYPE:
|
11706 | return 'audio';
|
11707 |
|
11708 | case streamTypes.METADATA_STREAM_TYPE:
|
11709 | return 'timed-metadata';
|
11710 |
|
11711 | default:
|
11712 | return null;
|
11713 | }
|
11714 | };
|
11715 |
|
11716 | var parsePesTime = function parsePesTime(packet) {
|
11717 | var pusi = parsePayloadUnitStartIndicator(packet);
|
11718 |
|
11719 | if (!pusi) {
|
11720 | return null;
|
11721 | }
|
11722 |
|
11723 | var offset = 4 + parseAdaptionField(packet);
|
11724 |
|
11725 | if (offset >= packet.byteLength) {
|
11726 |
|
11727 |
|
11728 |
|
11729 |
|
11730 |
|
11731 |
|
11732 |
|
11733 |
|
11734 |
|
11735 |
|
11736 | return null;
|
11737 | }
|
11738 |
|
11739 | var pes = null;
|
11740 | var ptsDtsFlags;
|
11741 |
|
11742 |
|
11743 |
|
11744 | ptsDtsFlags = packet[offset + 7];
|
11745 |
|
11746 |
|
11747 |
|
11748 |
|
11749 |
|
11750 |
|
11751 |
|
11752 |
|
11753 | if (ptsDtsFlags & 0xC0) {
|
11754 | pes = {};
|
11755 |
|
11756 |
|
11757 |
|
11758 | pes.pts = (packet[offset + 9] & 0x0E) << 27 | (packet[offset + 10] & 0xFF) << 20 | (packet[offset + 11] & 0xFE) << 12 | (packet[offset + 12] & 0xFF) << 5 | (packet[offset + 13] & 0xFE) >>> 3;
|
11759 | pes.pts *= 4;
|
11760 |
|
11761 | pes.pts += (packet[offset + 13] & 0x06) >>> 1;
|
11762 |
|
11763 | pes.dts = pes.pts;
|
11764 |
|
11765 | if (ptsDtsFlags & 0x40) {
|
11766 | pes.dts = (packet[offset + 14] & 0x0E) << 27 | (packet[offset + 15] & 0xFF) << 20 | (packet[offset + 16] & 0xFE) << 12 | (packet[offset + 17] & 0xFF) << 5 | (packet[offset + 18] & 0xFE) >>> 3;
|
11767 | pes.dts *= 4;
|
11768 |
|
11769 | pes.dts += (packet[offset + 18] & 0x06) >>> 1;
|
11770 | }
|
11771 | }
|
11772 |
|
11773 | return pes;
|
11774 | };
|
11775 |
|
11776 | var parseNalUnitType = function parseNalUnitType(type) {
|
11777 | switch (type) {
|
11778 | case 0x05:
|
11779 | return 'slice_layer_without_partitioning_rbsp_idr';
|
11780 |
|
11781 | case 0x06:
|
11782 | return 'sei_rbsp';
|
11783 |
|
11784 | case 0x07:
|
11785 | return 'seq_parameter_set_rbsp';
|
11786 |
|
11787 | case 0x08:
|
11788 | return 'pic_parameter_set_rbsp';
|
11789 |
|
11790 | case 0x09:
|
11791 | return 'access_unit_delimiter_rbsp';
|
11792 |
|
11793 | default:
|
11794 | return null;
|
11795 | }
|
11796 | };
|
11797 |
|
11798 | var videoPacketContainsKeyFrame = function videoPacketContainsKeyFrame(packet) {
|
11799 | var offset = 4 + parseAdaptionField(packet);
|
11800 | var frameBuffer = packet.subarray(offset);
|
11801 | var frameI = 0;
|
11802 | var frameSyncPoint = 0;
|
11803 | var foundKeyFrame = false;
|
11804 | var nalType;
|
11805 |
|
11806 | for (; frameSyncPoint < frameBuffer.byteLength - 3; frameSyncPoint++) {
|
11807 | if (frameBuffer[frameSyncPoint + 2] === 1) {
|
11808 |
|
11809 | frameI = frameSyncPoint + 5;
|
11810 | break;
|
11811 | }
|
11812 | }
|
11813 |
|
11814 | while (frameI < frameBuffer.byteLength) {
|
11815 |
|
11816 |
|
11817 | switch (frameBuffer[frameI]) {
|
11818 | case 0:
|
11819 |
|
11820 | if (frameBuffer[frameI - 1] !== 0) {
|
11821 | frameI += 2;
|
11822 | break;
|
11823 | } else if (frameBuffer[frameI - 2] !== 0) {
|
11824 | frameI++;
|
11825 | break;
|
11826 | }
|
11827 |
|
11828 | if (frameSyncPoint + 3 !== frameI - 2) {
|
11829 | nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
|
11830 |
|
11831 | if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
|
11832 | foundKeyFrame = true;
|
11833 | }
|
11834 | }
|
11835 |
|
11836 |
|
11837 | do {
|
11838 | frameI++;
|
11839 | } while (frameBuffer[frameI] !== 1 && frameI < frameBuffer.length);
|
11840 |
|
11841 | frameSyncPoint = frameI - 2;
|
11842 | frameI += 3;
|
11843 | break;
|
11844 |
|
11845 | case 1:
|
11846 |
|
11847 | if (frameBuffer[frameI - 1] !== 0 || frameBuffer[frameI - 2] !== 0) {
|
11848 | frameI += 3;
|
11849 | break;
|
11850 | }
|
11851 |
|
11852 | nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
|
11853 |
|
11854 | if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
|
11855 | foundKeyFrame = true;
|
11856 | }
|
11857 |
|
11858 | frameSyncPoint = frameI - 2;
|
11859 | frameI += 3;
|
11860 | break;
|
11861 |
|
11862 | default:
|
11863 |
|
11864 |
|
11865 | frameI += 3;
|
11866 | break;
|
11867 | }
|
11868 | }
|
11869 |
|
11870 | frameBuffer = frameBuffer.subarray(frameSyncPoint);
|
11871 | frameI -= frameSyncPoint;
|
11872 | frameSyncPoint = 0;
|
11873 |
|
11874 | if (frameBuffer && frameBuffer.byteLength > 3) {
|
11875 | nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
|
11876 |
|
11877 | if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
|
11878 | foundKeyFrame = true;
|
11879 | }
|
11880 | }
|
11881 |
|
11882 | return foundKeyFrame;
|
11883 | };
|
11884 |
|
11885 | var probe$1 = {
|
11886 | parseType: parseType,
|
11887 | parsePat: parsePat,
|
11888 | parsePmt: parsePmt,
|
11889 | parsePayloadUnitStartIndicator: parsePayloadUnitStartIndicator,
|
11890 | parsePesType: parsePesType,
|
11891 | parsePesTime: parsePesTime,
|
11892 | videoPacketContainsKeyFrame: videoPacketContainsKeyFrame
|
11893 | };
|
11894 | var handleRollover = timestampRolloverStream.handleRollover;
|
11895 | var probe = {};
|
11896 | probe.ts = probe$1;
|
11897 | probe.aac = utils;
|
11898 | var ONE_SECOND_IN_TS = clock.ONE_SECOND_IN_TS;
|
11899 | var MP2T_PACKET_LENGTH = 188,
|
11900 |
|
11901 | SYNC_BYTE = 0x47;
|
11902 | |
11903 |
|
11904 |
|
11905 |
|
11906 |
|
11907 | var parsePsi_ = function parsePsi_(bytes, pmt) {
|
11908 | var startIndex = 0,
|
11909 | endIndex = MP2T_PACKET_LENGTH,
|
11910 | packet,
|
11911 | type;
|
11912 |
|
11913 | while (endIndex < bytes.byteLength) {
|
11914 |
|
11915 | if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
|
11916 |
|
11917 | packet = bytes.subarray(startIndex, endIndex);
|
11918 | type = probe.ts.parseType(packet, pmt.pid);
|
11919 |
|
11920 | switch (type) {
|
11921 | case 'pat':
|
11922 | pmt.pid = probe.ts.parsePat(packet);
|
11923 | break;
|
11924 |
|
11925 | case 'pmt':
|
11926 | var table = probe.ts.parsePmt(packet);
|
11927 | pmt.table = pmt.table || {};
|
11928 | Object.keys(table).forEach(function (key) {
|
11929 | pmt.table[key] = table[key];
|
11930 | });
|
11931 | break;
|
11932 | }
|
11933 |
|
11934 | startIndex += MP2T_PACKET_LENGTH;
|
11935 | endIndex += MP2T_PACKET_LENGTH;
|
11936 | continue;
|
11937 | }
|
11938 |
|
11939 |
|
11940 |
|
11941 |
|
11942 | startIndex++;
|
11943 | endIndex++;
|
11944 | }
|
11945 | };
|
11946 | |
11947 |
|
11948 |
|
11949 |
|
11950 |
|
11951 |
|
11952 | var parseAudioPes_ = function parseAudioPes_(bytes, pmt, result) {
|
11953 | var startIndex = 0,
|
11954 | endIndex = MP2T_PACKET_LENGTH,
|
11955 | packet,
|
11956 | type,
|
11957 | pesType,
|
11958 | pusi,
|
11959 | parsed;
|
11960 | var endLoop = false;
|
11961 |
|
11962 | while (endIndex <= bytes.byteLength) {
|
11963 |
|
11964 | if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
|
11965 |
|
11966 | packet = bytes.subarray(startIndex, endIndex);
|
11967 | type = probe.ts.parseType(packet, pmt.pid);
|
11968 |
|
11969 | switch (type) {
|
11970 | case 'pes':
|
11971 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
11972 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
11973 |
|
11974 | if (pesType === 'audio' && pusi) {
|
11975 | parsed = probe.ts.parsePesTime(packet);
|
11976 |
|
11977 | if (parsed) {
|
11978 | parsed.type = 'audio';
|
11979 | result.audio.push(parsed);
|
11980 | endLoop = true;
|
11981 | }
|
11982 | }
|
11983 |
|
11984 | break;
|
11985 | }
|
11986 |
|
11987 | if (endLoop) {
|
11988 | break;
|
11989 | }
|
11990 |
|
11991 | startIndex += MP2T_PACKET_LENGTH;
|
11992 | endIndex += MP2T_PACKET_LENGTH;
|
11993 | continue;
|
11994 | }
|
11995 |
|
11996 |
|
11997 |
|
11998 |
|
11999 | startIndex++;
|
12000 | endIndex++;
|
12001 | }
|
12002 |
|
12003 |
|
12004 | endIndex = bytes.byteLength;
|
12005 | startIndex = endIndex - MP2T_PACKET_LENGTH;
|
12006 | endLoop = false;
|
12007 |
|
12008 | while (startIndex >= 0) {
|
12009 |
|
12010 | if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
|
12011 |
|
12012 | packet = bytes.subarray(startIndex, endIndex);
|
12013 | type = probe.ts.parseType(packet, pmt.pid);
|
12014 |
|
12015 | switch (type) {
|
12016 | case 'pes':
|
12017 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
12018 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
12019 |
|
12020 | if (pesType === 'audio' && pusi) {
|
12021 | parsed = probe.ts.parsePesTime(packet);
|
12022 |
|
12023 | if (parsed) {
|
12024 | parsed.type = 'audio';
|
12025 | result.audio.push(parsed);
|
12026 | endLoop = true;
|
12027 | }
|
12028 | }
|
12029 |
|
12030 | break;
|
12031 | }
|
12032 |
|
12033 | if (endLoop) {
|
12034 | break;
|
12035 | }
|
12036 |
|
12037 | startIndex -= MP2T_PACKET_LENGTH;
|
12038 | endIndex -= MP2T_PACKET_LENGTH;
|
12039 | continue;
|
12040 | }
|
12041 |
|
12042 |
|
12043 |
|
12044 |
|
12045 | startIndex--;
|
12046 | endIndex--;
|
12047 | }
|
12048 | };
|
12049 | |
12050 |
|
12051 |
|
12052 |
|
12053 |
|
12054 |
|
12055 |
|
12056 | var parseVideoPes_ = function parseVideoPes_(bytes, pmt, result) {
|
12057 | var startIndex = 0,
|
12058 | endIndex = MP2T_PACKET_LENGTH,
|
12059 | packet,
|
12060 | type,
|
12061 | pesType,
|
12062 | pusi,
|
12063 | parsed,
|
12064 | frame,
|
12065 | i,
|
12066 | pes;
|
12067 | var endLoop = false;
|
12068 | var currentFrame = {
|
12069 | data: [],
|
12070 | size: 0
|
12071 | };
|
12072 |
|
12073 | while (endIndex < bytes.byteLength) {
|
12074 |
|
12075 | if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
|
12076 |
|
12077 | packet = bytes.subarray(startIndex, endIndex);
|
12078 | type = probe.ts.parseType(packet, pmt.pid);
|
12079 |
|
12080 | switch (type) {
|
12081 | case 'pes':
|
12082 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
12083 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
12084 |
|
12085 | if (pesType === 'video') {
|
12086 | if (pusi && !endLoop) {
|
12087 | parsed = probe.ts.parsePesTime(packet);
|
12088 |
|
12089 | if (parsed) {
|
12090 | parsed.type = 'video';
|
12091 | result.video.push(parsed);
|
12092 | endLoop = true;
|
12093 | }
|
12094 | }
|
12095 |
|
12096 | if (!result.firstKeyFrame) {
|
12097 | if (pusi) {
|
12098 | if (currentFrame.size !== 0) {
|
12099 | frame = new Uint8Array(currentFrame.size);
|
12100 | i = 0;
|
12101 |
|
12102 | while (currentFrame.data.length) {
|
12103 | pes = currentFrame.data.shift();
|
12104 | frame.set(pes, i);
|
12105 | i += pes.byteLength;
|
12106 | }
|
12107 |
|
12108 | if (probe.ts.videoPacketContainsKeyFrame(frame)) {
|
12109 | var firstKeyFrame = probe.ts.parsePesTime(frame);
|
12110 |
|
12111 |
|
12112 |
|
12113 | if (firstKeyFrame) {
|
12114 | result.firstKeyFrame = firstKeyFrame;
|
12115 | result.firstKeyFrame.type = 'video';
|
12116 | } else {
|
12117 |
|
12118 | console.warn('Failed to extract PTS/DTS from PES at first keyframe. ' + 'This could be an unusual TS segment, or else mux.js did not ' + 'parse your TS segment correctly. If you know your TS ' + 'segments do contain PTS/DTS on keyframes please file a bug ' + 'report! You can try ffprobe to double check for yourself.');
|
12119 | }
|
12120 | }
|
12121 |
|
12122 | currentFrame.size = 0;
|
12123 | }
|
12124 | }
|
12125 |
|
12126 | currentFrame.data.push(packet);
|
12127 | currentFrame.size += packet.byteLength;
|
12128 | }
|
12129 | }
|
12130 |
|
12131 | break;
|
12132 | }
|
12133 |
|
12134 | if (endLoop && result.firstKeyFrame) {
|
12135 | break;
|
12136 | }
|
12137 |
|
12138 | startIndex += MP2T_PACKET_LENGTH;
|
12139 | endIndex += MP2T_PACKET_LENGTH;
|
12140 | continue;
|
12141 | }
|
12142 |
|
12143 |
|
12144 |
|
12145 |
|
12146 | startIndex++;
|
12147 | endIndex++;
|
12148 | }
|
12149 |
|
12150 |
|
12151 | endIndex = bytes.byteLength;
|
12152 | startIndex = endIndex - MP2T_PACKET_LENGTH;
|
12153 | endLoop = false;
|
12154 |
|
12155 | while (startIndex >= 0) {
|
12156 |
|
12157 | if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
|
12158 |
|
12159 | packet = bytes.subarray(startIndex, endIndex);
|
12160 | type = probe.ts.parseType(packet, pmt.pid);
|
12161 |
|
12162 | switch (type) {
|
12163 | case 'pes':
|
12164 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
12165 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
12166 |
|
12167 | if (pesType === 'video' && pusi) {
|
12168 | parsed = probe.ts.parsePesTime(packet);
|
12169 |
|
12170 | if (parsed) {
|
12171 | parsed.type = 'video';
|
12172 | result.video.push(parsed);
|
12173 | endLoop = true;
|
12174 | }
|
12175 | }
|
12176 |
|
12177 | break;
|
12178 | }
|
12179 |
|
12180 | if (endLoop) {
|
12181 | break;
|
12182 | }
|
12183 |
|
12184 | startIndex -= MP2T_PACKET_LENGTH;
|
12185 | endIndex -= MP2T_PACKET_LENGTH;
|
12186 | continue;
|
12187 | }
|
12188 |
|
12189 |
|
12190 |
|
12191 |
|
12192 | startIndex--;
|
12193 | endIndex--;
|
12194 | }
|
12195 | };
|
12196 | |
12197 |
|
12198 |
|
12199 |
|
12200 |
|
12201 |
|
12202 | var adjustTimestamp_ = function adjustTimestamp_(segmentInfo, baseTimestamp) {
|
12203 | if (segmentInfo.audio && segmentInfo.audio.length) {
|
12204 | var audioBaseTimestamp = baseTimestamp;
|
12205 |
|
12206 | if (typeof audioBaseTimestamp === 'undefined' || isNaN(audioBaseTimestamp)) {
|
12207 | audioBaseTimestamp = segmentInfo.audio[0].dts;
|
12208 | }
|
12209 |
|
12210 | segmentInfo.audio.forEach(function (info) {
|
12211 | info.dts = handleRollover(info.dts, audioBaseTimestamp);
|
12212 | info.pts = handleRollover(info.pts, audioBaseTimestamp);
|
12213 |
|
12214 | info.dtsTime = info.dts / ONE_SECOND_IN_TS;
|
12215 | info.ptsTime = info.pts / ONE_SECOND_IN_TS;
|
12216 | });
|
12217 | }
|
12218 |
|
12219 | if (segmentInfo.video && segmentInfo.video.length) {
|
12220 | var videoBaseTimestamp = baseTimestamp;
|
12221 |
|
12222 | if (typeof videoBaseTimestamp === 'undefined' || isNaN(videoBaseTimestamp)) {
|
12223 | videoBaseTimestamp = segmentInfo.video[0].dts;
|
12224 | }
|
12225 |
|
12226 | segmentInfo.video.forEach(function (info) {
|
12227 | info.dts = handleRollover(info.dts, videoBaseTimestamp);
|
12228 | info.pts = handleRollover(info.pts, videoBaseTimestamp);
|
12229 |
|
12230 | info.dtsTime = info.dts / ONE_SECOND_IN_TS;
|
12231 | info.ptsTime = info.pts / ONE_SECOND_IN_TS;
|
12232 | });
|
12233 |
|
12234 | if (segmentInfo.firstKeyFrame) {
|
12235 | var frame = segmentInfo.firstKeyFrame;
|
12236 | frame.dts = handleRollover(frame.dts, videoBaseTimestamp);
|
12237 | frame.pts = handleRollover(frame.pts, videoBaseTimestamp);
|
12238 |
|
12239 | frame.dtsTime = frame.dts / ONE_SECOND_IN_TS;
|
12240 | frame.ptsTime = frame.pts / ONE_SECOND_IN_TS;
|
12241 | }
|
12242 | }
|
12243 | };
|
12244 | |
12245 |
|
12246 |
|
12247 |
|
12248 |
|
12249 | var inspectAac_ = function inspectAac_(bytes) {
|
12250 | var endLoop = false,
|
12251 | audioCount = 0,
|
12252 | sampleRate = null,
|
12253 | timestamp = null,
|
12254 | frameSize = 0,
|
12255 | byteIndex = 0,
|
12256 | packet;
|
12257 |
|
12258 | while (bytes.length - byteIndex >= 3) {
|
12259 | var type = probe.aac.parseType(bytes, byteIndex);
|
12260 |
|
12261 | switch (type) {
|
12262 | case 'timed-metadata':
|
12263 |
|
12264 |
|
12265 | if (bytes.length - byteIndex < 10) {
|
12266 | endLoop = true;
|
12267 | break;
|
12268 | }
|
12269 |
|
12270 | frameSize = probe.aac.parseId3TagSize(bytes, byteIndex);
|
12271 |
|
12272 |
|
12273 | if (frameSize > bytes.length) {
|
12274 | endLoop = true;
|
12275 | break;
|
12276 | }
|
12277 |
|
12278 | if (timestamp === null) {
|
12279 | packet = bytes.subarray(byteIndex, byteIndex + frameSize);
|
12280 | timestamp = probe.aac.parseAacTimestamp(packet);
|
12281 | }
|
12282 |
|
12283 | byteIndex += frameSize;
|
12284 | break;
|
12285 |
|
12286 | case 'audio':
|
12287 |
|
12288 |
|
12289 | if (bytes.length - byteIndex < 7) {
|
12290 | endLoop = true;
|
12291 | break;
|
12292 | }
|
12293 |
|
12294 | frameSize = probe.aac.parseAdtsSize(bytes, byteIndex);
|
12295 |
|
12296 |
|
12297 | if (frameSize > bytes.length) {
|
12298 | endLoop = true;
|
12299 | break;
|
12300 | }
|
12301 |
|
12302 | if (sampleRate === null) {
|
12303 | packet = bytes.subarray(byteIndex, byteIndex + frameSize);
|
12304 | sampleRate = probe.aac.parseSampleRate(packet);
|
12305 | }
|
12306 |
|
12307 | audioCount++;
|
12308 | byteIndex += frameSize;
|
12309 | break;
|
12310 |
|
12311 | default:
|
12312 | byteIndex++;
|
12313 | break;
|
12314 | }
|
12315 |
|
12316 | if (endLoop) {
|
12317 | return null;
|
12318 | }
|
12319 | }
|
12320 |
|
12321 | if (sampleRate === null || timestamp === null) {
|
12322 | return null;
|
12323 | }
|
12324 |
|
12325 | var audioTimescale = ONE_SECOND_IN_TS / sampleRate;
|
12326 | var result = {
|
12327 | audio: [{
|
12328 | type: 'audio',
|
12329 | dts: timestamp,
|
12330 | pts: timestamp
|
12331 | }, {
|
12332 | type: 'audio',
|
12333 | dts: timestamp + audioCount * 1024 * audioTimescale,
|
12334 | pts: timestamp + audioCount * 1024 * audioTimescale
|
12335 | }]
|
12336 | };
|
12337 | return result;
|
12338 | };
|
12339 | |
12340 |
|
12341 |
|
12342 |
|
12343 |
|
12344 |
|
12345 |
|
12346 | var inspectTs_ = function inspectTs_(bytes) {
|
12347 | var pmt = {
|
12348 | pid: null,
|
12349 | table: null
|
12350 | };
|
12351 | var result = {};
|
12352 | parsePsi_(bytes, pmt);
|
12353 |
|
12354 | for (var pid in pmt.table) {
|
12355 | if (pmt.table.hasOwnProperty(pid)) {
|
12356 | var type = pmt.table[pid];
|
12357 |
|
12358 | switch (type) {
|
12359 | case streamTypes.H264_STREAM_TYPE:
|
12360 | result.video = [];
|
12361 | parseVideoPes_(bytes, pmt, result);
|
12362 |
|
12363 | if (result.video.length === 0) {
|
12364 | delete result.video;
|
12365 | }
|
12366 |
|
12367 | break;
|
12368 |
|
12369 | case streamTypes.ADTS_STREAM_TYPE:
|
12370 | result.audio = [];
|
12371 | parseAudioPes_(bytes, pmt, result);
|
12372 |
|
12373 | if (result.audio.length === 0) {
|
12374 | delete result.audio;
|
12375 | }
|
12376 |
|
12377 | break;
|
12378 | }
|
12379 | }
|
12380 | }
|
12381 |
|
12382 | return result;
|
12383 | };
|
12384 | |
12385 |
|
12386 |
|
12387 |
|
12388 |
|
12389 |
|
12390 |
|
12391 |
|
12392 |
|
12393 |
|
12394 | var inspect = function inspect(bytes, baseTimestamp) {
|
12395 | var isAacData = probe.aac.isLikelyAacData(bytes);
|
12396 | var result;
|
12397 |
|
12398 | if (isAacData) {
|
12399 | result = inspectAac_(bytes);
|
12400 | } else {
|
12401 | result = inspectTs_(bytes);
|
12402 | }
|
12403 |
|
12404 | if (!result || !result.audio && !result.video) {
|
12405 | return null;
|
12406 | }
|
12407 |
|
12408 | adjustTimestamp_(result, baseTimestamp);
|
12409 | return result;
|
12410 | };
|
12411 |
|
12412 | var tsInspector = {
|
12413 | inspect: inspect,
|
12414 | parseAudioPes_: parseAudioPes_
|
12415 | };
|
12416 |
|
12417 |
|
12418 | |
12419 |
|
12420 |
|
12421 |
|
12422 |
|
12423 |
|
12424 |
|
12425 |
|
12426 | var wireTransmuxerEvents = function wireTransmuxerEvents(self, transmuxer) {
|
12427 | transmuxer.on('data', function (segment) {
|
12428 |
|
12429 |
|
12430 |
|
12431 |
|
12432 | var initArray = segment.initSegment;
|
12433 | segment.initSegment = {
|
12434 | data: initArray.buffer,
|
12435 | byteOffset: initArray.byteOffset,
|
12436 | byteLength: initArray.byteLength
|
12437 | };
|
12438 | var typedArray = segment.data;
|
12439 | segment.data = typedArray.buffer;
|
12440 | self.postMessage({
|
12441 | action: 'data',
|
12442 | segment: segment,
|
12443 | byteOffset: typedArray.byteOffset,
|
12444 | byteLength: typedArray.byteLength
|
12445 | }, [segment.data]);
|
12446 | });
|
12447 | transmuxer.on('done', function (data) {
|
12448 | self.postMessage({
|
12449 | action: 'done'
|
12450 | });
|
12451 | });
|
12452 | transmuxer.on('gopInfo', function (gopInfo) {
|
12453 | self.postMessage({
|
12454 | action: 'gopInfo',
|
12455 | gopInfo: gopInfo
|
12456 | });
|
12457 | });
|
12458 | transmuxer.on('videoSegmentTimingInfo', function (timingInfo) {
|
12459 | var videoSegmentTimingInfo = {
|
12460 | start: {
|
12461 | decode: clock.videoTsToSeconds(timingInfo.start.dts),
|
12462 | presentation: clock.videoTsToSeconds(timingInfo.start.pts)
|
12463 | },
|
12464 | end: {
|
12465 | decode: clock.videoTsToSeconds(timingInfo.end.dts),
|
12466 | presentation: clock.videoTsToSeconds(timingInfo.end.pts)
|
12467 | },
|
12468 | baseMediaDecodeTime: clock.videoTsToSeconds(timingInfo.baseMediaDecodeTime)
|
12469 | };
|
12470 |
|
12471 | if (timingInfo.prependedContentDuration) {
|
12472 | videoSegmentTimingInfo.prependedContentDuration = clock.videoTsToSeconds(timingInfo.prependedContentDuration);
|
12473 | }
|
12474 |
|
12475 | self.postMessage({
|
12476 | action: 'videoSegmentTimingInfo',
|
12477 | videoSegmentTimingInfo: videoSegmentTimingInfo
|
12478 | });
|
12479 | });
|
12480 | transmuxer.on('audioSegmentTimingInfo', function (timingInfo) {
|
12481 |
|
12482 | var audioSegmentTimingInfo = {
|
12483 | start: {
|
12484 | decode: clock.videoTsToSeconds(timingInfo.start.dts),
|
12485 | presentation: clock.videoTsToSeconds(timingInfo.start.pts)
|
12486 | },
|
12487 | end: {
|
12488 | decode: clock.videoTsToSeconds(timingInfo.end.dts),
|
12489 | presentation: clock.videoTsToSeconds(timingInfo.end.pts)
|
12490 | },
|
12491 | baseMediaDecodeTime: clock.videoTsToSeconds(timingInfo.baseMediaDecodeTime)
|
12492 | };
|
12493 |
|
12494 | if (timingInfo.prependedContentDuration) {
|
12495 | audioSegmentTimingInfo.prependedContentDuration = clock.videoTsToSeconds(timingInfo.prependedContentDuration);
|
12496 | }
|
12497 |
|
12498 | self.postMessage({
|
12499 | action: 'audioSegmentTimingInfo',
|
12500 | audioSegmentTimingInfo: audioSegmentTimingInfo
|
12501 | });
|
12502 | });
|
12503 | transmuxer.on('id3Frame', function (id3Frame) {
|
12504 | self.postMessage({
|
12505 | action: 'id3Frame',
|
12506 | id3Frame: id3Frame
|
12507 | });
|
12508 | });
|
12509 | transmuxer.on('caption', function (caption) {
|
12510 | self.postMessage({
|
12511 | action: 'caption',
|
12512 | caption: caption
|
12513 | });
|
12514 | });
|
12515 | transmuxer.on('trackinfo', function (trackInfo) {
|
12516 | self.postMessage({
|
12517 | action: 'trackinfo',
|
12518 | trackInfo: trackInfo
|
12519 | });
|
12520 | });
|
12521 | transmuxer.on('audioTimingInfo', function (audioTimingInfo) {
|
12522 |
|
12523 | self.postMessage({
|
12524 | action: 'audioTimingInfo',
|
12525 | audioTimingInfo: {
|
12526 | start: clock.videoTsToSeconds(audioTimingInfo.start),
|
12527 | end: clock.videoTsToSeconds(audioTimingInfo.end)
|
12528 | }
|
12529 | });
|
12530 | });
|
12531 | transmuxer.on('videoTimingInfo', function (videoTimingInfo) {
|
12532 | self.postMessage({
|
12533 | action: 'videoTimingInfo',
|
12534 | videoTimingInfo: {
|
12535 | start: clock.videoTsToSeconds(videoTimingInfo.start),
|
12536 | end: clock.videoTsToSeconds(videoTimingInfo.end)
|
12537 | }
|
12538 | });
|
12539 | });
|
12540 | transmuxer.on('log', function (log) {
|
12541 | self.postMessage({
|
12542 | action: 'log',
|
12543 | log: log
|
12544 | });
|
12545 | });
|
12546 | };
|
12547 | |
12548 |
|
12549 |
|
12550 |
|
12551 |
|
12552 |
|
12553 |
|
12554 |
|
12555 |
|
12556 | var MessageHandlers = function () {
|
12557 | function MessageHandlers(self, options) {
|
12558 | this.options = options || {};
|
12559 | this.self = self;
|
12560 | this.init();
|
12561 | }
|
12562 | |
12563 |
|
12564 |
|
12565 |
|
12566 |
|
12567 | var _proto = MessageHandlers.prototype;
|
12568 |
|
12569 | _proto.init = function init() {
|
12570 | if (this.transmuxer) {
|
12571 | this.transmuxer.dispose();
|
12572 | }
|
12573 |
|
12574 | this.transmuxer = new transmuxer.Transmuxer(this.options);
|
12575 | wireTransmuxerEvents(this.self, this.transmuxer);
|
12576 | };
|
12577 |
|
12578 | _proto.pushMp4Captions = function pushMp4Captions(data) {
|
12579 | if (!this.captionParser) {
|
12580 | this.captionParser = new captionParser();
|
12581 | this.captionParser.init();
|
12582 | }
|
12583 |
|
12584 | var segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
|
12585 | var parsed = this.captionParser.parse(segment, data.trackIds, data.timescales);
|
12586 | this.self.postMessage({
|
12587 | action: 'mp4Captions',
|
12588 | captions: parsed && parsed.captions || [],
|
12589 | logs: parsed && parsed.logs || [],
|
12590 | data: segment.buffer
|
12591 | }, [segment.buffer]);
|
12592 | };
|
12593 |
|
12594 | _proto.probeMp4StartTime = function probeMp4StartTime(_ref) {
|
12595 | var timescales = _ref.timescales,
|
12596 | data = _ref.data;
|
12597 | var startTime = probe$2.startTime(timescales, data);
|
12598 | this.self.postMessage({
|
12599 | action: 'probeMp4StartTime',
|
12600 | startTime: startTime,
|
12601 | data: data
|
12602 | }, [data.buffer]);
|
12603 | };
|
12604 |
|
12605 | _proto.probeMp4Tracks = function probeMp4Tracks(_ref2) {
|
12606 | var data = _ref2.data;
|
12607 | var tracks = probe$2.tracks(data);
|
12608 | this.self.postMessage({
|
12609 | action: 'probeMp4Tracks',
|
12610 | tracks: tracks,
|
12611 | data: data
|
12612 | }, [data.buffer]);
|
12613 | }
|
12614 | |
12615 |
|
12616 |
|
12617 |
|
12618 |
|
12619 |
|
12620 |
|
12621 |
|
12622 |
|
12623 |
|
12624 |
|
12625 |
|
12626 |
|
12627 | ;
|
12628 |
|
12629 | _proto.probeTs = function probeTs(_ref3) {
|
12630 | var data = _ref3.data,
|
12631 | baseStartTime = _ref3.baseStartTime;
|
12632 | var tsStartTime = typeof baseStartTime === 'number' && !isNaN(baseStartTime) ? baseStartTime * clock.ONE_SECOND_IN_TS : void 0;
|
12633 | var timeInfo = tsInspector.inspect(data, tsStartTime);
|
12634 | var result = null;
|
12635 |
|
12636 | if (timeInfo) {
|
12637 | result = {
|
12638 |
|
12639 | hasVideo: timeInfo.video && timeInfo.video.length === 2 || false,
|
12640 | hasAudio: timeInfo.audio && timeInfo.audio.length === 2 || false
|
12641 | };
|
12642 |
|
12643 | if (result.hasVideo) {
|
12644 | result.videoStart = timeInfo.video[0].ptsTime;
|
12645 | }
|
12646 |
|
12647 | if (result.hasAudio) {
|
12648 | result.audioStart = timeInfo.audio[0].ptsTime;
|
12649 | }
|
12650 | }
|
12651 |
|
12652 | this.self.postMessage({
|
12653 | action: 'probeTs',
|
12654 | result: result,
|
12655 | data: data
|
12656 | }, [data.buffer]);
|
12657 | };
|
12658 |
|
12659 | _proto.clearAllMp4Captions = function clearAllMp4Captions() {
|
12660 | if (this.captionParser) {
|
12661 | this.captionParser.clearAllCaptions();
|
12662 | }
|
12663 | };
|
12664 |
|
12665 | _proto.clearParsedMp4Captions = function clearParsedMp4Captions() {
|
12666 | if (this.captionParser) {
|
12667 | this.captionParser.clearParsedCaptions();
|
12668 | }
|
12669 | }
|
12670 | |
12671 |
|
12672 |
|
12673 |
|
12674 |
|
12675 |
|
12676 | ;
|
12677 |
|
12678 | _proto.push = function push(data) {
|
12679 |
|
12680 | var segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
|
12681 | this.transmuxer.push(segment);
|
12682 | }
|
12683 | |
12684 |
|
12685 |
|
12686 |
|
12687 | ;
|
12688 |
|
12689 | _proto.reset = function reset() {
|
12690 | this.transmuxer.reset();
|
12691 | }
|
12692 | |
12693 |
|
12694 |
|
12695 |
|
12696 |
|
12697 |
|
12698 |
|
12699 | ;
|
12700 |
|
12701 | _proto.setTimestampOffset = function setTimestampOffset(data) {
|
12702 | var timestampOffset = data.timestampOffset || 0;
|
12703 | this.transmuxer.setBaseMediaDecodeTime(Math.round(clock.secondsToVideoTs(timestampOffset)));
|
12704 | };
|
12705 |
|
12706 | _proto.setAudioAppendStart = function setAudioAppendStart(data) {
|
12707 | this.transmuxer.setAudioAppendStart(Math.ceil(clock.secondsToVideoTs(data.appendStart)));
|
12708 | };
|
12709 |
|
12710 | _proto.setRemux = function setRemux(data) {
|
12711 | this.transmuxer.setRemux(data.remux);
|
12712 | }
|
12713 | |
12714 |
|
12715 |
|
12716 |
|
12717 |
|
12718 |
|
12719 | ;
|
12720 |
|
12721 | _proto.flush = function flush(data) {
|
12722 | this.transmuxer.flush();
|
12723 |
|
12724 | self.postMessage({
|
12725 | action: 'done',
|
12726 | type: 'transmuxed'
|
12727 | });
|
12728 | };
|
12729 |
|
12730 | _proto.endTimeline = function endTimeline() {
|
12731 | this.transmuxer.endTimeline();
|
12732 |
|
12733 |
|
12734 | self.postMessage({
|
12735 | action: 'endedtimeline',
|
12736 | type: 'transmuxed'
|
12737 | });
|
12738 | };
|
12739 |
|
12740 | _proto.alignGopsWith = function alignGopsWith(data) {
|
12741 | this.transmuxer.alignGopsWith(data.gopsToAlignWith.slice());
|
12742 | };
|
12743 |
|
12744 | return MessageHandlers;
|
12745 | }();
|
12746 | |
12747 |
|
12748 |
|
12749 |
|
12750 |
|
12751 |
|
12752 |
|
12753 |
|
12754 |
|
12755 | self.onmessage = function (event) {
|
12756 | if (event.data.action === 'init' && event.data.options) {
|
12757 | this.messageHandlers = new MessageHandlers(self, event.data.options);
|
12758 | return;
|
12759 | }
|
12760 |
|
12761 | if (!this.messageHandlers) {
|
12762 | this.messageHandlers = new MessageHandlers(self);
|
12763 | }
|
12764 |
|
12765 | if (event.data && event.data.action && event.data.action !== 'init') {
|
12766 | if (this.messageHandlers[event.data.action]) {
|
12767 | this.messageHandlers[event.data.action](event.data);
|
12768 | }
|
12769 | }
|
12770 | };
|
12771 | }));
|
12772 | var TransmuxWorker = factory(workerCode$1);
|
12773 |
|
12774 |
|
12775 | var handleData_ = function handleData_(event, transmuxedData, callback) {
|
12776 | var _event$data$segment = event.data.segment,
|
12777 | type = _event$data$segment.type,
|
12778 | initSegment = _event$data$segment.initSegment,
|
12779 | captions = _event$data$segment.captions,
|
12780 | captionStreams = _event$data$segment.captionStreams,
|
12781 | metadata = _event$data$segment.metadata,
|
12782 | videoFrameDtsTime = _event$data$segment.videoFrameDtsTime,
|
12783 | videoFramePtsTime = _event$data$segment.videoFramePtsTime;
|
12784 | transmuxedData.buffer.push({
|
12785 | captions: captions,
|
12786 | captionStreams: captionStreams,
|
12787 | metadata: metadata
|
12788 | });
|
12789 | var boxes = event.data.segment.boxes || {
|
12790 | data: event.data.segment.data
|
12791 | };
|
12792 | var result = {
|
12793 | type: type,
|
12794 |
|
12795 | data: new Uint8Array(boxes.data, boxes.data.byteOffset, boxes.data.byteLength),
|
12796 | initSegment: new Uint8Array(initSegment.data, initSegment.byteOffset, initSegment.byteLength)
|
12797 | };
|
12798 |
|
12799 | if (typeof videoFrameDtsTime !== 'undefined') {
|
12800 | result.videoFrameDtsTime = videoFrameDtsTime;
|
12801 | }
|
12802 |
|
12803 | if (typeof videoFramePtsTime !== 'undefined') {
|
12804 | result.videoFramePtsTime = videoFramePtsTime;
|
12805 | }
|
12806 |
|
12807 | callback(result);
|
12808 | };
|
12809 | var handleDone_ = function handleDone_(_ref) {
|
12810 | var transmuxedData = _ref.transmuxedData,
|
12811 | callback = _ref.callback;
|
12812 |
|
12813 |
|
12814 | transmuxedData.buffer = [];
|
12815 |
|
12816 |
|
12817 | callback(transmuxedData);
|
12818 | };
|
12819 | var handleGopInfo_ = function handleGopInfo_(event, transmuxedData) {
|
12820 | transmuxedData.gopInfo = event.data.gopInfo;
|
12821 | };
|
12822 | var processTransmux = function processTransmux(options) {
|
12823 | var transmuxer = options.transmuxer,
|
12824 | bytes = options.bytes,
|
12825 | audioAppendStart = options.audioAppendStart,
|
12826 | gopsToAlignWith = options.gopsToAlignWith,
|
12827 | remux = options.remux,
|
12828 | onData = options.onData,
|
12829 | onTrackInfo = options.onTrackInfo,
|
12830 | onAudioTimingInfo = options.onAudioTimingInfo,
|
12831 | onVideoTimingInfo = options.onVideoTimingInfo,
|
12832 | onVideoSegmentTimingInfo = options.onVideoSegmentTimingInfo,
|
12833 | onAudioSegmentTimingInfo = options.onAudioSegmentTimingInfo,
|
12834 | onId3 = options.onId3,
|
12835 | onCaptions = options.onCaptions,
|
12836 | onDone = options.onDone,
|
12837 | onEndedTimeline = options.onEndedTimeline,
|
12838 | onTransmuxerLog = options.onTransmuxerLog,
|
12839 | isEndOfTimeline = options.isEndOfTimeline;
|
12840 | var transmuxedData = {
|
12841 | buffer: []
|
12842 | };
|
12843 | var waitForEndedTimelineEvent = isEndOfTimeline;
|
12844 |
|
12845 | var handleMessage = function handleMessage(event) {
|
12846 | if (transmuxer.currentTransmux !== options) {
|
12847 |
|
12848 | return;
|
12849 | }
|
12850 |
|
12851 | if (event.data.action === 'data') {
|
12852 | handleData_(event, transmuxedData, onData);
|
12853 | }
|
12854 |
|
12855 | if (event.data.action === 'trackinfo') {
|
12856 | onTrackInfo(event.data.trackInfo);
|
12857 | }
|
12858 |
|
12859 | if (event.data.action === 'gopInfo') {
|
12860 | handleGopInfo_(event, transmuxedData);
|
12861 | }
|
12862 |
|
12863 | if (event.data.action === 'audioTimingInfo') {
|
12864 | onAudioTimingInfo(event.data.audioTimingInfo);
|
12865 | }
|
12866 |
|
12867 | if (event.data.action === 'videoTimingInfo') {
|
12868 | onVideoTimingInfo(event.data.videoTimingInfo);
|
12869 | }
|
12870 |
|
12871 | if (event.data.action === 'videoSegmentTimingInfo') {
|
12872 | onVideoSegmentTimingInfo(event.data.videoSegmentTimingInfo);
|
12873 | }
|
12874 |
|
12875 | if (event.data.action === 'audioSegmentTimingInfo') {
|
12876 | onAudioSegmentTimingInfo(event.data.audioSegmentTimingInfo);
|
12877 | }
|
12878 |
|
12879 | if (event.data.action === 'id3Frame') {
|
12880 | onId3([event.data.id3Frame], event.data.id3Frame.dispatchType);
|
12881 | }
|
12882 |
|
12883 | if (event.data.action === 'caption') {
|
12884 | onCaptions(event.data.caption);
|
12885 | }
|
12886 |
|
12887 | if (event.data.action === 'endedtimeline') {
|
12888 | waitForEndedTimelineEvent = false;
|
12889 | onEndedTimeline();
|
12890 | }
|
12891 |
|
12892 | if (event.data.action === 'log') {
|
12893 | onTransmuxerLog(event.data.log);
|
12894 | }
|
12895 |
|
12896 |
|
12897 | if (event.data.type !== 'transmuxed') {
|
12898 | return;
|
12899 | }
|
12900 |
|
12901 |
|
12902 |
|
12903 |
|
12904 |
|
12905 | if (waitForEndedTimelineEvent) {
|
12906 | return;
|
12907 | }
|
12908 |
|
12909 | transmuxer.onmessage = null;
|
12910 | handleDone_({
|
12911 | transmuxedData: transmuxedData,
|
12912 | callback: onDone
|
12913 | });
|
12914 |
|
12915 |
|
12916 | dequeue(transmuxer);
|
12917 |
|
12918 | };
|
12919 |
|
12920 | transmuxer.onmessage = handleMessage;
|
12921 |
|
12922 | if (audioAppendStart) {
|
12923 | transmuxer.postMessage({
|
12924 | action: 'setAudioAppendStart',
|
12925 | appendStart: audioAppendStart
|
12926 | });
|
12927 | }
|
12928 |
|
12929 |
|
12930 | if (Array.isArray(gopsToAlignWith)) {
|
12931 | transmuxer.postMessage({
|
12932 | action: 'alignGopsWith',
|
12933 | gopsToAlignWith: gopsToAlignWith
|
12934 | });
|
12935 | }
|
12936 |
|
12937 | if (typeof remux !== 'undefined') {
|
12938 | transmuxer.postMessage({
|
12939 | action: 'setRemux',
|
12940 | remux: remux
|
12941 | });
|
12942 | }
|
12943 |
|
12944 | if (bytes.byteLength) {
|
12945 | var buffer = bytes instanceof ArrayBuffer ? bytes : bytes.buffer;
|
12946 | var byteOffset = bytes instanceof ArrayBuffer ? 0 : bytes.byteOffset;
|
12947 | transmuxer.postMessage({
|
12948 | action: 'push',
|
12949 |
|
12950 |
|
12951 |
|
12952 | data: buffer,
|
12953 |
|
12954 |
|
12955 | byteOffset: byteOffset,
|
12956 | byteLength: bytes.byteLength
|
12957 | }, [buffer]);
|
12958 | }
|
12959 |
|
12960 | if (isEndOfTimeline) {
|
12961 | transmuxer.postMessage({
|
12962 | action: 'endTimeline'
|
12963 | });
|
12964 | }
|
12965 |
|
12966 |
|
12967 |
|
12968 | transmuxer.postMessage({
|
12969 | action: 'flush'
|
12970 | });
|
12971 | };
|
12972 | var dequeue = function dequeue(transmuxer) {
|
12973 | transmuxer.currentTransmux = null;
|
12974 |
|
12975 | if (transmuxer.transmuxQueue.length) {
|
12976 | transmuxer.currentTransmux = transmuxer.transmuxQueue.shift();
|
12977 |
|
12978 | if (typeof transmuxer.currentTransmux === 'function') {
|
12979 | transmuxer.currentTransmux();
|
12980 | } else {
|
12981 | processTransmux(transmuxer.currentTransmux);
|
12982 | }
|
12983 | }
|
12984 | };
|
12985 | var processAction = function processAction(transmuxer, action) {
|
12986 | transmuxer.postMessage({
|
12987 | action: action
|
12988 | });
|
12989 | dequeue(transmuxer);
|
12990 | };
|
12991 | var enqueueAction = function enqueueAction(action, transmuxer) {
|
12992 | if (!transmuxer.currentTransmux) {
|
12993 | transmuxer.currentTransmux = action;
|
12994 | processAction(transmuxer, action);
|
12995 | return;
|
12996 | }
|
12997 |
|
12998 | transmuxer.transmuxQueue.push(processAction.bind(null, transmuxer, action));
|
12999 | };
|
13000 | var reset = function reset(transmuxer) {
|
13001 | enqueueAction('reset', transmuxer);
|
13002 | };
|
13003 | var endTimeline = function endTimeline(transmuxer) {
|
13004 | enqueueAction('endTimeline', transmuxer);
|
13005 | };
|
13006 | var transmux = function transmux(options) {
|
13007 | if (!options.transmuxer.currentTransmux) {
|
13008 | options.transmuxer.currentTransmux = options;
|
13009 | processTransmux(options);
|
13010 | return;
|
13011 | }
|
13012 |
|
13013 | options.transmuxer.transmuxQueue.push(options);
|
13014 | };
|
13015 | var createTransmuxer = function createTransmuxer(options) {
|
13016 | var transmuxer = new TransmuxWorker();
|
13017 | transmuxer.currentTransmux = null;
|
13018 | transmuxer.transmuxQueue = [];
|
13019 | var term = transmuxer.terminate;
|
13020 |
|
13021 | transmuxer.terminate = function () {
|
13022 | transmuxer.currentTransmux = null;
|
13023 | transmuxer.transmuxQueue.length = 0;
|
13024 | return term.call(transmuxer);
|
13025 | };
|
13026 |
|
13027 | transmuxer.postMessage({
|
13028 | action: 'init',
|
13029 | options: options
|
13030 | });
|
13031 | return transmuxer;
|
13032 | };
|
13033 | var segmentTransmuxer = {
|
13034 | reset: reset,
|
13035 | endTimeline: endTimeline,
|
13036 | transmux: transmux,
|
13037 | createTransmuxer: createTransmuxer
|
13038 | };
|
13039 |
|
13040 | var workerCallback = function workerCallback(options) {
|
13041 | var transmuxer = options.transmuxer;
|
13042 | var endAction = options.endAction || options.action;
|
13043 | var callback = options.callback;
|
13044 |
|
13045 | var message = _extends({}, options, {
|
13046 | endAction: null,
|
13047 | transmuxer: null,
|
13048 | callback: null
|
13049 | });
|
13050 |
|
13051 | var listenForEndEvent = function listenForEndEvent(event) {
|
13052 | if (event.data.action !== endAction) {
|
13053 | return;
|
13054 | }
|
13055 |
|
13056 | transmuxer.removeEventListener('message', listenForEndEvent);
|
13057 |
|
13058 | if (event.data.data) {
|
13059 | event.data.data = new Uint8Array(event.data.data, options.byteOffset || 0, options.byteLength || event.data.data.byteLength);
|
13060 |
|
13061 | if (options.data) {
|
13062 | options.data = event.data.data;
|
13063 | }
|
13064 | }
|
13065 |
|
13066 | callback(event.data);
|
13067 | };
|
13068 |
|
13069 | transmuxer.addEventListener('message', listenForEndEvent);
|
13070 |
|
13071 | if (options.data) {
|
13072 | var isArrayBuffer = options.data instanceof ArrayBuffer;
|
13073 | message.byteOffset = isArrayBuffer ? 0 : options.data.byteOffset;
|
13074 | message.byteLength = options.data.byteLength;
|
13075 | var transfers = [isArrayBuffer ? options.data : options.data.buffer];
|
13076 | transmuxer.postMessage(message, transfers);
|
13077 | } else {
|
13078 | transmuxer.postMessage(message);
|
13079 | }
|
13080 | };
|
13081 |
|
13082 | var REQUEST_ERRORS = {
|
13083 | FAILURE: 2,
|
13084 | TIMEOUT: -101,
|
13085 | ABORTED: -102
|
13086 | };
|
13087 |
|
13088 |
|
13089 |
|
13090 |
|
13091 |
|
13092 |
|
13093 | var abortAll = function abortAll(activeXhrs) {
|
13094 | activeXhrs.forEach(function (xhr) {
|
13095 | xhr.abort();
|
13096 | });
|
13097 | };
|
13098 |
|
13099 |
|
13100 |
|
13101 |
|
13102 |
|
13103 |
|
13104 |
|
13105 | var getRequestStats = function getRequestStats(request) {
|
13106 | return {
|
13107 | bandwidth: request.bandwidth,
|
13108 | bytesReceived: request.bytesReceived || 0,
|
13109 | roundTripTime: request.roundTripTime || 0
|
13110 | };
|
13111 | };
|
13112 |
|
13113 |
|
13114 |
|
13115 |
|
13116 |
|
13117 |
|
13118 |
|
13119 |
|
13120 | var getProgressStats = function getProgressStats(progressEvent) {
|
13121 | var request = progressEvent.target;
|
13122 | var roundTripTime = Date.now() - request.requestTime;
|
13123 | var stats = {
|
13124 | bandwidth: Infinity,
|
13125 | bytesReceived: 0,
|
13126 | roundTripTime: roundTripTime || 0
|
13127 | };
|
13128 | stats.bytesReceived = progressEvent.loaded;
|
13129 |
|
13130 |
|
13131 |
|
13132 | stats.bandwidth = Math.floor(stats.bytesReceived / stats.roundTripTime * 8 * 1000);
|
13133 | return stats;
|
13134 | };
|
13135 |
|
13136 |
|
13137 |
|
13138 |
|
13139 |
|
13140 |
|
13141 |
|
13142 |
|
13143 |
|
13144 | var handleErrors = function handleErrors(error, request) {
|
13145 | if (request.timedout) {
|
13146 | return {
|
13147 | status: request.status,
|
13148 | message: 'HLS request timed-out at URL: ' + request.uri,
|
13149 | code: REQUEST_ERRORS.TIMEOUT,
|
13150 | xhr: request
|
13151 | };
|
13152 | }
|
13153 |
|
13154 | if (request.aborted) {
|
13155 | return {
|
13156 | status: request.status,
|
13157 | message: 'HLS request aborted at URL: ' + request.uri,
|
13158 | code: REQUEST_ERRORS.ABORTED,
|
13159 | xhr: request
|
13160 | };
|
13161 | }
|
13162 |
|
13163 | if (error) {
|
13164 | return {
|
13165 | status: request.status,
|
13166 | message: 'HLS request errored at URL: ' + request.uri,
|
13167 | code: REQUEST_ERRORS.FAILURE,
|
13168 | xhr: request
|
13169 | };
|
13170 | }
|
13171 |
|
13172 | if (request.responseType === 'arraybuffer' && request.response.byteLength === 0) {
|
13173 | return {
|
13174 | status: request.status,
|
13175 | message: 'Empty HLS response at URL: ' + request.uri,
|
13176 | code: REQUEST_ERRORS.FAILURE,
|
13177 | xhr: request
|
13178 | };
|
13179 | }
|
13180 |
|
13181 | return null;
|
13182 | };
|
13183 |
|
13184 |
|
13185 |
|
13186 |
|
13187 |
|
13188 |
|
13189 |
|
13190 |
|
13191 |
|
13192 |
|
13193 |
|
13194 |
|
13195 | var handleKeyResponse = function handleKeyResponse(segment, objects, finishProcessingFn) {
|
13196 | return function (error, request) {
|
13197 | var response = request.response;
|
13198 | var errorObj = handleErrors(error, request);
|
13199 |
|
13200 | if (errorObj) {
|
13201 | return finishProcessingFn(errorObj, segment);
|
13202 | }
|
13203 |
|
13204 | if (response.byteLength !== 16) {
|
13205 | return finishProcessingFn({
|
13206 | status: request.status,
|
13207 | message: 'Invalid HLS key at URL: ' + request.uri,
|
13208 | code: REQUEST_ERRORS.FAILURE,
|
13209 | xhr: request
|
13210 | }, segment);
|
13211 | }
|
13212 |
|
13213 | var view = new DataView(response);
|
13214 | var bytes = new Uint32Array([view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12)]);
|
13215 |
|
13216 | for (var i = 0; i < objects.length; i++) {
|
13217 | objects[i].bytes = bytes;
|
13218 | }
|
13219 |
|
13220 | return finishProcessingFn(null, segment);
|
13221 | };
|
13222 | };
|
13223 |
|
13224 | var parseInitSegment = function parseInitSegment(segment, _callback) {
|
13225 | var type = detectContainerForBytes(segment.map.bytes);
|
13226 |
|
13227 |
|
13228 | if (type !== 'mp4') {
|
13229 | var uri = segment.map.resolvedUri || segment.map.uri;
|
13230 | return _callback({
|
13231 | internal: true,
|
13232 | message: "Found unsupported " + (type || 'unknown') + " container for initialization segment at URL: " + uri,
|
13233 | code: REQUEST_ERRORS.FAILURE
|
13234 | });
|
13235 | }
|
13236 |
|
13237 | workerCallback({
|
13238 | action: 'probeMp4Tracks',
|
13239 | data: segment.map.bytes,
|
13240 | transmuxer: segment.transmuxer,
|
13241 | callback: function callback(_ref) {
|
13242 | var tracks = _ref.tracks,
|
13243 | data = _ref.data;
|
13244 |
|
13245 | segment.map.bytes = data;
|
13246 | tracks.forEach(function (track) {
|
13247 | segment.map.tracks = segment.map.tracks || {};
|
13248 |
|
13249 | if (segment.map.tracks[track.type]) {
|
13250 | return;
|
13251 | }
|
13252 |
|
13253 | segment.map.tracks[track.type] = track;
|
13254 |
|
13255 | if (typeof track.id === 'number' && track.timescale) {
|
13256 | segment.map.timescales = segment.map.timescales || {};
|
13257 | segment.map.timescales[track.id] = track.timescale;
|
13258 | }
|
13259 | });
|
13260 | return _callback(null);
|
13261 | }
|
13262 | });
|
13263 | };
|
13264 |
|
13265 |
|
13266 |
|
13267 |
|
13268 |
|
13269 |
|
13270 |
|
13271 |
|
13272 |
|
13273 |
|
13274 | var handleInitSegmentResponse = function handleInitSegmentResponse(_ref2) {
|
13275 | var segment = _ref2.segment,
|
13276 | finishProcessingFn = _ref2.finishProcessingFn;
|
13277 | return function (error, request) {
|
13278 | var errorObj = handleErrors(error, request);
|
13279 |
|
13280 | if (errorObj) {
|
13281 | return finishProcessingFn(errorObj, segment);
|
13282 | }
|
13283 |
|
13284 | var bytes = new Uint8Array(request.response);
|
13285 |
|
13286 |
|
13287 | if (segment.map.key) {
|
13288 | segment.map.encryptedBytes = bytes;
|
13289 | return finishProcessingFn(null, segment);
|
13290 | }
|
13291 |
|
13292 | segment.map.bytes = bytes;
|
13293 | parseInitSegment(segment, function (parseError) {
|
13294 | if (parseError) {
|
13295 | parseError.xhr = request;
|
13296 | parseError.status = request.status;
|
13297 | return finishProcessingFn(parseError, segment);
|
13298 | }
|
13299 |
|
13300 | finishProcessingFn(null, segment);
|
13301 | });
|
13302 | };
|
13303 | };
|
13304 |
|
13305 |
|
13306 |
|
13307 |
|
13308 |
|
13309 |
|
13310 |
|
13311 |
|
13312 |
|
13313 |
|
13314 |
|
13315 |
|
13316 | var handleSegmentResponse = function handleSegmentResponse(_ref3) {
|
13317 | var segment = _ref3.segment,
|
13318 | finishProcessingFn = _ref3.finishProcessingFn,
|
13319 | responseType = _ref3.responseType;
|
13320 | return function (error, request) {
|
13321 | var errorObj = handleErrors(error, request);
|
13322 |
|
13323 | if (errorObj) {
|
13324 | return finishProcessingFn(errorObj, segment);
|
13325 | }
|
13326 |
|
13327 | var newBytes =
|
13328 |
|
13329 |
|
13330 |
|
13331 |
|
13332 | responseType === 'arraybuffer' || !request.responseText ? request.response : stringToArrayBuffer(request.responseText.substring(segment.lastReachedChar || 0));
|
13333 | segment.stats = getRequestStats(request);
|
13334 |
|
13335 | if (segment.key) {
|
13336 | segment.encryptedBytes = new Uint8Array(newBytes);
|
13337 | } else {
|
13338 | segment.bytes = new Uint8Array(newBytes);
|
13339 | }
|
13340 |
|
13341 | return finishProcessingFn(null, segment);
|
13342 | };
|
13343 | };
|
13344 |
|
13345 | var transmuxAndNotify = function transmuxAndNotify(_ref4) {
|
13346 | var segment = _ref4.segment,
|
13347 | bytes = _ref4.bytes,
|
13348 | trackInfoFn = _ref4.trackInfoFn,
|
13349 | timingInfoFn = _ref4.timingInfoFn,
|
13350 | videoSegmentTimingInfoFn = _ref4.videoSegmentTimingInfoFn,
|
13351 | audioSegmentTimingInfoFn = _ref4.audioSegmentTimingInfoFn,
|
13352 | id3Fn = _ref4.id3Fn,
|
13353 | captionsFn = _ref4.captionsFn,
|
13354 | isEndOfTimeline = _ref4.isEndOfTimeline,
|
13355 | endedTimelineFn = _ref4.endedTimelineFn,
|
13356 | dataFn = _ref4.dataFn,
|
13357 | doneFn = _ref4.doneFn,
|
13358 | onTransmuxerLog = _ref4.onTransmuxerLog;
|
13359 | var fmp4Tracks = segment.map && segment.map.tracks || {};
|
13360 | var isMuxed = Boolean(fmp4Tracks.audio && fmp4Tracks.video);
|
13361 |
|
13362 |
|
13363 |
|
13364 | var audioStartFn = timingInfoFn.bind(null, segment, 'audio', 'start');
|
13365 | var audioEndFn = timingInfoFn.bind(null, segment, 'audio', 'end');
|
13366 | var videoStartFn = timingInfoFn.bind(null, segment, 'video', 'start');
|
13367 | var videoEndFn = timingInfoFn.bind(null, segment, 'video', 'end');
|
13368 |
|
13369 | var finish = function finish() {
|
13370 | return transmux({
|
13371 | bytes: bytes,
|
13372 | transmuxer: segment.transmuxer,
|
13373 | audioAppendStart: segment.audioAppendStart,
|
13374 | gopsToAlignWith: segment.gopsToAlignWith,
|
13375 | remux: isMuxed,
|
13376 | onData: function onData(result) {
|
13377 | result.type = result.type === 'combined' ? 'video' : result.type;
|
13378 | dataFn(segment, result);
|
13379 | },
|
13380 | onTrackInfo: function onTrackInfo(trackInfo) {
|
13381 | if (trackInfoFn) {
|
13382 | if (isMuxed) {
|
13383 | trackInfo.isMuxed = true;
|
13384 | }
|
13385 |
|
13386 | trackInfoFn(segment, trackInfo);
|
13387 | }
|
13388 | },
|
13389 | onAudioTimingInfo: function onAudioTimingInfo(audioTimingInfo) {
|
13390 |
|
13391 | if (audioStartFn && typeof audioTimingInfo.start !== 'undefined') {
|
13392 | audioStartFn(audioTimingInfo.start);
|
13393 | audioStartFn = null;
|
13394 | }
|
13395 |
|
13396 |
|
13397 | if (audioEndFn && typeof audioTimingInfo.end !== 'undefined') {
|
13398 | audioEndFn(audioTimingInfo.end);
|
13399 | }
|
13400 | },
|
13401 | onVideoTimingInfo: function onVideoTimingInfo(videoTimingInfo) {
|
13402 |
|
13403 | if (videoStartFn && typeof videoTimingInfo.start !== 'undefined') {
|
13404 | videoStartFn(videoTimingInfo.start);
|
13405 | videoStartFn = null;
|
13406 | }
|
13407 |
|
13408 |
|
13409 | if (videoEndFn && typeof videoTimingInfo.end !== 'undefined') {
|
13410 | videoEndFn(videoTimingInfo.end);
|
13411 | }
|
13412 | },
|
13413 | onVideoSegmentTimingInfo: function onVideoSegmentTimingInfo(videoSegmentTimingInfo) {
|
13414 | videoSegmentTimingInfoFn(videoSegmentTimingInfo);
|
13415 | },
|
13416 | onAudioSegmentTimingInfo: function onAudioSegmentTimingInfo(audioSegmentTimingInfo) {
|
13417 | audioSegmentTimingInfoFn(audioSegmentTimingInfo);
|
13418 | },
|
13419 | onId3: function onId3(id3Frames, dispatchType) {
|
13420 | id3Fn(segment, id3Frames, dispatchType);
|
13421 | },
|
13422 | onCaptions: function onCaptions(captions) {
|
13423 | captionsFn(segment, [captions]);
|
13424 | },
|
13425 | isEndOfTimeline: isEndOfTimeline,
|
13426 | onEndedTimeline: function onEndedTimeline() {
|
13427 | endedTimelineFn();
|
13428 | },
|
13429 | onTransmuxerLog: onTransmuxerLog,
|
13430 | onDone: function onDone(result) {
|
13431 | if (!doneFn) {
|
13432 | return;
|
13433 | }
|
13434 |
|
13435 | result.type = result.type === 'combined' ? 'video' : result.type;
|
13436 | doneFn(null, segment, result);
|
13437 | }
|
13438 | });
|
13439 | };
|
13440 |
|
13441 |
|
13442 |
|
13443 |
|
13444 | workerCallback({
|
13445 | action: 'probeTs',
|
13446 | transmuxer: segment.transmuxer,
|
13447 | data: bytes,
|
13448 | baseStartTime: segment.baseStartTime,
|
13449 | callback: function callback(data) {
|
13450 | segment.bytes = bytes = data.data;
|
13451 | var probeResult = data.result;
|
13452 |
|
13453 | if (probeResult) {
|
13454 | trackInfoFn(segment, {
|
13455 | hasAudio: probeResult.hasAudio,
|
13456 | hasVideo: probeResult.hasVideo,
|
13457 | isMuxed: isMuxed
|
13458 | });
|
13459 | trackInfoFn = null;
|
13460 |
|
13461 | if (probeResult.hasAudio && !isMuxed) {
|
13462 | audioStartFn(probeResult.audioStart);
|
13463 | }
|
13464 |
|
13465 | if (probeResult.hasVideo) {
|
13466 | videoStartFn(probeResult.videoStart);
|
13467 | }
|
13468 |
|
13469 | audioStartFn = null;
|
13470 | videoStartFn = null;
|
13471 | }
|
13472 |
|
13473 | finish();
|
13474 | }
|
13475 | });
|
13476 | };
|
13477 |
|
13478 | var handleSegmentBytes = function handleSegmentBytes(_ref5) {
|
13479 | var segment = _ref5.segment,
|
13480 | bytes = _ref5.bytes,
|
13481 | trackInfoFn = _ref5.trackInfoFn,
|
13482 | timingInfoFn = _ref5.timingInfoFn,
|
13483 | videoSegmentTimingInfoFn = _ref5.videoSegmentTimingInfoFn,
|
13484 | audioSegmentTimingInfoFn = _ref5.audioSegmentTimingInfoFn,
|
13485 | id3Fn = _ref5.id3Fn,
|
13486 | captionsFn = _ref5.captionsFn,
|
13487 | isEndOfTimeline = _ref5.isEndOfTimeline,
|
13488 | endedTimelineFn = _ref5.endedTimelineFn,
|
13489 | dataFn = _ref5.dataFn,
|
13490 | doneFn = _ref5.doneFn,
|
13491 | onTransmuxerLog = _ref5.onTransmuxerLog;
|
13492 | var bytesAsUint8Array = new Uint8Array(bytes);
|
13493 |
|
13494 |
|
13495 |
|
13496 |
|
13497 |
|
13498 | if (isLikelyFmp4MediaSegment(bytesAsUint8Array)) {
|
13499 | segment.isFmp4 = true;
|
13500 | var tracks = segment.map.tracks;
|
13501 | var trackInfo = {
|
13502 | isFmp4: true,
|
13503 | hasVideo: !!tracks.video,
|
13504 | hasAudio: !!tracks.audio
|
13505 | };
|
13506 |
|
13507 |
|
13508 | if (tracks.audio && tracks.audio.codec && tracks.audio.codec !== 'enca') {
|
13509 | trackInfo.audioCodec = tracks.audio.codec;
|
13510 | }
|
13511 |
|
13512 |
|
13513 |
|
13514 | if (tracks.video && tracks.video.codec && tracks.video.codec !== 'encv') {
|
13515 | trackInfo.videoCodec = tracks.video.codec;
|
13516 | }
|
13517 |
|
13518 | if (tracks.video && tracks.audio) {
|
13519 | trackInfo.isMuxed = true;
|
13520 | }
|
13521 |
|
13522 |
|
13523 |
|
13524 | trackInfoFn(segment, trackInfo);
|
13525 |
|
13526 |
|
13527 |
|
13528 |
|
13529 |
|
13530 |
|
13531 | var finishLoading = function finishLoading(captions) {
|
13532 |
|
13533 |
|
13534 |
|
13535 |
|
13536 | dataFn(segment, {
|
13537 | data: bytesAsUint8Array,
|
13538 | type: trackInfo.hasAudio && !trackInfo.isMuxed ? 'audio' : 'video'
|
13539 | });
|
13540 |
|
13541 | if (captions && captions.length) {
|
13542 | captionsFn(segment, captions);
|
13543 | }
|
13544 |
|
13545 | doneFn(null, segment, {});
|
13546 | };
|
13547 |
|
13548 | workerCallback({
|
13549 | action: 'probeMp4StartTime',
|
13550 | timescales: segment.map.timescales,
|
13551 | data: bytesAsUint8Array,
|
13552 | transmuxer: segment.transmuxer,
|
13553 | callback: function callback(_ref6) {
|
13554 | var data = _ref6.data,
|
13555 | startTime = _ref6.startTime;
|
13556 |
|
13557 | bytes = data.buffer;
|
13558 | segment.bytes = bytesAsUint8Array = data;
|
13559 |
|
13560 | if (trackInfo.hasAudio && !trackInfo.isMuxed) {
|
13561 | timingInfoFn(segment, 'audio', 'start', startTime);
|
13562 | }
|
13563 |
|
13564 | if (trackInfo.hasVideo) {
|
13565 | timingInfoFn(segment, 'video', 'start', startTime);
|
13566 | }
|
13567 |
|
13568 |
|
13569 |
|
13570 | if (!tracks.video || !data.byteLength || !segment.transmuxer) {
|
13571 | finishLoading();
|
13572 | return;
|
13573 | }
|
13574 |
|
13575 | workerCallback({
|
13576 | action: 'pushMp4Captions',
|
13577 | endAction: 'mp4Captions',
|
13578 | transmuxer: segment.transmuxer,
|
13579 | data: bytesAsUint8Array,
|
13580 | timescales: segment.map.timescales,
|
13581 | trackIds: [tracks.video.id],
|
13582 | callback: function callback(message) {
|
13583 |
|
13584 | bytes = message.data.buffer;
|
13585 | segment.bytes = bytesAsUint8Array = message.data;
|
13586 | message.logs.forEach(function (log) {
|
13587 | onTransmuxerLog(videojs.mergeOptions(log, {
|
13588 | stream: 'mp4CaptionParser'
|
13589 | }));
|
13590 | });
|
13591 | finishLoading(message.captions);
|
13592 | }
|
13593 | });
|
13594 | }
|
13595 | });
|
13596 | return;
|
13597 | }
|
13598 |
|
13599 |
|
13600 | if (!segment.transmuxer) {
|
13601 | doneFn(null, segment, {});
|
13602 | return;
|
13603 | }
|
13604 |
|
13605 | if (typeof segment.container === 'undefined') {
|
13606 | segment.container = detectContainerForBytes(bytesAsUint8Array);
|
13607 | }
|
13608 |
|
13609 | if (segment.container !== 'ts' && segment.container !== 'aac') {
|
13610 | trackInfoFn(segment, {
|
13611 | hasAudio: false,
|
13612 | hasVideo: false
|
13613 | });
|
13614 | doneFn(null, segment, {});
|
13615 | return;
|
13616 | }
|
13617 |
|
13618 |
|
13619 | transmuxAndNotify({
|
13620 | segment: segment,
|
13621 | bytes: bytes,
|
13622 | trackInfoFn: trackInfoFn,
|
13623 | timingInfoFn: timingInfoFn,
|
13624 | videoSegmentTimingInfoFn: videoSegmentTimingInfoFn,
|
13625 | audioSegmentTimingInfoFn: audioSegmentTimingInfoFn,
|
13626 | id3Fn: id3Fn,
|
13627 | captionsFn: captionsFn,
|
13628 | isEndOfTimeline: isEndOfTimeline,
|
13629 | endedTimelineFn: endedTimelineFn,
|
13630 | dataFn: dataFn,
|
13631 | doneFn: doneFn,
|
13632 | onTransmuxerLog: onTransmuxerLog
|
13633 | });
|
13634 | };
|
13635 |
|
13636 | var decrypt = function decrypt(_ref7, callback) {
|
13637 | var id = _ref7.id,
|
13638 | key = _ref7.key,
|
13639 | encryptedBytes = _ref7.encryptedBytes,
|
13640 | decryptionWorker = _ref7.decryptionWorker;
|
13641 |
|
13642 | var decryptionHandler = function decryptionHandler(event) {
|
13643 | if (event.data.source === id) {
|
13644 | decryptionWorker.removeEventListener('message', decryptionHandler);
|
13645 | var decrypted = event.data.decrypted;
|
13646 | callback(new Uint8Array(decrypted.bytes, decrypted.byteOffset, decrypted.byteLength));
|
13647 | }
|
13648 | };
|
13649 |
|
13650 | decryptionWorker.addEventListener('message', decryptionHandler);
|
13651 | var keyBytes;
|
13652 |
|
13653 | if (key.bytes.slice) {
|
13654 | keyBytes = key.bytes.slice();
|
13655 | } else {
|
13656 | keyBytes = new Uint32Array(Array.prototype.slice.call(key.bytes));
|
13657 | }
|
13658 |
|
13659 |
|
13660 | decryptionWorker.postMessage(createTransferableMessage({
|
13661 | source: id,
|
13662 | encrypted: encryptedBytes,
|
13663 | key: keyBytes,
|
13664 | iv: key.iv
|
13665 | }), [encryptedBytes.buffer, keyBytes.buffer]);
|
13666 | };
|
13667 |
|
13668 |
|
13669 |
|
13670 |
|
13671 |
|
13672 |
|
13673 |
|
13674 |
|
13675 |
|
13676 |
|
13677 |
|
13678 |
|
13679 |
|
13680 |
|
13681 |
|
13682 |
|
13683 |
|
13684 |
|
13685 |
|
13686 |
|
13687 |
|
13688 |
|
13689 |
|
13690 |
|
13691 |
|
13692 |
|
13693 | var decryptSegment = function decryptSegment(_ref8) {
|
13694 | var decryptionWorker = _ref8.decryptionWorker,
|
13695 | segment = _ref8.segment,
|
13696 | trackInfoFn = _ref8.trackInfoFn,
|
13697 | timingInfoFn = _ref8.timingInfoFn,
|
13698 | videoSegmentTimingInfoFn = _ref8.videoSegmentTimingInfoFn,
|
13699 | audioSegmentTimingInfoFn = _ref8.audioSegmentTimingInfoFn,
|
13700 | id3Fn = _ref8.id3Fn,
|
13701 | captionsFn = _ref8.captionsFn,
|
13702 | isEndOfTimeline = _ref8.isEndOfTimeline,
|
13703 | endedTimelineFn = _ref8.endedTimelineFn,
|
13704 | dataFn = _ref8.dataFn,
|
13705 | doneFn = _ref8.doneFn,
|
13706 | onTransmuxerLog = _ref8.onTransmuxerLog;
|
13707 | decrypt({
|
13708 | id: segment.requestId,
|
13709 | key: segment.key,
|
13710 | encryptedBytes: segment.encryptedBytes,
|
13711 | decryptionWorker: decryptionWorker
|
13712 | }, function (decryptedBytes) {
|
13713 | segment.bytes = decryptedBytes;
|
13714 | handleSegmentBytes({
|
13715 | segment: segment,
|
13716 | bytes: segment.bytes,
|
13717 | trackInfoFn: trackInfoFn,
|
13718 | timingInfoFn: timingInfoFn,
|
13719 | videoSegmentTimingInfoFn: videoSegmentTimingInfoFn,
|
13720 | audioSegmentTimingInfoFn: audioSegmentTimingInfoFn,
|
13721 | id3Fn: id3Fn,
|
13722 | captionsFn: captionsFn,
|
13723 | isEndOfTimeline: isEndOfTimeline,
|
13724 | endedTimelineFn: endedTimelineFn,
|
13725 | dataFn: dataFn,
|
13726 | doneFn: doneFn,
|
13727 | onTransmuxerLog: onTransmuxerLog
|
13728 | });
|
13729 | });
|
13730 | };
|
13731 |
|
13732 |
|
13733 |
|
13734 |
|
13735 |
|
13736 |
|
13737 |
|
13738 |
|
13739 |
|
13740 |
|
13741 |
|
13742 |
|
13743 |
|
13744 |
|
13745 |
|
13746 |
|
13747 |
|
13748 |
|
13749 |
|
13750 |
|
13751 |
|
13752 |
|
13753 |
|
13754 |
|
13755 |
|
13756 |
|
13757 |
|
13758 |
|
13759 |
|
13760 |
|
13761 |
|
13762 | var waitForCompletion = function waitForCompletion(_ref9) {
|
13763 | var activeXhrs = _ref9.activeXhrs,
|
13764 | decryptionWorker = _ref9.decryptionWorker,
|
13765 | trackInfoFn = _ref9.trackInfoFn,
|
13766 | timingInfoFn = _ref9.timingInfoFn,
|
13767 | videoSegmentTimingInfoFn = _ref9.videoSegmentTimingInfoFn,
|
13768 | audioSegmentTimingInfoFn = _ref9.audioSegmentTimingInfoFn,
|
13769 | id3Fn = _ref9.id3Fn,
|
13770 | captionsFn = _ref9.captionsFn,
|
13771 | isEndOfTimeline = _ref9.isEndOfTimeline,
|
13772 | endedTimelineFn = _ref9.endedTimelineFn,
|
13773 | dataFn = _ref9.dataFn,
|
13774 | doneFn = _ref9.doneFn,
|
13775 | onTransmuxerLog = _ref9.onTransmuxerLog;
|
13776 | var count = 0;
|
13777 | var didError = false;
|
13778 | return function (error, segment) {
|
13779 | if (didError) {
|
13780 | return;
|
13781 | }
|
13782 |
|
13783 | if (error) {
|
13784 | didError = true;
|
13785 |
|
13786 | abortAll(activeXhrs);
|
13787 |
|
13788 |
|
13789 |
|
13790 |
|
13791 |
|
13792 |
|
13793 |
|
13794 |
|
13795 |
|
13796 |
|
13797 |
|
13798 | return doneFn(error, segment);
|
13799 | }
|
13800 |
|
13801 | count += 1;
|
13802 |
|
13803 | if (count === activeXhrs.length) {
|
13804 | var segmentFinish = function segmentFinish() {
|
13805 | if (segment.encryptedBytes) {
|
13806 | return decryptSegment({
|
13807 | decryptionWorker: decryptionWorker,
|
13808 | segment: segment,
|
13809 | trackInfoFn: trackInfoFn,
|
13810 | timingInfoFn: timingInfoFn,
|
13811 | videoSegmentTimingInfoFn: videoSegmentTimingInfoFn,
|
13812 | audioSegmentTimingInfoFn: audioSegmentTimingInfoFn,
|
13813 | id3Fn: id3Fn,
|
13814 | captionsFn: captionsFn,
|
13815 | isEndOfTimeline: isEndOfTimeline,
|
13816 | endedTimelineFn: endedTimelineFn,
|
13817 | dataFn: dataFn,
|
13818 | doneFn: doneFn,
|
13819 | onTransmuxerLog: onTransmuxerLog
|
13820 | });
|
13821 | }
|
13822 |
|
13823 |
|
13824 | handleSegmentBytes({
|
13825 | segment: segment,
|
13826 | bytes: segment.bytes,
|
13827 | trackInfoFn: trackInfoFn,
|
13828 | timingInfoFn: timingInfoFn,
|
13829 | videoSegmentTimingInfoFn: videoSegmentTimingInfoFn,
|
13830 | audioSegmentTimingInfoFn: audioSegmentTimingInfoFn,
|
13831 | id3Fn: id3Fn,
|
13832 | captionsFn: captionsFn,
|
13833 | isEndOfTimeline: isEndOfTimeline,
|
13834 | endedTimelineFn: endedTimelineFn,
|
13835 | dataFn: dataFn,
|
13836 | doneFn: doneFn,
|
13837 | onTransmuxerLog: onTransmuxerLog
|
13838 | });
|
13839 | };
|
13840 |
|
13841 |
|
13842 | segment.endOfAllRequests = Date.now();
|
13843 |
|
13844 | if (segment.map && segment.map.encryptedBytes && !segment.map.bytes) {
|
13845 | return decrypt({
|
13846 | decryptionWorker: decryptionWorker,
|
13847 |
|
13848 |
|
13849 |
|
13850 | id: segment.requestId + '-init',
|
13851 | encryptedBytes: segment.map.encryptedBytes,
|
13852 | key: segment.map.key
|
13853 | }, function (decryptedBytes) {
|
13854 | segment.map.bytes = decryptedBytes;
|
13855 | parseInitSegment(segment, function (parseError) {
|
13856 | if (parseError) {
|
13857 | abortAll(activeXhrs);
|
13858 | return doneFn(parseError, segment);
|
13859 | }
|
13860 |
|
13861 | segmentFinish();
|
13862 | });
|
13863 | });
|
13864 | }
|
13865 |
|
13866 | segmentFinish();
|
13867 | }
|
13868 | };
|
13869 | };
|
13870 |
|
13871 |
|
13872 |
|
13873 |
|
13874 |
|
13875 |
|
13876 |
|
13877 |
|
13878 |
|
13879 | var handleLoadEnd = function handleLoadEnd(_ref10) {
|
13880 | var loadendState = _ref10.loadendState,
|
13881 | abortFn = _ref10.abortFn;
|
13882 | return function (event) {
|
13883 | var request = event.target;
|
13884 |
|
13885 | if (request.aborted && abortFn && !loadendState.calledAbortFn) {
|
13886 | abortFn();
|
13887 | loadendState.calledAbortFn = true;
|
13888 | }
|
13889 | };
|
13890 | };
|
13891 |
|
13892 |
|
13893 |
|
13894 |
|
13895 |
|
13896 |
|
13897 |
|
13898 |
|
13899 |
|
13900 |
|
13901 |
|
13902 |
|
13903 |
|
13904 |
|
13905 |
|
13906 |
|
13907 |
|
13908 |
|
13909 |
|
13910 |
|
13911 |
|
13912 |
|
13913 |
|
13914 |
|
13915 |
|
13916 |
|
13917 |
|
13918 | var handleProgress = function handleProgress(_ref11) {
|
13919 | var segment = _ref11.segment,
|
13920 | progressFn = _ref11.progressFn;
|
13921 | _ref11.trackInfoFn;
|
13922 | _ref11.timingInfoFn;
|
13923 | _ref11.videoSegmentTimingInfoFn;
|
13924 | _ref11.audioSegmentTimingInfoFn;
|
13925 | _ref11.id3Fn;
|
13926 | _ref11.captionsFn;
|
13927 | _ref11.isEndOfTimeline;
|
13928 | _ref11.endedTimelineFn;
|
13929 | _ref11.dataFn;
|
13930 | return function (event) {
|
13931 | var request = event.target;
|
13932 |
|
13933 | if (request.aborted) {
|
13934 | return;
|
13935 | }
|
13936 |
|
13937 | segment.stats = videojs.mergeOptions(segment.stats, getProgressStats(event));
|
13938 |
|
13939 | if (!segment.stats.firstBytesReceivedAt && segment.stats.bytesReceived) {
|
13940 | segment.stats.firstBytesReceivedAt = Date.now();
|
13941 | }
|
13942 |
|
13943 | return progressFn(event, segment);
|
13944 | };
|
13945 | };
|
13946 |
|
13947 |
|
13948 |
|
13949 |
|
13950 |
|
13951 |
|
13952 |
|
13953 |
|
13954 |
|
13955 |
|
13956 |
|
13957 |
|
13958 |
|
13959 |
|
13960 |
|
13961 |
|
13962 |
|
13963 |
|
13964 |
|
13965 |
|
13966 |
|
13967 |
|
13968 |
|
13969 |
|
13970 |
|
13971 |
|
13972 |
|
13973 |
|
13974 |
|
13975 |
|
13976 |
|
13977 |
|
13978 |
|
13979 |
|
13980 |
|
13981 |
|
13982 |
|
13983 |
|
13984 |
|
13985 |
|
13986 |
|
13987 |
|
13988 |
|
13989 |
|
13990 |
|
13991 |
|
13992 |
|
13993 |
|
13994 |
|
13995 |
|
13996 |
|
13997 |
|
13998 |
|
13999 |
|
14000 |
|
14001 |
|
14002 |
|
14003 |
|
14004 |
|
14005 |
|
14006 |
|
14007 |
|
14008 |
|
14009 |
|
14010 |
|
14011 |
|
14012 |
|
14013 |
|
14014 |
|
14015 |
|
14016 | var mediaSegmentRequest = function mediaSegmentRequest(_ref12) {
|
14017 | var xhr = _ref12.xhr,
|
14018 | xhrOptions = _ref12.xhrOptions,
|
14019 | decryptionWorker = _ref12.decryptionWorker,
|
14020 | segment = _ref12.segment,
|
14021 | abortFn = _ref12.abortFn,
|
14022 | progressFn = _ref12.progressFn,
|
14023 | trackInfoFn = _ref12.trackInfoFn,
|
14024 | timingInfoFn = _ref12.timingInfoFn,
|
14025 | videoSegmentTimingInfoFn = _ref12.videoSegmentTimingInfoFn,
|
14026 | audioSegmentTimingInfoFn = _ref12.audioSegmentTimingInfoFn,
|
14027 | id3Fn = _ref12.id3Fn,
|
14028 | captionsFn = _ref12.captionsFn,
|
14029 | isEndOfTimeline = _ref12.isEndOfTimeline,
|
14030 | endedTimelineFn = _ref12.endedTimelineFn,
|
14031 | dataFn = _ref12.dataFn,
|
14032 | doneFn = _ref12.doneFn,
|
14033 | onTransmuxerLog = _ref12.onTransmuxerLog;
|
14034 | var activeXhrs = [];
|
14035 | var finishProcessingFn = waitForCompletion({
|
14036 | activeXhrs: activeXhrs,
|
14037 | decryptionWorker: decryptionWorker,
|
14038 | trackInfoFn: trackInfoFn,
|
14039 | timingInfoFn: timingInfoFn,
|
14040 | videoSegmentTimingInfoFn: videoSegmentTimingInfoFn,
|
14041 | audioSegmentTimingInfoFn: audioSegmentTimingInfoFn,
|
14042 | id3Fn: id3Fn,
|
14043 | captionsFn: captionsFn,
|
14044 | isEndOfTimeline: isEndOfTimeline,
|
14045 | endedTimelineFn: endedTimelineFn,
|
14046 | dataFn: dataFn,
|
14047 | doneFn: doneFn,
|
14048 | onTransmuxerLog: onTransmuxerLog
|
14049 | });
|
14050 |
|
14051 | if (segment.key && !segment.key.bytes) {
|
14052 | var objects = [segment.key];
|
14053 |
|
14054 | if (segment.map && !segment.map.bytes && segment.map.key && segment.map.key.resolvedUri === segment.key.resolvedUri) {
|
14055 | objects.push(segment.map.key);
|
14056 | }
|
14057 |
|
14058 | var keyRequestOptions = videojs.mergeOptions(xhrOptions, {
|
14059 | uri: segment.key.resolvedUri,
|
14060 | responseType: 'arraybuffer'
|
14061 | });
|
14062 | var keyRequestCallback = handleKeyResponse(segment, objects, finishProcessingFn);
|
14063 | var keyXhr = xhr(keyRequestOptions, keyRequestCallback);
|
14064 | activeXhrs.push(keyXhr);
|
14065 | }
|
14066 |
|
14067 |
|
14068 | if (segment.map && !segment.map.bytes) {
|
14069 | var differentMapKey = segment.map.key && (!segment.key || segment.key.resolvedUri !== segment.map.key.resolvedUri);
|
14070 |
|
14071 | if (differentMapKey) {
|
14072 | var mapKeyRequestOptions = videojs.mergeOptions(xhrOptions, {
|
14073 | uri: segment.map.key.resolvedUri,
|
14074 | responseType: 'arraybuffer'
|
14075 | });
|
14076 | var mapKeyRequestCallback = handleKeyResponse(segment, [segment.map.key], finishProcessingFn);
|
14077 | var mapKeyXhr = xhr(mapKeyRequestOptions, mapKeyRequestCallback);
|
14078 | activeXhrs.push(mapKeyXhr);
|
14079 | }
|
14080 |
|
14081 | var initSegmentOptions = videojs.mergeOptions(xhrOptions, {
|
14082 | uri: segment.map.resolvedUri,
|
14083 | responseType: 'arraybuffer',
|
14084 | headers: segmentXhrHeaders(segment.map)
|
14085 | });
|
14086 | var initSegmentRequestCallback = handleInitSegmentResponse({
|
14087 | segment: segment,
|
14088 | finishProcessingFn: finishProcessingFn
|
14089 | });
|
14090 | var initSegmentXhr = xhr(initSegmentOptions, initSegmentRequestCallback);
|
14091 | activeXhrs.push(initSegmentXhr);
|
14092 | }
|
14093 |
|
14094 | var segmentRequestOptions = videojs.mergeOptions(xhrOptions, {
|
14095 | uri: segment.part && segment.part.resolvedUri || segment.resolvedUri,
|
14096 | responseType: 'arraybuffer',
|
14097 | headers: segmentXhrHeaders(segment)
|
14098 | });
|
14099 | var segmentRequestCallback = handleSegmentResponse({
|
14100 | segment: segment,
|
14101 | finishProcessingFn: finishProcessingFn,
|
14102 | responseType: segmentRequestOptions.responseType
|
14103 | });
|
14104 | var segmentXhr = xhr(segmentRequestOptions, segmentRequestCallback);
|
14105 | segmentXhr.addEventListener('progress', handleProgress({
|
14106 | segment: segment,
|
14107 | progressFn: progressFn,
|
14108 | trackInfoFn: trackInfoFn,
|
14109 | timingInfoFn: timingInfoFn,
|
14110 | videoSegmentTimingInfoFn: videoSegmentTimingInfoFn,
|
14111 | audioSegmentTimingInfoFn: audioSegmentTimingInfoFn,
|
14112 | id3Fn: id3Fn,
|
14113 | captionsFn: captionsFn,
|
14114 | isEndOfTimeline: isEndOfTimeline,
|
14115 | endedTimelineFn: endedTimelineFn,
|
14116 | dataFn: dataFn
|
14117 | }));
|
14118 | activeXhrs.push(segmentXhr);
|
14119 |
|
14120 |
|
14121 | var loadendState = {};
|
14122 | activeXhrs.forEach(function (activeXhr) {
|
14123 | activeXhr.addEventListener('loadend', handleLoadEnd({
|
14124 | loadendState: loadendState,
|
14125 | abortFn: abortFn
|
14126 | }));
|
14127 | });
|
14128 | return function () {
|
14129 | return abortAll(activeXhrs);
|
14130 | };
|
14131 | };
|
14132 |
|
14133 |
|
14134 |
|
14135 |
|
14136 |
|
14137 | var logFn$1 = logger('CodecUtils');
|
14138 |
|
14139 |
|
14140 |
|
14141 |
|
14142 |
|
14143 |
|
14144 |
|
14145 |
|
14146 | var getCodecs = function getCodecs(media) {
|
14147 |
|
14148 |
|
14149 | var mediaAttributes = media.attributes || {};
|
14150 |
|
14151 | if (mediaAttributes.CODECS) {
|
14152 | return parseCodecs(mediaAttributes.CODECS);
|
14153 | }
|
14154 | };
|
14155 |
|
14156 | var isMaat = function isMaat(master, media) {
|
14157 | var mediaAttributes = media.attributes || {};
|
14158 | return master && master.mediaGroups && master.mediaGroups.AUDIO && mediaAttributes.AUDIO && master.mediaGroups.AUDIO[mediaAttributes.AUDIO];
|
14159 | };
|
14160 | var isMuxed = function isMuxed(master, media) {
|
14161 | if (!isMaat(master, media)) {
|
14162 | return true;
|
14163 | }
|
14164 |
|
14165 | var mediaAttributes = media.attributes || {};
|
14166 | var audioGroup = master.mediaGroups.AUDIO[mediaAttributes.AUDIO];
|
14167 |
|
14168 | for (var groupId in audioGroup) {
|
14169 |
|
14170 |
|
14171 |
|
14172 |
|
14173 | if (!audioGroup[groupId].uri && !audioGroup[groupId].playlists) {
|
14174 | return true;
|
14175 | }
|
14176 | }
|
14177 |
|
14178 | return false;
|
14179 | };
|
14180 | var unwrapCodecList = function unwrapCodecList(codecList) {
|
14181 | var codecs = {};
|
14182 | codecList.forEach(function (_ref) {
|
14183 | var mediaType = _ref.mediaType,
|
14184 | type = _ref.type,
|
14185 | details = _ref.details;
|
14186 | codecs[mediaType] = codecs[mediaType] || [];
|
14187 | codecs[mediaType].push(translateLegacyCodec("" + type + details));
|
14188 | });
|
14189 | Object.keys(codecs).forEach(function (mediaType) {
|
14190 | if (codecs[mediaType].length > 1) {
|
14191 | logFn$1("multiple " + mediaType + " codecs found as attributes: " + codecs[mediaType].join(', ') + ". Setting playlist codecs to null so that we wait for mux.js to probe segments for real codecs.");
|
14192 | codecs[mediaType] = null;
|
14193 | return;
|
14194 | }
|
14195 |
|
14196 | codecs[mediaType] = codecs[mediaType][0];
|
14197 | });
|
14198 | return codecs;
|
14199 | };
|
14200 | var codecCount = function codecCount(codecObj) {
|
14201 | var count = 0;
|
14202 |
|
14203 | if (codecObj.audio) {
|
14204 | count++;
|
14205 | }
|
14206 |
|
14207 | if (codecObj.video) {
|
14208 | count++;
|
14209 | }
|
14210 |
|
14211 | return count;
|
14212 | };
|
14213 |
|
14214 |
|
14215 |
|
14216 |
|
14217 |
|
14218 |
|
14219 |
|
14220 |
|
14221 |
|
14222 |
|
14223 |
|
14224 |
|
14225 |
|
14226 | var codecsForPlaylist = function codecsForPlaylist(master, media) {
|
14227 | var mediaAttributes = media.attributes || {};
|
14228 | var codecInfo = unwrapCodecList(getCodecs(media) || []);
|
14229 |
|
14230 |
|
14231 | if (isMaat(master, media) && !codecInfo.audio) {
|
14232 | if (!isMuxed(master, media)) {
|
14233 |
|
14234 |
|
14235 |
|
14236 | var defaultCodecs = unwrapCodecList(codecsFromDefault(master, mediaAttributes.AUDIO) || []);
|
14237 |
|
14238 | if (defaultCodecs.audio) {
|
14239 | codecInfo.audio = defaultCodecs.audio;
|
14240 | }
|
14241 | }
|
14242 | }
|
14243 |
|
14244 | return codecInfo;
|
14245 | };
|
14246 |
|
14247 | var logFn = logger('PlaylistSelector');
|
14248 |
|
14249 | var representationToString = function representationToString(representation) {
|
14250 | if (!representation || !representation.playlist) {
|
14251 | return;
|
14252 | }
|
14253 |
|
14254 | var playlist = representation.playlist;
|
14255 | return JSON.stringify({
|
14256 | id: playlist.id,
|
14257 | bandwidth: representation.bandwidth,
|
14258 | width: representation.width,
|
14259 | height: representation.height,
|
14260 | codecs: playlist.attributes && playlist.attributes.CODECS || ''
|
14261 | });
|
14262 | };
|
14263 |
|
14264 |
|
14265 |
|
14266 |
|
14267 |
|
14268 |
|
14269 |
|
14270 |
|
14271 |
|
14272 |
|
14273 |
|
14274 |
|
14275 |
|
14276 | var safeGetComputedStyle = function safeGetComputedStyle(el, property) {
|
14277 | if (!el) {
|
14278 | return '';
|
14279 | }
|
14280 |
|
14281 | var result = window.getComputedStyle(el);
|
14282 |
|
14283 | if (!result) {
|
14284 | return '';
|
14285 | }
|
14286 |
|
14287 | return result[property];
|
14288 | };
|
14289 |
|
14290 |
|
14291 |
|
14292 |
|
14293 |
|
14294 |
|
14295 |
|
14296 |
|
14297 |
|
14298 | var stableSort = function stableSort(array, sortFn) {
|
14299 | var newArray = array.slice();
|
14300 | array.sort(function (left, right) {
|
14301 | var cmp = sortFn(left, right);
|
14302 |
|
14303 | if (cmp === 0) {
|
14304 | return newArray.indexOf(left) - newArray.indexOf(right);
|
14305 | }
|
14306 |
|
14307 | return cmp;
|
14308 | });
|
14309 | };
|
14310 |
|
14311 |
|
14312 |
|
14313 |
|
14314 |
|
14315 |
|
14316 |
|
14317 |
|
14318 |
|
14319 |
|
14320 |
|
14321 |
|
14322 | var comparePlaylistBandwidth = function comparePlaylistBandwidth(left, right) {
|
14323 | var leftBandwidth;
|
14324 | var rightBandwidth;
|
14325 |
|
14326 | if (left.attributes.BANDWIDTH) {
|
14327 | leftBandwidth = left.attributes.BANDWIDTH;
|
14328 | }
|
14329 |
|
14330 | leftBandwidth = leftBandwidth || window.Number.MAX_VALUE;
|
14331 |
|
14332 | if (right.attributes.BANDWIDTH) {
|
14333 | rightBandwidth = right.attributes.BANDWIDTH;
|
14334 | }
|
14335 |
|
14336 | rightBandwidth = rightBandwidth || window.Number.MAX_VALUE;
|
14337 | return leftBandwidth - rightBandwidth;
|
14338 | };
|
14339 |
|
14340 |
|
14341 |
|
14342 |
|
14343 |
|
14344 |
|
14345 |
|
14346 |
|
14347 |
|
14348 |
|
14349 |
|
14350 | var comparePlaylistResolution = function comparePlaylistResolution(left, right) {
|
14351 | var leftWidth;
|
14352 | var rightWidth;
|
14353 |
|
14354 | if (left.attributes.RESOLUTION && left.attributes.RESOLUTION.width) {
|
14355 | leftWidth = left.attributes.RESOLUTION.width;
|
14356 | }
|
14357 |
|
14358 | leftWidth = leftWidth || window.Number.MAX_VALUE;
|
14359 |
|
14360 | if (right.attributes.RESOLUTION && right.attributes.RESOLUTION.width) {
|
14361 | rightWidth = right.attributes.RESOLUTION.width;
|
14362 | }
|
14363 |
|
14364 | rightWidth = rightWidth || window.Number.MAX_VALUE;
|
14365 |
|
14366 |
|
14367 | if (leftWidth === rightWidth && left.attributes.BANDWIDTH && right.attributes.BANDWIDTH) {
|
14368 | return left.attributes.BANDWIDTH - right.attributes.BANDWIDTH;
|
14369 | }
|
14370 |
|
14371 | return leftWidth - rightWidth;
|
14372 | };
|
14373 |
|
14374 |
|
14375 |
|
14376 |
|
14377 |
|
14378 |
|
14379 |
|
14380 |
|
14381 |
|
14382 |
|
14383 |
|
14384 |
|
14385 |
|
14386 |
|
14387 |
|
14388 |
|
14389 |
|
14390 |
|
14391 |
|
14392 |
|
14393 | var simpleSelector = function simpleSelector(master, playerBandwidth, playerWidth, playerHeight, limitRenditionByPlayerDimensions, masterPlaylistController) {
|
14394 |
|
14395 | if (!master) {
|
14396 | return;
|
14397 | }
|
14398 |
|
14399 | var options = {
|
14400 | bandwidth: playerBandwidth,
|
14401 | width: playerWidth,
|
14402 | height: playerHeight,
|
14403 | limitRenditionByPlayerDimensions: limitRenditionByPlayerDimensions
|
14404 | };
|
14405 | var playlists = master.playlists;
|
14406 |
|
14407 | if (Playlist.isAudioOnly(master)) {
|
14408 | playlists = masterPlaylistController.getAudioTrackPlaylists_();
|
14409 |
|
14410 |
|
14411 | options.audioOnly = true;
|
14412 | }
|
14413 |
|
14414 |
|
14415 | var sortedPlaylistReps = playlists.map(function (playlist) {
|
14416 | var bandwidth;
|
14417 | var width = playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.width;
|
14418 | var height = playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height;
|
14419 | bandwidth = playlist.attributes && playlist.attributes.BANDWIDTH;
|
14420 | bandwidth = bandwidth || window.Number.MAX_VALUE;
|
14421 | return {
|
14422 | bandwidth: bandwidth,
|
14423 | width: width,
|
14424 | height: height,
|
14425 | playlist: playlist
|
14426 | };
|
14427 | });
|
14428 | stableSort(sortedPlaylistReps, function (left, right) {
|
14429 | return left.bandwidth - right.bandwidth;
|
14430 | });
|
14431 |
|
14432 |
|
14433 | sortedPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
14434 | return !Playlist.isIncompatible(rep.playlist);
|
14435 | });
|
14436 |
|
14437 |
|
14438 | var enabledPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
14439 | return Playlist.isEnabled(rep.playlist);
|
14440 | });
|
14441 |
|
14442 | if (!enabledPlaylistReps.length) {
|
14443 |
|
14444 |
|
14445 |
|
14446 | enabledPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
14447 | return !Playlist.isDisabled(rep.playlist);
|
14448 | });
|
14449 | }
|
14450 |
|
14451 |
|
14452 |
|
14453 | var bandwidthPlaylistReps = enabledPlaylistReps.filter(function (rep) {
|
14454 | return rep.bandwidth * Config.BANDWIDTH_VARIANCE < playerBandwidth;
|
14455 | });
|
14456 | var highestRemainingBandwidthRep = bandwidthPlaylistReps[bandwidthPlaylistReps.length - 1];
|
14457 |
|
14458 |
|
14459 | var bandwidthBestRep = bandwidthPlaylistReps.filter(function (rep) {
|
14460 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
14461 | })[0];
|
14462 |
|
14463 | if (limitRenditionByPlayerDimensions === false) {
|
14464 | var _chosenRep = bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
|
14465 |
|
14466 | if (_chosenRep && _chosenRep.playlist) {
|
14467 | var type = 'sortedPlaylistReps';
|
14468 |
|
14469 | if (bandwidthBestRep) {
|
14470 | type = 'bandwidthBestRep';
|
14471 | }
|
14472 |
|
14473 | if (enabledPlaylistReps[0]) {
|
14474 | type = 'enabledPlaylistReps';
|
14475 | }
|
14476 |
|
14477 | logFn("choosing " + representationToString(_chosenRep) + " using " + type + " with options", options);
|
14478 | return _chosenRep.playlist;
|
14479 | }
|
14480 |
|
14481 | logFn('could not choose a playlist with options', options);
|
14482 | return null;
|
14483 | }
|
14484 |
|
14485 |
|
14486 | var haveResolution = bandwidthPlaylistReps.filter(function (rep) {
|
14487 | return rep.width && rep.height;
|
14488 | });
|
14489 |
|
14490 | stableSort(haveResolution, function (left, right) {
|
14491 | return left.width - right.width;
|
14492 | });
|
14493 |
|
14494 | var resolutionBestRepList = haveResolution.filter(function (rep) {
|
14495 | return rep.width === playerWidth && rep.height === playerHeight;
|
14496 | });
|
14497 | highestRemainingBandwidthRep = resolutionBestRepList[resolutionBestRepList.length - 1];
|
14498 |
|
14499 | var resolutionBestRep = resolutionBestRepList.filter(function (rep) {
|
14500 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
14501 | })[0];
|
14502 | var resolutionPlusOneList;
|
14503 | var resolutionPlusOneSmallest;
|
14504 | var resolutionPlusOneRep;
|
14505 |
|
14506 |
|
14507 | if (!resolutionBestRep) {
|
14508 | resolutionPlusOneList = haveResolution.filter(function (rep) {
|
14509 | return rep.width > playerWidth || rep.height > playerHeight;
|
14510 | });
|
14511 |
|
14512 | resolutionPlusOneSmallest = resolutionPlusOneList.filter(function (rep) {
|
14513 | return rep.width === resolutionPlusOneList[0].width && rep.height === resolutionPlusOneList[0].height;
|
14514 | });
|
14515 |
|
14516 |
|
14517 | highestRemainingBandwidthRep = resolutionPlusOneSmallest[resolutionPlusOneSmallest.length - 1];
|
14518 | resolutionPlusOneRep = resolutionPlusOneSmallest.filter(function (rep) {
|
14519 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
14520 | })[0];
|
14521 | }
|
14522 |
|
14523 | var leastPixelDiffRep;
|
14524 |
|
14525 |
|
14526 |
|
14527 | if (masterPlaylistController.experimentalLeastPixelDiffSelector) {
|
14528 |
|
14529 | var leastPixelDiffList = haveResolution.map(function (rep) {
|
14530 | rep.pixelDiff = Math.abs(rep.width - playerWidth) + Math.abs(rep.height - playerHeight);
|
14531 | return rep;
|
14532 | });
|
14533 |
|
14534 | stableSort(leastPixelDiffList, function (left, right) {
|
14535 |
|
14536 | if (left.pixelDiff === right.pixelDiff) {
|
14537 | return right.bandwidth - left.bandwidth;
|
14538 | }
|
14539 |
|
14540 | return left.pixelDiff - right.pixelDiff;
|
14541 | });
|
14542 | leastPixelDiffRep = leastPixelDiffList[0];
|
14543 | }
|
14544 |
|
14545 |
|
14546 | var chosenRep = leastPixelDiffRep || resolutionPlusOneRep || resolutionBestRep || bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
|
14547 |
|
14548 | if (chosenRep && chosenRep.playlist) {
|
14549 | var _type = 'sortedPlaylistReps';
|
14550 |
|
14551 | if (leastPixelDiffRep) {
|
14552 | _type = 'leastPixelDiffRep';
|
14553 | } else if (resolutionPlusOneRep) {
|
14554 | _type = 'resolutionPlusOneRep';
|
14555 | } else if (resolutionBestRep) {
|
14556 | _type = 'resolutionBestRep';
|
14557 | } else if (bandwidthBestRep) {
|
14558 | _type = 'bandwidthBestRep';
|
14559 | } else if (enabledPlaylistReps[0]) {
|
14560 | _type = 'enabledPlaylistReps';
|
14561 | }
|
14562 |
|
14563 | logFn("choosing " + representationToString(chosenRep) + " using " + _type + " with options", options);
|
14564 | return chosenRep.playlist;
|
14565 | }
|
14566 |
|
14567 | logFn('could not choose a playlist with options', options);
|
14568 | return null;
|
14569 | };
|
14570 |
|
14571 |
|
14572 |
|
14573 |
|
14574 |
|
14575 |
|
14576 |
|
14577 |
|
14578 |
|
14579 |
|
14580 |
|
14581 |
|
14582 | var lastBandwidthSelector = function lastBandwidthSelector() {
|
14583 | var pixelRatio = this.useDevicePixelRatio ? window.devicePixelRatio || 1 : 1;
|
14584 | return simpleSelector(this.playlists.master, this.systemBandwidth, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions, this.masterPlaylistController_);
|
14585 | };
|
14586 |
|
14587 |
|
14588 |
|
14589 |
|
14590 |
|
14591 |
|
14592 |
|
14593 |
|
14594 |
|
14595 |
|
14596 |
|
14597 |
|
14598 |
|
14599 |
|
14600 |
|
14601 | var movingAverageBandwidthSelector = function movingAverageBandwidthSelector(decay) {
|
14602 | var average = -1;
|
14603 | var lastSystemBandwidth = -1;
|
14604 |
|
14605 | if (decay < 0 || decay > 1) {
|
14606 | throw new Error('Moving average bandwidth decay must be between 0 and 1.');
|
14607 | }
|
14608 |
|
14609 | return function () {
|
14610 | var pixelRatio = this.useDevicePixelRatio ? window.devicePixelRatio || 1 : 1;
|
14611 |
|
14612 | if (average < 0) {
|
14613 | average = this.systemBandwidth;
|
14614 | lastSystemBandwidth = this.systemBandwidth;
|
14615 | }
|
14616 |
|
14617 |
|
14618 |
|
14619 |
|
14620 |
|
14621 |
|
14622 | if (this.systemBandwidth > 0 && this.systemBandwidth !== lastSystemBandwidth) {
|
14623 | average = decay * this.systemBandwidth + (1 - decay) * average;
|
14624 | lastSystemBandwidth = this.systemBandwidth;
|
14625 | }
|
14626 |
|
14627 | return simpleSelector(this.playlists.master, average, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions, this.masterPlaylistController_);
|
14628 | };
|
14629 | };
|
14630 |
|
14631 |
|
14632 |
|
14633 |
|
14634 |
|
14635 |
|
14636 |
|
14637 |
|
14638 |
|
14639 |
|
14640 |
|
14641 |
|
14642 |
|
14643 |
|
14644 |
|
14645 |
|
14646 |
|
14647 |
|
14648 |
|
14649 |
|
14650 |
|
14651 |
|
14652 |
|
14653 |
|
14654 |
|
14655 |
|
14656 |
|
14657 |
|
14658 |
|
14659 | var minRebufferMaxBandwidthSelector = function minRebufferMaxBandwidthSelector(settings) {
|
14660 | var master = settings.master,
|
14661 | currentTime = settings.currentTime,
|
14662 | bandwidth = settings.bandwidth,
|
14663 | duration = settings.duration,
|
14664 | segmentDuration = settings.segmentDuration,
|
14665 | timeUntilRebuffer = settings.timeUntilRebuffer,
|
14666 | currentTimeline = settings.currentTimeline,
|
14667 | syncController = settings.syncController;
|
14668 |
|
14669 |
|
14670 | var compatiblePlaylists = master.playlists.filter(function (playlist) {
|
14671 | return !Playlist.isIncompatible(playlist);
|
14672 | });
|
14673 |
|
14674 |
|
14675 | var enabledPlaylists = compatiblePlaylists.filter(Playlist.isEnabled);
|
14676 |
|
14677 | if (!enabledPlaylists.length) {
|
14678 |
|
14679 |
|
14680 |
|
14681 | enabledPlaylists = compatiblePlaylists.filter(function (playlist) {
|
14682 | return !Playlist.isDisabled(playlist);
|
14683 | });
|
14684 | }
|
14685 |
|
14686 | var bandwidthPlaylists = enabledPlaylists.filter(Playlist.hasAttribute.bind(null, 'BANDWIDTH'));
|
14687 | var rebufferingEstimates = bandwidthPlaylists.map(function (playlist) {
|
14688 | var syncPoint = syncController.getSyncPoint(playlist, duration, currentTimeline, currentTime);
|
14689 |
|
14690 |
|
14691 | var numRequests = syncPoint ? 1 : 2;
|
14692 | var requestTimeEstimate = Playlist.estimateSegmentRequestTime(segmentDuration, bandwidth, playlist);
|
14693 | var rebufferingImpact = requestTimeEstimate * numRequests - timeUntilRebuffer;
|
14694 | return {
|
14695 | playlist: playlist,
|
14696 | rebufferingImpact: rebufferingImpact
|
14697 | };
|
14698 | });
|
14699 | var noRebufferingPlaylists = rebufferingEstimates.filter(function (estimate) {
|
14700 | return estimate.rebufferingImpact <= 0;
|
14701 | });
|
14702 |
|
14703 | stableSort(noRebufferingPlaylists, function (a, b) {
|
14704 | return comparePlaylistBandwidth(b.playlist, a.playlist);
|
14705 | });
|
14706 |
|
14707 | if (noRebufferingPlaylists.length) {
|
14708 | return noRebufferingPlaylists[0];
|
14709 | }
|
14710 |
|
14711 | stableSort(rebufferingEstimates, function (a, b) {
|
14712 | return a.rebufferingImpact - b.rebufferingImpact;
|
14713 | });
|
14714 | return rebufferingEstimates[0] || null;
|
14715 | };
|
14716 |
|
14717 |
|
14718 |
|
14719 |
|
14720 |
|
14721 |
|
14722 |
|
14723 |
|
14724 |
|
14725 |
|
14726 |
|
14727 |
|
14728 | var lowestBitrateCompatibleVariantSelector = function lowestBitrateCompatibleVariantSelector() {
|
14729 | var _this = this;
|
14730 |
|
14731 |
|
14732 |
|
14733 | var playlists = this.playlists.master.playlists.filter(Playlist.isEnabled);
|
14734 |
|
14735 | stableSort(playlists, function (a, b) {
|
14736 | return comparePlaylistBandwidth(a, b);
|
14737 | });
|
14738 |
|
14739 |
|
14740 |
|
14741 |
|
14742 |
|
14743 | var playlistsWithVideo = playlists.filter(function (playlist) {
|
14744 | return !!codecsForPlaylist(_this.playlists.master, playlist).video;
|
14745 | });
|
14746 | return playlistsWithVideo[0] || null;
|
14747 | };
|
14748 |
|
14749 |
|
14750 |
|
14751 |
|
14752 |
|
14753 |
|
14754 |
|
14755 |
|
14756 | var concatSegments = function concatSegments(segmentObj) {
|
14757 | var offset = 0;
|
14758 | var tempBuffer;
|
14759 |
|
14760 | if (segmentObj.bytes) {
|
14761 | tempBuffer = new Uint8Array(segmentObj.bytes);
|
14762 |
|
14763 | segmentObj.segments.forEach(function (segment) {
|
14764 | tempBuffer.set(segment, offset);
|
14765 | offset += segment.byteLength;
|
14766 | });
|
14767 | }
|
14768 |
|
14769 | return tempBuffer;
|
14770 | };
|
14771 |
|
14772 |
|
14773 |
|
14774 |
|
14775 |
|
14776 |
|
14777 |
|
14778 |
|
14779 |
|
14780 |
|
14781 |
|
14782 |
|
14783 |
|
14784 | var createCaptionsTrackIfNotExists = function createCaptionsTrackIfNotExists(inbandTextTracks, tech, captionStream) {
|
14785 | if (!inbandTextTracks[captionStream]) {
|
14786 | tech.trigger({
|
14787 | type: 'usage',
|
14788 | name: 'vhs-608'
|
14789 | });
|
14790 | tech.trigger({
|
14791 | type: 'usage',
|
14792 | name: 'hls-608'
|
14793 | });
|
14794 | var instreamId = captionStream;
|
14795 |
|
14796 | if (/^cc708_/.test(captionStream)) {
|
14797 | instreamId = 'SERVICE' + captionStream.split('_')[1];
|
14798 | }
|
14799 |
|
14800 | var track = tech.textTracks().getTrackById(instreamId);
|
14801 |
|
14802 | if (track) {
|
14803 |
|
14804 |
|
14805 |
|
14806 | inbandTextTracks[captionStream] = track;
|
14807 | } else {
|
14808 |
|
14809 |
|
14810 | var captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
|
14811 | var label = captionStream;
|
14812 | var language = captionStream;
|
14813 | var def = false;
|
14814 | var captionService = captionServices[instreamId];
|
14815 |
|
14816 | if (captionService) {
|
14817 | label = captionService.label;
|
14818 | language = captionService.language;
|
14819 | def = captionService.default;
|
14820 | }
|
14821 |
|
14822 |
|
14823 |
|
14824 | inbandTextTracks[captionStream] = tech.addRemoteTextTrack({
|
14825 | kind: 'captions',
|
14826 | id: instreamId,
|
14827 |
|
14828 | default: def,
|
14829 | label: label,
|
14830 | language: language
|
14831 | }, false).track;
|
14832 | }
|
14833 | }
|
14834 | };
|
14835 |
|
14836 |
|
14837 |
|
14838 |
|
14839 |
|
14840 |
|
14841 |
|
14842 |
|
14843 |
|
14844 |
|
14845 | var addCaptionData = function addCaptionData(_ref) {
|
14846 | var inbandTextTracks = _ref.inbandTextTracks,
|
14847 | captionArray = _ref.captionArray,
|
14848 | timestampOffset = _ref.timestampOffset;
|
14849 |
|
14850 | if (!captionArray) {
|
14851 | return;
|
14852 | }
|
14853 |
|
14854 | var Cue = window.WebKitDataCue || window.VTTCue;
|
14855 | captionArray.forEach(function (caption) {
|
14856 | var track = caption.stream;
|
14857 | inbandTextTracks[track].addCue(new Cue(caption.startTime + timestampOffset, caption.endTime + timestampOffset, caption.text));
|
14858 | });
|
14859 | };
|
14860 |
|
14861 |
|
14862 |
|
14863 |
|
14864 |
|
14865 |
|
14866 |
|
14867 |
|
14868 |
|
14869 | var deprecateOldCue = function deprecateOldCue(cue) {
|
14870 | Object.defineProperties(cue.frame, {
|
14871 | id: {
|
14872 | get: function get() {
|
14873 | videojs.log.warn('cue.frame.id is deprecated. Use cue.value.key instead.');
|
14874 | return cue.value.key;
|
14875 | }
|
14876 | },
|
14877 | value: {
|
14878 | get: function get() {
|
14879 | videojs.log.warn('cue.frame.value is deprecated. Use cue.value.data instead.');
|
14880 | return cue.value.data;
|
14881 | }
|
14882 | },
|
14883 | privateData: {
|
14884 | get: function get() {
|
14885 | videojs.log.warn('cue.frame.privateData is deprecated. Use cue.value.data instead.');
|
14886 | return cue.value.data;
|
14887 | }
|
14888 | }
|
14889 | });
|
14890 | };
|
14891 |
|
14892 |
|
14893 |
|
14894 |
|
14895 |
|
14896 |
|
14897 |
|
14898 |
|
14899 |
|
14900 |
|
14901 |
|
14902 |
|
14903 | var addMetadata = function addMetadata(_ref2) {
|
14904 | var inbandTextTracks = _ref2.inbandTextTracks,
|
14905 | metadataArray = _ref2.metadataArray,
|
14906 | timestampOffset = _ref2.timestampOffset,
|
14907 | videoDuration = _ref2.videoDuration;
|
14908 |
|
14909 | if (!metadataArray) {
|
14910 | return;
|
14911 | }
|
14912 |
|
14913 | var Cue = window.WebKitDataCue || window.VTTCue;
|
14914 | var metadataTrack = inbandTextTracks.metadataTrack_;
|
14915 |
|
14916 | if (!metadataTrack) {
|
14917 | return;
|
14918 | }
|
14919 |
|
14920 | metadataArray.forEach(function (metadata) {
|
14921 | var time = metadata.cueTime + timestampOffset;
|
14922 |
|
14923 |
|
14924 |
|
14925 |
|
14926 | if (typeof time !== 'number' || window.isNaN(time) || time < 0 || !(time < Infinity)) {
|
14927 | return;
|
14928 | }
|
14929 |
|
14930 | metadata.frames.forEach(function (frame) {
|
14931 | var cue = new Cue(time, time, frame.value || frame.url || frame.data || '');
|
14932 | cue.frame = frame;
|
14933 | cue.value = frame;
|
14934 | deprecateOldCue(cue);
|
14935 | metadataTrack.addCue(cue);
|
14936 | });
|
14937 | });
|
14938 |
|
14939 | if (!metadataTrack.cues || !metadataTrack.cues.length) {
|
14940 | return;
|
14941 | }
|
14942 |
|
14943 |
|
14944 |
|
14945 |
|
14946 | var cues = metadataTrack.cues;
|
14947 | var cuesArray = [];
|
14948 |
|
14949 |
|
14950 | for (var i = 0; i < cues.length; i++) {
|
14951 | if (cues[i]) {
|
14952 | cuesArray.push(cues[i]);
|
14953 | }
|
14954 | }
|
14955 |
|
14956 |
|
14957 | var cuesGroupedByStartTime = cuesArray.reduce(function (obj, cue) {
|
14958 | var timeSlot = obj[cue.startTime] || [];
|
14959 | timeSlot.push(cue);
|
14960 | obj[cue.startTime] = timeSlot;
|
14961 | return obj;
|
14962 | }, {});
|
14963 |
|
14964 | var sortedStartTimes = Object.keys(cuesGroupedByStartTime).sort(function (a, b) {
|
14965 | return Number(a) - Number(b);
|
14966 | });
|
14967 |
|
14968 | sortedStartTimes.forEach(function (startTime, idx) {
|
14969 | var cueGroup = cuesGroupedByStartTime[startTime];
|
14970 | var nextTime = Number(sortedStartTimes[idx + 1]) || videoDuration;
|
14971 |
|
14972 | cueGroup.forEach(function (cue) {
|
14973 | cue.endTime = nextTime;
|
14974 | });
|
14975 | });
|
14976 | };
|
14977 |
|
14978 |
|
14979 |
|
14980 |
|
14981 |
|
14982 |
|
14983 |
|
14984 |
|
14985 |
|
14986 | var createMetadataTrackIfNotExists = function createMetadataTrackIfNotExists(inbandTextTracks, dispatchType, tech) {
|
14987 | if (inbandTextTracks.metadataTrack_) {
|
14988 | return;
|
14989 | }
|
14990 |
|
14991 | inbandTextTracks.metadataTrack_ = tech.addRemoteTextTrack({
|
14992 | kind: 'metadata',
|
14993 | label: 'Timed Metadata'
|
14994 | }, false).track;
|
14995 | inbandTextTracks.metadataTrack_.inBandMetadataTrackDispatchType = dispatchType;
|
14996 | };
|
14997 |
|
14998 |
|
14999 |
|
15000 |
|
15001 |
|
15002 |
|
15003 |
|
15004 |
|
15005 |
|
15006 | var removeCuesFromTrack = function removeCuesFromTrack(start, end, track) {
|
15007 | var i;
|
15008 | var cue;
|
15009 |
|
15010 | if (!track) {
|
15011 | return;
|
15012 | }
|
15013 |
|
15014 | if (!track.cues) {
|
15015 | return;
|
15016 | }
|
15017 |
|
15018 | i = track.cues.length;
|
15019 |
|
15020 | while (i--) {
|
15021 | cue = track.cues[i];
|
15022 |
|
15023 | if (cue.startTime >= start && cue.endTime <= end) {
|
15024 | track.removeCue(cue);
|
15025 | }
|
15026 | }
|
15027 | };
|
15028 |
|
15029 |
|
15030 |
|
15031 |
|
15032 |
|
15033 |
|
15034 |
|
15035 |
|
15036 | var removeDuplicateCuesFromTrack = function removeDuplicateCuesFromTrack(track) {
|
15037 | var cues = track.cues;
|
15038 |
|
15039 | if (!cues) {
|
15040 | return;
|
15041 | }
|
15042 |
|
15043 | for (var i = 0; i < cues.length; i++) {
|
15044 | var duplicates = [];
|
15045 | var occurrences = 0;
|
15046 |
|
15047 | for (var j = 0; j < cues.length; j++) {
|
15048 | if (cues[i].startTime === cues[j].startTime && cues[i].endTime === cues[j].endTime && cues[i].text === cues[j].text) {
|
15049 | occurrences++;
|
15050 |
|
15051 | if (occurrences > 1) {
|
15052 | duplicates.push(cues[j]);
|
15053 | }
|
15054 | }
|
15055 | }
|
15056 |
|
15057 | if (duplicates.length) {
|
15058 | duplicates.forEach(function (dupe) {
|
15059 | return track.removeCue(dupe);
|
15060 | });
|
15061 | }
|
15062 | }
|
15063 | };
|
15064 |
|
15065 |
|
15066 |
|
15067 |
|
15068 |
|
15069 |
|
15070 |
|
15071 |
|
15072 |
|
15073 |
|
15074 |
|
15075 |
|
15076 |
|
15077 |
|
15078 |
|
15079 | var gopsSafeToAlignWith = function gopsSafeToAlignWith(buffer, currentTime, mapping) {
|
15080 | if (typeof currentTime === 'undefined' || currentTime === null || !buffer.length) {
|
15081 | return [];
|
15082 | }
|
15083 |
|
15084 |
|
15085 | var currentTimePts = Math.ceil((currentTime - mapping + 3) * ONE_SECOND_IN_TS);
|
15086 | var i;
|
15087 |
|
15088 | for (i = 0; i < buffer.length; i++) {
|
15089 | if (buffer[i].pts > currentTimePts) {
|
15090 | break;
|
15091 | }
|
15092 | }
|
15093 |
|
15094 | return buffer.slice(i);
|
15095 | };
|
15096 |
|
15097 |
|
15098 |
|
15099 |
|
15100 |
|
15101 |
|
15102 |
|
15103 |
|
15104 |
|
15105 |
|
15106 |
|
15107 |
|
15108 |
|
15109 |
|
15110 |
|
15111 | var updateGopBuffer = function updateGopBuffer(buffer, gops, replace) {
|
15112 | if (!gops.length) {
|
15113 | return buffer;
|
15114 | }
|
15115 |
|
15116 | if (replace) {
|
15117 |
|
15118 |
|
15119 |
|
15120 |
|
15121 | return gops.slice();
|
15122 | }
|
15123 |
|
15124 | var start = gops[0].pts;
|
15125 | var i = 0;
|
15126 |
|
15127 | for (i; i < buffer.length; i++) {
|
15128 | if (buffer[i].pts >= start) {
|
15129 | break;
|
15130 | }
|
15131 | }
|
15132 |
|
15133 | return buffer.slice(0, i).concat(gops);
|
15134 | };
|
15135 |
|
15136 |
|
15137 |
|
15138 |
|
15139 |
|
15140 |
|
15141 |
|
15142 |
|
15143 |
|
15144 |
|
15145 |
|
15146 |
|
15147 |
|
15148 | var removeGopBuffer = function removeGopBuffer(buffer, start, end, mapping) {
|
15149 | var startPts = Math.ceil((start - mapping) * ONE_SECOND_IN_TS);
|
15150 | var endPts = Math.ceil((end - mapping) * ONE_SECOND_IN_TS);
|
15151 | var updatedBuffer = buffer.slice();
|
15152 | var i = buffer.length;
|
15153 |
|
15154 | while (i--) {
|
15155 | if (buffer[i].pts <= endPts) {
|
15156 | break;
|
15157 | }
|
15158 | }
|
15159 |
|
15160 | if (i === -1) {
|
15161 |
|
15162 | return updatedBuffer;
|
15163 | }
|
15164 |
|
15165 | var j = i + 1;
|
15166 |
|
15167 | while (j--) {
|
15168 | if (buffer[j].pts <= startPts) {
|
15169 | break;
|
15170 | }
|
15171 | }
|
15172 |
|
15173 |
|
15174 | j = Math.max(j, 0);
|
15175 | updatedBuffer.splice(j, i - j + 1);
|
15176 | return updatedBuffer;
|
15177 | };
|
15178 |
|
15179 | var shallowEqual = function shallowEqual(a, b) {
|
15180 |
|
15181 |
|
15182 |
|
15183 | if (!a && !b || !a && b || a && !b) {
|
15184 | return false;
|
15185 | }
|
15186 |
|
15187 |
|
15188 | if (a === b) {
|
15189 | return true;
|
15190 | }
|
15191 |
|
15192 |
|
15193 |
|
15194 | var akeys = Object.keys(a).sort();
|
15195 | var bkeys = Object.keys(b).sort();
|
15196 |
|
15197 | if (akeys.length !== bkeys.length) {
|
15198 | return false;
|
15199 | }
|
15200 |
|
15201 | for (var i = 0; i < akeys.length; i++) {
|
15202 | var key = akeys[i];
|
15203 |
|
15204 | if (key !== bkeys[i]) {
|
15205 | return false;
|
15206 | }
|
15207 |
|
15208 |
|
15209 | if (a[key] !== b[key]) {
|
15210 | return false;
|
15211 | }
|
15212 | }
|
15213 |
|
15214 | return true;
|
15215 | };
|
15216 |
|
15217 |
|
15218 | var QUOTA_EXCEEDED_ERR = 22;
|
15219 |
|
15220 |
|
15221 |
|
15222 |
|
15223 |
|
15224 |
|
15225 |
|
15226 |
|
15227 |
|
15228 |
|
15229 |
|
15230 | var getSyncSegmentCandidate = function getSyncSegmentCandidate(currentTimeline, segments, targetTime) {
|
15231 | segments = segments || [];
|
15232 | var timelineSegments = [];
|
15233 | var time = 0;
|
15234 |
|
15235 | for (var i = 0; i < segments.length; i++) {
|
15236 | var segment = segments[i];
|
15237 |
|
15238 | if (currentTimeline === segment.timeline) {
|
15239 | timelineSegments.push(i);
|
15240 | time += segment.duration;
|
15241 |
|
15242 | if (time > targetTime) {
|
15243 | return i;
|
15244 | }
|
15245 | }
|
15246 | }
|
15247 |
|
15248 | if (timelineSegments.length === 0) {
|
15249 | return 0;
|
15250 | }
|
15251 |
|
15252 |
|
15253 | return timelineSegments[timelineSegments.length - 1];
|
15254 | };
|
15255 |
|
15256 |
|
15257 |
|
15258 |
|
15259 | var MIN_BACK_BUFFER = 1;
|
15260 |
|
15261 | var CHECK_BUFFER_DELAY = 500;
|
15262 |
|
15263 | var finite = function finite(num) {
|
15264 | return typeof num === 'number' && isFinite(num);
|
15265 | };
|
15266 |
|
15267 |
|
15268 |
|
15269 |
|
15270 | var MIN_SEGMENT_DURATION_TO_SAVE_STATS = 1 / 60;
|
15271 | var illegalMediaSwitch = function illegalMediaSwitch(loaderType, startingMedia, trackInfo) {
|
15272 |
|
15273 |
|
15274 | if (loaderType !== 'main' || !startingMedia || !trackInfo) {
|
15275 | return null;
|
15276 | }
|
15277 |
|
15278 | if (!trackInfo.hasAudio && !trackInfo.hasVideo) {
|
15279 | return 'Neither audio nor video found in segment.';
|
15280 | }
|
15281 |
|
15282 | if (startingMedia.hasVideo && !trackInfo.hasVideo) {
|
15283 | 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.';
|
15284 | }
|
15285 |
|
15286 | if (!startingMedia.hasVideo && trackInfo.hasVideo) {
|
15287 | 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.';
|
15288 | }
|
15289 |
|
15290 | return null;
|
15291 | };
|
15292 |
|
15293 |
|
15294 |
|
15295 |
|
15296 |
|
15297 |
|
15298 |
|
15299 |
|
15300 |
|
15301 |
|
15302 |
|
15303 |
|
15304 |
|
15305 |
|
15306 | var safeBackBufferTrimTime = function safeBackBufferTrimTime(seekable, currentTime, targetDuration) {
|
15307 |
|
15308 |
|
15309 |
|
15310 |
|
15311 |
|
15312 | var trimTime = currentTime - Config.BACK_BUFFER_LENGTH;
|
15313 |
|
15314 | if (seekable.length) {
|
15315 |
|
15316 |
|
15317 | trimTime = Math.max(trimTime, seekable.start(0));
|
15318 | }
|
15319 |
|
15320 |
|
15321 |
|
15322 | var maxTrimTime = currentTime - targetDuration;
|
15323 | return Math.min(maxTrimTime, trimTime);
|
15324 | };
|
15325 |
|
15326 | var segmentInfoString = function segmentInfoString(segmentInfo) {
|
15327 | var startOfSegment = segmentInfo.startOfSegment,
|
15328 | duration = segmentInfo.duration,
|
15329 | segment = segmentInfo.segment,
|
15330 | part = segmentInfo.part,
|
15331 | _segmentInfo$playlist = segmentInfo.playlist,
|
15332 | seq = _segmentInfo$playlist.mediaSequence,
|
15333 | id = _segmentInfo$playlist.id,
|
15334 | _segmentInfo$playlist2 = _segmentInfo$playlist.segments,
|
15335 | segments = _segmentInfo$playlist2 === void 0 ? [] : _segmentInfo$playlist2,
|
15336 | index = segmentInfo.mediaIndex,
|
15337 | partIndex = segmentInfo.partIndex,
|
15338 | timeline = segmentInfo.timeline;
|
15339 | var segmentLen = segments.length - 1;
|
15340 | var selection = 'mediaIndex/partIndex increment';
|
15341 |
|
15342 | if (segmentInfo.getMediaInfoForTime) {
|
15343 | selection = "getMediaInfoForTime (" + segmentInfo.getMediaInfoForTime + ")";
|
15344 | } else if (segmentInfo.isSyncRequest) {
|
15345 | selection = 'getSyncSegmentCandidate (isSyncRequest)';
|
15346 | }
|
15347 |
|
15348 | var hasPartIndex = typeof partIndex === 'number';
|
15349 | var name = segmentInfo.segment.uri ? 'segment' : 'pre-segment';
|
15350 | var zeroBasedPartCount = hasPartIndex ? getKnownPartCount({
|
15351 | preloadSegment: segment
|
15352 | }) - 1 : 0;
|
15353 | return name + " [" + (seq + index) + "/" + (seq + segmentLen) + "]" + (hasPartIndex ? " part [" + partIndex + "/" + zeroBasedPartCount + "]" : '') + (" segment start/end [" + segment.start + " => " + segment.end + "]") + (hasPartIndex ? " part start/end [" + part.start + " => " + part.end + "]" : '') + (" startOfSegment [" + startOfSegment + "]") + (" duration [" + duration + "]") + (" timeline [" + timeline + "]") + (" selected by [" + selection + "]") + (" playlist [" + id + "]");
|
15354 | };
|
15355 |
|
15356 | var timingInfoPropertyForMedia = function timingInfoPropertyForMedia(mediaType) {
|
15357 | return mediaType + "TimingInfo";
|
15358 | };
|
15359 |
|
15360 |
|
15361 |
|
15362 |
|
15363 |
|
15364 |
|
15365 |
|
15366 |
|
15367 |
|
15368 |
|
15369 |
|
15370 |
|
15371 |
|
15372 |
|
15373 |
|
15374 |
|
15375 |
|
15376 |
|
15377 |
|
15378 |
|
15379 |
|
15380 | var timestampOffsetForSegment = function timestampOffsetForSegment(_ref) {
|
15381 | var segmentTimeline = _ref.segmentTimeline,
|
15382 | currentTimeline = _ref.currentTimeline,
|
15383 | startOfSegment = _ref.startOfSegment,
|
15384 | buffered = _ref.buffered,
|
15385 | overrideCheck = _ref.overrideCheck;
|
15386 |
|
15387 |
|
15388 |
|
15389 |
|
15390 |
|
15391 |
|
15392 |
|
15393 | if (!overrideCheck && segmentTimeline === currentTimeline) {
|
15394 | return null;
|
15395 | }
|
15396 |
|
15397 |
|
15398 |
|
15399 |
|
15400 |
|
15401 |
|
15402 |
|
15403 |
|
15404 |
|
15405 |
|
15406 |
|
15407 |
|
15408 |
|
15409 |
|
15410 |
|
15411 |
|
15412 |
|
15413 |
|
15414 |
|
15415 |
|
15416 |
|
15417 |
|
15418 |
|
15419 |
|
15420 |
|
15421 | if (segmentTimeline < currentTimeline) {
|
15422 | return startOfSegment;
|
15423 | }
|
15424 |
|
15425 |
|
15426 |
|
15427 |
|
15428 |
|
15429 |
|
15430 | return buffered.length ? buffered.end(buffered.length - 1) : startOfSegment;
|
15431 | };
|
15432 |
|
15433 |
|
15434 |
|
15435 |
|
15436 |
|
15437 |
|
15438 |
|
15439 |
|
15440 |
|
15441 |
|
15442 |
|
15443 |
|
15444 |
|
15445 |
|
15446 |
|
15447 |
|
15448 |
|
15449 |
|
15450 |
|
15451 |
|
15452 |
|
15453 |
|
15454 |
|
15455 |
|
15456 |
|
15457 |
|
15458 |
|
15459 |
|
15460 |
|
15461 |
|
15462 |
|
15463 |
|
15464 |
|
15465 |
|
15466 |
|
15467 |
|
15468 |
|
15469 |
|
15470 |
|
15471 |
|
15472 |
|
15473 |
|
15474 |
|
15475 |
|
15476 |
|
15477 |
|
15478 |
|
15479 |
|
15480 |
|
15481 |
|
15482 |
|
15483 |
|
15484 |
|
15485 |
|
15486 |
|
15487 |
|
15488 |
|
15489 |
|
15490 |
|
15491 |
|
15492 |
|
15493 |
|
15494 |
|
15495 |
|
15496 |
|
15497 |
|
15498 |
|
15499 |
|
15500 |
|
15501 |
|
15502 |
|
15503 |
|
15504 |
|
15505 |
|
15506 |
|
15507 |
|
15508 |
|
15509 |
|
15510 |
|
15511 |
|
15512 |
|
15513 |
|
15514 |
|
15515 |
|
15516 |
|
15517 |
|
15518 |
|
15519 |
|
15520 |
|
15521 |
|
15522 |
|
15523 |
|
15524 |
|
15525 |
|
15526 |
|
15527 |
|
15528 | var shouldWaitForTimelineChange = function shouldWaitForTimelineChange(_ref2) {
|
15529 | var timelineChangeController = _ref2.timelineChangeController,
|
15530 | currentTimeline = _ref2.currentTimeline,
|
15531 | segmentTimeline = _ref2.segmentTimeline,
|
15532 | loaderType = _ref2.loaderType,
|
15533 | audioDisabled = _ref2.audioDisabled;
|
15534 |
|
15535 | if (currentTimeline === segmentTimeline) {
|
15536 | return false;
|
15537 | }
|
15538 |
|
15539 | if (loaderType === 'audio') {
|
15540 | var lastMainTimelineChange = timelineChangeController.lastTimelineChange({
|
15541 | type: 'main'
|
15542 | });
|
15543 |
|
15544 |
|
15545 |
|
15546 |
|
15547 | return !lastMainTimelineChange || lastMainTimelineChange.to !== segmentTimeline;
|
15548 | }
|
15549 |
|
15550 |
|
15551 |
|
15552 |
|
15553 |
|
15554 | if (loaderType === 'main' && audioDisabled) {
|
15555 | var pendingAudioTimelineChange = timelineChangeController.pendingTimelineChange({
|
15556 | type: 'audio'
|
15557 | });
|
15558 |
|
15559 |
|
15560 |
|
15561 |
|
15562 |
|
15563 |
|
15564 |
|
15565 |
|
15566 |
|
15567 |
|
15568 |
|
15569 |
|
15570 |
|
15571 |
|
15572 |
|
15573 |
|
15574 |
|
15575 |
|
15576 | if (pendingAudioTimelineChange && pendingAudioTimelineChange.to === segmentTimeline) {
|
15577 | return false;
|
15578 | }
|
15579 |
|
15580 | return true;
|
15581 | }
|
15582 |
|
15583 | return false;
|
15584 | };
|
15585 | var mediaDuration = function mediaDuration(audioTimingInfo, videoTimingInfo) {
|
15586 | var audioDuration = audioTimingInfo && typeof audioTimingInfo.start === 'number' && typeof audioTimingInfo.end === 'number' ? audioTimingInfo.end - audioTimingInfo.start : 0;
|
15587 | var videoDuration = videoTimingInfo && typeof videoTimingInfo.start === 'number' && typeof videoTimingInfo.end === 'number' ? videoTimingInfo.end - videoTimingInfo.start : 0;
|
15588 | return Math.max(audioDuration, videoDuration);
|
15589 | };
|
15590 | var segmentTooLong = function segmentTooLong(_ref3) {
|
15591 | var segmentDuration = _ref3.segmentDuration,
|
15592 | maxDuration = _ref3.maxDuration;
|
15593 |
|
15594 |
|
15595 |
|
15596 | if (!segmentDuration) {
|
15597 | return false;
|
15598 | }
|
15599 |
|
15600 |
|
15601 |
|
15602 |
|
15603 |
|
15604 |
|
15605 |
|
15606 |
|
15607 |
|
15608 |
|
15609 |
|
15610 |
|
15611 | return Math.round(segmentDuration) > maxDuration + TIME_FUDGE_FACTOR;
|
15612 | };
|
15613 | var getTroublesomeSegmentDurationMessage = function getTroublesomeSegmentDurationMessage(segmentInfo, sourceType) {
|
15614 |
|
15615 |
|
15616 | if (sourceType !== 'hls') {
|
15617 | return null;
|
15618 | }
|
15619 |
|
15620 | var segmentDuration = mediaDuration(segmentInfo.audioTimingInfo, segmentInfo.videoTimingInfo);
|
15621 |
|
15622 |
|
15623 |
|
15624 |
|
15625 | if (!segmentDuration) {
|
15626 | return null;
|
15627 | }
|
15628 |
|
15629 | var targetDuration = segmentInfo.playlist.targetDuration;
|
15630 | var isSegmentWayTooLong = segmentTooLong({
|
15631 | segmentDuration: segmentDuration,
|
15632 | maxDuration: targetDuration * 2
|
15633 | });
|
15634 | var isSegmentSlightlyTooLong = segmentTooLong({
|
15635 | segmentDuration: segmentDuration,
|
15636 | maxDuration: targetDuration
|
15637 | });
|
15638 | var segmentTooLongMessage = "Segment with index " + segmentInfo.mediaIndex + " " + ("from playlist " + segmentInfo.playlist.id + " ") + ("has a duration of " + segmentDuration + " ") + ("when the reported duration is " + segmentInfo.duration + " ") + ("and the target duration is " + targetDuration + ". ") + 'For HLS content, a duration in excess of the target duration may result in ' + 'playback issues. See the HLS specification section on EXT-X-TARGETDURATION for ' + 'more details: ' + 'https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.3.1';
|
15639 |
|
15640 | if (isSegmentWayTooLong || isSegmentSlightlyTooLong) {
|
15641 | return {
|
15642 | severity: isSegmentWayTooLong ? 'warn' : 'info',
|
15643 | message: segmentTooLongMessage
|
15644 | };
|
15645 | }
|
15646 |
|
15647 | return null;
|
15648 | };
|
15649 |
|
15650 |
|
15651 |
|
15652 |
|
15653 |
|
15654 |
|
15655 |
|
15656 |
|
15657 | var SegmentLoader = function (_videojs$EventTarget) {
|
15658 | _inheritsLoose(SegmentLoader, _videojs$EventTarget);
|
15659 |
|
15660 | function SegmentLoader(settings, options) {
|
15661 | var _this;
|
15662 |
|
15663 | _this = _videojs$EventTarget.call(this) || this;
|
15664 |
|
15665 | if (!settings) {
|
15666 | throw new TypeError('Initialization settings are required');
|
15667 | }
|
15668 |
|
15669 | if (typeof settings.currentTime !== 'function') {
|
15670 | throw new TypeError('No currentTime getter specified');
|
15671 | }
|
15672 |
|
15673 | if (!settings.mediaSource) {
|
15674 | throw new TypeError('No MediaSource specified');
|
15675 | }
|
15676 |
|
15677 |
|
15678 | _this.bandwidth = settings.bandwidth;
|
15679 | _this.throughput = {
|
15680 | rate: 0,
|
15681 | count: 0
|
15682 | };
|
15683 | _this.roundTrip = NaN;
|
15684 |
|
15685 | _this.resetStats_();
|
15686 |
|
15687 | _this.mediaIndex = null;
|
15688 | _this.partIndex = null;
|
15689 |
|
15690 | _this.hasPlayed_ = settings.hasPlayed;
|
15691 | _this.currentTime_ = settings.currentTime;
|
15692 | _this.seekable_ = settings.seekable;
|
15693 | _this.seeking_ = settings.seeking;
|
15694 | _this.duration_ = settings.duration;
|
15695 | _this.mediaSource_ = settings.mediaSource;
|
15696 | _this.vhs_ = settings.vhs;
|
15697 | _this.loaderType_ = settings.loaderType;
|
15698 | _this.currentMediaInfo_ = void 0;
|
15699 | _this.startingMediaInfo_ = void 0;
|
15700 | _this.segmentMetadataTrack_ = settings.segmentMetadataTrack;
|
15701 | _this.goalBufferLength_ = settings.goalBufferLength;
|
15702 | _this.sourceType_ = settings.sourceType;
|
15703 | _this.sourceUpdater_ = settings.sourceUpdater;
|
15704 | _this.inbandTextTracks_ = settings.inbandTextTracks;
|
15705 | _this.state_ = 'INIT';
|
15706 | _this.timelineChangeController_ = settings.timelineChangeController;
|
15707 | _this.shouldSaveSegmentTimingInfo_ = true;
|
15708 | _this.parse708captions_ = settings.parse708captions;
|
15709 | _this.captionServices_ = settings.captionServices;
|
15710 | _this.experimentalExactManifestTimings = settings.experimentalExactManifestTimings;
|
15711 |
|
15712 | _this.checkBufferTimeout_ = null;
|
15713 | _this.error_ = void 0;
|
15714 | _this.currentTimeline_ = -1;
|
15715 | _this.pendingSegment_ = null;
|
15716 | _this.xhrOptions_ = null;
|
15717 | _this.pendingSegments_ = [];
|
15718 | _this.audioDisabled_ = false;
|
15719 | _this.isPendingTimestampOffset_ = false;
|
15720 |
|
15721 | _this.gopBuffer_ = [];
|
15722 | _this.timeMapping_ = 0;
|
15723 | _this.safeAppend_ = videojs.browser.IE_VERSION >= 11;
|
15724 | _this.appendInitSegment_ = {
|
15725 | audio: true,
|
15726 | video: true
|
15727 | };
|
15728 | _this.playlistOfLastInitSegment_ = {
|
15729 | audio: null,
|
15730 | video: null
|
15731 | };
|
15732 | _this.callQueue_ = [];
|
15733 |
|
15734 |
|
15735 |
|
15736 |
|
15737 |
|
15738 | _this.loadQueue_ = [];
|
15739 | _this.metadataQueue_ = {
|
15740 | id3: [],
|
15741 | caption: []
|
15742 | };
|
15743 | _this.waitingOnRemove_ = false;
|
15744 | _this.quotaExceededErrorRetryTimeout_ = null;
|
15745 |
|
15746 | _this.activeInitSegmentId_ = null;
|
15747 | _this.initSegments_ = {};
|
15748 |
|
15749 | _this.cacheEncryptionKeys_ = settings.cacheEncryptionKeys;
|
15750 | _this.keyCache_ = {};
|
15751 | _this.decrypter_ = settings.decrypter;
|
15752 |
|
15753 |
|
15754 |
|
15755 | _this.syncController_ = settings.syncController;
|
15756 | _this.syncPoint_ = {
|
15757 | segmentIndex: 0,
|
15758 | time: 0
|
15759 | };
|
15760 | _this.transmuxer_ = _this.createTransmuxer_();
|
15761 |
|
15762 | _this.triggerSyncInfoUpdate_ = function () {
|
15763 | return _this.trigger('syncinfoupdate');
|
15764 | };
|
15765 |
|
15766 | _this.syncController_.on('syncinfoupdate', _this.triggerSyncInfoUpdate_);
|
15767 |
|
15768 | _this.mediaSource_.addEventListener('sourceopen', function () {
|
15769 | if (!_this.isEndOfStream_()) {
|
15770 | _this.ended_ = false;
|
15771 | }
|
15772 | });
|
15773 |
|
15774 |
|
15775 | _this.fetchAtBuffer_ = false;
|
15776 | _this.logger_ = logger("SegmentLoader[" + _this.loaderType_ + "]");
|
15777 | Object.defineProperty(_assertThisInitialized(_this), 'state', {
|
15778 | get: function get() {
|
15779 | return this.state_;
|
15780 | },
|
15781 | set: function set(newState) {
|
15782 | if (newState !== this.state_) {
|
15783 | this.logger_(this.state_ + " -> " + newState);
|
15784 | this.state_ = newState;
|
15785 | this.trigger('statechange');
|
15786 | }
|
15787 | }
|
15788 | });
|
15789 |
|
15790 | _this.sourceUpdater_.on('ready', function () {
|
15791 | if (_this.hasEnoughInfoToAppend_()) {
|
15792 | _this.processCallQueue_();
|
15793 | }
|
15794 | });
|
15795 |
|
15796 |
|
15797 |
|
15798 |
|
15799 |
|
15800 | if (_this.loaderType_ === 'main') {
|
15801 | _this.timelineChangeController_.on('pendingtimelinechange', function () {
|
15802 | if (_this.hasEnoughInfoToAppend_()) {
|
15803 | _this.processCallQueue_();
|
15804 | }
|
15805 | });
|
15806 | }
|
15807 |
|
15808 |
|
15809 |
|
15810 |
|
15811 | if (_this.loaderType_ === 'audio') {
|
15812 | _this.timelineChangeController_.on('timelinechange', function () {
|
15813 | if (_this.hasEnoughInfoToLoad_()) {
|
15814 | _this.processLoadQueue_();
|
15815 | }
|
15816 |
|
15817 | if (_this.hasEnoughInfoToAppend_()) {
|
15818 | _this.processCallQueue_();
|
15819 | }
|
15820 | });
|
15821 | }
|
15822 |
|
15823 | return _this;
|
15824 | }
|
15825 |
|
15826 | var _proto = SegmentLoader.prototype;
|
15827 |
|
15828 | _proto.createTransmuxer_ = function createTransmuxer_() {
|
15829 | return segmentTransmuxer.createTransmuxer({
|
15830 | remux: false,
|
15831 | alignGopsAtEnd: this.safeAppend_,
|
15832 | keepOriginalTimestamps: true,
|
15833 | parse708captions: this.parse708captions_,
|
15834 | captionServices: this.captionServices_
|
15835 | });
|
15836 | }
|
15837 | |
15838 |
|
15839 |
|
15840 |
|
15841 |
|
15842 | ;
|
15843 |
|
15844 | _proto.resetStats_ = function resetStats_() {
|
15845 | this.mediaBytesTransferred = 0;
|
15846 | this.mediaRequests = 0;
|
15847 | this.mediaRequestsAborted = 0;
|
15848 | this.mediaRequestsTimedout = 0;
|
15849 | this.mediaRequestsErrored = 0;
|
15850 | this.mediaTransferDuration = 0;
|
15851 | this.mediaSecondsLoaded = 0;
|
15852 | this.mediaAppends = 0;
|
15853 | }
|
15854 | |
15855 |
|
15856 |
|
15857 | ;
|
15858 |
|
15859 | _proto.dispose = function dispose() {
|
15860 | this.trigger('dispose');
|
15861 | this.state = 'DISPOSED';
|
15862 | this.pause();
|
15863 | this.abort_();
|
15864 |
|
15865 | if (this.transmuxer_) {
|
15866 | this.transmuxer_.terminate();
|
15867 | }
|
15868 |
|
15869 | this.resetStats_();
|
15870 |
|
15871 | if (this.checkBufferTimeout_) {
|
15872 | window.clearTimeout(this.checkBufferTimeout_);
|
15873 | }
|
15874 |
|
15875 | if (this.syncController_ && this.triggerSyncInfoUpdate_) {
|
15876 | this.syncController_.off('syncinfoupdate', this.triggerSyncInfoUpdate_);
|
15877 | }
|
15878 |
|
15879 | this.off();
|
15880 | };
|
15881 |
|
15882 | _proto.setAudio = function setAudio(enable) {
|
15883 | this.audioDisabled_ = !enable;
|
15884 |
|
15885 | if (enable) {
|
15886 | this.appendInitSegment_.audio = true;
|
15887 | } else {
|
15888 |
|
15889 | this.sourceUpdater_.removeAudio(0, this.duration_());
|
15890 | }
|
15891 | }
|
15892 | |
15893 |
|
15894 |
|
15895 |
|
15896 | ;
|
15897 |
|
15898 | _proto.abort = function abort() {
|
15899 | if (this.state !== 'WAITING') {
|
15900 | if (this.pendingSegment_) {
|
15901 | this.pendingSegment_ = null;
|
15902 | }
|
15903 |
|
15904 | return;
|
15905 | }
|
15906 |
|
15907 | this.abort_();
|
15908 |
|
15909 |
|
15910 |
|
15911 |
|
15912 | this.state = 'READY';
|
15913 |
|
15914 |
|
15915 | if (!this.paused()) {
|
15916 | this.monitorBuffer_();
|
15917 | }
|
15918 | }
|
15919 | |
15920 |
|
15921 |
|
15922 |
|
15923 |
|
15924 | ;
|
15925 |
|
15926 | _proto.abort_ = function abort_() {
|
15927 | if (this.pendingSegment_ && this.pendingSegment_.abortRequests) {
|
15928 | this.pendingSegment_.abortRequests();
|
15929 | }
|
15930 |
|
15931 |
|
15932 | this.pendingSegment_ = null;
|
15933 | this.callQueue_ = [];
|
15934 | this.loadQueue_ = [];
|
15935 | this.metadataQueue_.id3 = [];
|
15936 | this.metadataQueue_.caption = [];
|
15937 | this.timelineChangeController_.clearPendingTimelineChange(this.loaderType_);
|
15938 | this.waitingOnRemove_ = false;
|
15939 | window.clearTimeout(this.quotaExceededErrorRetryTimeout_);
|
15940 | this.quotaExceededErrorRetryTimeout_ = null;
|
15941 | };
|
15942 |
|
15943 | _proto.checkForAbort_ = function checkForAbort_(requestId) {
|
15944 |
|
15945 |
|
15946 | if (this.state === 'APPENDING' && !this.pendingSegment_) {
|
15947 | this.state = 'READY';
|
15948 | return true;
|
15949 | }
|
15950 |
|
15951 | if (!this.pendingSegment_ || this.pendingSegment_.requestId !== requestId) {
|
15952 | return true;
|
15953 | }
|
15954 |
|
15955 | return false;
|
15956 | }
|
15957 | |
15958 |
|
15959 |
|
15960 |
|
15961 |
|
15962 |
|
15963 | ;
|
15964 |
|
15965 | _proto.error = function error(_error) {
|
15966 | if (typeof _error !== 'undefined') {
|
15967 | this.logger_('error occurred:', _error);
|
15968 | this.error_ = _error;
|
15969 | }
|
15970 |
|
15971 | this.pendingSegment_ = null;
|
15972 | return this.error_;
|
15973 | };
|
15974 |
|
15975 | _proto.endOfStream = function endOfStream() {
|
15976 | this.ended_ = true;
|
15977 |
|
15978 | if (this.transmuxer_) {
|
15979 |
|
15980 | segmentTransmuxer.reset(this.transmuxer_);
|
15981 | }
|
15982 |
|
15983 | this.gopBuffer_.length = 0;
|
15984 | this.pause();
|
15985 | this.trigger('ended');
|
15986 | }
|
15987 | |
15988 |
|
15989 |
|
15990 |
|
15991 |
|
15992 |
|
15993 | ;
|
15994 |
|
15995 | _proto.buffered_ = function buffered_() {
|
15996 | var trackInfo = this.getMediaInfo_();
|
15997 |
|
15998 | if (!this.sourceUpdater_ || !trackInfo) {
|
15999 | return videojs.createTimeRanges();
|
16000 | }
|
16001 |
|
16002 | if (this.loaderType_ === 'main') {
|
16003 | var hasAudio = trackInfo.hasAudio,
|
16004 | hasVideo = trackInfo.hasVideo,
|
16005 | isMuxed = trackInfo.isMuxed;
|
16006 |
|
16007 | if (hasVideo && hasAudio && !this.audioDisabled_ && !isMuxed) {
|
16008 | return this.sourceUpdater_.buffered();
|
16009 | }
|
16010 |
|
16011 | if (hasVideo) {
|
16012 | return this.sourceUpdater_.videoBuffered();
|
16013 | }
|
16014 | }
|
16015 |
|
16016 |
|
16017 |
|
16018 | return this.sourceUpdater_.audioBuffered();
|
16019 | }
|
16020 | |
16021 |
|
16022 |
|
16023 |
|
16024 |
|
16025 |
|
16026 |
|
16027 |
|
16028 |
|
16029 |
|
16030 | ;
|
16031 |
|
16032 | _proto.initSegmentForMap = function initSegmentForMap(map, set) {
|
16033 | if (set === void 0) {
|
16034 | set = false;
|
16035 | }
|
16036 |
|
16037 | if (!map) {
|
16038 | return null;
|
16039 | }
|
16040 |
|
16041 | var id = initSegmentId(map);
|
16042 | var storedMap = this.initSegments_[id];
|
16043 |
|
16044 | if (set && !storedMap && map.bytes) {
|
16045 | this.initSegments_[id] = storedMap = {
|
16046 | resolvedUri: map.resolvedUri,
|
16047 | byterange: map.byterange,
|
16048 | bytes: map.bytes,
|
16049 | tracks: map.tracks,
|
16050 | timescales: map.timescales
|
16051 | };
|
16052 | }
|
16053 |
|
16054 | return storedMap || map;
|
16055 | }
|
16056 | |
16057 |
|
16058 |
|
16059 |
|
16060 |
|
16061 |
|
16062 |
|
16063 |
|
16064 |
|
16065 |
|
16066 | ;
|
16067 |
|
16068 | _proto.segmentKey = function segmentKey(key, set) {
|
16069 | if (set === void 0) {
|
16070 | set = false;
|
16071 | }
|
16072 |
|
16073 | if (!key) {
|
16074 | return null;
|
16075 | }
|
16076 |
|
16077 | var id = segmentKeyId(key);
|
16078 | var storedKey = this.keyCache_[id];
|
16079 |
|
16080 |
|
16081 | if (this.cacheEncryptionKeys_ && set && !storedKey && key.bytes) {
|
16082 | this.keyCache_[id] = storedKey = {
|
16083 | resolvedUri: key.resolvedUri,
|
16084 | bytes: key.bytes
|
16085 | };
|
16086 | }
|
16087 |
|
16088 | var result = {
|
16089 | resolvedUri: (storedKey || key).resolvedUri
|
16090 | };
|
16091 |
|
16092 | if (storedKey) {
|
16093 | result.bytes = storedKey.bytes;
|
16094 | }
|
16095 |
|
16096 | return result;
|
16097 | }
|
16098 | |
16099 |
|
16100 |
|
16101 |
|
16102 |
|
16103 |
|
16104 | ;
|
16105 |
|
16106 | _proto.couldBeginLoading_ = function couldBeginLoading_() {
|
16107 | return this.playlist_ && !this.paused();
|
16108 | }
|
16109 | |
16110 |
|
16111 |
|
16112 | ;
|
16113 |
|
16114 | _proto.load = function load() {
|
16115 |
|
16116 | this.monitorBuffer_();
|
16117 |
|
16118 |
|
16119 | if (!this.playlist_) {
|
16120 | return;
|
16121 | }
|
16122 |
|
16123 |
|
16124 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
16125 | return this.init_();
|
16126 | }
|
16127 |
|
16128 |
|
16129 |
|
16130 | if (!this.couldBeginLoading_() || this.state !== 'READY' && this.state !== 'INIT') {
|
16131 | return;
|
16132 | }
|
16133 |
|
16134 | this.state = 'READY';
|
16135 | }
|
16136 | |
16137 |
|
16138 |
|
16139 |
|
16140 |
|
16141 |
|
16142 |
|
16143 | ;
|
16144 |
|
16145 | _proto.init_ = function init_() {
|
16146 | this.state = 'READY';
|
16147 |
|
16148 |
|
16149 | this.resetEverything();
|
16150 | return this.monitorBuffer_();
|
16151 | }
|
16152 | |
16153 |
|
16154 |
|
16155 |
|
16156 |
|
16157 | ;
|
16158 |
|
16159 | _proto.playlist = function playlist(newPlaylist, options) {
|
16160 | if (options === void 0) {
|
16161 | options = {};
|
16162 | }
|
16163 |
|
16164 | if (!newPlaylist) {
|
16165 | return;
|
16166 | }
|
16167 |
|
16168 | var oldPlaylist = this.playlist_;
|
16169 | var segmentInfo = this.pendingSegment_;
|
16170 | this.playlist_ = newPlaylist;
|
16171 | this.xhrOptions_ = options;
|
16172 |
|
16173 |
|
16174 |
|
16175 |
|
16176 |
|
16177 |
|
16178 |
|
16179 | if (this.state === 'INIT') {
|
16180 | newPlaylist.syncInfo = {
|
16181 | mediaSequence: newPlaylist.mediaSequence,
|
16182 | time: 0
|
16183 | };
|
16184 |
|
16185 |
|
16186 |
|
16187 |
|
16188 |
|
16189 |
|
16190 |
|
16191 |
|
16192 | if (this.loaderType_ === 'main') {
|
16193 | this.syncController_.setDateTimeMappingForStart(newPlaylist);
|
16194 | }
|
16195 | }
|
16196 |
|
16197 | var oldId = null;
|
16198 |
|
16199 | if (oldPlaylist) {
|
16200 | if (oldPlaylist.id) {
|
16201 | oldId = oldPlaylist.id;
|
16202 | } else if (oldPlaylist.uri) {
|
16203 | oldId = oldPlaylist.uri;
|
16204 | }
|
16205 | }
|
16206 |
|
16207 | this.logger_("playlist update [" + oldId + " => " + (newPlaylist.id || newPlaylist.uri) + "]");
|
16208 |
|
16209 |
|
16210 | this.trigger('syncinfoupdate');
|
16211 |
|
16212 |
|
16213 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
16214 | return this.init_();
|
16215 | }
|
16216 |
|
16217 | if (!oldPlaylist || oldPlaylist.uri !== newPlaylist.uri) {
|
16218 | if (this.mediaIndex !== null) {
|
16219 |
|
16220 |
|
16221 | this.resyncLoader();
|
16222 | }
|
16223 |
|
16224 | this.currentMediaInfo_ = void 0;
|
16225 | this.trigger('playlistupdate');
|
16226 |
|
16227 | return;
|
16228 | }
|
16229 |
|
16230 |
|
16231 |
|
16232 | var mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
16233 | this.logger_("live window shift [" + mediaSequenceDiff + "]");
|
16234 |
|
16235 |
|
16236 |
|
16237 | if (this.mediaIndex !== null) {
|
16238 | this.mediaIndex -= mediaSequenceDiff;
|
16239 |
|
16240 |
|
16241 |
|
16242 | if (this.mediaIndex < 0) {
|
16243 | this.mediaIndex = null;
|
16244 | this.partIndex = null;
|
16245 | } else {
|
16246 | var segment = this.playlist_.segments[this.mediaIndex];
|
16247 |
|
16248 |
|
16249 |
|
16250 | if (this.partIndex && (!segment.parts || !segment.parts.length || !segment.parts[this.partIndex])) {
|
16251 | var mediaIndex = this.mediaIndex;
|
16252 | this.logger_("currently processing part (index " + this.partIndex + ") no longer exists.");
|
16253 | this.resetLoader();
|
16254 |
|
16255 |
|
16256 |
|
16257 | this.mediaIndex = mediaIndex;
|
16258 | }
|
16259 | }
|
16260 | }
|
16261 |
|
16262 |
|
16263 |
|
16264 |
|
16265 | if (segmentInfo) {
|
16266 | segmentInfo.mediaIndex -= mediaSequenceDiff;
|
16267 |
|
16268 | if (segmentInfo.mediaIndex < 0) {
|
16269 | segmentInfo.mediaIndex = null;
|
16270 | segmentInfo.partIndex = null;
|
16271 | } else {
|
16272 |
|
16273 |
|
16274 |
|
16275 | if (segmentInfo.mediaIndex >= 0) {
|
16276 | segmentInfo.segment = newPlaylist.segments[segmentInfo.mediaIndex];
|
16277 | }
|
16278 |
|
16279 | if (segmentInfo.partIndex >= 0 && segmentInfo.segment.parts) {
|
16280 | segmentInfo.part = segmentInfo.segment.parts[segmentInfo.partIndex];
|
16281 | }
|
16282 | }
|
16283 | }
|
16284 |
|
16285 | this.syncController_.saveExpiredSegmentInfo(oldPlaylist, newPlaylist);
|
16286 | }
|
16287 | |
16288 |
|
16289 |
|
16290 |
|
16291 |
|
16292 |
|
16293 | ;
|
16294 |
|
16295 | _proto.pause = function pause() {
|
16296 | if (this.checkBufferTimeout_) {
|
16297 | window.clearTimeout(this.checkBufferTimeout_);
|
16298 | this.checkBufferTimeout_ = null;
|
16299 | }
|
16300 | }
|
16301 | |
16302 |
|
16303 |
|
16304 |
|
16305 |
|
16306 | ;
|
16307 |
|
16308 | _proto.paused = function paused() {
|
16309 | return this.checkBufferTimeout_ === null;
|
16310 | }
|
16311 | |
16312 |
|
16313 |
|
16314 |
|
16315 |
|
16316 |
|
16317 | ;
|
16318 |
|
16319 | _proto.resetEverything = function resetEverything(done) {
|
16320 | this.ended_ = false;
|
16321 | this.appendInitSegment_ = {
|
16322 | audio: true,
|
16323 | video: true
|
16324 | };
|
16325 | this.resetLoader();
|
16326 |
|
16327 |
|
16328 |
|
16329 | this.remove(0, Infinity, done);
|
16330 |
|
16331 | if (this.transmuxer_) {
|
16332 | this.transmuxer_.postMessage({
|
16333 | action: 'clearAllMp4Captions'
|
16334 | });
|
16335 |
|
16336 | this.transmuxer_.postMessage({
|
16337 | action: 'reset'
|
16338 | });
|
16339 | }
|
16340 | }
|
16341 | |
16342 |
|
16343 |
|
16344 |
|
16345 |
|
16346 |
|
16347 | ;
|
16348 |
|
16349 | _proto.resetLoader = function resetLoader() {
|
16350 | this.fetchAtBuffer_ = false;
|
16351 | this.resyncLoader();
|
16352 | }
|
16353 | |
16354 |
|
16355 |
|
16356 |
|
16357 | ;
|
16358 |
|
16359 | _proto.resyncLoader = function resyncLoader() {
|
16360 | if (this.transmuxer_) {
|
16361 |
|
16362 | segmentTransmuxer.reset(this.transmuxer_);
|
16363 | }
|
16364 |
|
16365 | this.mediaIndex = null;
|
16366 | this.partIndex = null;
|
16367 | this.syncPoint_ = null;
|
16368 | this.isPendingTimestampOffset_ = false;
|
16369 | this.callQueue_ = [];
|
16370 | this.loadQueue_ = [];
|
16371 | this.metadataQueue_.id3 = [];
|
16372 | this.metadataQueue_.caption = [];
|
16373 | this.abort();
|
16374 |
|
16375 | if (this.transmuxer_) {
|
16376 | this.transmuxer_.postMessage({
|
16377 | action: 'clearParsedMp4Captions'
|
16378 | });
|
16379 | }
|
16380 | }
|
16381 | |
16382 |
|
16383 |
|
16384 |
|
16385 |
|
16386 |
|
16387 |
|
16388 |
|
16389 |
|
16390 | ;
|
16391 |
|
16392 | _proto.remove = function remove(start, end, done, force) {
|
16393 | if (done === void 0) {
|
16394 | done = function done() {};
|
16395 | }
|
16396 |
|
16397 | if (force === void 0) {
|
16398 | force = false;
|
16399 | }
|
16400 |
|
16401 |
|
16402 |
|
16403 |
|
16404 | if (end === Infinity) {
|
16405 | end = this.duration_();
|
16406 | }
|
16407 |
|
16408 |
|
16409 |
|
16410 |
|
16411 | if (end <= start) {
|
16412 | this.logger_('skipping remove because end ${end} is <= start ${start}');
|
16413 | return;
|
16414 | }
|
16415 |
|
16416 | if (!this.sourceUpdater_ || !this.getMediaInfo_()) {
|
16417 | this.logger_('skipping remove because no source updater or starting media info');
|
16418 |
|
16419 | return;
|
16420 | }
|
16421 |
|
16422 |
|
16423 | var removesRemaining = 1;
|
16424 |
|
16425 | var removeFinished = function removeFinished() {
|
16426 | removesRemaining--;
|
16427 |
|
16428 | if (removesRemaining === 0) {
|
16429 | done();
|
16430 | }
|
16431 | };
|
16432 |
|
16433 | if (force || !this.audioDisabled_) {
|
16434 | removesRemaining++;
|
16435 | this.sourceUpdater_.removeAudio(start, end, removeFinished);
|
16436 | }
|
16437 |
|
16438 |
|
16439 |
|
16440 |
|
16441 |
|
16442 |
|
16443 |
|
16444 |
|
16445 |
|
16446 | if (force || this.loaderType_ === 'main') {
|
16447 | this.gopBuffer_ = removeGopBuffer(this.gopBuffer_, start, end, this.timeMapping_);
|
16448 | removesRemaining++;
|
16449 | this.sourceUpdater_.removeVideo(start, end, removeFinished);
|
16450 | }
|
16451 |
|
16452 |
|
16453 | for (var track in this.inbandTextTracks_) {
|
16454 | removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
|
16455 | }
|
16456 |
|
16457 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
16458 |
|
16459 | removeFinished();
|
16460 | }
|
16461 | |
16462 |
|
16463 |
|
16464 |
|
16465 |
|
16466 | ;
|
16467 |
|
16468 | _proto.monitorBuffer_ = function monitorBuffer_() {
|
16469 | if (this.checkBufferTimeout_) {
|
16470 | window.clearTimeout(this.checkBufferTimeout_);
|
16471 | }
|
16472 |
|
16473 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this), 1);
|
16474 | }
|
16475 | |
16476 |
|
16477 |
|
16478 |
|
16479 |
|
16480 |
|
16481 | ;
|
16482 |
|
16483 | _proto.monitorBufferTick_ = function monitorBufferTick_() {
|
16484 | if (this.state === 'READY') {
|
16485 | this.fillBuffer_();
|
16486 | }
|
16487 |
|
16488 | if (this.checkBufferTimeout_) {
|
16489 | window.clearTimeout(this.checkBufferTimeout_);
|
16490 | }
|
16491 |
|
16492 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this), CHECK_BUFFER_DELAY);
|
16493 | }
|
16494 | |
16495 |
|
16496 |
|
16497 |
|
16498 |
|
16499 |
|
16500 |
|
16501 |
|
16502 |
|
16503 | ;
|
16504 |
|
16505 | _proto.fillBuffer_ = function fillBuffer_() {
|
16506 |
|
16507 |
|
16508 | if (this.sourceUpdater_.updating()) {
|
16509 | return;
|
16510 | }
|
16511 |
|
16512 |
|
16513 | var segmentInfo = this.chooseNextRequest_();
|
16514 |
|
16515 | if (!segmentInfo) {
|
16516 | return;
|
16517 | }
|
16518 |
|
16519 | if (typeof segmentInfo.timestampOffset === 'number') {
|
16520 | this.isPendingTimestampOffset_ = false;
|
16521 | this.timelineChangeController_.pendingTimelineChange({
|
16522 | type: this.loaderType_,
|
16523 | from: this.currentTimeline_,
|
16524 | to: segmentInfo.timeline
|
16525 | });
|
16526 | }
|
16527 |
|
16528 | this.loadSegment_(segmentInfo);
|
16529 | }
|
16530 | |
16531 |
|
16532 |
|
16533 |
|
16534 |
|
16535 |
|
16536 |
|
16537 |
|
16538 |
|
16539 | ;
|
16540 |
|
16541 | _proto.isEndOfStream_ = function isEndOfStream_(mediaIndex, playlist, partIndex) {
|
16542 | if (mediaIndex === void 0) {
|
16543 | mediaIndex = this.mediaIndex;
|
16544 | }
|
16545 |
|
16546 | if (playlist === void 0) {
|
16547 | playlist = this.playlist_;
|
16548 | }
|
16549 |
|
16550 | if (partIndex === void 0) {
|
16551 | partIndex = this.partIndex;
|
16552 | }
|
16553 |
|
16554 | if (!playlist || !this.mediaSource_) {
|
16555 | return false;
|
16556 | }
|
16557 |
|
16558 | var segment = typeof mediaIndex === 'number' && playlist.segments[mediaIndex];
|
16559 |
|
16560 | var appendedLastSegment = mediaIndex + 1 === playlist.segments.length;
|
16561 |
|
16562 | var appendedLastPart = !segment || !segment.parts || partIndex + 1 === segment.parts.length;
|
16563 |
|
16564 |
|
16565 |
|
16566 | return playlist.endList && this.mediaSource_.readyState === 'open' && appendedLastSegment && appendedLastPart;
|
16567 | }
|
16568 | |
16569 |
|
16570 |
|
16571 |
|
16572 |
|
16573 | ;
|
16574 |
|
16575 | _proto.chooseNextRequest_ = function chooseNextRequest_() {
|
16576 | var bufferedEnd = lastBufferedEnd(this.buffered_()) || 0;
|
16577 | var bufferedTime = Math.max(0, bufferedEnd - this.currentTime_());
|
16578 | var preloaded = !this.hasPlayed_() && bufferedTime >= 1;
|
16579 | var haveEnoughBuffer = bufferedTime >= this.goalBufferLength_();
|
16580 | var segments = this.playlist_.segments;
|
16581 |
|
16582 |
|
16583 |
|
16584 |
|
16585 | if (!segments.length || preloaded || haveEnoughBuffer) {
|
16586 | return null;
|
16587 | }
|
16588 |
|
16589 | this.syncPoint_ = this.syncPoint_ || this.syncController_.getSyncPoint(this.playlist_, this.duration_(), this.currentTimeline_, this.currentTime_());
|
16590 | var next = {
|
16591 | partIndex: null,
|
16592 | mediaIndex: null,
|
16593 | startOfSegment: null,
|
16594 | playlist: this.playlist_,
|
16595 | isSyncRequest: Boolean(!this.syncPoint_)
|
16596 | };
|
16597 |
|
16598 | if (next.isSyncRequest) {
|
16599 | next.mediaIndex = getSyncSegmentCandidate(this.currentTimeline_, segments, bufferedEnd);
|
16600 | } else if (this.mediaIndex !== null) {
|
16601 | var segment = segments[this.mediaIndex];
|
16602 | var partIndex = typeof this.partIndex === 'number' ? this.partIndex : -1;
|
16603 | next.startOfSegment = segment.end ? segment.end : bufferedEnd;
|
16604 |
|
16605 | if (segment.parts && segment.parts[partIndex + 1]) {
|
16606 | next.mediaIndex = this.mediaIndex;
|
16607 | next.partIndex = partIndex + 1;
|
16608 | } else {
|
16609 | next.mediaIndex = this.mediaIndex + 1;
|
16610 | }
|
16611 | } else {
|
16612 |
|
16613 | var _Playlist$getMediaInf = Playlist.getMediaInfoForTime({
|
16614 | experimentalExactManifestTimings: this.experimentalExactManifestTimings,
|
16615 | playlist: this.playlist_,
|
16616 | currentTime: this.fetchAtBuffer_ ? bufferedEnd : this.currentTime_(),
|
16617 | startingPartIndex: this.syncPoint_.partIndex,
|
16618 | startingSegmentIndex: this.syncPoint_.segmentIndex,
|
16619 | startTime: this.syncPoint_.time
|
16620 | }),
|
16621 | segmentIndex = _Playlist$getMediaInf.segmentIndex,
|
16622 | startTime = _Playlist$getMediaInf.startTime,
|
16623 | _partIndex = _Playlist$getMediaInf.partIndex;
|
16624 |
|
16625 | next.getMediaInfoForTime = this.fetchAtBuffer_ ? 'bufferedEnd' : 'currentTime';
|
16626 | next.mediaIndex = segmentIndex;
|
16627 | next.startOfSegment = startTime;
|
16628 | next.partIndex = _partIndex;
|
16629 | }
|
16630 |
|
16631 | var nextSegment = segments[next.mediaIndex];
|
16632 | var nextPart = nextSegment && typeof next.partIndex === 'number' && nextSegment.parts && nextSegment.parts[next.partIndex];
|
16633 |
|
16634 |
|
16635 | if (!nextSegment || typeof next.partIndex === 'number' && !nextPart) {
|
16636 | return null;
|
16637 | }
|
16638 |
|
16639 |
|
16640 |
|
16641 | if (typeof next.partIndex !== 'number' && nextSegment.parts) {
|
16642 | next.partIndex = 0;
|
16643 | }
|
16644 |
|
16645 | var ended = this.mediaSource_ && this.mediaSource_.readyState === 'ended';
|
16646 |
|
16647 |
|
16648 |
|
16649 |
|
16650 | if (next.mediaIndex >= segments.length - 1 && ended && !this.seeking_()) {
|
16651 | return null;
|
16652 | }
|
16653 |
|
16654 | return this.generateSegmentInfo_(next);
|
16655 | };
|
16656 |
|
16657 | _proto.generateSegmentInfo_ = function generateSegmentInfo_(options) {
|
16658 | var playlist = options.playlist,
|
16659 | mediaIndex = options.mediaIndex,
|
16660 | startOfSegment = options.startOfSegment,
|
16661 | isSyncRequest = options.isSyncRequest,
|
16662 | partIndex = options.partIndex,
|
16663 | forceTimestampOffset = options.forceTimestampOffset,
|
16664 | getMediaInfoForTime = options.getMediaInfoForTime;
|
16665 | var segment = playlist.segments[mediaIndex];
|
16666 | var part = typeof partIndex === 'number' && segment.parts[partIndex];
|
16667 | var segmentInfo = {
|
16668 | requestId: 'segment-loader-' + Math.random(),
|
16669 |
|
16670 | uri: part && part.resolvedUri || segment.resolvedUri,
|
16671 |
|
16672 | mediaIndex: mediaIndex,
|
16673 | partIndex: part ? partIndex : null,
|
16674 |
|
16675 |
|
16676 | isSyncRequest: isSyncRequest,
|
16677 | startOfSegment: startOfSegment,
|
16678 |
|
16679 | playlist: playlist,
|
16680 |
|
16681 | bytes: null,
|
16682 |
|
16683 | encryptedBytes: null,
|
16684 |
|
16685 |
|
16686 | timestampOffset: null,
|
16687 |
|
16688 | timeline: segment.timeline,
|
16689 |
|
16690 | duration: part && part.duration || segment.duration,
|
16691 |
|
16692 | segment: segment,
|
16693 | part: part,
|
16694 | byteLength: 0,
|
16695 | transmuxer: this.transmuxer_,
|
16696 |
|
16697 | getMediaInfoForTime: getMediaInfoForTime
|
16698 | };
|
16699 | var overrideCheck = typeof forceTimestampOffset !== 'undefined' ? forceTimestampOffset : this.isPendingTimestampOffset_;
|
16700 | segmentInfo.timestampOffset = this.timestampOffsetForSegment_({
|
16701 | segmentTimeline: segment.timeline,
|
16702 | currentTimeline: this.currentTimeline_,
|
16703 | startOfSegment: startOfSegment,
|
16704 | buffered: this.buffered_(),
|
16705 | overrideCheck: overrideCheck
|
16706 | });
|
16707 | var audioBufferedEnd = lastBufferedEnd(this.sourceUpdater_.audioBuffered());
|
16708 |
|
16709 | if (typeof audioBufferedEnd === 'number') {
|
16710 |
|
16711 |
|
16712 | segmentInfo.audioAppendStart = audioBufferedEnd - this.sourceUpdater_.audioTimestampOffset();
|
16713 | }
|
16714 |
|
16715 | if (this.sourceUpdater_.videoBuffered().length) {
|
16716 | segmentInfo.gopsToAlignWith = gopsSafeToAlignWith(this.gopBuffer_,
|
16717 |
|
16718 | this.currentTime_() - this.sourceUpdater_.videoTimestampOffset(), this.timeMapping_);
|
16719 | }
|
16720 |
|
16721 | return segmentInfo;
|
16722 | }
|
16723 |
|
16724 |
|
16725 | ;
|
16726 |
|
16727 | _proto.timestampOffsetForSegment_ = function timestampOffsetForSegment_(options) {
|
16728 | return timestampOffsetForSegment(options);
|
16729 | }
|
16730 | |
16731 |
|
16732 |
|
16733 |
|
16734 |
|
16735 |
|
16736 |
|
16737 |
|
16738 |
|
16739 | ;
|
16740 |
|
16741 | _proto.earlyAbortWhenNeeded_ = function earlyAbortWhenNeeded_(stats) {
|
16742 | if (this.vhs_.tech_.paused() ||
|
16743 |
|
16744 |
|
16745 | !this.xhrOptions_.timeout ||
|
16746 | !this.playlist_.attributes.BANDWIDTH) {
|
16747 | return;
|
16748 | }
|
16749 |
|
16750 |
|
16751 |
|
16752 |
|
16753 | if (Date.now() - (stats.firstBytesReceivedAt || Date.now()) < 1000) {
|
16754 | return;
|
16755 | }
|
16756 |
|
16757 | var currentTime = this.currentTime_();
|
16758 | var measuredBandwidth = stats.bandwidth;
|
16759 | var segmentDuration = this.pendingSegment_.duration;
|
16760 | var requestTimeRemaining = Playlist.estimateSegmentRequestTime(segmentDuration, measuredBandwidth, this.playlist_, stats.bytesReceived);
|
16761 |
|
16762 |
|
16763 |
|
16764 | var timeUntilRebuffer$1 = timeUntilRebuffer(this.buffered_(), currentTime, this.vhs_.tech_.playbackRate()) - 1;
|
16765 |
|
16766 |
|
16767 | if (requestTimeRemaining <= timeUntilRebuffer$1) {
|
16768 | return;
|
16769 | }
|
16770 |
|
16771 | var switchCandidate = minRebufferMaxBandwidthSelector({
|
16772 | master: this.vhs_.playlists.master,
|
16773 | currentTime: currentTime,
|
16774 | bandwidth: measuredBandwidth,
|
16775 | duration: this.duration_(),
|
16776 | segmentDuration: segmentDuration,
|
16777 | timeUntilRebuffer: timeUntilRebuffer$1,
|
16778 | currentTimeline: this.currentTimeline_,
|
16779 | syncController: this.syncController_
|
16780 | });
|
16781 |
|
16782 | if (!switchCandidate) {
|
16783 | return;
|
16784 | }
|
16785 |
|
16786 | var rebufferingImpact = requestTimeRemaining - timeUntilRebuffer$1;
|
16787 | var timeSavedBySwitching = rebufferingImpact - switchCandidate.rebufferingImpact;
|
16788 | var minimumTimeSaving = 0.5;
|
16789 |
|
16790 |
|
16791 |
|
16792 | if (timeUntilRebuffer$1 <= TIME_FUDGE_FACTOR) {
|
16793 | minimumTimeSaving = 1;
|
16794 | }
|
16795 |
|
16796 | if (!switchCandidate.playlist || switchCandidate.playlist.uri === this.playlist_.uri || timeSavedBySwitching < minimumTimeSaving) {
|
16797 | return;
|
16798 | }
|
16799 |
|
16800 |
|
16801 |
|
16802 |
|
16803 | this.bandwidth = switchCandidate.playlist.attributes.BANDWIDTH * Config.BANDWIDTH_VARIANCE + 1;
|
16804 | this.trigger('earlyabort');
|
16805 | };
|
16806 |
|
16807 | _proto.handleAbort_ = function handleAbort_(segmentInfo) {
|
16808 | this.logger_("Aborting " + segmentInfoString(segmentInfo));
|
16809 | this.mediaRequestsAborted += 1;
|
16810 | }
|
16811 | |
16812 |
|
16813 |
|
16814 |
|
16815 |
|
16816 |
|
16817 |
|
16818 |
|
16819 |
|
16820 | ;
|
16821 |
|
16822 | _proto.handleProgress_ = function handleProgress_(event, simpleSegment) {
|
16823 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
16824 |
|
16825 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
16826 | return;
|
16827 | }
|
16828 |
|
16829 | this.trigger('progress');
|
16830 | };
|
16831 |
|
16832 | _proto.handleTrackInfo_ = function handleTrackInfo_(simpleSegment, trackInfo) {
|
16833 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
16834 |
|
16835 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
16836 | return;
|
16837 | }
|
16838 |
|
16839 | if (this.checkForIllegalMediaSwitch(trackInfo)) {
|
16840 | return;
|
16841 | }
|
16842 |
|
16843 | trackInfo = trackInfo || {};
|
16844 |
|
16845 |
|
16846 |
|
16847 | if (!shallowEqual(this.currentMediaInfo_, trackInfo)) {
|
16848 | this.appendInitSegment_ = {
|
16849 | audio: true,
|
16850 | video: true
|
16851 | };
|
16852 | this.startingMediaInfo_ = trackInfo;
|
16853 | this.currentMediaInfo_ = trackInfo;
|
16854 | this.logger_('trackinfo update', trackInfo);
|
16855 | this.trigger('trackinfo');
|
16856 | }
|
16857 |
|
16858 |
|
16859 |
|
16860 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
16861 | return;
|
16862 | }
|
16863 |
|
16864 |
|
16865 |
|
16866 | this.pendingSegment_.trackInfo = trackInfo;
|
16867 |
|
16868 | if (this.hasEnoughInfoToAppend_()) {
|
16869 | this.processCallQueue_();
|
16870 | }
|
16871 | };
|
16872 |
|
16873 | _proto.handleTimingInfo_ = function handleTimingInfo_(simpleSegment, mediaType, timeType, time) {
|
16874 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
16875 |
|
16876 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
16877 | return;
|
16878 | }
|
16879 |
|
16880 | var segmentInfo = this.pendingSegment_;
|
16881 | var timingInfoProperty = timingInfoPropertyForMedia(mediaType);
|
16882 | segmentInfo[timingInfoProperty] = segmentInfo[timingInfoProperty] || {};
|
16883 | segmentInfo[timingInfoProperty][timeType] = time;
|
16884 | this.logger_("timinginfo: " + mediaType + " - " + timeType + " - " + time);
|
16885 |
|
16886 | if (this.hasEnoughInfoToAppend_()) {
|
16887 | this.processCallQueue_();
|
16888 | }
|
16889 | };
|
16890 |
|
16891 | _proto.handleCaptions_ = function handleCaptions_(simpleSegment, captionData) {
|
16892 | var _this2 = this;
|
16893 |
|
16894 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
16895 |
|
16896 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
16897 | return;
|
16898 | }
|
16899 |
|
16900 |
|
16901 |
|
16902 | if (captionData.length === 0) {
|
16903 | this.logger_('SegmentLoader received no captions from a caption event');
|
16904 | return;
|
16905 | }
|
16906 |
|
16907 | var segmentInfo = this.pendingSegment_;
|
16908 |
|
16909 |
|
16910 | if (!segmentInfo.hasAppendedData_) {
|
16911 | this.metadataQueue_.caption.push(this.handleCaptions_.bind(this, simpleSegment, captionData));
|
16912 | return;
|
16913 | }
|
16914 |
|
16915 | var timestampOffset = this.sourceUpdater_.videoTimestampOffset() === null ? this.sourceUpdater_.audioTimestampOffset() : this.sourceUpdater_.videoTimestampOffset();
|
16916 | var captionTracks = {};
|
16917 |
|
16918 | captionData.forEach(function (caption) {
|
16919 |
|
16920 |
|
16921 | captionTracks[caption.stream] = captionTracks[caption.stream] || {
|
16922 |
|
16923 | startTime: Infinity,
|
16924 | captions: [],
|
16925 |
|
16926 | endTime: 0
|
16927 | };
|
16928 | var captionTrack = captionTracks[caption.stream];
|
16929 | captionTrack.startTime = Math.min(captionTrack.startTime, caption.startTime + timestampOffset);
|
16930 | captionTrack.endTime = Math.max(captionTrack.endTime, caption.endTime + timestampOffset);
|
16931 | captionTrack.captions.push(caption);
|
16932 | });
|
16933 | Object.keys(captionTracks).forEach(function (trackName) {
|
16934 | var _captionTracks$trackN = captionTracks[trackName],
|
16935 | startTime = _captionTracks$trackN.startTime,
|
16936 | endTime = _captionTracks$trackN.endTime,
|
16937 | captions = _captionTracks$trackN.captions;
|
16938 | var inbandTextTracks = _this2.inbandTextTracks_;
|
16939 |
|
16940 | _this2.logger_("adding cues from " + startTime + " -> " + endTime + " for " + trackName);
|
16941 |
|
16942 | createCaptionsTrackIfNotExists(inbandTextTracks, _this2.vhs_.tech_, trackName);
|
16943 |
|
16944 |
|
16945 |
|
16946 |
|
16947 | removeCuesFromTrack(startTime, endTime, inbandTextTracks[trackName]);
|
16948 | addCaptionData({
|
16949 | captionArray: captions,
|
16950 | inbandTextTracks: inbandTextTracks,
|
16951 | timestampOffset: timestampOffset
|
16952 | });
|
16953 | });
|
16954 |
|
16955 |
|
16956 | if (this.transmuxer_) {
|
16957 | this.transmuxer_.postMessage({
|
16958 | action: 'clearParsedMp4Captions'
|
16959 | });
|
16960 | }
|
16961 | };
|
16962 |
|
16963 | _proto.handleId3_ = function handleId3_(simpleSegment, id3Frames, dispatchType) {
|
16964 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
16965 |
|
16966 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
16967 | return;
|
16968 | }
|
16969 |
|
16970 | var segmentInfo = this.pendingSegment_;
|
16971 |
|
16972 | if (!segmentInfo.hasAppendedData_) {
|
16973 | this.metadataQueue_.id3.push(this.handleId3_.bind(this, simpleSegment, id3Frames, dispatchType));
|
16974 | return;
|
16975 | }
|
16976 |
|
16977 | var timestampOffset = this.sourceUpdater_.videoTimestampOffset() === null ? this.sourceUpdater_.audioTimestampOffset() : this.sourceUpdater_.videoTimestampOffset();
|
16978 |
|
16979 |
|
16980 |
|
16981 | createMetadataTrackIfNotExists(this.inbandTextTracks_, dispatchType, this.vhs_.tech_);
|
16982 | addMetadata({
|
16983 | inbandTextTracks: this.inbandTextTracks_,
|
16984 | metadataArray: id3Frames,
|
16985 | timestampOffset: timestampOffset,
|
16986 | videoDuration: this.duration_()
|
16987 | });
|
16988 | };
|
16989 |
|
16990 | _proto.processMetadataQueue_ = function processMetadataQueue_() {
|
16991 | this.metadataQueue_.id3.forEach(function (fn) {
|
16992 | return fn();
|
16993 | });
|
16994 | this.metadataQueue_.caption.forEach(function (fn) {
|
16995 | return fn();
|
16996 | });
|
16997 | this.metadataQueue_.id3 = [];
|
16998 | this.metadataQueue_.caption = [];
|
16999 | };
|
17000 |
|
17001 | _proto.processCallQueue_ = function processCallQueue_() {
|
17002 | var callQueue = this.callQueue_;
|
17003 |
|
17004 |
|
17005 |
|
17006 | this.callQueue_ = [];
|
17007 | callQueue.forEach(function (fun) {
|
17008 | return fun();
|
17009 | });
|
17010 | };
|
17011 |
|
17012 | _proto.processLoadQueue_ = function processLoadQueue_() {
|
17013 | var loadQueue = this.loadQueue_;
|
17014 |
|
17015 |
|
17016 |
|
17017 | this.loadQueue_ = [];
|
17018 | loadQueue.forEach(function (fun) {
|
17019 | return fun();
|
17020 | });
|
17021 | }
|
17022 | |
17023 |
|
17024 |
|
17025 |
|
17026 |
|
17027 |
|
17028 | ;
|
17029 |
|
17030 | _proto.hasEnoughInfoToLoad_ = function hasEnoughInfoToLoad_() {
|
17031 |
|
17032 |
|
17033 | if (this.loaderType_ !== 'audio') {
|
17034 | return true;
|
17035 | }
|
17036 |
|
17037 | var segmentInfo = this.pendingSegment_;
|
17038 |
|
17039 |
|
17040 | if (!segmentInfo) {
|
17041 | return false;
|
17042 | }
|
17043 |
|
17044 |
|
17045 |
|
17046 |
|
17047 |
|
17048 | if (!this.getCurrentMediaInfo_()) {
|
17049 | return true;
|
17050 | }
|
17051 |
|
17052 | if (
|
17053 |
|
17054 |
|
17055 |
|
17056 |
|
17057 |
|
17058 |
|
17059 |
|
17060 |
|
17061 |
|
17062 |
|
17063 |
|
17064 |
|
17065 |
|
17066 |
|
17067 | shouldWaitForTimelineChange({
|
17068 | timelineChangeController: this.timelineChangeController_,
|
17069 | currentTimeline: this.currentTimeline_,
|
17070 | segmentTimeline: segmentInfo.timeline,
|
17071 | loaderType: this.loaderType_,
|
17072 | audioDisabled: this.audioDisabled_
|
17073 | })) {
|
17074 | return false;
|
17075 | }
|
17076 |
|
17077 | return true;
|
17078 | };
|
17079 |
|
17080 | _proto.getCurrentMediaInfo_ = function getCurrentMediaInfo_(segmentInfo) {
|
17081 | if (segmentInfo === void 0) {
|
17082 | segmentInfo = this.pendingSegment_;
|
17083 | }
|
17084 |
|
17085 | return segmentInfo && segmentInfo.trackInfo || this.currentMediaInfo_;
|
17086 | };
|
17087 |
|
17088 | _proto.getMediaInfo_ = function getMediaInfo_(segmentInfo) {
|
17089 | if (segmentInfo === void 0) {
|
17090 | segmentInfo = this.pendingSegment_;
|
17091 | }
|
17092 |
|
17093 | return this.getCurrentMediaInfo_(segmentInfo) || this.startingMediaInfo_;
|
17094 | };
|
17095 |
|
17096 | _proto.hasEnoughInfoToAppend_ = function hasEnoughInfoToAppend_() {
|
17097 | if (!this.sourceUpdater_.ready()) {
|
17098 | return false;
|
17099 | }
|
17100 |
|
17101 |
|
17102 |
|
17103 | if (this.waitingOnRemove_ || this.quotaExceededErrorRetryTimeout_) {
|
17104 | return false;
|
17105 | }
|
17106 |
|
17107 | var segmentInfo = this.pendingSegment_;
|
17108 | var trackInfo = this.getCurrentMediaInfo_();
|
17109 |
|
17110 |
|
17111 |
|
17112 | if (!segmentInfo || !trackInfo) {
|
17113 | return false;
|
17114 | }
|
17115 |
|
17116 | var hasAudio = trackInfo.hasAudio,
|
17117 | hasVideo = trackInfo.hasVideo,
|
17118 | isMuxed = trackInfo.isMuxed;
|
17119 |
|
17120 | if (hasVideo && !segmentInfo.videoTimingInfo) {
|
17121 | return false;
|
17122 | }
|
17123 |
|
17124 |
|
17125 | if (hasAudio && !this.audioDisabled_ && !isMuxed && !segmentInfo.audioTimingInfo) {
|
17126 | return false;
|
17127 | }
|
17128 |
|
17129 | if (shouldWaitForTimelineChange({
|
17130 | timelineChangeController: this.timelineChangeController_,
|
17131 | currentTimeline: this.currentTimeline_,
|
17132 | segmentTimeline: segmentInfo.timeline,
|
17133 | loaderType: this.loaderType_,
|
17134 | audioDisabled: this.audioDisabled_
|
17135 | })) {
|
17136 | return false;
|
17137 | }
|
17138 |
|
17139 | return true;
|
17140 | };
|
17141 |
|
17142 | _proto.handleData_ = function handleData_(simpleSegment, result) {
|
17143 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
17144 |
|
17145 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
17146 | return;
|
17147 | }
|
17148 |
|
17149 |
|
17150 |
|
17151 | if (this.callQueue_.length || !this.hasEnoughInfoToAppend_()) {
|
17152 | this.callQueue_.push(this.handleData_.bind(this, simpleSegment, result));
|
17153 | return;
|
17154 | }
|
17155 |
|
17156 | var segmentInfo = this.pendingSegment_;
|
17157 |
|
17158 | this.setTimeMapping_(segmentInfo.timeline);
|
17159 |
|
17160 | this.updateMediaSecondsLoaded_(segmentInfo.segment);
|
17161 |
|
17162 |
|
17163 |
|
17164 |
|
17165 |
|
17166 | if (this.mediaSource_.readyState === 'closed') {
|
17167 | return;
|
17168 | }
|
17169 |
|
17170 |
|
17171 |
|
17172 | if (simpleSegment.map) {
|
17173 | simpleSegment.map = this.initSegmentForMap(simpleSegment.map, true);
|
17174 |
|
17175 | segmentInfo.segment.map = simpleSegment.map;
|
17176 | }
|
17177 |
|
17178 |
|
17179 | if (simpleSegment.key) {
|
17180 | this.segmentKey(simpleSegment.key, true);
|
17181 | }
|
17182 |
|
17183 | segmentInfo.isFmp4 = simpleSegment.isFmp4;
|
17184 | segmentInfo.timingInfo = segmentInfo.timingInfo || {};
|
17185 |
|
17186 | if (segmentInfo.isFmp4) {
|
17187 | this.trigger('fmp4');
|
17188 | segmentInfo.timingInfo.start = segmentInfo[timingInfoPropertyForMedia(result.type)].start;
|
17189 | } else {
|
17190 | var trackInfo = this.getCurrentMediaInfo_();
|
17191 | var useVideoTimingInfo = this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
|
17192 | var firstVideoFrameTimeForData;
|
17193 |
|
17194 | if (useVideoTimingInfo) {
|
17195 | firstVideoFrameTimeForData = segmentInfo.videoTimingInfo.start;
|
17196 | }
|
17197 |
|
17198 |
|
17199 |
|
17200 |
|
17201 | segmentInfo.timingInfo.start = this.trueSegmentStart_({
|
17202 | currentStart: segmentInfo.timingInfo.start,
|
17203 | playlist: segmentInfo.playlist,
|
17204 | mediaIndex: segmentInfo.mediaIndex,
|
17205 | currentVideoTimestampOffset: this.sourceUpdater_.videoTimestampOffset(),
|
17206 | useVideoTimingInfo: useVideoTimingInfo,
|
17207 | firstVideoFrameTimeForData: firstVideoFrameTimeForData,
|
17208 | videoTimingInfo: segmentInfo.videoTimingInfo,
|
17209 | audioTimingInfo: segmentInfo.audioTimingInfo
|
17210 | });
|
17211 | }
|
17212 |
|
17213 |
|
17214 |
|
17215 |
|
17216 | this.updateAppendInitSegmentStatus(segmentInfo, result.type);
|
17217 |
|
17218 |
|
17219 |
|
17220 | this.updateSourceBufferTimestampOffset_(segmentInfo);
|
17221 |
|
17222 |
|
17223 | if (segmentInfo.isSyncRequest) {
|
17224 |
|
17225 |
|
17226 |
|
17227 | this.updateTimingInfoEnd_(segmentInfo);
|
17228 | this.syncController_.saveSegmentTimingInfo({
|
17229 | segmentInfo: segmentInfo,
|
17230 | shouldSaveTimelineMapping: this.loaderType_ === 'main'
|
17231 | });
|
17232 | var next = this.chooseNextRequest_();
|
17233 |
|
17234 |
|
17235 | if (next.mediaIndex !== segmentInfo.mediaIndex || next.partIndex !== segmentInfo.partIndex) {
|
17236 | this.logger_('sync segment was incorrect, not appending');
|
17237 | return;
|
17238 | }
|
17239 |
|
17240 |
|
17241 | this.logger_('sync segment was correct, appending');
|
17242 | }
|
17243 |
|
17244 |
|
17245 |
|
17246 |
|
17247 |
|
17248 | segmentInfo.hasAppendedData_ = true;
|
17249 |
|
17250 | this.processMetadataQueue_();
|
17251 | this.appendData_(segmentInfo, result);
|
17252 | };
|
17253 |
|
17254 | _proto.updateAppendInitSegmentStatus = function updateAppendInitSegmentStatus(segmentInfo, type) {
|
17255 |
|
17256 | if (this.loaderType_ === 'main' && typeof segmentInfo.timestampOffset === 'number' &&
|
17257 |
|
17258 | !segmentInfo.changedTimestampOffset) {
|
17259 |
|
17260 |
|
17261 | this.appendInitSegment_ = {
|
17262 | audio: true,
|
17263 | video: true
|
17264 | };
|
17265 | }
|
17266 |
|
17267 | if (this.playlistOfLastInitSegment_[type] !== segmentInfo.playlist) {
|
17268 |
|
17269 |
|
17270 | this.appendInitSegment_[type] = true;
|
17271 | }
|
17272 | };
|
17273 |
|
17274 | _proto.getInitSegmentAndUpdateState_ = function getInitSegmentAndUpdateState_(_ref4) {
|
17275 | var type = _ref4.type,
|
17276 | initSegment = _ref4.initSegment,
|
17277 | map = _ref4.map,
|
17278 | playlist = _ref4.playlist;
|
17279 |
|
17280 |
|
17281 |
|
17282 |
|
17283 |
|
17284 |
|
17285 | if (map) {
|
17286 | var id = initSegmentId(map);
|
17287 |
|
17288 | if (this.activeInitSegmentId_ === id) {
|
17289 |
|
17290 | return null;
|
17291 | }
|
17292 |
|
17293 |
|
17294 |
|
17295 |
|
17296 |
|
17297 | initSegment = this.initSegmentForMap(map, true).bytes;
|
17298 | this.activeInitSegmentId_ = id;
|
17299 | }
|
17300 |
|
17301 |
|
17302 |
|
17303 |
|
17304 |
|
17305 |
|
17306 | if (initSegment && this.appendInitSegment_[type]) {
|
17307 |
|
17308 |
|
17309 |
|
17310 | this.playlistOfLastInitSegment_[type] = playlist;
|
17311 |
|
17312 | this.appendInitSegment_[type] = false;
|
17313 |
|
17314 |
|
17315 | this.activeInitSegmentId_ = null;
|
17316 | return initSegment;
|
17317 | }
|
17318 |
|
17319 | return null;
|
17320 | };
|
17321 |
|
17322 | _proto.handleQuotaExceededError_ = function handleQuotaExceededError_(_ref5, error) {
|
17323 | var _this3 = this;
|
17324 |
|
17325 | var segmentInfo = _ref5.segmentInfo,
|
17326 | type = _ref5.type,
|
17327 | bytes = _ref5.bytes;
|
17328 | var audioBuffered = this.sourceUpdater_.audioBuffered();
|
17329 | var videoBuffered = this.sourceUpdater_.videoBuffered();
|
17330 |
|
17331 |
|
17332 |
|
17333 | if (audioBuffered.length > 1) {
|
17334 | this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the audio buffer: ' + timeRangesToArray(audioBuffered).join(', '));
|
17335 | }
|
17336 |
|
17337 | if (videoBuffered.length > 1) {
|
17338 | this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the video buffer: ' + timeRangesToArray(videoBuffered).join(', '));
|
17339 | }
|
17340 |
|
17341 | var audioBufferStart = audioBuffered.length ? audioBuffered.start(0) : 0;
|
17342 | var audioBufferEnd = audioBuffered.length ? audioBuffered.end(audioBuffered.length - 1) : 0;
|
17343 | var videoBufferStart = videoBuffered.length ? videoBuffered.start(0) : 0;
|
17344 | var videoBufferEnd = videoBuffered.length ? videoBuffered.end(videoBuffered.length - 1) : 0;
|
17345 |
|
17346 | if (audioBufferEnd - audioBufferStart <= MIN_BACK_BUFFER && videoBufferEnd - videoBufferStart <= MIN_BACK_BUFFER) {
|
17347 |
|
17348 |
|
17349 |
|
17350 |
|
17351 | this.logger_('On QUOTA_EXCEEDED_ERR, single segment too large to append to ' + 'buffer, triggering an error. ' + ("Appended byte length: " + bytes.byteLength + ", ") + ("audio buffer: " + timeRangesToArray(audioBuffered).join(', ') + ", ") + ("video buffer: " + timeRangesToArray(videoBuffered).join(', ') + ", "));
|
17352 | this.error({
|
17353 | message: 'Quota exceeded error with append of a single segment of content',
|
17354 | excludeUntil: Infinity
|
17355 | });
|
17356 | this.trigger('error');
|
17357 | return;
|
17358 | }
|
17359 |
|
17360 |
|
17361 |
|
17362 |
|
17363 |
|
17364 |
|
17365 |
|
17366 |
|
17367 |
|
17368 |
|
17369 |
|
17370 |
|
17371 |
|
17372 | this.waitingOnRemove_ = true;
|
17373 | this.callQueue_.push(this.appendToSourceBuffer_.bind(this, {
|
17374 | segmentInfo: segmentInfo,
|
17375 | type: type,
|
17376 | bytes: bytes
|
17377 | }));
|
17378 | var currentTime = this.currentTime_();
|
17379 |
|
17380 |
|
17381 | var timeToRemoveUntil = currentTime - MIN_BACK_BUFFER;
|
17382 | this.logger_("On QUOTA_EXCEEDED_ERR, removing audio/video from 0 to " + timeToRemoveUntil);
|
17383 | this.remove(0, timeToRemoveUntil, function () {
|
17384 | _this3.logger_("On QUOTA_EXCEEDED_ERR, retrying append in " + MIN_BACK_BUFFER + "s");
|
17385 |
|
17386 | _this3.waitingOnRemove_ = false;
|
17387 |
|
17388 |
|
17389 | _this3.quotaExceededErrorRetryTimeout_ = window.setTimeout(function () {
|
17390 | _this3.logger_('On QUOTA_EXCEEDED_ERR, re-processing call queue');
|
17391 |
|
17392 | _this3.quotaExceededErrorRetryTimeout_ = null;
|
17393 |
|
17394 | _this3.processCallQueue_();
|
17395 | }, MIN_BACK_BUFFER * 1000);
|
17396 | }, true);
|
17397 | };
|
17398 |
|
17399 | _proto.handleAppendError_ = function handleAppendError_(_ref6, error) {
|
17400 | var segmentInfo = _ref6.segmentInfo,
|
17401 | type = _ref6.type,
|
17402 | bytes = _ref6.bytes;
|
17403 |
|
17404 |
|
17405 | if (!error) {
|
17406 | return;
|
17407 | }
|
17408 |
|
17409 | if (error.code === QUOTA_EXCEEDED_ERR) {
|
17410 | this.handleQuotaExceededError_({
|
17411 | segmentInfo: segmentInfo,
|
17412 | type: type,
|
17413 | bytes: bytes
|
17414 | });
|
17415 |
|
17416 |
|
17417 | return;
|
17418 | }
|
17419 |
|
17420 | this.logger_('Received non QUOTA_EXCEEDED_ERR on append', error);
|
17421 | this.error(type + " append of " + bytes.length + "b failed for segment " + ("#" + segmentInfo.mediaIndex + " in playlist " + segmentInfo.playlist.id));
|
17422 |
|
17423 |
|
17424 |
|
17425 |
|
17426 |
|
17427 | this.trigger('appenderror');
|
17428 | };
|
17429 |
|
17430 | _proto.appendToSourceBuffer_ = function appendToSourceBuffer_(_ref7) {
|
17431 | var segmentInfo = _ref7.segmentInfo,
|
17432 | type = _ref7.type,
|
17433 | initSegment = _ref7.initSegment,
|
17434 | data = _ref7.data,
|
17435 | bytes = _ref7.bytes;
|
17436 |
|
17437 |
|
17438 | if (!bytes) {
|
17439 | var segments = [data];
|
17440 | var byteLength = data.byteLength;
|
17441 |
|
17442 | if (initSegment) {
|
17443 |
|
17444 |
|
17445 | segments.unshift(initSegment);
|
17446 | byteLength += initSegment.byteLength;
|
17447 | }
|
17448 |
|
17449 |
|
17450 |
|
17451 | bytes = concatSegments({
|
17452 | bytes: byteLength,
|
17453 | segments: segments
|
17454 | });
|
17455 | }
|
17456 |
|
17457 | this.sourceUpdater_.appendBuffer({
|
17458 | segmentInfo: segmentInfo,
|
17459 | type: type,
|
17460 | bytes: bytes
|
17461 | }, this.handleAppendError_.bind(this, {
|
17462 | segmentInfo: segmentInfo,
|
17463 | type: type,
|
17464 | bytes: bytes
|
17465 | }));
|
17466 | };
|
17467 |
|
17468 | _proto.handleSegmentTimingInfo_ = function handleSegmentTimingInfo_(type, requestId, segmentTimingInfo) {
|
17469 | if (!this.pendingSegment_ || requestId !== this.pendingSegment_.requestId) {
|
17470 | return;
|
17471 | }
|
17472 |
|
17473 | var segment = this.pendingSegment_.segment;
|
17474 | var timingInfoProperty = type + "TimingInfo";
|
17475 |
|
17476 | if (!segment[timingInfoProperty]) {
|
17477 | segment[timingInfoProperty] = {};
|
17478 | }
|
17479 |
|
17480 | segment[timingInfoProperty].transmuxerPrependedSeconds = segmentTimingInfo.prependedContentDuration || 0;
|
17481 | segment[timingInfoProperty].transmuxedPresentationStart = segmentTimingInfo.start.presentation;
|
17482 | segment[timingInfoProperty].transmuxedDecodeStart = segmentTimingInfo.start.decode;
|
17483 | segment[timingInfoProperty].transmuxedPresentationEnd = segmentTimingInfo.end.presentation;
|
17484 | segment[timingInfoProperty].transmuxedDecodeEnd = segmentTimingInfo.end.decode;
|
17485 |
|
17486 | segment[timingInfoProperty].baseMediaDecodeTime = segmentTimingInfo.baseMediaDecodeTime;
|
17487 | };
|
17488 |
|
17489 | _proto.appendData_ = function appendData_(segmentInfo, result) {
|
17490 | var type = result.type,
|
17491 | data = result.data;
|
17492 |
|
17493 | if (!data || !data.byteLength) {
|
17494 | return;
|
17495 | }
|
17496 |
|
17497 | if (type === 'audio' && this.audioDisabled_) {
|
17498 | return;
|
17499 | }
|
17500 |
|
17501 | var initSegment = this.getInitSegmentAndUpdateState_({
|
17502 | type: type,
|
17503 | initSegment: result.initSegment,
|
17504 | playlist: segmentInfo.playlist,
|
17505 | map: segmentInfo.isFmp4 ? segmentInfo.segment.map : null
|
17506 | });
|
17507 | this.appendToSourceBuffer_({
|
17508 | segmentInfo: segmentInfo,
|
17509 | type: type,
|
17510 | initSegment: initSegment,
|
17511 | data: data
|
17512 | });
|
17513 | }
|
17514 | |
17515 |
|
17516 |
|
17517 |
|
17518 |
|
17519 | ;
|
17520 |
|
17521 | _proto.loadSegment_ = function loadSegment_(segmentInfo) {
|
17522 | var _this4 = this;
|
17523 |
|
17524 | this.state = 'WAITING';
|
17525 | this.pendingSegment_ = segmentInfo;
|
17526 | this.trimBackBuffer_(segmentInfo);
|
17527 |
|
17528 | if (typeof segmentInfo.timestampOffset === 'number') {
|
17529 | if (this.transmuxer_) {
|
17530 | this.transmuxer_.postMessage({
|
17531 | action: 'clearAllMp4Captions'
|
17532 | });
|
17533 | }
|
17534 | }
|
17535 |
|
17536 | if (!this.hasEnoughInfoToLoad_()) {
|
17537 | this.loadQueue_.push(function () {
|
17538 |
|
17539 |
|
17540 | var options = _extends({}, segmentInfo, {
|
17541 | forceTimestampOffset: true
|
17542 | });
|
17543 |
|
17544 | _extends(segmentInfo, _this4.generateSegmentInfo_(options));
|
17545 |
|
17546 | _this4.isPendingTimestampOffset_ = false;
|
17547 |
|
17548 | _this4.updateTransmuxerAndRequestSegment_(segmentInfo);
|
17549 | });
|
17550 | return;
|
17551 | }
|
17552 |
|
17553 | this.updateTransmuxerAndRequestSegment_(segmentInfo);
|
17554 | };
|
17555 |
|
17556 | _proto.updateTransmuxerAndRequestSegment_ = function updateTransmuxerAndRequestSegment_(segmentInfo) {
|
17557 | var _this5 = this;
|
17558 |
|
17559 |
|
17560 |
|
17561 |
|
17562 |
|
17563 |
|
17564 | if (this.shouldUpdateTransmuxerTimestampOffset_(segmentInfo.timestampOffset)) {
|
17565 | this.gopBuffer_.length = 0;
|
17566 |
|
17567 | segmentInfo.gopsToAlignWith = [];
|
17568 | this.timeMapping_ = 0;
|
17569 |
|
17570 | this.transmuxer_.postMessage({
|
17571 | action: 'reset'
|
17572 | });
|
17573 | this.transmuxer_.postMessage({
|
17574 | action: 'setTimestampOffset',
|
17575 | timestampOffset: segmentInfo.timestampOffset
|
17576 | });
|
17577 | }
|
17578 |
|
17579 | var simpleSegment = this.createSimplifiedSegmentObj_(segmentInfo);
|
17580 | var isEndOfStream = this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex);
|
17581 | var isWalkingForward = this.mediaIndex !== null;
|
17582 | var isDiscontinuity = segmentInfo.timeline !== this.currentTimeline_ &&
|
17583 |
|
17584 | segmentInfo.timeline > 0;
|
17585 | var isEndOfTimeline = isEndOfStream || isWalkingForward && isDiscontinuity;
|
17586 | this.logger_("Requesting " + segmentInfoString(segmentInfo));
|
17587 |
|
17588 |
|
17589 |
|
17590 |
|
17591 |
|
17592 | if (simpleSegment.map && !simpleSegment.map.bytes) {
|
17593 | this.logger_('going to request init segment.');
|
17594 | this.appendInitSegment_ = {
|
17595 | video: true,
|
17596 | audio: true
|
17597 | };
|
17598 | }
|
17599 |
|
17600 | segmentInfo.abortRequests = mediaSegmentRequest({
|
17601 | xhr: this.vhs_.xhr,
|
17602 | xhrOptions: this.xhrOptions_,
|
17603 | decryptionWorker: this.decrypter_,
|
17604 | segment: simpleSegment,
|
17605 | abortFn: this.handleAbort_.bind(this, segmentInfo),
|
17606 | progressFn: this.handleProgress_.bind(this),
|
17607 | trackInfoFn: this.handleTrackInfo_.bind(this),
|
17608 | timingInfoFn: this.handleTimingInfo_.bind(this),
|
17609 | videoSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'video', segmentInfo.requestId),
|
17610 | audioSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'audio', segmentInfo.requestId),
|
17611 | captionsFn: this.handleCaptions_.bind(this),
|
17612 | isEndOfTimeline: isEndOfTimeline,
|
17613 | endedTimelineFn: function endedTimelineFn() {
|
17614 | _this5.logger_('received endedtimeline callback');
|
17615 | },
|
17616 | id3Fn: this.handleId3_.bind(this),
|
17617 | dataFn: this.handleData_.bind(this),
|
17618 | doneFn: this.segmentRequestFinished_.bind(this),
|
17619 | onTransmuxerLog: function onTransmuxerLog(_ref8) {
|
17620 | var message = _ref8.message,
|
17621 | level = _ref8.level,
|
17622 | stream = _ref8.stream;
|
17623 |
|
17624 | _this5.logger_(segmentInfoString(segmentInfo) + " logged from transmuxer stream " + stream + " as a " + level + ": " + message);
|
17625 | }
|
17626 | });
|
17627 | }
|
17628 | |
17629 |
|
17630 |
|
17631 |
|
17632 |
|
17633 |
|
17634 |
|
17635 |
|
17636 | ;
|
17637 |
|
17638 | _proto.trimBackBuffer_ = function trimBackBuffer_(segmentInfo) {
|
17639 | var removeToTime = safeBackBufferTrimTime(this.seekable_(), this.currentTime_(), this.playlist_.targetDuration || 10);
|
17640 |
|
17641 |
|
17642 |
|
17643 |
|
17644 |
|
17645 | if (removeToTime > 0) {
|
17646 | this.remove(0, removeToTime);
|
17647 | }
|
17648 | }
|
17649 | |
17650 |
|
17651 |
|
17652 |
|
17653 |
|
17654 |
|
17655 |
|
17656 |
|
17657 |
|
17658 | ;
|
17659 |
|
17660 | _proto.createSimplifiedSegmentObj_ = function createSimplifiedSegmentObj_(segmentInfo) {
|
17661 | var segment = segmentInfo.segment;
|
17662 | var part = segmentInfo.part;
|
17663 | var simpleSegment = {
|
17664 | resolvedUri: part ? part.resolvedUri : segment.resolvedUri,
|
17665 | byterange: part ? part.byterange : segment.byterange,
|
17666 | requestId: segmentInfo.requestId,
|
17667 | transmuxer: segmentInfo.transmuxer,
|
17668 | audioAppendStart: segmentInfo.audioAppendStart,
|
17669 | gopsToAlignWith: segmentInfo.gopsToAlignWith,
|
17670 | part: segmentInfo.part
|
17671 | };
|
17672 | var previousSegment = segmentInfo.playlist.segments[segmentInfo.mediaIndex - 1];
|
17673 |
|
17674 | if (previousSegment && previousSegment.timeline === segment.timeline) {
|
17675 |
|
17676 |
|
17677 |
|
17678 |
|
17679 |
|
17680 |
|
17681 |
|
17682 |
|
17683 | if (previousSegment.videoTimingInfo) {
|
17684 | simpleSegment.baseStartTime = previousSegment.videoTimingInfo.transmuxedDecodeEnd;
|
17685 | } else if (previousSegment.audioTimingInfo) {
|
17686 | simpleSegment.baseStartTime = previousSegment.audioTimingInfo.transmuxedDecodeEnd;
|
17687 | }
|
17688 | }
|
17689 |
|
17690 | if (segment.key) {
|
17691 |
|
17692 |
|
17693 | var iv = segment.key.iv || new Uint32Array([0, 0, 0, segmentInfo.mediaIndex + segmentInfo.playlist.mediaSequence]);
|
17694 | simpleSegment.key = this.segmentKey(segment.key);
|
17695 | simpleSegment.key.iv = iv;
|
17696 | }
|
17697 |
|
17698 | if (segment.map) {
|
17699 | simpleSegment.map = this.initSegmentForMap(segment.map);
|
17700 | }
|
17701 |
|
17702 | return simpleSegment;
|
17703 | };
|
17704 |
|
17705 | _proto.saveTransferStats_ = function saveTransferStats_(stats) {
|
17706 |
|
17707 |
|
17708 | this.mediaRequests += 1;
|
17709 |
|
17710 | if (stats) {
|
17711 | this.mediaBytesTransferred += stats.bytesReceived;
|
17712 | this.mediaTransferDuration += stats.roundTripTime;
|
17713 | }
|
17714 | };
|
17715 |
|
17716 | _proto.saveBandwidthRelatedStats_ = function saveBandwidthRelatedStats_(duration, stats) {
|
17717 |
|
17718 |
|
17719 |
|
17720 | this.pendingSegment_.byteLength = stats.bytesReceived;
|
17721 |
|
17722 | if (duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
|
17723 | this.logger_("Ignoring segment's bandwidth because its duration of " + duration + (" is less than the min to record " + MIN_SEGMENT_DURATION_TO_SAVE_STATS));
|
17724 | return;
|
17725 | }
|
17726 |
|
17727 | this.bandwidth = stats.bandwidth;
|
17728 | this.roundTrip = stats.roundTripTime;
|
17729 | };
|
17730 |
|
17731 | _proto.handleTimeout_ = function handleTimeout_() {
|
17732 |
|
17733 |
|
17734 | this.mediaRequestsTimedout += 1;
|
17735 | this.bandwidth = 1;
|
17736 | this.roundTrip = NaN;
|
17737 | this.trigger('bandwidthupdate');
|
17738 | }
|
17739 | |
17740 |
|
17741 |
|
17742 |
|
17743 |
|
17744 |
|
17745 | ;
|
17746 |
|
17747 | _proto.segmentRequestFinished_ = function segmentRequestFinished_(error, simpleSegment, result) {
|
17748 |
|
17749 |
|
17750 |
|
17751 |
|
17752 | if (this.callQueue_.length) {
|
17753 | this.callQueue_.push(this.segmentRequestFinished_.bind(this, error, simpleSegment, result));
|
17754 | return;
|
17755 | }
|
17756 |
|
17757 | this.saveTransferStats_(simpleSegment.stats);
|
17758 |
|
17759 | if (!this.pendingSegment_) {
|
17760 | return;
|
17761 | }
|
17762 |
|
17763 |
|
17764 |
|
17765 |
|
17766 |
|
17767 | if (simpleSegment.requestId !== this.pendingSegment_.requestId) {
|
17768 | return;
|
17769 | }
|
17770 |
|
17771 |
|
17772 | if (error) {
|
17773 | this.pendingSegment_ = null;
|
17774 | this.state = 'READY';
|
17775 |
|
17776 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
17777 | return;
|
17778 | }
|
17779 |
|
17780 | this.pause();
|
17781 |
|
17782 |
|
17783 |
|
17784 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
17785 | this.handleTimeout_();
|
17786 | return;
|
17787 | }
|
17788 |
|
17789 |
|
17790 |
|
17791 | this.mediaRequestsErrored += 1;
|
17792 | this.error(error);
|
17793 | this.trigger('error');
|
17794 | return;
|
17795 | }
|
17796 |
|
17797 | var segmentInfo = this.pendingSegment_;
|
17798 |
|
17799 |
|
17800 | this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats);
|
17801 | segmentInfo.endOfAllRequests = simpleSegment.endOfAllRequests;
|
17802 |
|
17803 | if (result.gopInfo) {
|
17804 | this.gopBuffer_ = updateGopBuffer(this.gopBuffer_, result.gopInfo, this.safeAppend_);
|
17805 | }
|
17806 |
|
17807 |
|
17808 |
|
17809 | this.state = 'APPENDING';
|
17810 |
|
17811 | this.trigger('appending');
|
17812 | this.waitForAppendsToComplete_(segmentInfo);
|
17813 | };
|
17814 |
|
17815 | _proto.setTimeMapping_ = function setTimeMapping_(timeline) {
|
17816 | var timelineMapping = this.syncController_.mappingForTimeline(timeline);
|
17817 |
|
17818 | if (timelineMapping !== null) {
|
17819 | this.timeMapping_ = timelineMapping;
|
17820 | }
|
17821 | };
|
17822 |
|
17823 | _proto.updateMediaSecondsLoaded_ = function updateMediaSecondsLoaded_(segment) {
|
17824 | if (typeof segment.start === 'number' && typeof segment.end === 'number') {
|
17825 | this.mediaSecondsLoaded += segment.end - segment.start;
|
17826 | } else {
|
17827 | this.mediaSecondsLoaded += segment.duration;
|
17828 | }
|
17829 | };
|
17830 |
|
17831 | _proto.shouldUpdateTransmuxerTimestampOffset_ = function shouldUpdateTransmuxerTimestampOffset_(timestampOffset) {
|
17832 | if (timestampOffset === null) {
|
17833 | return false;
|
17834 | }
|
17835 |
|
17836 |
|
17837 |
|
17838 | if (this.loaderType_ === 'main' && timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
|
17839 | return true;
|
17840 | }
|
17841 |
|
17842 | if (!this.audioDisabled_ && timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
|
17843 | return true;
|
17844 | }
|
17845 |
|
17846 | return false;
|
17847 | };
|
17848 |
|
17849 | _proto.trueSegmentStart_ = function trueSegmentStart_(_ref9) {
|
17850 | var currentStart = _ref9.currentStart,
|
17851 | playlist = _ref9.playlist,
|
17852 | mediaIndex = _ref9.mediaIndex,
|
17853 | firstVideoFrameTimeForData = _ref9.firstVideoFrameTimeForData,
|
17854 | currentVideoTimestampOffset = _ref9.currentVideoTimestampOffset,
|
17855 | useVideoTimingInfo = _ref9.useVideoTimingInfo,
|
17856 | videoTimingInfo = _ref9.videoTimingInfo,
|
17857 | audioTimingInfo = _ref9.audioTimingInfo;
|
17858 |
|
17859 | if (typeof currentStart !== 'undefined') {
|
17860 |
|
17861 | return currentStart;
|
17862 | }
|
17863 |
|
17864 | if (!useVideoTimingInfo) {
|
17865 | return audioTimingInfo.start;
|
17866 | }
|
17867 |
|
17868 | var previousSegment = playlist.segments[mediaIndex - 1];
|
17869 |
|
17870 |
|
17871 |
|
17872 |
|
17873 |
|
17874 | if (mediaIndex === 0 || !previousSegment || typeof previousSegment.start === 'undefined' || previousSegment.end !== firstVideoFrameTimeForData + currentVideoTimestampOffset) {
|
17875 | return firstVideoFrameTimeForData;
|
17876 | }
|
17877 |
|
17878 | return videoTimingInfo.start;
|
17879 | };
|
17880 |
|
17881 | _proto.waitForAppendsToComplete_ = function waitForAppendsToComplete_(segmentInfo) {
|
17882 | var trackInfo = this.getCurrentMediaInfo_(segmentInfo);
|
17883 |
|
17884 | if (!trackInfo) {
|
17885 | this.error({
|
17886 | message: 'No starting media returned, likely due to an unsupported media format.',
|
17887 | blacklistDuration: Infinity
|
17888 | });
|
17889 | this.trigger('error');
|
17890 | return;
|
17891 | }
|
17892 |
|
17893 |
|
17894 |
|
17895 |
|
17896 | var hasAudio = trackInfo.hasAudio,
|
17897 | hasVideo = trackInfo.hasVideo,
|
17898 | isMuxed = trackInfo.isMuxed;
|
17899 | var waitForVideo = this.loaderType_ === 'main' && hasVideo;
|
17900 | var waitForAudio = !this.audioDisabled_ && hasAudio && !isMuxed;
|
17901 | segmentInfo.waitingOnAppends = 0;
|
17902 |
|
17903 | if (!segmentInfo.hasAppendedData_) {
|
17904 | if (!segmentInfo.timingInfo && typeof segmentInfo.timestampOffset === 'number') {
|
17905 |
|
17906 |
|
17907 |
|
17908 |
|
17909 |
|
17910 |
|
17911 |
|
17912 |
|
17913 | this.isPendingTimestampOffset_ = true;
|
17914 | }
|
17915 |
|
17916 |
|
17917 | segmentInfo.timingInfo = {
|
17918 | start: 0
|
17919 | };
|
17920 | segmentInfo.waitingOnAppends++;
|
17921 |
|
17922 | if (!this.isPendingTimestampOffset_) {
|
17923 |
|
17924 | this.updateSourceBufferTimestampOffset_(segmentInfo);
|
17925 |
|
17926 |
|
17927 | this.processMetadataQueue_();
|
17928 | }
|
17929 |
|
17930 |
|
17931 | this.checkAppendsDone_(segmentInfo);
|
17932 | return;
|
17933 | }
|
17934 |
|
17935 |
|
17936 | if (waitForVideo) {
|
17937 | segmentInfo.waitingOnAppends++;
|
17938 | }
|
17939 |
|
17940 | if (waitForAudio) {
|
17941 | segmentInfo.waitingOnAppends++;
|
17942 | }
|
17943 |
|
17944 | if (waitForVideo) {
|
17945 | this.sourceUpdater_.videoQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
|
17946 | }
|
17947 |
|
17948 | if (waitForAudio) {
|
17949 | this.sourceUpdater_.audioQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
|
17950 | }
|
17951 | };
|
17952 |
|
17953 | _proto.checkAppendsDone_ = function checkAppendsDone_(segmentInfo) {
|
17954 | if (this.checkForAbort_(segmentInfo.requestId)) {
|
17955 | return;
|
17956 | }
|
17957 |
|
17958 | segmentInfo.waitingOnAppends--;
|
17959 |
|
17960 | if (segmentInfo.waitingOnAppends === 0) {
|
17961 | this.handleAppendsDone_();
|
17962 | }
|
17963 | };
|
17964 |
|
17965 | _proto.checkForIllegalMediaSwitch = function checkForIllegalMediaSwitch(trackInfo) {
|
17966 | var illegalMediaSwitchError = illegalMediaSwitch(this.loaderType_, this.getCurrentMediaInfo_(), trackInfo);
|
17967 |
|
17968 | if (illegalMediaSwitchError) {
|
17969 | this.error({
|
17970 | message: illegalMediaSwitchError,
|
17971 | blacklistDuration: Infinity
|
17972 | });
|
17973 | this.trigger('error');
|
17974 | return true;
|
17975 | }
|
17976 |
|
17977 | return false;
|
17978 | };
|
17979 |
|
17980 | _proto.updateSourceBufferTimestampOffset_ = function updateSourceBufferTimestampOffset_(segmentInfo) {
|
17981 | if (segmentInfo.timestampOffset === null ||
|
17982 |
|
17983 | typeof segmentInfo.timingInfo.start !== 'number' ||
|
17984 | segmentInfo.changedTimestampOffset ||
|
17985 | this.loaderType_ !== 'main') {
|
17986 | return;
|
17987 | }
|
17988 |
|
17989 | var didChange = false;
|
17990 |
|
17991 |
|
17992 |
|
17993 |
|
17994 | segmentInfo.timestampOffset -= segmentInfo.timingInfo.start;
|
17995 |
|
17996 |
|
17997 |
|
17998 | segmentInfo.changedTimestampOffset = true;
|
17999 |
|
18000 | if (segmentInfo.timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
|
18001 | this.sourceUpdater_.videoTimestampOffset(segmentInfo.timestampOffset);
|
18002 | didChange = true;
|
18003 | }
|
18004 |
|
18005 | if (segmentInfo.timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
|
18006 | this.sourceUpdater_.audioTimestampOffset(segmentInfo.timestampOffset);
|
18007 | didChange = true;
|
18008 | }
|
18009 |
|
18010 | if (didChange) {
|
18011 | this.trigger('timestampoffset');
|
18012 | }
|
18013 | };
|
18014 |
|
18015 | _proto.updateTimingInfoEnd_ = function updateTimingInfoEnd_(segmentInfo) {
|
18016 | segmentInfo.timingInfo = segmentInfo.timingInfo || {};
|
18017 | var trackInfo = this.getMediaInfo_();
|
18018 | var useVideoTimingInfo = this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
|
18019 | var prioritizedTimingInfo = useVideoTimingInfo && segmentInfo.videoTimingInfo ? segmentInfo.videoTimingInfo : segmentInfo.audioTimingInfo;
|
18020 |
|
18021 | if (!prioritizedTimingInfo) {
|
18022 | return;
|
18023 | }
|
18024 |
|
18025 | segmentInfo.timingInfo.end = typeof prioritizedTimingInfo.end === 'number' ?
|
18026 |
|
18027 |
|
18028 | prioritizedTimingInfo.end : prioritizedTimingInfo.start + segmentInfo.duration;
|
18029 | }
|
18030 | |
18031 |
|
18032 |
|
18033 |
|
18034 |
|
18035 |
|
18036 |
|
18037 | ;
|
18038 |
|
18039 | _proto.handleAppendsDone_ = function handleAppendsDone_() {
|
18040 |
|
18041 | if (this.pendingSegment_) {
|
18042 | this.trigger('appendsdone');
|
18043 | }
|
18044 |
|
18045 | if (!this.pendingSegment_) {
|
18046 | this.state = 'READY';
|
18047 |
|
18048 |
|
18049 | if (!this.paused()) {
|
18050 | this.monitorBuffer_();
|
18051 | }
|
18052 |
|
18053 | return;
|
18054 | }
|
18055 |
|
18056 | var segmentInfo = this.pendingSegment_;
|
18057 |
|
18058 |
|
18059 |
|
18060 | this.updateTimingInfoEnd_(segmentInfo);
|
18061 |
|
18062 | if (this.shouldSaveSegmentTimingInfo_) {
|
18063 |
|
18064 |
|
18065 |
|
18066 |
|
18067 |
|
18068 |
|
18069 |
|
18070 |
|
18071 |
|
18072 |
|
18073 |
|
18074 |
|
18075 |
|
18076 |
|
18077 |
|
18078 |
|
18079 |
|
18080 | this.syncController_.saveSegmentTimingInfo({
|
18081 | segmentInfo: segmentInfo,
|
18082 | shouldSaveTimelineMapping: this.loaderType_ === 'main'
|
18083 | });
|
18084 | }
|
18085 |
|
18086 | var segmentDurationMessage = getTroublesomeSegmentDurationMessage(segmentInfo, this.sourceType_);
|
18087 |
|
18088 | if (segmentDurationMessage) {
|
18089 | if (segmentDurationMessage.severity === 'warn') {
|
18090 | videojs.log.warn(segmentDurationMessage.message);
|
18091 | } else {
|
18092 | this.logger_(segmentDurationMessage.message);
|
18093 | }
|
18094 | }
|
18095 |
|
18096 | this.recordThroughput_(segmentInfo);
|
18097 | this.pendingSegment_ = null;
|
18098 | this.state = 'READY';
|
18099 |
|
18100 | if (segmentInfo.isSyncRequest) {
|
18101 | this.trigger('syncinfoupdate');
|
18102 |
|
18103 |
|
18104 |
|
18105 |
|
18106 | if (!segmentInfo.hasAppendedData_) {
|
18107 | this.logger_("Throwing away un-appended sync request " + segmentInfoString(segmentInfo));
|
18108 | return;
|
18109 | }
|
18110 | }
|
18111 |
|
18112 | this.logger_("Appended " + segmentInfoString(segmentInfo));
|
18113 | this.addSegmentMetadataCue_(segmentInfo);
|
18114 | this.fetchAtBuffer_ = true;
|
18115 |
|
18116 | if (this.currentTimeline_ !== segmentInfo.timeline) {
|
18117 | this.timelineChangeController_.lastTimelineChange({
|
18118 | type: this.loaderType_,
|
18119 | from: this.currentTimeline_,
|
18120 | to: segmentInfo.timeline
|
18121 | });
|
18122 |
|
18123 |
|
18124 |
|
18125 | if (this.loaderType_ === 'main' && !this.audioDisabled_) {
|
18126 | this.timelineChangeController_.lastTimelineChange({
|
18127 | type: 'audio',
|
18128 | from: this.currentTimeline_,
|
18129 | to: segmentInfo.timeline
|
18130 | });
|
18131 | }
|
18132 | }
|
18133 |
|
18134 | this.currentTimeline_ = segmentInfo.timeline;
|
18135 |
|
18136 |
|
18137 |
|
18138 |
|
18139 | this.trigger('syncinfoupdate');
|
18140 | var segment = segmentInfo.segment;
|
18141 |
|
18142 |
|
18143 |
|
18144 |
|
18145 | if (segment.end && this.currentTime_() - segment.end > segmentInfo.playlist.targetDuration * 3) {
|
18146 | this.resetEverything();
|
18147 | return;
|
18148 | }
|
18149 |
|
18150 | var isWalkingForward = this.mediaIndex !== null;
|
18151 |
|
18152 |
|
18153 | if (isWalkingForward) {
|
18154 | this.trigger('bandwidthupdate');
|
18155 | }
|
18156 |
|
18157 | this.trigger('progress');
|
18158 | this.mediaIndex = segmentInfo.mediaIndex;
|
18159 | this.partIndex = segmentInfo.partIndex;
|
18160 |
|
18161 |
|
18162 |
|
18163 | if (this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex)) {
|
18164 | this.endOfStream();
|
18165 | }
|
18166 |
|
18167 |
|
18168 | this.trigger('appended');
|
18169 |
|
18170 | if (segmentInfo.hasAppendedData_) {
|
18171 | this.mediaAppends++;
|
18172 | }
|
18173 |
|
18174 | if (!this.paused()) {
|
18175 | this.monitorBuffer_();
|
18176 | }
|
18177 | }
|
18178 | |
18179 |
|
18180 |
|
18181 |
|
18182 |
|
18183 |
|
18184 |
|
18185 |
|
18186 |
|
18187 | ;
|
18188 |
|
18189 | _proto.recordThroughput_ = function recordThroughput_(segmentInfo) {
|
18190 | if (segmentInfo.duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
|
18191 | this.logger_("Ignoring segment's throughput because its duration of " + segmentInfo.duration + (" is less than the min to record " + MIN_SEGMENT_DURATION_TO_SAVE_STATS));
|
18192 | return;
|
18193 | }
|
18194 |
|
18195 | var rate = this.throughput.rate;
|
18196 |
|
18197 |
|
18198 | var segmentProcessingTime = Date.now() - segmentInfo.endOfAllRequests + 1;
|
18199 |
|
18200 | var segmentProcessingThroughput = Math.floor(segmentInfo.byteLength / segmentProcessingTime * 8 * 1000);
|
18201 |
|
18202 |
|
18203 | this.throughput.rate += (segmentProcessingThroughput - rate) / ++this.throughput.count;
|
18204 | }
|
18205 | |
18206 |
|
18207 |
|
18208 |
|
18209 |
|
18210 |
|
18211 |
|
18212 |
|
18213 |
|
18214 | ;
|
18215 |
|
18216 | _proto.addSegmentMetadataCue_ = function addSegmentMetadataCue_(segmentInfo) {
|
18217 | if (!this.segmentMetadataTrack_) {
|
18218 | return;
|
18219 | }
|
18220 |
|
18221 | var segment = segmentInfo.segment;
|
18222 | var start = segment.start;
|
18223 | var end = segment.end;
|
18224 |
|
18225 | if (!finite(start) || !finite(end)) {
|
18226 | return;
|
18227 | }
|
18228 |
|
18229 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
18230 | var Cue = window.WebKitDataCue || window.VTTCue;
|
18231 | var value = {
|
18232 | custom: segment.custom,
|
18233 | dateTimeObject: segment.dateTimeObject,
|
18234 | dateTimeString: segment.dateTimeString,
|
18235 | bandwidth: segmentInfo.playlist.attributes.BANDWIDTH,
|
18236 | resolution: segmentInfo.playlist.attributes.RESOLUTION,
|
18237 | codecs: segmentInfo.playlist.attributes.CODECS,
|
18238 | byteLength: segmentInfo.byteLength,
|
18239 | uri: segmentInfo.uri,
|
18240 | timeline: segmentInfo.timeline,
|
18241 | playlist: segmentInfo.playlist.id,
|
18242 | start: start,
|
18243 | end: end
|
18244 | };
|
18245 | var data = JSON.stringify(value);
|
18246 | var cue = new Cue(start, end, data);
|
18247 |
|
18248 |
|
18249 | cue.value = value;
|
18250 | this.segmentMetadataTrack_.addCue(cue);
|
18251 | };
|
18252 |
|
18253 | return SegmentLoader;
|
18254 | }(videojs.EventTarget);
|
18255 |
|
18256 | function noop() {}
|
18257 |
|
18258 | var toTitleCase = function toTitleCase(string) {
|
18259 | if (typeof string !== 'string') {
|
18260 | return string;
|
18261 | }
|
18262 |
|
18263 | return string.replace(/./, function (w) {
|
18264 | return w.toUpperCase();
|
18265 | });
|
18266 | };
|
18267 |
|
18268 | var bufferTypes = ['video', 'audio'];
|
18269 |
|
18270 | var _updating = function updating(type, sourceUpdater) {
|
18271 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18272 | return sourceBuffer && sourceBuffer.updating || sourceUpdater.queuePending[type];
|
18273 | };
|
18274 |
|
18275 | var nextQueueIndexOfType = function nextQueueIndexOfType(type, queue) {
|
18276 | for (var i = 0; i < queue.length; i++) {
|
18277 | var queueEntry = queue[i];
|
18278 |
|
18279 | if (queueEntry.type === 'mediaSource') {
|
18280 |
|
18281 |
|
18282 | return null;
|
18283 | }
|
18284 |
|
18285 | if (queueEntry.type === type) {
|
18286 | return i;
|
18287 | }
|
18288 | }
|
18289 |
|
18290 | return null;
|
18291 | };
|
18292 |
|
18293 | var shiftQueue = function shiftQueue(type, sourceUpdater) {
|
18294 | if (sourceUpdater.queue.length === 0) {
|
18295 | return;
|
18296 | }
|
18297 |
|
18298 | var queueIndex = 0;
|
18299 | var queueEntry = sourceUpdater.queue[queueIndex];
|
18300 |
|
18301 | if (queueEntry.type === 'mediaSource') {
|
18302 | if (!sourceUpdater.updating() && sourceUpdater.mediaSource.readyState !== 'closed') {
|
18303 | sourceUpdater.queue.shift();
|
18304 | queueEntry.action(sourceUpdater);
|
18305 |
|
18306 | if (queueEntry.doneFn) {
|
18307 | queueEntry.doneFn();
|
18308 | }
|
18309 |
|
18310 |
|
18311 |
|
18312 |
|
18313 | shiftQueue('audio', sourceUpdater);
|
18314 | shiftQueue('video', sourceUpdater);
|
18315 | }
|
18316 |
|
18317 |
|
18318 |
|
18319 |
|
18320 | return;
|
18321 | }
|
18322 |
|
18323 | if (type === 'mediaSource') {
|
18324 |
|
18325 |
|
18326 |
|
18327 |
|
18328 | return;
|
18329 | }
|
18330 |
|
18331 |
|
18332 |
|
18333 |
|
18334 | if (!sourceUpdater.ready() || sourceUpdater.mediaSource.readyState === 'closed' || _updating(type, sourceUpdater)) {
|
18335 | return;
|
18336 | }
|
18337 |
|
18338 | if (queueEntry.type !== type) {
|
18339 | queueIndex = nextQueueIndexOfType(type, sourceUpdater.queue);
|
18340 |
|
18341 | if (queueIndex === null) {
|
18342 |
|
18343 |
|
18344 |
|
18345 | return;
|
18346 | }
|
18347 |
|
18348 | queueEntry = sourceUpdater.queue[queueIndex];
|
18349 | }
|
18350 |
|
18351 | sourceUpdater.queue.splice(queueIndex, 1);
|
18352 |
|
18353 |
|
18354 |
|
18355 |
|
18356 |
|
18357 |
|
18358 |
|
18359 | sourceUpdater.queuePending[type] = queueEntry;
|
18360 | queueEntry.action(type, sourceUpdater);
|
18361 |
|
18362 | if (!queueEntry.doneFn) {
|
18363 |
|
18364 | sourceUpdater.queuePending[type] = null;
|
18365 | shiftQueue(type, sourceUpdater);
|
18366 | return;
|
18367 | }
|
18368 | };
|
18369 |
|
18370 | var cleanupBuffer = function cleanupBuffer(type, sourceUpdater) {
|
18371 | var buffer = sourceUpdater[type + "Buffer"];
|
18372 | var titleType = toTitleCase(type);
|
18373 |
|
18374 | if (!buffer) {
|
18375 | return;
|
18376 | }
|
18377 |
|
18378 | buffer.removeEventListener('updateend', sourceUpdater["on" + titleType + "UpdateEnd_"]);
|
18379 | buffer.removeEventListener('error', sourceUpdater["on" + titleType + "Error_"]);
|
18380 | sourceUpdater.codecs[type] = null;
|
18381 | sourceUpdater[type + "Buffer"] = null;
|
18382 | };
|
18383 |
|
18384 | var inSourceBuffers = function inSourceBuffers(mediaSource, sourceBuffer) {
|
18385 | return mediaSource && sourceBuffer && Array.prototype.indexOf.call(mediaSource.sourceBuffers, sourceBuffer) !== -1;
|
18386 | };
|
18387 |
|
18388 | var actions = {
|
18389 | appendBuffer: function appendBuffer(bytes, segmentInfo, onError) {
|
18390 | return function (type, sourceUpdater) {
|
18391 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18392 |
|
18393 |
|
18394 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
18395 | return;
|
18396 | }
|
18397 |
|
18398 | sourceUpdater.logger_("Appending segment " + segmentInfo.mediaIndex + "'s " + bytes.length + " bytes to " + type + "Buffer");
|
18399 |
|
18400 | try {
|
18401 | sourceBuffer.appendBuffer(bytes);
|
18402 | } catch (e) {
|
18403 | sourceUpdater.logger_("Error with code " + e.code + " " + (e.code === QUOTA_EXCEEDED_ERR ? '(QUOTA_EXCEEDED_ERR) ' : '') + ("when appending segment " + segmentInfo.mediaIndex + " to " + type + "Buffer"));
|
18404 | sourceUpdater.queuePending[type] = null;
|
18405 | onError(e);
|
18406 | }
|
18407 | };
|
18408 | },
|
18409 | remove: function remove(start, end) {
|
18410 | return function (type, sourceUpdater) {
|
18411 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18412 |
|
18413 |
|
18414 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
18415 | return;
|
18416 | }
|
18417 |
|
18418 | sourceUpdater.logger_("Removing " + start + " to " + end + " from " + type + "Buffer");
|
18419 |
|
18420 | try {
|
18421 | sourceBuffer.remove(start, end);
|
18422 | } catch (e) {
|
18423 | sourceUpdater.logger_("Remove " + start + " to " + end + " from " + type + "Buffer failed");
|
18424 | }
|
18425 | };
|
18426 | },
|
18427 | timestampOffset: function timestampOffset(offset) {
|
18428 | return function (type, sourceUpdater) {
|
18429 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18430 |
|
18431 |
|
18432 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
18433 | return;
|
18434 | }
|
18435 |
|
18436 | sourceUpdater.logger_("Setting " + type + "timestampOffset to " + offset);
|
18437 | sourceBuffer.timestampOffset = offset;
|
18438 | };
|
18439 | },
|
18440 | callback: function callback(_callback) {
|
18441 | return function (type, sourceUpdater) {
|
18442 | _callback();
|
18443 | };
|
18444 | },
|
18445 | endOfStream: function endOfStream(error) {
|
18446 | return function (sourceUpdater) {
|
18447 | if (sourceUpdater.mediaSource.readyState !== 'open') {
|
18448 | return;
|
18449 | }
|
18450 |
|
18451 | sourceUpdater.logger_("Calling mediaSource endOfStream(" + (error || '') + ")");
|
18452 |
|
18453 | try {
|
18454 | sourceUpdater.mediaSource.endOfStream(error);
|
18455 | } catch (e) {
|
18456 | videojs.log.warn('Failed to call media source endOfStream', e);
|
18457 | }
|
18458 | };
|
18459 | },
|
18460 | duration: function duration(_duration) {
|
18461 | return function (sourceUpdater) {
|
18462 | sourceUpdater.logger_("Setting mediaSource duration to " + _duration);
|
18463 |
|
18464 | try {
|
18465 | sourceUpdater.mediaSource.duration = _duration;
|
18466 | } catch (e) {
|
18467 | videojs.log.warn('Failed to set media source duration', e);
|
18468 | }
|
18469 | };
|
18470 | },
|
18471 | abort: function abort() {
|
18472 | return function (type, sourceUpdater) {
|
18473 | if (sourceUpdater.mediaSource.readyState !== 'open') {
|
18474 | return;
|
18475 | }
|
18476 |
|
18477 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18478 |
|
18479 |
|
18480 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
18481 | return;
|
18482 | }
|
18483 |
|
18484 | sourceUpdater.logger_("calling abort on " + type + "Buffer");
|
18485 |
|
18486 | try {
|
18487 | sourceBuffer.abort();
|
18488 | } catch (e) {
|
18489 | videojs.log.warn("Failed to abort on " + type + "Buffer", e);
|
18490 | }
|
18491 | };
|
18492 | },
|
18493 | addSourceBuffer: function addSourceBuffer(type, codec) {
|
18494 | return function (sourceUpdater) {
|
18495 | var titleType = toTitleCase(type);
|
18496 | var mime = getMimeForCodec(codec);
|
18497 | sourceUpdater.logger_("Adding " + type + "Buffer with codec " + codec + " to mediaSource");
|
18498 | var sourceBuffer = sourceUpdater.mediaSource.addSourceBuffer(mime);
|
18499 | sourceBuffer.addEventListener('updateend', sourceUpdater["on" + titleType + "UpdateEnd_"]);
|
18500 | sourceBuffer.addEventListener('error', sourceUpdater["on" + titleType + "Error_"]);
|
18501 | sourceUpdater.codecs[type] = codec;
|
18502 | sourceUpdater[type + "Buffer"] = sourceBuffer;
|
18503 | };
|
18504 | },
|
18505 | removeSourceBuffer: function removeSourceBuffer(type) {
|
18506 | return function (sourceUpdater) {
|
18507 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18508 | cleanupBuffer(type, sourceUpdater);
|
18509 |
|
18510 |
|
18511 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
18512 | return;
|
18513 | }
|
18514 |
|
18515 | sourceUpdater.logger_("Removing " + type + "Buffer with codec " + sourceUpdater.codecs[type] + " from mediaSource");
|
18516 |
|
18517 | try {
|
18518 | sourceUpdater.mediaSource.removeSourceBuffer(sourceBuffer);
|
18519 | } catch (e) {
|
18520 | videojs.log.warn("Failed to removeSourceBuffer " + type + "Buffer", e);
|
18521 | }
|
18522 | };
|
18523 | },
|
18524 | changeType: function changeType(codec) {
|
18525 | return function (type, sourceUpdater) {
|
18526 | var sourceBuffer = sourceUpdater[type + "Buffer"];
|
18527 | var mime = getMimeForCodec(codec);
|
18528 |
|
18529 |
|
18530 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
18531 | return;
|
18532 | }
|
18533 |
|
18534 |
|
18535 | if (sourceUpdater.codecs[type] === codec) {
|
18536 | return;
|
18537 | }
|
18538 |
|
18539 | sourceUpdater.logger_("changing " + type + "Buffer codec from " + sourceUpdater.codecs[type] + " to " + codec);
|
18540 | sourceBuffer.changeType(mime);
|
18541 | sourceUpdater.codecs[type] = codec;
|
18542 | };
|
18543 | }
|
18544 | };
|
18545 |
|
18546 | var pushQueue = function pushQueue(_ref) {
|
18547 | var type = _ref.type,
|
18548 | sourceUpdater = _ref.sourceUpdater,
|
18549 | action = _ref.action,
|
18550 | doneFn = _ref.doneFn,
|
18551 | name = _ref.name;
|
18552 | sourceUpdater.queue.push({
|
18553 | type: type,
|
18554 | action: action,
|
18555 | doneFn: doneFn,
|
18556 | name: name
|
18557 | });
|
18558 | shiftQueue(type, sourceUpdater);
|
18559 | };
|
18560 |
|
18561 | var onUpdateend = function onUpdateend(type, sourceUpdater) {
|
18562 | return function (e) {
|
18563 |
|
18564 |
|
18565 |
|
18566 |
|
18567 |
|
18568 |
|
18569 | if (sourceUpdater.queuePending[type]) {
|
18570 | var doneFn = sourceUpdater.queuePending[type].doneFn;
|
18571 | sourceUpdater.queuePending[type] = null;
|
18572 |
|
18573 | if (doneFn) {
|
18574 |
|
18575 | doneFn(sourceUpdater[type + "Error_"]);
|
18576 | }
|
18577 | }
|
18578 |
|
18579 | shiftQueue(type, sourceUpdater);
|
18580 | };
|
18581 | };
|
18582 |
|
18583 |
|
18584 |
|
18585 |
|
18586 |
|
18587 |
|
18588 |
|
18589 |
|
18590 |
|
18591 |
|
18592 |
|
18593 |
|
18594 | var SourceUpdater = function (_videojs$EventTarget) {
|
18595 | _inheritsLoose(SourceUpdater, _videojs$EventTarget);
|
18596 |
|
18597 | function SourceUpdater(mediaSource) {
|
18598 | var _this;
|
18599 |
|
18600 | _this = _videojs$EventTarget.call(this) || this;
|
18601 | _this.mediaSource = mediaSource;
|
18602 |
|
18603 | _this.sourceopenListener_ = function () {
|
18604 | return shiftQueue('mediaSource', _assertThisInitialized(_this));
|
18605 | };
|
18606 |
|
18607 | _this.mediaSource.addEventListener('sourceopen', _this.sourceopenListener_);
|
18608 |
|
18609 | _this.logger_ = logger('SourceUpdater');
|
18610 |
|
18611 | _this.audioTimestampOffset_ = 0;
|
18612 | _this.videoTimestampOffset_ = 0;
|
18613 | _this.queue = [];
|
18614 | _this.queuePending = {
|
18615 | audio: null,
|
18616 | video: null
|
18617 | };
|
18618 | _this.delayedAudioAppendQueue_ = [];
|
18619 | _this.videoAppendQueued_ = false;
|
18620 | _this.codecs = {};
|
18621 | _this.onVideoUpdateEnd_ = onUpdateend('video', _assertThisInitialized(_this));
|
18622 | _this.onAudioUpdateEnd_ = onUpdateend('audio', _assertThisInitialized(_this));
|
18623 |
|
18624 | _this.onVideoError_ = function (e) {
|
18625 |
|
18626 | _this.videoError_ = e;
|
18627 | };
|
18628 |
|
18629 | _this.onAudioError_ = function (e) {
|
18630 |
|
18631 | _this.audioError_ = e;
|
18632 | };
|
18633 |
|
18634 | _this.createdSourceBuffers_ = false;
|
18635 | _this.initializedEme_ = false;
|
18636 | _this.triggeredReady_ = false;
|
18637 | return _this;
|
18638 | }
|
18639 |
|
18640 | var _proto = SourceUpdater.prototype;
|
18641 |
|
18642 | _proto.initializedEme = function initializedEme() {
|
18643 | this.initializedEme_ = true;
|
18644 | this.triggerReady();
|
18645 | };
|
18646 |
|
18647 | _proto.hasCreatedSourceBuffers = function hasCreatedSourceBuffers() {
|
18648 |
|
18649 |
|
18650 | return this.createdSourceBuffers_;
|
18651 | };
|
18652 |
|
18653 | _proto.hasInitializedAnyEme = function hasInitializedAnyEme() {
|
18654 | return this.initializedEme_;
|
18655 | };
|
18656 |
|
18657 | _proto.ready = function ready() {
|
18658 | return this.hasCreatedSourceBuffers() && this.hasInitializedAnyEme();
|
18659 | };
|
18660 |
|
18661 | _proto.createSourceBuffers = function createSourceBuffers(codecs) {
|
18662 | if (this.hasCreatedSourceBuffers()) {
|
18663 |
|
18664 | return;
|
18665 | }
|
18666 |
|
18667 |
|
18668 |
|
18669 | this.addOrChangeSourceBuffers(codecs);
|
18670 | this.createdSourceBuffers_ = true;
|
18671 | this.trigger('createdsourcebuffers');
|
18672 | this.triggerReady();
|
18673 | };
|
18674 |
|
18675 | _proto.triggerReady = function triggerReady() {
|
18676 |
|
18677 |
|
18678 |
|
18679 |
|
18680 |
|
18681 |
|
18682 | if (this.ready() && !this.triggeredReady_) {
|
18683 | this.triggeredReady_ = true;
|
18684 | this.trigger('ready');
|
18685 | }
|
18686 | }
|
18687 | |
18688 |
|
18689 |
|
18690 |
|
18691 |
|
18692 |
|
18693 |
|
18694 |
|
18695 |
|
18696 | ;
|
18697 |
|
18698 | _proto.addSourceBuffer = function addSourceBuffer(type, codec) {
|
18699 | pushQueue({
|
18700 | type: 'mediaSource',
|
18701 | sourceUpdater: this,
|
18702 | action: actions.addSourceBuffer(type, codec),
|
18703 | name: 'addSourceBuffer'
|
18704 | });
|
18705 | }
|
18706 | |
18707 |
|
18708 |
|
18709 |
|
18710 |
|
18711 |
|
18712 | ;
|
18713 |
|
18714 | _proto.abort = function abort(type) {
|
18715 | pushQueue({
|
18716 | type: type,
|
18717 | sourceUpdater: this,
|
18718 | action: actions.abort(type),
|
18719 | name: 'abort'
|
18720 | });
|
18721 | }
|
18722 | |
18723 |
|
18724 |
|
18725 |
|
18726 |
|
18727 |
|
18728 |
|
18729 | ;
|
18730 |
|
18731 | _proto.removeSourceBuffer = function removeSourceBuffer(type) {
|
18732 | if (!this.canRemoveSourceBuffer()) {
|
18733 | videojs.log.error('removeSourceBuffer is not supported!');
|
18734 | return;
|
18735 | }
|
18736 |
|
18737 | pushQueue({
|
18738 | type: 'mediaSource',
|
18739 | sourceUpdater: this,
|
18740 | action: actions.removeSourceBuffer(type),
|
18741 | name: 'removeSourceBuffer'
|
18742 | });
|
18743 | }
|
18744 | |
18745 |
|
18746 |
|
18747 |
|
18748 |
|
18749 |
|
18750 |
|
18751 | ;
|
18752 |
|
18753 | _proto.canRemoveSourceBuffer = function canRemoveSourceBuffer() {
|
18754 |
|
18755 |
|
18756 |
|
18757 |
|
18758 | return !videojs.browser.IE_VERSION && !videojs.browser.IS_FIREFOX && window.MediaSource && window.MediaSource.prototype && typeof window.MediaSource.prototype.removeSourceBuffer === 'function';
|
18759 | }
|
18760 | |
18761 |
|
18762 |
|
18763 |
|
18764 |
|
18765 |
|
18766 |
|
18767 | ;
|
18768 |
|
18769 | SourceUpdater.canChangeType = function canChangeType() {
|
18770 | return window.SourceBuffer && window.SourceBuffer.prototype && typeof window.SourceBuffer.prototype.changeType === 'function';
|
18771 | }
|
18772 | |
18773 |
|
18774 |
|
18775 |
|
18776 |
|
18777 |
|
18778 |
|
18779 | ;
|
18780 |
|
18781 | _proto.canChangeType = function canChangeType() {
|
18782 | return this.constructor.canChangeType();
|
18783 | }
|
18784 | |
18785 |
|
18786 |
|
18787 |
|
18788 |
|
18789 |
|
18790 |
|
18791 |
|
18792 |
|
18793 | ;
|
18794 |
|
18795 | _proto.changeType = function changeType(type, codec) {
|
18796 | if (!this.canChangeType()) {
|
18797 | videojs.log.error('changeType is not supported!');
|
18798 | return;
|
18799 | }
|
18800 |
|
18801 | pushQueue({
|
18802 | type: type,
|
18803 | sourceUpdater: this,
|
18804 | action: actions.changeType(codec),
|
18805 | name: 'changeType'
|
18806 | });
|
18807 | }
|
18808 | |
18809 |
|
18810 |
|
18811 |
|
18812 |
|
18813 |
|
18814 |
|
18815 | ;
|
18816 |
|
18817 | _proto.addOrChangeSourceBuffers = function addOrChangeSourceBuffers(codecs) {
|
18818 | var _this2 = this;
|
18819 |
|
18820 | if (!codecs || typeof codecs !== 'object' || Object.keys(codecs).length === 0) {
|
18821 | throw new Error('Cannot addOrChangeSourceBuffers to undefined codecs');
|
18822 | }
|
18823 |
|
18824 | Object.keys(codecs).forEach(function (type) {
|
18825 | var codec = codecs[type];
|
18826 |
|
18827 | if (!_this2.hasCreatedSourceBuffers()) {
|
18828 | return _this2.addSourceBuffer(type, codec);
|
18829 | }
|
18830 |
|
18831 | if (_this2.canChangeType()) {
|
18832 | _this2.changeType(type, codec);
|
18833 | }
|
18834 | });
|
18835 | }
|
18836 | |
18837 |
|
18838 |
|
18839 |
|
18840 |
|
18841 |
|
18842 |
|
18843 | ;
|
18844 |
|
18845 | _proto.appendBuffer = function appendBuffer(options, doneFn) {
|
18846 | var _this3 = this;
|
18847 |
|
18848 | var segmentInfo = options.segmentInfo,
|
18849 | type = options.type,
|
18850 | bytes = options.bytes;
|
18851 | this.processedAppend_ = true;
|
18852 |
|
18853 | if (type === 'audio' && this.videoBuffer && !this.videoAppendQueued_) {
|
18854 | this.delayedAudioAppendQueue_.push([options, doneFn]);
|
18855 | this.logger_("delayed audio append of " + bytes.length + " until video append");
|
18856 | return;
|
18857 | }
|
18858 |
|
18859 |
|
18860 |
|
18861 |
|
18862 |
|
18863 | var onError = doneFn;
|
18864 | pushQueue({
|
18865 | type: type,
|
18866 | sourceUpdater: this,
|
18867 | action: actions.appendBuffer(bytes, segmentInfo || {
|
18868 | mediaIndex: -1
|
18869 | }, onError),
|
18870 | doneFn: doneFn,
|
18871 | name: 'appendBuffer'
|
18872 | });
|
18873 |
|
18874 | if (type === 'video') {
|
18875 | this.videoAppendQueued_ = true;
|
18876 |
|
18877 | if (!this.delayedAudioAppendQueue_.length) {
|
18878 | return;
|
18879 | }
|
18880 |
|
18881 | var queue = this.delayedAudioAppendQueue_.slice();
|
18882 | this.logger_("queuing delayed audio " + queue.length + " appendBuffers");
|
18883 | this.delayedAudioAppendQueue_.length = 0;
|
18884 | queue.forEach(function (que) {
|
18885 | _this3.appendBuffer.apply(_this3, que);
|
18886 | });
|
18887 | }
|
18888 | }
|
18889 | |
18890 |
|
18891 |
|
18892 |
|
18893 |
|
18894 |
|
18895 | ;
|
18896 |
|
18897 | _proto.audioBuffered = function audioBuffered() {
|
18898 |
|
18899 |
|
18900 | if (!inSourceBuffers(this.mediaSource, this.audioBuffer)) {
|
18901 | return videojs.createTimeRange();
|
18902 | }
|
18903 |
|
18904 | return this.audioBuffer.buffered ? this.audioBuffer.buffered : videojs.createTimeRange();
|
18905 | }
|
18906 | |
18907 |
|
18908 |
|
18909 |
|
18910 |
|
18911 |
|
18912 | ;
|
18913 |
|
18914 | _proto.videoBuffered = function videoBuffered() {
|
18915 |
|
18916 |
|
18917 | if (!inSourceBuffers(this.mediaSource, this.videoBuffer)) {
|
18918 | return videojs.createTimeRange();
|
18919 | }
|
18920 |
|
18921 | return this.videoBuffer.buffered ? this.videoBuffer.buffered : videojs.createTimeRange();
|
18922 | }
|
18923 | |
18924 |
|
18925 |
|
18926 |
|
18927 |
|
18928 |
|
18929 | ;
|
18930 |
|
18931 | _proto.buffered = function buffered() {
|
18932 | var video = inSourceBuffers(this.mediaSource, this.videoBuffer) ? this.videoBuffer : null;
|
18933 | var audio = inSourceBuffers(this.mediaSource, this.audioBuffer) ? this.audioBuffer : null;
|
18934 |
|
18935 | if (audio && !video) {
|
18936 | return this.audioBuffered();
|
18937 | }
|
18938 |
|
18939 | if (video && !audio) {
|
18940 | return this.videoBuffered();
|
18941 | }
|
18942 |
|
18943 | return bufferIntersection(this.audioBuffered(), this.videoBuffered());
|
18944 | }
|
18945 | |
18946 |
|
18947 |
|
18948 |
|
18949 |
|
18950 |
|
18951 |
|
18952 |
|
18953 |
|
18954 | ;
|
18955 |
|
18956 | _proto.setDuration = function setDuration(duration, doneFn) {
|
18957 | if (doneFn === void 0) {
|
18958 | doneFn = noop;
|
18959 | }
|
18960 |
|
18961 |
|
18962 |
|
18963 |
|
18964 |
|
18965 | pushQueue({
|
18966 | type: 'mediaSource',
|
18967 | sourceUpdater: this,
|
18968 | action: actions.duration(duration),
|
18969 | name: 'duration',
|
18970 | doneFn: doneFn
|
18971 | });
|
18972 | }
|
18973 | |
18974 |
|
18975 |
|
18976 |
|
18977 |
|
18978 |
|
18979 |
|
18980 |
|
18981 |
|
18982 |
|
18983 | ;
|
18984 |
|
18985 | _proto.endOfStream = function endOfStream(error, doneFn) {
|
18986 | if (error === void 0) {
|
18987 | error = null;
|
18988 | }
|
18989 |
|
18990 | if (doneFn === void 0) {
|
18991 | doneFn = noop;
|
18992 | }
|
18993 |
|
18994 | if (typeof error !== 'string') {
|
18995 | error = undefined;
|
18996 | }
|
18997 |
|
18998 |
|
18999 |
|
19000 |
|
19001 |
|
19002 | pushQueue({
|
19003 | type: 'mediaSource',
|
19004 | sourceUpdater: this,
|
19005 | action: actions.endOfStream(error),
|
19006 | name: 'endOfStream',
|
19007 | doneFn: doneFn
|
19008 | });
|
19009 | }
|
19010 | |
19011 |
|
19012 |
|
19013 |
|
19014 |
|
19015 |
|
19016 |
|
19017 |
|
19018 |
|
19019 | ;
|
19020 |
|
19021 | _proto.removeAudio = function removeAudio(start, end, done) {
|
19022 | if (done === void 0) {
|
19023 | done = noop;
|
19024 | }
|
19025 |
|
19026 | if (!this.audioBuffered().length || this.audioBuffered().end(0) === 0) {
|
19027 | done();
|
19028 | return;
|
19029 | }
|
19030 |
|
19031 | pushQueue({
|
19032 | type: 'audio',
|
19033 | sourceUpdater: this,
|
19034 | action: actions.remove(start, end),
|
19035 | doneFn: done,
|
19036 | name: 'remove'
|
19037 | });
|
19038 | }
|
19039 | |
19040 |
|
19041 |
|
19042 |
|
19043 |
|
19044 |
|
19045 |
|
19046 |
|
19047 |
|
19048 | ;
|
19049 |
|
19050 | _proto.removeVideo = function removeVideo(start, end, done) {
|
19051 | if (done === void 0) {
|
19052 | done = noop;
|
19053 | }
|
19054 |
|
19055 | if (!this.videoBuffered().length || this.videoBuffered().end(0) === 0) {
|
19056 | done();
|
19057 | return;
|
19058 | }
|
19059 |
|
19060 | pushQueue({
|
19061 | type: 'video',
|
19062 | sourceUpdater: this,
|
19063 | action: actions.remove(start, end),
|
19064 | doneFn: done,
|
19065 | name: 'remove'
|
19066 | });
|
19067 | }
|
19068 | |
19069 |
|
19070 |
|
19071 |
|
19072 |
|
19073 | ;
|
19074 |
|
19075 | _proto.updating = function updating() {
|
19076 |
|
19077 | if (_updating('audio', this) || _updating('video', this)) {
|
19078 | return true;
|
19079 | }
|
19080 |
|
19081 | return false;
|
19082 | }
|
19083 | |
19084 |
|
19085 |
|
19086 |
|
19087 |
|
19088 | ;
|
19089 |
|
19090 | _proto.audioTimestampOffset = function audioTimestampOffset(offset) {
|
19091 | if (typeof offset !== 'undefined' && this.audioBuffer &&
|
19092 | this.audioTimestampOffset_ !== offset) {
|
19093 | pushQueue({
|
19094 | type: 'audio',
|
19095 | sourceUpdater: this,
|
19096 | action: actions.timestampOffset(offset),
|
19097 | name: 'timestampOffset'
|
19098 | });
|
19099 | this.audioTimestampOffset_ = offset;
|
19100 | }
|
19101 |
|
19102 | return this.audioTimestampOffset_;
|
19103 | }
|
19104 | |
19105 |
|
19106 |
|
19107 |
|
19108 |
|
19109 | ;
|
19110 |
|
19111 | _proto.videoTimestampOffset = function videoTimestampOffset(offset) {
|
19112 | if (typeof offset !== 'undefined' && this.videoBuffer &&
|
19113 | this.videoTimestampOffset !== offset) {
|
19114 | pushQueue({
|
19115 | type: 'video',
|
19116 | sourceUpdater: this,
|
19117 | action: actions.timestampOffset(offset),
|
19118 | name: 'timestampOffset'
|
19119 | });
|
19120 | this.videoTimestampOffset_ = offset;
|
19121 | }
|
19122 |
|
19123 | return this.videoTimestampOffset_;
|
19124 | }
|
19125 | |
19126 |
|
19127 |
|
19128 |
|
19129 |
|
19130 |
|
19131 |
|
19132 | ;
|
19133 |
|
19134 | _proto.audioQueueCallback = function audioQueueCallback(callback) {
|
19135 | if (!this.audioBuffer) {
|
19136 | return;
|
19137 | }
|
19138 |
|
19139 | pushQueue({
|
19140 | type: 'audio',
|
19141 | sourceUpdater: this,
|
19142 | action: actions.callback(callback),
|
19143 | name: 'callback'
|
19144 | });
|
19145 | }
|
19146 | |
19147 |
|
19148 |
|
19149 |
|
19150 |
|
19151 |
|
19152 |
|
19153 | ;
|
19154 |
|
19155 | _proto.videoQueueCallback = function videoQueueCallback(callback) {
|
19156 | if (!this.videoBuffer) {
|
19157 | return;
|
19158 | }
|
19159 |
|
19160 | pushQueue({
|
19161 | type: 'video',
|
19162 | sourceUpdater: this,
|
19163 | action: actions.callback(callback),
|
19164 | name: 'callback'
|
19165 | });
|
19166 | }
|
19167 | |
19168 |
|
19169 |
|
19170 | ;
|
19171 |
|
19172 | _proto.dispose = function dispose() {
|
19173 | var _this4 = this;
|
19174 |
|
19175 | this.trigger('dispose');
|
19176 | bufferTypes.forEach(function (type) {
|
19177 | _this4.abort(type);
|
19178 |
|
19179 | if (_this4.canRemoveSourceBuffer()) {
|
19180 | _this4.removeSourceBuffer(type);
|
19181 | } else {
|
19182 | _this4[type + "QueueCallback"](function () {
|
19183 | return cleanupBuffer(type, _this4);
|
19184 | });
|
19185 | }
|
19186 | });
|
19187 | this.videoAppendQueued_ = false;
|
19188 | this.delayedAudioAppendQueue_.length = 0;
|
19189 |
|
19190 | if (this.sourceopenListener_) {
|
19191 | this.mediaSource.removeEventListener('sourceopen', this.sourceopenListener_);
|
19192 | }
|
19193 |
|
19194 | this.off();
|
19195 | };
|
19196 |
|
19197 | return SourceUpdater;
|
19198 | }(videojs.EventTarget);
|
19199 |
|
19200 | var uint8ToUtf8 = function uint8ToUtf8(uintArray) {
|
19201 | return decodeURIComponent(escape(String.fromCharCode.apply(null, uintArray)));
|
19202 | };
|
19203 |
|
19204 | var VTT_LINE_TERMINATORS = new Uint8Array('\n\n'.split('').map(function (char) {
|
19205 | return char.charCodeAt(0);
|
19206 | }));
|
19207 |
|
19208 |
|
19209 |
|
19210 |
|
19211 |
|
19212 |
|
19213 |
|
19214 |
|
19215 | var VTTSegmentLoader = function (_SegmentLoader) {
|
19216 | _inheritsLoose(VTTSegmentLoader, _SegmentLoader);
|
19217 |
|
19218 | function VTTSegmentLoader(settings, options) {
|
19219 | var _this;
|
19220 |
|
19221 | if (options === void 0) {
|
19222 | options = {};
|
19223 | }
|
19224 |
|
19225 | _this = _SegmentLoader.call(this, settings, options) || this;
|
19226 |
|
19227 |
|
19228 | _this.mediaSource_ = null;
|
19229 | _this.subtitlesTrack_ = null;
|
19230 | _this.loaderType_ = 'subtitle';
|
19231 | _this.featuresNativeTextTracks_ = settings.featuresNativeTextTracks;
|
19232 |
|
19233 |
|
19234 | _this.shouldSaveSegmentTimingInfo_ = false;
|
19235 | return _this;
|
19236 | }
|
19237 |
|
19238 | var _proto = VTTSegmentLoader.prototype;
|
19239 |
|
19240 | _proto.createTransmuxer_ = function createTransmuxer_() {
|
19241 |
|
19242 | return null;
|
19243 | }
|
19244 | |
19245 |
|
19246 |
|
19247 |
|
19248 |
|
19249 |
|
19250 | ;
|
19251 |
|
19252 | _proto.buffered_ = function buffered_() {
|
19253 | if (!this.subtitlesTrack_ || !this.subtitlesTrack_.cues || !this.subtitlesTrack_.cues.length) {
|
19254 | return videojs.createTimeRanges();
|
19255 | }
|
19256 |
|
19257 | var cues = this.subtitlesTrack_.cues;
|
19258 | var start = cues[0].startTime;
|
19259 | var end = cues[cues.length - 1].startTime;
|
19260 | return videojs.createTimeRanges([[start, end]]);
|
19261 | }
|
19262 | |
19263 |
|
19264 |
|
19265 |
|
19266 |
|
19267 |
|
19268 |
|
19269 |
|
19270 |
|
19271 |
|
19272 | ;
|
19273 |
|
19274 | _proto.initSegmentForMap = function initSegmentForMap(map, set) {
|
19275 | if (set === void 0) {
|
19276 | set = false;
|
19277 | }
|
19278 |
|
19279 | if (!map) {
|
19280 | return null;
|
19281 | }
|
19282 |
|
19283 | var id = initSegmentId(map);
|
19284 | var storedMap = this.initSegments_[id];
|
19285 |
|
19286 | if (set && !storedMap && map.bytes) {
|
19287 |
|
19288 |
|
19289 |
|
19290 |
|
19291 | var combinedByteLength = VTT_LINE_TERMINATORS.byteLength + map.bytes.byteLength;
|
19292 | var combinedSegment = new Uint8Array(combinedByteLength);
|
19293 | combinedSegment.set(map.bytes);
|
19294 | combinedSegment.set(VTT_LINE_TERMINATORS, map.bytes.byteLength);
|
19295 | this.initSegments_[id] = storedMap = {
|
19296 | resolvedUri: map.resolvedUri,
|
19297 | byterange: map.byterange,
|
19298 | bytes: combinedSegment
|
19299 | };
|
19300 | }
|
19301 |
|
19302 | return storedMap || map;
|
19303 | }
|
19304 | |
19305 |
|
19306 |
|
19307 |
|
19308 |
|
19309 |
|
19310 | ;
|
19311 |
|
19312 | _proto.couldBeginLoading_ = function couldBeginLoading_() {
|
19313 | return this.playlist_ && this.subtitlesTrack_ && !this.paused();
|
19314 | }
|
19315 | |
19316 |
|
19317 |
|
19318 |
|
19319 |
|
19320 |
|
19321 |
|
19322 | ;
|
19323 |
|
19324 | _proto.init_ = function init_() {
|
19325 | this.state = 'READY';
|
19326 | this.resetEverything();
|
19327 | return this.monitorBuffer_();
|
19328 | }
|
19329 | |
19330 |
|
19331 |
|
19332 |
|
19333 |
|
19334 |
|
19335 |
|
19336 |
|
19337 | ;
|
19338 |
|
19339 | _proto.track = function track(_track) {
|
19340 | if (typeof _track === 'undefined') {
|
19341 | return this.subtitlesTrack_;
|
19342 | }
|
19343 |
|
19344 | this.subtitlesTrack_ = _track;
|
19345 |
|
19346 |
|
19347 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
19348 | this.init_();
|
19349 | }
|
19350 |
|
19351 | return this.subtitlesTrack_;
|
19352 | }
|
19353 | |
19354 |
|
19355 |
|
19356 |
|
19357 |
|
19358 |
|
19359 | ;
|
19360 |
|
19361 | _proto.remove = function remove(start, end) {
|
19362 | removeCuesFromTrack(start, end, this.subtitlesTrack_);
|
19363 | }
|
19364 | |
19365 |
|
19366 |
|
19367 |
|
19368 |
|
19369 |
|
19370 |
|
19371 |
|
19372 |
|
19373 | ;
|
19374 |
|
19375 | _proto.fillBuffer_ = function fillBuffer_() {
|
19376 | var _this2 = this;
|
19377 |
|
19378 |
|
19379 | var segmentInfo = this.chooseNextRequest_();
|
19380 |
|
19381 | if (!segmentInfo) {
|
19382 | return;
|
19383 | }
|
19384 |
|
19385 | if (this.syncController_.timestampOffsetForTimeline(segmentInfo.timeline) === null) {
|
19386 |
|
19387 |
|
19388 | var checkTimestampOffset = function checkTimestampOffset() {
|
19389 | _this2.state = 'READY';
|
19390 |
|
19391 | if (!_this2.paused()) {
|
19392 |
|
19393 | _this2.monitorBuffer_();
|
19394 | }
|
19395 | };
|
19396 |
|
19397 | this.syncController_.one('timestampoffset', checkTimestampOffset);
|
19398 | this.state = 'WAITING_ON_TIMELINE';
|
19399 | return;
|
19400 | }
|
19401 |
|
19402 | this.loadSegment_(segmentInfo);
|
19403 | }
|
19404 | ;
|
19405 |
|
19406 | _proto.timestampOffsetForSegment_ = function timestampOffsetForSegment_() {
|
19407 | return null;
|
19408 | };
|
19409 |
|
19410 | _proto.chooseNextRequest_ = function chooseNextRequest_() {
|
19411 | return this.skipEmptySegments_(_SegmentLoader.prototype.chooseNextRequest_.call(this));
|
19412 | }
|
19413 | |
19414 |
|
19415 |
|
19416 |
|
19417 |
|
19418 |
|
19419 |
|
19420 |
|
19421 |
|
19422 |
|
19423 | ;
|
19424 |
|
19425 | _proto.skipEmptySegments_ = function skipEmptySegments_(segmentInfo) {
|
19426 | while (segmentInfo && segmentInfo.segment.empty) {
|
19427 |
|
19428 | if (segmentInfo.mediaIndex + 1 >= segmentInfo.playlist.segments.length) {
|
19429 | segmentInfo = null;
|
19430 | break;
|
19431 | }
|
19432 |
|
19433 | segmentInfo = this.generateSegmentInfo_({
|
19434 | playlist: segmentInfo.playlist,
|
19435 | mediaIndex: segmentInfo.mediaIndex + 1,
|
19436 | startOfSegment: segmentInfo.startOfSegment + segmentInfo.duration,
|
19437 | isSyncRequest: segmentInfo.isSyncRequest
|
19438 | });
|
19439 | }
|
19440 |
|
19441 | return segmentInfo;
|
19442 | };
|
19443 |
|
19444 | _proto.stopForError = function stopForError(error) {
|
19445 | this.error(error);
|
19446 | this.state = 'READY';
|
19447 | this.pause();
|
19448 | this.trigger('error');
|
19449 | }
|
19450 | |
19451 |
|
19452 |
|
19453 |
|
19454 |
|
19455 | ;
|
19456 |
|
19457 | _proto.segmentRequestFinished_ = function segmentRequestFinished_(error, simpleSegment, result) {
|
19458 | var _this3 = this;
|
19459 |
|
19460 | if (!this.subtitlesTrack_) {
|
19461 | this.state = 'READY';
|
19462 | return;
|
19463 | }
|
19464 |
|
19465 | this.saveTransferStats_(simpleSegment.stats);
|
19466 |
|
19467 | if (!this.pendingSegment_) {
|
19468 | this.state = 'READY';
|
19469 | this.mediaRequestsAborted += 1;
|
19470 | return;
|
19471 | }
|
19472 |
|
19473 | if (error) {
|
19474 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
19475 | this.handleTimeout_();
|
19476 | }
|
19477 |
|
19478 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
19479 | this.mediaRequestsAborted += 1;
|
19480 | } else {
|
19481 | this.mediaRequestsErrored += 1;
|
19482 | }
|
19483 |
|
19484 | this.stopForError(error);
|
19485 | return;
|
19486 | }
|
19487 |
|
19488 | var segmentInfo = this.pendingSegment_;
|
19489 |
|
19490 |
|
19491 | this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats);
|
19492 | this.state = 'APPENDING';
|
19493 |
|
19494 | this.trigger('appending');
|
19495 | var segment = segmentInfo.segment;
|
19496 |
|
19497 | if (segment.map) {
|
19498 | segment.map.bytes = simpleSegment.map.bytes;
|
19499 | }
|
19500 |
|
19501 | segmentInfo.bytes = simpleSegment.bytes;
|
19502 |
|
19503 | if (typeof window.WebVTT !== 'function' && this.subtitlesTrack_ && this.subtitlesTrack_.tech_) {
|
19504 | var loadHandler;
|
19505 |
|
19506 | var errorHandler = function errorHandler() {
|
19507 | _this3.subtitlesTrack_.tech_.off('vttjsloaded', loadHandler);
|
19508 |
|
19509 | _this3.stopForError({
|
19510 | message: 'Error loading vtt.js'
|
19511 | });
|
19512 |
|
19513 | return;
|
19514 | };
|
19515 |
|
19516 | loadHandler = function loadHandler() {
|
19517 | _this3.subtitlesTrack_.tech_.off('vttjserror', errorHandler);
|
19518 |
|
19519 | _this3.segmentRequestFinished_(error, simpleSegment, result);
|
19520 | };
|
19521 |
|
19522 | this.state = 'WAITING_ON_VTTJS';
|
19523 | this.subtitlesTrack_.tech_.one('vttjsloaded', loadHandler);
|
19524 | this.subtitlesTrack_.tech_.one('vttjserror', errorHandler);
|
19525 | return;
|
19526 | }
|
19527 |
|
19528 | segment.requested = true;
|
19529 |
|
19530 | try {
|
19531 | this.parseVTTCues_(segmentInfo);
|
19532 | } catch (e) {
|
19533 | this.stopForError({
|
19534 | message: e.message
|
19535 | });
|
19536 | return;
|
19537 | }
|
19538 |
|
19539 | this.updateTimeMapping_(segmentInfo, this.syncController_.timelines[segmentInfo.timeline], this.playlist_);
|
19540 |
|
19541 | if (segmentInfo.cues.length) {
|
19542 | segmentInfo.timingInfo = {
|
19543 | start: segmentInfo.cues[0].startTime,
|
19544 | end: segmentInfo.cues[segmentInfo.cues.length - 1].endTime
|
19545 | };
|
19546 | } else {
|
19547 | segmentInfo.timingInfo = {
|
19548 | start: segmentInfo.startOfSegment,
|
19549 | end: segmentInfo.startOfSegment + segmentInfo.duration
|
19550 | };
|
19551 | }
|
19552 |
|
19553 | if (segmentInfo.isSyncRequest) {
|
19554 | this.trigger('syncinfoupdate');
|
19555 | this.pendingSegment_ = null;
|
19556 | this.state = 'READY';
|
19557 | return;
|
19558 | }
|
19559 |
|
19560 | segmentInfo.byteLength = segmentInfo.bytes.byteLength;
|
19561 | this.mediaSecondsLoaded += segment.duration;
|
19562 |
|
19563 |
|
19564 | segmentInfo.cues.forEach(function (cue) {
|
19565 | _this3.subtitlesTrack_.addCue(_this3.featuresNativeTextTracks_ ? new window.VTTCue(cue.startTime, cue.endTime, cue.text) : cue);
|
19566 | });
|
19567 |
|
19568 |
|
19569 |
|
19570 |
|
19571 | removeDuplicateCuesFromTrack(this.subtitlesTrack_);
|
19572 | this.handleAppendsDone_();
|
19573 | };
|
19574 |
|
19575 | _proto.handleData_ = function handleData_() {
|
19576 |
|
19577 | };
|
19578 |
|
19579 | _proto.updateTimingInfoEnd_ = function updateTimingInfoEnd_() {
|
19580 | }
|
19581 | |
19582 |
|
19583 |
|
19584 |
|
19585 |
|
19586 |
|
19587 |
|
19588 | ;
|
19589 |
|
19590 | _proto.parseVTTCues_ = function parseVTTCues_(segmentInfo) {
|
19591 | var decoder;
|
19592 | var decodeBytesToString = false;
|
19593 |
|
19594 | if (typeof window.TextDecoder === 'function') {
|
19595 | decoder = new window.TextDecoder('utf8');
|
19596 | } else {
|
19597 | decoder = window.WebVTT.StringDecoder();
|
19598 | decodeBytesToString = true;
|
19599 | }
|
19600 |
|
19601 | var parser = new window.WebVTT.Parser(window, window.vttjs, decoder);
|
19602 | segmentInfo.cues = [];
|
19603 | segmentInfo.timestampmap = {
|
19604 | MPEGTS: 0,
|
19605 | LOCAL: 0
|
19606 | };
|
19607 | parser.oncue = segmentInfo.cues.push.bind(segmentInfo.cues);
|
19608 |
|
19609 | parser.ontimestampmap = function (map) {
|
19610 | segmentInfo.timestampmap = map;
|
19611 | };
|
19612 |
|
19613 | parser.onparsingerror = function (error) {
|
19614 | videojs.log.warn('Error encountered when parsing cues: ' + error.message);
|
19615 | };
|
19616 |
|
19617 | if (segmentInfo.segment.map) {
|
19618 | var mapData = segmentInfo.segment.map.bytes;
|
19619 |
|
19620 | if (decodeBytesToString) {
|
19621 | mapData = uint8ToUtf8(mapData);
|
19622 | }
|
19623 |
|
19624 | parser.parse(mapData);
|
19625 | }
|
19626 |
|
19627 | var segmentData = segmentInfo.bytes;
|
19628 |
|
19629 | if (decodeBytesToString) {
|
19630 | segmentData = uint8ToUtf8(segmentData);
|
19631 | }
|
19632 |
|
19633 | parser.parse(segmentData);
|
19634 | parser.flush();
|
19635 | }
|
19636 | |
19637 |
|
19638 |
|
19639 |
|
19640 |
|
19641 |
|
19642 |
|
19643 |
|
19644 |
|
19645 |
|
19646 |
|
19647 |
|
19648 |
|
19649 | ;
|
19650 |
|
19651 | _proto.updateTimeMapping_ = function updateTimeMapping_(segmentInfo, mappingObj, playlist) {
|
19652 | var segment = segmentInfo.segment;
|
19653 |
|
19654 | if (!mappingObj) {
|
19655 |
|
19656 |
|
19657 |
|
19658 | return;
|
19659 | }
|
19660 |
|
19661 | if (!segmentInfo.cues.length) {
|
19662 |
|
19663 |
|
19664 |
|
19665 | segment.empty = true;
|
19666 | return;
|
19667 | }
|
19668 |
|
19669 | var timestampmap = segmentInfo.timestampmap;
|
19670 | var diff = timestampmap.MPEGTS / ONE_SECOND_IN_TS - timestampmap.LOCAL + mappingObj.mapping;
|
19671 | segmentInfo.cues.forEach(function (cue) {
|
19672 |
|
19673 | cue.startTime += diff;
|
19674 | cue.endTime += diff;
|
19675 | });
|
19676 |
|
19677 | if (!playlist.syncInfo) {
|
19678 | var firstStart = segmentInfo.cues[0].startTime;
|
19679 | var lastStart = segmentInfo.cues[segmentInfo.cues.length - 1].startTime;
|
19680 | playlist.syncInfo = {
|
19681 | mediaSequence: playlist.mediaSequence + segmentInfo.mediaIndex,
|
19682 | time: Math.min(firstStart, lastStart - segment.duration)
|
19683 | };
|
19684 | }
|
19685 | };
|
19686 |
|
19687 | return VTTSegmentLoader;
|
19688 | }(SegmentLoader);
|
19689 |
|
19690 |
|
19691 |
|
19692 |
|
19693 |
|
19694 |
|
19695 |
|
19696 |
|
19697 |
|
19698 |
|
19699 |
|
19700 |
|
19701 |
|
19702 |
|
19703 |
|
19704 |
|
19705 |
|
19706 | var findAdCue = function findAdCue(track, mediaTime) {
|
19707 | var cues = track.cues;
|
19708 |
|
19709 | for (var i = 0; i < cues.length; i++) {
|
19710 | var cue = cues[i];
|
19711 |
|
19712 | if (mediaTime >= cue.adStartTime && mediaTime <= cue.adEndTime) {
|
19713 | return cue;
|
19714 | }
|
19715 | }
|
19716 |
|
19717 | return null;
|
19718 | };
|
19719 | var updateAdCues = function updateAdCues(media, track, offset) {
|
19720 | if (offset === void 0) {
|
19721 | offset = 0;
|
19722 | }
|
19723 |
|
19724 | if (!media.segments) {
|
19725 | return;
|
19726 | }
|
19727 |
|
19728 | var mediaTime = offset;
|
19729 | var cue;
|
19730 |
|
19731 | for (var i = 0; i < media.segments.length; i++) {
|
19732 | var segment = media.segments[i];
|
19733 |
|
19734 | if (!cue) {
|
19735 |
|
19736 |
|
19737 |
|
19738 |
|
19739 | cue = findAdCue(track, mediaTime + segment.duration / 2);
|
19740 | }
|
19741 |
|
19742 | if (cue) {
|
19743 | if ('cueIn' in segment) {
|
19744 |
|
19745 | cue.endTime = mediaTime;
|
19746 | cue.adEndTime = mediaTime;
|
19747 | mediaTime += segment.duration;
|
19748 | cue = null;
|
19749 | continue;
|
19750 | }
|
19751 |
|
19752 | if (mediaTime < cue.endTime) {
|
19753 |
|
19754 | mediaTime += segment.duration;
|
19755 | continue;
|
19756 | }
|
19757 |
|
19758 |
|
19759 | cue.endTime += segment.duration;
|
19760 | } else {
|
19761 | if ('cueOut' in segment) {
|
19762 | cue = new window.VTTCue(mediaTime, mediaTime + segment.duration, segment.cueOut);
|
19763 | cue.adStartTime = mediaTime;
|
19764 |
|
19765 |
|
19766 | cue.adEndTime = mediaTime + parseFloat(segment.cueOut);
|
19767 | track.addCue(cue);
|
19768 | }
|
19769 |
|
19770 | if ('cueOutCont' in segment) {
|
19771 |
|
19772 |
|
19773 |
|
19774 | var _segment$cueOutCont$s = segment.cueOutCont.split('/').map(parseFloat),
|
19775 | adOffset = _segment$cueOutCont$s[0],
|
19776 | adTotal = _segment$cueOutCont$s[1];
|
19777 |
|
19778 | cue = new window.VTTCue(mediaTime, mediaTime + segment.duration, '');
|
19779 | cue.adStartTime = mediaTime - adOffset;
|
19780 | cue.adEndTime = cue.adStartTime + adTotal;
|
19781 | track.addCue(cue);
|
19782 | }
|
19783 | }
|
19784 |
|
19785 | mediaTime += segment.duration;
|
19786 | }
|
19787 | };
|
19788 |
|
19789 |
|
19790 |
|
19791 |
|
19792 |
|
19793 |
|
19794 | var MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC = 86400;
|
19795 | var syncPointStrategies = [
|
19796 |
|
19797 | {
|
19798 | name: 'VOD',
|
19799 | run: function run(syncController, playlist, duration, currentTimeline, currentTime) {
|
19800 | if (duration !== Infinity) {
|
19801 | var syncPoint = {
|
19802 | time: 0,
|
19803 | segmentIndex: 0,
|
19804 | partIndex: null
|
19805 | };
|
19806 | return syncPoint;
|
19807 | }
|
19808 |
|
19809 | return null;
|
19810 | }
|
19811 | },
|
19812 | {
|
19813 | name: 'ProgramDateTime',
|
19814 | run: function run(syncController, playlist, duration, currentTimeline, currentTime) {
|
19815 | if (!Object.keys(syncController.timelineToDatetimeMappings).length) {
|
19816 | return null;
|
19817 | }
|
19818 |
|
19819 | var syncPoint = null;
|
19820 | var lastDistance = null;
|
19821 | var partsAndSegments = getPartsAndSegments(playlist);
|
19822 | currentTime = currentTime || 0;
|
19823 |
|
19824 | for (var i = 0; i < partsAndSegments.length; i++) {
|
19825 |
|
19826 |
|
19827 | var index = playlist.endList || currentTime === 0 ? i : partsAndSegments.length - (i + 1);
|
19828 | var partAndSegment = partsAndSegments[index];
|
19829 | var segment = partAndSegment.segment;
|
19830 | var datetimeMapping = syncController.timelineToDatetimeMappings[segment.timeline];
|
19831 |
|
19832 | if (!datetimeMapping) {
|
19833 | continue;
|
19834 | }
|
19835 |
|
19836 | if (segment.dateTimeObject) {
|
19837 | var segmentTime = segment.dateTimeObject.getTime() / 1000;
|
19838 | var start = segmentTime + datetimeMapping;
|
19839 |
|
19840 | if (segment.parts && typeof partAndSegment.partIndex === 'number') {
|
19841 | for (var z = 0; z < partAndSegment.partIndex; z++) {
|
19842 | start += segment.parts[z].duration;
|
19843 | }
|
19844 | }
|
19845 |
|
19846 | var distance = Math.abs(currentTime - start);
|
19847 |
|
19848 |
|
19849 | if (lastDistance !== null && (distance === 0 || lastDistance < distance)) {
|
19850 | break;
|
19851 | }
|
19852 |
|
19853 | lastDistance = distance;
|
19854 | syncPoint = {
|
19855 | time: start,
|
19856 | segmentIndex: partAndSegment.segmentIndex,
|
19857 | partIndex: partAndSegment.partIndex
|
19858 | };
|
19859 | }
|
19860 | }
|
19861 |
|
19862 | return syncPoint;
|
19863 | }
|
19864 | },
|
19865 |
|
19866 | {
|
19867 | name: 'Segment',
|
19868 | run: function run(syncController, playlist, duration, currentTimeline, currentTime) {
|
19869 | var syncPoint = null;
|
19870 | var lastDistance = null;
|
19871 | currentTime = currentTime || 0;
|
19872 | var partsAndSegments = getPartsAndSegments(playlist);
|
19873 |
|
19874 | for (var i = 0; i < partsAndSegments.length; i++) {
|
19875 |
|
19876 |
|
19877 | var index = playlist.endList || currentTime === 0 ? i : partsAndSegments.length - (i + 1);
|
19878 | var partAndSegment = partsAndSegments[index];
|
19879 | var segment = partAndSegment.segment;
|
19880 | var start = partAndSegment.part && partAndSegment.part.start || segment && segment.start;
|
19881 |
|
19882 | if (segment.timeline === currentTimeline && typeof start !== 'undefined') {
|
19883 | var distance = Math.abs(currentTime - start);
|
19884 |
|
19885 |
|
19886 | if (lastDistance !== null && lastDistance < distance) {
|
19887 | break;
|
19888 | }
|
19889 |
|
19890 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
19891 | lastDistance = distance;
|
19892 | syncPoint = {
|
19893 | time: start,
|
19894 | segmentIndex: partAndSegment.segmentIndex,
|
19895 | partIndex: partAndSegment.partIndex
|
19896 | };
|
19897 | }
|
19898 | }
|
19899 | }
|
19900 |
|
19901 | return syncPoint;
|
19902 | }
|
19903 | },
|
19904 |
|
19905 | {
|
19906 | name: 'Discontinuity',
|
19907 | run: function run(syncController, playlist, duration, currentTimeline, currentTime) {
|
19908 | var syncPoint = null;
|
19909 | currentTime = currentTime || 0;
|
19910 |
|
19911 | if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
19912 | var lastDistance = null;
|
19913 |
|
19914 | for (var i = 0; i < playlist.discontinuityStarts.length; i++) {
|
19915 | var segmentIndex = playlist.discontinuityStarts[i];
|
19916 | var discontinuity = playlist.discontinuitySequence + i + 1;
|
19917 | var discontinuitySync = syncController.discontinuities[discontinuity];
|
19918 |
|
19919 | if (discontinuitySync) {
|
19920 | var distance = Math.abs(currentTime - discontinuitySync.time);
|
19921 |
|
19922 |
|
19923 | if (lastDistance !== null && lastDistance < distance) {
|
19924 | break;
|
19925 | }
|
19926 |
|
19927 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
19928 | lastDistance = distance;
|
19929 | syncPoint = {
|
19930 | time: discontinuitySync.time,
|
19931 | segmentIndex: segmentIndex,
|
19932 | partIndex: null
|
19933 | };
|
19934 | }
|
19935 | }
|
19936 | }
|
19937 | }
|
19938 |
|
19939 | return syncPoint;
|
19940 | }
|
19941 | },
|
19942 |
|
19943 | {
|
19944 | name: 'Playlist',
|
19945 | run: function run(syncController, playlist, duration, currentTimeline, currentTime) {
|
19946 | if (playlist.syncInfo) {
|
19947 | var syncPoint = {
|
19948 | time: playlist.syncInfo.time,
|
19949 | segmentIndex: playlist.syncInfo.mediaSequence - playlist.mediaSequence,
|
19950 | partIndex: null
|
19951 | };
|
19952 | return syncPoint;
|
19953 | }
|
19954 |
|
19955 | return null;
|
19956 | }
|
19957 | }];
|
19958 |
|
19959 | var SyncController = function (_videojs$EventTarget) {
|
19960 | _inheritsLoose(SyncController, _videojs$EventTarget);
|
19961 |
|
19962 | function SyncController(options) {
|
19963 | var _this;
|
19964 |
|
19965 | _this = _videojs$EventTarget.call(this) || this;
|
19966 |
|
19967 | _this.timelines = [];
|
19968 | _this.discontinuities = [];
|
19969 | _this.timelineToDatetimeMappings = {};
|
19970 | _this.logger_ = logger('SyncController');
|
19971 | return _this;
|
19972 | }
|
19973 | |
19974 |
|
19975 |
|
19976 |
|
19977 |
|
19978 |
|
19979 |
|
19980 |
|
19981 |
|
19982 |
|
19983 |
|
19984 |
|
19985 |
|
19986 |
|
19987 |
|
19988 |
|
19989 |
|
19990 | var _proto = SyncController.prototype;
|
19991 |
|
19992 | _proto.getSyncPoint = function getSyncPoint(playlist, duration, currentTimeline, currentTime) {
|
19993 | var syncPoints = this.runStrategies_(playlist, duration, currentTimeline, currentTime);
|
19994 |
|
19995 | if (!syncPoints.length) {
|
19996 |
|
19997 |
|
19998 |
|
19999 | return null;
|
20000 | }
|
20001 |
|
20002 |
|
20003 |
|
20004 |
|
20005 | return this.selectSyncPoint_(syncPoints, {
|
20006 | key: 'time',
|
20007 | value: currentTime
|
20008 | });
|
20009 | }
|
20010 | |
20011 |
|
20012 |
|
20013 |
|
20014 |
|
20015 |
|
20016 |
|
20017 |
|
20018 |
|
20019 |
|
20020 |
|
20021 | ;
|
20022 |
|
20023 | _proto.getExpiredTime = function getExpiredTime(playlist, duration) {
|
20024 | if (!playlist || !playlist.segments) {
|
20025 | return null;
|
20026 | }
|
20027 |
|
20028 | var syncPoints = this.runStrategies_(playlist, duration, playlist.discontinuitySequence, 0);
|
20029 |
|
20030 | if (!syncPoints.length) {
|
20031 | return null;
|
20032 | }
|
20033 |
|
20034 | var syncPoint = this.selectSyncPoint_(syncPoints, {
|
20035 | key: 'segmentIndex',
|
20036 | value: 0
|
20037 | });
|
20038 |
|
20039 |
|
20040 | if (syncPoint.segmentIndex > 0) {
|
20041 | syncPoint.time *= -1;
|
20042 | }
|
20043 |
|
20044 | return Math.abs(syncPoint.time + sumDurations({
|
20045 | defaultDuration: playlist.targetDuration,
|
20046 | durationList: playlist.segments,
|
20047 | startIndex: syncPoint.segmentIndex,
|
20048 | endIndex: 0
|
20049 | }));
|
20050 | }
|
20051 | |
20052 |
|
20053 |
|
20054 |
|
20055 |
|
20056 |
|
20057 |
|
20058 |
|
20059 |
|
20060 |
|
20061 |
|
20062 |
|
20063 |
|
20064 |
|
20065 | ;
|
20066 |
|
20067 | _proto.runStrategies_ = function runStrategies_(playlist, duration, currentTimeline, currentTime) {
|
20068 | var syncPoints = [];
|
20069 |
|
20070 | for (var i = 0; i < syncPointStrategies.length; i++) {
|
20071 | var strategy = syncPointStrategies[i];
|
20072 | var syncPoint = strategy.run(this, playlist, duration, currentTimeline, currentTime);
|
20073 |
|
20074 | if (syncPoint) {
|
20075 | syncPoint.strategy = strategy.name;
|
20076 | syncPoints.push({
|
20077 | strategy: strategy.name,
|
20078 | syncPoint: syncPoint
|
20079 | });
|
20080 | }
|
20081 | }
|
20082 |
|
20083 | return syncPoints;
|
20084 | }
|
20085 | |
20086 |
|
20087 |
|
20088 |
|
20089 |
|
20090 |
|
20091 |
|
20092 |
|
20093 |
|
20094 |
|
20095 |
|
20096 |
|
20097 |
|
20098 |
|
20099 |
|
20100 | ;
|
20101 |
|
20102 | _proto.selectSyncPoint_ = function selectSyncPoint_(syncPoints, target) {
|
20103 | var bestSyncPoint = syncPoints[0].syncPoint;
|
20104 | var bestDistance = Math.abs(syncPoints[0].syncPoint[target.key] - target.value);
|
20105 | var bestStrategy = syncPoints[0].strategy;
|
20106 |
|
20107 | for (var i = 1; i < syncPoints.length; i++) {
|
20108 | var newDistance = Math.abs(syncPoints[i].syncPoint[target.key] - target.value);
|
20109 |
|
20110 | if (newDistance < bestDistance) {
|
20111 | bestDistance = newDistance;
|
20112 | bestSyncPoint = syncPoints[i].syncPoint;
|
20113 | bestStrategy = syncPoints[i].strategy;
|
20114 | }
|
20115 | }
|
20116 |
|
20117 | this.logger_("syncPoint for [" + target.key + ": " + target.value + "] chosen with strategy" + (" [" + bestStrategy + "]: [time:" + bestSyncPoint.time + ",") + (" segmentIndex:" + bestSyncPoint.segmentIndex) + (typeof bestSyncPoint.partIndex === 'number' ? ",partIndex:" + bestSyncPoint.partIndex : '') + ']');
|
20118 | return bestSyncPoint;
|
20119 | }
|
20120 | |
20121 |
|
20122 |
|
20123 |
|
20124 |
|
20125 |
|
20126 |
|
20127 |
|
20128 | ;
|
20129 |
|
20130 | _proto.saveExpiredSegmentInfo = function saveExpiredSegmentInfo(oldPlaylist, newPlaylist) {
|
20131 | var mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
20132 |
|
20133 | if (mediaSequenceDiff > MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC) {
|
20134 | videojs.log.warn("Not saving expired segment info. Media sequence gap " + mediaSequenceDiff + " is too large.");
|
20135 | return;
|
20136 | }
|
20137 |
|
20138 |
|
20139 |
|
20140 | for (var i = mediaSequenceDiff - 1; i >= 0; i--) {
|
20141 | var lastRemovedSegment = oldPlaylist.segments[i];
|
20142 |
|
20143 | if (lastRemovedSegment && typeof lastRemovedSegment.start !== 'undefined') {
|
20144 | newPlaylist.syncInfo = {
|
20145 | mediaSequence: oldPlaylist.mediaSequence + i,
|
20146 | time: lastRemovedSegment.start
|
20147 | };
|
20148 | this.logger_("playlist refresh sync: [time:" + newPlaylist.syncInfo.time + "," + (" mediaSequence: " + newPlaylist.syncInfo.mediaSequence + "]"));
|
20149 | this.trigger('syncinfoupdate');
|
20150 | break;
|
20151 | }
|
20152 | }
|
20153 | }
|
20154 | |
20155 |
|
20156 |
|
20157 |
|
20158 |
|
20159 |
|
20160 | ;
|
20161 |
|
20162 | _proto.setDateTimeMappingForStart = function setDateTimeMappingForStart(playlist) {
|
20163 |
|
20164 |
|
20165 |
|
20166 |
|
20167 | this.timelineToDatetimeMappings = {};
|
20168 |
|
20169 | if (playlist.segments && playlist.segments.length && playlist.segments[0].dateTimeObject) {
|
20170 | var firstSegment = playlist.segments[0];
|
20171 | var playlistTimestamp = firstSegment.dateTimeObject.getTime() / 1000;
|
20172 | this.timelineToDatetimeMappings[firstSegment.timeline] = -playlistTimestamp;
|
20173 | }
|
20174 | }
|
20175 | |
20176 |
|
20177 |
|
20178 |
|
20179 |
|
20180 |
|
20181 |
|
20182 |
|
20183 |
|
20184 |
|
20185 |
|
20186 |
|
20187 | ;
|
20188 |
|
20189 | _proto.saveSegmentTimingInfo = function saveSegmentTimingInfo(_ref) {
|
20190 | var segmentInfo = _ref.segmentInfo,
|
20191 | shouldSaveTimelineMapping = _ref.shouldSaveTimelineMapping;
|
20192 | var didCalculateSegmentTimeMapping = this.calculateSegmentTimeMapping_(segmentInfo, segmentInfo.timingInfo, shouldSaveTimelineMapping);
|
20193 | var segment = segmentInfo.segment;
|
20194 |
|
20195 | if (didCalculateSegmentTimeMapping) {
|
20196 | this.saveDiscontinuitySyncInfo_(segmentInfo);
|
20197 |
|
20198 |
|
20199 | if (!segmentInfo.playlist.syncInfo) {
|
20200 | segmentInfo.playlist.syncInfo = {
|
20201 | mediaSequence: segmentInfo.playlist.mediaSequence + segmentInfo.mediaIndex,
|
20202 | time: segment.start
|
20203 | };
|
20204 | }
|
20205 | }
|
20206 |
|
20207 | var dateTime = segment.dateTimeObject;
|
20208 |
|
20209 | if (segment.discontinuity && shouldSaveTimelineMapping && dateTime) {
|
20210 | this.timelineToDatetimeMappings[segment.timeline] = -(dateTime.getTime() / 1000);
|
20211 | }
|
20212 | };
|
20213 |
|
20214 | _proto.timestampOffsetForTimeline = function timestampOffsetForTimeline(timeline) {
|
20215 | if (typeof this.timelines[timeline] === 'undefined') {
|
20216 | return null;
|
20217 | }
|
20218 |
|
20219 | return this.timelines[timeline].time;
|
20220 | };
|
20221 |
|
20222 | _proto.mappingForTimeline = function mappingForTimeline(timeline) {
|
20223 | if (typeof this.timelines[timeline] === 'undefined') {
|
20224 | return null;
|
20225 | }
|
20226 |
|
20227 | return this.timelines[timeline].mapping;
|
20228 | }
|
20229 | |
20230 |
|
20231 |
|
20232 |
|
20233 |
|
20234 |
|
20235 |
|
20236 |
|
20237 |
|
20238 |
|
20239 |
|
20240 |
|
20241 |
|
20242 |
|
20243 |
|
20244 | ;
|
20245 |
|
20246 | _proto.calculateSegmentTimeMapping_ = function calculateSegmentTimeMapping_(segmentInfo, timingInfo, shouldSaveTimelineMapping) {
|
20247 |
|
20248 | var segment = segmentInfo.segment;
|
20249 | var part = segmentInfo.part;
|
20250 | var mappingObj = this.timelines[segmentInfo.timeline];
|
20251 | var start;
|
20252 | var end;
|
20253 |
|
20254 | if (typeof segmentInfo.timestampOffset === 'number') {
|
20255 | mappingObj = {
|
20256 | time: segmentInfo.startOfSegment,
|
20257 | mapping: segmentInfo.startOfSegment - timingInfo.start
|
20258 | };
|
20259 |
|
20260 | if (shouldSaveTimelineMapping) {
|
20261 | this.timelines[segmentInfo.timeline] = mappingObj;
|
20262 | this.trigger('timestampoffset');
|
20263 | this.logger_("time mapping for timeline " + segmentInfo.timeline + ": " + ("[time: " + mappingObj.time + "] [mapping: " + mappingObj.mapping + "]"));
|
20264 | }
|
20265 |
|
20266 | start = segmentInfo.startOfSegment;
|
20267 | end = timingInfo.end + mappingObj.mapping;
|
20268 | } else if (mappingObj) {
|
20269 | start = timingInfo.start + mappingObj.mapping;
|
20270 | end = timingInfo.end + mappingObj.mapping;
|
20271 | } else {
|
20272 | return false;
|
20273 | }
|
20274 |
|
20275 | if (part) {
|
20276 | part.start = start;
|
20277 | part.end = end;
|
20278 | }
|
20279 |
|
20280 |
|
20281 |
|
20282 |
|
20283 |
|
20284 |
|
20285 | if (!segment.start || start < segment.start) {
|
20286 | segment.start = start;
|
20287 | }
|
20288 |
|
20289 | segment.end = end;
|
20290 | return true;
|
20291 | }
|
20292 | |
20293 |
|
20294 |
|
20295 |
|
20296 |
|
20297 |
|
20298 |
|
20299 |
|
20300 | ;
|
20301 |
|
20302 | _proto.saveDiscontinuitySyncInfo_ = function saveDiscontinuitySyncInfo_(segmentInfo) {
|
20303 | var playlist = segmentInfo.playlist;
|
20304 | var segment = segmentInfo.segment;
|
20305 |
|
20306 |
|
20307 |
|
20308 | if (segment.discontinuity) {
|
20309 | this.discontinuities[segment.timeline] = {
|
20310 | time: segment.start,
|
20311 | accuracy: 0
|
20312 | };
|
20313 | } else if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
20314 |
|
20315 |
|
20316 | for (var i = 0; i < playlist.discontinuityStarts.length; i++) {
|
20317 | var segmentIndex = playlist.discontinuityStarts[i];
|
20318 | var discontinuity = playlist.discontinuitySequence + i + 1;
|
20319 | var mediaIndexDiff = segmentIndex - segmentInfo.mediaIndex;
|
20320 | var accuracy = Math.abs(mediaIndexDiff);
|
20321 |
|
20322 | if (!this.discontinuities[discontinuity] || this.discontinuities[discontinuity].accuracy > accuracy) {
|
20323 | var time = void 0;
|
20324 |
|
20325 | if (mediaIndexDiff < 0) {
|
20326 | time = segment.start - sumDurations({
|
20327 | defaultDuration: playlist.targetDuration,
|
20328 | durationList: playlist.segments,
|
20329 | startIndex: segmentInfo.mediaIndex,
|
20330 | endIndex: segmentIndex
|
20331 | });
|
20332 | } else {
|
20333 | time = segment.end + sumDurations({
|
20334 | defaultDuration: playlist.targetDuration,
|
20335 | durationList: playlist.segments,
|
20336 | startIndex: segmentInfo.mediaIndex + 1,
|
20337 | endIndex: segmentIndex
|
20338 | });
|
20339 | }
|
20340 |
|
20341 | this.discontinuities[discontinuity] = {
|
20342 | time: time,
|
20343 | accuracy: accuracy
|
20344 | };
|
20345 | }
|
20346 | }
|
20347 | }
|
20348 | };
|
20349 |
|
20350 | _proto.dispose = function dispose() {
|
20351 | this.trigger('dispose');
|
20352 | this.off();
|
20353 | };
|
20354 |
|
20355 | return SyncController;
|
20356 | }(videojs.EventTarget);
|
20357 |
|
20358 |
|
20359 |
|
20360 |
|
20361 |
|
20362 |
|
20363 |
|
20364 |
|
20365 |
|
20366 |
|
20367 |
|
20368 | var TimelineChangeController = function (_videojs$EventTarget) {
|
20369 | _inheritsLoose(TimelineChangeController, _videojs$EventTarget);
|
20370 |
|
20371 | function TimelineChangeController() {
|
20372 | var _this;
|
20373 |
|
20374 | _this = _videojs$EventTarget.call(this) || this;
|
20375 | _this.pendingTimelineChanges_ = {};
|
20376 | _this.lastTimelineChanges_ = {};
|
20377 | return _this;
|
20378 | }
|
20379 |
|
20380 | var _proto = TimelineChangeController.prototype;
|
20381 |
|
20382 | _proto.clearPendingTimelineChange = function clearPendingTimelineChange(type) {
|
20383 | this.pendingTimelineChanges_[type] = null;
|
20384 | this.trigger('pendingtimelinechange');
|
20385 | };
|
20386 |
|
20387 | _proto.pendingTimelineChange = function pendingTimelineChange(_ref) {
|
20388 | var type = _ref.type,
|
20389 | from = _ref.from,
|
20390 | to = _ref.to;
|
20391 |
|
20392 | if (typeof from === 'number' && typeof to === 'number') {
|
20393 | this.pendingTimelineChanges_[type] = {
|
20394 | type: type,
|
20395 | from: from,
|
20396 | to: to
|
20397 | };
|
20398 | this.trigger('pendingtimelinechange');
|
20399 | }
|
20400 |
|
20401 | return this.pendingTimelineChanges_[type];
|
20402 | };
|
20403 |
|
20404 | _proto.lastTimelineChange = function lastTimelineChange(_ref2) {
|
20405 | var type = _ref2.type,
|
20406 | from = _ref2.from,
|
20407 | to = _ref2.to;
|
20408 |
|
20409 | if (typeof from === 'number' && typeof to === 'number') {
|
20410 | this.lastTimelineChanges_[type] = {
|
20411 | type: type,
|
20412 | from: from,
|
20413 | to: to
|
20414 | };
|
20415 | delete this.pendingTimelineChanges_[type];
|
20416 | this.trigger('timelinechange');
|
20417 | }
|
20418 |
|
20419 | return this.lastTimelineChanges_[type];
|
20420 | };
|
20421 |
|
20422 | _proto.dispose = function dispose() {
|
20423 | this.trigger('dispose');
|
20424 | this.pendingTimelineChanges_ = {};
|
20425 | this.lastTimelineChanges_ = {};
|
20426 | this.off();
|
20427 | };
|
20428 |
|
20429 | return TimelineChangeController;
|
20430 | }(videojs.EventTarget);
|
20431 |
|
20432 |
|
20433 | var workerCode = transform(getWorkerString(function () {
|
20434 |
|
20435 | function createCommonjsModule(fn, basedir, module) {
|
20436 | return module = {
|
20437 | path: basedir,
|
20438 | exports: {},
|
20439 | require: function require(path, base) {
|
20440 | return commonjsRequire(path, base === undefined || base === null ? module.path : base);
|
20441 | }
|
20442 | }, fn(module, module.exports), module.exports;
|
20443 | }
|
20444 |
|
20445 | function commonjsRequire() {
|
20446 | throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
|
20447 | }
|
20448 |
|
20449 | var createClass = createCommonjsModule(function (module) {
|
20450 | function _defineProperties(target, props) {
|
20451 | for (var i = 0; i < props.length; i++) {
|
20452 | var descriptor = props[i];
|
20453 | descriptor.enumerable = descriptor.enumerable || false;
|
20454 | descriptor.configurable = true;
|
20455 | if ("value" in descriptor) descriptor.writable = true;
|
20456 | Object.defineProperty(target, descriptor.key, descriptor);
|
20457 | }
|
20458 | }
|
20459 |
|
20460 | function _createClass(Constructor, protoProps, staticProps) {
|
20461 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
20462 | if (staticProps) _defineProperties(Constructor, staticProps);
|
20463 | return Constructor;
|
20464 | }
|
20465 |
|
20466 | module.exports = _createClass;
|
20467 | module.exports["default"] = module.exports, module.exports.__esModule = true;
|
20468 | });
|
20469 | var setPrototypeOf = createCommonjsModule(function (module) {
|
20470 | function _setPrototypeOf(o, p) {
|
20471 | module.exports = _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
20472 | o.__proto__ = p;
|
20473 | return o;
|
20474 | };
|
20475 |
|
20476 | module.exports["default"] = module.exports, module.exports.__esModule = true;
|
20477 | return _setPrototypeOf(o, p);
|
20478 | }
|
20479 |
|
20480 | module.exports = _setPrototypeOf;
|
20481 | module.exports["default"] = module.exports, module.exports.__esModule = true;
|
20482 | });
|
20483 | var inheritsLoose = createCommonjsModule(function (module) {
|
20484 | function _inheritsLoose(subClass, superClass) {
|
20485 | subClass.prototype = Object.create(superClass.prototype);
|
20486 | subClass.prototype.constructor = subClass;
|
20487 | setPrototypeOf(subClass, superClass);
|
20488 | }
|
20489 |
|
20490 | module.exports = _inheritsLoose;
|
20491 | module.exports["default"] = module.exports, module.exports.__esModule = true;
|
20492 | });
|
20493 | |
20494 |
|
20495 |
|
20496 |
|
20497 | |
20498 |
|
20499 |
|
20500 |
|
20501 |
|
20502 |
|
20503 | var Stream = function () {
|
20504 | function Stream() {
|
20505 | this.listeners = {};
|
20506 | }
|
20507 | |
20508 |
|
20509 |
|
20510 |
|
20511 |
|
20512 |
|
20513 |
|
20514 |
|
20515 |
|
20516 | var _proto = Stream.prototype;
|
20517 |
|
20518 | _proto.on = function on(type, listener) {
|
20519 | if (!this.listeners[type]) {
|
20520 | this.listeners[type] = [];
|
20521 | }
|
20522 |
|
20523 | this.listeners[type].push(listener);
|
20524 | }
|
20525 | |
20526 |
|
20527 |
|
20528 |
|
20529 |
|
20530 |
|
20531 |
|
20532 |
|
20533 | ;
|
20534 |
|
20535 | _proto.off = function off(type, listener) {
|
20536 | if (!this.listeners[type]) {
|
20537 | return false;
|
20538 | }
|
20539 |
|
20540 | var index = this.listeners[type].indexOf(listener);
|
20541 |
|
20542 |
|
20543 |
|
20544 |
|
20545 |
|
20546 |
|
20547 |
|
20548 |
|
20549 | this.listeners[type] = this.listeners[type].slice(0);
|
20550 | this.listeners[type].splice(index, 1);
|
20551 | return index > -1;
|
20552 | }
|
20553 | |
20554 |
|
20555 |
|
20556 |
|
20557 |
|
20558 |
|
20559 | ;
|
20560 |
|
20561 | _proto.trigger = function trigger(type) {
|
20562 | var callbacks = this.listeners[type];
|
20563 |
|
20564 | if (!callbacks) {
|
20565 | return;
|
20566 | }
|
20567 |
|
20568 |
|
20569 |
|
20570 |
|
20571 |
|
20572 | if (arguments.length === 2) {
|
20573 | var length = callbacks.length;
|
20574 |
|
20575 | for (var i = 0; i < length; ++i) {
|
20576 | callbacks[i].call(this, arguments[1]);
|
20577 | }
|
20578 | } else {
|
20579 | var args = Array.prototype.slice.call(arguments, 1);
|
20580 | var _length = callbacks.length;
|
20581 |
|
20582 | for (var _i = 0; _i < _length; ++_i) {
|
20583 | callbacks[_i].apply(this, args);
|
20584 | }
|
20585 | }
|
20586 | }
|
20587 | |
20588 |
|
20589 |
|
20590 | ;
|
20591 |
|
20592 | _proto.dispose = function dispose() {
|
20593 | this.listeners = {};
|
20594 | }
|
20595 | |
20596 |
|
20597 |
|
20598 |
|
20599 |
|
20600 |
|
20601 |
|
20602 |
|
20603 | ;
|
20604 |
|
20605 | _proto.pipe = function pipe(destination) {
|
20606 | this.on('data', function (data) {
|
20607 | destination.push(data);
|
20608 | });
|
20609 | };
|
20610 |
|
20611 | return Stream;
|
20612 | }();
|
20613 |
|
20614 |
|
20615 | |
20616 |
|
20617 |
|
20618 |
|
20619 |
|
20620 |
|
20621 |
|
20622 |
|
20623 |
|
20624 | function unpad(padded) {
|
20625 | return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
|
20626 | }
|
20627 |
|
20628 |
|
20629 | |
20630 |
|
20631 |
|
20632 |
|
20633 |
|
20634 |
|
20635 |
|
20636 |
|
20637 |
|
20638 |
|
20639 |
|
20640 |
|
20641 |
|
20642 |
|
20643 |
|
20644 |
|
20645 |
|
20646 |
|
20647 |
|
20648 |
|
20649 |
|
20650 |
|
20651 |
|
20652 |
|
20653 |
|
20654 |
|
20655 |
|
20656 |
|
20657 |
|
20658 |
|
20659 |
|
20660 |
|
20661 |
|
20662 |
|
20663 |
|
20664 |
|
20665 |
|
20666 |
|
20667 |
|
20668 | |
20669 |
|
20670 |
|
20671 |
|
20672 |
|
20673 |
|
20674 |
|
20675 | var precompute = function precompute() {
|
20676 | var tables = [[[], [], [], [], []], [[], [], [], [], []]];
|
20677 | var encTable = tables[0];
|
20678 | var decTable = tables[1];
|
20679 | var sbox = encTable[4];
|
20680 | var sboxInv = decTable[4];
|
20681 | var i;
|
20682 | var x;
|
20683 | var xInv;
|
20684 | var d = [];
|
20685 | var th = [];
|
20686 | var x2;
|
20687 | var x4;
|
20688 | var x8;
|
20689 | var s;
|
20690 | var tEnc;
|
20691 | var tDec;
|
20692 |
|
20693 | for (i = 0; i < 256; i++) {
|
20694 | th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
|
20695 | }
|
20696 |
|
20697 | for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
|
20698 |
|
20699 | s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
|
20700 | s = s >> 8 ^ s & 255 ^ 99;
|
20701 | sbox[x] = s;
|
20702 | sboxInv[s] = x;
|
20703 |
|
20704 | x8 = d[x4 = d[x2 = d[x]]];
|
20705 | tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
|
20706 | tEnc = d[s] * 0x101 ^ s * 0x1010100;
|
20707 |
|
20708 | for (i = 0; i < 4; i++) {
|
20709 | encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
|
20710 | decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
|
20711 | }
|
20712 | }
|
20713 |
|
20714 |
|
20715 | for (i = 0; i < 5; i++) {
|
20716 | encTable[i] = encTable[i].slice(0);
|
20717 | decTable[i] = decTable[i].slice(0);
|
20718 | }
|
20719 |
|
20720 | return tables;
|
20721 | };
|
20722 |
|
20723 | var aesTables = null;
|
20724 | |
20725 |
|
20726 |
|
20727 |
|
20728 |
|
20729 |
|
20730 |
|
20731 |
|
20732 | var AES = function () {
|
20733 | function AES(key) {
|
20734 | |
20735 |
|
20736 |
|
20737 |
|
20738 |
|
20739 |
|
20740 |
|
20741 |
|
20742 |
|
20743 |
|
20744 |
|
20745 |
|
20746 |
|
20747 |
|
20748 | if (!aesTables) {
|
20749 | aesTables = precompute();
|
20750 | }
|
20751 |
|
20752 |
|
20753 | this._tables = [[aesTables[0][0].slice(), aesTables[0][1].slice(), aesTables[0][2].slice(), aesTables[0][3].slice(), aesTables[0][4].slice()], [aesTables[1][0].slice(), aesTables[1][1].slice(), aesTables[1][2].slice(), aesTables[1][3].slice(), aesTables[1][4].slice()]];
|
20754 | var i;
|
20755 | var j;
|
20756 | var tmp;
|
20757 | var sbox = this._tables[0][4];
|
20758 | var decTable = this._tables[1];
|
20759 | var keyLen = key.length;
|
20760 | var rcon = 1;
|
20761 |
|
20762 | if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
|
20763 | throw new Error('Invalid aes key size');
|
20764 | }
|
20765 |
|
20766 | var encKey = key.slice(0);
|
20767 | var decKey = [];
|
20768 | this._key = [encKey, decKey];
|
20769 |
|
20770 | for (i = keyLen; i < 4 * keyLen + 28; i++) {
|
20771 | tmp = encKey[i - 1];
|
20772 |
|
20773 | if (i % keyLen === 0 || keyLen === 8 && i % keyLen === 4) {
|
20774 | tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
|
20775 |
|
20776 | if (i % keyLen === 0) {
|
20777 | tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
|
20778 | rcon = rcon << 1 ^ (rcon >> 7) * 283;
|
20779 | }
|
20780 | }
|
20781 |
|
20782 | encKey[i] = encKey[i - keyLen] ^ tmp;
|
20783 | }
|
20784 |
|
20785 |
|
20786 | for (j = 0; i; j++, i--) {
|
20787 | tmp = encKey[j & 3 ? i : i - 4];
|
20788 |
|
20789 | if (i <= 4 || j < 4) {
|
20790 | decKey[j] = tmp;
|
20791 | } else {
|
20792 | decKey[j] = decTable[0][sbox[tmp >>> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]];
|
20793 | }
|
20794 | }
|
20795 | }
|
20796 | |
20797 |
|
20798 |
|
20799 |
|
20800 |
|
20801 |
|
20802 |
|
20803 |
|
20804 |
|
20805 |
|
20806 |
|
20807 |
|
20808 |
|
20809 |
|
20810 |
|
20811 | var _proto = AES.prototype;
|
20812 |
|
20813 | _proto.decrypt = function decrypt(encrypted0, encrypted1, encrypted2, encrypted3, out, offset) {
|
20814 | var key = this._key[1];
|
20815 |
|
20816 | var a = encrypted0 ^ key[0];
|
20817 | var b = encrypted3 ^ key[1];
|
20818 | var c = encrypted2 ^ key[2];
|
20819 | var d = encrypted1 ^ key[3];
|
20820 | var a2;
|
20821 | var b2;
|
20822 | var c2;
|
20823 |
|
20824 | var nInnerRounds = key.length / 4 - 2;
|
20825 | var i;
|
20826 | var kIndex = 4;
|
20827 | var table = this._tables[1];
|
20828 |
|
20829 | var table0 = table[0];
|
20830 | var table1 = table[1];
|
20831 | var table2 = table[2];
|
20832 | var table3 = table[3];
|
20833 | var sbox = table[4];
|
20834 |
|
20835 | for (i = 0; i < nInnerRounds; i++) {
|
20836 | a2 = table0[a >>> 24] ^ table1[b >> 16 & 255] ^ table2[c >> 8 & 255] ^ table3[d & 255] ^ key[kIndex];
|
20837 | b2 = table0[b >>> 24] ^ table1[c >> 16 & 255] ^ table2[d >> 8 & 255] ^ table3[a & 255] ^ key[kIndex + 1];
|
20838 | c2 = table0[c >>> 24] ^ table1[d >> 16 & 255] ^ table2[a >> 8 & 255] ^ table3[b & 255] ^ key[kIndex + 2];
|
20839 | d = table0[d >>> 24] ^ table1[a >> 16 & 255] ^ table2[b >> 8 & 255] ^ table3[c & 255] ^ key[kIndex + 3];
|
20840 | kIndex += 4;
|
20841 | a = a2;
|
20842 | b = b2;
|
20843 | c = c2;
|
20844 | }
|
20845 |
|
20846 |
|
20847 | for (i = 0; i < 4; i++) {
|
20848 | out[(3 & -i) + offset] = sbox[a >>> 24] << 24 ^ sbox[b >> 16 & 255] << 16 ^ sbox[c >> 8 & 255] << 8 ^ sbox[d & 255] ^ key[kIndex++];
|
20849 | a2 = a;
|
20850 | a = b;
|
20851 | b = c;
|
20852 | c = d;
|
20853 | d = a2;
|
20854 | }
|
20855 | };
|
20856 |
|
20857 | return AES;
|
20858 | }();
|
20859 | |
20860 |
|
20861 |
|
20862 |
|
20863 |
|
20864 |
|
20865 |
|
20866 |
|
20867 |
|
20868 | var AsyncStream = function (_Stream) {
|
20869 | inheritsLoose(AsyncStream, _Stream);
|
20870 |
|
20871 | function AsyncStream() {
|
20872 | var _this;
|
20873 |
|
20874 | _this = _Stream.call(this, Stream) || this;
|
20875 | _this.jobs = [];
|
20876 | _this.delay = 1;
|
20877 | _this.timeout_ = null;
|
20878 | return _this;
|
20879 | }
|
20880 | |
20881 |
|
20882 |
|
20883 |
|
20884 |
|
20885 |
|
20886 |
|
20887 | var _proto = AsyncStream.prototype;
|
20888 |
|
20889 | _proto.processJob_ = function processJob_() {
|
20890 | this.jobs.shift()();
|
20891 |
|
20892 | if (this.jobs.length) {
|
20893 | this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
|
20894 | } else {
|
20895 | this.timeout_ = null;
|
20896 | }
|
20897 | }
|
20898 | |
20899 |
|
20900 |
|
20901 |
|
20902 |
|
20903 | ;
|
20904 |
|
20905 | _proto.push = function push(job) {
|
20906 | this.jobs.push(job);
|
20907 |
|
20908 | if (!this.timeout_) {
|
20909 | this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
|
20910 | }
|
20911 | };
|
20912 |
|
20913 | return AsyncStream;
|
20914 | }(Stream);
|
20915 | |
20916 |
|
20917 |
|
20918 |
|
20919 |
|
20920 |
|
20921 | var ntoh = function ntoh(word) {
|
20922 | return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
|
20923 | };
|
20924 | |
20925 |
|
20926 |
|
20927 |
|
20928 |
|
20929 |
|
20930 |
|
20931 |
|
20932 |
|
20933 |
|
20934 |
|
20935 |
|
20936 |
|
20937 |
|
20938 |
|
20939 | var decrypt = function decrypt(encrypted, key, initVector) {
|
20940 |
|
20941 | var encrypted32 = new Int32Array(encrypted.buffer, encrypted.byteOffset, encrypted.byteLength >> 2);
|
20942 | var decipher = new AES(Array.prototype.slice.call(key));
|
20943 |
|
20944 | var decrypted = new Uint8Array(encrypted.byteLength);
|
20945 | var decrypted32 = new Int32Array(decrypted.buffer);
|
20946 |
|
20947 |
|
20948 | var init0;
|
20949 | var init1;
|
20950 | var init2;
|
20951 | var init3;
|
20952 | var encrypted0;
|
20953 | var encrypted1;
|
20954 | var encrypted2;
|
20955 | var encrypted3;
|
20956 |
|
20957 | var wordIx;
|
20958 |
|
20959 |
|
20960 | init0 = initVector[0];
|
20961 | init1 = initVector[1];
|
20962 | init2 = initVector[2];
|
20963 | init3 = initVector[3];
|
20964 |
|
20965 |
|
20966 | for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
|
20967 |
|
20968 |
|
20969 | encrypted0 = ntoh(encrypted32[wordIx]);
|
20970 | encrypted1 = ntoh(encrypted32[wordIx + 1]);
|
20971 | encrypted2 = ntoh(encrypted32[wordIx + 2]);
|
20972 | encrypted3 = ntoh(encrypted32[wordIx + 3]);
|
20973 |
|
20974 | decipher.decrypt(encrypted0, encrypted1, encrypted2, encrypted3, decrypted32, wordIx);
|
20975 |
|
20976 |
|
20977 | decrypted32[wordIx] = ntoh(decrypted32[wordIx] ^ init0);
|
20978 | decrypted32[wordIx + 1] = ntoh(decrypted32[wordIx + 1] ^ init1);
|
20979 | decrypted32[wordIx + 2] = ntoh(decrypted32[wordIx + 2] ^ init2);
|
20980 | decrypted32[wordIx + 3] = ntoh(decrypted32[wordIx + 3] ^ init3);
|
20981 |
|
20982 | init0 = encrypted0;
|
20983 | init1 = encrypted1;
|
20984 | init2 = encrypted2;
|
20985 | init3 = encrypted3;
|
20986 | }
|
20987 |
|
20988 | return decrypted;
|
20989 | };
|
20990 | |
20991 |
|
20992 |
|
20993 |
|
20994 |
|
20995 |
|
20996 |
|
20997 |
|
20998 |
|
20999 |
|
21000 |
|
21001 |
|
21002 |
|
21003 | var Decrypter = function () {
|
21004 | function Decrypter(encrypted, key, initVector, done) {
|
21005 | var step = Decrypter.STEP;
|
21006 | var encrypted32 = new Int32Array(encrypted.buffer);
|
21007 | var decrypted = new Uint8Array(encrypted.byteLength);
|
21008 | var i = 0;
|
21009 | this.asyncStream_ = new AsyncStream();
|
21010 |
|
21011 | this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
|
21012 |
|
21013 | for (i = step; i < encrypted32.length; i += step) {
|
21014 | initVector = new Uint32Array([ntoh(encrypted32[i - 4]), ntoh(encrypted32[i - 3]), ntoh(encrypted32[i - 2]), ntoh(encrypted32[i - 1])]);
|
21015 | this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
|
21016 | }
|
21017 |
|
21018 |
|
21019 | this.asyncStream_.push(function () {
|
21020 |
|
21021 | done(null, unpad(decrypted));
|
21022 | });
|
21023 | }
|
21024 | |
21025 |
|
21026 |
|
21027 |
|
21028 |
|
21029 |
|
21030 |
|
21031 | var _proto = Decrypter.prototype;
|
21032 | |
21033 |
|
21034 |
|
21035 |
|
21036 | _proto.decryptChunk_ = function decryptChunk_(encrypted, key, initVector, decrypted) {
|
21037 | return function () {
|
21038 | var bytes = decrypt(encrypted, key, initVector);
|
21039 | decrypted.set(bytes, encrypted.byteOffset);
|
21040 | };
|
21041 | };
|
21042 |
|
21043 | createClass(Decrypter, null, [{
|
21044 | key: "STEP",
|
21045 | get: function get() {
|
21046 |
|
21047 | return 32000;
|
21048 | }
|
21049 | }]);
|
21050 | return Decrypter;
|
21051 | }();
|
21052 | |
21053 |
|
21054 |
|
21055 |
|
21056 | |
21057 |
|
21058 |
|
21059 |
|
21060 |
|
21061 |
|
21062 |
|
21063 |
|
21064 |
|
21065 |
|
21066 |
|
21067 |
|
21068 | var createTransferableMessage = function createTransferableMessage(message) {
|
21069 | var transferable = {};
|
21070 | Object.keys(message).forEach(function (key) {
|
21071 | var value = message[key];
|
21072 |
|
21073 | if (ArrayBuffer.isView(value)) {
|
21074 | transferable[key] = {
|
21075 | bytes: value.buffer,
|
21076 | byteOffset: value.byteOffset,
|
21077 | byteLength: value.byteLength
|
21078 | };
|
21079 | } else {
|
21080 | transferable[key] = value;
|
21081 | }
|
21082 | });
|
21083 | return transferable;
|
21084 | };
|
21085 |
|
21086 |
|
21087 | |
21088 |
|
21089 |
|
21090 |
|
21091 |
|
21092 |
|
21093 |
|
21094 | self.onmessage = function (event) {
|
21095 | var data = event.data;
|
21096 | var encrypted = new Uint8Array(data.encrypted.bytes, data.encrypted.byteOffset, data.encrypted.byteLength);
|
21097 | var key = new Uint32Array(data.key.bytes, data.key.byteOffset, data.key.byteLength / 4);
|
21098 | var iv = new Uint32Array(data.iv.bytes, data.iv.byteOffset, data.iv.byteLength / 4);
|
21099 |
|
21100 |
|
21101 | new Decrypter(encrypted, key, iv, function (err, bytes) {
|
21102 | self.postMessage(createTransferableMessage({
|
21103 | source: data.source,
|
21104 | decrypted: bytes
|
21105 | }), [bytes.buffer]);
|
21106 | });
|
21107 |
|
21108 | };
|
21109 | }));
|
21110 | var Decrypter = factory(workerCode);
|
21111 |
|
21112 |
|
21113 |
|
21114 |
|
21115 |
|
21116 |
|
21117 |
|
21118 |
|
21119 | var audioTrackKind_ = function audioTrackKind_(properties) {
|
21120 | var kind = properties.default ? 'main' : 'alternative';
|
21121 |
|
21122 | if (properties.characteristics && properties.characteristics.indexOf('public.accessibility.describes-video') >= 0) {
|
21123 | kind = 'main-desc';
|
21124 | }
|
21125 |
|
21126 | return kind;
|
21127 | };
|
21128 |
|
21129 |
|
21130 |
|
21131 |
|
21132 |
|
21133 |
|
21134 |
|
21135 |
|
21136 |
|
21137 |
|
21138 |
|
21139 | var stopLoaders = function stopLoaders(segmentLoader, mediaType) {
|
21140 | segmentLoader.abort();
|
21141 | segmentLoader.pause();
|
21142 |
|
21143 | if (mediaType && mediaType.activePlaylistLoader) {
|
21144 | mediaType.activePlaylistLoader.pause();
|
21145 | mediaType.activePlaylistLoader = null;
|
21146 | }
|
21147 | };
|
21148 |
|
21149 |
|
21150 |
|
21151 |
|
21152 |
|
21153 |
|
21154 |
|
21155 |
|
21156 |
|
21157 |
|
21158 | var startLoaders = function startLoaders(playlistLoader, mediaType) {
|
21159 |
|
21160 |
|
21161 | mediaType.activePlaylistLoader = playlistLoader;
|
21162 | playlistLoader.load();
|
21163 | };
|
21164 |
|
21165 |
|
21166 |
|
21167 |
|
21168 |
|
21169 |
|
21170 |
|
21171 |
|
21172 |
|
21173 |
|
21174 |
|
21175 |
|
21176 |
|
21177 |
|
21178 |
|
21179 |
|
21180 | var onGroupChanged = function onGroupChanged(type, settings) {
|
21181 | return function () {
|
21182 | var _settings$segmentLoad = settings.segmentLoaders,
|
21183 | segmentLoader = _settings$segmentLoad[type],
|
21184 | mainSegmentLoader = _settings$segmentLoad.main,
|
21185 | mediaType = settings.mediaTypes[type];
|
21186 | var activeTrack = mediaType.activeTrack();
|
21187 | var activeGroup = mediaType.getActiveGroup();
|
21188 | var previousActiveLoader = mediaType.activePlaylistLoader;
|
21189 | var lastGroup = mediaType.lastGroup_;
|
21190 |
|
21191 | if (activeGroup && lastGroup && activeGroup.id === lastGroup.id) {
|
21192 | return;
|
21193 | }
|
21194 |
|
21195 | mediaType.lastGroup_ = activeGroup;
|
21196 | mediaType.lastTrack_ = activeTrack;
|
21197 | stopLoaders(segmentLoader, mediaType);
|
21198 |
|
21199 | if (!activeGroup || activeGroup.isMasterPlaylist) {
|
21200 |
|
21201 | return;
|
21202 | }
|
21203 |
|
21204 | if (!activeGroup.playlistLoader) {
|
21205 | if (previousActiveLoader) {
|
21206 |
|
21207 |
|
21208 |
|
21209 |
|
21210 | mainSegmentLoader.resetEverything();
|
21211 | }
|
21212 |
|
21213 | return;
|
21214 | }
|
21215 |
|
21216 |
|
21217 | segmentLoader.resyncLoader();
|
21218 | startLoaders(activeGroup.playlistLoader, mediaType);
|
21219 | };
|
21220 | };
|
21221 | var onGroupChanging = function onGroupChanging(type, settings) {
|
21222 | return function () {
|
21223 | var segmentLoader = settings.segmentLoaders[type],
|
21224 | mediaType = settings.mediaTypes[type];
|
21225 | mediaType.lastGroup_ = null;
|
21226 | segmentLoader.abort();
|
21227 | segmentLoader.pause();
|
21228 | };
|
21229 | };
|
21230 |
|
21231 |
|
21232 |
|
21233 |
|
21234 |
|
21235 |
|
21236 |
|
21237 |
|
21238 |
|
21239 |
|
21240 |
|
21241 |
|
21242 |
|
21243 |
|
21244 |
|
21245 | var onTrackChanged = function onTrackChanged(type, settings) {
|
21246 | return function () {
|
21247 | var masterPlaylistLoader = settings.masterPlaylistLoader,
|
21248 | _settings$segmentLoad2 = settings.segmentLoaders,
|
21249 | segmentLoader = _settings$segmentLoad2[type],
|
21250 | mainSegmentLoader = _settings$segmentLoad2.main,
|
21251 | mediaType = settings.mediaTypes[type];
|
21252 | var activeTrack = mediaType.activeTrack();
|
21253 | var activeGroup = mediaType.getActiveGroup();
|
21254 | var previousActiveLoader = mediaType.activePlaylistLoader;
|
21255 | var lastTrack = mediaType.lastTrack_;
|
21256 |
|
21257 | if (lastTrack && activeTrack && lastTrack.id === activeTrack.id) {
|
21258 | return;
|
21259 | }
|
21260 |
|
21261 | mediaType.lastGroup_ = activeGroup;
|
21262 | mediaType.lastTrack_ = activeTrack;
|
21263 | stopLoaders(segmentLoader, mediaType);
|
21264 |
|
21265 | if (!activeGroup) {
|
21266 |
|
21267 | return;
|
21268 | }
|
21269 |
|
21270 | if (activeGroup.isMasterPlaylist) {
|
21271 |
|
21272 | if (!activeTrack || !lastTrack || activeTrack.id === lastTrack.id) {
|
21273 | return;
|
21274 | }
|
21275 |
|
21276 | var mpc = settings.vhs.masterPlaylistController_;
|
21277 | var newPlaylist = mpc.selectPlaylist();
|
21278 |
|
21279 | if (mpc.media() === newPlaylist) {
|
21280 | return;
|
21281 | }
|
21282 |
|
21283 | mediaType.logger_("track change. Switching master audio from " + lastTrack.id + " to " + activeTrack.id);
|
21284 | masterPlaylistLoader.pause();
|
21285 | mainSegmentLoader.resetEverything();
|
21286 | mpc.fastQualityChange_(newPlaylist);
|
21287 | return;
|
21288 | }
|
21289 |
|
21290 | if (type === 'AUDIO') {
|
21291 | if (!activeGroup.playlistLoader) {
|
21292 |
|
21293 |
|
21294 |
|
21295 | mainSegmentLoader.setAudio(true);
|
21296 |
|
21297 |
|
21298 | mainSegmentLoader.resetEverything();
|
21299 | return;
|
21300 | }
|
21301 |
|
21302 |
|
21303 |
|
21304 |
|
21305 | segmentLoader.setAudio(true);
|
21306 | mainSegmentLoader.setAudio(false);
|
21307 | }
|
21308 |
|
21309 | if (previousActiveLoader === activeGroup.playlistLoader) {
|
21310 |
|
21311 |
|
21312 |
|
21313 | startLoaders(activeGroup.playlistLoader, mediaType);
|
21314 | return;
|
21315 | }
|
21316 |
|
21317 | if (segmentLoader.track) {
|
21318 |
|
21319 | segmentLoader.track(activeTrack);
|
21320 | }
|
21321 |
|
21322 |
|
21323 | segmentLoader.resetEverything();
|
21324 | startLoaders(activeGroup.playlistLoader, mediaType);
|
21325 | };
|
21326 | };
|
21327 | var onError = {
|
21328 | |
21329 |
|
21330 |
|
21331 |
|
21332 |
|
21333 |
|
21334 |
|
21335 |
|
21336 |
|
21337 |
|
21338 |
|
21339 |
|
21340 |
|
21341 | AUDIO: function AUDIO(type, settings) {
|
21342 | return function () {
|
21343 | var segmentLoader = settings.segmentLoaders[type],
|
21344 | mediaType = settings.mediaTypes[type],
|
21345 | blacklistCurrentPlaylist = settings.blacklistCurrentPlaylist;
|
21346 | stopLoaders(segmentLoader, mediaType);
|
21347 |
|
21348 | var activeTrack = mediaType.activeTrack();
|
21349 | var activeGroup = mediaType.activeGroup();
|
21350 | var id = (activeGroup.filter(function (group) {
|
21351 | return group.default;
|
21352 | })[0] || activeGroup[0]).id;
|
21353 | var defaultTrack = mediaType.tracks[id];
|
21354 |
|
21355 | if (activeTrack === defaultTrack) {
|
21356 |
|
21357 |
|
21358 | blacklistCurrentPlaylist({
|
21359 | message: 'Problem encountered loading the default audio track.'
|
21360 | });
|
21361 | return;
|
21362 | }
|
21363 |
|
21364 | videojs.log.warn('Problem encountered loading the alternate audio track.' + 'Switching back to default.');
|
21365 |
|
21366 | for (var trackId in mediaType.tracks) {
|
21367 | mediaType.tracks[trackId].enabled = mediaType.tracks[trackId] === defaultTrack;
|
21368 | }
|
21369 |
|
21370 | mediaType.onTrackChanged();
|
21371 | };
|
21372 | },
|
21373 |
|
21374 | |
21375 |
|
21376 |
|
21377 |
|
21378 |
|
21379 |
|
21380 |
|
21381 |
|
21382 |
|
21383 |
|
21384 |
|
21385 |
|
21386 | SUBTITLES: function SUBTITLES(type, settings) {
|
21387 | return function () {
|
21388 | var segmentLoader = settings.segmentLoaders[type],
|
21389 | mediaType = settings.mediaTypes[type];
|
21390 | videojs.log.warn('Problem encountered loading the subtitle track.' + 'Disabling subtitle track.');
|
21391 | stopLoaders(segmentLoader, mediaType);
|
21392 | var track = mediaType.activeTrack();
|
21393 |
|
21394 | if (track) {
|
21395 | track.mode = 'disabled';
|
21396 | }
|
21397 |
|
21398 | mediaType.onTrackChanged();
|
21399 | };
|
21400 | }
|
21401 | };
|
21402 | var setupListeners = {
|
21403 | |
21404 |
|
21405 |
|
21406 |
|
21407 |
|
21408 |
|
21409 |
|
21410 |
|
21411 |
|
21412 |
|
21413 |
|
21414 | AUDIO: function AUDIO(type, playlistLoader, settings) {
|
21415 | if (!playlistLoader) {
|
21416 |
|
21417 | return;
|
21418 | }
|
21419 |
|
21420 | var tech = settings.tech,
|
21421 | requestOptions = settings.requestOptions,
|
21422 | segmentLoader = settings.segmentLoaders[type];
|
21423 | playlistLoader.on('loadedmetadata', function () {
|
21424 | var media = playlistLoader.media();
|
21425 | segmentLoader.playlist(media, requestOptions);
|
21426 |
|
21427 |
|
21428 | if (!tech.paused() || media.endList && tech.preload() !== 'none') {
|
21429 | segmentLoader.load();
|
21430 | }
|
21431 | });
|
21432 | playlistLoader.on('loadedplaylist', function () {
|
21433 | segmentLoader.playlist(playlistLoader.media(), requestOptions);
|
21434 |
|
21435 | if (!tech.paused()) {
|
21436 | segmentLoader.load();
|
21437 | }
|
21438 | });
|
21439 | playlistLoader.on('error', onError[type](type, settings));
|
21440 | },
|
21441 |
|
21442 | |
21443 |
|
21444 |
|
21445 |
|
21446 |
|
21447 |
|
21448 |
|
21449 |
|
21450 |
|
21451 |
|
21452 |
|
21453 | SUBTITLES: function SUBTITLES(type, playlistLoader, settings) {
|
21454 | var tech = settings.tech,
|
21455 | requestOptions = settings.requestOptions,
|
21456 | segmentLoader = settings.segmentLoaders[type],
|
21457 | mediaType = settings.mediaTypes[type];
|
21458 | playlistLoader.on('loadedmetadata', function () {
|
21459 | var media = playlistLoader.media();
|
21460 | segmentLoader.playlist(media, requestOptions);
|
21461 | segmentLoader.track(mediaType.activeTrack());
|
21462 |
|
21463 |
|
21464 | if (!tech.paused() || media.endList && tech.preload() !== 'none') {
|
21465 | segmentLoader.load();
|
21466 | }
|
21467 | });
|
21468 | playlistLoader.on('loadedplaylist', function () {
|
21469 | segmentLoader.playlist(playlistLoader.media(), requestOptions);
|
21470 |
|
21471 | if (!tech.paused()) {
|
21472 | segmentLoader.load();
|
21473 | }
|
21474 | });
|
21475 | playlistLoader.on('error', onError[type](type, settings));
|
21476 | }
|
21477 | };
|
21478 | var initialize = {
|
21479 | |
21480 |
|
21481 |
|
21482 |
|
21483 |
|
21484 |
|
21485 |
|
21486 |
|
21487 |
|
21488 | 'AUDIO': function AUDIO(type, settings) {
|
21489 | var vhs = settings.vhs,
|
21490 | sourceType = settings.sourceType,
|
21491 | segmentLoader = settings.segmentLoaders[type],
|
21492 | requestOptions = settings.requestOptions,
|
21493 | mediaGroups = settings.master.mediaGroups,
|
21494 | _settings$mediaTypes$ = settings.mediaTypes[type],
|
21495 | groups = _settings$mediaTypes$.groups,
|
21496 | tracks = _settings$mediaTypes$.tracks,
|
21497 | logger_ = _settings$mediaTypes$.logger_,
|
21498 | masterPlaylistLoader = settings.masterPlaylistLoader;
|
21499 | var audioOnlyMaster = isAudioOnly(masterPlaylistLoader.master);
|
21500 |
|
21501 | if (!mediaGroups[type] || Object.keys(mediaGroups[type]).length === 0) {
|
21502 | mediaGroups[type] = {
|
21503 | main: {
|
21504 | default: {
|
21505 | default: true
|
21506 | }
|
21507 | }
|
21508 | };
|
21509 |
|
21510 | if (audioOnlyMaster) {
|
21511 | mediaGroups[type].main.default.playlists = masterPlaylistLoader.master.playlists;
|
21512 | }
|
21513 | }
|
21514 |
|
21515 | for (var groupId in mediaGroups[type]) {
|
21516 | if (!groups[groupId]) {
|
21517 | groups[groupId] = [];
|
21518 | }
|
21519 |
|
21520 | for (var variantLabel in mediaGroups[type][groupId]) {
|
21521 | var properties = mediaGroups[type][groupId][variantLabel];
|
21522 | var playlistLoader = void 0;
|
21523 |
|
21524 | if (audioOnlyMaster) {
|
21525 | logger_("AUDIO group '" + groupId + "' label '" + variantLabel + "' is a master playlist");
|
21526 | properties.isMasterPlaylist = true;
|
21527 | playlistLoader = null;
|
21528 |
|
21529 | } else if (sourceType === 'vhs-json' && properties.playlists) {
|
21530 | playlistLoader = new PlaylistLoader(properties.playlists[0], vhs, requestOptions);
|
21531 | } else if (properties.resolvedUri) {
|
21532 | playlistLoader = new PlaylistLoader(properties.resolvedUri, vhs, requestOptions);
|
21533 |
|
21534 | } else if (properties.playlists && sourceType === 'dash') {
|
21535 | playlistLoader = new DashPlaylistLoader(properties.playlists[0], vhs, requestOptions, masterPlaylistLoader);
|
21536 | } else {
|
21537 |
|
21538 |
|
21539 | playlistLoader = null;
|
21540 | }
|
21541 |
|
21542 | properties = videojs.mergeOptions({
|
21543 | id: variantLabel,
|
21544 | playlistLoader: playlistLoader
|
21545 | }, properties);
|
21546 | setupListeners[type](type, properties.playlistLoader, settings);
|
21547 | groups[groupId].push(properties);
|
21548 |
|
21549 | if (typeof tracks[variantLabel] === 'undefined') {
|
21550 | var track = new videojs.AudioTrack({
|
21551 | id: variantLabel,
|
21552 | kind: audioTrackKind_(properties),
|
21553 | enabled: false,
|
21554 | language: properties.language,
|
21555 | default: properties.default,
|
21556 | label: variantLabel
|
21557 | });
|
21558 | tracks[variantLabel] = track;
|
21559 | }
|
21560 | }
|
21561 | }
|
21562 |
|
21563 |
|
21564 | segmentLoader.on('error', onError[type](type, settings));
|
21565 | },
|
21566 |
|
21567 | |
21568 |
|
21569 |
|
21570 |
|
21571 |
|
21572 |
|
21573 |
|
21574 |
|
21575 |
|
21576 | 'SUBTITLES': function SUBTITLES(type, settings) {
|
21577 | var tech = settings.tech,
|
21578 | vhs = settings.vhs,
|
21579 | sourceType = settings.sourceType,
|
21580 | segmentLoader = settings.segmentLoaders[type],
|
21581 | requestOptions = settings.requestOptions,
|
21582 | mediaGroups = settings.master.mediaGroups,
|
21583 | _settings$mediaTypes$2 = settings.mediaTypes[type],
|
21584 | groups = _settings$mediaTypes$2.groups,
|
21585 | tracks = _settings$mediaTypes$2.tracks,
|
21586 | masterPlaylistLoader = settings.masterPlaylistLoader;
|
21587 |
|
21588 | for (var groupId in mediaGroups[type]) {
|
21589 | if (!groups[groupId]) {
|
21590 | groups[groupId] = [];
|
21591 | }
|
21592 |
|
21593 | for (var variantLabel in mediaGroups[type][groupId]) {
|
21594 | if (mediaGroups[type][groupId][variantLabel].forced) {
|
21595 |
|
21596 |
|
21597 |
|
21598 |
|
21599 |
|
21600 |
|
21601 |
|
21602 |
|
21603 | continue;
|
21604 | }
|
21605 |
|
21606 | var properties = mediaGroups[type][groupId][variantLabel];
|
21607 | var playlistLoader = void 0;
|
21608 |
|
21609 | if (sourceType === 'hls') {
|
21610 | playlistLoader = new PlaylistLoader(properties.resolvedUri, vhs, requestOptions);
|
21611 | } else if (sourceType === 'dash') {
|
21612 | var playlists = properties.playlists.filter(function (p) {
|
21613 | return p.excludeUntil !== Infinity;
|
21614 | });
|
21615 |
|
21616 | if (!playlists.length) {
|
21617 | return;
|
21618 | }
|
21619 |
|
21620 | playlistLoader = new DashPlaylistLoader(properties.playlists[0], vhs, requestOptions, masterPlaylistLoader);
|
21621 | } else if (sourceType === 'vhs-json') {
|
21622 | playlistLoader = new PlaylistLoader(
|
21623 |
|
21624 | properties.playlists ? properties.playlists[0] : properties.resolvedUri, vhs, requestOptions);
|
21625 | }
|
21626 |
|
21627 | properties = videojs.mergeOptions({
|
21628 | id: variantLabel,
|
21629 | playlistLoader: playlistLoader
|
21630 | }, properties);
|
21631 | setupListeners[type](type, properties.playlistLoader, settings);
|
21632 | groups[groupId].push(properties);
|
21633 |
|
21634 | if (typeof tracks[variantLabel] === 'undefined') {
|
21635 | var track = tech.addRemoteTextTrack({
|
21636 | id: variantLabel,
|
21637 | kind: 'subtitles',
|
21638 | default: properties.default && properties.autoselect,
|
21639 | language: properties.language,
|
21640 | label: variantLabel
|
21641 | }, false).track;
|
21642 | tracks[variantLabel] = track;
|
21643 | }
|
21644 | }
|
21645 | }
|
21646 |
|
21647 |
|
21648 | segmentLoader.on('error', onError[type](type, settings));
|
21649 | },
|
21650 |
|
21651 | |
21652 |
|
21653 |
|
21654 |
|
21655 |
|
21656 |
|
21657 |
|
21658 |
|
21659 |
|
21660 | 'CLOSED-CAPTIONS': function CLOSEDCAPTIONS(type, settings) {
|
21661 | var tech = settings.tech,
|
21662 | mediaGroups = settings.master.mediaGroups,
|
21663 | _settings$mediaTypes$3 = settings.mediaTypes[type],
|
21664 | groups = _settings$mediaTypes$3.groups,
|
21665 | tracks = _settings$mediaTypes$3.tracks;
|
21666 |
|
21667 | for (var groupId in mediaGroups[type]) {
|
21668 | if (!groups[groupId]) {
|
21669 | groups[groupId] = [];
|
21670 | }
|
21671 |
|
21672 | for (var variantLabel in mediaGroups[type][groupId]) {
|
21673 | var properties = mediaGroups[type][groupId][variantLabel];
|
21674 |
|
21675 | if (!/^(?:CC|SERVICE)/.test(properties.instreamId)) {
|
21676 | continue;
|
21677 | }
|
21678 |
|
21679 | var captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
|
21680 | var newProps = {
|
21681 | label: variantLabel,
|
21682 | language: properties.language,
|
21683 | instreamId: properties.instreamId,
|
21684 | default: properties.default && properties.autoselect
|
21685 | };
|
21686 |
|
21687 | if (captionServices[newProps.instreamId]) {
|
21688 | newProps = videojs.mergeOptions(newProps, captionServices[newProps.instreamId]);
|
21689 | }
|
21690 |
|
21691 | if (newProps.default === undefined) {
|
21692 | delete newProps.default;
|
21693 | }
|
21694 |
|
21695 |
|
21696 |
|
21697 | groups[groupId].push(videojs.mergeOptions({
|
21698 | id: variantLabel
|
21699 | }, properties));
|
21700 |
|
21701 | if (typeof tracks[variantLabel] === 'undefined') {
|
21702 | var track = tech.addRemoteTextTrack({
|
21703 | id: newProps.instreamId,
|
21704 | kind: 'captions',
|
21705 | default: newProps.default,
|
21706 | language: newProps.language,
|
21707 | label: newProps.label
|
21708 | }, false).track;
|
21709 | tracks[variantLabel] = track;
|
21710 | }
|
21711 | }
|
21712 | }
|
21713 | }
|
21714 | };
|
21715 |
|
21716 | var groupMatch = function groupMatch(list, media) {
|
21717 | for (var i = 0; i < list.length; i++) {
|
21718 | if (playlistMatch(media, list[i])) {
|
21719 | return true;
|
21720 | }
|
21721 |
|
21722 | if (list[i].playlists && groupMatch(list[i].playlists, media)) {
|
21723 | return true;
|
21724 | }
|
21725 | }
|
21726 |
|
21727 | return false;
|
21728 | };
|
21729 |
|
21730 |
|
21731 |
|
21732 |
|
21733 |
|
21734 |
|
21735 |
|
21736 |
|
21737 |
|
21738 |
|
21739 |
|
21740 |
|
21741 |
|
21742 |
|
21743 |
|
21744 |
|
21745 | var activeGroup = function activeGroup(type, settings) {
|
21746 | return function (track) {
|
21747 | var masterPlaylistLoader = settings.masterPlaylistLoader,
|
21748 | groups = settings.mediaTypes[type].groups;
|
21749 | var media = masterPlaylistLoader.media();
|
21750 |
|
21751 | if (!media) {
|
21752 | return null;
|
21753 | }
|
21754 |
|
21755 | var variants = null;
|
21756 |
|
21757 | if (media.attributes[type]) {
|
21758 | variants = groups[media.attributes[type]];
|
21759 | }
|
21760 |
|
21761 | var groupKeys = Object.keys(groups);
|
21762 |
|
21763 | if (!variants) {
|
21764 |
|
21765 |
|
21766 |
|
21767 | if (type === 'AUDIO' && groupKeys.length > 1 && isAudioOnly(settings.master)) {
|
21768 | for (var i = 0; i < groupKeys.length; i++) {
|
21769 | var groupPropertyList = groups[groupKeys[i]];
|
21770 |
|
21771 | if (groupMatch(groupPropertyList, media)) {
|
21772 | variants = groupPropertyList;
|
21773 | break;
|
21774 | }
|
21775 | }
|
21776 |
|
21777 | } else if (groups.main) {
|
21778 | variants = groups.main;
|
21779 | } else if (groupKeys.length === 1) {
|
21780 | variants = groups[groupKeys[0]];
|
21781 | }
|
21782 | }
|
21783 |
|
21784 | if (typeof track === 'undefined') {
|
21785 | return variants;
|
21786 | }
|
21787 |
|
21788 | if (track === null || !variants) {
|
21789 |
|
21790 |
|
21791 | return null;
|
21792 | }
|
21793 |
|
21794 | return variants.filter(function (props) {
|
21795 | return props.id === track.id;
|
21796 | })[0] || null;
|
21797 | };
|
21798 | };
|
21799 | var activeTrack = {
|
21800 | |
21801 |
|
21802 |
|
21803 |
|
21804 |
|
21805 |
|
21806 |
|
21807 |
|
21808 |
|
21809 |
|
21810 |
|
21811 |
|
21812 | AUDIO: function AUDIO(type, settings) {
|
21813 | return function () {
|
21814 | var tracks = settings.mediaTypes[type].tracks;
|
21815 |
|
21816 | for (var id in tracks) {
|
21817 | if (tracks[id].enabled) {
|
21818 | return tracks[id];
|
21819 | }
|
21820 | }
|
21821 |
|
21822 | return null;
|
21823 | };
|
21824 | },
|
21825 |
|
21826 | |
21827 |
|
21828 |
|
21829 |
|
21830 |
|
21831 |
|
21832 |
|
21833 |
|
21834 |
|
21835 |
|
21836 |
|
21837 |
|
21838 | SUBTITLES: function SUBTITLES(type, settings) {
|
21839 | return function () {
|
21840 | var tracks = settings.mediaTypes[type].tracks;
|
21841 |
|
21842 | for (var id in tracks) {
|
21843 | if (tracks[id].mode === 'showing' || tracks[id].mode === 'hidden') {
|
21844 | return tracks[id];
|
21845 | }
|
21846 | }
|
21847 |
|
21848 | return null;
|
21849 | };
|
21850 | }
|
21851 | };
|
21852 | var getActiveGroup = function getActiveGroup(type, _ref) {
|
21853 | var mediaTypes = _ref.mediaTypes;
|
21854 | return function () {
|
21855 | var activeTrack_ = mediaTypes[type].activeTrack();
|
21856 |
|
21857 | if (!activeTrack_) {
|
21858 | return null;
|
21859 | }
|
21860 |
|
21861 | return mediaTypes[type].activeGroup(activeTrack_);
|
21862 | };
|
21863 | };
|
21864 |
|
21865 |
|
21866 |
|
21867 |
|
21868 |
|
21869 |
|
21870 |
|
21871 |
|
21872 |
|
21873 |
|
21874 |
|
21875 |
|
21876 |
|
21877 |
|
21878 |
|
21879 |
|
21880 |
|
21881 |
|
21882 |
|
21883 |
|
21884 |
|
21885 |
|
21886 |
|
21887 | var setupMediaGroups = function setupMediaGroups(settings) {
|
21888 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(function (type) {
|
21889 | initialize[type](type, settings);
|
21890 | });
|
21891 | var mediaTypes = settings.mediaTypes,
|
21892 | masterPlaylistLoader = settings.masterPlaylistLoader,
|
21893 | tech = settings.tech,
|
21894 | vhs = settings.vhs,
|
21895 | _settings$segmentLoad3 = settings.segmentLoaders,
|
21896 | audioSegmentLoader = _settings$segmentLoad3['AUDIO'],
|
21897 | mainSegmentLoader = _settings$segmentLoad3.main;
|
21898 |
|
21899 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
21900 | mediaTypes[type].activeGroup = activeGroup(type, settings);
|
21901 | mediaTypes[type].activeTrack = activeTrack[type](type, settings);
|
21902 | mediaTypes[type].onGroupChanged = onGroupChanged(type, settings);
|
21903 | mediaTypes[type].onGroupChanging = onGroupChanging(type, settings);
|
21904 | mediaTypes[type].onTrackChanged = onTrackChanged(type, settings);
|
21905 | mediaTypes[type].getActiveGroup = getActiveGroup(type, settings);
|
21906 | });
|
21907 |
|
21908 |
|
21909 | var audioGroup = mediaTypes.AUDIO.activeGroup();
|
21910 |
|
21911 | if (audioGroup) {
|
21912 | var groupId = (audioGroup.filter(function (group) {
|
21913 | return group.default;
|
21914 | })[0] || audioGroup[0]).id;
|
21915 | mediaTypes.AUDIO.tracks[groupId].enabled = true;
|
21916 | mediaTypes.AUDIO.onGroupChanged();
|
21917 | mediaTypes.AUDIO.onTrackChanged();
|
21918 | var activeAudioGroup = mediaTypes.AUDIO.getActiveGroup();
|
21919 |
|
21920 |
|
21921 |
|
21922 | if (!activeAudioGroup.playlistLoader) {
|
21923 |
|
21924 | mainSegmentLoader.setAudio(true);
|
21925 | } else {
|
21926 |
|
21927 | mainSegmentLoader.setAudio(false);
|
21928 | audioSegmentLoader.setAudio(true);
|
21929 | }
|
21930 | }
|
21931 |
|
21932 | masterPlaylistLoader.on('mediachange', function () {
|
21933 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
21934 | return mediaTypes[type].onGroupChanged();
|
21935 | });
|
21936 | });
|
21937 | masterPlaylistLoader.on('mediachanging', function () {
|
21938 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
21939 | return mediaTypes[type].onGroupChanging();
|
21940 | });
|
21941 | });
|
21942 |
|
21943 | var onAudioTrackChanged = function onAudioTrackChanged() {
|
21944 | mediaTypes.AUDIO.onTrackChanged();
|
21945 | tech.trigger({
|
21946 | type: 'usage',
|
21947 | name: 'vhs-audio-change'
|
21948 | });
|
21949 | tech.trigger({
|
21950 | type: 'usage',
|
21951 | name: 'hls-audio-change'
|
21952 | });
|
21953 | };
|
21954 |
|
21955 | tech.audioTracks().addEventListener('change', onAudioTrackChanged);
|
21956 | tech.remoteTextTracks().addEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
|
21957 | vhs.on('dispose', function () {
|
21958 | tech.audioTracks().removeEventListener('change', onAudioTrackChanged);
|
21959 | tech.remoteTextTracks().removeEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
|
21960 | });
|
21961 |
|
21962 | tech.clearTracks('audio');
|
21963 |
|
21964 | for (var id in mediaTypes.AUDIO.tracks) {
|
21965 | tech.audioTracks().addTrack(mediaTypes.AUDIO.tracks[id]);
|
21966 | }
|
21967 | };
|
21968 |
|
21969 |
|
21970 |
|
21971 |
|
21972 |
|
21973 |
|
21974 |
|
21975 |
|
21976 |
|
21977 | var createMediaTypes = function createMediaTypes() {
|
21978 | var mediaTypes = {};
|
21979 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(function (type) {
|
21980 | mediaTypes[type] = {
|
21981 | groups: {},
|
21982 | tracks: {},
|
21983 | activePlaylistLoader: null,
|
21984 | activeGroup: noop,
|
21985 | activeTrack: noop,
|
21986 | getActiveGroup: noop,
|
21987 | onGroupChanged: noop,
|
21988 | onTrackChanged: noop,
|
21989 | lastTrack_: null,
|
21990 | logger_: logger("MediaGroups[" + type + "]")
|
21991 | };
|
21992 | });
|
21993 | return mediaTypes;
|
21994 | };
|
21995 |
|
21996 | var ABORT_EARLY_BLACKLIST_SECONDS = 60 * 2;
|
21997 | var Vhs$1;
|
21998 |
|
21999 |
|
22000 | var loaderStats = ['mediaRequests', 'mediaRequestsAborted', 'mediaRequestsTimedout', 'mediaRequestsErrored', 'mediaTransferDuration', 'mediaBytesTransferred', 'mediaAppends'];
|
22001 |
|
22002 | var sumLoaderStat = function sumLoaderStat(stat) {
|
22003 | return this.audioSegmentLoader_[stat] + this.mainSegmentLoader_[stat];
|
22004 | };
|
22005 |
|
22006 | var shouldSwitchToMedia = function shouldSwitchToMedia(_ref) {
|
22007 | var currentPlaylist = _ref.currentPlaylist,
|
22008 | nextPlaylist = _ref.nextPlaylist,
|
22009 | forwardBuffer = _ref.forwardBuffer,
|
22010 | bufferLowWaterLine = _ref.bufferLowWaterLine,
|
22011 | bufferHighWaterLine = _ref.bufferHighWaterLine,
|
22012 | duration = _ref.duration,
|
22013 | experimentalBufferBasedABR = _ref.experimentalBufferBasedABR,
|
22014 | log = _ref.log;
|
22015 |
|
22016 |
|
22017 | if (!nextPlaylist) {
|
22018 | videojs.log.warn('We received no playlist to switch to. Please check your stream.');
|
22019 | return false;
|
22020 | }
|
22021 |
|
22022 | var sharedLogLine = "allowing switch " + (currentPlaylist && currentPlaylist.id || 'null') + " -> " + nextPlaylist.id;
|
22023 |
|
22024 | if (!currentPlaylist) {
|
22025 | log(sharedLogLine + " as current playlist is not set");
|
22026 | return true;
|
22027 | }
|
22028 |
|
22029 |
|
22030 | if (nextPlaylist.id === currentPlaylist.id) {
|
22031 | return false;
|
22032 | }
|
22033 |
|
22034 |
|
22035 |
|
22036 |
|
22037 |
|
22038 | if (!currentPlaylist.endList) {
|
22039 | log(sharedLogLine + " as current playlist is live");
|
22040 | return true;
|
22041 | }
|
22042 |
|
22043 | var maxBufferLowWaterLine = experimentalBufferBasedABR ? Config.EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE : Config.MAX_BUFFER_LOW_WATER_LINE;
|
22044 |
|
22045 |
|
22046 | if (duration < maxBufferLowWaterLine) {
|
22047 | log(sharedLogLine + " as duration < max low water line (" + duration + " < " + maxBufferLowWaterLine + ")");
|
22048 | return true;
|
22049 | }
|
22050 |
|
22051 | var nextBandwidth = nextPlaylist.attributes.BANDWIDTH;
|
22052 | var currBandwidth = currentPlaylist.attributes.BANDWIDTH;
|
22053 |
|
22054 |
|
22055 | if (nextBandwidth < currBandwidth && (!experimentalBufferBasedABR || forwardBuffer < bufferHighWaterLine)) {
|
22056 | var logLine = sharedLogLine + " as next bandwidth < current bandwidth (" + nextBandwidth + " < " + currBandwidth + ")";
|
22057 |
|
22058 | if (experimentalBufferBasedABR) {
|
22059 | logLine += " and forwardBuffer < bufferHighWaterLine (" + forwardBuffer + " < " + bufferHighWaterLine + ")";
|
22060 | }
|
22061 |
|
22062 | log(logLine);
|
22063 | return true;
|
22064 | }
|
22065 |
|
22066 |
|
22067 |
|
22068 | if ((!experimentalBufferBasedABR || nextBandwidth > currBandwidth) && forwardBuffer >= bufferLowWaterLine) {
|
22069 | var _logLine = sharedLogLine + " as forwardBuffer >= bufferLowWaterLine (" + forwardBuffer + " >= " + bufferLowWaterLine + ")";
|
22070 |
|
22071 | if (experimentalBufferBasedABR) {
|
22072 | _logLine += " and next bandwidth > current bandwidth (" + nextBandwidth + " > " + currBandwidth + ")";
|
22073 | }
|
22074 |
|
22075 | log(_logLine);
|
22076 | return true;
|
22077 | }
|
22078 |
|
22079 | log("not " + sharedLogLine + " as no switching criteria met");
|
22080 | return false;
|
22081 | };
|
22082 |
|
22083 |
|
22084 |
|
22085 |
|
22086 |
|
22087 |
|
22088 |
|
22089 |
|
22090 |
|
22091 |
|
22092 |
|
22093 | var MasterPlaylistController = function (_videojs$EventTarget) {
|
22094 | _inheritsLoose(MasterPlaylistController, _videojs$EventTarget);
|
22095 |
|
22096 | function MasterPlaylistController(options) {
|
22097 | var _this;
|
22098 |
|
22099 | _this = _videojs$EventTarget.call(this) || this;
|
22100 | var src = options.src,
|
22101 | handleManifestRedirects = options.handleManifestRedirects,
|
22102 | withCredentials = options.withCredentials,
|
22103 | tech = options.tech,
|
22104 | bandwidth = options.bandwidth,
|
22105 | externVhs = options.externVhs,
|
22106 | useCueTags = options.useCueTags,
|
22107 | blacklistDuration = options.blacklistDuration,
|
22108 | enableLowInitialPlaylist = options.enableLowInitialPlaylist,
|
22109 | sourceType = options.sourceType,
|
22110 | cacheEncryptionKeys = options.cacheEncryptionKeys,
|
22111 | experimentalBufferBasedABR = options.experimentalBufferBasedABR,
|
22112 | experimentalLeastPixelDiffSelector = options.experimentalLeastPixelDiffSelector,
|
22113 | captionServices = options.captionServices;
|
22114 |
|
22115 | if (!src) {
|
22116 | throw new Error('A non-empty playlist URL or JSON manifest string is required');
|
22117 | }
|
22118 |
|
22119 | var maxPlaylistRetries = options.maxPlaylistRetries;
|
22120 |
|
22121 | if (maxPlaylistRetries === null || typeof maxPlaylistRetries === 'undefined') {
|
22122 | maxPlaylistRetries = Infinity;
|
22123 | }
|
22124 |
|
22125 | Vhs$1 = externVhs;
|
22126 | _this.experimentalBufferBasedABR = Boolean(experimentalBufferBasedABR);
|
22127 | _this.experimentalLeastPixelDiffSelector = Boolean(experimentalLeastPixelDiffSelector);
|
22128 | _this.withCredentials = withCredentials;
|
22129 | _this.tech_ = tech;
|
22130 | _this.vhs_ = tech.vhs;
|
22131 | _this.sourceType_ = sourceType;
|
22132 | _this.useCueTags_ = useCueTags;
|
22133 | _this.blacklistDuration = blacklistDuration;
|
22134 | _this.maxPlaylistRetries = maxPlaylistRetries;
|
22135 | _this.enableLowInitialPlaylist = enableLowInitialPlaylist;
|
22136 |
|
22137 | if (_this.useCueTags_) {
|
22138 | _this.cueTagsTrack_ = _this.tech_.addTextTrack('metadata', 'ad-cues');
|
22139 | _this.cueTagsTrack_.inBandMetadataTrackDispatchType = '';
|
22140 | }
|
22141 |
|
22142 | _this.requestOptions_ = {
|
22143 | withCredentials: withCredentials,
|
22144 | handleManifestRedirects: handleManifestRedirects,
|
22145 | maxPlaylistRetries: maxPlaylistRetries,
|
22146 | timeout: null
|
22147 | };
|
22148 |
|
22149 | _this.on('error', _this.pauseLoading);
|
22150 |
|
22151 | _this.mediaTypes_ = createMediaTypes();
|
22152 | _this.mediaSource = new window.MediaSource();
|
22153 | _this.handleDurationChange_ = _this.handleDurationChange_.bind(_assertThisInitialized(_this));
|
22154 | _this.handleSourceOpen_ = _this.handleSourceOpen_.bind(_assertThisInitialized(_this));
|
22155 | _this.handleSourceEnded_ = _this.handleSourceEnded_.bind(_assertThisInitialized(_this));
|
22156 |
|
22157 | _this.mediaSource.addEventListener('durationchange', _this.handleDurationChange_);
|
22158 |
|
22159 |
|
22160 | _this.mediaSource.addEventListener('sourceopen', _this.handleSourceOpen_);
|
22161 |
|
22162 | _this.mediaSource.addEventListener('sourceended', _this.handleSourceEnded_);
|
22163 |
|
22164 |
|
22165 |
|
22166 | _this.seekable_ = videojs.createTimeRanges();
|
22167 | _this.hasPlayed_ = false;
|
22168 | _this.syncController_ = new SyncController(options);
|
22169 | _this.segmentMetadataTrack_ = tech.addRemoteTextTrack({
|
22170 | kind: 'metadata',
|
22171 | label: 'segment-metadata'
|
22172 | }, false).track;
|
22173 | _this.decrypter_ = new Decrypter();
|
22174 | _this.sourceUpdater_ = new SourceUpdater(_this.mediaSource);
|
22175 | _this.inbandTextTracks_ = {};
|
22176 | _this.timelineChangeController_ = new TimelineChangeController();
|
22177 | var segmentLoaderSettings = {
|
22178 | vhs: _this.vhs_,
|
22179 | parse708captions: options.parse708captions,
|
22180 | captionServices: captionServices,
|
22181 | mediaSource: _this.mediaSource,
|
22182 | currentTime: _this.tech_.currentTime.bind(_this.tech_),
|
22183 | seekable: function seekable() {
|
22184 | return _this.seekable();
|
22185 | },
|
22186 | seeking: function seeking() {
|
22187 | return _this.tech_.seeking();
|
22188 | },
|
22189 | duration: function duration() {
|
22190 | return _this.duration();
|
22191 | },
|
22192 | hasPlayed: function hasPlayed() {
|
22193 | return _this.hasPlayed_;
|
22194 | },
|
22195 | goalBufferLength: function goalBufferLength() {
|
22196 | return _this.goalBufferLength();
|
22197 | },
|
22198 | bandwidth: bandwidth,
|
22199 | syncController: _this.syncController_,
|
22200 | decrypter: _this.decrypter_,
|
22201 | sourceType: _this.sourceType_,
|
22202 | inbandTextTracks: _this.inbandTextTracks_,
|
22203 | cacheEncryptionKeys: cacheEncryptionKeys,
|
22204 | sourceUpdater: _this.sourceUpdater_,
|
22205 | timelineChangeController: _this.timelineChangeController_,
|
22206 | experimentalExactManifestTimings: options.experimentalExactManifestTimings
|
22207 | };
|
22208 |
|
22209 |
|
22210 |
|
22211 |
|
22212 | _this.masterPlaylistLoader_ = _this.sourceType_ === 'dash' ? new DashPlaylistLoader(src, _this.vhs_, _this.requestOptions_) : new PlaylistLoader(src, _this.vhs_, _this.requestOptions_);
|
22213 |
|
22214 | _this.setupMasterPlaylistLoaderListeners_();
|
22215 |
|
22216 |
|
22217 |
|
22218 | _this.mainSegmentLoader_ = new SegmentLoader(videojs.mergeOptions(segmentLoaderSettings, {
|
22219 | segmentMetadataTrack: _this.segmentMetadataTrack_,
|
22220 | loaderType: 'main'
|
22221 | }), options);
|
22222 |
|
22223 | _this.audioSegmentLoader_ = new SegmentLoader(videojs.mergeOptions(segmentLoaderSettings, {
|
22224 | loaderType: 'audio'
|
22225 | }), options);
|
22226 | _this.subtitleSegmentLoader_ = new VTTSegmentLoader(videojs.mergeOptions(segmentLoaderSettings, {
|
22227 | loaderType: 'vtt',
|
22228 | featuresNativeTextTracks: _this.tech_.featuresNativeTextTracks
|
22229 | }), options);
|
22230 |
|
22231 | _this.setupSegmentLoaderListeners_();
|
22232 |
|
22233 | if (_this.experimentalBufferBasedABR) {
|
22234 | _this.masterPlaylistLoader_.one('loadedplaylist', function () {
|
22235 | return _this.startABRTimer_();
|
22236 | });
|
22237 |
|
22238 | _this.tech_.on('pause', function () {
|
22239 | return _this.stopABRTimer_();
|
22240 | });
|
22241 |
|
22242 | _this.tech_.on('play', function () {
|
22243 | return _this.startABRTimer_();
|
22244 | });
|
22245 | }
|
22246 |
|
22247 |
|
22248 |
|
22249 |
|
22250 |
|
22251 |
|
22252 |
|
22253 |
|
22254 |
|
22255 | loaderStats.forEach(function (stat) {
|
22256 | _this[stat + '_'] = sumLoaderStat.bind(_assertThisInitialized(_this), stat);
|
22257 | });
|
22258 | _this.logger_ = logger('MPC');
|
22259 | _this.triggeredFmp4Usage = false;
|
22260 |
|
22261 | if (_this.tech_.preload() === 'none') {
|
22262 | _this.loadOnPlay_ = function () {
|
22263 | _this.loadOnPlay_ = null;
|
22264 |
|
22265 | _this.masterPlaylistLoader_.load();
|
22266 | };
|
22267 |
|
22268 | _this.tech_.one('play', _this.loadOnPlay_);
|
22269 | } else {
|
22270 | _this.masterPlaylistLoader_.load();
|
22271 | }
|
22272 |
|
22273 | _this.timeToLoadedData__ = -1;
|
22274 | _this.mainAppendsToLoadedData__ = -1;
|
22275 | _this.audioAppendsToLoadedData__ = -1;
|
22276 | var event = _this.tech_.preload() === 'none' ? 'play' : 'loadstart';
|
22277 |
|
22278 | _this.tech_.one(event, function () {
|
22279 | var timeToLoadedDataStart = Date.now();
|
22280 |
|
22281 | _this.tech_.one('loadeddata', function () {
|
22282 | _this.timeToLoadedData__ = Date.now() - timeToLoadedDataStart;
|
22283 | _this.mainAppendsToLoadedData__ = _this.mainSegmentLoader_.mediaAppends;
|
22284 | _this.audioAppendsToLoadedData__ = _this.audioSegmentLoader_.mediaAppends;
|
22285 | });
|
22286 | });
|
22287 |
|
22288 | return _this;
|
22289 | }
|
22290 |
|
22291 | var _proto = MasterPlaylistController.prototype;
|
22292 |
|
22293 | _proto.mainAppendsToLoadedData_ = function mainAppendsToLoadedData_() {
|
22294 | return this.mainAppendsToLoadedData__;
|
22295 | };
|
22296 |
|
22297 | _proto.audioAppendsToLoadedData_ = function audioAppendsToLoadedData_() {
|
22298 | return this.audioAppendsToLoadedData__;
|
22299 | };
|
22300 |
|
22301 | _proto.appendsToLoadedData_ = function appendsToLoadedData_() {
|
22302 | var main = this.mainAppendsToLoadedData_();
|
22303 | var audio = this.audioAppendsToLoadedData_();
|
22304 |
|
22305 | if (main === -1 || audio === -1) {
|
22306 | return -1;
|
22307 | }
|
22308 |
|
22309 | return main + audio;
|
22310 | };
|
22311 |
|
22312 | _proto.timeToLoadedData_ = function timeToLoadedData_() {
|
22313 | return this.timeToLoadedData__;
|
22314 | }
|
22315 | |
22316 |
|
22317 |
|
22318 |
|
22319 |
|
22320 |
|
22321 | ;
|
22322 |
|
22323 | _proto.checkABR_ = function checkABR_() {
|
22324 | var nextPlaylist = this.selectPlaylist();
|
22325 |
|
22326 | if (nextPlaylist && this.shouldSwitchToMedia_(nextPlaylist)) {
|
22327 | this.switchMedia_(nextPlaylist, 'abr');
|
22328 | }
|
22329 | };
|
22330 |
|
22331 | _proto.switchMedia_ = function switchMedia_(playlist, cause, delay) {
|
22332 | var oldMedia = this.media();
|
22333 | var oldId = oldMedia && (oldMedia.id || oldMedia.uri);
|
22334 | var newId = playlist.id || playlist.uri;
|
22335 |
|
22336 | if (oldId && oldId !== newId) {
|
22337 | this.logger_("switch media " + oldId + " -> " + newId + " from " + cause);
|
22338 | this.tech_.trigger({
|
22339 | type: 'usage',
|
22340 | name: "vhs-rendition-change-" + cause
|
22341 | });
|
22342 | }
|
22343 |
|
22344 | this.masterPlaylistLoader_.media(playlist, delay);
|
22345 | }
|
22346 | |
22347 |
|
22348 |
|
22349 |
|
22350 |
|
22351 | ;
|
22352 |
|
22353 | _proto.startABRTimer_ = function startABRTimer_() {
|
22354 | var _this2 = this;
|
22355 |
|
22356 | this.stopABRTimer_();
|
22357 | this.abrTimer_ = window.setInterval(function () {
|
22358 | return _this2.checkABR_();
|
22359 | }, 250);
|
22360 | }
|
22361 | |
22362 |
|
22363 |
|
22364 |
|
22365 |
|
22366 | ;
|
22367 |
|
22368 | _proto.stopABRTimer_ = function stopABRTimer_() {
|
22369 |
|
22370 |
|
22371 | if (this.tech_.scrubbing && this.tech_.scrubbing()) {
|
22372 | return;
|
22373 | }
|
22374 |
|
22375 | window.clearInterval(this.abrTimer_);
|
22376 | this.abrTimer_ = null;
|
22377 | }
|
22378 | |
22379 |
|
22380 |
|
22381 |
|
22382 |
|
22383 | ;
|
22384 |
|
22385 | _proto.getAudioTrackPlaylists_ = function getAudioTrackPlaylists_() {
|
22386 | var master = this.master();
|
22387 | var defaultPlaylists = master && master.playlists || [];
|
22388 |
|
22389 |
|
22390 |
|
22391 | if (!master || !master.mediaGroups || !master.mediaGroups.AUDIO) {
|
22392 | return defaultPlaylists;
|
22393 | }
|
22394 |
|
22395 | var AUDIO = master.mediaGroups.AUDIO;
|
22396 | var groupKeys = Object.keys(AUDIO);
|
22397 | var track;
|
22398 |
|
22399 | if (Object.keys(this.mediaTypes_.AUDIO.groups).length) {
|
22400 | track = this.mediaTypes_.AUDIO.activeTrack();
|
22401 | } else {
|
22402 |
|
22403 | var defaultGroup = AUDIO.main || groupKeys.length && AUDIO[groupKeys[0]];
|
22404 |
|
22405 | for (var label in defaultGroup) {
|
22406 | if (defaultGroup[label].default) {
|
22407 | track = {
|
22408 | label: label
|
22409 | };
|
22410 | break;
|
22411 | }
|
22412 | }
|
22413 | }
|
22414 |
|
22415 |
|
22416 | if (!track) {
|
22417 | return defaultPlaylists;
|
22418 | }
|
22419 |
|
22420 | var playlists = [];
|
22421 |
|
22422 |
|
22423 | for (var group in AUDIO) {
|
22424 | if (AUDIO[group][track.label]) {
|
22425 | var properties = AUDIO[group][track.label];
|
22426 |
|
22427 | if (properties.playlists && properties.playlists.length) {
|
22428 | playlists.push.apply(playlists, properties.playlists);
|
22429 | } else if (properties.uri) {
|
22430 | playlists.push(properties);
|
22431 | } else if (master.playlists.length) {
|
22432 |
|
22433 |
|
22434 |
|
22435 | for (var i = 0; i < master.playlists.length; i++) {
|
22436 | var playlist = master.playlists[i];
|
22437 |
|
22438 | if (playlist.attributes && playlist.attributes.AUDIO && playlist.attributes.AUDIO === group) {
|
22439 | playlists.push(playlist);
|
22440 | }
|
22441 | }
|
22442 | }
|
22443 | }
|
22444 | }
|
22445 |
|
22446 | if (!playlists.length) {
|
22447 | return defaultPlaylists;
|
22448 | }
|
22449 |
|
22450 | return playlists;
|
22451 | }
|
22452 | |
22453 |
|
22454 |
|
22455 |
|
22456 |
|
22457 |
|
22458 | ;
|
22459 |
|
22460 | _proto.setupMasterPlaylistLoaderListeners_ = function setupMasterPlaylistLoaderListeners_() {
|
22461 | var _this3 = this;
|
22462 |
|
22463 | this.masterPlaylistLoader_.on('loadedmetadata', function () {
|
22464 | var media = _this3.masterPlaylistLoader_.media();
|
22465 |
|
22466 | var requestTimeout = media.targetDuration * 1.5 * 1000;
|
22467 |
|
22468 |
|
22469 | if (isLowestEnabledRendition(_this3.masterPlaylistLoader_.master, _this3.masterPlaylistLoader_.media())) {
|
22470 | _this3.requestOptions_.timeout = 0;
|
22471 | } else {
|
22472 | _this3.requestOptions_.timeout = requestTimeout;
|
22473 | }
|
22474 |
|
22475 |
|
22476 |
|
22477 | if (media.endList && _this3.tech_.preload() !== 'none') {
|
22478 | _this3.mainSegmentLoader_.playlist(media, _this3.requestOptions_);
|
22479 |
|
22480 | _this3.mainSegmentLoader_.load();
|
22481 | }
|
22482 |
|
22483 | setupMediaGroups({
|
22484 | sourceType: _this3.sourceType_,
|
22485 | segmentLoaders: {
|
22486 | AUDIO: _this3.audioSegmentLoader_,
|
22487 | SUBTITLES: _this3.subtitleSegmentLoader_,
|
22488 | main: _this3.mainSegmentLoader_
|
22489 | },
|
22490 | tech: _this3.tech_,
|
22491 | requestOptions: _this3.requestOptions_,
|
22492 | masterPlaylistLoader: _this3.masterPlaylistLoader_,
|
22493 | vhs: _this3.vhs_,
|
22494 | master: _this3.master(),
|
22495 | mediaTypes: _this3.mediaTypes_,
|
22496 | blacklistCurrentPlaylist: _this3.blacklistCurrentPlaylist.bind(_this3)
|
22497 | });
|
22498 |
|
22499 | _this3.triggerPresenceUsage_(_this3.master(), media);
|
22500 |
|
22501 | _this3.setupFirstPlay();
|
22502 |
|
22503 | if (!_this3.mediaTypes_.AUDIO.activePlaylistLoader || _this3.mediaTypes_.AUDIO.activePlaylistLoader.media()) {
|
22504 | _this3.trigger('selectedinitialmedia');
|
22505 | } else {
|
22506 |
|
22507 |
|
22508 |
|
22509 | _this3.mediaTypes_.AUDIO.activePlaylistLoader.one('loadedmetadata', function () {
|
22510 | _this3.trigger('selectedinitialmedia');
|
22511 | });
|
22512 | }
|
22513 | });
|
22514 | this.masterPlaylistLoader_.on('loadedplaylist', function () {
|
22515 | if (_this3.loadOnPlay_) {
|
22516 | _this3.tech_.off('play', _this3.loadOnPlay_);
|
22517 | }
|
22518 |
|
22519 | var updatedPlaylist = _this3.masterPlaylistLoader_.media();
|
22520 |
|
22521 | if (!updatedPlaylist) {
|
22522 |
|
22523 |
|
22524 | _this3.excludeUnsupportedVariants_();
|
22525 |
|
22526 | var selectedMedia;
|
22527 |
|
22528 | if (_this3.enableLowInitialPlaylist) {
|
22529 | selectedMedia = _this3.selectInitialPlaylist();
|
22530 | }
|
22531 |
|
22532 | if (!selectedMedia) {
|
22533 | selectedMedia = _this3.selectPlaylist();
|
22534 | }
|
22535 |
|
22536 | if (!selectedMedia || !_this3.shouldSwitchToMedia_(selectedMedia)) {
|
22537 | return;
|
22538 | }
|
22539 |
|
22540 | _this3.initialMedia_ = selectedMedia;
|
22541 |
|
22542 | _this3.switchMedia_(_this3.initialMedia_, 'initial');
|
22543 |
|
22544 |
|
22545 |
|
22546 |
|
22547 |
|
22548 |
|
22549 |
|
22550 | var haveJsonSource = _this3.sourceType_ === 'vhs-json' && _this3.initialMedia_.segments;
|
22551 |
|
22552 | if (!haveJsonSource) {
|
22553 | return;
|
22554 | }
|
22555 |
|
22556 | updatedPlaylist = _this3.initialMedia_;
|
22557 | }
|
22558 |
|
22559 | _this3.handleUpdatedMediaPlaylist(updatedPlaylist);
|
22560 | });
|
22561 | this.masterPlaylistLoader_.on('error', function () {
|
22562 | _this3.blacklistCurrentPlaylist(_this3.masterPlaylistLoader_.error);
|
22563 | });
|
22564 | this.masterPlaylistLoader_.on('mediachanging', function () {
|
22565 | _this3.mainSegmentLoader_.abort();
|
22566 |
|
22567 | _this3.mainSegmentLoader_.pause();
|
22568 | });
|
22569 | this.masterPlaylistLoader_.on('mediachange', function () {
|
22570 | var media = _this3.masterPlaylistLoader_.media();
|
22571 |
|
22572 | var requestTimeout = media.targetDuration * 1.5 * 1000;
|
22573 |
|
22574 |
|
22575 | if (isLowestEnabledRendition(_this3.masterPlaylistLoader_.master, _this3.masterPlaylistLoader_.media())) {
|
22576 | _this3.requestOptions_.timeout = 0;
|
22577 | } else {
|
22578 | _this3.requestOptions_.timeout = requestTimeout;
|
22579 | }
|
22580 |
|
22581 |
|
22582 |
|
22583 |
|
22584 |
|
22585 | _this3.mainSegmentLoader_.playlist(media, _this3.requestOptions_);
|
22586 |
|
22587 | _this3.mainSegmentLoader_.load();
|
22588 |
|
22589 | _this3.tech_.trigger({
|
22590 | type: 'mediachange',
|
22591 | bubbles: true
|
22592 | });
|
22593 | });
|
22594 | this.masterPlaylistLoader_.on('playlistunchanged', function () {
|
22595 | var updatedPlaylist = _this3.masterPlaylistLoader_.media();
|
22596 |
|
22597 |
|
22598 |
|
22599 |
|
22600 | if (updatedPlaylist.lastExcludeReason_ === 'playlist-unchanged') {
|
22601 | return;
|
22602 | }
|
22603 |
|
22604 | var playlistOutdated = _this3.stuckAtPlaylistEnd_(updatedPlaylist);
|
22605 |
|
22606 | if (playlistOutdated) {
|
22607 |
|
22608 |
|
22609 |
|
22610 |
|
22611 | _this3.blacklistCurrentPlaylist({
|
22612 | message: 'Playlist no longer updating.',
|
22613 | reason: 'playlist-unchanged'
|
22614 | });
|
22615 |
|
22616 |
|
22617 | _this3.tech_.trigger('playliststuck');
|
22618 | }
|
22619 | });
|
22620 | this.masterPlaylistLoader_.on('renditiondisabled', function () {
|
22621 | _this3.tech_.trigger({
|
22622 | type: 'usage',
|
22623 | name: 'vhs-rendition-disabled'
|
22624 | });
|
22625 |
|
22626 | _this3.tech_.trigger({
|
22627 | type: 'usage',
|
22628 | name: 'hls-rendition-disabled'
|
22629 | });
|
22630 | });
|
22631 | this.masterPlaylistLoader_.on('renditionenabled', function () {
|
22632 | _this3.tech_.trigger({
|
22633 | type: 'usage',
|
22634 | name: 'vhs-rendition-enabled'
|
22635 | });
|
22636 |
|
22637 | _this3.tech_.trigger({
|
22638 | type: 'usage',
|
22639 | name: 'hls-rendition-enabled'
|
22640 | });
|
22641 | });
|
22642 | }
|
22643 | |
22644 |
|
22645 |
|
22646 |
|
22647 |
|
22648 |
|
22649 |
|
22650 |
|
22651 |
|
22652 | ;
|
22653 |
|
22654 | _proto.handleUpdatedMediaPlaylist = function handleUpdatedMediaPlaylist(updatedPlaylist) {
|
22655 | if (this.useCueTags_) {
|
22656 | this.updateAdCues_(updatedPlaylist);
|
22657 | }
|
22658 |
|
22659 |
|
22660 |
|
22661 |
|
22662 |
|
22663 | this.mainSegmentLoader_.playlist(updatedPlaylist, this.requestOptions_);
|
22664 | this.updateDuration(!updatedPlaylist.endList);
|
22665 |
|
22666 |
|
22667 |
|
22668 | if (!this.tech_.paused()) {
|
22669 | this.mainSegmentLoader_.load();
|
22670 |
|
22671 | if (this.audioSegmentLoader_) {
|
22672 | this.audioSegmentLoader_.load();
|
22673 | }
|
22674 | }
|
22675 | }
|
22676 | |
22677 |
|
22678 |
|
22679 |
|
22680 |
|
22681 | ;
|
22682 |
|
22683 | _proto.triggerPresenceUsage_ = function triggerPresenceUsage_(master, media) {
|
22684 | var mediaGroups = master.mediaGroups || {};
|
22685 | var defaultDemuxed = true;
|
22686 | var audioGroupKeys = Object.keys(mediaGroups.AUDIO);
|
22687 |
|
22688 | for (var mediaGroup in mediaGroups.AUDIO) {
|
22689 | for (var label in mediaGroups.AUDIO[mediaGroup]) {
|
22690 | var properties = mediaGroups.AUDIO[mediaGroup][label];
|
22691 |
|
22692 | if (!properties.uri) {
|
22693 | defaultDemuxed = false;
|
22694 | }
|
22695 | }
|
22696 | }
|
22697 |
|
22698 | if (defaultDemuxed) {
|
22699 | this.tech_.trigger({
|
22700 | type: 'usage',
|
22701 | name: 'vhs-demuxed'
|
22702 | });
|
22703 | this.tech_.trigger({
|
22704 | type: 'usage',
|
22705 | name: 'hls-demuxed'
|
22706 | });
|
22707 | }
|
22708 |
|
22709 | if (Object.keys(mediaGroups.SUBTITLES).length) {
|
22710 | this.tech_.trigger({
|
22711 | type: 'usage',
|
22712 | name: 'vhs-webvtt'
|
22713 | });
|
22714 | this.tech_.trigger({
|
22715 | type: 'usage',
|
22716 | name: 'hls-webvtt'
|
22717 | });
|
22718 | }
|
22719 |
|
22720 | if (Vhs$1.Playlist.isAes(media)) {
|
22721 | this.tech_.trigger({
|
22722 | type: 'usage',
|
22723 | name: 'vhs-aes'
|
22724 | });
|
22725 | this.tech_.trigger({
|
22726 | type: 'usage',
|
22727 | name: 'hls-aes'
|
22728 | });
|
22729 | }
|
22730 |
|
22731 | if (audioGroupKeys.length && Object.keys(mediaGroups.AUDIO[audioGroupKeys[0]]).length > 1) {
|
22732 | this.tech_.trigger({
|
22733 | type: 'usage',
|
22734 | name: 'vhs-alternate-audio'
|
22735 | });
|
22736 | this.tech_.trigger({
|
22737 | type: 'usage',
|
22738 | name: 'hls-alternate-audio'
|
22739 | });
|
22740 | }
|
22741 |
|
22742 | if (this.useCueTags_) {
|
22743 | this.tech_.trigger({
|
22744 | type: 'usage',
|
22745 | name: 'vhs-playlist-cue-tags'
|
22746 | });
|
22747 | this.tech_.trigger({
|
22748 | type: 'usage',
|
22749 | name: 'hls-playlist-cue-tags'
|
22750 | });
|
22751 | }
|
22752 | };
|
22753 |
|
22754 | _proto.shouldSwitchToMedia_ = function shouldSwitchToMedia_(nextPlaylist) {
|
22755 | var currentPlaylist = this.masterPlaylistLoader_.media();
|
22756 | var buffered = this.tech_.buffered();
|
22757 | var forwardBuffer = buffered.length ? buffered.end(buffered.length - 1) - this.tech_.currentTime() : 0;
|
22758 | var bufferLowWaterLine = this.bufferLowWaterLine();
|
22759 | var bufferHighWaterLine = this.bufferHighWaterLine();
|
22760 | return shouldSwitchToMedia({
|
22761 | currentPlaylist: currentPlaylist,
|
22762 | nextPlaylist: nextPlaylist,
|
22763 | forwardBuffer: forwardBuffer,
|
22764 | bufferLowWaterLine: bufferLowWaterLine,
|
22765 | bufferHighWaterLine: bufferHighWaterLine,
|
22766 | duration: this.duration(),
|
22767 | experimentalBufferBasedABR: this.experimentalBufferBasedABR,
|
22768 | log: this.logger_
|
22769 | });
|
22770 | }
|
22771 | |
22772 |
|
22773 |
|
22774 |
|
22775 |
|
22776 |
|
22777 | ;
|
22778 |
|
22779 | _proto.setupSegmentLoaderListeners_ = function setupSegmentLoaderListeners_() {
|
22780 | var _this4 = this;
|
22781 |
|
22782 | if (!this.experimentalBufferBasedABR) {
|
22783 | this.mainSegmentLoader_.on('bandwidthupdate', function () {
|
22784 | var nextPlaylist = _this4.selectPlaylist();
|
22785 |
|
22786 | if (_this4.shouldSwitchToMedia_(nextPlaylist)) {
|
22787 | _this4.switchMedia_(nextPlaylist, 'bandwidthupdate');
|
22788 | }
|
22789 |
|
22790 | _this4.tech_.trigger('bandwidthupdate');
|
22791 | });
|
22792 | this.mainSegmentLoader_.on('progress', function () {
|
22793 | _this4.trigger('progress');
|
22794 | });
|
22795 | }
|
22796 |
|
22797 | this.mainSegmentLoader_.on('error', function () {
|
22798 | _this4.blacklistCurrentPlaylist(_this4.mainSegmentLoader_.error());
|
22799 | });
|
22800 | this.mainSegmentLoader_.on('appenderror', function () {
|
22801 | _this4.error = _this4.mainSegmentLoader_.error_;
|
22802 |
|
22803 | _this4.trigger('error');
|
22804 | });
|
22805 | this.mainSegmentLoader_.on('syncinfoupdate', function () {
|
22806 | _this4.onSyncInfoUpdate_();
|
22807 | });
|
22808 | this.mainSegmentLoader_.on('timestampoffset', function () {
|
22809 | _this4.tech_.trigger({
|
22810 | type: 'usage',
|
22811 | name: 'vhs-timestamp-offset'
|
22812 | });
|
22813 |
|
22814 | _this4.tech_.trigger({
|
22815 | type: 'usage',
|
22816 | name: 'hls-timestamp-offset'
|
22817 | });
|
22818 | });
|
22819 | this.audioSegmentLoader_.on('syncinfoupdate', function () {
|
22820 | _this4.onSyncInfoUpdate_();
|
22821 | });
|
22822 | this.audioSegmentLoader_.on('appenderror', function () {
|
22823 | _this4.error = _this4.audioSegmentLoader_.error_;
|
22824 |
|
22825 | _this4.trigger('error');
|
22826 | });
|
22827 | this.mainSegmentLoader_.on('ended', function () {
|
22828 | _this4.logger_('main segment loader ended');
|
22829 |
|
22830 | _this4.onEndOfStream();
|
22831 | });
|
22832 | this.mainSegmentLoader_.on('earlyabort', function (event) {
|
22833 |
|
22834 | if (_this4.experimentalBufferBasedABR) {
|
22835 | return;
|
22836 | }
|
22837 |
|
22838 | _this4.delegateLoaders_('all', ['abort']);
|
22839 |
|
22840 | _this4.blacklistCurrentPlaylist({
|
22841 | message: 'Aborted early because there isn\'t enough bandwidth to complete the ' + 'request without rebuffering.'
|
22842 | }, ABORT_EARLY_BLACKLIST_SECONDS);
|
22843 | });
|
22844 |
|
22845 | var updateCodecs = function updateCodecs() {
|
22846 | if (!_this4.sourceUpdater_.hasCreatedSourceBuffers()) {
|
22847 | return _this4.tryToCreateSourceBuffers_();
|
22848 | }
|
22849 |
|
22850 | var codecs = _this4.getCodecsOrExclude_();
|
22851 |
|
22852 |
|
22853 | if (!codecs) {
|
22854 | return;
|
22855 | }
|
22856 |
|
22857 | _this4.sourceUpdater_.addOrChangeSourceBuffers(codecs);
|
22858 | };
|
22859 |
|
22860 | this.mainSegmentLoader_.on('trackinfo', updateCodecs);
|
22861 | this.audioSegmentLoader_.on('trackinfo', updateCodecs);
|
22862 | this.mainSegmentLoader_.on('fmp4', function () {
|
22863 | if (!_this4.triggeredFmp4Usage) {
|
22864 | _this4.tech_.trigger({
|
22865 | type: 'usage',
|
22866 | name: 'vhs-fmp4'
|
22867 | });
|
22868 |
|
22869 | _this4.tech_.trigger({
|
22870 | type: 'usage',
|
22871 | name: 'hls-fmp4'
|
22872 | });
|
22873 |
|
22874 | _this4.triggeredFmp4Usage = true;
|
22875 | }
|
22876 | });
|
22877 | this.audioSegmentLoader_.on('fmp4', function () {
|
22878 | if (!_this4.triggeredFmp4Usage) {
|
22879 | _this4.tech_.trigger({
|
22880 | type: 'usage',
|
22881 | name: 'vhs-fmp4'
|
22882 | });
|
22883 |
|
22884 | _this4.tech_.trigger({
|
22885 | type: 'usage',
|
22886 | name: 'hls-fmp4'
|
22887 | });
|
22888 |
|
22889 | _this4.triggeredFmp4Usage = true;
|
22890 | }
|
22891 | });
|
22892 | this.audioSegmentLoader_.on('ended', function () {
|
22893 | _this4.logger_('audioSegmentLoader ended');
|
22894 |
|
22895 | _this4.onEndOfStream();
|
22896 | });
|
22897 | };
|
22898 |
|
22899 | _proto.mediaSecondsLoaded_ = function mediaSecondsLoaded_() {
|
22900 | return Math.max(this.audioSegmentLoader_.mediaSecondsLoaded + this.mainSegmentLoader_.mediaSecondsLoaded);
|
22901 | }
|
22902 | |
22903 |
|
22904 |
|
22905 | ;
|
22906 |
|
22907 | _proto.load = function load() {
|
22908 | this.mainSegmentLoader_.load();
|
22909 |
|
22910 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
22911 | this.audioSegmentLoader_.load();
|
22912 | }
|
22913 |
|
22914 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
22915 | this.subtitleSegmentLoader_.load();
|
22916 | }
|
22917 | }
|
22918 | |
22919 |
|
22920 |
|
22921 |
|
22922 |
|
22923 |
|
22924 |
|
22925 |
|
22926 | ;
|
22927 |
|
22928 | _proto.smoothQualityChange_ = function smoothQualityChange_(media) {
|
22929 | if (media === void 0) {
|
22930 | media = this.selectPlaylist();
|
22931 | }
|
22932 |
|
22933 | this.fastQualityChange_(media);
|
22934 | }
|
22935 | |
22936 |
|
22937 |
|
22938 |
|
22939 |
|
22940 |
|
22941 |
|
22942 |
|
22943 | ;
|
22944 |
|
22945 | _proto.fastQualityChange_ = function fastQualityChange_(media) {
|
22946 | var _this5 = this;
|
22947 |
|
22948 | if (media === void 0) {
|
22949 | media = this.selectPlaylist();
|
22950 | }
|
22951 |
|
22952 | if (media === this.masterPlaylistLoader_.media()) {
|
22953 | this.logger_('skipping fastQualityChange because new media is same as old');
|
22954 | return;
|
22955 | }
|
22956 |
|
22957 | this.switchMedia_(media, 'fast-quality');
|
22958 |
|
22959 |
|
22960 |
|
22961 |
|
22962 |
|
22963 |
|
22964 | this.mainSegmentLoader_.resetEverything(function () {
|
22965 |
|
22966 |
|
22967 | if (videojs.browser.IE_VERSION || videojs.browser.IS_EDGE) {
|
22968 | _this5.tech_.setCurrentTime(_this5.tech_.currentTime() + 0.04);
|
22969 | } else {
|
22970 | _this5.tech_.setCurrentTime(_this5.tech_.currentTime());
|
22971 | }
|
22972 | });
|
22973 | }
|
22974 | |
22975 |
|
22976 |
|
22977 | ;
|
22978 |
|
22979 | _proto.play = function play() {
|
22980 | if (this.setupFirstPlay()) {
|
22981 | return;
|
22982 | }
|
22983 |
|
22984 | if (this.tech_.ended()) {
|
22985 | this.tech_.setCurrentTime(0);
|
22986 | }
|
22987 |
|
22988 | if (this.hasPlayed_) {
|
22989 | this.load();
|
22990 | }
|
22991 |
|
22992 | var seekable = this.tech_.seekable();
|
22993 |
|
22994 |
|
22995 | if (this.tech_.duration() === Infinity) {
|
22996 | if (this.tech_.currentTime() < seekable.start(0)) {
|
22997 | return this.tech_.setCurrentTime(seekable.end(seekable.length - 1));
|
22998 | }
|
22999 | }
|
23000 | }
|
23001 | |
23002 |
|
23003 |
|
23004 |
|
23005 | ;
|
23006 |
|
23007 | _proto.setupFirstPlay = function setupFirstPlay() {
|
23008 | var _this6 = this;
|
23009 |
|
23010 | var media = this.masterPlaylistLoader_.media();
|
23011 |
|
23012 |
|
23013 |
|
23014 |
|
23015 |
|
23016 | if (!media || this.tech_.paused() || this.hasPlayed_) {
|
23017 | return false;
|
23018 | }
|
23019 |
|
23020 |
|
23021 | if (!media.endList) {
|
23022 | var seekable = this.seekable();
|
23023 |
|
23024 | if (!seekable.length) {
|
23025 |
|
23026 |
|
23027 | return false;
|
23028 | }
|
23029 |
|
23030 | if (videojs.browser.IE_VERSION && this.tech_.readyState() === 0) {
|
23031 |
|
23032 |
|
23033 | this.tech_.one('loadedmetadata', function () {
|
23034 | _this6.trigger('firstplay');
|
23035 |
|
23036 | _this6.tech_.setCurrentTime(seekable.end(0));
|
23037 |
|
23038 | _this6.hasPlayed_ = true;
|
23039 | });
|
23040 | return false;
|
23041 | }
|
23042 |
|
23043 |
|
23044 | this.trigger('firstplay');
|
23045 |
|
23046 | this.tech_.setCurrentTime(seekable.end(0));
|
23047 | }
|
23048 |
|
23049 | this.hasPlayed_ = true;
|
23050 |
|
23051 | this.load();
|
23052 | return true;
|
23053 | }
|
23054 | |
23055 |
|
23056 |
|
23057 |
|
23058 |
|
23059 | ;
|
23060 |
|
23061 | _proto.handleSourceOpen_ = function handleSourceOpen_() {
|
23062 |
|
23063 |
|
23064 |
|
23065 | this.tryToCreateSourceBuffers_();
|
23066 |
|
23067 |
|
23068 |
|
23069 | if (this.tech_.autoplay()) {
|
23070 | var playPromise = this.tech_.play();
|
23071 |
|
23072 |
|
23073 | if (typeof playPromise !== 'undefined' && typeof playPromise.then === 'function') {
|
23074 | playPromise.then(null, function (e) {});
|
23075 | }
|
23076 | }
|
23077 |
|
23078 | this.trigger('sourceopen');
|
23079 | }
|
23080 | |
23081 |
|
23082 |
|
23083 |
|
23084 |
|
23085 | ;
|
23086 |
|
23087 | _proto.handleSourceEnded_ = function handleSourceEnded_() {
|
23088 | if (!this.inbandTextTracks_.metadataTrack_) {
|
23089 | return;
|
23090 | }
|
23091 |
|
23092 | var cues = this.inbandTextTracks_.metadataTrack_.cues;
|
23093 |
|
23094 | if (!cues || !cues.length) {
|
23095 | return;
|
23096 | }
|
23097 |
|
23098 | var duration = this.duration();
|
23099 | cues[cues.length - 1].endTime = isNaN(duration) || Math.abs(duration) === Infinity ? Number.MAX_VALUE : duration;
|
23100 | }
|
23101 | |
23102 |
|
23103 |
|
23104 |
|
23105 |
|
23106 | ;
|
23107 |
|
23108 | _proto.handleDurationChange_ = function handleDurationChange_() {
|
23109 | this.tech_.trigger('durationchange');
|
23110 | }
|
23111 | |
23112 |
|
23113 |
|
23114 |
|
23115 |
|
23116 |
|
23117 |
|
23118 |
|
23119 | ;
|
23120 |
|
23121 | _proto.onEndOfStream = function onEndOfStream() {
|
23122 | var isEndOfStream = this.mainSegmentLoader_.ended_;
|
23123 |
|
23124 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
23125 | var mainMediaInfo = this.mainSegmentLoader_.getCurrentMediaInfo_();
|
23126 |
|
23127 | if (!mainMediaInfo || mainMediaInfo.hasVideo) {
|
23128 |
|
23129 |
|
23130 |
|
23131 | isEndOfStream = isEndOfStream && this.audioSegmentLoader_.ended_;
|
23132 | } else {
|
23133 |
|
23134 | isEndOfStream = this.audioSegmentLoader_.ended_;
|
23135 | }
|
23136 | }
|
23137 |
|
23138 | if (!isEndOfStream) {
|
23139 | return;
|
23140 | }
|
23141 |
|
23142 | this.stopABRTimer_();
|
23143 | this.sourceUpdater_.endOfStream();
|
23144 | }
|
23145 | |
23146 |
|
23147 |
|
23148 |
|
23149 |
|
23150 |
|
23151 | ;
|
23152 |
|
23153 | _proto.stuckAtPlaylistEnd_ = function stuckAtPlaylistEnd_(playlist) {
|
23154 | var seekable = this.seekable();
|
23155 |
|
23156 | if (!seekable.length) {
|
23157 |
|
23158 | return false;
|
23159 | }
|
23160 |
|
23161 | var expired = this.syncController_.getExpiredTime(playlist, this.duration());
|
23162 |
|
23163 | if (expired === null) {
|
23164 | return false;
|
23165 | }
|
23166 |
|
23167 |
|
23168 |
|
23169 | var absolutePlaylistEnd = Vhs$1.Playlist.playlistEnd(playlist, expired);
|
23170 | var currentTime = this.tech_.currentTime();
|
23171 | var buffered = this.tech_.buffered();
|
23172 |
|
23173 | if (!buffered.length) {
|
23174 |
|
23175 | return absolutePlaylistEnd - currentTime <= SAFE_TIME_DELTA;
|
23176 | }
|
23177 |
|
23178 | var bufferedEnd = buffered.end(buffered.length - 1);
|
23179 |
|
23180 |
|
23181 | return bufferedEnd - currentTime <= SAFE_TIME_DELTA && absolutePlaylistEnd - bufferedEnd <= SAFE_TIME_DELTA;
|
23182 | }
|
23183 | |
23184 |
|
23185 |
|
23186 |
|
23187 |
|
23188 |
|
23189 |
|
23190 |
|
23191 |
|
23192 |
|
23193 | ;
|
23194 |
|
23195 | _proto.blacklistCurrentPlaylist = function blacklistCurrentPlaylist(error, blacklistDuration) {
|
23196 | if (error === void 0) {
|
23197 | error = {};
|
23198 | }
|
23199 |
|
23200 |
|
23201 |
|
23202 |
|
23203 |
|
23204 | var currentPlaylist = error.playlist || this.masterPlaylistLoader_.media();
|
23205 | blacklistDuration = blacklistDuration || error.blacklistDuration || this.blacklistDuration;
|
23206 |
|
23207 |
|
23208 | if (!currentPlaylist) {
|
23209 | this.error = error;
|
23210 |
|
23211 | if (this.mediaSource.readyState !== 'open') {
|
23212 | this.trigger('error');
|
23213 | } else {
|
23214 | this.sourceUpdater_.endOfStream('network');
|
23215 | }
|
23216 |
|
23217 | return;
|
23218 | }
|
23219 |
|
23220 | currentPlaylist.playlistErrors_++;
|
23221 | var playlists = this.masterPlaylistLoader_.master.playlists;
|
23222 | var enabledPlaylists = playlists.filter(isEnabled);
|
23223 | var isFinalRendition = enabledPlaylists.length === 1 && enabledPlaylists[0] === currentPlaylist;
|
23224 |
|
23225 |
|
23226 | if (playlists.length === 1 && blacklistDuration !== Infinity) {
|
23227 | videojs.log.warn("Problem encountered with playlist " + currentPlaylist.id + ". " + 'Trying again since it is the only playlist.');
|
23228 | this.tech_.trigger('retryplaylist');
|
23229 |
|
23230 | return this.masterPlaylistLoader_.load(isFinalRendition);
|
23231 | }
|
23232 |
|
23233 | if (isFinalRendition) {
|
23234 |
|
23235 |
|
23236 |
|
23237 |
|
23238 | var reincluded = false;
|
23239 | playlists.forEach(function (playlist) {
|
23240 |
|
23241 | if (playlist === currentPlaylist) {
|
23242 | return;
|
23243 | }
|
23244 |
|
23245 | var excludeUntil = playlist.excludeUntil;
|
23246 |
|
23247 | if (typeof excludeUntil !== 'undefined' && excludeUntil !== Infinity) {
|
23248 | reincluded = true;
|
23249 | delete playlist.excludeUntil;
|
23250 | }
|
23251 | });
|
23252 |
|
23253 | if (reincluded) {
|
23254 | videojs.log.warn('Removing other playlists from the exclusion list because the last ' + 'rendition is about to be excluded.');
|
23255 |
|
23256 |
|
23257 |
|
23258 | this.tech_.trigger('retryplaylist');
|
23259 | }
|
23260 | }
|
23261 |
|
23262 |
|
23263 | var excludeUntil;
|
23264 |
|
23265 | if (currentPlaylist.playlistErrors_ > this.maxPlaylistRetries) {
|
23266 | excludeUntil = Infinity;
|
23267 | } else {
|
23268 | excludeUntil = Date.now() + blacklistDuration * 1000;
|
23269 | }
|
23270 |
|
23271 | currentPlaylist.excludeUntil = excludeUntil;
|
23272 |
|
23273 | if (error.reason) {
|
23274 | currentPlaylist.lastExcludeReason_ = error.reason;
|
23275 | }
|
23276 |
|
23277 | this.tech_.trigger('blacklistplaylist');
|
23278 | this.tech_.trigger({
|
23279 | type: 'usage',
|
23280 | name: 'vhs-rendition-blacklisted'
|
23281 | });
|
23282 | this.tech_.trigger({
|
23283 | type: 'usage',
|
23284 | name: 'hls-rendition-blacklisted'
|
23285 | });
|
23286 |
|
23287 |
|
23288 |
|
23289 |
|
23290 |
|
23291 | var nextPlaylist = this.selectPlaylist();
|
23292 |
|
23293 | if (!nextPlaylist) {
|
23294 | this.error = 'Playback cannot continue. No available working or supported playlists.';
|
23295 | this.trigger('error');
|
23296 | return;
|
23297 | }
|
23298 |
|
23299 | var logFn = error.internal ? this.logger_ : videojs.log.warn;
|
23300 | var errorMessage = error.message ? ' ' + error.message : '';
|
23301 | logFn((error.internal ? 'Internal problem' : 'Problem') + " encountered with playlist " + currentPlaylist.id + "." + (errorMessage + " Switching to playlist " + nextPlaylist.id + "."));
|
23302 |
|
23303 | if (nextPlaylist.attributes.AUDIO !== currentPlaylist.attributes.AUDIO) {
|
23304 | this.delegateLoaders_('audio', ['abort', 'pause']);
|
23305 | }
|
23306 |
|
23307 |
|
23308 | if (nextPlaylist.attributes.SUBTITLES !== currentPlaylist.attributes.SUBTITLES) {
|
23309 | this.delegateLoaders_('subtitle', ['abort', 'pause']);
|
23310 | }
|
23311 |
|
23312 | this.delegateLoaders_('main', ['abort', 'pause']);
|
23313 | var delayDuration = nextPlaylist.targetDuration / 2 * 1000 || 5 * 1000;
|
23314 | var shouldDelay = typeof nextPlaylist.lastRequest === 'number' && Date.now() - nextPlaylist.lastRequest <= delayDuration;
|
23315 |
|
23316 | return this.switchMedia_(nextPlaylist, 'exclude', isFinalRendition || shouldDelay);
|
23317 | }
|
23318 | |
23319 |
|
23320 |
|
23321 | ;
|
23322 |
|
23323 | _proto.pauseLoading = function pauseLoading() {
|
23324 | this.delegateLoaders_('all', ['abort', 'pause']);
|
23325 | this.stopABRTimer_();
|
23326 | }
|
23327 | |
23328 |
|
23329 |
|
23330 |
|
23331 |
|
23332 |
|
23333 |
|
23334 |
|
23335 |
|
23336 |
|
23337 |
|
23338 |
|
23339 |
|
23340 |
|
23341 | ;
|
23342 |
|
23343 | _proto.delegateLoaders_ = function delegateLoaders_(filter, fnNames) {
|
23344 | var _this7 = this;
|
23345 |
|
23346 | var loaders = [];
|
23347 | var dontFilterPlaylist = filter === 'all';
|
23348 |
|
23349 | if (dontFilterPlaylist || filter === 'main') {
|
23350 | loaders.push(this.masterPlaylistLoader_);
|
23351 | }
|
23352 |
|
23353 | var mediaTypes = [];
|
23354 |
|
23355 | if (dontFilterPlaylist || filter === 'audio') {
|
23356 | mediaTypes.push('AUDIO');
|
23357 | }
|
23358 |
|
23359 | if (dontFilterPlaylist || filter === 'subtitle') {
|
23360 | mediaTypes.push('CLOSED-CAPTIONS');
|
23361 | mediaTypes.push('SUBTITLES');
|
23362 | }
|
23363 |
|
23364 | mediaTypes.forEach(function (mediaType) {
|
23365 | var loader = _this7.mediaTypes_[mediaType] && _this7.mediaTypes_[mediaType].activePlaylistLoader;
|
23366 |
|
23367 | if (loader) {
|
23368 | loaders.push(loader);
|
23369 | }
|
23370 | });
|
23371 | ['main', 'audio', 'subtitle'].forEach(function (name) {
|
23372 | var loader = _this7[name + "SegmentLoader_"];
|
23373 |
|
23374 | if (loader && (filter === name || filter === 'all')) {
|
23375 | loaders.push(loader);
|
23376 | }
|
23377 | });
|
23378 | loaders.forEach(function (loader) {
|
23379 | return fnNames.forEach(function (fnName) {
|
23380 | if (typeof loader[fnName] === 'function') {
|
23381 | loader[fnName]();
|
23382 | }
|
23383 | });
|
23384 | });
|
23385 | }
|
23386 | |
23387 |
|
23388 |
|
23389 |
|
23390 |
|
23391 |
|
23392 | ;
|
23393 |
|
23394 | _proto.setCurrentTime = function setCurrentTime(currentTime) {
|
23395 | var buffered = findRange(this.tech_.buffered(), currentTime);
|
23396 |
|
23397 | if (!(this.masterPlaylistLoader_ && this.masterPlaylistLoader_.media())) {
|
23398 |
|
23399 | return 0;
|
23400 | }
|
23401 |
|
23402 |
|
23403 |
|
23404 | if (!this.masterPlaylistLoader_.media().segments) {
|
23405 | return 0;
|
23406 | }
|
23407 |
|
23408 |
|
23409 | if (buffered && buffered.length) {
|
23410 | return currentTime;
|
23411 | }
|
23412 |
|
23413 |
|
23414 |
|
23415 | this.mainSegmentLoader_.resetEverything();
|
23416 | this.mainSegmentLoader_.abort();
|
23417 |
|
23418 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
23419 | this.audioSegmentLoader_.resetEverything();
|
23420 | this.audioSegmentLoader_.abort();
|
23421 | }
|
23422 |
|
23423 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
23424 | this.subtitleSegmentLoader_.resetEverything();
|
23425 | this.subtitleSegmentLoader_.abort();
|
23426 | }
|
23427 |
|
23428 |
|
23429 | this.load();
|
23430 | }
|
23431 | |
23432 |
|
23433 |
|
23434 |
|
23435 |
|
23436 | ;
|
23437 |
|
23438 | _proto.duration = function duration() {
|
23439 | if (!this.masterPlaylistLoader_) {
|
23440 | return 0;
|
23441 | }
|
23442 |
|
23443 | var media = this.masterPlaylistLoader_.media();
|
23444 |
|
23445 | if (!media) {
|
23446 |
|
23447 | return 0;
|
23448 | }
|
23449 |
|
23450 |
|
23451 |
|
23452 |
|
23453 |
|
23454 |
|
23455 |
|
23456 |
|
23457 |
|
23458 |
|
23459 |
|
23460 | if (!media.endList) {
|
23461 | return Infinity;
|
23462 | }
|
23463 |
|
23464 |
|
23465 |
|
23466 | if (this.mediaSource) {
|
23467 | return this.mediaSource.duration;
|
23468 | }
|
23469 |
|
23470 | return Vhs$1.Playlist.duration(media);
|
23471 | }
|
23472 | |
23473 |
|
23474 |
|
23475 |
|
23476 |
|
23477 | ;
|
23478 |
|
23479 | _proto.seekable = function seekable() {
|
23480 | return this.seekable_;
|
23481 | };
|
23482 |
|
23483 | _proto.onSyncInfoUpdate_ = function onSyncInfoUpdate_() {
|
23484 | var audioSeekable;
|
23485 |
|
23486 | if (!this.masterPlaylistLoader_) {
|
23487 | return;
|
23488 | }
|
23489 |
|
23490 | var media = this.masterPlaylistLoader_.media();
|
23491 |
|
23492 | if (!media) {
|
23493 | return;
|
23494 | }
|
23495 |
|
23496 | var expired = this.syncController_.getExpiredTime(media, this.duration());
|
23497 |
|
23498 | if (expired === null) {
|
23499 |
|
23500 | return;
|
23501 | }
|
23502 |
|
23503 | var master = this.masterPlaylistLoader_.master;
|
23504 | var mainSeekable = Vhs$1.Playlist.seekable(media, expired, Vhs$1.Playlist.liveEdgeDelay(master, media));
|
23505 |
|
23506 | if (mainSeekable.length === 0) {
|
23507 | return;
|
23508 | }
|
23509 |
|
23510 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
23511 | media = this.mediaTypes_.AUDIO.activePlaylistLoader.media();
|
23512 | expired = this.syncController_.getExpiredTime(media, this.duration());
|
23513 |
|
23514 | if (expired === null) {
|
23515 | return;
|
23516 | }
|
23517 |
|
23518 | audioSeekable = Vhs$1.Playlist.seekable(media, expired, Vhs$1.Playlist.liveEdgeDelay(master, media));
|
23519 |
|
23520 | if (audioSeekable.length === 0) {
|
23521 | return;
|
23522 | }
|
23523 | }
|
23524 |
|
23525 | var oldEnd;
|
23526 | var oldStart;
|
23527 |
|
23528 | if (this.seekable_ && this.seekable_.length) {
|
23529 | oldEnd = this.seekable_.end(0);
|
23530 | oldStart = this.seekable_.start(0);
|
23531 | }
|
23532 |
|
23533 | if (!audioSeekable) {
|
23534 |
|
23535 |
|
23536 | this.seekable_ = mainSeekable;
|
23537 | } else if (audioSeekable.start(0) > mainSeekable.end(0) || mainSeekable.start(0) > audioSeekable.end(0)) {
|
23538 |
|
23539 | this.seekable_ = mainSeekable;
|
23540 | } else {
|
23541 | this.seekable_ = videojs.createTimeRanges([[audioSeekable.start(0) > mainSeekable.start(0) ? audioSeekable.start(0) : mainSeekable.start(0), audioSeekable.end(0) < mainSeekable.end(0) ? audioSeekable.end(0) : mainSeekable.end(0)]]);
|
23542 | }
|
23543 |
|
23544 |
|
23545 | if (this.seekable_ && this.seekable_.length) {
|
23546 | if (this.seekable_.end(0) === oldEnd && this.seekable_.start(0) === oldStart) {
|
23547 | return;
|
23548 | }
|
23549 | }
|
23550 |
|
23551 | this.logger_("seekable updated [" + printableRange(this.seekable_) + "]");
|
23552 | this.tech_.trigger('seekablechanged');
|
23553 | }
|
23554 | |
23555 |
|
23556 |
|
23557 | ;
|
23558 |
|
23559 | _proto.updateDuration = function updateDuration(isLive) {
|
23560 | if (this.updateDuration_) {
|
23561 | this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
|
23562 | this.updateDuration_ = null;
|
23563 | }
|
23564 |
|
23565 | if (this.mediaSource.readyState !== 'open') {
|
23566 | this.updateDuration_ = this.updateDuration.bind(this, isLive);
|
23567 | this.mediaSource.addEventListener('sourceopen', this.updateDuration_);
|
23568 | return;
|
23569 | }
|
23570 |
|
23571 | if (isLive) {
|
23572 | var seekable = this.seekable();
|
23573 |
|
23574 | if (!seekable.length) {
|
23575 | return;
|
23576 | }
|
23577 |
|
23578 |
|
23579 |
|
23580 |
|
23581 |
|
23582 |
|
23583 |
|
23584 |
|
23585 |
|
23586 |
|
23587 |
|
23588 |
|
23589 |
|
23590 |
|
23591 |
|
23592 |
|
23593 |
|
23594 |
|
23595 |
|
23596 |
|
23597 |
|
23598 |
|
23599 |
|
23600 |
|
23601 | if (isNaN(this.mediaSource.duration) || this.mediaSource.duration < seekable.end(seekable.length - 1)) {
|
23602 | this.sourceUpdater_.setDuration(seekable.end(seekable.length - 1));
|
23603 | }
|
23604 |
|
23605 | return;
|
23606 | }
|
23607 |
|
23608 | var buffered = this.tech_.buffered();
|
23609 | var duration = Vhs$1.Playlist.duration(this.masterPlaylistLoader_.media());
|
23610 |
|
23611 | if (buffered.length > 0) {
|
23612 | duration = Math.max(duration, buffered.end(buffered.length - 1));
|
23613 | }
|
23614 |
|
23615 | if (this.mediaSource.duration !== duration) {
|
23616 | this.sourceUpdater_.setDuration(duration);
|
23617 | }
|
23618 | }
|
23619 | |
23620 |
|
23621 |
|
23622 |
|
23623 | ;
|
23624 |
|
23625 | _proto.dispose = function dispose() {
|
23626 | var _this8 = this;
|
23627 |
|
23628 | this.trigger('dispose');
|
23629 | this.decrypter_.terminate();
|
23630 | this.masterPlaylistLoader_.dispose();
|
23631 | this.mainSegmentLoader_.dispose();
|
23632 |
|
23633 | if (this.loadOnPlay_) {
|
23634 | this.tech_.off('play', this.loadOnPlay_);
|
23635 | }
|
23636 |
|
23637 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
23638 | var groups = _this8.mediaTypes_[type].groups;
|
23639 |
|
23640 | for (var id in groups) {
|
23641 | groups[id].forEach(function (group) {
|
23642 | if (group.playlistLoader) {
|
23643 | group.playlistLoader.dispose();
|
23644 | }
|
23645 | });
|
23646 | }
|
23647 | });
|
23648 | this.audioSegmentLoader_.dispose();
|
23649 | this.subtitleSegmentLoader_.dispose();
|
23650 | this.sourceUpdater_.dispose();
|
23651 | this.timelineChangeController_.dispose();
|
23652 | this.stopABRTimer_();
|
23653 |
|
23654 | if (this.updateDuration_) {
|
23655 | this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
|
23656 | }
|
23657 |
|
23658 | this.mediaSource.removeEventListener('durationchange', this.handleDurationChange_);
|
23659 |
|
23660 | this.mediaSource.removeEventListener('sourceopen', this.handleSourceOpen_);
|
23661 | this.mediaSource.removeEventListener('sourceended', this.handleSourceEnded_);
|
23662 | this.off();
|
23663 | }
|
23664 | |
23665 |
|
23666 |
|
23667 |
|
23668 |
|
23669 | ;
|
23670 |
|
23671 | _proto.master = function master() {
|
23672 | return this.masterPlaylistLoader_.master;
|
23673 | }
|
23674 | |
23675 |
|
23676 |
|
23677 |
|
23678 |
|
23679 | ;
|
23680 |
|
23681 | _proto.media = function media() {
|
23682 |
|
23683 | return this.masterPlaylistLoader_.media() || this.initialMedia_;
|
23684 | };
|
23685 |
|
23686 | _proto.areMediaTypesKnown_ = function areMediaTypesKnown_() {
|
23687 | var usingAudioLoader = !!this.mediaTypes_.AUDIO.activePlaylistLoader;
|
23688 | var hasMainMediaInfo = !!this.mainSegmentLoader_.getCurrentMediaInfo_();
|
23689 |
|
23690 |
|
23691 | var hasAudioMediaInfo = !usingAudioLoader ? true : !!this.audioSegmentLoader_.getCurrentMediaInfo_();
|
23692 |
|
23693 | if (!hasMainMediaInfo || !hasAudioMediaInfo) {
|
23694 | return false;
|
23695 | }
|
23696 |
|
23697 | return true;
|
23698 | };
|
23699 |
|
23700 | _proto.getCodecsOrExclude_ = function getCodecsOrExclude_() {
|
23701 | var _this9 = this;
|
23702 |
|
23703 | var media = {
|
23704 | main: this.mainSegmentLoader_.getCurrentMediaInfo_() || {},
|
23705 | audio: this.audioSegmentLoader_.getCurrentMediaInfo_() || {}
|
23706 | };
|
23707 |
|
23708 | media.video = media.main;
|
23709 | var playlistCodecs = codecsForPlaylist(this.master(), this.media());
|
23710 | var codecs = {};
|
23711 | var usingAudioLoader = !!this.mediaTypes_.AUDIO.activePlaylistLoader;
|
23712 |
|
23713 | if (media.main.hasVideo) {
|
23714 | codecs.video = playlistCodecs.video || media.main.videoCodec || DEFAULT_VIDEO_CODEC;
|
23715 | }
|
23716 |
|
23717 | if (media.main.isMuxed) {
|
23718 | codecs.video += "," + (playlistCodecs.audio || media.main.audioCodec || DEFAULT_AUDIO_CODEC);
|
23719 | }
|
23720 |
|
23721 | if (media.main.hasAudio && !media.main.isMuxed || media.audio.hasAudio || usingAudioLoader) {
|
23722 | codecs.audio = playlistCodecs.audio || media.main.audioCodec || media.audio.audioCodec || DEFAULT_AUDIO_CODEC;
|
23723 |
|
23724 | media.audio.isFmp4 = media.main.hasAudio && !media.main.isMuxed ? media.main.isFmp4 : media.audio.isFmp4;
|
23725 | }
|
23726 |
|
23727 |
|
23728 | if (!codecs.audio && !codecs.video) {
|
23729 | this.blacklistCurrentPlaylist({
|
23730 | playlist: this.media(),
|
23731 | message: 'Could not determine codecs for playlist.',
|
23732 | blacklistDuration: Infinity
|
23733 | });
|
23734 | return;
|
23735 | }
|
23736 |
|
23737 |
|
23738 | var supportFunction = function supportFunction(isFmp4, codec) {
|
23739 | return isFmp4 ? browserSupportsCodec(codec) : muxerSupportsCodec(codec);
|
23740 | };
|
23741 |
|
23742 | var unsupportedCodecs = {};
|
23743 | var unsupportedAudio;
|
23744 | ['video', 'audio'].forEach(function (type) {
|
23745 | if (codecs.hasOwnProperty(type) && !supportFunction(media[type].isFmp4, codecs[type])) {
|
23746 | var supporter = media[type].isFmp4 ? 'browser' : 'muxer';
|
23747 | unsupportedCodecs[supporter] = unsupportedCodecs[supporter] || [];
|
23748 | unsupportedCodecs[supporter].push(codecs[type]);
|
23749 |
|
23750 | if (type === 'audio') {
|
23751 | unsupportedAudio = supporter;
|
23752 | }
|
23753 | }
|
23754 | });
|
23755 |
|
23756 | if (usingAudioLoader && unsupportedAudio && this.media().attributes.AUDIO) {
|
23757 | var audioGroup = this.media().attributes.AUDIO;
|
23758 | this.master().playlists.forEach(function (variant) {
|
23759 | var variantAudioGroup = variant.attributes && variant.attributes.AUDIO;
|
23760 |
|
23761 | if (variantAudioGroup === audioGroup && variant !== _this9.media()) {
|
23762 | variant.excludeUntil = Infinity;
|
23763 | }
|
23764 | });
|
23765 | this.logger_("excluding audio group " + audioGroup + " as " + unsupportedAudio + " does not support codec(s): \"" + codecs.audio + "\"");
|
23766 | }
|
23767 |
|
23768 |
|
23769 | if (Object.keys(unsupportedCodecs).length) {
|
23770 | var message = Object.keys(unsupportedCodecs).reduce(function (acc, supporter) {
|
23771 | if (acc) {
|
23772 | acc += ', ';
|
23773 | }
|
23774 |
|
23775 | acc += supporter + " does not support codec(s): \"" + unsupportedCodecs[supporter].join(',') + "\"";
|
23776 | return acc;
|
23777 | }, '') + '.';
|
23778 | this.blacklistCurrentPlaylist({
|
23779 | playlist: this.media(),
|
23780 | internal: true,
|
23781 | message: message,
|
23782 | blacklistDuration: Infinity
|
23783 | });
|
23784 | return;
|
23785 | }
|
23786 |
|
23787 |
|
23788 | if (this.sourceUpdater_.hasCreatedSourceBuffers() && !this.sourceUpdater_.canChangeType()) {
|
23789 | var switchMessages = [];
|
23790 | ['video', 'audio'].forEach(function (type) {
|
23791 | var newCodec = (parseCodecs(_this9.sourceUpdater_.codecs[type] || '')[0] || {}).type;
|
23792 | var oldCodec = (parseCodecs(codecs[type] || '')[0] || {}).type;
|
23793 |
|
23794 | if (newCodec && oldCodec && newCodec.toLowerCase() !== oldCodec.toLowerCase()) {
|
23795 | switchMessages.push("\"" + _this9.sourceUpdater_.codecs[type] + "\" -> \"" + codecs[type] + "\"");
|
23796 | }
|
23797 | });
|
23798 |
|
23799 | if (switchMessages.length) {
|
23800 | this.blacklistCurrentPlaylist({
|
23801 | playlist: this.media(),
|
23802 | message: "Codec switching not supported: " + switchMessages.join(', ') + ".",
|
23803 | blacklistDuration: Infinity,
|
23804 | internal: true
|
23805 | });
|
23806 | return;
|
23807 | }
|
23808 | }
|
23809 |
|
23810 |
|
23811 |
|
23812 | return codecs;
|
23813 | }
|
23814 | |
23815 |
|
23816 |
|
23817 |
|
23818 |
|
23819 | ;
|
23820 |
|
23821 | _proto.tryToCreateSourceBuffers_ = function tryToCreateSourceBuffers_() {
|
23822 |
|
23823 |
|
23824 | if (this.mediaSource.readyState !== 'open' || this.sourceUpdater_.hasCreatedSourceBuffers()) {
|
23825 | return;
|
23826 | }
|
23827 |
|
23828 | if (!this.areMediaTypesKnown_()) {
|
23829 | return;
|
23830 | }
|
23831 |
|
23832 | var codecs = this.getCodecsOrExclude_();
|
23833 |
|
23834 | if (!codecs) {
|
23835 | return;
|
23836 | }
|
23837 |
|
23838 | this.sourceUpdater_.createSourceBuffers(codecs);
|
23839 | var codecString = [codecs.video, codecs.audio].filter(Boolean).join(',');
|
23840 | this.excludeIncompatibleVariants_(codecString);
|
23841 | }
|
23842 | |
23843 |
|
23844 |
|
23845 | ;
|
23846 |
|
23847 | _proto.excludeUnsupportedVariants_ = function excludeUnsupportedVariants_() {
|
23848 | var _this10 = this;
|
23849 |
|
23850 | var playlists = this.master().playlists;
|
23851 | var ids = [];
|
23852 |
|
23853 |
|
23854 | Object.keys(playlists).forEach(function (key) {
|
23855 | var variant = playlists[key];
|
23856 |
|
23857 | if (ids.indexOf(variant.id) !== -1) {
|
23858 | return;
|
23859 | }
|
23860 |
|
23861 | ids.push(variant.id);
|
23862 | var codecs = codecsForPlaylist(_this10.master, variant);
|
23863 | var unsupported = [];
|
23864 |
|
23865 | if (codecs.audio && !muxerSupportsCodec(codecs.audio) && !browserSupportsCodec(codecs.audio)) {
|
23866 | unsupported.push("audio codec " + codecs.audio);
|
23867 | }
|
23868 |
|
23869 | if (codecs.video && !muxerSupportsCodec(codecs.video) && !browserSupportsCodec(codecs.video)) {
|
23870 | unsupported.push("video codec " + codecs.video);
|
23871 | }
|
23872 |
|
23873 | if (codecs.text && codecs.text === 'stpp.ttml.im1t') {
|
23874 | unsupported.push("text codec " + codecs.text);
|
23875 | }
|
23876 |
|
23877 | if (unsupported.length) {
|
23878 | variant.excludeUntil = Infinity;
|
23879 |
|
23880 | _this10.logger_("excluding " + variant.id + " for unsupported: " + unsupported.join(', '));
|
23881 | }
|
23882 | });
|
23883 | }
|
23884 | |
23885 |
|
23886 |
|
23887 |
|
23888 |
|
23889 |
|
23890 |
|
23891 |
|
23892 |
|
23893 |
|
23894 |
|
23895 |
|
23896 |
|
23897 |
|
23898 | ;
|
23899 |
|
23900 | _proto.excludeIncompatibleVariants_ = function excludeIncompatibleVariants_(codecString) {
|
23901 | var _this11 = this;
|
23902 |
|
23903 | var ids = [];
|
23904 | var playlists = this.master().playlists;
|
23905 | var codecs = unwrapCodecList(parseCodecs(codecString));
|
23906 | var codecCount_ = codecCount(codecs);
|
23907 | var videoDetails = codecs.video && parseCodecs(codecs.video)[0] || null;
|
23908 | var audioDetails = codecs.audio && parseCodecs(codecs.audio)[0] || null;
|
23909 | Object.keys(playlists).forEach(function (key) {
|
23910 | var variant = playlists[key];
|
23911 |
|
23912 |
|
23913 | if (ids.indexOf(variant.id) !== -1 || variant.excludeUntil === Infinity) {
|
23914 | return;
|
23915 | }
|
23916 |
|
23917 | ids.push(variant.id);
|
23918 | var blacklistReasons = [];
|
23919 |
|
23920 | var variantCodecs = codecsForPlaylist(_this11.masterPlaylistLoader_.master, variant);
|
23921 | var variantCodecCount = codecCount(variantCodecs);
|
23922 |
|
23923 |
|
23924 | if (!variantCodecs.audio && !variantCodecs.video) {
|
23925 | return;
|
23926 | }
|
23927 |
|
23928 |
|
23929 |
|
23930 |
|
23931 | if (variantCodecCount !== codecCount_) {
|
23932 | blacklistReasons.push("codec count \"" + variantCodecCount + "\" !== \"" + codecCount_ + "\"");
|
23933 | }
|
23934 |
|
23935 |
|
23936 |
|
23937 | if (!_this11.sourceUpdater_.canChangeType()) {
|
23938 | var variantVideoDetails = variantCodecs.video && parseCodecs(variantCodecs.video)[0] || null;
|
23939 | var variantAudioDetails = variantCodecs.audio && parseCodecs(variantCodecs.audio)[0] || null;
|
23940 |
|
23941 | if (variantVideoDetails && videoDetails && variantVideoDetails.type.toLowerCase() !== videoDetails.type.toLowerCase()) {
|
23942 | blacklistReasons.push("video codec \"" + variantVideoDetails.type + "\" !== \"" + videoDetails.type + "\"");
|
23943 | }
|
23944 |
|
23945 |
|
23946 | if (variantAudioDetails && audioDetails && variantAudioDetails.type.toLowerCase() !== audioDetails.type.toLowerCase()) {
|
23947 | blacklistReasons.push("audio codec \"" + variantAudioDetails.type + "\" !== \"" + audioDetails.type + "\"");
|
23948 | }
|
23949 | }
|
23950 |
|
23951 | if (blacklistReasons.length) {
|
23952 | variant.excludeUntil = Infinity;
|
23953 |
|
23954 | _this11.logger_("blacklisting " + variant.id + ": " + blacklistReasons.join(' && '));
|
23955 | }
|
23956 | });
|
23957 | };
|
23958 |
|
23959 | _proto.updateAdCues_ = function updateAdCues_(media) {
|
23960 | var offset = 0;
|
23961 | var seekable = this.seekable();
|
23962 |
|
23963 | if (seekable.length) {
|
23964 | offset = seekable.start(0);
|
23965 | }
|
23966 |
|
23967 | updateAdCues(media, this.cueTagsTrack_, offset);
|
23968 | }
|
23969 | |
23970 |
|
23971 |
|
23972 |
|
23973 |
|
23974 | ;
|
23975 |
|
23976 | _proto.goalBufferLength = function goalBufferLength() {
|
23977 | var currentTime = this.tech_.currentTime();
|
23978 | var initial = Config.GOAL_BUFFER_LENGTH;
|
23979 | var rate = Config.GOAL_BUFFER_LENGTH_RATE;
|
23980 | var max = Math.max(initial, Config.MAX_GOAL_BUFFER_LENGTH);
|
23981 | return Math.min(initial + currentTime * rate, max);
|
23982 | }
|
23983 | |
23984 |
|
23985 |
|
23986 |
|
23987 |
|
23988 | ;
|
23989 |
|
23990 | _proto.bufferLowWaterLine = function bufferLowWaterLine() {
|
23991 | var currentTime = this.tech_.currentTime();
|
23992 | var initial = Config.BUFFER_LOW_WATER_LINE;
|
23993 | var rate = Config.BUFFER_LOW_WATER_LINE_RATE;
|
23994 | var max = Math.max(initial, Config.MAX_BUFFER_LOW_WATER_LINE);
|
23995 | var newMax = Math.max(initial, Config.EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE);
|
23996 | return Math.min(initial + currentTime * rate, this.experimentalBufferBasedABR ? newMax : max);
|
23997 | };
|
23998 |
|
23999 | _proto.bufferHighWaterLine = function bufferHighWaterLine() {
|
24000 | return Config.BUFFER_HIGH_WATER_LINE;
|
24001 | };
|
24002 |
|
24003 | return MasterPlaylistController;
|
24004 | }(videojs.EventTarget);
|
24005 |
|
24006 |
|
24007 |
|
24008 |
|
24009 |
|
24010 |
|
24011 |
|
24012 |
|
24013 |
|
24014 |
|
24015 |
|
24016 |
|
24017 |
|
24018 |
|
24019 | var enableFunction = function enableFunction(loader, playlistID, changePlaylistFn) {
|
24020 | return function (enable) {
|
24021 | var playlist = loader.master.playlists[playlistID];
|
24022 | var incompatible = isIncompatible(playlist);
|
24023 | var currentlyEnabled = isEnabled(playlist);
|
24024 |
|
24025 | if (typeof enable === 'undefined') {
|
24026 | return currentlyEnabled;
|
24027 | }
|
24028 |
|
24029 | if (enable) {
|
24030 | delete playlist.disabled;
|
24031 | } else {
|
24032 | playlist.disabled = true;
|
24033 | }
|
24034 |
|
24035 | if (enable !== currentlyEnabled && !incompatible) {
|
24036 |
|
24037 | changePlaylistFn();
|
24038 |
|
24039 | if (enable) {
|
24040 | loader.trigger('renditionenabled');
|
24041 | } else {
|
24042 | loader.trigger('renditiondisabled');
|
24043 | }
|
24044 | }
|
24045 |
|
24046 | return enable;
|
24047 | };
|
24048 | };
|
24049 |
|
24050 |
|
24051 |
|
24052 |
|
24053 |
|
24054 |
|
24055 |
|
24056 |
|
24057 |
|
24058 | var Representation = function Representation(vhsHandler, playlist, id) {
|
24059 | var mpc = vhsHandler.masterPlaylistController_,
|
24060 | smoothQualityChange = vhsHandler.options_.smoothQualityChange;
|
24061 |
|
24062 | var changeType = smoothQualityChange ? 'smooth' : 'fast';
|
24063 | var qualityChangeFunction = mpc[changeType + "QualityChange_"].bind(mpc);
|
24064 |
|
24065 | if (playlist.attributes) {
|
24066 | var resolution = playlist.attributes.RESOLUTION;
|
24067 | this.width = resolution && resolution.width;
|
24068 | this.height = resolution && resolution.height;
|
24069 | this.bandwidth = playlist.attributes.BANDWIDTH;
|
24070 | }
|
24071 |
|
24072 | this.codecs = codecsForPlaylist(mpc.master(), playlist);
|
24073 | this.playlist = playlist;
|
24074 |
|
24075 |
|
24076 | this.id = id;
|
24077 |
|
24078 |
|
24079 | this.enabled = enableFunction(vhsHandler.playlists, playlist.id, qualityChangeFunction);
|
24080 | };
|
24081 |
|
24082 |
|
24083 |
|
24084 |
|
24085 |
|
24086 |
|
24087 |
|
24088 |
|
24089 |
|
24090 | var renditionSelectionMixin = function renditionSelectionMixin(vhsHandler) {
|
24091 |
|
24092 | vhsHandler.representations = function () {
|
24093 | var master = vhsHandler.masterPlaylistController_.master();
|
24094 | var playlists = isAudioOnly(master) ? vhsHandler.masterPlaylistController_.getAudioTrackPlaylists_() : master.playlists;
|
24095 |
|
24096 | if (!playlists) {
|
24097 | return [];
|
24098 | }
|
24099 |
|
24100 | return playlists.filter(function (media) {
|
24101 | return !isIncompatible(media);
|
24102 | }).map(function (e, i) {
|
24103 | return new Representation(vhsHandler, e, e.id);
|
24104 | });
|
24105 | };
|
24106 | };
|
24107 |
|
24108 |
|
24109 |
|
24110 |
|
24111 |
|
24112 |
|
24113 |
|
24114 |
|
24115 |
|
24116 |
|
24117 |
|
24118 | var timerCancelEvents = ['seeking', 'seeked', 'pause', 'playing', 'error'];
|
24119 |
|
24120 |
|
24121 |
|
24122 |
|
24123 | var PlaybackWatcher = function () {
|
24124 | |
24125 |
|
24126 |
|
24127 |
|
24128 |
|
24129 |
|
24130 | function PlaybackWatcher(options) {
|
24131 | var _this = this;
|
24132 |
|
24133 | this.masterPlaylistController_ = options.masterPlaylistController;
|
24134 | this.tech_ = options.tech;
|
24135 | this.seekable = options.seekable;
|
24136 | this.allowSeeksWithinUnsafeLiveWindow = options.allowSeeksWithinUnsafeLiveWindow;
|
24137 | this.liveRangeSafeTimeDelta = options.liveRangeSafeTimeDelta;
|
24138 | this.media = options.media;
|
24139 | this.consecutiveUpdates = 0;
|
24140 | this.lastRecordedTime = null;
|
24141 | this.timer_ = null;
|
24142 | this.checkCurrentTimeTimeout_ = null;
|
24143 | this.logger_ = logger('PlaybackWatcher');
|
24144 | this.logger_('initialize');
|
24145 |
|
24146 | var playHandler = function playHandler() {
|
24147 | return _this.monitorCurrentTime_();
|
24148 | };
|
24149 |
|
24150 | var canPlayHandler = function canPlayHandler() {
|
24151 | return _this.monitorCurrentTime_();
|
24152 | };
|
24153 |
|
24154 | var waitingHandler = function waitingHandler() {
|
24155 | return _this.techWaiting_();
|
24156 | };
|
24157 |
|
24158 | var cancelTimerHandler = function cancelTimerHandler() {
|
24159 | return _this.cancelTimer_();
|
24160 | };
|
24161 |
|
24162 | var mpc = this.masterPlaylistController_;
|
24163 | var loaderTypes = ['main', 'subtitle', 'audio'];
|
24164 | var loaderChecks = {};
|
24165 | loaderTypes.forEach(function (type) {
|
24166 | loaderChecks[type] = {
|
24167 | reset: function reset() {
|
24168 | return _this.resetSegmentDownloads_(type);
|
24169 | },
|
24170 | updateend: function updateend() {
|
24171 | return _this.checkSegmentDownloads_(type);
|
24172 | }
|
24173 | };
|
24174 | mpc[type + "SegmentLoader_"].on('appendsdone', loaderChecks[type].updateend);
|
24175 |
|
24176 |
|
24177 |
|
24178 | mpc[type + "SegmentLoader_"].on('playlistupdate', loaderChecks[type].reset);
|
24179 |
|
24180 |
|
24181 |
|
24182 |
|
24183 | _this.tech_.on(['seeked', 'seeking'], loaderChecks[type].reset);
|
24184 | });
|
24185 | |
24186 |
|
24187 |
|
24188 |
|
24189 |
|
24190 |
|
24191 |
|
24192 |
|
24193 |
|
24194 | var setSeekingHandlers = function setSeekingHandlers(fn) {
|
24195 | ['main', 'audio'].forEach(function (type) {
|
24196 | mpc[type + "SegmentLoader_"][fn]('appended', _this.seekingAppendCheck_);
|
24197 | });
|
24198 | };
|
24199 |
|
24200 | this.seekingAppendCheck_ = function () {
|
24201 | if (_this.fixesBadSeeks_()) {
|
24202 | _this.consecutiveUpdates = 0;
|
24203 | _this.lastRecordedTime = _this.tech_.currentTime();
|
24204 | setSeekingHandlers('off');
|
24205 | }
|
24206 | };
|
24207 |
|
24208 | this.clearSeekingAppendCheck_ = function () {
|
24209 | return setSeekingHandlers('off');
|
24210 | };
|
24211 |
|
24212 | this.watchForBadSeeking_ = function () {
|
24213 | _this.clearSeekingAppendCheck_();
|
24214 |
|
24215 | setSeekingHandlers('on');
|
24216 | };
|
24217 |
|
24218 | this.tech_.on('seeked', this.clearSeekingAppendCheck_);
|
24219 | this.tech_.on('seeking', this.watchForBadSeeking_);
|
24220 | this.tech_.on('waiting', waitingHandler);
|
24221 | this.tech_.on(timerCancelEvents, cancelTimerHandler);
|
24222 | this.tech_.on('canplay', canPlayHandler);
|
24223 | |
24224 |
|
24225 |
|
24226 |
|
24227 |
|
24228 |
|
24229 |
|
24230 |
|
24231 |
|
24232 |
|
24233 |
|
24234 | this.tech_.one('play', playHandler);
|
24235 |
|
24236 | this.dispose = function () {
|
24237 | _this.clearSeekingAppendCheck_();
|
24238 |
|
24239 | _this.logger_('dispose');
|
24240 |
|
24241 | _this.tech_.off('waiting', waitingHandler);
|
24242 |
|
24243 | _this.tech_.off(timerCancelEvents, cancelTimerHandler);
|
24244 |
|
24245 | _this.tech_.off('canplay', canPlayHandler);
|
24246 |
|
24247 | _this.tech_.off('play', playHandler);
|
24248 |
|
24249 | _this.tech_.off('seeking', _this.watchForBadSeeking_);
|
24250 |
|
24251 | _this.tech_.off('seeked', _this.clearSeekingAppendCheck_);
|
24252 |
|
24253 | loaderTypes.forEach(function (type) {
|
24254 | mpc[type + "SegmentLoader_"].off('appendsdone', loaderChecks[type].updateend);
|
24255 | mpc[type + "SegmentLoader_"].off('playlistupdate', loaderChecks[type].reset);
|
24256 |
|
24257 | _this.tech_.off(['seeked', 'seeking'], loaderChecks[type].reset);
|
24258 | });
|
24259 |
|
24260 | if (_this.checkCurrentTimeTimeout_) {
|
24261 | window.clearTimeout(_this.checkCurrentTimeTimeout_);
|
24262 | }
|
24263 |
|
24264 | _this.cancelTimer_();
|
24265 | };
|
24266 | }
|
24267 | |
24268 |
|
24269 |
|
24270 |
|
24271 |
|
24272 |
|
24273 |
|
24274 | var _proto = PlaybackWatcher.prototype;
|
24275 |
|
24276 | _proto.monitorCurrentTime_ = function monitorCurrentTime_() {
|
24277 | this.checkCurrentTime_();
|
24278 |
|
24279 | if (this.checkCurrentTimeTimeout_) {
|
24280 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
24281 | }
|
24282 |
|
24283 |
|
24284 | this.checkCurrentTimeTimeout_ = window.setTimeout(this.monitorCurrentTime_.bind(this), 250);
|
24285 | }
|
24286 | |
24287 |
|
24288 |
|
24289 |
|
24290 |
|
24291 |
|
24292 |
|
24293 |
|
24294 |
|
24295 |
|
24296 | ;
|
24297 |
|
24298 | _proto.resetSegmentDownloads_ = function resetSegmentDownloads_(type) {
|
24299 | var loader = this.masterPlaylistController_[type + "SegmentLoader_"];
|
24300 |
|
24301 | if (this[type + "StalledDownloads_"] > 0) {
|
24302 | this.logger_("resetting possible stalled download count for " + type + " loader");
|
24303 | }
|
24304 |
|
24305 | this[type + "StalledDownloads_"] = 0;
|
24306 | this[type + "Buffered_"] = loader.buffered_();
|
24307 | }
|
24308 | |
24309 |
|
24310 |
|
24311 |
|
24312 |
|
24313 |
|
24314 |
|
24315 |
|
24316 |
|
24317 |
|
24318 | ;
|
24319 |
|
24320 | _proto.checkSegmentDownloads_ = function checkSegmentDownloads_(type) {
|
24321 | var mpc = this.masterPlaylistController_;
|
24322 | var loader = mpc[type + "SegmentLoader_"];
|
24323 | var buffered = loader.buffered_();
|
24324 | var isBufferedDifferent = isRangeDifferent(this[type + "Buffered_"], buffered);
|
24325 | this[type + "Buffered_"] = buffered;
|
24326 |
|
24327 |
|
24328 |
|
24329 | if (isBufferedDifferent) {
|
24330 | this.resetSegmentDownloads_(type);
|
24331 | return;
|
24332 | }
|
24333 |
|
24334 | this[type + "StalledDownloads_"]++;
|
24335 | this.logger_("found #" + this[type + "StalledDownloads_"] + " " + type + " appends that did not increase buffer (possible stalled download)", {
|
24336 | playlistId: loader.playlist_ && loader.playlist_.id,
|
24337 | buffered: timeRangesToArray(buffered)
|
24338 | });
|
24339 |
|
24340 | if (this[type + "StalledDownloads_"] < 10) {
|
24341 | return;
|
24342 | }
|
24343 |
|
24344 | this.logger_(type + " loader stalled download exclusion");
|
24345 | this.resetSegmentDownloads_(type);
|
24346 | this.tech_.trigger({
|
24347 | type: 'usage',
|
24348 | name: "vhs-" + type + "-download-exclusion"
|
24349 | });
|
24350 |
|
24351 | if (type === 'subtitle') {
|
24352 | return;
|
24353 | }
|
24354 |
|
24355 |
|
24356 |
|
24357 | mpc.blacklistCurrentPlaylist({
|
24358 | message: "Excessive " + type + " segment downloading detected."
|
24359 | }, Infinity);
|
24360 | }
|
24361 | |
24362 |
|
24363 |
|
24364 |
|
24365 |
|
24366 |
|
24367 |
|
24368 | ;
|
24369 |
|
24370 | _proto.checkCurrentTime_ = function checkCurrentTime_() {
|
24371 | if (this.tech_.paused() || this.tech_.seeking()) {
|
24372 | return;
|
24373 | }
|
24374 |
|
24375 | var currentTime = this.tech_.currentTime();
|
24376 | var buffered = this.tech_.buffered();
|
24377 |
|
24378 | if (this.lastRecordedTime === currentTime && (!buffered.length || currentTime + SAFE_TIME_DELTA >= buffered.end(buffered.length - 1))) {
|
24379 |
|
24380 |
|
24381 |
|
24382 |
|
24383 |
|
24384 | return this.techWaiting_();
|
24385 | }
|
24386 |
|
24387 | if (this.consecutiveUpdates >= 5 && currentTime === this.lastRecordedTime) {
|
24388 | this.consecutiveUpdates++;
|
24389 | this.waiting_();
|
24390 | } else if (currentTime === this.lastRecordedTime) {
|
24391 | this.consecutiveUpdates++;
|
24392 | } else {
|
24393 | this.consecutiveUpdates = 0;
|
24394 | this.lastRecordedTime = currentTime;
|
24395 | }
|
24396 | }
|
24397 | |
24398 |
|
24399 |
|
24400 |
|
24401 |
|
24402 |
|
24403 | ;
|
24404 |
|
24405 | _proto.cancelTimer_ = function cancelTimer_() {
|
24406 | this.consecutiveUpdates = 0;
|
24407 |
|
24408 | if (this.timer_) {
|
24409 | this.logger_('cancelTimer_');
|
24410 | clearTimeout(this.timer_);
|
24411 | }
|
24412 |
|
24413 | this.timer_ = null;
|
24414 | }
|
24415 | |
24416 |
|
24417 |
|
24418 |
|
24419 |
|
24420 |
|
24421 | ;
|
24422 |
|
24423 | _proto.fixesBadSeeks_ = function fixesBadSeeks_() {
|
24424 | var seeking = this.tech_.seeking();
|
24425 |
|
24426 | if (!seeking) {
|
24427 | return false;
|
24428 | }
|
24429 |
|
24430 |
|
24431 |
|
24432 |
|
24433 |
|
24434 | var seekable = this.seekable();
|
24435 | var currentTime = this.tech_.currentTime();
|
24436 | var isAfterSeekableRange = this.afterSeekableWindow_(seekable, currentTime, this.media(), this.allowSeeksWithinUnsafeLiveWindow);
|
24437 | var seekTo;
|
24438 |
|
24439 | if (isAfterSeekableRange) {
|
24440 | var seekableEnd = seekable.end(seekable.length - 1);
|
24441 |
|
24442 | seekTo = seekableEnd;
|
24443 | }
|
24444 |
|
24445 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
24446 | var seekableStart = seekable.start(0);
|
24447 |
|
24448 |
|
24449 | seekTo = seekableStart + (
|
24450 |
|
24451 | seekableStart === seekable.end(0) ? 0 : SAFE_TIME_DELTA);
|
24452 | }
|
24453 |
|
24454 | if (typeof seekTo !== 'undefined') {
|
24455 | this.logger_("Trying to seek outside of seekable at time " + currentTime + " with " + ("seekable range " + printableRange(seekable) + ". Seeking to ") + (seekTo + "."));
|
24456 | this.tech_.setCurrentTime(seekTo);
|
24457 | return true;
|
24458 | }
|
24459 |
|
24460 | var sourceUpdater = this.masterPlaylistController_.sourceUpdater_;
|
24461 | var buffered = this.tech_.buffered();
|
24462 | var audioBuffered = sourceUpdater.audioBuffer ? sourceUpdater.audioBuffered() : null;
|
24463 | var videoBuffered = sourceUpdater.videoBuffer ? sourceUpdater.videoBuffered() : null;
|
24464 |
|
24465 |
|
24466 | var twoSegmentDurations = (this.media().targetDuration - TIME_FUDGE_FACTOR) * 2;
|
24467 | var bufferedToCheck = [audioBuffered, videoBuffered];
|
24468 |
|
24469 | for (var i = 0; i < bufferedToCheck.length; i++) {
|
24470 |
|
24471 | if (!bufferedToCheck[i]) {
|
24472 | continue;
|
24473 | }
|
24474 |
|
24475 | var timeAhead = timeAheadOf(bufferedToCheck[i], currentTime);
|
24476 |
|
24477 |
|
24478 | if (timeAhead < twoSegmentDurations) {
|
24479 | return false;
|
24480 | }
|
24481 | }
|
24482 |
|
24483 | var nextRange = findNextRange(buffered, currentTime);
|
24484 |
|
24485 |
|
24486 | if (nextRange.length === 0) {
|
24487 | return false;
|
24488 | }
|
24489 |
|
24490 | seekTo = nextRange.start(0) + SAFE_TIME_DELTA;
|
24491 | this.logger_("Buffered region starts (" + nextRange.start(0) + ") " + (" just beyond seek point (" + currentTime + "). Seeking to " + seekTo + "."));
|
24492 | this.tech_.setCurrentTime(seekTo);
|
24493 | return true;
|
24494 | }
|
24495 | |
24496 |
|
24497 |
|
24498 |
|
24499 |
|
24500 | ;
|
24501 |
|
24502 | _proto.waiting_ = function waiting_() {
|
24503 | if (this.techWaiting_()) {
|
24504 | return;
|
24505 | }
|
24506 |
|
24507 |
|
24508 | var currentTime = this.tech_.currentTime();
|
24509 | var buffered = this.tech_.buffered();
|
24510 | var currentRange = findRange(buffered, currentTime);
|
24511 |
|
24512 |
|
24513 |
|
24514 |
|
24515 |
|
24516 |
|
24517 |
|
24518 |
|
24519 | if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
|
24520 | this.cancelTimer_();
|
24521 | this.tech_.setCurrentTime(currentTime);
|
24522 | this.logger_("Stopped at " + currentTime + " while inside a buffered region " + ("[" + currentRange.start(0) + " -> " + currentRange.end(0) + "]. Attempting to resume ") + 'playback by seeking to the current time.');
|
24523 |
|
24524 | this.tech_.trigger({
|
24525 | type: 'usage',
|
24526 | name: 'vhs-unknown-waiting'
|
24527 | });
|
24528 | this.tech_.trigger({
|
24529 | type: 'usage',
|
24530 | name: 'hls-unknown-waiting'
|
24531 | });
|
24532 | return;
|
24533 | }
|
24534 | }
|
24535 | |
24536 |
|
24537 |
|
24538 |
|
24539 |
|
24540 |
|
24541 |
|
24542 |
|
24543 | ;
|
24544 |
|
24545 | _proto.techWaiting_ = function techWaiting_() {
|
24546 | var seekable = this.seekable();
|
24547 | var currentTime = this.tech_.currentTime();
|
24548 |
|
24549 | if (this.tech_.seeking() || this.timer_ !== null) {
|
24550 |
|
24551 | return true;
|
24552 | }
|
24553 |
|
24554 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
24555 | var livePoint = seekable.end(seekable.length - 1);
|
24556 | this.logger_("Fell out of live window at time " + currentTime + ". Seeking to " + ("live point (seekable end) " + livePoint));
|
24557 | this.cancelTimer_();
|
24558 | this.tech_.setCurrentTime(livePoint);
|
24559 |
|
24560 | this.tech_.trigger({
|
24561 | type: 'usage',
|
24562 | name: 'vhs-live-resync'
|
24563 | });
|
24564 | this.tech_.trigger({
|
24565 | type: 'usage',
|
24566 | name: 'hls-live-resync'
|
24567 | });
|
24568 | return true;
|
24569 | }
|
24570 |
|
24571 | var sourceUpdater = this.tech_.vhs.masterPlaylistController_.sourceUpdater_;
|
24572 | var buffered = this.tech_.buffered();
|
24573 | var videoUnderflow = this.videoUnderflow_({
|
24574 | audioBuffered: sourceUpdater.audioBuffered(),
|
24575 | videoBuffered: sourceUpdater.videoBuffered(),
|
24576 | currentTime: currentTime
|
24577 | });
|
24578 |
|
24579 | if (videoUnderflow) {
|
24580 |
|
24581 |
|
24582 |
|
24583 |
|
24584 | this.cancelTimer_();
|
24585 | this.tech_.setCurrentTime(currentTime);
|
24586 |
|
24587 | this.tech_.trigger({
|
24588 | type: 'usage',
|
24589 | name: 'vhs-video-underflow'
|
24590 | });
|
24591 | this.tech_.trigger({
|
24592 | type: 'usage',
|
24593 | name: 'hls-video-underflow'
|
24594 | });
|
24595 | return true;
|
24596 | }
|
24597 |
|
24598 | var nextRange = findNextRange(buffered, currentTime);
|
24599 |
|
24600 | if (nextRange.length > 0) {
|
24601 | var difference = nextRange.start(0) - currentTime;
|
24602 | this.logger_("Stopped at " + currentTime + ", setting timer for " + difference + ", seeking " + ("to " + nextRange.start(0)));
|
24603 | this.cancelTimer_();
|
24604 | this.timer_ = setTimeout(this.skipTheGap_.bind(this), difference * 1000, currentTime);
|
24605 | return true;
|
24606 | }
|
24607 |
|
24608 |
|
24609 | return false;
|
24610 | };
|
24611 |
|
24612 | _proto.afterSeekableWindow_ = function afterSeekableWindow_(seekable, currentTime, playlist, allowSeeksWithinUnsafeLiveWindow) {
|
24613 | if (allowSeeksWithinUnsafeLiveWindow === void 0) {
|
24614 | allowSeeksWithinUnsafeLiveWindow = false;
|
24615 | }
|
24616 |
|
24617 | if (!seekable.length) {
|
24618 |
|
24619 | return false;
|
24620 | }
|
24621 |
|
24622 | var allowedEnd = seekable.end(seekable.length - 1) + SAFE_TIME_DELTA;
|
24623 | var isLive = !playlist.endList;
|
24624 |
|
24625 | if (isLive && allowSeeksWithinUnsafeLiveWindow) {
|
24626 | allowedEnd = seekable.end(seekable.length - 1) + playlist.targetDuration * 3;
|
24627 | }
|
24628 |
|
24629 | if (currentTime > allowedEnd) {
|
24630 | return true;
|
24631 | }
|
24632 |
|
24633 | return false;
|
24634 | };
|
24635 |
|
24636 | _proto.beforeSeekableWindow_ = function beforeSeekableWindow_(seekable, currentTime) {
|
24637 | if (seekable.length &&
|
24638 | seekable.start(0) > 0 && currentTime < seekable.start(0) - this.liveRangeSafeTimeDelta) {
|
24639 | return true;
|
24640 | }
|
24641 |
|
24642 | return false;
|
24643 | };
|
24644 |
|
24645 | _proto.videoUnderflow_ = function videoUnderflow_(_ref) {
|
24646 | var videoBuffered = _ref.videoBuffered,
|
24647 | audioBuffered = _ref.audioBuffered,
|
24648 | currentTime = _ref.currentTime;
|
24649 |
|
24650 |
|
24651 | if (!videoBuffered) {
|
24652 | return;
|
24653 | }
|
24654 |
|
24655 | var gap;
|
24656 |
|
24657 | if (videoBuffered.length && audioBuffered.length) {
|
24658 |
|
24659 |
|
24660 |
|
24661 | var lastVideoRange = findRange(videoBuffered, currentTime - 3);
|
24662 | var videoRange = findRange(videoBuffered, currentTime);
|
24663 | var audioRange = findRange(audioBuffered, currentTime);
|
24664 |
|
24665 | if (audioRange.length && !videoRange.length && lastVideoRange.length) {
|
24666 | gap = {
|
24667 | start: lastVideoRange.end(0),
|
24668 | end: audioRange.end(0)
|
24669 | };
|
24670 | }
|
24671 |
|
24672 | } else {
|
24673 | var nextRange = findNextRange(videoBuffered, currentTime);
|
24674 |
|
24675 |
|
24676 | if (!nextRange.length) {
|
24677 | gap = this.gapFromVideoUnderflow_(videoBuffered, currentTime);
|
24678 | }
|
24679 | }
|
24680 |
|
24681 | if (gap) {
|
24682 | this.logger_("Encountered a gap in video from " + gap.start + " to " + gap.end + ". " + ("Seeking to current time " + currentTime));
|
24683 | return true;
|
24684 | }
|
24685 |
|
24686 | return false;
|
24687 | }
|
24688 | |
24689 |
|
24690 |
|
24691 |
|
24692 |
|
24693 |
|
24694 | ;
|
24695 |
|
24696 | _proto.skipTheGap_ = function skipTheGap_(scheduledCurrentTime) {
|
24697 | var buffered = this.tech_.buffered();
|
24698 | var currentTime = this.tech_.currentTime();
|
24699 | var nextRange = findNextRange(buffered, currentTime);
|
24700 | this.cancelTimer_();
|
24701 |
|
24702 | if (nextRange.length === 0 || currentTime !== scheduledCurrentTime) {
|
24703 | return;
|
24704 | }
|
24705 |
|
24706 | this.logger_('skipTheGap_:', 'currentTime:', currentTime, 'scheduled currentTime:', scheduledCurrentTime, 'nextRange start:', nextRange.start(0));
|
24707 |
|
24708 | this.tech_.setCurrentTime(nextRange.start(0) + TIME_FUDGE_FACTOR);
|
24709 | this.tech_.trigger({
|
24710 | type: 'usage',
|
24711 | name: 'vhs-gap-skip'
|
24712 | });
|
24713 | this.tech_.trigger({
|
24714 | type: 'usage',
|
24715 | name: 'hls-gap-skip'
|
24716 | });
|
24717 | };
|
24718 |
|
24719 | _proto.gapFromVideoUnderflow_ = function gapFromVideoUnderflow_(buffered, currentTime) {
|
24720 |
|
24721 |
|
24722 |
|
24723 |
|
24724 |
|
24725 |
|
24726 |
|
24727 |
|
24728 |
|
24729 |
|
24730 |
|
24731 |
|
24732 |
|
24733 |
|
24734 |
|
24735 |
|
24736 |
|
24737 |
|
24738 |
|
24739 |
|
24740 |
|
24741 |
|
24742 | var gaps = findGaps(buffered);
|
24743 |
|
24744 | for (var i = 0; i < gaps.length; i++) {
|
24745 | var start = gaps.start(i);
|
24746 | var end = gaps.end(i);
|
24747 |
|
24748 | if (currentTime - start < 4 && currentTime - start > 2) {
|
24749 | return {
|
24750 | start: start,
|
24751 | end: end
|
24752 | };
|
24753 | }
|
24754 | }
|
24755 |
|
24756 | return null;
|
24757 | };
|
24758 |
|
24759 | return PlaybackWatcher;
|
24760 | }();
|
24761 |
|
24762 | var defaultOptions = {
|
24763 | errorInterval: 30,
|
24764 | getSource: function getSource(next) {
|
24765 | var tech = this.tech({
|
24766 | IWillNotUseThisInPlugins: true
|
24767 | });
|
24768 | var sourceObj = tech.currentSource_ || this.currentSource();
|
24769 | return next(sourceObj);
|
24770 | }
|
24771 | };
|
24772 |
|
24773 |
|
24774 |
|
24775 |
|
24776 |
|
24777 |
|
24778 |
|
24779 |
|
24780 | var initPlugin = function initPlugin(player, options) {
|
24781 | var lastCalled = 0;
|
24782 | var seekTo = 0;
|
24783 | var localOptions = videojs.mergeOptions(defaultOptions, options);
|
24784 | player.ready(function () {
|
24785 | player.trigger({
|
24786 | type: 'usage',
|
24787 | name: 'vhs-error-reload-initialized'
|
24788 | });
|
24789 | player.trigger({
|
24790 | type: 'usage',
|
24791 | name: 'hls-error-reload-initialized'
|
24792 | });
|
24793 | });
|
24794 | |
24795 |
|
24796 |
|
24797 |
|
24798 |
|
24799 |
|
24800 |
|
24801 | var loadedMetadataHandler = function loadedMetadataHandler() {
|
24802 | if (seekTo) {
|
24803 | player.currentTime(seekTo);
|
24804 | }
|
24805 | };
|
24806 | |
24807 |
|
24808 |
|
24809 |
|
24810 |
|
24811 |
|
24812 |
|
24813 |
|
24814 | var setSource = function setSource(sourceObj) {
|
24815 | if (sourceObj === null || sourceObj === undefined) {
|
24816 | return;
|
24817 | }
|
24818 |
|
24819 | seekTo = player.duration() !== Infinity && player.currentTime() || 0;
|
24820 | player.one('loadedmetadata', loadedMetadataHandler);
|
24821 | player.src(sourceObj);
|
24822 | player.trigger({
|
24823 | type: 'usage',
|
24824 | name: 'vhs-error-reload'
|
24825 | });
|
24826 | player.trigger({
|
24827 | type: 'usage',
|
24828 | name: 'hls-error-reload'
|
24829 | });
|
24830 | player.play();
|
24831 | };
|
24832 | |
24833 |
|
24834 |
|
24835 |
|
24836 |
|
24837 |
|
24838 |
|
24839 |
|
24840 | var errorHandler = function errorHandler() {
|
24841 |
|
24842 |
|
24843 | if (Date.now() - lastCalled < localOptions.errorInterval * 1000) {
|
24844 | player.trigger({
|
24845 | type: 'usage',
|
24846 | name: 'vhs-error-reload-canceled'
|
24847 | });
|
24848 | player.trigger({
|
24849 | type: 'usage',
|
24850 | name: 'hls-error-reload-canceled'
|
24851 | });
|
24852 | return;
|
24853 | }
|
24854 |
|
24855 | if (!localOptions.getSource || typeof localOptions.getSource !== 'function') {
|
24856 | videojs.log.error('ERROR: reloadSourceOnError - The option getSource must be a function!');
|
24857 | return;
|
24858 | }
|
24859 |
|
24860 | lastCalled = Date.now();
|
24861 | return localOptions.getSource.call(player, setSource);
|
24862 | };
|
24863 | |
24864 |
|
24865 |
|
24866 |
|
24867 |
|
24868 |
|
24869 |
|
24870 | var cleanupEvents = function cleanupEvents() {
|
24871 | player.off('loadedmetadata', loadedMetadataHandler);
|
24872 | player.off('error', errorHandler);
|
24873 | player.off('dispose', cleanupEvents);
|
24874 | };
|
24875 | |
24876 |
|
24877 |
|
24878 |
|
24879 |
|
24880 |
|
24881 |
|
24882 |
|
24883 | var reinitPlugin = function reinitPlugin(newOptions) {
|
24884 | cleanupEvents();
|
24885 | initPlugin(player, newOptions);
|
24886 | };
|
24887 |
|
24888 | player.on('error', errorHandler);
|
24889 | player.on('dispose', cleanupEvents);
|
24890 |
|
24891 |
|
24892 | player.reloadSourceOnError = reinitPlugin;
|
24893 | };
|
24894 |
|
24895 |
|
24896 |
|
24897 |
|
24898 |
|
24899 |
|
24900 |
|
24901 |
|
24902 | var reloadSourceOnError = function reloadSourceOnError(options) {
|
24903 | initPlugin(this, options);
|
24904 | };
|
24905 |
|
24906 | var version$4 = "2.11.1";
|
24907 |
|
24908 | var version$3 = "5.14.1";
|
24909 |
|
24910 | var version$2 = "0.19.2";
|
24911 |
|
24912 | var version$1 = "4.7.0";
|
24913 |
|
24914 | var version = "3.1.2";
|
24915 |
|
24916 | var Vhs = {
|
24917 | PlaylistLoader: PlaylistLoader,
|
24918 | Playlist: Playlist,
|
24919 | utils: utils,
|
24920 | STANDARD_PLAYLIST_SELECTOR: lastBandwidthSelector,
|
24921 | INITIAL_PLAYLIST_SELECTOR: lowestBitrateCompatibleVariantSelector,
|
24922 | lastBandwidthSelector: lastBandwidthSelector,
|
24923 | movingAverageBandwidthSelector: movingAverageBandwidthSelector,
|
24924 | comparePlaylistBandwidth: comparePlaylistBandwidth,
|
24925 | comparePlaylistResolution: comparePlaylistResolution,
|
24926 | xhr: xhrFactory()
|
24927 | };
|
24928 |
|
24929 | Object.keys(Config).forEach(function (prop) {
|
24930 | Object.defineProperty(Vhs, prop, {
|
24931 | get: function get() {
|
24932 | videojs.log.warn("using Vhs." + prop + " is UNSAFE be sure you know what you are doing");
|
24933 | return Config[prop];
|
24934 | },
|
24935 | set: function set(value) {
|
24936 | videojs.log.warn("using Vhs." + prop + " is UNSAFE be sure you know what you are doing");
|
24937 |
|
24938 | if (typeof value !== 'number' || value < 0) {
|
24939 | videojs.log.warn("value of Vhs." + prop + " must be greater than or equal to 0");
|
24940 | return;
|
24941 | }
|
24942 |
|
24943 | Config[prop] = value;
|
24944 | }
|
24945 | });
|
24946 | });
|
24947 | var LOCAL_STORAGE_KEY = 'videojs-vhs';
|
24948 |
|
24949 |
|
24950 |
|
24951 |
|
24952 |
|
24953 |
|
24954 |
|
24955 |
|
24956 | var handleVhsMediaChange = function handleVhsMediaChange(qualityLevels, playlistLoader) {
|
24957 | var newPlaylist = playlistLoader.media();
|
24958 | var selectedIndex = -1;
|
24959 |
|
24960 | for (var i = 0; i < qualityLevels.length; i++) {
|
24961 | if (qualityLevels[i].id === newPlaylist.id) {
|
24962 | selectedIndex = i;
|
24963 | break;
|
24964 | }
|
24965 | }
|
24966 |
|
24967 | qualityLevels.selectedIndex_ = selectedIndex;
|
24968 | qualityLevels.trigger({
|
24969 | selectedIndex: selectedIndex,
|
24970 | type: 'change'
|
24971 | });
|
24972 | };
|
24973 |
|
24974 |
|
24975 |
|
24976 |
|
24977 |
|
24978 |
|
24979 |
|
24980 |
|
24981 |
|
24982 | var handleVhsLoadedMetadata = function handleVhsLoadedMetadata(qualityLevels, vhs) {
|
24983 | vhs.representations().forEach(function (rep) {
|
24984 | qualityLevels.addQualityLevel(rep);
|
24985 | });
|
24986 | handleVhsMediaChange(qualityLevels, vhs.playlists);
|
24987 | };
|
24988 |
|
24989 |
|
24990 |
|
24991 | Vhs.canPlaySource = function () {
|
24992 | return videojs.log.warn('HLS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
|
24993 | };
|
24994 |
|
24995 | var emeKeySystems = function emeKeySystems(keySystemOptions, mainPlaylist, audioPlaylist) {
|
24996 | if (!keySystemOptions) {
|
24997 | return keySystemOptions;
|
24998 | }
|
24999 |
|
25000 | var codecs = {};
|
25001 |
|
25002 | if (mainPlaylist && mainPlaylist.attributes && mainPlaylist.attributes.CODECS) {
|
25003 | codecs = unwrapCodecList(parseCodecs(mainPlaylist.attributes.CODECS));
|
25004 | }
|
25005 |
|
25006 | if (audioPlaylist && audioPlaylist.attributes && audioPlaylist.attributes.CODECS) {
|
25007 | codecs.audio = audioPlaylist.attributes.CODECS;
|
25008 | }
|
25009 |
|
25010 | var videoContentType = getMimeForCodec(codecs.video);
|
25011 | var audioContentType = getMimeForCodec(codecs.audio);
|
25012 |
|
25013 | var keySystemContentTypes = {};
|
25014 |
|
25015 | for (var keySystem in keySystemOptions) {
|
25016 | keySystemContentTypes[keySystem] = {};
|
25017 |
|
25018 | if (audioContentType) {
|
25019 | keySystemContentTypes[keySystem].audioContentType = audioContentType;
|
25020 | }
|
25021 |
|
25022 | if (videoContentType) {
|
25023 | keySystemContentTypes[keySystem].videoContentType = videoContentType;
|
25024 | }
|
25025 |
|
25026 |
|
25027 |
|
25028 |
|
25029 |
|
25030 |
|
25031 |
|
25032 | if (mainPlaylist.contentProtection && mainPlaylist.contentProtection[keySystem] && mainPlaylist.contentProtection[keySystem].pssh) {
|
25033 | keySystemContentTypes[keySystem].pssh = mainPlaylist.contentProtection[keySystem].pssh;
|
25034 | }
|
25035 |
|
25036 |
|
25037 |
|
25038 | if (typeof keySystemOptions[keySystem] === 'string') {
|
25039 | keySystemContentTypes[keySystem].url = keySystemOptions[keySystem];
|
25040 | }
|
25041 | }
|
25042 |
|
25043 | return videojs.mergeOptions(keySystemOptions, keySystemContentTypes);
|
25044 | };
|
25045 |
|
25046 |
|
25047 |
|
25048 |
|
25049 |
|
25050 |
|
25051 |
|
25052 |
|
25053 |
|
25054 |
|
25055 |
|
25056 |
|
25057 |
|
25058 |
|
25059 |
|
25060 |
|
25061 |
|
25062 |
|
25063 |
|
25064 |
|
25065 |
|
25066 |
|
25067 |
|
25068 |
|
25069 |
|
25070 | var getAllPsshKeySystemsOptions = function getAllPsshKeySystemsOptions(playlists, keySystems) {
|
25071 | return playlists.reduce(function (keySystemsArr, playlist) {
|
25072 | if (!playlist.contentProtection) {
|
25073 | return keySystemsArr;
|
25074 | }
|
25075 |
|
25076 | var keySystemsOptions = keySystems.reduce(function (keySystemsObj, keySystem) {
|
25077 | var keySystemOptions = playlist.contentProtection[keySystem];
|
25078 |
|
25079 | if (keySystemOptions && keySystemOptions.pssh) {
|
25080 | keySystemsObj[keySystem] = {
|
25081 | pssh: keySystemOptions.pssh
|
25082 | };
|
25083 | }
|
25084 |
|
25085 | return keySystemsObj;
|
25086 | }, {});
|
25087 |
|
25088 | if (Object.keys(keySystemsOptions).length) {
|
25089 | keySystemsArr.push(keySystemsOptions);
|
25090 | }
|
25091 |
|
25092 | return keySystemsArr;
|
25093 | }, []);
|
25094 | };
|
25095 |
|
25096 |
|
25097 |
|
25098 |
|
25099 |
|
25100 |
|
25101 |
|
25102 |
|
25103 |
|
25104 |
|
25105 |
|
25106 |
|
25107 |
|
25108 |
|
25109 |
|
25110 |
|
25111 |
|
25112 |
|
25113 |
|
25114 |
|
25115 |
|
25116 |
|
25117 |
|
25118 |
|
25119 |
|
25120 |
|
25121 | var waitForKeySessionCreation = function waitForKeySessionCreation(_ref) {
|
25122 | var player = _ref.player,
|
25123 | sourceKeySystems = _ref.sourceKeySystems,
|
25124 | audioMedia = _ref.audioMedia,
|
25125 | mainPlaylists = _ref.mainPlaylists;
|
25126 |
|
25127 | if (!player.eme.initializeMediaKeys) {
|
25128 | return Promise.resolve();
|
25129 | }
|
25130 |
|
25131 |
|
25132 |
|
25133 |
|
25134 |
|
25135 |
|
25136 |
|
25137 |
|
25138 |
|
25139 | var playlists = audioMedia ? mainPlaylists.concat([audioMedia]) : mainPlaylists;
|
25140 | var keySystemsOptionsArr = getAllPsshKeySystemsOptions(playlists, Object.keys(sourceKeySystems));
|
25141 | var initializationFinishedPromises = [];
|
25142 | var keySessionCreatedPromises = [];
|
25143 |
|
25144 |
|
25145 |
|
25146 |
|
25147 |
|
25148 | keySystemsOptionsArr.forEach(function (keySystemsOptions) {
|
25149 | keySessionCreatedPromises.push(new Promise(function (resolve, reject) {
|
25150 | player.tech_.one('keysessioncreated', resolve);
|
25151 | }));
|
25152 | initializationFinishedPromises.push(new Promise(function (resolve, reject) {
|
25153 | player.eme.initializeMediaKeys({
|
25154 | keySystems: keySystemsOptions
|
25155 | }, function (err) {
|
25156 | if (err) {
|
25157 | reject(err);
|
25158 | return;
|
25159 | }
|
25160 |
|
25161 | resolve();
|
25162 | });
|
25163 | }));
|
25164 | });
|
25165 |
|
25166 |
|
25167 |
|
25168 |
|
25169 |
|
25170 |
|
25171 | return Promise.race([
|
25172 |
|
25173 |
|
25174 | Promise.all(initializationFinishedPromises),
|
25175 | Promise.race(keySessionCreatedPromises)]);
|
25176 | };
|
25177 |
|
25178 |
|
25179 |
|
25180 |
|
25181 |
|
25182 |
|
25183 |
|
25184 |
|
25185 |
|
25186 |
|
25187 |
|
25188 |
|
25189 |
|
25190 |
|
25191 |
|
25192 |
|
25193 |
|
25194 |
|
25195 | var setupEmeOptions = function setupEmeOptions(_ref2) {
|
25196 | var player = _ref2.player,
|
25197 | sourceKeySystems = _ref2.sourceKeySystems,
|
25198 | media = _ref2.media,
|
25199 | audioMedia = _ref2.audioMedia;
|
25200 | var sourceOptions = emeKeySystems(sourceKeySystems, media, audioMedia);
|
25201 |
|
25202 | if (!sourceOptions) {
|
25203 | return false;
|
25204 | }
|
25205 |
|
25206 | player.currentSource().keySystems = sourceOptions;
|
25207 |
|
25208 |
|
25209 | if (sourceOptions && !player.eme) {
|
25210 | videojs.log.warn('DRM encrypted source cannot be decrypted without a DRM plugin');
|
25211 | return false;
|
25212 | }
|
25213 |
|
25214 | return true;
|
25215 | };
|
25216 |
|
25217 | var getVhsLocalStorage = function getVhsLocalStorage() {
|
25218 | if (!window.localStorage) {
|
25219 | return null;
|
25220 | }
|
25221 |
|
25222 | var storedObject = window.localStorage.getItem(LOCAL_STORAGE_KEY);
|
25223 |
|
25224 | if (!storedObject) {
|
25225 | return null;
|
25226 | }
|
25227 |
|
25228 | try {
|
25229 | return JSON.parse(storedObject);
|
25230 | } catch (e) {
|
25231 |
|
25232 | return null;
|
25233 | }
|
25234 | };
|
25235 |
|
25236 | var updateVhsLocalStorage = function updateVhsLocalStorage(options) {
|
25237 | if (!window.localStorage) {
|
25238 | return false;
|
25239 | }
|
25240 |
|
25241 | var objectToStore = getVhsLocalStorage();
|
25242 | objectToStore = objectToStore ? videojs.mergeOptions(objectToStore, options) : options;
|
25243 |
|
25244 | try {
|
25245 | window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(objectToStore));
|
25246 | } catch (e) {
|
25247 |
|
25248 |
|
25249 |
|
25250 |
|
25251 | return false;
|
25252 | }
|
25253 |
|
25254 | return objectToStore;
|
25255 | };
|
25256 |
|
25257 |
|
25258 |
|
25259 |
|
25260 |
|
25261 |
|
25262 |
|
25263 |
|
25264 |
|
25265 |
|
25266 |
|
25267 |
|
25268 |
|
25269 |
|
25270 | var expandDataUri = function expandDataUri(dataUri) {
|
25271 | if (dataUri.toLowerCase().indexOf('data:application/vnd.videojs.vhs+json,') === 0) {
|
25272 | return JSON.parse(dataUri.substring(dataUri.indexOf(',') + 1));
|
25273 | }
|
25274 |
|
25275 |
|
25276 | return dataUri;
|
25277 | };
|
25278 |
|
25279 |
|
25280 |
|
25281 |
|
25282 |
|
25283 | Vhs.supportsNativeHls = function () {
|
25284 | if (!document || !document.createElement) {
|
25285 | return false;
|
25286 | }
|
25287 |
|
25288 | var video = document.createElement('video');
|
25289 |
|
25290 | if (!videojs.getTech('Html5').isSupported()) {
|
25291 | return false;
|
25292 | }
|
25293 |
|
25294 |
|
25295 | var canPlay = [
|
25296 | 'application/vnd.apple.mpegurl',
|
25297 | 'audio/mpegurl',
|
25298 | 'audio/x-mpegurl',
|
25299 | 'application/x-mpegurl',
|
25300 | 'video/x-mpegurl', 'video/mpegurl', 'application/mpegurl'];
|
25301 | return canPlay.some(function (canItPlay) {
|
25302 | return /maybe|probably/i.test(video.canPlayType(canItPlay));
|
25303 | });
|
25304 | }();
|
25305 |
|
25306 | Vhs.supportsNativeDash = function () {
|
25307 | if (!document || !document.createElement || !videojs.getTech('Html5').isSupported()) {
|
25308 | return false;
|
25309 | }
|
25310 |
|
25311 | return /maybe|probably/i.test(document.createElement('video').canPlayType('application/dash+xml'));
|
25312 | }();
|
25313 |
|
25314 | Vhs.supportsTypeNatively = function (type) {
|
25315 | if (type === 'hls') {
|
25316 | return Vhs.supportsNativeHls;
|
25317 | }
|
25318 |
|
25319 | if (type === 'dash') {
|
25320 | return Vhs.supportsNativeDash;
|
25321 | }
|
25322 |
|
25323 | return false;
|
25324 | };
|
25325 |
|
25326 |
|
25327 |
|
25328 |
|
25329 |
|
25330 |
|
25331 | Vhs.isSupported = function () {
|
25332 | return videojs.log.warn('HLS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
|
25333 | };
|
25334 |
|
25335 | var Component = videojs.getComponent('Component');
|
25336 |
|
25337 |
|
25338 |
|
25339 |
|
25340 |
|
25341 |
|
25342 |
|
25343 |
|
25344 |
|
25345 |
|
25346 |
|
25347 | var VhsHandler = function (_Component) {
|
25348 | _inheritsLoose(VhsHandler, _Component);
|
25349 |
|
25350 | function VhsHandler(source, tech, options) {
|
25351 | var _this;
|
25352 |
|
25353 | _this = _Component.call(this, tech, videojs.mergeOptions(options.hls, options.vhs)) || this;
|
25354 |
|
25355 | if (options.hls && Object.keys(options.hls).length) {
|
25356 | videojs.log.warn('Using hls options is deprecated. Use vhs instead.');
|
25357 | }
|
25358 |
|
25359 |
|
25360 |
|
25361 | if (typeof options.initialBandwidth === 'number') {
|
25362 | _this.options_.bandwidth = options.initialBandwidth;
|
25363 | }
|
25364 |
|
25365 | _this.logger_ = logger('VhsHandler');
|
25366 |
|
25367 |
|
25368 | if (tech.options_ && tech.options_.playerId) {
|
25369 | var _player = videojs(tech.options_.playerId);
|
25370 |
|
25371 | if (!_player.hasOwnProperty('hls')) {
|
25372 | Object.defineProperty(_player, 'hls', {
|
25373 | get: function get() {
|
25374 | videojs.log.warn('player.hls is deprecated. Use player.tech().vhs instead.');
|
25375 | tech.trigger({
|
25376 | type: 'usage',
|
25377 | name: 'hls-player-access'
|
25378 | });
|
25379 | return _assertThisInitialized(_this);
|
25380 | },
|
25381 | configurable: true
|
25382 | });
|
25383 | }
|
25384 |
|
25385 | if (!_player.hasOwnProperty('vhs')) {
|
25386 | Object.defineProperty(_player, 'vhs', {
|
25387 | get: function get() {
|
25388 | videojs.log.warn('player.vhs is deprecated. Use player.tech().vhs instead.');
|
25389 | tech.trigger({
|
25390 | type: 'usage',
|
25391 | name: 'vhs-player-access'
|
25392 | });
|
25393 | return _assertThisInitialized(_this);
|
25394 | },
|
25395 | configurable: true
|
25396 | });
|
25397 | }
|
25398 |
|
25399 | if (!_player.hasOwnProperty('dash')) {
|
25400 | Object.defineProperty(_player, 'dash', {
|
25401 | get: function get() {
|
25402 | videojs.log.warn('player.dash is deprecated. Use player.tech().vhs instead.');
|
25403 | return _assertThisInitialized(_this);
|
25404 | },
|
25405 | configurable: true
|
25406 | });
|
25407 | }
|
25408 |
|
25409 | _this.player_ = _player;
|
25410 | }
|
25411 |
|
25412 | _this.tech_ = tech;
|
25413 | _this.source_ = source;
|
25414 | _this.stats = {};
|
25415 | _this.ignoreNextSeekingEvent_ = false;
|
25416 |
|
25417 | _this.setOptions_();
|
25418 |
|
25419 | if (_this.options_.overrideNative && tech.overrideNativeAudioTracks && tech.overrideNativeVideoTracks) {
|
25420 | tech.overrideNativeAudioTracks(true);
|
25421 | tech.overrideNativeVideoTracks(true);
|
25422 | } else if (_this.options_.overrideNative && (tech.featuresNativeVideoTracks || tech.featuresNativeAudioTracks)) {
|
25423 |
|
25424 |
|
25425 | throw new Error('Overriding native HLS requires emulated tracks. ' + 'See https://git.io/vMpjB');
|
25426 | }
|
25427 |
|
25428 |
|
25429 |
|
25430 | _this.on(document, ['fullscreenchange', 'webkitfullscreenchange', 'mozfullscreenchange', 'MSFullscreenChange'], function (event) {
|
25431 | var fullscreenElement = document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement;
|
25432 |
|
25433 | if (fullscreenElement && fullscreenElement.contains(_this.tech_.el())) {
|
25434 | _this.masterPlaylistController_.fastQualityChange_();
|
25435 | } else {
|
25436 |
|
25437 |
|
25438 |
|
25439 | _this.masterPlaylistController_.checkABR_();
|
25440 | }
|
25441 | });
|
25442 |
|
25443 | _this.on(_this.tech_, 'seeking', function () {
|
25444 | if (this.ignoreNextSeekingEvent_) {
|
25445 | this.ignoreNextSeekingEvent_ = false;
|
25446 | return;
|
25447 | }
|
25448 |
|
25449 | this.setCurrentTime(this.tech_.currentTime());
|
25450 | });
|
25451 |
|
25452 | _this.on(_this.tech_, 'error', function () {
|
25453 |
|
25454 |
|
25455 | if (this.tech_.error() && this.masterPlaylistController_) {
|
25456 | this.masterPlaylistController_.pauseLoading();
|
25457 | }
|
25458 | });
|
25459 |
|
25460 | _this.on(_this.tech_, 'play', _this.play);
|
25461 |
|
25462 | return _this;
|
25463 | }
|
25464 |
|
25465 | var _proto = VhsHandler.prototype;
|
25466 |
|
25467 | _proto.setOptions_ = function setOptions_() {
|
25468 | var _this2 = this;
|
25469 |
|
25470 |
|
25471 | this.options_.withCredentials = this.options_.withCredentials || false;
|
25472 | this.options_.handleManifestRedirects = this.options_.handleManifestRedirects === false ? false : true;
|
25473 | this.options_.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions === false ? false : true;
|
25474 | this.options_.useDevicePixelRatio = this.options_.useDevicePixelRatio || false;
|
25475 | this.options_.smoothQualityChange = this.options_.smoothQualityChange || false;
|
25476 | this.options_.useBandwidthFromLocalStorage = typeof this.source_.useBandwidthFromLocalStorage !== 'undefined' ? this.source_.useBandwidthFromLocalStorage : this.options_.useBandwidthFromLocalStorage || false;
|
25477 | this.options_.customTagParsers = this.options_.customTagParsers || [];
|
25478 | this.options_.customTagMappers = this.options_.customTagMappers || [];
|
25479 | this.options_.cacheEncryptionKeys = this.options_.cacheEncryptionKeys || false;
|
25480 |
|
25481 | if (typeof this.options_.blacklistDuration !== 'number') {
|
25482 | this.options_.blacklistDuration = 5 * 60;
|
25483 | }
|
25484 |
|
25485 | if (typeof this.options_.bandwidth !== 'number') {
|
25486 | if (this.options_.useBandwidthFromLocalStorage) {
|
25487 | var storedObject = getVhsLocalStorage();
|
25488 |
|
25489 | if (storedObject && storedObject.bandwidth) {
|
25490 | this.options_.bandwidth = storedObject.bandwidth;
|
25491 | this.tech_.trigger({
|
25492 | type: 'usage',
|
25493 | name: 'vhs-bandwidth-from-local-storage'
|
25494 | });
|
25495 | this.tech_.trigger({
|
25496 | type: 'usage',
|
25497 | name: 'hls-bandwidth-from-local-storage'
|
25498 | });
|
25499 | }
|
25500 |
|
25501 | if (storedObject && storedObject.throughput) {
|
25502 | this.options_.throughput = storedObject.throughput;
|
25503 | this.tech_.trigger({
|
25504 | type: 'usage',
|
25505 | name: 'vhs-throughput-from-local-storage'
|
25506 | });
|
25507 | this.tech_.trigger({
|
25508 | type: 'usage',
|
25509 | name: 'hls-throughput-from-local-storage'
|
25510 | });
|
25511 | }
|
25512 | }
|
25513 | }
|
25514 |
|
25515 |
|
25516 |
|
25517 | if (typeof this.options_.bandwidth !== 'number') {
|
25518 | this.options_.bandwidth = Config.INITIAL_BANDWIDTH;
|
25519 | }
|
25520 |
|
25521 |
|
25522 |
|
25523 | this.options_.enableLowInitialPlaylist = this.options_.enableLowInitialPlaylist && this.options_.bandwidth === Config.INITIAL_BANDWIDTH;
|
25524 |
|
25525 | ['withCredentials', 'useDevicePixelRatio', 'limitRenditionByPlayerDimensions', 'bandwidth', 'smoothQualityChange', 'customTagParsers', 'customTagMappers', 'handleManifestRedirects', 'cacheEncryptionKeys', 'playlistSelector', 'initialPlaylistSelector', 'experimentalBufferBasedABR', 'liveRangeSafeTimeDelta', 'experimentalLLHLS', 'experimentalExactManifestTimings', 'experimentalLeastPixelDiffSelector'].forEach(function (option) {
|
25526 | if (typeof _this2.source_[option] !== 'undefined') {
|
25527 | _this2.options_[option] = _this2.source_[option];
|
25528 | }
|
25529 | });
|
25530 | this.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions;
|
25531 | this.useDevicePixelRatio = this.options_.useDevicePixelRatio;
|
25532 | }
|
25533 | |
25534 |
|
25535 |
|
25536 |
|
25537 |
|
25538 | ;
|
25539 |
|
25540 | _proto.src = function src(_src, type) {
|
25541 | var _this3 = this;
|
25542 |
|
25543 |
|
25544 | if (!_src) {
|
25545 | return;
|
25546 | }
|
25547 |
|
25548 | this.setOptions_();
|
25549 |
|
25550 | this.options_.src = expandDataUri(this.source_.src);
|
25551 | this.options_.tech = this.tech_;
|
25552 | this.options_.externVhs = Vhs;
|
25553 | this.options_.sourceType = simpleTypeFromSourceType(type);
|
25554 |
|
25555 | this.options_.seekTo = function (time) {
|
25556 | _this3.tech_.setCurrentTime(time);
|
25557 | };
|
25558 |
|
25559 | if (this.options_.smoothQualityChange) {
|
25560 | videojs.log.warn('smoothQualityChange is deprecated and will be removed in the next major version');
|
25561 | }
|
25562 |
|
25563 | this.masterPlaylistController_ = new MasterPlaylistController(this.options_);
|
25564 | var playbackWatcherOptions = videojs.mergeOptions({
|
25565 | liveRangeSafeTimeDelta: SAFE_TIME_DELTA
|
25566 | }, this.options_, {
|
25567 | seekable: function seekable() {
|
25568 | return _this3.seekable();
|
25569 | },
|
25570 | media: function media() {
|
25571 | return _this3.masterPlaylistController_.media();
|
25572 | },
|
25573 | masterPlaylistController: this.masterPlaylistController_
|
25574 | });
|
25575 | this.playbackWatcher_ = new PlaybackWatcher(playbackWatcherOptions);
|
25576 | this.masterPlaylistController_.on('error', function () {
|
25577 | var player = videojs.players[_this3.tech_.options_.playerId];
|
25578 | var error = _this3.masterPlaylistController_.error;
|
25579 |
|
25580 | if (typeof error === 'object' && !error.code) {
|
25581 | error.code = 3;
|
25582 | } else if (typeof error === 'string') {
|
25583 | error = {
|
25584 | message: error,
|
25585 | code: 3
|
25586 | };
|
25587 | }
|
25588 |
|
25589 | player.error(error);
|
25590 | });
|
25591 | var defaultSelector = this.options_.experimentalBufferBasedABR ? Vhs.movingAverageBandwidthSelector(0.55) : Vhs.STANDARD_PLAYLIST_SELECTOR;
|
25592 |
|
25593 |
|
25594 | this.masterPlaylistController_.selectPlaylist = this.selectPlaylist ? this.selectPlaylist.bind(this) : defaultSelector.bind(this);
|
25595 | this.masterPlaylistController_.selectInitialPlaylist = Vhs.INITIAL_PLAYLIST_SELECTOR.bind(this);
|
25596 |
|
25597 | this.playlists = this.masterPlaylistController_.masterPlaylistLoader_;
|
25598 | this.mediaSource = this.masterPlaylistController_.mediaSource;
|
25599 |
|
25600 |
|
25601 |
|
25602 | Object.defineProperties(this, {
|
25603 | selectPlaylist: {
|
25604 | get: function get() {
|
25605 | return this.masterPlaylistController_.selectPlaylist;
|
25606 | },
|
25607 | set: function set(selectPlaylist) {
|
25608 | this.masterPlaylistController_.selectPlaylist = selectPlaylist.bind(this);
|
25609 | }
|
25610 | },
|
25611 | throughput: {
|
25612 | get: function get() {
|
25613 | return this.masterPlaylistController_.mainSegmentLoader_.throughput.rate;
|
25614 | },
|
25615 | set: function set(throughput) {
|
25616 | this.masterPlaylistController_.mainSegmentLoader_.throughput.rate = throughput;
|
25617 |
|
25618 |
|
25619 | this.masterPlaylistController_.mainSegmentLoader_.throughput.count = 1;
|
25620 | }
|
25621 | },
|
25622 | bandwidth: {
|
25623 | get: function get() {
|
25624 | return this.masterPlaylistController_.mainSegmentLoader_.bandwidth;
|
25625 | },
|
25626 | set: function set(bandwidth) {
|
25627 | this.masterPlaylistController_.mainSegmentLoader_.bandwidth = bandwidth;
|
25628 |
|
25629 |
|
25630 |
|
25631 | this.masterPlaylistController_.mainSegmentLoader_.throughput = {
|
25632 | rate: 0,
|
25633 | count: 0
|
25634 | };
|
25635 | }
|
25636 | },
|
25637 |
|
25638 | |
25639 |
|
25640 |
|
25641 |
|
25642 |
|
25643 |
|
25644 |
|
25645 |
|
25646 |
|
25647 | systemBandwidth: {
|
25648 | get: function get() {
|
25649 | var invBandwidth = 1 / (this.bandwidth || 1);
|
25650 | var invThroughput;
|
25651 |
|
25652 | if (this.throughput > 0) {
|
25653 | invThroughput = 1 / this.throughput;
|
25654 | } else {
|
25655 | invThroughput = 0;
|
25656 | }
|
25657 |
|
25658 | var systemBitrate = Math.floor(1 / (invBandwidth + invThroughput));
|
25659 | return systemBitrate;
|
25660 | },
|
25661 | set: function set() {
|
25662 | videojs.log.error('The "systemBandwidth" property is read-only');
|
25663 | }
|
25664 | }
|
25665 | });
|
25666 |
|
25667 | if (this.options_.bandwidth) {
|
25668 | this.bandwidth = this.options_.bandwidth;
|
25669 | }
|
25670 |
|
25671 | if (this.options_.throughput) {
|
25672 | this.throughput = this.options_.throughput;
|
25673 | }
|
25674 |
|
25675 | Object.defineProperties(this.stats, {
|
25676 | bandwidth: {
|
25677 | get: function get() {
|
25678 | return _this3.bandwidth || 0;
|
25679 | },
|
25680 | enumerable: true
|
25681 | },
|
25682 | mediaRequests: {
|
25683 | get: function get() {
|
25684 | return _this3.masterPlaylistController_.mediaRequests_() || 0;
|
25685 | },
|
25686 | enumerable: true
|
25687 | },
|
25688 | mediaRequestsAborted: {
|
25689 | get: function get() {
|
25690 | return _this3.masterPlaylistController_.mediaRequestsAborted_() || 0;
|
25691 | },
|
25692 | enumerable: true
|
25693 | },
|
25694 | mediaRequestsTimedout: {
|
25695 | get: function get() {
|
25696 | return _this3.masterPlaylistController_.mediaRequestsTimedout_() || 0;
|
25697 | },
|
25698 | enumerable: true
|
25699 | },
|
25700 | mediaRequestsErrored: {
|
25701 | get: function get() {
|
25702 | return _this3.masterPlaylistController_.mediaRequestsErrored_() || 0;
|
25703 | },
|
25704 | enumerable: true
|
25705 | },
|
25706 | mediaTransferDuration: {
|
25707 | get: function get() {
|
25708 | return _this3.masterPlaylistController_.mediaTransferDuration_() || 0;
|
25709 | },
|
25710 | enumerable: true
|
25711 | },
|
25712 | mediaBytesTransferred: {
|
25713 | get: function get() {
|
25714 | return _this3.masterPlaylistController_.mediaBytesTransferred_() || 0;
|
25715 | },
|
25716 | enumerable: true
|
25717 | },
|
25718 | mediaSecondsLoaded: {
|
25719 | get: function get() {
|
25720 | return _this3.masterPlaylistController_.mediaSecondsLoaded_() || 0;
|
25721 | },
|
25722 | enumerable: true
|
25723 | },
|
25724 | mediaAppends: {
|
25725 | get: function get() {
|
25726 | return _this3.masterPlaylistController_.mediaAppends_() || 0;
|
25727 | },
|
25728 | enumerable: true
|
25729 | },
|
25730 | mainAppendsToLoadedData: {
|
25731 | get: function get() {
|
25732 | return _this3.masterPlaylistController_.mainAppendsToLoadedData_() || 0;
|
25733 | },
|
25734 | enumerable: true
|
25735 | },
|
25736 | audioAppendsToLoadedData: {
|
25737 | get: function get() {
|
25738 | return _this3.masterPlaylistController_.audioAppendsToLoadedData_() || 0;
|
25739 | },
|
25740 | enumerable: true
|
25741 | },
|
25742 | appendsToLoadedData: {
|
25743 | get: function get() {
|
25744 | return _this3.masterPlaylistController_.appendsToLoadedData_() || 0;
|
25745 | },
|
25746 | enumerable: true
|
25747 | },
|
25748 | timeToLoadedData: {
|
25749 | get: function get() {
|
25750 | return _this3.masterPlaylistController_.timeToLoadedData_() || 0;
|
25751 | },
|
25752 | enumerable: true
|
25753 | },
|
25754 | buffered: {
|
25755 | get: function get() {
|
25756 | return timeRangesToArray(_this3.tech_.buffered());
|
25757 | },
|
25758 | enumerable: true
|
25759 | },
|
25760 | currentTime: {
|
25761 | get: function get() {
|
25762 | return _this3.tech_.currentTime();
|
25763 | },
|
25764 | enumerable: true
|
25765 | },
|
25766 | currentSource: {
|
25767 | get: function get() {
|
25768 | return _this3.tech_.currentSource_;
|
25769 | },
|
25770 | enumerable: true
|
25771 | },
|
25772 | currentTech: {
|
25773 | get: function get() {
|
25774 | return _this3.tech_.name_;
|
25775 | },
|
25776 | enumerable: true
|
25777 | },
|
25778 | duration: {
|
25779 | get: function get() {
|
25780 | return _this3.tech_.duration();
|
25781 | },
|
25782 | enumerable: true
|
25783 | },
|
25784 | master: {
|
25785 | get: function get() {
|
25786 | return _this3.playlists.master;
|
25787 | },
|
25788 | enumerable: true
|
25789 | },
|
25790 | playerDimensions: {
|
25791 | get: function get() {
|
25792 | return _this3.tech_.currentDimensions();
|
25793 | },
|
25794 | enumerable: true
|
25795 | },
|
25796 | seekable: {
|
25797 | get: function get() {
|
25798 | return timeRangesToArray(_this3.tech_.seekable());
|
25799 | },
|
25800 | enumerable: true
|
25801 | },
|
25802 | timestamp: {
|
25803 | get: function get() {
|
25804 | return Date.now();
|
25805 | },
|
25806 | enumerable: true
|
25807 | },
|
25808 | videoPlaybackQuality: {
|
25809 | get: function get() {
|
25810 | return _this3.tech_.getVideoPlaybackQuality();
|
25811 | },
|
25812 | enumerable: true
|
25813 | }
|
25814 | });
|
25815 | this.tech_.one('canplay', this.masterPlaylistController_.setupFirstPlay.bind(this.masterPlaylistController_));
|
25816 | this.tech_.on('bandwidthupdate', function () {
|
25817 | if (_this3.options_.useBandwidthFromLocalStorage) {
|
25818 | updateVhsLocalStorage({
|
25819 | bandwidth: _this3.bandwidth,
|
25820 | throughput: Math.round(_this3.throughput)
|
25821 | });
|
25822 | }
|
25823 | });
|
25824 | this.masterPlaylistController_.on('selectedinitialmedia', function () {
|
25825 |
|
25826 | renditionSelectionMixin(_this3);
|
25827 | });
|
25828 | this.masterPlaylistController_.sourceUpdater_.on('createdsourcebuffers', function () {
|
25829 | _this3.setupEme_();
|
25830 | });
|
25831 |
|
25832 |
|
25833 | this.on(this.masterPlaylistController_, 'progress', function () {
|
25834 | this.tech_.trigger('progress');
|
25835 | });
|
25836 |
|
25837 |
|
25838 | this.on(this.masterPlaylistController_, 'firstplay', function () {
|
25839 | this.ignoreNextSeekingEvent_ = true;
|
25840 | });
|
25841 | this.setupQualityLevels_();
|
25842 |
|
25843 |
|
25844 | if (!this.tech_.el()) {
|
25845 | return;
|
25846 | }
|
25847 |
|
25848 | this.mediaSourceUrl_ = window.URL.createObjectURL(this.masterPlaylistController_.mediaSource);
|
25849 | this.tech_.src(this.mediaSourceUrl_);
|
25850 | }
|
25851 | |
25852 |
|
25853 |
|
25854 |
|
25855 |
|
25856 |
|
25857 |
|
25858 |
|
25859 | ;
|
25860 |
|
25861 | _proto.setupEme_ = function setupEme_() {
|
25862 | var _this4 = this;
|
25863 |
|
25864 | var audioPlaylistLoader = this.masterPlaylistController_.mediaTypes_.AUDIO.activePlaylistLoader;
|
25865 | var didSetupEmeOptions = setupEmeOptions({
|
25866 | player: this.player_,
|
25867 | sourceKeySystems: this.source_.keySystems,
|
25868 | media: this.playlists.media(),
|
25869 | audioMedia: audioPlaylistLoader && audioPlaylistLoader.media()
|
25870 | });
|
25871 | this.player_.tech_.on('keystatuschange', function (e) {
|
25872 | if (e.status === 'output-restricted') {
|
25873 | _this4.masterPlaylistController_.blacklistCurrentPlaylist({
|
25874 | playlist: _this4.masterPlaylistController_.media(),
|
25875 | message: "DRM keystatus changed to " + e.status + ". Playlist will fail to play. Check for HDCP content.",
|
25876 | blacklistDuration: Infinity
|
25877 | });
|
25878 | }
|
25879 | });
|
25880 |
|
25881 |
|
25882 | if (videojs.browser.IE_VERSION === 11 || !didSetupEmeOptions) {
|
25883 |
|
25884 | this.masterPlaylistController_.sourceUpdater_.initializedEme();
|
25885 | return;
|
25886 | }
|
25887 |
|
25888 | this.logger_('waiting for EME key session creation');
|
25889 | waitForKeySessionCreation({
|
25890 | player: this.player_,
|
25891 | sourceKeySystems: this.source_.keySystems,
|
25892 | audioMedia: audioPlaylistLoader && audioPlaylistLoader.media(),
|
25893 | mainPlaylists: this.playlists.master.playlists
|
25894 | }).then(function () {
|
25895 | _this4.logger_('created EME key session');
|
25896 |
|
25897 | _this4.masterPlaylistController_.sourceUpdater_.initializedEme();
|
25898 | }).catch(function (err) {
|
25899 | _this4.logger_('error while creating EME key session', err);
|
25900 |
|
25901 | _this4.player_.error({
|
25902 | message: 'Failed to initialize media keys for EME',
|
25903 | code: 3
|
25904 | });
|
25905 | });
|
25906 | }
|
25907 | |
25908 |
|
25909 |
|
25910 |
|
25911 |
|
25912 |
|
25913 | ;
|
25914 |
|
25915 | _proto.setupQualityLevels_ = function setupQualityLevels_() {
|
25916 | var _this5 = this;
|
25917 |
|
25918 | var player = videojs.players[this.tech_.options_.playerId];
|
25919 |
|
25920 |
|
25921 | if (!player || !player.qualityLevels || this.qualityLevels_) {
|
25922 | return;
|
25923 | }
|
25924 |
|
25925 | this.qualityLevels_ = player.qualityLevels();
|
25926 | this.masterPlaylistController_.on('selectedinitialmedia', function () {
|
25927 | handleVhsLoadedMetadata(_this5.qualityLevels_, _this5);
|
25928 | });
|
25929 | this.playlists.on('mediachange', function () {
|
25930 | handleVhsMediaChange(_this5.qualityLevels_, _this5.playlists);
|
25931 | });
|
25932 | }
|
25933 | |
25934 |
|
25935 |
|
25936 | ;
|
25937 |
|
25938 | VhsHandler.version = function version$5() {
|
25939 | return {
|
25940 | '@videojs/http-streaming': version$4,
|
25941 | 'mux.js': version$3,
|
25942 | 'mpd-parser': version$2,
|
25943 | 'm3u8-parser': version$1,
|
25944 | 'aes-decrypter': version
|
25945 | };
|
25946 | }
|
25947 | |
25948 |
|
25949 |
|
25950 | ;
|
25951 |
|
25952 | _proto.version = function version() {
|
25953 | return this.constructor.version();
|
25954 | };
|
25955 |
|
25956 | _proto.canChangeType = function canChangeType() {
|
25957 | return SourceUpdater.canChangeType();
|
25958 | }
|
25959 | |
25960 |
|
25961 |
|
25962 | ;
|
25963 |
|
25964 | _proto.play = function play() {
|
25965 | this.masterPlaylistController_.play();
|
25966 | }
|
25967 | |
25968 |
|
25969 |
|
25970 | ;
|
25971 |
|
25972 | _proto.setCurrentTime = function setCurrentTime(currentTime) {
|
25973 | this.masterPlaylistController_.setCurrentTime(currentTime);
|
25974 | }
|
25975 | |
25976 |
|
25977 |
|
25978 | ;
|
25979 |
|
25980 | _proto.duration = function duration() {
|
25981 | return this.masterPlaylistController_.duration();
|
25982 | }
|
25983 | |
25984 |
|
25985 |
|
25986 | ;
|
25987 |
|
25988 | _proto.seekable = function seekable() {
|
25989 | return this.masterPlaylistController_.seekable();
|
25990 | }
|
25991 | |
25992 |
|
25993 |
|
25994 | ;
|
25995 |
|
25996 | _proto.dispose = function dispose() {
|
25997 | if (this.playbackWatcher_) {
|
25998 | this.playbackWatcher_.dispose();
|
25999 | }
|
26000 |
|
26001 | if (this.masterPlaylistController_) {
|
26002 | this.masterPlaylistController_.dispose();
|
26003 | }
|
26004 |
|
26005 | if (this.qualityLevels_) {
|
26006 | this.qualityLevels_.dispose();
|
26007 | }
|
26008 |
|
26009 | if (this.player_) {
|
26010 | delete this.player_.vhs;
|
26011 | delete this.player_.dash;
|
26012 | delete this.player_.hls;
|
26013 | }
|
26014 |
|
26015 | if (this.tech_ && this.tech_.vhs) {
|
26016 | delete this.tech_.vhs;
|
26017 | }
|
26018 |
|
26019 |
|
26020 | if (this.tech_) {
|
26021 | delete this.tech_.hls;
|
26022 | }
|
26023 |
|
26024 | if (this.mediaSourceUrl_ && window.URL.revokeObjectURL) {
|
26025 | window.URL.revokeObjectURL(this.mediaSourceUrl_);
|
26026 | this.mediaSourceUrl_ = null;
|
26027 | }
|
26028 |
|
26029 | _Component.prototype.dispose.call(this);
|
26030 | };
|
26031 |
|
26032 | _proto.convertToProgramTime = function convertToProgramTime(time, callback) {
|
26033 | return getProgramTime({
|
26034 | playlist: this.masterPlaylistController_.media(),
|
26035 | time: time,
|
26036 | callback: callback
|
26037 | });
|
26038 | }
|
26039 | ;
|
26040 |
|
26041 | _proto.seekToProgramTime = function seekToProgramTime$1(programTime, callback, pauseAfterSeek, retryCount) {
|
26042 | if (pauseAfterSeek === void 0) {
|
26043 | pauseAfterSeek = true;
|
26044 | }
|
26045 |
|
26046 | if (retryCount === void 0) {
|
26047 | retryCount = 2;
|
26048 | }
|
26049 |
|
26050 | return seekToProgramTime({
|
26051 | programTime: programTime,
|
26052 | playlist: this.masterPlaylistController_.media(),
|
26053 | retryCount: retryCount,
|
26054 | pauseAfterSeek: pauseAfterSeek,
|
26055 | seekTo: this.options_.seekTo,
|
26056 | tech: this.options_.tech,
|
26057 | callback: callback
|
26058 | });
|
26059 | };
|
26060 |
|
26061 | return VhsHandler;
|
26062 | }(Component);
|
26063 |
|
26064 |
|
26065 |
|
26066 |
|
26067 |
|
26068 |
|
26069 |
|
26070 |
|
26071 |
|
26072 | var VhsSourceHandler = {
|
26073 | name: 'videojs-http-streaming',
|
26074 | VERSION: version$4,
|
26075 | canHandleSource: function canHandleSource(srcObj, options) {
|
26076 | if (options === void 0) {
|
26077 | options = {};
|
26078 | }
|
26079 |
|
26080 | var localOptions = videojs.mergeOptions(videojs.options, options);
|
26081 | return VhsSourceHandler.canPlayType(srcObj.type, localOptions);
|
26082 | },
|
26083 | handleSource: function handleSource(source, tech, options) {
|
26084 | if (options === void 0) {
|
26085 | options = {};
|
26086 | }
|
26087 |
|
26088 | var localOptions = videojs.mergeOptions(videojs.options, options);
|
26089 | tech.vhs = new VhsHandler(source, tech, localOptions);
|
26090 |
|
26091 | if (!videojs.hasOwnProperty('hls')) {
|
26092 | Object.defineProperty(tech, 'hls', {
|
26093 | get: function get() {
|
26094 | videojs.log.warn('player.tech().hls is deprecated. Use player.tech().vhs instead.');
|
26095 | return tech.vhs;
|
26096 | },
|
26097 | configurable: true
|
26098 | });
|
26099 | }
|
26100 |
|
26101 | tech.vhs.xhr = xhrFactory();
|
26102 | tech.vhs.src(source.src, source.type);
|
26103 | return tech.vhs;
|
26104 | },
|
26105 | canPlayType: function canPlayType(type, options) {
|
26106 | if (options === void 0) {
|
26107 | options = {};
|
26108 | }
|
26109 |
|
26110 | var _videojs$mergeOptions = videojs.mergeOptions(videojs.options, options),
|
26111 | _videojs$mergeOptions2 = _videojs$mergeOptions.vhs.overrideNative,
|
26112 | overrideNative = _videojs$mergeOptions2 === void 0 ? !videojs.browser.IS_ANY_SAFARI : _videojs$mergeOptions2;
|
26113 |
|
26114 | var supportedType = simpleTypeFromSourceType(type);
|
26115 | var canUseMsePlayback = supportedType && (!Vhs.supportsTypeNatively(supportedType) || overrideNative);
|
26116 | return canUseMsePlayback ? 'maybe' : '';
|
26117 | }
|
26118 | };
|
26119 |
|
26120 |
|
26121 |
|
26122 |
|
26123 |
|
26124 |
|
26125 |
|
26126 | var supportsNativeMediaSources = function supportsNativeMediaSources() {
|
26127 | return browserSupportsCodec('avc1.4d400d,mp4a.40.2');
|
26128 | };
|
26129 |
|
26130 |
|
26131 | if (supportsNativeMediaSources()) {
|
26132 | videojs.getTech('Html5').registerSourceHandler(VhsSourceHandler, 0);
|
26133 | }
|
26134 |
|
26135 | videojs.VhsHandler = VhsHandler;
|
26136 | Object.defineProperty(videojs, 'HlsHandler', {
|
26137 | get: function get() {
|
26138 | videojs.log.warn('videojs.HlsHandler is deprecated. Use videojs.VhsHandler instead.');
|
26139 | return VhsHandler;
|
26140 | },
|
26141 | configurable: true
|
26142 | });
|
26143 | videojs.VhsSourceHandler = VhsSourceHandler;
|
26144 | Object.defineProperty(videojs, 'HlsSourceHandler', {
|
26145 | get: function get() {
|
26146 | videojs.log.warn('videojs.HlsSourceHandler is deprecated. ' + 'Use videojs.VhsSourceHandler instead.');
|
26147 | return VhsSourceHandler;
|
26148 | },
|
26149 | configurable: true
|
26150 | });
|
26151 | videojs.Vhs = Vhs;
|
26152 | Object.defineProperty(videojs, 'Hls', {
|
26153 | get: function get() {
|
26154 | videojs.log.warn('videojs.Hls is deprecated. Use videojs.Vhs instead.');
|
26155 | return Vhs;
|
26156 | },
|
26157 | configurable: true
|
26158 | });
|
26159 |
|
26160 | if (!videojs.use) {
|
26161 | videojs.registerComponent('Hls', Vhs);
|
26162 | videojs.registerComponent('Vhs', Vhs);
|
26163 | }
|
26164 |
|
26165 | videojs.options.vhs = videojs.options.vhs || {};
|
26166 | videojs.options.hls = videojs.options.hls || {};
|
26167 |
|
26168 | if (!videojs.getPlugin || !videojs.getPlugin('reloadSourceOnError')) {
|
26169 | var registerPlugin = videojs.registerPlugin || videojs.plugin;
|
26170 | registerPlugin('reloadSourceOnError', reloadSourceOnError);
|
26171 | }
|
26172 |
|
26173 | export { LOCAL_STORAGE_KEY, Vhs, VhsHandler, VhsSourceHandler, emeKeySystems, expandDataUri, getAllPsshKeySystemsOptions, setupEmeOptions, waitForKeySessionCreation };
|