1 |
|
2 | (function (global, factory) {
|
3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('video.js'), require('@xmldom/xmldom')) :
|
4 | typeof define === 'function' && define.amd ? define(['exports', 'video.js', '@xmldom/xmldom'], factory) :
|
5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.httpStreaming = {}, global.videojs, global.window));
|
6 | })(this, (function (exports, videojs, xmldom) { 'use strict';
|
7 |
|
8 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
9 |
|
10 | var videojs__default = _interopDefaultLegacy(videojs);
|
11 |
|
12 | function _extends() {
|
13 | _extends = Object.assign || function (target) {
|
14 | for (var i = 1; i < arguments.length; i++) {
|
15 | var source = arguments[i];
|
16 |
|
17 | for (var key in source) {
|
18 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
19 | target[key] = source[key];
|
20 | }
|
21 | }
|
22 | }
|
23 |
|
24 | return target;
|
25 | };
|
26 |
|
27 | return _extends.apply(this, arguments);
|
28 | }
|
29 |
|
30 | var urlToolkit = {exports: {}};
|
31 |
|
32 | (function (module, exports) {
|
33 |
|
34 | (function (root) {
|
35 | var URL_REGEX = /^((?:[a-zA-Z0-9+\-.]+:)?)(\/\/[^\/?#]*)?((?:[^\/?#]*\/)*[^;?#]*)?(;[^?#]*)?(\?[^#]*)?(#[^]*)?$/;
|
36 | var FIRST_SEGMENT_REGEX = /^([^\/?#]*)([^]*)$/;
|
37 | var SLASH_DOT_REGEX = /(?:\/|^)\.(?=\/)/g;
|
38 | var SLASH_DOT_DOT_REGEX = /(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g;
|
39 | var URLToolkit = {
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | buildAbsoluteURL: function (baseURL, relativeURL, opts) {
|
47 | opts = opts || {};
|
48 |
|
49 | baseURL = baseURL.trim();
|
50 | relativeURL = relativeURL.trim();
|
51 |
|
52 | if (!relativeURL) {
|
53 |
|
54 |
|
55 |
|
56 | if (!opts.alwaysNormalize) {
|
57 | return baseURL;
|
58 | }
|
59 |
|
60 | var basePartsForNormalise = URLToolkit.parseURL(baseURL);
|
61 |
|
62 | if (!basePartsForNormalise) {
|
63 | throw new Error('Error trying to parse base URL.');
|
64 | }
|
65 |
|
66 | basePartsForNormalise.path = URLToolkit.normalizePath(basePartsForNormalise.path);
|
67 | return URLToolkit.buildURLFromParts(basePartsForNormalise);
|
68 | }
|
69 |
|
70 | var relativeParts = URLToolkit.parseURL(relativeURL);
|
71 |
|
72 | if (!relativeParts) {
|
73 | throw new Error('Error trying to parse relative URL.');
|
74 | }
|
75 |
|
76 | if (relativeParts.scheme) {
|
77 |
|
78 |
|
79 | if (!opts.alwaysNormalize) {
|
80 | return relativeURL;
|
81 | }
|
82 |
|
83 | relativeParts.path = URLToolkit.normalizePath(relativeParts.path);
|
84 | return URLToolkit.buildURLFromParts(relativeParts);
|
85 | }
|
86 |
|
87 | var baseParts = URLToolkit.parseURL(baseURL);
|
88 |
|
89 | if (!baseParts) {
|
90 | throw new Error('Error trying to parse base URL.');
|
91 | }
|
92 |
|
93 | if (!baseParts.netLoc && baseParts.path && baseParts.path[0] !== '/') {
|
94 |
|
95 |
|
96 | var pathParts = FIRST_SEGMENT_REGEX.exec(baseParts.path);
|
97 | baseParts.netLoc = pathParts[1];
|
98 | baseParts.path = pathParts[2];
|
99 | }
|
100 |
|
101 | if (baseParts.netLoc && !baseParts.path) {
|
102 | baseParts.path = '/';
|
103 | }
|
104 |
|
105 | var builtParts = {
|
106 |
|
107 |
|
108 | scheme: baseParts.scheme,
|
109 | netLoc: relativeParts.netLoc,
|
110 | path: null,
|
111 | params: relativeParts.params,
|
112 | query: relativeParts.query,
|
113 | fragment: relativeParts.fragment
|
114 | };
|
115 |
|
116 | if (!relativeParts.netLoc) {
|
117 |
|
118 |
|
119 |
|
120 | builtParts.netLoc = baseParts.netLoc;
|
121 |
|
122 |
|
123 | if (relativeParts.path[0] !== '/') {
|
124 | if (!relativeParts.path) {
|
125 |
|
126 |
|
127 | builtParts.path = baseParts.path;
|
128 |
|
129 |
|
130 |
|
131 | if (!relativeParts.params) {
|
132 | builtParts.params = baseParts.params;
|
133 |
|
134 |
|
135 |
|
136 | if (!relativeParts.query) {
|
137 | builtParts.query = baseParts.query;
|
138 | }
|
139 | }
|
140 | } else {
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | var baseURLPath = baseParts.path;
|
146 | var newPath = baseURLPath.substring(0, baseURLPath.lastIndexOf('/') + 1) + relativeParts.path;
|
147 | builtParts.path = URLToolkit.normalizePath(newPath);
|
148 | }
|
149 | }
|
150 | }
|
151 |
|
152 | if (builtParts.path === null) {
|
153 | builtParts.path = opts.alwaysNormalize ? URLToolkit.normalizePath(relativeParts.path) : relativeParts.path;
|
154 | }
|
155 |
|
156 | return URLToolkit.buildURLFromParts(builtParts);
|
157 | },
|
158 | parseURL: function (url) {
|
159 | var parts = URL_REGEX.exec(url);
|
160 |
|
161 | if (!parts) {
|
162 | return null;
|
163 | }
|
164 |
|
165 | return {
|
166 | scheme: parts[1] || '',
|
167 | netLoc: parts[2] || '',
|
168 | path: parts[3] || '',
|
169 | params: parts[4] || '',
|
170 | query: parts[5] || '',
|
171 | fragment: parts[6] || ''
|
172 | };
|
173 | },
|
174 | normalizePath: function (path) {
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | path = path.split('').reverse().join('').replace(SLASH_DOT_REGEX, '');
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | while (path.length !== (path = path.replace(SLASH_DOT_DOT_REGEX, '')).length) {}
|
191 |
|
192 | return path.split('').reverse().join('');
|
193 | },
|
194 | buildURLFromParts: function (parts) {
|
195 | return parts.scheme + parts.netLoc + parts.path + parts.params + parts.query + parts.fragment;
|
196 | }
|
197 | };
|
198 | module.exports = URLToolkit;
|
199 | })();
|
200 | })(urlToolkit);
|
201 |
|
202 | var URLToolkit = urlToolkit.exports;
|
203 |
|
204 | var DEFAULT_LOCATION = 'http://example.com';
|
205 |
|
206 | var resolveUrl$1 = function resolveUrl(baseUrl, relativeUrl) {
|
207 |
|
208 | if (/^[a-z]+:/i.test(relativeUrl)) {
|
209 | return relativeUrl;
|
210 | }
|
211 |
|
212 |
|
213 | if (/^data:/.test(baseUrl)) {
|
214 | baseUrl = window.location && window.location.href || '';
|
215 | }
|
216 |
|
217 |
|
218 |
|
219 | var nativeURL = typeof window.URL === 'function';
|
220 | var protocolLess = /^\/\//.test(baseUrl);
|
221 |
|
222 |
|
223 | var removeLocation = !window.location && !/\/\//i.test(baseUrl);
|
224 |
|
225 | if (nativeURL) {
|
226 | baseUrl = new window.URL(baseUrl, window.location || DEFAULT_LOCATION);
|
227 | } else if (!/\/\//i.test(baseUrl)) {
|
228 | baseUrl = URLToolkit.buildAbsoluteURL(window.location && window.location.href || '', baseUrl);
|
229 | }
|
230 |
|
231 | if (nativeURL) {
|
232 | var newUrl = new URL(relativeUrl, baseUrl);
|
233 |
|
234 |
|
235 |
|
236 | if (removeLocation) {
|
237 | return newUrl.href.slice(DEFAULT_LOCATION.length);
|
238 | } else if (protocolLess) {
|
239 | return newUrl.href.slice(newUrl.protocol.length);
|
240 | }
|
241 |
|
242 | return newUrl.href;
|
243 | }
|
244 |
|
245 | return URLToolkit.buildAbsoluteURL(baseUrl, relativeUrl);
|
246 | };
|
247 |
|
248 | |
249 |
|
250 |
|
251 | const resolveUrl = resolveUrl$1;
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | const resolveManifestRedirect = (url, req) => {
|
265 |
|
266 |
|
267 |
|
268 | if (req && req.responseURL && url !== req.responseURL) {
|
269 | return req.responseURL;
|
270 | }
|
271 |
|
272 | return url;
|
273 | };
|
274 |
|
275 | const logger = source => {
|
276 | if (videojs__default["default"].log.debug) {
|
277 | return videojs__default["default"].log.debug.bind(videojs__default["default"], 'VHS:', `${source} >`);
|
278 | }
|
279 |
|
280 | return function () {};
|
281 | };
|
282 |
|
283 | |
284 |
|
285 |
|
286 |
|
287 | |
288 |
|
289 |
|
290 |
|
291 |
|
292 | var Stream = function () {
|
293 | function Stream() {
|
294 | this.listeners = {};
|
295 | }
|
296 | |
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | var _proto = Stream.prototype;
|
306 |
|
307 | _proto.on = function on(type, listener) {
|
308 | if (!this.listeners[type]) {
|
309 | this.listeners[type] = [];
|
310 | }
|
311 |
|
312 | this.listeners[type].push(listener);
|
313 | }
|
314 | |
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | ;
|
323 |
|
324 | _proto.off = function off(type, listener) {
|
325 | if (!this.listeners[type]) {
|
326 | return false;
|
327 | }
|
328 |
|
329 | var index = this.listeners[type].indexOf(listener);
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | this.listeners[type] = this.listeners[type].slice(0);
|
339 | this.listeners[type].splice(index, 1);
|
340 | return index > -1;
|
341 | }
|
342 | |
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | ;
|
349 |
|
350 | _proto.trigger = function trigger(type) {
|
351 | var callbacks = this.listeners[type];
|
352 |
|
353 | if (!callbacks) {
|
354 | return;
|
355 | }
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | if (arguments.length === 2) {
|
362 | var length = callbacks.length;
|
363 |
|
364 | for (var i = 0; i < length; ++i) {
|
365 | callbacks[i].call(this, arguments[1]);
|
366 | }
|
367 | } else {
|
368 | var args = Array.prototype.slice.call(arguments, 1);
|
369 | var _length = callbacks.length;
|
370 |
|
371 | for (var _i = 0; _i < _length; ++_i) {
|
372 | callbacks[_i].apply(this, args);
|
373 | }
|
374 | }
|
375 | }
|
376 | |
377 |
|
378 |
|
379 | ;
|
380 |
|
381 | _proto.dispose = function dispose() {
|
382 | this.listeners = {};
|
383 | }
|
384 | |
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | ;
|
393 |
|
394 | _proto.pipe = function pipe(destination) {
|
395 | this.on('data', function (data) {
|
396 | destination.push(data);
|
397 | });
|
398 | };
|
399 |
|
400 | return Stream;
|
401 | }();
|
402 |
|
403 | var atob = function atob(s) {
|
404 | return window.atob ? window.atob(s) : Buffer.from(s, 'base64').toString('binary');
|
405 | };
|
406 |
|
407 | function decodeB64ToUint8Array(b64Text) {
|
408 | var decodedString = atob(b64Text);
|
409 | var array = new Uint8Array(decodedString.length);
|
410 |
|
411 | for (var i = 0; i < decodedString.length; i++) {
|
412 | array[i] = decodedString.charCodeAt(i);
|
413 | }
|
414 |
|
415 | return array;
|
416 | }
|
417 |
|
418 |
|
419 | |
420 |
|
421 |
|
422 |
|
423 | |
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | class LineStream extends Stream {
|
432 | constructor() {
|
433 | super();
|
434 | this.buffer = '';
|
435 | }
|
436 | |
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 | push(data) {
|
444 | let nextNewline;
|
445 | this.buffer += data;
|
446 | nextNewline = this.buffer.indexOf('\n');
|
447 |
|
448 | for (; nextNewline > -1; nextNewline = this.buffer.indexOf('\n')) {
|
449 | this.trigger('data', this.buffer.substring(0, nextNewline));
|
450 | this.buffer = this.buffer.substring(nextNewline + 1);
|
451 | }
|
452 | }
|
453 |
|
454 | }
|
455 |
|
456 | const TAB = String.fromCharCode(0x09);
|
457 |
|
458 | const parseByterange = function (byterangeString) {
|
459 |
|
460 |
|
461 | const match = /([0-9.]*)?@?([0-9.]*)?/.exec(byterangeString || '');
|
462 | const result = {};
|
463 |
|
464 | if (match[1]) {
|
465 | result.length = parseInt(match[1], 10);
|
466 | }
|
467 |
|
468 | if (match[2]) {
|
469 | result.offset = parseInt(match[2], 10);
|
470 | }
|
471 |
|
472 | return result;
|
473 | };
|
474 | |
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | const attributeSeparator = function () {
|
484 | const key = '[^=]*';
|
485 | const value = '"[^"]*"|[^,]*';
|
486 | const keyvalue = '(?:' + key + ')=(?:' + value + ')';
|
487 | return new RegExp('(?:^|,)(' + keyvalue + ')');
|
488 | };
|
489 | |
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | const parseAttributes$1 = function (attributes) {
|
497 | const result = {};
|
498 |
|
499 | if (!attributes) {
|
500 | return result;
|
501 | }
|
502 |
|
503 |
|
504 | const attrs = attributes.split(attributeSeparator());
|
505 | let i = attrs.length;
|
506 | let attr;
|
507 |
|
508 | while (i--) {
|
509 |
|
510 | if (attrs[i] === '') {
|
511 | continue;
|
512 | }
|
513 |
|
514 |
|
515 | attr = /([^=]*)=(.*)/.exec(attrs[i]).slice(1);
|
516 |
|
517 | attr[0] = attr[0].replace(/^\s+|\s+$/g, '');
|
518 | attr[1] = attr[1].replace(/^\s+|\s+$/g, '');
|
519 | attr[1] = attr[1].replace(/^['"](.*)['"]$/g, '$1');
|
520 | result[attr[0]] = attr[1];
|
521 | }
|
522 |
|
523 | return result;
|
524 | };
|
525 | |
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | class ParseStream extends Stream {
|
552 | constructor() {
|
553 | super();
|
554 | this.customParsers = [];
|
555 | this.tagMappers = [];
|
556 | }
|
557 | |
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 | push(line) {
|
565 | let match;
|
566 | let event;
|
567 |
|
568 | line = line.trim();
|
569 |
|
570 | if (line.length === 0) {
|
571 |
|
572 | return;
|
573 | }
|
574 |
|
575 |
|
576 | if (line[0] !== '#') {
|
577 | this.trigger('data', {
|
578 | type: 'uri',
|
579 | uri: line
|
580 | });
|
581 | return;
|
582 | }
|
583 |
|
584 |
|
585 | const newLines = this.tagMappers.reduce((acc, mapper) => {
|
586 | const mappedLine = mapper(line);
|
587 |
|
588 | if (mappedLine === line) {
|
589 | return acc;
|
590 | }
|
591 |
|
592 | return acc.concat([mappedLine]);
|
593 | }, [line]);
|
594 | newLines.forEach(newLine => {
|
595 | for (let i = 0; i < this.customParsers.length; i++) {
|
596 | if (this.customParsers[i].call(this, newLine)) {
|
597 | return;
|
598 | }
|
599 | }
|
600 |
|
601 |
|
602 | if (newLine.indexOf('#EXT') !== 0) {
|
603 | this.trigger('data', {
|
604 | type: 'comment',
|
605 | text: newLine.slice(1)
|
606 | });
|
607 | return;
|
608 | }
|
609 |
|
610 |
|
611 |
|
612 | newLine = newLine.replace('\r', '');
|
613 |
|
614 | match = /^#EXTM3U/.exec(newLine);
|
615 |
|
616 | if (match) {
|
617 | this.trigger('data', {
|
618 | type: 'tag',
|
619 | tagType: 'm3u'
|
620 | });
|
621 | return;
|
622 | }
|
623 |
|
624 | match = /^#EXTINF:([0-9\.]*)?,?(.*)?$/.exec(newLine);
|
625 |
|
626 | if (match) {
|
627 | event = {
|
628 | type: 'tag',
|
629 | tagType: 'inf'
|
630 | };
|
631 |
|
632 | if (match[1]) {
|
633 | event.duration = parseFloat(match[1]);
|
634 | }
|
635 |
|
636 | if (match[2]) {
|
637 | event.title = match[2];
|
638 | }
|
639 |
|
640 | this.trigger('data', event);
|
641 | return;
|
642 | }
|
643 |
|
644 | match = /^#EXT-X-TARGETDURATION:([0-9.]*)?/.exec(newLine);
|
645 |
|
646 | if (match) {
|
647 | event = {
|
648 | type: 'tag',
|
649 | tagType: 'targetduration'
|
650 | };
|
651 |
|
652 | if (match[1]) {
|
653 | event.duration = parseInt(match[1], 10);
|
654 | }
|
655 |
|
656 | this.trigger('data', event);
|
657 | return;
|
658 | }
|
659 |
|
660 | match = /^#EXT-X-VERSION:([0-9.]*)?/.exec(newLine);
|
661 |
|
662 | if (match) {
|
663 | event = {
|
664 | type: 'tag',
|
665 | tagType: 'version'
|
666 | };
|
667 |
|
668 | if (match[1]) {
|
669 | event.version = parseInt(match[1], 10);
|
670 | }
|
671 |
|
672 | this.trigger('data', event);
|
673 | return;
|
674 | }
|
675 |
|
676 | match = /^#EXT-X-MEDIA-SEQUENCE:(\-?[0-9.]*)?/.exec(newLine);
|
677 |
|
678 | if (match) {
|
679 | event = {
|
680 | type: 'tag',
|
681 | tagType: 'media-sequence'
|
682 | };
|
683 |
|
684 | if (match[1]) {
|
685 | event.number = parseInt(match[1], 10);
|
686 | }
|
687 |
|
688 | this.trigger('data', event);
|
689 | return;
|
690 | }
|
691 |
|
692 | match = /^#EXT-X-DISCONTINUITY-SEQUENCE:(\-?[0-9.]*)?/.exec(newLine);
|
693 |
|
694 | if (match) {
|
695 | event = {
|
696 | type: 'tag',
|
697 | tagType: 'discontinuity-sequence'
|
698 | };
|
699 |
|
700 | if (match[1]) {
|
701 | event.number = parseInt(match[1], 10);
|
702 | }
|
703 |
|
704 | this.trigger('data', event);
|
705 | return;
|
706 | }
|
707 |
|
708 | match = /^#EXT-X-PLAYLIST-TYPE:(.*)?$/.exec(newLine);
|
709 |
|
710 | if (match) {
|
711 | event = {
|
712 | type: 'tag',
|
713 | tagType: 'playlist-type'
|
714 | };
|
715 |
|
716 | if (match[1]) {
|
717 | event.playlistType = match[1];
|
718 | }
|
719 |
|
720 | this.trigger('data', event);
|
721 | return;
|
722 | }
|
723 |
|
724 | match = /^#EXT-X-BYTERANGE:(.*)?$/.exec(newLine);
|
725 |
|
726 | if (match) {
|
727 | event = _extends(parseByterange(match[1]), {
|
728 | type: 'tag',
|
729 | tagType: 'byterange'
|
730 | });
|
731 | this.trigger('data', event);
|
732 | return;
|
733 | }
|
734 |
|
735 | match = /^#EXT-X-ALLOW-CACHE:(YES|NO)?/.exec(newLine);
|
736 |
|
737 | if (match) {
|
738 | event = {
|
739 | type: 'tag',
|
740 | tagType: 'allow-cache'
|
741 | };
|
742 |
|
743 | if (match[1]) {
|
744 | event.allowed = !/NO/.test(match[1]);
|
745 | }
|
746 |
|
747 | this.trigger('data', event);
|
748 | return;
|
749 | }
|
750 |
|
751 | match = /^#EXT-X-MAP:(.*)$/.exec(newLine);
|
752 |
|
753 | if (match) {
|
754 | event = {
|
755 | type: 'tag',
|
756 | tagType: 'map'
|
757 | };
|
758 |
|
759 | if (match[1]) {
|
760 | const attributes = parseAttributes$1(match[1]);
|
761 |
|
762 | if (attributes.URI) {
|
763 | event.uri = attributes.URI;
|
764 | }
|
765 |
|
766 | if (attributes.BYTERANGE) {
|
767 | event.byterange = parseByterange(attributes.BYTERANGE);
|
768 | }
|
769 | }
|
770 |
|
771 | this.trigger('data', event);
|
772 | return;
|
773 | }
|
774 |
|
775 | match = /^#EXT-X-STREAM-INF:(.*)$/.exec(newLine);
|
776 |
|
777 | if (match) {
|
778 | event = {
|
779 | type: 'tag',
|
780 | tagType: 'stream-inf'
|
781 | };
|
782 |
|
783 | if (match[1]) {
|
784 | event.attributes = parseAttributes$1(match[1]);
|
785 |
|
786 | if (event.attributes.RESOLUTION) {
|
787 | const split = event.attributes.RESOLUTION.split('x');
|
788 | const resolution = {};
|
789 |
|
790 | if (split[0]) {
|
791 | resolution.width = parseInt(split[0], 10);
|
792 | }
|
793 |
|
794 | if (split[1]) {
|
795 | resolution.height = parseInt(split[1], 10);
|
796 | }
|
797 |
|
798 | event.attributes.RESOLUTION = resolution;
|
799 | }
|
800 |
|
801 | if (event.attributes.BANDWIDTH) {
|
802 | event.attributes.BANDWIDTH = parseInt(event.attributes.BANDWIDTH, 10);
|
803 | }
|
804 |
|
805 | if (event.attributes['FRAME-RATE']) {
|
806 | event.attributes['FRAME-RATE'] = parseFloat(event.attributes['FRAME-RATE']);
|
807 | }
|
808 |
|
809 | if (event.attributes['PROGRAM-ID']) {
|
810 | event.attributes['PROGRAM-ID'] = parseInt(event.attributes['PROGRAM-ID'], 10);
|
811 | }
|
812 | }
|
813 |
|
814 | this.trigger('data', event);
|
815 | return;
|
816 | }
|
817 |
|
818 | match = /^#EXT-X-MEDIA:(.*)$/.exec(newLine);
|
819 |
|
820 | if (match) {
|
821 | event = {
|
822 | type: 'tag',
|
823 | tagType: 'media'
|
824 | };
|
825 |
|
826 | if (match[1]) {
|
827 | event.attributes = parseAttributes$1(match[1]);
|
828 | }
|
829 |
|
830 | this.trigger('data', event);
|
831 | return;
|
832 | }
|
833 |
|
834 | match = /^#EXT-X-ENDLIST/.exec(newLine);
|
835 |
|
836 | if (match) {
|
837 | this.trigger('data', {
|
838 | type: 'tag',
|
839 | tagType: 'endlist'
|
840 | });
|
841 | return;
|
842 | }
|
843 |
|
844 | match = /^#EXT-X-DISCONTINUITY/.exec(newLine);
|
845 |
|
846 | if (match) {
|
847 | this.trigger('data', {
|
848 | type: 'tag',
|
849 | tagType: 'discontinuity'
|
850 | });
|
851 | return;
|
852 | }
|
853 |
|
854 | match = /^#EXT-X-PROGRAM-DATE-TIME:(.*)$/.exec(newLine);
|
855 |
|
856 | if (match) {
|
857 | event = {
|
858 | type: 'tag',
|
859 | tagType: 'program-date-time'
|
860 | };
|
861 |
|
862 | if (match[1]) {
|
863 | event.dateTimeString = match[1];
|
864 | event.dateTimeObject = new Date(match[1]);
|
865 | }
|
866 |
|
867 | this.trigger('data', event);
|
868 | return;
|
869 | }
|
870 |
|
871 | match = /^#EXT-X-KEY:(.*)$/.exec(newLine);
|
872 |
|
873 | if (match) {
|
874 | event = {
|
875 | type: 'tag',
|
876 | tagType: 'key'
|
877 | };
|
878 |
|
879 | if (match[1]) {
|
880 | event.attributes = parseAttributes$1(match[1]);
|
881 |
|
882 | if (event.attributes.IV) {
|
883 | if (event.attributes.IV.substring(0, 2).toLowerCase() === '0x') {
|
884 | event.attributes.IV = event.attributes.IV.substring(2);
|
885 | }
|
886 |
|
887 | event.attributes.IV = event.attributes.IV.match(/.{8}/g);
|
888 | event.attributes.IV[0] = parseInt(event.attributes.IV[0], 16);
|
889 | event.attributes.IV[1] = parseInt(event.attributes.IV[1], 16);
|
890 | event.attributes.IV[2] = parseInt(event.attributes.IV[2], 16);
|
891 | event.attributes.IV[3] = parseInt(event.attributes.IV[3], 16);
|
892 | event.attributes.IV = new Uint32Array(event.attributes.IV);
|
893 | }
|
894 | }
|
895 |
|
896 | this.trigger('data', event);
|
897 | return;
|
898 | }
|
899 |
|
900 | match = /^#EXT-X-START:(.*)$/.exec(newLine);
|
901 |
|
902 | if (match) {
|
903 | event = {
|
904 | type: 'tag',
|
905 | tagType: 'start'
|
906 | };
|
907 |
|
908 | if (match[1]) {
|
909 | event.attributes = parseAttributes$1(match[1]);
|
910 | event.attributes['TIME-OFFSET'] = parseFloat(event.attributes['TIME-OFFSET']);
|
911 | event.attributes.PRECISE = /YES/.test(event.attributes.PRECISE);
|
912 | }
|
913 |
|
914 | this.trigger('data', event);
|
915 | return;
|
916 | }
|
917 |
|
918 | match = /^#EXT-X-CUE-OUT-CONT:(.*)?$/.exec(newLine);
|
919 |
|
920 | if (match) {
|
921 | event = {
|
922 | type: 'tag',
|
923 | tagType: 'cue-out-cont'
|
924 | };
|
925 |
|
926 | if (match[1]) {
|
927 | event.data = match[1];
|
928 | } else {
|
929 | event.data = '';
|
930 | }
|
931 |
|
932 | this.trigger('data', event);
|
933 | return;
|
934 | }
|
935 |
|
936 | match = /^#EXT-X-CUE-OUT:(.*)?$/.exec(newLine);
|
937 |
|
938 | if (match) {
|
939 | event = {
|
940 | type: 'tag',
|
941 | tagType: 'cue-out'
|
942 | };
|
943 |
|
944 | if (match[1]) {
|
945 | event.data = match[1];
|
946 | } else {
|
947 | event.data = '';
|
948 | }
|
949 |
|
950 | this.trigger('data', event);
|
951 | return;
|
952 | }
|
953 |
|
954 | match = /^#EXT-X-CUE-IN:(.*)?$/.exec(newLine);
|
955 |
|
956 | if (match) {
|
957 | event = {
|
958 | type: 'tag',
|
959 | tagType: 'cue-in'
|
960 | };
|
961 |
|
962 | if (match[1]) {
|
963 | event.data = match[1];
|
964 | } else {
|
965 | event.data = '';
|
966 | }
|
967 |
|
968 | this.trigger('data', event);
|
969 | return;
|
970 | }
|
971 |
|
972 | match = /^#EXT-X-SKIP:(.*)$/.exec(newLine);
|
973 |
|
974 | if (match && match[1]) {
|
975 | event = {
|
976 | type: 'tag',
|
977 | tagType: 'skip'
|
978 | };
|
979 | event.attributes = parseAttributes$1(match[1]);
|
980 |
|
981 | if (event.attributes.hasOwnProperty('SKIPPED-SEGMENTS')) {
|
982 | event.attributes['SKIPPED-SEGMENTS'] = parseInt(event.attributes['SKIPPED-SEGMENTS'], 10);
|
983 | }
|
984 |
|
985 | if (event.attributes.hasOwnProperty('RECENTLY-REMOVED-DATERANGES')) {
|
986 | event.attributes['RECENTLY-REMOVED-DATERANGES'] = event.attributes['RECENTLY-REMOVED-DATERANGES'].split(TAB);
|
987 | }
|
988 |
|
989 | this.trigger('data', event);
|
990 | return;
|
991 | }
|
992 |
|
993 | match = /^#EXT-X-PART:(.*)$/.exec(newLine);
|
994 |
|
995 | if (match && match[1]) {
|
996 | event = {
|
997 | type: 'tag',
|
998 | tagType: 'part'
|
999 | };
|
1000 | event.attributes = parseAttributes$1(match[1]);
|
1001 | ['DURATION'].forEach(function (key) {
|
1002 | if (event.attributes.hasOwnProperty(key)) {
|
1003 | event.attributes[key] = parseFloat(event.attributes[key]);
|
1004 | }
|
1005 | });
|
1006 | ['INDEPENDENT', 'GAP'].forEach(function (key) {
|
1007 | if (event.attributes.hasOwnProperty(key)) {
|
1008 | event.attributes[key] = /YES/.test(event.attributes[key]);
|
1009 | }
|
1010 | });
|
1011 |
|
1012 | if (event.attributes.hasOwnProperty('BYTERANGE')) {
|
1013 | event.attributes.byterange = parseByterange(event.attributes.BYTERANGE);
|
1014 | }
|
1015 |
|
1016 | this.trigger('data', event);
|
1017 | return;
|
1018 | }
|
1019 |
|
1020 | match = /^#EXT-X-SERVER-CONTROL:(.*)$/.exec(newLine);
|
1021 |
|
1022 | if (match && match[1]) {
|
1023 | event = {
|
1024 | type: 'tag',
|
1025 | tagType: 'server-control'
|
1026 | };
|
1027 | event.attributes = parseAttributes$1(match[1]);
|
1028 | ['CAN-SKIP-UNTIL', 'PART-HOLD-BACK', 'HOLD-BACK'].forEach(function (key) {
|
1029 | if (event.attributes.hasOwnProperty(key)) {
|
1030 | event.attributes[key] = parseFloat(event.attributes[key]);
|
1031 | }
|
1032 | });
|
1033 | ['CAN-SKIP-DATERANGES', 'CAN-BLOCK-RELOAD'].forEach(function (key) {
|
1034 | if (event.attributes.hasOwnProperty(key)) {
|
1035 | event.attributes[key] = /YES/.test(event.attributes[key]);
|
1036 | }
|
1037 | });
|
1038 | this.trigger('data', event);
|
1039 | return;
|
1040 | }
|
1041 |
|
1042 | match = /^#EXT-X-PART-INF:(.*)$/.exec(newLine);
|
1043 |
|
1044 | if (match && match[1]) {
|
1045 | event = {
|
1046 | type: 'tag',
|
1047 | tagType: 'part-inf'
|
1048 | };
|
1049 | event.attributes = parseAttributes$1(match[1]);
|
1050 | ['PART-TARGET'].forEach(function (key) {
|
1051 | if (event.attributes.hasOwnProperty(key)) {
|
1052 | event.attributes[key] = parseFloat(event.attributes[key]);
|
1053 | }
|
1054 | });
|
1055 | this.trigger('data', event);
|
1056 | return;
|
1057 | }
|
1058 |
|
1059 | match = /^#EXT-X-PRELOAD-HINT:(.*)$/.exec(newLine);
|
1060 |
|
1061 | if (match && match[1]) {
|
1062 | event = {
|
1063 | type: 'tag',
|
1064 | tagType: 'preload-hint'
|
1065 | };
|
1066 | event.attributes = parseAttributes$1(match[1]);
|
1067 | ['BYTERANGE-START', 'BYTERANGE-LENGTH'].forEach(function (key) {
|
1068 | if (event.attributes.hasOwnProperty(key)) {
|
1069 | event.attributes[key] = parseInt(event.attributes[key], 10);
|
1070 | const subkey = key === 'BYTERANGE-LENGTH' ? 'length' : 'offset';
|
1071 | event.attributes.byterange = event.attributes.byterange || {};
|
1072 | event.attributes.byterange[subkey] = event.attributes[key];
|
1073 |
|
1074 | delete event.attributes[key];
|
1075 | }
|
1076 | });
|
1077 | this.trigger('data', event);
|
1078 | return;
|
1079 | }
|
1080 |
|
1081 | match = /^#EXT-X-RENDITION-REPORT:(.*)$/.exec(newLine);
|
1082 |
|
1083 | if (match && match[1]) {
|
1084 | event = {
|
1085 | type: 'tag',
|
1086 | tagType: 'rendition-report'
|
1087 | };
|
1088 | event.attributes = parseAttributes$1(match[1]);
|
1089 | ['LAST-MSN', 'LAST-PART'].forEach(function (key) {
|
1090 | if (event.attributes.hasOwnProperty(key)) {
|
1091 | event.attributes[key] = parseInt(event.attributes[key], 10);
|
1092 | }
|
1093 | });
|
1094 | this.trigger('data', event);
|
1095 | return;
|
1096 | }
|
1097 |
|
1098 | match = /^#EXT-X-DATERANGE:(.*)$/.exec(newLine);
|
1099 |
|
1100 | if (match && match[1]) {
|
1101 | event = {
|
1102 | type: 'tag',
|
1103 | tagType: 'daterange'
|
1104 | };
|
1105 | event.attributes = parseAttributes$1(match[1]);
|
1106 | ['ID', 'CLASS'].forEach(function (key) {
|
1107 | if (event.attributes.hasOwnProperty(key)) {
|
1108 | event.attributes[key] = String(event.attributes[key]);
|
1109 | }
|
1110 | });
|
1111 | ['START-DATE', 'END-DATE'].forEach(function (key) {
|
1112 | if (event.attributes.hasOwnProperty(key)) {
|
1113 | event.attributes[key] = new Date(event.attributes[key]);
|
1114 | }
|
1115 | });
|
1116 | ['DURATION', 'PLANNED-DURATION'].forEach(function (key) {
|
1117 | if (event.attributes.hasOwnProperty(key)) {
|
1118 | event.attributes[key] = parseFloat(event.attributes[key]);
|
1119 | }
|
1120 | });
|
1121 | ['END-ON-NEXT'].forEach(function (key) {
|
1122 | if (event.attributes.hasOwnProperty(key)) {
|
1123 | event.attributes[key] = /YES/i.test(event.attributes[key]);
|
1124 | }
|
1125 | });
|
1126 | ['SCTE35-CMD', ' SCTE35-OUT', 'SCTE35-IN'].forEach(function (key) {
|
1127 | if (event.attributes.hasOwnProperty(key)) {
|
1128 | event.attributes[key] = event.attributes[key].toString(16);
|
1129 | }
|
1130 | });
|
1131 | const clientAttributePattern = /^X-([A-Z]+-)+[A-Z]+$/;
|
1132 |
|
1133 | for (const key in event.attributes) {
|
1134 | if (!clientAttributePattern.test(key)) {
|
1135 | continue;
|
1136 | }
|
1137 |
|
1138 | const isHexaDecimal = /[0-9A-Fa-f]{6}/g.test(event.attributes[key]);
|
1139 | const isDecimalFloating = /^\d+(\.\d+)?$/.test(event.attributes[key]);
|
1140 | event.attributes[key] = isHexaDecimal ? event.attributes[key].toString(16) : isDecimalFloating ? parseFloat(event.attributes[key]) : String(event.attributes[key]);
|
1141 | }
|
1142 |
|
1143 | this.trigger('data', event);
|
1144 | return;
|
1145 | }
|
1146 |
|
1147 | match = /^#EXT-X-INDEPENDENT-SEGMENTS/.exec(newLine);
|
1148 |
|
1149 | if (match) {
|
1150 | this.trigger('data', {
|
1151 | type: 'tag',
|
1152 | tagType: 'independent-segments'
|
1153 | });
|
1154 | return;
|
1155 | }
|
1156 |
|
1157 | match = /^#EXT-X-CONTENT-STEERING:(.*)$/.exec(newLine);
|
1158 |
|
1159 | if (match) {
|
1160 | event = {
|
1161 | type: 'tag',
|
1162 | tagType: 'content-steering'
|
1163 | };
|
1164 | event.attributes = parseAttributes$1(match[1]);
|
1165 | this.trigger('data', event);
|
1166 | return;
|
1167 | }
|
1168 |
|
1169 |
|
1170 | this.trigger('data', {
|
1171 | type: 'tag',
|
1172 | data: newLine.slice(4)
|
1173 | });
|
1174 | });
|
1175 | }
|
1176 | |
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 | addParser({
|
1188 | expression,
|
1189 | customType,
|
1190 | dataParser,
|
1191 | segment
|
1192 | }) {
|
1193 | if (typeof dataParser !== 'function') {
|
1194 | dataParser = line => line;
|
1195 | }
|
1196 |
|
1197 | this.customParsers.push(line => {
|
1198 | const match = expression.exec(line);
|
1199 |
|
1200 | if (match) {
|
1201 | this.trigger('data', {
|
1202 | type: 'custom',
|
1203 | data: dataParser(line),
|
1204 | customType,
|
1205 | segment
|
1206 | });
|
1207 | return true;
|
1208 | }
|
1209 | });
|
1210 | }
|
1211 | |
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 | addTagMapper({
|
1221 | expression,
|
1222 | map
|
1223 | }) {
|
1224 | const mapFn = line => {
|
1225 | if (expression.test(line)) {
|
1226 | return map(line);
|
1227 | }
|
1228 |
|
1229 | return line;
|
1230 | };
|
1231 |
|
1232 | this.tagMappers.push(mapFn);
|
1233 | }
|
1234 |
|
1235 | }
|
1236 |
|
1237 | const camelCase = str => str.toLowerCase().replace(/-(\w)/g, a => a[1].toUpperCase());
|
1238 |
|
1239 | const camelCaseKeys = function (attributes) {
|
1240 | const result = {};
|
1241 | Object.keys(attributes).forEach(function (key) {
|
1242 | result[camelCase(key)] = attributes[key];
|
1243 | });
|
1244 | return result;
|
1245 | };
|
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 |
|
1251 | const setHoldBack = function (manifest) {
|
1252 | const {
|
1253 | serverControl,
|
1254 | targetDuration,
|
1255 | partTargetDuration
|
1256 | } = manifest;
|
1257 |
|
1258 | if (!serverControl) {
|
1259 | return;
|
1260 | }
|
1261 |
|
1262 | const tag = '#EXT-X-SERVER-CONTROL';
|
1263 | const hb = 'holdBack';
|
1264 | const phb = 'partHoldBack';
|
1265 | const minTargetDuration = targetDuration && targetDuration * 3;
|
1266 | const minPartDuration = partTargetDuration && partTargetDuration * 2;
|
1267 |
|
1268 | if (targetDuration && !serverControl.hasOwnProperty(hb)) {
|
1269 | serverControl[hb] = minTargetDuration;
|
1270 | this.trigger('info', {
|
1271 | message: `${tag} defaulting HOLD-BACK to targetDuration * 3 (${minTargetDuration}).`
|
1272 | });
|
1273 | }
|
1274 |
|
1275 | if (minTargetDuration && serverControl[hb] < minTargetDuration) {
|
1276 | this.trigger('warn', {
|
1277 | message: `${tag} clamping HOLD-BACK (${serverControl[hb]}) to targetDuration * 3 (${minTargetDuration})`
|
1278 | });
|
1279 | serverControl[hb] = minTargetDuration;
|
1280 | }
|
1281 |
|
1282 |
|
1283 | if (partTargetDuration && !serverControl.hasOwnProperty(phb)) {
|
1284 | serverControl[phb] = partTargetDuration * 3;
|
1285 | this.trigger('info', {
|
1286 | message: `${tag} defaulting PART-HOLD-BACK to partTargetDuration * 3 (${serverControl[phb]}).`
|
1287 | });
|
1288 | }
|
1289 |
|
1290 |
|
1291 | if (partTargetDuration && serverControl[phb] < minPartDuration) {
|
1292 | this.trigger('warn', {
|
1293 | message: `${tag} clamping PART-HOLD-BACK (${serverControl[phb]}) to partTargetDuration * 2 (${minPartDuration}).`
|
1294 | });
|
1295 | serverControl[phb] = minPartDuration;
|
1296 | }
|
1297 | };
|
1298 | |
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 | class Parser extends Stream {
|
1322 | constructor() {
|
1323 | super();
|
1324 | this.lineStream = new LineStream();
|
1325 | this.parseStream = new ParseStream();
|
1326 | this.lineStream.pipe(this.parseStream);
|
1327 | this.lastProgramDateTime = null;
|
1328 |
|
1329 |
|
1330 | const self = this;
|
1331 |
|
1332 |
|
1333 | const uris = [];
|
1334 | let currentUri = {};
|
1335 |
|
1336 | let currentMap;
|
1337 |
|
1338 | let key;
|
1339 | let hasParts = false;
|
1340 |
|
1341 | const noop = function () {};
|
1342 |
|
1343 | const defaultMediaGroups = {
|
1344 | 'AUDIO': {},
|
1345 | 'VIDEO': {},
|
1346 | 'CLOSED-CAPTIONS': {},
|
1347 | 'SUBTITLES': {}
|
1348 | };
|
1349 |
|
1350 |
|
1351 | const widevineUuid = 'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed';
|
1352 |
|
1353 | let currentTimeline = 0;
|
1354 |
|
1355 | this.manifest = {
|
1356 | allowCache: true,
|
1357 | discontinuityStarts: [],
|
1358 | dateRanges: [],
|
1359 | segments: []
|
1360 | };
|
1361 |
|
1362 |
|
1363 |
|
1364 | let lastByterangeEnd = 0;
|
1365 |
|
1366 | let lastPartByterangeEnd = 0;
|
1367 | const dateRangeTags = {};
|
1368 | this.on('end', () => {
|
1369 |
|
1370 |
|
1371 | if (currentUri.uri || !currentUri.parts && !currentUri.preloadHints) {
|
1372 | return;
|
1373 | }
|
1374 |
|
1375 | if (!currentUri.map && currentMap) {
|
1376 | currentUri.map = currentMap;
|
1377 | }
|
1378 |
|
1379 | if (!currentUri.key && key) {
|
1380 | currentUri.key = key;
|
1381 | }
|
1382 |
|
1383 | if (!currentUri.timeline && typeof currentTimeline === 'number') {
|
1384 | currentUri.timeline = currentTimeline;
|
1385 | }
|
1386 |
|
1387 | this.manifest.preloadSegment = currentUri;
|
1388 | });
|
1389 |
|
1390 | this.parseStream.on('data', function (entry) {
|
1391 | let mediaGroup;
|
1392 | let rendition;
|
1393 | ({
|
1394 | tag() {
|
1395 |
|
1396 | (({
|
1397 | version() {
|
1398 | if (entry.version) {
|
1399 | this.manifest.version = entry.version;
|
1400 | }
|
1401 | },
|
1402 |
|
1403 | 'allow-cache'() {
|
1404 | this.manifest.allowCache = entry.allowed;
|
1405 |
|
1406 | if (!('allowed' in entry)) {
|
1407 | this.trigger('info', {
|
1408 | message: 'defaulting allowCache to YES'
|
1409 | });
|
1410 | this.manifest.allowCache = true;
|
1411 | }
|
1412 | },
|
1413 |
|
1414 | byterange() {
|
1415 | const byterange = {};
|
1416 |
|
1417 | if ('length' in entry) {
|
1418 | currentUri.byterange = byterange;
|
1419 | byterange.length = entry.length;
|
1420 |
|
1421 | if (!('offset' in entry)) {
|
1422 | |
1423 |
|
1424 |
|
1425 |
|
1426 |
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 |
|
1432 | entry.offset = lastByterangeEnd;
|
1433 | }
|
1434 | }
|
1435 |
|
1436 | if ('offset' in entry) {
|
1437 | currentUri.byterange = byterange;
|
1438 | byterange.offset = entry.offset;
|
1439 | }
|
1440 |
|
1441 | lastByterangeEnd = byterange.offset + byterange.length;
|
1442 | },
|
1443 |
|
1444 | endlist() {
|
1445 | this.manifest.endList = true;
|
1446 | },
|
1447 |
|
1448 | inf() {
|
1449 | if (!('mediaSequence' in this.manifest)) {
|
1450 | this.manifest.mediaSequence = 0;
|
1451 | this.trigger('info', {
|
1452 | message: 'defaulting media sequence to zero'
|
1453 | });
|
1454 | }
|
1455 |
|
1456 | if (!('discontinuitySequence' in this.manifest)) {
|
1457 | this.manifest.discontinuitySequence = 0;
|
1458 | this.trigger('info', {
|
1459 | message: 'defaulting discontinuity sequence to zero'
|
1460 | });
|
1461 | }
|
1462 |
|
1463 | if (entry.title) {
|
1464 | currentUri.title = entry.title;
|
1465 | }
|
1466 |
|
1467 | if (entry.duration > 0) {
|
1468 | currentUri.duration = entry.duration;
|
1469 | }
|
1470 |
|
1471 | if (entry.duration === 0) {
|
1472 | currentUri.duration = 0.01;
|
1473 | this.trigger('info', {
|
1474 | message: 'updating zero segment duration to a small value'
|
1475 | });
|
1476 | }
|
1477 |
|
1478 | this.manifest.segments = uris;
|
1479 | },
|
1480 |
|
1481 | key() {
|
1482 | if (!entry.attributes) {
|
1483 | this.trigger('warn', {
|
1484 | message: 'ignoring key declaration without attribute list'
|
1485 | });
|
1486 | return;
|
1487 | }
|
1488 |
|
1489 |
|
1490 | if (entry.attributes.METHOD === 'NONE') {
|
1491 | key = null;
|
1492 | return;
|
1493 | }
|
1494 |
|
1495 | if (!entry.attributes.URI) {
|
1496 | this.trigger('warn', {
|
1497 | message: 'ignoring key declaration without URI'
|
1498 | });
|
1499 | return;
|
1500 | }
|
1501 |
|
1502 | if (entry.attributes.KEYFORMAT === 'com.apple.streamingkeydelivery') {
|
1503 | this.manifest.contentProtection = this.manifest.contentProtection || {};
|
1504 |
|
1505 | this.manifest.contentProtection['com.apple.fps.1_0'] = {
|
1506 | attributes: entry.attributes
|
1507 | };
|
1508 | return;
|
1509 | }
|
1510 |
|
1511 | if (entry.attributes.KEYFORMAT === 'com.microsoft.playready') {
|
1512 | this.manifest.contentProtection = this.manifest.contentProtection || {};
|
1513 |
|
1514 | this.manifest.contentProtection['com.microsoft.playready'] = {
|
1515 | uri: entry.attributes.URI
|
1516 | };
|
1517 | return;
|
1518 | }
|
1519 |
|
1520 |
|
1521 |
|
1522 | if (entry.attributes.KEYFORMAT === widevineUuid) {
|
1523 | const VALID_METHODS = ['SAMPLE-AES', 'SAMPLE-AES-CTR', 'SAMPLE-AES-CENC'];
|
1524 |
|
1525 | if (VALID_METHODS.indexOf(entry.attributes.METHOD) === -1) {
|
1526 | this.trigger('warn', {
|
1527 | message: 'invalid key method provided for Widevine'
|
1528 | });
|
1529 | return;
|
1530 | }
|
1531 |
|
1532 | if (entry.attributes.METHOD === 'SAMPLE-AES-CENC') {
|
1533 | this.trigger('warn', {
|
1534 | message: 'SAMPLE-AES-CENC is deprecated, please use SAMPLE-AES-CTR instead'
|
1535 | });
|
1536 | }
|
1537 |
|
1538 | if (entry.attributes.URI.substring(0, 23) !== 'data:text/plain;base64,') {
|
1539 | this.trigger('warn', {
|
1540 | message: 'invalid key URI provided for Widevine'
|
1541 | });
|
1542 | return;
|
1543 | }
|
1544 |
|
1545 | if (!(entry.attributes.KEYID && entry.attributes.KEYID.substring(0, 2) === '0x')) {
|
1546 | this.trigger('warn', {
|
1547 | message: 'invalid key ID provided for Widevine'
|
1548 | });
|
1549 | return;
|
1550 | }
|
1551 |
|
1552 |
|
1553 |
|
1554 | this.manifest.contentProtection = this.manifest.contentProtection || {};
|
1555 | this.manifest.contentProtection['com.widevine.alpha'] = {
|
1556 | attributes: {
|
1557 | schemeIdUri: entry.attributes.KEYFORMAT,
|
1558 |
|
1559 | keyId: entry.attributes.KEYID.substring(2)
|
1560 | },
|
1561 |
|
1562 | pssh: decodeB64ToUint8Array(entry.attributes.URI.split(',')[1])
|
1563 | };
|
1564 | return;
|
1565 | }
|
1566 |
|
1567 | if (!entry.attributes.METHOD) {
|
1568 | this.trigger('warn', {
|
1569 | message: 'defaulting key method to AES-128'
|
1570 | });
|
1571 | }
|
1572 |
|
1573 |
|
1574 | key = {
|
1575 | method: entry.attributes.METHOD || 'AES-128',
|
1576 | uri: entry.attributes.URI
|
1577 | };
|
1578 |
|
1579 | if (typeof entry.attributes.IV !== 'undefined') {
|
1580 | key.iv = entry.attributes.IV;
|
1581 | }
|
1582 | },
|
1583 |
|
1584 | 'media-sequence'() {
|
1585 | if (!isFinite(entry.number)) {
|
1586 | this.trigger('warn', {
|
1587 | message: 'ignoring invalid media sequence: ' + entry.number
|
1588 | });
|
1589 | return;
|
1590 | }
|
1591 |
|
1592 | this.manifest.mediaSequence = entry.number;
|
1593 | },
|
1594 |
|
1595 | 'discontinuity-sequence'() {
|
1596 | if (!isFinite(entry.number)) {
|
1597 | this.trigger('warn', {
|
1598 | message: 'ignoring invalid discontinuity sequence: ' + entry.number
|
1599 | });
|
1600 | return;
|
1601 | }
|
1602 |
|
1603 | this.manifest.discontinuitySequence = entry.number;
|
1604 | currentTimeline = entry.number;
|
1605 | },
|
1606 |
|
1607 | 'playlist-type'() {
|
1608 | if (!/VOD|EVENT/.test(entry.playlistType)) {
|
1609 | this.trigger('warn', {
|
1610 | message: 'ignoring unknown playlist type: ' + entry.playlist
|
1611 | });
|
1612 | return;
|
1613 | }
|
1614 |
|
1615 | this.manifest.playlistType = entry.playlistType;
|
1616 | },
|
1617 |
|
1618 | map() {
|
1619 | currentMap = {};
|
1620 |
|
1621 | if (entry.uri) {
|
1622 | currentMap.uri = entry.uri;
|
1623 | }
|
1624 |
|
1625 | if (entry.byterange) {
|
1626 | currentMap.byterange = entry.byterange;
|
1627 | }
|
1628 |
|
1629 | if (key) {
|
1630 | currentMap.key = key;
|
1631 | }
|
1632 | },
|
1633 |
|
1634 | 'stream-inf'() {
|
1635 | this.manifest.playlists = uris;
|
1636 | this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
|
1637 |
|
1638 | if (!entry.attributes) {
|
1639 | this.trigger('warn', {
|
1640 | message: 'ignoring empty stream-inf attributes'
|
1641 | });
|
1642 | return;
|
1643 | }
|
1644 |
|
1645 | if (!currentUri.attributes) {
|
1646 | currentUri.attributes = {};
|
1647 | }
|
1648 |
|
1649 | _extends(currentUri.attributes, entry.attributes);
|
1650 | },
|
1651 |
|
1652 | media() {
|
1653 | this.manifest.mediaGroups = this.manifest.mediaGroups || defaultMediaGroups;
|
1654 |
|
1655 | if (!(entry.attributes && entry.attributes.TYPE && entry.attributes['GROUP-ID'] && entry.attributes.NAME)) {
|
1656 | this.trigger('warn', {
|
1657 | message: 'ignoring incomplete or missing media group'
|
1658 | });
|
1659 | return;
|
1660 | }
|
1661 |
|
1662 |
|
1663 | const mediaGroupType = this.manifest.mediaGroups[entry.attributes.TYPE];
|
1664 | mediaGroupType[entry.attributes['GROUP-ID']] = mediaGroupType[entry.attributes['GROUP-ID']] || {};
|
1665 | mediaGroup = mediaGroupType[entry.attributes['GROUP-ID']];
|
1666 |
|
1667 | rendition = {
|
1668 | default: /yes/i.test(entry.attributes.DEFAULT)
|
1669 | };
|
1670 |
|
1671 | if (rendition.default) {
|
1672 | rendition.autoselect = true;
|
1673 | } else {
|
1674 | rendition.autoselect = /yes/i.test(entry.attributes.AUTOSELECT);
|
1675 | }
|
1676 |
|
1677 | if (entry.attributes.LANGUAGE) {
|
1678 | rendition.language = entry.attributes.LANGUAGE;
|
1679 | }
|
1680 |
|
1681 | if (entry.attributes.URI) {
|
1682 | rendition.uri = entry.attributes.URI;
|
1683 | }
|
1684 |
|
1685 | if (entry.attributes['INSTREAM-ID']) {
|
1686 | rendition.instreamId = entry.attributes['INSTREAM-ID'];
|
1687 | }
|
1688 |
|
1689 | if (entry.attributes.CHARACTERISTICS) {
|
1690 | rendition.characteristics = entry.attributes.CHARACTERISTICS;
|
1691 | }
|
1692 |
|
1693 | if (entry.attributes.FORCED) {
|
1694 | rendition.forced = /yes/i.test(entry.attributes.FORCED);
|
1695 | }
|
1696 |
|
1697 |
|
1698 | mediaGroup[entry.attributes.NAME] = rendition;
|
1699 | },
|
1700 |
|
1701 | discontinuity() {
|
1702 | currentTimeline += 1;
|
1703 | currentUri.discontinuity = true;
|
1704 | this.manifest.discontinuityStarts.push(uris.length);
|
1705 | },
|
1706 |
|
1707 | 'program-date-time'() {
|
1708 | if (typeof this.manifest.dateTimeString === 'undefined') {
|
1709 |
|
1710 |
|
1711 |
|
1712 |
|
1713 | this.manifest.dateTimeString = entry.dateTimeString;
|
1714 | this.manifest.dateTimeObject = entry.dateTimeObject;
|
1715 | }
|
1716 |
|
1717 | currentUri.dateTimeString = entry.dateTimeString;
|
1718 | currentUri.dateTimeObject = entry.dateTimeObject;
|
1719 | const {
|
1720 | lastProgramDateTime
|
1721 | } = this;
|
1722 | this.lastProgramDateTime = new Date(entry.dateTimeString).getTime();
|
1723 |
|
1724 |
|
1725 | if (lastProgramDateTime === null) {
|
1726 |
|
1727 |
|
1728 |
|
1729 | this.manifest.segments.reduceRight((programDateTime, segment) => {
|
1730 | segment.programDateTime = programDateTime - segment.duration * 1000;
|
1731 | return segment.programDateTime;
|
1732 | }, this.lastProgramDateTime);
|
1733 | }
|
1734 | },
|
1735 |
|
1736 | targetduration() {
|
1737 | if (!isFinite(entry.duration) || entry.duration < 0) {
|
1738 | this.trigger('warn', {
|
1739 | message: 'ignoring invalid target duration: ' + entry.duration
|
1740 | });
|
1741 | return;
|
1742 | }
|
1743 |
|
1744 | this.manifest.targetDuration = entry.duration;
|
1745 | setHoldBack.call(this, this.manifest);
|
1746 | },
|
1747 |
|
1748 | start() {
|
1749 | if (!entry.attributes || isNaN(entry.attributes['TIME-OFFSET'])) {
|
1750 | this.trigger('warn', {
|
1751 | message: 'ignoring start declaration without appropriate attribute list'
|
1752 | });
|
1753 | return;
|
1754 | }
|
1755 |
|
1756 | this.manifest.start = {
|
1757 | timeOffset: entry.attributes['TIME-OFFSET'],
|
1758 | precise: entry.attributes.PRECISE
|
1759 | };
|
1760 | },
|
1761 |
|
1762 | 'cue-out'() {
|
1763 | currentUri.cueOut = entry.data;
|
1764 | },
|
1765 |
|
1766 | 'cue-out-cont'() {
|
1767 | currentUri.cueOutCont = entry.data;
|
1768 | },
|
1769 |
|
1770 | 'cue-in'() {
|
1771 | currentUri.cueIn = entry.data;
|
1772 | },
|
1773 |
|
1774 | 'skip'() {
|
1775 | this.manifest.skip = camelCaseKeys(entry.attributes);
|
1776 | this.warnOnMissingAttributes_('#EXT-X-SKIP', entry.attributes, ['SKIPPED-SEGMENTS']);
|
1777 | },
|
1778 |
|
1779 | 'part'() {
|
1780 | hasParts = true;
|
1781 |
|
1782 | const segmentIndex = this.manifest.segments.length;
|
1783 | const part = camelCaseKeys(entry.attributes);
|
1784 | currentUri.parts = currentUri.parts || [];
|
1785 | currentUri.parts.push(part);
|
1786 |
|
1787 | if (part.byterange) {
|
1788 | if (!part.byterange.hasOwnProperty('offset')) {
|
1789 | part.byterange.offset = lastPartByterangeEnd;
|
1790 | }
|
1791 |
|
1792 | lastPartByterangeEnd = part.byterange.offset + part.byterange.length;
|
1793 | }
|
1794 |
|
1795 | const partIndex = currentUri.parts.length - 1;
|
1796 | this.warnOnMissingAttributes_(`#EXT-X-PART #${partIndex} for segment #${segmentIndex}`, entry.attributes, ['URI', 'DURATION']);
|
1797 |
|
1798 | if (this.manifest.renditionReports) {
|
1799 | this.manifest.renditionReports.forEach((r, i) => {
|
1800 | if (!r.hasOwnProperty('lastPart')) {
|
1801 | this.trigger('warn', {
|
1802 | message: `#EXT-X-RENDITION-REPORT #${i} lacks required attribute(s): LAST-PART`
|
1803 | });
|
1804 | }
|
1805 | });
|
1806 | }
|
1807 | },
|
1808 |
|
1809 | 'server-control'() {
|
1810 | const attrs = this.manifest.serverControl = camelCaseKeys(entry.attributes);
|
1811 |
|
1812 | if (!attrs.hasOwnProperty('canBlockReload')) {
|
1813 | attrs.canBlockReload = false;
|
1814 | this.trigger('info', {
|
1815 | message: '#EXT-X-SERVER-CONTROL defaulting CAN-BLOCK-RELOAD to false'
|
1816 | });
|
1817 | }
|
1818 |
|
1819 | setHoldBack.call(this, this.manifest);
|
1820 |
|
1821 | if (attrs.canSkipDateranges && !attrs.hasOwnProperty('canSkipUntil')) {
|
1822 | this.trigger('warn', {
|
1823 | message: '#EXT-X-SERVER-CONTROL lacks required attribute CAN-SKIP-UNTIL which is required when CAN-SKIP-DATERANGES is set'
|
1824 | });
|
1825 | }
|
1826 | },
|
1827 |
|
1828 | 'preload-hint'() {
|
1829 |
|
1830 | const segmentIndex = this.manifest.segments.length;
|
1831 | const hint = camelCaseKeys(entry.attributes);
|
1832 | const isPart = hint.type && hint.type === 'PART';
|
1833 | currentUri.preloadHints = currentUri.preloadHints || [];
|
1834 | currentUri.preloadHints.push(hint);
|
1835 |
|
1836 | if (hint.byterange) {
|
1837 | if (!hint.byterange.hasOwnProperty('offset')) {
|
1838 |
|
1839 | hint.byterange.offset = isPart ? lastPartByterangeEnd : 0;
|
1840 |
|
1841 | if (isPart) {
|
1842 | lastPartByterangeEnd = hint.byterange.offset + hint.byterange.length;
|
1843 | }
|
1844 | }
|
1845 | }
|
1846 |
|
1847 | const index = currentUri.preloadHints.length - 1;
|
1848 | this.warnOnMissingAttributes_(`#EXT-X-PRELOAD-HINT #${index} for segment #${segmentIndex}`, entry.attributes, ['TYPE', 'URI']);
|
1849 |
|
1850 | if (!hint.type) {
|
1851 | return;
|
1852 | }
|
1853 |
|
1854 |
|
1855 |
|
1856 | for (let i = 0; i < currentUri.preloadHints.length - 1; i++) {
|
1857 | const otherHint = currentUri.preloadHints[i];
|
1858 |
|
1859 | if (!otherHint.type) {
|
1860 | continue;
|
1861 | }
|
1862 |
|
1863 | if (otherHint.type === hint.type) {
|
1864 | this.trigger('warn', {
|
1865 | message: `#EXT-X-PRELOAD-HINT #${index} for segment #${segmentIndex} has the same TYPE ${hint.type} as preload hint #${i}`
|
1866 | });
|
1867 | }
|
1868 | }
|
1869 | },
|
1870 |
|
1871 | 'rendition-report'() {
|
1872 | const report = camelCaseKeys(entry.attributes);
|
1873 | this.manifest.renditionReports = this.manifest.renditionReports || [];
|
1874 | this.manifest.renditionReports.push(report);
|
1875 | const index = this.manifest.renditionReports.length - 1;
|
1876 | const required = ['LAST-MSN', 'URI'];
|
1877 |
|
1878 | if (hasParts) {
|
1879 | required.push('LAST-PART');
|
1880 | }
|
1881 |
|
1882 | this.warnOnMissingAttributes_(`#EXT-X-RENDITION-REPORT #${index}`, entry.attributes, required);
|
1883 | },
|
1884 |
|
1885 | 'part-inf'() {
|
1886 | this.manifest.partInf = camelCaseKeys(entry.attributes);
|
1887 | this.warnOnMissingAttributes_('#EXT-X-PART-INF', entry.attributes, ['PART-TARGET']);
|
1888 |
|
1889 | if (this.manifest.partInf.partTarget) {
|
1890 | this.manifest.partTargetDuration = this.manifest.partInf.partTarget;
|
1891 | }
|
1892 |
|
1893 | setHoldBack.call(this, this.manifest);
|
1894 | },
|
1895 |
|
1896 | 'daterange'() {
|
1897 | this.manifest.dateRanges.push(camelCaseKeys(entry.attributes));
|
1898 | const index = this.manifest.dateRanges.length - 1;
|
1899 | this.warnOnMissingAttributes_(`#EXT-X-DATERANGE #${index}`, entry.attributes, ['ID', 'START-DATE']);
|
1900 | const dateRange = this.manifest.dateRanges[index];
|
1901 |
|
1902 | if (dateRange.endDate && dateRange.startDate && new Date(dateRange.endDate) < new Date(dateRange.startDate)) {
|
1903 | this.trigger('warn', {
|
1904 | message: 'EXT-X-DATERANGE END-DATE must be equal to or later than the value of the START-DATE'
|
1905 | });
|
1906 | }
|
1907 |
|
1908 | if (dateRange.duration && dateRange.duration < 0) {
|
1909 | this.trigger('warn', {
|
1910 | message: 'EXT-X-DATERANGE DURATION must not be negative'
|
1911 | });
|
1912 | }
|
1913 |
|
1914 | if (dateRange.plannedDuration && dateRange.plannedDuration < 0) {
|
1915 | this.trigger('warn', {
|
1916 | message: 'EXT-X-DATERANGE PLANNED-DURATION must not be negative'
|
1917 | });
|
1918 | }
|
1919 |
|
1920 | const endOnNextYes = !!dateRange.endOnNext;
|
1921 |
|
1922 | if (endOnNextYes && !dateRange.class) {
|
1923 | this.trigger('warn', {
|
1924 | message: 'EXT-X-DATERANGE with an END-ON-NEXT=YES attribute must have a CLASS attribute'
|
1925 | });
|
1926 | }
|
1927 |
|
1928 | if (endOnNextYes && (dateRange.duration || dateRange.endDate)) {
|
1929 | this.trigger('warn', {
|
1930 | message: 'EXT-X-DATERANGE with an END-ON-NEXT=YES attribute must not contain DURATION or END-DATE attributes'
|
1931 | });
|
1932 | }
|
1933 |
|
1934 | if (dateRange.duration && dateRange.endDate) {
|
1935 | const startDate = dateRange.startDate;
|
1936 | const newDateInSeconds = startDate.getTime() + dateRange.duration * 1000;
|
1937 | this.manifest.dateRanges[index].endDate = new Date(newDateInSeconds);
|
1938 | }
|
1939 |
|
1940 | if (!dateRangeTags[dateRange.id]) {
|
1941 | dateRangeTags[dateRange.id] = dateRange;
|
1942 | } else {
|
1943 | for (const attribute in dateRangeTags[dateRange.id]) {
|
1944 | if (!!dateRange[attribute] && JSON.stringify(dateRangeTags[dateRange.id][attribute]) !== JSON.stringify(dateRange[attribute])) {
|
1945 | this.trigger('warn', {
|
1946 | message: 'EXT-X-DATERANGE tags with the same ID in a playlist must have the same attributes values'
|
1947 | });
|
1948 | break;
|
1949 | }
|
1950 | }
|
1951 |
|
1952 |
|
1953 | const dateRangeWithSameId = this.manifest.dateRanges.findIndex(dateRangeToFind => dateRangeToFind.id === dateRange.id);
|
1954 | this.manifest.dateRanges[dateRangeWithSameId] = _extends(this.manifest.dateRanges[dateRangeWithSameId], dateRange);
|
1955 | dateRangeTags[dateRange.id] = _extends(dateRangeTags[dateRange.id], dateRange);
|
1956 |
|
1957 | this.manifest.dateRanges.pop();
|
1958 | }
|
1959 | },
|
1960 |
|
1961 | 'independent-segments'() {
|
1962 | this.manifest.independentSegments = true;
|
1963 | },
|
1964 |
|
1965 | 'content-steering'() {
|
1966 | this.manifest.contentSteering = camelCaseKeys(entry.attributes);
|
1967 | this.warnOnMissingAttributes_('#EXT-X-CONTENT-STEERING', entry.attributes, ['SERVER-URI']);
|
1968 | }
|
1969 |
|
1970 | })[entry.tagType] || noop).call(self);
|
1971 | },
|
1972 |
|
1973 | uri() {
|
1974 | currentUri.uri = entry.uri;
|
1975 | uris.push(currentUri);
|
1976 |
|
1977 | if (this.manifest.targetDuration && !('duration' in currentUri)) {
|
1978 | this.trigger('warn', {
|
1979 | message: 'defaulting segment duration to the target duration'
|
1980 | });
|
1981 | currentUri.duration = this.manifest.targetDuration;
|
1982 | }
|
1983 |
|
1984 |
|
1985 | if (key) {
|
1986 | currentUri.key = key;
|
1987 | }
|
1988 |
|
1989 | currentUri.timeline = currentTimeline;
|
1990 |
|
1991 | if (currentMap) {
|
1992 | currentUri.map = currentMap;
|
1993 | }
|
1994 |
|
1995 |
|
1996 | lastPartByterangeEnd = 0;
|
1997 |
|
1998 | if (this.lastProgramDateTime !== null) {
|
1999 | currentUri.programDateTime = this.lastProgramDateTime;
|
2000 | this.lastProgramDateTime += currentUri.duration * 1000;
|
2001 | }
|
2002 |
|
2003 |
|
2004 | currentUri = {};
|
2005 | },
|
2006 |
|
2007 | comment() {
|
2008 | },
|
2009 |
|
2010 | custom() {
|
2011 |
|
2012 | if (entry.segment) {
|
2013 | currentUri.custom = currentUri.custom || {};
|
2014 | currentUri.custom[entry.customType] = entry.data;
|
2015 | } else {
|
2016 | this.manifest.custom = this.manifest.custom || {};
|
2017 | this.manifest.custom[entry.customType] = entry.data;
|
2018 | }
|
2019 | }
|
2020 |
|
2021 | })[entry.type].call(self);
|
2022 | });
|
2023 | }
|
2024 |
|
2025 | warnOnMissingAttributes_(identifier, attributes, required) {
|
2026 | const missing = [];
|
2027 | required.forEach(function (key) {
|
2028 | if (!attributes.hasOwnProperty(key)) {
|
2029 | missing.push(key);
|
2030 | }
|
2031 | });
|
2032 |
|
2033 | if (missing.length) {
|
2034 | this.trigger('warn', {
|
2035 | message: `${identifier} lacks required attribute(s): ${missing.join(', ')}`
|
2036 | });
|
2037 | }
|
2038 | }
|
2039 | |
2040 |
|
2041 |
|
2042 |
|
2043 |
|
2044 |
|
2045 |
|
2046 | push(chunk) {
|
2047 | this.lineStream.push(chunk);
|
2048 | }
|
2049 | |
2050 |
|
2051 |
|
2052 |
|
2053 |
|
2054 |
|
2055 |
|
2056 | end() {
|
2057 |
|
2058 | this.lineStream.push('\n');
|
2059 |
|
2060 | if (this.manifest.dateRanges.length && this.lastProgramDateTime === null) {
|
2061 | this.trigger('warn', {
|
2062 | message: 'A playlist with EXT-X-DATERANGE tag must contain atleast one EXT-X-PROGRAM-DATE-TIME tag'
|
2063 | });
|
2064 | }
|
2065 |
|
2066 | this.lastProgramDateTime = null;
|
2067 | this.trigger('end');
|
2068 | }
|
2069 | |
2070 |
|
2071 |
|
2072 |
|
2073 |
|
2074 |
|
2075 |
|
2076 |
|
2077 |
|
2078 |
|
2079 |
|
2080 | addParser(options) {
|
2081 | this.parseStream.addParser(options);
|
2082 | }
|
2083 | |
2084 |
|
2085 |
|
2086 |
|
2087 |
|
2088 |
|
2089 |
|
2090 |
|
2091 |
|
2092 | addTagMapper(options) {
|
2093 | this.parseStream.addTagMapper(options);
|
2094 | }
|
2095 |
|
2096 | }
|
2097 |
|
2098 | var regexs = {
|
2099 |
|
2100 | mp4: /^(av0?1|avc0?[1234]|vp0?9|flac|opus|mp3|mp4a|mp4v|stpp.ttml.im1t)/,
|
2101 | webm: /^(vp0?[89]|av0?1|opus|vorbis)/,
|
2102 | ogg: /^(vp0?[89]|theora|flac|opus|vorbis)/,
|
2103 |
|
2104 | video: /^(av0?1|avc0?[1234]|vp0?[89]|hvc1|hev1|theora|mp4v)/,
|
2105 | audio: /^(mp4a|flac|vorbis|opus|ac-[34]|ec-3|alac|mp3|speex|aac)/,
|
2106 | text: /^(stpp.ttml.im1t)/,
|
2107 |
|
2108 | muxerVideo: /^(avc0?1)/,
|
2109 | muxerAudio: /^(mp4a)/,
|
2110 |
|
2111 |
|
2112 |
|
2113 | muxerText: /a^/
|
2114 | };
|
2115 | var mediaTypes = ['video', 'audio', 'text'];
|
2116 | var upperMediaTypes = ['Video', 'Audio', 'Text'];
|
2117 | |
2118 |
|
2119 |
|
2120 |
|
2121 |
|
2122 |
|
2123 |
|
2124 |
|
2125 |
|
2126 |
|
2127 | var translateLegacyCodec = function translateLegacyCodec(codec) {
|
2128 | if (!codec) {
|
2129 | return codec;
|
2130 | }
|
2131 |
|
2132 | return codec.replace(/avc1\.(\d+)\.(\d+)/i, function (orig, profile, avcLevel) {
|
2133 | var profileHex = ('00' + Number(profile).toString(16)).slice(-2);
|
2134 | var avcLevelHex = ('00' + Number(avcLevel).toString(16)).slice(-2);
|
2135 | return 'avc1.' + profileHex + '00' + avcLevelHex;
|
2136 | });
|
2137 | };
|
2138 | |
2139 |
|
2140 |
|
2141 |
|
2142 |
|
2143 |
|
2144 |
|
2145 |
|
2146 |
|
2147 |
|
2148 |
|
2149 |
|
2150 | |
2151 |
|
2152 |
|
2153 |
|
2154 |
|
2155 |
|
2156 |
|
2157 |
|
2158 |
|
2159 |
|
2160 | var parseCodecs = function parseCodecs(codecString) {
|
2161 | if (codecString === void 0) {
|
2162 | codecString = '';
|
2163 | }
|
2164 |
|
2165 | var codecs = codecString.split(',');
|
2166 | var result = [];
|
2167 | codecs.forEach(function (codec) {
|
2168 | codec = codec.trim();
|
2169 | var codecType;
|
2170 | mediaTypes.forEach(function (name) {
|
2171 | var match = regexs[name].exec(codec.toLowerCase());
|
2172 |
|
2173 | if (!match || match.length <= 1) {
|
2174 | return;
|
2175 | }
|
2176 |
|
2177 | codecType = name;
|
2178 |
|
2179 | var type = codec.substring(0, match[1].length);
|
2180 | var details = codec.replace(type, '');
|
2181 | result.push({
|
2182 | type: type,
|
2183 | details: details,
|
2184 | mediaType: name
|
2185 | });
|
2186 | });
|
2187 |
|
2188 | if (!codecType) {
|
2189 | result.push({
|
2190 | type: codec,
|
2191 | details: '',
|
2192 | mediaType: 'unknown'
|
2193 | });
|
2194 | }
|
2195 | });
|
2196 | return result;
|
2197 | };
|
2198 | |
2199 |
|
2200 |
|
2201 |
|
2202 |
|
2203 |
|
2204 |
|
2205 |
|
2206 |
|
2207 |
|
2208 |
|
2209 |
|
2210 | var codecsFromDefault = function codecsFromDefault(master, audioGroupId) {
|
2211 | if (!master.mediaGroups.AUDIO || !audioGroupId) {
|
2212 | return null;
|
2213 | }
|
2214 |
|
2215 | var audioGroup = master.mediaGroups.AUDIO[audioGroupId];
|
2216 |
|
2217 | if (!audioGroup) {
|
2218 | return null;
|
2219 | }
|
2220 |
|
2221 | for (var name in audioGroup) {
|
2222 | var audioType = audioGroup[name];
|
2223 |
|
2224 | if (audioType.default && audioType.playlists) {
|
2225 |
|
2226 | return parseCodecs(audioType.playlists[0].attributes.CODECS);
|
2227 | }
|
2228 | }
|
2229 |
|
2230 | return null;
|
2231 | };
|
2232 | var isAudioCodec = function isAudioCodec(codec) {
|
2233 | if (codec === void 0) {
|
2234 | codec = '';
|
2235 | }
|
2236 |
|
2237 | return regexs.audio.test(codec.trim().toLowerCase());
|
2238 | };
|
2239 | var isTextCodec = function isTextCodec(codec) {
|
2240 | if (codec === void 0) {
|
2241 | codec = '';
|
2242 | }
|
2243 |
|
2244 | return regexs.text.test(codec.trim().toLowerCase());
|
2245 | };
|
2246 | var getMimeForCodec = function getMimeForCodec(codecString) {
|
2247 | if (!codecString || typeof codecString !== 'string') {
|
2248 | return;
|
2249 | }
|
2250 |
|
2251 | var codecs = codecString.toLowerCase().split(',').map(function (c) {
|
2252 | return translateLegacyCodec(c.trim());
|
2253 | });
|
2254 |
|
2255 | var type = 'video';
|
2256 |
|
2257 |
|
2258 | if (codecs.length === 1 && isAudioCodec(codecs[0])) {
|
2259 | type = 'audio';
|
2260 | } else if (codecs.length === 1 && isTextCodec(codecs[0])) {
|
2261 |
|
2262 | type = 'application';
|
2263 | }
|
2264 |
|
2265 |
|
2266 | var container = 'mp4';
|
2267 |
|
2268 |
|
2269 | if (codecs.every(function (c) {
|
2270 | return regexs.mp4.test(c);
|
2271 | })) {
|
2272 | container = 'mp4';
|
2273 | } else if (codecs.every(function (c) {
|
2274 | return regexs.webm.test(c);
|
2275 | })) {
|
2276 | container = 'webm';
|
2277 | } else if (codecs.every(function (c) {
|
2278 | return regexs.ogg.test(c);
|
2279 | })) {
|
2280 | container = 'ogg';
|
2281 | }
|
2282 |
|
2283 | return type + "/" + container + ";codecs=\"" + codecString + "\"";
|
2284 | };
|
2285 | var browserSupportsCodec = function browserSupportsCodec(codecString) {
|
2286 | if (codecString === void 0) {
|
2287 | codecString = '';
|
2288 | }
|
2289 |
|
2290 | return window.MediaSource && window.MediaSource.isTypeSupported && window.MediaSource.isTypeSupported(getMimeForCodec(codecString)) || false;
|
2291 | };
|
2292 | var muxerSupportsCodec = function muxerSupportsCodec(codecString) {
|
2293 | if (codecString === void 0) {
|
2294 | codecString = '';
|
2295 | }
|
2296 |
|
2297 | return codecString.toLowerCase().split(',').every(function (codec) {
|
2298 | codec = codec.trim();
|
2299 |
|
2300 | for (var i = 0; i < upperMediaTypes.length; i++) {
|
2301 | var type = upperMediaTypes[i];
|
2302 |
|
2303 | if (regexs["muxer" + type].test(codec)) {
|
2304 | return true;
|
2305 | }
|
2306 | }
|
2307 |
|
2308 | return false;
|
2309 | });
|
2310 | };
|
2311 | var DEFAULT_AUDIO_CODEC = 'mp4a.40.2';
|
2312 | var DEFAULT_VIDEO_CODEC = 'avc1.4d400d';
|
2313 |
|
2314 | |
2315 |
|
2316 |
|
2317 | |
2318 |
|
2319 |
|
2320 |
|
2321 |
|
2322 | function merge$1(...args) {
|
2323 | const context = videojs__default["default"].obj || videojs__default["default"];
|
2324 | const fn = context.merge || context.mergeOptions;
|
2325 | return fn.apply(context, args);
|
2326 | }
|
2327 | |
2328 |
|
2329 |
|
2330 |
|
2331 |
|
2332 | function createTimeRanges(...args) {
|
2333 | const context = videojs__default["default"].time || videojs__default["default"];
|
2334 | const fn = context.createTimeRanges || context.createTimeRanges;
|
2335 | return fn.apply(context, args);
|
2336 | }
|
2337 |
|
2338 | |
2339 |
|
2340 |
|
2341 |
|
2342 |
|
2343 |
|
2344 |
|
2345 | const TIME_FUDGE_FACTOR = 1 / 30;
|
2346 |
|
2347 |
|
2348 |
|
2349 |
|
2350 |
|
2351 | const SAFE_TIME_DELTA = TIME_FUDGE_FACTOR * 3;
|
2352 |
|
2353 | const filterRanges = function (timeRanges, predicate) {
|
2354 | const results = [];
|
2355 | let i;
|
2356 |
|
2357 | if (timeRanges && timeRanges.length) {
|
2358 |
|
2359 | for (i = 0; i < timeRanges.length; i++) {
|
2360 | if (predicate(timeRanges.start(i), timeRanges.end(i))) {
|
2361 | results.push([timeRanges.start(i), timeRanges.end(i)]);
|
2362 | }
|
2363 | }
|
2364 | }
|
2365 |
|
2366 | return createTimeRanges(results);
|
2367 | };
|
2368 | |
2369 |
|
2370 |
|
2371 |
|
2372 |
|
2373 |
|
2374 |
|
2375 |
|
2376 |
|
2377 |
|
2378 | const findRange = function (buffered, time) {
|
2379 | return filterRanges(buffered, function (start, end) {
|
2380 | return start - SAFE_TIME_DELTA <= time && end + SAFE_TIME_DELTA >= time;
|
2381 | });
|
2382 | };
|
2383 | |
2384 |
|
2385 |
|
2386 |
|
2387 |
|
2388 |
|
2389 |
|
2390 |
|
2391 | const findNextRange = function (timeRanges, time) {
|
2392 | return filterRanges(timeRanges, function (start) {
|
2393 | return start - TIME_FUDGE_FACTOR >= time;
|
2394 | });
|
2395 | };
|
2396 | |
2397 |
|
2398 |
|
2399 |
|
2400 |
|
2401 |
|
2402 |
|
2403 | const findGaps = function (buffered) {
|
2404 | if (buffered.length < 2) {
|
2405 | return createTimeRanges();
|
2406 | }
|
2407 |
|
2408 | const ranges = [];
|
2409 |
|
2410 | for (let i = 1; i < buffered.length; i++) {
|
2411 | const start = buffered.end(i - 1);
|
2412 | const end = buffered.start(i);
|
2413 | ranges.push([start, end]);
|
2414 | }
|
2415 |
|
2416 | return createTimeRanges(ranges);
|
2417 | };
|
2418 | |
2419 |
|
2420 |
|
2421 |
|
2422 |
|
2423 |
|
2424 |
|
2425 |
|
2426 | const bufferIntersection = function (bufferA, bufferB) {
|
2427 | let start = null;
|
2428 | let end = null;
|
2429 | let arity = 0;
|
2430 | const extents = [];
|
2431 | const ranges = [];
|
2432 |
|
2433 | if (!bufferA || !bufferA.length || !bufferB || !bufferB.length) {
|
2434 | return createTimeRanges();
|
2435 | }
|
2436 |
|
2437 |
|
2438 |
|
2439 | let count = bufferA.length;
|
2440 |
|
2441 | while (count--) {
|
2442 | extents.push({
|
2443 | time: bufferA.start(count),
|
2444 | type: 'start'
|
2445 | });
|
2446 | extents.push({
|
2447 | time: bufferA.end(count),
|
2448 | type: 'end'
|
2449 | });
|
2450 | }
|
2451 |
|
2452 | count = bufferB.length;
|
2453 |
|
2454 | while (count--) {
|
2455 | extents.push({
|
2456 | time: bufferB.start(count),
|
2457 | type: 'start'
|
2458 | });
|
2459 | extents.push({
|
2460 | time: bufferB.end(count),
|
2461 | type: 'end'
|
2462 | });
|
2463 | }
|
2464 |
|
2465 |
|
2466 | extents.sort(function (a, b) {
|
2467 | return a.time - b.time;
|
2468 | });
|
2469 |
|
2470 |
|
2471 | for (count = 0; count < extents.length; count++) {
|
2472 | if (extents[count].type === 'start') {
|
2473 | arity++;
|
2474 |
|
2475 |
|
2476 | if (arity === 2) {
|
2477 | start = extents[count].time;
|
2478 | }
|
2479 | } else if (extents[count].type === 'end') {
|
2480 | arity--;
|
2481 |
|
2482 |
|
2483 | if (arity === 1) {
|
2484 | end = extents[count].time;
|
2485 | }
|
2486 | }
|
2487 |
|
2488 |
|
2489 | if (start !== null && end !== null) {
|
2490 | ranges.push([start, end]);
|
2491 | start = null;
|
2492 | end = null;
|
2493 | }
|
2494 | }
|
2495 |
|
2496 | return createTimeRanges(ranges);
|
2497 | };
|
2498 | |
2499 |
|
2500 |
|
2501 |
|
2502 |
|
2503 |
|
2504 |
|
2505 | const printableRange = range => {
|
2506 | const strArr = [];
|
2507 |
|
2508 | if (!range || !range.length) {
|
2509 | return '';
|
2510 | }
|
2511 |
|
2512 | for (let i = 0; i < range.length; i++) {
|
2513 | strArr.push(range.start(i) + ' => ' + range.end(i));
|
2514 | }
|
2515 |
|
2516 | return strArr.join(', ');
|
2517 | };
|
2518 | |
2519 |
|
2520 |
|
2521 |
|
2522 |
|
2523 |
|
2524 |
|
2525 |
|
2526 |
|
2527 |
|
2528 |
|
2529 |
|
2530 |
|
2531 |
|
2532 |
|
2533 | const timeUntilRebuffer = function (buffered, currentTime, playbackRate = 1) {
|
2534 | const bufferedEnd = buffered.length ? buffered.end(buffered.length - 1) : 0;
|
2535 | return (bufferedEnd - currentTime) / playbackRate;
|
2536 | };
|
2537 | |
2538 |
|
2539 |
|
2540 |
|
2541 |
|
2542 |
|
2543 |
|
2544 | const timeRangesToArray = timeRanges => {
|
2545 | const timeRangesList = [];
|
2546 |
|
2547 | for (let i = 0; i < timeRanges.length; i++) {
|
2548 | timeRangesList.push({
|
2549 | start: timeRanges.start(i),
|
2550 | end: timeRanges.end(i)
|
2551 | });
|
2552 | }
|
2553 |
|
2554 | return timeRangesList;
|
2555 | };
|
2556 | |
2557 |
|
2558 |
|
2559 |
|
2560 |
|
2561 |
|
2562 |
|
2563 |
|
2564 |
|
2565 |
|
2566 |
|
2567 |
|
2568 |
|
2569 | const isRangeDifferent = function (a, b) {
|
2570 |
|
2571 | if (a === b) {
|
2572 | return false;
|
2573 | }
|
2574 |
|
2575 |
|
2576 | if (!a && b || !b && a) {
|
2577 | return true;
|
2578 | }
|
2579 |
|
2580 |
|
2581 | if (a.length !== b.length) {
|
2582 | return true;
|
2583 | }
|
2584 |
|
2585 |
|
2586 | for (let i = 0; i < a.length; i++) {
|
2587 | if (a.start(i) !== b.start(i) || a.end(i) !== b.end(i)) {
|
2588 | return true;
|
2589 | }
|
2590 | }
|
2591 |
|
2592 |
|
2593 |
|
2594 | return false;
|
2595 | };
|
2596 | const lastBufferedEnd = function (a) {
|
2597 | if (!a || !a.length || !a.end) {
|
2598 | return;
|
2599 | }
|
2600 |
|
2601 | return a.end(a.length - 1);
|
2602 | };
|
2603 | |
2604 |
|
2605 |
|
2606 |
|
2607 |
|
2608 |
|
2609 |
|
2610 |
|
2611 |
|
2612 |
|
2613 |
|
2614 |
|
2615 |
|
2616 |
|
2617 |
|
2618 | const timeAheadOf = function (range, startTime) {
|
2619 | let time = 0;
|
2620 |
|
2621 | if (!range || !range.length) {
|
2622 | return time;
|
2623 | }
|
2624 |
|
2625 | for (let i = 0; i < range.length; i++) {
|
2626 | const start = range.start(i);
|
2627 | const end = range.end(i);
|
2628 |
|
2629 | if (startTime > end) {
|
2630 | continue;
|
2631 | }
|
2632 |
|
2633 |
|
2634 | if (startTime > start && startTime <= end) {
|
2635 | time += end - startTime;
|
2636 | continue;
|
2637 | }
|
2638 |
|
2639 |
|
2640 | time += end - start;
|
2641 | }
|
2642 |
|
2643 | return time;
|
2644 | };
|
2645 |
|
2646 | |
2647 |
|
2648 |
|
2649 |
|
2650 |
|
2651 | |
2652 |
|
2653 |
|
2654 |
|
2655 |
|
2656 |
|
2657 |
|
2658 |
|
2659 |
|
2660 |
|
2661 |
|
2662 |
|
2663 |
|
2664 | const segmentDurationWithParts = (playlist, segment) => {
|
2665 |
|
2666 |
|
2667 | if (!segment.preload) {
|
2668 | return segment.duration;
|
2669 | }
|
2670 |
|
2671 |
|
2672 |
|
2673 | let result = 0;
|
2674 | (segment.parts || []).forEach(function (p) {
|
2675 | result += p.duration;
|
2676 | });
|
2677 |
|
2678 |
|
2679 | (segment.preloadHints || []).forEach(function (p) {
|
2680 | if (p.type === 'PART') {
|
2681 | result += playlist.partTargetDuration;
|
2682 | }
|
2683 | });
|
2684 | return result;
|
2685 | };
|
2686 | |
2687 |
|
2688 |
|
2689 |
|
2690 |
|
2691 |
|
2692 |
|
2693 |
|
2694 |
|
2695 | const getPartsAndSegments = playlist => (playlist.segments || []).reduce((acc, segment, si) => {
|
2696 | if (segment.parts) {
|
2697 | segment.parts.forEach(function (part, pi) {
|
2698 | acc.push({
|
2699 | duration: part.duration,
|
2700 | segmentIndex: si,
|
2701 | partIndex: pi,
|
2702 | part,
|
2703 | segment
|
2704 | });
|
2705 | });
|
2706 | } else {
|
2707 | acc.push({
|
2708 | duration: segment.duration,
|
2709 | segmentIndex: si,
|
2710 | partIndex: null,
|
2711 | segment,
|
2712 | part: null
|
2713 | });
|
2714 | }
|
2715 |
|
2716 | return acc;
|
2717 | }, []);
|
2718 | const getLastParts = media => {
|
2719 | const lastSegment = media.segments && media.segments.length && media.segments[media.segments.length - 1];
|
2720 | return lastSegment && lastSegment.parts || [];
|
2721 | };
|
2722 | const getKnownPartCount = ({
|
2723 | preloadSegment
|
2724 | }) => {
|
2725 | if (!preloadSegment) {
|
2726 | return;
|
2727 | }
|
2728 |
|
2729 | const {
|
2730 | parts,
|
2731 | preloadHints
|
2732 | } = preloadSegment;
|
2733 | let partCount = (preloadHints || []).reduce((count, hint) => count + (hint.type === 'PART' ? 1 : 0), 0);
|
2734 | partCount += parts && parts.length ? parts.length : 0;
|
2735 | return partCount;
|
2736 | };
|
2737 | |
2738 |
|
2739 |
|
2740 |
|
2741 |
|
2742 |
|
2743 |
|
2744 |
|
2745 |
|
2746 | const liveEdgeDelay = (main, media) => {
|
2747 | if (media.endList) {
|
2748 | return 0;
|
2749 | }
|
2750 |
|
2751 |
|
2752 | if (main && main.suggestedPresentationDelay) {
|
2753 | return main.suggestedPresentationDelay;
|
2754 | }
|
2755 |
|
2756 | const hasParts = getLastParts(media).length > 0;
|
2757 |
|
2758 | if (hasParts && media.serverControl && media.serverControl.partHoldBack) {
|
2759 | return media.serverControl.partHoldBack;
|
2760 | } else if (hasParts && media.partTargetDuration) {
|
2761 | return media.partTargetDuration * 3;
|
2762 | } else if (media.serverControl && media.serverControl.holdBack) {
|
2763 | return media.serverControl.holdBack;
|
2764 | } else if (media.targetDuration) {
|
2765 | return media.targetDuration * 3;
|
2766 | }
|
2767 |
|
2768 | return 0;
|
2769 | };
|
2770 | |
2771 |
|
2772 |
|
2773 |
|
2774 |
|
2775 |
|
2776 |
|
2777 |
|
2778 | const backwardDuration = function (playlist, endSequence) {
|
2779 | let result = 0;
|
2780 | let i = endSequence - playlist.mediaSequence;
|
2781 |
|
2782 |
|
2783 | let segment = playlist.segments[i];
|
2784 |
|
2785 |
|
2786 | if (segment) {
|
2787 | if (typeof segment.start !== 'undefined') {
|
2788 | return {
|
2789 | result: segment.start,
|
2790 | precise: true
|
2791 | };
|
2792 | }
|
2793 |
|
2794 | if (typeof segment.end !== 'undefined') {
|
2795 | return {
|
2796 | result: segment.end - segment.duration,
|
2797 | precise: true
|
2798 | };
|
2799 | }
|
2800 | }
|
2801 |
|
2802 | while (i--) {
|
2803 | segment = playlist.segments[i];
|
2804 |
|
2805 | if (typeof segment.end !== 'undefined') {
|
2806 | return {
|
2807 | result: result + segment.end,
|
2808 | precise: true
|
2809 | };
|
2810 | }
|
2811 |
|
2812 | result += segmentDurationWithParts(playlist, segment);
|
2813 |
|
2814 | if (typeof segment.start !== 'undefined') {
|
2815 | return {
|
2816 | result: result + segment.start,
|
2817 | precise: true
|
2818 | };
|
2819 | }
|
2820 | }
|
2821 |
|
2822 | return {
|
2823 | result,
|
2824 | precise: false
|
2825 | };
|
2826 | };
|
2827 | |
2828 |
|
2829 |
|
2830 |
|
2831 |
|
2832 |
|
2833 |
|
2834 |
|
2835 |
|
2836 | const forwardDuration = function (playlist, endSequence) {
|
2837 | let result = 0;
|
2838 | let segment;
|
2839 | let i = endSequence - playlist.mediaSequence;
|
2840 |
|
2841 |
|
2842 | for (; i < playlist.segments.length; i++) {
|
2843 | segment = playlist.segments[i];
|
2844 |
|
2845 | if (typeof segment.start !== 'undefined') {
|
2846 | return {
|
2847 | result: segment.start - result,
|
2848 | precise: true
|
2849 | };
|
2850 | }
|
2851 |
|
2852 | result += segmentDurationWithParts(playlist, segment);
|
2853 |
|
2854 | if (typeof segment.end !== 'undefined') {
|
2855 | return {
|
2856 | result: segment.end - result,
|
2857 | precise: true
|
2858 | };
|
2859 | }
|
2860 | }
|
2861 |
|
2862 |
|
2863 | return {
|
2864 | result: -1,
|
2865 | precise: false
|
2866 | };
|
2867 | };
|
2868 | |
2869 |
|
2870 |
|
2871 |
|
2872 |
|
2873 |
|
2874 |
|
2875 |
|
2876 |
|
2877 |
|
2878 |
|
2879 |
|
2880 |
|
2881 |
|
2882 |
|
2883 | const intervalDuration = function (playlist, endSequence, expired) {
|
2884 | if (typeof endSequence === 'undefined') {
|
2885 | endSequence = playlist.mediaSequence + playlist.segments.length;
|
2886 | }
|
2887 |
|
2888 | if (endSequence < playlist.mediaSequence) {
|
2889 | return 0;
|
2890 | }
|
2891 |
|
2892 |
|
2893 | const backward = backwardDuration(playlist, endSequence);
|
2894 |
|
2895 | if (backward.precise) {
|
2896 |
|
2897 |
|
2898 |
|
2899 | return backward.result;
|
2900 | }
|
2901 |
|
2902 |
|
2903 |
|
2904 | const forward = forwardDuration(playlist, endSequence);
|
2905 |
|
2906 | if (forward.precise) {
|
2907 |
|
2908 |
|
2909 | return forward.result;
|
2910 | }
|
2911 |
|
2912 |
|
2913 | return backward.result + expired;
|
2914 | };
|
2915 | |
2916 |
|
2917 |
|
2918 |
|
2919 |
|
2920 |
|
2921 |
|
2922 |
|
2923 |
|
2924 |
|
2925 |
|
2926 |
|
2927 |
|
2928 |
|
2929 |
|
2930 |
|
2931 |
|
2932 | const duration = function (playlist, endSequence, expired) {
|
2933 | if (!playlist) {
|
2934 | return 0;
|
2935 | }
|
2936 |
|
2937 | if (typeof expired !== 'number') {
|
2938 | expired = 0;
|
2939 | }
|
2940 |
|
2941 |
|
2942 |
|
2943 | if (typeof endSequence === 'undefined') {
|
2944 |
|
2945 | if (playlist.totalDuration) {
|
2946 | return playlist.totalDuration;
|
2947 | }
|
2948 |
|
2949 |
|
2950 | if (!playlist.endList) {
|
2951 | return window.Infinity;
|
2952 | }
|
2953 | }
|
2954 |
|
2955 |
|
2956 | return intervalDuration(playlist, endSequence, expired);
|
2957 | };
|
2958 | |
2959 |
|
2960 |
|
2961 |
|
2962 |
|
2963 |
|
2964 |
|
2965 |
|
2966 |
|
2967 |
|
2968 |
|
2969 |
|
2970 |
|
2971 | const sumDurations = function ({
|
2972 | defaultDuration,
|
2973 | durationList,
|
2974 | startIndex,
|
2975 | endIndex
|
2976 | }) {
|
2977 | let durations = 0;
|
2978 |
|
2979 | if (startIndex > endIndex) {
|
2980 | [startIndex, endIndex] = [endIndex, startIndex];
|
2981 | }
|
2982 |
|
2983 | if (startIndex < 0) {
|
2984 | for (let i = startIndex; i < Math.min(0, endIndex); i++) {
|
2985 | durations += defaultDuration;
|
2986 | }
|
2987 |
|
2988 | startIndex = 0;
|
2989 | }
|
2990 |
|
2991 | for (let i = startIndex; i < endIndex; i++) {
|
2992 | durations += durationList[i].duration;
|
2993 | }
|
2994 |
|
2995 | return durations;
|
2996 | };
|
2997 | |
2998 |
|
2999 |
|
3000 |
|
3001 |
|
3002 |
|
3003 |
|
3004 |
|
3005 |
|
3006 |
|
3007 |
|
3008 |
|
3009 |
|
3010 |
|
3011 |
|
3012 |
|
3013 |
|
3014 |
|
3015 |
|
3016 |
|
3017 | const playlistEnd = function (playlist, expired, useSafeLiveEnd, liveEdgePadding) {
|
3018 | if (!playlist || !playlist.segments) {
|
3019 | return null;
|
3020 | }
|
3021 |
|
3022 | if (playlist.endList) {
|
3023 | return duration(playlist);
|
3024 | }
|
3025 |
|
3026 | if (expired === null) {
|
3027 | return null;
|
3028 | }
|
3029 |
|
3030 | expired = expired || 0;
|
3031 | let lastSegmentEndTime = intervalDuration(playlist, playlist.mediaSequence + playlist.segments.length, expired);
|
3032 |
|
3033 | if (useSafeLiveEnd) {
|
3034 | liveEdgePadding = typeof liveEdgePadding === 'number' ? liveEdgePadding : liveEdgeDelay(null, playlist);
|
3035 | lastSegmentEndTime -= liveEdgePadding;
|
3036 | }
|
3037 |
|
3038 |
|
3039 | return Math.max(0, lastSegmentEndTime);
|
3040 | };
|
3041 | |
3042 |
|
3043 |
|
3044 |
|
3045 |
|
3046 |
|
3047 |
|
3048 |
|
3049 |
|
3050 |
|
3051 |
|
3052 |
|
3053 |
|
3054 |
|
3055 |
|
3056 |
|
3057 |
|
3058 |
|
3059 | const seekable = function (playlist, expired, liveEdgePadding) {
|
3060 | const useSafeLiveEnd = true;
|
3061 | const seekableStart = expired || 0;
|
3062 | let seekableEnd = playlistEnd(playlist, expired, useSafeLiveEnd, liveEdgePadding);
|
3063 |
|
3064 | if (seekableEnd === null) {
|
3065 | return createTimeRanges();
|
3066 | }
|
3067 |
|
3068 |
|
3069 | if (seekableEnd < seekableStart) {
|
3070 | seekableEnd = seekableStart;
|
3071 | }
|
3072 |
|
3073 | return createTimeRanges(seekableStart, seekableEnd);
|
3074 | };
|
3075 | |
3076 |
|
3077 |
|
3078 |
|
3079 |
|
3080 |
|
3081 |
|
3082 |
|
3083 |
|
3084 |
|
3085 |
|
3086 |
|
3087 |
|
3088 |
|
3089 | const getMediaInfoForTime = function ({
|
3090 | playlist,
|
3091 | currentTime,
|
3092 | startingSegmentIndex,
|
3093 | startingPartIndex,
|
3094 | startTime,
|
3095 | exactManifestTimings
|
3096 | }) {
|
3097 | let time = currentTime - startTime;
|
3098 | const partsAndSegments = getPartsAndSegments(playlist);
|
3099 | let startIndex = 0;
|
3100 |
|
3101 | for (let i = 0; i < partsAndSegments.length; i++) {
|
3102 | const partAndSegment = partsAndSegments[i];
|
3103 |
|
3104 | if (startingSegmentIndex !== partAndSegment.segmentIndex) {
|
3105 | continue;
|
3106 | }
|
3107 |
|
3108 |
|
3109 | if (typeof startingPartIndex === 'number' && typeof partAndSegment.partIndex === 'number' && startingPartIndex !== partAndSegment.partIndex) {
|
3110 | continue;
|
3111 | }
|
3112 |
|
3113 | startIndex = i;
|
3114 | break;
|
3115 | }
|
3116 |
|
3117 | if (time < 0) {
|
3118 |
|
3119 |
|
3120 | if (startIndex > 0) {
|
3121 | for (let i = startIndex - 1; i >= 0; i--) {
|
3122 | const partAndSegment = partsAndSegments[i];
|
3123 | time += partAndSegment.duration;
|
3124 |
|
3125 | if (exactManifestTimings) {
|
3126 | if (time < 0) {
|
3127 | continue;
|
3128 | }
|
3129 | } else if (time + TIME_FUDGE_FACTOR <= 0) {
|
3130 | continue;
|
3131 | }
|
3132 |
|
3133 | return {
|
3134 | partIndex: partAndSegment.partIndex,
|
3135 | segmentIndex: partAndSegment.segmentIndex,
|
3136 | startTime: startTime - sumDurations({
|
3137 | defaultDuration: playlist.targetDuration,
|
3138 | durationList: partsAndSegments,
|
3139 | startIndex,
|
3140 | endIndex: i
|
3141 | })
|
3142 | };
|
3143 | }
|
3144 | }
|
3145 |
|
3146 |
|
3147 |
|
3148 | return {
|
3149 | partIndex: partsAndSegments[0] && partsAndSegments[0].partIndex || null,
|
3150 | segmentIndex: partsAndSegments[0] && partsAndSegments[0].segmentIndex || 0,
|
3151 | startTime: currentTime
|
3152 | };
|
3153 | }
|
3154 |
|
3155 |
|
3156 |
|
3157 |
|
3158 | if (startIndex < 0) {
|
3159 | for (let i = startIndex; i < 0; i++) {
|
3160 | time -= playlist.targetDuration;
|
3161 |
|
3162 | if (time < 0) {
|
3163 | return {
|
3164 | partIndex: partsAndSegments[0] && partsAndSegments[0].partIndex || null,
|
3165 | segmentIndex: partsAndSegments[0] && partsAndSegments[0].segmentIndex || 0,
|
3166 | startTime: currentTime
|
3167 | };
|
3168 | }
|
3169 | }
|
3170 |
|
3171 | startIndex = 0;
|
3172 | }
|
3173 |
|
3174 |
|
3175 |
|
3176 | for (let i = startIndex; i < partsAndSegments.length; i++) {
|
3177 | const partAndSegment = partsAndSegments[i];
|
3178 | time -= partAndSegment.duration;
|
3179 | const canUseFudgeFactor = partAndSegment.duration > TIME_FUDGE_FACTOR;
|
3180 | const isExactlyAtTheEnd = time === 0;
|
3181 | const isExtremelyCloseToTheEnd = canUseFudgeFactor && time + TIME_FUDGE_FACTOR >= 0;
|
3182 |
|
3183 | if (isExactlyAtTheEnd || isExtremelyCloseToTheEnd) {
|
3184 |
|
3185 |
|
3186 |
|
3187 |
|
3188 |
|
3189 |
|
3190 |
|
3191 |
|
3192 |
|
3193 |
|
3194 |
|
3195 |
|
3196 | if (i !== partsAndSegments.length - 1) {
|
3197 | continue;
|
3198 | }
|
3199 | }
|
3200 |
|
3201 | if (exactManifestTimings) {
|
3202 | if (time > 0) {
|
3203 | continue;
|
3204 | }
|
3205 | } else if (time - TIME_FUDGE_FACTOR >= 0) {
|
3206 | continue;
|
3207 | }
|
3208 |
|
3209 | return {
|
3210 | partIndex: partAndSegment.partIndex,
|
3211 | segmentIndex: partAndSegment.segmentIndex,
|
3212 | startTime: startTime + sumDurations({
|
3213 | defaultDuration: playlist.targetDuration,
|
3214 | durationList: partsAndSegments,
|
3215 | startIndex,
|
3216 | endIndex: i
|
3217 | })
|
3218 | };
|
3219 | }
|
3220 |
|
3221 |
|
3222 | return {
|
3223 | segmentIndex: partsAndSegments[partsAndSegments.length - 1].segmentIndex,
|
3224 | partIndex: partsAndSegments[partsAndSegments.length - 1].partIndex,
|
3225 | startTime: currentTime
|
3226 | };
|
3227 | };
|
3228 | |
3229 |
|
3230 |
|
3231 |
|
3232 |
|
3233 |
|
3234 |
|
3235 |
|
3236 | const isExcluded = function (playlist) {
|
3237 | return playlist.excludeUntil && playlist.excludeUntil > Date.now();
|
3238 | };
|
3239 | |
3240 |
|
3241 |
|
3242 |
|
3243 |
|
3244 |
|
3245 |
|
3246 |
|
3247 |
|
3248 | const isIncompatible = function (playlist) {
|
3249 | return playlist.excludeUntil && playlist.excludeUntil === Infinity;
|
3250 | };
|
3251 | |
3252 |
|
3253 |
|
3254 |
|
3255 |
|
3256 |
|
3257 |
|
3258 |
|
3259 | const isEnabled = function (playlist) {
|
3260 | const excluded = isExcluded(playlist);
|
3261 | return !playlist.disabled && !excluded;
|
3262 | };
|
3263 | |
3264 |
|
3265 |
|
3266 |
|
3267 |
|
3268 |
|
3269 |
|
3270 |
|
3271 | const isDisabled = function (playlist) {
|
3272 | return playlist.disabled;
|
3273 | };
|
3274 | |
3275 |
|
3276 |
|
3277 |
|
3278 |
|
3279 |
|
3280 | const isAes = function (media) {
|
3281 | for (let i = 0; i < media.segments.length; i++) {
|
3282 | if (media.segments[i].key) {
|
3283 | return true;
|
3284 | }
|
3285 | }
|
3286 |
|
3287 | return false;
|
3288 | };
|
3289 | |
3290 |
|
3291 |
|
3292 |
|
3293 |
|
3294 |
|
3295 |
|
3296 |
|
3297 |
|
3298 |
|
3299 |
|
3300 |
|
3301 | const hasAttribute = function (attr, playlist) {
|
3302 | return playlist.attributes && playlist.attributes[attr];
|
3303 | };
|
3304 | |
3305 |
|
3306 |
|
3307 |
|
3308 |
|
3309 |
|
3310 |
|
3311 |
|
3312 |
|
3313 |
|
3314 |
|
3315 |
|
3316 |
|
3317 |
|
3318 |
|
3319 |
|
3320 |
|
3321 | const estimateSegmentRequestTime = function (segmentDuration, bandwidth, playlist, bytesReceived = 0) {
|
3322 | if (!hasAttribute('BANDWIDTH', playlist)) {
|
3323 | return NaN;
|
3324 | }
|
3325 |
|
3326 | const size = segmentDuration * playlist.attributes.BANDWIDTH;
|
3327 | return (size - bytesReceived * 8) / bandwidth;
|
3328 | };
|
3329 | |
3330 |
|
3331 |
|
3332 |
|
3333 |
|
3334 |
|
3335 | const isLowestEnabledRendition = (main, media) => {
|
3336 | if (main.playlists.length === 1) {
|
3337 | return true;
|
3338 | }
|
3339 |
|
3340 | const currentBandwidth = media.attributes.BANDWIDTH || Number.MAX_VALUE;
|
3341 | return main.playlists.filter(playlist => {
|
3342 | if (!isEnabled(playlist)) {
|
3343 | return false;
|
3344 | }
|
3345 |
|
3346 | return (playlist.attributes.BANDWIDTH || 0) < currentBandwidth;
|
3347 | }).length === 0;
|
3348 | };
|
3349 | const playlistMatch = (a, b) => {
|
3350 |
|
3351 |
|
3352 |
|
3353 | if (!a && !b || !a && b || a && !b) {
|
3354 | return false;
|
3355 | }
|
3356 |
|
3357 |
|
3358 | if (a === b) {
|
3359 | return true;
|
3360 | }
|
3361 |
|
3362 |
|
3363 |
|
3364 | if (a.id && b.id && a.id === b.id) {
|
3365 | return true;
|
3366 | }
|
3367 |
|
3368 |
|
3369 |
|
3370 | if (a.resolvedUri && b.resolvedUri && a.resolvedUri === b.resolvedUri) {
|
3371 | return true;
|
3372 | }
|
3373 |
|
3374 |
|
3375 |
|
3376 | if (a.uri && b.uri && a.uri === b.uri) {
|
3377 | return true;
|
3378 | }
|
3379 |
|
3380 | return false;
|
3381 | };
|
3382 |
|
3383 | const someAudioVariant = function (main, callback) {
|
3384 | const AUDIO = main && main.mediaGroups && main.mediaGroups.AUDIO || {};
|
3385 | let found = false;
|
3386 |
|
3387 | for (const groupName in AUDIO) {
|
3388 | for (const label in AUDIO[groupName]) {
|
3389 | found = callback(AUDIO[groupName][label]);
|
3390 |
|
3391 | if (found) {
|
3392 | break;
|
3393 | }
|
3394 | }
|
3395 |
|
3396 | if (found) {
|
3397 | break;
|
3398 | }
|
3399 | }
|
3400 |
|
3401 | return !!found;
|
3402 | };
|
3403 |
|
3404 | const isAudioOnly = main => {
|
3405 |
|
3406 |
|
3407 | if (!main || !main.playlists || !main.playlists.length) {
|
3408 |
|
3409 |
|
3410 | const found = someAudioVariant(main, variant => variant.playlists && variant.playlists.length || variant.uri);
|
3411 | return found;
|
3412 | }
|
3413 |
|
3414 |
|
3415 | for (let i = 0; i < main.playlists.length; i++) {
|
3416 | const playlist = main.playlists[i];
|
3417 | const CODECS = playlist.attributes && playlist.attributes.CODECS;
|
3418 |
|
3419 | if (CODECS && CODECS.split(',').every(c => isAudioCodec(c))) {
|
3420 | continue;
|
3421 | }
|
3422 |
|
3423 |
|
3424 | const found = someAudioVariant(main, variant => playlistMatch(playlist, variant));
|
3425 |
|
3426 | if (found) {
|
3427 | continue;
|
3428 | }
|
3429 |
|
3430 |
|
3431 |
|
3432 | return false;
|
3433 | }
|
3434 |
|
3435 |
|
3436 |
|
3437 | return true;
|
3438 | };
|
3439 |
|
3440 | var Playlist = {
|
3441 | liveEdgeDelay,
|
3442 | duration,
|
3443 | seekable,
|
3444 | getMediaInfoForTime,
|
3445 | isEnabled,
|
3446 | isDisabled,
|
3447 | isExcluded,
|
3448 | isIncompatible,
|
3449 | playlistEnd,
|
3450 | isAes,
|
3451 | hasAttribute,
|
3452 | estimateSegmentRequestTime,
|
3453 | isLowestEnabledRendition,
|
3454 | isAudioOnly,
|
3455 | playlistMatch,
|
3456 | segmentDurationWithParts
|
3457 | };
|
3458 |
|
3459 | const {
|
3460 | log
|
3461 | } = videojs__default["default"];
|
3462 | const createPlaylistID = (index, uri) => {
|
3463 | return `${index}-${uri}`;
|
3464 | };
|
3465 |
|
3466 | const groupID = (type, group, label) => {
|
3467 | return `placeholder-uri-${type}-${group}-${label}`;
|
3468 | };
|
3469 | |
3470 |
|
3471 |
|
3472 |
|
3473 |
|
3474 |
|
3475 |
|
3476 |
|
3477 |
|
3478 |
|
3479 |
|
3480 |
|
3481 |
|
3482 |
|
3483 |
|
3484 |
|
3485 |
|
3486 |
|
3487 |
|
3488 | const parseManifest = ({
|
3489 | onwarn,
|
3490 | oninfo,
|
3491 | manifestString,
|
3492 | customTagParsers = [],
|
3493 | customTagMappers = [],
|
3494 | llhls
|
3495 | }) => {
|
3496 | const parser = new Parser();
|
3497 |
|
3498 | if (onwarn) {
|
3499 | parser.on('warn', onwarn);
|
3500 | }
|
3501 |
|
3502 | if (oninfo) {
|
3503 | parser.on('info', oninfo);
|
3504 | }
|
3505 |
|
3506 | customTagParsers.forEach(customParser => parser.addParser(customParser));
|
3507 | customTagMappers.forEach(mapper => parser.addTagMapper(mapper));
|
3508 | parser.push(manifestString);
|
3509 | parser.end();
|
3510 | const manifest = parser.manifest;
|
3511 |
|
3512 |
|
3513 | if (!llhls) {
|
3514 | ['preloadSegment', 'skip', 'serverControl', 'renditionReports', 'partInf', 'partTargetDuration'].forEach(function (k) {
|
3515 | if (manifest.hasOwnProperty(k)) {
|
3516 | delete manifest[k];
|
3517 | }
|
3518 | });
|
3519 |
|
3520 | if (manifest.segments) {
|
3521 | manifest.segments.forEach(function (segment) {
|
3522 | ['parts', 'preloadHints'].forEach(function (k) {
|
3523 | if (segment.hasOwnProperty(k)) {
|
3524 | delete segment[k];
|
3525 | }
|
3526 | });
|
3527 | });
|
3528 | }
|
3529 | }
|
3530 |
|
3531 | if (!manifest.targetDuration) {
|
3532 | let targetDuration = 10;
|
3533 |
|
3534 | if (manifest.segments && manifest.segments.length) {
|
3535 | targetDuration = manifest.segments.reduce((acc, s) => Math.max(acc, s.duration), 0);
|
3536 | }
|
3537 |
|
3538 | if (onwarn) {
|
3539 | onwarn({
|
3540 | message: `manifest has no targetDuration defaulting to ${targetDuration}`
|
3541 | });
|
3542 | }
|
3543 |
|
3544 | manifest.targetDuration = targetDuration;
|
3545 | }
|
3546 |
|
3547 | const parts = getLastParts(manifest);
|
3548 |
|
3549 | if (parts.length && !manifest.partTargetDuration) {
|
3550 | const partTargetDuration = parts.reduce((acc, p) => Math.max(acc, p.duration), 0);
|
3551 |
|
3552 | if (onwarn) {
|
3553 | onwarn({
|
3554 | message: `manifest has no partTargetDuration defaulting to ${partTargetDuration}`
|
3555 | });
|
3556 | 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.');
|
3557 | }
|
3558 |
|
3559 | manifest.partTargetDuration = partTargetDuration;
|
3560 | }
|
3561 |
|
3562 | return manifest;
|
3563 | };
|
3564 | |
3565 |
|
3566 |
|
3567 |
|
3568 |
|
3569 |
|
3570 |
|
3571 |
|
3572 |
|
3573 |
|
3574 | const forEachMediaGroup$1 = (main, callback) => {
|
3575 | if (!main.mediaGroups) {
|
3576 | return;
|
3577 | }
|
3578 |
|
3579 | ['AUDIO', 'SUBTITLES'].forEach(mediaType => {
|
3580 | if (!main.mediaGroups[mediaType]) {
|
3581 | return;
|
3582 | }
|
3583 |
|
3584 | for (const groupKey in main.mediaGroups[mediaType]) {
|
3585 | for (const labelKey in main.mediaGroups[mediaType][groupKey]) {
|
3586 | const mediaProperties = main.mediaGroups[mediaType][groupKey][labelKey];
|
3587 | callback(mediaProperties, mediaType, groupKey, labelKey);
|
3588 | }
|
3589 | }
|
3590 | });
|
3591 | };
|
3592 | |
3593 |
|
3594 |
|
3595 |
|
3596 |
|
3597 |
|
3598 |
|
3599 |
|
3600 |
|
3601 |
|
3602 |
|
3603 |
|
3604 |
|
3605 |
|
3606 |
|
3607 | const setupMediaPlaylist = ({
|
3608 | playlist,
|
3609 | uri,
|
3610 | id
|
3611 | }) => {
|
3612 | playlist.id = id;
|
3613 | playlist.playlistErrors_ = 0;
|
3614 |
|
3615 | if (uri) {
|
3616 |
|
3617 |
|
3618 |
|
3619 | playlist.uri = uri;
|
3620 | }
|
3621 |
|
3622 |
|
3623 |
|
3624 |
|
3625 |
|
3626 |
|
3627 |
|
3628 |
|
3629 | playlist.attributes = playlist.attributes || {};
|
3630 | };
|
3631 | |
3632 |
|
3633 |
|
3634 |
|
3635 |
|
3636 |
|
3637 |
|
3638 |
|
3639 |
|
3640 | const setupMediaPlaylists = main => {
|
3641 | let i = main.playlists.length;
|
3642 |
|
3643 | while (i--) {
|
3644 | const playlist = main.playlists[i];
|
3645 | setupMediaPlaylist({
|
3646 | playlist,
|
3647 | id: createPlaylistID(i, playlist.uri)
|
3648 | });
|
3649 | playlist.resolvedUri = resolveUrl(main.uri, playlist.uri);
|
3650 | main.playlists[playlist.id] = playlist;
|
3651 |
|
3652 | main.playlists[playlist.uri] = playlist;
|
3653 |
|
3654 |
|
3655 |
|
3656 |
|
3657 | if (!playlist.attributes.BANDWIDTH) {
|
3658 | log.warn('Invalid playlist STREAM-INF detected. Missing BANDWIDTH attribute.');
|
3659 | }
|
3660 | }
|
3661 | };
|
3662 | |
3663 |
|
3664 |
|
3665 |
|
3666 |
|
3667 |
|
3668 |
|
3669 | const resolveMediaGroupUris = main => {
|
3670 | forEachMediaGroup$1(main, properties => {
|
3671 | if (properties.uri) {
|
3672 | properties.resolvedUri = resolveUrl(main.uri, properties.uri);
|
3673 | }
|
3674 | });
|
3675 | };
|
3676 | |
3677 |
|
3678 |
|
3679 |
|
3680 |
|
3681 |
|
3682 |
|
3683 |
|
3684 |
|
3685 |
|
3686 |
|
3687 |
|
3688 | const mainForMedia = (media, uri) => {
|
3689 | const id = createPlaylistID(0, uri);
|
3690 | const main = {
|
3691 | mediaGroups: {
|
3692 | 'AUDIO': {},
|
3693 | 'VIDEO': {},
|
3694 | 'CLOSED-CAPTIONS': {},
|
3695 | 'SUBTITLES': {}
|
3696 | },
|
3697 | uri: window.location.href,
|
3698 | resolvedUri: window.location.href,
|
3699 | playlists: [{
|
3700 | uri,
|
3701 | id,
|
3702 | resolvedUri: uri,
|
3703 |
|
3704 |
|
3705 | attributes: {}
|
3706 | }]
|
3707 | };
|
3708 |
|
3709 | main.playlists[id] = main.playlists[0];
|
3710 |
|
3711 | main.playlists[uri] = main.playlists[0];
|
3712 | return main;
|
3713 | };
|
3714 | |
3715 |
|
3716 |
|
3717 |
|
3718 |
|
3719 |
|
3720 |
|
3721 |
|
3722 |
|
3723 |
|
3724 |
|
3725 |
|
3726 | const addPropertiesToMain = (main, uri, createGroupID = groupID) => {
|
3727 | main.uri = uri;
|
3728 |
|
3729 | for (let i = 0; i < main.playlists.length; i++) {
|
3730 | if (!main.playlists[i].uri) {
|
3731 |
|
3732 |
|
3733 |
|
3734 | const phonyUri = `placeholder-uri-${i}`;
|
3735 | main.playlists[i].uri = phonyUri;
|
3736 | }
|
3737 | }
|
3738 |
|
3739 | const audioOnlyMain = isAudioOnly(main);
|
3740 | forEachMediaGroup$1(main, (properties, mediaType, groupKey, labelKey) => {
|
3741 |
|
3742 | if (!properties.playlists || !properties.playlists.length) {
|
3743 |
|
3744 |
|
3745 |
|
3746 | if (audioOnlyMain && mediaType === 'AUDIO' && !properties.uri) {
|
3747 | for (let i = 0; i < main.playlists.length; i++) {
|
3748 | const p = main.playlists[i];
|
3749 |
|
3750 | if (p.attributes && p.attributes.AUDIO && p.attributes.AUDIO === groupKey) {
|
3751 | return;
|
3752 | }
|
3753 | }
|
3754 | }
|
3755 |
|
3756 | properties.playlists = [_extends({}, properties)];
|
3757 | }
|
3758 |
|
3759 | properties.playlists.forEach(function (p, i) {
|
3760 | const groupId = createGroupID(mediaType, groupKey, labelKey, p);
|
3761 | const id = createPlaylistID(i, groupId);
|
3762 |
|
3763 | if (p.uri) {
|
3764 | p.resolvedUri = p.resolvedUri || resolveUrl(main.uri, p.uri);
|
3765 | } else {
|
3766 |
|
3767 |
|
3768 |
|
3769 |
|
3770 | p.uri = i === 0 ? groupId : id;
|
3771 |
|
3772 |
|
3773 | p.resolvedUri = p.uri;
|
3774 | }
|
3775 |
|
3776 | p.id = p.id || id;
|
3777 |
|
3778 |
|
3779 | p.attributes = p.attributes || {};
|
3780 |
|
3781 | main.playlists[p.id] = p;
|
3782 | main.playlists[p.uri] = p;
|
3783 | });
|
3784 | });
|
3785 | setupMediaPlaylists(main);
|
3786 | resolveMediaGroupUris(main);
|
3787 | };
|
3788 |
|
3789 | class DateRangesStorage {
|
3790 | constructor() {
|
3791 | this.offset_ = null;
|
3792 | this.pendingDateRanges_ = new Map();
|
3793 | this.processedDateRanges_ = new Map();
|
3794 | }
|
3795 |
|
3796 | setOffset(segments = []) {
|
3797 |
|
3798 | if (this.offset_ !== null) {
|
3799 | return;
|
3800 | }
|
3801 |
|
3802 |
|
3803 | if (!segments.length) {
|
3804 | return;
|
3805 | }
|
3806 |
|
3807 | const [firstSegment] = segments;
|
3808 |
|
3809 | if (firstSegment.programDateTime === undefined) {
|
3810 | return;
|
3811 | }
|
3812 |
|
3813 |
|
3814 | this.offset_ = firstSegment.programDateTime / 1000;
|
3815 | }
|
3816 |
|
3817 | setPendingDateRanges(dateRanges = []) {
|
3818 | if (!dateRanges.length) {
|
3819 | return;
|
3820 | }
|
3821 |
|
3822 | const [dateRange] = dateRanges;
|
3823 | const startTime = dateRange.startDate.getTime();
|
3824 | this.trimProcessedDateRanges_(startTime);
|
3825 | this.pendingDateRanges_ = dateRanges.reduce((map, pendingDateRange) => {
|
3826 | map.set(pendingDateRange.id, pendingDateRange);
|
3827 | return map;
|
3828 | }, new Map());
|
3829 | }
|
3830 |
|
3831 | processDateRange(dateRange) {
|
3832 | this.pendingDateRanges_.delete(dateRange.id);
|
3833 | this.processedDateRanges_.set(dateRange.id, dateRange);
|
3834 | }
|
3835 |
|
3836 | getDateRangesToProcess() {
|
3837 | if (this.offset_ === null) {
|
3838 | return [];
|
3839 | }
|
3840 |
|
3841 | const dateRangeClasses = {};
|
3842 | const dateRangesToProcess = [];
|
3843 | this.pendingDateRanges_.forEach((dateRange, id) => {
|
3844 | if (this.processedDateRanges_.has(id)) {
|
3845 | return;
|
3846 | }
|
3847 |
|
3848 | dateRange.startTime = dateRange.startDate.getTime() / 1000 - this.offset_;
|
3849 |
|
3850 | dateRange.processDateRange = () => this.processDateRange(dateRange);
|
3851 |
|
3852 | dateRangesToProcess.push(dateRange);
|
3853 |
|
3854 | if (!dateRange.class) {
|
3855 | return;
|
3856 | }
|
3857 |
|
3858 | if (dateRangeClasses[dateRange.class]) {
|
3859 | const length = dateRangeClasses[dateRange.class].push(dateRange);
|
3860 | dateRange.classListIndex = length - 1;
|
3861 | } else {
|
3862 | dateRangeClasses[dateRange.class] = [dateRange];
|
3863 | dateRange.classListIndex = 0;
|
3864 | }
|
3865 | });
|
3866 |
|
3867 | for (const dateRange of dateRangesToProcess) {
|
3868 | const classList = dateRangeClasses[dateRange.class] || [];
|
3869 |
|
3870 | if (dateRange.endDate) {
|
3871 | dateRange.endTime = dateRange.endDate.getTime() / 1000 - this.offset_;
|
3872 | } else if (dateRange.endOnNext && classList[dateRange.classListIndex + 1]) {
|
3873 | dateRange.endTime = classList[dateRange.classListIndex + 1].startTime;
|
3874 | } else if (dateRange.duration) {
|
3875 | dateRange.endTime = dateRange.startTime + dateRange.duration;
|
3876 | } else if (dateRange.plannedDuration) {
|
3877 | dateRange.endTime = dateRange.startTime + dateRange.plannedDuration;
|
3878 | } else {
|
3879 | dateRange.endTime = dateRange.startTime;
|
3880 | }
|
3881 | }
|
3882 |
|
3883 | return dateRangesToProcess;
|
3884 | }
|
3885 |
|
3886 | trimProcessedDateRanges_(startTime) {
|
3887 | const copy = new Map(this.processedDateRanges_);
|
3888 | copy.forEach((dateRange, id) => {
|
3889 | if (dateRange.startDate.getTime() < startTime) {
|
3890 | this.processedDateRanges_.delete(id);
|
3891 | }
|
3892 | });
|
3893 | }
|
3894 |
|
3895 | }
|
3896 |
|
3897 | const {
|
3898 | EventTarget: EventTarget$1
|
3899 | } = videojs__default["default"];
|
3900 |
|
3901 | const addLLHLSQueryDirectives = (uri, media) => {
|
3902 | if (media.endList || !media.serverControl) {
|
3903 | return uri;
|
3904 | }
|
3905 |
|
3906 | const parameters = {};
|
3907 |
|
3908 | if (media.serverControl.canBlockReload) {
|
3909 | const {
|
3910 | preloadSegment
|
3911 | } = media;
|
3912 |
|
3913 | let nextMSN = media.mediaSequence + media.segments.length;
|
3914 |
|
3915 |
|
3916 |
|
3917 | if (preloadSegment) {
|
3918 | const parts = preloadSegment.parts || [];
|
3919 |
|
3920 | const nextPart = getKnownPartCount(media) - 1;
|
3921 |
|
3922 |
|
3923 |
|
3924 | if (nextPart > -1 && nextPart !== parts.length - 1) {
|
3925 |
|
3926 |
|
3927 | parameters._HLS_part = nextPart;
|
3928 | }
|
3929 |
|
3930 |
|
3931 |
|
3932 |
|
3933 |
|
3934 |
|
3935 |
|
3936 |
|
3937 |
|
3938 |
|
3939 | if (nextPart > -1 || parts.length) {
|
3940 | nextMSN--;
|
3941 | }
|
3942 | }
|
3943 |
|
3944 |
|
3945 |
|
3946 | parameters._HLS_msn = nextMSN;
|
3947 | }
|
3948 |
|
3949 | if (media.serverControl && media.serverControl.canSkipUntil) {
|
3950 |
|
3951 |
|
3952 | parameters._HLS_skip = media.serverControl.canSkipDateranges ? 'v2' : 'YES';
|
3953 | }
|
3954 |
|
3955 | if (Object.keys(parameters).length) {
|
3956 | const parsedUri = new window.URL(uri);
|
3957 | ['_HLS_skip', '_HLS_msn', '_HLS_part'].forEach(function (name) {
|
3958 | if (!parameters.hasOwnProperty(name)) {
|
3959 | return;
|
3960 | }
|
3961 |
|
3962 | parsedUri.searchParams.set(name, parameters[name]);
|
3963 | });
|
3964 | uri = parsedUri.toString();
|
3965 | }
|
3966 |
|
3967 | return uri;
|
3968 | };
|
3969 | |
3970 |
|
3971 |
|
3972 |
|
3973 |
|
3974 |
|
3975 |
|
3976 |
|
3977 |
|
3978 |
|
3979 |
|
3980 | const updateSegment = (a, b) => {
|
3981 | if (!a) {
|
3982 | return b;
|
3983 | }
|
3984 |
|
3985 | const result = merge$1(a, b);
|
3986 |
|
3987 |
|
3988 | if (a.preloadHints && !b.preloadHints) {
|
3989 | delete result.preloadHints;
|
3990 | }
|
3991 |
|
3992 |
|
3993 |
|
3994 | if (a.parts && !b.parts) {
|
3995 | delete result.parts;
|
3996 |
|
3997 |
|
3998 | } else if (a.parts && b.parts) {
|
3999 | for (let i = 0; i < b.parts.length; i++) {
|
4000 | if (a.parts && a.parts[i]) {
|
4001 | result.parts[i] = merge$1(a.parts[i], b.parts[i]);
|
4002 | }
|
4003 | }
|
4004 | }
|
4005 |
|
4006 |
|
4007 |
|
4008 | if (!a.skipped && b.skipped) {
|
4009 | result.skipped = false;
|
4010 | }
|
4011 |
|
4012 |
|
4013 |
|
4014 | if (a.preload && !b.preload) {
|
4015 | result.preload = false;
|
4016 | }
|
4017 |
|
4018 | return result;
|
4019 | };
|
4020 | |
4021 |
|
4022 |
|
4023 |
|
4024 |
|
4025 |
|
4026 |
|
4027 |
|
4028 |
|
4029 |
|
4030 |
|
4031 |
|
4032 |
|
4033 |
|
4034 |
|
4035 |
|
4036 | const updateSegments = (original, update, offset) => {
|
4037 | const oldSegments = original.slice();
|
4038 | const newSegments = update.slice();
|
4039 | offset = offset || 0;
|
4040 | const result = [];
|
4041 | let currentMap;
|
4042 |
|
4043 | for (let newIndex = 0; newIndex < newSegments.length; newIndex++) {
|
4044 | const oldSegment = oldSegments[newIndex + offset];
|
4045 | const newSegment = newSegments[newIndex];
|
4046 |
|
4047 | if (oldSegment) {
|
4048 | currentMap = oldSegment.map || currentMap;
|
4049 | result.push(updateSegment(oldSegment, newSegment));
|
4050 | } else {
|
4051 |
|
4052 | if (currentMap && !newSegment.map) {
|
4053 | newSegment.map = currentMap;
|
4054 | }
|
4055 |
|
4056 | result.push(newSegment);
|
4057 | }
|
4058 | }
|
4059 |
|
4060 | return result;
|
4061 | };
|
4062 | const resolveSegmentUris = (segment, baseUri) => {
|
4063 |
|
4064 |
|
4065 | if (!segment.resolvedUri && segment.uri) {
|
4066 | segment.resolvedUri = resolveUrl(baseUri, segment.uri);
|
4067 | }
|
4068 |
|
4069 | if (segment.key && !segment.key.resolvedUri) {
|
4070 | segment.key.resolvedUri = resolveUrl(baseUri, segment.key.uri);
|
4071 | }
|
4072 |
|
4073 | if (segment.map && !segment.map.resolvedUri) {
|
4074 | segment.map.resolvedUri = resolveUrl(baseUri, segment.map.uri);
|
4075 | }
|
4076 |
|
4077 | if (segment.map && segment.map.key && !segment.map.key.resolvedUri) {
|
4078 | segment.map.key.resolvedUri = resolveUrl(baseUri, segment.map.key.uri);
|
4079 | }
|
4080 |
|
4081 | if (segment.parts && segment.parts.length) {
|
4082 | segment.parts.forEach(p => {
|
4083 | if (p.resolvedUri) {
|
4084 | return;
|
4085 | }
|
4086 |
|
4087 | p.resolvedUri = resolveUrl(baseUri, p.uri);
|
4088 | });
|
4089 | }
|
4090 |
|
4091 | if (segment.preloadHints && segment.preloadHints.length) {
|
4092 | segment.preloadHints.forEach(p => {
|
4093 | if (p.resolvedUri) {
|
4094 | return;
|
4095 | }
|
4096 |
|
4097 | p.resolvedUri = resolveUrl(baseUri, p.uri);
|
4098 | });
|
4099 | }
|
4100 | };
|
4101 |
|
4102 | const getAllSegments = function (media) {
|
4103 | const segments = media.segments || [];
|
4104 | const preloadSegment = media.preloadSegment;
|
4105 |
|
4106 |
|
4107 |
|
4108 | if (preloadSegment && preloadSegment.parts && preloadSegment.parts.length) {
|
4109 |
|
4110 |
|
4111 |
|
4112 | if (preloadSegment.preloadHints) {
|
4113 | for (let i = 0; i < preloadSegment.preloadHints.length; i++) {
|
4114 | if (preloadSegment.preloadHints[i].type === 'MAP') {
|
4115 | return segments;
|
4116 | }
|
4117 | }
|
4118 | }
|
4119 |
|
4120 |
|
4121 | preloadSegment.duration = media.targetDuration;
|
4122 | preloadSegment.preload = true;
|
4123 | segments.push(preloadSegment);
|
4124 | }
|
4125 |
|
4126 | return segments;
|
4127 | };
|
4128 |
|
4129 |
|
4130 |
|
4131 |
|
4132 | const isPlaylistUnchanged = (a, b) => a === b || a.segments && b.segments && a.segments.length === b.segments.length && a.endList === b.endList && a.mediaSequence === b.mediaSequence && a.preloadSegment === b.preloadSegment;
|
4133 | |
4134 |
|
4135 |
|
4136 |
|
4137 |
|
4138 |
|
4139 |
|
4140 |
|
4141 |
|
4142 |
|
4143 |
|
4144 |
|
4145 |
|
4146 | const updateMain$1 = (main, newMedia, unchangedCheck = isPlaylistUnchanged) => {
|
4147 | const result = merge$1(main, {});
|
4148 | const oldMedia = result.playlists[newMedia.id];
|
4149 |
|
4150 | if (!oldMedia) {
|
4151 | return null;
|
4152 | }
|
4153 |
|
4154 | if (unchangedCheck(oldMedia, newMedia)) {
|
4155 | return null;
|
4156 | }
|
4157 |
|
4158 | newMedia.segments = getAllSegments(newMedia);
|
4159 | const mergedPlaylist = merge$1(oldMedia, newMedia);
|
4160 |
|
4161 | if (mergedPlaylist.preloadSegment && !newMedia.preloadSegment) {
|
4162 | delete mergedPlaylist.preloadSegment;
|
4163 | }
|
4164 |
|
4165 |
|
4166 | if (oldMedia.segments) {
|
4167 | if (newMedia.skip) {
|
4168 | newMedia.segments = newMedia.segments || [];
|
4169 |
|
4170 |
|
4171 | for (let i = 0; i < newMedia.skip.skippedSegments; i++) {
|
4172 | newMedia.segments.unshift({
|
4173 | skipped: true
|
4174 | });
|
4175 | }
|
4176 | }
|
4177 |
|
4178 | mergedPlaylist.segments = updateSegments(oldMedia.segments, newMedia.segments, newMedia.mediaSequence - oldMedia.mediaSequence);
|
4179 | }
|
4180 |
|
4181 |
|
4182 | mergedPlaylist.segments.forEach(segment => {
|
4183 | resolveSegmentUris(segment, mergedPlaylist.resolvedUri);
|
4184 | });
|
4185 |
|
4186 |
|
4187 |
|
4188 | for (let i = 0; i < result.playlists.length; i++) {
|
4189 | if (result.playlists[i].id === newMedia.id) {
|
4190 | result.playlists[i] = mergedPlaylist;
|
4191 | }
|
4192 | }
|
4193 |
|
4194 | result.playlists[newMedia.id] = mergedPlaylist;
|
4195 |
|
4196 | result.playlists[newMedia.uri] = mergedPlaylist;
|
4197 |
|
4198 | forEachMediaGroup$1(main, (properties, mediaType, groupKey, labelKey) => {
|
4199 | if (!properties.playlists) {
|
4200 | return;
|
4201 | }
|
4202 |
|
4203 | for (let i = 0; i < properties.playlists.length; i++) {
|
4204 | if (newMedia.id === properties.playlists[i].id) {
|
4205 | properties.playlists[i] = mergedPlaylist;
|
4206 | }
|
4207 | }
|
4208 | });
|
4209 | return result;
|
4210 | };
|
4211 | |
4212 |
|
4213 |
|
4214 |
|
4215 |
|
4216 |
|
4217 |
|
4218 |
|
4219 |
|
4220 |
|
4221 |
|
4222 | const refreshDelay = (media, update) => {
|
4223 | const segments = media.segments || [];
|
4224 | const lastSegment = segments[segments.length - 1];
|
4225 | const lastPart = lastSegment && lastSegment.parts && lastSegment.parts[lastSegment.parts.length - 1];
|
4226 | const lastDuration = lastPart && lastPart.duration || lastSegment && lastSegment.duration;
|
4227 |
|
4228 | if (update && lastDuration) {
|
4229 | return lastDuration * 1000;
|
4230 | }
|
4231 |
|
4232 |
|
4233 |
|
4234 | return (media.partTargetDuration || media.targetDuration || 10) * 500;
|
4235 | };
|
4236 | |
4237 |
|
4238 |
|
4239 |
|
4240 |
|
4241 |
|
4242 |
|
4243 |
|
4244 |
|
4245 |
|
4246 | class PlaylistLoader extends EventTarget$1 {
|
4247 | constructor(src, vhs, options = {}) {
|
4248 | super();
|
4249 |
|
4250 | if (!src) {
|
4251 | throw new Error('A non-empty playlist URL or object is required');
|
4252 | }
|
4253 |
|
4254 | this.logger_ = logger('PlaylistLoader');
|
4255 | const {
|
4256 | withCredentials = false
|
4257 | } = options;
|
4258 | this.src = src;
|
4259 | this.vhs_ = vhs;
|
4260 | this.withCredentials = withCredentials;
|
4261 | this.addDateRangesToTextTrack_ = options.addDateRangesToTextTrack;
|
4262 | const vhsOptions = vhs.options_;
|
4263 | this.customTagParsers = vhsOptions && vhsOptions.customTagParsers || [];
|
4264 | this.customTagMappers = vhsOptions && vhsOptions.customTagMappers || [];
|
4265 | this.llhls = vhsOptions && vhsOptions.llhls;
|
4266 | this.dateRangesStorage_ = new DateRangesStorage();
|
4267 |
|
4268 | this.state = 'HAVE_NOTHING';
|
4269 |
|
4270 | this.handleMediaupdatetimeout_ = this.handleMediaupdatetimeout_.bind(this);
|
4271 | this.on('mediaupdatetimeout', this.handleMediaupdatetimeout_);
|
4272 | this.on('loadedplaylist', this.handleLoadedPlaylist_.bind(this));
|
4273 | }
|
4274 |
|
4275 | handleLoadedPlaylist_() {
|
4276 | const mediaPlaylist = this.media();
|
4277 |
|
4278 | if (!mediaPlaylist) {
|
4279 | return;
|
4280 | }
|
4281 |
|
4282 | this.dateRangesStorage_.setOffset(mediaPlaylist.segments);
|
4283 | this.dateRangesStorage_.setPendingDateRanges(mediaPlaylist.dateRanges);
|
4284 | const availableDateRanges = this.dateRangesStorage_.getDateRangesToProcess();
|
4285 |
|
4286 | if (!availableDateRanges.length || !this.addDateRangesToTextTrack_) {
|
4287 | return;
|
4288 | }
|
4289 |
|
4290 | this.addDateRangesToTextTrack_(availableDateRanges);
|
4291 | }
|
4292 |
|
4293 | handleMediaupdatetimeout_() {
|
4294 | if (this.state !== 'HAVE_METADATA') {
|
4295 |
|
4296 | return;
|
4297 | }
|
4298 |
|
4299 | const media = this.media();
|
4300 | let uri = resolveUrl(this.main.uri, media.uri);
|
4301 |
|
4302 | if (this.llhls) {
|
4303 | uri = addLLHLSQueryDirectives(uri, media);
|
4304 | }
|
4305 |
|
4306 | this.state = 'HAVE_CURRENT_METADATA';
|
4307 | this.request = this.vhs_.xhr({
|
4308 | uri,
|
4309 | withCredentials: this.withCredentials
|
4310 | }, (error, req) => {
|
4311 |
|
4312 | if (!this.request) {
|
4313 | return;
|
4314 | }
|
4315 |
|
4316 | if (error) {
|
4317 | return this.playlistRequestError(this.request, this.media(), 'HAVE_METADATA');
|
4318 | }
|
4319 |
|
4320 | this.haveMetadata({
|
4321 | playlistString: this.request.responseText,
|
4322 | url: this.media().uri,
|
4323 | id: this.media().id
|
4324 | });
|
4325 | });
|
4326 | }
|
4327 |
|
4328 | playlistRequestError(xhr, playlist, startingState) {
|
4329 | const {
|
4330 | uri,
|
4331 | id
|
4332 | } = playlist;
|
4333 |
|
4334 | this.request = null;
|
4335 |
|
4336 | if (startingState) {
|
4337 | this.state = startingState;
|
4338 | }
|
4339 |
|
4340 | this.error = {
|
4341 | playlist: this.main.playlists[id],
|
4342 | status: xhr.status,
|
4343 | message: `HLS playlist request error at URL: ${uri}.`,
|
4344 | responseText: xhr.responseText,
|
4345 | code: xhr.status >= 500 ? 4 : 2
|
4346 | };
|
4347 | this.trigger('error');
|
4348 | }
|
4349 |
|
4350 | parseManifest_({
|
4351 | url,
|
4352 | manifestString
|
4353 | }) {
|
4354 | return parseManifest({
|
4355 | onwarn: ({
|
4356 | message
|
4357 | }) => this.logger_(`m3u8-parser warn for ${url}: ${message}`),
|
4358 | oninfo: ({
|
4359 | message
|
4360 | }) => this.logger_(`m3u8-parser info for ${url}: ${message}`),
|
4361 | manifestString,
|
4362 | customTagParsers: this.customTagParsers,
|
4363 | customTagMappers: this.customTagMappers,
|
4364 | llhls: this.llhls
|
4365 | });
|
4366 | }
|
4367 | |
4368 |
|
4369 |
|
4370 |
|
4371 |
|
4372 |
|
4373 |
|
4374 |
|
4375 |
|
4376 |
|
4377 |
|
4378 |
|
4379 |
|
4380 |
|
4381 | haveMetadata({
|
4382 | playlistString,
|
4383 | playlistObject,
|
4384 | url,
|
4385 | id
|
4386 | }) {
|
4387 |
|
4388 | this.request = null;
|
4389 | this.state = 'HAVE_METADATA';
|
4390 | const playlist = playlistObject || this.parseManifest_({
|
4391 | url,
|
4392 | manifestString: playlistString
|
4393 | });
|
4394 | playlist.lastRequest = Date.now();
|
4395 | setupMediaPlaylist({
|
4396 | playlist,
|
4397 | uri: url,
|
4398 | id
|
4399 | });
|
4400 |
|
4401 | const update = updateMain$1(this.main, playlist);
|
4402 | this.targetDuration = playlist.partTargetDuration || playlist.targetDuration;
|
4403 | this.pendingMedia_ = null;
|
4404 |
|
4405 | if (update) {
|
4406 | this.main = update;
|
4407 | this.media_ = this.main.playlists[id];
|
4408 | } else {
|
4409 | this.trigger('playlistunchanged');
|
4410 | }
|
4411 |
|
4412 | this.updateMediaUpdateTimeout_(refreshDelay(this.media(), !!update));
|
4413 | this.trigger('loadedplaylist');
|
4414 | }
|
4415 | |
4416 |
|
4417 |
|
4418 |
|
4419 |
|
4420 | dispose() {
|
4421 | this.trigger('dispose');
|
4422 | this.stopRequest();
|
4423 | window.clearTimeout(this.mediaUpdateTimeout);
|
4424 | window.clearTimeout(this.finalRenditionTimeout);
|
4425 | this.dateRangesStorage_ = new DateRangesStorage();
|
4426 | this.off();
|
4427 | }
|
4428 |
|
4429 | stopRequest() {
|
4430 | if (this.request) {
|
4431 | const oldRequest = this.request;
|
4432 | this.request = null;
|
4433 | oldRequest.onreadystatechange = null;
|
4434 | oldRequest.abort();
|
4435 | }
|
4436 | }
|
4437 | |
4438 |
|
4439 |
|
4440 |
|
4441 |
|
4442 |
|
4443 |
|
4444 |
|
4445 |
|
4446 |
|
4447 |
|
4448 |
|
4449 |
|
4450 |
|
4451 |
|
4452 |
|
4453 | media(playlist, shouldDelay) {
|
4454 |
|
4455 | if (!playlist) {
|
4456 | return this.media_;
|
4457 | }
|
4458 |
|
4459 |
|
4460 | if (this.state === 'HAVE_NOTHING') {
|
4461 | throw new Error('Cannot switch media playlist from ' + this.state);
|
4462 | }
|
4463 |
|
4464 |
|
4465 |
|
4466 | if (typeof playlist === 'string') {
|
4467 | if (!this.main.playlists[playlist]) {
|
4468 | throw new Error('Unknown playlist URI: ' + playlist);
|
4469 | }
|
4470 |
|
4471 | playlist = this.main.playlists[playlist];
|
4472 | }
|
4473 |
|
4474 | window.clearTimeout(this.finalRenditionTimeout);
|
4475 |
|
4476 | if (shouldDelay) {
|
4477 | const delay = (playlist.partTargetDuration || playlist.targetDuration) / 2 * 1000 || 5 * 1000;
|
4478 | this.finalRenditionTimeout = window.setTimeout(this.media.bind(this, playlist, false), delay);
|
4479 | return;
|
4480 | }
|
4481 |
|
4482 | const startingState = this.state;
|
4483 | const mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
4484 | const mainPlaylistRef = this.main.playlists[playlist.id];
|
4485 |
|
4486 | if (mainPlaylistRef && mainPlaylistRef.endList ||
|
4487 |
|
4488 | playlist.endList && playlist.segments.length) {
|
4489 |
|
4490 | if (this.request) {
|
4491 | this.request.onreadystatechange = null;
|
4492 | this.request.abort();
|
4493 | this.request = null;
|
4494 | }
|
4495 |
|
4496 | this.state = 'HAVE_METADATA';
|
4497 | this.media_ = playlist;
|
4498 |
|
4499 | if (mediaChange) {
|
4500 | this.trigger('mediachanging');
|
4501 |
|
4502 | if (startingState === 'HAVE_MAIN_MANIFEST') {
|
4503 |
|
4504 |
|
4505 |
|
4506 |
|
4507 |
|
4508 | this.trigger('loadedmetadata');
|
4509 | } else {
|
4510 | this.trigger('mediachange');
|
4511 | }
|
4512 | }
|
4513 |
|
4514 | return;
|
4515 | }
|
4516 |
|
4517 |
|
4518 |
|
4519 |
|
4520 |
|
4521 |
|
4522 | this.updateMediaUpdateTimeout_(refreshDelay(playlist, true));
|
4523 |
|
4524 | if (!mediaChange) {
|
4525 | return;
|
4526 | }
|
4527 |
|
4528 | this.state = 'SWITCHING_MEDIA';
|
4529 |
|
4530 | if (this.request) {
|
4531 | if (playlist.resolvedUri === this.request.url) {
|
4532 |
|
4533 |
|
4534 | return;
|
4535 | }
|
4536 |
|
4537 | this.request.onreadystatechange = null;
|
4538 | this.request.abort();
|
4539 | this.request = null;
|
4540 | }
|
4541 |
|
4542 |
|
4543 | if (this.media_) {
|
4544 | this.trigger('mediachanging');
|
4545 | }
|
4546 |
|
4547 | this.pendingMedia_ = playlist;
|
4548 | this.request = this.vhs_.xhr({
|
4549 | uri: playlist.resolvedUri,
|
4550 | withCredentials: this.withCredentials
|
4551 | }, (error, req) => {
|
4552 |
|
4553 | if (!this.request) {
|
4554 | return;
|
4555 | }
|
4556 |
|
4557 | playlist.lastRequest = Date.now();
|
4558 | playlist.resolvedUri = resolveManifestRedirect(playlist.resolvedUri, req);
|
4559 |
|
4560 | if (error) {
|
4561 | return this.playlistRequestError(this.request, playlist, startingState);
|
4562 | }
|
4563 |
|
4564 | this.haveMetadata({
|
4565 | playlistString: req.responseText,
|
4566 | url: playlist.uri,
|
4567 | id: playlist.id
|
4568 | });
|
4569 |
|
4570 | if (startingState === 'HAVE_MAIN_MANIFEST') {
|
4571 | this.trigger('loadedmetadata');
|
4572 | } else {
|
4573 | this.trigger('mediachange');
|
4574 | }
|
4575 | });
|
4576 | }
|
4577 | |
4578 |
|
4579 |
|
4580 |
|
4581 |
|
4582 | pause() {
|
4583 | if (this.mediaUpdateTimeout) {
|
4584 | window.clearTimeout(this.mediaUpdateTimeout);
|
4585 | this.mediaUpdateTimeout = null;
|
4586 | }
|
4587 |
|
4588 | this.stopRequest();
|
4589 |
|
4590 | if (this.state === 'HAVE_NOTHING') {
|
4591 |
|
4592 |
|
4593 | this.started = false;
|
4594 | }
|
4595 |
|
4596 |
|
4597 | if (this.state === 'SWITCHING_MEDIA') {
|
4598 |
|
4599 |
|
4600 |
|
4601 | if (this.media_) {
|
4602 | this.state = 'HAVE_METADATA';
|
4603 | } else {
|
4604 | this.state = 'HAVE_MAIN_MANIFEST';
|
4605 | }
|
4606 | } else if (this.state === 'HAVE_CURRENT_METADATA') {
|
4607 | this.state = 'HAVE_METADATA';
|
4608 | }
|
4609 | }
|
4610 | |
4611 |
|
4612 |
|
4613 |
|
4614 |
|
4615 | load(shouldDelay) {
|
4616 | if (this.mediaUpdateTimeout) {
|
4617 | window.clearTimeout(this.mediaUpdateTimeout);
|
4618 | this.mediaUpdateTimeout = null;
|
4619 | }
|
4620 |
|
4621 | const media = this.media();
|
4622 |
|
4623 | if (shouldDelay) {
|
4624 | const delay = media ? (media.partTargetDuration || media.targetDuration) / 2 * 1000 : 5 * 1000;
|
4625 | this.mediaUpdateTimeout = window.setTimeout(() => {
|
4626 | this.mediaUpdateTimeout = null;
|
4627 | this.load();
|
4628 | }, delay);
|
4629 | return;
|
4630 | }
|
4631 |
|
4632 | if (!this.started) {
|
4633 | this.start();
|
4634 | return;
|
4635 | }
|
4636 |
|
4637 | if (media && !media.endList) {
|
4638 | this.trigger('mediaupdatetimeout');
|
4639 | } else {
|
4640 | this.trigger('loadedplaylist');
|
4641 | }
|
4642 | }
|
4643 |
|
4644 | updateMediaUpdateTimeout_(delay) {
|
4645 | if (this.mediaUpdateTimeout) {
|
4646 | window.clearTimeout(this.mediaUpdateTimeout);
|
4647 | this.mediaUpdateTimeout = null;
|
4648 | }
|
4649 |
|
4650 |
|
4651 | if (!this.media() || this.media().endList) {
|
4652 | return;
|
4653 | }
|
4654 |
|
4655 | this.mediaUpdateTimeout = window.setTimeout(() => {
|
4656 | this.mediaUpdateTimeout = null;
|
4657 | this.trigger('mediaupdatetimeout');
|
4658 | this.updateMediaUpdateTimeout_(delay);
|
4659 | }, delay);
|
4660 | }
|
4661 | |
4662 |
|
4663 |
|
4664 |
|
4665 |
|
4666 | start() {
|
4667 | this.started = true;
|
4668 |
|
4669 | if (typeof this.src === 'object') {
|
4670 |
|
4671 |
|
4672 | if (!this.src.uri) {
|
4673 | this.src.uri = window.location.href;
|
4674 | }
|
4675 |
|
4676 |
|
4677 |
|
4678 | this.src.resolvedUri = this.src.uri;
|
4679 |
|
4680 |
|
4681 |
|
4682 |
|
4683 |
|
4684 |
|
4685 |
|
4686 |
|
4687 |
|
4688 | setTimeout(() => {
|
4689 | this.setupInitialPlaylist(this.src);
|
4690 | }, 0);
|
4691 | return;
|
4692 | }
|
4693 |
|
4694 |
|
4695 | this.request = this.vhs_.xhr({
|
4696 | uri: this.src,
|
4697 | withCredentials: this.withCredentials
|
4698 | }, (error, req) => {
|
4699 |
|
4700 | if (!this.request) {
|
4701 | return;
|
4702 | }
|
4703 |
|
4704 |
|
4705 | this.request = null;
|
4706 |
|
4707 | if (error) {
|
4708 | this.error = {
|
4709 | status: req.status,
|
4710 | message: `HLS playlist request error at URL: ${this.src}.`,
|
4711 | responseText: req.responseText,
|
4712 |
|
4713 | code: 2
|
4714 | };
|
4715 |
|
4716 | if (this.state === 'HAVE_NOTHING') {
|
4717 | this.started = false;
|
4718 | }
|
4719 |
|
4720 | return this.trigger('error');
|
4721 | }
|
4722 |
|
4723 | this.src = resolveManifestRedirect(this.src, req);
|
4724 | const manifest = this.parseManifest_({
|
4725 | manifestString: req.responseText,
|
4726 | url: this.src
|
4727 | });
|
4728 | this.setupInitialPlaylist(manifest);
|
4729 | });
|
4730 | }
|
4731 |
|
4732 | srcUri() {
|
4733 | return typeof this.src === 'string' ? this.src : this.src.uri;
|
4734 | }
|
4735 | |
4736 |
|
4737 |
|
4738 |
|
4739 |
|
4740 |
|
4741 |
|
4742 |
|
4743 |
|
4744 |
|
4745 |
|
4746 |
|
4747 |
|
4748 |
|
4749 |
|
4750 |
|
4751 |
|
4752 |
|
4753 |
|
4754 |
|
4755 | setupInitialPlaylist(manifest) {
|
4756 | this.state = 'HAVE_MAIN_MANIFEST';
|
4757 |
|
4758 | if (manifest.playlists) {
|
4759 | this.main = manifest;
|
4760 | addPropertiesToMain(this.main, this.srcUri());
|
4761 |
|
4762 |
|
4763 |
|
4764 | manifest.playlists.forEach(playlist => {
|
4765 | playlist.segments = getAllSegments(playlist);
|
4766 | playlist.segments.forEach(segment => {
|
4767 | resolveSegmentUris(segment, playlist.resolvedUri);
|
4768 | });
|
4769 | });
|
4770 | this.trigger('loadedplaylist');
|
4771 |
|
4772 | if (!this.request) {
|
4773 |
|
4774 |
|
4775 | this.media(this.main.playlists[0]);
|
4776 | }
|
4777 |
|
4778 | return;
|
4779 | }
|
4780 |
|
4781 |
|
4782 |
|
4783 |
|
4784 | const uri = this.srcUri() || window.location.href;
|
4785 | this.main = mainForMedia(manifest, uri);
|
4786 | this.haveMetadata({
|
4787 | playlistObject: manifest,
|
4788 | url: uri,
|
4789 | id: this.main.playlists[0].id
|
4790 | });
|
4791 | this.trigger('loadedmetadata');
|
4792 | }
|
4793 | |
4794 |
|
4795 |
|
4796 |
|
4797 |
|
4798 |
|
4799 |
|
4800 |
|
4801 |
|
4802 |
|
4803 |
|
4804 |
|
4805 | updateOrDeleteClone(clone, isUpdate) {
|
4806 | const main = this.main;
|
4807 | const pathway = clone.ID;
|
4808 | let i = main.playlists.length;
|
4809 |
|
4810 | while (i--) {
|
4811 | const p = main.playlists[i];
|
4812 |
|
4813 | if (p.attributes['PATHWAY-ID'] === pathway) {
|
4814 | const oldPlaylistUri = p.resolvedUri;
|
4815 | const oldPlaylistId = p.id;
|
4816 |
|
4817 | if (isUpdate) {
|
4818 | const newPlaylistUri = this.createCloneURI_(p.resolvedUri, clone);
|
4819 | const newPlaylistId = createPlaylistID(pathway, newPlaylistUri);
|
4820 | const attributes = this.createCloneAttributes_(pathway, p.attributes);
|
4821 | const updatedPlaylist = this.createClonePlaylist_(p, newPlaylistId, clone, attributes);
|
4822 | main.playlists[i] = updatedPlaylist;
|
4823 | main.playlists[newPlaylistId] = updatedPlaylist;
|
4824 | main.playlists[newPlaylistUri] = updatedPlaylist;
|
4825 | } else {
|
4826 |
|
4827 | main.playlists.splice(i, 1);
|
4828 | }
|
4829 |
|
4830 |
|
4831 | delete main.playlists[oldPlaylistId];
|
4832 | delete main.playlists[oldPlaylistUri];
|
4833 | }
|
4834 | }
|
4835 |
|
4836 | this.updateOrDeleteCloneMedia(clone, isUpdate);
|
4837 | }
|
4838 | |
4839 |
|
4840 |
|
4841 |
|
4842 |
|
4843 |
|
4844 |
|
4845 |
|
4846 |
|
4847 |
|
4848 |
|
4849 |
|
4850 |
|
4851 | updateOrDeleteCloneMedia(clone, isUpdate) {
|
4852 | const main = this.main;
|
4853 | const id = clone.ID;
|
4854 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(mediaType => {
|
4855 | if (!main.mediaGroups[mediaType] || !main.mediaGroups[mediaType][id]) {
|
4856 | return;
|
4857 | }
|
4858 |
|
4859 | for (const groupKey in main.mediaGroups[mediaType]) {
|
4860 |
|
4861 | if (groupKey === id) {
|
4862 | for (const labelKey in main.mediaGroups[mediaType][groupKey]) {
|
4863 | const oldMedia = main.mediaGroups[mediaType][groupKey][labelKey];
|
4864 | oldMedia.playlists.forEach((p, i) => {
|
4865 | const oldMediaPlaylist = main.playlists[p.id];
|
4866 | const oldPlaylistId = oldMediaPlaylist.id;
|
4867 | const oldPlaylistUri = oldMediaPlaylist.resolvedUri;
|
4868 | delete main.playlists[oldPlaylistId];
|
4869 | delete main.playlists[oldPlaylistUri];
|
4870 | });
|
4871 | }
|
4872 |
|
4873 |
|
4874 | delete main.mediaGroups[mediaType][groupKey];
|
4875 | }
|
4876 | }
|
4877 | });
|
4878 |
|
4879 | if (isUpdate) {
|
4880 | this.createClonedMediaGroups_(clone);
|
4881 | }
|
4882 | }
|
4883 | |
4884 |
|
4885 |
|
4886 |
|
4887 |
|
4888 |
|
4889 |
|
4890 |
|
4891 | addClonePathway(clone, basePlaylist = {}) {
|
4892 | const main = this.main;
|
4893 | const index = main.playlists.length;
|
4894 | const uri = this.createCloneURI_(basePlaylist.resolvedUri, clone);
|
4895 | const playlistId = createPlaylistID(clone.ID, uri);
|
4896 | const attributes = this.createCloneAttributes_(clone.ID, basePlaylist.attributes);
|
4897 | const playlist = this.createClonePlaylist_(basePlaylist, playlistId, clone, attributes);
|
4898 | main.playlists[index] = playlist;
|
4899 |
|
4900 | main.playlists[playlistId] = playlist;
|
4901 | main.playlists[uri] = playlist;
|
4902 | this.createClonedMediaGroups_(clone);
|
4903 | }
|
4904 | |
4905 |
|
4906 |
|
4907 |
|
4908 |
|
4909 |
|
4910 |
|
4911 |
|
4912 |
|
4913 |
|
4914 |
|
4915 | createClonedMediaGroups_(clone) {
|
4916 | const id = clone.ID;
|
4917 | const baseID = clone['BASE-ID'];
|
4918 | const main = this.main;
|
4919 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(mediaType => {
|
4920 |
|
4921 |
|
4922 | if (!main.mediaGroups[mediaType] || main.mediaGroups[mediaType][id]) {
|
4923 | return;
|
4924 | }
|
4925 |
|
4926 | for (const groupKey in main.mediaGroups[mediaType]) {
|
4927 | if (groupKey === baseID) {
|
4928 |
|
4929 | main.mediaGroups[mediaType][id] = {};
|
4930 | } else {
|
4931 |
|
4932 | continue;
|
4933 | }
|
4934 |
|
4935 | for (const labelKey in main.mediaGroups[mediaType][groupKey]) {
|
4936 | const oldMedia = main.mediaGroups[mediaType][groupKey][labelKey];
|
4937 | main.mediaGroups[mediaType][id][labelKey] = _extends({}, oldMedia);
|
4938 | const newMedia = main.mediaGroups[mediaType][id][labelKey];
|
4939 |
|
4940 | const newUri = this.createCloneURI_(oldMedia.resolvedUri, clone);
|
4941 | newMedia.resolvedUri = newUri;
|
4942 | newMedia.uri = newUri;
|
4943 |
|
4944 | newMedia.playlists = [];
|
4945 |
|
4946 | oldMedia.playlists.forEach((p, i) => {
|
4947 | const oldMediaPlaylist = main.playlists[p.id];
|
4948 | const group = groupID(mediaType, id, labelKey);
|
4949 | const newPlaylistID = createPlaylistID(id, group);
|
4950 |
|
4951 | if (oldMediaPlaylist && !main.playlists[newPlaylistID]) {
|
4952 | const newMediaPlaylist = this.createClonePlaylist_(oldMediaPlaylist, newPlaylistID, clone);
|
4953 | const newPlaylistUri = newMediaPlaylist.resolvedUri;
|
4954 | main.playlists[newPlaylistID] = newMediaPlaylist;
|
4955 | main.playlists[newPlaylistUri] = newMediaPlaylist;
|
4956 | }
|
4957 |
|
4958 | newMedia.playlists[i] = this.createClonePlaylist_(p, newPlaylistID, clone);
|
4959 | });
|
4960 | }
|
4961 | }
|
4962 | });
|
4963 | }
|
4964 | |
4965 |
|
4966 |
|
4967 |
|
4968 |
|
4969 |
|
4970 |
|
4971 |
|
4972 |
|
4973 |
|
4974 |
|
4975 |
|
4976 |
|
4977 | createClonePlaylist_(basePlaylist, id, clone, attributes) {
|
4978 | const uri = this.createCloneURI_(basePlaylist.resolvedUri, clone);
|
4979 | const newProps = {
|
4980 | resolvedUri: uri,
|
4981 | uri,
|
4982 | id
|
4983 | };
|
4984 |
|
4985 | if (basePlaylist.segments) {
|
4986 | newProps.segments = [];
|
4987 | }
|
4988 |
|
4989 | if (attributes) {
|
4990 | newProps.attributes = attributes;
|
4991 | }
|
4992 |
|
4993 | return merge$1(basePlaylist, newProps);
|
4994 | }
|
4995 | |
4996 |
|
4997 |
|
4998 |
|
4999 |
|
5000 |
|
5001 |
|
5002 |
|
5003 |
|
5004 |
|
5005 |
|
5006 |
|
5007 | createCloneURI_(baseURI, clone) {
|
5008 | const uri = new URL(baseURI);
|
5009 | uri.hostname = clone['URI-REPLACEMENT'].HOST;
|
5010 | const params = clone['URI-REPLACEMENT'].PARAMS;
|
5011 |
|
5012 | for (const key of Object.keys(params)) {
|
5013 | uri.searchParams.set(key, params[key]);
|
5014 | }
|
5015 |
|
5016 | return uri.href;
|
5017 | }
|
5018 | |
5019 |
|
5020 |
|
5021 |
|
5022 |
|
5023 |
|
5024 |
|
5025 |
|
5026 |
|
5027 |
|
5028 | createCloneAttributes_(id, oldAttributes) {
|
5029 | const attributes = {
|
5030 | ['PATHWAY-ID']: id
|
5031 | };
|
5032 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(mediaType => {
|
5033 | if (oldAttributes[mediaType]) {
|
5034 | attributes[mediaType] = id;
|
5035 | }
|
5036 | });
|
5037 | return attributes;
|
5038 | }
|
5039 | |
5040 |
|
5041 |
|
5042 |
|
5043 |
|
5044 |
|
5045 |
|
5046 |
|
5047 | getKeyIdSet(playlist) {
|
5048 | if (playlist.contentProtection) {
|
5049 | const keyIds = new Set();
|
5050 |
|
5051 | for (const keysystem in playlist.contentProtection) {
|
5052 | const keyId = playlist.contentProtection[keysystem].attributes.keyId;
|
5053 |
|
5054 | if (keyId) {
|
5055 | keyIds.add(keyId.toLowerCase());
|
5056 | }
|
5057 | }
|
5058 |
|
5059 | return keyIds;
|
5060 | }
|
5061 | }
|
5062 |
|
5063 | }
|
5064 |
|
5065 | |
5066 |
|
5067 |
|
5068 | const {
|
5069 | xhr: videojsXHR
|
5070 | } = videojs__default["default"];
|
5071 |
|
5072 | const callbackWrapper = function (request, error, response, callback) {
|
5073 | const reqResponse = request.responseType === 'arraybuffer' ? request.response : request.responseText;
|
5074 |
|
5075 | if (!error && reqResponse) {
|
5076 | request.responseTime = Date.now();
|
5077 | request.roundTripTime = request.responseTime - request.requestTime;
|
5078 | request.bytesReceived = reqResponse.byteLength || reqResponse.length;
|
5079 |
|
5080 | if (!request.bandwidth) {
|
5081 | request.bandwidth = Math.floor(request.bytesReceived / request.roundTripTime * 8 * 1000);
|
5082 | }
|
5083 | }
|
5084 |
|
5085 | if (response.headers) {
|
5086 | request.responseHeaders = response.headers;
|
5087 | }
|
5088 |
|
5089 |
|
5090 |
|
5091 |
|
5092 | if (error && error.code === 'ETIMEDOUT') {
|
5093 | request.timedout = true;
|
5094 | }
|
5095 |
|
5096 |
|
5097 |
|
5098 |
|
5099 | if (!error && !request.aborted && response.statusCode !== 200 && response.statusCode !== 206 && response.statusCode !== 0) {
|
5100 | error = new Error('XHR Failed with a response of: ' + (request && (reqResponse || request.responseText)));
|
5101 | }
|
5102 |
|
5103 | callback(error, request);
|
5104 | };
|
5105 | |
5106 |
|
5107 |
|
5108 |
|
5109 |
|
5110 |
|
5111 |
|
5112 |
|
5113 |
|
5114 | const callAllRequestHooks = (requestSet, options) => {
|
5115 | if (!requestSet || !requestSet.size) {
|
5116 | return;
|
5117 | }
|
5118 |
|
5119 | let newOptions = options;
|
5120 | requestSet.forEach(requestCallback => {
|
5121 | newOptions = requestCallback(newOptions);
|
5122 | });
|
5123 | return newOptions;
|
5124 | };
|
5125 | |
5126 |
|
5127 |
|
5128 |
|
5129 |
|
5130 |
|
5131 |
|
5132 |
|
5133 |
|
5134 |
|
5135 | const callAllResponseHooks = (responseSet, request, error, response) => {
|
5136 | if (!responseSet || !responseSet.size) {
|
5137 | return;
|
5138 | }
|
5139 |
|
5140 | responseSet.forEach(responseCallback => {
|
5141 | responseCallback(request, error, response);
|
5142 | });
|
5143 | };
|
5144 |
|
5145 | const xhrFactory = function () {
|
5146 | const xhr = function XhrFunction(options, callback) {
|
5147 |
|
5148 | options = merge$1({
|
5149 | timeout: 45e3
|
5150 | }, options);
|
5151 |
|
5152 |
|
5153 |
|
5154 | const beforeRequest = XhrFunction.beforeRequest || videojs__default["default"].Vhs.xhr.beforeRequest;
|
5155 |
|
5156 |
|
5157 | const _requestCallbackSet = XhrFunction._requestCallbackSet || videojs__default["default"].Vhs.xhr._requestCallbackSet || new Set();
|
5158 |
|
5159 | const _responseCallbackSet = XhrFunction._responseCallbackSet || videojs__default["default"].Vhs.xhr._responseCallbackSet;
|
5160 |
|
5161 | if (beforeRequest && typeof beforeRequest === 'function') {
|
5162 | videojs__default["default"].log.warn('beforeRequest is deprecated, use onRequest instead.');
|
5163 |
|
5164 | _requestCallbackSet.add(beforeRequest);
|
5165 | }
|
5166 |
|
5167 |
|
5168 |
|
5169 | const xhrMethod = videojs__default["default"].Vhs.xhr.original === true ? videojsXHR : videojs__default["default"].Vhs.xhr;
|
5170 |
|
5171 | const beforeRequestOptions = callAllRequestHooks(_requestCallbackSet, options);
|
5172 |
|
5173 | _requestCallbackSet.delete(beforeRequest);
|
5174 |
|
5175 |
|
5176 | const request = xhrMethod(beforeRequestOptions || options, function (error, response) {
|
5177 |
|
5178 | callAllResponseHooks(_responseCallbackSet, request, error, response);
|
5179 | return callbackWrapper(request, error, response, callback);
|
5180 | });
|
5181 | const originalAbort = request.abort;
|
5182 |
|
5183 | request.abort = function () {
|
5184 | request.aborted = true;
|
5185 | return originalAbort.apply(request, arguments);
|
5186 | };
|
5187 |
|
5188 | request.uri = options.uri;
|
5189 | request.requestTime = Date.now();
|
5190 | return request;
|
5191 | };
|
5192 |
|
5193 | xhr.original = true;
|
5194 | return xhr;
|
5195 | };
|
5196 | |
5197 |
|
5198 |
|
5199 |
|
5200 |
|
5201 |
|
5202 |
|
5203 |
|
5204 |
|
5205 | const byterangeStr = function (byterange) {
|
5206 |
|
5207 |
|
5208 | let byterangeEnd;
|
5209 | const byterangeStart = byterange.offset;
|
5210 |
|
5211 | if (typeof byterange.offset === 'bigint' || typeof byterange.length === 'bigint') {
|
5212 | byterangeEnd = window.BigInt(byterange.offset) + window.BigInt(byterange.length) - window.BigInt(1);
|
5213 | } else {
|
5214 | byterangeEnd = byterange.offset + byterange.length - 1;
|
5215 | }
|
5216 |
|
5217 | return 'bytes=' + byterangeStart + '-' + byterangeEnd;
|
5218 | };
|
5219 | |
5220 |
|
5221 |
|
5222 |
|
5223 |
|
5224 |
|
5225 |
|
5226 | const segmentXhrHeaders = function (segment) {
|
5227 | const headers = {};
|
5228 |
|
5229 | if (segment.byterange) {
|
5230 | headers.Range = byterangeStr(segment.byterange);
|
5231 | }
|
5232 |
|
5233 | return headers;
|
5234 | };
|
5235 |
|
5236 | var MPEGURL_REGEX = /^(audio|video|application)\/(x-|vnd\.apple\.)?mpegurl/i;
|
5237 | var DASH_REGEX = /^application\/dash\+xml/i;
|
5238 | |
5239 |
|
5240 |
|
5241 |
|
5242 |
|
5243 |
|
5244 |
|
5245 |
|
5246 |
|
5247 |
|
5248 |
|
5249 |
|
5250 | var simpleTypeFromSourceType = function simpleTypeFromSourceType(type) {
|
5251 | if (MPEGURL_REGEX.test(type)) {
|
5252 | return 'hls';
|
5253 | }
|
5254 |
|
5255 | if (DASH_REGEX.test(type)) {
|
5256 | return 'dash';
|
5257 | }
|
5258 |
|
5259 |
|
5260 |
|
5261 |
|
5262 |
|
5263 |
|
5264 |
|
5265 |
|
5266 | if (type === 'application/vnd.videojs.vhs+json') {
|
5267 | return 'vhs-json';
|
5268 | }
|
5269 |
|
5270 | return null;
|
5271 | };
|
5272 |
|
5273 |
|
5274 |
|
5275 |
|
5276 |
|
5277 |
|
5278 | var countBits = function countBits(x) {
|
5279 | return x.toString(2).length;
|
5280 | };
|
5281 |
|
5282 | var countBytes = function countBytes(x) {
|
5283 | return Math.ceil(countBits(x) / 8);
|
5284 | };
|
5285 | var isArrayBufferView = function isArrayBufferView(obj) {
|
5286 | if (ArrayBuffer.isView === 'function') {
|
5287 | return ArrayBuffer.isView(obj);
|
5288 | }
|
5289 |
|
5290 | return obj && obj.buffer instanceof ArrayBuffer;
|
5291 | };
|
5292 | var isTypedArray = function isTypedArray(obj) {
|
5293 | return isArrayBufferView(obj);
|
5294 | };
|
5295 | var toUint8 = function toUint8(bytes) {
|
5296 | if (bytes instanceof Uint8Array) {
|
5297 | return bytes;
|
5298 | }
|
5299 |
|
5300 | if (!Array.isArray(bytes) && !isTypedArray(bytes) && !(bytes instanceof ArrayBuffer)) {
|
5301 |
|
5302 |
|
5303 | if (typeof bytes !== 'number' || typeof bytes === 'number' && bytes !== bytes) {
|
5304 | bytes = 0;
|
5305 | } else {
|
5306 | bytes = [bytes];
|
5307 | }
|
5308 | }
|
5309 |
|
5310 | return new Uint8Array(bytes && bytes.buffer || bytes, bytes && bytes.byteOffset || 0, bytes && bytes.byteLength || 0);
|
5311 | };
|
5312 | var BigInt = window.BigInt || Number;
|
5313 | var BYTE_TABLE = [BigInt('0x1'), BigInt('0x100'), BigInt('0x10000'), BigInt('0x1000000'), BigInt('0x100000000'), BigInt('0x10000000000'), BigInt('0x1000000000000'), BigInt('0x100000000000000'), BigInt('0x10000000000000000')];
|
5314 | (function () {
|
5315 | var a = new Uint16Array([0xFFCC]);
|
5316 | var b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
5317 |
|
5318 | if (b[0] === 0xFF) {
|
5319 | return 'big';
|
5320 | }
|
5321 |
|
5322 | if (b[0] === 0xCC) {
|
5323 | return 'little';
|
5324 | }
|
5325 |
|
5326 | return 'unknown';
|
5327 | })();
|
5328 | var bytesToNumber = function bytesToNumber(bytes, _temp) {
|
5329 | var _ref = _temp === void 0 ? {} : _temp,
|
5330 | _ref$signed = _ref.signed,
|
5331 | signed = _ref$signed === void 0 ? false : _ref$signed,
|
5332 | _ref$le = _ref.le,
|
5333 | le = _ref$le === void 0 ? false : _ref$le;
|
5334 |
|
5335 | bytes = toUint8(bytes);
|
5336 | var fn = le ? 'reduce' : 'reduceRight';
|
5337 | var obj = bytes[fn] ? bytes[fn] : Array.prototype[fn];
|
5338 | var number = obj.call(bytes, function (total, byte, i) {
|
5339 | var exponent = le ? i : Math.abs(i + 1 - bytes.length);
|
5340 | return total + BigInt(byte) * BYTE_TABLE[exponent];
|
5341 | }, BigInt(0));
|
5342 |
|
5343 | if (signed) {
|
5344 | var max = BYTE_TABLE[bytes.length] / BigInt(2) - BigInt(1);
|
5345 | number = BigInt(number);
|
5346 |
|
5347 | if (number > max) {
|
5348 | number -= max;
|
5349 | number -= max;
|
5350 | number -= BigInt(2);
|
5351 | }
|
5352 | }
|
5353 |
|
5354 | return Number(number);
|
5355 | };
|
5356 | var numberToBytes = function numberToBytes(number, _temp2) {
|
5357 | var _ref2 = _temp2 === void 0 ? {} : _temp2,
|
5358 | _ref2$le = _ref2.le,
|
5359 | le = _ref2$le === void 0 ? false : _ref2$le;
|
5360 |
|
5361 |
|
5362 | if (typeof number !== 'bigint' && typeof number !== 'number' || typeof number === 'number' && number !== number) {
|
5363 | number = 0;
|
5364 | }
|
5365 |
|
5366 | number = BigInt(number);
|
5367 | var byteCount = countBytes(number);
|
5368 | var bytes = new Uint8Array(new ArrayBuffer(byteCount));
|
5369 |
|
5370 | for (var i = 0; i < byteCount; i++) {
|
5371 | var byteIndex = le ? i : Math.abs(i + 1 - bytes.length);
|
5372 | bytes[byteIndex] = Number(number / BYTE_TABLE[i] & BigInt(0xFF));
|
5373 |
|
5374 | if (number < 0) {
|
5375 | bytes[byteIndex] = Math.abs(~bytes[byteIndex]);
|
5376 | bytes[byteIndex] -= i === 0 ? 1 : 2;
|
5377 | }
|
5378 | }
|
5379 |
|
5380 | return bytes;
|
5381 | };
|
5382 | var stringToBytes = function stringToBytes(string, stringIsBytes) {
|
5383 | if (typeof string !== 'string' && string && typeof string.toString === 'function') {
|
5384 | string = string.toString();
|
5385 | }
|
5386 |
|
5387 | if (typeof string !== 'string') {
|
5388 | return new Uint8Array();
|
5389 | }
|
5390 |
|
5391 |
|
5392 |
|
5393 |
|
5394 | if (!stringIsBytes) {
|
5395 | string = unescape(encodeURIComponent(string));
|
5396 | }
|
5397 |
|
5398 | var view = new Uint8Array(string.length);
|
5399 |
|
5400 | for (var i = 0; i < string.length; i++) {
|
5401 | view[i] = string.charCodeAt(i);
|
5402 | }
|
5403 |
|
5404 | return view;
|
5405 | };
|
5406 | var concatTypedArrays = function concatTypedArrays() {
|
5407 | for (var _len = arguments.length, buffers = new Array(_len), _key = 0; _key < _len; _key++) {
|
5408 | buffers[_key] = arguments[_key];
|
5409 | }
|
5410 |
|
5411 | buffers = buffers.filter(function (b) {
|
5412 | return b && (b.byteLength || b.length) && typeof b !== 'string';
|
5413 | });
|
5414 |
|
5415 | if (buffers.length <= 1) {
|
5416 |
|
5417 |
|
5418 | return toUint8(buffers[0]);
|
5419 | }
|
5420 |
|
5421 | var totalLen = buffers.reduce(function (total, buf, i) {
|
5422 | return total + (buf.byteLength || buf.length);
|
5423 | }, 0);
|
5424 | var tempBuffer = new Uint8Array(totalLen);
|
5425 | var offset = 0;
|
5426 | buffers.forEach(function (buf) {
|
5427 | buf = toUint8(buf);
|
5428 | tempBuffer.set(buf, offset);
|
5429 | offset += buf.byteLength;
|
5430 | });
|
5431 | return tempBuffer;
|
5432 | };
|
5433 | |
5434 |
|
5435 |
|
5436 |
|
5437 |
|
5438 |
|
5439 |
|
5440 |
|
5441 |
|
5442 |
|
5443 |
|
5444 |
|
5445 |
|
5446 |
|
5447 |
|
5448 |
|
5449 |
|
5450 |
|
5451 |
|
5452 |
|
5453 |
|
5454 |
|
5455 |
|
5456 | var bytesMatch = function bytesMatch(a, b, _temp3) {
|
5457 | var _ref3 = _temp3 === void 0 ? {} : _temp3,
|
5458 | _ref3$offset = _ref3.offset,
|
5459 | offset = _ref3$offset === void 0 ? 0 : _ref3$offset,
|
5460 | _ref3$mask = _ref3.mask,
|
5461 | mask = _ref3$mask === void 0 ? [] : _ref3$mask;
|
5462 |
|
5463 | a = toUint8(a);
|
5464 | b = toUint8(b);
|
5465 |
|
5466 | var fn = b.every ? b.every : Array.prototype.every;
|
5467 | return b.length && a.length - offset >= b.length &&
|
5468 | fn.call(b, function (bByte, i) {
|
5469 | var aByte = mask[i] ? mask[i] & a[offset + i] : a[offset + i];
|
5470 | return bByte === aByte;
|
5471 | });
|
5472 | };
|
5473 |
|
5474 | |
5475 |
|
5476 |
|
5477 |
|
5478 | |
5479 |
|
5480 |
|
5481 |
|
5482 |
|
5483 |
|
5484 |
|
5485 |
|
5486 | const textRange = function (range, i) {
|
5487 | return range.start(i) + '-' + range.end(i);
|
5488 | };
|
5489 | |
5490 |
|
5491 |
|
5492 |
|
5493 |
|
5494 |
|
5495 |
|
5496 |
|
5497 |
|
5498 | const formatHexString = function (e, i) {
|
5499 | const value = e.toString(16);
|
5500 | return '00'.substring(0, 2 - value.length) + value + (i % 2 ? ' ' : '');
|
5501 | };
|
5502 |
|
5503 | const formatAsciiString = function (e) {
|
5504 | if (e >= 0x20 && e < 0x7e) {
|
5505 | return String.fromCharCode(e);
|
5506 | }
|
5507 |
|
5508 | return '.';
|
5509 | };
|
5510 | |
5511 |
|
5512 |
|
5513 |
|
5514 |
|
5515 |
|
5516 |
|
5517 |
|
5518 |
|
5519 |
|
5520 |
|
5521 |
|
5522 | const createTransferableMessage = function (message) {
|
5523 | const transferable = {};
|
5524 | Object.keys(message).forEach(key => {
|
5525 | const value = message[key];
|
5526 |
|
5527 | if (isArrayBufferView(value)) {
|
5528 | transferable[key] = {
|
5529 | bytes: value.buffer,
|
5530 | byteOffset: value.byteOffset,
|
5531 | byteLength: value.byteLength
|
5532 | };
|
5533 | } else {
|
5534 | transferable[key] = value;
|
5535 | }
|
5536 | });
|
5537 | return transferable;
|
5538 | };
|
5539 | |
5540 |
|
5541 |
|
5542 |
|
5543 |
|
5544 |
|
5545 |
|
5546 |
|
5547 |
|
5548 |
|
5549 | const initSegmentId = function (initSegment) {
|
5550 | const byterange = initSegment.byterange || {
|
5551 | length: Infinity,
|
5552 | offset: 0
|
5553 | };
|
5554 | return [byterange.length, byterange.offset, initSegment.resolvedUri].join(',');
|
5555 | };
|
5556 | |
5557 |
|
5558 |
|
5559 |
|
5560 |
|
5561 |
|
5562 |
|
5563 | const segmentKeyId = function (key) {
|
5564 | return key.resolvedUri;
|
5565 | };
|
5566 | |
5567 |
|
5568 |
|
5569 |
|
5570 |
|
5571 |
|
5572 |
|
5573 |
|
5574 |
|
5575 | const hexDump = data => {
|
5576 | const bytes = Array.prototype.slice.call(data);
|
5577 | const step = 16;
|
5578 | let result = '';
|
5579 | let hex;
|
5580 | let ascii;
|
5581 |
|
5582 | for (let j = 0; j < bytes.length / step; j++) {
|
5583 | hex = bytes.slice(j * step, j * step + step).map(formatHexString).join('');
|
5584 | ascii = bytes.slice(j * step, j * step + step).map(formatAsciiString).join('');
|
5585 | result += hex + ' ' + ascii + '\n';
|
5586 | }
|
5587 |
|
5588 | return result;
|
5589 | };
|
5590 | const tagDump = ({
|
5591 | bytes
|
5592 | }) => hexDump(bytes);
|
5593 | const textRanges = ranges => {
|
5594 | let result = '';
|
5595 | let i;
|
5596 |
|
5597 | for (i = 0; i < ranges.length; i++) {
|
5598 | result += textRange(ranges, i) + ' ';
|
5599 | }
|
5600 |
|
5601 | return result;
|
5602 | };
|
5603 |
|
5604 | var utils = Object.freeze({
|
5605 | __proto__: null,
|
5606 | createTransferableMessage: createTransferableMessage,
|
5607 | initSegmentId: initSegmentId,
|
5608 | segmentKeyId: segmentKeyId,
|
5609 | hexDump: hexDump,
|
5610 | tagDump: tagDump,
|
5611 | textRanges: textRanges
|
5612 | });
|
5613 |
|
5614 |
|
5615 |
|
5616 |
|
5617 | const SEGMENT_END_FUDGE_PERCENT = 0.25;
|
5618 | |
5619 |
|
5620 |
|
5621 |
|
5622 |
|
5623 |
|
5624 |
|
5625 |
|
5626 |
|
5627 |
|
5628 |
|
5629 |
|
5630 |
|
5631 |
|
5632 |
|
5633 |
|
5634 | const playerTimeToProgramTime = (playerTime, segment) => {
|
5635 | if (!segment.dateTimeObject) {
|
5636 |
|
5637 |
|
5638 | return null;
|
5639 | }
|
5640 |
|
5641 | const transmuxerPrependedSeconds = segment.videoTimingInfo.transmuxerPrependedSeconds;
|
5642 | const transmuxedStart = segment.videoTimingInfo.transmuxedPresentationStart;
|
5643 |
|
5644 | const startOfSegment = transmuxedStart + transmuxerPrependedSeconds;
|
5645 | const offsetFromSegmentStart = playerTime - startOfSegment;
|
5646 | return new Date(segment.dateTimeObject.getTime() + offsetFromSegmentStart * 1000);
|
5647 | };
|
5648 | const originalSegmentVideoDuration = videoTimingInfo => {
|
5649 | return videoTimingInfo.transmuxedPresentationEnd - videoTimingInfo.transmuxedPresentationStart - videoTimingInfo.transmuxerPrependedSeconds;
|
5650 | };
|
5651 | |
5652 |
|
5653 |
|
5654 |
|
5655 |
|
5656 |
|
5657 |
|
5658 |
|
5659 | const findSegmentForProgramTime = (programTime, playlist) => {
|
5660 |
|
5661 |
|
5662 |
|
5663 | let dateTimeObject;
|
5664 |
|
5665 | try {
|
5666 | dateTimeObject = new Date(programTime);
|
5667 | } catch (e) {
|
5668 | return null;
|
5669 | }
|
5670 |
|
5671 | if (!playlist || !playlist.segments || playlist.segments.length === 0) {
|
5672 | return null;
|
5673 | }
|
5674 |
|
5675 | let segment = playlist.segments[0];
|
5676 |
|
5677 | if (dateTimeObject < new Date(segment.dateTimeObject)) {
|
5678 |
|
5679 | return null;
|
5680 | }
|
5681 |
|
5682 | for (let i = 0; i < playlist.segments.length - 1; i++) {
|
5683 | segment = playlist.segments[i];
|
5684 | const nextSegmentStart = new Date(playlist.segments[i + 1].dateTimeObject);
|
5685 |
|
5686 | if (dateTimeObject < nextSegmentStart) {
|
5687 | break;
|
5688 | }
|
5689 | }
|
5690 |
|
5691 | const lastSegment = playlist.segments[playlist.segments.length - 1];
|
5692 | const lastSegmentStart = lastSegment.dateTimeObject;
|
5693 | const lastSegmentDuration = lastSegment.videoTimingInfo ? originalSegmentVideoDuration(lastSegment.videoTimingInfo) : lastSegment.duration + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT;
|
5694 | const lastSegmentEnd = new Date(lastSegmentStart.getTime() + lastSegmentDuration * 1000);
|
5695 |
|
5696 | if (dateTimeObject > lastSegmentEnd) {
|
5697 |
|
5698 | return null;
|
5699 | }
|
5700 |
|
5701 | if (dateTimeObject > new Date(lastSegmentStart)) {
|
5702 | segment = lastSegment;
|
5703 | }
|
5704 |
|
5705 | return {
|
5706 | segment,
|
5707 | estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : Playlist.duration(playlist, playlist.mediaSequence + playlist.segments.indexOf(segment)),
|
5708 |
|
5709 |
|
5710 |
|
5711 |
|
5712 | type: segment.videoTimingInfo ? 'accurate' : 'estimate'
|
5713 | };
|
5714 | };
|
5715 | |
5716 |
|
5717 |
|
5718 |
|
5719 |
|
5720 |
|
5721 |
|
5722 | const findSegmentForPlayerTime = (time, playlist) => {
|
5723 |
|
5724 |
|
5725 |
|
5726 |
|
5727 | if (!playlist || !playlist.segments || playlist.segments.length === 0) {
|
5728 | return null;
|
5729 | }
|
5730 |
|
5731 | let segmentEnd = 0;
|
5732 | let segment;
|
5733 |
|
5734 | for (let i = 0; i < playlist.segments.length; i++) {
|
5735 | segment = playlist.segments[i];
|
5736 |
|
5737 |
|
5738 |
|
5739 |
|
5740 |
|
5741 |
|
5742 | segmentEnd = segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationEnd : segmentEnd + segment.duration;
|
5743 |
|
5744 | if (time <= segmentEnd) {
|
5745 | break;
|
5746 | }
|
5747 | }
|
5748 |
|
5749 | const lastSegment = playlist.segments[playlist.segments.length - 1];
|
5750 |
|
5751 | if (lastSegment.videoTimingInfo && lastSegment.videoTimingInfo.transmuxedPresentationEnd < time) {
|
5752 |
|
5753 | return null;
|
5754 | }
|
5755 |
|
5756 | if (time > segmentEnd) {
|
5757 |
|
5758 |
|
5759 |
|
5760 | if (time > segmentEnd + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT) {
|
5761 |
|
5762 |
|
5763 |
|
5764 | return null;
|
5765 | }
|
5766 |
|
5767 | segment = lastSegment;
|
5768 | }
|
5769 |
|
5770 | return {
|
5771 | segment,
|
5772 | estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : segmentEnd - segment.duration,
|
5773 |
|
5774 |
|
5775 | type: segment.videoTimingInfo ? 'accurate' : 'estimate'
|
5776 | };
|
5777 | };
|
5778 | |
5779 |
|
5780 |
|
5781 |
|
5782 |
|
5783 |
|
5784 |
|
5785 |
|
5786 |
|
5787 |
|
5788 |
|
5789 | const getOffsetFromTimestamp = (comparisonTimeStamp, programTime) => {
|
5790 | let segmentDateTime;
|
5791 | let programDateTime;
|
5792 |
|
5793 | try {
|
5794 | segmentDateTime = new Date(comparisonTimeStamp);
|
5795 | programDateTime = new Date(programTime);
|
5796 | } catch (e) {
|
5797 | }
|
5798 |
|
5799 | const segmentTimeEpoch = segmentDateTime.getTime();
|
5800 | const programTimeEpoch = programDateTime.getTime();
|
5801 | return (programTimeEpoch - segmentTimeEpoch) / 1000;
|
5802 | };
|
5803 | |
5804 |
|
5805 |
|
5806 |
|
5807 |
|
5808 |
|
5809 | const verifyProgramDateTimeTags = playlist => {
|
5810 | if (!playlist.segments || playlist.segments.length === 0) {
|
5811 | return false;
|
5812 | }
|
5813 |
|
5814 | for (let i = 0; i < playlist.segments.length; i++) {
|
5815 | const segment = playlist.segments[i];
|
5816 |
|
5817 | if (!segment.dateTimeObject) {
|
5818 | return false;
|
5819 | }
|
5820 | }
|
5821 |
|
5822 | return true;
|
5823 | };
|
5824 | |
5825 |
|
5826 |
|
5827 |
|
5828 |
|
5829 |
|
5830 |
|
5831 |
|
5832 |
|
5833 |
|
5834 |
|
5835 |
|
5836 |
|
5837 |
|
5838 |
|
5839 |
|
5840 | const getProgramTime = ({
|
5841 | playlist,
|
5842 | time = undefined,
|
5843 | callback
|
5844 | }) => {
|
5845 | if (!callback) {
|
5846 | throw new Error('getProgramTime: callback must be provided');
|
5847 | }
|
5848 |
|
5849 | if (!playlist || time === undefined) {
|
5850 | return callback({
|
5851 | message: 'getProgramTime: playlist and time must be provided'
|
5852 | });
|
5853 | }
|
5854 |
|
5855 | const matchedSegment = findSegmentForPlayerTime(time, playlist);
|
5856 |
|
5857 | if (!matchedSegment) {
|
5858 | return callback({
|
5859 | message: 'valid programTime was not found'
|
5860 | });
|
5861 | }
|
5862 |
|
5863 | if (matchedSegment.type === 'estimate') {
|
5864 | return callback({
|
5865 | message: 'Accurate programTime could not be determined.' + ' Please seek to e.seekTime and try again',
|
5866 | seekTime: matchedSegment.estimatedStart
|
5867 | });
|
5868 | }
|
5869 |
|
5870 | const programTimeObject = {
|
5871 | mediaSeconds: time
|
5872 | };
|
5873 | const programTime = playerTimeToProgramTime(time, matchedSegment.segment);
|
5874 |
|
5875 | if (programTime) {
|
5876 | programTimeObject.programDateTime = programTime.toISOString();
|
5877 | }
|
5878 |
|
5879 | return callback(null, programTimeObject);
|
5880 | };
|
5881 | |
5882 |
|
5883 |
|
5884 |
|
5885 |
|
5886 |
|
5887 |
|
5888 |
|
5889 |
|
5890 |
|
5891 |
|
5892 |
|
5893 |
|
5894 |
|
5895 |
|
5896 | const seekToProgramTime = ({
|
5897 | programTime,
|
5898 | playlist,
|
5899 | retryCount = 2,
|
5900 | seekTo,
|
5901 | pauseAfterSeek = true,
|
5902 | tech,
|
5903 | callback
|
5904 | }) => {
|
5905 | if (!callback) {
|
5906 | throw new Error('seekToProgramTime: callback must be provided');
|
5907 | }
|
5908 |
|
5909 | if (typeof programTime === 'undefined' || !playlist || !seekTo) {
|
5910 | return callback({
|
5911 | message: 'seekToProgramTime: programTime, seekTo and playlist must be provided'
|
5912 | });
|
5913 | }
|
5914 |
|
5915 | if (!playlist.endList && !tech.hasStarted_) {
|
5916 | return callback({
|
5917 | message: 'player must be playing a live stream to start buffering'
|
5918 | });
|
5919 | }
|
5920 |
|
5921 | if (!verifyProgramDateTimeTags(playlist)) {
|
5922 | return callback({
|
5923 | message: 'programDateTime tags must be provided in the manifest ' + playlist.resolvedUri
|
5924 | });
|
5925 | }
|
5926 |
|
5927 | const matchedSegment = findSegmentForProgramTime(programTime, playlist);
|
5928 |
|
5929 | if (!matchedSegment) {
|
5930 | return callback({
|
5931 | message: `${programTime} was not found in the stream`
|
5932 | });
|
5933 | }
|
5934 |
|
5935 | const segment = matchedSegment.segment;
|
5936 | const mediaOffset = getOffsetFromTimestamp(segment.dateTimeObject, programTime);
|
5937 |
|
5938 | if (matchedSegment.type === 'estimate') {
|
5939 |
|
5940 | if (retryCount === 0) {
|
5941 | return callback({
|
5942 | message: `${programTime} is not buffered yet. Try again`
|
5943 | });
|
5944 | }
|
5945 |
|
5946 | seekTo(matchedSegment.estimatedStart + mediaOffset);
|
5947 | tech.one('seeked', () => {
|
5948 | seekToProgramTime({
|
5949 | programTime,
|
5950 | playlist,
|
5951 | retryCount: retryCount - 1,
|
5952 | seekTo,
|
5953 | pauseAfterSeek,
|
5954 | tech,
|
5955 | callback
|
5956 | });
|
5957 | });
|
5958 | return;
|
5959 | }
|
5960 |
|
5961 |
|
5962 |
|
5963 |
|
5964 | const seekToTime = segment.start + mediaOffset;
|
5965 |
|
5966 | const seekedCallback = () => {
|
5967 | return callback(null, tech.currentTime());
|
5968 | };
|
5969 |
|
5970 |
|
5971 | tech.one('seeked', seekedCallback);
|
5972 |
|
5973 | if (pauseAfterSeek) {
|
5974 | tech.pause();
|
5975 | }
|
5976 |
|
5977 | seekTo(seekToTime);
|
5978 | };
|
5979 |
|
5980 | |
5981 |
|
5982 |
|
5983 |
|
5984 |
|
5985 |
|
5986 |
|
5987 |
|
5988 |
|
5989 |
|
5990 |
|
5991 | var forEachMediaGroup = function forEachMediaGroup(master, groups, callback) {
|
5992 | groups.forEach(function (mediaType) {
|
5993 | for (var groupKey in master.mediaGroups[mediaType]) {
|
5994 | for (var labelKey in master.mediaGroups[mediaType][groupKey]) {
|
5995 | var mediaProperties = master.mediaGroups[mediaType][groupKey][labelKey];
|
5996 | callback(mediaProperties, mediaType, groupKey, labelKey);
|
5997 | }
|
5998 | }
|
5999 | });
|
6000 | };
|
6001 |
|
6002 |
|
6003 |
|
6004 | const isObject = obj => {
|
6005 | return !!obj && typeof obj === 'object';
|
6006 | };
|
6007 |
|
6008 | const merge = (...objects) => {
|
6009 | return objects.reduce((result, source) => {
|
6010 | if (typeof source !== 'object') {
|
6011 | return result;
|
6012 | }
|
6013 |
|
6014 | Object.keys(source).forEach(key => {
|
6015 | if (Array.isArray(result[key]) && Array.isArray(source[key])) {
|
6016 | result[key] = result[key].concat(source[key]);
|
6017 | } else if (isObject(result[key]) && isObject(source[key])) {
|
6018 | result[key] = merge(result[key], source[key]);
|
6019 | } else {
|
6020 | result[key] = source[key];
|
6021 | }
|
6022 | });
|
6023 | return result;
|
6024 | }, {});
|
6025 | };
|
6026 |
|
6027 | const values = o => Object.keys(o).map(k => o[k]);
|
6028 |
|
6029 | const range = (start, end) => {
|
6030 | const result = [];
|
6031 |
|
6032 | for (let i = start; i < end; i++) {
|
6033 | result.push(i);
|
6034 | }
|
6035 |
|
6036 | return result;
|
6037 | };
|
6038 |
|
6039 | const flatten = lists => lists.reduce((x, y) => x.concat(y), []);
|
6040 |
|
6041 | const from = list => {
|
6042 | if (!list.length) {
|
6043 | return [];
|
6044 | }
|
6045 |
|
6046 | const result = [];
|
6047 |
|
6048 | for (let i = 0; i < list.length; i++) {
|
6049 | result.push(list[i]);
|
6050 | }
|
6051 |
|
6052 | return result;
|
6053 | };
|
6054 |
|
6055 | const findIndexes = (l, key) => l.reduce((a, e, i) => {
|
6056 | if (e[key]) {
|
6057 | a.push(i);
|
6058 | }
|
6059 |
|
6060 | return a;
|
6061 | }, []);
|
6062 | |
6063 |
|
6064 |
|
6065 |
|
6066 |
|
6067 |
|
6068 |
|
6069 |
|
6070 |
|
6071 |
|
6072 | const union = (lists, keyFunction) => {
|
6073 | return values(lists.reduce((acc, list) => {
|
6074 | list.forEach(el => {
|
6075 | acc[keyFunction(el)] = el;
|
6076 | });
|
6077 | return acc;
|
6078 | }, {}));
|
6079 | };
|
6080 |
|
6081 | var errors = {
|
6082 | INVALID_NUMBER_OF_PERIOD: 'INVALID_NUMBER_OF_PERIOD',
|
6083 | INVALID_NUMBER_OF_CONTENT_STEERING: 'INVALID_NUMBER_OF_CONTENT_STEERING',
|
6084 | DASH_EMPTY_MANIFEST: 'DASH_EMPTY_MANIFEST',
|
6085 | DASH_INVALID_XML: 'DASH_INVALID_XML',
|
6086 | NO_BASE_URL: 'NO_BASE_URL',
|
6087 | MISSING_SEGMENT_INFORMATION: 'MISSING_SEGMENT_INFORMATION',
|
6088 | SEGMENT_TIME_UNSPECIFIED: 'SEGMENT_TIME_UNSPECIFIED',
|
6089 | UNSUPPORTED_UTC_TIMING_SCHEME: 'UNSUPPORTED_UTC_TIMING_SCHEME'
|
6090 | };
|
6091 | |
6092 |
|
6093 |
|
6094 |
|
6095 |
|
6096 |
|
6097 |
|
6098 |
|
6099 |
|
6100 |
|
6101 |
|
6102 |
|
6103 | |
6104 |
|
6105 |
|
6106 |
|
6107 |
|
6108 |
|
6109 |
|
6110 |
|
6111 |
|
6112 |
|
6113 |
|
6114 |
|
6115 |
|
6116 |
|
6117 | const urlTypeToSegment = ({
|
6118 | baseUrl = '',
|
6119 | source = '',
|
6120 | range = '',
|
6121 | indexRange = ''
|
6122 | }) => {
|
6123 | const segment = {
|
6124 | uri: source,
|
6125 | resolvedUri: resolveUrl$1(baseUrl || '', source)
|
6126 | };
|
6127 |
|
6128 | if (range || indexRange) {
|
6129 | const rangeStr = range ? range : indexRange;
|
6130 | const ranges = rangeStr.split('-');
|
6131 |
|
6132 | let startRange = window.BigInt ? window.BigInt(ranges[0]) : parseInt(ranges[0], 10);
|
6133 | let endRange = window.BigInt ? window.BigInt(ranges[1]) : parseInt(ranges[1], 10);
|
6134 |
|
6135 | if (startRange < Number.MAX_SAFE_INTEGER && typeof startRange === 'bigint') {
|
6136 | startRange = Number(startRange);
|
6137 | }
|
6138 |
|
6139 | if (endRange < Number.MAX_SAFE_INTEGER && typeof endRange === 'bigint') {
|
6140 | endRange = Number(endRange);
|
6141 | }
|
6142 |
|
6143 | let length;
|
6144 |
|
6145 | if (typeof endRange === 'bigint' || typeof startRange === 'bigint') {
|
6146 | length = window.BigInt(endRange) - window.BigInt(startRange) + window.BigInt(1);
|
6147 | } else {
|
6148 | length = endRange - startRange + 1;
|
6149 | }
|
6150 |
|
6151 | if (typeof length === 'bigint' && length < Number.MAX_SAFE_INTEGER) {
|
6152 | length = Number(length);
|
6153 | }
|
6154 |
|
6155 |
|
6156 |
|
6157 | segment.byterange = {
|
6158 | length,
|
6159 | offset: startRange
|
6160 | };
|
6161 | }
|
6162 |
|
6163 | return segment;
|
6164 | };
|
6165 |
|
6166 | const byteRangeToString = byterange => {
|
6167 |
|
6168 |
|
6169 | let endRange;
|
6170 |
|
6171 | if (typeof byterange.offset === 'bigint' || typeof byterange.length === 'bigint') {
|
6172 | endRange = window.BigInt(byterange.offset) + window.BigInt(byterange.length) - window.BigInt(1);
|
6173 | } else {
|
6174 | endRange = byterange.offset + byterange.length - 1;
|
6175 | }
|
6176 |
|
6177 | return `${byterange.offset}-${endRange}`;
|
6178 | };
|
6179 | |
6180 |
|
6181 |
|
6182 |
|
6183 |
|
6184 |
|
6185 |
|
6186 |
|
6187 |
|
6188 |
|
6189 |
|
6190 |
|
6191 | const parseEndNumber = endNumber => {
|
6192 | if (endNumber && typeof endNumber !== 'number') {
|
6193 | endNumber = parseInt(endNumber, 10);
|
6194 | }
|
6195 |
|
6196 | if (isNaN(endNumber)) {
|
6197 | return null;
|
6198 | }
|
6199 |
|
6200 | return endNumber;
|
6201 | };
|
6202 | |
6203 |
|
6204 |
|
6205 |
|
6206 |
|
6207 |
|
6208 | const segmentRange = {
|
6209 | |
6210 |
|
6211 |
|
6212 |
|
6213 |
|
6214 |
|
6215 |
|
6216 |
|
6217 | static(attributes) {
|
6218 | const {
|
6219 | duration,
|
6220 | timescale = 1,
|
6221 | sourceDuration,
|
6222 | periodDuration
|
6223 | } = attributes;
|
6224 | const endNumber = parseEndNumber(attributes.endNumber);
|
6225 | const segmentDuration = duration / timescale;
|
6226 |
|
6227 | if (typeof endNumber === 'number') {
|
6228 | return {
|
6229 | start: 0,
|
6230 | end: endNumber
|
6231 | };
|
6232 | }
|
6233 |
|
6234 | if (typeof periodDuration === 'number') {
|
6235 | return {
|
6236 | start: 0,
|
6237 | end: periodDuration / segmentDuration
|
6238 | };
|
6239 | }
|
6240 |
|
6241 | return {
|
6242 | start: 0,
|
6243 | end: sourceDuration / segmentDuration
|
6244 | };
|
6245 | },
|
6246 |
|
6247 | |
6248 |
|
6249 |
|
6250 |
|
6251 |
|
6252 |
|
6253 |
|
6254 |
|
6255 | dynamic(attributes) {
|
6256 | const {
|
6257 | NOW,
|
6258 | clientOffset,
|
6259 | availabilityStartTime,
|
6260 | timescale = 1,
|
6261 | duration,
|
6262 | periodStart = 0,
|
6263 | minimumUpdatePeriod = 0,
|
6264 | timeShiftBufferDepth = Infinity
|
6265 | } = attributes;
|
6266 | const endNumber = parseEndNumber(attributes.endNumber);
|
6267 |
|
6268 |
|
6269 | const now = (NOW + clientOffset) / 1000;
|
6270 |
|
6271 |
|
6272 | const periodStartWC = availabilityStartTime + periodStart;
|
6273 |
|
6274 | const periodEndWC = now + minimumUpdatePeriod;
|
6275 | const periodDuration = periodEndWC - periodStartWC;
|
6276 | const segmentCount = Math.ceil(periodDuration * timescale / duration);
|
6277 | const availableStart = Math.floor((now - periodStartWC - timeShiftBufferDepth) * timescale / duration);
|
6278 | const availableEnd = Math.floor((now - periodStartWC) * timescale / duration);
|
6279 | return {
|
6280 | start: Math.max(0, availableStart),
|
6281 | end: typeof endNumber === 'number' ? endNumber : Math.min(segmentCount, availableEnd)
|
6282 | };
|
6283 | }
|
6284 |
|
6285 | };
|
6286 | |
6287 |
|
6288 |
|
6289 |
|
6290 |
|
6291 |
|
6292 |
|
6293 |
|
6294 |
|
6295 |
|
6296 |
|
6297 |
|
6298 |
|
6299 |
|
6300 | |
6301 |
|
6302 |
|
6303 |
|
6304 |
|
6305 |
|
6306 |
|
6307 |
|
6308 |
|
6309 |
|
6310 | const toSegments = attributes => number => {
|
6311 | const {
|
6312 | duration,
|
6313 | timescale = 1,
|
6314 | periodStart,
|
6315 | startNumber = 1
|
6316 | } = attributes;
|
6317 | return {
|
6318 | number: startNumber + number,
|
6319 | duration: duration / timescale,
|
6320 | timeline: periodStart,
|
6321 | time: number * duration
|
6322 | };
|
6323 | };
|
6324 | |
6325 |
|
6326 |
|
6327 |
|
6328 |
|
6329 |
|
6330 |
|
6331 |
|
6332 |
|
6333 |
|
6334 |
|
6335 |
|
6336 | const parseByDuration = attributes => {
|
6337 | const {
|
6338 | type,
|
6339 | duration,
|
6340 | timescale = 1,
|
6341 | periodDuration,
|
6342 | sourceDuration
|
6343 | } = attributes;
|
6344 | const {
|
6345 | start,
|
6346 | end
|
6347 | } = segmentRange[type](attributes);
|
6348 | const segments = range(start, end).map(toSegments(attributes));
|
6349 |
|
6350 | if (type === 'static') {
|
6351 | const index = segments.length - 1;
|
6352 |
|
6353 | const sectionDuration = typeof periodDuration === 'number' ? periodDuration : sourceDuration;
|
6354 |
|
6355 | segments[index].duration = sectionDuration - duration / timescale * index;
|
6356 | }
|
6357 |
|
6358 | return segments;
|
6359 | };
|
6360 | |
6361 |
|
6362 |
|
6363 |
|
6364 |
|
6365 |
|
6366 |
|
6367 |
|
6368 |
|
6369 |
|
6370 |
|
6371 |
|
6372 | const segmentsFromBase = attributes => {
|
6373 | const {
|
6374 | baseUrl,
|
6375 | initialization = {},
|
6376 | sourceDuration,
|
6377 | indexRange = '',
|
6378 | periodStart,
|
6379 | presentationTime,
|
6380 | number = 0,
|
6381 | duration
|
6382 | } = attributes;
|
6383 |
|
6384 | if (!baseUrl) {
|
6385 | throw new Error(errors.NO_BASE_URL);
|
6386 | }
|
6387 |
|
6388 | const initSegment = urlTypeToSegment({
|
6389 | baseUrl,
|
6390 | source: initialization.sourceURL,
|
6391 | range: initialization.range
|
6392 | });
|
6393 | const segment = urlTypeToSegment({
|
6394 | baseUrl,
|
6395 | source: baseUrl,
|
6396 | indexRange
|
6397 | });
|
6398 | segment.map = initSegment;
|
6399 |
|
6400 |
|
6401 | if (duration) {
|
6402 | const segmentTimeInfo = parseByDuration(attributes);
|
6403 |
|
6404 | if (segmentTimeInfo.length) {
|
6405 | segment.duration = segmentTimeInfo[0].duration;
|
6406 | segment.timeline = segmentTimeInfo[0].timeline;
|
6407 | }
|
6408 | } else if (sourceDuration) {
|
6409 | segment.duration = sourceDuration;
|
6410 | segment.timeline = periodStart;
|
6411 | }
|
6412 |
|
6413 |
|
6414 |
|
6415 |
|
6416 |
|
6417 | segment.presentationTime = presentationTime || periodStart;
|
6418 | segment.number = number;
|
6419 | return [segment];
|
6420 | };
|
6421 | |
6422 |
|
6423 |
|
6424 |
|
6425 |
|
6426 |
|
6427 |
|
6428 |
|
6429 |
|
6430 |
|
6431 |
|
6432 |
|
6433 |
|
6434 | const addSidxSegmentsToPlaylist$1 = (playlist, sidx, baseUrl) => {
|
6435 |
|
6436 | const initSegment = playlist.sidx.map ? playlist.sidx.map : null;
|
6437 |
|
6438 | const sourceDuration = playlist.sidx.duration;
|
6439 |
|
6440 | const timeline = playlist.timeline || 0;
|
6441 | const sidxByteRange = playlist.sidx.byterange;
|
6442 | const sidxEnd = sidxByteRange.offset + sidxByteRange.length;
|
6443 |
|
6444 | const timescale = sidx.timescale;
|
6445 |
|
6446 | const mediaReferences = sidx.references.filter(r => r.referenceType !== 1);
|
6447 | const segments = [];
|
6448 | const type = playlist.endList ? 'static' : 'dynamic';
|
6449 | const periodStart = playlist.sidx.timeline;
|
6450 | let presentationTime = periodStart;
|
6451 | let number = playlist.mediaSequence || 0;
|
6452 |
|
6453 | let startIndex;
|
6454 |
|
6455 | if (typeof sidx.firstOffset === 'bigint') {
|
6456 | startIndex = window.BigInt(sidxEnd) + sidx.firstOffset;
|
6457 | } else {
|
6458 | startIndex = sidxEnd + sidx.firstOffset;
|
6459 | }
|
6460 |
|
6461 | for (let i = 0; i < mediaReferences.length; i++) {
|
6462 | const reference = sidx.references[i];
|
6463 |
|
6464 | const size = reference.referencedSize;
|
6465 |
|
6466 |
|
6467 | const duration = reference.subsegmentDuration;
|
6468 |
|
6469 | let endIndex;
|
6470 |
|
6471 | if (typeof startIndex === 'bigint') {
|
6472 | endIndex = startIndex + window.BigInt(size) - window.BigInt(1);
|
6473 | } else {
|
6474 | endIndex = startIndex + size - 1;
|
6475 | }
|
6476 |
|
6477 | const indexRange = `${startIndex}-${endIndex}`;
|
6478 | const attributes = {
|
6479 | baseUrl,
|
6480 | timescale,
|
6481 | timeline,
|
6482 | periodStart,
|
6483 | presentationTime,
|
6484 | number,
|
6485 | duration,
|
6486 | sourceDuration,
|
6487 | indexRange,
|
6488 | type
|
6489 | };
|
6490 | const segment = segmentsFromBase(attributes)[0];
|
6491 |
|
6492 | if (initSegment) {
|
6493 | segment.map = initSegment;
|
6494 | }
|
6495 |
|
6496 | segments.push(segment);
|
6497 |
|
6498 | if (typeof startIndex === 'bigint') {
|
6499 | startIndex += window.BigInt(size);
|
6500 | } else {
|
6501 | startIndex += size;
|
6502 | }
|
6503 |
|
6504 | presentationTime += duration / timescale;
|
6505 | number++;
|
6506 | }
|
6507 |
|
6508 | playlist.segments = segments;
|
6509 | return playlist;
|
6510 | };
|
6511 |
|
6512 | const SUPPORTED_MEDIA_TYPES = ['AUDIO', 'SUBTITLES'];
|
6513 |
|
6514 | const TIME_FUDGE = 1 / 60;
|
6515 | |
6516 |
|
6517 |
|
6518 |
|
6519 |
|
6520 |
|
6521 |
|
6522 |
|
6523 | const getUniqueTimelineStarts = timelineStarts => {
|
6524 | return union(timelineStarts, ({
|
6525 | timeline
|
6526 | }) => timeline).sort((a, b) => a.timeline > b.timeline ? 1 : -1);
|
6527 | };
|
6528 | |
6529 |
|
6530 |
|
6531 |
|
6532 |
|
6533 |
|
6534 |
|
6535 |
|
6536 |
|
6537 |
|
6538 | const findPlaylistWithName = (playlists, name) => {
|
6539 | for (let i = 0; i < playlists.length; i++) {
|
6540 | if (playlists[i].attributes.NAME === name) {
|
6541 | return playlists[i];
|
6542 | }
|
6543 | }
|
6544 |
|
6545 | return null;
|
6546 | };
|
6547 | |
6548 |
|
6549 |
|
6550 |
|
6551 |
|
6552 |
|
6553 |
|
6554 |
|
6555 |
|
6556 | const getMediaGroupPlaylists = manifest => {
|
6557 | let mediaGroupPlaylists = [];
|
6558 | forEachMediaGroup(manifest, SUPPORTED_MEDIA_TYPES, (properties, type, group, label) => {
|
6559 | mediaGroupPlaylists = mediaGroupPlaylists.concat(properties.playlists || []);
|
6560 | });
|
6561 | return mediaGroupPlaylists;
|
6562 | };
|
6563 | |
6564 |
|
6565 |
|
6566 |
|
6567 |
|
6568 |
|
6569 |
|
6570 |
|
6571 |
|
6572 | const updateMediaSequenceForPlaylist = ({
|
6573 | playlist,
|
6574 | mediaSequence
|
6575 | }) => {
|
6576 | playlist.mediaSequence = mediaSequence;
|
6577 | playlist.segments.forEach((segment, index) => {
|
6578 | segment.number = playlist.mediaSequence + index;
|
6579 | });
|
6580 | };
|
6581 | |
6582 |
|
6583 |
|
6584 |
|
6585 |
|
6586 |
|
6587 |
|
6588 |
|
6589 |
|
6590 |
|
6591 |
|
6592 |
|
6593 |
|
6594 |
|
6595 |
|
6596 |
|
6597 | const updateSequenceNumbers = ({
|
6598 | oldPlaylists,
|
6599 | newPlaylists,
|
6600 | timelineStarts
|
6601 | }) => {
|
6602 | newPlaylists.forEach(playlist => {
|
6603 | playlist.discontinuitySequence = timelineStarts.findIndex(function ({
|
6604 | timeline
|
6605 | }) {
|
6606 | return timeline === playlist.timeline;
|
6607 | });
|
6608 |
|
6609 |
|
6610 |
|
6611 |
|
6612 | const oldPlaylist = findPlaylistWithName(oldPlaylists, playlist.attributes.NAME);
|
6613 |
|
6614 | if (!oldPlaylist) {
|
6615 |
|
6616 |
|
6617 | return;
|
6618 | }
|
6619 |
|
6620 |
|
6621 |
|
6622 |
|
6623 |
|
6624 |
|
6625 |
|
6626 |
|
6627 |
|
6628 |
|
6629 |
|
6630 | if (playlist.sidx) {
|
6631 | return;
|
6632 | }
|
6633 |
|
6634 |
|
6635 |
|
6636 | const firstNewSegment = playlist.segments[0];
|
6637 | const oldMatchingSegmentIndex = oldPlaylist.segments.findIndex(function (oldSegment) {
|
6638 | return Math.abs(oldSegment.presentationTime - firstNewSegment.presentationTime) < TIME_FUDGE;
|
6639 | });
|
6640 |
|
6641 |
|
6642 |
|
6643 |
|
6644 | if (oldMatchingSegmentIndex === -1) {
|
6645 | updateMediaSequenceForPlaylist({
|
6646 | playlist,
|
6647 | mediaSequence: oldPlaylist.mediaSequence + oldPlaylist.segments.length
|
6648 | });
|
6649 | playlist.segments[0].discontinuity = true;
|
6650 | playlist.discontinuityStarts.unshift(0);
|
6651 |
|
6652 |
|
6653 |
|
6654 |
|
6655 |
|
6656 |
|
6657 |
|
6658 |
|
6659 |
|
6660 |
|
6661 |
|
6662 |
|
6663 |
|
6664 |
|
6665 |
|
6666 |
|
6667 |
|
6668 |
|
6669 |
|
6670 | if (!oldPlaylist.segments.length && playlist.timeline > oldPlaylist.timeline || oldPlaylist.segments.length && playlist.timeline > oldPlaylist.segments[oldPlaylist.segments.length - 1].timeline) {
|
6671 | playlist.discontinuitySequence--;
|
6672 | }
|
6673 |
|
6674 | return;
|
6675 | }
|
6676 |
|
6677 |
|
6678 |
|
6679 |
|
6680 |
|
6681 |
|
6682 |
|
6683 |
|
6684 |
|
6685 |
|
6686 |
|
6687 |
|
6688 | const oldMatchingSegment = oldPlaylist.segments[oldMatchingSegmentIndex];
|
6689 |
|
6690 | if (oldMatchingSegment.discontinuity && !firstNewSegment.discontinuity) {
|
6691 | firstNewSegment.discontinuity = true;
|
6692 | playlist.discontinuityStarts.unshift(0);
|
6693 | playlist.discontinuitySequence--;
|
6694 | }
|
6695 |
|
6696 | updateMediaSequenceForPlaylist({
|
6697 | playlist,
|
6698 | mediaSequence: oldPlaylist.segments[oldMatchingSegmentIndex].number
|
6699 | });
|
6700 | });
|
6701 | };
|
6702 | |
6703 |
|
6704 |
|
6705 |
|
6706 |
|
6707 |
|
6708 |
|
6709 |
|
6710 |
|
6711 |
|
6712 |
|
6713 |
|
6714 | const positionManifestOnTimeline = ({
|
6715 | oldManifest,
|
6716 | newManifest
|
6717 | }) => {
|
6718 |
|
6719 |
|
6720 |
|
6721 |
|
6722 |
|
6723 |
|
6724 |
|
6725 |
|
6726 |
|
6727 |
|
6728 |
|
6729 |
|
6730 |
|
6731 |
|
6732 |
|
6733 |
|
6734 |
|
6735 |
|
6736 |
|
6737 | const oldPlaylists = oldManifest.playlists.concat(getMediaGroupPlaylists(oldManifest));
|
6738 | const newPlaylists = newManifest.playlists.concat(getMediaGroupPlaylists(newManifest));
|
6739 |
|
6740 |
|
6741 |
|
6742 |
|
6743 |
|
6744 |
|
6745 | newManifest.timelineStarts = getUniqueTimelineStarts([oldManifest.timelineStarts, newManifest.timelineStarts]);
|
6746 | updateSequenceNumbers({
|
6747 | oldPlaylists,
|
6748 | newPlaylists,
|
6749 | timelineStarts: newManifest.timelineStarts
|
6750 | });
|
6751 | return newManifest;
|
6752 | };
|
6753 |
|
6754 | const generateSidxKey = sidx => sidx && sidx.uri + '-' + byteRangeToString(sidx.byterange);
|
6755 |
|
6756 | const mergeDiscontiguousPlaylists = playlists => {
|
6757 |
|
6758 | const playlistsByBaseUrl = playlists.reduce(function (acc, cur) {
|
6759 | if (!acc[cur.attributes.baseUrl]) {
|
6760 | acc[cur.attributes.baseUrl] = [];
|
6761 | }
|
6762 |
|
6763 | acc[cur.attributes.baseUrl].push(cur);
|
6764 | return acc;
|
6765 | }, {});
|
6766 | let allPlaylists = [];
|
6767 | Object.values(playlistsByBaseUrl).forEach(playlistGroup => {
|
6768 | const mergedPlaylists = values(playlistGroup.reduce((acc, playlist) => {
|
6769 |
|
6770 |
|
6771 |
|
6772 | const name = playlist.attributes.id + (playlist.attributes.lang || '');
|
6773 |
|
6774 | if (!acc[name]) {
|
6775 |
|
6776 | acc[name] = playlist;
|
6777 | acc[name].attributes.timelineStarts = [];
|
6778 | } else {
|
6779 |
|
6780 | if (playlist.segments) {
|
6781 |
|
6782 | if (playlist.segments[0]) {
|
6783 | playlist.segments[0].discontinuity = true;
|
6784 | }
|
6785 |
|
6786 | acc[name].segments.push(...playlist.segments);
|
6787 | }
|
6788 |
|
6789 |
|
6790 |
|
6791 | if (playlist.attributes.contentProtection) {
|
6792 | acc[name].attributes.contentProtection = playlist.attributes.contentProtection;
|
6793 | }
|
6794 | }
|
6795 |
|
6796 | acc[name].attributes.timelineStarts.push({
|
6797 |
|
6798 |
|
6799 | start: playlist.attributes.periodStart,
|
6800 | timeline: playlist.attributes.periodStart
|
6801 | });
|
6802 | return acc;
|
6803 | }, {}));
|
6804 | allPlaylists = allPlaylists.concat(mergedPlaylists);
|
6805 | });
|
6806 | return allPlaylists.map(playlist => {
|
6807 | playlist.discontinuityStarts = findIndexes(playlist.segments || [], 'discontinuity');
|
6808 | return playlist;
|
6809 | });
|
6810 | };
|
6811 |
|
6812 | const addSidxSegmentsToPlaylist = (playlist, sidxMapping) => {
|
6813 | const sidxKey = generateSidxKey(playlist.sidx);
|
6814 | const sidxMatch = sidxKey && sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx;
|
6815 |
|
6816 | if (sidxMatch) {
|
6817 | addSidxSegmentsToPlaylist$1(playlist, sidxMatch, playlist.sidx.resolvedUri);
|
6818 | }
|
6819 |
|
6820 | return playlist;
|
6821 | };
|
6822 |
|
6823 | const addSidxSegmentsToPlaylists = (playlists, sidxMapping = {}) => {
|
6824 | if (!Object.keys(sidxMapping).length) {
|
6825 | return playlists;
|
6826 | }
|
6827 |
|
6828 | for (const i in playlists) {
|
6829 | playlists[i] = addSidxSegmentsToPlaylist(playlists[i], sidxMapping);
|
6830 | }
|
6831 |
|
6832 | return playlists;
|
6833 | };
|
6834 |
|
6835 | const formatAudioPlaylist = ({
|
6836 | attributes,
|
6837 | segments,
|
6838 | sidx,
|
6839 | mediaSequence,
|
6840 | discontinuitySequence,
|
6841 | discontinuityStarts
|
6842 | }, isAudioOnly) => {
|
6843 | const playlist = {
|
6844 | attributes: {
|
6845 | NAME: attributes.id,
|
6846 | BANDWIDTH: attributes.bandwidth,
|
6847 | CODECS: attributes.codecs,
|
6848 | ['PROGRAM-ID']: 1
|
6849 | },
|
6850 | uri: '',
|
6851 | endList: attributes.type === 'static',
|
6852 | timeline: attributes.periodStart,
|
6853 | resolvedUri: attributes.baseUrl || '',
|
6854 | targetDuration: attributes.duration,
|
6855 | discontinuitySequence,
|
6856 | discontinuityStarts,
|
6857 | timelineStarts: attributes.timelineStarts,
|
6858 | mediaSequence,
|
6859 | segments
|
6860 | };
|
6861 |
|
6862 | if (attributes.contentProtection) {
|
6863 | playlist.contentProtection = attributes.contentProtection;
|
6864 | }
|
6865 |
|
6866 | if (attributes.serviceLocation) {
|
6867 | playlist.attributes.serviceLocation = attributes.serviceLocation;
|
6868 | }
|
6869 |
|
6870 | if (sidx) {
|
6871 | playlist.sidx = sidx;
|
6872 | }
|
6873 |
|
6874 | if (isAudioOnly) {
|
6875 | playlist.attributes.AUDIO = 'audio';
|
6876 | playlist.attributes.SUBTITLES = 'subs';
|
6877 | }
|
6878 |
|
6879 | return playlist;
|
6880 | };
|
6881 |
|
6882 | const formatVttPlaylist = ({
|
6883 | attributes,
|
6884 | segments,
|
6885 | mediaSequence,
|
6886 | discontinuityStarts,
|
6887 | discontinuitySequence
|
6888 | }) => {
|
6889 | if (typeof segments === 'undefined') {
|
6890 |
|
6891 | segments = [{
|
6892 | uri: attributes.baseUrl,
|
6893 | timeline: attributes.periodStart,
|
6894 | resolvedUri: attributes.baseUrl || '',
|
6895 | duration: attributes.sourceDuration,
|
6896 | number: 0
|
6897 | }];
|
6898 |
|
6899 | attributes.duration = attributes.sourceDuration;
|
6900 | }
|
6901 |
|
6902 | const m3u8Attributes = {
|
6903 | NAME: attributes.id,
|
6904 | BANDWIDTH: attributes.bandwidth,
|
6905 | ['PROGRAM-ID']: 1
|
6906 | };
|
6907 |
|
6908 | if (attributes.codecs) {
|
6909 | m3u8Attributes.CODECS = attributes.codecs;
|
6910 | }
|
6911 |
|
6912 | const vttPlaylist = {
|
6913 | attributes: m3u8Attributes,
|
6914 | uri: '',
|
6915 | endList: attributes.type === 'static',
|
6916 | timeline: attributes.periodStart,
|
6917 | resolvedUri: attributes.baseUrl || '',
|
6918 | targetDuration: attributes.duration,
|
6919 | timelineStarts: attributes.timelineStarts,
|
6920 | discontinuityStarts,
|
6921 | discontinuitySequence,
|
6922 | mediaSequence,
|
6923 | segments
|
6924 | };
|
6925 |
|
6926 | if (attributes.serviceLocation) {
|
6927 | vttPlaylist.attributes.serviceLocation = attributes.serviceLocation;
|
6928 | }
|
6929 |
|
6930 | return vttPlaylist;
|
6931 | };
|
6932 |
|
6933 | const organizeAudioPlaylists = (playlists, sidxMapping = {}, isAudioOnly = false) => {
|
6934 | let mainPlaylist;
|
6935 | const formattedPlaylists = playlists.reduce((a, playlist) => {
|
6936 | const role = playlist.attributes.role && playlist.attributes.role.value || '';
|
6937 | const language = playlist.attributes.lang || '';
|
6938 | let label = playlist.attributes.label || 'main';
|
6939 |
|
6940 | if (language && !playlist.attributes.label) {
|
6941 | const roleLabel = role ? ` (${role})` : '';
|
6942 | label = `${playlist.attributes.lang}${roleLabel}`;
|
6943 | }
|
6944 |
|
6945 | if (!a[label]) {
|
6946 | a[label] = {
|
6947 | language,
|
6948 | autoselect: true,
|
6949 | default: role === 'main',
|
6950 | playlists: [],
|
6951 | uri: ''
|
6952 | };
|
6953 | }
|
6954 |
|
6955 | const formatted = addSidxSegmentsToPlaylist(formatAudioPlaylist(playlist, isAudioOnly), sidxMapping);
|
6956 | a[label].playlists.push(formatted);
|
6957 |
|
6958 | if (typeof mainPlaylist === 'undefined' && role === 'main') {
|
6959 | mainPlaylist = playlist;
|
6960 | mainPlaylist.default = true;
|
6961 | }
|
6962 |
|
6963 | return a;
|
6964 | }, {});
|
6965 |
|
6966 | if (!mainPlaylist) {
|
6967 | const firstLabel = Object.keys(formattedPlaylists)[0];
|
6968 | formattedPlaylists[firstLabel].default = true;
|
6969 | }
|
6970 |
|
6971 | return formattedPlaylists;
|
6972 | };
|
6973 |
|
6974 | const organizeVttPlaylists = (playlists, sidxMapping = {}) => {
|
6975 | return playlists.reduce((a, playlist) => {
|
6976 | const label = playlist.attributes.label || playlist.attributes.lang || 'text';
|
6977 |
|
6978 | if (!a[label]) {
|
6979 | a[label] = {
|
6980 | language: label,
|
6981 | default: false,
|
6982 | autoselect: false,
|
6983 | playlists: [],
|
6984 | uri: ''
|
6985 | };
|
6986 | }
|
6987 |
|
6988 | a[label].playlists.push(addSidxSegmentsToPlaylist(formatVttPlaylist(playlist), sidxMapping));
|
6989 | return a;
|
6990 | }, {});
|
6991 | };
|
6992 |
|
6993 | const organizeCaptionServices = captionServices => captionServices.reduce((svcObj, svc) => {
|
6994 | if (!svc) {
|
6995 | return svcObj;
|
6996 | }
|
6997 |
|
6998 | svc.forEach(service => {
|
6999 | const {
|
7000 | channel,
|
7001 | language
|
7002 | } = service;
|
7003 | svcObj[language] = {
|
7004 | autoselect: false,
|
7005 | default: false,
|
7006 | instreamId: channel,
|
7007 | language
|
7008 | };
|
7009 |
|
7010 | if (service.hasOwnProperty('aspectRatio')) {
|
7011 | svcObj[language].aspectRatio = service.aspectRatio;
|
7012 | }
|
7013 |
|
7014 | if (service.hasOwnProperty('easyReader')) {
|
7015 | svcObj[language].easyReader = service.easyReader;
|
7016 | }
|
7017 |
|
7018 | if (service.hasOwnProperty('3D')) {
|
7019 | svcObj[language]['3D'] = service['3D'];
|
7020 | }
|
7021 | });
|
7022 | return svcObj;
|
7023 | }, {});
|
7024 |
|
7025 | const formatVideoPlaylist = ({
|
7026 | attributes,
|
7027 | segments,
|
7028 | sidx,
|
7029 | discontinuityStarts
|
7030 | }) => {
|
7031 | const playlist = {
|
7032 | attributes: {
|
7033 | NAME: attributes.id,
|
7034 | AUDIO: 'audio',
|
7035 | SUBTITLES: 'subs',
|
7036 | RESOLUTION: {
|
7037 | width: attributes.width,
|
7038 | height: attributes.height
|
7039 | },
|
7040 | CODECS: attributes.codecs,
|
7041 | BANDWIDTH: attributes.bandwidth,
|
7042 | ['PROGRAM-ID']: 1
|
7043 | },
|
7044 | uri: '',
|
7045 | endList: attributes.type === 'static',
|
7046 | timeline: attributes.periodStart,
|
7047 | resolvedUri: attributes.baseUrl || '',
|
7048 | targetDuration: attributes.duration,
|
7049 | discontinuityStarts,
|
7050 | timelineStarts: attributes.timelineStarts,
|
7051 | segments
|
7052 | };
|
7053 |
|
7054 | if (attributes.frameRate) {
|
7055 | playlist.attributes['FRAME-RATE'] = attributes.frameRate;
|
7056 | }
|
7057 |
|
7058 | if (attributes.contentProtection) {
|
7059 | playlist.contentProtection = attributes.contentProtection;
|
7060 | }
|
7061 |
|
7062 | if (attributes.serviceLocation) {
|
7063 | playlist.attributes.serviceLocation = attributes.serviceLocation;
|
7064 | }
|
7065 |
|
7066 | if (sidx) {
|
7067 | playlist.sidx = sidx;
|
7068 | }
|
7069 |
|
7070 | return playlist;
|
7071 | };
|
7072 |
|
7073 | const videoOnly = ({
|
7074 | attributes
|
7075 | }) => attributes.mimeType === 'video/mp4' || attributes.mimeType === 'video/webm' || attributes.contentType === 'video';
|
7076 |
|
7077 | const audioOnly = ({
|
7078 | attributes
|
7079 | }) => attributes.mimeType === 'audio/mp4' || attributes.mimeType === 'audio/webm' || attributes.contentType === 'audio';
|
7080 |
|
7081 | const vttOnly = ({
|
7082 | attributes
|
7083 | }) => attributes.mimeType === 'text/vtt' || attributes.contentType === 'text';
|
7084 | |
7085 |
|
7086 |
|
7087 |
|
7088 |
|
7089 |
|
7090 |
|
7091 |
|
7092 |
|
7093 | |
7094 |
|
7095 |
|
7096 |
|
7097 |
|
7098 |
|
7099 |
|
7100 |
|
7101 |
|
7102 |
|
7103 |
|
7104 |
|
7105 |
|
7106 |
|
7107 |
|
7108 |
|
7109 |
|
7110 |
|
7111 |
|
7112 |
|
7113 |
|
7114 |
|
7115 |
|
7116 | const addMediaSequenceValues = (playlists, timelineStarts) => {
|
7117 |
|
7118 | playlists.forEach(playlist => {
|
7119 | playlist.mediaSequence = 0;
|
7120 | playlist.discontinuitySequence = timelineStarts.findIndex(function ({
|
7121 | timeline
|
7122 | }) {
|
7123 | return timeline === playlist.timeline;
|
7124 | });
|
7125 |
|
7126 | if (!playlist.segments) {
|
7127 | return;
|
7128 | }
|
7129 |
|
7130 | playlist.segments.forEach((segment, index) => {
|
7131 | segment.number = index;
|
7132 | });
|
7133 | });
|
7134 | };
|
7135 | |
7136 |
|
7137 |
|
7138 |
|
7139 |
|
7140 |
|
7141 |
|
7142 |
|
7143 |
|
7144 |
|
7145 |
|
7146 | const flattenMediaGroupPlaylists = mediaGroupObject => {
|
7147 | if (!mediaGroupObject) {
|
7148 | return [];
|
7149 | }
|
7150 |
|
7151 | return Object.keys(mediaGroupObject).reduce((acc, label) => {
|
7152 | const labelContents = mediaGroupObject[label];
|
7153 | return acc.concat(labelContents.playlists);
|
7154 | }, []);
|
7155 | };
|
7156 |
|
7157 | const toM3u8 = ({
|
7158 | dashPlaylists,
|
7159 | locations,
|
7160 | contentSteering,
|
7161 | sidxMapping = {},
|
7162 | previousManifest,
|
7163 | eventStream
|
7164 | }) => {
|
7165 | if (!dashPlaylists.length) {
|
7166 | return {};
|
7167 | }
|
7168 |
|
7169 |
|
7170 | const {
|
7171 | sourceDuration: duration,
|
7172 | type,
|
7173 | suggestedPresentationDelay,
|
7174 | minimumUpdatePeriod
|
7175 | } = dashPlaylists[0].attributes;
|
7176 | const videoPlaylists = mergeDiscontiguousPlaylists(dashPlaylists.filter(videoOnly)).map(formatVideoPlaylist);
|
7177 | const audioPlaylists = mergeDiscontiguousPlaylists(dashPlaylists.filter(audioOnly));
|
7178 | const vttPlaylists = mergeDiscontiguousPlaylists(dashPlaylists.filter(vttOnly));
|
7179 | const captions = dashPlaylists.map(playlist => playlist.attributes.captionServices).filter(Boolean);
|
7180 | const manifest = {
|
7181 | allowCache: true,
|
7182 | discontinuityStarts: [],
|
7183 | segments: [],
|
7184 | endList: true,
|
7185 | mediaGroups: {
|
7186 | AUDIO: {},
|
7187 | VIDEO: {},
|
7188 | ['CLOSED-CAPTIONS']: {},
|
7189 | SUBTITLES: {}
|
7190 | },
|
7191 | uri: '',
|
7192 | duration,
|
7193 | playlists: addSidxSegmentsToPlaylists(videoPlaylists, sidxMapping)
|
7194 | };
|
7195 |
|
7196 | if (minimumUpdatePeriod >= 0) {
|
7197 | manifest.minimumUpdatePeriod = minimumUpdatePeriod * 1000;
|
7198 | }
|
7199 |
|
7200 | if (locations) {
|
7201 | manifest.locations = locations;
|
7202 | }
|
7203 |
|
7204 | if (contentSteering) {
|
7205 | manifest.contentSteering = contentSteering;
|
7206 | }
|
7207 |
|
7208 | if (type === 'dynamic') {
|
7209 | manifest.suggestedPresentationDelay = suggestedPresentationDelay;
|
7210 | }
|
7211 |
|
7212 | if (eventStream && eventStream.length > 0) {
|
7213 | manifest.eventStream = eventStream;
|
7214 | }
|
7215 |
|
7216 | const isAudioOnly = manifest.playlists.length === 0;
|
7217 | const organizedAudioGroup = audioPlaylists.length ? organizeAudioPlaylists(audioPlaylists, sidxMapping, isAudioOnly) : null;
|
7218 | const organizedVttGroup = vttPlaylists.length ? organizeVttPlaylists(vttPlaylists, sidxMapping) : null;
|
7219 | const formattedPlaylists = videoPlaylists.concat(flattenMediaGroupPlaylists(organizedAudioGroup), flattenMediaGroupPlaylists(organizedVttGroup));
|
7220 | const playlistTimelineStarts = formattedPlaylists.map(({
|
7221 | timelineStarts
|
7222 | }) => timelineStarts);
|
7223 | manifest.timelineStarts = getUniqueTimelineStarts(playlistTimelineStarts);
|
7224 | addMediaSequenceValues(formattedPlaylists, manifest.timelineStarts);
|
7225 |
|
7226 | if (organizedAudioGroup) {
|
7227 | manifest.mediaGroups.AUDIO.audio = organizedAudioGroup;
|
7228 | }
|
7229 |
|
7230 | if (organizedVttGroup) {
|
7231 | manifest.mediaGroups.SUBTITLES.subs = organizedVttGroup;
|
7232 | }
|
7233 |
|
7234 | if (captions.length) {
|
7235 | manifest.mediaGroups['CLOSED-CAPTIONS'].cc = organizeCaptionServices(captions);
|
7236 | }
|
7237 |
|
7238 | if (previousManifest) {
|
7239 | return positionManifestOnTimeline({
|
7240 | oldManifest: previousManifest,
|
7241 | newManifest: manifest
|
7242 | });
|
7243 | }
|
7244 |
|
7245 | return manifest;
|
7246 | };
|
7247 | |
7248 |
|
7249 |
|
7250 |
|
7251 |
|
7252 |
|
7253 |
|
7254 |
|
7255 |
|
7256 |
|
7257 |
|
7258 |
|
7259 |
|
7260 |
|
7261 |
|
7262 |
|
7263 |
|
7264 | const getLiveRValue = (attributes, time, duration) => {
|
7265 | const {
|
7266 | NOW,
|
7267 | clientOffset,
|
7268 | availabilityStartTime,
|
7269 | timescale = 1,
|
7270 | periodStart = 0,
|
7271 | minimumUpdatePeriod = 0
|
7272 | } = attributes;
|
7273 | const now = (NOW + clientOffset) / 1000;
|
7274 | const periodStartWC = availabilityStartTime + periodStart;
|
7275 | const periodEndWC = now + minimumUpdatePeriod;
|
7276 | const periodDuration = periodEndWC - periodStartWC;
|
7277 | return Math.ceil((periodDuration * timescale - time) / duration);
|
7278 | };
|
7279 | |
7280 |
|
7281 |
|
7282 |
|
7283 |
|
7284 |
|
7285 |
|
7286 |
|
7287 |
|
7288 |
|
7289 |
|
7290 |
|
7291 |
|
7292 |
|
7293 |
|
7294 | const parseByTimeline = (attributes, segmentTimeline) => {
|
7295 | const {
|
7296 | type,
|
7297 | minimumUpdatePeriod = 0,
|
7298 | media = '',
|
7299 | sourceDuration,
|
7300 | timescale = 1,
|
7301 | startNumber = 1,
|
7302 | periodStart: timeline
|
7303 | } = attributes;
|
7304 | const segments = [];
|
7305 | let time = -1;
|
7306 |
|
7307 | for (let sIndex = 0; sIndex < segmentTimeline.length; sIndex++) {
|
7308 | const S = segmentTimeline[sIndex];
|
7309 | const duration = S.d;
|
7310 | const repeat = S.r || 0;
|
7311 | const segmentTime = S.t || 0;
|
7312 |
|
7313 | if (time < 0) {
|
7314 |
|
7315 | time = segmentTime;
|
7316 | }
|
7317 |
|
7318 | if (segmentTime && segmentTime > time) {
|
7319 |
|
7320 |
|
7321 |
|
7322 |
|
7323 |
|
7324 |
|
7325 |
|
7326 |
|
7327 |
|
7328 |
|
7329 |
|
7330 |
|
7331 |
|
7332 |
|
7333 |
|
7334 |
|
7335 |
|
7336 |
|
7337 |
|
7338 |
|
7339 | time = segmentTime;
|
7340 | }
|
7341 |
|
7342 | let count;
|
7343 |
|
7344 | if (repeat < 0) {
|
7345 | const nextS = sIndex + 1;
|
7346 |
|
7347 | if (nextS === segmentTimeline.length) {
|
7348 |
|
7349 | if (type === 'dynamic' && minimumUpdatePeriod > 0 && media.indexOf('$Number$') > 0) {
|
7350 | count = getLiveRValue(attributes, time, duration);
|
7351 | } else {
|
7352 |
|
7353 | count = (sourceDuration * timescale - time) / duration;
|
7354 | }
|
7355 | } else {
|
7356 | count = (segmentTimeline[nextS].t - time) / duration;
|
7357 | }
|
7358 | } else {
|
7359 | count = repeat + 1;
|
7360 | }
|
7361 |
|
7362 | const end = startNumber + segments.length + count;
|
7363 | let number = startNumber + segments.length;
|
7364 |
|
7365 | while (number < end) {
|
7366 | segments.push({
|
7367 | number,
|
7368 | duration: duration / timescale,
|
7369 | time,
|
7370 | timeline
|
7371 | });
|
7372 | time += duration;
|
7373 | number++;
|
7374 | }
|
7375 | }
|
7376 |
|
7377 | return segments;
|
7378 | };
|
7379 |
|
7380 | const identifierPattern = /\$([A-z]*)(?:(%0)([0-9]+)d)?\$/g;
|
7381 | |
7382 |
|
7383 |
|
7384 |
|
7385 |
|
7386 |
|
7387 |
|
7388 |
|
7389 |
|
7390 |
|
7391 |
|
7392 |
|
7393 |
|
7394 |
|
7395 |
|
7396 |
|
7397 |
|
7398 |
|
7399 |
|
7400 | |
7401 |
|
7402 |
|
7403 |
|
7404 |
|
7405 |
|
7406 |
|
7407 |
|
7408 |
|
7409 |
|
7410 |
|
7411 |
|
7412 |
|
7413 |
|
7414 |
|
7415 |
|
7416 |
|
7417 |
|
7418 | const identifierReplacement = values => (match, identifier, format, width) => {
|
7419 | if (match === '$$') {
|
7420 |
|
7421 | return '$';
|
7422 | }
|
7423 |
|
7424 | if (typeof values[identifier] === 'undefined') {
|
7425 | return match;
|
7426 | }
|
7427 |
|
7428 | const value = '' + values[identifier];
|
7429 |
|
7430 | if (identifier === 'RepresentationID') {
|
7431 |
|
7432 | return value;
|
7433 | }
|
7434 |
|
7435 | if (!format) {
|
7436 | width = 1;
|
7437 | } else {
|
7438 | width = parseInt(width, 10);
|
7439 | }
|
7440 |
|
7441 | if (value.length >= width) {
|
7442 | return value;
|
7443 | }
|
7444 |
|
7445 | return `${new Array(width - value.length + 1).join('0')}${value}`;
|
7446 | };
|
7447 | |
7448 |
|
7449 |
|
7450 |
|
7451 |
|
7452 |
|
7453 |
|
7454 |
|
7455 |
|
7456 |
|
7457 |
|
7458 |
|
7459 |
|
7460 |
|
7461 |
|
7462 |
|
7463 |
|
7464 |
|
7465 |
|
7466 |
|
7467 | const constructTemplateUrl = (url, values) => url.replace(identifierPattern, identifierReplacement(values));
|
7468 | |
7469 |
|
7470 |
|
7471 |
|
7472 |
|
7473 |
|
7474 |
|
7475 |
|
7476 |
|
7477 |
|
7478 |
|
7479 |
|
7480 |
|
7481 |
|
7482 |
|
7483 | const parseTemplateInfo = (attributes, segmentTimeline) => {
|
7484 | if (!attributes.duration && !segmentTimeline) {
|
7485 |
|
7486 |
|
7487 | return [{
|
7488 | number: attributes.startNumber || 1,
|
7489 | duration: attributes.sourceDuration,
|
7490 | time: 0,
|
7491 | timeline: attributes.periodStart
|
7492 | }];
|
7493 | }
|
7494 |
|
7495 | if (attributes.duration) {
|
7496 | return parseByDuration(attributes);
|
7497 | }
|
7498 |
|
7499 | return parseByTimeline(attributes, segmentTimeline);
|
7500 | };
|
7501 | |
7502 |
|
7503 |
|
7504 |
|
7505 |
|
7506 |
|
7507 |
|
7508 |
|
7509 |
|
7510 |
|
7511 |
|
7512 |
|
7513 |
|
7514 |
|
7515 | const segmentsFromTemplate = (attributes, segmentTimeline) => {
|
7516 | const templateValues = {
|
7517 | RepresentationID: attributes.id,
|
7518 | Bandwidth: attributes.bandwidth || 0
|
7519 | };
|
7520 | const {
|
7521 | initialization = {
|
7522 | sourceURL: '',
|
7523 | range: ''
|
7524 | }
|
7525 | } = attributes;
|
7526 | const mapSegment = urlTypeToSegment({
|
7527 | baseUrl: attributes.baseUrl,
|
7528 | source: constructTemplateUrl(initialization.sourceURL, templateValues),
|
7529 | range: initialization.range
|
7530 | });
|
7531 | const segments = parseTemplateInfo(attributes, segmentTimeline);
|
7532 | return segments.map(segment => {
|
7533 | templateValues.Number = segment.number;
|
7534 | templateValues.Time = segment.time;
|
7535 | const uri = constructTemplateUrl(attributes.media || '', templateValues);
|
7536 |
|
7537 |
|
7538 | const timescale = attributes.timescale || 1;
|
7539 |
|
7540 | const presentationTimeOffset = attributes.presentationTimeOffset || 0;
|
7541 | const presentationTime =
|
7542 |
|
7543 | attributes.periodStart + (segment.time - presentationTimeOffset) / timescale;
|
7544 | const map = {
|
7545 | uri,
|
7546 | timeline: segment.timeline,
|
7547 | duration: segment.duration,
|
7548 | resolvedUri: resolveUrl$1(attributes.baseUrl || '', uri),
|
7549 | map: mapSegment,
|
7550 | number: segment.number,
|
7551 | presentationTime
|
7552 | };
|
7553 | return map;
|
7554 | });
|
7555 | };
|
7556 | |
7557 |
|
7558 |
|
7559 |
|
7560 |
|
7561 |
|
7562 |
|
7563 |
|
7564 |
|
7565 |
|
7566 |
|
7567 |
|
7568 |
|
7569 | const SegmentURLToSegmentObject = (attributes, segmentUrl) => {
|
7570 | const {
|
7571 | baseUrl,
|
7572 | initialization = {}
|
7573 | } = attributes;
|
7574 | const initSegment = urlTypeToSegment({
|
7575 | baseUrl,
|
7576 | source: initialization.sourceURL,
|
7577 | range: initialization.range
|
7578 | });
|
7579 | const segment = urlTypeToSegment({
|
7580 | baseUrl,
|
7581 | source: segmentUrl.media,
|
7582 | range: segmentUrl.mediaRange
|
7583 | });
|
7584 | segment.map = initSegment;
|
7585 | return segment;
|
7586 | };
|
7587 | |
7588 |
|
7589 |
|
7590 |
|
7591 |
|
7592 |
|
7593 |
|
7594 |
|
7595 |
|
7596 |
|
7597 |
|
7598 |
|
7599 |
|
7600 |
|
7601 |
|
7602 | const segmentsFromList = (attributes, segmentTimeline) => {
|
7603 | const {
|
7604 | duration,
|
7605 | segmentUrls = [],
|
7606 | periodStart
|
7607 | } = attributes;
|
7608 |
|
7609 |
|
7610 | if (!duration && !segmentTimeline || duration && segmentTimeline) {
|
7611 | throw new Error(errors.SEGMENT_TIME_UNSPECIFIED);
|
7612 | }
|
7613 |
|
7614 | const segmentUrlMap = segmentUrls.map(segmentUrlObject => SegmentURLToSegmentObject(attributes, segmentUrlObject));
|
7615 | let segmentTimeInfo;
|
7616 |
|
7617 | if (duration) {
|
7618 | segmentTimeInfo = parseByDuration(attributes);
|
7619 | }
|
7620 |
|
7621 | if (segmentTimeline) {
|
7622 | segmentTimeInfo = parseByTimeline(attributes, segmentTimeline);
|
7623 | }
|
7624 |
|
7625 | const segments = segmentTimeInfo.map((segmentTime, index) => {
|
7626 | if (segmentUrlMap[index]) {
|
7627 | const segment = segmentUrlMap[index];
|
7628 |
|
7629 |
|
7630 | const timescale = attributes.timescale || 1;
|
7631 |
|
7632 | const presentationTimeOffset = attributes.presentationTimeOffset || 0;
|
7633 | segment.timeline = segmentTime.timeline;
|
7634 | segment.duration = segmentTime.duration;
|
7635 | segment.number = segmentTime.number;
|
7636 | segment.presentationTime = periodStart + (segmentTime.time - presentationTimeOffset) / timescale;
|
7637 | return segment;
|
7638 | }
|
7639 |
|
7640 |
|
7641 |
|
7642 | }).filter(segment => segment);
|
7643 | return segments;
|
7644 | };
|
7645 |
|
7646 | const generateSegments = ({
|
7647 | attributes,
|
7648 | segmentInfo
|
7649 | }) => {
|
7650 | let segmentAttributes;
|
7651 | let segmentsFn;
|
7652 |
|
7653 | if (segmentInfo.template) {
|
7654 | segmentsFn = segmentsFromTemplate;
|
7655 | segmentAttributes = merge(attributes, segmentInfo.template);
|
7656 | } else if (segmentInfo.base) {
|
7657 | segmentsFn = segmentsFromBase;
|
7658 | segmentAttributes = merge(attributes, segmentInfo.base);
|
7659 | } else if (segmentInfo.list) {
|
7660 | segmentsFn = segmentsFromList;
|
7661 | segmentAttributes = merge(attributes, segmentInfo.list);
|
7662 | }
|
7663 |
|
7664 | const segmentsInfo = {
|
7665 | attributes
|
7666 | };
|
7667 |
|
7668 | if (!segmentsFn) {
|
7669 | return segmentsInfo;
|
7670 | }
|
7671 |
|
7672 | const segments = segmentsFn(segmentAttributes, segmentInfo.segmentTimeline);
|
7673 |
|
7674 |
|
7675 |
|
7676 | if (segmentAttributes.duration) {
|
7677 | const {
|
7678 | duration,
|
7679 | timescale = 1
|
7680 | } = segmentAttributes;
|
7681 | segmentAttributes.duration = duration / timescale;
|
7682 | } else if (segments.length) {
|
7683 |
|
7684 |
|
7685 | segmentAttributes.duration = segments.reduce((max, segment) => {
|
7686 | return Math.max(max, Math.ceil(segment.duration));
|
7687 | }, 0);
|
7688 | } else {
|
7689 | segmentAttributes.duration = 0;
|
7690 | }
|
7691 |
|
7692 | segmentsInfo.attributes = segmentAttributes;
|
7693 | segmentsInfo.segments = segments;
|
7694 |
|
7695 | if (segmentInfo.base && segmentAttributes.indexRange) {
|
7696 | segmentsInfo.sidx = segments[0];
|
7697 | segmentsInfo.segments = [];
|
7698 | }
|
7699 |
|
7700 | return segmentsInfo;
|
7701 | };
|
7702 |
|
7703 | const toPlaylists = representations => representations.map(generateSegments);
|
7704 |
|
7705 | const findChildren = (element, name) => from(element.childNodes).filter(({
|
7706 | tagName
|
7707 | }) => tagName === name);
|
7708 |
|
7709 | const getContent = element => element.textContent.trim();
|
7710 | |
7711 |
|
7712 |
|
7713 |
|
7714 |
|
7715 |
|
7716 |
|
7717 |
|
7718 |
|
7719 | const parseDivisionValue = value => {
|
7720 | return parseFloat(value.split('/').reduce((prev, current) => prev / current));
|
7721 | };
|
7722 |
|
7723 | const parseDuration = str => {
|
7724 | const SECONDS_IN_YEAR = 365 * 24 * 60 * 60;
|
7725 | const SECONDS_IN_MONTH = 30 * 24 * 60 * 60;
|
7726 | const SECONDS_IN_DAY = 24 * 60 * 60;
|
7727 | const SECONDS_IN_HOUR = 60 * 60;
|
7728 | const SECONDS_IN_MIN = 60;
|
7729 |
|
7730 | const durationRegex = /P(?:(\d*)Y)?(?:(\d*)M)?(?:(\d*)D)?(?:T(?:(\d*)H)?(?:(\d*)M)?(?:([\d.]*)S)?)?/;
|
7731 | const match = durationRegex.exec(str);
|
7732 |
|
7733 | if (!match) {
|
7734 | return 0;
|
7735 | }
|
7736 |
|
7737 | const [year, month, day, hour, minute, second] = match.slice(1);
|
7738 | return parseFloat(year || 0) * SECONDS_IN_YEAR + parseFloat(month || 0) * SECONDS_IN_MONTH + parseFloat(day || 0) * SECONDS_IN_DAY + parseFloat(hour || 0) * SECONDS_IN_HOUR + parseFloat(minute || 0) * SECONDS_IN_MIN + parseFloat(second || 0);
|
7739 | };
|
7740 |
|
7741 | const parseDate = str => {
|
7742 |
|
7743 |
|
7744 | const dateRegex = /^\d+-\d+-\d+T\d+:\d+:\d+(\.\d+)?$/;
|
7745 |
|
7746 |
|
7747 | if (dateRegex.test(str)) {
|
7748 | str += 'Z';
|
7749 | }
|
7750 |
|
7751 | return Date.parse(str);
|
7752 | };
|
7753 |
|
7754 | const parsers = {
|
7755 | |
7756 |
|
7757 |
|
7758 |
|
7759 |
|
7760 |
|
7761 |
|
7762 |
|
7763 |
|
7764 | mediaPresentationDuration(value) {
|
7765 | return parseDuration(value);
|
7766 | },
|
7767 |
|
7768 | |
7769 |
|
7770 |
|
7771 |
|
7772 |
|
7773 |
|
7774 |
|
7775 |
|
7776 |
|
7777 |
|
7778 | availabilityStartTime(value) {
|
7779 | return parseDate(value) / 1000;
|
7780 | },
|
7781 |
|
7782 | |
7783 |
|
7784 |
|
7785 |
|
7786 |
|
7787 |
|
7788 |
|
7789 |
|
7790 |
|
7791 | minimumUpdatePeriod(value) {
|
7792 | return parseDuration(value);
|
7793 | },
|
7794 |
|
7795 | |
7796 |
|
7797 |
|
7798 |
|
7799 |
|
7800 |
|
7801 |
|
7802 |
|
7803 |
|
7804 | suggestedPresentationDelay(value) {
|
7805 | return parseDuration(value);
|
7806 | },
|
7807 |
|
7808 | |
7809 |
|
7810 |
|
7811 |
|
7812 |
|
7813 |
|
7814 |
|
7815 |
|
7816 |
|
7817 | type(value) {
|
7818 | return value;
|
7819 | },
|
7820 |
|
7821 | |
7822 |
|
7823 |
|
7824 |
|
7825 |
|
7826 |
|
7827 |
|
7828 |
|
7829 |
|
7830 | timeShiftBufferDepth(value) {
|
7831 | return parseDuration(value);
|
7832 | },
|
7833 |
|
7834 | |
7835 |
|
7836 |
|
7837 |
|
7838 |
|
7839 |
|
7840 |
|
7841 |
|
7842 |
|
7843 | start(value) {
|
7844 | return parseDuration(value);
|
7845 | },
|
7846 |
|
7847 | |
7848 |
|
7849 |
|
7850 |
|
7851 |
|
7852 |
|
7853 |
|
7854 |
|
7855 | width(value) {
|
7856 | return parseInt(value, 10);
|
7857 | },
|
7858 |
|
7859 | |
7860 |
|
7861 |
|
7862 |
|
7863 |
|
7864 |
|
7865 |
|
7866 |
|
7867 | height(value) {
|
7868 | return parseInt(value, 10);
|
7869 | },
|
7870 |
|
7871 | |
7872 |
|
7873 |
|
7874 |
|
7875 |
|
7876 |
|
7877 |
|
7878 |
|
7879 | bandwidth(value) {
|
7880 | return parseInt(value, 10);
|
7881 | },
|
7882 |
|
7883 | |
7884 |
|
7885 |
|
7886 |
|
7887 |
|
7888 |
|
7889 |
|
7890 |
|
7891 | frameRate(value) {
|
7892 | return parseDivisionValue(value);
|
7893 | },
|
7894 |
|
7895 | |
7896 |
|
7897 |
|
7898 |
|
7899 |
|
7900 |
|
7901 |
|
7902 |
|
7903 | startNumber(value) {
|
7904 | return parseInt(value, 10);
|
7905 | },
|
7906 |
|
7907 | |
7908 |
|
7909 |
|
7910 |
|
7911 |
|
7912 |
|
7913 |
|
7914 |
|
7915 | timescale(value) {
|
7916 | return parseInt(value, 10);
|
7917 | },
|
7918 |
|
7919 | |
7920 |
|
7921 |
|
7922 |
|
7923 |
|
7924 |
|
7925 |
|
7926 |
|
7927 |
|
7928 | presentationTimeOffset(value) {
|
7929 | return parseInt(value, 10);
|
7930 | },
|
7931 |
|
7932 | |
7933 |
|
7934 |
|
7935 |
|
7936 |
|
7937 |
|
7938 |
|
7939 |
|
7940 |
|
7941 |
|
7942 |
|
7943 |
|
7944 | duration(value) {
|
7945 | const parsedValue = parseInt(value, 10);
|
7946 |
|
7947 | if (isNaN(parsedValue)) {
|
7948 | return parseDuration(value);
|
7949 | }
|
7950 |
|
7951 | return parsedValue;
|
7952 | },
|
7953 |
|
7954 | |
7955 |
|
7956 |
|
7957 |
|
7958 |
|
7959 |
|
7960 |
|
7961 |
|
7962 | d(value) {
|
7963 | return parseInt(value, 10);
|
7964 | },
|
7965 |
|
7966 | |
7967 |
|
7968 |
|
7969 |
|
7970 |
|
7971 |
|
7972 |
|
7973 |
|
7974 |
|
7975 | t(value) {
|
7976 | return parseInt(value, 10);
|
7977 | },
|
7978 |
|
7979 | |
7980 |
|
7981 |
|
7982 |
|
7983 |
|
7984 |
|
7985 |
|
7986 |
|
7987 |
|
7988 | r(value) {
|
7989 | return parseInt(value, 10);
|
7990 | },
|
7991 |
|
7992 | |
7993 |
|
7994 |
|
7995 |
|
7996 |
|
7997 |
|
7998 |
|
7999 |
|
8000 |
|
8001 | presentationTime(value) {
|
8002 | return parseInt(value, 10);
|
8003 | },
|
8004 |
|
8005 | |
8006 |
|
8007 |
|
8008 |
|
8009 |
|
8010 |
|
8011 |
|
8012 |
|
8013 |
|
8014 | DEFAULT(value) {
|
8015 | return value;
|
8016 | }
|
8017 |
|
8018 | };
|
8019 | |
8020 |
|
8021 |
|
8022 |
|
8023 |
|
8024 |
|
8025 |
|
8026 |
|
8027 |
|
8028 |
|
8029 | const parseAttributes = el => {
|
8030 | if (!(el && el.attributes)) {
|
8031 | return {};
|
8032 | }
|
8033 |
|
8034 | return from(el.attributes).reduce((a, e) => {
|
8035 | const parseFn = parsers[e.name] || parsers.DEFAULT;
|
8036 | a[e.name] = parseFn(e.value);
|
8037 | return a;
|
8038 | }, {});
|
8039 | };
|
8040 |
|
8041 | const keySystemsMap = {
|
8042 | 'urn:uuid:1077efec-c0b2-4d02-ace3-3c1e52e2fb4b': 'org.w3.clearkey',
|
8043 | 'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed': 'com.widevine.alpha',
|
8044 | 'urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95': 'com.microsoft.playready',
|
8045 | 'urn:uuid:f239e769-efa3-4850-9c16-a903c6932efb': 'com.adobe.primetime',
|
8046 |
|
8047 | 'urn:mpeg:dash:mp4protection:2011': 'mp4protection'
|
8048 | };
|
8049 | |
8050 |
|
8051 |
|
8052 |
|
8053 |
|
8054 |
|
8055 |
|
8056 |
|
8057 |
|
8058 |
|
8059 |
|
8060 | const buildBaseUrls = (references, baseUrlElements) => {
|
8061 | if (!baseUrlElements.length) {
|
8062 | return references;
|
8063 | }
|
8064 |
|
8065 | return flatten(references.map(function (reference) {
|
8066 | return baseUrlElements.map(function (baseUrlElement) {
|
8067 | const initialBaseUrl = getContent(baseUrlElement);
|
8068 | const resolvedBaseUrl = resolveUrl$1(reference.baseUrl, initialBaseUrl);
|
8069 | const finalBaseUrl = merge(parseAttributes(baseUrlElement), {
|
8070 | baseUrl: resolvedBaseUrl
|
8071 | });
|
8072 |
|
8073 |
|
8074 | if (resolvedBaseUrl !== initialBaseUrl && !finalBaseUrl.serviceLocation && reference.serviceLocation) {
|
8075 | finalBaseUrl.serviceLocation = reference.serviceLocation;
|
8076 | }
|
8077 |
|
8078 | return finalBaseUrl;
|
8079 | });
|
8080 | }));
|
8081 | };
|
8082 | |
8083 |
|
8084 |
|
8085 |
|
8086 |
|
8087 |
|
8088 |
|
8089 |
|
8090 |
|
8091 |
|
8092 |
|
8093 |
|
8094 |
|
8095 |
|
8096 | |
8097 |
|
8098 |
|
8099 |
|
8100 |
|
8101 |
|
8102 |
|
8103 |
|
8104 |
|
8105 |
|
8106 | const getSegmentInformation = adaptationSet => {
|
8107 | const segmentTemplate = findChildren(adaptationSet, 'SegmentTemplate')[0];
|
8108 | const segmentList = findChildren(adaptationSet, 'SegmentList')[0];
|
8109 | const segmentUrls = segmentList && findChildren(segmentList, 'SegmentURL').map(s => merge({
|
8110 | tag: 'SegmentURL'
|
8111 | }, parseAttributes(s)));
|
8112 | const segmentBase = findChildren(adaptationSet, 'SegmentBase')[0];
|
8113 | const segmentTimelineParentNode = segmentList || segmentTemplate;
|
8114 | const segmentTimeline = segmentTimelineParentNode && findChildren(segmentTimelineParentNode, 'SegmentTimeline')[0];
|
8115 | const segmentInitializationParentNode = segmentList || segmentBase || segmentTemplate;
|
8116 | const segmentInitialization = segmentInitializationParentNode && findChildren(segmentInitializationParentNode, 'Initialization')[0];
|
8117 |
|
8118 |
|
8119 |
|
8120 |
|
8121 |
|
8122 | const template = segmentTemplate && parseAttributes(segmentTemplate);
|
8123 |
|
8124 | if (template && segmentInitialization) {
|
8125 | template.initialization = segmentInitialization && parseAttributes(segmentInitialization);
|
8126 | } else if (template && template.initialization) {
|
8127 |
|
8128 |
|
8129 |
|
8130 | template.initialization = {
|
8131 | sourceURL: template.initialization
|
8132 | };
|
8133 | }
|
8134 |
|
8135 | const segmentInfo = {
|
8136 | template,
|
8137 | segmentTimeline: segmentTimeline && findChildren(segmentTimeline, 'S').map(s => parseAttributes(s)),
|
8138 | list: segmentList && merge(parseAttributes(segmentList), {
|
8139 | segmentUrls,
|
8140 | initialization: parseAttributes(segmentInitialization)
|
8141 | }),
|
8142 | base: segmentBase && merge(parseAttributes(segmentBase), {
|
8143 | initialization: parseAttributes(segmentInitialization)
|
8144 | })
|
8145 | };
|
8146 | Object.keys(segmentInfo).forEach(key => {
|
8147 | if (!segmentInfo[key]) {
|
8148 | delete segmentInfo[key];
|
8149 | }
|
8150 | });
|
8151 | return segmentInfo;
|
8152 | };
|
8153 | |
8154 |
|
8155 |
|
8156 |
|
8157 |
|
8158 |
|
8159 |
|
8160 |
|
8161 |
|
8162 |
|
8163 |
|
8164 | |
8165 |
|
8166 |
|
8167 |
|
8168 |
|
8169 |
|
8170 |
|
8171 |
|
8172 |
|
8173 |
|
8174 |
|
8175 | |
8176 |
|
8177 |
|
8178 |
|
8179 |
|
8180 |
|
8181 |
|
8182 |
|
8183 |
|
8184 |
|
8185 |
|
8186 |
|
8187 |
|
8188 |
|
8189 |
|
8190 |
|
8191 | const inheritBaseUrls = (adaptationSetAttributes, adaptationSetBaseUrls, adaptationSetSegmentInfo) => representation => {
|
8192 | const repBaseUrlElements = findChildren(representation, 'BaseURL');
|
8193 | const repBaseUrls = buildBaseUrls(adaptationSetBaseUrls, repBaseUrlElements);
|
8194 | const attributes = merge(adaptationSetAttributes, parseAttributes(representation));
|
8195 | const representationSegmentInfo = getSegmentInformation(representation);
|
8196 | return repBaseUrls.map(baseUrl => {
|
8197 | return {
|
8198 | segmentInfo: merge(adaptationSetSegmentInfo, representationSegmentInfo),
|
8199 | attributes: merge(attributes, baseUrl)
|
8200 | };
|
8201 | });
|
8202 | };
|
8203 | |
8204 |
|
8205 |
|
8206 |
|
8207 |
|
8208 |
|
8209 |
|
8210 |
|
8211 |
|
8212 |
|
8213 |
|
8214 | const generateKeySystemInformation = contentProtectionNodes => {
|
8215 | return contentProtectionNodes.reduce((acc, node) => {
|
8216 | const attributes = parseAttributes(node);
|
8217 |
|
8218 |
|
8219 |
|
8220 |
|
8221 | if (attributes.schemeIdUri) {
|
8222 | attributes.schemeIdUri = attributes.schemeIdUri.toLowerCase();
|
8223 | }
|
8224 |
|
8225 | const keySystem = keySystemsMap[attributes.schemeIdUri];
|
8226 |
|
8227 | if (keySystem) {
|
8228 | acc[keySystem] = {
|
8229 | attributes
|
8230 | };
|
8231 | const psshNode = findChildren(node, 'cenc:pssh')[0];
|
8232 |
|
8233 | if (psshNode) {
|
8234 | const pssh = getContent(psshNode);
|
8235 | acc[keySystem].pssh = pssh && decodeB64ToUint8Array(pssh);
|
8236 | }
|
8237 | }
|
8238 |
|
8239 | return acc;
|
8240 | }, {});
|
8241 | };
|
8242 |
|
8243 |
|
8244 | const parseCaptionServiceMetadata = service => {
|
8245 |
|
8246 | if (service.schemeIdUri === 'urn:scte:dash:cc:cea-608:2015') {
|
8247 | const values = typeof service.value !== 'string' ? [] : service.value.split(';');
|
8248 | return values.map(value => {
|
8249 | let channel;
|
8250 | let language;
|
8251 |
|
8252 | language = value;
|
8253 |
|
8254 | if (/^CC\d=/.test(value)) {
|
8255 | [channel, language] = value.split('=');
|
8256 | } else if (/^CC\d$/.test(value)) {
|
8257 | channel = value;
|
8258 | }
|
8259 |
|
8260 | return {
|
8261 | channel,
|
8262 | language
|
8263 | };
|
8264 | });
|
8265 | } else if (service.schemeIdUri === 'urn:scte:dash:cc:cea-708:2015') {
|
8266 | const values = typeof service.value !== 'string' ? [] : service.value.split(';');
|
8267 | return values.map(value => {
|
8268 | const flags = {
|
8269 |
|
8270 | 'channel': undefined,
|
8271 |
|
8272 |
|
8273 | 'language': undefined,
|
8274 |
|
8275 |
|
8276 | 'aspectRatio': 1,
|
8277 |
|
8278 |
|
8279 |
|
8280 | 'easyReader': 0,
|
8281 |
|
8282 |
|
8283 |
|
8284 | '3D': 0
|
8285 | };
|
8286 |
|
8287 | if (/=/.test(value)) {
|
8288 | const [channel, opts = ''] = value.split('=');
|
8289 | flags.channel = channel;
|
8290 | flags.language = value;
|
8291 | opts.split(',').forEach(opt => {
|
8292 | const [name, val] = opt.split(':');
|
8293 |
|
8294 | if (name === 'lang') {
|
8295 | flags.language = val;
|
8296 | } else if (name === 'er') {
|
8297 | flags.easyReader = Number(val);
|
8298 | } else if (name === 'war') {
|
8299 | flags.aspectRatio = Number(val);
|
8300 | } else if (name === '3D') {
|
8301 | flags['3D'] = Number(val);
|
8302 | }
|
8303 | });
|
8304 | } else {
|
8305 | flags.language = value;
|
8306 | }
|
8307 |
|
8308 | if (flags.channel) {
|
8309 | flags.channel = 'SERVICE' + flags.channel;
|
8310 | }
|
8311 |
|
8312 | return flags;
|
8313 | });
|
8314 | }
|
8315 | };
|
8316 | |
8317 |
|
8318 |
|
8319 |
|
8320 |
|
8321 |
|
8322 |
|
8323 |
|
8324 |
|
8325 |
|
8326 | const toEventStream = period => {
|
8327 |
|
8328 | return flatten(findChildren(period.node, 'EventStream').map(eventStream => {
|
8329 | const eventStreamAttributes = parseAttributes(eventStream);
|
8330 | const schemeIdUri = eventStreamAttributes.schemeIdUri;
|
8331 |
|
8332 | return findChildren(eventStream, 'Event').map(event => {
|
8333 | const eventAttributes = parseAttributes(event);
|
8334 | const presentationTime = eventAttributes.presentationTime || 0;
|
8335 | const timescale = eventStreamAttributes.timescale || 1;
|
8336 | const duration = eventAttributes.duration || 0;
|
8337 | const start = presentationTime / timescale + period.attributes.start;
|
8338 | return {
|
8339 | schemeIdUri,
|
8340 | value: eventStreamAttributes.value,
|
8341 | id: eventAttributes.id,
|
8342 | start,
|
8343 | end: start + duration / timescale,
|
8344 | messageData: getContent(event) || eventAttributes.messageData,
|
8345 | contentEncoding: eventStreamAttributes.contentEncoding,
|
8346 | presentationTimeOffset: eventStreamAttributes.presentationTimeOffset || 0
|
8347 | };
|
8348 | });
|
8349 | }));
|
8350 | };
|
8351 | |
8352 |
|
8353 |
|
8354 |
|
8355 |
|
8356 |
|
8357 |
|
8358 |
|
8359 |
|
8360 |
|
8361 |
|
8362 | |
8363 |
|
8364 |
|
8365 |
|
8366 |
|
8367 |
|
8368 |
|
8369 |
|
8370 |
|
8371 |
|
8372 |
|
8373 |
|
8374 |
|
8375 |
|
8376 |
|
8377 |
|
8378 | const toRepresentations = (periodAttributes, periodBaseUrls, periodSegmentInfo) => adaptationSet => {
|
8379 | const adaptationSetAttributes = parseAttributes(adaptationSet);
|
8380 | const adaptationSetBaseUrls = buildBaseUrls(periodBaseUrls, findChildren(adaptationSet, 'BaseURL'));
|
8381 | const role = findChildren(adaptationSet, 'Role')[0];
|
8382 | const roleAttributes = {
|
8383 | role: parseAttributes(role)
|
8384 | };
|
8385 | let attrs = merge(periodAttributes, adaptationSetAttributes, roleAttributes);
|
8386 | const accessibility = findChildren(adaptationSet, 'Accessibility')[0];
|
8387 | const captionServices = parseCaptionServiceMetadata(parseAttributes(accessibility));
|
8388 |
|
8389 | if (captionServices) {
|
8390 | attrs = merge(attrs, {
|
8391 | captionServices
|
8392 | });
|
8393 | }
|
8394 |
|
8395 | const label = findChildren(adaptationSet, 'Label')[0];
|
8396 |
|
8397 | if (label && label.childNodes.length) {
|
8398 | const labelVal = label.childNodes[0].nodeValue.trim();
|
8399 | attrs = merge(attrs, {
|
8400 | label: labelVal
|
8401 | });
|
8402 | }
|
8403 |
|
8404 | const contentProtection = generateKeySystemInformation(findChildren(adaptationSet, 'ContentProtection'));
|
8405 |
|
8406 | if (Object.keys(contentProtection).length) {
|
8407 | attrs = merge(attrs, {
|
8408 | contentProtection
|
8409 | });
|
8410 | }
|
8411 |
|
8412 | const segmentInfo = getSegmentInformation(adaptationSet);
|
8413 | const representations = findChildren(adaptationSet, 'Representation');
|
8414 | const adaptationSetSegmentInfo = merge(periodSegmentInfo, segmentInfo);
|
8415 | return flatten(representations.map(inheritBaseUrls(attrs, adaptationSetBaseUrls, adaptationSetSegmentInfo)));
|
8416 | };
|
8417 | |
8418 |
|
8419 |
|
8420 |
|
8421 |
|
8422 |
|
8423 |
|
8424 |
|
8425 |
|
8426 |
|
8427 | |
8428 |
|
8429 |
|
8430 |
|
8431 |
|
8432 |
|
8433 |
|
8434 |
|
8435 |
|
8436 |
|
8437 |
|
8438 |
|
8439 |
|
8440 |
|
8441 | |
8442 |
|
8443 |
|
8444 |
|
8445 |
|
8446 |
|
8447 |
|
8448 |
|
8449 |
|
8450 |
|
8451 |
|
8452 |
|
8453 |
|
8454 |
|
8455 | const toAdaptationSets = (mpdAttributes, mpdBaseUrls) => (period, index) => {
|
8456 | const periodBaseUrls = buildBaseUrls(mpdBaseUrls, findChildren(period.node, 'BaseURL'));
|
8457 | const periodAttributes = merge(mpdAttributes, {
|
8458 | periodStart: period.attributes.start
|
8459 | });
|
8460 |
|
8461 | if (typeof period.attributes.duration === 'number') {
|
8462 | periodAttributes.periodDuration = period.attributes.duration;
|
8463 | }
|
8464 |
|
8465 | const adaptationSets = findChildren(period.node, 'AdaptationSet');
|
8466 | const periodSegmentInfo = getSegmentInformation(period.node);
|
8467 | return flatten(adaptationSets.map(toRepresentations(periodAttributes, periodBaseUrls, periodSegmentInfo)));
|
8468 | };
|
8469 | |
8470 |
|
8471 |
|
8472 |
|
8473 |
|
8474 |
|
8475 |
|
8476 |
|
8477 |
|
8478 |
|
8479 |
|
8480 |
|
8481 |
|
8482 |
|
8483 |
|
8484 |
|
8485 | const generateContentSteeringInformation = (contentSteeringNodes, eventHandler) => {
|
8486 |
|
8487 | if (contentSteeringNodes.length > 1) {
|
8488 | eventHandler({
|
8489 | type: 'warn',
|
8490 | message: 'The MPD manifest should contain no more than one ContentSteering tag'
|
8491 | });
|
8492 | }
|
8493 |
|
8494 |
|
8495 | if (!contentSteeringNodes.length) {
|
8496 | return null;
|
8497 | }
|
8498 |
|
8499 | const infoFromContentSteeringTag = merge({
|
8500 | serverURL: getContent(contentSteeringNodes[0])
|
8501 | }, parseAttributes(contentSteeringNodes[0]));
|
8502 |
|
8503 |
|
8504 | infoFromContentSteeringTag.queryBeforeStart = infoFromContentSteeringTag.queryBeforeStart === 'true';
|
8505 | return infoFromContentSteeringTag;
|
8506 | };
|
8507 | |
8508 |
|
8509 |
|
8510 |
|
8511 |
|
8512 |
|
8513 |
|
8514 |
|
8515 |
|
8516 |
|
8517 |
|
8518 |
|
8519 |
|
8520 |
|
8521 |
|
8522 |
|
8523 | const getPeriodStart = ({
|
8524 | attributes,
|
8525 | priorPeriodAttributes,
|
8526 | mpdType
|
8527 | }) => {
|
8528 |
|
8529 |
|
8530 |
|
8531 |
|
8532 |
|
8533 |
|
8534 |
|
8535 |
|
8536 |
|
8537 |
|
8538 |
|
8539 |
|
8540 |
|
8541 |
|
8542 | if (typeof attributes.start === 'number') {
|
8543 | return attributes.start;
|
8544 | }
|
8545 |
|
8546 |
|
8547 | if (priorPeriodAttributes && typeof priorPeriodAttributes.start === 'number' && typeof priorPeriodAttributes.duration === 'number') {
|
8548 | return priorPeriodAttributes.start + priorPeriodAttributes.duration;
|
8549 | }
|
8550 |
|
8551 |
|
8552 | if (!priorPeriodAttributes && mpdType === 'static') {
|
8553 | return 0;
|
8554 | }
|
8555 |
|
8556 |
|
8557 |
|
8558 |
|
8559 |
|
8560 |
|
8561 |
|
8562 |
|
8563 | return null;
|
8564 | };
|
8565 | |
8566 |
|
8567 |
|
8568 |
|
8569 |
|
8570 |
|
8571 |
|
8572 |
|
8573 |
|
8574 |
|
8575 |
|
8576 |
|
8577 |
|
8578 |
|
8579 |
|
8580 |
|
8581 |
|
8582 |
|
8583 |
|
8584 | const inheritAttributes = (mpd, options = {}) => {
|
8585 | const {
|
8586 | manifestUri = '',
|
8587 | NOW = Date.now(),
|
8588 | clientOffset = 0,
|
8589 |
|
8590 |
|
8591 |
|
8592 |
|
8593 |
|
8594 |
|
8595 | eventHandler = function () {}
|
8596 | } = options;
|
8597 | const periodNodes = findChildren(mpd, 'Period');
|
8598 |
|
8599 | if (!periodNodes.length) {
|
8600 | throw new Error(errors.INVALID_NUMBER_OF_PERIOD);
|
8601 | }
|
8602 |
|
8603 | const locations = findChildren(mpd, 'Location');
|
8604 | const mpdAttributes = parseAttributes(mpd);
|
8605 | const mpdBaseUrls = buildBaseUrls([{
|
8606 | baseUrl: manifestUri
|
8607 | }], findChildren(mpd, 'BaseURL'));
|
8608 | const contentSteeringNodes = findChildren(mpd, 'ContentSteering');
|
8609 |
|
8610 | mpdAttributes.type = mpdAttributes.type || 'static';
|
8611 | mpdAttributes.sourceDuration = mpdAttributes.mediaPresentationDuration || 0;
|
8612 | mpdAttributes.NOW = NOW;
|
8613 | mpdAttributes.clientOffset = clientOffset;
|
8614 |
|
8615 | if (locations.length) {
|
8616 | mpdAttributes.locations = locations.map(getContent);
|
8617 | }
|
8618 |
|
8619 | const periods = [];
|
8620 |
|
8621 |
|
8622 |
|
8623 |
|
8624 | periodNodes.forEach((node, index) => {
|
8625 | const attributes = parseAttributes(node);
|
8626 |
|
8627 |
|
8628 | const priorPeriod = periods[index - 1];
|
8629 | attributes.start = getPeriodStart({
|
8630 | attributes,
|
8631 | priorPeriodAttributes: priorPeriod ? priorPeriod.attributes : null,
|
8632 | mpdType: mpdAttributes.type
|
8633 | });
|
8634 | periods.push({
|
8635 | node,
|
8636 | attributes
|
8637 | });
|
8638 | });
|
8639 | return {
|
8640 | locations: mpdAttributes.locations,
|
8641 | contentSteeringInfo: generateContentSteeringInformation(contentSteeringNodes, eventHandler),
|
8642 |
|
8643 |
|
8644 |
|
8645 |
|
8646 |
|
8647 |
|
8648 |
|
8649 | representationInfo: flatten(periods.map(toAdaptationSets(mpdAttributes, mpdBaseUrls))),
|
8650 | eventStream: flatten(periods.map(toEventStream))
|
8651 | };
|
8652 | };
|
8653 |
|
8654 | const stringToMpdXml = manifestString => {
|
8655 | if (manifestString === '') {
|
8656 | throw new Error(errors.DASH_EMPTY_MANIFEST);
|
8657 | }
|
8658 |
|
8659 | const parser = new xmldom.DOMParser();
|
8660 | let xml;
|
8661 | let mpd;
|
8662 |
|
8663 | try {
|
8664 | xml = parser.parseFromString(manifestString, 'application/xml');
|
8665 | mpd = xml && xml.documentElement.tagName === 'MPD' ? xml.documentElement : null;
|
8666 | } catch (e) {
|
8667 | }
|
8668 |
|
8669 | if (!mpd || mpd && mpd.getElementsByTagName('parsererror').length > 0) {
|
8670 | throw new Error(errors.DASH_INVALID_XML);
|
8671 | }
|
8672 |
|
8673 | return mpd;
|
8674 | };
|
8675 | |
8676 |
|
8677 |
|
8678 |
|
8679 |
|
8680 |
|
8681 |
|
8682 |
|
8683 |
|
8684 |
|
8685 | const parseUTCTimingScheme = mpd => {
|
8686 | const UTCTimingNode = findChildren(mpd, 'UTCTiming')[0];
|
8687 |
|
8688 | if (!UTCTimingNode) {
|
8689 | return null;
|
8690 | }
|
8691 |
|
8692 | const attributes = parseAttributes(UTCTimingNode);
|
8693 |
|
8694 | switch (attributes.schemeIdUri) {
|
8695 | case 'urn:mpeg:dash:utc:http-head:2014':
|
8696 | case 'urn:mpeg:dash:utc:http-head:2012':
|
8697 | attributes.method = 'HEAD';
|
8698 | break;
|
8699 |
|
8700 | case 'urn:mpeg:dash:utc:http-xsdate:2014':
|
8701 | case 'urn:mpeg:dash:utc:http-iso:2014':
|
8702 | case 'urn:mpeg:dash:utc:http-xsdate:2012':
|
8703 | case 'urn:mpeg:dash:utc:http-iso:2012':
|
8704 | attributes.method = 'GET';
|
8705 | break;
|
8706 |
|
8707 | case 'urn:mpeg:dash:utc:direct:2014':
|
8708 | case 'urn:mpeg:dash:utc:direct:2012':
|
8709 | attributes.method = 'DIRECT';
|
8710 | attributes.value = Date.parse(attributes.value);
|
8711 | break;
|
8712 |
|
8713 | case 'urn:mpeg:dash:utc:http-ntp:2014':
|
8714 | case 'urn:mpeg:dash:utc:ntp:2014':
|
8715 | case 'urn:mpeg:dash:utc:sntp:2014':
|
8716 | default:
|
8717 | throw new Error(errors.UNSUPPORTED_UTC_TIMING_SCHEME);
|
8718 | }
|
8719 |
|
8720 | return attributes;
|
8721 | };
|
8722 | |
8723 |
|
8724 |
|
8725 |
|
8726 |
|
8727 |
|
8728 |
|
8729 |
|
8730 |
|
8731 |
|
8732 |
|
8733 |
|
8734 |
|
8735 |
|
8736 | const parse = (manifestString, options = {}) => {
|
8737 | const parsedManifestInfo = inheritAttributes(stringToMpdXml(manifestString), options);
|
8738 | const playlists = toPlaylists(parsedManifestInfo.representationInfo);
|
8739 | return toM3u8({
|
8740 | dashPlaylists: playlists,
|
8741 | locations: parsedManifestInfo.locations,
|
8742 | contentSteering: parsedManifestInfo.contentSteeringInfo,
|
8743 | sidxMapping: options.sidxMapping,
|
8744 | previousManifest: options.previousManifest,
|
8745 | eventStream: parsedManifestInfo.eventStream
|
8746 | });
|
8747 | };
|
8748 | |
8749 |
|
8750 |
|
8751 |
|
8752 |
|
8753 |
|
8754 |
|
8755 |
|
8756 |
|
8757 |
|
8758 | const parseUTCTiming = manifestString => parseUTCTimingScheme(stringToMpdXml(manifestString));
|
8759 |
|
8760 | var MAX_UINT32 = Math.pow(2, 32);
|
8761 |
|
8762 | var getUint64$1 = function (uint8) {
|
8763 | var dv = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
|
8764 | var value;
|
8765 |
|
8766 | if (dv.getBigUint64) {
|
8767 | value = dv.getBigUint64(0);
|
8768 |
|
8769 | if (value < Number.MAX_SAFE_INTEGER) {
|
8770 | return Number(value);
|
8771 | }
|
8772 |
|
8773 | return value;
|
8774 | }
|
8775 |
|
8776 | return dv.getUint32(0) * MAX_UINT32 + dv.getUint32(4);
|
8777 | };
|
8778 |
|
8779 | var numbers = {
|
8780 | getUint64: getUint64$1,
|
8781 | MAX_UINT32: MAX_UINT32
|
8782 | };
|
8783 |
|
8784 | var getUint64 = numbers.getUint64;
|
8785 |
|
8786 | var parseSidx = function (data) {
|
8787 | var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
|
8788 | result = {
|
8789 | version: data[0],
|
8790 | flags: new Uint8Array(data.subarray(1, 4)),
|
8791 | references: [],
|
8792 | referenceId: view.getUint32(4),
|
8793 | timescale: view.getUint32(8)
|
8794 | },
|
8795 | i = 12;
|
8796 |
|
8797 | if (result.version === 0) {
|
8798 | result.earliestPresentationTime = view.getUint32(i);
|
8799 | result.firstOffset = view.getUint32(i + 4);
|
8800 | i += 8;
|
8801 | } else {
|
8802 |
|
8803 | result.earliestPresentationTime = getUint64(data.subarray(i));
|
8804 | result.firstOffset = getUint64(data.subarray(i + 8));
|
8805 | i += 16;
|
8806 | }
|
8807 |
|
8808 | i += 2;
|
8809 |
|
8810 | var referenceCount = view.getUint16(i);
|
8811 | i += 2;
|
8812 |
|
8813 | for (; referenceCount > 0; i += 12, referenceCount--) {
|
8814 | result.references.push({
|
8815 | referenceType: (data[i] & 0x80) >>> 7,
|
8816 | referencedSize: view.getUint32(i) & 0x7FFFFFFF,
|
8817 | subsegmentDuration: view.getUint32(i + 4),
|
8818 | startsWithSap: !!(data[i + 8] & 0x80),
|
8819 | sapType: (data[i + 8] & 0x70) >>> 4,
|
8820 | sapDeltaTime: view.getUint32(i + 8) & 0x0FFFFFFF
|
8821 | });
|
8822 | }
|
8823 |
|
8824 | return result;
|
8825 | };
|
8826 |
|
8827 | var parseSidx_1 = parseSidx;
|
8828 |
|
8829 | var ID3 = toUint8([0x49, 0x44, 0x33]);
|
8830 | var getId3Size = function getId3Size(bytes, offset) {
|
8831 | if (offset === void 0) {
|
8832 | offset = 0;
|
8833 | }
|
8834 |
|
8835 | bytes = toUint8(bytes);
|
8836 | var flags = bytes[offset + 5];
|
8837 | var returnSize = bytes[offset + 6] << 21 | bytes[offset + 7] << 14 | bytes[offset + 8] << 7 | bytes[offset + 9];
|
8838 | var footerPresent = (flags & 16) >> 4;
|
8839 |
|
8840 | if (footerPresent) {
|
8841 | return returnSize + 20;
|
8842 | }
|
8843 |
|
8844 | return returnSize + 10;
|
8845 | };
|
8846 | var getId3Offset = function getId3Offset(bytes, offset) {
|
8847 | if (offset === void 0) {
|
8848 | offset = 0;
|
8849 | }
|
8850 |
|
8851 | bytes = toUint8(bytes);
|
8852 |
|
8853 | if (bytes.length - offset < 10 || !bytesMatch(bytes, ID3, {
|
8854 | offset: offset
|
8855 | })) {
|
8856 | return offset;
|
8857 | }
|
8858 |
|
8859 | offset += getId3Size(bytes, offset);
|
8860 |
|
8861 |
|
8862 |
|
8863 | return getId3Offset(bytes, offset);
|
8864 | };
|
8865 |
|
8866 | var normalizePath$1 = function normalizePath(path) {
|
8867 | if (typeof path === 'string') {
|
8868 | return stringToBytes(path);
|
8869 | }
|
8870 |
|
8871 | if (typeof path === 'number') {
|
8872 | return path;
|
8873 | }
|
8874 |
|
8875 | return path;
|
8876 | };
|
8877 |
|
8878 | var normalizePaths$1 = function normalizePaths(paths) {
|
8879 | if (!Array.isArray(paths)) {
|
8880 | return [normalizePath$1(paths)];
|
8881 | }
|
8882 |
|
8883 | return paths.map(function (p) {
|
8884 | return normalizePath$1(p);
|
8885 | });
|
8886 | };
|
8887 | |
8888 |
|
8889 |
|
8890 |
|
8891 |
|
8892 |
|
8893 |
|
8894 |
|
8895 |
|
8896 |
|
8897 |
|
8898 |
|
8899 |
|
8900 |
|
8901 |
|
8902 |
|
8903 |
|
8904 |
|
8905 |
|
8906 |
|
8907 |
|
8908 | var findBox = function findBox(bytes, paths, complete) {
|
8909 | if (complete === void 0) {
|
8910 | complete = false;
|
8911 | }
|
8912 |
|
8913 | paths = normalizePaths$1(paths);
|
8914 | bytes = toUint8(bytes);
|
8915 | var results = [];
|
8916 |
|
8917 | if (!paths.length) {
|
8918 |
|
8919 | return results;
|
8920 | }
|
8921 |
|
8922 | var i = 0;
|
8923 |
|
8924 | while (i < bytes.length) {
|
8925 | var size = (bytes[i] << 24 | bytes[i + 1] << 16 | bytes[i + 2] << 8 | bytes[i + 3]) >>> 0;
|
8926 | var type = bytes.subarray(i + 4, i + 8);
|
8927 |
|
8928 | if (size === 0) {
|
8929 | break;
|
8930 | }
|
8931 |
|
8932 | var end = i + size;
|
8933 |
|
8934 | if (end > bytes.length) {
|
8935 |
|
8936 |
|
8937 | if (complete) {
|
8938 | break;
|
8939 | }
|
8940 |
|
8941 | end = bytes.length;
|
8942 | }
|
8943 |
|
8944 | var data = bytes.subarray(i + 8, end);
|
8945 |
|
8946 | if (bytesMatch(type, paths[0])) {
|
8947 | if (paths.length === 1) {
|
8948 |
|
8949 |
|
8950 | results.push(data);
|
8951 | } else {
|
8952 |
|
8953 | results.push.apply(results, findBox(data, paths.slice(1), complete));
|
8954 | }
|
8955 | }
|
8956 |
|
8957 | i = end;
|
8958 | }
|
8959 |
|
8960 |
|
8961 | return results;
|
8962 | };
|
8963 |
|
8964 |
|
8965 |
|
8966 |
|
8967 |
|
8968 | var EBML_TAGS = {
|
8969 | EBML: toUint8([0x1A, 0x45, 0xDF, 0xA3]),
|
8970 | DocType: toUint8([0x42, 0x82]),
|
8971 | Segment: toUint8([0x18, 0x53, 0x80, 0x67]),
|
8972 | SegmentInfo: toUint8([0x15, 0x49, 0xA9, 0x66]),
|
8973 | Tracks: toUint8([0x16, 0x54, 0xAE, 0x6B]),
|
8974 | Track: toUint8([0xAE]),
|
8975 | TrackNumber: toUint8([0xd7]),
|
8976 | DefaultDuration: toUint8([0x23, 0xe3, 0x83]),
|
8977 | TrackEntry: toUint8([0xAE]),
|
8978 | TrackType: toUint8([0x83]),
|
8979 | FlagDefault: toUint8([0x88]),
|
8980 | CodecID: toUint8([0x86]),
|
8981 | CodecPrivate: toUint8([0x63, 0xA2]),
|
8982 | VideoTrack: toUint8([0xe0]),
|
8983 | AudioTrack: toUint8([0xe1]),
|
8984 |
|
8985 |
|
8986 |
|
8987 | Cluster: toUint8([0x1F, 0x43, 0xB6, 0x75]),
|
8988 | Timestamp: toUint8([0xE7]),
|
8989 | TimestampScale: toUint8([0x2A, 0xD7, 0xB1]),
|
8990 | BlockGroup: toUint8([0xA0]),
|
8991 | BlockDuration: toUint8([0x9B]),
|
8992 | Block: toUint8([0xA1]),
|
8993 | SimpleBlock: toUint8([0xA3])
|
8994 | };
|
8995 | |
8996 |
|
8997 |
|
8998 |
|
8999 |
|
9000 |
|
9001 |
|
9002 |
|
9003 | var LENGTH_TABLE = [128, 64, 32, 16, 8, 4, 2, 1];
|
9004 |
|
9005 | var getLength = function getLength(byte) {
|
9006 | var len = 1;
|
9007 |
|
9008 | for (var i = 0; i < LENGTH_TABLE.length; i++) {
|
9009 | if (byte & LENGTH_TABLE[i]) {
|
9010 | break;
|
9011 | }
|
9012 |
|
9013 | len++;
|
9014 | }
|
9015 |
|
9016 | return len;
|
9017 | };
|
9018 |
|
9019 |
|
9020 |
|
9021 |
|
9022 |
|
9023 |
|
9024 | var getvint = function getvint(bytes, offset, removeLength, signed) {
|
9025 | if (removeLength === void 0) {
|
9026 | removeLength = true;
|
9027 | }
|
9028 |
|
9029 | if (signed === void 0) {
|
9030 | signed = false;
|
9031 | }
|
9032 |
|
9033 | var length = getLength(bytes[offset]);
|
9034 | var valueBytes = bytes.subarray(offset, offset + length);
|
9035 |
|
9036 |
|
9037 |
|
9038 |
|
9039 | if (removeLength) {
|
9040 | valueBytes = Array.prototype.slice.call(bytes, offset, offset + length);
|
9041 | valueBytes[0] ^= LENGTH_TABLE[length - 1];
|
9042 | }
|
9043 |
|
9044 | return {
|
9045 | length: length,
|
9046 | value: bytesToNumber(valueBytes, {
|
9047 | signed: signed
|
9048 | }),
|
9049 | bytes: valueBytes
|
9050 | };
|
9051 | };
|
9052 |
|
9053 | var normalizePath = function normalizePath(path) {
|
9054 | if (typeof path === 'string') {
|
9055 | return path.match(/.{1,2}/g).map(function (p) {
|
9056 | return normalizePath(p);
|
9057 | });
|
9058 | }
|
9059 |
|
9060 | if (typeof path === 'number') {
|
9061 | return numberToBytes(path);
|
9062 | }
|
9063 |
|
9064 | return path;
|
9065 | };
|
9066 |
|
9067 | var normalizePaths = function normalizePaths(paths) {
|
9068 | if (!Array.isArray(paths)) {
|
9069 | return [normalizePath(paths)];
|
9070 | }
|
9071 |
|
9072 | return paths.map(function (p) {
|
9073 | return normalizePath(p);
|
9074 | });
|
9075 | };
|
9076 |
|
9077 | var getInfinityDataSize = function getInfinityDataSize(id, bytes, offset) {
|
9078 | if (offset >= bytes.length) {
|
9079 | return bytes.length;
|
9080 | }
|
9081 |
|
9082 | var innerid = getvint(bytes, offset, false);
|
9083 |
|
9084 | if (bytesMatch(id.bytes, innerid.bytes)) {
|
9085 | return offset;
|
9086 | }
|
9087 |
|
9088 | var dataHeader = getvint(bytes, offset + innerid.length);
|
9089 | return getInfinityDataSize(id, bytes, offset + dataHeader.length + dataHeader.value + innerid.length);
|
9090 | };
|
9091 | |
9092 |
|
9093 |
|
9094 |
|
9095 |
|
9096 |
|
9097 |
|
9098 |
|
9099 |
|
9100 |
|
9101 |
|
9102 |
|
9103 |
|
9104 |
|
9105 |
|
9106 |
|
9107 |
|
9108 |
|
9109 |
|
9110 |
|
9111 |
|
9112 | var findEbml = function findEbml(bytes, paths) {
|
9113 | paths = normalizePaths(paths);
|
9114 | bytes = toUint8(bytes);
|
9115 | var results = [];
|
9116 |
|
9117 | if (!paths.length) {
|
9118 | return results;
|
9119 | }
|
9120 |
|
9121 | var i = 0;
|
9122 |
|
9123 | while (i < bytes.length) {
|
9124 | var id = getvint(bytes, i, false);
|
9125 | var dataHeader = getvint(bytes, i + id.length);
|
9126 | var dataStart = i + id.length + dataHeader.length;
|
9127 |
|
9128 | if (dataHeader.value === 0x7f) {
|
9129 | dataHeader.value = getInfinityDataSize(id, bytes, dataStart);
|
9130 |
|
9131 | if (dataHeader.value !== bytes.length) {
|
9132 | dataHeader.value -= dataStart;
|
9133 | }
|
9134 | }
|
9135 |
|
9136 | var dataEnd = dataStart + dataHeader.value > bytes.length ? bytes.length : dataStart + dataHeader.value;
|
9137 | var data = bytes.subarray(dataStart, dataEnd);
|
9138 |
|
9139 | if (bytesMatch(paths[0], id.bytes)) {
|
9140 | if (paths.length === 1) {
|
9141 |
|
9142 |
|
9143 | results.push(data);
|
9144 | } else {
|
9145 |
|
9146 |
|
9147 | results = results.concat(findEbml(data, paths.slice(1)));
|
9148 | }
|
9149 | }
|
9150 |
|
9151 | var totalLength = id.length + dataHeader.length + data.length;
|
9152 |
|
9153 | i += totalLength;
|
9154 | }
|
9155 |
|
9156 | return results;
|
9157 | };
|
9158 |
|
9159 | var NAL_TYPE_ONE = toUint8([0x00, 0x00, 0x00, 0x01]);
|
9160 | var NAL_TYPE_TWO = toUint8([0x00, 0x00, 0x01]);
|
9161 | var EMULATION_PREVENTION = toUint8([0x00, 0x00, 0x03]);
|
9162 | |
9163 |
|
9164 |
|
9165 |
|
9166 |
|
9167 |
|
9168 |
|
9169 |
|
9170 |
|
9171 |
|
9172 | var discardEmulationPreventionBytes = function discardEmulationPreventionBytes(bytes) {
|
9173 | var positions = [];
|
9174 | var i = 1;
|
9175 |
|
9176 | while (i < bytes.length - 2) {
|
9177 | if (bytesMatch(bytes.subarray(i, i + 3), EMULATION_PREVENTION)) {
|
9178 | positions.push(i + 2);
|
9179 | i++;
|
9180 | }
|
9181 |
|
9182 | i++;
|
9183 | }
|
9184 |
|
9185 |
|
9186 |
|
9187 | if (positions.length === 0) {
|
9188 | return bytes;
|
9189 | }
|
9190 |
|
9191 |
|
9192 | var newLength = bytes.length - positions.length;
|
9193 | var newData = new Uint8Array(newLength);
|
9194 | var sourceIndex = 0;
|
9195 |
|
9196 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
9197 | if (sourceIndex === positions[0]) {
|
9198 |
|
9199 | sourceIndex++;
|
9200 |
|
9201 | positions.shift();
|
9202 | }
|
9203 |
|
9204 | newData[i] = bytes[sourceIndex];
|
9205 | }
|
9206 |
|
9207 | return newData;
|
9208 | };
|
9209 | var findNal = function findNal(bytes, dataType, types, nalLimit) {
|
9210 | if (nalLimit === void 0) {
|
9211 | nalLimit = Infinity;
|
9212 | }
|
9213 |
|
9214 | bytes = toUint8(bytes);
|
9215 | types = [].concat(types);
|
9216 | var i = 0;
|
9217 | var nalStart;
|
9218 | var nalsFound = 0;
|
9219 |
|
9220 |
|
9221 |
|
9222 |
|
9223 |
|
9224 | while (i < bytes.length && (nalsFound < nalLimit || nalStart)) {
|
9225 | var nalOffset = void 0;
|
9226 |
|
9227 | if (bytesMatch(bytes.subarray(i), NAL_TYPE_ONE)) {
|
9228 | nalOffset = 4;
|
9229 | } else if (bytesMatch(bytes.subarray(i), NAL_TYPE_TWO)) {
|
9230 | nalOffset = 3;
|
9231 | }
|
9232 |
|
9233 |
|
9234 |
|
9235 | if (!nalOffset) {
|
9236 | i++;
|
9237 | continue;
|
9238 | }
|
9239 |
|
9240 | nalsFound++;
|
9241 |
|
9242 | if (nalStart) {
|
9243 | return discardEmulationPreventionBytes(bytes.subarray(nalStart, i));
|
9244 | }
|
9245 |
|
9246 | var nalType = void 0;
|
9247 |
|
9248 | if (dataType === 'h264') {
|
9249 | nalType = bytes[i + nalOffset] & 0x1f;
|
9250 | } else if (dataType === 'h265') {
|
9251 | nalType = bytes[i + nalOffset] >> 1 & 0x3f;
|
9252 | }
|
9253 |
|
9254 | if (types.indexOf(nalType) !== -1) {
|
9255 | nalStart = i + nalOffset;
|
9256 | }
|
9257 |
|
9258 |
|
9259 | i += nalOffset + (dataType === 'h264' ? 1 : 2);
|
9260 | }
|
9261 |
|
9262 | return bytes.subarray(0, 0);
|
9263 | };
|
9264 | var findH264Nal = function findH264Nal(bytes, type, nalLimit) {
|
9265 | return findNal(bytes, 'h264', type, nalLimit);
|
9266 | };
|
9267 | var findH265Nal = function findH265Nal(bytes, type, nalLimit) {
|
9268 | return findNal(bytes, 'h265', type, nalLimit);
|
9269 | };
|
9270 |
|
9271 | var CONSTANTS = {
|
9272 |
|
9273 | 'webm': toUint8([0x77, 0x65, 0x62, 0x6d]),
|
9274 |
|
9275 | 'matroska': toUint8([0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61]),
|
9276 |
|
9277 | 'flac': toUint8([0x66, 0x4c, 0x61, 0x43]),
|
9278 |
|
9279 | 'ogg': toUint8([0x4f, 0x67, 0x67, 0x53]),
|
9280 |
|
9281 |
|
9282 | 'ac3': toUint8([0x0b, 0x77]),
|
9283 |
|
9284 | 'riff': toUint8([0x52, 0x49, 0x46, 0x46]),
|
9285 |
|
9286 | 'avi': toUint8([0x41, 0x56, 0x49]),
|
9287 |
|
9288 | 'wav': toUint8([0x57, 0x41, 0x56, 0x45]),
|
9289 |
|
9290 | '3gp': toUint8([0x66, 0x74, 0x79, 0x70, 0x33, 0x67]),
|
9291 |
|
9292 | 'mp4': toUint8([0x66, 0x74, 0x79, 0x70]),
|
9293 |
|
9294 | 'fmp4': toUint8([0x73, 0x74, 0x79, 0x70]),
|
9295 |
|
9296 | 'mov': toUint8([0x66, 0x74, 0x79, 0x70, 0x71, 0x74]),
|
9297 |
|
9298 | 'moov': toUint8([0x6D, 0x6F, 0x6F, 0x76]),
|
9299 |
|
9300 | 'moof': toUint8([0x6D, 0x6F, 0x6F, 0x66])
|
9301 | };
|
9302 | var _isLikely = {
|
9303 | aac: function aac(bytes) {
|
9304 | var offset = getId3Offset(bytes);
|
9305 | return bytesMatch(bytes, [0xFF, 0x10], {
|
9306 | offset: offset,
|
9307 | mask: [0xFF, 0x16]
|
9308 | });
|
9309 | },
|
9310 | mp3: function mp3(bytes) {
|
9311 | var offset = getId3Offset(bytes);
|
9312 | return bytesMatch(bytes, [0xFF, 0x02], {
|
9313 | offset: offset,
|
9314 | mask: [0xFF, 0x06]
|
9315 | });
|
9316 | },
|
9317 | webm: function webm(bytes) {
|
9318 | var docType = findEbml(bytes, [EBML_TAGS.EBML, EBML_TAGS.DocType])[0];
|
9319 |
|
9320 | return bytesMatch(docType, CONSTANTS.webm);
|
9321 | },
|
9322 | mkv: function mkv(bytes) {
|
9323 | var docType = findEbml(bytes, [EBML_TAGS.EBML, EBML_TAGS.DocType])[0];
|
9324 |
|
9325 | return bytesMatch(docType, CONSTANTS.matroska);
|
9326 | },
|
9327 | mp4: function mp4(bytes) {
|
9328 |
|
9329 | if (_isLikely['3gp'](bytes) || _isLikely.mov(bytes)) {
|
9330 | return false;
|
9331 | }
|
9332 |
|
9333 |
|
9334 | if (bytesMatch(bytes, CONSTANTS.mp4, {
|
9335 | offset: 4
|
9336 | }) || bytesMatch(bytes, CONSTANTS.fmp4, {
|
9337 | offset: 4
|
9338 | })) {
|
9339 | return true;
|
9340 | }
|
9341 |
|
9342 |
|
9343 | if (bytesMatch(bytes, CONSTANTS.moof, {
|
9344 | offset: 4
|
9345 | }) || bytesMatch(bytes, CONSTANTS.moov, {
|
9346 | offset: 4
|
9347 | })) {
|
9348 | return true;
|
9349 | }
|
9350 | },
|
9351 | mov: function mov(bytes) {
|
9352 | return bytesMatch(bytes, CONSTANTS.mov, {
|
9353 | offset: 4
|
9354 | });
|
9355 | },
|
9356 | '3gp': function gp(bytes) {
|
9357 | return bytesMatch(bytes, CONSTANTS['3gp'], {
|
9358 | offset: 4
|
9359 | });
|
9360 | },
|
9361 | ac3: function ac3(bytes) {
|
9362 | var offset = getId3Offset(bytes);
|
9363 | return bytesMatch(bytes, CONSTANTS.ac3, {
|
9364 | offset: offset
|
9365 | });
|
9366 | },
|
9367 | ts: function ts(bytes) {
|
9368 | if (bytes.length < 189 && bytes.length >= 1) {
|
9369 | return bytes[0] === 0x47;
|
9370 | }
|
9371 |
|
9372 | var i = 0;
|
9373 |
|
9374 | while (i + 188 < bytes.length && i < 188) {
|
9375 | if (bytes[i] === 0x47 && bytes[i + 188] === 0x47) {
|
9376 | return true;
|
9377 | }
|
9378 |
|
9379 | i += 1;
|
9380 | }
|
9381 |
|
9382 | return false;
|
9383 | },
|
9384 | flac: function flac(bytes) {
|
9385 | var offset = getId3Offset(bytes);
|
9386 | return bytesMatch(bytes, CONSTANTS.flac, {
|
9387 | offset: offset
|
9388 | });
|
9389 | },
|
9390 | ogg: function ogg(bytes) {
|
9391 | return bytesMatch(bytes, CONSTANTS.ogg);
|
9392 | },
|
9393 | avi: function avi(bytes) {
|
9394 | return bytesMatch(bytes, CONSTANTS.riff) && bytesMatch(bytes, CONSTANTS.avi, {
|
9395 | offset: 8
|
9396 | });
|
9397 | },
|
9398 | wav: function wav(bytes) {
|
9399 | return bytesMatch(bytes, CONSTANTS.riff) && bytesMatch(bytes, CONSTANTS.wav, {
|
9400 | offset: 8
|
9401 | });
|
9402 | },
|
9403 | 'h264': function h264(bytes) {
|
9404 |
|
9405 | return findH264Nal(bytes, 7, 3).length;
|
9406 | },
|
9407 | 'h265': function h265(bytes) {
|
9408 |
|
9409 | return findH265Nal(bytes, [32, 33], 3).length;
|
9410 | }
|
9411 | };
|
9412 |
|
9413 |
|
9414 |
|
9415 | var isLikelyTypes = Object.keys(_isLikely)
|
9416 | .filter(function (t) {
|
9417 | return t !== 'ts' && t !== 'h264' && t !== 'h265';
|
9418 | })
|
9419 | .concat(['ts', 'h264', 'h265']);
|
9420 |
|
9421 | isLikelyTypes.forEach(function (type) {
|
9422 | var isLikelyFn = _isLikely[type];
|
9423 |
|
9424 | _isLikely[type] = function (bytes) {
|
9425 | return isLikelyFn(toUint8(bytes));
|
9426 | };
|
9427 | });
|
9428 |
|
9429 | var isLikely = _isLikely;
|
9430 |
|
9431 |
|
9432 | var detectContainerForBytes = function detectContainerForBytes(bytes) {
|
9433 | bytes = toUint8(bytes);
|
9434 |
|
9435 | for (var i = 0; i < isLikelyTypes.length; i++) {
|
9436 | var type = isLikelyTypes[i];
|
9437 |
|
9438 | if (isLikely[type](bytes)) {
|
9439 | return type;
|
9440 | }
|
9441 | }
|
9442 |
|
9443 | return '';
|
9444 | };
|
9445 |
|
9446 | var isLikelyFmp4MediaSegment = function isLikelyFmp4MediaSegment(bytes) {
|
9447 | return findBox(bytes, ['moof']).length > 0;
|
9448 | };
|
9449 |
|
9450 |
|
9451 |
|
9452 | const callbackOnCompleted = (request, cb) => {
|
9453 | if (request.readyState === 4) {
|
9454 | return cb();
|
9455 | }
|
9456 |
|
9457 | return;
|
9458 | };
|
9459 |
|
9460 | const containerRequest = (uri, xhr, cb) => {
|
9461 | let bytes = [];
|
9462 | let id3Offset;
|
9463 | let finished = false;
|
9464 |
|
9465 | const endRequestAndCallback = function (err, req, type, _bytes) {
|
9466 | req.abort();
|
9467 | finished = true;
|
9468 | return cb(err, req, type, _bytes);
|
9469 | };
|
9470 |
|
9471 | const progressListener = function (error, request) {
|
9472 | if (finished) {
|
9473 | return;
|
9474 | }
|
9475 |
|
9476 | if (error) {
|
9477 | return endRequestAndCallback(error, request, '', bytes);
|
9478 | }
|
9479 |
|
9480 |
|
9481 | const newPart = request.responseText.substring(bytes && bytes.byteLength || 0, request.responseText.length);
|
9482 |
|
9483 | bytes = concatTypedArrays(bytes, stringToBytes(newPart, true));
|
9484 | id3Offset = id3Offset || getId3Offset(bytes);
|
9485 |
|
9486 |
|
9487 | if (bytes.length < 10 || id3Offset && bytes.length < id3Offset + 2) {
|
9488 | return callbackOnCompleted(request, () => endRequestAndCallback(error, request, '', bytes));
|
9489 | }
|
9490 |
|
9491 | const type = detectContainerForBytes(bytes);
|
9492 |
|
9493 |
|
9494 |
|
9495 | if (type === 'ts' && bytes.length < 188) {
|
9496 | return callbackOnCompleted(request, () => endRequestAndCallback(error, request, '', bytes));
|
9497 | }
|
9498 |
|
9499 |
|
9500 |
|
9501 | if (!type && bytes.length < 376) {
|
9502 | return callbackOnCompleted(request, () => endRequestAndCallback(error, request, '', bytes));
|
9503 | }
|
9504 |
|
9505 | return endRequestAndCallback(null, request, type, bytes);
|
9506 | };
|
9507 |
|
9508 | const options = {
|
9509 | uri,
|
9510 |
|
9511 | beforeSend(request) {
|
9512 |
|
9513 | request.overrideMimeType('text/plain; charset=x-user-defined');
|
9514 | request.addEventListener('progress', function ({
|
9515 | total,
|
9516 | loaded
|
9517 | }) {
|
9518 | return callbackWrapper(request, null, {
|
9519 | statusCode: request.status
|
9520 | }, progressListener);
|
9521 | });
|
9522 | }
|
9523 |
|
9524 | };
|
9525 | const request = xhr(options, function (error, response) {
|
9526 | return callbackWrapper(request, error, response, progressListener);
|
9527 | });
|
9528 | return request;
|
9529 | };
|
9530 |
|
9531 | const {
|
9532 | EventTarget
|
9533 | } = videojs__default["default"];
|
9534 |
|
9535 | const dashPlaylistUnchanged = function (a, b) {
|
9536 | if (!isPlaylistUnchanged(a, b)) {
|
9537 | return false;
|
9538 | }
|
9539 |
|
9540 |
|
9541 |
|
9542 |
|
9543 |
|
9544 |
|
9545 |
|
9546 | if (a.sidx && b.sidx && (a.sidx.offset !== b.sidx.offset || a.sidx.length !== b.sidx.length)) {
|
9547 | return false;
|
9548 | } else if (!a.sidx && b.sidx || a.sidx && !b.sidx) {
|
9549 | return false;
|
9550 | }
|
9551 |
|
9552 |
|
9553 |
|
9554 | if (a.segments && !b.segments || !a.segments && b.segments) {
|
9555 | return false;
|
9556 | }
|
9557 |
|
9558 |
|
9559 | if (!a.segments && !b.segments) {
|
9560 | return true;
|
9561 | }
|
9562 |
|
9563 |
|
9564 | for (let i = 0; i < a.segments.length; i++) {
|
9565 | const aSegment = a.segments[i];
|
9566 | const bSegment = b.segments[i];
|
9567 |
|
9568 | if (aSegment.uri !== bSegment.uri) {
|
9569 | return false;
|
9570 | }
|
9571 |
|
9572 |
|
9573 | if (!aSegment.byterange && !bSegment.byterange) {
|
9574 | continue;
|
9575 | }
|
9576 |
|
9577 | const aByterange = aSegment.byterange;
|
9578 | const bByterange = bSegment.byterange;
|
9579 |
|
9580 | if (aByterange && !bByterange || !aByterange && bByterange) {
|
9581 | return false;
|
9582 | }
|
9583 |
|
9584 |
|
9585 | if (aByterange.offset !== bByterange.offset || aByterange.length !== bByterange.length) {
|
9586 | return false;
|
9587 | }
|
9588 | }
|
9589 |
|
9590 |
|
9591 | return true;
|
9592 | };
|
9593 | |
9594 |
|
9595 |
|
9596 |
|
9597 |
|
9598 |
|
9599 |
|
9600 |
|
9601 | const dashGroupId = (type, group, label, playlist) => {
|
9602 |
|
9603 | const playlistId = playlist.attributes.NAME || label;
|
9604 | return `placeholder-uri-${type}-${group}-${playlistId}`;
|
9605 | };
|
9606 | |
9607 |
|
9608 |
|
9609 |
|
9610 |
|
9611 |
|
9612 |
|
9613 |
|
9614 |
|
9615 |
|
9616 |
|
9617 |
|
9618 |
|
9619 |
|
9620 |
|
9621 |
|
9622 |
|
9623 |
|
9624 | const parseMainXml = ({
|
9625 | mainXml,
|
9626 | srcUrl,
|
9627 | clientOffset,
|
9628 | sidxMapping,
|
9629 | previousManifest
|
9630 | }) => {
|
9631 | const manifest = parse(mainXml, {
|
9632 | manifestUri: srcUrl,
|
9633 | clientOffset,
|
9634 | sidxMapping,
|
9635 | previousManifest
|
9636 | });
|
9637 | addPropertiesToMain(manifest, srcUrl, dashGroupId);
|
9638 | return manifest;
|
9639 | };
|
9640 | |
9641 |
|
9642 |
|
9643 |
|
9644 |
|
9645 |
|
9646 |
|
9647 |
|
9648 |
|
9649 | const removeOldMediaGroupLabels = (update, newMain) => {
|
9650 | forEachMediaGroup$1(update, (properties, type, group, label) => {
|
9651 | if (!(label in newMain.mediaGroups[type][group])) {
|
9652 | delete update.mediaGroups[type][group][label];
|
9653 | }
|
9654 | });
|
9655 | };
|
9656 | |
9657 |
|
9658 |
|
9659 |
|
9660 |
|
9661 |
|
9662 |
|
9663 |
|
9664 |
|
9665 |
|
9666 |
|
9667 |
|
9668 |
|
9669 |
|
9670 | const updateMain = (oldMain, newMain, sidxMapping) => {
|
9671 | let noChanges = true;
|
9672 | let update = merge$1(oldMain, {
|
9673 |
|
9674 | duration: newMain.duration,
|
9675 | minimumUpdatePeriod: newMain.minimumUpdatePeriod,
|
9676 | timelineStarts: newMain.timelineStarts
|
9677 | });
|
9678 |
|
9679 | for (let i = 0; i < newMain.playlists.length; i++) {
|
9680 | const playlist = newMain.playlists[i];
|
9681 |
|
9682 | if (playlist.sidx) {
|
9683 | const sidxKey = generateSidxKey(playlist.sidx);
|
9684 |
|
9685 | if (sidxMapping && sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx) {
|
9686 | addSidxSegmentsToPlaylist$1(playlist, sidxMapping[sidxKey].sidx, playlist.sidx.resolvedUri);
|
9687 | }
|
9688 | }
|
9689 |
|
9690 | const playlistUpdate = updateMain$1(update, playlist, dashPlaylistUnchanged);
|
9691 |
|
9692 | if (playlistUpdate) {
|
9693 | update = playlistUpdate;
|
9694 | noChanges = false;
|
9695 | }
|
9696 | }
|
9697 |
|
9698 |
|
9699 | forEachMediaGroup$1(newMain, (properties, type, group, label) => {
|
9700 | if (properties.playlists && properties.playlists.length) {
|
9701 | const id = properties.playlists[0].id;
|
9702 | const playlistUpdate = updateMain$1(update, properties.playlists[0], dashPlaylistUnchanged);
|
9703 |
|
9704 | if (playlistUpdate) {
|
9705 | update = playlistUpdate;
|
9706 |
|
9707 | if (!(label in update.mediaGroups[type][group])) {
|
9708 | update.mediaGroups[type][group][label] = properties;
|
9709 | }
|
9710 |
|
9711 |
|
9712 | update.mediaGroups[type][group][label].playlists[0] = update.playlists[id];
|
9713 | noChanges = false;
|
9714 | }
|
9715 | }
|
9716 | });
|
9717 |
|
9718 | removeOldMediaGroupLabels(update, newMain);
|
9719 |
|
9720 | if (newMain.minimumUpdatePeriod !== oldMain.minimumUpdatePeriod) {
|
9721 | noChanges = false;
|
9722 | }
|
9723 |
|
9724 | if (noChanges) {
|
9725 | return null;
|
9726 | }
|
9727 |
|
9728 | return update;
|
9729 | };
|
9730 |
|
9731 |
|
9732 |
|
9733 |
|
9734 | const equivalentSidx = (a, b) => {
|
9735 | const neitherMap = Boolean(!a.map && !b.map);
|
9736 | const equivalentMap = neitherMap || Boolean(a.map && b.map && a.map.byterange.offset === b.map.byterange.offset && a.map.byterange.length === b.map.byterange.length);
|
9737 | return equivalentMap && a.uri === b.uri && a.byterange.offset === b.byterange.offset && a.byterange.length === b.byterange.length;
|
9738 | };
|
9739 |
|
9740 |
|
9741 | const compareSidxEntry = (playlists, oldSidxMapping) => {
|
9742 | const newSidxMapping = {};
|
9743 |
|
9744 | for (const id in playlists) {
|
9745 | const playlist = playlists[id];
|
9746 | const currentSidxInfo = playlist.sidx;
|
9747 |
|
9748 | if (currentSidxInfo) {
|
9749 | const key = generateSidxKey(currentSidxInfo);
|
9750 |
|
9751 | if (!oldSidxMapping[key]) {
|
9752 | break;
|
9753 | }
|
9754 |
|
9755 | const savedSidxInfo = oldSidxMapping[key].sidxInfo;
|
9756 |
|
9757 | if (equivalentSidx(savedSidxInfo, currentSidxInfo)) {
|
9758 | newSidxMapping[key] = oldSidxMapping[key];
|
9759 | }
|
9760 | }
|
9761 | }
|
9762 |
|
9763 | return newSidxMapping;
|
9764 | };
|
9765 | |
9766 |
|
9767 |
|
9768 |
|
9769 |
|
9770 |
|
9771 |
|
9772 |
|
9773 |
|
9774 | const filterChangedSidxMappings = (main, oldSidxMapping) => {
|
9775 | const videoSidx = compareSidxEntry(main.playlists, oldSidxMapping);
|
9776 | let mediaGroupSidx = videoSidx;
|
9777 | forEachMediaGroup$1(main, (properties, mediaType, groupKey, labelKey) => {
|
9778 | if (properties.playlists && properties.playlists.length) {
|
9779 | const playlists = properties.playlists;
|
9780 | mediaGroupSidx = merge$1(mediaGroupSidx, compareSidxEntry(playlists, oldSidxMapping));
|
9781 | }
|
9782 | });
|
9783 | return mediaGroupSidx;
|
9784 | };
|
9785 | class DashPlaylistLoader extends EventTarget {
|
9786 |
|
9787 |
|
9788 |
|
9789 | constructor(srcUrlOrPlaylist, vhs, options = {}, mainPlaylistLoader) {
|
9790 | super();
|
9791 | this.mainPlaylistLoader_ = mainPlaylistLoader || this;
|
9792 |
|
9793 | if (!mainPlaylistLoader) {
|
9794 | this.isMain_ = true;
|
9795 | }
|
9796 |
|
9797 | const {
|
9798 | withCredentials = false
|
9799 | } = options;
|
9800 | this.vhs_ = vhs;
|
9801 | this.withCredentials = withCredentials;
|
9802 | this.addMetadataToTextTrack = options.addMetadataToTextTrack;
|
9803 |
|
9804 | if (!srcUrlOrPlaylist) {
|
9805 | throw new Error('A non-empty playlist URL or object is required');
|
9806 | }
|
9807 |
|
9808 |
|
9809 | this.on('minimumUpdatePeriod', () => {
|
9810 | this.refreshXml_();
|
9811 | });
|
9812 |
|
9813 | this.on('mediaupdatetimeout', () => {
|
9814 | this.refreshMedia_(this.media().id);
|
9815 | });
|
9816 | this.state = 'HAVE_NOTHING';
|
9817 | this.loadedPlaylists_ = {};
|
9818 | this.logger_ = logger('DashPlaylistLoader');
|
9819 |
|
9820 |
|
9821 | if (this.isMain_) {
|
9822 | this.mainPlaylistLoader_.srcUrl = srcUrlOrPlaylist;
|
9823 |
|
9824 |
|
9825 | this.mainPlaylistLoader_.sidxMapping_ = {};
|
9826 | } else {
|
9827 | this.childPlaylist_ = srcUrlOrPlaylist;
|
9828 | }
|
9829 | }
|
9830 |
|
9831 | requestErrored_(err, request, startingState) {
|
9832 |
|
9833 | if (!this.request) {
|
9834 | return true;
|
9835 | }
|
9836 |
|
9837 |
|
9838 | this.request = null;
|
9839 |
|
9840 | if (err) {
|
9841 |
|
9842 |
|
9843 | this.error = typeof err === 'object' && !(err instanceof Error) ? err : {
|
9844 | status: request.status,
|
9845 | message: 'DASH request error at URL: ' + request.uri,
|
9846 | response: request.response,
|
9847 |
|
9848 | code: 2
|
9849 | };
|
9850 |
|
9851 | if (startingState) {
|
9852 | this.state = startingState;
|
9853 | }
|
9854 |
|
9855 | this.trigger('error');
|
9856 | return true;
|
9857 | }
|
9858 | }
|
9859 | |
9860 |
|
9861 |
|
9862 |
|
9863 |
|
9864 |
|
9865 | addSidxSegments_(playlist, startingState, cb) {
|
9866 | const sidxKey = playlist.sidx && generateSidxKey(playlist.sidx);
|
9867 |
|
9868 | if (!playlist.sidx || !sidxKey || this.mainPlaylistLoader_.sidxMapping_[sidxKey]) {
|
9869 |
|
9870 | this.mediaRequest_ = window.setTimeout(() => cb(false), 0);
|
9871 | return;
|
9872 | }
|
9873 |
|
9874 |
|
9875 | const uri = resolveManifestRedirect(playlist.sidx.resolvedUri);
|
9876 |
|
9877 | const fin = (err, request) => {
|
9878 | if (this.requestErrored_(err, request, startingState)) {
|
9879 | return;
|
9880 | }
|
9881 |
|
9882 | const sidxMapping = this.mainPlaylistLoader_.sidxMapping_;
|
9883 | let sidx;
|
9884 |
|
9885 | try {
|
9886 | sidx = parseSidx_1(toUint8(request.response).subarray(8));
|
9887 | } catch (e) {
|
9888 |
|
9889 | this.requestErrored_(e, request, startingState);
|
9890 | return;
|
9891 | }
|
9892 |
|
9893 | sidxMapping[sidxKey] = {
|
9894 | sidxInfo: playlist.sidx,
|
9895 | sidx
|
9896 | };
|
9897 | addSidxSegmentsToPlaylist$1(playlist, sidx, playlist.sidx.resolvedUri);
|
9898 | return cb(true);
|
9899 | };
|
9900 |
|
9901 | this.request = containerRequest(uri, this.vhs_.xhr, (err, request, container, bytes) => {
|
9902 | if (err) {
|
9903 | return fin(err, request);
|
9904 | }
|
9905 |
|
9906 | if (!container || container !== 'mp4') {
|
9907 | return fin({
|
9908 | status: request.status,
|
9909 | message: `Unsupported ${container || 'unknown'} container type for sidx segment at URL: ${uri}`,
|
9910 |
|
9911 |
|
9912 | response: '',
|
9913 | playlist,
|
9914 | internal: true,
|
9915 | playlistExclusionDuration: Infinity,
|
9916 |
|
9917 | code: 2
|
9918 | }, request);
|
9919 | }
|
9920 |
|
9921 |
|
9922 | const {
|
9923 | offset,
|
9924 | length
|
9925 | } = playlist.sidx.byterange;
|
9926 |
|
9927 | if (bytes.length >= length + offset) {
|
9928 | return fin(err, {
|
9929 | response: bytes.subarray(offset, offset + length),
|
9930 | status: request.status,
|
9931 | uri: request.uri
|
9932 | });
|
9933 | }
|
9934 |
|
9935 |
|
9936 | this.request = this.vhs_.xhr({
|
9937 | uri,
|
9938 | responseType: 'arraybuffer',
|
9939 | headers: segmentXhrHeaders({
|
9940 | byterange: playlist.sidx.byterange
|
9941 | })
|
9942 | }, fin);
|
9943 | });
|
9944 | }
|
9945 |
|
9946 | dispose() {
|
9947 | this.trigger('dispose');
|
9948 | this.stopRequest();
|
9949 | this.loadedPlaylists_ = {};
|
9950 | window.clearTimeout(this.minimumUpdatePeriodTimeout_);
|
9951 | window.clearTimeout(this.mediaRequest_);
|
9952 | window.clearTimeout(this.mediaUpdateTimeout);
|
9953 | this.mediaUpdateTimeout = null;
|
9954 | this.mediaRequest_ = null;
|
9955 | this.minimumUpdatePeriodTimeout_ = null;
|
9956 |
|
9957 | if (this.mainPlaylistLoader_.createMupOnMedia_) {
|
9958 | this.off('loadedmetadata', this.mainPlaylistLoader_.createMupOnMedia_);
|
9959 | this.mainPlaylistLoader_.createMupOnMedia_ = null;
|
9960 | }
|
9961 |
|
9962 | this.off();
|
9963 | }
|
9964 |
|
9965 | hasPendingRequest() {
|
9966 | return this.request || this.mediaRequest_;
|
9967 | }
|
9968 |
|
9969 | stopRequest() {
|
9970 | if (this.request) {
|
9971 | const oldRequest = this.request;
|
9972 | this.request = null;
|
9973 | oldRequest.onreadystatechange = null;
|
9974 | oldRequest.abort();
|
9975 | }
|
9976 | }
|
9977 |
|
9978 | media(playlist) {
|
9979 |
|
9980 | if (!playlist) {
|
9981 | return this.media_;
|
9982 | }
|
9983 |
|
9984 |
|
9985 | if (this.state === 'HAVE_NOTHING') {
|
9986 | throw new Error('Cannot switch media playlist from ' + this.state);
|
9987 | }
|
9988 |
|
9989 | const startingState = this.state;
|
9990 |
|
9991 | if (typeof playlist === 'string') {
|
9992 | if (!this.mainPlaylistLoader_.main.playlists[playlist]) {
|
9993 | throw new Error('Unknown playlist URI: ' + playlist);
|
9994 | }
|
9995 |
|
9996 | playlist = this.mainPlaylistLoader_.main.playlists[playlist];
|
9997 | }
|
9998 |
|
9999 | const mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
10000 |
|
10001 | if (mediaChange && this.loadedPlaylists_[playlist.id] && this.loadedPlaylists_[playlist.id].endList) {
|
10002 | this.state = 'HAVE_METADATA';
|
10003 | this.media_ = playlist;
|
10004 |
|
10005 | if (mediaChange) {
|
10006 | this.trigger('mediachanging');
|
10007 | this.trigger('mediachange');
|
10008 | }
|
10009 |
|
10010 | return;
|
10011 | }
|
10012 |
|
10013 |
|
10014 | if (!mediaChange) {
|
10015 | return;
|
10016 | }
|
10017 |
|
10018 |
|
10019 | if (this.media_) {
|
10020 | this.trigger('mediachanging');
|
10021 | }
|
10022 |
|
10023 | this.addSidxSegments_(playlist, startingState, sidxChanged => {
|
10024 |
|
10025 | this.haveMetadata({
|
10026 | startingState,
|
10027 | playlist
|
10028 | });
|
10029 | });
|
10030 | }
|
10031 |
|
10032 | haveMetadata({
|
10033 | startingState,
|
10034 | playlist
|
10035 | }) {
|
10036 | this.state = 'HAVE_METADATA';
|
10037 | this.loadedPlaylists_[playlist.id] = playlist;
|
10038 | this.mediaRequest_ = null;
|
10039 |
|
10040 | this.refreshMedia_(playlist.id);
|
10041 |
|
10042 |
|
10043 | if (startingState === 'HAVE_MAIN_MANIFEST') {
|
10044 | this.trigger('loadedmetadata');
|
10045 | } else {
|
10046 |
|
10047 | this.trigger('mediachange');
|
10048 | }
|
10049 | }
|
10050 |
|
10051 | pause() {
|
10052 | if (this.mainPlaylistLoader_.createMupOnMedia_) {
|
10053 | this.off('loadedmetadata', this.mainPlaylistLoader_.createMupOnMedia_);
|
10054 | this.mainPlaylistLoader_.createMupOnMedia_ = null;
|
10055 | }
|
10056 |
|
10057 | this.stopRequest();
|
10058 | window.clearTimeout(this.mediaUpdateTimeout);
|
10059 | this.mediaUpdateTimeout = null;
|
10060 |
|
10061 | if (this.isMain_) {
|
10062 | window.clearTimeout(this.mainPlaylistLoader_.minimumUpdatePeriodTimeout_);
|
10063 | this.mainPlaylistLoader_.minimumUpdatePeriodTimeout_ = null;
|
10064 | }
|
10065 |
|
10066 | if (this.state === 'HAVE_NOTHING') {
|
10067 |
|
10068 |
|
10069 | this.started = false;
|
10070 | }
|
10071 | }
|
10072 |
|
10073 | load(isFinalRendition) {
|
10074 | window.clearTimeout(this.mediaUpdateTimeout);
|
10075 | this.mediaUpdateTimeout = null;
|
10076 | const media = this.media();
|
10077 |
|
10078 | if (isFinalRendition) {
|
10079 | const delay = media ? media.targetDuration / 2 * 1000 : 5 * 1000;
|
10080 | this.mediaUpdateTimeout = window.setTimeout(() => this.load(), delay);
|
10081 | return;
|
10082 | }
|
10083 |
|
10084 |
|
10085 |
|
10086 | if (!this.started) {
|
10087 | this.start();
|
10088 | return;
|
10089 | }
|
10090 |
|
10091 | if (media && !media.endList) {
|
10092 |
|
10093 |
|
10094 |
|
10095 | if (this.isMain_ && !this.minimumUpdatePeriodTimeout_) {
|
10096 |
|
10097 | this.trigger('minimumUpdatePeriod');
|
10098 |
|
10099 | this.updateMinimumUpdatePeriodTimeout_();
|
10100 | }
|
10101 |
|
10102 | this.trigger('mediaupdatetimeout');
|
10103 | } else {
|
10104 | this.trigger('loadedplaylist');
|
10105 | }
|
10106 | }
|
10107 |
|
10108 | start() {
|
10109 | this.started = true;
|
10110 |
|
10111 |
|
10112 | if (!this.isMain_) {
|
10113 | this.mediaRequest_ = window.setTimeout(() => this.haveMain_(), 0);
|
10114 | return;
|
10115 | }
|
10116 |
|
10117 | this.requestMain_((req, mainChanged) => {
|
10118 | this.haveMain_();
|
10119 |
|
10120 | if (!this.hasPendingRequest() && !this.media_) {
|
10121 | this.media(this.mainPlaylistLoader_.main.playlists[0]);
|
10122 | }
|
10123 | });
|
10124 | }
|
10125 |
|
10126 | requestMain_(cb) {
|
10127 | this.request = this.vhs_.xhr({
|
10128 | uri: this.mainPlaylistLoader_.srcUrl,
|
10129 | withCredentials: this.withCredentials
|
10130 | }, (error, req) => {
|
10131 | if (this.requestErrored_(error, req)) {
|
10132 | if (this.state === 'HAVE_NOTHING') {
|
10133 | this.started = false;
|
10134 | }
|
10135 |
|
10136 | return;
|
10137 | }
|
10138 |
|
10139 | const mainChanged = req.responseText !== this.mainPlaylistLoader_.mainXml_;
|
10140 | this.mainPlaylistLoader_.mainXml_ = req.responseText;
|
10141 |
|
10142 | if (req.responseHeaders && req.responseHeaders.date) {
|
10143 | this.mainLoaded_ = Date.parse(req.responseHeaders.date);
|
10144 | } else {
|
10145 | this.mainLoaded_ = Date.now();
|
10146 | }
|
10147 |
|
10148 | this.mainPlaylistLoader_.srcUrl = resolveManifestRedirect(this.mainPlaylistLoader_.srcUrl, req);
|
10149 |
|
10150 | if (mainChanged) {
|
10151 | this.handleMain_();
|
10152 | this.syncClientServerClock_(() => {
|
10153 | return cb(req, mainChanged);
|
10154 | });
|
10155 | return;
|
10156 | }
|
10157 |
|
10158 | return cb(req, mainChanged);
|
10159 | });
|
10160 | }
|
10161 | |
10162 |
|
10163 |
|
10164 |
|
10165 |
|
10166 |
|
10167 |
|
10168 |
|
10169 |
|
10170 | syncClientServerClock_(done) {
|
10171 | const utcTiming = parseUTCTiming(this.mainPlaylistLoader_.mainXml_);
|
10172 |
|
10173 |
|
10174 | if (utcTiming === null) {
|
10175 | this.mainPlaylistLoader_.clientOffset_ = this.mainLoaded_ - Date.now();
|
10176 | return done();
|
10177 | }
|
10178 |
|
10179 | if (utcTiming.method === 'DIRECT') {
|
10180 | this.mainPlaylistLoader_.clientOffset_ = utcTiming.value - Date.now();
|
10181 | return done();
|
10182 | }
|
10183 |
|
10184 | this.request = this.vhs_.xhr({
|
10185 | uri: resolveUrl(this.mainPlaylistLoader_.srcUrl, utcTiming.value),
|
10186 | method: utcTiming.method,
|
10187 | withCredentials: this.withCredentials
|
10188 | }, (error, req) => {
|
10189 |
|
10190 | if (!this.request) {
|
10191 | return;
|
10192 | }
|
10193 |
|
10194 | if (error) {
|
10195 |
|
10196 |
|
10197 | this.mainPlaylistLoader_.clientOffset_ = this.mainLoaded_ - Date.now();
|
10198 | return done();
|
10199 | }
|
10200 |
|
10201 | let serverTime;
|
10202 |
|
10203 | if (utcTiming.method === 'HEAD') {
|
10204 | if (!req.responseHeaders || !req.responseHeaders.date) {
|
10205 |
|
10206 |
|
10207 | serverTime = this.mainLoaded_;
|
10208 | } else {
|
10209 | serverTime = Date.parse(req.responseHeaders.date);
|
10210 | }
|
10211 | } else {
|
10212 | serverTime = Date.parse(req.responseText);
|
10213 | }
|
10214 |
|
10215 | this.mainPlaylistLoader_.clientOffset_ = serverTime - Date.now();
|
10216 | done();
|
10217 | });
|
10218 | }
|
10219 |
|
10220 | haveMain_() {
|
10221 | this.state = 'HAVE_MAIN_MANIFEST';
|
10222 |
|
10223 | if (this.isMain_) {
|
10224 |
|
10225 |
|
10226 |
|
10227 | this.trigger('loadedplaylist');
|
10228 | } else if (!this.media_) {
|
10229 |
|
10230 |
|
10231 | this.media(this.childPlaylist_);
|
10232 | }
|
10233 | }
|
10234 |
|
10235 | handleMain_() {
|
10236 |
|
10237 | this.mediaRequest_ = null;
|
10238 | const oldMain = this.mainPlaylistLoader_.main;
|
10239 | let newMain = parseMainXml({
|
10240 | mainXml: this.mainPlaylistLoader_.mainXml_,
|
10241 | srcUrl: this.mainPlaylistLoader_.srcUrl,
|
10242 | clientOffset: this.mainPlaylistLoader_.clientOffset_,
|
10243 | sidxMapping: this.mainPlaylistLoader_.sidxMapping_,
|
10244 | previousManifest: oldMain
|
10245 | });
|
10246 |
|
10247 | if (oldMain) {
|
10248 | newMain = updateMain(oldMain, newMain, this.mainPlaylistLoader_.sidxMapping_);
|
10249 | }
|
10250 |
|
10251 |
|
10252 | this.mainPlaylistLoader_.main = newMain ? newMain : oldMain;
|
10253 | const location = this.mainPlaylistLoader_.main.locations && this.mainPlaylistLoader_.main.locations[0];
|
10254 |
|
10255 | if (location && location !== this.mainPlaylistLoader_.srcUrl) {
|
10256 | this.mainPlaylistLoader_.srcUrl = location;
|
10257 | }
|
10258 |
|
10259 | if (!oldMain || newMain && newMain.minimumUpdatePeriod !== oldMain.minimumUpdatePeriod) {
|
10260 | this.updateMinimumUpdatePeriodTimeout_();
|
10261 | }
|
10262 |
|
10263 | this.addEventStreamToMetadataTrack_(newMain);
|
10264 | return Boolean(newMain);
|
10265 | }
|
10266 |
|
10267 | updateMinimumUpdatePeriodTimeout_() {
|
10268 | const mpl = this.mainPlaylistLoader_;
|
10269 |
|
10270 |
|
10271 | if (mpl.createMupOnMedia_) {
|
10272 | mpl.off('loadedmetadata', mpl.createMupOnMedia_);
|
10273 | mpl.createMupOnMedia_ = null;
|
10274 | }
|
10275 |
|
10276 |
|
10277 | if (mpl.minimumUpdatePeriodTimeout_) {
|
10278 | window.clearTimeout(mpl.minimumUpdatePeriodTimeout_);
|
10279 | mpl.minimumUpdatePeriodTimeout_ = null;
|
10280 | }
|
10281 |
|
10282 | let mup = mpl.main && mpl.main.minimumUpdatePeriod;
|
10283 |
|
10284 |
|
10285 |
|
10286 |
|
10287 | if (mup === 0) {
|
10288 | if (mpl.media()) {
|
10289 | mup = mpl.media().targetDuration * 1000;
|
10290 | } else {
|
10291 | mpl.createMupOnMedia_ = mpl.updateMinimumUpdatePeriodTimeout_;
|
10292 | mpl.one('loadedmetadata', mpl.createMupOnMedia_);
|
10293 | }
|
10294 | }
|
10295 |
|
10296 |
|
10297 |
|
10298 |
|
10299 | if (typeof mup !== 'number' || mup <= 0) {
|
10300 | if (mup < 0) {
|
10301 | this.logger_(`found invalid minimumUpdatePeriod of ${mup}, not setting a timeout`);
|
10302 | }
|
10303 |
|
10304 | return;
|
10305 | }
|
10306 |
|
10307 | this.createMUPTimeout_(mup);
|
10308 | }
|
10309 |
|
10310 | createMUPTimeout_(mup) {
|
10311 | const mpl = this.mainPlaylistLoader_;
|
10312 | mpl.minimumUpdatePeriodTimeout_ = window.setTimeout(() => {
|
10313 | mpl.minimumUpdatePeriodTimeout_ = null;
|
10314 | mpl.trigger('minimumUpdatePeriod');
|
10315 | mpl.createMUPTimeout_(mup);
|
10316 | }, mup);
|
10317 | }
|
10318 | |
10319 |
|
10320 |
|
10321 |
|
10322 |
|
10323 | refreshXml_() {
|
10324 | this.requestMain_((req, mainChanged) => {
|
10325 | if (!mainChanged) {
|
10326 | return;
|
10327 | }
|
10328 |
|
10329 | if (this.media_) {
|
10330 | this.media_ = this.mainPlaylistLoader_.main.playlists[this.media_.id];
|
10331 | }
|
10332 |
|
10333 |
|
10334 | this.mainPlaylistLoader_.sidxMapping_ = filterChangedSidxMappings(this.mainPlaylistLoader_.main, this.mainPlaylistLoader_.sidxMapping_);
|
10335 | this.addSidxSegments_(this.media(), this.state, sidxChanged => {
|
10336 |
|
10337 | this.refreshMedia_(this.media().id);
|
10338 | });
|
10339 | });
|
10340 | }
|
10341 | |
10342 |
|
10343 |
|
10344 |
|
10345 |
|
10346 |
|
10347 |
|
10348 | refreshMedia_(mediaID) {
|
10349 | if (!mediaID) {
|
10350 | throw new Error('refreshMedia_ must take a media id');
|
10351 | }
|
10352 |
|
10353 |
|
10354 |
|
10355 |
|
10356 |
|
10357 |
|
10358 | if (this.media_ && this.isMain_) {
|
10359 | this.handleMain_();
|
10360 | }
|
10361 |
|
10362 | const playlists = this.mainPlaylistLoader_.main.playlists;
|
10363 | const mediaChanged = !this.media_ || this.media_ !== playlists[mediaID];
|
10364 |
|
10365 | if (mediaChanged) {
|
10366 | this.media_ = playlists[mediaID];
|
10367 | } else {
|
10368 | this.trigger('playlistunchanged');
|
10369 | }
|
10370 |
|
10371 | if (!this.mediaUpdateTimeout) {
|
10372 | const createMediaUpdateTimeout = () => {
|
10373 | if (this.media().endList) {
|
10374 | return;
|
10375 | }
|
10376 |
|
10377 | this.mediaUpdateTimeout = window.setTimeout(() => {
|
10378 | this.trigger('mediaupdatetimeout');
|
10379 | createMediaUpdateTimeout();
|
10380 | }, refreshDelay(this.media(), Boolean(mediaChanged)));
|
10381 | };
|
10382 |
|
10383 | createMediaUpdateTimeout();
|
10384 | }
|
10385 |
|
10386 | this.trigger('loadedplaylist');
|
10387 | }
|
10388 | |
10389 |
|
10390 |
|
10391 |
|
10392 |
|
10393 |
|
10394 |
|
10395 | addEventStreamToMetadataTrack_(newMain) {
|
10396 |
|
10397 | if (newMain && this.mainPlaylistLoader_.main.eventStream) {
|
10398 |
|
10399 | const metadataArray = this.mainPlaylistLoader_.main.eventStream.map(eventStreamNode => {
|
10400 | return {
|
10401 | cueTime: eventStreamNode.start,
|
10402 | frames: [{
|
10403 | data: eventStreamNode.messageData
|
10404 | }]
|
10405 | };
|
10406 | });
|
10407 | this.addMetadataToTextTrack('EventStream', metadataArray, this.mainPlaylistLoader_.main.duration);
|
10408 | }
|
10409 | }
|
10410 | |
10411 |
|
10412 |
|
10413 |
|
10414 |
|
10415 |
|
10416 |
|
10417 |
|
10418 | getKeyIdSet(playlist) {
|
10419 | if (playlist.contentProtection) {
|
10420 | const keyIds = new Set();
|
10421 |
|
10422 | for (const keysystem in playlist.contentProtection) {
|
10423 | const defaultKID = playlist.contentProtection[keysystem].attributes['cenc:default_KID'];
|
10424 |
|
10425 | if (defaultKID) {
|
10426 |
|
10427 | keyIds.add(defaultKID.replace(/-/g, '').toLowerCase());
|
10428 | }
|
10429 | }
|
10430 |
|
10431 | return keyIds;
|
10432 | }
|
10433 | }
|
10434 |
|
10435 | }
|
10436 |
|
10437 | var Config = {
|
10438 | GOAL_BUFFER_LENGTH: 30,
|
10439 | MAX_GOAL_BUFFER_LENGTH: 60,
|
10440 | BACK_BUFFER_LENGTH: 30,
|
10441 | GOAL_BUFFER_LENGTH_RATE: 1,
|
10442 |
|
10443 | INITIAL_BANDWIDTH: 4194304,
|
10444 |
|
10445 |
|
10446 | BANDWIDTH_VARIANCE: 1.2,
|
10447 |
|
10448 | BUFFER_LOW_WATER_LINE: 0,
|
10449 | MAX_BUFFER_LOW_WATER_LINE: 30,
|
10450 |
|
10451 | EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE: 16,
|
10452 | BUFFER_LOW_WATER_LINE_RATE: 1,
|
10453 |
|
10454 | BUFFER_HIGH_WATER_LINE: 30
|
10455 | };
|
10456 |
|
10457 | const stringToArrayBuffer = string => {
|
10458 | const view = new Uint8Array(new ArrayBuffer(string.length));
|
10459 |
|
10460 | for (let i = 0; i < string.length; i++) {
|
10461 | view[i] = string.charCodeAt(i);
|
10462 | }
|
10463 |
|
10464 | return view.buffer;
|
10465 | };
|
10466 |
|
10467 |
|
10468 |
|
10469 | const browserWorkerPolyFill = function (workerObj) {
|
10470 |
|
10471 | workerObj.on = workerObj.addEventListener;
|
10472 | workerObj.off = workerObj.removeEventListener;
|
10473 | return workerObj;
|
10474 | };
|
10475 |
|
10476 | const createObjectURL = function (str) {
|
10477 | try {
|
10478 | return URL.createObjectURL(new Blob([str], {
|
10479 | type: 'application/javascript'
|
10480 | }));
|
10481 | } catch (e) {
|
10482 | const blob = new BlobBuilder();
|
10483 | blob.append(str);
|
10484 | return URL.createObjectURL(blob.getBlob());
|
10485 | }
|
10486 | };
|
10487 |
|
10488 | const factory = function (code) {
|
10489 | return function () {
|
10490 | const objectUrl = createObjectURL(code);
|
10491 | const worker = browserWorkerPolyFill(new Worker(objectUrl));
|
10492 | worker.objURL = objectUrl;
|
10493 | const terminate = worker.terminate;
|
10494 | worker.on = worker.addEventListener;
|
10495 | worker.off = worker.removeEventListener;
|
10496 |
|
10497 | worker.terminate = function () {
|
10498 | URL.revokeObjectURL(objectUrl);
|
10499 | return terminate.call(this);
|
10500 | };
|
10501 |
|
10502 | return worker;
|
10503 | };
|
10504 | };
|
10505 | const transform = function (code) {
|
10506 | return `var browserWorkerPolyFill = ${browserWorkerPolyFill.toString()};\n` + 'browserWorkerPolyFill(self);\n' + code;
|
10507 | };
|
10508 |
|
10509 | const getWorkerString = function (fn) {
|
10510 | return fn.toString().replace(/^function.+?{/, '').slice(0, -1);
|
10511 | };
|
10512 |
|
10513 |
|
10514 | const workerCode$1 = transform(getWorkerString(function () {
|
10515 |
|
10516 | var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
10517 | |
10518 |
|
10519 |
|
10520 |
|
10521 |
|
10522 |
|
10523 |
|
10524 |
|
10525 |
|
10526 |
|
10527 | var Stream$8 = function () {
|
10528 | this.init = function () {
|
10529 | var listeners = {};
|
10530 | |
10531 |
|
10532 |
|
10533 |
|
10534 |
|
10535 |
|
10536 |
|
10537 | this.on = function (type, listener) {
|
10538 | if (!listeners[type]) {
|
10539 | listeners[type] = [];
|
10540 | }
|
10541 |
|
10542 | listeners[type] = listeners[type].concat(listener);
|
10543 | };
|
10544 | |
10545 |
|
10546 |
|
10547 |
|
10548 |
|
10549 |
|
10550 |
|
10551 |
|
10552 | this.off = function (type, listener) {
|
10553 | var index;
|
10554 |
|
10555 | if (!listeners[type]) {
|
10556 | return false;
|
10557 | }
|
10558 |
|
10559 | index = listeners[type].indexOf(listener);
|
10560 | listeners[type] = listeners[type].slice();
|
10561 | listeners[type].splice(index, 1);
|
10562 | return index > -1;
|
10563 | };
|
10564 | |
10565 |
|
10566 |
|
10567 |
|
10568 |
|
10569 |
|
10570 |
|
10571 | this.trigger = function (type) {
|
10572 | var callbacks, i, length, args;
|
10573 | callbacks = listeners[type];
|
10574 |
|
10575 | if (!callbacks) {
|
10576 | return;
|
10577 | }
|
10578 |
|
10579 |
|
10580 |
|
10581 |
|
10582 |
|
10583 | if (arguments.length === 2) {
|
10584 | length = callbacks.length;
|
10585 |
|
10586 | for (i = 0; i < length; ++i) {
|
10587 | callbacks[i].call(this, arguments[1]);
|
10588 | }
|
10589 | } else {
|
10590 | args = [];
|
10591 | i = arguments.length;
|
10592 |
|
10593 | for (i = 1; i < arguments.length; ++i) {
|
10594 | args.push(arguments[i]);
|
10595 | }
|
10596 |
|
10597 | length = callbacks.length;
|
10598 |
|
10599 | for (i = 0; i < length; ++i) {
|
10600 | callbacks[i].apply(this, args);
|
10601 | }
|
10602 | }
|
10603 | };
|
10604 | |
10605 |
|
10606 |
|
10607 |
|
10608 |
|
10609 | this.dispose = function () {
|
10610 | listeners = {};
|
10611 | };
|
10612 | };
|
10613 | };
|
10614 | |
10615 |
|
10616 |
|
10617 |
|
10618 |
|
10619 |
|
10620 |
|
10621 |
|
10622 |
|
10623 |
|
10624 |
|
10625 | Stream$8.prototype.pipe = function (destination) {
|
10626 | this.on('data', function (data) {
|
10627 | destination.push(data);
|
10628 | });
|
10629 | this.on('done', function (flushSource) {
|
10630 | destination.flush(flushSource);
|
10631 | });
|
10632 | this.on('partialdone', function (flushSource) {
|
10633 | destination.partialFlush(flushSource);
|
10634 | });
|
10635 | this.on('endedtimeline', function (flushSource) {
|
10636 | destination.endTimeline(flushSource);
|
10637 | });
|
10638 | this.on('reset', function (flushSource) {
|
10639 | destination.reset(flushSource);
|
10640 | });
|
10641 | return destination;
|
10642 | };
|
10643 |
|
10644 |
|
10645 |
|
10646 |
|
10647 |
|
10648 | Stream$8.prototype.push = function (data) {
|
10649 | this.trigger('data', data);
|
10650 | };
|
10651 |
|
10652 | Stream$8.prototype.flush = function (flushSource) {
|
10653 | this.trigger('done', flushSource);
|
10654 | };
|
10655 |
|
10656 | Stream$8.prototype.partialFlush = function (flushSource) {
|
10657 | this.trigger('partialdone', flushSource);
|
10658 | };
|
10659 |
|
10660 | Stream$8.prototype.endTimeline = function (flushSource) {
|
10661 | this.trigger('endedtimeline', flushSource);
|
10662 | };
|
10663 |
|
10664 | Stream$8.prototype.reset = function (flushSource) {
|
10665 | this.trigger('reset', flushSource);
|
10666 | };
|
10667 |
|
10668 | var stream = Stream$8;
|
10669 | var MAX_UINT32$1 = Math.pow(2, 32);
|
10670 |
|
10671 | var getUint64$3 = function (uint8) {
|
10672 | var dv = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
|
10673 | var value;
|
10674 |
|
10675 | if (dv.getBigUint64) {
|
10676 | value = dv.getBigUint64(0);
|
10677 |
|
10678 | if (value < Number.MAX_SAFE_INTEGER) {
|
10679 | return Number(value);
|
10680 | }
|
10681 |
|
10682 | return value;
|
10683 | }
|
10684 |
|
10685 | return dv.getUint32(0) * MAX_UINT32$1 + dv.getUint32(4);
|
10686 | };
|
10687 |
|
10688 | var numbers = {
|
10689 | getUint64: getUint64$3,
|
10690 | MAX_UINT32: MAX_UINT32$1
|
10691 | };
|
10692 | |
10693 |
|
10694 |
|
10695 |
|
10696 |
|
10697 |
|
10698 |
|
10699 |
|
10700 |
|
10701 |
|
10702 | var MAX_UINT32 = numbers.MAX_UINT32;
|
10703 | 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;
|
10704 |
|
10705 | (function () {
|
10706 | var i;
|
10707 | types = {
|
10708 | avc1: [],
|
10709 |
|
10710 | avcC: [],
|
10711 | btrt: [],
|
10712 | dinf: [],
|
10713 | dref: [],
|
10714 | esds: [],
|
10715 | ftyp: [],
|
10716 | hdlr: [],
|
10717 | mdat: [],
|
10718 | mdhd: [],
|
10719 | mdia: [],
|
10720 | mfhd: [],
|
10721 | minf: [],
|
10722 | moof: [],
|
10723 | moov: [],
|
10724 | mp4a: [],
|
10725 |
|
10726 | mvex: [],
|
10727 | mvhd: [],
|
10728 | pasp: [],
|
10729 | sdtp: [],
|
10730 | smhd: [],
|
10731 | stbl: [],
|
10732 | stco: [],
|
10733 | stsc: [],
|
10734 | stsd: [],
|
10735 | stsz: [],
|
10736 | stts: [],
|
10737 | styp: [],
|
10738 | tfdt: [],
|
10739 | tfhd: [],
|
10740 | traf: [],
|
10741 | trak: [],
|
10742 | trun: [],
|
10743 | trex: [],
|
10744 | tkhd: [],
|
10745 | vmhd: []
|
10746 | };
|
10747 |
|
10748 |
|
10749 | if (typeof Uint8Array === 'undefined') {
|
10750 | return;
|
10751 | }
|
10752 |
|
10753 | for (i in types) {
|
10754 | if (types.hasOwnProperty(i)) {
|
10755 | types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
|
10756 | }
|
10757 | }
|
10758 |
|
10759 | MAJOR_BRAND = new Uint8Array(['i'.charCodeAt(0), 's'.charCodeAt(0), 'o'.charCodeAt(0), 'm'.charCodeAt(0)]);
|
10760 | AVC1_BRAND = new Uint8Array(['a'.charCodeAt(0), 'v'.charCodeAt(0), 'c'.charCodeAt(0), '1'.charCodeAt(0)]);
|
10761 | MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
|
10762 | VIDEO_HDLR = new Uint8Array([0x00,
|
10763 | 0x00, 0x00, 0x00,
|
10764 | 0x00, 0x00, 0x00, 0x00,
|
10765 | 0x76, 0x69, 0x64, 0x65,
|
10766 | 0x00, 0x00, 0x00, 0x00,
|
10767 | 0x00, 0x00, 0x00, 0x00,
|
10768 | 0x00, 0x00, 0x00, 0x00,
|
10769 | 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00
|
10770 | ]);
|
10771 | AUDIO_HDLR = new Uint8Array([0x00,
|
10772 | 0x00, 0x00, 0x00,
|
10773 | 0x00, 0x00, 0x00, 0x00,
|
10774 | 0x73, 0x6f, 0x75, 0x6e,
|
10775 | 0x00, 0x00, 0x00, 0x00,
|
10776 | 0x00, 0x00, 0x00, 0x00,
|
10777 | 0x00, 0x00, 0x00, 0x00,
|
10778 | 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00
|
10779 | ]);
|
10780 | HDLR_TYPES = {
|
10781 | video: VIDEO_HDLR,
|
10782 | audio: AUDIO_HDLR
|
10783 | };
|
10784 | DREF = new Uint8Array([0x00,
|
10785 | 0x00, 0x00, 0x00,
|
10786 | 0x00, 0x00, 0x00, 0x01,
|
10787 | 0x00, 0x00, 0x00, 0x0c,
|
10788 | 0x75, 0x72, 0x6c, 0x20,
|
10789 | 0x00,
|
10790 | 0x00, 0x00, 0x01
|
10791 | ]);
|
10792 | SMHD = new Uint8Array([0x00,
|
10793 | 0x00, 0x00, 0x00,
|
10794 | 0x00, 0x00,
|
10795 | 0x00, 0x00
|
10796 | ]);
|
10797 | STCO = new Uint8Array([0x00,
|
10798 | 0x00, 0x00, 0x00,
|
10799 | 0x00, 0x00, 0x00, 0x00
|
10800 | ]);
|
10801 | STSC = STCO;
|
10802 | STSZ = new Uint8Array([0x00,
|
10803 | 0x00, 0x00, 0x00,
|
10804 | 0x00, 0x00, 0x00, 0x00,
|
10805 | 0x00, 0x00, 0x00, 0x00
|
10806 | ]);
|
10807 | STTS = STCO;
|
10808 | VMHD = new Uint8Array([0x00,
|
10809 | 0x00, 0x00, 0x01,
|
10810 | 0x00, 0x00,
|
10811 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
10812 | ]);
|
10813 | })();
|
10814 |
|
10815 | box = function (type) {
|
10816 | var payload = [],
|
10817 | size = 0,
|
10818 | i,
|
10819 | result,
|
10820 | view;
|
10821 |
|
10822 | for (i = 1; i < arguments.length; i++) {
|
10823 | payload.push(arguments[i]);
|
10824 | }
|
10825 |
|
10826 | i = payload.length;
|
10827 |
|
10828 | while (i--) {
|
10829 | size += payload[i].byteLength;
|
10830 | }
|
10831 |
|
10832 | result = new Uint8Array(size + 8);
|
10833 | view = new DataView(result.buffer, result.byteOffset, result.byteLength);
|
10834 | view.setUint32(0, result.byteLength);
|
10835 | result.set(type, 4);
|
10836 |
|
10837 | for (i = 0, size = 8; i < payload.length; i++) {
|
10838 | result.set(payload[i], size);
|
10839 | size += payload[i].byteLength;
|
10840 | }
|
10841 |
|
10842 | return result;
|
10843 | };
|
10844 |
|
10845 | dinf = function () {
|
10846 | return box(types.dinf, box(types.dref, DREF));
|
10847 | };
|
10848 |
|
10849 | esds = function (track) {
|
10850 | return box(types.esds, new Uint8Array([0x00,
|
10851 | 0x00, 0x00, 0x00,
|
10852 |
|
10853 | 0x03,
|
10854 | 0x19,
|
10855 | 0x00, 0x00,
|
10856 | 0x00,
|
10857 |
|
10858 | 0x04,
|
10859 | 0x11,
|
10860 | 0x40,
|
10861 | 0x15,
|
10862 | 0x00, 0x06, 0x00,
|
10863 | 0x00, 0x00, 0xda, 0xc0,
|
10864 | 0x00, 0x00, 0xda, 0xc0,
|
10865 |
|
10866 | 0x05,
|
10867 | 0x02,
|
10868 |
|
10869 |
|
10870 | track.audioobjecttype << 3 | track.samplingfrequencyindex >>> 1, track.samplingfrequencyindex << 7 | track.channelcount << 3, 0x06, 0x01, 0x02
|
10871 | ]));
|
10872 | };
|
10873 |
|
10874 | ftyp = function () {
|
10875 | return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
|
10876 | };
|
10877 |
|
10878 | hdlr = function (type) {
|
10879 | return box(types.hdlr, HDLR_TYPES[type]);
|
10880 | };
|
10881 |
|
10882 | mdat = function (data) {
|
10883 | return box(types.mdat, data);
|
10884 | };
|
10885 |
|
10886 | mdhd = function (track) {
|
10887 | var result = new Uint8Array([0x00,
|
10888 | 0x00, 0x00, 0x00,
|
10889 | 0x00, 0x00, 0x00, 0x02,
|
10890 | 0x00, 0x00, 0x00, 0x03,
|
10891 | 0x00, 0x01, 0x5f, 0x90,
|
10892 | track.duration >>> 24 & 0xFF, track.duration >>> 16 & 0xFF, track.duration >>> 8 & 0xFF, track.duration & 0xFF,
|
10893 | 0x55, 0xc4,
|
10894 | 0x00, 0x00]);
|
10895 |
|
10896 |
|
10897 |
|
10898 | if (track.samplerate) {
|
10899 | result[12] = track.samplerate >>> 24 & 0xFF;
|
10900 | result[13] = track.samplerate >>> 16 & 0xFF;
|
10901 | result[14] = track.samplerate >>> 8 & 0xFF;
|
10902 | result[15] = track.samplerate & 0xFF;
|
10903 | }
|
10904 |
|
10905 | return box(types.mdhd, result);
|
10906 | };
|
10907 |
|
10908 | mdia = function (track) {
|
10909 | return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
|
10910 | };
|
10911 |
|
10912 | mfhd = function (sequenceNumber) {
|
10913 | return box(types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
|
10914 | (sequenceNumber & 0xFF000000) >> 24, (sequenceNumber & 0xFF0000) >> 16, (sequenceNumber & 0xFF00) >> 8, sequenceNumber & 0xFF
|
10915 | ]));
|
10916 | };
|
10917 |
|
10918 | minf = function (track) {
|
10919 | return box(types.minf, track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD), dinf(), stbl(track));
|
10920 | };
|
10921 |
|
10922 | moof = function (sequenceNumber, tracks) {
|
10923 | var trackFragments = [],
|
10924 | i = tracks.length;
|
10925 |
|
10926 | while (i--) {
|
10927 | trackFragments[i] = traf(tracks[i]);
|
10928 | }
|
10929 |
|
10930 | return box.apply(null, [types.moof, mfhd(sequenceNumber)].concat(trackFragments));
|
10931 | };
|
10932 | |
10933 |
|
10934 |
|
10935 |
|
10936 |
|
10937 |
|
10938 |
|
10939 | moov = function (tracks) {
|
10940 | var i = tracks.length,
|
10941 | boxes = [];
|
10942 |
|
10943 | while (i--) {
|
10944 | boxes[i] = trak(tracks[i]);
|
10945 | }
|
10946 |
|
10947 | return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
|
10948 | };
|
10949 |
|
10950 | mvex = function (tracks) {
|
10951 | var i = tracks.length,
|
10952 | boxes = [];
|
10953 |
|
10954 | while (i--) {
|
10955 | boxes[i] = trex(tracks[i]);
|
10956 | }
|
10957 |
|
10958 | return box.apply(null, [types.mvex].concat(boxes));
|
10959 | };
|
10960 |
|
10961 | mvhd = function (duration) {
|
10962 | var bytes = new Uint8Array([0x00,
|
10963 | 0x00, 0x00, 0x00,
|
10964 | 0x00, 0x00, 0x00, 0x01,
|
10965 | 0x00, 0x00, 0x00, 0x02,
|
10966 | 0x00, 0x01, 0x5f, 0x90,
|
10967 | (duration & 0xFF000000) >> 24, (duration & 0xFF0000) >> 16, (duration & 0xFF00) >> 8, duration & 0xFF,
|
10968 | 0x00, 0x01, 0x00, 0x00,
|
10969 | 0x01, 0x00,
|
10970 | 0x00, 0x00,
|
10971 | 0x00, 0x00, 0x00, 0x00,
|
10972 | 0x00, 0x00, 0x00, 0x00,
|
10973 | 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,
|
10974 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
10975 | 0xff, 0xff, 0xff, 0xff
|
10976 | ]);
|
10977 | return box(types.mvhd, bytes);
|
10978 | };
|
10979 |
|
10980 | sdtp = function (track) {
|
10981 | var samples = track.samples || [],
|
10982 | bytes = new Uint8Array(4 + samples.length),
|
10983 | flags,
|
10984 | i;
|
10985 |
|
10986 |
|
10987 | for (i = 0; i < samples.length; i++) {
|
10988 | flags = samples[i].flags;
|
10989 | bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
|
10990 | }
|
10991 |
|
10992 | return box(types.sdtp, bytes);
|
10993 | };
|
10994 |
|
10995 | stbl = function (track) {
|
10996 | return box(types.stbl, stsd(track), box(types.stts, STTS), box(types.stsc, STSC), box(types.stsz, STSZ), box(types.stco, STCO));
|
10997 | };
|
10998 |
|
10999 | (function () {
|
11000 | var videoSample, audioSample;
|
11001 |
|
11002 | stsd = function (track) {
|
11003 | return box(types.stsd, new Uint8Array([0x00,
|
11004 | 0x00, 0x00, 0x00,
|
11005 | 0x00, 0x00, 0x00, 0x01]), track.type === 'video' ? videoSample(track) : audioSample(track));
|
11006 | };
|
11007 |
|
11008 | videoSample = function (track) {
|
11009 | var sps = track.sps || [],
|
11010 | pps = track.pps || [],
|
11011 | sequenceParameterSets = [],
|
11012 | pictureParameterSets = [],
|
11013 | i,
|
11014 | avc1Box;
|
11015 |
|
11016 | for (i = 0; i < sps.length; i++) {
|
11017 | sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
|
11018 | sequenceParameterSets.push(sps[i].byteLength & 0xFF);
|
11019 |
|
11020 | sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i]));
|
11021 | }
|
11022 |
|
11023 |
|
11024 | for (i = 0; i < pps.length; i++) {
|
11025 | pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
|
11026 | pictureParameterSets.push(pps[i].byteLength & 0xFF);
|
11027 | pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
|
11028 | }
|
11029 |
|
11030 | avc1Box = [types.avc1, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
11031 | 0x00, 0x01,
|
11032 | 0x00, 0x00,
|
11033 | 0x00, 0x00,
|
11034 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
11035 | (track.width & 0xff00) >> 8, track.width & 0xff,
|
11036 | (track.height & 0xff00) >> 8, track.height & 0xff,
|
11037 | 0x00, 0x48, 0x00, 0x00,
|
11038 | 0x00, 0x48, 0x00, 0x00,
|
11039 | 0x00, 0x00, 0x00, 0x00,
|
11040 | 0x00, 0x01,
|
11041 | 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,
|
11042 | 0x00, 0x18,
|
11043 | 0x11, 0x11
|
11044 | ]), box(types.avcC, new Uint8Array([0x01,
|
11045 | track.profileIdc,
|
11046 | track.profileCompatibility,
|
11047 | track.levelIdc,
|
11048 | 0xff
|
11049 | ].concat([sps.length],
|
11050 | sequenceParameterSets,
|
11051 | [pps.length],
|
11052 | pictureParameterSets
|
11053 | ))), box(types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
|
11054 | 0x00, 0x2d, 0xc6, 0xc0,
|
11055 | 0x00, 0x2d, 0xc6, 0xc0
|
11056 | ]))];
|
11057 |
|
11058 | if (track.sarRatio) {
|
11059 | var hSpacing = track.sarRatio[0],
|
11060 | vSpacing = track.sarRatio[1];
|
11061 | 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])));
|
11062 | }
|
11063 |
|
11064 | return box.apply(null, avc1Box);
|
11065 | };
|
11066 |
|
11067 | audioSample = function (track) {
|
11068 | return box(types.mp4a, new Uint8Array([
|
11069 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
11070 | 0x00, 0x01,
|
11071 |
|
11072 | 0x00, 0x00, 0x00, 0x00,
|
11073 | 0x00, 0x00, 0x00, 0x00,
|
11074 | (track.channelcount & 0xff00) >> 8, track.channelcount & 0xff,
|
11075 | (track.samplesize & 0xff00) >> 8, track.samplesize & 0xff,
|
11076 | 0x00, 0x00,
|
11077 | 0x00, 0x00,
|
11078 | (track.samplerate & 0xff00) >> 8, track.samplerate & 0xff, 0x00, 0x00
|
11079 |
|
11080 | ]), esds(track));
|
11081 | };
|
11082 | })();
|
11083 |
|
11084 | tkhd = function (track) {
|
11085 | var result = new Uint8Array([0x00,
|
11086 | 0x00, 0x00, 0x07,
|
11087 | 0x00, 0x00, 0x00, 0x00,
|
11088 | 0x00, 0x00, 0x00, 0x00,
|
11089 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
11090 | 0x00, 0x00, 0x00, 0x00,
|
11091 | (track.duration & 0xFF000000) >> 24, (track.duration & 0xFF0000) >> 16, (track.duration & 0xFF00) >> 8, track.duration & 0xFF,
|
11092 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
11093 | 0x00, 0x00,
|
11094 | 0x00, 0x00,
|
11095 | 0x01, 0x00,
|
11096 | 0x00, 0x00,
|
11097 | 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,
|
11098 | (track.width & 0xFF00) >> 8, track.width & 0xFF, 0x00, 0x00,
|
11099 | (track.height & 0xFF00) >> 8, track.height & 0xFF, 0x00, 0x00
|
11100 | ]);
|
11101 | return box(types.tkhd, result);
|
11102 | };
|
11103 | |
11104 |
|
11105 |
|
11106 |
|
11107 |
|
11108 |
|
11109 | traf = function (track) {
|
11110 | var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable, dataOffset, upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
|
11111 | trackFragmentHeader = box(types.tfhd, new Uint8Array([0x00,
|
11112 | 0x00, 0x00, 0x3a,
|
11113 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
11114 | 0x00, 0x00, 0x00, 0x01,
|
11115 | 0x00, 0x00, 0x00, 0x00,
|
11116 | 0x00, 0x00, 0x00, 0x00,
|
11117 | 0x00, 0x00, 0x00, 0x00
|
11118 | ]));
|
11119 | upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / MAX_UINT32);
|
11120 | lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % MAX_UINT32);
|
11121 | trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([0x01,
|
11122 | 0x00, 0x00, 0x00,
|
11123 |
|
11124 | upperWordBaseMediaDecodeTime >>> 24 & 0xFF, upperWordBaseMediaDecodeTime >>> 16 & 0xFF, upperWordBaseMediaDecodeTime >>> 8 & 0xFF, upperWordBaseMediaDecodeTime & 0xFF, lowerWordBaseMediaDecodeTime >>> 24 & 0xFF, lowerWordBaseMediaDecodeTime >>> 16 & 0xFF, lowerWordBaseMediaDecodeTime >>> 8 & 0xFF, lowerWordBaseMediaDecodeTime & 0xFF]));
|
11125 |
|
11126 |
|
11127 |
|
11128 | dataOffset = 32 +
|
11129 | 20 +
|
11130 | 8 +
|
11131 | 16 +
|
11132 | 8 +
|
11133 | 8;
|
11134 |
|
11135 |
|
11136 | if (track.type === 'audio') {
|
11137 | trackFragmentRun = trun$1(track, dataOffset);
|
11138 | return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun);
|
11139 | }
|
11140 |
|
11141 |
|
11142 |
|
11143 |
|
11144 | sampleDependencyTable = sdtp(track);
|
11145 | trackFragmentRun = trun$1(track, sampleDependencyTable.length + dataOffset);
|
11146 | return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable);
|
11147 | };
|
11148 | |
11149 |
|
11150 |
|
11151 |
|
11152 |
|
11153 |
|
11154 |
|
11155 | trak = function (track) {
|
11156 | track.duration = track.duration || 0xffffffff;
|
11157 | return box(types.trak, tkhd(track), mdia(track));
|
11158 | };
|
11159 |
|
11160 | trex = function (track) {
|
11161 | var result = new Uint8Array([0x00,
|
11162 | 0x00, 0x00, 0x00,
|
11163 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
11164 | 0x00, 0x00, 0x00, 0x01,
|
11165 | 0x00, 0x00, 0x00, 0x00,
|
11166 | 0x00, 0x00, 0x00, 0x00,
|
11167 | 0x00, 0x01, 0x00, 0x01
|
11168 | ]);
|
11169 |
|
11170 |
|
11171 |
|
11172 |
|
11173 | if (track.type !== 'video') {
|
11174 | result[result.length - 1] = 0x00;
|
11175 | }
|
11176 |
|
11177 | return box(types.trex, result);
|
11178 | };
|
11179 |
|
11180 | (function () {
|
11181 | var audioTrun, videoTrun, trunHeader;
|
11182 |
|
11183 |
|
11184 |
|
11185 |
|
11186 | trunHeader = function (samples, offset) {
|
11187 | var durationPresent = 0,
|
11188 | sizePresent = 0,
|
11189 | flagsPresent = 0,
|
11190 | compositionTimeOffset = 0;
|
11191 |
|
11192 | if (samples.length) {
|
11193 | if (samples[0].duration !== undefined) {
|
11194 | durationPresent = 0x1;
|
11195 | }
|
11196 |
|
11197 | if (samples[0].size !== undefined) {
|
11198 | sizePresent = 0x2;
|
11199 | }
|
11200 |
|
11201 | if (samples[0].flags !== undefined) {
|
11202 | flagsPresent = 0x4;
|
11203 | }
|
11204 |
|
11205 | if (samples[0].compositionTimeOffset !== undefined) {
|
11206 | compositionTimeOffset = 0x8;
|
11207 | }
|
11208 | }
|
11209 |
|
11210 | return [0x00,
|
11211 | 0x00, durationPresent | sizePresent | flagsPresent | compositionTimeOffset, 0x01,
|
11212 | (samples.length & 0xFF000000) >>> 24, (samples.length & 0xFF0000) >>> 16, (samples.length & 0xFF00) >>> 8, samples.length & 0xFF,
|
11213 | (offset & 0xFF000000) >>> 24, (offset & 0xFF0000) >>> 16, (offset & 0xFF00) >>> 8, offset & 0xFF
|
11214 | ];
|
11215 | };
|
11216 |
|
11217 | videoTrun = function (track, offset) {
|
11218 | var bytesOffest, bytes, header, samples, sample, i;
|
11219 | samples = track.samples || [];
|
11220 | offset += 8 + 12 + 16 * samples.length;
|
11221 | header = trunHeader(samples, offset);
|
11222 | bytes = new Uint8Array(header.length + samples.length * 16);
|
11223 | bytes.set(header);
|
11224 | bytesOffest = header.length;
|
11225 |
|
11226 | for (i = 0; i < samples.length; i++) {
|
11227 | sample = samples[i];
|
11228 | bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
|
11229 | bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
|
11230 | bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
|
11231 | bytes[bytesOffest++] = sample.duration & 0xFF;
|
11232 |
|
11233 | bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
|
11234 | bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
|
11235 | bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
|
11236 | bytes[bytesOffest++] = sample.size & 0xFF;
|
11237 |
|
11238 | bytes[bytesOffest++] = sample.flags.isLeading << 2 | sample.flags.dependsOn;
|
11239 | bytes[bytesOffest++] = sample.flags.isDependedOn << 6 | sample.flags.hasRedundancy << 4 | sample.flags.paddingValue << 1 | sample.flags.isNonSyncSample;
|
11240 | bytes[bytesOffest++] = sample.flags.degradationPriority & 0xF0 << 8;
|
11241 | bytes[bytesOffest++] = sample.flags.degradationPriority & 0x0F;
|
11242 |
|
11243 | bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF000000) >>> 24;
|
11244 | bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF0000) >>> 16;
|
11245 | bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF00) >>> 8;
|
11246 | bytes[bytesOffest++] = sample.compositionTimeOffset & 0xFF;
|
11247 | }
|
11248 |
|
11249 | return box(types.trun, bytes);
|
11250 | };
|
11251 |
|
11252 | audioTrun = function (track, offset) {
|
11253 | var bytes, bytesOffest, header, samples, sample, i;
|
11254 | samples = track.samples || [];
|
11255 | offset += 8 + 12 + 8 * samples.length;
|
11256 | header = trunHeader(samples, offset);
|
11257 | bytes = new Uint8Array(header.length + samples.length * 8);
|
11258 | bytes.set(header);
|
11259 | bytesOffest = header.length;
|
11260 |
|
11261 | for (i = 0; i < samples.length; i++) {
|
11262 | sample = samples[i];
|
11263 | bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
|
11264 | bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
|
11265 | bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
|
11266 | bytes[bytesOffest++] = sample.duration & 0xFF;
|
11267 |
|
11268 | bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
|
11269 | bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
|
11270 | bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
|
11271 | bytes[bytesOffest++] = sample.size & 0xFF;
|
11272 | }
|
11273 |
|
11274 | return box(types.trun, bytes);
|
11275 | };
|
11276 |
|
11277 | trun$1 = function (track, offset) {
|
11278 | if (track.type === 'audio') {
|
11279 | return audioTrun(track, offset);
|
11280 | }
|
11281 |
|
11282 | return videoTrun(track, offset);
|
11283 | };
|
11284 | })();
|
11285 |
|
11286 | var mp4Generator = {
|
11287 | ftyp: ftyp,
|
11288 | mdat: mdat,
|
11289 | moof: moof,
|
11290 | moov: moov,
|
11291 | initSegment: function (tracks) {
|
11292 | var fileType = ftyp(),
|
11293 | movie = moov(tracks),
|
11294 | result;
|
11295 | result = new Uint8Array(fileType.byteLength + movie.byteLength);
|
11296 | result.set(fileType);
|
11297 | result.set(movie, fileType.byteLength);
|
11298 | return result;
|
11299 | }
|
11300 | };
|
11301 | |
11302 |
|
11303 |
|
11304 |
|
11305 |
|
11306 |
|
11307 |
|
11308 |
|
11309 |
|
11310 |
|
11311 | var groupNalsIntoFrames = function (nalUnits) {
|
11312 | var i,
|
11313 | currentNal,
|
11314 | currentFrame = [],
|
11315 | frames = [];
|
11316 |
|
11317 | frames.byteLength = 0;
|
11318 | frames.nalCount = 0;
|
11319 | frames.duration = 0;
|
11320 | currentFrame.byteLength = 0;
|
11321 |
|
11322 | for (i = 0; i < nalUnits.length; i++) {
|
11323 | currentNal = nalUnits[i];
|
11324 |
|
11325 | if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
|
11326 |
|
11327 |
|
11328 | if (currentFrame.length) {
|
11329 | currentFrame.duration = currentNal.dts - currentFrame.dts;
|
11330 |
|
11331 | frames.byteLength += currentFrame.byteLength;
|
11332 | frames.nalCount += currentFrame.length;
|
11333 | frames.duration += currentFrame.duration;
|
11334 | frames.push(currentFrame);
|
11335 | }
|
11336 |
|
11337 | currentFrame = [currentNal];
|
11338 | currentFrame.byteLength = currentNal.data.byteLength;
|
11339 | currentFrame.pts = currentNal.pts;
|
11340 | currentFrame.dts = currentNal.dts;
|
11341 | } else {
|
11342 |
|
11343 | if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
|
11344 | currentFrame.keyFrame = true;
|
11345 | }
|
11346 |
|
11347 | currentFrame.duration = currentNal.dts - currentFrame.dts;
|
11348 | currentFrame.byteLength += currentNal.data.byteLength;
|
11349 | currentFrame.push(currentNal);
|
11350 | }
|
11351 | }
|
11352 |
|
11353 |
|
11354 |
|
11355 | if (frames.length && (!currentFrame.duration || currentFrame.duration <= 0)) {
|
11356 | currentFrame.duration = frames[frames.length - 1].duration;
|
11357 | }
|
11358 |
|
11359 |
|
11360 |
|
11361 | frames.byteLength += currentFrame.byteLength;
|
11362 | frames.nalCount += currentFrame.length;
|
11363 | frames.duration += currentFrame.duration;
|
11364 | frames.push(currentFrame);
|
11365 | return frames;
|
11366 | };
|
11367 |
|
11368 |
|
11369 |
|
11370 |
|
11371 |
|
11372 | var groupFramesIntoGops = function (frames) {
|
11373 | var i,
|
11374 | currentFrame,
|
11375 | currentGop = [],
|
11376 | gops = [];
|
11377 |
|
11378 |
|
11379 | currentGop.byteLength = 0;
|
11380 | currentGop.nalCount = 0;
|
11381 | currentGop.duration = 0;
|
11382 | currentGop.pts = frames[0].pts;
|
11383 | currentGop.dts = frames[0].dts;
|
11384 |
|
11385 | gops.byteLength = 0;
|
11386 | gops.nalCount = 0;
|
11387 | gops.duration = 0;
|
11388 | gops.pts = frames[0].pts;
|
11389 | gops.dts = frames[0].dts;
|
11390 |
|
11391 | for (i = 0; i < frames.length; i++) {
|
11392 | currentFrame = frames[i];
|
11393 |
|
11394 | if (currentFrame.keyFrame) {
|
11395 |
|
11396 |
|
11397 | if (currentGop.length) {
|
11398 | gops.push(currentGop);
|
11399 | gops.byteLength += currentGop.byteLength;
|
11400 | gops.nalCount += currentGop.nalCount;
|
11401 | gops.duration += currentGop.duration;
|
11402 | }
|
11403 |
|
11404 | currentGop = [currentFrame];
|
11405 | currentGop.nalCount = currentFrame.length;
|
11406 | currentGop.byteLength = currentFrame.byteLength;
|
11407 | currentGop.pts = currentFrame.pts;
|
11408 | currentGop.dts = currentFrame.dts;
|
11409 | currentGop.duration = currentFrame.duration;
|
11410 | } else {
|
11411 | currentGop.duration += currentFrame.duration;
|
11412 | currentGop.nalCount += currentFrame.length;
|
11413 | currentGop.byteLength += currentFrame.byteLength;
|
11414 | currentGop.push(currentFrame);
|
11415 | }
|
11416 | }
|
11417 |
|
11418 | if (gops.length && currentGop.duration <= 0) {
|
11419 | currentGop.duration = gops[gops.length - 1].duration;
|
11420 | }
|
11421 |
|
11422 | gops.byteLength += currentGop.byteLength;
|
11423 | gops.nalCount += currentGop.nalCount;
|
11424 | gops.duration += currentGop.duration;
|
11425 |
|
11426 | gops.push(currentGop);
|
11427 | return gops;
|
11428 | };
|
11429 | |
11430 |
|
11431 |
|
11432 |
|
11433 |
|
11434 |
|
11435 |
|
11436 |
|
11437 |
|
11438 |
|
11439 |
|
11440 | var extendFirstKeyFrame = function (gops) {
|
11441 | var currentGop;
|
11442 |
|
11443 | if (!gops[0][0].keyFrame && gops.length > 1) {
|
11444 |
|
11445 | currentGop = gops.shift();
|
11446 | gops.byteLength -= currentGop.byteLength;
|
11447 | gops.nalCount -= currentGop.nalCount;
|
11448 |
|
11449 |
|
11450 |
|
11451 | gops[0][0].dts = currentGop.dts;
|
11452 | gops[0][0].pts = currentGop.pts;
|
11453 | gops[0][0].duration += currentGop.duration;
|
11454 | }
|
11455 |
|
11456 | return gops;
|
11457 | };
|
11458 | |
11459 |
|
11460 |
|
11461 |
|
11462 |
|
11463 |
|
11464 | var createDefaultSample = function () {
|
11465 | return {
|
11466 | size: 0,
|
11467 | flags: {
|
11468 | isLeading: 0,
|
11469 | dependsOn: 1,
|
11470 | isDependedOn: 0,
|
11471 | hasRedundancy: 0,
|
11472 | degradationPriority: 0,
|
11473 | isNonSyncSample: 1
|
11474 | }
|
11475 | };
|
11476 | };
|
11477 | |
11478 |
|
11479 |
|
11480 |
|
11481 |
|
11482 |
|
11483 |
|
11484 |
|
11485 |
|
11486 |
|
11487 | var sampleForFrame = function (frame, dataOffset) {
|
11488 | var sample = createDefaultSample();
|
11489 | sample.dataOffset = dataOffset;
|
11490 | sample.compositionTimeOffset = frame.pts - frame.dts;
|
11491 | sample.duration = frame.duration;
|
11492 | sample.size = 4 * frame.length;
|
11493 |
|
11494 | sample.size += frame.byteLength;
|
11495 |
|
11496 | if (frame.keyFrame) {
|
11497 | sample.flags.dependsOn = 2;
|
11498 | sample.flags.isNonSyncSample = 0;
|
11499 | }
|
11500 |
|
11501 | return sample;
|
11502 | };
|
11503 |
|
11504 |
|
11505 | var generateSampleTable$1 = function (gops, baseDataOffset) {
|
11506 | var h,
|
11507 | i,
|
11508 | sample,
|
11509 | currentGop,
|
11510 | currentFrame,
|
11511 | dataOffset = baseDataOffset || 0,
|
11512 | samples = [];
|
11513 |
|
11514 | for (h = 0; h < gops.length; h++) {
|
11515 | currentGop = gops[h];
|
11516 |
|
11517 | for (i = 0; i < currentGop.length; i++) {
|
11518 | currentFrame = currentGop[i];
|
11519 | sample = sampleForFrame(currentFrame, dataOffset);
|
11520 | dataOffset += sample.size;
|
11521 | samples.push(sample);
|
11522 | }
|
11523 | }
|
11524 |
|
11525 | return samples;
|
11526 | };
|
11527 |
|
11528 |
|
11529 | var concatenateNalData = function (gops) {
|
11530 | var h,
|
11531 | i,
|
11532 | j,
|
11533 | currentGop,
|
11534 | currentFrame,
|
11535 | currentNal,
|
11536 | dataOffset = 0,
|
11537 | nalsByteLength = gops.byteLength,
|
11538 | numberOfNals = gops.nalCount,
|
11539 | totalByteLength = nalsByteLength + 4 * numberOfNals,
|
11540 | data = new Uint8Array(totalByteLength),
|
11541 | view = new DataView(data.buffer);
|
11542 |
|
11543 | for (h = 0; h < gops.length; h++) {
|
11544 | currentGop = gops[h];
|
11545 |
|
11546 | for (i = 0; i < currentGop.length; i++) {
|
11547 | currentFrame = currentGop[i];
|
11548 |
|
11549 | for (j = 0; j < currentFrame.length; j++) {
|
11550 | currentNal = currentFrame[j];
|
11551 | view.setUint32(dataOffset, currentNal.data.byteLength);
|
11552 | dataOffset += 4;
|
11553 | data.set(currentNal.data, dataOffset);
|
11554 | dataOffset += currentNal.data.byteLength;
|
11555 | }
|
11556 | }
|
11557 | }
|
11558 |
|
11559 | return data;
|
11560 | };
|
11561 |
|
11562 |
|
11563 | var generateSampleTableForFrame = function (frame, baseDataOffset) {
|
11564 | var sample,
|
11565 | dataOffset = baseDataOffset || 0,
|
11566 | samples = [];
|
11567 | sample = sampleForFrame(frame, dataOffset);
|
11568 | samples.push(sample);
|
11569 | return samples;
|
11570 | };
|
11571 |
|
11572 |
|
11573 | var concatenateNalDataForFrame = function (frame) {
|
11574 | var i,
|
11575 | currentNal,
|
11576 | dataOffset = 0,
|
11577 | nalsByteLength = frame.byteLength,
|
11578 | numberOfNals = frame.length,
|
11579 | totalByteLength = nalsByteLength + 4 * numberOfNals,
|
11580 | data = new Uint8Array(totalByteLength),
|
11581 | view = new DataView(data.buffer);
|
11582 |
|
11583 | for (i = 0; i < frame.length; i++) {
|
11584 | currentNal = frame[i];
|
11585 | view.setUint32(dataOffset, currentNal.data.byteLength);
|
11586 | dataOffset += 4;
|
11587 | data.set(currentNal.data, dataOffset);
|
11588 | dataOffset += currentNal.data.byteLength;
|
11589 | }
|
11590 |
|
11591 | return data;
|
11592 | };
|
11593 |
|
11594 | var frameUtils$1 = {
|
11595 | groupNalsIntoFrames: groupNalsIntoFrames,
|
11596 | groupFramesIntoGops: groupFramesIntoGops,
|
11597 | extendFirstKeyFrame: extendFirstKeyFrame,
|
11598 | generateSampleTable: generateSampleTable$1,
|
11599 | concatenateNalData: concatenateNalData,
|
11600 | generateSampleTableForFrame: generateSampleTableForFrame,
|
11601 | concatenateNalDataForFrame: concatenateNalDataForFrame
|
11602 | };
|
11603 | |
11604 |
|
11605 |
|
11606 |
|
11607 |
|
11608 |
|
11609 |
|
11610 | var highPrefix = [33, 16, 5, 32, 164, 27];
|
11611 | var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
|
11612 |
|
11613 | var zeroFill = function (count) {
|
11614 | var a = [];
|
11615 |
|
11616 | while (count--) {
|
11617 | a.push(0);
|
11618 | }
|
11619 |
|
11620 | return a;
|
11621 | };
|
11622 |
|
11623 | var makeTable = function (metaTable) {
|
11624 | return Object.keys(metaTable).reduce(function (obj, key) {
|
11625 | obj[key] = new Uint8Array(metaTable[key].reduce(function (arr, part) {
|
11626 | return arr.concat(part);
|
11627 | }, []));
|
11628 | return obj;
|
11629 | }, {});
|
11630 | };
|
11631 |
|
11632 | var silence;
|
11633 |
|
11634 | var silence_1 = function () {
|
11635 | if (!silence) {
|
11636 |
|
11637 | var coneOfSilence = {
|
11638 | 96000: [highPrefix, [227, 64], zeroFill(154), [56]],
|
11639 | 88200: [highPrefix, [231], zeroFill(170), [56]],
|
11640 | 64000: [highPrefix, [248, 192], zeroFill(240), [56]],
|
11641 | 48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
|
11642 | 44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
|
11643 | 32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
|
11644 | 24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
|
11645 | 16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
|
11646 | 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]],
|
11647 | 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]],
|
11648 | 8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
|
11649 | };
|
11650 | silence = makeTable(coneOfSilence);
|
11651 | }
|
11652 |
|
11653 | return silence;
|
11654 | };
|
11655 | |
11656 |
|
11657 |
|
11658 |
|
11659 |
|
11660 |
|
11661 |
|
11662 |
|
11663 | var ONE_SECOND_IN_TS$4 = 90000,
|
11664 |
|
11665 | secondsToVideoTs,
|
11666 | secondsToAudioTs,
|
11667 | videoTsToSeconds,
|
11668 | audioTsToSeconds,
|
11669 | audioTsToVideoTs,
|
11670 | videoTsToAudioTs,
|
11671 | metadataTsToSeconds;
|
11672 |
|
11673 | secondsToVideoTs = function (seconds) {
|
11674 | return seconds * ONE_SECOND_IN_TS$4;
|
11675 | };
|
11676 |
|
11677 | secondsToAudioTs = function (seconds, sampleRate) {
|
11678 | return seconds * sampleRate;
|
11679 | };
|
11680 |
|
11681 | videoTsToSeconds = function (timestamp) {
|
11682 | return timestamp / ONE_SECOND_IN_TS$4;
|
11683 | };
|
11684 |
|
11685 | audioTsToSeconds = function (timestamp, sampleRate) {
|
11686 | return timestamp / sampleRate;
|
11687 | };
|
11688 |
|
11689 | audioTsToVideoTs = function (timestamp, sampleRate) {
|
11690 | return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
|
11691 | };
|
11692 |
|
11693 | videoTsToAudioTs = function (timestamp, sampleRate) {
|
11694 | return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
|
11695 | };
|
11696 | |
11697 |
|
11698 |
|
11699 |
|
11700 |
|
11701 |
|
11702 | metadataTsToSeconds = function (timestamp, timelineStartPts, keepOriginalTimestamps) {
|
11703 | return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
|
11704 | };
|
11705 |
|
11706 | var clock$2 = {
|
11707 | ONE_SECOND_IN_TS: ONE_SECOND_IN_TS$4,
|
11708 | secondsToVideoTs: secondsToVideoTs,
|
11709 | secondsToAudioTs: secondsToAudioTs,
|
11710 | videoTsToSeconds: videoTsToSeconds,
|
11711 | audioTsToSeconds: audioTsToSeconds,
|
11712 | audioTsToVideoTs: audioTsToVideoTs,
|
11713 | videoTsToAudioTs: videoTsToAudioTs,
|
11714 | metadataTsToSeconds: metadataTsToSeconds
|
11715 | };
|
11716 | |
11717 |
|
11718 |
|
11719 |
|
11720 |
|
11721 |
|
11722 |
|
11723 | var coneOfSilence = silence_1;
|
11724 | var clock$1 = clock$2;
|
11725 | |
11726 |
|
11727 |
|
11728 |
|
11729 | var sumFrameByteLengths = function (array) {
|
11730 | var i,
|
11731 | currentObj,
|
11732 | sum = 0;
|
11733 |
|
11734 | for (i = 0; i < array.length; i++) {
|
11735 | currentObj = array[i];
|
11736 | sum += currentObj.data.byteLength;
|
11737 | }
|
11738 |
|
11739 | return sum;
|
11740 | };
|
11741 |
|
11742 |
|
11743 |
|
11744 | var prefixWithSilence = function (track, frames, audioAppendStartTs, videoBaseMediaDecodeTime) {
|
11745 | var baseMediaDecodeTimeTs,
|
11746 | frameDuration = 0,
|
11747 | audioGapDuration = 0,
|
11748 | audioFillFrameCount = 0,
|
11749 | audioFillDuration = 0,
|
11750 | silentFrame,
|
11751 | i,
|
11752 | firstFrame;
|
11753 |
|
11754 | if (!frames.length) {
|
11755 | return;
|
11756 | }
|
11757 |
|
11758 | baseMediaDecodeTimeTs = clock$1.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate);
|
11759 |
|
11760 | frameDuration = Math.ceil(clock$1.ONE_SECOND_IN_TS / (track.samplerate / 1024));
|
11761 |
|
11762 | if (audioAppendStartTs && videoBaseMediaDecodeTime) {
|
11763 |
|
11764 | audioGapDuration = baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime);
|
11765 |
|
11766 | audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
|
11767 | audioFillDuration = audioFillFrameCount * frameDuration;
|
11768 | }
|
11769 |
|
11770 |
|
11771 |
|
11772 | if (audioFillFrameCount < 1 || audioFillDuration > clock$1.ONE_SECOND_IN_TS / 2) {
|
11773 | return;
|
11774 | }
|
11775 |
|
11776 | silentFrame = coneOfSilence()[track.samplerate];
|
11777 |
|
11778 | if (!silentFrame) {
|
11779 |
|
11780 |
|
11781 | silentFrame = frames[0].data;
|
11782 | }
|
11783 |
|
11784 | for (i = 0; i < audioFillFrameCount; i++) {
|
11785 | firstFrame = frames[0];
|
11786 | frames.splice(0, 0, {
|
11787 | data: silentFrame,
|
11788 | dts: firstFrame.dts - frameDuration,
|
11789 | pts: firstFrame.pts - frameDuration
|
11790 | });
|
11791 | }
|
11792 |
|
11793 | track.baseMediaDecodeTime -= Math.floor(clock$1.videoTsToAudioTs(audioFillDuration, track.samplerate));
|
11794 | return audioFillDuration;
|
11795 | };
|
11796 |
|
11797 |
|
11798 |
|
11799 |
|
11800 |
|
11801 | var trimAdtsFramesByEarliestDts = function (adtsFrames, track, earliestAllowedDts) {
|
11802 | if (track.minSegmentDts >= earliestAllowedDts) {
|
11803 | return adtsFrames;
|
11804 | }
|
11805 |
|
11806 |
|
11807 | track.minSegmentDts = Infinity;
|
11808 | return adtsFrames.filter(function (currentFrame) {
|
11809 |
|
11810 | if (currentFrame.dts >= earliestAllowedDts) {
|
11811 | track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
|
11812 | track.minSegmentPts = track.minSegmentDts;
|
11813 | return true;
|
11814 | }
|
11815 |
|
11816 |
|
11817 | return false;
|
11818 | });
|
11819 | };
|
11820 |
|
11821 |
|
11822 | var generateSampleTable = function (frames) {
|
11823 | var i,
|
11824 | currentFrame,
|
11825 | samples = [];
|
11826 |
|
11827 | for (i = 0; i < frames.length; i++) {
|
11828 | currentFrame = frames[i];
|
11829 | samples.push({
|
11830 | size: currentFrame.data.byteLength,
|
11831 | duration: 1024
|
11832 |
|
11833 | });
|
11834 | }
|
11835 |
|
11836 | return samples;
|
11837 | };
|
11838 |
|
11839 |
|
11840 | var concatenateFrameData = function (frames) {
|
11841 | var i,
|
11842 | currentFrame,
|
11843 | dataOffset = 0,
|
11844 | data = new Uint8Array(sumFrameByteLengths(frames));
|
11845 |
|
11846 | for (i = 0; i < frames.length; i++) {
|
11847 | currentFrame = frames[i];
|
11848 | data.set(currentFrame.data, dataOffset);
|
11849 | dataOffset += currentFrame.data.byteLength;
|
11850 | }
|
11851 |
|
11852 | return data;
|
11853 | };
|
11854 |
|
11855 | var audioFrameUtils$1 = {
|
11856 | prefixWithSilence: prefixWithSilence,
|
11857 | trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
|
11858 | generateSampleTable: generateSampleTable,
|
11859 | concatenateFrameData: concatenateFrameData
|
11860 | };
|
11861 | |
11862 |
|
11863 |
|
11864 |
|
11865 |
|
11866 |
|
11867 |
|
11868 | var ONE_SECOND_IN_TS$3 = clock$2.ONE_SECOND_IN_TS;
|
11869 | |
11870 |
|
11871 |
|
11872 |
|
11873 |
|
11874 |
|
11875 | var collectDtsInfo = function (track, data) {
|
11876 | if (typeof data.pts === 'number') {
|
11877 | if (track.timelineStartInfo.pts === undefined) {
|
11878 | track.timelineStartInfo.pts = data.pts;
|
11879 | }
|
11880 |
|
11881 | if (track.minSegmentPts === undefined) {
|
11882 | track.minSegmentPts = data.pts;
|
11883 | } else {
|
11884 | track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
|
11885 | }
|
11886 |
|
11887 | if (track.maxSegmentPts === undefined) {
|
11888 | track.maxSegmentPts = data.pts;
|
11889 | } else {
|
11890 | track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
|
11891 | }
|
11892 | }
|
11893 |
|
11894 | if (typeof data.dts === 'number') {
|
11895 | if (track.timelineStartInfo.dts === undefined) {
|
11896 | track.timelineStartInfo.dts = data.dts;
|
11897 | }
|
11898 |
|
11899 | if (track.minSegmentDts === undefined) {
|
11900 | track.minSegmentDts = data.dts;
|
11901 | } else {
|
11902 | track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
|
11903 | }
|
11904 |
|
11905 | if (track.maxSegmentDts === undefined) {
|
11906 | track.maxSegmentDts = data.dts;
|
11907 | } else {
|
11908 | track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
|
11909 | }
|
11910 | }
|
11911 | };
|
11912 | |
11913 |
|
11914 |
|
11915 |
|
11916 |
|
11917 |
|
11918 | var clearDtsInfo = function (track) {
|
11919 | delete track.minSegmentDts;
|
11920 | delete track.maxSegmentDts;
|
11921 | delete track.minSegmentPts;
|
11922 | delete track.maxSegmentPts;
|
11923 | };
|
11924 | |
11925 |
|
11926 |
|
11927 |
|
11928 |
|
11929 |
|
11930 |
|
11931 |
|
11932 |
|
11933 |
|
11934 | var calculateTrackBaseMediaDecodeTime = function (track, keepOriginalTimestamps) {
|
11935 | var baseMediaDecodeTime,
|
11936 | scale,
|
11937 | minSegmentDts = track.minSegmentDts;
|
11938 |
|
11939 | if (!keepOriginalTimestamps) {
|
11940 | minSegmentDts -= track.timelineStartInfo.dts;
|
11941 | }
|
11942 |
|
11943 |
|
11944 |
|
11945 | baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime;
|
11946 |
|
11947 | baseMediaDecodeTime += minSegmentDts;
|
11948 |
|
11949 | baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
|
11950 |
|
11951 | if (track.type === 'audio') {
|
11952 |
|
11953 |
|
11954 | scale = track.samplerate / ONE_SECOND_IN_TS$3;
|
11955 | baseMediaDecodeTime *= scale;
|
11956 | baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
|
11957 | }
|
11958 |
|
11959 | return baseMediaDecodeTime;
|
11960 | };
|
11961 |
|
11962 | var trackDecodeInfo$1 = {
|
11963 | clearDtsInfo: clearDtsInfo,
|
11964 | calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
|
11965 | collectDtsInfo: collectDtsInfo
|
11966 | };
|
11967 | |
11968 |
|
11969 |
|
11970 |
|
11971 |
|
11972 |
|
11973 |
|
11974 |
|
11975 |
|
11976 |
|
11977 |
|
11978 |
|
11979 |
|
11980 |
|
11981 |
|
11982 |
|
11983 | var USER_DATA_REGISTERED_ITU_T_T35 = 4,
|
11984 | RBSP_TRAILING_BITS = 128;
|
11985 | |
11986 |
|
11987 |
|
11988 |
|
11989 |
|
11990 |
|
11991 |
|
11992 |
|
11993 |
|
11994 | var parseSei = function (bytes) {
|
11995 | var i = 0,
|
11996 | result = {
|
11997 | payloadType: -1,
|
11998 | payloadSize: 0
|
11999 | },
|
12000 | payloadType = 0,
|
12001 | payloadSize = 0;
|
12002 |
|
12003 | while (i < bytes.byteLength) {
|
12004 |
|
12005 | if (bytes[i] === RBSP_TRAILING_BITS) {
|
12006 | break;
|
12007 | }
|
12008 |
|
12009 |
|
12010 | while (bytes[i] === 0xFF) {
|
12011 | payloadType += 255;
|
12012 | i++;
|
12013 | }
|
12014 |
|
12015 | payloadType += bytes[i++];
|
12016 |
|
12017 | while (bytes[i] === 0xFF) {
|
12018 | payloadSize += 255;
|
12019 | i++;
|
12020 | }
|
12021 |
|
12022 | payloadSize += bytes[i++];
|
12023 |
|
12024 |
|
12025 | if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
|
12026 | var userIdentifier = String.fromCharCode(bytes[i + 3], bytes[i + 4], bytes[i + 5], bytes[i + 6]);
|
12027 |
|
12028 | if (userIdentifier === 'GA94') {
|
12029 | result.payloadType = payloadType;
|
12030 | result.payloadSize = payloadSize;
|
12031 | result.payload = bytes.subarray(i, i + payloadSize);
|
12032 | break;
|
12033 | } else {
|
12034 | result.payload = void 0;
|
12035 | }
|
12036 | }
|
12037 |
|
12038 |
|
12039 | i += payloadSize;
|
12040 | payloadType = 0;
|
12041 | payloadSize = 0;
|
12042 | }
|
12043 |
|
12044 | return result;
|
12045 | };
|
12046 |
|
12047 |
|
12048 | var parseUserData = function (sei) {
|
12049 |
|
12050 |
|
12051 | if (sei.payload[0] !== 181) {
|
12052 | return null;
|
12053 | }
|
12054 |
|
12055 |
|
12056 | if ((sei.payload[1] << 8 | sei.payload[2]) !== 49) {
|
12057 | return null;
|
12058 | }
|
12059 |
|
12060 |
|
12061 | if (String.fromCharCode(sei.payload[3], sei.payload[4], sei.payload[5], sei.payload[6]) !== 'GA94') {
|
12062 | return null;
|
12063 | }
|
12064 |
|
12065 |
|
12066 | if (sei.payload[7] !== 0x03) {
|
12067 | return null;
|
12068 | }
|
12069 |
|
12070 |
|
12071 |
|
12072 | return sei.payload.subarray(8, sei.payload.length - 1);
|
12073 | };
|
12074 |
|
12075 |
|
12076 | var parseCaptionPackets = function (pts, userData) {
|
12077 | var results = [],
|
12078 | i,
|
12079 | count,
|
12080 | offset,
|
12081 | data;
|
12082 |
|
12083 | if (!(userData[0] & 0x40)) {
|
12084 | return results;
|
12085 | }
|
12086 |
|
12087 |
|
12088 | count = userData[0] & 0x1f;
|
12089 |
|
12090 | for (i = 0; i < count; i++) {
|
12091 | offset = i * 3;
|
12092 | data = {
|
12093 | type: userData[offset + 2] & 0x03,
|
12094 | pts: pts
|
12095 | };
|
12096 |
|
12097 | if (userData[offset + 2] & 0x04) {
|
12098 | data.ccData = userData[offset + 3] << 8 | userData[offset + 4];
|
12099 | results.push(data);
|
12100 | }
|
12101 | }
|
12102 |
|
12103 | return results;
|
12104 | };
|
12105 |
|
12106 | var discardEmulationPreventionBytes$1 = function (data) {
|
12107 | var length = data.byteLength,
|
12108 | emulationPreventionBytesPositions = [],
|
12109 | i = 1,
|
12110 | newLength,
|
12111 | newData;
|
12112 |
|
12113 | while (i < length - 2) {
|
12114 | if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
|
12115 | emulationPreventionBytesPositions.push(i + 2);
|
12116 | i += 2;
|
12117 | } else {
|
12118 | i++;
|
12119 | }
|
12120 | }
|
12121 |
|
12122 |
|
12123 |
|
12124 | if (emulationPreventionBytesPositions.length === 0) {
|
12125 | return data;
|
12126 | }
|
12127 |
|
12128 |
|
12129 | newLength = length - emulationPreventionBytesPositions.length;
|
12130 | newData = new Uint8Array(newLength);
|
12131 | var sourceIndex = 0;
|
12132 |
|
12133 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
12134 | if (sourceIndex === emulationPreventionBytesPositions[0]) {
|
12135 |
|
12136 | sourceIndex++;
|
12137 |
|
12138 | emulationPreventionBytesPositions.shift();
|
12139 | }
|
12140 |
|
12141 | newData[i] = data[sourceIndex];
|
12142 | }
|
12143 |
|
12144 | return newData;
|
12145 | };
|
12146 |
|
12147 |
|
12148 | var captionPacketParser = {
|
12149 | parseSei: parseSei,
|
12150 | parseUserData: parseUserData,
|
12151 | parseCaptionPackets: parseCaptionPackets,
|
12152 | discardEmulationPreventionBytes: discardEmulationPreventionBytes$1,
|
12153 | USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
|
12154 | };
|
12155 | |
12156 |
|
12157 |
|
12158 |
|
12159 |
|
12160 |
|
12161 |
|
12162 |
|
12163 |
|
12164 |
|
12165 |
|
12166 |
|
12167 |
|
12168 |
|
12169 |
|
12170 | var Stream$7 = stream;
|
12171 | var cea708Parser = captionPacketParser;
|
12172 |
|
12173 | var CaptionStream$2 = function (options) {
|
12174 | options = options || {};
|
12175 | CaptionStream$2.prototype.init.call(this);
|
12176 |
|
12177 | this.parse708captions_ = typeof options.parse708captions === 'boolean' ? options.parse708captions : true;
|
12178 | this.captionPackets_ = [];
|
12179 | this.ccStreams_ = [new Cea608Stream(0, 0),
|
12180 | new Cea608Stream(0, 1),
|
12181 | new Cea608Stream(1, 0),
|
12182 | new Cea608Stream(1, 1)
|
12183 | ];
|
12184 |
|
12185 | if (this.parse708captions_) {
|
12186 | this.cc708Stream_ = new Cea708Stream({
|
12187 | captionServices: options.captionServices
|
12188 | });
|
12189 | }
|
12190 |
|
12191 | this.reset();
|
12192 |
|
12193 | this.ccStreams_.forEach(function (cc) {
|
12194 | cc.on('data', this.trigger.bind(this, 'data'));
|
12195 | cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
|
12196 | cc.on('done', this.trigger.bind(this, 'done'));
|
12197 | }, this);
|
12198 |
|
12199 | if (this.parse708captions_) {
|
12200 | this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
|
12201 | this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
|
12202 | this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
|
12203 | }
|
12204 | };
|
12205 |
|
12206 | CaptionStream$2.prototype = new Stream$7();
|
12207 |
|
12208 | CaptionStream$2.prototype.push = function (event) {
|
12209 | var sei, userData, newCaptionPackets;
|
12210 |
|
12211 | if (event.nalUnitType !== 'sei_rbsp') {
|
12212 | return;
|
12213 | }
|
12214 |
|
12215 |
|
12216 | sei = cea708Parser.parseSei(event.escapedRBSP);
|
12217 |
|
12218 | if (!sei.payload) {
|
12219 | return;
|
12220 | }
|
12221 |
|
12222 |
|
12223 | if (sei.payloadType !== cea708Parser.USER_DATA_REGISTERED_ITU_T_T35) {
|
12224 | return;
|
12225 | }
|
12226 |
|
12227 |
|
12228 | userData = cea708Parser.parseUserData(sei);
|
12229 |
|
12230 | if (!userData) {
|
12231 | return;
|
12232 | }
|
12233 |
|
12234 |
|
12235 |
|
12236 |
|
12237 |
|
12238 |
|
12239 |
|
12240 |
|
12241 |
|
12242 | if (event.dts < this.latestDts_) {
|
12243 |
|
12244 | this.ignoreNextEqualDts_ = true;
|
12245 | return;
|
12246 | } else if (event.dts === this.latestDts_ && this.ignoreNextEqualDts_) {
|
12247 | this.numSameDts_--;
|
12248 |
|
12249 | if (!this.numSameDts_) {
|
12250 |
|
12251 | this.ignoreNextEqualDts_ = false;
|
12252 | }
|
12253 |
|
12254 | return;
|
12255 | }
|
12256 |
|
12257 |
|
12258 | newCaptionPackets = cea708Parser.parseCaptionPackets(event.pts, userData);
|
12259 | this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
|
12260 |
|
12261 | if (this.latestDts_ !== event.dts) {
|
12262 | this.numSameDts_ = 0;
|
12263 | }
|
12264 |
|
12265 | this.numSameDts_++;
|
12266 | this.latestDts_ = event.dts;
|
12267 | };
|
12268 |
|
12269 | CaptionStream$2.prototype.flushCCStreams = function (flushType) {
|
12270 | this.ccStreams_.forEach(function (cc) {
|
12271 | return flushType === 'flush' ? cc.flush() : cc.partialFlush();
|
12272 | }, this);
|
12273 | };
|
12274 |
|
12275 | CaptionStream$2.prototype.flushStream = function (flushType) {
|
12276 |
|
12277 | if (!this.captionPackets_.length) {
|
12278 | this.flushCCStreams(flushType);
|
12279 | return;
|
12280 | }
|
12281 |
|
12282 |
|
12283 |
|
12284 | this.captionPackets_.forEach(function (elem, idx) {
|
12285 | elem.presortIndex = idx;
|
12286 | });
|
12287 |
|
12288 | this.captionPackets_.sort(function (a, b) {
|
12289 | if (a.pts === b.pts) {
|
12290 | return a.presortIndex - b.presortIndex;
|
12291 | }
|
12292 |
|
12293 | return a.pts - b.pts;
|
12294 | });
|
12295 | this.captionPackets_.forEach(function (packet) {
|
12296 | if (packet.type < 2) {
|
12297 |
|
12298 | this.dispatchCea608Packet(packet);
|
12299 | } else {
|
12300 |
|
12301 | this.dispatchCea708Packet(packet);
|
12302 | }
|
12303 | }, this);
|
12304 | this.captionPackets_.length = 0;
|
12305 | this.flushCCStreams(flushType);
|
12306 | };
|
12307 |
|
12308 | CaptionStream$2.prototype.flush = function () {
|
12309 | return this.flushStream('flush');
|
12310 | };
|
12311 |
|
12312 |
|
12313 | CaptionStream$2.prototype.partialFlush = function () {
|
12314 | return this.flushStream('partialFlush');
|
12315 | };
|
12316 |
|
12317 | CaptionStream$2.prototype.reset = function () {
|
12318 | this.latestDts_ = null;
|
12319 | this.ignoreNextEqualDts_ = false;
|
12320 | this.numSameDts_ = 0;
|
12321 | this.activeCea608Channel_ = [null, null];
|
12322 | this.ccStreams_.forEach(function (ccStream) {
|
12323 | ccStream.reset();
|
12324 | });
|
12325 | };
|
12326 |
|
12327 | |
12328 |
|
12329 |
|
12330 |
|
12331 |
|
12332 |
|
12333 |
|
12334 |
|
12335 |
|
12336 |
|
12337 |
|
12338 |
|
12339 | CaptionStream$2.prototype.dispatchCea608Packet = function (packet) {
|
12340 |
|
12341 | if (this.setsTextOrXDSActive(packet)) {
|
12342 | this.activeCea608Channel_[packet.type] = null;
|
12343 | } else if (this.setsChannel1Active(packet)) {
|
12344 | this.activeCea608Channel_[packet.type] = 0;
|
12345 | } else if (this.setsChannel2Active(packet)) {
|
12346 | this.activeCea608Channel_[packet.type] = 1;
|
12347 | }
|
12348 |
|
12349 | if (this.activeCea608Channel_[packet.type] === null) {
|
12350 |
|
12351 |
|
12352 |
|
12353 | return;
|
12354 | }
|
12355 |
|
12356 | this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
|
12357 | };
|
12358 |
|
12359 | CaptionStream$2.prototype.setsChannel1Active = function (packet) {
|
12360 | return (packet.ccData & 0x7800) === 0x1000;
|
12361 | };
|
12362 |
|
12363 | CaptionStream$2.prototype.setsChannel2Active = function (packet) {
|
12364 | return (packet.ccData & 0x7800) === 0x1800;
|
12365 | };
|
12366 |
|
12367 | CaptionStream$2.prototype.setsTextOrXDSActive = function (packet) {
|
12368 | return (packet.ccData & 0x7100) === 0x0100 || (packet.ccData & 0x78fe) === 0x102a || (packet.ccData & 0x78fe) === 0x182a;
|
12369 | };
|
12370 |
|
12371 | CaptionStream$2.prototype.dispatchCea708Packet = function (packet) {
|
12372 | if (this.parse708captions_) {
|
12373 | this.cc708Stream_.push(packet);
|
12374 | }
|
12375 | };
|
12376 |
|
12377 |
|
12378 |
|
12379 |
|
12380 |
|
12381 |
|
12382 |
|
12383 |
|
12384 |
|
12385 |
|
12386 |
|
12387 |
|
12388 |
|
12389 |
|
12390 |
|
12391 |
|
12392 |
|
12393 |
|
12394 |
|
12395 | var CHARACTER_TRANSLATION_708 = {
|
12396 | 0x7f: 0x266a,
|
12397 |
|
12398 | 0x1020: 0x20,
|
12399 |
|
12400 | 0x1021: 0xa0,
|
12401 |
|
12402 | 0x1025: 0x2026,
|
12403 |
|
12404 | 0x102a: 0x0160,
|
12405 |
|
12406 | 0x102c: 0x0152,
|
12407 |
|
12408 | 0x1030: 0x2588,
|
12409 |
|
12410 | 0x1031: 0x2018,
|
12411 |
|
12412 | 0x1032: 0x2019,
|
12413 |
|
12414 | 0x1033: 0x201c,
|
12415 |
|
12416 | 0x1034: 0x201d,
|
12417 |
|
12418 | 0x1035: 0x2022,
|
12419 |
|
12420 | 0x1039: 0x2122,
|
12421 |
|
12422 | 0x103a: 0x0161,
|
12423 |
|
12424 | 0x103c: 0x0153,
|
12425 |
|
12426 | 0x103d: 0x2120,
|
12427 |
|
12428 | 0x103f: 0x0178,
|
12429 |
|
12430 | 0x1076: 0x215b,
|
12431 |
|
12432 | 0x1077: 0x215c,
|
12433 |
|
12434 | 0x1078: 0x215d,
|
12435 |
|
12436 | 0x1079: 0x215e,
|
12437 |
|
12438 | 0x107a: 0x23d0,
|
12439 |
|
12440 | 0x107b: 0x23a4,
|
12441 |
|
12442 | 0x107c: 0x23a3,
|
12443 |
|
12444 | 0x107d: 0x23af,
|
12445 |
|
12446 | 0x107e: 0x23a6,
|
12447 |
|
12448 | 0x107f: 0x23a1,
|
12449 |
|
12450 | 0x10a0: 0x3138
|
12451 |
|
12452 | };
|
12453 |
|
12454 | var get708CharFromCode = function (code) {
|
12455 | var newCode = CHARACTER_TRANSLATION_708[code] || code;
|
12456 |
|
12457 | if (code & 0x1000 && code === newCode) {
|
12458 |
|
12459 | return '';
|
12460 | }
|
12461 |
|
12462 | return String.fromCharCode(newCode);
|
12463 | };
|
12464 |
|
12465 | var within708TextBlock = function (b) {
|
12466 | return 0x20 <= b && b <= 0x7f || 0xa0 <= b && b <= 0xff;
|
12467 | };
|
12468 |
|
12469 | var Cea708Window = function (windowNum) {
|
12470 | this.windowNum = windowNum;
|
12471 | this.reset();
|
12472 | };
|
12473 |
|
12474 | Cea708Window.prototype.reset = function () {
|
12475 | this.clearText();
|
12476 | this.pendingNewLine = false;
|
12477 | this.winAttr = {};
|
12478 | this.penAttr = {};
|
12479 | this.penLoc = {};
|
12480 | this.penColor = {};
|
12481 |
|
12482 |
|
12483 | this.visible = 0;
|
12484 | this.rowLock = 0;
|
12485 | this.columnLock = 0;
|
12486 | this.priority = 0;
|
12487 | this.relativePositioning = 0;
|
12488 | this.anchorVertical = 0;
|
12489 | this.anchorHorizontal = 0;
|
12490 | this.anchorPoint = 0;
|
12491 | this.rowCount = 1;
|
12492 | this.virtualRowCount = this.rowCount + 1;
|
12493 | this.columnCount = 41;
|
12494 | this.windowStyle = 0;
|
12495 | this.penStyle = 0;
|
12496 | };
|
12497 |
|
12498 | Cea708Window.prototype.getText = function () {
|
12499 | return this.rows.join('\n');
|
12500 | };
|
12501 |
|
12502 | Cea708Window.prototype.clearText = function () {
|
12503 | this.rows = [''];
|
12504 | this.rowIdx = 0;
|
12505 | };
|
12506 |
|
12507 | Cea708Window.prototype.newLine = function (pts) {
|
12508 | if (this.rows.length >= this.virtualRowCount && typeof this.beforeRowOverflow === 'function') {
|
12509 | this.beforeRowOverflow(pts);
|
12510 | }
|
12511 |
|
12512 | if (this.rows.length > 0) {
|
12513 | this.rows.push('');
|
12514 | this.rowIdx++;
|
12515 | }
|
12516 |
|
12517 |
|
12518 | while (this.rows.length > this.virtualRowCount) {
|
12519 | this.rows.shift();
|
12520 | this.rowIdx--;
|
12521 | }
|
12522 | };
|
12523 |
|
12524 | Cea708Window.prototype.isEmpty = function () {
|
12525 | if (this.rows.length === 0) {
|
12526 | return true;
|
12527 | } else if (this.rows.length === 1) {
|
12528 | return this.rows[0] === '';
|
12529 | }
|
12530 |
|
12531 | return false;
|
12532 | };
|
12533 |
|
12534 | Cea708Window.prototype.addText = function (text) {
|
12535 | this.rows[this.rowIdx] += text;
|
12536 | };
|
12537 |
|
12538 | Cea708Window.prototype.backspace = function () {
|
12539 | if (!this.isEmpty()) {
|
12540 | var row = this.rows[this.rowIdx];
|
12541 | this.rows[this.rowIdx] = row.substr(0, row.length - 1);
|
12542 | }
|
12543 | };
|
12544 |
|
12545 | var Cea708Service = function (serviceNum, encoding, stream) {
|
12546 | this.serviceNum = serviceNum;
|
12547 | this.text = '';
|
12548 | this.currentWindow = new Cea708Window(-1);
|
12549 | this.windows = [];
|
12550 | this.stream = stream;
|
12551 |
|
12552 | if (typeof encoding === 'string') {
|
12553 | this.createTextDecoder(encoding);
|
12554 | }
|
12555 | };
|
12556 | |
12557 |
|
12558 |
|
12559 |
|
12560 |
|
12561 |
|
12562 |
|
12563 |
|
12564 |
|
12565 | Cea708Service.prototype.init = function (pts, beforeRowOverflow) {
|
12566 | this.startPts = pts;
|
12567 |
|
12568 | for (var win = 0; win < 8; win++) {
|
12569 | this.windows[win] = new Cea708Window(win);
|
12570 |
|
12571 | if (typeof beforeRowOverflow === 'function') {
|
12572 | this.windows[win].beforeRowOverflow = beforeRowOverflow;
|
12573 | }
|
12574 | }
|
12575 | };
|
12576 | |
12577 |
|
12578 |
|
12579 |
|
12580 |
|
12581 |
|
12582 |
|
12583 | Cea708Service.prototype.setCurrentWindow = function (windowNum) {
|
12584 | this.currentWindow = this.windows[windowNum];
|
12585 | };
|
12586 | |
12587 |
|
12588 |
|
12589 |
|
12590 |
|
12591 | Cea708Service.prototype.createTextDecoder = function (encoding) {
|
12592 | if (typeof TextDecoder === 'undefined') {
|
12593 | this.stream.trigger('log', {
|
12594 | level: 'warn',
|
12595 | message: 'The `encoding` option is unsupported without TextDecoder support'
|
12596 | });
|
12597 | } else {
|
12598 | try {
|
12599 | this.textDecoder_ = new TextDecoder(encoding);
|
12600 | } catch (error) {
|
12601 | this.stream.trigger('log', {
|
12602 | level: 'warn',
|
12603 | message: 'TextDecoder could not be created with ' + encoding + ' encoding. ' + error
|
12604 | });
|
12605 | }
|
12606 | }
|
12607 | };
|
12608 |
|
12609 | var Cea708Stream = function (options) {
|
12610 | options = options || {};
|
12611 | Cea708Stream.prototype.init.call(this);
|
12612 | var self = this;
|
12613 | var captionServices = options.captionServices || {};
|
12614 | var captionServiceEncodings = {};
|
12615 | var serviceProps;
|
12616 |
|
12617 | Object.keys(captionServices).forEach(serviceName => {
|
12618 | serviceProps = captionServices[serviceName];
|
12619 |
|
12620 | if (/^SERVICE/.test(serviceName)) {
|
12621 | captionServiceEncodings[serviceName] = serviceProps.encoding;
|
12622 | }
|
12623 | });
|
12624 | this.serviceEncodings = captionServiceEncodings;
|
12625 | this.current708Packet = null;
|
12626 | this.services = {};
|
12627 |
|
12628 | this.push = function (packet) {
|
12629 | if (packet.type === 3) {
|
12630 |
|
12631 | self.new708Packet();
|
12632 | self.add708Bytes(packet);
|
12633 | } else {
|
12634 | if (self.current708Packet === null) {
|
12635 |
|
12636 | self.new708Packet();
|
12637 | }
|
12638 |
|
12639 | self.add708Bytes(packet);
|
12640 | }
|
12641 | };
|
12642 | };
|
12643 |
|
12644 | Cea708Stream.prototype = new Stream$7();
|
12645 | |
12646 |
|
12647 |
|
12648 |
|
12649 | Cea708Stream.prototype.new708Packet = function () {
|
12650 | if (this.current708Packet !== null) {
|
12651 | this.push708Packet();
|
12652 | }
|
12653 |
|
12654 | this.current708Packet = {
|
12655 | data: [],
|
12656 | ptsVals: []
|
12657 | };
|
12658 | };
|
12659 | |
12660 |
|
12661 |
|
12662 |
|
12663 |
|
12664 | Cea708Stream.prototype.add708Bytes = function (packet) {
|
12665 | var data = packet.ccData;
|
12666 | var byte0 = data >>> 8;
|
12667 | var byte1 = data & 0xff;
|
12668 |
|
12669 |
|
12670 | this.current708Packet.ptsVals.push(packet.pts);
|
12671 | this.current708Packet.data.push(byte0);
|
12672 | this.current708Packet.data.push(byte1);
|
12673 | };
|
12674 | |
12675 |
|
12676 |
|
12677 |
|
12678 |
|
12679 | Cea708Stream.prototype.push708Packet = function () {
|
12680 | var packet708 = this.current708Packet;
|
12681 | var packetData = packet708.data;
|
12682 | var serviceNum = null;
|
12683 | var blockSize = null;
|
12684 | var i = 0;
|
12685 | var b = packetData[i++];
|
12686 | packet708.seq = b >> 6;
|
12687 | packet708.sizeCode = b & 0x3f;
|
12688 |
|
12689 | for (; i < packetData.length; i++) {
|
12690 | b = packetData[i++];
|
12691 | serviceNum = b >> 5;
|
12692 | blockSize = b & 0x1f;
|
12693 |
|
12694 | if (serviceNum === 7 && blockSize > 0) {
|
12695 |
|
12696 | b = packetData[i++];
|
12697 | serviceNum = b;
|
12698 | }
|
12699 |
|
12700 | this.pushServiceBlock(serviceNum, i, blockSize);
|
12701 |
|
12702 | if (blockSize > 0) {
|
12703 | i += blockSize - 1;
|
12704 | }
|
12705 | }
|
12706 | };
|
12707 | |
12708 |
|
12709 |
|
12710 |
|
12711 |
|
12712 |
|
12713 |
|
12714 |
|
12715 |
|
12716 |
|
12717 |
|
12718 |
|
12719 |
|
12720 |
|
12721 | Cea708Stream.prototype.pushServiceBlock = function (serviceNum, start, size) {
|
12722 | var b;
|
12723 | var i = start;
|
12724 | var packetData = this.current708Packet.data;
|
12725 | var service = this.services[serviceNum];
|
12726 |
|
12727 | if (!service) {
|
12728 | service = this.initService(serviceNum, i);
|
12729 | }
|
12730 |
|
12731 | for (; i < start + size && i < packetData.length; i++) {
|
12732 | b = packetData[i];
|
12733 |
|
12734 | if (within708TextBlock(b)) {
|
12735 | i = this.handleText(i, service);
|
12736 | } else if (b === 0x18) {
|
12737 | i = this.multiByteCharacter(i, service);
|
12738 | } else if (b === 0x10) {
|
12739 | i = this.extendedCommands(i, service);
|
12740 | } else if (0x80 <= b && b <= 0x87) {
|
12741 | i = this.setCurrentWindow(i, service);
|
12742 | } else if (0x98 <= b && b <= 0x9f) {
|
12743 | i = this.defineWindow(i, service);
|
12744 | } else if (b === 0x88) {
|
12745 | i = this.clearWindows(i, service);
|
12746 | } else if (b === 0x8c) {
|
12747 | i = this.deleteWindows(i, service);
|
12748 | } else if (b === 0x89) {
|
12749 | i = this.displayWindows(i, service);
|
12750 | } else if (b === 0x8a) {
|
12751 | i = this.hideWindows(i, service);
|
12752 | } else if (b === 0x8b) {
|
12753 | i = this.toggleWindows(i, service);
|
12754 | } else if (b === 0x97) {
|
12755 | i = this.setWindowAttributes(i, service);
|
12756 | } else if (b === 0x90) {
|
12757 | i = this.setPenAttributes(i, service);
|
12758 | } else if (b === 0x91) {
|
12759 | i = this.setPenColor(i, service);
|
12760 | } else if (b === 0x92) {
|
12761 | i = this.setPenLocation(i, service);
|
12762 | } else if (b === 0x8f) {
|
12763 | service = this.reset(i, service);
|
12764 | } else if (b === 0x08) {
|
12765 |
|
12766 | service.currentWindow.backspace();
|
12767 | } else if (b === 0x0c) {
|
12768 |
|
12769 | service.currentWindow.clearText();
|
12770 | } else if (b === 0x0d) {
|
12771 |
|
12772 | service.currentWindow.pendingNewLine = true;
|
12773 | } else if (b === 0x0e) {
|
12774 |
|
12775 | service.currentWindow.clearText();
|
12776 | } else if (b === 0x8d) {
|
12777 |
|
12778 | i++;
|
12779 | } else ;
|
12780 | }
|
12781 | };
|
12782 | |
12783 |
|
12784 |
|
12785 |
|
12786 |
|
12787 |
|
12788 |
|
12789 |
|
12790 |
|
12791 | Cea708Stream.prototype.extendedCommands = function (i, service) {
|
12792 | var packetData = this.current708Packet.data;
|
12793 | var b = packetData[++i];
|
12794 |
|
12795 | if (within708TextBlock(b)) {
|
12796 | i = this.handleText(i, service, {
|
12797 | isExtended: true
|
12798 | });
|
12799 | }
|
12800 |
|
12801 | return i;
|
12802 | };
|
12803 | |
12804 |
|
12805 |
|
12806 |
|
12807 |
|
12808 |
|
12809 |
|
12810 |
|
12811 | Cea708Stream.prototype.getPts = function (byteIndex) {
|
12812 |
|
12813 | return this.current708Packet.ptsVals[Math.floor(byteIndex / 2)];
|
12814 | };
|
12815 | |
12816 |
|
12817 |
|
12818 |
|
12819 |
|
12820 |
|
12821 |
|
12822 |
|
12823 | Cea708Stream.prototype.initService = function (serviceNum, i) {
|
12824 | var serviceName = 'SERVICE' + serviceNum;
|
12825 | var self = this;
|
12826 | var serviceName;
|
12827 | var encoding;
|
12828 |
|
12829 | if (serviceName in this.serviceEncodings) {
|
12830 | encoding = this.serviceEncodings[serviceName];
|
12831 | }
|
12832 |
|
12833 | this.services[serviceNum] = new Cea708Service(serviceNum, encoding, self);
|
12834 | this.services[serviceNum].init(this.getPts(i), function (pts) {
|
12835 | self.flushDisplayed(pts, self.services[serviceNum]);
|
12836 | });
|
12837 | return this.services[serviceNum];
|
12838 | };
|
12839 | |
12840 |
|
12841 |
|
12842 |
|
12843 |
|
12844 |
|
12845 |
|
12846 |
|
12847 |
|
12848 | Cea708Stream.prototype.handleText = function (i, service, options) {
|
12849 | var isExtended = options && options.isExtended;
|
12850 | var isMultiByte = options && options.isMultiByte;
|
12851 | var packetData = this.current708Packet.data;
|
12852 | var extended = isExtended ? 0x1000 : 0x0000;
|
12853 | var currentByte = packetData[i];
|
12854 | var nextByte = packetData[i + 1];
|
12855 | var win = service.currentWindow;
|
12856 | var char;
|
12857 | var charCodeArray;
|
12858 |
|
12859 | function toHexString(byteArray) {
|
12860 | return byteArray.map(byte => {
|
12861 | return ('0' + (byte & 0xFF).toString(16)).slice(-2);
|
12862 | }).join('');
|
12863 | }
|
12864 |
|
12865 | if (isMultiByte) {
|
12866 | charCodeArray = [currentByte, nextByte];
|
12867 | i++;
|
12868 | } else {
|
12869 | charCodeArray = [currentByte];
|
12870 | }
|
12871 |
|
12872 |
|
12873 | if (service.textDecoder_ && !isExtended) {
|
12874 | char = service.textDecoder_.decode(new Uint8Array(charCodeArray));
|
12875 | } else {
|
12876 |
|
12877 | if (isMultiByte) {
|
12878 | const unicode = toHexString(charCodeArray);
|
12879 |
|
12880 | char = String.fromCharCode(parseInt(unicode, 16));
|
12881 | } else {
|
12882 | char = get708CharFromCode(extended | currentByte);
|
12883 | }
|
12884 | }
|
12885 |
|
12886 | if (win.pendingNewLine && !win.isEmpty()) {
|
12887 | win.newLine(this.getPts(i));
|
12888 | }
|
12889 |
|
12890 | win.pendingNewLine = false;
|
12891 | win.addText(char);
|
12892 | return i;
|
12893 | };
|
12894 | |
12895 |
|
12896 |
|
12897 |
|
12898 |
|
12899 |
|
12900 |
|
12901 |
|
12902 |
|
12903 | Cea708Stream.prototype.multiByteCharacter = function (i, service) {
|
12904 | var packetData = this.current708Packet.data;
|
12905 | var firstByte = packetData[i + 1];
|
12906 | var secondByte = packetData[i + 2];
|
12907 |
|
12908 | if (within708TextBlock(firstByte) && within708TextBlock(secondByte)) {
|
12909 | i = this.handleText(++i, service, {
|
12910 | isMultiByte: true
|
12911 | });
|
12912 | }
|
12913 |
|
12914 | return i;
|
12915 | };
|
12916 | |
12917 |
|
12918 |
|
12919 |
|
12920 |
|
12921 |
|
12922 |
|
12923 |
|
12924 |
|
12925 |
|
12926 |
|
12927 | Cea708Stream.prototype.setCurrentWindow = function (i, service) {
|
12928 | var packetData = this.current708Packet.data;
|
12929 | var b = packetData[i];
|
12930 | var windowNum = b & 0x07;
|
12931 | service.setCurrentWindow(windowNum);
|
12932 | return i;
|
12933 | };
|
12934 | |
12935 |
|
12936 |
|
12937 |
|
12938 |
|
12939 |
|
12940 |
|
12941 |
|
12942 |
|
12943 |
|
12944 |
|
12945 | Cea708Stream.prototype.defineWindow = function (i, service) {
|
12946 | var packetData = this.current708Packet.data;
|
12947 | var b = packetData[i];
|
12948 | var windowNum = b & 0x07;
|
12949 | service.setCurrentWindow(windowNum);
|
12950 | var win = service.currentWindow;
|
12951 | b = packetData[++i];
|
12952 | win.visible = (b & 0x20) >> 5;
|
12953 |
|
12954 | win.rowLock = (b & 0x10) >> 4;
|
12955 |
|
12956 | win.columnLock = (b & 0x08) >> 3;
|
12957 |
|
12958 | win.priority = b & 0x07;
|
12959 |
|
12960 | b = packetData[++i];
|
12961 | win.relativePositioning = (b & 0x80) >> 7;
|
12962 |
|
12963 | win.anchorVertical = b & 0x7f;
|
12964 |
|
12965 | b = packetData[++i];
|
12966 | win.anchorHorizontal = b;
|
12967 |
|
12968 | b = packetData[++i];
|
12969 | win.anchorPoint = (b & 0xf0) >> 4;
|
12970 |
|
12971 | win.rowCount = b & 0x0f;
|
12972 |
|
12973 | b = packetData[++i];
|
12974 | win.columnCount = b & 0x3f;
|
12975 |
|
12976 | b = packetData[++i];
|
12977 | win.windowStyle = (b & 0x38) >> 3;
|
12978 |
|
12979 | win.penStyle = b & 0x07;
|
12980 |
|
12981 |
|
12982 | win.virtualRowCount = win.rowCount + 1;
|
12983 | return i;
|
12984 | };
|
12985 | |
12986 |
|
12987 |
|
12988 |
|
12989 |
|
12990 |
|
12991 |
|
12992 |
|
12993 |
|
12994 |
|
12995 |
|
12996 | Cea708Stream.prototype.setWindowAttributes = function (i, service) {
|
12997 | var packetData = this.current708Packet.data;
|
12998 | var b = packetData[i];
|
12999 | var winAttr = service.currentWindow.winAttr;
|
13000 | b = packetData[++i];
|
13001 | winAttr.fillOpacity = (b & 0xc0) >> 6;
|
13002 |
|
13003 | winAttr.fillRed = (b & 0x30) >> 4;
|
13004 |
|
13005 | winAttr.fillGreen = (b & 0x0c) >> 2;
|
13006 |
|
13007 | winAttr.fillBlue = b & 0x03;
|
13008 |
|
13009 | b = packetData[++i];
|
13010 | winAttr.borderType = (b & 0xc0) >> 6;
|
13011 |
|
13012 | winAttr.borderRed = (b & 0x30) >> 4;
|
13013 |
|
13014 | winAttr.borderGreen = (b & 0x0c) >> 2;
|
13015 |
|
13016 | winAttr.borderBlue = b & 0x03;
|
13017 |
|
13018 | b = packetData[++i];
|
13019 | winAttr.borderType += (b & 0x80) >> 5;
|
13020 |
|
13021 | winAttr.wordWrap = (b & 0x40) >> 6;
|
13022 |
|
13023 | winAttr.printDirection = (b & 0x30) >> 4;
|
13024 |
|
13025 | winAttr.scrollDirection = (b & 0x0c) >> 2;
|
13026 |
|
13027 | winAttr.justify = b & 0x03;
|
13028 |
|
13029 | b = packetData[++i];
|
13030 | winAttr.effectSpeed = (b & 0xf0) >> 4;
|
13031 |
|
13032 | winAttr.effectDirection = (b & 0x0c) >> 2;
|
13033 |
|
13034 | winAttr.displayEffect = b & 0x03;
|
13035 |
|
13036 | return i;
|
13037 | };
|
13038 | |
13039 |
|
13040 |
|
13041 |
|
13042 |
|
13043 |
|
13044 |
|
13045 |
|
13046 | Cea708Stream.prototype.flushDisplayed = function (pts, service) {
|
13047 | var displayedText = [];
|
13048 |
|
13049 |
|
13050 | for (var winId = 0; winId < 8; winId++) {
|
13051 | if (service.windows[winId].visible && !service.windows[winId].isEmpty()) {
|
13052 | displayedText.push(service.windows[winId].getText());
|
13053 | }
|
13054 | }
|
13055 |
|
13056 | service.endPts = pts;
|
13057 | service.text = displayedText.join('\n\n');
|
13058 | this.pushCaption(service);
|
13059 | service.startPts = pts;
|
13060 | };
|
13061 | |
13062 |
|
13063 |
|
13064 |
|
13065 |
|
13066 |
|
13067 |
|
13068 | Cea708Stream.prototype.pushCaption = function (service) {
|
13069 | if (service.text !== '') {
|
13070 | this.trigger('data', {
|
13071 | startPts: service.startPts,
|
13072 | endPts: service.endPts,
|
13073 | text: service.text,
|
13074 | stream: 'cc708_' + service.serviceNum
|
13075 | });
|
13076 | service.text = '';
|
13077 | service.startPts = service.endPts;
|
13078 | }
|
13079 | };
|
13080 | |
13081 |
|
13082 |
|
13083 |
|
13084 |
|
13085 |
|
13086 |
|
13087 |
|
13088 |
|
13089 |
|
13090 |
|
13091 | Cea708Stream.prototype.displayWindows = function (i, service) {
|
13092 | var packetData = this.current708Packet.data;
|
13093 | var b = packetData[++i];
|
13094 | var pts = this.getPts(i);
|
13095 | this.flushDisplayed(pts, service);
|
13096 |
|
13097 | for (var winId = 0; winId < 8; winId++) {
|
13098 | if (b & 0x01 << winId) {
|
13099 | service.windows[winId].visible = 1;
|
13100 | }
|
13101 | }
|
13102 |
|
13103 | return i;
|
13104 | };
|
13105 | |
13106 |
|
13107 |
|
13108 |
|
13109 |
|
13110 |
|
13111 |
|
13112 |
|
13113 |
|
13114 |
|
13115 |
|
13116 | Cea708Stream.prototype.hideWindows = function (i, service) {
|
13117 | var packetData = this.current708Packet.data;
|
13118 | var b = packetData[++i];
|
13119 | var pts = this.getPts(i);
|
13120 | this.flushDisplayed(pts, service);
|
13121 |
|
13122 | for (var winId = 0; winId < 8; winId++) {
|
13123 | if (b & 0x01 << winId) {
|
13124 | service.windows[winId].visible = 0;
|
13125 | }
|
13126 | }
|
13127 |
|
13128 | return i;
|
13129 | };
|
13130 | |
13131 |
|
13132 |
|
13133 |
|
13134 |
|
13135 |
|
13136 |
|
13137 |
|
13138 |
|
13139 |
|
13140 |
|
13141 | Cea708Stream.prototype.toggleWindows = function (i, service) {
|
13142 | var packetData = this.current708Packet.data;
|
13143 | var b = packetData[++i];
|
13144 | var pts = this.getPts(i);
|
13145 | this.flushDisplayed(pts, service);
|
13146 |
|
13147 | for (var winId = 0; winId < 8; winId++) {
|
13148 | if (b & 0x01 << winId) {
|
13149 | service.windows[winId].visible ^= 1;
|
13150 | }
|
13151 | }
|
13152 |
|
13153 | return i;
|
13154 | };
|
13155 | |
13156 |
|
13157 |
|
13158 |
|
13159 |
|
13160 |
|
13161 |
|
13162 |
|
13163 |
|
13164 |
|
13165 |
|
13166 | Cea708Stream.prototype.clearWindows = function (i, service) {
|
13167 | var packetData = this.current708Packet.data;
|
13168 | var b = packetData[++i];
|
13169 | var pts = this.getPts(i);
|
13170 | this.flushDisplayed(pts, service);
|
13171 |
|
13172 | for (var winId = 0; winId < 8; winId++) {
|
13173 | if (b & 0x01 << winId) {
|
13174 | service.windows[winId].clearText();
|
13175 | }
|
13176 | }
|
13177 |
|
13178 | return i;
|
13179 | };
|
13180 | |
13181 |
|
13182 |
|
13183 |
|
13184 |
|
13185 |
|
13186 |
|
13187 |
|
13188 |
|
13189 |
|
13190 |
|
13191 | Cea708Stream.prototype.deleteWindows = function (i, service) {
|
13192 | var packetData = this.current708Packet.data;
|
13193 | var b = packetData[++i];
|
13194 | var pts = this.getPts(i);
|
13195 | this.flushDisplayed(pts, service);
|
13196 |
|
13197 | for (var winId = 0; winId < 8; winId++) {
|
13198 | if (b & 0x01 << winId) {
|
13199 | service.windows[winId].reset();
|
13200 | }
|
13201 | }
|
13202 |
|
13203 | return i;
|
13204 | };
|
13205 | |
13206 |
|
13207 |
|
13208 |
|
13209 |
|
13210 |
|
13211 |
|
13212 |
|
13213 |
|
13214 |
|
13215 |
|
13216 | Cea708Stream.prototype.setPenAttributes = function (i, service) {
|
13217 | var packetData = this.current708Packet.data;
|
13218 | var b = packetData[i];
|
13219 | var penAttr = service.currentWindow.penAttr;
|
13220 | b = packetData[++i];
|
13221 | penAttr.textTag = (b & 0xf0) >> 4;
|
13222 |
|
13223 | penAttr.offset = (b & 0x0c) >> 2;
|
13224 |
|
13225 | penAttr.penSize = b & 0x03;
|
13226 |
|
13227 | b = packetData[++i];
|
13228 | penAttr.italics = (b & 0x80) >> 7;
|
13229 |
|
13230 | penAttr.underline = (b & 0x40) >> 6;
|
13231 |
|
13232 | penAttr.edgeType = (b & 0x38) >> 3;
|
13233 |
|
13234 | penAttr.fontStyle = b & 0x07;
|
13235 |
|
13236 | return i;
|
13237 | };
|
13238 | |
13239 |
|
13240 |
|
13241 |
|
13242 |
|
13243 |
|
13244 |
|
13245 |
|
13246 |
|
13247 |
|
13248 |
|
13249 | Cea708Stream.prototype.setPenColor = function (i, service) {
|
13250 | var packetData = this.current708Packet.data;
|
13251 | var b = packetData[i];
|
13252 | var penColor = service.currentWindow.penColor;
|
13253 | b = packetData[++i];
|
13254 | penColor.fgOpacity = (b & 0xc0) >> 6;
|
13255 |
|
13256 | penColor.fgRed = (b & 0x30) >> 4;
|
13257 |
|
13258 | penColor.fgGreen = (b & 0x0c) >> 2;
|
13259 |
|
13260 | penColor.fgBlue = b & 0x03;
|
13261 |
|
13262 | b = packetData[++i];
|
13263 | penColor.bgOpacity = (b & 0xc0) >> 6;
|
13264 |
|
13265 | penColor.bgRed = (b & 0x30) >> 4;
|
13266 |
|
13267 | penColor.bgGreen = (b & 0x0c) >> 2;
|
13268 |
|
13269 | penColor.bgBlue = b & 0x03;
|
13270 |
|
13271 | b = packetData[++i];
|
13272 | penColor.edgeRed = (b & 0x30) >> 4;
|
13273 |
|
13274 | penColor.edgeGreen = (b & 0x0c) >> 2;
|
13275 |
|
13276 | penColor.edgeBlue = b & 0x03;
|
13277 |
|
13278 | return i;
|
13279 | };
|
13280 | |
13281 |
|
13282 |
|
13283 |
|
13284 |
|
13285 |
|
13286 |
|
13287 |
|
13288 |
|
13289 |
|
13290 |
|
13291 | Cea708Stream.prototype.setPenLocation = function (i, service) {
|
13292 | var packetData = this.current708Packet.data;
|
13293 | var b = packetData[i];
|
13294 | var penLoc = service.currentWindow.penLoc;
|
13295 |
|
13296 | service.currentWindow.pendingNewLine = true;
|
13297 | b = packetData[++i];
|
13298 | penLoc.row = b & 0x0f;
|
13299 |
|
13300 | b = packetData[++i];
|
13301 | penLoc.column = b & 0x3f;
|
13302 |
|
13303 | return i;
|
13304 | };
|
13305 | |
13306 |
|
13307 |
|
13308 |
|
13309 |
|
13310 |
|
13311 |
|
13312 |
|
13313 |
|
13314 |
|
13315 |
|
13316 | Cea708Stream.prototype.reset = function (i, service) {
|
13317 | var pts = this.getPts(i);
|
13318 | this.flushDisplayed(pts, service);
|
13319 | return this.initService(service.serviceNum, i);
|
13320 | };
|
13321 |
|
13322 |
|
13323 |
|
13324 |
|
13325 |
|
13326 |
|
13327 |
|
13328 |
|
13329 | var CHARACTER_TRANSLATION = {
|
13330 | 0x2a: 0xe1,
|
13331 |
|
13332 | 0x5c: 0xe9,
|
13333 |
|
13334 | 0x5e: 0xed,
|
13335 |
|
13336 | 0x5f: 0xf3,
|
13337 |
|
13338 | 0x60: 0xfa,
|
13339 |
|
13340 | 0x7b: 0xe7,
|
13341 |
|
13342 | 0x7c: 0xf7,
|
13343 |
|
13344 | 0x7d: 0xd1,
|
13345 |
|
13346 | 0x7e: 0xf1,
|
13347 |
|
13348 | 0x7f: 0x2588,
|
13349 |
|
13350 | 0x0130: 0xae,
|
13351 |
|
13352 | 0x0131: 0xb0,
|
13353 |
|
13354 | 0x0132: 0xbd,
|
13355 |
|
13356 | 0x0133: 0xbf,
|
13357 |
|
13358 | 0x0134: 0x2122,
|
13359 |
|
13360 | 0x0135: 0xa2,
|
13361 |
|
13362 | 0x0136: 0xa3,
|
13363 |
|
13364 | 0x0137: 0x266a,
|
13365 |
|
13366 | 0x0138: 0xe0,
|
13367 |
|
13368 | 0x0139: 0xa0,
|
13369 |
|
13370 | 0x013a: 0xe8,
|
13371 |
|
13372 | 0x013b: 0xe2,
|
13373 |
|
13374 | 0x013c: 0xea,
|
13375 |
|
13376 | 0x013d: 0xee,
|
13377 |
|
13378 | 0x013e: 0xf4,
|
13379 |
|
13380 | 0x013f: 0xfb,
|
13381 |
|
13382 | 0x0220: 0xc1,
|
13383 |
|
13384 | 0x0221: 0xc9,
|
13385 |
|
13386 | 0x0222: 0xd3,
|
13387 |
|
13388 | 0x0223: 0xda,
|
13389 |
|
13390 | 0x0224: 0xdc,
|
13391 |
|
13392 | 0x0225: 0xfc,
|
13393 |
|
13394 | 0x0226: 0x2018,
|
13395 |
|
13396 | 0x0227: 0xa1,
|
13397 |
|
13398 | 0x0228: 0x2a,
|
13399 |
|
13400 | 0x0229: 0x27,
|
13401 |
|
13402 | 0x022a: 0x2014,
|
13403 |
|
13404 | 0x022b: 0xa9,
|
13405 |
|
13406 | 0x022c: 0x2120,
|
13407 |
|
13408 | 0x022d: 0x2022,
|
13409 |
|
13410 | 0x022e: 0x201c,
|
13411 |
|
13412 | 0x022f: 0x201d,
|
13413 |
|
13414 | 0x0230: 0xc0,
|
13415 |
|
13416 | 0x0231: 0xc2,
|
13417 |
|
13418 | 0x0232: 0xc7,
|
13419 |
|
13420 | 0x0233: 0xc8,
|
13421 |
|
13422 | 0x0234: 0xca,
|
13423 |
|
13424 | 0x0235: 0xcb,
|
13425 |
|
13426 | 0x0236: 0xeb,
|
13427 |
|
13428 | 0x0237: 0xce,
|
13429 |
|
13430 | 0x0238: 0xcf,
|
13431 |
|
13432 | 0x0239: 0xef,
|
13433 |
|
13434 | 0x023a: 0xd4,
|
13435 |
|
13436 | 0x023b: 0xd9,
|
13437 |
|
13438 | 0x023c: 0xf9,
|
13439 |
|
13440 | 0x023d: 0xdb,
|
13441 |
|
13442 | 0x023e: 0xab,
|
13443 |
|
13444 | 0x023f: 0xbb,
|
13445 |
|
13446 | 0x0320: 0xc3,
|
13447 |
|
13448 | 0x0321: 0xe3,
|
13449 |
|
13450 | 0x0322: 0xcd,
|
13451 |
|
13452 | 0x0323: 0xcc,
|
13453 |
|
13454 | 0x0324: 0xec,
|
13455 |
|
13456 | 0x0325: 0xd2,
|
13457 |
|
13458 | 0x0326: 0xf2,
|
13459 |
|
13460 | 0x0327: 0xd5,
|
13461 |
|
13462 | 0x0328: 0xf5,
|
13463 |
|
13464 | 0x0329: 0x7b,
|
13465 |
|
13466 | 0x032a: 0x7d,
|
13467 |
|
13468 | 0x032b: 0x5c,
|
13469 |
|
13470 | 0x032c: 0x5e,
|
13471 |
|
13472 | 0x032d: 0x5f,
|
13473 |
|
13474 | 0x032e: 0x7c,
|
13475 |
|
13476 | 0x032f: 0x7e,
|
13477 |
|
13478 | 0x0330: 0xc4,
|
13479 |
|
13480 | 0x0331: 0xe4,
|
13481 |
|
13482 | 0x0332: 0xd6,
|
13483 |
|
13484 | 0x0333: 0xf6,
|
13485 |
|
13486 | 0x0334: 0xdf,
|
13487 |
|
13488 | 0x0335: 0xa5,
|
13489 |
|
13490 | 0x0336: 0xa4,
|
13491 |
|
13492 | 0x0337: 0x2502,
|
13493 |
|
13494 | 0x0338: 0xc5,
|
13495 |
|
13496 | 0x0339: 0xe5,
|
13497 |
|
13498 | 0x033a: 0xd8,
|
13499 |
|
13500 | 0x033b: 0xf8,
|
13501 |
|
13502 | 0x033c: 0x250c,
|
13503 |
|
13504 | 0x033d: 0x2510,
|
13505 |
|
13506 | 0x033e: 0x2514,
|
13507 |
|
13508 | 0x033f: 0x2518
|
13509 |
|
13510 | };
|
13511 |
|
13512 | var getCharFromCode = function (code) {
|
13513 | if (code === null) {
|
13514 | return '';
|
13515 | }
|
13516 |
|
13517 | code = CHARACTER_TRANSLATION[code] || code;
|
13518 | return String.fromCharCode(code);
|
13519 | };
|
13520 |
|
13521 |
|
13522 | var BOTTOM_ROW = 14;
|
13523 |
|
13524 |
|
13525 | var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620, 0x1700, 0x1720, 0x1000, 0x1300, 0x1320, 0x1400, 0x1420];
|
13526 |
|
13527 |
|
13528 |
|
13529 |
|
13530 | var createDisplayBuffer = function () {
|
13531 | var result = [],
|
13532 | i = BOTTOM_ROW + 1;
|
13533 |
|
13534 | while (i--) {
|
13535 | result.push({
|
13536 | text: '',
|
13537 | indent: 0,
|
13538 | offset: 0
|
13539 | });
|
13540 | }
|
13541 |
|
13542 | return result;
|
13543 | };
|
13544 |
|
13545 | var Cea608Stream = function (field, dataChannel) {
|
13546 | Cea608Stream.prototype.init.call(this);
|
13547 | this.field_ = field || 0;
|
13548 | this.dataChannel_ = dataChannel || 0;
|
13549 | this.name_ = 'CC' + ((this.field_ << 1 | this.dataChannel_) + 1);
|
13550 | this.setConstants();
|
13551 | this.reset();
|
13552 |
|
13553 | this.push = function (packet) {
|
13554 | var data, swap, char0, char1, text;
|
13555 |
|
13556 | data = packet.ccData & 0x7f7f;
|
13557 |
|
13558 | if (data === this.lastControlCode_) {
|
13559 | this.lastControlCode_ = null;
|
13560 | return;
|
13561 | }
|
13562 |
|
13563 |
|
13564 | if ((data & 0xf000) === 0x1000) {
|
13565 | this.lastControlCode_ = data;
|
13566 | } else if (data !== this.PADDING_) {
|
13567 | this.lastControlCode_ = null;
|
13568 | }
|
13569 |
|
13570 | char0 = data >>> 8;
|
13571 | char1 = data & 0xff;
|
13572 |
|
13573 | if (data === this.PADDING_) {
|
13574 | return;
|
13575 | } else if (data === this.RESUME_CAPTION_LOADING_) {
|
13576 | this.mode_ = 'popOn';
|
13577 | } else if (data === this.END_OF_CAPTION_) {
|
13578 |
|
13579 |
|
13580 |
|
13581 |
|
13582 | this.mode_ = 'popOn';
|
13583 | this.clearFormatting(packet.pts);
|
13584 |
|
13585 | this.flushDisplayed(packet.pts);
|
13586 |
|
13587 | swap = this.displayed_;
|
13588 | this.displayed_ = this.nonDisplayed_;
|
13589 | this.nonDisplayed_ = swap;
|
13590 |
|
13591 | this.startPts_ = packet.pts;
|
13592 | } else if (data === this.ROLL_UP_2_ROWS_) {
|
13593 | this.rollUpRows_ = 2;
|
13594 | this.setRollUp(packet.pts);
|
13595 | } else if (data === this.ROLL_UP_3_ROWS_) {
|
13596 | this.rollUpRows_ = 3;
|
13597 | this.setRollUp(packet.pts);
|
13598 | } else if (data === this.ROLL_UP_4_ROWS_) {
|
13599 | this.rollUpRows_ = 4;
|
13600 | this.setRollUp(packet.pts);
|
13601 | } else if (data === this.CARRIAGE_RETURN_) {
|
13602 | this.clearFormatting(packet.pts);
|
13603 | this.flushDisplayed(packet.pts);
|
13604 | this.shiftRowsUp_();
|
13605 | this.startPts_ = packet.pts;
|
13606 | } else if (data === this.BACKSPACE_) {
|
13607 | if (this.mode_ === 'popOn') {
|
13608 | this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
|
13609 | } else {
|
13610 | this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
|
13611 | }
|
13612 | } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
|
13613 | this.flushDisplayed(packet.pts);
|
13614 | this.displayed_ = createDisplayBuffer();
|
13615 | } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
|
13616 | this.nonDisplayed_ = createDisplayBuffer();
|
13617 | } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
|
13618 | if (this.mode_ !== 'paintOn') {
|
13619 |
|
13620 |
|
13621 | this.flushDisplayed(packet.pts);
|
13622 | this.displayed_ = createDisplayBuffer();
|
13623 | }
|
13624 |
|
13625 | this.mode_ = 'paintOn';
|
13626 | this.startPts_ = packet.pts;
|
13627 | } else if (this.isSpecialCharacter(char0, char1)) {
|
13628 |
|
13629 |
|
13630 |
|
13631 |
|
13632 | char0 = (char0 & 0x03) << 8;
|
13633 | text = getCharFromCode(char0 | char1);
|
13634 | this[this.mode_](packet.pts, text);
|
13635 | this.column_++;
|
13636 | } else if (this.isExtCharacter(char0, char1)) {
|
13637 |
|
13638 |
|
13639 |
|
13640 |
|
13641 |
|
13642 | if (this.mode_ === 'popOn') {
|
13643 | this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
|
13644 | } else {
|
13645 | this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
|
13646 | }
|
13647 |
|
13648 |
|
13649 |
|
13650 |
|
13651 |
|
13652 | char0 = (char0 & 0x03) << 8;
|
13653 | text = getCharFromCode(char0 | char1);
|
13654 | this[this.mode_](packet.pts, text);
|
13655 | this.column_++;
|
13656 | } else if (this.isMidRowCode(char0, char1)) {
|
13657 |
|
13658 | this.clearFormatting(packet.pts);
|
13659 |
|
13660 |
|
13661 | this[this.mode_](packet.pts, ' ');
|
13662 | this.column_++;
|
13663 |
|
13664 | if ((char1 & 0xe) === 0xe) {
|
13665 | this.addFormatting(packet.pts, ['i']);
|
13666 | }
|
13667 |
|
13668 | if ((char1 & 0x1) === 0x1) {
|
13669 | this.addFormatting(packet.pts, ['u']);
|
13670 | }
|
13671 |
|
13672 | } else if (this.isOffsetControlCode(char0, char1)) {
|
13673 |
|
13674 |
|
13675 |
|
13676 |
|
13677 | const offset = char1 & 0x03;
|
13678 |
|
13679 |
|
13680 | this.nonDisplayed_[this.row_].offset = offset;
|
13681 | this.column_ += offset;
|
13682 | } else if (this.isPAC(char0, char1)) {
|
13683 |
|
13684 |
|
13685 | var row = ROWS.indexOf(data & 0x1f20);
|
13686 |
|
13687 | if (this.mode_ === 'rollUp') {
|
13688 |
|
13689 |
|
13690 |
|
13691 | if (row - this.rollUpRows_ + 1 < 0) {
|
13692 | row = this.rollUpRows_ - 1;
|
13693 | }
|
13694 |
|
13695 | this.setRollUp(packet.pts, row);
|
13696 | }
|
13697 |
|
13698 | if (row !== this.row_) {
|
13699 |
|
13700 | this.clearFormatting(packet.pts);
|
13701 | this.row_ = row;
|
13702 | }
|
13703 |
|
13704 |
|
13705 |
|
13706 | if (char1 & 0x1 && this.formatting_.indexOf('u') === -1) {
|
13707 | this.addFormatting(packet.pts, ['u']);
|
13708 | }
|
13709 |
|
13710 | if ((data & 0x10) === 0x10) {
|
13711 |
|
13712 |
|
13713 |
|
13714 |
|
13715 | const indentations = (data & 0xe) >> 1;
|
13716 | this.column_ = indentations * 4;
|
13717 |
|
13718 | this.nonDisplayed_[this.row_].indent += indentations;
|
13719 | }
|
13720 |
|
13721 | if (this.isColorPAC(char1)) {
|
13722 |
|
13723 |
|
13724 |
|
13725 |
|
13726 | if ((char1 & 0xe) === 0xe) {
|
13727 | this.addFormatting(packet.pts, ['i']);
|
13728 | }
|
13729 | }
|
13730 |
|
13731 | } else if (this.isNormalChar(char0)) {
|
13732 | if (char1 === 0x00) {
|
13733 | char1 = null;
|
13734 | }
|
13735 |
|
13736 | text = getCharFromCode(char0);
|
13737 | text += getCharFromCode(char1);
|
13738 | this[this.mode_](packet.pts, text);
|
13739 | this.column_ += text.length;
|
13740 | }
|
13741 |
|
13742 | };
|
13743 | };
|
13744 |
|
13745 | Cea608Stream.prototype = new Stream$7();
|
13746 |
|
13747 |
|
13748 | Cea608Stream.prototype.flushDisplayed = function (pts) {
|
13749 | const logWarning = index => {
|
13750 | this.trigger('log', {
|
13751 | level: 'warn',
|
13752 | message: 'Skipping a malformed 608 caption at index ' + index + '.'
|
13753 | });
|
13754 | };
|
13755 |
|
13756 | const content = [];
|
13757 | this.displayed_.forEach((row, i) => {
|
13758 | if (row && row.text && row.text.length) {
|
13759 | try {
|
13760 |
|
13761 | row.text = row.text.trim();
|
13762 | } catch (e) {
|
13763 |
|
13764 |
|
13765 |
|
13766 | logWarning(i);
|
13767 | }
|
13768 |
|
13769 |
|
13770 |
|
13771 | if (row.text.length) {
|
13772 | content.push({
|
13773 |
|
13774 | text: row.text,
|
13775 |
|
13776 | line: i + 1,
|
13777 |
|
13778 |
|
13779 |
|
13780 | position: 10 + Math.min(70, row.indent * 10) + row.offset * 2.5
|
13781 | });
|
13782 | }
|
13783 | } else if (row === undefined || row === null) {
|
13784 | logWarning(i);
|
13785 | }
|
13786 | });
|
13787 |
|
13788 | if (content.length) {
|
13789 | this.trigger('data', {
|
13790 | startPts: this.startPts_,
|
13791 | endPts: pts,
|
13792 | content,
|
13793 | stream: this.name_
|
13794 | });
|
13795 | }
|
13796 | };
|
13797 | |
13798 |
|
13799 |
|
13800 |
|
13801 |
|
13802 | Cea608Stream.prototype.reset = function () {
|
13803 | this.mode_ = 'popOn';
|
13804 |
|
13805 |
|
13806 |
|
13807 |
|
13808 | this.topRow_ = 0;
|
13809 | this.startPts_ = 0;
|
13810 | this.displayed_ = createDisplayBuffer();
|
13811 | this.nonDisplayed_ = createDisplayBuffer();
|
13812 | this.lastControlCode_ = null;
|
13813 |
|
13814 | this.column_ = 0;
|
13815 | this.row_ = BOTTOM_ROW;
|
13816 | this.rollUpRows_ = 2;
|
13817 |
|
13818 | this.formatting_ = [];
|
13819 | };
|
13820 | |
13821 |
|
13822 |
|
13823 |
|
13824 |
|
13825 | Cea608Stream.prototype.setConstants = function () {
|
13826 |
|
13827 |
|
13828 |
|
13829 |
|
13830 |
|
13831 |
|
13832 |
|
13833 |
|
13834 |
|
13835 |
|
13836 |
|
13837 |
|
13838 | if (this.dataChannel_ === 0) {
|
13839 | this.BASE_ = 0x10;
|
13840 | this.EXT_ = 0x11;
|
13841 | this.CONTROL_ = (0x14 | this.field_) << 8;
|
13842 | this.OFFSET_ = 0x17;
|
13843 | } else if (this.dataChannel_ === 1) {
|
13844 | this.BASE_ = 0x18;
|
13845 | this.EXT_ = 0x19;
|
13846 | this.CONTROL_ = (0x1c | this.field_) << 8;
|
13847 | this.OFFSET_ = 0x1f;
|
13848 | }
|
13849 |
|
13850 |
|
13851 |
|
13852 |
|
13853 |
|
13854 | this.PADDING_ = 0x0000;
|
13855 |
|
13856 | this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
|
13857 | this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f;
|
13858 |
|
13859 | this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
|
13860 | this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
|
13861 | this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
|
13862 | this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d;
|
13863 |
|
13864 | this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29;
|
13865 |
|
13866 | this.BACKSPACE_ = this.CONTROL_ | 0x21;
|
13867 | this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
|
13868 | this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
|
13869 | };
|
13870 | |
13871 |
|
13872 |
|
13873 |
|
13874 |
|
13875 |
|
13876 |
|
13877 |
|
13878 |
|
13879 |
|
13880 |
|
13881 |
|
13882 |
|
13883 | Cea608Stream.prototype.isSpecialCharacter = function (char0, char1) {
|
13884 | return char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f;
|
13885 | };
|
13886 | |
13887 |
|
13888 |
|
13889 |
|
13890 |
|
13891 |
|
13892 |
|
13893 |
|
13894 |
|
13895 |
|
13896 |
|
13897 |
|
13898 |
|
13899 | Cea608Stream.prototype.isExtCharacter = function (char0, char1) {
|
13900 | return (char0 === this.EXT_ + 1 || char0 === this.EXT_ + 2) && char1 >= 0x20 && char1 <= 0x3f;
|
13901 | };
|
13902 | |
13903 |
|
13904 |
|
13905 |
|
13906 |
|
13907 |
|
13908 |
|
13909 |
|
13910 |
|
13911 |
|
13912 |
|
13913 |
|
13914 |
|
13915 | Cea608Stream.prototype.isMidRowCode = function (char0, char1) {
|
13916 | return char0 === this.EXT_ && char1 >= 0x20 && char1 <= 0x2f;
|
13917 | };
|
13918 | |
13919 |
|
13920 |
|
13921 |
|
13922 |
|
13923 |
|
13924 |
|
13925 |
|
13926 |
|
13927 |
|
13928 |
|
13929 |
|
13930 |
|
13931 | Cea608Stream.prototype.isOffsetControlCode = function (char0, char1) {
|
13932 | return char0 === this.OFFSET_ && char1 >= 0x21 && char1 <= 0x23;
|
13933 | };
|
13934 | |
13935 |
|
13936 |
|
13937 |
|
13938 |
|
13939 |
|
13940 |
|
13941 |
|
13942 |
|
13943 |
|
13944 |
|
13945 |
|
13946 |
|
13947 | Cea608Stream.prototype.isPAC = function (char0, char1) {
|
13948 | return char0 >= this.BASE_ && char0 < this.BASE_ + 8 && char1 >= 0x40 && char1 <= 0x7f;
|
13949 | };
|
13950 | |
13951 |
|
13952 |
|
13953 |
|
13954 |
|
13955 |
|
13956 |
|
13957 |
|
13958 |
|
13959 |
|
13960 |
|
13961 | Cea608Stream.prototype.isColorPAC = function (char1) {
|
13962 | return char1 >= 0x40 && char1 <= 0x4f || char1 >= 0x60 && char1 <= 0x7f;
|
13963 | };
|
13964 | |
13965 |
|
13966 |
|
13967 |
|
13968 |
|
13969 |
|
13970 |
|
13971 |
|
13972 |
|
13973 |
|
13974 | Cea608Stream.prototype.isNormalChar = function (char) {
|
13975 | return char >= 0x20 && char <= 0x7f;
|
13976 | };
|
13977 | |
13978 |
|
13979 |
|
13980 |
|
13981 |
|
13982 |
|
13983 |
|
13984 |
|
13985 |
|
13986 | Cea608Stream.prototype.setRollUp = function (pts, newBaseRow) {
|
13987 |
|
13988 | if (this.mode_ !== 'rollUp') {
|
13989 | this.row_ = BOTTOM_ROW;
|
13990 | this.mode_ = 'rollUp';
|
13991 |
|
13992 | this.flushDisplayed(pts);
|
13993 | this.nonDisplayed_ = createDisplayBuffer();
|
13994 | this.displayed_ = createDisplayBuffer();
|
13995 | }
|
13996 |
|
13997 | if (newBaseRow !== undefined && newBaseRow !== this.row_) {
|
13998 |
|
13999 | for (var i = 0; i < this.rollUpRows_; i++) {
|
14000 | this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
|
14001 | this.displayed_[this.row_ - i] = {
|
14002 | text: '',
|
14003 | indent: 0,
|
14004 | offset: 0
|
14005 | };
|
14006 | }
|
14007 | }
|
14008 |
|
14009 | if (newBaseRow === undefined) {
|
14010 | newBaseRow = this.row_;
|
14011 | }
|
14012 |
|
14013 | this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
|
14014 | };
|
14015 |
|
14016 |
|
14017 |
|
14018 | Cea608Stream.prototype.addFormatting = function (pts, format) {
|
14019 | this.formatting_ = this.formatting_.concat(format);
|
14020 | var text = format.reduce(function (text, format) {
|
14021 | return text + '<' + format + '>';
|
14022 | }, '');
|
14023 | this[this.mode_](pts, text);
|
14024 | };
|
14025 |
|
14026 |
|
14027 |
|
14028 | Cea608Stream.prototype.clearFormatting = function (pts) {
|
14029 | if (!this.formatting_.length) {
|
14030 | return;
|
14031 | }
|
14032 |
|
14033 | var text = this.formatting_.reverse().reduce(function (text, format) {
|
14034 | return text + '</' + format + '>';
|
14035 | }, '');
|
14036 | this.formatting_ = [];
|
14037 | this[this.mode_](pts, text);
|
14038 | };
|
14039 |
|
14040 |
|
14041 | Cea608Stream.prototype.popOn = function (pts, text) {
|
14042 | var baseRow = this.nonDisplayed_[this.row_].text;
|
14043 |
|
14044 | baseRow += text;
|
14045 | this.nonDisplayed_[this.row_].text = baseRow;
|
14046 | };
|
14047 |
|
14048 | Cea608Stream.prototype.rollUp = function (pts, text) {
|
14049 | var baseRow = this.displayed_[this.row_].text;
|
14050 | baseRow += text;
|
14051 | this.displayed_[this.row_].text = baseRow;
|
14052 | };
|
14053 |
|
14054 | Cea608Stream.prototype.shiftRowsUp_ = function () {
|
14055 | var i;
|
14056 |
|
14057 | for (i = 0; i < this.topRow_; i++) {
|
14058 | this.displayed_[i] = {
|
14059 | text: '',
|
14060 | indent: 0,
|
14061 | offset: 0
|
14062 | };
|
14063 | }
|
14064 |
|
14065 | for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
|
14066 | this.displayed_[i] = {
|
14067 | text: '',
|
14068 | indent: 0,
|
14069 | offset: 0
|
14070 | };
|
14071 | }
|
14072 |
|
14073 |
|
14074 | for (i = this.topRow_; i < this.row_; i++) {
|
14075 | this.displayed_[i] = this.displayed_[i + 1];
|
14076 | }
|
14077 |
|
14078 |
|
14079 | this.displayed_[this.row_] = {
|
14080 | text: '',
|
14081 | indent: 0,
|
14082 | offset: 0
|
14083 | };
|
14084 | };
|
14085 |
|
14086 | Cea608Stream.prototype.paintOn = function (pts, text) {
|
14087 | var baseRow = this.displayed_[this.row_].text;
|
14088 | baseRow += text;
|
14089 | this.displayed_[this.row_].text = baseRow;
|
14090 | };
|
14091 |
|
14092 |
|
14093 | var captionStream = {
|
14094 | CaptionStream: CaptionStream$2,
|
14095 | Cea608Stream: Cea608Stream,
|
14096 | Cea708Stream: Cea708Stream
|
14097 | };
|
14098 | |
14099 |
|
14100 |
|
14101 |
|
14102 |
|
14103 |
|
14104 |
|
14105 | var streamTypes = {
|
14106 | H264_STREAM_TYPE: 0x1B,
|
14107 | ADTS_STREAM_TYPE: 0x0F,
|
14108 | METADATA_STREAM_TYPE: 0x15
|
14109 | };
|
14110 | |
14111 |
|
14112 |
|
14113 |
|
14114 |
|
14115 |
|
14116 |
|
14117 |
|
14118 |
|
14119 |
|
14120 |
|
14121 | var Stream$6 = stream;
|
14122 | var MAX_TS = 8589934592;
|
14123 | var RO_THRESH = 4294967296;
|
14124 | var TYPE_SHARED = 'shared';
|
14125 |
|
14126 | var handleRollover$1 = function (value, reference) {
|
14127 | var direction = 1;
|
14128 |
|
14129 | if (value > reference) {
|
14130 |
|
14131 |
|
14132 |
|
14133 |
|
14134 |
|
14135 |
|
14136 |
|
14137 | direction = -1;
|
14138 | }
|
14139 |
|
14140 |
|
14141 |
|
14142 | while (Math.abs(reference - value) > RO_THRESH) {
|
14143 | value += direction * MAX_TS;
|
14144 | }
|
14145 |
|
14146 | return value;
|
14147 | };
|
14148 |
|
14149 | var TimestampRolloverStream$1 = function (type) {
|
14150 | var lastDTS, referenceDTS;
|
14151 | TimestampRolloverStream$1.prototype.init.call(this);
|
14152 |
|
14153 |
|
14154 |
|
14155 | this.type_ = type || TYPE_SHARED;
|
14156 |
|
14157 | this.push = function (data) {
|
14158 | |
14159 |
|
14160 |
|
14161 |
|
14162 |
|
14163 |
|
14164 |
|
14165 |
|
14166 |
|
14167 |
|
14168 | if (data.type === 'metadata') {
|
14169 | this.trigger('data', data);
|
14170 | return;
|
14171 | }
|
14172 |
|
14173 |
|
14174 |
|
14175 | if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
|
14176 | return;
|
14177 | }
|
14178 |
|
14179 | if (referenceDTS === undefined) {
|
14180 | referenceDTS = data.dts;
|
14181 | }
|
14182 |
|
14183 | data.dts = handleRollover$1(data.dts, referenceDTS);
|
14184 | data.pts = handleRollover$1(data.pts, referenceDTS);
|
14185 | lastDTS = data.dts;
|
14186 | this.trigger('data', data);
|
14187 | };
|
14188 |
|
14189 | this.flush = function () {
|
14190 | referenceDTS = lastDTS;
|
14191 | this.trigger('done');
|
14192 | };
|
14193 |
|
14194 | this.endTimeline = function () {
|
14195 | this.flush();
|
14196 | this.trigger('endedtimeline');
|
14197 | };
|
14198 |
|
14199 | this.discontinuity = function () {
|
14200 | referenceDTS = void 0;
|
14201 | lastDTS = void 0;
|
14202 | };
|
14203 |
|
14204 | this.reset = function () {
|
14205 | this.discontinuity();
|
14206 | this.trigger('reset');
|
14207 | };
|
14208 | };
|
14209 |
|
14210 | TimestampRolloverStream$1.prototype = new Stream$6();
|
14211 | var timestampRolloverStream = {
|
14212 | TimestampRolloverStream: TimestampRolloverStream$1,
|
14213 | handleRollover: handleRollover$1
|
14214 | };
|
14215 |
|
14216 | var typedArrayIndexOf$1 = (typedArray, element, fromIndex) => {
|
14217 | if (!typedArray) {
|
14218 | return -1;
|
14219 | }
|
14220 |
|
14221 | var currentIndex = fromIndex;
|
14222 |
|
14223 | for (; currentIndex < typedArray.length; currentIndex++) {
|
14224 | if (typedArray[currentIndex] === element) {
|
14225 | return currentIndex;
|
14226 | }
|
14227 | }
|
14228 |
|
14229 | return -1;
|
14230 | };
|
14231 |
|
14232 | var typedArray = {
|
14233 | typedArrayIndexOf: typedArrayIndexOf$1
|
14234 | };
|
14235 | |
14236 |
|
14237 |
|
14238 |
|
14239 |
|
14240 |
|
14241 |
|
14242 |
|
14243 |
|
14244 |
|
14245 | var typedArrayIndexOf = typedArray.typedArrayIndexOf,
|
14246 |
|
14247 |
|
14248 | textEncodingDescriptionByte = {
|
14249 | Iso88591: 0x00,
|
14250 |
|
14251 | Utf16: 0x01,
|
14252 |
|
14253 | Utf16be: 0x02,
|
14254 |
|
14255 | Utf8: 0x03
|
14256 |
|
14257 | },
|
14258 |
|
14259 |
|
14260 | percentEncode$1 = function (bytes, start, end) {
|
14261 | var i,
|
14262 | result = '';
|
14263 |
|
14264 | for (i = start; i < end; i++) {
|
14265 | result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
|
14266 | }
|
14267 |
|
14268 | return result;
|
14269 | },
|
14270 |
|
14271 |
|
14272 | parseUtf8 = function (bytes, start, end) {
|
14273 | return decodeURIComponent(percentEncode$1(bytes, start, end));
|
14274 | },
|
14275 |
|
14276 |
|
14277 | parseIso88591$1 = function (bytes, start, end) {
|
14278 | return unescape(percentEncode$1(bytes, start, end));
|
14279 | },
|
14280 | parseSyncSafeInteger$1 = function (data) {
|
14281 | return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
|
14282 | },
|
14283 | frameParsers = {
|
14284 | 'APIC': function (frame) {
|
14285 | var i = 1,
|
14286 | mimeTypeEndIndex,
|
14287 | descriptionEndIndex,
|
14288 | LINK_MIME_TYPE = '-->';
|
14289 |
|
14290 | if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
|
14291 |
|
14292 | return;
|
14293 | }
|
14294 |
|
14295 |
|
14296 | mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i);
|
14297 |
|
14298 | if (mimeTypeEndIndex < 0) {
|
14299 |
|
14300 | return;
|
14301 | }
|
14302 |
|
14303 |
|
14304 | frame.mimeType = parseIso88591$1(frame.data, i, mimeTypeEndIndex);
|
14305 | i = mimeTypeEndIndex + 1;
|
14306 |
|
14307 | frame.pictureType = frame.data[i];
|
14308 | i++;
|
14309 | descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i);
|
14310 |
|
14311 | if (descriptionEndIndex < 0) {
|
14312 |
|
14313 | return;
|
14314 | }
|
14315 |
|
14316 |
|
14317 | frame.description = parseUtf8(frame.data, i, descriptionEndIndex);
|
14318 | i = descriptionEndIndex + 1;
|
14319 |
|
14320 | if (frame.mimeType === LINK_MIME_TYPE) {
|
14321 |
|
14322 | frame.url = parseIso88591$1(frame.data, i, frame.data.length);
|
14323 | } else {
|
14324 |
|
14325 | frame.pictureData = frame.data.subarray(i, frame.data.length);
|
14326 | }
|
14327 | },
|
14328 | 'T*': function (frame) {
|
14329 | if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
|
14330 |
|
14331 | return;
|
14332 | }
|
14333 |
|
14334 |
|
14335 |
|
14336 | frame.value = parseUtf8(frame.data, 1, frame.data.length).replace(/\0*$/, '');
|
14337 |
|
14338 | frame.values = frame.value.split('\0');
|
14339 | },
|
14340 | 'TXXX': function (frame) {
|
14341 | var descriptionEndIndex;
|
14342 |
|
14343 | if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
|
14344 |
|
14345 | return;
|
14346 | }
|
14347 |
|
14348 | descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
|
14349 |
|
14350 | if (descriptionEndIndex === -1) {
|
14351 | return;
|
14352 | }
|
14353 |
|
14354 |
|
14355 | frame.description = parseUtf8(frame.data, 1, descriptionEndIndex);
|
14356 |
|
14357 |
|
14358 |
|
14359 | frame.value = parseUtf8(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0*$/, '');
|
14360 | frame.data = frame.value;
|
14361 | },
|
14362 | 'W*': function (frame) {
|
14363 |
|
14364 |
|
14365 | frame.url = parseIso88591$1(frame.data, 0, frame.data.length).replace(/\0.*$/, '');
|
14366 | },
|
14367 | 'WXXX': function (frame) {
|
14368 | var descriptionEndIndex;
|
14369 |
|
14370 | if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
|
14371 |
|
14372 | return;
|
14373 | }
|
14374 |
|
14375 | descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
|
14376 |
|
14377 | if (descriptionEndIndex === -1) {
|
14378 | return;
|
14379 | }
|
14380 |
|
14381 |
|
14382 | frame.description = parseUtf8(frame.data, 1, descriptionEndIndex);
|
14383 |
|
14384 |
|
14385 |
|
14386 | frame.url = parseIso88591$1(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0.*$/, '');
|
14387 | },
|
14388 | 'PRIV': function (frame) {
|
14389 | var i;
|
14390 |
|
14391 | for (i = 0; i < frame.data.length; i++) {
|
14392 | if (frame.data[i] === 0) {
|
14393 |
|
14394 | frame.owner = parseIso88591$1(frame.data, 0, i);
|
14395 | break;
|
14396 | }
|
14397 | }
|
14398 |
|
14399 | frame.privateData = frame.data.subarray(i + 1);
|
14400 | frame.data = frame.privateData;
|
14401 | }
|
14402 | };
|
14403 |
|
14404 | var parseId3Frames$1 = function (data) {
|
14405 | var frameSize,
|
14406 | frameHeader,
|
14407 | frameStart = 10,
|
14408 | tagSize = 0,
|
14409 | frames = [];
|
14410 |
|
14411 |
|
14412 | if (data.length < 10 || data[0] !== 'I'.charCodeAt(0) || data[1] !== 'D'.charCodeAt(0) || data[2] !== '3'.charCodeAt(0)) {
|
14413 | return;
|
14414 | }
|
14415 |
|
14416 |
|
14417 |
|
14418 |
|
14419 |
|
14420 | tagSize = parseSyncSafeInteger$1(data.subarray(6, 10));
|
14421 |
|
14422 |
|
14423 | tagSize += 10;
|
14424 |
|
14425 | var hasExtendedHeader = data[5] & 0x40;
|
14426 |
|
14427 | if (hasExtendedHeader) {
|
14428 |
|
14429 | frameStart += 4;
|
14430 |
|
14431 | frameStart += parseSyncSafeInteger$1(data.subarray(10, 14));
|
14432 | tagSize -= parseSyncSafeInteger$1(data.subarray(16, 20));
|
14433 | }
|
14434 |
|
14435 |
|
14436 |
|
14437 | do {
|
14438 |
|
14439 | frameSize = parseSyncSafeInteger$1(data.subarray(frameStart + 4, frameStart + 8));
|
14440 |
|
14441 | if (frameSize < 1) {
|
14442 | break;
|
14443 | }
|
14444 |
|
14445 | frameHeader = String.fromCharCode(data[frameStart], data[frameStart + 1], data[frameStart + 2], data[frameStart + 3]);
|
14446 | var frame = {
|
14447 | id: frameHeader,
|
14448 | data: data.subarray(frameStart + 10, frameStart + frameSize + 10)
|
14449 | };
|
14450 | frame.key = frame.id;
|
14451 |
|
14452 | if (frameParsers[frame.id]) {
|
14453 |
|
14454 | frameParsers[frame.id](frame);
|
14455 | } else if (frame.id[0] === 'T') {
|
14456 |
|
14457 | frameParsers['T*'](frame);
|
14458 | } else if (frame.id[0] === 'W') {
|
14459 |
|
14460 | frameParsers['W*'](frame);
|
14461 | }
|
14462 |
|
14463 | frames.push(frame);
|
14464 | frameStart += 10;
|
14465 |
|
14466 | frameStart += frameSize;
|
14467 | } while (frameStart < tagSize);
|
14468 |
|
14469 | return frames;
|
14470 | };
|
14471 |
|
14472 | var parseId3 = {
|
14473 | parseId3Frames: parseId3Frames$1,
|
14474 | parseSyncSafeInteger: parseSyncSafeInteger$1,
|
14475 | frameParsers: frameParsers
|
14476 | };
|
14477 | |
14478 |
|
14479 |
|
14480 |
|
14481 |
|
14482 |
|
14483 |
|
14484 |
|
14485 |
|
14486 |
|
14487 |
|
14488 | var Stream$5 = stream,
|
14489 | StreamTypes$3 = streamTypes,
|
14490 | id3 = parseId3,
|
14491 | MetadataStream;
|
14492 |
|
14493 | MetadataStream = function (options) {
|
14494 | var settings = {
|
14495 |
|
14496 |
|
14497 |
|
14498 | descriptor: options && options.descriptor
|
14499 | },
|
14500 |
|
14501 | tagSize = 0,
|
14502 |
|
14503 | buffer = [],
|
14504 |
|
14505 | bufferSize = 0,
|
14506 | i;
|
14507 | MetadataStream.prototype.init.call(this);
|
14508 |
|
14509 |
|
14510 | this.dispatchType = StreamTypes$3.METADATA_STREAM_TYPE.toString(16);
|
14511 |
|
14512 | if (settings.descriptor) {
|
14513 | for (i = 0; i < settings.descriptor.length; i++) {
|
14514 | this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
|
14515 | }
|
14516 | }
|
14517 |
|
14518 | this.push = function (chunk) {
|
14519 | var tag, frameStart, frameSize, frame, i, frameHeader;
|
14520 |
|
14521 | if (chunk.type !== 'timed-metadata') {
|
14522 | return;
|
14523 | }
|
14524 |
|
14525 |
|
14526 |
|
14527 |
|
14528 | if (chunk.dataAlignmentIndicator) {
|
14529 | bufferSize = 0;
|
14530 | buffer.length = 0;
|
14531 | }
|
14532 |
|
14533 |
|
14534 | 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))) {
|
14535 | this.trigger('log', {
|
14536 | level: 'warn',
|
14537 | message: 'Skipping unrecognized metadata packet'
|
14538 | });
|
14539 | return;
|
14540 | }
|
14541 |
|
14542 |
|
14543 | buffer.push(chunk);
|
14544 | bufferSize += chunk.data.byteLength;
|
14545 |
|
14546 | if (buffer.length === 1) {
|
14547 |
|
14548 |
|
14549 |
|
14550 |
|
14551 | tagSize = id3.parseSyncSafeInteger(chunk.data.subarray(6, 10));
|
14552 |
|
14553 |
|
14554 | tagSize += 10;
|
14555 | }
|
14556 |
|
14557 |
|
14558 | if (bufferSize < tagSize) {
|
14559 | return;
|
14560 | }
|
14561 |
|
14562 |
|
14563 | tag = {
|
14564 | data: new Uint8Array(tagSize),
|
14565 | frames: [],
|
14566 | pts: buffer[0].pts,
|
14567 | dts: buffer[0].dts
|
14568 | };
|
14569 |
|
14570 | for (i = 0; i < tagSize;) {
|
14571 | tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
|
14572 | i += buffer[0].data.byteLength;
|
14573 | bufferSize -= buffer[0].data.byteLength;
|
14574 | buffer.shift();
|
14575 | }
|
14576 |
|
14577 |
|
14578 | frameStart = 10;
|
14579 |
|
14580 | if (tag.data[5] & 0x40) {
|
14581 |
|
14582 | frameStart += 4;
|
14583 |
|
14584 | frameStart += id3.parseSyncSafeInteger(tag.data.subarray(10, 14));
|
14585 |
|
14586 | tagSize -= id3.parseSyncSafeInteger(tag.data.subarray(16, 20));
|
14587 | }
|
14588 |
|
14589 |
|
14590 |
|
14591 | do {
|
14592 |
|
14593 | frameSize = id3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
|
14594 |
|
14595 | if (frameSize < 1) {
|
14596 | this.trigger('log', {
|
14597 | level: 'warn',
|
14598 | message: 'Malformed ID3 frame encountered. Skipping remaining metadata parsing.'
|
14599 | });
|
14600 |
|
14601 |
|
14602 | break;
|
14603 | }
|
14604 |
|
14605 | frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
|
14606 | frame = {
|
14607 | id: frameHeader,
|
14608 | data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
|
14609 | };
|
14610 | frame.key = frame.id;
|
14611 |
|
14612 | if (id3.frameParsers[frame.id]) {
|
14613 |
|
14614 | id3.frameParsers[frame.id](frame);
|
14615 | } else if (frame.id[0] === 'T') {
|
14616 |
|
14617 | id3.frameParsers['T*'](frame);
|
14618 | } else if (frame.id[0] === 'W') {
|
14619 |
|
14620 | id3.frameParsers['W*'](frame);
|
14621 | }
|
14622 |
|
14623 |
|
14624 |
|
14625 | if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
|
14626 | var d = frame.data,
|
14627 | size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
|
14628 | size *= 4;
|
14629 | size += d[7] & 0x03;
|
14630 | frame.timeStamp = size;
|
14631 |
|
14632 |
|
14633 |
|
14634 |
|
14635 | if (tag.pts === undefined && tag.dts === undefined) {
|
14636 | tag.pts = frame.timeStamp;
|
14637 | tag.dts = frame.timeStamp;
|
14638 | }
|
14639 |
|
14640 | this.trigger('timestamp', frame);
|
14641 | }
|
14642 |
|
14643 | tag.frames.push(frame);
|
14644 | frameStart += 10;
|
14645 |
|
14646 | frameStart += frameSize;
|
14647 | } while (frameStart < tagSize);
|
14648 |
|
14649 | this.trigger('data', tag);
|
14650 | };
|
14651 | };
|
14652 |
|
14653 | MetadataStream.prototype = new Stream$5();
|
14654 | var metadataStream = MetadataStream;
|
14655 | |
14656 |
|
14657 |
|
14658 |
|
14659 |
|
14660 |
|
14661 |
|
14662 |
|
14663 |
|
14664 |
|
14665 |
|
14666 | var Stream$4 = stream,
|
14667 | CaptionStream$1 = captionStream,
|
14668 | StreamTypes$2 = streamTypes,
|
14669 | TimestampRolloverStream = timestampRolloverStream.TimestampRolloverStream;
|
14670 |
|
14671 | var TransportPacketStream, TransportParseStream, ElementaryStream;
|
14672 |
|
14673 | var MP2T_PACKET_LENGTH$1 = 188,
|
14674 |
|
14675 | SYNC_BYTE$1 = 0x47;
|
14676 | |
14677 |
|
14678 |
|
14679 |
|
14680 |
|
14681 | TransportPacketStream = function () {
|
14682 | var buffer = new Uint8Array(MP2T_PACKET_LENGTH$1),
|
14683 | bytesInBuffer = 0;
|
14684 | TransportPacketStream.prototype.init.call(this);
|
14685 |
|
14686 | |
14687 |
|
14688 |
|
14689 |
|
14690 | this.push = function (bytes) {
|
14691 | var startIndex = 0,
|
14692 | endIndex = MP2T_PACKET_LENGTH$1,
|
14693 | everything;
|
14694 |
|
14695 |
|
14696 | if (bytesInBuffer) {
|
14697 | everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
|
14698 | everything.set(buffer.subarray(0, bytesInBuffer));
|
14699 | everything.set(bytes, bytesInBuffer);
|
14700 | bytesInBuffer = 0;
|
14701 | } else {
|
14702 | everything = bytes;
|
14703 | }
|
14704 |
|
14705 |
|
14706 | while (endIndex < everything.byteLength) {
|
14707 |
|
14708 | if (everything[startIndex] === SYNC_BYTE$1 && everything[endIndex] === SYNC_BYTE$1) {
|
14709 |
|
14710 |
|
14711 | this.trigger('data', everything.subarray(startIndex, endIndex));
|
14712 | startIndex += MP2T_PACKET_LENGTH$1;
|
14713 | endIndex += MP2T_PACKET_LENGTH$1;
|
14714 | continue;
|
14715 | }
|
14716 |
|
14717 |
|
14718 |
|
14719 |
|
14720 | startIndex++;
|
14721 | endIndex++;
|
14722 | }
|
14723 |
|
14724 |
|
14725 |
|
14726 |
|
14727 | if (startIndex < everything.byteLength) {
|
14728 | buffer.set(everything.subarray(startIndex), 0);
|
14729 | bytesInBuffer = everything.byteLength - startIndex;
|
14730 | }
|
14731 | };
|
14732 | |
14733 |
|
14734 |
|
14735 |
|
14736 |
|
14737 | this.flush = function () {
|
14738 |
|
14739 |
|
14740 |
|
14741 | if (bytesInBuffer === MP2T_PACKET_LENGTH$1 && buffer[0] === SYNC_BYTE$1) {
|
14742 | this.trigger('data', buffer);
|
14743 | bytesInBuffer = 0;
|
14744 | }
|
14745 |
|
14746 | this.trigger('done');
|
14747 | };
|
14748 |
|
14749 | this.endTimeline = function () {
|
14750 | this.flush();
|
14751 | this.trigger('endedtimeline');
|
14752 | };
|
14753 |
|
14754 | this.reset = function () {
|
14755 | bytesInBuffer = 0;
|
14756 | this.trigger('reset');
|
14757 | };
|
14758 | };
|
14759 |
|
14760 | TransportPacketStream.prototype = new Stream$4();
|
14761 | |
14762 |
|
14763 |
|
14764 |
|
14765 |
|
14766 | TransportParseStream = function () {
|
14767 | var parsePsi, parsePat, parsePmt, self;
|
14768 | TransportParseStream.prototype.init.call(this);
|
14769 | self = this;
|
14770 | this.packetsWaitingForPmt = [];
|
14771 | this.programMapTable = undefined;
|
14772 |
|
14773 | parsePsi = function (payload, psi) {
|
14774 | var offset = 0;
|
14775 |
|
14776 |
|
14777 |
|
14778 |
|
14779 |
|
14780 |
|
14781 | if (psi.payloadUnitStartIndicator) {
|
14782 | offset += payload[offset] + 1;
|
14783 | }
|
14784 |
|
14785 | if (psi.type === 'pat') {
|
14786 | parsePat(payload.subarray(offset), psi);
|
14787 | } else {
|
14788 | parsePmt(payload.subarray(offset), psi);
|
14789 | }
|
14790 | };
|
14791 |
|
14792 | parsePat = function (payload, pat) {
|
14793 | pat.section_number = payload[7];
|
14794 |
|
14795 | pat.last_section_number = payload[8];
|
14796 |
|
14797 |
|
14798 | self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
|
14799 | pat.pmtPid = self.pmtPid;
|
14800 | };
|
14801 | |
14802 |
|
14803 |
|
14804 |
|
14805 |
|
14806 |
|
14807 |
|
14808 |
|
14809 |
|
14810 |
|
14811 | parsePmt = function (payload, pmt) {
|
14812 | var sectionLength, tableEnd, programInfoLength, offset;
|
14813 |
|
14814 |
|
14815 |
|
14816 |
|
14817 |
|
14818 | if (!(payload[5] & 0x01)) {
|
14819 | return;
|
14820 | }
|
14821 |
|
14822 |
|
14823 | self.programMapTable = {
|
14824 | video: null,
|
14825 | audio: null,
|
14826 | 'timed-metadata': {}
|
14827 | };
|
14828 |
|
14829 | sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
|
14830 | tableEnd = 3 + sectionLength - 4;
|
14831 |
|
14832 |
|
14833 | programInfoLength = (payload[10] & 0x0f) << 8 | payload[11];
|
14834 |
|
14835 | offset = 12 + programInfoLength;
|
14836 |
|
14837 | while (offset < tableEnd) {
|
14838 | var streamType = payload[offset];
|
14839 | var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2];
|
14840 |
|
14841 |
|
14842 |
|
14843 | if (streamType === StreamTypes$2.H264_STREAM_TYPE && self.programMapTable.video === null) {
|
14844 | self.programMapTable.video = pid;
|
14845 | } else if (streamType === StreamTypes$2.ADTS_STREAM_TYPE && self.programMapTable.audio === null) {
|
14846 | self.programMapTable.audio = pid;
|
14847 | } else if (streamType === StreamTypes$2.METADATA_STREAM_TYPE) {
|
14848 |
|
14849 | self.programMapTable['timed-metadata'][pid] = streamType;
|
14850 | }
|
14851 |
|
14852 |
|
14853 |
|
14854 | offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
|
14855 | }
|
14856 |
|
14857 |
|
14858 | pmt.programMapTable = self.programMapTable;
|
14859 | };
|
14860 | |
14861 |
|
14862 |
|
14863 |
|
14864 |
|
14865 | this.push = function (packet) {
|
14866 | var result = {},
|
14867 | offset = 4;
|
14868 | result.payloadUnitStartIndicator = !!(packet[1] & 0x40);
|
14869 |
|
14870 | result.pid = packet[1] & 0x1f;
|
14871 | result.pid <<= 8;
|
14872 | result.pid |= packet[2];
|
14873 |
|
14874 |
|
14875 |
|
14876 |
|
14877 |
|
14878 | if ((packet[3] & 0x30) >>> 4 > 0x01) {
|
14879 | offset += packet[offset] + 1;
|
14880 | }
|
14881 |
|
14882 |
|
14883 | if (result.pid === 0) {
|
14884 | result.type = 'pat';
|
14885 | parsePsi(packet.subarray(offset), result);
|
14886 | this.trigger('data', result);
|
14887 | } else if (result.pid === this.pmtPid) {
|
14888 | result.type = 'pmt';
|
14889 | parsePsi(packet.subarray(offset), result);
|
14890 | this.trigger('data', result);
|
14891 |
|
14892 | while (this.packetsWaitingForPmt.length) {
|
14893 | this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
|
14894 | }
|
14895 | } else if (this.programMapTable === undefined) {
|
14896 |
|
14897 |
|
14898 | this.packetsWaitingForPmt.push([packet, offset, result]);
|
14899 | } else {
|
14900 | this.processPes_(packet, offset, result);
|
14901 | }
|
14902 | };
|
14903 |
|
14904 | this.processPes_ = function (packet, offset, result) {
|
14905 |
|
14906 | if (result.pid === this.programMapTable.video) {
|
14907 | result.streamType = StreamTypes$2.H264_STREAM_TYPE;
|
14908 | } else if (result.pid === this.programMapTable.audio) {
|
14909 | result.streamType = StreamTypes$2.ADTS_STREAM_TYPE;
|
14910 | } else {
|
14911 |
|
14912 |
|
14913 | result.streamType = this.programMapTable['timed-metadata'][result.pid];
|
14914 | }
|
14915 |
|
14916 | result.type = 'pes';
|
14917 | result.data = packet.subarray(offset);
|
14918 | this.trigger('data', result);
|
14919 | };
|
14920 | };
|
14921 |
|
14922 | TransportParseStream.prototype = new Stream$4();
|
14923 | TransportParseStream.STREAM_TYPES = {
|
14924 | h264: 0x1b,
|
14925 | adts: 0x0f
|
14926 | };
|
14927 | |
14928 |
|
14929 |
|
14930 |
|
14931 |
|
14932 |
|
14933 |
|
14934 |
|
14935 |
|
14936 | ElementaryStream = function () {
|
14937 | var self = this,
|
14938 | segmentHadPmt = false,
|
14939 |
|
14940 | video = {
|
14941 | data: [],
|
14942 | size: 0
|
14943 | },
|
14944 | audio = {
|
14945 | data: [],
|
14946 | size: 0
|
14947 | },
|
14948 | timedMetadata = {
|
14949 | data: [],
|
14950 | size: 0
|
14951 | },
|
14952 | programMapTable,
|
14953 | parsePes = function (payload, pes) {
|
14954 | var ptsDtsFlags;
|
14955 | const startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2];
|
14956 |
|
14957 | pes.data = new Uint8Array();
|
14958 |
|
14959 |
|
14960 |
|
14961 | if (startPrefix !== 1) {
|
14962 | return;
|
14963 | }
|
14964 |
|
14965 |
|
14966 | pes.packetLength = 6 + (payload[4] << 8 | payload[5]);
|
14967 |
|
14968 | pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0;
|
14969 |
|
14970 |
|
14971 |
|
14972 | ptsDtsFlags = payload[7];
|
14973 |
|
14974 |
|
14975 |
|
14976 |
|
14977 |
|
14978 |
|
14979 |
|
14980 |
|
14981 | if (ptsDtsFlags & 0xC0) {
|
14982 |
|
14983 |
|
14984 |
|
14985 | pes.pts = (payload[9] & 0x0E) << 27 | (payload[10] & 0xFF) << 20 | (payload[11] & 0xFE) << 12 | (payload[12] & 0xFF) << 5 | (payload[13] & 0xFE) >>> 3;
|
14986 | pes.pts *= 4;
|
14987 |
|
14988 | pes.pts += (payload[13] & 0x06) >>> 1;
|
14989 |
|
14990 | pes.dts = pes.pts;
|
14991 |
|
14992 | if (ptsDtsFlags & 0x40) {
|
14993 | pes.dts = (payload[14] & 0x0E) << 27 | (payload[15] & 0xFF) << 20 | (payload[16] & 0xFE) << 12 | (payload[17] & 0xFF) << 5 | (payload[18] & 0xFE) >>> 3;
|
14994 | pes.dts *= 4;
|
14995 |
|
14996 | pes.dts += (payload[18] & 0x06) >>> 1;
|
14997 | }
|
14998 | }
|
14999 |
|
15000 |
|
15001 |
|
15002 |
|
15003 | pes.data = payload.subarray(9 + payload[8]);
|
15004 | },
|
15005 |
|
15006 | |
15007 |
|
15008 |
|
15009 | flushStream = function (stream, type, forceFlush) {
|
15010 | var packetData = new Uint8Array(stream.size),
|
15011 | event = {
|
15012 | type: type
|
15013 | },
|
15014 | i = 0,
|
15015 | offset = 0,
|
15016 | packetFlushable = false,
|
15017 | fragment;
|
15018 |
|
15019 |
|
15020 | if (!stream.data.length || stream.size < 9) {
|
15021 | return;
|
15022 | }
|
15023 |
|
15024 | event.trackId = stream.data[0].pid;
|
15025 |
|
15026 | for (i = 0; i < stream.data.length; i++) {
|
15027 | fragment = stream.data[i];
|
15028 | packetData.set(fragment.data, offset);
|
15029 | offset += fragment.data.byteLength;
|
15030 | }
|
15031 |
|
15032 |
|
15033 | parsePes(packetData, event);
|
15034 |
|
15035 |
|
15036 | packetFlushable = type === 'video' || event.packetLength <= stream.size;
|
15037 |
|
15038 | if (forceFlush || packetFlushable) {
|
15039 | stream.size = 0;
|
15040 | stream.data.length = 0;
|
15041 | }
|
15042 |
|
15043 |
|
15044 |
|
15045 | if (packetFlushable) {
|
15046 | self.trigger('data', event);
|
15047 | }
|
15048 | };
|
15049 |
|
15050 | ElementaryStream.prototype.init.call(this);
|
15051 | |
15052 |
|
15053 |
|
15054 |
|
15055 |
|
15056 | this.push = function (data) {
|
15057 | ({
|
15058 | pat: function () {
|
15059 |
|
15060 | },
|
15061 | pes: function () {
|
15062 | var stream, streamType;
|
15063 |
|
15064 | switch (data.streamType) {
|
15065 | case StreamTypes$2.H264_STREAM_TYPE:
|
15066 | stream = video;
|
15067 | streamType = 'video';
|
15068 | break;
|
15069 |
|
15070 | case StreamTypes$2.ADTS_STREAM_TYPE:
|
15071 | stream = audio;
|
15072 | streamType = 'audio';
|
15073 | break;
|
15074 |
|
15075 | case StreamTypes$2.METADATA_STREAM_TYPE:
|
15076 | stream = timedMetadata;
|
15077 | streamType = 'timed-metadata';
|
15078 | break;
|
15079 |
|
15080 | default:
|
15081 |
|
15082 | return;
|
15083 | }
|
15084 |
|
15085 |
|
15086 |
|
15087 | if (data.payloadUnitStartIndicator) {
|
15088 | flushStream(stream, streamType, true);
|
15089 | }
|
15090 |
|
15091 |
|
15092 |
|
15093 | stream.data.push(data);
|
15094 | stream.size += data.data.byteLength;
|
15095 | },
|
15096 | pmt: function () {
|
15097 | var event = {
|
15098 | type: 'metadata',
|
15099 | tracks: []
|
15100 | };
|
15101 | programMapTable = data.programMapTable;
|
15102 |
|
15103 | if (programMapTable.video !== null) {
|
15104 | event.tracks.push({
|
15105 | timelineStartInfo: {
|
15106 | baseMediaDecodeTime: 0
|
15107 | },
|
15108 | id: +programMapTable.video,
|
15109 | codec: 'avc',
|
15110 | type: 'video'
|
15111 | });
|
15112 | }
|
15113 |
|
15114 | if (programMapTable.audio !== null) {
|
15115 | event.tracks.push({
|
15116 | timelineStartInfo: {
|
15117 | baseMediaDecodeTime: 0
|
15118 | },
|
15119 | id: +programMapTable.audio,
|
15120 | codec: 'adts',
|
15121 | type: 'audio'
|
15122 | });
|
15123 | }
|
15124 |
|
15125 | segmentHadPmt = true;
|
15126 | self.trigger('data', event);
|
15127 | }
|
15128 | })[data.type]();
|
15129 | };
|
15130 |
|
15131 | this.reset = function () {
|
15132 | video.size = 0;
|
15133 | video.data.length = 0;
|
15134 | audio.size = 0;
|
15135 | audio.data.length = 0;
|
15136 | this.trigger('reset');
|
15137 | };
|
15138 | |
15139 |
|
15140 |
|
15141 |
|
15142 |
|
15143 |
|
15144 |
|
15145 |
|
15146 |
|
15147 |
|
15148 |
|
15149 | this.flushStreams_ = function () {
|
15150 |
|
15151 |
|
15152 | flushStream(video, 'video');
|
15153 | flushStream(audio, 'audio');
|
15154 | flushStream(timedMetadata, 'timed-metadata');
|
15155 | };
|
15156 |
|
15157 | this.flush = function () {
|
15158 |
|
15159 |
|
15160 |
|
15161 | if (!segmentHadPmt && programMapTable) {
|
15162 | var pmt = {
|
15163 | type: 'metadata',
|
15164 | tracks: []
|
15165 | };
|
15166 |
|
15167 | if (programMapTable.video !== null) {
|
15168 | pmt.tracks.push({
|
15169 | timelineStartInfo: {
|
15170 | baseMediaDecodeTime: 0
|
15171 | },
|
15172 | id: +programMapTable.video,
|
15173 | codec: 'avc',
|
15174 | type: 'video'
|
15175 | });
|
15176 | }
|
15177 |
|
15178 | if (programMapTable.audio !== null) {
|
15179 | pmt.tracks.push({
|
15180 | timelineStartInfo: {
|
15181 | baseMediaDecodeTime: 0
|
15182 | },
|
15183 | id: +programMapTable.audio,
|
15184 | codec: 'adts',
|
15185 | type: 'audio'
|
15186 | });
|
15187 | }
|
15188 |
|
15189 | self.trigger('data', pmt);
|
15190 | }
|
15191 |
|
15192 | segmentHadPmt = false;
|
15193 | this.flushStreams_();
|
15194 | this.trigger('done');
|
15195 | };
|
15196 | };
|
15197 |
|
15198 | ElementaryStream.prototype = new Stream$4();
|
15199 | var m2ts$1 = {
|
15200 | PAT_PID: 0x0000,
|
15201 | MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH$1,
|
15202 | TransportPacketStream: TransportPacketStream,
|
15203 | TransportParseStream: TransportParseStream,
|
15204 | ElementaryStream: ElementaryStream,
|
15205 | TimestampRolloverStream: TimestampRolloverStream,
|
15206 | CaptionStream: CaptionStream$1.CaptionStream,
|
15207 | Cea608Stream: CaptionStream$1.Cea608Stream,
|
15208 | Cea708Stream: CaptionStream$1.Cea708Stream,
|
15209 | MetadataStream: metadataStream
|
15210 | };
|
15211 |
|
15212 | for (var type in StreamTypes$2) {
|
15213 | if (StreamTypes$2.hasOwnProperty(type)) {
|
15214 | m2ts$1[type] = StreamTypes$2[type];
|
15215 | }
|
15216 | }
|
15217 |
|
15218 | var m2ts_1 = m2ts$1;
|
15219 | |
15220 |
|
15221 |
|
15222 |
|
15223 |
|
15224 |
|
15225 |
|
15226 | var Stream$3 = stream;
|
15227 | var ONE_SECOND_IN_TS$2 = clock$2.ONE_SECOND_IN_TS;
|
15228 | var AdtsStream$1;
|
15229 | var ADTS_SAMPLING_FREQUENCIES$1 = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
15230 | |
15231 |
|
15232 |
|
15233 |
|
15234 |
|
15235 |
|
15236 |
|
15237 |
|
15238 |
|
15239 | AdtsStream$1 = function (handlePartialSegments) {
|
15240 | var buffer,
|
15241 | frameNum = 0;
|
15242 | AdtsStream$1.prototype.init.call(this);
|
15243 |
|
15244 | this.skipWarn_ = function (start, end) {
|
15245 | this.trigger('log', {
|
15246 | level: 'warn',
|
15247 | message: `adts skiping bytes ${start} to ${end} in frame ${frameNum} outside syncword`
|
15248 | });
|
15249 | };
|
15250 |
|
15251 | this.push = function (packet) {
|
15252 | var i = 0,
|
15253 | frameLength,
|
15254 | protectionSkipBytes,
|
15255 | oldBuffer,
|
15256 | sampleCount,
|
15257 | adtsFrameDuration;
|
15258 |
|
15259 | if (!handlePartialSegments) {
|
15260 | frameNum = 0;
|
15261 | }
|
15262 |
|
15263 | if (packet.type !== 'audio') {
|
15264 |
|
15265 | return;
|
15266 | }
|
15267 |
|
15268 |
|
15269 |
|
15270 | if (buffer && buffer.length) {
|
15271 | oldBuffer = buffer;
|
15272 | buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
|
15273 | buffer.set(oldBuffer);
|
15274 | buffer.set(packet.data, oldBuffer.byteLength);
|
15275 | } else {
|
15276 | buffer = packet.data;
|
15277 | }
|
15278 |
|
15279 |
|
15280 |
|
15281 | var skip;
|
15282 |
|
15283 |
|
15284 | while (i + 7 < buffer.length) {
|
15285 |
|
15286 | if (buffer[i] !== 0xFF || (buffer[i + 1] & 0xF6) !== 0xF0) {
|
15287 | if (typeof skip !== 'number') {
|
15288 | skip = i;
|
15289 | }
|
15290 |
|
15291 |
|
15292 |
|
15293 | i++;
|
15294 | continue;
|
15295 | }
|
15296 |
|
15297 | if (typeof skip === 'number') {
|
15298 | this.skipWarn_(skip, i);
|
15299 | skip = null;
|
15300 | }
|
15301 |
|
15302 |
|
15303 |
|
15304 | protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2;
|
15305 |
|
15306 |
|
15307 |
|
15308 | frameLength = (buffer[i + 3] & 0x03) << 11 | buffer[i + 4] << 3 | (buffer[i + 5] & 0xe0) >> 5;
|
15309 | sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
|
15310 | adtsFrameDuration = sampleCount * ONE_SECOND_IN_TS$2 / ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2];
|
15311 |
|
15312 |
|
15313 | if (buffer.byteLength - i < frameLength) {
|
15314 | break;
|
15315 | }
|
15316 |
|
15317 |
|
15318 | this.trigger('data', {
|
15319 | pts: packet.pts + frameNum * adtsFrameDuration,
|
15320 | dts: packet.dts + frameNum * adtsFrameDuration,
|
15321 | sampleCount: sampleCount,
|
15322 | audioobjecttype: (buffer[i + 2] >>> 6 & 0x03) + 1,
|
15323 | channelcount: (buffer[i + 2] & 1) << 2 | (buffer[i + 3] & 0xc0) >>> 6,
|
15324 | samplerate: ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2],
|
15325 | samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
|
15326 |
|
15327 | samplesize: 16,
|
15328 |
|
15329 | data: buffer.subarray(i + 7 + protectionSkipBytes, i + frameLength)
|
15330 | });
|
15331 | frameNum++;
|
15332 | i += frameLength;
|
15333 | }
|
15334 |
|
15335 | if (typeof skip === 'number') {
|
15336 | this.skipWarn_(skip, i);
|
15337 | skip = null;
|
15338 | }
|
15339 |
|
15340 |
|
15341 | buffer = buffer.subarray(i);
|
15342 | };
|
15343 |
|
15344 | this.flush = function () {
|
15345 | frameNum = 0;
|
15346 | this.trigger('done');
|
15347 | };
|
15348 |
|
15349 | this.reset = function () {
|
15350 | buffer = void 0;
|
15351 | this.trigger('reset');
|
15352 | };
|
15353 |
|
15354 | this.endTimeline = function () {
|
15355 | buffer = void 0;
|
15356 | this.trigger('endedtimeline');
|
15357 | };
|
15358 | };
|
15359 |
|
15360 | AdtsStream$1.prototype = new Stream$3();
|
15361 | var adts = AdtsStream$1;
|
15362 | |
15363 |
|
15364 |
|
15365 |
|
15366 |
|
15367 |
|
15368 |
|
15369 | var ExpGolomb$1;
|
15370 | |
15371 |
|
15372 |
|
15373 |
|
15374 |
|
15375 | ExpGolomb$1 = function (workingData) {
|
15376 | var
|
15377 | workingBytesAvailable = workingData.byteLength,
|
15378 |
|
15379 | workingWord = 0,
|
15380 |
|
15381 |
|
15382 | workingBitsAvailable = 0;
|
15383 |
|
15384 |
|
15385 | this.length = function () {
|
15386 | return 8 * workingBytesAvailable;
|
15387 | };
|
15388 |
|
15389 |
|
15390 | this.bitsAvailable = function () {
|
15391 | return 8 * workingBytesAvailable + workingBitsAvailable;
|
15392 | };
|
15393 |
|
15394 |
|
15395 | this.loadWord = function () {
|
15396 | var position = workingData.byteLength - workingBytesAvailable,
|
15397 | workingBytes = new Uint8Array(4),
|
15398 | availableBytes = Math.min(4, workingBytesAvailable);
|
15399 |
|
15400 | if (availableBytes === 0) {
|
15401 | throw new Error('no bytes available');
|
15402 | }
|
15403 |
|
15404 | workingBytes.set(workingData.subarray(position, position + availableBytes));
|
15405 | workingWord = new DataView(workingBytes.buffer).getUint32(0);
|
15406 |
|
15407 | workingBitsAvailable = availableBytes * 8;
|
15408 | workingBytesAvailable -= availableBytes;
|
15409 | };
|
15410 |
|
15411 |
|
15412 | this.skipBits = function (count) {
|
15413 | var skipBytes;
|
15414 |
|
15415 | if (workingBitsAvailable > count) {
|
15416 | workingWord <<= count;
|
15417 | workingBitsAvailable -= count;
|
15418 | } else {
|
15419 | count -= workingBitsAvailable;
|
15420 | skipBytes = Math.floor(count / 8);
|
15421 | count -= skipBytes * 8;
|
15422 | workingBytesAvailable -= skipBytes;
|
15423 | this.loadWord();
|
15424 | workingWord <<= count;
|
15425 | workingBitsAvailable -= count;
|
15426 | }
|
15427 | };
|
15428 |
|
15429 |
|
15430 | this.readBits = function (size) {
|
15431 | var bits = Math.min(workingBitsAvailable, size),
|
15432 |
|
15433 | valu = workingWord >>> 32 - bits;
|
15434 |
|
15435 |
|
15436 | workingBitsAvailable -= bits;
|
15437 |
|
15438 | if (workingBitsAvailable > 0) {
|
15439 | workingWord <<= bits;
|
15440 | } else if (workingBytesAvailable > 0) {
|
15441 | this.loadWord();
|
15442 | }
|
15443 |
|
15444 | bits = size - bits;
|
15445 |
|
15446 | if (bits > 0) {
|
15447 | return valu << bits | this.readBits(bits);
|
15448 | }
|
15449 |
|
15450 | return valu;
|
15451 | };
|
15452 |
|
15453 |
|
15454 | this.skipLeadingZeros = function () {
|
15455 | var leadingZeroCount;
|
15456 |
|
15457 | for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
|
15458 | if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
|
15459 |
|
15460 | workingWord <<= leadingZeroCount;
|
15461 | workingBitsAvailable -= leadingZeroCount;
|
15462 | return leadingZeroCount;
|
15463 | }
|
15464 | }
|
15465 |
|
15466 |
|
15467 | this.loadWord();
|
15468 | return leadingZeroCount + this.skipLeadingZeros();
|
15469 | };
|
15470 |
|
15471 |
|
15472 | this.skipUnsignedExpGolomb = function () {
|
15473 | this.skipBits(1 + this.skipLeadingZeros());
|
15474 | };
|
15475 |
|
15476 |
|
15477 | this.skipExpGolomb = function () {
|
15478 | this.skipBits(1 + this.skipLeadingZeros());
|
15479 | };
|
15480 |
|
15481 |
|
15482 | this.readUnsignedExpGolomb = function () {
|
15483 | var clz = this.skipLeadingZeros();
|
15484 |
|
15485 | return this.readBits(clz + 1) - 1;
|
15486 | };
|
15487 |
|
15488 |
|
15489 | this.readExpGolomb = function () {
|
15490 | var valu = this.readUnsignedExpGolomb();
|
15491 |
|
15492 | if (0x01 & valu) {
|
15493 |
|
15494 | return 1 + valu >>> 1;
|
15495 | }
|
15496 |
|
15497 | return -1 * (valu >>> 1);
|
15498 | };
|
15499 |
|
15500 |
|
15501 |
|
15502 | this.readBoolean = function () {
|
15503 | return this.readBits(1) === 1;
|
15504 | };
|
15505 |
|
15506 |
|
15507 | this.readUnsignedByte = function () {
|
15508 | return this.readBits(8);
|
15509 | };
|
15510 |
|
15511 | this.loadWord();
|
15512 | };
|
15513 |
|
15514 | var expGolomb = ExpGolomb$1;
|
15515 | |
15516 |
|
15517 |
|
15518 |
|
15519 |
|
15520 |
|
15521 |
|
15522 | var Stream$2 = stream;
|
15523 | var ExpGolomb = expGolomb;
|
15524 | var H264Stream$1, NalByteStream;
|
15525 | var PROFILES_WITH_OPTIONAL_SPS_DATA;
|
15526 | |
15527 |
|
15528 |
|
15529 |
|
15530 | NalByteStream = function () {
|
15531 | var syncPoint = 0,
|
15532 | i,
|
15533 | buffer;
|
15534 | NalByteStream.prototype.init.call(this);
|
15535 | |
15536 |
|
15537 |
|
15538 |
|
15539 |
|
15540 |
|
15541 |
|
15542 |
|
15543 | this.push = function (data) {
|
15544 | var swapBuffer;
|
15545 |
|
15546 | if (!buffer) {
|
15547 | buffer = data.data;
|
15548 | } else {
|
15549 | swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
|
15550 | swapBuffer.set(buffer);
|
15551 | swapBuffer.set(data.data, buffer.byteLength);
|
15552 | buffer = swapBuffer;
|
15553 | }
|
15554 |
|
15555 | var len = buffer.byteLength;
|
15556 |
|
15557 |
|
15558 |
|
15559 |
|
15560 |
|
15561 |
|
15562 |
|
15563 |
|
15564 |
|
15565 | for (; syncPoint < len - 3; syncPoint++) {
|
15566 | if (buffer[syncPoint + 2] === 1) {
|
15567 |
|
15568 | i = syncPoint + 5;
|
15569 | break;
|
15570 | }
|
15571 | }
|
15572 |
|
15573 | while (i < len) {
|
15574 |
|
15575 |
|
15576 | switch (buffer[i]) {
|
15577 | case 0:
|
15578 |
|
15579 | if (buffer[i - 1] !== 0) {
|
15580 | i += 2;
|
15581 | break;
|
15582 | } else if (buffer[i - 2] !== 0) {
|
15583 | i++;
|
15584 | break;
|
15585 | }
|
15586 |
|
15587 |
|
15588 | if (syncPoint + 3 !== i - 2) {
|
15589 | this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
|
15590 | }
|
15591 |
|
15592 |
|
15593 | do {
|
15594 | i++;
|
15595 | } while (buffer[i] !== 1 && i < len);
|
15596 |
|
15597 | syncPoint = i - 2;
|
15598 | i += 3;
|
15599 | break;
|
15600 |
|
15601 | case 1:
|
15602 |
|
15603 | if (buffer[i - 1] !== 0 || buffer[i - 2] !== 0) {
|
15604 | i += 3;
|
15605 | break;
|
15606 | }
|
15607 |
|
15608 |
|
15609 | this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
|
15610 | syncPoint = i - 2;
|
15611 | i += 3;
|
15612 | break;
|
15613 |
|
15614 | default:
|
15615 |
|
15616 |
|
15617 | i += 3;
|
15618 | break;
|
15619 | }
|
15620 | }
|
15621 |
|
15622 |
|
15623 | buffer = buffer.subarray(syncPoint);
|
15624 | i -= syncPoint;
|
15625 | syncPoint = 0;
|
15626 | };
|
15627 |
|
15628 | this.reset = function () {
|
15629 | buffer = null;
|
15630 | syncPoint = 0;
|
15631 | this.trigger('reset');
|
15632 | };
|
15633 |
|
15634 | this.flush = function () {
|
15635 |
|
15636 | if (buffer && buffer.byteLength > 3) {
|
15637 | this.trigger('data', buffer.subarray(syncPoint + 3));
|
15638 | }
|
15639 |
|
15640 |
|
15641 | buffer = null;
|
15642 | syncPoint = 0;
|
15643 | this.trigger('done');
|
15644 | };
|
15645 |
|
15646 | this.endTimeline = function () {
|
15647 | this.flush();
|
15648 | this.trigger('endedtimeline');
|
15649 | };
|
15650 | };
|
15651 |
|
15652 | NalByteStream.prototype = new Stream$2();
|
15653 |
|
15654 |
|
15655 |
|
15656 | PROFILES_WITH_OPTIONAL_SPS_DATA = {
|
15657 | 100: true,
|
15658 | 110: true,
|
15659 | 122: true,
|
15660 | 244: true,
|
15661 | 44: true,
|
15662 | 83: true,
|
15663 | 86: true,
|
15664 | 118: true,
|
15665 | 128: true,
|
15666 |
|
15667 |
|
15668 | 138: true,
|
15669 | 139: true,
|
15670 | 134: true
|
15671 | };
|
15672 | |
15673 |
|
15674 |
|
15675 |
|
15676 |
|
15677 | H264Stream$1 = function () {
|
15678 | var nalByteStream = new NalByteStream(),
|
15679 | self,
|
15680 | trackId,
|
15681 | currentPts,
|
15682 | currentDts,
|
15683 | discardEmulationPreventionBytes,
|
15684 | readSequenceParameterSet,
|
15685 | skipScalingList;
|
15686 | H264Stream$1.prototype.init.call(this);
|
15687 | self = this;
|
15688 | |
15689 |
|
15690 |
|
15691 |
|
15692 |
|
15693 |
|
15694 |
|
15695 |
|
15696 |
|
15697 |
|
15698 |
|
15699 |
|
15700 | this.push = function (packet) {
|
15701 | if (packet.type !== 'video') {
|
15702 | return;
|
15703 | }
|
15704 |
|
15705 | trackId = packet.trackId;
|
15706 | currentPts = packet.pts;
|
15707 | currentDts = packet.dts;
|
15708 | nalByteStream.push(packet);
|
15709 | };
|
15710 | |
15711 |
|
15712 |
|
15713 |
|
15714 |
|
15715 |
|
15716 |
|
15717 |
|
15718 |
|
15719 |
|
15720 | nalByteStream.on('data', function (data) {
|
15721 | var event = {
|
15722 | trackId: trackId,
|
15723 | pts: currentPts,
|
15724 | dts: currentDts,
|
15725 | data: data,
|
15726 | nalUnitTypeCode: data[0] & 0x1f
|
15727 | };
|
15728 |
|
15729 | switch (event.nalUnitTypeCode) {
|
15730 | case 0x05:
|
15731 | event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
|
15732 | break;
|
15733 |
|
15734 | case 0x06:
|
15735 | event.nalUnitType = 'sei_rbsp';
|
15736 | event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
|
15737 | break;
|
15738 |
|
15739 | case 0x07:
|
15740 | event.nalUnitType = 'seq_parameter_set_rbsp';
|
15741 | event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
|
15742 | event.config = readSequenceParameterSet(event.escapedRBSP);
|
15743 | break;
|
15744 |
|
15745 | case 0x08:
|
15746 | event.nalUnitType = 'pic_parameter_set_rbsp';
|
15747 | break;
|
15748 |
|
15749 | case 0x09:
|
15750 | event.nalUnitType = 'access_unit_delimiter_rbsp';
|
15751 | break;
|
15752 | }
|
15753 |
|
15754 |
|
15755 | self.trigger('data', event);
|
15756 | });
|
15757 | nalByteStream.on('done', function () {
|
15758 | self.trigger('done');
|
15759 | });
|
15760 | nalByteStream.on('partialdone', function () {
|
15761 | self.trigger('partialdone');
|
15762 | });
|
15763 | nalByteStream.on('reset', function () {
|
15764 | self.trigger('reset');
|
15765 | });
|
15766 | nalByteStream.on('endedtimeline', function () {
|
15767 | self.trigger('endedtimeline');
|
15768 | });
|
15769 |
|
15770 | this.flush = function () {
|
15771 | nalByteStream.flush();
|
15772 | };
|
15773 |
|
15774 | this.partialFlush = function () {
|
15775 | nalByteStream.partialFlush();
|
15776 | };
|
15777 |
|
15778 | this.reset = function () {
|
15779 | nalByteStream.reset();
|
15780 | };
|
15781 |
|
15782 | this.endTimeline = function () {
|
15783 | nalByteStream.endTimeline();
|
15784 | };
|
15785 | |
15786 |
|
15787 |
|
15788 |
|
15789 |
|
15790 |
|
15791 |
|
15792 |
|
15793 |
|
15794 |
|
15795 |
|
15796 | skipScalingList = function (count, expGolombDecoder) {
|
15797 | var lastScale = 8,
|
15798 | nextScale = 8,
|
15799 | j,
|
15800 | deltaScale;
|
15801 |
|
15802 | for (j = 0; j < count; j++) {
|
15803 | if (nextScale !== 0) {
|
15804 | deltaScale = expGolombDecoder.readExpGolomb();
|
15805 | nextScale = (lastScale + deltaScale + 256) % 256;
|
15806 | }
|
15807 |
|
15808 | lastScale = nextScale === 0 ? lastScale : nextScale;
|
15809 | }
|
15810 | };
|
15811 | |
15812 |
|
15813 |
|
15814 |
|
15815 |
|
15816 |
|
15817 |
|
15818 |
|
15819 |
|
15820 |
|
15821 | discardEmulationPreventionBytes = function (data) {
|
15822 | var length = data.byteLength,
|
15823 | emulationPreventionBytesPositions = [],
|
15824 | i = 1,
|
15825 | newLength,
|
15826 | newData;
|
15827 |
|
15828 | while (i < length - 2) {
|
15829 | if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
|
15830 | emulationPreventionBytesPositions.push(i + 2);
|
15831 | i += 2;
|
15832 | } else {
|
15833 | i++;
|
15834 | }
|
15835 | }
|
15836 |
|
15837 |
|
15838 |
|
15839 | if (emulationPreventionBytesPositions.length === 0) {
|
15840 | return data;
|
15841 | }
|
15842 |
|
15843 |
|
15844 | newLength = length - emulationPreventionBytesPositions.length;
|
15845 | newData = new Uint8Array(newLength);
|
15846 | var sourceIndex = 0;
|
15847 |
|
15848 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
15849 | if (sourceIndex === emulationPreventionBytesPositions[0]) {
|
15850 |
|
15851 | sourceIndex++;
|
15852 |
|
15853 | emulationPreventionBytesPositions.shift();
|
15854 | }
|
15855 |
|
15856 | newData[i] = data[sourceIndex];
|
15857 | }
|
15858 |
|
15859 | return newData;
|
15860 | };
|
15861 | |
15862 |
|
15863 |
|
15864 |
|
15865 |
|
15866 |
|
15867 |
|
15868 |
|
15869 |
|
15870 |
|
15871 |
|
15872 | readSequenceParameterSet = function (data) {
|
15873 | var frameCropLeftOffset = 0,
|
15874 | frameCropRightOffset = 0,
|
15875 | frameCropTopOffset = 0,
|
15876 | frameCropBottomOffset = 0,
|
15877 | expGolombDecoder,
|
15878 | profileIdc,
|
15879 | levelIdc,
|
15880 | profileCompatibility,
|
15881 | chromaFormatIdc,
|
15882 | picOrderCntType,
|
15883 | numRefFramesInPicOrderCntCycle,
|
15884 | picWidthInMbsMinus1,
|
15885 | picHeightInMapUnitsMinus1,
|
15886 | frameMbsOnlyFlag,
|
15887 | scalingListCount,
|
15888 | sarRatio = [1, 1],
|
15889 | aspectRatioIdc,
|
15890 | i;
|
15891 | expGolombDecoder = new ExpGolomb(data);
|
15892 | profileIdc = expGolombDecoder.readUnsignedByte();
|
15893 |
|
15894 | profileCompatibility = expGolombDecoder.readUnsignedByte();
|
15895 |
|
15896 | levelIdc = expGolombDecoder.readUnsignedByte();
|
15897 |
|
15898 | expGolombDecoder.skipUnsignedExpGolomb();
|
15899 |
|
15900 |
|
15901 | if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
|
15902 | chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
|
15903 |
|
15904 | if (chromaFormatIdc === 3) {
|
15905 | expGolombDecoder.skipBits(1);
|
15906 | }
|
15907 |
|
15908 | expGolombDecoder.skipUnsignedExpGolomb();
|
15909 |
|
15910 | expGolombDecoder.skipUnsignedExpGolomb();
|
15911 |
|
15912 | expGolombDecoder.skipBits(1);
|
15913 |
|
15914 | if (expGolombDecoder.readBoolean()) {
|
15915 |
|
15916 | scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
|
15917 |
|
15918 | for (i = 0; i < scalingListCount; i++) {
|
15919 | if (expGolombDecoder.readBoolean()) {
|
15920 |
|
15921 | if (i < 6) {
|
15922 | skipScalingList(16, expGolombDecoder);
|
15923 | } else {
|
15924 | skipScalingList(64, expGolombDecoder);
|
15925 | }
|
15926 | }
|
15927 | }
|
15928 | }
|
15929 | }
|
15930 |
|
15931 | expGolombDecoder.skipUnsignedExpGolomb();
|
15932 |
|
15933 | picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
|
15934 |
|
15935 | if (picOrderCntType === 0) {
|
15936 | expGolombDecoder.readUnsignedExpGolomb();
|
15937 | } else if (picOrderCntType === 1) {
|
15938 | expGolombDecoder.skipBits(1);
|
15939 |
|
15940 | expGolombDecoder.skipExpGolomb();
|
15941 |
|
15942 | expGolombDecoder.skipExpGolomb();
|
15943 |
|
15944 | numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
|
15945 |
|
15946 | for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
|
15947 | expGolombDecoder.skipExpGolomb();
|
15948 | }
|
15949 | }
|
15950 |
|
15951 | expGolombDecoder.skipUnsignedExpGolomb();
|
15952 |
|
15953 | expGolombDecoder.skipBits(1);
|
15954 |
|
15955 | picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
|
15956 | picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
|
15957 | frameMbsOnlyFlag = expGolombDecoder.readBits(1);
|
15958 |
|
15959 | if (frameMbsOnlyFlag === 0) {
|
15960 | expGolombDecoder.skipBits(1);
|
15961 | }
|
15962 |
|
15963 | expGolombDecoder.skipBits(1);
|
15964 |
|
15965 | if (expGolombDecoder.readBoolean()) {
|
15966 |
|
15967 | frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
|
15968 | frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
|
15969 | frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
|
15970 | frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
|
15971 | }
|
15972 |
|
15973 | if (expGolombDecoder.readBoolean()) {
|
15974 |
|
15975 | if (expGolombDecoder.readBoolean()) {
|
15976 |
|
15977 | aspectRatioIdc = expGolombDecoder.readUnsignedByte();
|
15978 |
|
15979 | switch (aspectRatioIdc) {
|
15980 | case 1:
|
15981 | sarRatio = [1, 1];
|
15982 | break;
|
15983 |
|
15984 | case 2:
|
15985 | sarRatio = [12, 11];
|
15986 | break;
|
15987 |
|
15988 | case 3:
|
15989 | sarRatio = [10, 11];
|
15990 | break;
|
15991 |
|
15992 | case 4:
|
15993 | sarRatio = [16, 11];
|
15994 | break;
|
15995 |
|
15996 | case 5:
|
15997 | sarRatio = [40, 33];
|
15998 | break;
|
15999 |
|
16000 | case 6:
|
16001 | sarRatio = [24, 11];
|
16002 | break;
|
16003 |
|
16004 | case 7:
|
16005 | sarRatio = [20, 11];
|
16006 | break;
|
16007 |
|
16008 | case 8:
|
16009 | sarRatio = [32, 11];
|
16010 | break;
|
16011 |
|
16012 | case 9:
|
16013 | sarRatio = [80, 33];
|
16014 | break;
|
16015 |
|
16016 | case 10:
|
16017 | sarRatio = [18, 11];
|
16018 | break;
|
16019 |
|
16020 | case 11:
|
16021 | sarRatio = [15, 11];
|
16022 | break;
|
16023 |
|
16024 | case 12:
|
16025 | sarRatio = [64, 33];
|
16026 | break;
|
16027 |
|
16028 | case 13:
|
16029 | sarRatio = [160, 99];
|
16030 | break;
|
16031 |
|
16032 | case 14:
|
16033 | sarRatio = [4, 3];
|
16034 | break;
|
16035 |
|
16036 | case 15:
|
16037 | sarRatio = [3, 2];
|
16038 | break;
|
16039 |
|
16040 | case 16:
|
16041 | sarRatio = [2, 1];
|
16042 | break;
|
16043 |
|
16044 | case 255:
|
16045 | {
|
16046 | sarRatio = [expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte(), expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte()];
|
16047 | break;
|
16048 | }
|
16049 | }
|
16050 |
|
16051 | if (sarRatio) {
|
16052 | sarRatio[0] / sarRatio[1];
|
16053 | }
|
16054 | }
|
16055 | }
|
16056 |
|
16057 | return {
|
16058 | profileIdc: profileIdc,
|
16059 | levelIdc: levelIdc,
|
16060 | profileCompatibility: profileCompatibility,
|
16061 | width: (picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2,
|
16062 | height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2,
|
16063 |
|
16064 | sarRatio: sarRatio
|
16065 | };
|
16066 | };
|
16067 | };
|
16068 |
|
16069 | H264Stream$1.prototype = new Stream$2();
|
16070 | var h264 = {
|
16071 | H264Stream: H264Stream$1,
|
16072 | NalByteStream: NalByteStream
|
16073 | };
|
16074 | |
16075 |
|
16076 |
|
16077 |
|
16078 |
|
16079 |
|
16080 |
|
16081 |
|
16082 |
|
16083 | var ADTS_SAMPLING_FREQUENCIES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
16084 |
|
16085 | var parseId3TagSize = function (header, byteIndex) {
|
16086 | var returnSize = header[byteIndex + 6] << 21 | header[byteIndex + 7] << 14 | header[byteIndex + 8] << 7 | header[byteIndex + 9],
|
16087 | flags = header[byteIndex + 5],
|
16088 | footerPresent = (flags & 16) >> 4;
|
16089 |
|
16090 | returnSize = returnSize >= 0 ? returnSize : 0;
|
16091 |
|
16092 | if (footerPresent) {
|
16093 | return returnSize + 20;
|
16094 | }
|
16095 |
|
16096 | return returnSize + 10;
|
16097 | };
|
16098 |
|
16099 | var getId3Offset = function (data, offset) {
|
16100 | if (data.length - offset < 10 || data[offset] !== 'I'.charCodeAt(0) || data[offset + 1] !== 'D'.charCodeAt(0) || data[offset + 2] !== '3'.charCodeAt(0)) {
|
16101 | return offset;
|
16102 | }
|
16103 |
|
16104 | offset += parseId3TagSize(data, offset);
|
16105 | return getId3Offset(data, offset);
|
16106 | };
|
16107 |
|
16108 |
|
16109 | var isLikelyAacData$1 = function (data) {
|
16110 | var offset = getId3Offset(data, 0);
|
16111 | return data.length >= offset + 2 && (data[offset] & 0xFF) === 0xFF && (data[offset + 1] & 0xF0) === 0xF0 &&
|
16112 |
|
16113 | (data[offset + 1] & 0x16) === 0x10;
|
16114 | };
|
16115 |
|
16116 | var parseSyncSafeInteger = function (data) {
|
16117 | return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
|
16118 | };
|
16119 |
|
16120 |
|
16121 |
|
16122 | var percentEncode = function (bytes, start, end) {
|
16123 | var i,
|
16124 | result = '';
|
16125 |
|
16126 | for (i = start; i < end; i++) {
|
16127 | result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
|
16128 | }
|
16129 |
|
16130 | return result;
|
16131 | };
|
16132 |
|
16133 |
|
16134 |
|
16135 | var parseIso88591 = function (bytes, start, end) {
|
16136 | return unescape(percentEncode(bytes, start, end));
|
16137 | };
|
16138 |
|
16139 | var parseAdtsSize = function (header, byteIndex) {
|
16140 | var lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
|
16141 | middle = header[byteIndex + 4] << 3,
|
16142 | highTwo = header[byteIndex + 3] & 0x3 << 11;
|
16143 | return highTwo | middle | lowThree;
|
16144 | };
|
16145 |
|
16146 | var parseType$4 = function (header, byteIndex) {
|
16147 | if (header[byteIndex] === 'I'.charCodeAt(0) && header[byteIndex + 1] === 'D'.charCodeAt(0) && header[byteIndex + 2] === '3'.charCodeAt(0)) {
|
16148 | return 'timed-metadata';
|
16149 | } else if (header[byteIndex] & 0xff === 0xff && (header[byteIndex + 1] & 0xf0) === 0xf0) {
|
16150 | return 'audio';
|
16151 | }
|
16152 |
|
16153 | return null;
|
16154 | };
|
16155 |
|
16156 | var parseSampleRate = function (packet) {
|
16157 | var i = 0;
|
16158 |
|
16159 | while (i + 5 < packet.length) {
|
16160 | if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
|
16161 |
|
16162 |
|
16163 | i++;
|
16164 | continue;
|
16165 | }
|
16166 |
|
16167 | return ADTS_SAMPLING_FREQUENCIES[(packet[i + 2] & 0x3c) >>> 2];
|
16168 | }
|
16169 |
|
16170 | return null;
|
16171 | };
|
16172 |
|
16173 | var parseAacTimestamp = function (packet) {
|
16174 | var frameStart, frameSize, frame, frameHeader;
|
16175 |
|
16176 | frameStart = 10;
|
16177 |
|
16178 | if (packet[5] & 0x40) {
|
16179 |
|
16180 | frameStart += 4;
|
16181 |
|
16182 | frameStart += parseSyncSafeInteger(packet.subarray(10, 14));
|
16183 | }
|
16184 |
|
16185 |
|
16186 |
|
16187 | do {
|
16188 |
|
16189 | frameSize = parseSyncSafeInteger(packet.subarray(frameStart + 4, frameStart + 8));
|
16190 |
|
16191 | if (frameSize < 1) {
|
16192 | return null;
|
16193 | }
|
16194 |
|
16195 | frameHeader = String.fromCharCode(packet[frameStart], packet[frameStart + 1], packet[frameStart + 2], packet[frameStart + 3]);
|
16196 |
|
16197 | if (frameHeader === 'PRIV') {
|
16198 | frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
|
16199 |
|
16200 | for (var i = 0; i < frame.byteLength; i++) {
|
16201 | if (frame[i] === 0) {
|
16202 | var owner = parseIso88591(frame, 0, i);
|
16203 |
|
16204 | if (owner === 'com.apple.streaming.transportStreamTimestamp') {
|
16205 | var d = frame.subarray(i + 1);
|
16206 | var size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
|
16207 | size *= 4;
|
16208 | size += d[7] & 0x03;
|
16209 | return size;
|
16210 | }
|
16211 |
|
16212 | break;
|
16213 | }
|
16214 | }
|
16215 | }
|
16216 |
|
16217 | frameStart += 10;
|
16218 |
|
16219 | frameStart += frameSize;
|
16220 | } while (frameStart < packet.byteLength);
|
16221 |
|
16222 | return null;
|
16223 | };
|
16224 |
|
16225 | var utils = {
|
16226 | isLikelyAacData: isLikelyAacData$1,
|
16227 | parseId3TagSize: parseId3TagSize,
|
16228 | parseAdtsSize: parseAdtsSize,
|
16229 | parseType: parseType$4,
|
16230 | parseSampleRate: parseSampleRate,
|
16231 | parseAacTimestamp: parseAacTimestamp
|
16232 | };
|
16233 | |
16234 |
|
16235 |
|
16236 |
|
16237 |
|
16238 |
|
16239 |
|
16240 |
|
16241 |
|
16242 |
|
16243 |
|
16244 | var Stream$1 = stream;
|
16245 | var aacUtils = utils;
|
16246 |
|
16247 | var AacStream$1;
|
16248 | |
16249 |
|
16250 |
|
16251 |
|
16252 | AacStream$1 = function () {
|
16253 | var everything = new Uint8Array(),
|
16254 | timeStamp = 0;
|
16255 | AacStream$1.prototype.init.call(this);
|
16256 |
|
16257 | this.setTimestamp = function (timestamp) {
|
16258 | timeStamp = timestamp;
|
16259 | };
|
16260 |
|
16261 | this.push = function (bytes) {
|
16262 | var frameSize = 0,
|
16263 | byteIndex = 0,
|
16264 | bytesLeft,
|
16265 | chunk,
|
16266 | packet,
|
16267 | tempLength;
|
16268 |
|
16269 |
|
16270 | if (everything.length) {
|
16271 | tempLength = everything.length;
|
16272 | everything = new Uint8Array(bytes.byteLength + tempLength);
|
16273 | everything.set(everything.subarray(0, tempLength));
|
16274 | everything.set(bytes, tempLength);
|
16275 | } else {
|
16276 | everything = bytes;
|
16277 | }
|
16278 |
|
16279 | while (everything.length - byteIndex >= 3) {
|
16280 | if (everything[byteIndex] === 'I'.charCodeAt(0) && everything[byteIndex + 1] === 'D'.charCodeAt(0) && everything[byteIndex + 2] === '3'.charCodeAt(0)) {
|
16281 |
|
16282 |
|
16283 | if (everything.length - byteIndex < 10) {
|
16284 | break;
|
16285 | }
|
16286 |
|
16287 |
|
16288 | frameSize = aacUtils.parseId3TagSize(everything, byteIndex);
|
16289 |
|
16290 |
|
16291 |
|
16292 | if (byteIndex + frameSize > everything.length) {
|
16293 | break;
|
16294 | }
|
16295 |
|
16296 | chunk = {
|
16297 | type: 'timed-metadata',
|
16298 | data: everything.subarray(byteIndex, byteIndex + frameSize)
|
16299 | };
|
16300 | this.trigger('data', chunk);
|
16301 | byteIndex += frameSize;
|
16302 | continue;
|
16303 | } else if ((everything[byteIndex] & 0xff) === 0xff && (everything[byteIndex + 1] & 0xf0) === 0xf0) {
|
16304 |
|
16305 |
|
16306 | if (everything.length - byteIndex < 7) {
|
16307 | break;
|
16308 | }
|
16309 |
|
16310 | frameSize = aacUtils.parseAdtsSize(everything, byteIndex);
|
16311 |
|
16312 |
|
16313 | if (byteIndex + frameSize > everything.length) {
|
16314 | break;
|
16315 | }
|
16316 |
|
16317 | packet = {
|
16318 | type: 'audio',
|
16319 | data: everything.subarray(byteIndex, byteIndex + frameSize),
|
16320 | pts: timeStamp,
|
16321 | dts: timeStamp
|
16322 | };
|
16323 | this.trigger('data', packet);
|
16324 | byteIndex += frameSize;
|
16325 | continue;
|
16326 | }
|
16327 |
|
16328 | byteIndex++;
|
16329 | }
|
16330 |
|
16331 | bytesLeft = everything.length - byteIndex;
|
16332 |
|
16333 | if (bytesLeft > 0) {
|
16334 | everything = everything.subarray(byteIndex);
|
16335 | } else {
|
16336 | everything = new Uint8Array();
|
16337 | }
|
16338 | };
|
16339 |
|
16340 | this.reset = function () {
|
16341 | everything = new Uint8Array();
|
16342 | this.trigger('reset');
|
16343 | };
|
16344 |
|
16345 | this.endTimeline = function () {
|
16346 | everything = new Uint8Array();
|
16347 | this.trigger('endedtimeline');
|
16348 | };
|
16349 | };
|
16350 |
|
16351 | AacStream$1.prototype = new Stream$1();
|
16352 | var aac = AacStream$1;
|
16353 | var AUDIO_PROPERTIES$1 = ['audioobjecttype', 'channelcount', 'samplerate', 'samplingfrequencyindex', 'samplesize'];
|
16354 | var audioProperties = AUDIO_PROPERTIES$1;
|
16355 | var VIDEO_PROPERTIES$1 = ['width', 'height', 'profileIdc', 'levelIdc', 'profileCompatibility', 'sarRatio'];
|
16356 | var videoProperties = VIDEO_PROPERTIES$1;
|
16357 | |
16358 |
|
16359 |
|
16360 |
|
16361 |
|
16362 |
|
16363 |
|
16364 |
|
16365 |
|
16366 |
|
16367 |
|
16368 | var Stream = stream;
|
16369 | var mp4 = mp4Generator;
|
16370 | var frameUtils = frameUtils$1;
|
16371 | var audioFrameUtils = audioFrameUtils$1;
|
16372 | var trackDecodeInfo = trackDecodeInfo$1;
|
16373 | var m2ts = m2ts_1;
|
16374 | var clock = clock$2;
|
16375 | var AdtsStream = adts;
|
16376 | var H264Stream = h264.H264Stream;
|
16377 | var AacStream = aac;
|
16378 | var isLikelyAacData = utils.isLikelyAacData;
|
16379 | var ONE_SECOND_IN_TS$1 = clock$2.ONE_SECOND_IN_TS;
|
16380 | var AUDIO_PROPERTIES = audioProperties;
|
16381 | var VIDEO_PROPERTIES = videoProperties;
|
16382 |
|
16383 | var VideoSegmentStream, AudioSegmentStream, Transmuxer, CoalesceStream;
|
16384 |
|
16385 | var retriggerForStream = function (key, event) {
|
16386 | event.stream = key;
|
16387 | this.trigger('log', event);
|
16388 | };
|
16389 |
|
16390 | var addPipelineLogRetriggers = function (transmuxer, pipeline) {
|
16391 | var keys = Object.keys(pipeline);
|
16392 |
|
16393 | for (var i = 0; i < keys.length; i++) {
|
16394 | var key = keys[i];
|
16395 |
|
16396 |
|
16397 | if (key === 'headOfPipeline' || !pipeline[key].on) {
|
16398 | continue;
|
16399 | }
|
16400 |
|
16401 | pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
|
16402 | }
|
16403 | };
|
16404 | |
16405 |
|
16406 |
|
16407 |
|
16408 |
|
16409 | var arrayEquals = function (a, b) {
|
16410 | var i;
|
16411 |
|
16412 | if (a.length !== b.length) {
|
16413 | return false;
|
16414 | }
|
16415 |
|
16416 |
|
16417 | for (i = 0; i < a.length; i++) {
|
16418 | if (a[i] !== b[i]) {
|
16419 | return false;
|
16420 | }
|
16421 | }
|
16422 |
|
16423 | return true;
|
16424 | };
|
16425 |
|
16426 | var generateSegmentTimingInfo = function (baseMediaDecodeTime, startDts, startPts, endDts, endPts, prependedContentDuration) {
|
16427 | var ptsOffsetFromDts = startPts - startDts,
|
16428 | decodeDuration = endDts - startDts,
|
16429 | presentationDuration = endPts - startPts;
|
16430 |
|
16431 |
|
16432 |
|
16433 |
|
16434 | return {
|
16435 | start: {
|
16436 | dts: baseMediaDecodeTime,
|
16437 | pts: baseMediaDecodeTime + ptsOffsetFromDts
|
16438 | },
|
16439 | end: {
|
16440 | dts: baseMediaDecodeTime + decodeDuration,
|
16441 | pts: baseMediaDecodeTime + presentationDuration
|
16442 | },
|
16443 | prependedContentDuration: prependedContentDuration,
|
16444 | baseMediaDecodeTime: baseMediaDecodeTime
|
16445 | };
|
16446 | };
|
16447 | |
16448 |
|
16449 |
|
16450 |
|
16451 |
|
16452 |
|
16453 |
|
16454 |
|
16455 |
|
16456 |
|
16457 |
|
16458 | AudioSegmentStream = function (track, options) {
|
16459 | var adtsFrames = [],
|
16460 | sequenceNumber,
|
16461 | earliestAllowedDts = 0,
|
16462 | audioAppendStartTs = 0,
|
16463 | videoBaseMediaDecodeTime = Infinity;
|
16464 | options = options || {};
|
16465 | sequenceNumber = options.firstSequenceNumber || 0;
|
16466 | AudioSegmentStream.prototype.init.call(this);
|
16467 |
|
16468 | this.push = function (data) {
|
16469 | trackDecodeInfo.collectDtsInfo(track, data);
|
16470 |
|
16471 | if (track) {
|
16472 | AUDIO_PROPERTIES.forEach(function (prop) {
|
16473 | track[prop] = data[prop];
|
16474 | });
|
16475 | }
|
16476 |
|
16477 |
|
16478 | adtsFrames.push(data);
|
16479 | };
|
16480 |
|
16481 | this.setEarliestDts = function (earliestDts) {
|
16482 | earliestAllowedDts = earliestDts;
|
16483 | };
|
16484 |
|
16485 | this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
|
16486 | videoBaseMediaDecodeTime = baseMediaDecodeTime;
|
16487 | };
|
16488 |
|
16489 | this.setAudioAppendStart = function (timestamp) {
|
16490 | audioAppendStartTs = timestamp;
|
16491 | };
|
16492 |
|
16493 | this.flush = function () {
|
16494 | var frames, moof, mdat, boxes, frameDuration, segmentDuration, videoClockCyclesOfSilencePrefixed;
|
16495 |
|
16496 | if (adtsFrames.length === 0) {
|
16497 | this.trigger('done', 'AudioSegmentStream');
|
16498 | return;
|
16499 | }
|
16500 |
|
16501 | frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
|
16502 | track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
|
16503 |
|
16504 | videoClockCyclesOfSilencePrefixed = audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime);
|
16505 |
|
16506 |
|
16507 | track.samples = audioFrameUtils.generateSampleTable(frames);
|
16508 |
|
16509 | mdat = mp4.mdat(audioFrameUtils.concatenateFrameData(frames));
|
16510 | adtsFrames = [];
|
16511 | moof = mp4.moof(sequenceNumber, [track]);
|
16512 | boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
|
16513 |
|
16514 | sequenceNumber++;
|
16515 | boxes.set(moof);
|
16516 | boxes.set(mdat, moof.byteLength);
|
16517 | trackDecodeInfo.clearDtsInfo(track);
|
16518 | frameDuration = Math.ceil(ONE_SECOND_IN_TS$1 * 1024 / track.samplerate);
|
16519 |
|
16520 |
|
16521 |
|
16522 |
|
16523 | if (frames.length) {
|
16524 | segmentDuration = frames.length * frameDuration;
|
16525 | this.trigger('segmentTimingInfo', generateSegmentTimingInfo(
|
16526 |
|
16527 |
|
16528 | clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate),
|
16529 | frames[0].dts, frames[0].pts, frames[0].dts + segmentDuration, frames[0].pts + segmentDuration, videoClockCyclesOfSilencePrefixed || 0));
|
16530 | this.trigger('timingInfo', {
|
16531 | start: frames[0].pts,
|
16532 | end: frames[0].pts + segmentDuration
|
16533 | });
|
16534 | }
|
16535 |
|
16536 | this.trigger('data', {
|
16537 | track: track,
|
16538 | boxes: boxes
|
16539 | });
|
16540 | this.trigger('done', 'AudioSegmentStream');
|
16541 | };
|
16542 |
|
16543 | this.reset = function () {
|
16544 | trackDecodeInfo.clearDtsInfo(track);
|
16545 | adtsFrames = [];
|
16546 | this.trigger('reset');
|
16547 | };
|
16548 | };
|
16549 |
|
16550 | AudioSegmentStream.prototype = new Stream();
|
16551 | |
16552 |
|
16553 |
|
16554 |
|
16555 |
|
16556 |
|
16557 |
|
16558 |
|
16559 |
|
16560 |
|
16561 |
|
16562 |
|
16563 | VideoSegmentStream = function (track, options) {
|
16564 | var sequenceNumber,
|
16565 | nalUnits = [],
|
16566 | gopsToAlignWith = [],
|
16567 | config,
|
16568 | pps;
|
16569 | options = options || {};
|
16570 | sequenceNumber = options.firstSequenceNumber || 0;
|
16571 | VideoSegmentStream.prototype.init.call(this);
|
16572 | delete track.minPTS;
|
16573 | this.gopCache_ = [];
|
16574 | |
16575 |
|
16576 |
|
16577 |
|
16578 |
|
16579 |
|
16580 |
|
16581 |
|
16582 |
|
16583 | this.push = function (nalUnit) {
|
16584 | trackDecodeInfo.collectDtsInfo(track, nalUnit);
|
16585 |
|
16586 | if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
|
16587 | config = nalUnit.config;
|
16588 | track.sps = [nalUnit.data];
|
16589 | VIDEO_PROPERTIES.forEach(function (prop) {
|
16590 | track[prop] = config[prop];
|
16591 | }, this);
|
16592 | }
|
16593 |
|
16594 | if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
|
16595 | pps = nalUnit.data;
|
16596 | track.pps = [nalUnit.data];
|
16597 | }
|
16598 |
|
16599 |
|
16600 | nalUnits.push(nalUnit);
|
16601 | };
|
16602 | |
16603 |
|
16604 |
|
16605 |
|
16606 |
|
16607 |
|
16608 | this.flush = function () {
|
16609 | var frames,
|
16610 | gopForFusion,
|
16611 | gops,
|
16612 | moof,
|
16613 | mdat,
|
16614 | boxes,
|
16615 | prependedContentDuration = 0,
|
16616 | firstGop,
|
16617 | lastGop;
|
16618 |
|
16619 |
|
16620 | while (nalUnits.length) {
|
16621 | if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
|
16622 | break;
|
16623 | }
|
16624 |
|
16625 | nalUnits.shift();
|
16626 | }
|
16627 |
|
16628 |
|
16629 | if (nalUnits.length === 0) {
|
16630 | this.resetStream_();
|
16631 | this.trigger('done', 'VideoSegmentStream');
|
16632 | return;
|
16633 | }
|
16634 |
|
16635 |
|
16636 |
|
16637 |
|
16638 | frames = frameUtils.groupNalsIntoFrames(nalUnits);
|
16639 | gops = frameUtils.groupFramesIntoGops(frames);
|
16640 |
|
16641 |
|
16642 |
|
16643 |
|
16644 |
|
16645 |
|
16646 |
|
16647 |
|
16648 |
|
16649 |
|
16650 |
|
16651 |
|
16652 |
|
16653 |
|
16654 |
|
16655 |
|
16656 |
|
16657 |
|
16658 | if (!gops[0][0].keyFrame) {
|
16659 |
|
16660 | gopForFusion = this.getGopForFusion_(nalUnits[0], track);
|
16661 |
|
16662 | if (gopForFusion) {
|
16663 |
|
16664 |
|
16665 | prependedContentDuration = gopForFusion.duration;
|
16666 | gops.unshift(gopForFusion);
|
16667 |
|
16668 |
|
16669 | gops.byteLength += gopForFusion.byteLength;
|
16670 | gops.nalCount += gopForFusion.nalCount;
|
16671 | gops.pts = gopForFusion.pts;
|
16672 | gops.dts = gopForFusion.dts;
|
16673 | gops.duration += gopForFusion.duration;
|
16674 | } else {
|
16675 |
|
16676 | gops = frameUtils.extendFirstKeyFrame(gops);
|
16677 | }
|
16678 | }
|
16679 |
|
16680 |
|
16681 | if (gopsToAlignWith.length) {
|
16682 | var alignedGops;
|
16683 |
|
16684 | if (options.alignGopsAtEnd) {
|
16685 | alignedGops = this.alignGopsAtEnd_(gops);
|
16686 | } else {
|
16687 | alignedGops = this.alignGopsAtStart_(gops);
|
16688 | }
|
16689 |
|
16690 | if (!alignedGops) {
|
16691 |
|
16692 | this.gopCache_.unshift({
|
16693 | gop: gops.pop(),
|
16694 | pps: track.pps,
|
16695 | sps: track.sps
|
16696 | });
|
16697 |
|
16698 | this.gopCache_.length = Math.min(6, this.gopCache_.length);
|
16699 |
|
16700 | nalUnits = [];
|
16701 |
|
16702 | this.resetStream_();
|
16703 | this.trigger('done', 'VideoSegmentStream');
|
16704 | return;
|
16705 | }
|
16706 |
|
16707 |
|
16708 |
|
16709 | trackDecodeInfo.clearDtsInfo(track);
|
16710 | gops = alignedGops;
|
16711 | }
|
16712 |
|
16713 | trackDecodeInfo.collectDtsInfo(track, gops);
|
16714 |
|
16715 |
|
16716 | track.samples = frameUtils.generateSampleTable(gops);
|
16717 |
|
16718 | mdat = mp4.mdat(frameUtils.concatenateNalData(gops));
|
16719 | track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
|
16720 | this.trigger('processedGopsInfo', gops.map(function (gop) {
|
16721 | return {
|
16722 | pts: gop.pts,
|
16723 | dts: gop.dts,
|
16724 | byteLength: gop.byteLength
|
16725 | };
|
16726 | }));
|
16727 | firstGop = gops[0];
|
16728 | lastGop = gops[gops.length - 1];
|
16729 | this.trigger('segmentTimingInfo', generateSegmentTimingInfo(track.baseMediaDecodeTime, firstGop.dts, firstGop.pts, lastGop.dts + lastGop.duration, lastGop.pts + lastGop.duration, prependedContentDuration));
|
16730 | this.trigger('timingInfo', {
|
16731 | start: gops[0].pts,
|
16732 | end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
|
16733 | });
|
16734 |
|
16735 | this.gopCache_.unshift({
|
16736 | gop: gops.pop(),
|
16737 | pps: track.pps,
|
16738 | sps: track.sps
|
16739 | });
|
16740 |
|
16741 | this.gopCache_.length = Math.min(6, this.gopCache_.length);
|
16742 |
|
16743 | nalUnits = [];
|
16744 | this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
|
16745 | this.trigger('timelineStartInfo', track.timelineStartInfo);
|
16746 | moof = mp4.moof(sequenceNumber, [track]);
|
16747 |
|
16748 |
|
16749 | boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
|
16750 |
|
16751 | sequenceNumber++;
|
16752 | boxes.set(moof);
|
16753 | boxes.set(mdat, moof.byteLength);
|
16754 | this.trigger('data', {
|
16755 | track: track,
|
16756 | boxes: boxes
|
16757 | });
|
16758 | this.resetStream_();
|
16759 |
|
16760 | this.trigger('done', 'VideoSegmentStream');
|
16761 | };
|
16762 |
|
16763 | this.reset = function () {
|
16764 | this.resetStream_();
|
16765 | nalUnits = [];
|
16766 | this.gopCache_.length = 0;
|
16767 | gopsToAlignWith.length = 0;
|
16768 | this.trigger('reset');
|
16769 | };
|
16770 |
|
16771 | this.resetStream_ = function () {
|
16772 | trackDecodeInfo.clearDtsInfo(track);
|
16773 |
|
16774 |
|
16775 | config = undefined;
|
16776 | pps = undefined;
|
16777 | };
|
16778 |
|
16779 |
|
16780 |
|
16781 | this.getGopForFusion_ = function (nalUnit) {
|
16782 | var halfSecond = 45000,
|
16783 |
|
16784 | allowableOverlap = 10000,
|
16785 |
|
16786 | nearestDistance = Infinity,
|
16787 | dtsDistance,
|
16788 | nearestGopObj,
|
16789 | currentGop,
|
16790 | currentGopObj,
|
16791 | i;
|
16792 |
|
16793 | for (i = 0; i < this.gopCache_.length; i++) {
|
16794 | currentGopObj = this.gopCache_[i];
|
16795 | currentGop = currentGopObj.gop;
|
16796 |
|
16797 | if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) || !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
|
16798 | continue;
|
16799 | }
|
16800 |
|
16801 |
|
16802 | if (currentGop.dts < track.timelineStartInfo.dts) {
|
16803 | continue;
|
16804 | }
|
16805 |
|
16806 |
|
16807 | dtsDistance = nalUnit.dts - currentGop.dts - currentGop.duration;
|
16808 |
|
16809 |
|
16810 | if (dtsDistance >= -allowableOverlap && dtsDistance <= halfSecond) {
|
16811 |
|
16812 |
|
16813 | if (!nearestGopObj || nearestDistance > dtsDistance) {
|
16814 | nearestGopObj = currentGopObj;
|
16815 | nearestDistance = dtsDistance;
|
16816 | }
|
16817 | }
|
16818 | }
|
16819 |
|
16820 | if (nearestGopObj) {
|
16821 | return nearestGopObj.gop;
|
16822 | }
|
16823 |
|
16824 | return null;
|
16825 | };
|
16826 |
|
16827 |
|
16828 |
|
16829 | this.alignGopsAtStart_ = function (gops) {
|
16830 | var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
|
16831 | byteLength = gops.byteLength;
|
16832 | nalCount = gops.nalCount;
|
16833 | duration = gops.duration;
|
16834 | alignIndex = gopIndex = 0;
|
16835 |
|
16836 | while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
|
16837 | align = gopsToAlignWith[alignIndex];
|
16838 | gop = gops[gopIndex];
|
16839 |
|
16840 | if (align.pts === gop.pts) {
|
16841 | break;
|
16842 | }
|
16843 |
|
16844 | if (gop.pts > align.pts) {
|
16845 |
|
16846 |
|
16847 | alignIndex++;
|
16848 | continue;
|
16849 | }
|
16850 |
|
16851 |
|
16852 |
|
16853 | gopIndex++;
|
16854 | byteLength -= gop.byteLength;
|
16855 | nalCount -= gop.nalCount;
|
16856 | duration -= gop.duration;
|
16857 | }
|
16858 |
|
16859 | if (gopIndex === 0) {
|
16860 |
|
16861 | return gops;
|
16862 | }
|
16863 |
|
16864 | if (gopIndex === gops.length) {
|
16865 |
|
16866 | return null;
|
16867 | }
|
16868 |
|
16869 | alignedGops = gops.slice(gopIndex);
|
16870 | alignedGops.byteLength = byteLength;
|
16871 | alignedGops.duration = duration;
|
16872 | alignedGops.nalCount = nalCount;
|
16873 | alignedGops.pts = alignedGops[0].pts;
|
16874 | alignedGops.dts = alignedGops[0].dts;
|
16875 | return alignedGops;
|
16876 | };
|
16877 |
|
16878 |
|
16879 |
|
16880 | this.alignGopsAtEnd_ = function (gops) {
|
16881 | var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
|
16882 | alignIndex = gopsToAlignWith.length - 1;
|
16883 | gopIndex = gops.length - 1;
|
16884 | alignEndIndex = null;
|
16885 | matchFound = false;
|
16886 |
|
16887 | while (alignIndex >= 0 && gopIndex >= 0) {
|
16888 | align = gopsToAlignWith[alignIndex];
|
16889 | gop = gops[gopIndex];
|
16890 |
|
16891 | if (align.pts === gop.pts) {
|
16892 | matchFound = true;
|
16893 | break;
|
16894 | }
|
16895 |
|
16896 | if (align.pts > gop.pts) {
|
16897 | alignIndex--;
|
16898 | continue;
|
16899 | }
|
16900 |
|
16901 | if (alignIndex === gopsToAlignWith.length - 1) {
|
16902 |
|
16903 |
|
16904 |
|
16905 | alignEndIndex = gopIndex;
|
16906 | }
|
16907 |
|
16908 | gopIndex--;
|
16909 | }
|
16910 |
|
16911 | if (!matchFound && alignEndIndex === null) {
|
16912 | return null;
|
16913 | }
|
16914 |
|
16915 | var trimIndex;
|
16916 |
|
16917 | if (matchFound) {
|
16918 | trimIndex = gopIndex;
|
16919 | } else {
|
16920 | trimIndex = alignEndIndex;
|
16921 | }
|
16922 |
|
16923 | if (trimIndex === 0) {
|
16924 | return gops;
|
16925 | }
|
16926 |
|
16927 | var alignedGops = gops.slice(trimIndex);
|
16928 | var metadata = alignedGops.reduce(function (total, gop) {
|
16929 | total.byteLength += gop.byteLength;
|
16930 | total.duration += gop.duration;
|
16931 | total.nalCount += gop.nalCount;
|
16932 | return total;
|
16933 | }, {
|
16934 | byteLength: 0,
|
16935 | duration: 0,
|
16936 | nalCount: 0
|
16937 | });
|
16938 | alignedGops.byteLength = metadata.byteLength;
|
16939 | alignedGops.duration = metadata.duration;
|
16940 | alignedGops.nalCount = metadata.nalCount;
|
16941 | alignedGops.pts = alignedGops[0].pts;
|
16942 | alignedGops.dts = alignedGops[0].dts;
|
16943 | return alignedGops;
|
16944 | };
|
16945 |
|
16946 | this.alignGopsWith = function (newGopsToAlignWith) {
|
16947 | gopsToAlignWith = newGopsToAlignWith;
|
16948 | };
|
16949 | };
|
16950 |
|
16951 | VideoSegmentStream.prototype = new Stream();
|
16952 | |
16953 |
|
16954 |
|
16955 |
|
16956 |
|
16957 |
|
16958 |
|
16959 |
|
16960 |
|
16961 | CoalesceStream = function (options, metadataStream) {
|
16962 |
|
16963 |
|
16964 |
|
16965 | this.numberOfTracks = 0;
|
16966 | this.metadataStream = metadataStream;
|
16967 | options = options || {};
|
16968 |
|
16969 | if (typeof options.remux !== 'undefined') {
|
16970 | this.remuxTracks = !!options.remux;
|
16971 | } else {
|
16972 | this.remuxTracks = true;
|
16973 | }
|
16974 |
|
16975 | if (typeof options.keepOriginalTimestamps === 'boolean') {
|
16976 | this.keepOriginalTimestamps = options.keepOriginalTimestamps;
|
16977 | } else {
|
16978 | this.keepOriginalTimestamps = false;
|
16979 | }
|
16980 |
|
16981 | this.pendingTracks = [];
|
16982 | this.videoTrack = null;
|
16983 | this.pendingBoxes = [];
|
16984 | this.pendingCaptions = [];
|
16985 | this.pendingMetadata = [];
|
16986 | this.pendingBytes = 0;
|
16987 | this.emittedTracks = 0;
|
16988 | CoalesceStream.prototype.init.call(this);
|
16989 |
|
16990 | this.push = function (output) {
|
16991 |
|
16992 |
|
16993 | if (output.content || output.text) {
|
16994 | return this.pendingCaptions.push(output);
|
16995 | }
|
16996 |
|
16997 |
|
16998 | if (output.frames) {
|
16999 | return this.pendingMetadata.push(output);
|
17000 | }
|
17001 |
|
17002 |
|
17003 |
|
17004 |
|
17005 | this.pendingTracks.push(output.track);
|
17006 | this.pendingBytes += output.boxes.byteLength;
|
17007 |
|
17008 |
|
17009 |
|
17010 |
|
17011 |
|
17012 |
|
17013 | if (output.track.type === 'video') {
|
17014 | this.videoTrack = output.track;
|
17015 | this.pendingBoxes.push(output.boxes);
|
17016 | }
|
17017 |
|
17018 | if (output.track.type === 'audio') {
|
17019 | this.audioTrack = output.track;
|
17020 | this.pendingBoxes.unshift(output.boxes);
|
17021 | }
|
17022 | };
|
17023 | };
|
17024 |
|
17025 | CoalesceStream.prototype = new Stream();
|
17026 |
|
17027 | CoalesceStream.prototype.flush = function (flushSource) {
|
17028 | var offset = 0,
|
17029 | event = {
|
17030 | captions: [],
|
17031 | captionStreams: {},
|
17032 | metadata: [],
|
17033 | info: {}
|
17034 | },
|
17035 | caption,
|
17036 | id3,
|
17037 | initSegment,
|
17038 | timelineStartPts = 0,
|
17039 | i;
|
17040 |
|
17041 | if (this.pendingTracks.length < this.numberOfTracks) {
|
17042 | if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
|
17043 |
|
17044 |
|
17045 |
|
17046 | return;
|
17047 | } else if (this.remuxTracks) {
|
17048 |
|
17049 |
|
17050 | return;
|
17051 | } else if (this.pendingTracks.length === 0) {
|
17052 |
|
17053 |
|
17054 |
|
17055 |
|
17056 |
|
17057 |
|
17058 | this.emittedTracks++;
|
17059 |
|
17060 | if (this.emittedTracks >= this.numberOfTracks) {
|
17061 | this.trigger('done');
|
17062 | this.emittedTracks = 0;
|
17063 | }
|
17064 |
|
17065 | return;
|
17066 | }
|
17067 | }
|
17068 |
|
17069 | if (this.videoTrack) {
|
17070 | timelineStartPts = this.videoTrack.timelineStartInfo.pts;
|
17071 | VIDEO_PROPERTIES.forEach(function (prop) {
|
17072 | event.info[prop] = this.videoTrack[prop];
|
17073 | }, this);
|
17074 | } else if (this.audioTrack) {
|
17075 | timelineStartPts = this.audioTrack.timelineStartInfo.pts;
|
17076 | AUDIO_PROPERTIES.forEach(function (prop) {
|
17077 | event.info[prop] = this.audioTrack[prop];
|
17078 | }, this);
|
17079 | }
|
17080 |
|
17081 | if (this.videoTrack || this.audioTrack) {
|
17082 | if (this.pendingTracks.length === 1) {
|
17083 | event.type = this.pendingTracks[0].type;
|
17084 | } else {
|
17085 | event.type = 'combined';
|
17086 | }
|
17087 |
|
17088 | this.emittedTracks += this.pendingTracks.length;
|
17089 | initSegment = mp4.initSegment(this.pendingTracks);
|
17090 |
|
17091 | event.initSegment = new Uint8Array(initSegment.byteLength);
|
17092 |
|
17093 |
|
17094 | event.initSegment.set(initSegment);
|
17095 |
|
17096 | event.data = new Uint8Array(this.pendingBytes);
|
17097 |
|
17098 | for (i = 0; i < this.pendingBoxes.length; i++) {
|
17099 | event.data.set(this.pendingBoxes[i], offset);
|
17100 | offset += this.pendingBoxes[i].byteLength;
|
17101 | }
|
17102 |
|
17103 |
|
17104 |
|
17105 | for (i = 0; i < this.pendingCaptions.length; i++) {
|
17106 | caption = this.pendingCaptions[i];
|
17107 | caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
|
17108 | caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
|
17109 | event.captionStreams[caption.stream] = true;
|
17110 | event.captions.push(caption);
|
17111 | }
|
17112 |
|
17113 |
|
17114 |
|
17115 | for (i = 0; i < this.pendingMetadata.length; i++) {
|
17116 | id3 = this.pendingMetadata[i];
|
17117 | id3.cueTime = clock.metadataTsToSeconds(id3.pts, timelineStartPts, this.keepOriginalTimestamps);
|
17118 | event.metadata.push(id3);
|
17119 | }
|
17120 |
|
17121 |
|
17122 |
|
17123 | event.metadata.dispatchType = this.metadataStream.dispatchType;
|
17124 |
|
17125 | this.pendingTracks.length = 0;
|
17126 | this.videoTrack = null;
|
17127 | this.pendingBoxes.length = 0;
|
17128 | this.pendingCaptions.length = 0;
|
17129 | this.pendingBytes = 0;
|
17130 | this.pendingMetadata.length = 0;
|
17131 |
|
17132 |
|
17133 |
|
17134 | this.trigger('data', event);
|
17135 |
|
17136 |
|
17137 |
|
17138 |
|
17139 | for (i = 0; i < event.captions.length; i++) {
|
17140 | caption = event.captions[i];
|
17141 | this.trigger('caption', caption);
|
17142 | }
|
17143 |
|
17144 |
|
17145 |
|
17146 |
|
17147 |
|
17148 | for (i = 0; i < event.metadata.length; i++) {
|
17149 | id3 = event.metadata[i];
|
17150 | this.trigger('id3Frame', id3);
|
17151 | }
|
17152 | }
|
17153 |
|
17154 |
|
17155 | if (this.emittedTracks >= this.numberOfTracks) {
|
17156 | this.trigger('done');
|
17157 | this.emittedTracks = 0;
|
17158 | }
|
17159 | };
|
17160 |
|
17161 | CoalesceStream.prototype.setRemux = function (val) {
|
17162 | this.remuxTracks = val;
|
17163 | };
|
17164 | |
17165 |
|
17166 |
|
17167 |
|
17168 |
|
17169 |
|
17170 |
|
17171 |
|
17172 | Transmuxer = function (options) {
|
17173 | var self = this,
|
17174 | hasFlushed = true,
|
17175 | videoTrack,
|
17176 | audioTrack;
|
17177 | Transmuxer.prototype.init.call(this);
|
17178 | options = options || {};
|
17179 | this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
|
17180 | this.transmuxPipeline_ = {};
|
17181 |
|
17182 | this.setupAacPipeline = function () {
|
17183 | var pipeline = {};
|
17184 | this.transmuxPipeline_ = pipeline;
|
17185 | pipeline.type = 'aac';
|
17186 | pipeline.metadataStream = new m2ts.MetadataStream();
|
17187 |
|
17188 | pipeline.aacStream = new AacStream();
|
17189 | pipeline.audioTimestampRolloverStream = new m2ts.TimestampRolloverStream('audio');
|
17190 | pipeline.timedMetadataTimestampRolloverStream = new m2ts.TimestampRolloverStream('timed-metadata');
|
17191 | pipeline.adtsStream = new AdtsStream();
|
17192 | pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
|
17193 | pipeline.headOfPipeline = pipeline.aacStream;
|
17194 | pipeline.aacStream.pipe(pipeline.audioTimestampRolloverStream).pipe(pipeline.adtsStream);
|
17195 | pipeline.aacStream.pipe(pipeline.timedMetadataTimestampRolloverStream).pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
|
17196 | pipeline.metadataStream.on('timestamp', function (frame) {
|
17197 | pipeline.aacStream.setTimestamp(frame.timeStamp);
|
17198 | });
|
17199 | pipeline.aacStream.on('data', function (data) {
|
17200 | if (data.type !== 'timed-metadata' && data.type !== 'audio' || pipeline.audioSegmentStream) {
|
17201 | return;
|
17202 | }
|
17203 |
|
17204 | audioTrack = audioTrack || {
|
17205 | timelineStartInfo: {
|
17206 | baseMediaDecodeTime: self.baseMediaDecodeTime
|
17207 | },
|
17208 | codec: 'adts',
|
17209 | type: 'audio'
|
17210 | };
|
17211 |
|
17212 | pipeline.coalesceStream.numberOfTracks++;
|
17213 | pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
|
17214 | pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
|
17215 | pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
|
17216 |
|
17217 | pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
|
17218 |
|
17219 | self.trigger('trackinfo', {
|
17220 | hasAudio: !!audioTrack,
|
17221 | hasVideo: !!videoTrack
|
17222 | });
|
17223 | });
|
17224 |
|
17225 | pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
|
17226 |
|
17227 | pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
|
17228 | addPipelineLogRetriggers(this, pipeline);
|
17229 | };
|
17230 |
|
17231 | this.setupTsPipeline = function () {
|
17232 | var pipeline = {};
|
17233 | this.transmuxPipeline_ = pipeline;
|
17234 | pipeline.type = 'ts';
|
17235 | pipeline.metadataStream = new m2ts.MetadataStream();
|
17236 |
|
17237 | pipeline.packetStream = new m2ts.TransportPacketStream();
|
17238 | pipeline.parseStream = new m2ts.TransportParseStream();
|
17239 | pipeline.elementaryStream = new m2ts.ElementaryStream();
|
17240 | pipeline.timestampRolloverStream = new m2ts.TimestampRolloverStream();
|
17241 | pipeline.adtsStream = new AdtsStream();
|
17242 | pipeline.h264Stream = new H264Stream();
|
17243 | pipeline.captionStream = new m2ts.CaptionStream(options);
|
17244 | pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
|
17245 | pipeline.headOfPipeline = pipeline.packetStream;
|
17246 |
|
17247 | pipeline.packetStream.pipe(pipeline.parseStream).pipe(pipeline.elementaryStream).pipe(pipeline.timestampRolloverStream);
|
17248 |
|
17249 |
|
17250 | pipeline.timestampRolloverStream.pipe(pipeline.h264Stream);
|
17251 | pipeline.timestampRolloverStream.pipe(pipeline.adtsStream);
|
17252 | pipeline.timestampRolloverStream.pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
|
17253 |
|
17254 | pipeline.h264Stream.pipe(pipeline.captionStream).pipe(pipeline.coalesceStream);
|
17255 | pipeline.elementaryStream.on('data', function (data) {
|
17256 | var i;
|
17257 |
|
17258 | if (data.type === 'metadata') {
|
17259 | i = data.tracks.length;
|
17260 |
|
17261 | while (i--) {
|
17262 | if (!videoTrack && data.tracks[i].type === 'video') {
|
17263 | videoTrack = data.tracks[i];
|
17264 | videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
|
17265 | } else if (!audioTrack && data.tracks[i].type === 'audio') {
|
17266 | audioTrack = data.tracks[i];
|
17267 | audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
|
17268 | }
|
17269 | }
|
17270 |
|
17271 |
|
17272 | if (videoTrack && !pipeline.videoSegmentStream) {
|
17273 | pipeline.coalesceStream.numberOfTracks++;
|
17274 | pipeline.videoSegmentStream = new VideoSegmentStream(videoTrack, options);
|
17275 | pipeline.videoSegmentStream.on('log', self.getLogTrigger_('videoSegmentStream'));
|
17276 | pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
|
17277 |
|
17278 |
|
17279 |
|
17280 |
|
17281 | if (audioTrack && !options.keepOriginalTimestamps) {
|
17282 | audioTrack.timelineStartInfo = timelineStartInfo;
|
17283 |
|
17284 |
|
17285 |
|
17286 |
|
17287 | pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - self.baseMediaDecodeTime);
|
17288 | }
|
17289 | });
|
17290 | pipeline.videoSegmentStream.on('processedGopsInfo', self.trigger.bind(self, 'gopInfo'));
|
17291 | pipeline.videoSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'videoSegmentTimingInfo'));
|
17292 | pipeline.videoSegmentStream.on('baseMediaDecodeTime', function (baseMediaDecodeTime) {
|
17293 | if (audioTrack) {
|
17294 | pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
|
17295 | }
|
17296 | });
|
17297 | pipeline.videoSegmentStream.on('timingInfo', self.trigger.bind(self, 'videoTimingInfo'));
|
17298 |
|
17299 | pipeline.h264Stream.pipe(pipeline.videoSegmentStream).pipe(pipeline.coalesceStream);
|
17300 | }
|
17301 |
|
17302 | if (audioTrack && !pipeline.audioSegmentStream) {
|
17303 |
|
17304 | pipeline.coalesceStream.numberOfTracks++;
|
17305 | pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
|
17306 | pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
|
17307 | pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
|
17308 | pipeline.audioSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'audioSegmentTimingInfo'));
|
17309 |
|
17310 | pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
|
17311 | }
|
17312 |
|
17313 |
|
17314 | self.trigger('trackinfo', {
|
17315 | hasAudio: !!audioTrack,
|
17316 | hasVideo: !!videoTrack
|
17317 | });
|
17318 | }
|
17319 | });
|
17320 |
|
17321 | pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
|
17322 | pipeline.coalesceStream.on('id3Frame', function (id3Frame) {
|
17323 | id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
|
17324 | self.trigger('id3Frame', id3Frame);
|
17325 | });
|
17326 | pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
|
17327 |
|
17328 | pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
|
17329 | addPipelineLogRetriggers(this, pipeline);
|
17330 | };
|
17331 |
|
17332 |
|
17333 | this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
|
17334 | var pipeline = this.transmuxPipeline_;
|
17335 |
|
17336 | if (!options.keepOriginalTimestamps) {
|
17337 | this.baseMediaDecodeTime = baseMediaDecodeTime;
|
17338 | }
|
17339 |
|
17340 | if (audioTrack) {
|
17341 | audioTrack.timelineStartInfo.dts = undefined;
|
17342 | audioTrack.timelineStartInfo.pts = undefined;
|
17343 | trackDecodeInfo.clearDtsInfo(audioTrack);
|
17344 |
|
17345 | if (pipeline.audioTimestampRolloverStream) {
|
17346 | pipeline.audioTimestampRolloverStream.discontinuity();
|
17347 | }
|
17348 | }
|
17349 |
|
17350 | if (videoTrack) {
|
17351 | if (pipeline.videoSegmentStream) {
|
17352 | pipeline.videoSegmentStream.gopCache_ = [];
|
17353 | }
|
17354 |
|
17355 | videoTrack.timelineStartInfo.dts = undefined;
|
17356 | videoTrack.timelineStartInfo.pts = undefined;
|
17357 | trackDecodeInfo.clearDtsInfo(videoTrack);
|
17358 | pipeline.captionStream.reset();
|
17359 | }
|
17360 |
|
17361 | if (pipeline.timestampRolloverStream) {
|
17362 | pipeline.timestampRolloverStream.discontinuity();
|
17363 | }
|
17364 | };
|
17365 |
|
17366 | this.setAudioAppendStart = function (timestamp) {
|
17367 | if (audioTrack) {
|
17368 | this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
|
17369 | }
|
17370 | };
|
17371 |
|
17372 | this.setRemux = function (val) {
|
17373 | var pipeline = this.transmuxPipeline_;
|
17374 | options.remux = val;
|
17375 |
|
17376 | if (pipeline && pipeline.coalesceStream) {
|
17377 | pipeline.coalesceStream.setRemux(val);
|
17378 | }
|
17379 | };
|
17380 |
|
17381 | this.alignGopsWith = function (gopsToAlignWith) {
|
17382 | if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
|
17383 | this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
|
17384 | }
|
17385 | };
|
17386 |
|
17387 | this.getLogTrigger_ = function (key) {
|
17388 | var self = this;
|
17389 | return function (event) {
|
17390 | event.stream = key;
|
17391 | self.trigger('log', event);
|
17392 | };
|
17393 | };
|
17394 |
|
17395 |
|
17396 | this.push = function (data) {
|
17397 | if (hasFlushed) {
|
17398 | var isAac = isLikelyAacData(data);
|
17399 |
|
17400 | if (isAac && this.transmuxPipeline_.type !== 'aac') {
|
17401 | this.setupAacPipeline();
|
17402 | } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
|
17403 | this.setupTsPipeline();
|
17404 | }
|
17405 |
|
17406 | hasFlushed = false;
|
17407 | }
|
17408 |
|
17409 | this.transmuxPipeline_.headOfPipeline.push(data);
|
17410 | };
|
17411 |
|
17412 |
|
17413 | this.flush = function () {
|
17414 | hasFlushed = true;
|
17415 |
|
17416 | this.transmuxPipeline_.headOfPipeline.flush();
|
17417 | };
|
17418 |
|
17419 | this.endTimeline = function () {
|
17420 | this.transmuxPipeline_.headOfPipeline.endTimeline();
|
17421 | };
|
17422 |
|
17423 | this.reset = function () {
|
17424 | if (this.transmuxPipeline_.headOfPipeline) {
|
17425 | this.transmuxPipeline_.headOfPipeline.reset();
|
17426 | }
|
17427 | };
|
17428 |
|
17429 |
|
17430 | this.resetCaptions = function () {
|
17431 | if (this.transmuxPipeline_.captionStream) {
|
17432 | this.transmuxPipeline_.captionStream.reset();
|
17433 | }
|
17434 | };
|
17435 | };
|
17436 |
|
17437 | Transmuxer.prototype = new Stream();
|
17438 | var transmuxer = {
|
17439 | Transmuxer: Transmuxer,
|
17440 | VideoSegmentStream: VideoSegmentStream,
|
17441 | AudioSegmentStream: AudioSegmentStream,
|
17442 | AUDIO_PROPERTIES: AUDIO_PROPERTIES,
|
17443 | VIDEO_PROPERTIES: VIDEO_PROPERTIES,
|
17444 |
|
17445 | generateSegmentTimingInfo: generateSegmentTimingInfo
|
17446 | };
|
17447 | |
17448 |
|
17449 |
|
17450 |
|
17451 |
|
17452 |
|
17453 |
|
17454 | var toUnsigned$3 = function (value) {
|
17455 | return value >>> 0;
|
17456 | };
|
17457 |
|
17458 | var toHexString$1 = function (value) {
|
17459 | return ('00' + value.toString(16)).slice(-2);
|
17460 | };
|
17461 |
|
17462 | var bin = {
|
17463 | toUnsigned: toUnsigned$3,
|
17464 | toHexString: toHexString$1
|
17465 | };
|
17466 |
|
17467 | var parseType$3 = function (buffer) {
|
17468 | var result = '';
|
17469 | result += String.fromCharCode(buffer[0]);
|
17470 | result += String.fromCharCode(buffer[1]);
|
17471 | result += String.fromCharCode(buffer[2]);
|
17472 | result += String.fromCharCode(buffer[3]);
|
17473 | return result;
|
17474 | };
|
17475 |
|
17476 | var parseType_1 = parseType$3;
|
17477 | var toUnsigned$2 = bin.toUnsigned;
|
17478 | var parseType$2 = parseType_1;
|
17479 |
|
17480 | var findBox$2 = function (data, path) {
|
17481 | var results = [],
|
17482 | i,
|
17483 | size,
|
17484 | type,
|
17485 | end,
|
17486 | subresults;
|
17487 |
|
17488 | if (!path.length) {
|
17489 |
|
17490 | return null;
|
17491 | }
|
17492 |
|
17493 | for (i = 0; i < data.byteLength;) {
|
17494 | size = toUnsigned$2(data[i] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3]);
|
17495 | type = parseType$2(data.subarray(i + 4, i + 8));
|
17496 | end = size > 1 ? i + size : data.byteLength;
|
17497 |
|
17498 | if (type === path[0]) {
|
17499 | if (path.length === 1) {
|
17500 |
|
17501 |
|
17502 | results.push(data.subarray(i + 8, end));
|
17503 | } else {
|
17504 |
|
17505 | subresults = findBox$2(data.subarray(i + 8, end), path.slice(1));
|
17506 |
|
17507 | if (subresults.length) {
|
17508 | results = results.concat(subresults);
|
17509 | }
|
17510 | }
|
17511 | }
|
17512 |
|
17513 | i = end;
|
17514 | }
|
17515 |
|
17516 |
|
17517 | return results;
|
17518 | };
|
17519 |
|
17520 | var findBox_1 = findBox$2;
|
17521 | var toUnsigned$1 = bin.toUnsigned;
|
17522 | var getUint64$2 = numbers.getUint64;
|
17523 |
|
17524 | var tfdt = function (data) {
|
17525 | var result = {
|
17526 | version: data[0],
|
17527 | flags: new Uint8Array(data.subarray(1, 4))
|
17528 | };
|
17529 |
|
17530 | if (result.version === 1) {
|
17531 | result.baseMediaDecodeTime = getUint64$2(data.subarray(4));
|
17532 | } else {
|
17533 | result.baseMediaDecodeTime = toUnsigned$1(data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]);
|
17534 | }
|
17535 |
|
17536 | return result;
|
17537 | };
|
17538 |
|
17539 | var parseTfdt$2 = tfdt;
|
17540 |
|
17541 | var parseSampleFlags$1 = function (flags) {
|
17542 | return {
|
17543 | isLeading: (flags[0] & 0x0c) >>> 2,
|
17544 | dependsOn: flags[0] & 0x03,
|
17545 | isDependedOn: (flags[1] & 0xc0) >>> 6,
|
17546 | hasRedundancy: (flags[1] & 0x30) >>> 4,
|
17547 | paddingValue: (flags[1] & 0x0e) >>> 1,
|
17548 | isNonSyncSample: flags[1] & 0x01,
|
17549 | degradationPriority: flags[2] << 8 | flags[3]
|
17550 | };
|
17551 | };
|
17552 |
|
17553 | var parseSampleFlags_1 = parseSampleFlags$1;
|
17554 | var parseSampleFlags = parseSampleFlags_1;
|
17555 |
|
17556 | var trun = function (data) {
|
17557 | var result = {
|
17558 | version: data[0],
|
17559 | flags: new Uint8Array(data.subarray(1, 4)),
|
17560 | samples: []
|
17561 | },
|
17562 | view = new DataView(data.buffer, data.byteOffset, data.byteLength),
|
17563 |
|
17564 | dataOffsetPresent = result.flags[2] & 0x01,
|
17565 |
|
17566 | firstSampleFlagsPresent = result.flags[2] & 0x04,
|
17567 |
|
17568 | sampleDurationPresent = result.flags[1] & 0x01,
|
17569 |
|
17570 | sampleSizePresent = result.flags[1] & 0x02,
|
17571 |
|
17572 | sampleFlagsPresent = result.flags[1] & 0x04,
|
17573 |
|
17574 | sampleCompositionTimeOffsetPresent = result.flags[1] & 0x08,
|
17575 |
|
17576 | sampleCount = view.getUint32(4),
|
17577 | offset = 8,
|
17578 | sample;
|
17579 |
|
17580 | if (dataOffsetPresent) {
|
17581 |
|
17582 | result.dataOffset = view.getInt32(offset);
|
17583 | offset += 4;
|
17584 | }
|
17585 |
|
17586 |
|
17587 |
|
17588 | if (firstSampleFlagsPresent && sampleCount) {
|
17589 | sample = {
|
17590 | flags: parseSampleFlags(data.subarray(offset, offset + 4))
|
17591 | };
|
17592 | offset += 4;
|
17593 |
|
17594 | if (sampleDurationPresent) {
|
17595 | sample.duration = view.getUint32(offset);
|
17596 | offset += 4;
|
17597 | }
|
17598 |
|
17599 | if (sampleSizePresent) {
|
17600 | sample.size = view.getUint32(offset);
|
17601 | offset += 4;
|
17602 | }
|
17603 |
|
17604 | if (sampleCompositionTimeOffsetPresent) {
|
17605 | if (result.version === 1) {
|
17606 | sample.compositionTimeOffset = view.getInt32(offset);
|
17607 | } else {
|
17608 | sample.compositionTimeOffset = view.getUint32(offset);
|
17609 | }
|
17610 |
|
17611 | offset += 4;
|
17612 | }
|
17613 |
|
17614 | result.samples.push(sample);
|
17615 | sampleCount--;
|
17616 | }
|
17617 |
|
17618 | while (sampleCount--) {
|
17619 | sample = {};
|
17620 |
|
17621 | if (sampleDurationPresent) {
|
17622 | sample.duration = view.getUint32(offset);
|
17623 | offset += 4;
|
17624 | }
|
17625 |
|
17626 | if (sampleSizePresent) {
|
17627 | sample.size = view.getUint32(offset);
|
17628 | offset += 4;
|
17629 | }
|
17630 |
|
17631 | if (sampleFlagsPresent) {
|
17632 | sample.flags = parseSampleFlags(data.subarray(offset, offset + 4));
|
17633 | offset += 4;
|
17634 | }
|
17635 |
|
17636 | if (sampleCompositionTimeOffsetPresent) {
|
17637 | if (result.version === 1) {
|
17638 | sample.compositionTimeOffset = view.getInt32(offset);
|
17639 | } else {
|
17640 | sample.compositionTimeOffset = view.getUint32(offset);
|
17641 | }
|
17642 |
|
17643 | offset += 4;
|
17644 | }
|
17645 |
|
17646 | result.samples.push(sample);
|
17647 | }
|
17648 |
|
17649 | return result;
|
17650 | };
|
17651 |
|
17652 | var parseTrun$2 = trun;
|
17653 |
|
17654 | var tfhd = function (data) {
|
17655 | var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
|
17656 | result = {
|
17657 | version: data[0],
|
17658 | flags: new Uint8Array(data.subarray(1, 4)),
|
17659 | trackId: view.getUint32(4)
|
17660 | },
|
17661 | baseDataOffsetPresent = result.flags[2] & 0x01,
|
17662 | sampleDescriptionIndexPresent = result.flags[2] & 0x02,
|
17663 | defaultSampleDurationPresent = result.flags[2] & 0x08,
|
17664 | defaultSampleSizePresent = result.flags[2] & 0x10,
|
17665 | defaultSampleFlagsPresent = result.flags[2] & 0x20,
|
17666 | durationIsEmpty = result.flags[0] & 0x010000,
|
17667 | defaultBaseIsMoof = result.flags[0] & 0x020000,
|
17668 | i;
|
17669 | i = 8;
|
17670 |
|
17671 | if (baseDataOffsetPresent) {
|
17672 | i += 4;
|
17673 |
|
17674 |
|
17675 | result.baseDataOffset = view.getUint32(12);
|
17676 | i += 4;
|
17677 | }
|
17678 |
|
17679 | if (sampleDescriptionIndexPresent) {
|
17680 | result.sampleDescriptionIndex = view.getUint32(i);
|
17681 | i += 4;
|
17682 | }
|
17683 |
|
17684 | if (defaultSampleDurationPresent) {
|
17685 | result.defaultSampleDuration = view.getUint32(i);
|
17686 | i += 4;
|
17687 | }
|
17688 |
|
17689 | if (defaultSampleSizePresent) {
|
17690 | result.defaultSampleSize = view.getUint32(i);
|
17691 | i += 4;
|
17692 | }
|
17693 |
|
17694 | if (defaultSampleFlagsPresent) {
|
17695 | result.defaultSampleFlags = view.getUint32(i);
|
17696 | }
|
17697 |
|
17698 | if (durationIsEmpty) {
|
17699 | result.durationIsEmpty = true;
|
17700 | }
|
17701 |
|
17702 | if (!baseDataOffsetPresent && defaultBaseIsMoof) {
|
17703 | result.baseDataOffsetIsMoof = true;
|
17704 | }
|
17705 |
|
17706 | return result;
|
17707 | };
|
17708 |
|
17709 | var parseTfhd$2 = tfhd;
|
17710 | var win;
|
17711 |
|
17712 | if (typeof window !== "undefined") {
|
17713 | win = window;
|
17714 | } else if (typeof commonjsGlobal !== "undefined") {
|
17715 | win = commonjsGlobal;
|
17716 | } else if (typeof self !== "undefined") {
|
17717 | win = self;
|
17718 | } else {
|
17719 | win = {};
|
17720 | }
|
17721 |
|
17722 | var window_1 = win;
|
17723 | |
17724 |
|
17725 |
|
17726 |
|
17727 |
|
17728 |
|
17729 |
|
17730 |
|
17731 |
|
17732 |
|
17733 | var discardEmulationPreventionBytes = captionPacketParser.discardEmulationPreventionBytes;
|
17734 | var CaptionStream = captionStream.CaptionStream;
|
17735 | var findBox$1 = findBox_1;
|
17736 | var parseTfdt$1 = parseTfdt$2;
|
17737 | var parseTrun$1 = parseTrun$2;
|
17738 | var parseTfhd$1 = parseTfhd$2;
|
17739 | var window$2 = window_1;
|
17740 | |
17741 |
|
17742 |
|
17743 |
|
17744 |
|
17745 |
|
17746 |
|
17747 |
|
17748 |
|
17749 |
|
17750 |
|
17751 | var mapToSample = function (offset, samples) {
|
17752 | var approximateOffset = offset;
|
17753 |
|
17754 | for (var i = 0; i < samples.length; i++) {
|
17755 | var sample = samples[i];
|
17756 |
|
17757 | if (approximateOffset < sample.size) {
|
17758 | return sample;
|
17759 | }
|
17760 |
|
17761 | approximateOffset -= sample.size;
|
17762 | }
|
17763 |
|
17764 | return null;
|
17765 | };
|
17766 | |
17767 |
|
17768 |
|
17769 |
|
17770 |
|
17771 |
|
17772 |
|
17773 |
|
17774 |
|
17775 |
|
17776 |
|
17777 |
|
17778 |
|
17779 |
|
17780 |
|
17781 |
|
17782 | var findSeiNals = function (avcStream, samples, trackId) {
|
17783 | var avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
|
17784 | result = {
|
17785 | logs: [],
|
17786 | seiNals: []
|
17787 | },
|
17788 | seiNal,
|
17789 | i,
|
17790 | length,
|
17791 | lastMatchedSample;
|
17792 |
|
17793 | for (i = 0; i + 4 < avcStream.length; i += length) {
|
17794 | length = avcView.getUint32(i);
|
17795 | i += 4;
|
17796 |
|
17797 | if (length <= 0) {
|
17798 | continue;
|
17799 | }
|
17800 |
|
17801 | switch (avcStream[i] & 0x1F) {
|
17802 | case 0x06:
|
17803 | var data = avcStream.subarray(i + 1, i + 1 + length);
|
17804 | var matchingSample = mapToSample(i, samples);
|
17805 | seiNal = {
|
17806 | nalUnitType: 'sei_rbsp',
|
17807 | size: length,
|
17808 | data: data,
|
17809 | escapedRBSP: discardEmulationPreventionBytes(data),
|
17810 | trackId: trackId
|
17811 | };
|
17812 |
|
17813 | if (matchingSample) {
|
17814 | seiNal.pts = matchingSample.pts;
|
17815 | seiNal.dts = matchingSample.dts;
|
17816 | lastMatchedSample = matchingSample;
|
17817 | } else if (lastMatchedSample) {
|
17818 |
|
17819 |
|
17820 | seiNal.pts = lastMatchedSample.pts;
|
17821 | seiNal.dts = lastMatchedSample.dts;
|
17822 | } else {
|
17823 | result.logs.push({
|
17824 | level: 'warn',
|
17825 | message: 'We\'ve encountered a nal unit without data at ' + i + ' for trackId ' + trackId + '. See mux.js#223.'
|
17826 | });
|
17827 | break;
|
17828 | }
|
17829 |
|
17830 | result.seiNals.push(seiNal);
|
17831 | break;
|
17832 | }
|
17833 | }
|
17834 |
|
17835 | return result;
|
17836 | };
|
17837 | |
17838 |
|
17839 |
|
17840 |
|
17841 |
|
17842 |
|
17843 |
|
17844 |
|
17845 |
|
17846 |
|
17847 |
|
17848 |
|
17849 |
|
17850 |
|
17851 |
|
17852 | var parseSamples = function (truns, baseMediaDecodeTime, tfhd) {
|
17853 | var currentDts = baseMediaDecodeTime;
|
17854 | var defaultSampleDuration = tfhd.defaultSampleDuration || 0;
|
17855 | var defaultSampleSize = tfhd.defaultSampleSize || 0;
|
17856 | var trackId = tfhd.trackId;
|
17857 | var allSamples = [];
|
17858 | truns.forEach(function (trun) {
|
17859 |
|
17860 |
|
17861 |
|
17862 | var trackRun = parseTrun$1(trun);
|
17863 | var samples = trackRun.samples;
|
17864 | samples.forEach(function (sample) {
|
17865 | if (sample.duration === undefined) {
|
17866 | sample.duration = defaultSampleDuration;
|
17867 | }
|
17868 |
|
17869 | if (sample.size === undefined) {
|
17870 | sample.size = defaultSampleSize;
|
17871 | }
|
17872 |
|
17873 | sample.trackId = trackId;
|
17874 | sample.dts = currentDts;
|
17875 |
|
17876 | if (sample.compositionTimeOffset === undefined) {
|
17877 | sample.compositionTimeOffset = 0;
|
17878 | }
|
17879 |
|
17880 | if (typeof currentDts === 'bigint') {
|
17881 | sample.pts = currentDts + window$2.BigInt(sample.compositionTimeOffset);
|
17882 | currentDts += window$2.BigInt(sample.duration);
|
17883 | } else {
|
17884 | sample.pts = currentDts + sample.compositionTimeOffset;
|
17885 | currentDts += sample.duration;
|
17886 | }
|
17887 | });
|
17888 | allSamples = allSamples.concat(samples);
|
17889 | });
|
17890 | return allSamples;
|
17891 | };
|
17892 | |
17893 |
|
17894 |
|
17895 |
|
17896 |
|
17897 |
|
17898 |
|
17899 |
|
17900 |
|
17901 |
|
17902 | var parseCaptionNals = function (segment, videoTrackId) {
|
17903 |
|
17904 | var trafs = findBox$1(segment, ['moof', 'traf']);
|
17905 |
|
17906 | var mdats = findBox$1(segment, ['mdat']);
|
17907 | var captionNals = {};
|
17908 | var mdatTrafPairs = [];
|
17909 |
|
17910 | mdats.forEach(function (mdat, index) {
|
17911 | var matchingTraf = trafs[index];
|
17912 | mdatTrafPairs.push({
|
17913 | mdat: mdat,
|
17914 | traf: matchingTraf
|
17915 | });
|
17916 | });
|
17917 | mdatTrafPairs.forEach(function (pair) {
|
17918 | var mdat = pair.mdat;
|
17919 | var traf = pair.traf;
|
17920 | var tfhd = findBox$1(traf, ['tfhd']);
|
17921 |
|
17922 | var headerInfo = parseTfhd$1(tfhd[0]);
|
17923 | var trackId = headerInfo.trackId;
|
17924 | var tfdt = findBox$1(traf, ['tfdt']);
|
17925 |
|
17926 | var baseMediaDecodeTime = tfdt.length > 0 ? parseTfdt$1(tfdt[0]).baseMediaDecodeTime : 0;
|
17927 | var truns = findBox$1(traf, ['trun']);
|
17928 | var samples;
|
17929 | var result;
|
17930 |
|
17931 | if (videoTrackId === trackId && truns.length > 0) {
|
17932 | samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);
|
17933 | result = findSeiNals(mdat, samples, trackId);
|
17934 |
|
17935 | if (!captionNals[trackId]) {
|
17936 | captionNals[trackId] = {
|
17937 | seiNals: [],
|
17938 | logs: []
|
17939 | };
|
17940 | }
|
17941 |
|
17942 | captionNals[trackId].seiNals = captionNals[trackId].seiNals.concat(result.seiNals);
|
17943 | captionNals[trackId].logs = captionNals[trackId].logs.concat(result.logs);
|
17944 | }
|
17945 | });
|
17946 | return captionNals;
|
17947 | };
|
17948 | |
17949 |
|
17950 |
|
17951 |
|
17952 |
|
17953 |
|
17954 |
|
17955 |
|
17956 |
|
17957 |
|
17958 |
|
17959 |
|
17960 |
|
17961 |
|
17962 |
|
17963 |
|
17964 |
|
17965 |
|
17966 |
|
17967 |
|
17968 |
|
17969 | var parseEmbeddedCaptions = function (segment, trackId, timescale) {
|
17970 | var captionNals;
|
17971 |
|
17972 | if (trackId === null) {
|
17973 | return null;
|
17974 | }
|
17975 |
|
17976 | captionNals = parseCaptionNals(segment, trackId);
|
17977 | var trackNals = captionNals[trackId] || {};
|
17978 | return {
|
17979 | seiNals: trackNals.seiNals,
|
17980 | logs: trackNals.logs,
|
17981 | timescale: timescale
|
17982 | };
|
17983 | };
|
17984 | |
17985 |
|
17986 |
|
17987 |
|
17988 |
|
17989 | var CaptionParser = function () {
|
17990 | var isInitialized = false;
|
17991 | var captionStream;
|
17992 |
|
17993 | var segmentCache;
|
17994 |
|
17995 | var trackId;
|
17996 |
|
17997 | var timescale;
|
17998 |
|
17999 | var parsedCaptions;
|
18000 |
|
18001 | var parsingPartial;
|
18002 | |
18003 |
|
18004 |
|
18005 |
|
18006 |
|
18007 | this.isInitialized = function () {
|
18008 | return isInitialized;
|
18009 | };
|
18010 | |
18011 |
|
18012 |
|
18013 |
|
18014 |
|
18015 |
|
18016 | this.init = function (options) {
|
18017 | captionStream = new CaptionStream();
|
18018 | isInitialized = true;
|
18019 | parsingPartial = options ? options.isPartial : false;
|
18020 |
|
18021 | captionStream.on('data', function (event) {
|
18022 |
|
18023 | event.startTime = event.startPts / timescale;
|
18024 | event.endTime = event.endPts / timescale;
|
18025 | parsedCaptions.captions.push(event);
|
18026 | parsedCaptions.captionStreams[event.stream] = true;
|
18027 | });
|
18028 | captionStream.on('log', function (log) {
|
18029 | parsedCaptions.logs.push(log);
|
18030 | });
|
18031 | };
|
18032 | |
18033 |
|
18034 |
|
18035 |
|
18036 |
|
18037 |
|
18038 |
|
18039 | this.isNewInit = function (videoTrackIds, timescales) {
|
18040 | if (videoTrackIds && videoTrackIds.length === 0 || timescales && typeof timescales === 'object' && Object.keys(timescales).length === 0) {
|
18041 | return false;
|
18042 | }
|
18043 |
|
18044 | return trackId !== videoTrackIds[0] || timescale !== timescales[trackId];
|
18045 | };
|
18046 | |
18047 |
|
18048 |
|
18049 |
|
18050 |
|
18051 |
|
18052 |
|
18053 |
|
18054 |
|
18055 |
|
18056 |
|
18057 |
|
18058 | this.parse = function (segment, videoTrackIds, timescales) {
|
18059 | var parsedData;
|
18060 |
|
18061 | if (!this.isInitialized()) {
|
18062 | return null;
|
18063 | } else if (!videoTrackIds || !timescales) {
|
18064 | return null;
|
18065 | } else if (this.isNewInit(videoTrackIds, timescales)) {
|
18066 |
|
18067 |
|
18068 | trackId = videoTrackIds[0];
|
18069 | timescale = timescales[trackId];
|
18070 |
|
18071 |
|
18072 | } else if (trackId === null || !timescale) {
|
18073 | segmentCache.push(segment);
|
18074 | return null;
|
18075 | }
|
18076 |
|
18077 |
|
18078 | while (segmentCache.length > 0) {
|
18079 | var cachedSegment = segmentCache.shift();
|
18080 | this.parse(cachedSegment, videoTrackIds, timescales);
|
18081 | }
|
18082 |
|
18083 | parsedData = parseEmbeddedCaptions(segment, trackId, timescale);
|
18084 |
|
18085 | if (parsedData && parsedData.logs) {
|
18086 | parsedCaptions.logs = parsedCaptions.logs.concat(parsedData.logs);
|
18087 | }
|
18088 |
|
18089 | if (parsedData === null || !parsedData.seiNals) {
|
18090 | if (parsedCaptions.logs.length) {
|
18091 | return {
|
18092 | logs: parsedCaptions.logs,
|
18093 | captions: [],
|
18094 | captionStreams: []
|
18095 | };
|
18096 | }
|
18097 |
|
18098 | return null;
|
18099 | }
|
18100 |
|
18101 | this.pushNals(parsedData.seiNals);
|
18102 |
|
18103 | this.flushStream();
|
18104 | return parsedCaptions;
|
18105 | };
|
18106 | |
18107 |
|
18108 |
|
18109 |
|
18110 |
|
18111 |
|
18112 |
|
18113 |
|
18114 | this.pushNals = function (nals) {
|
18115 | if (!this.isInitialized() || !nals || nals.length === 0) {
|
18116 | return null;
|
18117 | }
|
18118 |
|
18119 | nals.forEach(function (nal) {
|
18120 | captionStream.push(nal);
|
18121 | });
|
18122 | };
|
18123 | |
18124 |
|
18125 |
|
18126 |
|
18127 |
|
18128 |
|
18129 | this.flushStream = function () {
|
18130 | if (!this.isInitialized()) {
|
18131 | return null;
|
18132 | }
|
18133 |
|
18134 | if (!parsingPartial) {
|
18135 | captionStream.flush();
|
18136 | } else {
|
18137 | captionStream.partialFlush();
|
18138 | }
|
18139 | };
|
18140 | |
18141 |
|
18142 |
|
18143 |
|
18144 |
|
18145 | this.clearParsedCaptions = function () {
|
18146 | parsedCaptions.captions = [];
|
18147 | parsedCaptions.captionStreams = {};
|
18148 | parsedCaptions.logs = [];
|
18149 | };
|
18150 | |
18151 |
|
18152 |
|
18153 |
|
18154 |
|
18155 |
|
18156 | this.resetCaptionStream = function () {
|
18157 | if (!this.isInitialized()) {
|
18158 | return null;
|
18159 | }
|
18160 |
|
18161 | captionStream.reset();
|
18162 | };
|
18163 | |
18164 |
|
18165 |
|
18166 |
|
18167 |
|
18168 |
|
18169 |
|
18170 | this.clearAllCaptions = function () {
|
18171 | this.clearParsedCaptions();
|
18172 | this.resetCaptionStream();
|
18173 | };
|
18174 | |
18175 |
|
18176 |
|
18177 |
|
18178 |
|
18179 | this.reset = function () {
|
18180 | segmentCache = [];
|
18181 | trackId = null;
|
18182 | timescale = null;
|
18183 |
|
18184 | if (!parsedCaptions) {
|
18185 | parsedCaptions = {
|
18186 | captions: [],
|
18187 |
|
18188 | captionStreams: {},
|
18189 | logs: []
|
18190 | };
|
18191 | } else {
|
18192 | this.clearParsedCaptions();
|
18193 | }
|
18194 |
|
18195 | this.resetCaptionStream();
|
18196 | };
|
18197 |
|
18198 | this.reset();
|
18199 | };
|
18200 |
|
18201 | var captionParser = CaptionParser;
|
18202 | |
18203 |
|
18204 |
|
18205 |
|
18206 |
|
18207 |
|
18208 | var uint8ToCString$1 = function (data) {
|
18209 | var index = 0;
|
18210 | var curChar = String.fromCharCode(data[index]);
|
18211 | var retString = '';
|
18212 |
|
18213 | while (curChar !== '\0') {
|
18214 | retString += curChar;
|
18215 | index++;
|
18216 | curChar = String.fromCharCode(data[index]);
|
18217 | }
|
18218 |
|
18219 |
|
18220 | retString += curChar;
|
18221 | return retString;
|
18222 | };
|
18223 |
|
18224 | var string = {
|
18225 | uint8ToCString: uint8ToCString$1
|
18226 | };
|
18227 | var uint8ToCString = string.uint8ToCString;
|
18228 | var getUint64$1 = numbers.getUint64;
|
18229 | |
18230 |
|
18231 |
|
18232 |
|
18233 |
|
18234 |
|
18235 |
|
18236 |
|
18237 |
|
18238 |
|
18239 |
|
18240 | var parseEmsgBox = function (boxData) {
|
18241 |
|
18242 | var offset = 4;
|
18243 | var version = boxData[0];
|
18244 | var scheme_id_uri, value, timescale, presentation_time, presentation_time_delta, event_duration, id, message_data;
|
18245 |
|
18246 | if (version === 0) {
|
18247 | scheme_id_uri = uint8ToCString(boxData.subarray(offset));
|
18248 | offset += scheme_id_uri.length;
|
18249 | value = uint8ToCString(boxData.subarray(offset));
|
18250 | offset += value.length;
|
18251 | var dv = new DataView(boxData.buffer);
|
18252 | timescale = dv.getUint32(offset);
|
18253 | offset += 4;
|
18254 | presentation_time_delta = dv.getUint32(offset);
|
18255 | offset += 4;
|
18256 | event_duration = dv.getUint32(offset);
|
18257 | offset += 4;
|
18258 | id = dv.getUint32(offset);
|
18259 | offset += 4;
|
18260 | } else if (version === 1) {
|
18261 | var dv = new DataView(boxData.buffer);
|
18262 | timescale = dv.getUint32(offset);
|
18263 | offset += 4;
|
18264 | presentation_time = getUint64$1(boxData.subarray(offset));
|
18265 | offset += 8;
|
18266 | event_duration = dv.getUint32(offset);
|
18267 | offset += 4;
|
18268 | id = dv.getUint32(offset);
|
18269 | offset += 4;
|
18270 | scheme_id_uri = uint8ToCString(boxData.subarray(offset));
|
18271 | offset += scheme_id_uri.length;
|
18272 | value = uint8ToCString(boxData.subarray(offset));
|
18273 | offset += value.length;
|
18274 | }
|
18275 |
|
18276 | message_data = new Uint8Array(boxData.subarray(offset, boxData.byteLength));
|
18277 | var emsgBox = {
|
18278 | scheme_id_uri,
|
18279 | value,
|
18280 |
|
18281 | timescale: timescale ? timescale : 1,
|
18282 | presentation_time,
|
18283 | presentation_time_delta,
|
18284 | event_duration,
|
18285 | id,
|
18286 | message_data
|
18287 | };
|
18288 | return isValidEmsgBox(version, emsgBox) ? emsgBox : undefined;
|
18289 | };
|
18290 | |
18291 |
|
18292 |
|
18293 |
|
18294 |
|
18295 |
|
18296 |
|
18297 |
|
18298 |
|
18299 |
|
18300 | var scaleTime = function (presentationTime, timescale, timeDelta, offset) {
|
18301 | return presentationTime || presentationTime === 0 ? presentationTime / timescale : offset + timeDelta / timescale;
|
18302 | };
|
18303 | |
18304 |
|
18305 |
|
18306 |
|
18307 |
|
18308 |
|
18309 |
|
18310 |
|
18311 | var isValidEmsgBox = function (version, emsg) {
|
18312 | var hasScheme = emsg.scheme_id_uri !== '\0';
|
18313 | var isValidV0Box = version === 0 && isDefined(emsg.presentation_time_delta) && hasScheme;
|
18314 | var isValidV1Box = version === 1 && isDefined(emsg.presentation_time) && hasScheme;
|
18315 |
|
18316 | return !(version > 1) && isValidV0Box || isValidV1Box;
|
18317 | };
|
18318 |
|
18319 |
|
18320 | var isDefined = function (data) {
|
18321 | return data !== undefined || data !== null;
|
18322 | };
|
18323 |
|
18324 | var emsg$1 = {
|
18325 | parseEmsgBox: parseEmsgBox,
|
18326 | scaleTime: scaleTime
|
18327 | };
|
18328 | |
18329 |
|
18330 |
|
18331 |
|
18332 |
|
18333 |
|
18334 |
|
18335 |
|
18336 |
|
18337 | var toUnsigned = bin.toUnsigned;
|
18338 | var toHexString = bin.toHexString;
|
18339 | var findBox = findBox_1;
|
18340 | var parseType$1 = parseType_1;
|
18341 | var emsg = emsg$1;
|
18342 | var parseTfhd = parseTfhd$2;
|
18343 | var parseTrun = parseTrun$2;
|
18344 | var parseTfdt = parseTfdt$2;
|
18345 | var getUint64 = numbers.getUint64;
|
18346 | var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader, getEmsgID3;
|
18347 | var window$1 = window_1;
|
18348 | var parseId3Frames = parseId3.parseId3Frames;
|
18349 | |
18350 |
|
18351 |
|
18352 |
|
18353 |
|
18354 |
|
18355 |
|
18356 |
|
18357 |
|
18358 |
|
18359 |
|
18360 |
|
18361 |
|
18362 |
|
18363 |
|
18364 |
|
18365 |
|
18366 |
|
18367 |
|
18368 | timescale = function (init) {
|
18369 | var result = {},
|
18370 | traks = findBox(init, ['moov', 'trak']);
|
18371 |
|
18372 | return traks.reduce(function (result, trak) {
|
18373 | var tkhd, version, index, id, mdhd;
|
18374 | tkhd = findBox(trak, ['tkhd'])[0];
|
18375 |
|
18376 | if (!tkhd) {
|
18377 | return null;
|
18378 | }
|
18379 |
|
18380 | version = tkhd[0];
|
18381 | index = version === 0 ? 12 : 20;
|
18382 | id = toUnsigned(tkhd[index] << 24 | tkhd[index + 1] << 16 | tkhd[index + 2] << 8 | tkhd[index + 3]);
|
18383 | mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
|
18384 |
|
18385 | if (!mdhd) {
|
18386 | return null;
|
18387 | }
|
18388 |
|
18389 | version = mdhd[0];
|
18390 | index = version === 0 ? 12 : 20;
|
18391 | result[id] = toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
|
18392 | return result;
|
18393 | }, result);
|
18394 | };
|
18395 | |
18396 |
|
18397 |
|
18398 |
|
18399 |
|
18400 |
|
18401 |
|
18402 |
|
18403 |
|
18404 |
|
18405 |
|
18406 |
|
18407 |
|
18408 |
|
18409 |
|
18410 |
|
18411 |
|
18412 |
|
18413 | startTime = function (timescale, fragment) {
|
18414 | var trafs;
|
18415 |
|
18416 | trafs = findBox(fragment, ['moof', 'traf']);
|
18417 |
|
18418 | var lowestTime = trafs.reduce(function (acc, traf) {
|
18419 | var tfhd = findBox(traf, ['tfhd'])[0];
|
18420 |
|
18421 | var id = toUnsigned(tfhd[4] << 24 | tfhd[5] << 16 | tfhd[6] << 8 | tfhd[7]);
|
18422 |
|
18423 | var scale = timescale[id] || 90e3;
|
18424 |
|
18425 | var tfdt = findBox(traf, ['tfdt'])[0];
|
18426 | var dv = new DataView(tfdt.buffer, tfdt.byteOffset, tfdt.byteLength);
|
18427 | var baseTime;
|
18428 |
|
18429 | if (tfdt[0] === 1) {
|
18430 | baseTime = getUint64(tfdt.subarray(4, 12));
|
18431 | } else {
|
18432 | baseTime = dv.getUint32(4);
|
18433 | }
|
18434 |
|
18435 |
|
18436 | let seconds;
|
18437 |
|
18438 | if (typeof baseTime === 'bigint') {
|
18439 | seconds = baseTime / window$1.BigInt(scale);
|
18440 | } else if (typeof baseTime === 'number' && !isNaN(baseTime)) {
|
18441 | seconds = baseTime / scale;
|
18442 | }
|
18443 |
|
18444 | if (seconds < Number.MAX_SAFE_INTEGER) {
|
18445 | seconds = Number(seconds);
|
18446 | }
|
18447 |
|
18448 | if (seconds < acc) {
|
18449 | acc = seconds;
|
18450 | }
|
18451 |
|
18452 | return acc;
|
18453 | }, Infinity);
|
18454 | return typeof lowestTime === 'bigint' || isFinite(lowestTime) ? lowestTime : 0;
|
18455 | };
|
18456 | |
18457 |
|
18458 |
|
18459 |
|
18460 |
|
18461 |
|
18462 |
|
18463 |
|
18464 |
|
18465 |
|
18466 |
|
18467 |
|
18468 |
|
18469 |
|
18470 |
|
18471 |
|
18472 |
|
18473 |
|
18474 |
|
18475 |
|
18476 |
|
18477 | compositionStartTime = function (timescales, fragment) {
|
18478 | var trafBoxes = findBox(fragment, ['moof', 'traf']);
|
18479 | var baseMediaDecodeTime = 0;
|
18480 | var compositionTimeOffset = 0;
|
18481 | var trackId;
|
18482 |
|
18483 | if (trafBoxes && trafBoxes.length) {
|
18484 |
|
18485 |
|
18486 |
|
18487 | var tfhd = findBox(trafBoxes[0], ['tfhd'])[0];
|
18488 | var trun = findBox(trafBoxes[0], ['trun'])[0];
|
18489 | var tfdt = findBox(trafBoxes[0], ['tfdt'])[0];
|
18490 |
|
18491 | if (tfhd) {
|
18492 | var parsedTfhd = parseTfhd(tfhd);
|
18493 | trackId = parsedTfhd.trackId;
|
18494 | }
|
18495 |
|
18496 | if (tfdt) {
|
18497 | var parsedTfdt = parseTfdt(tfdt);
|
18498 | baseMediaDecodeTime = parsedTfdt.baseMediaDecodeTime;
|
18499 | }
|
18500 |
|
18501 | if (trun) {
|
18502 | var parsedTrun = parseTrun(trun);
|
18503 |
|
18504 | if (parsedTrun.samples && parsedTrun.samples.length) {
|
18505 | compositionTimeOffset = parsedTrun.samples[0].compositionTimeOffset || 0;
|
18506 | }
|
18507 | }
|
18508 | }
|
18509 |
|
18510 |
|
18511 |
|
18512 | var timescale = timescales[trackId] || 90e3;
|
18513 |
|
18514 | if (typeof baseMediaDecodeTime === 'bigint') {
|
18515 | compositionTimeOffset = window$1.BigInt(compositionTimeOffset);
|
18516 | timescale = window$1.BigInt(timescale);
|
18517 | }
|
18518 |
|
18519 | var result = (baseMediaDecodeTime + compositionTimeOffset) / timescale;
|
18520 |
|
18521 | if (typeof result === 'bigint' && result < Number.MAX_SAFE_INTEGER) {
|
18522 | result = Number(result);
|
18523 | }
|
18524 |
|
18525 | return result;
|
18526 | };
|
18527 | |
18528 |
|
18529 |
|
18530 |
|
18531 |
|
18532 |
|
18533 |
|
18534 |
|
18535 |
|
18536 |
|
18537 |
|
18538 |
|
18539 |
|
18540 | getVideoTrackIds = function (init) {
|
18541 | var traks = findBox(init, ['moov', 'trak']);
|
18542 | var videoTrackIds = [];
|
18543 | traks.forEach(function (trak) {
|
18544 | var hdlrs = findBox(trak, ['mdia', 'hdlr']);
|
18545 | var tkhds = findBox(trak, ['tkhd']);
|
18546 | hdlrs.forEach(function (hdlr, index) {
|
18547 | var handlerType = parseType$1(hdlr.subarray(8, 12));
|
18548 | var tkhd = tkhds[index];
|
18549 | var view;
|
18550 | var version;
|
18551 | var trackId;
|
18552 |
|
18553 | if (handlerType === 'vide') {
|
18554 | view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
|
18555 | version = view.getUint8(0);
|
18556 | trackId = version === 0 ? view.getUint32(12) : view.getUint32(20);
|
18557 | videoTrackIds.push(trackId);
|
18558 | }
|
18559 | });
|
18560 | });
|
18561 | return videoTrackIds;
|
18562 | };
|
18563 |
|
18564 | getTimescaleFromMediaHeader = function (mdhd) {
|
18565 |
|
18566 | var version = mdhd[0];
|
18567 | var index = version === 0 ? 12 : 20;
|
18568 | return toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
|
18569 | };
|
18570 | |
18571 |
|
18572 |
|
18573 |
|
18574 |
|
18575 |
|
18576 | getTracks = function (init) {
|
18577 | var traks = findBox(init, ['moov', 'trak']);
|
18578 | var tracks = [];
|
18579 | traks.forEach(function (trak) {
|
18580 | var track = {};
|
18581 | var tkhd = findBox(trak, ['tkhd'])[0];
|
18582 | var view, tkhdVersion;
|
18583 |
|
18584 | if (tkhd) {
|
18585 | view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
|
18586 | tkhdVersion = view.getUint8(0);
|
18587 | track.id = tkhdVersion === 0 ? view.getUint32(12) : view.getUint32(20);
|
18588 | }
|
18589 |
|
18590 | var hdlr = findBox(trak, ['mdia', 'hdlr'])[0];
|
18591 |
|
18592 | if (hdlr) {
|
18593 | var type = parseType$1(hdlr.subarray(8, 12));
|
18594 |
|
18595 | if (type === 'vide') {
|
18596 | track.type = 'video';
|
18597 | } else if (type === 'soun') {
|
18598 | track.type = 'audio';
|
18599 | } else {
|
18600 | track.type = type;
|
18601 | }
|
18602 | }
|
18603 |
|
18604 |
|
18605 | var stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
|
18606 |
|
18607 | if (stsd) {
|
18608 | var sampleDescriptions = stsd.subarray(8);
|
18609 |
|
18610 | track.codec = parseType$1(sampleDescriptions.subarray(4, 8));
|
18611 | var codecBox = findBox(sampleDescriptions, [track.codec])[0];
|
18612 | var codecConfig, codecConfigType;
|
18613 |
|
18614 | if (codecBox) {
|
18615 |
|
18616 | if (/^[asm]vc[1-9]$/i.test(track.codec)) {
|
18617 |
|
18618 |
|
18619 | codecConfig = codecBox.subarray(78);
|
18620 | codecConfigType = parseType$1(codecConfig.subarray(4, 8));
|
18621 |
|
18622 | if (codecConfigType === 'avcC' && codecConfig.length > 11) {
|
18623 | track.codec += '.';
|
18624 |
|
18625 |
|
18626 | track.codec += toHexString(codecConfig[9]);
|
18627 |
|
18628 | track.codec += toHexString(codecConfig[10]);
|
18629 |
|
18630 | track.codec += toHexString(codecConfig[11]);
|
18631 | } else {
|
18632 |
|
18633 |
|
18634 | track.codec = 'avc1.4d400d';
|
18635 | }
|
18636 | } else if (/^mp4[a,v]$/i.test(track.codec)) {
|
18637 |
|
18638 | codecConfig = codecBox.subarray(28);
|
18639 | codecConfigType = parseType$1(codecConfig.subarray(4, 8));
|
18640 |
|
18641 | if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
|
18642 | track.codec += '.' + toHexString(codecConfig[19]);
|
18643 |
|
18644 | track.codec += '.' + toHexString(codecConfig[20] >>> 2 & 0x3f).replace(/^0/, '');
|
18645 | } else {
|
18646 |
|
18647 |
|
18648 | track.codec = 'mp4a.40.2';
|
18649 | }
|
18650 | } else {
|
18651 |
|
18652 | track.codec = track.codec.toLowerCase();
|
18653 | }
|
18654 | }
|
18655 | }
|
18656 |
|
18657 | var mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
|
18658 |
|
18659 | if (mdhd) {
|
18660 | track.timescale = getTimescaleFromMediaHeader(mdhd);
|
18661 | }
|
18662 |
|
18663 | tracks.push(track);
|
18664 | });
|
18665 | return tracks;
|
18666 | };
|
18667 | |
18668 |
|
18669 |
|
18670 |
|
18671 |
|
18672 |
|
18673 |
|
18674 |
|
18675 |
|
18676 |
|
18677 |
|
18678 |
|
18679 | getEmsgID3 = function (segmentData, offset = 0) {
|
18680 | var emsgBoxes = findBox(segmentData, ['emsg']);
|
18681 | return emsgBoxes.map(data => {
|
18682 | var parsedBox = emsg.parseEmsgBox(new Uint8Array(data));
|
18683 | var parsedId3Frames = parseId3Frames(parsedBox.message_data);
|
18684 | return {
|
18685 | cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset),
|
18686 | duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale),
|
18687 | frames: parsedId3Frames
|
18688 | };
|
18689 | });
|
18690 | };
|
18691 |
|
18692 | var probe$2 = {
|
18693 |
|
18694 | findBox: findBox,
|
18695 | parseType: parseType$1,
|
18696 | timescale: timescale,
|
18697 | startTime: startTime,
|
18698 | compositionStartTime: compositionStartTime,
|
18699 | videoTrackIds: getVideoTrackIds,
|
18700 | tracks: getTracks,
|
18701 | getTimescaleFromMediaHeader: getTimescaleFromMediaHeader,
|
18702 | getEmsgID3: getEmsgID3
|
18703 | };
|
18704 | |
18705 |
|
18706 |
|
18707 |
|
18708 |
|
18709 |
|
18710 |
|
18711 |
|
18712 |
|
18713 | var StreamTypes$1 = streamTypes;
|
18714 |
|
18715 | var parsePid = function (packet) {
|
18716 | var pid = packet[1] & 0x1f;
|
18717 | pid <<= 8;
|
18718 | pid |= packet[2];
|
18719 | return pid;
|
18720 | };
|
18721 |
|
18722 | var parsePayloadUnitStartIndicator = function (packet) {
|
18723 | return !!(packet[1] & 0x40);
|
18724 | };
|
18725 |
|
18726 | var parseAdaptionField = function (packet) {
|
18727 | var offset = 0;
|
18728 |
|
18729 |
|
18730 |
|
18731 |
|
18732 |
|
18733 | if ((packet[3] & 0x30) >>> 4 > 0x01) {
|
18734 | offset += packet[4] + 1;
|
18735 | }
|
18736 |
|
18737 | return offset;
|
18738 | };
|
18739 |
|
18740 | var parseType = function (packet, pmtPid) {
|
18741 | var pid = parsePid(packet);
|
18742 |
|
18743 | if (pid === 0) {
|
18744 | return 'pat';
|
18745 | } else if (pid === pmtPid) {
|
18746 | return 'pmt';
|
18747 | } else if (pmtPid) {
|
18748 | return 'pes';
|
18749 | }
|
18750 |
|
18751 | return null;
|
18752 | };
|
18753 |
|
18754 | var parsePat = function (packet) {
|
18755 | var pusi = parsePayloadUnitStartIndicator(packet);
|
18756 | var offset = 4 + parseAdaptionField(packet);
|
18757 |
|
18758 | if (pusi) {
|
18759 | offset += packet[offset] + 1;
|
18760 | }
|
18761 |
|
18762 | return (packet[offset + 10] & 0x1f) << 8 | packet[offset + 11];
|
18763 | };
|
18764 |
|
18765 | var parsePmt = function (packet) {
|
18766 | var programMapTable = {};
|
18767 | var pusi = parsePayloadUnitStartIndicator(packet);
|
18768 | var payloadOffset = 4 + parseAdaptionField(packet);
|
18769 |
|
18770 | if (pusi) {
|
18771 | payloadOffset += packet[payloadOffset] + 1;
|
18772 | }
|
18773 |
|
18774 |
|
18775 |
|
18776 |
|
18777 |
|
18778 |
|
18779 | if (!(packet[payloadOffset + 5] & 0x01)) {
|
18780 | return;
|
18781 | }
|
18782 |
|
18783 | var sectionLength, tableEnd, programInfoLength;
|
18784 |
|
18785 | sectionLength = (packet[payloadOffset + 1] & 0x0f) << 8 | packet[payloadOffset + 2];
|
18786 | tableEnd = 3 + sectionLength - 4;
|
18787 |
|
18788 |
|
18789 | programInfoLength = (packet[payloadOffset + 10] & 0x0f) << 8 | packet[payloadOffset + 11];
|
18790 |
|
18791 | var offset = 12 + programInfoLength;
|
18792 |
|
18793 | while (offset < tableEnd) {
|
18794 | var i = payloadOffset + offset;
|
18795 |
|
18796 | programMapTable[(packet[i + 1] & 0x1F) << 8 | packet[i + 2]] = packet[i];
|
18797 |
|
18798 |
|
18799 | offset += ((packet[i + 3] & 0x0F) << 8 | packet[i + 4]) + 5;
|
18800 | }
|
18801 |
|
18802 | return programMapTable;
|
18803 | };
|
18804 |
|
18805 | var parsePesType = function (packet, programMapTable) {
|
18806 | var pid = parsePid(packet);
|
18807 | var type = programMapTable[pid];
|
18808 |
|
18809 | switch (type) {
|
18810 | case StreamTypes$1.H264_STREAM_TYPE:
|
18811 | return 'video';
|
18812 |
|
18813 | case StreamTypes$1.ADTS_STREAM_TYPE:
|
18814 | return 'audio';
|
18815 |
|
18816 | case StreamTypes$1.METADATA_STREAM_TYPE:
|
18817 | return 'timed-metadata';
|
18818 |
|
18819 | default:
|
18820 | return null;
|
18821 | }
|
18822 | };
|
18823 |
|
18824 | var parsePesTime = function (packet) {
|
18825 | var pusi = parsePayloadUnitStartIndicator(packet);
|
18826 |
|
18827 | if (!pusi) {
|
18828 | return null;
|
18829 | }
|
18830 |
|
18831 | var offset = 4 + parseAdaptionField(packet);
|
18832 |
|
18833 | if (offset >= packet.byteLength) {
|
18834 |
|
18835 |
|
18836 |
|
18837 |
|
18838 |
|
18839 |
|
18840 |
|
18841 |
|
18842 |
|
18843 |
|
18844 | return null;
|
18845 | }
|
18846 |
|
18847 | var pes = null;
|
18848 | var ptsDtsFlags;
|
18849 |
|
18850 |
|
18851 |
|
18852 | ptsDtsFlags = packet[offset + 7];
|
18853 |
|
18854 |
|
18855 |
|
18856 |
|
18857 |
|
18858 |
|
18859 |
|
18860 |
|
18861 | if (ptsDtsFlags & 0xC0) {
|
18862 | pes = {};
|
18863 |
|
18864 |
|
18865 |
|
18866 | 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;
|
18867 | pes.pts *= 4;
|
18868 |
|
18869 | pes.pts += (packet[offset + 13] & 0x06) >>> 1;
|
18870 |
|
18871 | pes.dts = pes.pts;
|
18872 |
|
18873 | if (ptsDtsFlags & 0x40) {
|
18874 | 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;
|
18875 | pes.dts *= 4;
|
18876 |
|
18877 | pes.dts += (packet[offset + 18] & 0x06) >>> 1;
|
18878 | }
|
18879 | }
|
18880 |
|
18881 | return pes;
|
18882 | };
|
18883 |
|
18884 | var parseNalUnitType = function (type) {
|
18885 | switch (type) {
|
18886 | case 0x05:
|
18887 | return 'slice_layer_without_partitioning_rbsp_idr';
|
18888 |
|
18889 | case 0x06:
|
18890 | return 'sei_rbsp';
|
18891 |
|
18892 | case 0x07:
|
18893 | return 'seq_parameter_set_rbsp';
|
18894 |
|
18895 | case 0x08:
|
18896 | return 'pic_parameter_set_rbsp';
|
18897 |
|
18898 | case 0x09:
|
18899 | return 'access_unit_delimiter_rbsp';
|
18900 |
|
18901 | default:
|
18902 | return null;
|
18903 | }
|
18904 | };
|
18905 |
|
18906 | var videoPacketContainsKeyFrame = function (packet) {
|
18907 | var offset = 4 + parseAdaptionField(packet);
|
18908 | var frameBuffer = packet.subarray(offset);
|
18909 | var frameI = 0;
|
18910 | var frameSyncPoint = 0;
|
18911 | var foundKeyFrame = false;
|
18912 | var nalType;
|
18913 |
|
18914 | for (; frameSyncPoint < frameBuffer.byteLength - 3; frameSyncPoint++) {
|
18915 | if (frameBuffer[frameSyncPoint + 2] === 1) {
|
18916 |
|
18917 | frameI = frameSyncPoint + 5;
|
18918 | break;
|
18919 | }
|
18920 | }
|
18921 |
|
18922 | while (frameI < frameBuffer.byteLength) {
|
18923 |
|
18924 |
|
18925 | switch (frameBuffer[frameI]) {
|
18926 | case 0:
|
18927 |
|
18928 | if (frameBuffer[frameI - 1] !== 0) {
|
18929 | frameI += 2;
|
18930 | break;
|
18931 | } else if (frameBuffer[frameI - 2] !== 0) {
|
18932 | frameI++;
|
18933 | break;
|
18934 | }
|
18935 |
|
18936 | if (frameSyncPoint + 3 !== frameI - 2) {
|
18937 | nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
|
18938 |
|
18939 | if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
|
18940 | foundKeyFrame = true;
|
18941 | }
|
18942 | }
|
18943 |
|
18944 |
|
18945 | do {
|
18946 | frameI++;
|
18947 | } while (frameBuffer[frameI] !== 1 && frameI < frameBuffer.length);
|
18948 |
|
18949 | frameSyncPoint = frameI - 2;
|
18950 | frameI += 3;
|
18951 | break;
|
18952 |
|
18953 | case 1:
|
18954 |
|
18955 | if (frameBuffer[frameI - 1] !== 0 || frameBuffer[frameI - 2] !== 0) {
|
18956 | frameI += 3;
|
18957 | break;
|
18958 | }
|
18959 |
|
18960 | nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
|
18961 |
|
18962 | if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
|
18963 | foundKeyFrame = true;
|
18964 | }
|
18965 |
|
18966 | frameSyncPoint = frameI - 2;
|
18967 | frameI += 3;
|
18968 | break;
|
18969 |
|
18970 | default:
|
18971 |
|
18972 |
|
18973 | frameI += 3;
|
18974 | break;
|
18975 | }
|
18976 | }
|
18977 |
|
18978 | frameBuffer = frameBuffer.subarray(frameSyncPoint);
|
18979 | frameI -= frameSyncPoint;
|
18980 | frameSyncPoint = 0;
|
18981 |
|
18982 | if (frameBuffer && frameBuffer.byteLength > 3) {
|
18983 | nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
|
18984 |
|
18985 | if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
|
18986 | foundKeyFrame = true;
|
18987 | }
|
18988 | }
|
18989 |
|
18990 | return foundKeyFrame;
|
18991 | };
|
18992 |
|
18993 | var probe$1 = {
|
18994 | parseType: parseType,
|
18995 | parsePat: parsePat,
|
18996 | parsePmt: parsePmt,
|
18997 | parsePayloadUnitStartIndicator: parsePayloadUnitStartIndicator,
|
18998 | parsePesType: parsePesType,
|
18999 | parsePesTime: parsePesTime,
|
19000 | videoPacketContainsKeyFrame: videoPacketContainsKeyFrame
|
19001 | };
|
19002 | |
19003 |
|
19004 |
|
19005 |
|
19006 |
|
19007 |
|
19008 |
|
19009 |
|
19010 |
|
19011 | var StreamTypes = streamTypes;
|
19012 | var handleRollover = timestampRolloverStream.handleRollover;
|
19013 | var probe = {};
|
19014 | probe.ts = probe$1;
|
19015 | probe.aac = utils;
|
19016 | var ONE_SECOND_IN_TS = clock$2.ONE_SECOND_IN_TS;
|
19017 | var MP2T_PACKET_LENGTH = 188,
|
19018 |
|
19019 | SYNC_BYTE = 0x47;
|
19020 | |
19021 |
|
19022 |
|
19023 |
|
19024 |
|
19025 | var parsePsi_ = function (bytes, pmt) {
|
19026 | var startIndex = 0,
|
19027 | endIndex = MP2T_PACKET_LENGTH,
|
19028 | packet,
|
19029 | type;
|
19030 |
|
19031 | while (endIndex < bytes.byteLength) {
|
19032 |
|
19033 | if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
|
19034 |
|
19035 | packet = bytes.subarray(startIndex, endIndex);
|
19036 | type = probe.ts.parseType(packet, pmt.pid);
|
19037 |
|
19038 | switch (type) {
|
19039 | case 'pat':
|
19040 | pmt.pid = probe.ts.parsePat(packet);
|
19041 | break;
|
19042 |
|
19043 | case 'pmt':
|
19044 | var table = probe.ts.parsePmt(packet);
|
19045 | pmt.table = pmt.table || {};
|
19046 | Object.keys(table).forEach(function (key) {
|
19047 | pmt.table[key] = table[key];
|
19048 | });
|
19049 | break;
|
19050 | }
|
19051 |
|
19052 | startIndex += MP2T_PACKET_LENGTH;
|
19053 | endIndex += MP2T_PACKET_LENGTH;
|
19054 | continue;
|
19055 | }
|
19056 |
|
19057 |
|
19058 |
|
19059 |
|
19060 | startIndex++;
|
19061 | endIndex++;
|
19062 | }
|
19063 | };
|
19064 | |
19065 |
|
19066 |
|
19067 |
|
19068 |
|
19069 |
|
19070 | var parseAudioPes_ = function (bytes, pmt, result) {
|
19071 | var startIndex = 0,
|
19072 | endIndex = MP2T_PACKET_LENGTH,
|
19073 | packet,
|
19074 | type,
|
19075 | pesType,
|
19076 | pusi,
|
19077 | parsed;
|
19078 | var endLoop = false;
|
19079 |
|
19080 | while (endIndex <= bytes.byteLength) {
|
19081 |
|
19082 | if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
|
19083 |
|
19084 | packet = bytes.subarray(startIndex, endIndex);
|
19085 | type = probe.ts.parseType(packet, pmt.pid);
|
19086 |
|
19087 | switch (type) {
|
19088 | case 'pes':
|
19089 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
19090 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
19091 |
|
19092 | if (pesType === 'audio' && pusi) {
|
19093 | parsed = probe.ts.parsePesTime(packet);
|
19094 |
|
19095 | if (parsed) {
|
19096 | parsed.type = 'audio';
|
19097 | result.audio.push(parsed);
|
19098 | endLoop = true;
|
19099 | }
|
19100 | }
|
19101 |
|
19102 | break;
|
19103 | }
|
19104 |
|
19105 | if (endLoop) {
|
19106 | break;
|
19107 | }
|
19108 |
|
19109 | startIndex += MP2T_PACKET_LENGTH;
|
19110 | endIndex += MP2T_PACKET_LENGTH;
|
19111 | continue;
|
19112 | }
|
19113 |
|
19114 |
|
19115 |
|
19116 |
|
19117 | startIndex++;
|
19118 | endIndex++;
|
19119 | }
|
19120 |
|
19121 |
|
19122 | endIndex = bytes.byteLength;
|
19123 | startIndex = endIndex - MP2T_PACKET_LENGTH;
|
19124 | endLoop = false;
|
19125 |
|
19126 | while (startIndex >= 0) {
|
19127 |
|
19128 | if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
|
19129 |
|
19130 | packet = bytes.subarray(startIndex, endIndex);
|
19131 | type = probe.ts.parseType(packet, pmt.pid);
|
19132 |
|
19133 | switch (type) {
|
19134 | case 'pes':
|
19135 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
19136 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
19137 |
|
19138 | if (pesType === 'audio' && pusi) {
|
19139 | parsed = probe.ts.parsePesTime(packet);
|
19140 |
|
19141 | if (parsed) {
|
19142 | parsed.type = 'audio';
|
19143 | result.audio.push(parsed);
|
19144 | endLoop = true;
|
19145 | }
|
19146 | }
|
19147 |
|
19148 | break;
|
19149 | }
|
19150 |
|
19151 | if (endLoop) {
|
19152 | break;
|
19153 | }
|
19154 |
|
19155 | startIndex -= MP2T_PACKET_LENGTH;
|
19156 | endIndex -= MP2T_PACKET_LENGTH;
|
19157 | continue;
|
19158 | }
|
19159 |
|
19160 |
|
19161 |
|
19162 |
|
19163 | startIndex--;
|
19164 | endIndex--;
|
19165 | }
|
19166 | };
|
19167 | |
19168 |
|
19169 |
|
19170 |
|
19171 |
|
19172 |
|
19173 |
|
19174 | var parseVideoPes_ = function (bytes, pmt, result) {
|
19175 | var startIndex = 0,
|
19176 | endIndex = MP2T_PACKET_LENGTH,
|
19177 | packet,
|
19178 | type,
|
19179 | pesType,
|
19180 | pusi,
|
19181 | parsed,
|
19182 | frame,
|
19183 | i,
|
19184 | pes;
|
19185 | var endLoop = false;
|
19186 | var currentFrame = {
|
19187 | data: [],
|
19188 | size: 0
|
19189 | };
|
19190 |
|
19191 | while (endIndex < bytes.byteLength) {
|
19192 |
|
19193 | if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
|
19194 |
|
19195 | packet = bytes.subarray(startIndex, endIndex);
|
19196 | type = probe.ts.parseType(packet, pmt.pid);
|
19197 |
|
19198 | switch (type) {
|
19199 | case 'pes':
|
19200 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
19201 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
19202 |
|
19203 | if (pesType === 'video') {
|
19204 | if (pusi && !endLoop) {
|
19205 | parsed = probe.ts.parsePesTime(packet);
|
19206 |
|
19207 | if (parsed) {
|
19208 | parsed.type = 'video';
|
19209 | result.video.push(parsed);
|
19210 | endLoop = true;
|
19211 | }
|
19212 | }
|
19213 |
|
19214 | if (!result.firstKeyFrame) {
|
19215 | if (pusi) {
|
19216 | if (currentFrame.size !== 0) {
|
19217 | frame = new Uint8Array(currentFrame.size);
|
19218 | i = 0;
|
19219 |
|
19220 | while (currentFrame.data.length) {
|
19221 | pes = currentFrame.data.shift();
|
19222 | frame.set(pes, i);
|
19223 | i += pes.byteLength;
|
19224 | }
|
19225 |
|
19226 | if (probe.ts.videoPacketContainsKeyFrame(frame)) {
|
19227 | var firstKeyFrame = probe.ts.parsePesTime(frame);
|
19228 |
|
19229 |
|
19230 |
|
19231 | if (firstKeyFrame) {
|
19232 | result.firstKeyFrame = firstKeyFrame;
|
19233 | result.firstKeyFrame.type = 'video';
|
19234 | } else {
|
19235 |
|
19236 | 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.');
|
19237 | }
|
19238 | }
|
19239 |
|
19240 | currentFrame.size = 0;
|
19241 | }
|
19242 | }
|
19243 |
|
19244 | currentFrame.data.push(packet);
|
19245 | currentFrame.size += packet.byteLength;
|
19246 | }
|
19247 | }
|
19248 |
|
19249 | break;
|
19250 | }
|
19251 |
|
19252 | if (endLoop && result.firstKeyFrame) {
|
19253 | break;
|
19254 | }
|
19255 |
|
19256 | startIndex += MP2T_PACKET_LENGTH;
|
19257 | endIndex += MP2T_PACKET_LENGTH;
|
19258 | continue;
|
19259 | }
|
19260 |
|
19261 |
|
19262 |
|
19263 |
|
19264 | startIndex++;
|
19265 | endIndex++;
|
19266 | }
|
19267 |
|
19268 |
|
19269 | endIndex = bytes.byteLength;
|
19270 | startIndex = endIndex - MP2T_PACKET_LENGTH;
|
19271 | endLoop = false;
|
19272 |
|
19273 | while (startIndex >= 0) {
|
19274 |
|
19275 | if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
|
19276 |
|
19277 | packet = bytes.subarray(startIndex, endIndex);
|
19278 | type = probe.ts.parseType(packet, pmt.pid);
|
19279 |
|
19280 | switch (type) {
|
19281 | case 'pes':
|
19282 | pesType = probe.ts.parsePesType(packet, pmt.table);
|
19283 | pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
|
19284 |
|
19285 | if (pesType === 'video' && pusi) {
|
19286 | parsed = probe.ts.parsePesTime(packet);
|
19287 |
|
19288 | if (parsed) {
|
19289 | parsed.type = 'video';
|
19290 | result.video.push(parsed);
|
19291 | endLoop = true;
|
19292 | }
|
19293 | }
|
19294 |
|
19295 | break;
|
19296 | }
|
19297 |
|
19298 | if (endLoop) {
|
19299 | break;
|
19300 | }
|
19301 |
|
19302 | startIndex -= MP2T_PACKET_LENGTH;
|
19303 | endIndex -= MP2T_PACKET_LENGTH;
|
19304 | continue;
|
19305 | }
|
19306 |
|
19307 |
|
19308 |
|
19309 |
|
19310 | startIndex--;
|
19311 | endIndex--;
|
19312 | }
|
19313 | };
|
19314 | |
19315 |
|
19316 |
|
19317 |
|
19318 |
|
19319 |
|
19320 | var adjustTimestamp_ = function (segmentInfo, baseTimestamp) {
|
19321 | if (segmentInfo.audio && segmentInfo.audio.length) {
|
19322 | var audioBaseTimestamp = baseTimestamp;
|
19323 |
|
19324 | if (typeof audioBaseTimestamp === 'undefined' || isNaN(audioBaseTimestamp)) {
|
19325 | audioBaseTimestamp = segmentInfo.audio[0].dts;
|
19326 | }
|
19327 |
|
19328 | segmentInfo.audio.forEach(function (info) {
|
19329 | info.dts = handleRollover(info.dts, audioBaseTimestamp);
|
19330 | info.pts = handleRollover(info.pts, audioBaseTimestamp);
|
19331 |
|
19332 | info.dtsTime = info.dts / ONE_SECOND_IN_TS;
|
19333 | info.ptsTime = info.pts / ONE_SECOND_IN_TS;
|
19334 | });
|
19335 | }
|
19336 |
|
19337 | if (segmentInfo.video && segmentInfo.video.length) {
|
19338 | var videoBaseTimestamp = baseTimestamp;
|
19339 |
|
19340 | if (typeof videoBaseTimestamp === 'undefined' || isNaN(videoBaseTimestamp)) {
|
19341 | videoBaseTimestamp = segmentInfo.video[0].dts;
|
19342 | }
|
19343 |
|
19344 | segmentInfo.video.forEach(function (info) {
|
19345 | info.dts = handleRollover(info.dts, videoBaseTimestamp);
|
19346 | info.pts = handleRollover(info.pts, videoBaseTimestamp);
|
19347 |
|
19348 | info.dtsTime = info.dts / ONE_SECOND_IN_TS;
|
19349 | info.ptsTime = info.pts / ONE_SECOND_IN_TS;
|
19350 | });
|
19351 |
|
19352 | if (segmentInfo.firstKeyFrame) {
|
19353 | var frame = segmentInfo.firstKeyFrame;
|
19354 | frame.dts = handleRollover(frame.dts, videoBaseTimestamp);
|
19355 | frame.pts = handleRollover(frame.pts, videoBaseTimestamp);
|
19356 |
|
19357 | frame.dtsTime = frame.dts / ONE_SECOND_IN_TS;
|
19358 | frame.ptsTime = frame.pts / ONE_SECOND_IN_TS;
|
19359 | }
|
19360 | }
|
19361 | };
|
19362 | |
19363 |
|
19364 |
|
19365 |
|
19366 |
|
19367 | var inspectAac_ = function (bytes) {
|
19368 | var endLoop = false,
|
19369 | audioCount = 0,
|
19370 | sampleRate = null,
|
19371 | timestamp = null,
|
19372 | frameSize = 0,
|
19373 | byteIndex = 0,
|
19374 | packet;
|
19375 |
|
19376 | while (bytes.length - byteIndex >= 3) {
|
19377 | var type = probe.aac.parseType(bytes, byteIndex);
|
19378 |
|
19379 | switch (type) {
|
19380 | case 'timed-metadata':
|
19381 |
|
19382 |
|
19383 | if (bytes.length - byteIndex < 10) {
|
19384 | endLoop = true;
|
19385 | break;
|
19386 | }
|
19387 |
|
19388 | frameSize = probe.aac.parseId3TagSize(bytes, byteIndex);
|
19389 |
|
19390 |
|
19391 | if (frameSize > bytes.length) {
|
19392 | endLoop = true;
|
19393 | break;
|
19394 | }
|
19395 |
|
19396 | if (timestamp === null) {
|
19397 | packet = bytes.subarray(byteIndex, byteIndex + frameSize);
|
19398 | timestamp = probe.aac.parseAacTimestamp(packet);
|
19399 | }
|
19400 |
|
19401 | byteIndex += frameSize;
|
19402 | break;
|
19403 |
|
19404 | case 'audio':
|
19405 |
|
19406 |
|
19407 | if (bytes.length - byteIndex < 7) {
|
19408 | endLoop = true;
|
19409 | break;
|
19410 | }
|
19411 |
|
19412 | frameSize = probe.aac.parseAdtsSize(bytes, byteIndex);
|
19413 |
|
19414 |
|
19415 | if (frameSize > bytes.length) {
|
19416 | endLoop = true;
|
19417 | break;
|
19418 | }
|
19419 |
|
19420 | if (sampleRate === null) {
|
19421 | packet = bytes.subarray(byteIndex, byteIndex + frameSize);
|
19422 | sampleRate = probe.aac.parseSampleRate(packet);
|
19423 | }
|
19424 |
|
19425 | audioCount++;
|
19426 | byteIndex += frameSize;
|
19427 | break;
|
19428 |
|
19429 | default:
|
19430 | byteIndex++;
|
19431 | break;
|
19432 | }
|
19433 |
|
19434 | if (endLoop) {
|
19435 | return null;
|
19436 | }
|
19437 | }
|
19438 |
|
19439 | if (sampleRate === null || timestamp === null) {
|
19440 | return null;
|
19441 | }
|
19442 |
|
19443 | var audioTimescale = ONE_SECOND_IN_TS / sampleRate;
|
19444 | var result = {
|
19445 | audio: [{
|
19446 | type: 'audio',
|
19447 | dts: timestamp,
|
19448 | pts: timestamp
|
19449 | }, {
|
19450 | type: 'audio',
|
19451 | dts: timestamp + audioCount * 1024 * audioTimescale,
|
19452 | pts: timestamp + audioCount * 1024 * audioTimescale
|
19453 | }]
|
19454 | };
|
19455 | return result;
|
19456 | };
|
19457 | |
19458 |
|
19459 |
|
19460 |
|
19461 |
|
19462 |
|
19463 |
|
19464 | var inspectTs_ = function (bytes) {
|
19465 | var pmt = {
|
19466 | pid: null,
|
19467 | table: null
|
19468 | };
|
19469 | var result = {};
|
19470 | parsePsi_(bytes, pmt);
|
19471 |
|
19472 | for (var pid in pmt.table) {
|
19473 | if (pmt.table.hasOwnProperty(pid)) {
|
19474 | var type = pmt.table[pid];
|
19475 |
|
19476 | switch (type) {
|
19477 | case StreamTypes.H264_STREAM_TYPE:
|
19478 | result.video = [];
|
19479 | parseVideoPes_(bytes, pmt, result);
|
19480 |
|
19481 | if (result.video.length === 0) {
|
19482 | delete result.video;
|
19483 | }
|
19484 |
|
19485 | break;
|
19486 |
|
19487 | case StreamTypes.ADTS_STREAM_TYPE:
|
19488 | result.audio = [];
|
19489 | parseAudioPes_(bytes, pmt, result);
|
19490 |
|
19491 | if (result.audio.length === 0) {
|
19492 | delete result.audio;
|
19493 | }
|
19494 |
|
19495 | break;
|
19496 | }
|
19497 | }
|
19498 | }
|
19499 |
|
19500 | return result;
|
19501 | };
|
19502 | |
19503 |
|
19504 |
|
19505 |
|
19506 |
|
19507 |
|
19508 |
|
19509 |
|
19510 |
|
19511 |
|
19512 | var inspect = function (bytes, baseTimestamp) {
|
19513 | var isAacData = probe.aac.isLikelyAacData(bytes);
|
19514 | var result;
|
19515 |
|
19516 | if (isAacData) {
|
19517 | result = inspectAac_(bytes);
|
19518 | } else {
|
19519 | result = inspectTs_(bytes);
|
19520 | }
|
19521 |
|
19522 | if (!result || !result.audio && !result.video) {
|
19523 | return null;
|
19524 | }
|
19525 |
|
19526 | adjustTimestamp_(result, baseTimestamp);
|
19527 | return result;
|
19528 | };
|
19529 |
|
19530 | var tsInspector = {
|
19531 | inspect: inspect,
|
19532 | parseAudioPes_: parseAudioPes_
|
19533 | };
|
19534 |
|
19535 |
|
19536 | |
19537 |
|
19538 |
|
19539 |
|
19540 |
|
19541 |
|
19542 |
|
19543 |
|
19544 | const wireTransmuxerEvents = function (self, transmuxer) {
|
19545 | transmuxer.on('data', function (segment) {
|
19546 |
|
19547 |
|
19548 |
|
19549 |
|
19550 | const initArray = segment.initSegment;
|
19551 | segment.initSegment = {
|
19552 | data: initArray.buffer,
|
19553 | byteOffset: initArray.byteOffset,
|
19554 | byteLength: initArray.byteLength
|
19555 | };
|
19556 | const typedArray = segment.data;
|
19557 | segment.data = typedArray.buffer;
|
19558 | self.postMessage({
|
19559 | action: 'data',
|
19560 | segment,
|
19561 | byteOffset: typedArray.byteOffset,
|
19562 | byteLength: typedArray.byteLength
|
19563 | }, [segment.data]);
|
19564 | });
|
19565 | transmuxer.on('done', function (data) {
|
19566 | self.postMessage({
|
19567 | action: 'done'
|
19568 | });
|
19569 | });
|
19570 | transmuxer.on('gopInfo', function (gopInfo) {
|
19571 | self.postMessage({
|
19572 | action: 'gopInfo',
|
19573 | gopInfo
|
19574 | });
|
19575 | });
|
19576 | transmuxer.on('videoSegmentTimingInfo', function (timingInfo) {
|
19577 | const videoSegmentTimingInfo = {
|
19578 | start: {
|
19579 | decode: clock$2.videoTsToSeconds(timingInfo.start.dts),
|
19580 | presentation: clock$2.videoTsToSeconds(timingInfo.start.pts)
|
19581 | },
|
19582 | end: {
|
19583 | decode: clock$2.videoTsToSeconds(timingInfo.end.dts),
|
19584 | presentation: clock$2.videoTsToSeconds(timingInfo.end.pts)
|
19585 | },
|
19586 | baseMediaDecodeTime: clock$2.videoTsToSeconds(timingInfo.baseMediaDecodeTime)
|
19587 | };
|
19588 |
|
19589 | if (timingInfo.prependedContentDuration) {
|
19590 | videoSegmentTimingInfo.prependedContentDuration = clock$2.videoTsToSeconds(timingInfo.prependedContentDuration);
|
19591 | }
|
19592 |
|
19593 | self.postMessage({
|
19594 | action: 'videoSegmentTimingInfo',
|
19595 | videoSegmentTimingInfo
|
19596 | });
|
19597 | });
|
19598 | transmuxer.on('audioSegmentTimingInfo', function (timingInfo) {
|
19599 |
|
19600 | const audioSegmentTimingInfo = {
|
19601 | start: {
|
19602 | decode: clock$2.videoTsToSeconds(timingInfo.start.dts),
|
19603 | presentation: clock$2.videoTsToSeconds(timingInfo.start.pts)
|
19604 | },
|
19605 | end: {
|
19606 | decode: clock$2.videoTsToSeconds(timingInfo.end.dts),
|
19607 | presentation: clock$2.videoTsToSeconds(timingInfo.end.pts)
|
19608 | },
|
19609 | baseMediaDecodeTime: clock$2.videoTsToSeconds(timingInfo.baseMediaDecodeTime)
|
19610 | };
|
19611 |
|
19612 | if (timingInfo.prependedContentDuration) {
|
19613 | audioSegmentTimingInfo.prependedContentDuration = clock$2.videoTsToSeconds(timingInfo.prependedContentDuration);
|
19614 | }
|
19615 |
|
19616 | self.postMessage({
|
19617 | action: 'audioSegmentTimingInfo',
|
19618 | audioSegmentTimingInfo
|
19619 | });
|
19620 | });
|
19621 | transmuxer.on('id3Frame', function (id3Frame) {
|
19622 | self.postMessage({
|
19623 | action: 'id3Frame',
|
19624 | id3Frame
|
19625 | });
|
19626 | });
|
19627 | transmuxer.on('caption', function (caption) {
|
19628 | self.postMessage({
|
19629 | action: 'caption',
|
19630 | caption
|
19631 | });
|
19632 | });
|
19633 | transmuxer.on('trackinfo', function (trackInfo) {
|
19634 | self.postMessage({
|
19635 | action: 'trackinfo',
|
19636 | trackInfo
|
19637 | });
|
19638 | });
|
19639 | transmuxer.on('audioTimingInfo', function (audioTimingInfo) {
|
19640 |
|
19641 | self.postMessage({
|
19642 | action: 'audioTimingInfo',
|
19643 | audioTimingInfo: {
|
19644 | start: clock$2.videoTsToSeconds(audioTimingInfo.start),
|
19645 | end: clock$2.videoTsToSeconds(audioTimingInfo.end)
|
19646 | }
|
19647 | });
|
19648 | });
|
19649 | transmuxer.on('videoTimingInfo', function (videoTimingInfo) {
|
19650 | self.postMessage({
|
19651 | action: 'videoTimingInfo',
|
19652 | videoTimingInfo: {
|
19653 | start: clock$2.videoTsToSeconds(videoTimingInfo.start),
|
19654 | end: clock$2.videoTsToSeconds(videoTimingInfo.end)
|
19655 | }
|
19656 | });
|
19657 | });
|
19658 | transmuxer.on('log', function (log) {
|
19659 | self.postMessage({
|
19660 | action: 'log',
|
19661 | log
|
19662 | });
|
19663 | });
|
19664 | };
|
19665 | |
19666 |
|
19667 |
|
19668 |
|
19669 |
|
19670 |
|
19671 |
|
19672 |
|
19673 |
|
19674 | class MessageHandlers {
|
19675 | constructor(self, options) {
|
19676 | this.options = options || {};
|
19677 | this.self = self;
|
19678 | this.init();
|
19679 | }
|
19680 | |
19681 |
|
19682 |
|
19683 |
|
19684 |
|
19685 | init() {
|
19686 | if (this.transmuxer) {
|
19687 | this.transmuxer.dispose();
|
19688 | }
|
19689 |
|
19690 | this.transmuxer = new transmuxer.Transmuxer(this.options);
|
19691 | wireTransmuxerEvents(this.self, this.transmuxer);
|
19692 | }
|
19693 |
|
19694 | pushMp4Captions(data) {
|
19695 | if (!this.captionParser) {
|
19696 | this.captionParser = new captionParser();
|
19697 | this.captionParser.init();
|
19698 | }
|
19699 |
|
19700 | const segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
|
19701 | const parsed = this.captionParser.parse(segment, data.trackIds, data.timescales);
|
19702 | this.self.postMessage({
|
19703 | action: 'mp4Captions',
|
19704 | captions: parsed && parsed.captions || [],
|
19705 | logs: parsed && parsed.logs || [],
|
19706 | data: segment.buffer
|
19707 | }, [segment.buffer]);
|
19708 | }
|
19709 |
|
19710 | probeMp4StartTime({
|
19711 | timescales,
|
19712 | data
|
19713 | }) {
|
19714 | const startTime = probe$2.startTime(timescales, data);
|
19715 | this.self.postMessage({
|
19716 | action: 'probeMp4StartTime',
|
19717 | startTime,
|
19718 | data
|
19719 | }, [data.buffer]);
|
19720 | }
|
19721 |
|
19722 | probeMp4Tracks({
|
19723 | data
|
19724 | }) {
|
19725 | const tracks = probe$2.tracks(data);
|
19726 | this.self.postMessage({
|
19727 | action: 'probeMp4Tracks',
|
19728 | tracks,
|
19729 | data
|
19730 | }, [data.buffer]);
|
19731 | }
|
19732 | |
19733 |
|
19734 |
|
19735 |
|
19736 |
|
19737 |
|
19738 |
|
19739 |
|
19740 |
|
19741 |
|
19742 | probeEmsgID3({
|
19743 | data,
|
19744 | offset
|
19745 | }) {
|
19746 | const id3Frames = probe$2.getEmsgID3(data, offset);
|
19747 | this.self.postMessage({
|
19748 | action: 'probeEmsgID3',
|
19749 | id3Frames,
|
19750 | emsgData: data
|
19751 | }, [data.buffer]);
|
19752 | }
|
19753 | |
19754 |
|
19755 |
|
19756 |
|
19757 |
|
19758 |
|
19759 |
|
19760 |
|
19761 |
|
19762 |
|
19763 |
|
19764 |
|
19765 |
|
19766 |
|
19767 |
|
19768 | probeTs({
|
19769 | data,
|
19770 | baseStartTime
|
19771 | }) {
|
19772 | const tsStartTime = typeof baseStartTime === 'number' && !isNaN(baseStartTime) ? baseStartTime * clock$2.ONE_SECOND_IN_TS : void 0;
|
19773 | const timeInfo = tsInspector.inspect(data, tsStartTime);
|
19774 | let result = null;
|
19775 |
|
19776 | if (timeInfo) {
|
19777 | result = {
|
19778 |
|
19779 | hasVideo: timeInfo.video && timeInfo.video.length === 2 || false,
|
19780 | hasAudio: timeInfo.audio && timeInfo.audio.length === 2 || false
|
19781 | };
|
19782 |
|
19783 | if (result.hasVideo) {
|
19784 | result.videoStart = timeInfo.video[0].ptsTime;
|
19785 | }
|
19786 |
|
19787 | if (result.hasAudio) {
|
19788 | result.audioStart = timeInfo.audio[0].ptsTime;
|
19789 | }
|
19790 | }
|
19791 |
|
19792 | this.self.postMessage({
|
19793 | action: 'probeTs',
|
19794 | result,
|
19795 | data
|
19796 | }, [data.buffer]);
|
19797 | }
|
19798 |
|
19799 | clearAllMp4Captions() {
|
19800 | if (this.captionParser) {
|
19801 | this.captionParser.clearAllCaptions();
|
19802 | }
|
19803 | }
|
19804 |
|
19805 | clearParsedMp4Captions() {
|
19806 | if (this.captionParser) {
|
19807 | this.captionParser.clearParsedCaptions();
|
19808 | }
|
19809 | }
|
19810 | |
19811 |
|
19812 |
|
19813 |
|
19814 |
|
19815 |
|
19816 |
|
19817 |
|
19818 | push(data) {
|
19819 |
|
19820 | const segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
|
19821 | this.transmuxer.push(segment);
|
19822 | }
|
19823 | |
19824 |
|
19825 |
|
19826 |
|
19827 |
|
19828 |
|
19829 | reset() {
|
19830 | this.transmuxer.reset();
|
19831 | }
|
19832 | |
19833 |
|
19834 |
|
19835 |
|
19836 |
|
19837 |
|
19838 |
|
19839 |
|
19840 |
|
19841 | setTimestampOffset(data) {
|
19842 | const timestampOffset = data.timestampOffset || 0;
|
19843 | this.transmuxer.setBaseMediaDecodeTime(Math.round(clock$2.secondsToVideoTs(timestampOffset)));
|
19844 | }
|
19845 |
|
19846 | setAudioAppendStart(data) {
|
19847 | this.transmuxer.setAudioAppendStart(Math.ceil(clock$2.secondsToVideoTs(data.appendStart)));
|
19848 | }
|
19849 |
|
19850 | setRemux(data) {
|
19851 | this.transmuxer.setRemux(data.remux);
|
19852 | }
|
19853 | |
19854 |
|
19855 |
|
19856 |
|
19857 |
|
19858 |
|
19859 |
|
19860 |
|
19861 | flush(data) {
|
19862 | this.transmuxer.flush();
|
19863 |
|
19864 | self.postMessage({
|
19865 | action: 'done',
|
19866 | type: 'transmuxed'
|
19867 | });
|
19868 | }
|
19869 |
|
19870 | endTimeline() {
|
19871 | this.transmuxer.endTimeline();
|
19872 |
|
19873 |
|
19874 | self.postMessage({
|
19875 | action: 'endedtimeline',
|
19876 | type: 'transmuxed'
|
19877 | });
|
19878 | }
|
19879 |
|
19880 | alignGopsWith(data) {
|
19881 | this.transmuxer.alignGopsWith(data.gopsToAlignWith.slice());
|
19882 | }
|
19883 |
|
19884 | }
|
19885 | |
19886 |
|
19887 |
|
19888 |
|
19889 |
|
19890 |
|
19891 |
|
19892 |
|
19893 |
|
19894 | self.onmessage = function (event) {
|
19895 | if (event.data.action === 'init' && event.data.options) {
|
19896 | this.messageHandlers = new MessageHandlers(self, event.data.options);
|
19897 | return;
|
19898 | }
|
19899 |
|
19900 | if (!this.messageHandlers) {
|
19901 | this.messageHandlers = new MessageHandlers(self);
|
19902 | }
|
19903 |
|
19904 | if (event.data && event.data.action && event.data.action !== 'init') {
|
19905 | if (this.messageHandlers[event.data.action]) {
|
19906 | this.messageHandlers[event.data.action](event.data);
|
19907 | }
|
19908 | }
|
19909 | };
|
19910 | }));
|
19911 | var TransmuxWorker = factory(workerCode$1);
|
19912 |
|
19913 |
|
19914 | const handleData_ = (event, transmuxedData, callback) => {
|
19915 | const {
|
19916 | type,
|
19917 | initSegment,
|
19918 | captions,
|
19919 | captionStreams,
|
19920 | metadata,
|
19921 | videoFrameDtsTime,
|
19922 | videoFramePtsTime
|
19923 | } = event.data.segment;
|
19924 | transmuxedData.buffer.push({
|
19925 | captions,
|
19926 | captionStreams,
|
19927 | metadata
|
19928 | });
|
19929 | const boxes = event.data.segment.boxes || {
|
19930 | data: event.data.segment.data
|
19931 | };
|
19932 | const result = {
|
19933 | type,
|
19934 |
|
19935 | data: new Uint8Array(boxes.data, boxes.data.byteOffset, boxes.data.byteLength),
|
19936 | initSegment: new Uint8Array(initSegment.data, initSegment.byteOffset, initSegment.byteLength)
|
19937 | };
|
19938 |
|
19939 | if (typeof videoFrameDtsTime !== 'undefined') {
|
19940 | result.videoFrameDtsTime = videoFrameDtsTime;
|
19941 | }
|
19942 |
|
19943 | if (typeof videoFramePtsTime !== 'undefined') {
|
19944 | result.videoFramePtsTime = videoFramePtsTime;
|
19945 | }
|
19946 |
|
19947 | callback(result);
|
19948 | };
|
19949 | const handleDone_ = ({
|
19950 | transmuxedData,
|
19951 | callback
|
19952 | }) => {
|
19953 |
|
19954 |
|
19955 | transmuxedData.buffer = [];
|
19956 |
|
19957 |
|
19958 | callback(transmuxedData);
|
19959 | };
|
19960 | const handleGopInfo_ = (event, transmuxedData) => {
|
19961 | transmuxedData.gopInfo = event.data.gopInfo;
|
19962 | };
|
19963 | const processTransmux = options => {
|
19964 | const {
|
19965 | transmuxer,
|
19966 | bytes,
|
19967 | audioAppendStart,
|
19968 | gopsToAlignWith,
|
19969 | remux,
|
19970 | onData,
|
19971 | onTrackInfo,
|
19972 | onAudioTimingInfo,
|
19973 | onVideoTimingInfo,
|
19974 | onVideoSegmentTimingInfo,
|
19975 | onAudioSegmentTimingInfo,
|
19976 | onId3,
|
19977 | onCaptions,
|
19978 | onDone,
|
19979 | onEndedTimeline,
|
19980 | onTransmuxerLog,
|
19981 | isEndOfTimeline
|
19982 | } = options;
|
19983 | const transmuxedData = {
|
19984 | buffer: []
|
19985 | };
|
19986 | let waitForEndedTimelineEvent = isEndOfTimeline;
|
19987 |
|
19988 | const handleMessage = event => {
|
19989 | if (transmuxer.currentTransmux !== options) {
|
19990 |
|
19991 | return;
|
19992 | }
|
19993 |
|
19994 | if (event.data.action === 'data') {
|
19995 | handleData_(event, transmuxedData, onData);
|
19996 | }
|
19997 |
|
19998 | if (event.data.action === 'trackinfo') {
|
19999 | onTrackInfo(event.data.trackInfo);
|
20000 | }
|
20001 |
|
20002 | if (event.data.action === 'gopInfo') {
|
20003 | handleGopInfo_(event, transmuxedData);
|
20004 | }
|
20005 |
|
20006 | if (event.data.action === 'audioTimingInfo') {
|
20007 | onAudioTimingInfo(event.data.audioTimingInfo);
|
20008 | }
|
20009 |
|
20010 | if (event.data.action === 'videoTimingInfo') {
|
20011 | onVideoTimingInfo(event.data.videoTimingInfo);
|
20012 | }
|
20013 |
|
20014 | if (event.data.action === 'videoSegmentTimingInfo') {
|
20015 | onVideoSegmentTimingInfo(event.data.videoSegmentTimingInfo);
|
20016 | }
|
20017 |
|
20018 | if (event.data.action === 'audioSegmentTimingInfo') {
|
20019 | onAudioSegmentTimingInfo(event.data.audioSegmentTimingInfo);
|
20020 | }
|
20021 |
|
20022 | if (event.data.action === 'id3Frame') {
|
20023 | onId3([event.data.id3Frame], event.data.id3Frame.dispatchType);
|
20024 | }
|
20025 |
|
20026 | if (event.data.action === 'caption') {
|
20027 | onCaptions(event.data.caption);
|
20028 | }
|
20029 |
|
20030 | if (event.data.action === 'endedtimeline') {
|
20031 | waitForEndedTimelineEvent = false;
|
20032 | onEndedTimeline();
|
20033 | }
|
20034 |
|
20035 | if (event.data.action === 'log') {
|
20036 | onTransmuxerLog(event.data.log);
|
20037 | }
|
20038 |
|
20039 |
|
20040 | if (event.data.type !== 'transmuxed') {
|
20041 | return;
|
20042 | }
|
20043 |
|
20044 |
|
20045 |
|
20046 |
|
20047 |
|
20048 | if (waitForEndedTimelineEvent) {
|
20049 | return;
|
20050 | }
|
20051 |
|
20052 | transmuxer.onmessage = null;
|
20053 | handleDone_({
|
20054 | transmuxedData,
|
20055 | callback: onDone
|
20056 | });
|
20057 |
|
20058 |
|
20059 | dequeue(transmuxer);
|
20060 |
|
20061 | };
|
20062 |
|
20063 | transmuxer.onmessage = handleMessage;
|
20064 |
|
20065 | if (audioAppendStart) {
|
20066 | transmuxer.postMessage({
|
20067 | action: 'setAudioAppendStart',
|
20068 | appendStart: audioAppendStart
|
20069 | });
|
20070 | }
|
20071 |
|
20072 |
|
20073 | if (Array.isArray(gopsToAlignWith)) {
|
20074 | transmuxer.postMessage({
|
20075 | action: 'alignGopsWith',
|
20076 | gopsToAlignWith
|
20077 | });
|
20078 | }
|
20079 |
|
20080 | if (typeof remux !== 'undefined') {
|
20081 | transmuxer.postMessage({
|
20082 | action: 'setRemux',
|
20083 | remux
|
20084 | });
|
20085 | }
|
20086 |
|
20087 | if (bytes.byteLength) {
|
20088 | const buffer = bytes instanceof ArrayBuffer ? bytes : bytes.buffer;
|
20089 | const byteOffset = bytes instanceof ArrayBuffer ? 0 : bytes.byteOffset;
|
20090 | transmuxer.postMessage({
|
20091 | action: 'push',
|
20092 |
|
20093 |
|
20094 |
|
20095 | data: buffer,
|
20096 |
|
20097 |
|
20098 | byteOffset,
|
20099 | byteLength: bytes.byteLength
|
20100 | }, [buffer]);
|
20101 | }
|
20102 |
|
20103 | if (isEndOfTimeline) {
|
20104 | transmuxer.postMessage({
|
20105 | action: 'endTimeline'
|
20106 | });
|
20107 | }
|
20108 |
|
20109 |
|
20110 |
|
20111 | transmuxer.postMessage({
|
20112 | action: 'flush'
|
20113 | });
|
20114 | };
|
20115 | const dequeue = transmuxer => {
|
20116 | transmuxer.currentTransmux = null;
|
20117 |
|
20118 | if (transmuxer.transmuxQueue.length) {
|
20119 | transmuxer.currentTransmux = transmuxer.transmuxQueue.shift();
|
20120 |
|
20121 | if (typeof transmuxer.currentTransmux === 'function') {
|
20122 | transmuxer.currentTransmux();
|
20123 | } else {
|
20124 | processTransmux(transmuxer.currentTransmux);
|
20125 | }
|
20126 | }
|
20127 | };
|
20128 | const processAction = (transmuxer, action) => {
|
20129 | transmuxer.postMessage({
|
20130 | action
|
20131 | });
|
20132 | dequeue(transmuxer);
|
20133 | };
|
20134 | const enqueueAction = (action, transmuxer) => {
|
20135 | if (!transmuxer.currentTransmux) {
|
20136 | transmuxer.currentTransmux = action;
|
20137 | processAction(transmuxer, action);
|
20138 | return;
|
20139 | }
|
20140 |
|
20141 | transmuxer.transmuxQueue.push(processAction.bind(null, transmuxer, action));
|
20142 | };
|
20143 | const reset = transmuxer => {
|
20144 | enqueueAction('reset', transmuxer);
|
20145 | };
|
20146 | const endTimeline = transmuxer => {
|
20147 | enqueueAction('endTimeline', transmuxer);
|
20148 | };
|
20149 | const transmux = options => {
|
20150 | if (!options.transmuxer.currentTransmux) {
|
20151 | options.transmuxer.currentTransmux = options;
|
20152 | processTransmux(options);
|
20153 | return;
|
20154 | }
|
20155 |
|
20156 | options.transmuxer.transmuxQueue.push(options);
|
20157 | };
|
20158 | const createTransmuxer = options => {
|
20159 | const transmuxer = new TransmuxWorker();
|
20160 | transmuxer.currentTransmux = null;
|
20161 | transmuxer.transmuxQueue = [];
|
20162 | const term = transmuxer.terminate;
|
20163 |
|
20164 | transmuxer.terminate = () => {
|
20165 | transmuxer.currentTransmux = null;
|
20166 | transmuxer.transmuxQueue.length = 0;
|
20167 | return term.call(transmuxer);
|
20168 | };
|
20169 |
|
20170 | transmuxer.postMessage({
|
20171 | action: 'init',
|
20172 | options
|
20173 | });
|
20174 | return transmuxer;
|
20175 | };
|
20176 | var segmentTransmuxer = {
|
20177 | reset,
|
20178 | endTimeline,
|
20179 | transmux,
|
20180 | createTransmuxer
|
20181 | };
|
20182 |
|
20183 | const workerCallback = function (options) {
|
20184 | const transmuxer = options.transmuxer;
|
20185 | const endAction = options.endAction || options.action;
|
20186 | const callback = options.callback;
|
20187 |
|
20188 | const message = _extends({}, options, {
|
20189 | endAction: null,
|
20190 | transmuxer: null,
|
20191 | callback: null
|
20192 | });
|
20193 |
|
20194 | const listenForEndEvent = event => {
|
20195 | if (event.data.action !== endAction) {
|
20196 | return;
|
20197 | }
|
20198 |
|
20199 | transmuxer.removeEventListener('message', listenForEndEvent);
|
20200 |
|
20201 | if (event.data.data) {
|
20202 | event.data.data = new Uint8Array(event.data.data, options.byteOffset || 0, options.byteLength || event.data.data.byteLength);
|
20203 |
|
20204 | if (options.data) {
|
20205 | options.data = event.data.data;
|
20206 | }
|
20207 | }
|
20208 |
|
20209 | callback(event.data);
|
20210 | };
|
20211 |
|
20212 | transmuxer.addEventListener('message', listenForEndEvent);
|
20213 |
|
20214 | if (options.data) {
|
20215 | const isArrayBuffer = options.data instanceof ArrayBuffer;
|
20216 | message.byteOffset = isArrayBuffer ? 0 : options.data.byteOffset;
|
20217 | message.byteLength = options.data.byteLength;
|
20218 | const transfers = [isArrayBuffer ? options.data : options.data.buffer];
|
20219 | transmuxer.postMessage(message, transfers);
|
20220 | } else {
|
20221 | transmuxer.postMessage(message);
|
20222 | }
|
20223 | };
|
20224 |
|
20225 | const REQUEST_ERRORS = {
|
20226 | FAILURE: 2,
|
20227 | TIMEOUT: -101,
|
20228 | ABORTED: -102
|
20229 | };
|
20230 | |
20231 |
|
20232 |
|
20233 |
|
20234 |
|
20235 |
|
20236 | const abortAll = activeXhrs => {
|
20237 | activeXhrs.forEach(xhr => {
|
20238 | xhr.abort();
|
20239 | });
|
20240 | };
|
20241 | |
20242 |
|
20243 |
|
20244 |
|
20245 |
|
20246 |
|
20247 |
|
20248 | const getRequestStats = request => {
|
20249 | return {
|
20250 | bandwidth: request.bandwidth,
|
20251 | bytesReceived: request.bytesReceived || 0,
|
20252 | roundTripTime: request.roundTripTime || 0
|
20253 | };
|
20254 | };
|
20255 | |
20256 |
|
20257 |
|
20258 |
|
20259 |
|
20260 |
|
20261 |
|
20262 |
|
20263 | const getProgressStats = progressEvent => {
|
20264 | const request = progressEvent.target;
|
20265 | const roundTripTime = Date.now() - request.requestTime;
|
20266 | const stats = {
|
20267 | bandwidth: Infinity,
|
20268 | bytesReceived: 0,
|
20269 | roundTripTime: roundTripTime || 0
|
20270 | };
|
20271 | stats.bytesReceived = progressEvent.loaded;
|
20272 |
|
20273 |
|
20274 |
|
20275 | stats.bandwidth = Math.floor(stats.bytesReceived / stats.roundTripTime * 8 * 1000);
|
20276 | return stats;
|
20277 | };
|
20278 | |
20279 |
|
20280 |
|
20281 |
|
20282 |
|
20283 |
|
20284 |
|
20285 |
|
20286 |
|
20287 | const handleErrors = (error, request) => {
|
20288 | if (request.timedout) {
|
20289 | return {
|
20290 | status: request.status,
|
20291 | message: 'HLS request timed-out at URL: ' + request.uri,
|
20292 | code: REQUEST_ERRORS.TIMEOUT,
|
20293 | xhr: request
|
20294 | };
|
20295 | }
|
20296 |
|
20297 | if (request.aborted) {
|
20298 | return {
|
20299 | status: request.status,
|
20300 | message: 'HLS request aborted at URL: ' + request.uri,
|
20301 | code: REQUEST_ERRORS.ABORTED,
|
20302 | xhr: request
|
20303 | };
|
20304 | }
|
20305 |
|
20306 | if (error) {
|
20307 | return {
|
20308 | status: request.status,
|
20309 | message: 'HLS request errored at URL: ' + request.uri,
|
20310 | code: REQUEST_ERRORS.FAILURE,
|
20311 | xhr: request
|
20312 | };
|
20313 | }
|
20314 |
|
20315 | if (request.responseType === 'arraybuffer' && request.response.byteLength === 0) {
|
20316 | return {
|
20317 | status: request.status,
|
20318 | message: 'Empty HLS response at URL: ' + request.uri,
|
20319 | code: REQUEST_ERRORS.FAILURE,
|
20320 | xhr: request
|
20321 | };
|
20322 | }
|
20323 |
|
20324 | return null;
|
20325 | };
|
20326 | |
20327 |
|
20328 |
|
20329 |
|
20330 |
|
20331 |
|
20332 |
|
20333 |
|
20334 |
|
20335 |
|
20336 |
|
20337 |
|
20338 | const handleKeyResponse = (segment, objects, finishProcessingFn) => (error, request) => {
|
20339 | const response = request.response;
|
20340 | const errorObj = handleErrors(error, request);
|
20341 |
|
20342 | if (errorObj) {
|
20343 | return finishProcessingFn(errorObj, segment);
|
20344 | }
|
20345 |
|
20346 | if (response.byteLength !== 16) {
|
20347 | return finishProcessingFn({
|
20348 | status: request.status,
|
20349 | message: 'Invalid HLS key at URL: ' + request.uri,
|
20350 | code: REQUEST_ERRORS.FAILURE,
|
20351 | xhr: request
|
20352 | }, segment);
|
20353 | }
|
20354 |
|
20355 | const view = new DataView(response);
|
20356 | const bytes = new Uint32Array([view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12)]);
|
20357 |
|
20358 | for (let i = 0; i < objects.length; i++) {
|
20359 | objects[i].bytes = bytes;
|
20360 | }
|
20361 |
|
20362 | return finishProcessingFn(null, segment);
|
20363 | };
|
20364 |
|
20365 | const parseInitSegment = (segment, callback) => {
|
20366 | const type = detectContainerForBytes(segment.map.bytes);
|
20367 |
|
20368 |
|
20369 | if (type !== 'mp4') {
|
20370 | const uri = segment.map.resolvedUri || segment.map.uri;
|
20371 | return callback({
|
20372 | internal: true,
|
20373 | message: `Found unsupported ${type || 'unknown'} container for initialization segment at URL: ${uri}`,
|
20374 | code: REQUEST_ERRORS.FAILURE
|
20375 | });
|
20376 | }
|
20377 |
|
20378 | workerCallback({
|
20379 | action: 'probeMp4Tracks',
|
20380 | data: segment.map.bytes,
|
20381 | transmuxer: segment.transmuxer,
|
20382 | callback: ({
|
20383 | tracks,
|
20384 | data
|
20385 | }) => {
|
20386 |
|
20387 | segment.map.bytes = data;
|
20388 | tracks.forEach(function (track) {
|
20389 | segment.map.tracks = segment.map.tracks || {};
|
20390 |
|
20391 | if (segment.map.tracks[track.type]) {
|
20392 | return;
|
20393 | }
|
20394 |
|
20395 | segment.map.tracks[track.type] = track;
|
20396 |
|
20397 | if (typeof track.id === 'number' && track.timescale) {
|
20398 | segment.map.timescales = segment.map.timescales || {};
|
20399 | segment.map.timescales[track.id] = track.timescale;
|
20400 | }
|
20401 | });
|
20402 | return callback(null);
|
20403 | }
|
20404 | });
|
20405 | };
|
20406 | |
20407 |
|
20408 |
|
20409 |
|
20410 |
|
20411 |
|
20412 |
|
20413 |
|
20414 |
|
20415 |
|
20416 | const handleInitSegmentResponse = ({
|
20417 | segment,
|
20418 | finishProcessingFn
|
20419 | }) => (error, request) => {
|
20420 | const errorObj = handleErrors(error, request);
|
20421 |
|
20422 | if (errorObj) {
|
20423 | return finishProcessingFn(errorObj, segment);
|
20424 | }
|
20425 |
|
20426 | const bytes = new Uint8Array(request.response);
|
20427 |
|
20428 |
|
20429 | if (segment.map.key) {
|
20430 | segment.map.encryptedBytes = bytes;
|
20431 | return finishProcessingFn(null, segment);
|
20432 | }
|
20433 |
|
20434 | segment.map.bytes = bytes;
|
20435 | parseInitSegment(segment, function (parseError) {
|
20436 | if (parseError) {
|
20437 | parseError.xhr = request;
|
20438 | parseError.status = request.status;
|
20439 | return finishProcessingFn(parseError, segment);
|
20440 | }
|
20441 |
|
20442 | finishProcessingFn(null, segment);
|
20443 | });
|
20444 | };
|
20445 | |
20446 |
|
20447 |
|
20448 |
|
20449 |
|
20450 |
|
20451 |
|
20452 |
|
20453 |
|
20454 |
|
20455 |
|
20456 |
|
20457 | const handleSegmentResponse = ({
|
20458 | segment,
|
20459 | finishProcessingFn,
|
20460 | responseType
|
20461 | }) => (error, request) => {
|
20462 | const errorObj = handleErrors(error, request);
|
20463 |
|
20464 | if (errorObj) {
|
20465 | return finishProcessingFn(errorObj, segment);
|
20466 | }
|
20467 |
|
20468 | const newBytes =
|
20469 |
|
20470 |
|
20471 |
|
20472 |
|
20473 | responseType === 'arraybuffer' || !request.responseText ? request.response : stringToArrayBuffer(request.responseText.substring(segment.lastReachedChar || 0));
|
20474 | segment.stats = getRequestStats(request);
|
20475 |
|
20476 | if (segment.key) {
|
20477 | segment.encryptedBytes = new Uint8Array(newBytes);
|
20478 | } else {
|
20479 | segment.bytes = new Uint8Array(newBytes);
|
20480 | }
|
20481 |
|
20482 | return finishProcessingFn(null, segment);
|
20483 | };
|
20484 |
|
20485 | const transmuxAndNotify = ({
|
20486 | segment,
|
20487 | bytes,
|
20488 | trackInfoFn,
|
20489 | timingInfoFn,
|
20490 | videoSegmentTimingInfoFn,
|
20491 | audioSegmentTimingInfoFn,
|
20492 | id3Fn,
|
20493 | captionsFn,
|
20494 | isEndOfTimeline,
|
20495 | endedTimelineFn,
|
20496 | dataFn,
|
20497 | doneFn,
|
20498 | onTransmuxerLog
|
20499 | }) => {
|
20500 | const fmp4Tracks = segment.map && segment.map.tracks || {};
|
20501 | const isMuxed = Boolean(fmp4Tracks.audio && fmp4Tracks.video);
|
20502 |
|
20503 |
|
20504 |
|
20505 | let audioStartFn = timingInfoFn.bind(null, segment, 'audio', 'start');
|
20506 | const audioEndFn = timingInfoFn.bind(null, segment, 'audio', 'end');
|
20507 | let videoStartFn = timingInfoFn.bind(null, segment, 'video', 'start');
|
20508 | const videoEndFn = timingInfoFn.bind(null, segment, 'video', 'end');
|
20509 |
|
20510 | const finish = () => transmux({
|
20511 | bytes,
|
20512 | transmuxer: segment.transmuxer,
|
20513 | audioAppendStart: segment.audioAppendStart,
|
20514 | gopsToAlignWith: segment.gopsToAlignWith,
|
20515 | remux: isMuxed,
|
20516 | onData: result => {
|
20517 | result.type = result.type === 'combined' ? 'video' : result.type;
|
20518 | dataFn(segment, result);
|
20519 | },
|
20520 | onTrackInfo: trackInfo => {
|
20521 | if (trackInfoFn) {
|
20522 | if (isMuxed) {
|
20523 | trackInfo.isMuxed = true;
|
20524 | }
|
20525 |
|
20526 | trackInfoFn(segment, trackInfo);
|
20527 | }
|
20528 | },
|
20529 | onAudioTimingInfo: audioTimingInfo => {
|
20530 |
|
20531 | if (audioStartFn && typeof audioTimingInfo.start !== 'undefined') {
|
20532 | audioStartFn(audioTimingInfo.start);
|
20533 | audioStartFn = null;
|
20534 | }
|
20535 |
|
20536 |
|
20537 | if (audioEndFn && typeof audioTimingInfo.end !== 'undefined') {
|
20538 | audioEndFn(audioTimingInfo.end);
|
20539 | }
|
20540 | },
|
20541 | onVideoTimingInfo: videoTimingInfo => {
|
20542 |
|
20543 | if (videoStartFn && typeof videoTimingInfo.start !== 'undefined') {
|
20544 | videoStartFn(videoTimingInfo.start);
|
20545 | videoStartFn = null;
|
20546 | }
|
20547 |
|
20548 |
|
20549 | if (videoEndFn && typeof videoTimingInfo.end !== 'undefined') {
|
20550 | videoEndFn(videoTimingInfo.end);
|
20551 | }
|
20552 | },
|
20553 | onVideoSegmentTimingInfo: videoSegmentTimingInfo => {
|
20554 | videoSegmentTimingInfoFn(videoSegmentTimingInfo);
|
20555 | },
|
20556 | onAudioSegmentTimingInfo: audioSegmentTimingInfo => {
|
20557 | audioSegmentTimingInfoFn(audioSegmentTimingInfo);
|
20558 | },
|
20559 | onId3: (id3Frames, dispatchType) => {
|
20560 | id3Fn(segment, id3Frames, dispatchType);
|
20561 | },
|
20562 | onCaptions: captions => {
|
20563 | captionsFn(segment, [captions]);
|
20564 | },
|
20565 | isEndOfTimeline,
|
20566 | onEndedTimeline: () => {
|
20567 | endedTimelineFn();
|
20568 | },
|
20569 | onTransmuxerLog,
|
20570 | onDone: result => {
|
20571 | if (!doneFn) {
|
20572 | return;
|
20573 | }
|
20574 |
|
20575 | result.type = result.type === 'combined' ? 'video' : result.type;
|
20576 | doneFn(null, segment, result);
|
20577 | }
|
20578 | });
|
20579 |
|
20580 |
|
20581 |
|
20582 |
|
20583 | workerCallback({
|
20584 | action: 'probeTs',
|
20585 | transmuxer: segment.transmuxer,
|
20586 | data: bytes,
|
20587 | baseStartTime: segment.baseStartTime,
|
20588 | callback: data => {
|
20589 | segment.bytes = bytes = data.data;
|
20590 | const probeResult = data.result;
|
20591 |
|
20592 | if (probeResult) {
|
20593 | trackInfoFn(segment, {
|
20594 | hasAudio: probeResult.hasAudio,
|
20595 | hasVideo: probeResult.hasVideo,
|
20596 | isMuxed
|
20597 | });
|
20598 | trackInfoFn = null;
|
20599 | }
|
20600 |
|
20601 | finish();
|
20602 | }
|
20603 | });
|
20604 | };
|
20605 |
|
20606 | const handleSegmentBytes = ({
|
20607 | segment,
|
20608 | bytes,
|
20609 | trackInfoFn,
|
20610 | timingInfoFn,
|
20611 | videoSegmentTimingInfoFn,
|
20612 | audioSegmentTimingInfoFn,
|
20613 | id3Fn,
|
20614 | captionsFn,
|
20615 | isEndOfTimeline,
|
20616 | endedTimelineFn,
|
20617 | dataFn,
|
20618 | doneFn,
|
20619 | onTransmuxerLog
|
20620 | }) => {
|
20621 | let bytesAsUint8Array = new Uint8Array(bytes);
|
20622 |
|
20623 |
|
20624 |
|
20625 |
|
20626 |
|
20627 | if (isLikelyFmp4MediaSegment(bytesAsUint8Array)) {
|
20628 | segment.isFmp4 = true;
|
20629 | const {
|
20630 | tracks
|
20631 | } = segment.map;
|
20632 | const trackInfo = {
|
20633 | isFmp4: true,
|
20634 | hasVideo: !!tracks.video,
|
20635 | hasAudio: !!tracks.audio
|
20636 | };
|
20637 |
|
20638 |
|
20639 | if (tracks.audio && tracks.audio.codec && tracks.audio.codec !== 'enca') {
|
20640 | trackInfo.audioCodec = tracks.audio.codec;
|
20641 | }
|
20642 |
|
20643 |
|
20644 |
|
20645 | if (tracks.video && tracks.video.codec && tracks.video.codec !== 'encv') {
|
20646 | trackInfo.videoCodec = tracks.video.codec;
|
20647 | }
|
20648 |
|
20649 | if (tracks.video && tracks.audio) {
|
20650 | trackInfo.isMuxed = true;
|
20651 | }
|
20652 |
|
20653 |
|
20654 |
|
20655 | trackInfoFn(segment, trackInfo);
|
20656 |
|
20657 |
|
20658 |
|
20659 |
|
20660 |
|
20661 |
|
20662 | const finishLoading = (captions, id3Frames) => {
|
20663 |
|
20664 |
|
20665 |
|
20666 |
|
20667 | dataFn(segment, {
|
20668 | data: bytesAsUint8Array,
|
20669 | type: trackInfo.hasAudio && !trackInfo.isMuxed ? 'audio' : 'video'
|
20670 | });
|
20671 |
|
20672 | if (id3Frames && id3Frames.length) {
|
20673 | id3Fn(segment, id3Frames);
|
20674 | }
|
20675 |
|
20676 | if (captions && captions.length) {
|
20677 | captionsFn(segment, captions);
|
20678 | }
|
20679 |
|
20680 | doneFn(null, segment, {});
|
20681 | };
|
20682 |
|
20683 | workerCallback({
|
20684 | action: 'probeMp4StartTime',
|
20685 | timescales: segment.map.timescales,
|
20686 | data: bytesAsUint8Array,
|
20687 | transmuxer: segment.transmuxer,
|
20688 | callback: ({
|
20689 | data,
|
20690 | startTime
|
20691 | }) => {
|
20692 |
|
20693 | bytes = data.buffer;
|
20694 | segment.bytes = bytesAsUint8Array = data;
|
20695 |
|
20696 | if (trackInfo.hasAudio && !trackInfo.isMuxed) {
|
20697 | timingInfoFn(segment, 'audio', 'start', startTime);
|
20698 | }
|
20699 |
|
20700 | if (trackInfo.hasVideo) {
|
20701 | timingInfoFn(segment, 'video', 'start', startTime);
|
20702 | }
|
20703 |
|
20704 | workerCallback({
|
20705 | action: 'probeEmsgID3',
|
20706 | data: bytesAsUint8Array,
|
20707 | transmuxer: segment.transmuxer,
|
20708 | offset: startTime,
|
20709 | callback: ({
|
20710 | emsgData,
|
20711 | id3Frames
|
20712 | }) => {
|
20713 |
|
20714 | bytes = emsgData.buffer;
|
20715 | segment.bytes = bytesAsUint8Array = emsgData;
|
20716 |
|
20717 |
|
20718 | if (!tracks.video || !emsgData.byteLength || !segment.transmuxer) {
|
20719 | finishLoading(undefined, id3Frames);
|
20720 | return;
|
20721 | }
|
20722 |
|
20723 | workerCallback({
|
20724 | action: 'pushMp4Captions',
|
20725 | endAction: 'mp4Captions',
|
20726 | transmuxer: segment.transmuxer,
|
20727 | data: bytesAsUint8Array,
|
20728 | timescales: segment.map.timescales,
|
20729 | trackIds: [tracks.video.id],
|
20730 | callback: message => {
|
20731 |
|
20732 | bytes = message.data.buffer;
|
20733 | segment.bytes = bytesAsUint8Array = message.data;
|
20734 | message.logs.forEach(function (log) {
|
20735 | onTransmuxerLog(merge$1(log, {
|
20736 | stream: 'mp4CaptionParser'
|
20737 | }));
|
20738 | });
|
20739 | finishLoading(message.captions, id3Frames);
|
20740 | }
|
20741 | });
|
20742 | }
|
20743 | });
|
20744 | }
|
20745 | });
|
20746 | return;
|
20747 | }
|
20748 |
|
20749 |
|
20750 | if (!segment.transmuxer) {
|
20751 | doneFn(null, segment, {});
|
20752 | return;
|
20753 | }
|
20754 |
|
20755 | if (typeof segment.container === 'undefined') {
|
20756 | segment.container = detectContainerForBytes(bytesAsUint8Array);
|
20757 | }
|
20758 |
|
20759 | if (segment.container !== 'ts' && segment.container !== 'aac') {
|
20760 | trackInfoFn(segment, {
|
20761 | hasAudio: false,
|
20762 | hasVideo: false
|
20763 | });
|
20764 | doneFn(null, segment, {});
|
20765 | return;
|
20766 | }
|
20767 |
|
20768 |
|
20769 | transmuxAndNotify({
|
20770 | segment,
|
20771 | bytes,
|
20772 | trackInfoFn,
|
20773 | timingInfoFn,
|
20774 | videoSegmentTimingInfoFn,
|
20775 | audioSegmentTimingInfoFn,
|
20776 | id3Fn,
|
20777 | captionsFn,
|
20778 | isEndOfTimeline,
|
20779 | endedTimelineFn,
|
20780 | dataFn,
|
20781 | doneFn,
|
20782 | onTransmuxerLog
|
20783 | });
|
20784 | };
|
20785 |
|
20786 | const decrypt = function ({
|
20787 | id,
|
20788 | key,
|
20789 | encryptedBytes,
|
20790 | decryptionWorker
|
20791 | }, callback) {
|
20792 | const decryptionHandler = event => {
|
20793 | if (event.data.source === id) {
|
20794 | decryptionWorker.removeEventListener('message', decryptionHandler);
|
20795 | const decrypted = event.data.decrypted;
|
20796 | callback(new Uint8Array(decrypted.bytes, decrypted.byteOffset, decrypted.byteLength));
|
20797 | }
|
20798 | };
|
20799 |
|
20800 | decryptionWorker.addEventListener('message', decryptionHandler);
|
20801 | let keyBytes;
|
20802 |
|
20803 | if (key.bytes.slice) {
|
20804 | keyBytes = key.bytes.slice();
|
20805 | } else {
|
20806 | keyBytes = new Uint32Array(Array.prototype.slice.call(key.bytes));
|
20807 | }
|
20808 |
|
20809 |
|
20810 | decryptionWorker.postMessage(createTransferableMessage({
|
20811 | source: id,
|
20812 | encrypted: encryptedBytes,
|
20813 | key: keyBytes,
|
20814 | iv: key.iv
|
20815 | }), [encryptedBytes.buffer, keyBytes.buffer]);
|
20816 | };
|
20817 | |
20818 |
|
20819 |
|
20820 |
|
20821 |
|
20822 |
|
20823 |
|
20824 |
|
20825 |
|
20826 |
|
20827 |
|
20828 |
|
20829 |
|
20830 |
|
20831 |
|
20832 |
|
20833 |
|
20834 |
|
20835 |
|
20836 |
|
20837 |
|
20838 |
|
20839 |
|
20840 |
|
20841 |
|
20842 |
|
20843 | const decryptSegment = ({
|
20844 | decryptionWorker,
|
20845 | segment,
|
20846 | trackInfoFn,
|
20847 | timingInfoFn,
|
20848 | videoSegmentTimingInfoFn,
|
20849 | audioSegmentTimingInfoFn,
|
20850 | id3Fn,
|
20851 | captionsFn,
|
20852 | isEndOfTimeline,
|
20853 | endedTimelineFn,
|
20854 | dataFn,
|
20855 | doneFn,
|
20856 | onTransmuxerLog
|
20857 | }) => {
|
20858 | decrypt({
|
20859 | id: segment.requestId,
|
20860 | key: segment.key,
|
20861 | encryptedBytes: segment.encryptedBytes,
|
20862 | decryptionWorker
|
20863 | }, decryptedBytes => {
|
20864 | segment.bytes = decryptedBytes;
|
20865 | handleSegmentBytes({
|
20866 | segment,
|
20867 | bytes: segment.bytes,
|
20868 | trackInfoFn,
|
20869 | timingInfoFn,
|
20870 | videoSegmentTimingInfoFn,
|
20871 | audioSegmentTimingInfoFn,
|
20872 | id3Fn,
|
20873 | captionsFn,
|
20874 | isEndOfTimeline,
|
20875 | endedTimelineFn,
|
20876 | dataFn,
|
20877 | doneFn,
|
20878 | onTransmuxerLog
|
20879 | });
|
20880 | });
|
20881 | };
|
20882 | |
20883 |
|
20884 |
|
20885 |
|
20886 |
|
20887 |
|
20888 |
|
20889 |
|
20890 |
|
20891 |
|
20892 |
|
20893 |
|
20894 |
|
20895 |
|
20896 |
|
20897 |
|
20898 |
|
20899 |
|
20900 |
|
20901 |
|
20902 |
|
20903 |
|
20904 |
|
20905 |
|
20906 |
|
20907 |
|
20908 |
|
20909 |
|
20910 |
|
20911 |
|
20912 |
|
20913 | const waitForCompletion = ({
|
20914 | activeXhrs,
|
20915 | decryptionWorker,
|
20916 | trackInfoFn,
|
20917 | timingInfoFn,
|
20918 | videoSegmentTimingInfoFn,
|
20919 | audioSegmentTimingInfoFn,
|
20920 | id3Fn,
|
20921 | captionsFn,
|
20922 | isEndOfTimeline,
|
20923 | endedTimelineFn,
|
20924 | dataFn,
|
20925 | doneFn,
|
20926 | onTransmuxerLog
|
20927 | }) => {
|
20928 | let count = 0;
|
20929 | let didError = false;
|
20930 | return (error, segment) => {
|
20931 | if (didError) {
|
20932 | return;
|
20933 | }
|
20934 |
|
20935 | if (error) {
|
20936 | didError = true;
|
20937 |
|
20938 | abortAll(activeXhrs);
|
20939 |
|
20940 |
|
20941 |
|
20942 |
|
20943 |
|
20944 |
|
20945 |
|
20946 |
|
20947 |
|
20948 |
|
20949 |
|
20950 | return doneFn(error, segment);
|
20951 | }
|
20952 |
|
20953 | count += 1;
|
20954 |
|
20955 | if (count === activeXhrs.length) {
|
20956 | const segmentFinish = function () {
|
20957 | if (segment.encryptedBytes) {
|
20958 | return decryptSegment({
|
20959 | decryptionWorker,
|
20960 | segment,
|
20961 | trackInfoFn,
|
20962 | timingInfoFn,
|
20963 | videoSegmentTimingInfoFn,
|
20964 | audioSegmentTimingInfoFn,
|
20965 | id3Fn,
|
20966 | captionsFn,
|
20967 | isEndOfTimeline,
|
20968 | endedTimelineFn,
|
20969 | dataFn,
|
20970 | doneFn,
|
20971 | onTransmuxerLog
|
20972 | });
|
20973 | }
|
20974 |
|
20975 |
|
20976 | handleSegmentBytes({
|
20977 | segment,
|
20978 | bytes: segment.bytes,
|
20979 | trackInfoFn,
|
20980 | timingInfoFn,
|
20981 | videoSegmentTimingInfoFn,
|
20982 | audioSegmentTimingInfoFn,
|
20983 | id3Fn,
|
20984 | captionsFn,
|
20985 | isEndOfTimeline,
|
20986 | endedTimelineFn,
|
20987 | dataFn,
|
20988 | doneFn,
|
20989 | onTransmuxerLog
|
20990 | });
|
20991 | };
|
20992 |
|
20993 |
|
20994 | segment.endOfAllRequests = Date.now();
|
20995 |
|
20996 | if (segment.map && segment.map.encryptedBytes && !segment.map.bytes) {
|
20997 | return decrypt({
|
20998 | decryptionWorker,
|
20999 |
|
21000 |
|
21001 |
|
21002 | id: segment.requestId + '-init',
|
21003 | encryptedBytes: segment.map.encryptedBytes,
|
21004 | key: segment.map.key
|
21005 | }, decryptedBytes => {
|
21006 | segment.map.bytes = decryptedBytes;
|
21007 | parseInitSegment(segment, parseError => {
|
21008 | if (parseError) {
|
21009 | abortAll(activeXhrs);
|
21010 | return doneFn(parseError, segment);
|
21011 | }
|
21012 |
|
21013 | segmentFinish();
|
21014 | });
|
21015 | });
|
21016 | }
|
21017 |
|
21018 | segmentFinish();
|
21019 | }
|
21020 | };
|
21021 | };
|
21022 | |
21023 |
|
21024 |
|
21025 |
|
21026 |
|
21027 |
|
21028 |
|
21029 |
|
21030 |
|
21031 | const handleLoadEnd = ({
|
21032 | loadendState,
|
21033 | abortFn
|
21034 | }) => event => {
|
21035 | const request = event.target;
|
21036 |
|
21037 | if (request.aborted && abortFn && !loadendState.calledAbortFn) {
|
21038 | abortFn();
|
21039 | loadendState.calledAbortFn = true;
|
21040 | }
|
21041 | };
|
21042 | |
21043 |
|
21044 |
|
21045 |
|
21046 |
|
21047 |
|
21048 |
|
21049 |
|
21050 |
|
21051 |
|
21052 |
|
21053 |
|
21054 |
|
21055 |
|
21056 |
|
21057 |
|
21058 |
|
21059 |
|
21060 |
|
21061 |
|
21062 |
|
21063 |
|
21064 |
|
21065 |
|
21066 |
|
21067 |
|
21068 |
|
21069 | const handleProgress = ({
|
21070 | segment,
|
21071 | progressFn,
|
21072 | trackInfoFn,
|
21073 | timingInfoFn,
|
21074 | videoSegmentTimingInfoFn,
|
21075 | audioSegmentTimingInfoFn,
|
21076 | id3Fn,
|
21077 | captionsFn,
|
21078 | isEndOfTimeline,
|
21079 | endedTimelineFn,
|
21080 | dataFn
|
21081 | }) => event => {
|
21082 | const request = event.target;
|
21083 |
|
21084 | if (request.aborted) {
|
21085 | return;
|
21086 | }
|
21087 |
|
21088 | segment.stats = merge$1(segment.stats, getProgressStats(event));
|
21089 |
|
21090 | if (!segment.stats.firstBytesReceivedAt && segment.stats.bytesReceived) {
|
21091 | segment.stats.firstBytesReceivedAt = Date.now();
|
21092 | }
|
21093 |
|
21094 | return progressFn(event, segment);
|
21095 | };
|
21096 | |
21097 |
|
21098 |
|
21099 |
|
21100 |
|
21101 |
|
21102 |
|
21103 |
|
21104 |
|
21105 |
|
21106 |
|
21107 |
|
21108 |
|
21109 |
|
21110 |
|
21111 |
|
21112 |
|
21113 |
|
21114 |
|
21115 |
|
21116 |
|
21117 |
|
21118 |
|
21119 |
|
21120 |
|
21121 |
|
21122 |
|
21123 |
|
21124 |
|
21125 |
|
21126 |
|
21127 |
|
21128 |
|
21129 |
|
21130 |
|
21131 |
|
21132 |
|
21133 |
|
21134 |
|
21135 |
|
21136 |
|
21137 |
|
21138 |
|
21139 |
|
21140 |
|
21141 |
|
21142 |
|
21143 |
|
21144 |
|
21145 |
|
21146 |
|
21147 |
|
21148 |
|
21149 |
|
21150 |
|
21151 |
|
21152 |
|
21153 |
|
21154 |
|
21155 |
|
21156 |
|
21157 |
|
21158 |
|
21159 |
|
21160 |
|
21161 |
|
21162 |
|
21163 |
|
21164 |
|
21165 |
|
21166 | const mediaSegmentRequest = ({
|
21167 | xhr,
|
21168 | xhrOptions,
|
21169 | decryptionWorker,
|
21170 | segment,
|
21171 | abortFn,
|
21172 | progressFn,
|
21173 | trackInfoFn,
|
21174 | timingInfoFn,
|
21175 | videoSegmentTimingInfoFn,
|
21176 | audioSegmentTimingInfoFn,
|
21177 | id3Fn,
|
21178 | captionsFn,
|
21179 | isEndOfTimeline,
|
21180 | endedTimelineFn,
|
21181 | dataFn,
|
21182 | doneFn,
|
21183 | onTransmuxerLog
|
21184 | }) => {
|
21185 | const activeXhrs = [];
|
21186 | const finishProcessingFn = waitForCompletion({
|
21187 | activeXhrs,
|
21188 | decryptionWorker,
|
21189 | trackInfoFn,
|
21190 | timingInfoFn,
|
21191 | videoSegmentTimingInfoFn,
|
21192 | audioSegmentTimingInfoFn,
|
21193 | id3Fn,
|
21194 | captionsFn,
|
21195 | isEndOfTimeline,
|
21196 | endedTimelineFn,
|
21197 | dataFn,
|
21198 | doneFn,
|
21199 | onTransmuxerLog
|
21200 | });
|
21201 |
|
21202 | if (segment.key && !segment.key.bytes) {
|
21203 | const objects = [segment.key];
|
21204 |
|
21205 | if (segment.map && !segment.map.bytes && segment.map.key && segment.map.key.resolvedUri === segment.key.resolvedUri) {
|
21206 | objects.push(segment.map.key);
|
21207 | }
|
21208 |
|
21209 | const keyRequestOptions = merge$1(xhrOptions, {
|
21210 | uri: segment.key.resolvedUri,
|
21211 | responseType: 'arraybuffer'
|
21212 | });
|
21213 | const keyRequestCallback = handleKeyResponse(segment, objects, finishProcessingFn);
|
21214 | const keyXhr = xhr(keyRequestOptions, keyRequestCallback);
|
21215 | activeXhrs.push(keyXhr);
|
21216 | }
|
21217 |
|
21218 |
|
21219 | if (segment.map && !segment.map.bytes) {
|
21220 | const differentMapKey = segment.map.key && (!segment.key || segment.key.resolvedUri !== segment.map.key.resolvedUri);
|
21221 |
|
21222 | if (differentMapKey) {
|
21223 | const mapKeyRequestOptions = merge$1(xhrOptions, {
|
21224 | uri: segment.map.key.resolvedUri,
|
21225 | responseType: 'arraybuffer'
|
21226 | });
|
21227 | const mapKeyRequestCallback = handleKeyResponse(segment, [segment.map.key], finishProcessingFn);
|
21228 | const mapKeyXhr = xhr(mapKeyRequestOptions, mapKeyRequestCallback);
|
21229 | activeXhrs.push(mapKeyXhr);
|
21230 | }
|
21231 |
|
21232 | const initSegmentOptions = merge$1(xhrOptions, {
|
21233 | uri: segment.map.resolvedUri,
|
21234 | responseType: 'arraybuffer',
|
21235 | headers: segmentXhrHeaders(segment.map)
|
21236 | });
|
21237 | const initSegmentRequestCallback = handleInitSegmentResponse({
|
21238 | segment,
|
21239 | finishProcessingFn
|
21240 | });
|
21241 | const initSegmentXhr = xhr(initSegmentOptions, initSegmentRequestCallback);
|
21242 | activeXhrs.push(initSegmentXhr);
|
21243 | }
|
21244 |
|
21245 | const segmentRequestOptions = merge$1(xhrOptions, {
|
21246 | uri: segment.part && segment.part.resolvedUri || segment.resolvedUri,
|
21247 | responseType: 'arraybuffer',
|
21248 | headers: segmentXhrHeaders(segment)
|
21249 | });
|
21250 | const segmentRequestCallback = handleSegmentResponse({
|
21251 | segment,
|
21252 | finishProcessingFn,
|
21253 | responseType: segmentRequestOptions.responseType
|
21254 | });
|
21255 | const segmentXhr = xhr(segmentRequestOptions, segmentRequestCallback);
|
21256 | segmentXhr.addEventListener('progress', handleProgress({
|
21257 | segment,
|
21258 | progressFn,
|
21259 | trackInfoFn,
|
21260 | timingInfoFn,
|
21261 | videoSegmentTimingInfoFn,
|
21262 | audioSegmentTimingInfoFn,
|
21263 | id3Fn,
|
21264 | captionsFn,
|
21265 | isEndOfTimeline,
|
21266 | endedTimelineFn,
|
21267 | dataFn
|
21268 | }));
|
21269 | activeXhrs.push(segmentXhr);
|
21270 |
|
21271 |
|
21272 | const loadendState = {};
|
21273 | activeXhrs.forEach(activeXhr => {
|
21274 | activeXhr.addEventListener('loadend', handleLoadEnd({
|
21275 | loadendState,
|
21276 | abortFn
|
21277 | }));
|
21278 | });
|
21279 | return () => abortAll(activeXhrs);
|
21280 | };
|
21281 |
|
21282 | |
21283 |
|
21284 |
|
21285 |
|
21286 | const logFn$1 = logger('CodecUtils');
|
21287 | |
21288 |
|
21289 |
|
21290 |
|
21291 |
|
21292 |
|
21293 |
|
21294 |
|
21295 | const getCodecs = function (media) {
|
21296 |
|
21297 |
|
21298 | const mediaAttributes = media.attributes || {};
|
21299 |
|
21300 | if (mediaAttributes.CODECS) {
|
21301 | return parseCodecs(mediaAttributes.CODECS);
|
21302 | }
|
21303 | };
|
21304 |
|
21305 | const isMaat = (main, media) => {
|
21306 | const mediaAttributes = media.attributes || {};
|
21307 | return main && main.mediaGroups && main.mediaGroups.AUDIO && mediaAttributes.AUDIO && main.mediaGroups.AUDIO[mediaAttributes.AUDIO];
|
21308 | };
|
21309 | const isMuxed = (main, media) => {
|
21310 | if (!isMaat(main, media)) {
|
21311 | return true;
|
21312 | }
|
21313 |
|
21314 | const mediaAttributes = media.attributes || {};
|
21315 | const audioGroup = main.mediaGroups.AUDIO[mediaAttributes.AUDIO];
|
21316 |
|
21317 | for (const groupId in audioGroup) {
|
21318 |
|
21319 |
|
21320 |
|
21321 |
|
21322 | if (!audioGroup[groupId].uri && !audioGroup[groupId].playlists) {
|
21323 | return true;
|
21324 | }
|
21325 | }
|
21326 |
|
21327 | return false;
|
21328 | };
|
21329 | const unwrapCodecList = function (codecList) {
|
21330 | const codecs = {};
|
21331 | codecList.forEach(({
|
21332 | mediaType,
|
21333 | type,
|
21334 | details
|
21335 | }) => {
|
21336 | codecs[mediaType] = codecs[mediaType] || [];
|
21337 | codecs[mediaType].push(translateLegacyCodec(`${type}${details}`));
|
21338 | });
|
21339 | Object.keys(codecs).forEach(function (mediaType) {
|
21340 | if (codecs[mediaType].length > 1) {
|
21341 | 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.`);
|
21342 | codecs[mediaType] = null;
|
21343 | return;
|
21344 | }
|
21345 |
|
21346 | codecs[mediaType] = codecs[mediaType][0];
|
21347 | });
|
21348 | return codecs;
|
21349 | };
|
21350 | const codecCount = function (codecObj) {
|
21351 | let count = 0;
|
21352 |
|
21353 | if (codecObj.audio) {
|
21354 | count++;
|
21355 | }
|
21356 |
|
21357 | if (codecObj.video) {
|
21358 | count++;
|
21359 | }
|
21360 |
|
21361 | return count;
|
21362 | };
|
21363 | |
21364 |
|
21365 |
|
21366 |
|
21367 |
|
21368 |
|
21369 |
|
21370 |
|
21371 |
|
21372 |
|
21373 |
|
21374 |
|
21375 |
|
21376 | const codecsForPlaylist = function (main, media) {
|
21377 | const mediaAttributes = media.attributes || {};
|
21378 | const codecInfo = unwrapCodecList(getCodecs(media) || []);
|
21379 |
|
21380 |
|
21381 | if (isMaat(main, media) && !codecInfo.audio) {
|
21382 | if (!isMuxed(main, media)) {
|
21383 |
|
21384 |
|
21385 |
|
21386 | const defaultCodecs = unwrapCodecList(codecsFromDefault(main, mediaAttributes.AUDIO) || []);
|
21387 |
|
21388 | if (defaultCodecs.audio) {
|
21389 | codecInfo.audio = defaultCodecs.audio;
|
21390 | }
|
21391 | }
|
21392 | }
|
21393 |
|
21394 | return codecInfo;
|
21395 | };
|
21396 |
|
21397 | const logFn = logger('PlaylistSelector');
|
21398 |
|
21399 | const representationToString = function (representation) {
|
21400 | if (!representation || !representation.playlist) {
|
21401 | return;
|
21402 | }
|
21403 |
|
21404 | const playlist = representation.playlist;
|
21405 | return JSON.stringify({
|
21406 | id: playlist.id,
|
21407 | bandwidth: representation.bandwidth,
|
21408 | width: representation.width,
|
21409 | height: representation.height,
|
21410 | codecs: playlist.attributes && playlist.attributes.CODECS || ''
|
21411 | });
|
21412 | };
|
21413 |
|
21414 | |
21415 |
|
21416 |
|
21417 |
|
21418 |
|
21419 |
|
21420 |
|
21421 |
|
21422 |
|
21423 |
|
21424 |
|
21425 |
|
21426 | const safeGetComputedStyle = function (el, property) {
|
21427 | if (!el) {
|
21428 | return '';
|
21429 | }
|
21430 |
|
21431 | const result = window.getComputedStyle(el);
|
21432 |
|
21433 | if (!result) {
|
21434 | return '';
|
21435 | }
|
21436 |
|
21437 | return result[property];
|
21438 | };
|
21439 | |
21440 |
|
21441 |
|
21442 |
|
21443 |
|
21444 |
|
21445 |
|
21446 |
|
21447 |
|
21448 | const stableSort = function (array, sortFn) {
|
21449 | const newArray = array.slice();
|
21450 | array.sort(function (left, right) {
|
21451 | const cmp = sortFn(left, right);
|
21452 |
|
21453 | if (cmp === 0) {
|
21454 | return newArray.indexOf(left) - newArray.indexOf(right);
|
21455 | }
|
21456 |
|
21457 | return cmp;
|
21458 | });
|
21459 | };
|
21460 | |
21461 |
|
21462 |
|
21463 |
|
21464 |
|
21465 |
|
21466 |
|
21467 |
|
21468 |
|
21469 |
|
21470 |
|
21471 |
|
21472 | const comparePlaylistBandwidth = function (left, right) {
|
21473 | let leftBandwidth;
|
21474 | let rightBandwidth;
|
21475 |
|
21476 | if (left.attributes.BANDWIDTH) {
|
21477 | leftBandwidth = left.attributes.BANDWIDTH;
|
21478 | }
|
21479 |
|
21480 | leftBandwidth = leftBandwidth || window.Number.MAX_VALUE;
|
21481 |
|
21482 | if (right.attributes.BANDWIDTH) {
|
21483 | rightBandwidth = right.attributes.BANDWIDTH;
|
21484 | }
|
21485 |
|
21486 | rightBandwidth = rightBandwidth || window.Number.MAX_VALUE;
|
21487 | return leftBandwidth - rightBandwidth;
|
21488 | };
|
21489 | |
21490 |
|
21491 |
|
21492 |
|
21493 |
|
21494 |
|
21495 |
|
21496 |
|
21497 |
|
21498 |
|
21499 |
|
21500 | const comparePlaylistResolution = function (left, right) {
|
21501 | let leftWidth;
|
21502 | let rightWidth;
|
21503 |
|
21504 | if (left.attributes.RESOLUTION && left.attributes.RESOLUTION.width) {
|
21505 | leftWidth = left.attributes.RESOLUTION.width;
|
21506 | }
|
21507 |
|
21508 | leftWidth = leftWidth || window.Number.MAX_VALUE;
|
21509 |
|
21510 | if (right.attributes.RESOLUTION && right.attributes.RESOLUTION.width) {
|
21511 | rightWidth = right.attributes.RESOLUTION.width;
|
21512 | }
|
21513 |
|
21514 | rightWidth = rightWidth || window.Number.MAX_VALUE;
|
21515 |
|
21516 |
|
21517 | if (leftWidth === rightWidth && left.attributes.BANDWIDTH && right.attributes.BANDWIDTH) {
|
21518 | return left.attributes.BANDWIDTH - right.attributes.BANDWIDTH;
|
21519 | }
|
21520 |
|
21521 | return leftWidth - rightWidth;
|
21522 | };
|
21523 | |
21524 |
|
21525 |
|
21526 |
|
21527 |
|
21528 |
|
21529 |
|
21530 |
|
21531 |
|
21532 |
|
21533 |
|
21534 |
|
21535 |
|
21536 |
|
21537 |
|
21538 |
|
21539 |
|
21540 |
|
21541 |
|
21542 |
|
21543 | let simpleSelector = function (main, playerBandwidth, playerWidth, playerHeight, limitRenditionByPlayerDimensions, playlistController) {
|
21544 |
|
21545 | if (!main) {
|
21546 | return;
|
21547 | }
|
21548 |
|
21549 | const options = {
|
21550 | bandwidth: playerBandwidth,
|
21551 | width: playerWidth,
|
21552 | height: playerHeight,
|
21553 | limitRenditionByPlayerDimensions
|
21554 | };
|
21555 | let playlists = main.playlists;
|
21556 |
|
21557 | if (Playlist.isAudioOnly(main)) {
|
21558 | playlists = playlistController.getAudioTrackPlaylists_();
|
21559 |
|
21560 |
|
21561 | options.audioOnly = true;
|
21562 | }
|
21563 |
|
21564 |
|
21565 | let sortedPlaylistReps = playlists.map(playlist => {
|
21566 | let bandwidth;
|
21567 | const width = playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.width;
|
21568 | const height = playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height;
|
21569 | bandwidth = playlist.attributes && playlist.attributes.BANDWIDTH;
|
21570 | bandwidth = bandwidth || window.Number.MAX_VALUE;
|
21571 | return {
|
21572 | bandwidth,
|
21573 | width,
|
21574 | height,
|
21575 | playlist
|
21576 | };
|
21577 | });
|
21578 | stableSort(sortedPlaylistReps, (left, right) => left.bandwidth - right.bandwidth);
|
21579 |
|
21580 |
|
21581 | sortedPlaylistReps = sortedPlaylistReps.filter(rep => !Playlist.isIncompatible(rep.playlist));
|
21582 |
|
21583 |
|
21584 | let enabledPlaylistReps = sortedPlaylistReps.filter(rep => Playlist.isEnabled(rep.playlist));
|
21585 |
|
21586 | if (!enabledPlaylistReps.length) {
|
21587 |
|
21588 |
|
21589 |
|
21590 | enabledPlaylistReps = sortedPlaylistReps.filter(rep => !Playlist.isDisabled(rep.playlist));
|
21591 | }
|
21592 |
|
21593 |
|
21594 |
|
21595 | const bandwidthPlaylistReps = enabledPlaylistReps.filter(rep => rep.bandwidth * Config.BANDWIDTH_VARIANCE < playerBandwidth);
|
21596 | let highestRemainingBandwidthRep = bandwidthPlaylistReps[bandwidthPlaylistReps.length - 1];
|
21597 |
|
21598 |
|
21599 | const bandwidthBestRep = bandwidthPlaylistReps.filter(rep => rep.bandwidth === highestRemainingBandwidthRep.bandwidth)[0];
|
21600 |
|
21601 | if (limitRenditionByPlayerDimensions === false) {
|
21602 | const chosenRep = bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
|
21603 |
|
21604 | if (chosenRep && chosenRep.playlist) {
|
21605 | let type = 'sortedPlaylistReps';
|
21606 |
|
21607 | if (bandwidthBestRep) {
|
21608 | type = 'bandwidthBestRep';
|
21609 | }
|
21610 |
|
21611 | if (enabledPlaylistReps[0]) {
|
21612 | type = 'enabledPlaylistReps';
|
21613 | }
|
21614 |
|
21615 | logFn(`choosing ${representationToString(chosenRep)} using ${type} with options`, options);
|
21616 | return chosenRep.playlist;
|
21617 | }
|
21618 |
|
21619 | logFn('could not choose a playlist with options', options);
|
21620 | return null;
|
21621 | }
|
21622 |
|
21623 |
|
21624 | const haveResolution = bandwidthPlaylistReps.filter(rep => rep.width && rep.height);
|
21625 |
|
21626 | stableSort(haveResolution, (left, right) => left.width - right.width);
|
21627 |
|
21628 | const resolutionBestRepList = haveResolution.filter(rep => rep.width === playerWidth && rep.height === playerHeight);
|
21629 | highestRemainingBandwidthRep = resolutionBestRepList[resolutionBestRepList.length - 1];
|
21630 |
|
21631 | const resolutionBestRep = resolutionBestRepList.filter(rep => rep.bandwidth === highestRemainingBandwidthRep.bandwidth)[0];
|
21632 | let resolutionPlusOneList;
|
21633 | let resolutionPlusOneSmallest;
|
21634 | let resolutionPlusOneRep;
|
21635 |
|
21636 |
|
21637 | if (!resolutionBestRep) {
|
21638 | resolutionPlusOneList = haveResolution.filter(rep => rep.width > playerWidth || rep.height > playerHeight);
|
21639 |
|
21640 | resolutionPlusOneSmallest = resolutionPlusOneList.filter(rep => rep.width === resolutionPlusOneList[0].width && rep.height === resolutionPlusOneList[0].height);
|
21641 |
|
21642 |
|
21643 | highestRemainingBandwidthRep = resolutionPlusOneSmallest[resolutionPlusOneSmallest.length - 1];
|
21644 | resolutionPlusOneRep = resolutionPlusOneSmallest.filter(rep => rep.bandwidth === highestRemainingBandwidthRep.bandwidth)[0];
|
21645 | }
|
21646 |
|
21647 | let leastPixelDiffRep;
|
21648 |
|
21649 |
|
21650 |
|
21651 | if (playlistController.leastPixelDiffSelector) {
|
21652 |
|
21653 | const leastPixelDiffList = haveResolution.map(rep => {
|
21654 | rep.pixelDiff = Math.abs(rep.width - playerWidth) + Math.abs(rep.height - playerHeight);
|
21655 | return rep;
|
21656 | });
|
21657 |
|
21658 | stableSort(leastPixelDiffList, (left, right) => {
|
21659 |
|
21660 | if (left.pixelDiff === right.pixelDiff) {
|
21661 | return right.bandwidth - left.bandwidth;
|
21662 | }
|
21663 |
|
21664 | return left.pixelDiff - right.pixelDiff;
|
21665 | });
|
21666 | leastPixelDiffRep = leastPixelDiffList[0];
|
21667 | }
|
21668 |
|
21669 |
|
21670 | const chosenRep = leastPixelDiffRep || resolutionPlusOneRep || resolutionBestRep || bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
|
21671 |
|
21672 | if (chosenRep && chosenRep.playlist) {
|
21673 | let type = 'sortedPlaylistReps';
|
21674 |
|
21675 | if (leastPixelDiffRep) {
|
21676 | type = 'leastPixelDiffRep';
|
21677 | } else if (resolutionPlusOneRep) {
|
21678 | type = 'resolutionPlusOneRep';
|
21679 | } else if (resolutionBestRep) {
|
21680 | type = 'resolutionBestRep';
|
21681 | } else if (bandwidthBestRep) {
|
21682 | type = 'bandwidthBestRep';
|
21683 | } else if (enabledPlaylistReps[0]) {
|
21684 | type = 'enabledPlaylistReps';
|
21685 | }
|
21686 |
|
21687 | logFn(`choosing ${representationToString(chosenRep)} using ${type} with options`, options);
|
21688 | return chosenRep.playlist;
|
21689 | }
|
21690 |
|
21691 | logFn('could not choose a playlist with options', options);
|
21692 | return null;
|
21693 | };
|
21694 |
|
21695 | |
21696 |
|
21697 |
|
21698 |
|
21699 |
|
21700 |
|
21701 |
|
21702 |
|
21703 |
|
21704 |
|
21705 |
|
21706 | const lastBandwidthSelector = function () {
|
21707 | const pixelRatio = this.useDevicePixelRatio ? window.devicePixelRatio || 1 : 1;
|
21708 | return simpleSelector(this.playlists.main, this.systemBandwidth, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions, this.playlistController_);
|
21709 | };
|
21710 | |
21711 |
|
21712 |
|
21713 |
|
21714 |
|
21715 |
|
21716 |
|
21717 |
|
21718 |
|
21719 |
|
21720 |
|
21721 |
|
21722 |
|
21723 |
|
21724 |
|
21725 | const movingAverageBandwidthSelector = function (decay) {
|
21726 | let average = -1;
|
21727 | let lastSystemBandwidth = -1;
|
21728 |
|
21729 | if (decay < 0 || decay > 1) {
|
21730 | throw new Error('Moving average bandwidth decay must be between 0 and 1.');
|
21731 | }
|
21732 |
|
21733 | return function () {
|
21734 | const pixelRatio = this.useDevicePixelRatio ? window.devicePixelRatio || 1 : 1;
|
21735 |
|
21736 | if (average < 0) {
|
21737 | average = this.systemBandwidth;
|
21738 | lastSystemBandwidth = this.systemBandwidth;
|
21739 | }
|
21740 |
|
21741 |
|
21742 |
|
21743 |
|
21744 |
|
21745 |
|
21746 | if (this.systemBandwidth > 0 && this.systemBandwidth !== lastSystemBandwidth) {
|
21747 | average = decay * this.systemBandwidth + (1 - decay) * average;
|
21748 | lastSystemBandwidth = this.systemBandwidth;
|
21749 | }
|
21750 |
|
21751 | return simpleSelector(this.playlists.main, average, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions, this.playlistController_);
|
21752 | };
|
21753 | };
|
21754 | |
21755 |
|
21756 |
|
21757 |
|
21758 |
|
21759 |
|
21760 |
|
21761 |
|
21762 |
|
21763 |
|
21764 |
|
21765 |
|
21766 |
|
21767 |
|
21768 |
|
21769 |
|
21770 |
|
21771 |
|
21772 |
|
21773 |
|
21774 |
|
21775 |
|
21776 |
|
21777 |
|
21778 |
|
21779 |
|
21780 |
|
21781 |
|
21782 |
|
21783 | const minRebufferMaxBandwidthSelector = function (settings) {
|
21784 | const {
|
21785 | main,
|
21786 | currentTime,
|
21787 | bandwidth,
|
21788 | duration,
|
21789 | segmentDuration,
|
21790 | timeUntilRebuffer,
|
21791 | currentTimeline,
|
21792 | syncController
|
21793 | } = settings;
|
21794 |
|
21795 |
|
21796 | const compatiblePlaylists = main.playlists.filter(playlist => !Playlist.isIncompatible(playlist));
|
21797 |
|
21798 |
|
21799 | let enabledPlaylists = compatiblePlaylists.filter(Playlist.isEnabled);
|
21800 |
|
21801 | if (!enabledPlaylists.length) {
|
21802 |
|
21803 |
|
21804 |
|
21805 | enabledPlaylists = compatiblePlaylists.filter(playlist => !Playlist.isDisabled(playlist));
|
21806 | }
|
21807 |
|
21808 | const bandwidthPlaylists = enabledPlaylists.filter(Playlist.hasAttribute.bind(null, 'BANDWIDTH'));
|
21809 | const rebufferingEstimates = bandwidthPlaylists.map(playlist => {
|
21810 | const syncPoint = syncController.getSyncPoint(playlist, duration, currentTimeline, currentTime);
|
21811 |
|
21812 |
|
21813 | const numRequests = syncPoint ? 1 : 2;
|
21814 | const requestTimeEstimate = Playlist.estimateSegmentRequestTime(segmentDuration, bandwidth, playlist);
|
21815 | const rebufferingImpact = requestTimeEstimate * numRequests - timeUntilRebuffer;
|
21816 | return {
|
21817 | playlist,
|
21818 | rebufferingImpact
|
21819 | };
|
21820 | });
|
21821 | const noRebufferingPlaylists = rebufferingEstimates.filter(estimate => estimate.rebufferingImpact <= 0);
|
21822 |
|
21823 | stableSort(noRebufferingPlaylists, (a, b) => comparePlaylistBandwidth(b.playlist, a.playlist));
|
21824 |
|
21825 | if (noRebufferingPlaylists.length) {
|
21826 | return noRebufferingPlaylists[0];
|
21827 | }
|
21828 |
|
21829 | stableSort(rebufferingEstimates, (a, b) => a.rebufferingImpact - b.rebufferingImpact);
|
21830 | return rebufferingEstimates[0] || null;
|
21831 | };
|
21832 | |
21833 |
|
21834 |
|
21835 |
|
21836 |
|
21837 |
|
21838 |
|
21839 |
|
21840 |
|
21841 |
|
21842 |
|
21843 |
|
21844 | const lowestBitrateCompatibleVariantSelector = function () {
|
21845 |
|
21846 |
|
21847 | const playlists = this.playlists.main.playlists.filter(Playlist.isEnabled);
|
21848 |
|
21849 | stableSort(playlists, (a, b) => comparePlaylistBandwidth(a, b));
|
21850 |
|
21851 |
|
21852 |
|
21853 |
|
21854 |
|
21855 | const playlistsWithVideo = playlists.filter(playlist => !!codecsForPlaylist(this.playlists.main, playlist).video);
|
21856 | return playlistsWithVideo[0] || null;
|
21857 | };
|
21858 |
|
21859 | |
21860 |
|
21861 |
|
21862 |
|
21863 |
|
21864 |
|
21865 |
|
21866 | const concatSegments = segmentObj => {
|
21867 | let offset = 0;
|
21868 | let tempBuffer;
|
21869 |
|
21870 | if (segmentObj.bytes) {
|
21871 | tempBuffer = new Uint8Array(segmentObj.bytes);
|
21872 |
|
21873 | segmentObj.segments.forEach(segment => {
|
21874 | tempBuffer.set(segment, offset);
|
21875 | offset += segment.byteLength;
|
21876 | });
|
21877 | }
|
21878 |
|
21879 | return tempBuffer;
|
21880 | };
|
21881 |
|
21882 | |
21883 |
|
21884 |
|
21885 | |
21886 |
|
21887 |
|
21888 |
|
21889 |
|
21890 |
|
21891 |
|
21892 |
|
21893 |
|
21894 | const createCaptionsTrackIfNotExists = function (inbandTextTracks, tech, captionStream) {
|
21895 | if (!inbandTextTracks[captionStream]) {
|
21896 | tech.trigger({
|
21897 | type: 'usage',
|
21898 | name: 'vhs-608'
|
21899 | });
|
21900 | let instreamId = captionStream;
|
21901 |
|
21902 | if (/^cc708_/.test(captionStream)) {
|
21903 | instreamId = 'SERVICE' + captionStream.split('_')[1];
|
21904 | }
|
21905 |
|
21906 | const track = tech.textTracks().getTrackById(instreamId);
|
21907 |
|
21908 | if (track) {
|
21909 |
|
21910 |
|
21911 |
|
21912 | inbandTextTracks[captionStream] = track;
|
21913 | } else {
|
21914 |
|
21915 |
|
21916 | const captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
|
21917 | let label = captionStream;
|
21918 | let language = captionStream;
|
21919 | let def = false;
|
21920 | const captionService = captionServices[instreamId];
|
21921 |
|
21922 | if (captionService) {
|
21923 | label = captionService.label;
|
21924 | language = captionService.language;
|
21925 | def = captionService.default;
|
21926 | }
|
21927 |
|
21928 |
|
21929 |
|
21930 | inbandTextTracks[captionStream] = tech.addRemoteTextTrack({
|
21931 | kind: 'captions',
|
21932 | id: instreamId,
|
21933 |
|
21934 | default: def,
|
21935 | label,
|
21936 | language
|
21937 | }, false).track;
|
21938 | }
|
21939 | }
|
21940 | };
|
21941 | |
21942 |
|
21943 |
|
21944 |
|
21945 |
|
21946 |
|
21947 |
|
21948 |
|
21949 |
|
21950 |
|
21951 | const addCaptionData = function ({
|
21952 | inbandTextTracks,
|
21953 | captionArray,
|
21954 | timestampOffset
|
21955 | }) {
|
21956 | if (!captionArray) {
|
21957 | return;
|
21958 | }
|
21959 |
|
21960 | const Cue = window.WebKitDataCue || window.VTTCue;
|
21961 | captionArray.forEach(caption => {
|
21962 | const track = caption.stream;
|
21963 |
|
21964 |
|
21965 | if (caption.content) {
|
21966 | caption.content.forEach(value => {
|
21967 | const cue = new Cue(caption.startTime + timestampOffset, caption.endTime + timestampOffset, value.text);
|
21968 | cue.line = value.line;
|
21969 | cue.align = 'left';
|
21970 | cue.position = value.position;
|
21971 | cue.positionAlign = 'line-left';
|
21972 | inbandTextTracks[track].addCue(cue);
|
21973 | });
|
21974 | } else {
|
21975 |
|
21976 | inbandTextTracks[track].addCue(new Cue(caption.startTime + timestampOffset, caption.endTime + timestampOffset, caption.text));
|
21977 | }
|
21978 | });
|
21979 | };
|
21980 | |
21981 |
|
21982 |
|
21983 |
|
21984 |
|
21985 |
|
21986 |
|
21987 |
|
21988 |
|
21989 | const deprecateOldCue = function (cue) {
|
21990 | Object.defineProperties(cue.frame, {
|
21991 | id: {
|
21992 | get() {
|
21993 | videojs__default["default"].log.warn('cue.frame.id is deprecated. Use cue.value.key instead.');
|
21994 | return cue.value.key;
|
21995 | }
|
21996 |
|
21997 | },
|
21998 | value: {
|
21999 | get() {
|
22000 | videojs__default["default"].log.warn('cue.frame.value is deprecated. Use cue.value.data instead.');
|
22001 | return cue.value.data;
|
22002 | }
|
22003 |
|
22004 | },
|
22005 | privateData: {
|
22006 | get() {
|
22007 | videojs__default["default"].log.warn('cue.frame.privateData is deprecated. Use cue.value.data instead.');
|
22008 | return cue.value.data;
|
22009 | }
|
22010 |
|
22011 | }
|
22012 | });
|
22013 | };
|
22014 | |
22015 |
|
22016 |
|
22017 |
|
22018 |
|
22019 |
|
22020 |
|
22021 |
|
22022 |
|
22023 |
|
22024 |
|
22025 |
|
22026 | const addMetadata = ({
|
22027 | inbandTextTracks,
|
22028 | metadataArray,
|
22029 | timestampOffset,
|
22030 | videoDuration
|
22031 | }) => {
|
22032 | if (!metadataArray) {
|
22033 | return;
|
22034 | }
|
22035 |
|
22036 | const Cue = window.WebKitDataCue || window.VTTCue;
|
22037 | const metadataTrack = inbandTextTracks.metadataTrack_;
|
22038 |
|
22039 | if (!metadataTrack) {
|
22040 | return;
|
22041 | }
|
22042 |
|
22043 | metadataArray.forEach(metadata => {
|
22044 | const time = metadata.cueTime + timestampOffset;
|
22045 |
|
22046 |
|
22047 |
|
22048 |
|
22049 | if (typeof time !== 'number' || window.isNaN(time) || time < 0 || !(time < Infinity)) {
|
22050 | return;
|
22051 | }
|
22052 |
|
22053 |
|
22054 | if (!metadata.frames || !metadata.frames.length) {
|
22055 | return;
|
22056 | }
|
22057 |
|
22058 | metadata.frames.forEach(frame => {
|
22059 | const cue = new Cue(time, time, frame.value || frame.url || frame.data || '');
|
22060 | cue.frame = frame;
|
22061 | cue.value = frame;
|
22062 | deprecateOldCue(cue);
|
22063 | metadataTrack.addCue(cue);
|
22064 | });
|
22065 | });
|
22066 |
|
22067 | if (!metadataTrack.cues || !metadataTrack.cues.length) {
|
22068 | return;
|
22069 | }
|
22070 |
|
22071 |
|
22072 |
|
22073 |
|
22074 | const cues = metadataTrack.cues;
|
22075 | const cuesArray = [];
|
22076 |
|
22077 |
|
22078 | for (let i = 0; i < cues.length; i++) {
|
22079 | if (cues[i]) {
|
22080 | cuesArray.push(cues[i]);
|
22081 | }
|
22082 | }
|
22083 |
|
22084 |
|
22085 | const cuesGroupedByStartTime = cuesArray.reduce((obj, cue) => {
|
22086 | const timeSlot = obj[cue.startTime] || [];
|
22087 | timeSlot.push(cue);
|
22088 | obj[cue.startTime] = timeSlot;
|
22089 | return obj;
|
22090 | }, {});
|
22091 |
|
22092 | const sortedStartTimes = Object.keys(cuesGroupedByStartTime).sort((a, b) => Number(a) - Number(b));
|
22093 |
|
22094 | sortedStartTimes.forEach((startTime, idx) => {
|
22095 | const cueGroup = cuesGroupedByStartTime[startTime];
|
22096 | const finiteDuration = isFinite(videoDuration) ? videoDuration : startTime;
|
22097 | const nextTime = Number(sortedStartTimes[idx + 1]) || finiteDuration;
|
22098 |
|
22099 | cueGroup.forEach(cue => {
|
22100 | cue.endTime = nextTime;
|
22101 | });
|
22102 | });
|
22103 | };
|
22104 |
|
22105 | const dateRangeAttr = {
|
22106 | id: 'ID',
|
22107 | class: 'CLASS',
|
22108 | startDate: 'START-DATE',
|
22109 | duration: 'DURATION',
|
22110 | endDate: 'END-DATE',
|
22111 | endOnNext: 'END-ON-NEXT',
|
22112 | plannedDuration: 'PLANNED-DURATION',
|
22113 | scte35Out: 'SCTE35-OUT',
|
22114 | scte35In: 'SCTE35-IN'
|
22115 | };
|
22116 | const dateRangeKeysToOmit = new Set(['id', 'class', 'startDate', 'duration', 'endDate', 'endOnNext', 'startTime', 'endTime', 'processDateRange']);
|
22117 | |
22118 |
|
22119 |
|
22120 |
|
22121 |
|
22122 |
|
22123 |
|
22124 |
|
22125 |
|
22126 | const addDateRangeMetadata = ({
|
22127 | inbandTextTracks,
|
22128 | dateRanges
|
22129 | }) => {
|
22130 | const metadataTrack = inbandTextTracks.metadataTrack_;
|
22131 |
|
22132 | if (!metadataTrack) {
|
22133 | return;
|
22134 | }
|
22135 |
|
22136 | const Cue = window.WebKitDataCue || window.VTTCue;
|
22137 | dateRanges.forEach(dateRange => {
|
22138 |
|
22139 | for (const key of Object.keys(dateRange)) {
|
22140 | if (dateRangeKeysToOmit.has(key)) {
|
22141 | continue;
|
22142 | }
|
22143 |
|
22144 | const cue = new Cue(dateRange.startTime, dateRange.endTime, '');
|
22145 | cue.id = dateRange.id;
|
22146 | cue.type = 'com.apple.quicktime.HLS';
|
22147 | cue.value = {
|
22148 | key: dateRangeAttr[key],
|
22149 | data: dateRange[key]
|
22150 | };
|
22151 |
|
22152 | if (key === 'scte35Out' || key === 'scte35In') {
|
22153 | cue.value.data = new Uint8Array(cue.value.data.match(/[\da-f]{2}/gi)).buffer;
|
22154 | }
|
22155 |
|
22156 | metadataTrack.addCue(cue);
|
22157 | }
|
22158 |
|
22159 | dateRange.processDateRange();
|
22160 | });
|
22161 | };
|
22162 | |
22163 |
|
22164 |
|
22165 |
|
22166 |
|
22167 |
|
22168 |
|
22169 |
|
22170 |
|
22171 | const createMetadataTrackIfNotExists = (inbandTextTracks, dispatchType, tech) => {
|
22172 | if (inbandTextTracks.metadataTrack_) {
|
22173 | return;
|
22174 | }
|
22175 |
|
22176 | inbandTextTracks.metadataTrack_ = tech.addRemoteTextTrack({
|
22177 | kind: 'metadata',
|
22178 | label: 'Timed Metadata'
|
22179 | }, false).track;
|
22180 |
|
22181 | if (!videojs__default["default"].browser.IS_ANY_SAFARI) {
|
22182 | inbandTextTracks.metadataTrack_.inBandMetadataTrackDispatchType = dispatchType;
|
22183 | }
|
22184 | };
|
22185 | |
22186 |
|
22187 |
|
22188 |
|
22189 |
|
22190 |
|
22191 |
|
22192 |
|
22193 |
|
22194 | const removeCuesFromTrack = function (start, end, track) {
|
22195 | let i;
|
22196 | let cue;
|
22197 |
|
22198 | if (!track) {
|
22199 | return;
|
22200 | }
|
22201 |
|
22202 | if (!track.cues) {
|
22203 | return;
|
22204 | }
|
22205 |
|
22206 | i = track.cues.length;
|
22207 |
|
22208 | while (i--) {
|
22209 | cue = track.cues[i];
|
22210 |
|
22211 | if (cue.startTime >= start && cue.endTime <= end) {
|
22212 | track.removeCue(cue);
|
22213 | }
|
22214 | }
|
22215 | };
|
22216 | |
22217 |
|
22218 |
|
22219 |
|
22220 |
|
22221 |
|
22222 |
|
22223 |
|
22224 | const removeDuplicateCuesFromTrack = function (track) {
|
22225 | const cues = track.cues;
|
22226 |
|
22227 | if (!cues) {
|
22228 | return;
|
22229 | }
|
22230 |
|
22231 | const uniqueCues = {};
|
22232 |
|
22233 | for (let i = cues.length - 1; i >= 0; i--) {
|
22234 | const cue = cues[i];
|
22235 | const cueKey = `${cue.startTime}-${cue.endTime}-${cue.text}`;
|
22236 |
|
22237 | if (uniqueCues[cueKey]) {
|
22238 | track.removeCue(cue);
|
22239 | } else {
|
22240 | uniqueCues[cueKey] = cue;
|
22241 | }
|
22242 | }
|
22243 | };
|
22244 |
|
22245 | |
22246 |
|
22247 |
|
22248 |
|
22249 |
|
22250 |
|
22251 | var ONE_SECOND_IN_TS = 90000,
|
22252 |
|
22253 | secondsToVideoTs,
|
22254 | secondsToAudioTs,
|
22255 | videoTsToSeconds,
|
22256 | audioTsToSeconds,
|
22257 | audioTsToVideoTs,
|
22258 | videoTsToAudioTs,
|
22259 | metadataTsToSeconds;
|
22260 |
|
22261 | secondsToVideoTs = function (seconds) {
|
22262 | return seconds * ONE_SECOND_IN_TS;
|
22263 | };
|
22264 |
|
22265 | secondsToAudioTs = function (seconds, sampleRate) {
|
22266 | return seconds * sampleRate;
|
22267 | };
|
22268 |
|
22269 | videoTsToSeconds = function (timestamp) {
|
22270 | return timestamp / ONE_SECOND_IN_TS;
|
22271 | };
|
22272 |
|
22273 | audioTsToSeconds = function (timestamp, sampleRate) {
|
22274 | return timestamp / sampleRate;
|
22275 | };
|
22276 |
|
22277 | audioTsToVideoTs = function (timestamp, sampleRate) {
|
22278 | return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
|
22279 | };
|
22280 |
|
22281 | videoTsToAudioTs = function (timestamp, sampleRate) {
|
22282 | return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
|
22283 | };
|
22284 | |
22285 |
|
22286 |
|
22287 |
|
22288 |
|
22289 |
|
22290 | metadataTsToSeconds = function (timestamp, timelineStartPts, keepOriginalTimestamps) {
|
22291 | return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
|
22292 | };
|
22293 |
|
22294 | var clock = {
|
22295 | ONE_SECOND_IN_TS: ONE_SECOND_IN_TS,
|
22296 | secondsToVideoTs: secondsToVideoTs,
|
22297 | secondsToAudioTs: secondsToAudioTs,
|
22298 | videoTsToSeconds: videoTsToSeconds,
|
22299 | audioTsToSeconds: audioTsToSeconds,
|
22300 | audioTsToVideoTs: audioTsToVideoTs,
|
22301 | videoTsToAudioTs: videoTsToAudioTs,
|
22302 | metadataTsToSeconds: metadataTsToSeconds
|
22303 | };
|
22304 |
|
22305 | |
22306 |
|
22307 |
|
22308 |
|
22309 |
|
22310 |
|
22311 |
|
22312 |
|
22313 |
|
22314 |
|
22315 |
|
22316 |
|
22317 |
|
22318 |
|
22319 | const gopsSafeToAlignWith = (buffer, currentTime, mapping) => {
|
22320 | if (typeof currentTime === 'undefined' || currentTime === null || !buffer.length) {
|
22321 | return [];
|
22322 | }
|
22323 |
|
22324 |
|
22325 | const currentTimePts = Math.ceil((currentTime - mapping + 3) * clock.ONE_SECOND_IN_TS);
|
22326 | let i;
|
22327 |
|
22328 | for (i = 0; i < buffer.length; i++) {
|
22329 | if (buffer[i].pts > currentTimePts) {
|
22330 | break;
|
22331 | }
|
22332 | }
|
22333 |
|
22334 | return buffer.slice(i);
|
22335 | };
|
22336 | |
22337 |
|
22338 |
|
22339 |
|
22340 |
|
22341 |
|
22342 |
|
22343 |
|
22344 |
|
22345 |
|
22346 |
|
22347 |
|
22348 |
|
22349 |
|
22350 |
|
22351 | const updateGopBuffer = (buffer, gops, replace) => {
|
22352 | if (!gops.length) {
|
22353 | return buffer;
|
22354 | }
|
22355 |
|
22356 | if (replace) {
|
22357 |
|
22358 |
|
22359 |
|
22360 |
|
22361 | return gops.slice();
|
22362 | }
|
22363 |
|
22364 | const start = gops[0].pts;
|
22365 | let i = 0;
|
22366 |
|
22367 | for (i; i < buffer.length; i++) {
|
22368 | if (buffer[i].pts >= start) {
|
22369 | break;
|
22370 | }
|
22371 | }
|
22372 |
|
22373 | return buffer.slice(0, i).concat(gops);
|
22374 | };
|
22375 | |
22376 |
|
22377 |
|
22378 |
|
22379 |
|
22380 |
|
22381 |
|
22382 |
|
22383 |
|
22384 |
|
22385 |
|
22386 |
|
22387 |
|
22388 | const removeGopBuffer = (buffer, start, end, mapping) => {
|
22389 | const startPts = Math.ceil((start - mapping) * clock.ONE_SECOND_IN_TS);
|
22390 | const endPts = Math.ceil((end - mapping) * clock.ONE_SECOND_IN_TS);
|
22391 | const updatedBuffer = buffer.slice();
|
22392 | let i = buffer.length;
|
22393 |
|
22394 | while (i--) {
|
22395 | if (buffer[i].pts <= endPts) {
|
22396 | break;
|
22397 | }
|
22398 | }
|
22399 |
|
22400 | if (i === -1) {
|
22401 |
|
22402 | return updatedBuffer;
|
22403 | }
|
22404 |
|
22405 | let j = i + 1;
|
22406 |
|
22407 | while (j--) {
|
22408 | if (buffer[j].pts <= startPts) {
|
22409 | break;
|
22410 | }
|
22411 | }
|
22412 |
|
22413 |
|
22414 | j = Math.max(j, 0);
|
22415 | updatedBuffer.splice(j, i - j + 1);
|
22416 | return updatedBuffer;
|
22417 | };
|
22418 |
|
22419 | const shallowEqual = function (a, b) {
|
22420 |
|
22421 |
|
22422 |
|
22423 | if (!a && !b || !a && b || a && !b) {
|
22424 | return false;
|
22425 | }
|
22426 |
|
22427 |
|
22428 | if (a === b) {
|
22429 | return true;
|
22430 | }
|
22431 |
|
22432 |
|
22433 |
|
22434 | const akeys = Object.keys(a).sort();
|
22435 | const bkeys = Object.keys(b).sort();
|
22436 |
|
22437 | if (akeys.length !== bkeys.length) {
|
22438 | return false;
|
22439 | }
|
22440 |
|
22441 | for (let i = 0; i < akeys.length; i++) {
|
22442 | const key = akeys[i];
|
22443 |
|
22444 | if (key !== bkeys[i]) {
|
22445 | return false;
|
22446 | }
|
22447 |
|
22448 |
|
22449 | if (a[key] !== b[key]) {
|
22450 | return false;
|
22451 | }
|
22452 | }
|
22453 |
|
22454 | return true;
|
22455 | };
|
22456 |
|
22457 |
|
22458 | const QUOTA_EXCEEDED_ERR = 22;
|
22459 |
|
22460 | |
22461 |
|
22462 |
|
22463 |
|
22464 |
|
22465 |
|
22466 |
|
22467 |
|
22468 |
|
22469 |
|
22470 | const getSyncSegmentCandidate = function (currentTimeline, segments, targetTime) {
|
22471 | segments = segments || [];
|
22472 | const timelineSegments = [];
|
22473 | let time = 0;
|
22474 |
|
22475 | for (let i = 0; i < segments.length; i++) {
|
22476 | const segment = segments[i];
|
22477 |
|
22478 | if (currentTimeline === segment.timeline) {
|
22479 | timelineSegments.push(i);
|
22480 | time += segment.duration;
|
22481 |
|
22482 | if (time > targetTime) {
|
22483 | return i;
|
22484 | }
|
22485 | }
|
22486 | }
|
22487 |
|
22488 | if (timelineSegments.length === 0) {
|
22489 | return 0;
|
22490 | }
|
22491 |
|
22492 |
|
22493 | return timelineSegments[timelineSegments.length - 1];
|
22494 | };
|
22495 |
|
22496 |
|
22497 |
|
22498 |
|
22499 | const MIN_BACK_BUFFER = 1;
|
22500 |
|
22501 | const CHECK_BUFFER_DELAY = 500;
|
22502 |
|
22503 | const finite = num => typeof num === 'number' && isFinite(num);
|
22504 |
|
22505 |
|
22506 |
|
22507 |
|
22508 | const MIN_SEGMENT_DURATION_TO_SAVE_STATS = 1 / 60;
|
22509 | const illegalMediaSwitch = (loaderType, startingMedia, trackInfo) => {
|
22510 |
|
22511 |
|
22512 | if (loaderType !== 'main' || !startingMedia || !trackInfo) {
|
22513 | return null;
|
22514 | }
|
22515 |
|
22516 | if (!trackInfo.hasAudio && !trackInfo.hasVideo) {
|
22517 | return 'Neither audio nor video found in segment.';
|
22518 | }
|
22519 |
|
22520 | if (startingMedia.hasVideo && !trackInfo.hasVideo) {
|
22521 | 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.';
|
22522 | }
|
22523 |
|
22524 | if (!startingMedia.hasVideo && trackInfo.hasVideo) {
|
22525 | 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.';
|
22526 | }
|
22527 |
|
22528 | return null;
|
22529 | };
|
22530 | |
22531 |
|
22532 |
|
22533 |
|
22534 |
|
22535 |
|
22536 |
|
22537 |
|
22538 |
|
22539 |
|
22540 |
|
22541 |
|
22542 |
|
22543 |
|
22544 | const safeBackBufferTrimTime = (seekable, currentTime, targetDuration) => {
|
22545 |
|
22546 |
|
22547 |
|
22548 |
|
22549 |
|
22550 | let trimTime = currentTime - Config.BACK_BUFFER_LENGTH;
|
22551 |
|
22552 | if (seekable.length) {
|
22553 |
|
22554 |
|
22555 | trimTime = Math.max(trimTime, seekable.start(0));
|
22556 | }
|
22557 |
|
22558 |
|
22559 |
|
22560 | const maxTrimTime = currentTime - targetDuration;
|
22561 | return Math.min(maxTrimTime, trimTime);
|
22562 | };
|
22563 | const segmentInfoString = segmentInfo => {
|
22564 | const {
|
22565 | startOfSegment,
|
22566 | duration,
|
22567 | segment,
|
22568 | part,
|
22569 | playlist: {
|
22570 | mediaSequence: seq,
|
22571 | id,
|
22572 | segments = []
|
22573 | },
|
22574 | mediaIndex: index,
|
22575 | partIndex,
|
22576 | timeline
|
22577 | } = segmentInfo;
|
22578 | const segmentLen = segments.length - 1;
|
22579 | let selection = 'mediaIndex/partIndex increment';
|
22580 |
|
22581 | if (segmentInfo.getMediaInfoForTime) {
|
22582 | selection = `getMediaInfoForTime (${segmentInfo.getMediaInfoForTime})`;
|
22583 | } else if (segmentInfo.isSyncRequest) {
|
22584 | selection = 'getSyncSegmentCandidate (isSyncRequest)';
|
22585 | }
|
22586 |
|
22587 | if (segmentInfo.independent) {
|
22588 | selection += ` with independent ${segmentInfo.independent}`;
|
22589 | }
|
22590 |
|
22591 | const hasPartIndex = typeof partIndex === 'number';
|
22592 | const name = segmentInfo.segment.uri ? 'segment' : 'pre-segment';
|
22593 | const zeroBasedPartCount = hasPartIndex ? getKnownPartCount({
|
22594 | preloadSegment: segment
|
22595 | }) - 1 : 0;
|
22596 | 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}]`;
|
22597 | };
|
22598 |
|
22599 | const timingInfoPropertyForMedia = mediaType => `${mediaType}TimingInfo`;
|
22600 | |
22601 |
|
22602 |
|
22603 |
|
22604 |
|
22605 |
|
22606 |
|
22607 |
|
22608 |
|
22609 |
|
22610 |
|
22611 |
|
22612 |
|
22613 |
|
22614 |
|
22615 |
|
22616 |
|
22617 |
|
22618 |
|
22619 |
|
22620 |
|
22621 | const timestampOffsetForSegment = ({
|
22622 | segmentTimeline,
|
22623 | currentTimeline,
|
22624 | startOfSegment,
|
22625 | buffered,
|
22626 | overrideCheck
|
22627 | }) => {
|
22628 |
|
22629 |
|
22630 |
|
22631 |
|
22632 |
|
22633 |
|
22634 | if (!overrideCheck && segmentTimeline === currentTimeline) {
|
22635 | return null;
|
22636 | }
|
22637 |
|
22638 |
|
22639 |
|
22640 |
|
22641 |
|
22642 |
|
22643 |
|
22644 |
|
22645 |
|
22646 |
|
22647 |
|
22648 |
|
22649 |
|
22650 |
|
22651 |
|
22652 |
|
22653 |
|
22654 |
|
22655 |
|
22656 |
|
22657 |
|
22658 |
|
22659 |
|
22660 |
|
22661 |
|
22662 | if (segmentTimeline < currentTimeline) {
|
22663 | return startOfSegment;
|
22664 | }
|
22665 |
|
22666 |
|
22667 |
|
22668 |
|
22669 |
|
22670 |
|
22671 | return buffered.length ? buffered.end(buffered.length - 1) : startOfSegment;
|
22672 | };
|
22673 | |
22674 |
|
22675 |
|
22676 |
|
22677 |
|
22678 |
|
22679 |
|
22680 |
|
22681 |
|
22682 |
|
22683 |
|
22684 |
|
22685 |
|
22686 |
|
22687 |
|
22688 |
|
22689 |
|
22690 |
|
22691 |
|
22692 |
|
22693 |
|
22694 |
|
22695 |
|
22696 |
|
22697 |
|
22698 |
|
22699 |
|
22700 |
|
22701 |
|
22702 |
|
22703 |
|
22704 |
|
22705 |
|
22706 |
|
22707 |
|
22708 |
|
22709 |
|
22710 |
|
22711 |
|
22712 |
|
22713 |
|
22714 |
|
22715 |
|
22716 |
|
22717 |
|
22718 |
|
22719 |
|
22720 |
|
22721 |
|
22722 |
|
22723 |
|
22724 |
|
22725 |
|
22726 |
|
22727 |
|
22728 |
|
22729 |
|
22730 |
|
22731 |
|
22732 |
|
22733 |
|
22734 |
|
22735 |
|
22736 |
|
22737 |
|
22738 |
|
22739 |
|
22740 |
|
22741 |
|
22742 |
|
22743 |
|
22744 |
|
22745 |
|
22746 |
|
22747 |
|
22748 |
|
22749 |
|
22750 |
|
22751 |
|
22752 |
|
22753 |
|
22754 |
|
22755 |
|
22756 |
|
22757 |
|
22758 |
|
22759 |
|
22760 |
|
22761 |
|
22762 |
|
22763 |
|
22764 |
|
22765 |
|
22766 |
|
22767 |
|
22768 |
|
22769 | const shouldWaitForTimelineChange = ({
|
22770 | timelineChangeController,
|
22771 | currentTimeline,
|
22772 | segmentTimeline,
|
22773 | loaderType,
|
22774 | audioDisabled
|
22775 | }) => {
|
22776 | if (currentTimeline === segmentTimeline) {
|
22777 | return false;
|
22778 | }
|
22779 |
|
22780 | if (loaderType === 'audio') {
|
22781 | const lastMainTimelineChange = timelineChangeController.lastTimelineChange({
|
22782 | type: 'main'
|
22783 | });
|
22784 |
|
22785 |
|
22786 |
|
22787 |
|
22788 | return !lastMainTimelineChange || lastMainTimelineChange.to !== segmentTimeline;
|
22789 | }
|
22790 |
|
22791 |
|
22792 |
|
22793 |
|
22794 |
|
22795 | if (loaderType === 'main' && audioDisabled) {
|
22796 | const pendingAudioTimelineChange = timelineChangeController.pendingTimelineChange({
|
22797 | type: 'audio'
|
22798 | });
|
22799 |
|
22800 |
|
22801 |
|
22802 |
|
22803 |
|
22804 |
|
22805 |
|
22806 |
|
22807 |
|
22808 |
|
22809 |
|
22810 |
|
22811 |
|
22812 |
|
22813 |
|
22814 |
|
22815 |
|
22816 |
|
22817 | if (pendingAudioTimelineChange && pendingAudioTimelineChange.to === segmentTimeline) {
|
22818 | return false;
|
22819 | }
|
22820 |
|
22821 | return true;
|
22822 | }
|
22823 |
|
22824 | return false;
|
22825 | };
|
22826 | const mediaDuration = timingInfos => {
|
22827 | let maxDuration = 0;
|
22828 | ['video', 'audio'].forEach(function (type) {
|
22829 | const typeTimingInfo = timingInfos[`${type}TimingInfo`];
|
22830 |
|
22831 | if (!typeTimingInfo) {
|
22832 | return;
|
22833 | }
|
22834 |
|
22835 | const {
|
22836 | start,
|
22837 | end
|
22838 | } = typeTimingInfo;
|
22839 | let duration;
|
22840 |
|
22841 | if (typeof start === 'bigint' || typeof end === 'bigint') {
|
22842 | duration = window.BigInt(end) - window.BigInt(start);
|
22843 | } else if (typeof start === 'number' && typeof end === 'number') {
|
22844 | duration = end - start;
|
22845 | }
|
22846 |
|
22847 | if (typeof duration !== 'undefined' && duration > maxDuration) {
|
22848 | maxDuration = duration;
|
22849 | }
|
22850 | });
|
22851 |
|
22852 |
|
22853 | if (typeof maxDuration === 'bigint' && maxDuration < Number.MAX_SAFE_INTEGER) {
|
22854 | maxDuration = Number(maxDuration);
|
22855 | }
|
22856 |
|
22857 | return maxDuration;
|
22858 | };
|
22859 | const segmentTooLong = ({
|
22860 | segmentDuration,
|
22861 | maxDuration
|
22862 | }) => {
|
22863 |
|
22864 |
|
22865 | if (!segmentDuration) {
|
22866 | return false;
|
22867 | }
|
22868 |
|
22869 |
|
22870 |
|
22871 |
|
22872 |
|
22873 |
|
22874 |
|
22875 |
|
22876 |
|
22877 |
|
22878 |
|
22879 |
|
22880 | return Math.round(segmentDuration) > maxDuration + TIME_FUDGE_FACTOR;
|
22881 | };
|
22882 | const getTroublesomeSegmentDurationMessage = (segmentInfo, sourceType) => {
|
22883 |
|
22884 |
|
22885 | if (sourceType !== 'hls') {
|
22886 | return null;
|
22887 | }
|
22888 |
|
22889 | const segmentDuration = mediaDuration({
|
22890 | audioTimingInfo: segmentInfo.audioTimingInfo,
|
22891 | videoTimingInfo: segmentInfo.videoTimingInfo
|
22892 | });
|
22893 |
|
22894 |
|
22895 |
|
22896 |
|
22897 | if (!segmentDuration) {
|
22898 | return null;
|
22899 | }
|
22900 |
|
22901 | const targetDuration = segmentInfo.playlist.targetDuration;
|
22902 | const isSegmentWayTooLong = segmentTooLong({
|
22903 | segmentDuration,
|
22904 | maxDuration: targetDuration * 2
|
22905 | });
|
22906 | const isSegmentSlightlyTooLong = segmentTooLong({
|
22907 | segmentDuration,
|
22908 | maxDuration: targetDuration
|
22909 | });
|
22910 | const 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';
|
22911 |
|
22912 | if (isSegmentWayTooLong || isSegmentSlightlyTooLong) {
|
22913 | return {
|
22914 | severity: isSegmentWayTooLong ? 'warn' : 'info',
|
22915 | message: segmentTooLongMessage
|
22916 | };
|
22917 | }
|
22918 |
|
22919 | return null;
|
22920 | };
|
22921 | |
22922 |
|
22923 |
|
22924 |
|
22925 |
|
22926 |
|
22927 |
|
22928 |
|
22929 | class SegmentLoader extends videojs__default["default"].EventTarget {
|
22930 | constructor(settings, options = {}) {
|
22931 | super();
|
22932 |
|
22933 | if (!settings) {
|
22934 | throw new TypeError('Initialization settings are required');
|
22935 | }
|
22936 |
|
22937 | if (typeof settings.currentTime !== 'function') {
|
22938 | throw new TypeError('No currentTime getter specified');
|
22939 | }
|
22940 |
|
22941 | if (!settings.mediaSource) {
|
22942 | throw new TypeError('No MediaSource specified');
|
22943 | }
|
22944 |
|
22945 |
|
22946 | this.bandwidth = settings.bandwidth;
|
22947 | this.throughput = {
|
22948 | rate: 0,
|
22949 | count: 0
|
22950 | };
|
22951 | this.roundTrip = NaN;
|
22952 | this.resetStats_();
|
22953 | this.mediaIndex = null;
|
22954 | this.partIndex = null;
|
22955 |
|
22956 | this.hasPlayed_ = settings.hasPlayed;
|
22957 | this.currentTime_ = settings.currentTime;
|
22958 | this.seekable_ = settings.seekable;
|
22959 | this.seeking_ = settings.seeking;
|
22960 | this.duration_ = settings.duration;
|
22961 | this.mediaSource_ = settings.mediaSource;
|
22962 | this.vhs_ = settings.vhs;
|
22963 | this.loaderType_ = settings.loaderType;
|
22964 | this.currentMediaInfo_ = void 0;
|
22965 | this.startingMediaInfo_ = void 0;
|
22966 | this.segmentMetadataTrack_ = settings.segmentMetadataTrack;
|
22967 | this.goalBufferLength_ = settings.goalBufferLength;
|
22968 | this.sourceType_ = settings.sourceType;
|
22969 | this.sourceUpdater_ = settings.sourceUpdater;
|
22970 | this.inbandTextTracks_ = settings.inbandTextTracks;
|
22971 | this.state_ = 'INIT';
|
22972 | this.timelineChangeController_ = settings.timelineChangeController;
|
22973 | this.shouldSaveSegmentTimingInfo_ = true;
|
22974 | this.parse708captions_ = settings.parse708captions;
|
22975 | this.useDtsForTimestampOffset_ = settings.useDtsForTimestampOffset;
|
22976 | this.captionServices_ = settings.captionServices;
|
22977 | this.exactManifestTimings = settings.exactManifestTimings;
|
22978 | this.addMetadataToTextTrack = settings.addMetadataToTextTrack;
|
22979 |
|
22980 | this.checkBufferTimeout_ = null;
|
22981 | this.error_ = void 0;
|
22982 | this.currentTimeline_ = -1;
|
22983 | this.shouldForceTimestampOffsetAfterResync_ = false;
|
22984 | this.pendingSegment_ = null;
|
22985 | this.xhrOptions_ = null;
|
22986 | this.pendingSegments_ = [];
|
22987 | this.audioDisabled_ = false;
|
22988 | this.isPendingTimestampOffset_ = false;
|
22989 |
|
22990 | this.gopBuffer_ = [];
|
22991 | this.timeMapping_ = 0;
|
22992 | this.safeAppend_ = false;
|
22993 | this.appendInitSegment_ = {
|
22994 | audio: true,
|
22995 | video: true
|
22996 | };
|
22997 | this.playlistOfLastInitSegment_ = {
|
22998 | audio: null,
|
22999 | video: null
|
23000 | };
|
23001 | this.callQueue_ = [];
|
23002 |
|
23003 |
|
23004 |
|
23005 |
|
23006 |
|
23007 | this.loadQueue_ = [];
|
23008 | this.metadataQueue_ = {
|
23009 | id3: [],
|
23010 | caption: []
|
23011 | };
|
23012 | this.waitingOnRemove_ = false;
|
23013 | this.quotaExceededErrorRetryTimeout_ = null;
|
23014 |
|
23015 | this.activeInitSegmentId_ = null;
|
23016 | this.initSegments_ = {};
|
23017 |
|
23018 | this.cacheEncryptionKeys_ = settings.cacheEncryptionKeys;
|
23019 | this.keyCache_ = {};
|
23020 | this.decrypter_ = settings.decrypter;
|
23021 |
|
23022 |
|
23023 |
|
23024 | this.syncController_ = settings.syncController;
|
23025 | this.syncPoint_ = {
|
23026 | segmentIndex: 0,
|
23027 | time: 0
|
23028 | };
|
23029 | this.transmuxer_ = this.createTransmuxer_();
|
23030 |
|
23031 | this.triggerSyncInfoUpdate_ = () => this.trigger('syncinfoupdate');
|
23032 |
|
23033 | this.syncController_.on('syncinfoupdate', this.triggerSyncInfoUpdate_);
|
23034 | this.mediaSource_.addEventListener('sourceopen', () => {
|
23035 | if (!this.isEndOfStream_()) {
|
23036 | this.ended_ = false;
|
23037 | }
|
23038 | });
|
23039 |
|
23040 | this.fetchAtBuffer_ = false;
|
23041 | this.logger_ = logger(`SegmentLoader[${this.loaderType_}]`);
|
23042 | Object.defineProperty(this, 'state', {
|
23043 | get() {
|
23044 | return this.state_;
|
23045 | },
|
23046 |
|
23047 | set(newState) {
|
23048 | if (newState !== this.state_) {
|
23049 | this.logger_(`${this.state_} -> ${newState}`);
|
23050 | this.state_ = newState;
|
23051 | this.trigger('statechange');
|
23052 | }
|
23053 | }
|
23054 |
|
23055 | });
|
23056 | this.sourceUpdater_.on('ready', () => {
|
23057 | if (this.hasEnoughInfoToAppend_()) {
|
23058 | this.processCallQueue_();
|
23059 | }
|
23060 | });
|
23061 |
|
23062 |
|
23063 |
|
23064 |
|
23065 | if (this.loaderType_ === 'main') {
|
23066 | this.timelineChangeController_.on('pendingtimelinechange', () => {
|
23067 | if (this.hasEnoughInfoToAppend_()) {
|
23068 | this.processCallQueue_();
|
23069 | }
|
23070 | });
|
23071 | }
|
23072 |
|
23073 |
|
23074 |
|
23075 |
|
23076 | if (this.loaderType_ === 'audio') {
|
23077 | this.timelineChangeController_.on('timelinechange', () => {
|
23078 | if (this.hasEnoughInfoToLoad_()) {
|
23079 | this.processLoadQueue_();
|
23080 | }
|
23081 |
|
23082 | if (this.hasEnoughInfoToAppend_()) {
|
23083 | this.processCallQueue_();
|
23084 | }
|
23085 | });
|
23086 | }
|
23087 | }
|
23088 |
|
23089 | createTransmuxer_() {
|
23090 | return segmentTransmuxer.createTransmuxer({
|
23091 | remux: false,
|
23092 | alignGopsAtEnd: this.safeAppend_,
|
23093 | keepOriginalTimestamps: true,
|
23094 | parse708captions: this.parse708captions_,
|
23095 | captionServices: this.captionServices_
|
23096 | });
|
23097 | }
|
23098 | |
23099 |
|
23100 |
|
23101 |
|
23102 |
|
23103 |
|
23104 |
|
23105 | resetStats_() {
|
23106 | this.mediaBytesTransferred = 0;
|
23107 | this.mediaRequests = 0;
|
23108 | this.mediaRequestsAborted = 0;
|
23109 | this.mediaRequestsTimedout = 0;
|
23110 | this.mediaRequestsErrored = 0;
|
23111 | this.mediaTransferDuration = 0;
|
23112 | this.mediaSecondsLoaded = 0;
|
23113 | this.mediaAppends = 0;
|
23114 | }
|
23115 | |
23116 |
|
23117 |
|
23118 |
|
23119 |
|
23120 | dispose() {
|
23121 | this.trigger('dispose');
|
23122 | this.state = 'DISPOSED';
|
23123 | this.pause();
|
23124 | this.abort_();
|
23125 |
|
23126 | if (this.transmuxer_) {
|
23127 | this.transmuxer_.terminate();
|
23128 | }
|
23129 |
|
23130 | this.resetStats_();
|
23131 |
|
23132 | if (this.checkBufferTimeout_) {
|
23133 | window.clearTimeout(this.checkBufferTimeout_);
|
23134 | }
|
23135 |
|
23136 | if (this.syncController_ && this.triggerSyncInfoUpdate_) {
|
23137 | this.syncController_.off('syncinfoupdate', this.triggerSyncInfoUpdate_);
|
23138 | }
|
23139 |
|
23140 | this.off();
|
23141 | }
|
23142 |
|
23143 | setAudio(enable) {
|
23144 | this.audioDisabled_ = !enable;
|
23145 |
|
23146 | if (enable) {
|
23147 | this.appendInitSegment_.audio = true;
|
23148 | } else {
|
23149 |
|
23150 | this.sourceUpdater_.removeAudio(0, this.duration_());
|
23151 | }
|
23152 | }
|
23153 | |
23154 |
|
23155 |
|
23156 |
|
23157 |
|
23158 |
|
23159 | abort() {
|
23160 | if (this.state !== 'WAITING') {
|
23161 | if (this.pendingSegment_) {
|
23162 | this.pendingSegment_ = null;
|
23163 | }
|
23164 |
|
23165 | return;
|
23166 | }
|
23167 |
|
23168 | this.abort_();
|
23169 |
|
23170 |
|
23171 |
|
23172 |
|
23173 | this.state = 'READY';
|
23174 |
|
23175 |
|
23176 | if (!this.paused()) {
|
23177 | this.monitorBuffer_();
|
23178 | }
|
23179 | }
|
23180 | |
23181 |
|
23182 |
|
23183 |
|
23184 |
|
23185 |
|
23186 |
|
23187 | abort_() {
|
23188 | if (this.pendingSegment_ && this.pendingSegment_.abortRequests) {
|
23189 | this.pendingSegment_.abortRequests();
|
23190 | }
|
23191 |
|
23192 |
|
23193 | this.pendingSegment_ = null;
|
23194 | this.callQueue_ = [];
|
23195 | this.loadQueue_ = [];
|
23196 | this.metadataQueue_.id3 = [];
|
23197 | this.metadataQueue_.caption = [];
|
23198 | this.timelineChangeController_.clearPendingTimelineChange(this.loaderType_);
|
23199 | this.waitingOnRemove_ = false;
|
23200 | window.clearTimeout(this.quotaExceededErrorRetryTimeout_);
|
23201 | this.quotaExceededErrorRetryTimeout_ = null;
|
23202 | }
|
23203 |
|
23204 | checkForAbort_(requestId) {
|
23205 |
|
23206 |
|
23207 | if (this.state === 'APPENDING' && !this.pendingSegment_) {
|
23208 | this.state = 'READY';
|
23209 | return true;
|
23210 | }
|
23211 |
|
23212 | if (!this.pendingSegment_ || this.pendingSegment_.requestId !== requestId) {
|
23213 | return true;
|
23214 | }
|
23215 |
|
23216 | return false;
|
23217 | }
|
23218 | |
23219 |
|
23220 |
|
23221 |
|
23222 |
|
23223 |
|
23224 |
|
23225 |
|
23226 | error(error) {
|
23227 | if (typeof error !== 'undefined') {
|
23228 | this.logger_('error occurred:', error);
|
23229 | this.error_ = error;
|
23230 | }
|
23231 |
|
23232 | this.pendingSegment_ = null;
|
23233 | return this.error_;
|
23234 | }
|
23235 |
|
23236 | endOfStream() {
|
23237 | this.ended_ = true;
|
23238 |
|
23239 | if (this.transmuxer_) {
|
23240 |
|
23241 | segmentTransmuxer.reset(this.transmuxer_);
|
23242 | }
|
23243 |
|
23244 | this.gopBuffer_.length = 0;
|
23245 | this.pause();
|
23246 | this.trigger('ended');
|
23247 | }
|
23248 | |
23249 |
|
23250 |
|
23251 |
|
23252 |
|
23253 |
|
23254 |
|
23255 |
|
23256 | buffered_() {
|
23257 | const trackInfo = this.getMediaInfo_();
|
23258 |
|
23259 | if (!this.sourceUpdater_ || !trackInfo) {
|
23260 | return createTimeRanges();
|
23261 | }
|
23262 |
|
23263 | if (this.loaderType_ === 'main') {
|
23264 | const {
|
23265 | hasAudio,
|
23266 | hasVideo,
|
23267 | isMuxed
|
23268 | } = trackInfo;
|
23269 |
|
23270 | if (hasVideo && hasAudio && !this.audioDisabled_ && !isMuxed) {
|
23271 | return this.sourceUpdater_.buffered();
|
23272 | }
|
23273 |
|
23274 | if (hasVideo) {
|
23275 | return this.sourceUpdater_.videoBuffered();
|
23276 | }
|
23277 | }
|
23278 |
|
23279 |
|
23280 |
|
23281 | return this.sourceUpdater_.audioBuffered();
|
23282 | }
|
23283 | |
23284 |
|
23285 |
|
23286 |
|
23287 |
|
23288 |
|
23289 |
|
23290 |
|
23291 |
|
23292 |
|
23293 |
|
23294 |
|
23295 | initSegmentForMap(map, set = false) {
|
23296 | if (!map) {
|
23297 | return null;
|
23298 | }
|
23299 |
|
23300 | const id = initSegmentId(map);
|
23301 | let storedMap = this.initSegments_[id];
|
23302 |
|
23303 | if (set && !storedMap && map.bytes) {
|
23304 | this.initSegments_[id] = storedMap = {
|
23305 | resolvedUri: map.resolvedUri,
|
23306 | byterange: map.byterange,
|
23307 | bytes: map.bytes,
|
23308 | tracks: map.tracks,
|
23309 | timescales: map.timescales
|
23310 | };
|
23311 | }
|
23312 |
|
23313 | return storedMap || map;
|
23314 | }
|
23315 | |
23316 |
|
23317 |
|
23318 |
|
23319 |
|
23320 |
|
23321 |
|
23322 |
|
23323 |
|
23324 |
|
23325 |
|
23326 |
|
23327 | segmentKey(key, set = false) {
|
23328 | if (!key) {
|
23329 | return null;
|
23330 | }
|
23331 |
|
23332 | const id = segmentKeyId(key);
|
23333 | let storedKey = this.keyCache_[id];
|
23334 |
|
23335 |
|
23336 | if (this.cacheEncryptionKeys_ && set && !storedKey && key.bytes) {
|
23337 | this.keyCache_[id] = storedKey = {
|
23338 | resolvedUri: key.resolvedUri,
|
23339 | bytes: key.bytes
|
23340 | };
|
23341 | }
|
23342 |
|
23343 | const result = {
|
23344 | resolvedUri: (storedKey || key).resolvedUri
|
23345 | };
|
23346 |
|
23347 | if (storedKey) {
|
23348 | result.bytes = storedKey.bytes;
|
23349 | }
|
23350 |
|
23351 | return result;
|
23352 | }
|
23353 | |
23354 |
|
23355 |
|
23356 |
|
23357 |
|
23358 |
|
23359 |
|
23360 |
|
23361 | couldBeginLoading_() {
|
23362 | return this.playlist_ && !this.paused();
|
23363 | }
|
23364 | |
23365 |
|
23366 |
|
23367 |
|
23368 |
|
23369 | load() {
|
23370 |
|
23371 | this.monitorBuffer_();
|
23372 |
|
23373 |
|
23374 | if (!this.playlist_) {
|
23375 | return;
|
23376 | }
|
23377 |
|
23378 |
|
23379 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
23380 | return this.init_();
|
23381 | }
|
23382 |
|
23383 |
|
23384 |
|
23385 | if (!this.couldBeginLoading_() || this.state !== 'READY' && this.state !== 'INIT') {
|
23386 | return;
|
23387 | }
|
23388 |
|
23389 | this.state = 'READY';
|
23390 | }
|
23391 | |
23392 |
|
23393 |
|
23394 |
|
23395 |
|
23396 |
|
23397 |
|
23398 |
|
23399 |
|
23400 | init_() {
|
23401 | this.state = 'READY';
|
23402 |
|
23403 |
|
23404 | this.resetEverything();
|
23405 | return this.monitorBuffer_();
|
23406 | }
|
23407 | |
23408 |
|
23409 |
|
23410 |
|
23411 |
|
23412 |
|
23413 |
|
23414 | playlist(newPlaylist, options = {}) {
|
23415 | if (!newPlaylist) {
|
23416 | return;
|
23417 | }
|
23418 |
|
23419 | const oldPlaylist = this.playlist_;
|
23420 | const segmentInfo = this.pendingSegment_;
|
23421 | this.playlist_ = newPlaylist;
|
23422 | this.xhrOptions_ = options;
|
23423 |
|
23424 |
|
23425 |
|
23426 |
|
23427 |
|
23428 |
|
23429 |
|
23430 | if (this.state === 'INIT') {
|
23431 | newPlaylist.syncInfo = {
|
23432 | mediaSequence: newPlaylist.mediaSequence,
|
23433 | time: 0
|
23434 | };
|
23435 |
|
23436 |
|
23437 |
|
23438 |
|
23439 |
|
23440 |
|
23441 |
|
23442 |
|
23443 | if (this.loaderType_ === 'main') {
|
23444 | this.syncController_.setDateTimeMappingForStart(newPlaylist);
|
23445 | }
|
23446 | }
|
23447 |
|
23448 | let oldId = null;
|
23449 |
|
23450 | if (oldPlaylist) {
|
23451 | if (oldPlaylist.id) {
|
23452 | oldId = oldPlaylist.id;
|
23453 | } else if (oldPlaylist.uri) {
|
23454 | oldId = oldPlaylist.uri;
|
23455 | }
|
23456 | }
|
23457 |
|
23458 | this.logger_(`playlist update [${oldId} => ${newPlaylist.id || newPlaylist.uri}]`);
|
23459 | this.syncController_.updateMediaSequenceMap(newPlaylist, this.currentTime_(), this.loaderType_);
|
23460 |
|
23461 |
|
23462 | this.trigger('syncinfoupdate');
|
23463 |
|
23464 |
|
23465 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
23466 | return this.init_();
|
23467 | }
|
23468 |
|
23469 | if (!oldPlaylist || oldPlaylist.uri !== newPlaylist.uri) {
|
23470 | if (this.mediaIndex !== null) {
|
23471 |
|
23472 |
|
23473 |
|
23474 |
|
23475 |
|
23476 |
|
23477 |
|
23478 |
|
23479 | const isLLHLS = !newPlaylist.endList && typeof newPlaylist.partTargetDuration === 'number';
|
23480 |
|
23481 | if (isLLHLS) {
|
23482 | this.resetLoader();
|
23483 | } else {
|
23484 | this.resyncLoader();
|
23485 | }
|
23486 | }
|
23487 |
|
23488 | this.currentMediaInfo_ = void 0;
|
23489 | this.trigger('playlistupdate');
|
23490 |
|
23491 | return;
|
23492 | }
|
23493 |
|
23494 |
|
23495 |
|
23496 | const mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
23497 | this.logger_(`live window shift [${mediaSequenceDiff}]`);
|
23498 |
|
23499 |
|
23500 |
|
23501 | if (this.mediaIndex !== null) {
|
23502 | this.mediaIndex -= mediaSequenceDiff;
|
23503 |
|
23504 |
|
23505 |
|
23506 | if (this.mediaIndex < 0) {
|
23507 | this.mediaIndex = null;
|
23508 | this.partIndex = null;
|
23509 | } else {
|
23510 | const segment = this.playlist_.segments[this.mediaIndex];
|
23511 |
|
23512 |
|
23513 |
|
23514 | if (this.partIndex && (!segment.parts || !segment.parts.length || !segment.parts[this.partIndex])) {
|
23515 | const mediaIndex = this.mediaIndex;
|
23516 | this.logger_(`currently processing part (index ${this.partIndex}) no longer exists.`);
|
23517 | this.resetLoader();
|
23518 |
|
23519 |
|
23520 |
|
23521 | this.mediaIndex = mediaIndex;
|
23522 | }
|
23523 | }
|
23524 | }
|
23525 |
|
23526 |
|
23527 |
|
23528 |
|
23529 | if (segmentInfo) {
|
23530 | segmentInfo.mediaIndex -= mediaSequenceDiff;
|
23531 |
|
23532 | if (segmentInfo.mediaIndex < 0) {
|
23533 | segmentInfo.mediaIndex = null;
|
23534 | segmentInfo.partIndex = null;
|
23535 | } else {
|
23536 |
|
23537 |
|
23538 |
|
23539 | if (segmentInfo.mediaIndex >= 0) {
|
23540 | segmentInfo.segment = newPlaylist.segments[segmentInfo.mediaIndex];
|
23541 | }
|
23542 |
|
23543 | if (segmentInfo.partIndex >= 0 && segmentInfo.segment.parts) {
|
23544 | segmentInfo.part = segmentInfo.segment.parts[segmentInfo.partIndex];
|
23545 | }
|
23546 | }
|
23547 | }
|
23548 |
|
23549 | this.syncController_.saveExpiredSegmentInfo(oldPlaylist, newPlaylist);
|
23550 | }
|
23551 | |
23552 |
|
23553 |
|
23554 |
|
23555 |
|
23556 |
|
23557 |
|
23558 |
|
23559 | pause() {
|
23560 | if (this.checkBufferTimeout_) {
|
23561 | window.clearTimeout(this.checkBufferTimeout_);
|
23562 | this.checkBufferTimeout_ = null;
|
23563 | }
|
23564 | }
|
23565 | |
23566 |
|
23567 |
|
23568 |
|
23569 |
|
23570 |
|
23571 |
|
23572 | paused() {
|
23573 | return this.checkBufferTimeout_ === null;
|
23574 | }
|
23575 | |
23576 |
|
23577 |
|
23578 |
|
23579 |
|
23580 |
|
23581 |
|
23582 |
|
23583 | resetEverything(done) {
|
23584 | this.ended_ = false;
|
23585 | this.activeInitSegmentId_ = null;
|
23586 | this.appendInitSegment_ = {
|
23587 | audio: true,
|
23588 | video: true
|
23589 | };
|
23590 | this.resetLoader();
|
23591 |
|
23592 |
|
23593 |
|
23594 | this.remove(0, Infinity, done);
|
23595 |
|
23596 | if (this.transmuxer_) {
|
23597 | this.transmuxer_.postMessage({
|
23598 | action: 'clearAllMp4Captions'
|
23599 | });
|
23600 |
|
23601 | this.transmuxer_.postMessage({
|
23602 | action: 'reset'
|
23603 | });
|
23604 | }
|
23605 | }
|
23606 | |
23607 |
|
23608 |
|
23609 |
|
23610 |
|
23611 |
|
23612 |
|
23613 |
|
23614 | resetLoader() {
|
23615 | this.fetchAtBuffer_ = false;
|
23616 | this.resyncLoader();
|
23617 | }
|
23618 | |
23619 |
|
23620 |
|
23621 |
|
23622 |
|
23623 |
|
23624 | resyncLoader() {
|
23625 | if (this.transmuxer_) {
|
23626 |
|
23627 | segmentTransmuxer.reset(this.transmuxer_);
|
23628 | }
|
23629 |
|
23630 | this.mediaIndex = null;
|
23631 | this.partIndex = null;
|
23632 | this.syncPoint_ = null;
|
23633 | this.isPendingTimestampOffset_ = false;
|
23634 | this.shouldForceTimestampOffsetAfterResync_ = true;
|
23635 | this.callQueue_ = [];
|
23636 | this.loadQueue_ = [];
|
23637 | this.metadataQueue_.id3 = [];
|
23638 | this.metadataQueue_.caption = [];
|
23639 | this.abort();
|
23640 |
|
23641 | if (this.transmuxer_) {
|
23642 | this.transmuxer_.postMessage({
|
23643 | action: 'clearParsedMp4Captions'
|
23644 | });
|
23645 | }
|
23646 | }
|
23647 | |
23648 |
|
23649 |
|
23650 |
|
23651 |
|
23652 |
|
23653 |
|
23654 |
|
23655 |
|
23656 |
|
23657 |
|
23658 | remove(start, end, done = () => {}, force = false) {
|
23659 |
|
23660 |
|
23661 |
|
23662 | if (end === Infinity) {
|
23663 | end = this.duration_();
|
23664 | }
|
23665 |
|
23666 |
|
23667 |
|
23668 |
|
23669 | if (end <= start) {
|
23670 | this.logger_('skipping remove because end ${end} is <= start ${start}');
|
23671 | return;
|
23672 | }
|
23673 |
|
23674 | if (!this.sourceUpdater_ || !this.getMediaInfo_()) {
|
23675 | this.logger_('skipping remove because no source updater or starting media info');
|
23676 |
|
23677 | return;
|
23678 | }
|
23679 |
|
23680 |
|
23681 | let removesRemaining = 1;
|
23682 |
|
23683 | const removeFinished = () => {
|
23684 | removesRemaining--;
|
23685 |
|
23686 | if (removesRemaining === 0) {
|
23687 | done();
|
23688 | }
|
23689 | };
|
23690 |
|
23691 | if (force || !this.audioDisabled_) {
|
23692 | removesRemaining++;
|
23693 | this.sourceUpdater_.removeAudio(start, end, removeFinished);
|
23694 | }
|
23695 |
|
23696 |
|
23697 |
|
23698 |
|
23699 |
|
23700 |
|
23701 |
|
23702 |
|
23703 |
|
23704 | if (force || this.loaderType_ === 'main') {
|
23705 | this.gopBuffer_ = removeGopBuffer(this.gopBuffer_, start, end, this.timeMapping_);
|
23706 | removesRemaining++;
|
23707 | this.sourceUpdater_.removeVideo(start, end, removeFinished);
|
23708 | }
|
23709 |
|
23710 |
|
23711 | for (const track in this.inbandTextTracks_) {
|
23712 | removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
|
23713 | }
|
23714 |
|
23715 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
23716 |
|
23717 | removeFinished();
|
23718 | }
|
23719 | |
23720 |
|
23721 |
|
23722 |
|
23723 |
|
23724 |
|
23725 |
|
23726 | monitorBuffer_() {
|
23727 | if (this.checkBufferTimeout_) {
|
23728 | window.clearTimeout(this.checkBufferTimeout_);
|
23729 | }
|
23730 |
|
23731 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this), 1);
|
23732 | }
|
23733 | |
23734 |
|
23735 |
|
23736 |
|
23737 |
|
23738 |
|
23739 |
|
23740 |
|
23741 | monitorBufferTick_() {
|
23742 | if (this.state === 'READY') {
|
23743 | this.fillBuffer_();
|
23744 | }
|
23745 |
|
23746 | if (this.checkBufferTimeout_) {
|
23747 | window.clearTimeout(this.checkBufferTimeout_);
|
23748 | }
|
23749 |
|
23750 | this.checkBufferTimeout_ = window.setTimeout(this.monitorBufferTick_.bind(this), CHECK_BUFFER_DELAY);
|
23751 | }
|
23752 | |
23753 |
|
23754 |
|
23755 |
|
23756 |
|
23757 |
|
23758 |
|
23759 |
|
23760 |
|
23761 |
|
23762 |
|
23763 | fillBuffer_() {
|
23764 |
|
23765 |
|
23766 | if (this.sourceUpdater_.updating()) {
|
23767 | return;
|
23768 | }
|
23769 |
|
23770 |
|
23771 | const segmentInfo = this.chooseNextRequest_();
|
23772 |
|
23773 | if (!segmentInfo) {
|
23774 | return;
|
23775 | }
|
23776 |
|
23777 | if (typeof segmentInfo.timestampOffset === 'number') {
|
23778 | this.isPendingTimestampOffset_ = false;
|
23779 | this.timelineChangeController_.pendingTimelineChange({
|
23780 | type: this.loaderType_,
|
23781 | from: this.currentTimeline_,
|
23782 | to: segmentInfo.timeline
|
23783 | });
|
23784 | }
|
23785 |
|
23786 | this.loadSegment_(segmentInfo);
|
23787 | }
|
23788 | |
23789 |
|
23790 |
|
23791 |
|
23792 |
|
23793 |
|
23794 |
|
23795 |
|
23796 |
|
23797 |
|
23798 |
|
23799 | isEndOfStream_(mediaIndex = this.mediaIndex, playlist = this.playlist_, partIndex = this.partIndex) {
|
23800 | if (!playlist || !this.mediaSource_) {
|
23801 | return false;
|
23802 | }
|
23803 |
|
23804 | const segment = typeof mediaIndex === 'number' && playlist.segments[mediaIndex];
|
23805 |
|
23806 | const appendedLastSegment = mediaIndex + 1 === playlist.segments.length;
|
23807 |
|
23808 | const appendedLastPart = !segment || !segment.parts || partIndex + 1 === segment.parts.length;
|
23809 |
|
23810 |
|
23811 |
|
23812 | return playlist.endList && this.mediaSource_.readyState === 'open' && appendedLastSegment && appendedLastPart;
|
23813 | }
|
23814 | |
23815 |
|
23816 |
|
23817 |
|
23818 |
|
23819 |
|
23820 |
|
23821 | chooseNextRequest_() {
|
23822 | const buffered = this.buffered_();
|
23823 | const bufferedEnd = lastBufferedEnd(buffered) || 0;
|
23824 | const bufferedTime = timeAheadOf(buffered, this.currentTime_());
|
23825 | const preloaded = !this.hasPlayed_() && bufferedTime >= 1;
|
23826 | const haveEnoughBuffer = bufferedTime >= this.goalBufferLength_();
|
23827 | const segments = this.playlist_.segments;
|
23828 |
|
23829 |
|
23830 |
|
23831 |
|
23832 | if (!segments.length || preloaded || haveEnoughBuffer) {
|
23833 | return null;
|
23834 | }
|
23835 |
|
23836 | this.syncPoint_ = this.syncPoint_ || this.syncController_.getSyncPoint(this.playlist_, this.duration_(), this.currentTimeline_, this.currentTime_(), this.loaderType_);
|
23837 | const next = {
|
23838 | partIndex: null,
|
23839 | mediaIndex: null,
|
23840 | startOfSegment: null,
|
23841 | playlist: this.playlist_,
|
23842 | isSyncRequest: Boolean(!this.syncPoint_)
|
23843 | };
|
23844 |
|
23845 | if (next.isSyncRequest) {
|
23846 | next.mediaIndex = getSyncSegmentCandidate(this.currentTimeline_, segments, bufferedEnd);
|
23847 | this.logger_(`choose next request. Can not find sync point. Fallback to media Index: ${next.mediaIndex}`);
|
23848 | } else if (this.mediaIndex !== null) {
|
23849 | const segment = segments[this.mediaIndex];
|
23850 | const partIndex = typeof this.partIndex === 'number' ? this.partIndex : -1;
|
23851 | next.startOfSegment = segment.end ? segment.end : bufferedEnd;
|
23852 |
|
23853 | if (segment.parts && segment.parts[partIndex + 1]) {
|
23854 | next.mediaIndex = this.mediaIndex;
|
23855 | next.partIndex = partIndex + 1;
|
23856 | } else {
|
23857 | next.mediaIndex = this.mediaIndex + 1;
|
23858 | }
|
23859 | } else {
|
23860 |
|
23861 | const {
|
23862 | segmentIndex,
|
23863 | startTime,
|
23864 | partIndex
|
23865 | } = Playlist.getMediaInfoForTime({
|
23866 | exactManifestTimings: this.exactManifestTimings,
|
23867 | playlist: this.playlist_,
|
23868 | currentTime: this.fetchAtBuffer_ ? bufferedEnd : this.currentTime_(),
|
23869 | startingPartIndex: this.syncPoint_.partIndex,
|
23870 | startingSegmentIndex: this.syncPoint_.segmentIndex,
|
23871 | startTime: this.syncPoint_.time
|
23872 | });
|
23873 | next.getMediaInfoForTime = this.fetchAtBuffer_ ? `bufferedEnd ${bufferedEnd}` : `currentTime ${this.currentTime_()}`;
|
23874 | next.mediaIndex = segmentIndex;
|
23875 | next.startOfSegment = startTime;
|
23876 | next.partIndex = partIndex;
|
23877 | this.logger_(`choose next request. Playlist switched and we have a sync point. Media Index: ${next.mediaIndex} `);
|
23878 | }
|
23879 |
|
23880 | const nextSegment = segments[next.mediaIndex];
|
23881 | let nextPart = nextSegment && typeof next.partIndex === 'number' && nextSegment.parts && nextSegment.parts[next.partIndex];
|
23882 |
|
23883 |
|
23884 | if (!nextSegment || typeof next.partIndex === 'number' && !nextPart) {
|
23885 | return null;
|
23886 | }
|
23887 |
|
23888 |
|
23889 |
|
23890 | if (typeof next.partIndex !== 'number' && nextSegment.parts) {
|
23891 | next.partIndex = 0;
|
23892 | nextPart = nextSegment.parts[0];
|
23893 | }
|
23894 |
|
23895 |
|
23896 |
|
23897 |
|
23898 | const hasIndependentSegments = this.vhs_.playlists && this.vhs_.playlists.main && this.vhs_.playlists.main.independentSegments || this.playlist_.independentSegments;
|
23899 |
|
23900 |
|
23901 |
|
23902 |
|
23903 | if (!bufferedTime && nextPart && !hasIndependentSegments && !nextPart.independent) {
|
23904 | if (next.partIndex === 0) {
|
23905 | const lastSegment = segments[next.mediaIndex - 1];
|
23906 | const lastSegmentLastPart = lastSegment.parts && lastSegment.parts.length && lastSegment.parts[lastSegment.parts.length - 1];
|
23907 |
|
23908 | if (lastSegmentLastPart && lastSegmentLastPart.independent) {
|
23909 | next.mediaIndex -= 1;
|
23910 | next.partIndex = lastSegment.parts.length - 1;
|
23911 | next.independent = 'previous segment';
|
23912 | }
|
23913 | } else if (nextSegment.parts[next.partIndex - 1].independent) {
|
23914 | next.partIndex -= 1;
|
23915 | next.independent = 'previous part';
|
23916 | }
|
23917 | }
|
23918 |
|
23919 | const ended = this.mediaSource_ && this.mediaSource_.readyState === 'ended';
|
23920 |
|
23921 |
|
23922 |
|
23923 |
|
23924 | if (next.mediaIndex >= segments.length - 1 && ended && !this.seeking_()) {
|
23925 | return null;
|
23926 | }
|
23927 |
|
23928 | if (this.shouldForceTimestampOffsetAfterResync_) {
|
23929 | this.shouldForceTimestampOffsetAfterResync_ = false;
|
23930 | next.forceTimestampOffset = true;
|
23931 | this.logger_('choose next request. Force timestamp offset after loader resync');
|
23932 | }
|
23933 |
|
23934 | return this.generateSegmentInfo_(next);
|
23935 | }
|
23936 |
|
23937 | generateSegmentInfo_(options) {
|
23938 | const {
|
23939 | independent,
|
23940 | playlist,
|
23941 | mediaIndex,
|
23942 | startOfSegment,
|
23943 | isSyncRequest,
|
23944 | partIndex,
|
23945 | forceTimestampOffset,
|
23946 | getMediaInfoForTime
|
23947 | } = options;
|
23948 | const segment = playlist.segments[mediaIndex];
|
23949 | const part = typeof partIndex === 'number' && segment.parts[partIndex];
|
23950 | const segmentInfo = {
|
23951 | requestId: 'segment-loader-' + Math.random(),
|
23952 |
|
23953 | uri: part && part.resolvedUri || segment.resolvedUri,
|
23954 |
|
23955 | mediaIndex,
|
23956 | partIndex: part ? partIndex : null,
|
23957 |
|
23958 |
|
23959 | isSyncRequest,
|
23960 | startOfSegment,
|
23961 |
|
23962 | playlist,
|
23963 |
|
23964 | bytes: null,
|
23965 |
|
23966 | encryptedBytes: null,
|
23967 |
|
23968 |
|
23969 | timestampOffset: null,
|
23970 |
|
23971 | timeline: segment.timeline,
|
23972 |
|
23973 | duration: part && part.duration || segment.duration,
|
23974 |
|
23975 | segment,
|
23976 | part,
|
23977 | byteLength: 0,
|
23978 | transmuxer: this.transmuxer_,
|
23979 |
|
23980 | getMediaInfoForTime,
|
23981 | independent
|
23982 | };
|
23983 | const overrideCheck = typeof forceTimestampOffset !== 'undefined' ? forceTimestampOffset : this.isPendingTimestampOffset_;
|
23984 | segmentInfo.timestampOffset = this.timestampOffsetForSegment_({
|
23985 | segmentTimeline: segment.timeline,
|
23986 | currentTimeline: this.currentTimeline_,
|
23987 | startOfSegment,
|
23988 | buffered: this.buffered_(),
|
23989 | overrideCheck
|
23990 | });
|
23991 | const audioBufferedEnd = lastBufferedEnd(this.sourceUpdater_.audioBuffered());
|
23992 |
|
23993 | if (typeof audioBufferedEnd === 'number') {
|
23994 |
|
23995 |
|
23996 | segmentInfo.audioAppendStart = audioBufferedEnd - this.sourceUpdater_.audioTimestampOffset();
|
23997 | }
|
23998 |
|
23999 | if (this.sourceUpdater_.videoBuffered().length) {
|
24000 | segmentInfo.gopsToAlignWith = gopsSafeToAlignWith(this.gopBuffer_,
|
24001 |
|
24002 | this.currentTime_() - this.sourceUpdater_.videoTimestampOffset(), this.timeMapping_);
|
24003 | }
|
24004 |
|
24005 | return segmentInfo;
|
24006 | }
|
24007 |
|
24008 |
|
24009 |
|
24010 |
|
24011 | timestampOffsetForSegment_(options) {
|
24012 | return timestampOffsetForSegment(options);
|
24013 | }
|
24014 | |
24015 |
|
24016 |
|
24017 |
|
24018 |
|
24019 |
|
24020 |
|
24021 |
|
24022 |
|
24023 |
|
24024 |
|
24025 | earlyAbortWhenNeeded_(stats) {
|
24026 | if (this.vhs_.tech_.paused() ||
|
24027 |
|
24028 |
|
24029 | !this.xhrOptions_.timeout ||
|
24030 | !this.playlist_.attributes.BANDWIDTH) {
|
24031 | return;
|
24032 | }
|
24033 |
|
24034 |
|
24035 |
|
24036 |
|
24037 | if (Date.now() - (stats.firstBytesReceivedAt || Date.now()) < 1000) {
|
24038 | return;
|
24039 | }
|
24040 |
|
24041 | const currentTime = this.currentTime_();
|
24042 | const measuredBandwidth = stats.bandwidth;
|
24043 | const segmentDuration = this.pendingSegment_.duration;
|
24044 | const requestTimeRemaining = Playlist.estimateSegmentRequestTime(segmentDuration, measuredBandwidth, this.playlist_, stats.bytesReceived);
|
24045 |
|
24046 |
|
24047 |
|
24048 | const timeUntilRebuffer$1 = timeUntilRebuffer(this.buffered_(), currentTime, this.vhs_.tech_.playbackRate()) - 1;
|
24049 |
|
24050 |
|
24051 | if (requestTimeRemaining <= timeUntilRebuffer$1) {
|
24052 | return;
|
24053 | }
|
24054 |
|
24055 | const switchCandidate = minRebufferMaxBandwidthSelector({
|
24056 | main: this.vhs_.playlists.main,
|
24057 | currentTime,
|
24058 | bandwidth: measuredBandwidth,
|
24059 | duration: this.duration_(),
|
24060 | segmentDuration,
|
24061 | timeUntilRebuffer: timeUntilRebuffer$1,
|
24062 | currentTimeline: this.currentTimeline_,
|
24063 | syncController: this.syncController_
|
24064 | });
|
24065 |
|
24066 | if (!switchCandidate) {
|
24067 | return;
|
24068 | }
|
24069 |
|
24070 | const rebufferingImpact = requestTimeRemaining - timeUntilRebuffer$1;
|
24071 | const timeSavedBySwitching = rebufferingImpact - switchCandidate.rebufferingImpact;
|
24072 | let minimumTimeSaving = 0.5;
|
24073 |
|
24074 |
|
24075 |
|
24076 | if (timeUntilRebuffer$1 <= TIME_FUDGE_FACTOR) {
|
24077 | minimumTimeSaving = 1;
|
24078 | }
|
24079 |
|
24080 | if (!switchCandidate.playlist || switchCandidate.playlist.uri === this.playlist_.uri || timeSavedBySwitching < minimumTimeSaving) {
|
24081 | return;
|
24082 | }
|
24083 |
|
24084 |
|
24085 |
|
24086 |
|
24087 | this.bandwidth = switchCandidate.playlist.attributes.BANDWIDTH * Config.BANDWIDTH_VARIANCE + 1;
|
24088 | this.trigger('earlyabort');
|
24089 | }
|
24090 |
|
24091 | handleAbort_(segmentInfo) {
|
24092 | this.logger_(`Aborting ${segmentInfoString(segmentInfo)}`);
|
24093 | this.mediaRequestsAborted += 1;
|
24094 | }
|
24095 | |
24096 |
|
24097 |
|
24098 |
|
24099 |
|
24100 |
|
24101 |
|
24102 |
|
24103 |
|
24104 |
|
24105 |
|
24106 | handleProgress_(event, simpleSegment) {
|
24107 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
24108 |
|
24109 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24110 | return;
|
24111 | }
|
24112 |
|
24113 | this.trigger('progress');
|
24114 | }
|
24115 |
|
24116 | handleTrackInfo_(simpleSegment, trackInfo) {
|
24117 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
24118 |
|
24119 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24120 | return;
|
24121 | }
|
24122 |
|
24123 | if (this.checkForIllegalMediaSwitch(trackInfo)) {
|
24124 | return;
|
24125 | }
|
24126 |
|
24127 | trackInfo = trackInfo || {};
|
24128 |
|
24129 |
|
24130 |
|
24131 | if (!shallowEqual(this.currentMediaInfo_, trackInfo)) {
|
24132 | this.appendInitSegment_ = {
|
24133 | audio: true,
|
24134 | video: true
|
24135 | };
|
24136 | this.startingMediaInfo_ = trackInfo;
|
24137 | this.currentMediaInfo_ = trackInfo;
|
24138 | this.logger_('trackinfo update', trackInfo);
|
24139 | this.trigger('trackinfo');
|
24140 | }
|
24141 |
|
24142 |
|
24143 |
|
24144 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24145 | return;
|
24146 | }
|
24147 |
|
24148 |
|
24149 |
|
24150 | this.pendingSegment_.trackInfo = trackInfo;
|
24151 |
|
24152 | if (this.hasEnoughInfoToAppend_()) {
|
24153 | this.processCallQueue_();
|
24154 | }
|
24155 | }
|
24156 |
|
24157 | handleTimingInfo_(simpleSegment, mediaType, timeType, time) {
|
24158 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
24159 |
|
24160 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24161 | return;
|
24162 | }
|
24163 |
|
24164 | const segmentInfo = this.pendingSegment_;
|
24165 | const timingInfoProperty = timingInfoPropertyForMedia(mediaType);
|
24166 | segmentInfo[timingInfoProperty] = segmentInfo[timingInfoProperty] || {};
|
24167 | segmentInfo[timingInfoProperty][timeType] = time;
|
24168 | this.logger_(`timinginfo: ${mediaType} - ${timeType} - ${time}`);
|
24169 |
|
24170 | if (this.hasEnoughInfoToAppend_()) {
|
24171 | this.processCallQueue_();
|
24172 | }
|
24173 | }
|
24174 |
|
24175 | handleCaptions_(simpleSegment, captionData) {
|
24176 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
24177 |
|
24178 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24179 | return;
|
24180 | }
|
24181 |
|
24182 |
|
24183 |
|
24184 | if (captionData.length === 0) {
|
24185 | this.logger_('SegmentLoader received no captions from a caption event');
|
24186 | return;
|
24187 | }
|
24188 |
|
24189 | const segmentInfo = this.pendingSegment_;
|
24190 |
|
24191 |
|
24192 | if (!segmentInfo.hasAppendedData_) {
|
24193 | this.metadataQueue_.caption.push(this.handleCaptions_.bind(this, simpleSegment, captionData));
|
24194 | return;
|
24195 | }
|
24196 |
|
24197 | const timestampOffset = this.sourceUpdater_.videoTimestampOffset() === null ? this.sourceUpdater_.audioTimestampOffset() : this.sourceUpdater_.videoTimestampOffset();
|
24198 | const captionTracks = {};
|
24199 |
|
24200 | captionData.forEach(caption => {
|
24201 |
|
24202 |
|
24203 | captionTracks[caption.stream] = captionTracks[caption.stream] || {
|
24204 |
|
24205 | startTime: Infinity,
|
24206 | captions: [],
|
24207 |
|
24208 | endTime: 0
|
24209 | };
|
24210 | const captionTrack = captionTracks[caption.stream];
|
24211 | captionTrack.startTime = Math.min(captionTrack.startTime, caption.startTime + timestampOffset);
|
24212 | captionTrack.endTime = Math.max(captionTrack.endTime, caption.endTime + timestampOffset);
|
24213 | captionTrack.captions.push(caption);
|
24214 | });
|
24215 | Object.keys(captionTracks).forEach(trackName => {
|
24216 | const {
|
24217 | startTime,
|
24218 | endTime,
|
24219 | captions
|
24220 | } = captionTracks[trackName];
|
24221 | const inbandTextTracks = this.inbandTextTracks_;
|
24222 | this.logger_(`adding cues from ${startTime} -> ${endTime} for ${trackName}`);
|
24223 | createCaptionsTrackIfNotExists(inbandTextTracks, this.vhs_.tech_, trackName);
|
24224 |
|
24225 |
|
24226 |
|
24227 |
|
24228 | removeCuesFromTrack(startTime, endTime, inbandTextTracks[trackName]);
|
24229 | addCaptionData({
|
24230 | captionArray: captions,
|
24231 | inbandTextTracks,
|
24232 | timestampOffset
|
24233 | });
|
24234 | });
|
24235 |
|
24236 |
|
24237 | if (this.transmuxer_) {
|
24238 | this.transmuxer_.postMessage({
|
24239 | action: 'clearParsedMp4Captions'
|
24240 | });
|
24241 | }
|
24242 | }
|
24243 |
|
24244 | handleId3_(simpleSegment, id3Frames, dispatchType) {
|
24245 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
24246 |
|
24247 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24248 | return;
|
24249 | }
|
24250 |
|
24251 | const segmentInfo = this.pendingSegment_;
|
24252 |
|
24253 | if (!segmentInfo.hasAppendedData_) {
|
24254 | this.metadataQueue_.id3.push(this.handleId3_.bind(this, simpleSegment, id3Frames, dispatchType));
|
24255 | return;
|
24256 | }
|
24257 |
|
24258 | this.addMetadataToTextTrack(dispatchType, id3Frames, this.duration_());
|
24259 | }
|
24260 |
|
24261 | processMetadataQueue_() {
|
24262 | this.metadataQueue_.id3.forEach(fn => fn());
|
24263 | this.metadataQueue_.caption.forEach(fn => fn());
|
24264 | this.metadataQueue_.id3 = [];
|
24265 | this.metadataQueue_.caption = [];
|
24266 | }
|
24267 |
|
24268 | processCallQueue_() {
|
24269 | const callQueue = this.callQueue_;
|
24270 |
|
24271 |
|
24272 |
|
24273 | this.callQueue_ = [];
|
24274 | callQueue.forEach(fun => fun());
|
24275 | }
|
24276 |
|
24277 | processLoadQueue_() {
|
24278 | const loadQueue = this.loadQueue_;
|
24279 |
|
24280 |
|
24281 |
|
24282 | this.loadQueue_ = [];
|
24283 | loadQueue.forEach(fun => fun());
|
24284 | }
|
24285 | |
24286 |
|
24287 |
|
24288 |
|
24289 |
|
24290 |
|
24291 |
|
24292 |
|
24293 | hasEnoughInfoToLoad_() {
|
24294 |
|
24295 |
|
24296 | if (this.loaderType_ !== 'audio') {
|
24297 | return true;
|
24298 | }
|
24299 |
|
24300 | const segmentInfo = this.pendingSegment_;
|
24301 |
|
24302 |
|
24303 | if (!segmentInfo) {
|
24304 | return false;
|
24305 | }
|
24306 |
|
24307 |
|
24308 |
|
24309 |
|
24310 |
|
24311 | if (!this.getCurrentMediaInfo_()) {
|
24312 | return true;
|
24313 | }
|
24314 |
|
24315 | if (
|
24316 |
|
24317 |
|
24318 |
|
24319 |
|
24320 |
|
24321 |
|
24322 |
|
24323 |
|
24324 |
|
24325 |
|
24326 |
|
24327 |
|
24328 |
|
24329 |
|
24330 | shouldWaitForTimelineChange({
|
24331 | timelineChangeController: this.timelineChangeController_,
|
24332 | currentTimeline: this.currentTimeline_,
|
24333 | segmentTimeline: segmentInfo.timeline,
|
24334 | loaderType: this.loaderType_,
|
24335 | audioDisabled: this.audioDisabled_
|
24336 | })) {
|
24337 | return false;
|
24338 | }
|
24339 |
|
24340 | return true;
|
24341 | }
|
24342 |
|
24343 | getCurrentMediaInfo_(segmentInfo = this.pendingSegment_) {
|
24344 | return segmentInfo && segmentInfo.trackInfo || this.currentMediaInfo_;
|
24345 | }
|
24346 |
|
24347 | getMediaInfo_(segmentInfo = this.pendingSegment_) {
|
24348 | return this.getCurrentMediaInfo_(segmentInfo) || this.startingMediaInfo_;
|
24349 | }
|
24350 |
|
24351 | getPendingSegmentPlaylist() {
|
24352 | return this.pendingSegment_ ? this.pendingSegment_.playlist : null;
|
24353 | }
|
24354 |
|
24355 | hasEnoughInfoToAppend_() {
|
24356 | if (!this.sourceUpdater_.ready()) {
|
24357 | return false;
|
24358 | }
|
24359 |
|
24360 |
|
24361 |
|
24362 | if (this.waitingOnRemove_ || this.quotaExceededErrorRetryTimeout_) {
|
24363 | return false;
|
24364 | }
|
24365 |
|
24366 | const segmentInfo = this.pendingSegment_;
|
24367 | const trackInfo = this.getCurrentMediaInfo_();
|
24368 |
|
24369 |
|
24370 |
|
24371 | if (!segmentInfo || !trackInfo) {
|
24372 | return false;
|
24373 | }
|
24374 |
|
24375 | const {
|
24376 | hasAudio,
|
24377 | hasVideo,
|
24378 | isMuxed
|
24379 | } = trackInfo;
|
24380 |
|
24381 | if (hasVideo && !segmentInfo.videoTimingInfo) {
|
24382 | return false;
|
24383 | }
|
24384 |
|
24385 |
|
24386 | if (hasAudio && !this.audioDisabled_ && !isMuxed && !segmentInfo.audioTimingInfo) {
|
24387 | return false;
|
24388 | }
|
24389 |
|
24390 | if (shouldWaitForTimelineChange({
|
24391 | timelineChangeController: this.timelineChangeController_,
|
24392 | currentTimeline: this.currentTimeline_,
|
24393 | segmentTimeline: segmentInfo.timeline,
|
24394 | loaderType: this.loaderType_,
|
24395 | audioDisabled: this.audioDisabled_
|
24396 | })) {
|
24397 | return false;
|
24398 | }
|
24399 |
|
24400 | return true;
|
24401 | }
|
24402 |
|
24403 | handleData_(simpleSegment, result) {
|
24404 | this.earlyAbortWhenNeeded_(simpleSegment.stats);
|
24405 |
|
24406 | if (this.checkForAbort_(simpleSegment.requestId)) {
|
24407 | return;
|
24408 | }
|
24409 |
|
24410 |
|
24411 |
|
24412 | if (this.callQueue_.length || !this.hasEnoughInfoToAppend_()) {
|
24413 | this.callQueue_.push(this.handleData_.bind(this, simpleSegment, result));
|
24414 | return;
|
24415 | }
|
24416 |
|
24417 | const segmentInfo = this.pendingSegment_;
|
24418 |
|
24419 | this.setTimeMapping_(segmentInfo.timeline);
|
24420 |
|
24421 | this.updateMediaSecondsLoaded_(segmentInfo.part || segmentInfo.segment);
|
24422 |
|
24423 |
|
24424 |
|
24425 |
|
24426 |
|
24427 | if (this.mediaSource_.readyState === 'closed') {
|
24428 | return;
|
24429 | }
|
24430 |
|
24431 |
|
24432 |
|
24433 | if (simpleSegment.map) {
|
24434 | simpleSegment.map = this.initSegmentForMap(simpleSegment.map, true);
|
24435 |
|
24436 | segmentInfo.segment.map = simpleSegment.map;
|
24437 | }
|
24438 |
|
24439 |
|
24440 | if (simpleSegment.key) {
|
24441 | this.segmentKey(simpleSegment.key, true);
|
24442 | }
|
24443 |
|
24444 | segmentInfo.isFmp4 = simpleSegment.isFmp4;
|
24445 | segmentInfo.timingInfo = segmentInfo.timingInfo || {};
|
24446 |
|
24447 | if (segmentInfo.isFmp4) {
|
24448 | this.trigger('fmp4');
|
24449 | segmentInfo.timingInfo.start = segmentInfo[timingInfoPropertyForMedia(result.type)].start;
|
24450 | } else {
|
24451 | const trackInfo = this.getCurrentMediaInfo_();
|
24452 | const useVideoTimingInfo = this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
|
24453 | let firstVideoFrameTimeForData;
|
24454 |
|
24455 | if (useVideoTimingInfo) {
|
24456 | firstVideoFrameTimeForData = segmentInfo.videoTimingInfo.start;
|
24457 | }
|
24458 |
|
24459 |
|
24460 |
|
24461 |
|
24462 | segmentInfo.timingInfo.start = this.trueSegmentStart_({
|
24463 | currentStart: segmentInfo.timingInfo.start,
|
24464 | playlist: segmentInfo.playlist,
|
24465 | mediaIndex: segmentInfo.mediaIndex,
|
24466 | currentVideoTimestampOffset: this.sourceUpdater_.videoTimestampOffset(),
|
24467 | useVideoTimingInfo,
|
24468 | firstVideoFrameTimeForData,
|
24469 | videoTimingInfo: segmentInfo.videoTimingInfo,
|
24470 | audioTimingInfo: segmentInfo.audioTimingInfo
|
24471 | });
|
24472 | }
|
24473 |
|
24474 |
|
24475 |
|
24476 |
|
24477 | this.updateAppendInitSegmentStatus(segmentInfo, result.type);
|
24478 |
|
24479 |
|
24480 |
|
24481 | this.updateSourceBufferTimestampOffset_(segmentInfo);
|
24482 |
|
24483 |
|
24484 | if (segmentInfo.isSyncRequest) {
|
24485 |
|
24486 |
|
24487 |
|
24488 | this.updateTimingInfoEnd_(segmentInfo);
|
24489 | this.syncController_.saveSegmentTimingInfo({
|
24490 | segmentInfo,
|
24491 | shouldSaveTimelineMapping: this.loaderType_ === 'main'
|
24492 | });
|
24493 | const next = this.chooseNextRequest_();
|
24494 |
|
24495 |
|
24496 | if (next.mediaIndex !== segmentInfo.mediaIndex || next.partIndex !== segmentInfo.partIndex) {
|
24497 | this.logger_('sync segment was incorrect, not appending');
|
24498 | return;
|
24499 | }
|
24500 |
|
24501 |
|
24502 | this.logger_('sync segment was correct, appending');
|
24503 | }
|
24504 |
|
24505 |
|
24506 |
|
24507 |
|
24508 |
|
24509 | segmentInfo.hasAppendedData_ = true;
|
24510 |
|
24511 | this.processMetadataQueue_();
|
24512 | this.appendData_(segmentInfo, result);
|
24513 | }
|
24514 |
|
24515 | updateAppendInitSegmentStatus(segmentInfo, type) {
|
24516 |
|
24517 | if (this.loaderType_ === 'main' && typeof segmentInfo.timestampOffset === 'number' &&
|
24518 |
|
24519 | !segmentInfo.changedTimestampOffset) {
|
24520 |
|
24521 |
|
24522 | this.appendInitSegment_ = {
|
24523 | audio: true,
|
24524 | video: true
|
24525 | };
|
24526 | }
|
24527 |
|
24528 | if (this.playlistOfLastInitSegment_[type] !== segmentInfo.playlist) {
|
24529 |
|
24530 |
|
24531 | this.appendInitSegment_[type] = true;
|
24532 | }
|
24533 | }
|
24534 |
|
24535 | getInitSegmentAndUpdateState_({
|
24536 | type,
|
24537 | initSegment,
|
24538 | map,
|
24539 | playlist
|
24540 | }) {
|
24541 |
|
24542 |
|
24543 |
|
24544 |
|
24545 |
|
24546 | if (map) {
|
24547 | const id = initSegmentId(map);
|
24548 |
|
24549 | if (this.activeInitSegmentId_ === id) {
|
24550 |
|
24551 | return null;
|
24552 | }
|
24553 |
|
24554 |
|
24555 |
|
24556 |
|
24557 |
|
24558 | initSegment = this.initSegmentForMap(map, true).bytes;
|
24559 | this.activeInitSegmentId_ = id;
|
24560 | }
|
24561 |
|
24562 |
|
24563 |
|
24564 |
|
24565 |
|
24566 |
|
24567 | if (initSegment && this.appendInitSegment_[type]) {
|
24568 |
|
24569 |
|
24570 |
|
24571 | this.playlistOfLastInitSegment_[type] = playlist;
|
24572 |
|
24573 | this.appendInitSegment_[type] = false;
|
24574 |
|
24575 |
|
24576 | this.activeInitSegmentId_ = null;
|
24577 | return initSegment;
|
24578 | }
|
24579 |
|
24580 | return null;
|
24581 | }
|
24582 |
|
24583 | handleQuotaExceededError_({
|
24584 | segmentInfo,
|
24585 | type,
|
24586 | bytes
|
24587 | }, error) {
|
24588 | const audioBuffered = this.sourceUpdater_.audioBuffered();
|
24589 | const videoBuffered = this.sourceUpdater_.videoBuffered();
|
24590 |
|
24591 |
|
24592 |
|
24593 | if (audioBuffered.length > 1) {
|
24594 | this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the audio buffer: ' + timeRangesToArray(audioBuffered).join(', '));
|
24595 | }
|
24596 |
|
24597 | if (videoBuffered.length > 1) {
|
24598 | this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the video buffer: ' + timeRangesToArray(videoBuffered).join(', '));
|
24599 | }
|
24600 |
|
24601 | const audioBufferStart = audioBuffered.length ? audioBuffered.start(0) : 0;
|
24602 | const audioBufferEnd = audioBuffered.length ? audioBuffered.end(audioBuffered.length - 1) : 0;
|
24603 | const videoBufferStart = videoBuffered.length ? videoBuffered.start(0) : 0;
|
24604 | const videoBufferEnd = videoBuffered.length ? videoBuffered.end(videoBuffered.length - 1) : 0;
|
24605 |
|
24606 | if (audioBufferEnd - audioBufferStart <= MIN_BACK_BUFFER && videoBufferEnd - videoBufferStart <= MIN_BACK_BUFFER) {
|
24607 |
|
24608 |
|
24609 |
|
24610 |
|
24611 | 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(', ')}, `);
|
24612 | this.error({
|
24613 | message: 'Quota exceeded error with append of a single segment of content',
|
24614 | excludeUntil: Infinity
|
24615 | });
|
24616 | this.trigger('error');
|
24617 | return;
|
24618 | }
|
24619 |
|
24620 |
|
24621 |
|
24622 |
|
24623 |
|
24624 |
|
24625 |
|
24626 |
|
24627 |
|
24628 |
|
24629 |
|
24630 |
|
24631 |
|
24632 | this.waitingOnRemove_ = true;
|
24633 | this.callQueue_.push(this.appendToSourceBuffer_.bind(this, {
|
24634 | segmentInfo,
|
24635 | type,
|
24636 | bytes
|
24637 | }));
|
24638 | const currentTime = this.currentTime_();
|
24639 |
|
24640 |
|
24641 | const timeToRemoveUntil = currentTime - MIN_BACK_BUFFER;
|
24642 | this.logger_(`On QUOTA_EXCEEDED_ERR, removing audio/video from 0 to ${timeToRemoveUntil}`);
|
24643 | this.remove(0, timeToRemoveUntil, () => {
|
24644 | this.logger_(`On QUOTA_EXCEEDED_ERR, retrying append in ${MIN_BACK_BUFFER}s`);
|
24645 | this.waitingOnRemove_ = false;
|
24646 |
|
24647 |
|
24648 | this.quotaExceededErrorRetryTimeout_ = window.setTimeout(() => {
|
24649 | this.logger_('On QUOTA_EXCEEDED_ERR, re-processing call queue');
|
24650 | this.quotaExceededErrorRetryTimeout_ = null;
|
24651 | this.processCallQueue_();
|
24652 | }, MIN_BACK_BUFFER * 1000);
|
24653 | }, true);
|
24654 | }
|
24655 |
|
24656 | handleAppendError_({
|
24657 | segmentInfo,
|
24658 | type,
|
24659 | bytes
|
24660 | }, error) {
|
24661 |
|
24662 | if (!error) {
|
24663 | return;
|
24664 | }
|
24665 |
|
24666 | if (error.code === QUOTA_EXCEEDED_ERR) {
|
24667 | this.handleQuotaExceededError_({
|
24668 | segmentInfo,
|
24669 | type,
|
24670 | bytes
|
24671 | });
|
24672 |
|
24673 |
|
24674 | return;
|
24675 | }
|
24676 |
|
24677 | this.logger_('Received non QUOTA_EXCEEDED_ERR on append', error);
|
24678 | this.error(`${type} append of ${bytes.length}b failed for segment ` + `#${segmentInfo.mediaIndex} in playlist ${segmentInfo.playlist.id}`);
|
24679 |
|
24680 |
|
24681 |
|
24682 |
|
24683 |
|
24684 | this.trigger('appenderror');
|
24685 | }
|
24686 |
|
24687 | appendToSourceBuffer_({
|
24688 | segmentInfo,
|
24689 | type,
|
24690 | initSegment,
|
24691 | data,
|
24692 | bytes
|
24693 | }) {
|
24694 |
|
24695 | if (!bytes) {
|
24696 | const segments = [data];
|
24697 | let byteLength = data.byteLength;
|
24698 |
|
24699 | if (initSegment) {
|
24700 |
|
24701 |
|
24702 | segments.unshift(initSegment);
|
24703 | byteLength += initSegment.byteLength;
|
24704 | }
|
24705 |
|
24706 |
|
24707 |
|
24708 | bytes = concatSegments({
|
24709 | bytes: byteLength,
|
24710 | segments
|
24711 | });
|
24712 | }
|
24713 |
|
24714 | this.sourceUpdater_.appendBuffer({
|
24715 | segmentInfo,
|
24716 | type,
|
24717 | bytes
|
24718 | }, this.handleAppendError_.bind(this, {
|
24719 | segmentInfo,
|
24720 | type,
|
24721 | bytes
|
24722 | }));
|
24723 | }
|
24724 |
|
24725 | handleSegmentTimingInfo_(type, requestId, segmentTimingInfo) {
|
24726 | if (!this.pendingSegment_ || requestId !== this.pendingSegment_.requestId) {
|
24727 | return;
|
24728 | }
|
24729 |
|
24730 | const segment = this.pendingSegment_.segment;
|
24731 | const timingInfoProperty = `${type}TimingInfo`;
|
24732 |
|
24733 | if (!segment[timingInfoProperty]) {
|
24734 | segment[timingInfoProperty] = {};
|
24735 | }
|
24736 |
|
24737 | segment[timingInfoProperty].transmuxerPrependedSeconds = segmentTimingInfo.prependedContentDuration || 0;
|
24738 | segment[timingInfoProperty].transmuxedPresentationStart = segmentTimingInfo.start.presentation;
|
24739 | segment[timingInfoProperty].transmuxedDecodeStart = segmentTimingInfo.start.decode;
|
24740 | segment[timingInfoProperty].transmuxedPresentationEnd = segmentTimingInfo.end.presentation;
|
24741 | segment[timingInfoProperty].transmuxedDecodeEnd = segmentTimingInfo.end.decode;
|
24742 |
|
24743 | segment[timingInfoProperty].baseMediaDecodeTime = segmentTimingInfo.baseMediaDecodeTime;
|
24744 | }
|
24745 |
|
24746 | appendData_(segmentInfo, result) {
|
24747 | const {
|
24748 | type,
|
24749 | data
|
24750 | } = result;
|
24751 |
|
24752 | if (!data || !data.byteLength) {
|
24753 | return;
|
24754 | }
|
24755 |
|
24756 | if (type === 'audio' && this.audioDisabled_) {
|
24757 | return;
|
24758 | }
|
24759 |
|
24760 | const initSegment = this.getInitSegmentAndUpdateState_({
|
24761 | type,
|
24762 | initSegment: result.initSegment,
|
24763 | playlist: segmentInfo.playlist,
|
24764 | map: segmentInfo.isFmp4 ? segmentInfo.segment.map : null
|
24765 | });
|
24766 | this.appendToSourceBuffer_({
|
24767 | segmentInfo,
|
24768 | type,
|
24769 | initSegment,
|
24770 | data
|
24771 | });
|
24772 | }
|
24773 | |
24774 |
|
24775 |
|
24776 |
|
24777 |
|
24778 |
|
24779 |
|
24780 | loadSegment_(segmentInfo) {
|
24781 | this.state = 'WAITING';
|
24782 | this.pendingSegment_ = segmentInfo;
|
24783 | this.trimBackBuffer_(segmentInfo);
|
24784 |
|
24785 | if (typeof segmentInfo.timestampOffset === 'number') {
|
24786 | if (this.transmuxer_) {
|
24787 | this.transmuxer_.postMessage({
|
24788 | action: 'clearAllMp4Captions'
|
24789 | });
|
24790 | }
|
24791 | }
|
24792 |
|
24793 | if (!this.hasEnoughInfoToLoad_()) {
|
24794 | this.loadQueue_.push(() => {
|
24795 |
|
24796 |
|
24797 | const options = _extends({}, segmentInfo, {
|
24798 | forceTimestampOffset: true
|
24799 | });
|
24800 |
|
24801 | _extends(segmentInfo, this.generateSegmentInfo_(options));
|
24802 |
|
24803 | this.isPendingTimestampOffset_ = false;
|
24804 | this.updateTransmuxerAndRequestSegment_(segmentInfo);
|
24805 | });
|
24806 | return;
|
24807 | }
|
24808 |
|
24809 | this.updateTransmuxerAndRequestSegment_(segmentInfo);
|
24810 | }
|
24811 |
|
24812 | updateTransmuxerAndRequestSegment_(segmentInfo) {
|
24813 |
|
24814 |
|
24815 |
|
24816 |
|
24817 |
|
24818 | if (this.shouldUpdateTransmuxerTimestampOffset_(segmentInfo.timestampOffset)) {
|
24819 | this.gopBuffer_.length = 0;
|
24820 |
|
24821 | segmentInfo.gopsToAlignWith = [];
|
24822 | this.timeMapping_ = 0;
|
24823 |
|
24824 | this.transmuxer_.postMessage({
|
24825 | action: 'reset'
|
24826 | });
|
24827 | this.transmuxer_.postMessage({
|
24828 | action: 'setTimestampOffset',
|
24829 | timestampOffset: segmentInfo.timestampOffset
|
24830 | });
|
24831 | }
|
24832 |
|
24833 | const simpleSegment = this.createSimplifiedSegmentObj_(segmentInfo);
|
24834 | const isEndOfStream = this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex);
|
24835 | const isWalkingForward = this.mediaIndex !== null;
|
24836 | const isDiscontinuity = segmentInfo.timeline !== this.currentTimeline_ &&
|
24837 |
|
24838 | segmentInfo.timeline > 0;
|
24839 | const isEndOfTimeline = isEndOfStream || isWalkingForward && isDiscontinuity;
|
24840 | this.logger_(`Requesting ${segmentInfoString(segmentInfo)}`);
|
24841 |
|
24842 |
|
24843 |
|
24844 |
|
24845 |
|
24846 | if (simpleSegment.map && !simpleSegment.map.bytes) {
|
24847 | this.logger_('going to request init segment.');
|
24848 | this.appendInitSegment_ = {
|
24849 | video: true,
|
24850 | audio: true
|
24851 | };
|
24852 | }
|
24853 |
|
24854 | segmentInfo.abortRequests = mediaSegmentRequest({
|
24855 | xhr: this.vhs_.xhr,
|
24856 | xhrOptions: this.xhrOptions_,
|
24857 | decryptionWorker: this.decrypter_,
|
24858 | segment: simpleSegment,
|
24859 | abortFn: this.handleAbort_.bind(this, segmentInfo),
|
24860 | progressFn: this.handleProgress_.bind(this),
|
24861 | trackInfoFn: this.handleTrackInfo_.bind(this),
|
24862 | timingInfoFn: this.handleTimingInfo_.bind(this),
|
24863 | videoSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'video', segmentInfo.requestId),
|
24864 | audioSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'audio', segmentInfo.requestId),
|
24865 | captionsFn: this.handleCaptions_.bind(this),
|
24866 | isEndOfTimeline,
|
24867 | endedTimelineFn: () => {
|
24868 | this.logger_('received endedtimeline callback');
|
24869 | },
|
24870 | id3Fn: this.handleId3_.bind(this),
|
24871 | dataFn: this.handleData_.bind(this),
|
24872 | doneFn: this.segmentRequestFinished_.bind(this),
|
24873 | onTransmuxerLog: ({
|
24874 | message,
|
24875 | level,
|
24876 | stream
|
24877 | }) => {
|
24878 | this.logger_(`${segmentInfoString(segmentInfo)} logged from transmuxer stream ${stream} as a ${level}: ${message}`);
|
24879 | }
|
24880 | });
|
24881 | }
|
24882 | |
24883 |
|
24884 |
|
24885 |
|
24886 |
|
24887 |
|
24888 |
|
24889 |
|
24890 |
|
24891 |
|
24892 | trimBackBuffer_(segmentInfo) {
|
24893 | const removeToTime = safeBackBufferTrimTime(this.seekable_(), this.currentTime_(), this.playlist_.targetDuration || 10);
|
24894 |
|
24895 |
|
24896 |
|
24897 |
|
24898 |
|
24899 | if (removeToTime > 0) {
|
24900 | this.remove(0, removeToTime);
|
24901 | }
|
24902 | }
|
24903 | |
24904 |
|
24905 |
|
24906 |
|
24907 |
|
24908 |
|
24909 |
|
24910 |
|
24911 |
|
24912 |
|
24913 |
|
24914 | createSimplifiedSegmentObj_(segmentInfo) {
|
24915 | const segment = segmentInfo.segment;
|
24916 | const part = segmentInfo.part;
|
24917 | const simpleSegment = {
|
24918 | resolvedUri: part ? part.resolvedUri : segment.resolvedUri,
|
24919 | byterange: part ? part.byterange : segment.byterange,
|
24920 | requestId: segmentInfo.requestId,
|
24921 | transmuxer: segmentInfo.transmuxer,
|
24922 | audioAppendStart: segmentInfo.audioAppendStart,
|
24923 | gopsToAlignWith: segmentInfo.gopsToAlignWith,
|
24924 | part: segmentInfo.part
|
24925 | };
|
24926 | const previousSegment = segmentInfo.playlist.segments[segmentInfo.mediaIndex - 1];
|
24927 |
|
24928 | if (previousSegment && previousSegment.timeline === segment.timeline) {
|
24929 |
|
24930 |
|
24931 |
|
24932 |
|
24933 |
|
24934 |
|
24935 |
|
24936 |
|
24937 | if (previousSegment.videoTimingInfo) {
|
24938 | simpleSegment.baseStartTime = previousSegment.videoTimingInfo.transmuxedDecodeEnd;
|
24939 | } else if (previousSegment.audioTimingInfo) {
|
24940 | simpleSegment.baseStartTime = previousSegment.audioTimingInfo.transmuxedDecodeEnd;
|
24941 | }
|
24942 | }
|
24943 |
|
24944 | if (segment.key) {
|
24945 |
|
24946 |
|
24947 | const iv = segment.key.iv || new Uint32Array([0, 0, 0, segmentInfo.mediaIndex + segmentInfo.playlist.mediaSequence]);
|
24948 | simpleSegment.key = this.segmentKey(segment.key);
|
24949 | simpleSegment.key.iv = iv;
|
24950 | }
|
24951 |
|
24952 | if (segment.map) {
|
24953 | simpleSegment.map = this.initSegmentForMap(segment.map);
|
24954 | }
|
24955 |
|
24956 | return simpleSegment;
|
24957 | }
|
24958 |
|
24959 | saveTransferStats_(stats) {
|
24960 |
|
24961 |
|
24962 | this.mediaRequests += 1;
|
24963 |
|
24964 | if (stats) {
|
24965 | this.mediaBytesTransferred += stats.bytesReceived;
|
24966 | this.mediaTransferDuration += stats.roundTripTime;
|
24967 | }
|
24968 | }
|
24969 |
|
24970 | saveBandwidthRelatedStats_(duration, stats) {
|
24971 |
|
24972 |
|
24973 |
|
24974 | this.pendingSegment_.byteLength = stats.bytesReceived;
|
24975 |
|
24976 | if (duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
|
24977 | this.logger_(`Ignoring segment's bandwidth because its duration of ${duration}` + ` is less than the min to record ${MIN_SEGMENT_DURATION_TO_SAVE_STATS}`);
|
24978 | return;
|
24979 | }
|
24980 |
|
24981 | this.bandwidth = stats.bandwidth;
|
24982 | this.roundTrip = stats.roundTripTime;
|
24983 | }
|
24984 |
|
24985 | handleTimeout_() {
|
24986 |
|
24987 |
|
24988 | this.mediaRequestsTimedout += 1;
|
24989 | this.bandwidth = 1;
|
24990 | this.roundTrip = NaN;
|
24991 | this.trigger('bandwidthupdate');
|
24992 | this.trigger('timeout');
|
24993 | }
|
24994 | |
24995 |
|
24996 |
|
24997 |
|
24998 |
|
24999 |
|
25000 |
|
25001 |
|
25002 | segmentRequestFinished_(error, simpleSegment, result) {
|
25003 |
|
25004 |
|
25005 |
|
25006 |
|
25007 | if (this.callQueue_.length) {
|
25008 | this.callQueue_.push(this.segmentRequestFinished_.bind(this, error, simpleSegment, result));
|
25009 | return;
|
25010 | }
|
25011 |
|
25012 | this.saveTransferStats_(simpleSegment.stats);
|
25013 |
|
25014 | if (!this.pendingSegment_) {
|
25015 | return;
|
25016 | }
|
25017 |
|
25018 |
|
25019 |
|
25020 |
|
25021 |
|
25022 | if (simpleSegment.requestId !== this.pendingSegment_.requestId) {
|
25023 | return;
|
25024 | }
|
25025 |
|
25026 |
|
25027 | if (error) {
|
25028 | this.pendingSegment_ = null;
|
25029 | this.state = 'READY';
|
25030 |
|
25031 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
25032 | return;
|
25033 | }
|
25034 |
|
25035 | this.pause();
|
25036 |
|
25037 |
|
25038 |
|
25039 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
25040 | this.handleTimeout_();
|
25041 | return;
|
25042 | }
|
25043 |
|
25044 |
|
25045 |
|
25046 | this.mediaRequestsErrored += 1;
|
25047 | this.error(error);
|
25048 | this.trigger('error');
|
25049 | return;
|
25050 | }
|
25051 |
|
25052 | const segmentInfo = this.pendingSegment_;
|
25053 |
|
25054 |
|
25055 | this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats);
|
25056 | segmentInfo.endOfAllRequests = simpleSegment.endOfAllRequests;
|
25057 |
|
25058 | if (result.gopInfo) {
|
25059 | this.gopBuffer_ = updateGopBuffer(this.gopBuffer_, result.gopInfo, this.safeAppend_);
|
25060 | }
|
25061 |
|
25062 |
|
25063 |
|
25064 | this.state = 'APPENDING';
|
25065 |
|
25066 | this.trigger('appending');
|
25067 | this.waitForAppendsToComplete_(segmentInfo);
|
25068 | }
|
25069 |
|
25070 | setTimeMapping_(timeline) {
|
25071 | const timelineMapping = this.syncController_.mappingForTimeline(timeline);
|
25072 |
|
25073 | if (timelineMapping !== null) {
|
25074 | this.timeMapping_ = timelineMapping;
|
25075 | }
|
25076 | }
|
25077 |
|
25078 | updateMediaSecondsLoaded_(segment) {
|
25079 | if (typeof segment.start === 'number' && typeof segment.end === 'number') {
|
25080 | this.mediaSecondsLoaded += segment.end - segment.start;
|
25081 | } else {
|
25082 | this.mediaSecondsLoaded += segment.duration;
|
25083 | }
|
25084 | }
|
25085 |
|
25086 | shouldUpdateTransmuxerTimestampOffset_(timestampOffset) {
|
25087 | if (timestampOffset === null) {
|
25088 | return false;
|
25089 | }
|
25090 |
|
25091 |
|
25092 |
|
25093 | if (this.loaderType_ === 'main' && timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
|
25094 | return true;
|
25095 | }
|
25096 |
|
25097 | if (!this.audioDisabled_ && timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
|
25098 | return true;
|
25099 | }
|
25100 |
|
25101 | return false;
|
25102 | }
|
25103 |
|
25104 | trueSegmentStart_({
|
25105 | currentStart,
|
25106 | playlist,
|
25107 | mediaIndex,
|
25108 | firstVideoFrameTimeForData,
|
25109 | currentVideoTimestampOffset,
|
25110 | useVideoTimingInfo,
|
25111 | videoTimingInfo,
|
25112 | audioTimingInfo
|
25113 | }) {
|
25114 | if (typeof currentStart !== 'undefined') {
|
25115 |
|
25116 | return currentStart;
|
25117 | }
|
25118 |
|
25119 | if (!useVideoTimingInfo) {
|
25120 | return audioTimingInfo.start;
|
25121 | }
|
25122 |
|
25123 | const previousSegment = playlist.segments[mediaIndex - 1];
|
25124 |
|
25125 |
|
25126 |
|
25127 |
|
25128 |
|
25129 | if (mediaIndex === 0 || !previousSegment || typeof previousSegment.start === 'undefined' || previousSegment.end !== firstVideoFrameTimeForData + currentVideoTimestampOffset) {
|
25130 | return firstVideoFrameTimeForData;
|
25131 | }
|
25132 |
|
25133 | return videoTimingInfo.start;
|
25134 | }
|
25135 |
|
25136 | waitForAppendsToComplete_(segmentInfo) {
|
25137 | const trackInfo = this.getCurrentMediaInfo_(segmentInfo);
|
25138 |
|
25139 | if (!trackInfo) {
|
25140 | this.error({
|
25141 | message: 'No starting media returned, likely due to an unsupported media format.',
|
25142 | playlistExclusionDuration: Infinity
|
25143 | });
|
25144 | this.trigger('error');
|
25145 | return;
|
25146 | }
|
25147 |
|
25148 |
|
25149 |
|
25150 |
|
25151 | const {
|
25152 | hasAudio,
|
25153 | hasVideo,
|
25154 | isMuxed
|
25155 | } = trackInfo;
|
25156 | const waitForVideo = this.loaderType_ === 'main' && hasVideo;
|
25157 | const waitForAudio = !this.audioDisabled_ && hasAudio && !isMuxed;
|
25158 | segmentInfo.waitingOnAppends = 0;
|
25159 |
|
25160 | if (!segmentInfo.hasAppendedData_) {
|
25161 | if (!segmentInfo.timingInfo && typeof segmentInfo.timestampOffset === 'number') {
|
25162 |
|
25163 |
|
25164 |
|
25165 |
|
25166 |
|
25167 |
|
25168 |
|
25169 |
|
25170 | this.isPendingTimestampOffset_ = true;
|
25171 | }
|
25172 |
|
25173 |
|
25174 | segmentInfo.timingInfo = {
|
25175 | start: 0
|
25176 | };
|
25177 | segmentInfo.waitingOnAppends++;
|
25178 |
|
25179 | if (!this.isPendingTimestampOffset_) {
|
25180 |
|
25181 | this.updateSourceBufferTimestampOffset_(segmentInfo);
|
25182 |
|
25183 |
|
25184 | this.processMetadataQueue_();
|
25185 | }
|
25186 |
|
25187 |
|
25188 | this.checkAppendsDone_(segmentInfo);
|
25189 | return;
|
25190 | }
|
25191 |
|
25192 |
|
25193 | if (waitForVideo) {
|
25194 | segmentInfo.waitingOnAppends++;
|
25195 | }
|
25196 |
|
25197 | if (waitForAudio) {
|
25198 | segmentInfo.waitingOnAppends++;
|
25199 | }
|
25200 |
|
25201 | if (waitForVideo) {
|
25202 | this.sourceUpdater_.videoQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
|
25203 | }
|
25204 |
|
25205 | if (waitForAudio) {
|
25206 | this.sourceUpdater_.audioQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
|
25207 | }
|
25208 | }
|
25209 |
|
25210 | checkAppendsDone_(segmentInfo) {
|
25211 | if (this.checkForAbort_(segmentInfo.requestId)) {
|
25212 | return;
|
25213 | }
|
25214 |
|
25215 | segmentInfo.waitingOnAppends--;
|
25216 |
|
25217 | if (segmentInfo.waitingOnAppends === 0) {
|
25218 | this.handleAppendsDone_();
|
25219 | }
|
25220 | }
|
25221 |
|
25222 | checkForIllegalMediaSwitch(trackInfo) {
|
25223 | const illegalMediaSwitchError = illegalMediaSwitch(this.loaderType_, this.getCurrentMediaInfo_(), trackInfo);
|
25224 |
|
25225 | if (illegalMediaSwitchError) {
|
25226 | this.error({
|
25227 | message: illegalMediaSwitchError,
|
25228 | playlistExclusionDuration: Infinity
|
25229 | });
|
25230 | this.trigger('error');
|
25231 | return true;
|
25232 | }
|
25233 |
|
25234 | return false;
|
25235 | }
|
25236 |
|
25237 | updateSourceBufferTimestampOffset_(segmentInfo) {
|
25238 | if (segmentInfo.timestampOffset === null ||
|
25239 |
|
25240 | typeof segmentInfo.timingInfo.start !== 'number' ||
|
25241 | segmentInfo.changedTimestampOffset ||
|
25242 | this.loaderType_ !== 'main') {
|
25243 | return;
|
25244 | }
|
25245 |
|
25246 | let didChange = false;
|
25247 |
|
25248 |
|
25249 |
|
25250 |
|
25251 | segmentInfo.timestampOffset -= this.getSegmentStartTimeForTimestampOffsetCalculation_({
|
25252 | videoTimingInfo: segmentInfo.segment.videoTimingInfo,
|
25253 | audioTimingInfo: segmentInfo.segment.audioTimingInfo,
|
25254 | timingInfo: segmentInfo.timingInfo
|
25255 | });
|
25256 |
|
25257 |
|
25258 |
|
25259 | segmentInfo.changedTimestampOffset = true;
|
25260 |
|
25261 | if (segmentInfo.timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
|
25262 | this.sourceUpdater_.videoTimestampOffset(segmentInfo.timestampOffset);
|
25263 | didChange = true;
|
25264 | }
|
25265 |
|
25266 | if (segmentInfo.timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
|
25267 | this.sourceUpdater_.audioTimestampOffset(segmentInfo.timestampOffset);
|
25268 | didChange = true;
|
25269 | }
|
25270 |
|
25271 | if (didChange) {
|
25272 | this.trigger('timestampoffset');
|
25273 | }
|
25274 | }
|
25275 |
|
25276 | getSegmentStartTimeForTimestampOffsetCalculation_({
|
25277 | videoTimingInfo,
|
25278 | audioTimingInfo,
|
25279 | timingInfo
|
25280 | }) {
|
25281 | if (!this.useDtsForTimestampOffset_) {
|
25282 | return timingInfo.start;
|
25283 | }
|
25284 |
|
25285 | if (videoTimingInfo && typeof videoTimingInfo.transmuxedDecodeStart === 'number') {
|
25286 | return videoTimingInfo.transmuxedDecodeStart;
|
25287 | }
|
25288 |
|
25289 |
|
25290 | if (audioTimingInfo && typeof audioTimingInfo.transmuxedDecodeStart === 'number') {
|
25291 | return audioTimingInfo.transmuxedDecodeStart;
|
25292 | }
|
25293 |
|
25294 |
|
25295 | return timingInfo.start;
|
25296 | }
|
25297 |
|
25298 | updateTimingInfoEnd_(segmentInfo) {
|
25299 | segmentInfo.timingInfo = segmentInfo.timingInfo || {};
|
25300 | const trackInfo = this.getMediaInfo_();
|
25301 | const useVideoTimingInfo = this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
|
25302 | const prioritizedTimingInfo = useVideoTimingInfo && segmentInfo.videoTimingInfo ? segmentInfo.videoTimingInfo : segmentInfo.audioTimingInfo;
|
25303 |
|
25304 | if (!prioritizedTimingInfo) {
|
25305 | return;
|
25306 | }
|
25307 |
|
25308 | segmentInfo.timingInfo.end = typeof prioritizedTimingInfo.end === 'number' ?
|
25309 |
|
25310 |
|
25311 | prioritizedTimingInfo.end : prioritizedTimingInfo.start + segmentInfo.duration;
|
25312 | }
|
25313 | |
25314 |
|
25315 |
|
25316 |
|
25317 |
|
25318 |
|
25319 |
|
25320 |
|
25321 |
|
25322 | handleAppendsDone_() {
|
25323 |
|
25324 | if (this.pendingSegment_) {
|
25325 | this.trigger('appendsdone');
|
25326 | }
|
25327 |
|
25328 | if (!this.pendingSegment_) {
|
25329 | this.state = 'READY';
|
25330 |
|
25331 |
|
25332 | if (!this.paused()) {
|
25333 | this.monitorBuffer_();
|
25334 | }
|
25335 |
|
25336 | return;
|
25337 | }
|
25338 |
|
25339 | const segmentInfo = this.pendingSegment_;
|
25340 |
|
25341 |
|
25342 |
|
25343 | this.updateTimingInfoEnd_(segmentInfo);
|
25344 |
|
25345 | if (this.shouldSaveSegmentTimingInfo_) {
|
25346 |
|
25347 |
|
25348 |
|
25349 |
|
25350 |
|
25351 |
|
25352 |
|
25353 |
|
25354 |
|
25355 |
|
25356 |
|
25357 |
|
25358 |
|
25359 |
|
25360 |
|
25361 |
|
25362 |
|
25363 | this.syncController_.saveSegmentTimingInfo({
|
25364 | segmentInfo,
|
25365 | shouldSaveTimelineMapping: this.loaderType_ === 'main'
|
25366 | });
|
25367 | }
|
25368 |
|
25369 | const segmentDurationMessage = getTroublesomeSegmentDurationMessage(segmentInfo, this.sourceType_);
|
25370 |
|
25371 | if (segmentDurationMessage) {
|
25372 | if (segmentDurationMessage.severity === 'warn') {
|
25373 | videojs__default["default"].log.warn(segmentDurationMessage.message);
|
25374 | } else {
|
25375 | this.logger_(segmentDurationMessage.message);
|
25376 | }
|
25377 | }
|
25378 |
|
25379 | this.recordThroughput_(segmentInfo);
|
25380 | this.pendingSegment_ = null;
|
25381 | this.state = 'READY';
|
25382 |
|
25383 | if (segmentInfo.isSyncRequest) {
|
25384 | this.trigger('syncinfoupdate');
|
25385 |
|
25386 |
|
25387 |
|
25388 |
|
25389 | if (!segmentInfo.hasAppendedData_) {
|
25390 | this.logger_(`Throwing away un-appended sync request ${segmentInfoString(segmentInfo)}`);
|
25391 | return;
|
25392 | }
|
25393 | }
|
25394 |
|
25395 | this.logger_(`Appended ${segmentInfoString(segmentInfo)}`);
|
25396 | this.addSegmentMetadataCue_(segmentInfo);
|
25397 | this.fetchAtBuffer_ = true;
|
25398 |
|
25399 | if (this.currentTimeline_ !== segmentInfo.timeline) {
|
25400 | this.timelineChangeController_.lastTimelineChange({
|
25401 | type: this.loaderType_,
|
25402 | from: this.currentTimeline_,
|
25403 | to: segmentInfo.timeline
|
25404 | });
|
25405 |
|
25406 |
|
25407 |
|
25408 | if (this.loaderType_ === 'main' && !this.audioDisabled_) {
|
25409 | this.timelineChangeController_.lastTimelineChange({
|
25410 | type: 'audio',
|
25411 | from: this.currentTimeline_,
|
25412 | to: segmentInfo.timeline
|
25413 | });
|
25414 | }
|
25415 | }
|
25416 |
|
25417 | this.currentTimeline_ = segmentInfo.timeline;
|
25418 |
|
25419 |
|
25420 |
|
25421 |
|
25422 | this.trigger('syncinfoupdate');
|
25423 | const segment = segmentInfo.segment;
|
25424 | const part = segmentInfo.part;
|
25425 | const badSegmentGuess = segment.end && this.currentTime_() - segment.end > segmentInfo.playlist.targetDuration * 3;
|
25426 | const badPartGuess = part && part.end && this.currentTime_() - part.end > segmentInfo.playlist.partTargetDuration * 3;
|
25427 |
|
25428 |
|
25429 |
|
25430 |
|
25431 | if (badSegmentGuess || badPartGuess) {
|
25432 | this.logger_(`bad ${badSegmentGuess ? 'segment' : 'part'} ${segmentInfoString(segmentInfo)}`);
|
25433 | this.resetEverything();
|
25434 | return;
|
25435 | }
|
25436 |
|
25437 | const isWalkingForward = this.mediaIndex !== null;
|
25438 |
|
25439 |
|
25440 | if (isWalkingForward) {
|
25441 | this.trigger('bandwidthupdate');
|
25442 | }
|
25443 |
|
25444 | this.trigger('progress');
|
25445 | this.mediaIndex = segmentInfo.mediaIndex;
|
25446 | this.partIndex = segmentInfo.partIndex;
|
25447 |
|
25448 |
|
25449 |
|
25450 | if (this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex)) {
|
25451 | this.endOfStream();
|
25452 | }
|
25453 |
|
25454 |
|
25455 | this.trigger('appended');
|
25456 |
|
25457 | if (segmentInfo.hasAppendedData_) {
|
25458 | this.mediaAppends++;
|
25459 | }
|
25460 |
|
25461 | if (!this.paused()) {
|
25462 | this.monitorBuffer_();
|
25463 | }
|
25464 | }
|
25465 | |
25466 |
|
25467 |
|
25468 |
|
25469 |
|
25470 |
|
25471 |
|
25472 |
|
25473 |
|
25474 |
|
25475 |
|
25476 | recordThroughput_(segmentInfo) {
|
25477 | if (segmentInfo.duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
|
25478 | 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}`);
|
25479 | return;
|
25480 | }
|
25481 |
|
25482 | const rate = this.throughput.rate;
|
25483 |
|
25484 |
|
25485 | const segmentProcessingTime = Date.now() - segmentInfo.endOfAllRequests + 1;
|
25486 |
|
25487 | const segmentProcessingThroughput = Math.floor(segmentInfo.byteLength / segmentProcessingTime * 8 * 1000);
|
25488 |
|
25489 |
|
25490 | this.throughput.rate += (segmentProcessingThroughput - rate) / ++this.throughput.count;
|
25491 | }
|
25492 | |
25493 |
|
25494 |
|
25495 |
|
25496 |
|
25497 |
|
25498 |
|
25499 |
|
25500 |
|
25501 |
|
25502 |
|
25503 | addSegmentMetadataCue_(segmentInfo) {
|
25504 | if (!this.segmentMetadataTrack_) {
|
25505 | return;
|
25506 | }
|
25507 |
|
25508 | const segment = segmentInfo.segment;
|
25509 | const start = segment.start;
|
25510 | const end = segment.end;
|
25511 |
|
25512 | if (!finite(start) || !finite(end)) {
|
25513 | return;
|
25514 | }
|
25515 |
|
25516 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
25517 | const Cue = window.WebKitDataCue || window.VTTCue;
|
25518 | const value = {
|
25519 | custom: segment.custom,
|
25520 | dateTimeObject: segment.dateTimeObject,
|
25521 | dateTimeString: segment.dateTimeString,
|
25522 | programDateTime: segment.programDateTime,
|
25523 | bandwidth: segmentInfo.playlist.attributes.BANDWIDTH,
|
25524 | resolution: segmentInfo.playlist.attributes.RESOLUTION,
|
25525 | codecs: segmentInfo.playlist.attributes.CODECS,
|
25526 | byteLength: segmentInfo.byteLength,
|
25527 | uri: segmentInfo.uri,
|
25528 | timeline: segmentInfo.timeline,
|
25529 | playlist: segmentInfo.playlist.id,
|
25530 | start,
|
25531 | end
|
25532 | };
|
25533 | const data = JSON.stringify(value);
|
25534 | const cue = new Cue(start, end, data);
|
25535 |
|
25536 |
|
25537 | cue.value = value;
|
25538 | this.segmentMetadataTrack_.addCue(cue);
|
25539 | }
|
25540 |
|
25541 | }
|
25542 |
|
25543 | function noop() {}
|
25544 |
|
25545 | const toTitleCase = function (string) {
|
25546 | if (typeof string !== 'string') {
|
25547 | return string;
|
25548 | }
|
25549 |
|
25550 | return string.replace(/./, w => w.toUpperCase());
|
25551 | };
|
25552 |
|
25553 | |
25554 |
|
25555 |
|
25556 | const bufferTypes = ['video', 'audio'];
|
25557 |
|
25558 | const updating = (type, sourceUpdater) => {
|
25559 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25560 | return sourceBuffer && sourceBuffer.updating || sourceUpdater.queuePending[type];
|
25561 | };
|
25562 |
|
25563 | const nextQueueIndexOfType = (type, queue) => {
|
25564 | for (let i = 0; i < queue.length; i++) {
|
25565 | const queueEntry = queue[i];
|
25566 |
|
25567 | if (queueEntry.type === 'mediaSource') {
|
25568 |
|
25569 |
|
25570 | return null;
|
25571 | }
|
25572 |
|
25573 | if (queueEntry.type === type) {
|
25574 | return i;
|
25575 | }
|
25576 | }
|
25577 |
|
25578 | return null;
|
25579 | };
|
25580 |
|
25581 | const shiftQueue = (type, sourceUpdater) => {
|
25582 | if (sourceUpdater.queue.length === 0) {
|
25583 | return;
|
25584 | }
|
25585 |
|
25586 | let queueIndex = 0;
|
25587 | let queueEntry = sourceUpdater.queue[queueIndex];
|
25588 |
|
25589 | if (queueEntry.type === 'mediaSource') {
|
25590 | if (!sourceUpdater.updating() && sourceUpdater.mediaSource.readyState !== 'closed') {
|
25591 | sourceUpdater.queue.shift();
|
25592 | queueEntry.action(sourceUpdater);
|
25593 |
|
25594 | if (queueEntry.doneFn) {
|
25595 | queueEntry.doneFn();
|
25596 | }
|
25597 |
|
25598 |
|
25599 |
|
25600 |
|
25601 | shiftQueue('audio', sourceUpdater);
|
25602 | shiftQueue('video', sourceUpdater);
|
25603 | }
|
25604 |
|
25605 |
|
25606 |
|
25607 |
|
25608 | return;
|
25609 | }
|
25610 |
|
25611 | if (type === 'mediaSource') {
|
25612 |
|
25613 |
|
25614 |
|
25615 |
|
25616 | return;
|
25617 | }
|
25618 |
|
25619 |
|
25620 |
|
25621 |
|
25622 | if (!sourceUpdater.ready() || sourceUpdater.mediaSource.readyState === 'closed' || updating(type, sourceUpdater)) {
|
25623 | return;
|
25624 | }
|
25625 |
|
25626 | if (queueEntry.type !== type) {
|
25627 | queueIndex = nextQueueIndexOfType(type, sourceUpdater.queue);
|
25628 |
|
25629 | if (queueIndex === null) {
|
25630 |
|
25631 |
|
25632 |
|
25633 | return;
|
25634 | }
|
25635 |
|
25636 | queueEntry = sourceUpdater.queue[queueIndex];
|
25637 | }
|
25638 |
|
25639 | sourceUpdater.queue.splice(queueIndex, 1);
|
25640 |
|
25641 |
|
25642 |
|
25643 |
|
25644 |
|
25645 |
|
25646 |
|
25647 | sourceUpdater.queuePending[type] = queueEntry;
|
25648 | queueEntry.action(type, sourceUpdater);
|
25649 |
|
25650 | if (!queueEntry.doneFn) {
|
25651 |
|
25652 | sourceUpdater.queuePending[type] = null;
|
25653 | shiftQueue(type, sourceUpdater);
|
25654 | return;
|
25655 | }
|
25656 | };
|
25657 |
|
25658 | const cleanupBuffer = (type, sourceUpdater) => {
|
25659 | const buffer = sourceUpdater[`${type}Buffer`];
|
25660 | const titleType = toTitleCase(type);
|
25661 |
|
25662 | if (!buffer) {
|
25663 | return;
|
25664 | }
|
25665 |
|
25666 | buffer.removeEventListener('updateend', sourceUpdater[`on${titleType}UpdateEnd_`]);
|
25667 | buffer.removeEventListener('error', sourceUpdater[`on${titleType}Error_`]);
|
25668 | sourceUpdater.codecs[type] = null;
|
25669 | sourceUpdater[`${type}Buffer`] = null;
|
25670 | };
|
25671 |
|
25672 | const inSourceBuffers = (mediaSource, sourceBuffer) => mediaSource && sourceBuffer && Array.prototype.indexOf.call(mediaSource.sourceBuffers, sourceBuffer) !== -1;
|
25673 |
|
25674 | const actions = {
|
25675 | appendBuffer: (bytes, segmentInfo, onError) => (type, sourceUpdater) => {
|
25676 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25677 |
|
25678 |
|
25679 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
25680 | return;
|
25681 | }
|
25682 |
|
25683 | sourceUpdater.logger_(`Appending segment ${segmentInfo.mediaIndex}'s ${bytes.length} bytes to ${type}Buffer`);
|
25684 |
|
25685 | try {
|
25686 | sourceBuffer.appendBuffer(bytes);
|
25687 | } catch (e) {
|
25688 | sourceUpdater.logger_(`Error with code ${e.code} ` + (e.code === QUOTA_EXCEEDED_ERR ? '(QUOTA_EXCEEDED_ERR) ' : '') + `when appending segment ${segmentInfo.mediaIndex} to ${type}Buffer`);
|
25689 | sourceUpdater.queuePending[type] = null;
|
25690 | onError(e);
|
25691 | }
|
25692 | },
|
25693 | remove: (start, end) => (type, sourceUpdater) => {
|
25694 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25695 |
|
25696 |
|
25697 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
25698 | return;
|
25699 | }
|
25700 |
|
25701 | sourceUpdater.logger_(`Removing ${start} to ${end} from ${type}Buffer`);
|
25702 |
|
25703 | try {
|
25704 | sourceBuffer.remove(start, end);
|
25705 | } catch (e) {
|
25706 | sourceUpdater.logger_(`Remove ${start} to ${end} from ${type}Buffer failed`);
|
25707 | }
|
25708 | },
|
25709 | timestampOffset: offset => (type, sourceUpdater) => {
|
25710 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25711 |
|
25712 |
|
25713 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
25714 | return;
|
25715 | }
|
25716 |
|
25717 | sourceUpdater.logger_(`Setting ${type}timestampOffset to ${offset}`);
|
25718 | sourceBuffer.timestampOffset = offset;
|
25719 | },
|
25720 | callback: callback => (type, sourceUpdater) => {
|
25721 | callback();
|
25722 | },
|
25723 | endOfStream: error => sourceUpdater => {
|
25724 | if (sourceUpdater.mediaSource.readyState !== 'open') {
|
25725 | return;
|
25726 | }
|
25727 |
|
25728 | sourceUpdater.logger_(`Calling mediaSource endOfStream(${error || ''})`);
|
25729 |
|
25730 | try {
|
25731 | sourceUpdater.mediaSource.endOfStream(error);
|
25732 | } catch (e) {
|
25733 | videojs__default["default"].log.warn('Failed to call media source endOfStream', e);
|
25734 | }
|
25735 | },
|
25736 | duration: duration => sourceUpdater => {
|
25737 | sourceUpdater.logger_(`Setting mediaSource duration to ${duration}`);
|
25738 |
|
25739 | try {
|
25740 | sourceUpdater.mediaSource.duration = duration;
|
25741 | } catch (e) {
|
25742 | videojs__default["default"].log.warn('Failed to set media source duration', e);
|
25743 | }
|
25744 | },
|
25745 | abort: () => (type, sourceUpdater) => {
|
25746 | if (sourceUpdater.mediaSource.readyState !== 'open') {
|
25747 | return;
|
25748 | }
|
25749 |
|
25750 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25751 |
|
25752 |
|
25753 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
25754 | return;
|
25755 | }
|
25756 |
|
25757 | sourceUpdater.logger_(`calling abort on ${type}Buffer`);
|
25758 |
|
25759 | try {
|
25760 | sourceBuffer.abort();
|
25761 | } catch (e) {
|
25762 | videojs__default["default"].log.warn(`Failed to abort on ${type}Buffer`, e);
|
25763 | }
|
25764 | },
|
25765 | addSourceBuffer: (type, codec) => sourceUpdater => {
|
25766 | const titleType = toTitleCase(type);
|
25767 | const mime = getMimeForCodec(codec);
|
25768 | sourceUpdater.logger_(`Adding ${type}Buffer with codec ${codec} to mediaSource`);
|
25769 | const sourceBuffer = sourceUpdater.mediaSource.addSourceBuffer(mime);
|
25770 | sourceBuffer.addEventListener('updateend', sourceUpdater[`on${titleType}UpdateEnd_`]);
|
25771 | sourceBuffer.addEventListener('error', sourceUpdater[`on${titleType}Error_`]);
|
25772 | sourceUpdater.codecs[type] = codec;
|
25773 | sourceUpdater[`${type}Buffer`] = sourceBuffer;
|
25774 | },
|
25775 | removeSourceBuffer: type => sourceUpdater => {
|
25776 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25777 | cleanupBuffer(type, sourceUpdater);
|
25778 |
|
25779 |
|
25780 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
25781 | return;
|
25782 | }
|
25783 |
|
25784 | sourceUpdater.logger_(`Removing ${type}Buffer with codec ${sourceUpdater.codecs[type]} from mediaSource`);
|
25785 |
|
25786 | try {
|
25787 | sourceUpdater.mediaSource.removeSourceBuffer(sourceBuffer);
|
25788 | } catch (e) {
|
25789 | videojs__default["default"].log.warn(`Failed to removeSourceBuffer ${type}Buffer`, e);
|
25790 | }
|
25791 | },
|
25792 | changeType: codec => (type, sourceUpdater) => {
|
25793 | const sourceBuffer = sourceUpdater[`${type}Buffer`];
|
25794 | const mime = getMimeForCodec(codec);
|
25795 |
|
25796 |
|
25797 | if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
|
25798 | return;
|
25799 | }
|
25800 |
|
25801 |
|
25802 | if (sourceUpdater.codecs[type] === codec) {
|
25803 | return;
|
25804 | }
|
25805 |
|
25806 | sourceUpdater.logger_(`changing ${type}Buffer codec from ${sourceUpdater.codecs[type]} to ${codec}`);
|
25807 |
|
25808 | try {
|
25809 | sourceBuffer.changeType(mime);
|
25810 | sourceUpdater.codecs[type] = codec;
|
25811 | } catch (e) {
|
25812 | videojs__default["default"].log.warn(`Failed to changeType on ${type}Buffer`, e);
|
25813 | }
|
25814 | }
|
25815 | };
|
25816 |
|
25817 | const pushQueue = ({
|
25818 | type,
|
25819 | sourceUpdater,
|
25820 | action,
|
25821 | doneFn,
|
25822 | name
|
25823 | }) => {
|
25824 | sourceUpdater.queue.push({
|
25825 | type,
|
25826 | action,
|
25827 | doneFn,
|
25828 | name
|
25829 | });
|
25830 | shiftQueue(type, sourceUpdater);
|
25831 | };
|
25832 |
|
25833 | const onUpdateend = (type, sourceUpdater) => e => {
|
25834 |
|
25835 |
|
25836 |
|
25837 |
|
25838 |
|
25839 |
|
25840 | if (sourceUpdater.queuePending[type]) {
|
25841 | const doneFn = sourceUpdater.queuePending[type].doneFn;
|
25842 | sourceUpdater.queuePending[type] = null;
|
25843 |
|
25844 | if (doneFn) {
|
25845 |
|
25846 | doneFn(sourceUpdater[`${type}Error_`]);
|
25847 | }
|
25848 | }
|
25849 |
|
25850 | shiftQueue(type, sourceUpdater);
|
25851 | };
|
25852 | |
25853 |
|
25854 |
|
25855 |
|
25856 |
|
25857 |
|
25858 |
|
25859 |
|
25860 |
|
25861 |
|
25862 |
|
25863 |
|
25864 | class SourceUpdater extends videojs__default["default"].EventTarget {
|
25865 | constructor(mediaSource) {
|
25866 | super();
|
25867 | this.mediaSource = mediaSource;
|
25868 |
|
25869 | this.sourceopenListener_ = () => shiftQueue('mediaSource', this);
|
25870 |
|
25871 | this.mediaSource.addEventListener('sourceopen', this.sourceopenListener_);
|
25872 | this.logger_ = logger('SourceUpdater');
|
25873 |
|
25874 | this.audioTimestampOffset_ = 0;
|
25875 | this.videoTimestampOffset_ = 0;
|
25876 | this.queue = [];
|
25877 | this.queuePending = {
|
25878 | audio: null,
|
25879 | video: null
|
25880 | };
|
25881 | this.delayedAudioAppendQueue_ = [];
|
25882 | this.videoAppendQueued_ = false;
|
25883 | this.codecs = {};
|
25884 | this.onVideoUpdateEnd_ = onUpdateend('video', this);
|
25885 | this.onAudioUpdateEnd_ = onUpdateend('audio', this);
|
25886 |
|
25887 | this.onVideoError_ = e => {
|
25888 |
|
25889 | this.videoError_ = e;
|
25890 | };
|
25891 |
|
25892 | this.onAudioError_ = e => {
|
25893 |
|
25894 | this.audioError_ = e;
|
25895 | };
|
25896 |
|
25897 | this.createdSourceBuffers_ = false;
|
25898 | this.initializedEme_ = false;
|
25899 | this.triggeredReady_ = false;
|
25900 | }
|
25901 |
|
25902 | initializedEme() {
|
25903 | this.initializedEme_ = true;
|
25904 | this.triggerReady();
|
25905 | }
|
25906 |
|
25907 | hasCreatedSourceBuffers() {
|
25908 |
|
25909 |
|
25910 | return this.createdSourceBuffers_;
|
25911 | }
|
25912 |
|
25913 | hasInitializedAnyEme() {
|
25914 | return this.initializedEme_;
|
25915 | }
|
25916 |
|
25917 | ready() {
|
25918 | return this.hasCreatedSourceBuffers() && this.hasInitializedAnyEme();
|
25919 | }
|
25920 |
|
25921 | createSourceBuffers(codecs) {
|
25922 | if (this.hasCreatedSourceBuffers()) {
|
25923 |
|
25924 | return;
|
25925 | }
|
25926 |
|
25927 |
|
25928 |
|
25929 | this.addOrChangeSourceBuffers(codecs);
|
25930 | this.createdSourceBuffers_ = true;
|
25931 | this.trigger('createdsourcebuffers');
|
25932 | this.triggerReady();
|
25933 | }
|
25934 |
|
25935 | triggerReady() {
|
25936 |
|
25937 |
|
25938 |
|
25939 |
|
25940 |
|
25941 |
|
25942 | if (this.ready() && !this.triggeredReady_) {
|
25943 | this.triggeredReady_ = true;
|
25944 | this.trigger('ready');
|
25945 | }
|
25946 | }
|
25947 | |
25948 |
|
25949 |
|
25950 |
|
25951 |
|
25952 |
|
25953 |
|
25954 |
|
25955 |
|
25956 |
|
25957 |
|
25958 | addSourceBuffer(type, codec) {
|
25959 | pushQueue({
|
25960 | type: 'mediaSource',
|
25961 | sourceUpdater: this,
|
25962 | action: actions.addSourceBuffer(type, codec),
|
25963 | name: 'addSourceBuffer'
|
25964 | });
|
25965 | }
|
25966 | |
25967 |
|
25968 |
|
25969 |
|
25970 |
|
25971 |
|
25972 |
|
25973 |
|
25974 | abort(type) {
|
25975 | pushQueue({
|
25976 | type,
|
25977 | sourceUpdater: this,
|
25978 | action: actions.abort(type),
|
25979 | name: 'abort'
|
25980 | });
|
25981 | }
|
25982 | |
25983 |
|
25984 |
|
25985 |
|
25986 |
|
25987 |
|
25988 |
|
25989 |
|
25990 |
|
25991 | removeSourceBuffer(type) {
|
25992 | if (!this.canRemoveSourceBuffer()) {
|
25993 | videojs__default["default"].log.error('removeSourceBuffer is not supported!');
|
25994 | return;
|
25995 | }
|
25996 |
|
25997 | pushQueue({
|
25998 | type: 'mediaSource',
|
25999 | sourceUpdater: this,
|
26000 | action: actions.removeSourceBuffer(type),
|
26001 | name: 'removeSourceBuffer'
|
26002 | });
|
26003 | }
|
26004 | |
26005 |
|
26006 |
|
26007 |
|
26008 |
|
26009 |
|
26010 |
|
26011 |
|
26012 |
|
26013 | canRemoveSourceBuffer() {
|
26014 |
|
26015 |
|
26016 | return !videojs__default["default"].browser.IS_FIREFOX && window.MediaSource && window.MediaSource.prototype && typeof window.MediaSource.prototype.removeSourceBuffer === 'function';
|
26017 | }
|
26018 | |
26019 |
|
26020 |
|
26021 |
|
26022 |
|
26023 |
|
26024 |
|
26025 |
|
26026 |
|
26027 | static canChangeType() {
|
26028 | return window.SourceBuffer && window.SourceBuffer.prototype && typeof window.SourceBuffer.prototype.changeType === 'function';
|
26029 | }
|
26030 | |
26031 |
|
26032 |
|
26033 |
|
26034 |
|
26035 |
|
26036 |
|
26037 |
|
26038 |
|
26039 | canChangeType() {
|
26040 | return this.constructor.canChangeType();
|
26041 | }
|
26042 | |
26043 |
|
26044 |
|
26045 |
|
26046 |
|
26047 |
|
26048 |
|
26049 |
|
26050 |
|
26051 |
|
26052 |
|
26053 | changeType(type, codec) {
|
26054 | if (!this.canChangeType()) {
|
26055 | videojs__default["default"].log.error('changeType is not supported!');
|
26056 | return;
|
26057 | }
|
26058 |
|
26059 | pushQueue({
|
26060 | type,
|
26061 | sourceUpdater: this,
|
26062 | action: actions.changeType(codec),
|
26063 | name: 'changeType'
|
26064 | });
|
26065 | }
|
26066 | |
26067 |
|
26068 |
|
26069 |
|
26070 |
|
26071 |
|
26072 |
|
26073 |
|
26074 |
|
26075 | addOrChangeSourceBuffers(codecs) {
|
26076 | if (!codecs || typeof codecs !== 'object' || Object.keys(codecs).length === 0) {
|
26077 | throw new Error('Cannot addOrChangeSourceBuffers to undefined codecs');
|
26078 | }
|
26079 |
|
26080 | Object.keys(codecs).forEach(type => {
|
26081 | const codec = codecs[type];
|
26082 |
|
26083 | if (!this.hasCreatedSourceBuffers()) {
|
26084 | return this.addSourceBuffer(type, codec);
|
26085 | }
|
26086 |
|
26087 | if (this.canChangeType()) {
|
26088 | this.changeType(type, codec);
|
26089 | }
|
26090 | });
|
26091 | }
|
26092 | |
26093 |
|
26094 |
|
26095 |
|
26096 |
|
26097 |
|
26098 |
|
26099 |
|
26100 |
|
26101 | appendBuffer(options, doneFn) {
|
26102 | const {
|
26103 | segmentInfo,
|
26104 | type,
|
26105 | bytes
|
26106 | } = options;
|
26107 | this.processedAppend_ = true;
|
26108 |
|
26109 | if (type === 'audio' && this.videoBuffer && !this.videoAppendQueued_) {
|
26110 | this.delayedAudioAppendQueue_.push([options, doneFn]);
|
26111 | this.logger_(`delayed audio append of ${bytes.length} until video append`);
|
26112 | return;
|
26113 | }
|
26114 |
|
26115 |
|
26116 |
|
26117 |
|
26118 |
|
26119 | const onError = doneFn;
|
26120 | pushQueue({
|
26121 | type,
|
26122 | sourceUpdater: this,
|
26123 | action: actions.appendBuffer(bytes, segmentInfo || {
|
26124 | mediaIndex: -1
|
26125 | }, onError),
|
26126 | doneFn,
|
26127 | name: 'appendBuffer'
|
26128 | });
|
26129 |
|
26130 | if (type === 'video') {
|
26131 | this.videoAppendQueued_ = true;
|
26132 |
|
26133 | if (!this.delayedAudioAppendQueue_.length) {
|
26134 | return;
|
26135 | }
|
26136 |
|
26137 | const queue = this.delayedAudioAppendQueue_.slice();
|
26138 | this.logger_(`queuing delayed audio ${queue.length} appendBuffers`);
|
26139 | this.delayedAudioAppendQueue_.length = 0;
|
26140 | queue.forEach(que => {
|
26141 | this.appendBuffer.apply(this, que);
|
26142 | });
|
26143 | }
|
26144 | }
|
26145 | |
26146 |
|
26147 |
|
26148 |
|
26149 |
|
26150 |
|
26151 |
|
26152 |
|
26153 | audioBuffered() {
|
26154 |
|
26155 |
|
26156 | if (!inSourceBuffers(this.mediaSource, this.audioBuffer)) {
|
26157 | return createTimeRanges();
|
26158 | }
|
26159 |
|
26160 | return this.audioBuffer.buffered ? this.audioBuffer.buffered : createTimeRanges();
|
26161 | }
|
26162 | |
26163 |
|
26164 |
|
26165 |
|
26166 |
|
26167 |
|
26168 |
|
26169 |
|
26170 | videoBuffered() {
|
26171 |
|
26172 |
|
26173 | if (!inSourceBuffers(this.mediaSource, this.videoBuffer)) {
|
26174 | return createTimeRanges();
|
26175 | }
|
26176 |
|
26177 | return this.videoBuffer.buffered ? this.videoBuffer.buffered : createTimeRanges();
|
26178 | }
|
26179 | |
26180 |
|
26181 |
|
26182 |
|
26183 |
|
26184 |
|
26185 |
|
26186 |
|
26187 | buffered() {
|
26188 | const video = inSourceBuffers(this.mediaSource, this.videoBuffer) ? this.videoBuffer : null;
|
26189 | const audio = inSourceBuffers(this.mediaSource, this.audioBuffer) ? this.audioBuffer : null;
|
26190 |
|
26191 | if (audio && !video) {
|
26192 | return this.audioBuffered();
|
26193 | }
|
26194 |
|
26195 | if (video && !audio) {
|
26196 | return this.videoBuffered();
|
26197 | }
|
26198 |
|
26199 | return bufferIntersection(this.audioBuffered(), this.videoBuffered());
|
26200 | }
|
26201 | |
26202 |
|
26203 |
|
26204 |
|
26205 |
|
26206 |
|
26207 |
|
26208 |
|
26209 |
|
26210 |
|
26211 |
|
26212 | setDuration(duration, doneFn = noop) {
|
26213 |
|
26214 |
|
26215 |
|
26216 |
|
26217 | pushQueue({
|
26218 | type: 'mediaSource',
|
26219 | sourceUpdater: this,
|
26220 | action: actions.duration(duration),
|
26221 | name: 'duration',
|
26222 | doneFn
|
26223 | });
|
26224 | }
|
26225 | |
26226 |
|
26227 |
|
26228 |
|
26229 |
|
26230 |
|
26231 |
|
26232 |
|
26233 |
|
26234 |
|
26235 |
|
26236 |
|
26237 | endOfStream(error = null, doneFn = noop) {
|
26238 | if (typeof error !== 'string') {
|
26239 | error = undefined;
|
26240 | }
|
26241 |
|
26242 |
|
26243 |
|
26244 |
|
26245 |
|
26246 | pushQueue({
|
26247 | type: 'mediaSource',
|
26248 | sourceUpdater: this,
|
26249 | action: actions.endOfStream(error),
|
26250 | name: 'endOfStream',
|
26251 | doneFn
|
26252 | });
|
26253 | }
|
26254 | |
26255 |
|
26256 |
|
26257 |
|
26258 |
|
26259 |
|
26260 |
|
26261 |
|
26262 |
|
26263 |
|
26264 |
|
26265 | removeAudio(start, end, done = noop) {
|
26266 | if (!this.audioBuffered().length || this.audioBuffered().end(0) === 0) {
|
26267 | done();
|
26268 | return;
|
26269 | }
|
26270 |
|
26271 | pushQueue({
|
26272 | type: 'audio',
|
26273 | sourceUpdater: this,
|
26274 | action: actions.remove(start, end),
|
26275 | doneFn: done,
|
26276 | name: 'remove'
|
26277 | });
|
26278 | }
|
26279 | |
26280 |
|
26281 |
|
26282 |
|
26283 |
|
26284 |
|
26285 |
|
26286 |
|
26287 |
|
26288 |
|
26289 |
|
26290 | removeVideo(start, end, done = noop) {
|
26291 | if (!this.videoBuffered().length || this.videoBuffered().end(0) === 0) {
|
26292 | done();
|
26293 | return;
|
26294 | }
|
26295 |
|
26296 | pushQueue({
|
26297 | type: 'video',
|
26298 | sourceUpdater: this,
|
26299 | action: actions.remove(start, end),
|
26300 | doneFn: done,
|
26301 | name: 'remove'
|
26302 | });
|
26303 | }
|
26304 | |
26305 |
|
26306 |
|
26307 |
|
26308 |
|
26309 |
|
26310 |
|
26311 | updating() {
|
26312 |
|
26313 | if (updating('audio', this) || updating('video', this)) {
|
26314 | return true;
|
26315 | }
|
26316 |
|
26317 | return false;
|
26318 | }
|
26319 | |
26320 |
|
26321 |
|
26322 |
|
26323 |
|
26324 |
|
26325 |
|
26326 | audioTimestampOffset(offset) {
|
26327 | if (typeof offset !== 'undefined' && this.audioBuffer &&
|
26328 | this.audioTimestampOffset_ !== offset) {
|
26329 | pushQueue({
|
26330 | type: 'audio',
|
26331 | sourceUpdater: this,
|
26332 | action: actions.timestampOffset(offset),
|
26333 | name: 'timestampOffset'
|
26334 | });
|
26335 | this.audioTimestampOffset_ = offset;
|
26336 | }
|
26337 |
|
26338 | return this.audioTimestampOffset_;
|
26339 | }
|
26340 | |
26341 |
|
26342 |
|
26343 |
|
26344 |
|
26345 |
|
26346 |
|
26347 | videoTimestampOffset(offset) {
|
26348 | if (typeof offset !== 'undefined' && this.videoBuffer &&
|
26349 | this.videoTimestampOffset !== offset) {
|
26350 | pushQueue({
|
26351 | type: 'video',
|
26352 | sourceUpdater: this,
|
26353 | action: actions.timestampOffset(offset),
|
26354 | name: 'timestampOffset'
|
26355 | });
|
26356 | this.videoTimestampOffset_ = offset;
|
26357 | }
|
26358 |
|
26359 | return this.videoTimestampOffset_;
|
26360 | }
|
26361 | |
26362 |
|
26363 |
|
26364 |
|
26365 |
|
26366 |
|
26367 |
|
26368 |
|
26369 |
|
26370 | audioQueueCallback(callback) {
|
26371 | if (!this.audioBuffer) {
|
26372 | return;
|
26373 | }
|
26374 |
|
26375 | pushQueue({
|
26376 | type: 'audio',
|
26377 | sourceUpdater: this,
|
26378 | action: actions.callback(callback),
|
26379 | name: 'callback'
|
26380 | });
|
26381 | }
|
26382 | |
26383 |
|
26384 |
|
26385 |
|
26386 |
|
26387 |
|
26388 |
|
26389 |
|
26390 |
|
26391 | videoQueueCallback(callback) {
|
26392 | if (!this.videoBuffer) {
|
26393 | return;
|
26394 | }
|
26395 |
|
26396 | pushQueue({
|
26397 | type: 'video',
|
26398 | sourceUpdater: this,
|
26399 | action: actions.callback(callback),
|
26400 | name: 'callback'
|
26401 | });
|
26402 | }
|
26403 | |
26404 |
|
26405 |
|
26406 |
|
26407 |
|
26408 | dispose() {
|
26409 | this.trigger('dispose');
|
26410 | bufferTypes.forEach(type => {
|
26411 | this.abort(type);
|
26412 |
|
26413 | if (this.canRemoveSourceBuffer()) {
|
26414 | this.removeSourceBuffer(type);
|
26415 | } else {
|
26416 | this[`${type}QueueCallback`](() => cleanupBuffer(type, this));
|
26417 | }
|
26418 | });
|
26419 | this.videoAppendQueued_ = false;
|
26420 | this.delayedAudioAppendQueue_.length = 0;
|
26421 |
|
26422 | if (this.sourceopenListener_) {
|
26423 | this.mediaSource.removeEventListener('sourceopen', this.sourceopenListener_);
|
26424 | }
|
26425 |
|
26426 | this.off();
|
26427 | }
|
26428 |
|
26429 | }
|
26430 |
|
26431 | const uint8ToUtf8 = uintArray => decodeURIComponent(escape(String.fromCharCode.apply(null, uintArray)));
|
26432 | const bufferToHexString = buffer => {
|
26433 | const uInt8Buffer = new Uint8Array(buffer);
|
26434 | return Array.from(uInt8Buffer).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
26435 | };
|
26436 |
|
26437 | |
26438 |
|
26439 |
|
26440 | const VTT_LINE_TERMINATORS = new Uint8Array('\n\n'.split('').map(char => char.charCodeAt(0)));
|
26441 |
|
26442 | class NoVttJsError extends Error {
|
26443 | constructor() {
|
26444 | super('Trying to parse received VTT cues, but there is no WebVTT. Make sure vtt.js is loaded.');
|
26445 | }
|
26446 |
|
26447 | }
|
26448 | |
26449 |
|
26450 |
|
26451 |
|
26452 |
|
26453 |
|
26454 |
|
26455 |
|
26456 |
|
26457 | class VTTSegmentLoader extends SegmentLoader {
|
26458 | constructor(settings, options = {}) {
|
26459 | super(settings, options);
|
26460 |
|
26461 |
|
26462 | this.mediaSource_ = null;
|
26463 | this.subtitlesTrack_ = null;
|
26464 | this.loaderType_ = 'subtitle';
|
26465 | this.featuresNativeTextTracks_ = settings.featuresNativeTextTracks;
|
26466 | this.loadVttJs = settings.loadVttJs;
|
26467 |
|
26468 |
|
26469 | this.shouldSaveSegmentTimingInfo_ = false;
|
26470 | }
|
26471 |
|
26472 | createTransmuxer_() {
|
26473 |
|
26474 | return null;
|
26475 | }
|
26476 | |
26477 |
|
26478 |
|
26479 |
|
26480 |
|
26481 |
|
26482 |
|
26483 |
|
26484 | buffered_() {
|
26485 | if (!this.subtitlesTrack_ || !this.subtitlesTrack_.cues || !this.subtitlesTrack_.cues.length) {
|
26486 | return createTimeRanges();
|
26487 | }
|
26488 |
|
26489 | const cues = this.subtitlesTrack_.cues;
|
26490 | const start = cues[0].startTime;
|
26491 | const end = cues[cues.length - 1].startTime;
|
26492 | return createTimeRanges([[start, end]]);
|
26493 | }
|
26494 | |
26495 |
|
26496 |
|
26497 |
|
26498 |
|
26499 |
|
26500 |
|
26501 |
|
26502 |
|
26503 |
|
26504 |
|
26505 |
|
26506 | initSegmentForMap(map, set = false) {
|
26507 | if (!map) {
|
26508 | return null;
|
26509 | }
|
26510 |
|
26511 | const id = initSegmentId(map);
|
26512 | let storedMap = this.initSegments_[id];
|
26513 |
|
26514 | if (set && !storedMap && map.bytes) {
|
26515 |
|
26516 |
|
26517 |
|
26518 |
|
26519 | const combinedByteLength = VTT_LINE_TERMINATORS.byteLength + map.bytes.byteLength;
|
26520 | const combinedSegment = new Uint8Array(combinedByteLength);
|
26521 | combinedSegment.set(map.bytes);
|
26522 | combinedSegment.set(VTT_LINE_TERMINATORS, map.bytes.byteLength);
|
26523 | this.initSegments_[id] = storedMap = {
|
26524 | resolvedUri: map.resolvedUri,
|
26525 | byterange: map.byterange,
|
26526 | bytes: combinedSegment
|
26527 | };
|
26528 | }
|
26529 |
|
26530 | return storedMap || map;
|
26531 | }
|
26532 | |
26533 |
|
26534 |
|
26535 |
|
26536 |
|
26537 |
|
26538 |
|
26539 |
|
26540 | couldBeginLoading_() {
|
26541 | return this.playlist_ && this.subtitlesTrack_ && !this.paused();
|
26542 | }
|
26543 | |
26544 |
|
26545 |
|
26546 |
|
26547 |
|
26548 |
|
26549 |
|
26550 |
|
26551 |
|
26552 | init_() {
|
26553 | this.state = 'READY';
|
26554 | this.resetEverything();
|
26555 | return this.monitorBuffer_();
|
26556 | }
|
26557 | |
26558 |
|
26559 |
|
26560 |
|
26561 |
|
26562 |
|
26563 |
|
26564 |
|
26565 |
|
26566 |
|
26567 | track(track) {
|
26568 | if (typeof track === 'undefined') {
|
26569 | return this.subtitlesTrack_;
|
26570 | }
|
26571 |
|
26572 | this.subtitlesTrack_ = track;
|
26573 |
|
26574 |
|
26575 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
26576 | this.init_();
|
26577 | }
|
26578 |
|
26579 | return this.subtitlesTrack_;
|
26580 | }
|
26581 | |
26582 |
|
26583 |
|
26584 |
|
26585 |
|
26586 |
|
26587 |
|
26588 |
|
26589 | remove(start, end) {
|
26590 | removeCuesFromTrack(start, end, this.subtitlesTrack_);
|
26591 | }
|
26592 | |
26593 |
|
26594 |
|
26595 |
|
26596 |
|
26597 |
|
26598 |
|
26599 |
|
26600 |
|
26601 |
|
26602 |
|
26603 | fillBuffer_() {
|
26604 |
|
26605 | const segmentInfo = this.chooseNextRequest_();
|
26606 |
|
26607 | if (!segmentInfo) {
|
26608 | return;
|
26609 | }
|
26610 |
|
26611 | if (this.syncController_.timestampOffsetForTimeline(segmentInfo.timeline) === null) {
|
26612 |
|
26613 |
|
26614 | const checkTimestampOffset = () => {
|
26615 | this.state = 'READY';
|
26616 |
|
26617 | if (!this.paused()) {
|
26618 |
|
26619 | this.monitorBuffer_();
|
26620 | }
|
26621 | };
|
26622 |
|
26623 | this.syncController_.one('timestampoffset', checkTimestampOffset);
|
26624 | this.state = 'WAITING_ON_TIMELINE';
|
26625 | return;
|
26626 | }
|
26627 |
|
26628 | this.loadSegment_(segmentInfo);
|
26629 | }
|
26630 |
|
26631 |
|
26632 | timestampOffsetForSegment_() {
|
26633 | return null;
|
26634 | }
|
26635 |
|
26636 | chooseNextRequest_() {
|
26637 | return this.skipEmptySegments_(super.chooseNextRequest_());
|
26638 | }
|
26639 | |
26640 |
|
26641 |
|
26642 |
|
26643 |
|
26644 |
|
26645 |
|
26646 |
|
26647 |
|
26648 |
|
26649 |
|
26650 |
|
26651 | skipEmptySegments_(segmentInfo) {
|
26652 | while (segmentInfo && segmentInfo.segment.empty) {
|
26653 |
|
26654 | if (segmentInfo.mediaIndex + 1 >= segmentInfo.playlist.segments.length) {
|
26655 | segmentInfo = null;
|
26656 | break;
|
26657 | }
|
26658 |
|
26659 | segmentInfo = this.generateSegmentInfo_({
|
26660 | playlist: segmentInfo.playlist,
|
26661 | mediaIndex: segmentInfo.mediaIndex + 1,
|
26662 | startOfSegment: segmentInfo.startOfSegment + segmentInfo.duration,
|
26663 | isSyncRequest: segmentInfo.isSyncRequest
|
26664 | });
|
26665 | }
|
26666 |
|
26667 | return segmentInfo;
|
26668 | }
|
26669 |
|
26670 | stopForError(error) {
|
26671 | this.error(error);
|
26672 | this.state = 'READY';
|
26673 | this.pause();
|
26674 | this.trigger('error');
|
26675 | }
|
26676 | |
26677 |
|
26678 |
|
26679 |
|
26680 |
|
26681 |
|
26682 |
|
26683 | segmentRequestFinished_(error, simpleSegment, result) {
|
26684 | if (!this.subtitlesTrack_) {
|
26685 | this.state = 'READY';
|
26686 | return;
|
26687 | }
|
26688 |
|
26689 | this.saveTransferStats_(simpleSegment.stats);
|
26690 |
|
26691 | if (!this.pendingSegment_) {
|
26692 | this.state = 'READY';
|
26693 | this.mediaRequestsAborted += 1;
|
26694 | return;
|
26695 | }
|
26696 |
|
26697 | if (error) {
|
26698 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
26699 | this.handleTimeout_();
|
26700 | }
|
26701 |
|
26702 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
26703 | this.mediaRequestsAborted += 1;
|
26704 | } else {
|
26705 | this.mediaRequestsErrored += 1;
|
26706 | }
|
26707 |
|
26708 | this.stopForError(error);
|
26709 | return;
|
26710 | }
|
26711 |
|
26712 | const segmentInfo = this.pendingSegment_;
|
26713 |
|
26714 |
|
26715 | this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats);
|
26716 |
|
26717 | if (simpleSegment.key) {
|
26718 | this.segmentKey(simpleSegment.key, true);
|
26719 | }
|
26720 |
|
26721 | this.state = 'APPENDING';
|
26722 |
|
26723 | this.trigger('appending');
|
26724 | const segment = segmentInfo.segment;
|
26725 |
|
26726 | if (segment.map) {
|
26727 | segment.map.bytes = simpleSegment.map.bytes;
|
26728 | }
|
26729 |
|
26730 | segmentInfo.bytes = simpleSegment.bytes;
|
26731 |
|
26732 | if (typeof window.WebVTT !== 'function' && typeof this.loadVttJs === 'function') {
|
26733 | this.state = 'WAITING_ON_VTTJS';
|
26734 |
|
26735 |
|
26736 | this.loadVttJs().then(() => this.segmentRequestFinished_(error, simpleSegment, result), () => this.stopForError({
|
26737 | message: 'Error loading vtt.js'
|
26738 | }));
|
26739 | return;
|
26740 | }
|
26741 |
|
26742 | segment.requested = true;
|
26743 |
|
26744 | try {
|
26745 | this.parseVTTCues_(segmentInfo);
|
26746 | } catch (e) {
|
26747 | this.stopForError({
|
26748 | message: e.message
|
26749 | });
|
26750 | return;
|
26751 | }
|
26752 |
|
26753 | this.updateTimeMapping_(segmentInfo, this.syncController_.timelines[segmentInfo.timeline], this.playlist_);
|
26754 |
|
26755 | if (segmentInfo.cues.length) {
|
26756 | segmentInfo.timingInfo = {
|
26757 | start: segmentInfo.cues[0].startTime,
|
26758 | end: segmentInfo.cues[segmentInfo.cues.length - 1].endTime
|
26759 | };
|
26760 | } else {
|
26761 | segmentInfo.timingInfo = {
|
26762 | start: segmentInfo.startOfSegment,
|
26763 | end: segmentInfo.startOfSegment + segmentInfo.duration
|
26764 | };
|
26765 | }
|
26766 |
|
26767 | if (segmentInfo.isSyncRequest) {
|
26768 | this.trigger('syncinfoupdate');
|
26769 | this.pendingSegment_ = null;
|
26770 | this.state = 'READY';
|
26771 | return;
|
26772 | }
|
26773 |
|
26774 | segmentInfo.byteLength = segmentInfo.bytes.byteLength;
|
26775 | this.mediaSecondsLoaded += segment.duration;
|
26776 |
|
26777 |
|
26778 | segmentInfo.cues.forEach(cue => {
|
26779 | this.subtitlesTrack_.addCue(this.featuresNativeTextTracks_ ? new window.VTTCue(cue.startTime, cue.endTime, cue.text) : cue);
|
26780 | });
|
26781 |
|
26782 |
|
26783 |
|
26784 |
|
26785 | removeDuplicateCuesFromTrack(this.subtitlesTrack_);
|
26786 | this.handleAppendsDone_();
|
26787 | }
|
26788 |
|
26789 | handleData_() {
|
26790 |
|
26791 | }
|
26792 |
|
26793 | updateTimingInfoEnd_() {
|
26794 | }
|
26795 | |
26796 |
|
26797 |
|
26798 |
|
26799 |
|
26800 |
|
26801 |
|
26802 |
|
26803 |
|
26804 |
|
26805 |
|
26806 | parseVTTCues_(segmentInfo) {
|
26807 | let decoder;
|
26808 | let decodeBytesToString = false;
|
26809 |
|
26810 | if (typeof window.WebVTT !== 'function') {
|
26811 |
|
26812 | throw new NoVttJsError();
|
26813 | }
|
26814 |
|
26815 | if (typeof window.TextDecoder === 'function') {
|
26816 | decoder = new window.TextDecoder('utf8');
|
26817 | } else {
|
26818 | decoder = window.WebVTT.StringDecoder();
|
26819 | decodeBytesToString = true;
|
26820 | }
|
26821 |
|
26822 | const parser = new window.WebVTT.Parser(window, window.vttjs, decoder);
|
26823 | segmentInfo.cues = [];
|
26824 | segmentInfo.timestampmap = {
|
26825 | MPEGTS: 0,
|
26826 | LOCAL: 0
|
26827 | };
|
26828 | parser.oncue = segmentInfo.cues.push.bind(segmentInfo.cues);
|
26829 |
|
26830 | parser.ontimestampmap = map => {
|
26831 | segmentInfo.timestampmap = map;
|
26832 | };
|
26833 |
|
26834 | parser.onparsingerror = error => {
|
26835 | videojs__default["default"].log.warn('Error encountered when parsing cues: ' + error.message);
|
26836 | };
|
26837 |
|
26838 | if (segmentInfo.segment.map) {
|
26839 | let mapData = segmentInfo.segment.map.bytes;
|
26840 |
|
26841 | if (decodeBytesToString) {
|
26842 | mapData = uint8ToUtf8(mapData);
|
26843 | }
|
26844 |
|
26845 | parser.parse(mapData);
|
26846 | }
|
26847 |
|
26848 | let segmentData = segmentInfo.bytes;
|
26849 |
|
26850 | if (decodeBytesToString) {
|
26851 | segmentData = uint8ToUtf8(segmentData);
|
26852 | }
|
26853 |
|
26854 | parser.parse(segmentData);
|
26855 | parser.flush();
|
26856 | }
|
26857 | |
26858 |
|
26859 |
|
26860 |
|
26861 |
|
26862 |
|
26863 |
|
26864 |
|
26865 |
|
26866 |
|
26867 |
|
26868 |
|
26869 |
|
26870 |
|
26871 |
|
26872 | updateTimeMapping_(segmentInfo, mappingObj, playlist) {
|
26873 | const segment = segmentInfo.segment;
|
26874 |
|
26875 | if (!mappingObj) {
|
26876 |
|
26877 |
|
26878 |
|
26879 | return;
|
26880 | }
|
26881 |
|
26882 | if (!segmentInfo.cues.length) {
|
26883 |
|
26884 |
|
26885 |
|
26886 | segment.empty = true;
|
26887 | return;
|
26888 | }
|
26889 |
|
26890 | const {
|
26891 | MPEGTS,
|
26892 | LOCAL
|
26893 | } = segmentInfo.timestampmap;
|
26894 | |
26895 |
|
26896 |
|
26897 |
|
26898 |
|
26899 |
|
26900 | const mpegTsInSeconds = MPEGTS / clock.ONE_SECOND_IN_TS;
|
26901 | const diff = mpegTsInSeconds - LOCAL + mappingObj.mapping;
|
26902 | segmentInfo.cues.forEach(cue => {
|
26903 | const duration = cue.endTime - cue.startTime;
|
26904 | const startTime = MPEGTS === 0 ? cue.startTime + diff : this.handleRollover_(cue.startTime + diff, mappingObj.time);
|
26905 | cue.startTime = Math.max(startTime, 0);
|
26906 | cue.endTime = Math.max(startTime + duration, 0);
|
26907 | });
|
26908 |
|
26909 | if (!playlist.syncInfo) {
|
26910 | const firstStart = segmentInfo.cues[0].startTime;
|
26911 | const lastStart = segmentInfo.cues[segmentInfo.cues.length - 1].startTime;
|
26912 | playlist.syncInfo = {
|
26913 | mediaSequence: playlist.mediaSequence + segmentInfo.mediaIndex,
|
26914 | time: Math.min(firstStart, lastStart - segment.duration)
|
26915 | };
|
26916 | }
|
26917 | }
|
26918 | |
26919 |
|
26920 |
|
26921 |
|
26922 |
|
26923 |
|
26924 |
|
26925 |
|
26926 |
|
26927 |
|
26928 |
|
26929 |
|
26930 |
|
26931 |
|
26932 |
|
26933 |
|
26934 |
|
26935 |
|
26936 |
|
26937 |
|
26938 | handleRollover_(value, reference) {
|
26939 | if (reference === null) {
|
26940 | return value;
|
26941 | }
|
26942 |
|
26943 | let valueIn90khz = value * clock.ONE_SECOND_IN_TS;
|
26944 | const referenceIn90khz = reference * clock.ONE_SECOND_IN_TS;
|
26945 | let offset;
|
26946 |
|
26947 | if (referenceIn90khz < valueIn90khz) {
|
26948 |
|
26949 | offset = -8589934592;
|
26950 | } else {
|
26951 |
|
26952 | offset = 8589934592;
|
26953 | }
|
26954 |
|
26955 |
|
26956 | while (Math.abs(valueIn90khz - referenceIn90khz) > 4294967296) {
|
26957 | valueIn90khz += offset;
|
26958 | }
|
26959 |
|
26960 | return valueIn90khz / clock.ONE_SECOND_IN_TS;
|
26961 | }
|
26962 |
|
26963 | }
|
26964 |
|
26965 | |
26966 |
|
26967 |
|
26968 |
|
26969 | |
26970 |
|
26971 |
|
26972 |
|
26973 |
|
26974 |
|
26975 |
|
26976 |
|
26977 |
|
26978 |
|
26979 |
|
26980 |
|
26981 | const findAdCue = function (track, mediaTime) {
|
26982 | const cues = track.cues;
|
26983 |
|
26984 | for (let i = 0; i < cues.length; i++) {
|
26985 | const cue = cues[i];
|
26986 |
|
26987 | if (mediaTime >= cue.adStartTime && mediaTime <= cue.adEndTime) {
|
26988 | return cue;
|
26989 | }
|
26990 | }
|
26991 |
|
26992 | return null;
|
26993 | };
|
26994 | const updateAdCues = function (media, track, offset = 0) {
|
26995 | if (!media.segments) {
|
26996 | return;
|
26997 | }
|
26998 |
|
26999 | let mediaTime = offset;
|
27000 | let cue;
|
27001 |
|
27002 | for (let i = 0; i < media.segments.length; i++) {
|
27003 | const segment = media.segments[i];
|
27004 |
|
27005 | if (!cue) {
|
27006 |
|
27007 |
|
27008 |
|
27009 |
|
27010 | cue = findAdCue(track, mediaTime + segment.duration / 2);
|
27011 | }
|
27012 |
|
27013 | if (cue) {
|
27014 | if ('cueIn' in segment) {
|
27015 |
|
27016 | cue.endTime = mediaTime;
|
27017 | cue.adEndTime = mediaTime;
|
27018 | mediaTime += segment.duration;
|
27019 | cue = null;
|
27020 | continue;
|
27021 | }
|
27022 |
|
27023 | if (mediaTime < cue.endTime) {
|
27024 |
|
27025 | mediaTime += segment.duration;
|
27026 | continue;
|
27027 | }
|
27028 |
|
27029 |
|
27030 | cue.endTime += segment.duration;
|
27031 | } else {
|
27032 | if ('cueOut' in segment) {
|
27033 | cue = new window.VTTCue(mediaTime, mediaTime + segment.duration, segment.cueOut);
|
27034 | cue.adStartTime = mediaTime;
|
27035 |
|
27036 |
|
27037 | cue.adEndTime = mediaTime + parseFloat(segment.cueOut);
|
27038 | track.addCue(cue);
|
27039 | }
|
27040 |
|
27041 | if ('cueOutCont' in segment) {
|
27042 |
|
27043 |
|
27044 |
|
27045 | const [adOffset, adTotal] = segment.cueOutCont.split('/').map(parseFloat);
|
27046 | cue = new window.VTTCue(mediaTime, mediaTime + segment.duration, '');
|
27047 | cue.adStartTime = mediaTime - adOffset;
|
27048 | cue.adEndTime = cue.adStartTime + adTotal;
|
27049 | track.addCue(cue);
|
27050 | }
|
27051 | }
|
27052 |
|
27053 | mediaTime += segment.duration;
|
27054 | }
|
27055 | };
|
27056 |
|
27057 | |
27058 |
|
27059 |
|
27060 |
|
27061 |
|
27062 |
|
27063 |
|
27064 |
|
27065 | const MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC = 86400;
|
27066 | const syncPointStrategies = [
|
27067 |
|
27068 | {
|
27069 | name: 'VOD',
|
27070 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
27071 | if (duration !== Infinity) {
|
27072 | const syncPoint = {
|
27073 | time: 0,
|
27074 | segmentIndex: 0,
|
27075 | partIndex: null
|
27076 | };
|
27077 | return syncPoint;
|
27078 | }
|
27079 |
|
27080 | return null;
|
27081 | }
|
27082 | }, {
|
27083 | name: 'MediaSequence',
|
27084 |
|
27085 | |
27086 |
|
27087 |
|
27088 |
|
27089 |
|
27090 |
|
27091 |
|
27092 |
|
27093 |
|
27094 |
|
27095 | run: (syncController, playlist, duration, currentTimeline, currentTime, type) => {
|
27096 | if (!type) {
|
27097 | return null;
|
27098 | }
|
27099 |
|
27100 | const mediaSequenceMap = syncController.getMediaSequenceMap(type);
|
27101 |
|
27102 | if (!mediaSequenceMap || mediaSequenceMap.size === 0) {
|
27103 | return null;
|
27104 | }
|
27105 |
|
27106 | if (playlist.mediaSequence === undefined || !Array.isArray(playlist.segments) || !playlist.segments.length) {
|
27107 | return null;
|
27108 | }
|
27109 |
|
27110 | let currentMediaSequence = playlist.mediaSequence;
|
27111 | let segmentIndex = 0;
|
27112 |
|
27113 | for (const segment of playlist.segments) {
|
27114 | const range = mediaSequenceMap.get(currentMediaSequence);
|
27115 |
|
27116 | if (!range) {
|
27117 |
|
27118 |
|
27119 |
|
27120 | break;
|
27121 | }
|
27122 |
|
27123 | if (currentTime >= range.start && currentTime < range.end) {
|
27124 |
|
27125 | if (Array.isArray(segment.parts) && segment.parts.length) {
|
27126 | let currentPartStart = range.start;
|
27127 | let partIndex = 0;
|
27128 |
|
27129 | for (const part of segment.parts) {
|
27130 | const start = currentPartStart;
|
27131 | const end = start + part.duration;
|
27132 |
|
27133 | if (currentTime >= start && currentTime < end) {
|
27134 | return {
|
27135 | time: range.start,
|
27136 | segmentIndex,
|
27137 | partIndex
|
27138 | };
|
27139 | }
|
27140 |
|
27141 | partIndex++;
|
27142 | currentPartStart = end;
|
27143 | }
|
27144 | }
|
27145 |
|
27146 |
|
27147 | return {
|
27148 | time: range.start,
|
27149 | segmentIndex,
|
27150 | partIndex: null
|
27151 | };
|
27152 | }
|
27153 |
|
27154 | segmentIndex++;
|
27155 | currentMediaSequence++;
|
27156 | }
|
27157 |
|
27158 |
|
27159 | return null;
|
27160 | }
|
27161 | },
|
27162 | {
|
27163 | name: 'ProgramDateTime',
|
27164 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
27165 | if (!Object.keys(syncController.timelineToDatetimeMappings).length) {
|
27166 | return null;
|
27167 | }
|
27168 |
|
27169 | let syncPoint = null;
|
27170 | let lastDistance = null;
|
27171 | const partsAndSegments = getPartsAndSegments(playlist);
|
27172 | currentTime = currentTime || 0;
|
27173 |
|
27174 | for (let i = 0; i < partsAndSegments.length; i++) {
|
27175 |
|
27176 |
|
27177 | const index = playlist.endList || currentTime === 0 ? i : partsAndSegments.length - (i + 1);
|
27178 | const partAndSegment = partsAndSegments[index];
|
27179 | const segment = partAndSegment.segment;
|
27180 | const datetimeMapping = syncController.timelineToDatetimeMappings[segment.timeline];
|
27181 |
|
27182 | if (!datetimeMapping || !segment.dateTimeObject) {
|
27183 | continue;
|
27184 | }
|
27185 |
|
27186 | const segmentTime = segment.dateTimeObject.getTime() / 1000;
|
27187 | let start = segmentTime + datetimeMapping;
|
27188 |
|
27189 | if (segment.parts && typeof partAndSegment.partIndex === 'number') {
|
27190 | for (let z = 0; z < partAndSegment.partIndex; z++) {
|
27191 | start += segment.parts[z].duration;
|
27192 | }
|
27193 | }
|
27194 |
|
27195 | const distance = Math.abs(currentTime - start);
|
27196 |
|
27197 |
|
27198 | if (lastDistance !== null && (distance === 0 || lastDistance < distance)) {
|
27199 | break;
|
27200 | }
|
27201 |
|
27202 | lastDistance = distance;
|
27203 | syncPoint = {
|
27204 | time: start,
|
27205 | segmentIndex: partAndSegment.segmentIndex,
|
27206 | partIndex: partAndSegment.partIndex
|
27207 | };
|
27208 | }
|
27209 |
|
27210 | return syncPoint;
|
27211 | }
|
27212 | },
|
27213 |
|
27214 | {
|
27215 | name: 'Segment',
|
27216 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
27217 | let syncPoint = null;
|
27218 | let lastDistance = null;
|
27219 | currentTime = currentTime || 0;
|
27220 | const partsAndSegments = getPartsAndSegments(playlist);
|
27221 |
|
27222 | for (let i = 0; i < partsAndSegments.length; i++) {
|
27223 |
|
27224 |
|
27225 | const index = playlist.endList || currentTime === 0 ? i : partsAndSegments.length - (i + 1);
|
27226 | const partAndSegment = partsAndSegments[index];
|
27227 | const segment = partAndSegment.segment;
|
27228 | const start = partAndSegment.part && partAndSegment.part.start || segment && segment.start;
|
27229 |
|
27230 | if (segment.timeline === currentTimeline && typeof start !== 'undefined') {
|
27231 | const distance = Math.abs(currentTime - start);
|
27232 |
|
27233 |
|
27234 | if (lastDistance !== null && lastDistance < distance) {
|
27235 | break;
|
27236 | }
|
27237 |
|
27238 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
27239 | lastDistance = distance;
|
27240 | syncPoint = {
|
27241 | time: start,
|
27242 | segmentIndex: partAndSegment.segmentIndex,
|
27243 | partIndex: partAndSegment.partIndex
|
27244 | };
|
27245 | }
|
27246 | }
|
27247 | }
|
27248 |
|
27249 | return syncPoint;
|
27250 | }
|
27251 | },
|
27252 |
|
27253 | {
|
27254 | name: 'Discontinuity',
|
27255 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
27256 | let syncPoint = null;
|
27257 | currentTime = currentTime || 0;
|
27258 |
|
27259 | if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
27260 | let lastDistance = null;
|
27261 |
|
27262 | for (let i = 0; i < playlist.discontinuityStarts.length; i++) {
|
27263 | const segmentIndex = playlist.discontinuityStarts[i];
|
27264 | const discontinuity = playlist.discontinuitySequence + i + 1;
|
27265 | const discontinuitySync = syncController.discontinuities[discontinuity];
|
27266 |
|
27267 | if (discontinuitySync) {
|
27268 | const distance = Math.abs(currentTime - discontinuitySync.time);
|
27269 |
|
27270 |
|
27271 | if (lastDistance !== null && lastDistance < distance) {
|
27272 | break;
|
27273 | }
|
27274 |
|
27275 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
27276 | lastDistance = distance;
|
27277 | syncPoint = {
|
27278 | time: discontinuitySync.time,
|
27279 | segmentIndex,
|
27280 | partIndex: null
|
27281 | };
|
27282 | }
|
27283 | }
|
27284 | }
|
27285 | }
|
27286 |
|
27287 | return syncPoint;
|
27288 | }
|
27289 | },
|
27290 |
|
27291 | {
|
27292 | name: 'Playlist',
|
27293 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
27294 | if (playlist.syncInfo) {
|
27295 | const syncPoint = {
|
27296 | time: playlist.syncInfo.time,
|
27297 | segmentIndex: playlist.syncInfo.mediaSequence - playlist.mediaSequence,
|
27298 | partIndex: null
|
27299 | };
|
27300 | return syncPoint;
|
27301 | }
|
27302 |
|
27303 | return null;
|
27304 | }
|
27305 | }];
|
27306 | class SyncController extends videojs__default["default"].EventTarget {
|
27307 | constructor(options = {}) {
|
27308 | super();
|
27309 |
|
27310 | this.timelines = [];
|
27311 | this.discontinuities = [];
|
27312 | this.timelineToDatetimeMappings = {};
|
27313 | |
27314 |
|
27315 |
|
27316 |
|
27317 |
|
27318 | this.mediaSequenceStorage_ = new Map();
|
27319 | this.logger_ = logger('SyncController');
|
27320 | }
|
27321 | |
27322 |
|
27323 |
|
27324 |
|
27325 |
|
27326 |
|
27327 |
|
27328 |
|
27329 | getMediaSequenceMap(type) {
|
27330 | return this.mediaSequenceStorage_.get(type);
|
27331 | }
|
27332 | |
27333 |
|
27334 |
|
27335 |
|
27336 |
|
27337 |
|
27338 |
|
27339 |
|
27340 |
|
27341 |
|
27342 | updateMediaSequenceMap(playlist, currentTime, type) {
|
27343 |
|
27344 | if (playlist.mediaSequence === undefined || !Array.isArray(playlist.segments) || !playlist.segments.length) {
|
27345 | return;
|
27346 | }
|
27347 |
|
27348 | const currentMap = this.getMediaSequenceMap(type);
|
27349 | const result = new Map();
|
27350 | let currentMediaSequence = playlist.mediaSequence;
|
27351 | let currentBaseTime;
|
27352 |
|
27353 | if (!currentMap) {
|
27354 |
|
27355 | currentBaseTime = 0;
|
27356 | } else if (currentMap.has(playlist.mediaSequence)) {
|
27357 |
|
27358 | currentBaseTime = currentMap.get(playlist.mediaSequence).start;
|
27359 | } else {
|
27360 |
|
27361 | this.logger_(`MediaSequence sync for ${type} segment loader - received a gap between playlists.
|
27362 | Fallback base time to: ${currentTime}.
|
27363 | Received media sequence: ${currentMediaSequence}.
|
27364 | Current map: `, currentMap);
|
27365 | currentBaseTime = currentTime;
|
27366 | }
|
27367 |
|
27368 | this.logger_(`MediaSequence sync for ${type} segment loader.
|
27369 | Received media sequence: ${currentMediaSequence}.
|
27370 | base time is ${currentBaseTime}
|
27371 | Current map: `, currentMap);
|
27372 | playlist.segments.forEach(segment => {
|
27373 | const start = currentBaseTime;
|
27374 | const end = start + segment.duration;
|
27375 | const range = {
|
27376 | start,
|
27377 | end
|
27378 | };
|
27379 | result.set(currentMediaSequence, range);
|
27380 | currentMediaSequence++;
|
27381 | currentBaseTime = end;
|
27382 | });
|
27383 | this.mediaSequenceStorage_.set(type, result);
|
27384 | }
|
27385 | |
27386 |
|
27387 |
|
27388 |
|
27389 |
|
27390 |
|
27391 |
|
27392 |
|
27393 |
|
27394 |
|
27395 |
|
27396 |
|
27397 |
|
27398 |
|
27399 |
|
27400 |
|
27401 |
|
27402 |
|
27403 |
|
27404 |
|
27405 |
|
27406 | getSyncPoint(playlist, duration, currentTimeline, currentTime, type) {
|
27407 |
|
27408 | if (duration !== Infinity) {
|
27409 | const vodSyncPointStrategy = syncPointStrategies.find(({
|
27410 | name
|
27411 | }) => name === 'VOD');
|
27412 | return vodSyncPointStrategy.run(this, playlist, duration);
|
27413 | }
|
27414 |
|
27415 | const syncPoints = this.runStrategies_(playlist, duration, currentTimeline, currentTime, type);
|
27416 |
|
27417 | if (!syncPoints.length) {
|
27418 |
|
27419 |
|
27420 |
|
27421 | return null;
|
27422 | }
|
27423 |
|
27424 |
|
27425 | for (const syncPointInfo of syncPoints) {
|
27426 | const {
|
27427 | syncPoint,
|
27428 | strategy
|
27429 | } = syncPointInfo;
|
27430 | const {
|
27431 | segmentIndex,
|
27432 | time
|
27433 | } = syncPoint;
|
27434 |
|
27435 | if (segmentIndex < 0) {
|
27436 | continue;
|
27437 | }
|
27438 |
|
27439 | const selectedSegment = playlist.segments[segmentIndex];
|
27440 | const start = time;
|
27441 | const end = start + selectedSegment.duration;
|
27442 | this.logger_(`Strategy: ${strategy}. Current time: ${currentTime}. selected segment: ${segmentIndex}. Time: [${start} -> ${end}]}`);
|
27443 |
|
27444 | if (currentTime >= start && currentTime < end) {
|
27445 | this.logger_('Found sync point with exact match: ', syncPoint);
|
27446 | return syncPoint;
|
27447 | }
|
27448 | }
|
27449 |
|
27450 |
|
27451 |
|
27452 |
|
27453 | return this.selectSyncPoint_(syncPoints, {
|
27454 | key: 'time',
|
27455 | value: currentTime
|
27456 | });
|
27457 | }
|
27458 | |
27459 |
|
27460 |
|
27461 |
|
27462 |
|
27463 |
|
27464 |
|
27465 |
|
27466 |
|
27467 |
|
27468 |
|
27469 |
|
27470 |
|
27471 | getExpiredTime(playlist, duration) {
|
27472 | if (!playlist || !playlist.segments) {
|
27473 | return null;
|
27474 | }
|
27475 |
|
27476 | const syncPoints = this.runStrategies_(playlist, duration, playlist.discontinuitySequence, 0, 'main');
|
27477 |
|
27478 | if (!syncPoints.length) {
|
27479 | return null;
|
27480 | }
|
27481 |
|
27482 | const syncPoint = this.selectSyncPoint_(syncPoints, {
|
27483 | key: 'segmentIndex',
|
27484 | value: 0
|
27485 | });
|
27486 |
|
27487 |
|
27488 | if (syncPoint.segmentIndex > 0) {
|
27489 | syncPoint.time *= -1;
|
27490 | }
|
27491 |
|
27492 | return Math.abs(syncPoint.time + sumDurations({
|
27493 | defaultDuration: playlist.targetDuration,
|
27494 | durationList: playlist.segments,
|
27495 | startIndex: syncPoint.segmentIndex,
|
27496 | endIndex: 0
|
27497 | }));
|
27498 | }
|
27499 | |
27500 |
|
27501 |
|
27502 |
|
27503 |
|
27504 |
|
27505 |
|
27506 |
|
27507 |
|
27508 |
|
27509 |
|
27510 |
|
27511 |
|
27512 |
|
27513 |
|
27514 |
|
27515 |
|
27516 |
|
27517 |
|
27518 |
|
27519 | runStrategies_(playlist, duration, currentTimeline, currentTime, type) {
|
27520 | const syncPoints = [];
|
27521 |
|
27522 | for (let i = 0; i < syncPointStrategies.length; i++) {
|
27523 | const strategy = syncPointStrategies[i];
|
27524 | const syncPoint = strategy.run(this, playlist, duration, currentTimeline, currentTime, type);
|
27525 |
|
27526 | if (syncPoint) {
|
27527 | syncPoint.strategy = strategy.name;
|
27528 | syncPoints.push({
|
27529 | strategy: strategy.name,
|
27530 | syncPoint
|
27531 | });
|
27532 | }
|
27533 | }
|
27534 |
|
27535 | return syncPoints;
|
27536 | }
|
27537 | |
27538 |
|
27539 |
|
27540 |
|
27541 |
|
27542 |
|
27543 |
|
27544 |
|
27545 |
|
27546 |
|
27547 |
|
27548 |
|
27549 |
|
27550 |
|
27551 |
|
27552 |
|
27553 |
|
27554 | selectSyncPoint_(syncPoints, target) {
|
27555 | let bestSyncPoint = syncPoints[0].syncPoint;
|
27556 | let bestDistance = Math.abs(syncPoints[0].syncPoint[target.key] - target.value);
|
27557 | let bestStrategy = syncPoints[0].strategy;
|
27558 |
|
27559 | for (let i = 1; i < syncPoints.length; i++) {
|
27560 | const newDistance = Math.abs(syncPoints[i].syncPoint[target.key] - target.value);
|
27561 |
|
27562 | if (newDistance < bestDistance) {
|
27563 | bestDistance = newDistance;
|
27564 | bestSyncPoint = syncPoints[i].syncPoint;
|
27565 | bestStrategy = syncPoints[i].strategy;
|
27566 | }
|
27567 | }
|
27568 |
|
27569 | 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}` : '') + ']');
|
27570 | return bestSyncPoint;
|
27571 | }
|
27572 | |
27573 |
|
27574 |
|
27575 |
|
27576 |
|
27577 |
|
27578 |
|
27579 |
|
27580 |
|
27581 |
|
27582 | saveExpiredSegmentInfo(oldPlaylist, newPlaylist) {
|
27583 | const mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
27584 |
|
27585 | if (mediaSequenceDiff > MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC) {
|
27586 | videojs__default["default"].log.warn(`Not saving expired segment info. Media sequence gap ${mediaSequenceDiff} is too large.`);
|
27587 | return;
|
27588 | }
|
27589 |
|
27590 |
|
27591 |
|
27592 | for (let i = mediaSequenceDiff - 1; i >= 0; i--) {
|
27593 | const lastRemovedSegment = oldPlaylist.segments[i];
|
27594 |
|
27595 | if (lastRemovedSegment && typeof lastRemovedSegment.start !== 'undefined') {
|
27596 | newPlaylist.syncInfo = {
|
27597 | mediaSequence: oldPlaylist.mediaSequence + i,
|
27598 | time: lastRemovedSegment.start
|
27599 | };
|
27600 | this.logger_(`playlist refresh sync: [time:${newPlaylist.syncInfo.time},` + ` mediaSequence: ${newPlaylist.syncInfo.mediaSequence}]`);
|
27601 | this.trigger('syncinfoupdate');
|
27602 | break;
|
27603 | }
|
27604 | }
|
27605 | }
|
27606 | |
27607 |
|
27608 |
|
27609 |
|
27610 |
|
27611 |
|
27612 |
|
27613 |
|
27614 | setDateTimeMappingForStart(playlist) {
|
27615 |
|
27616 |
|
27617 |
|
27618 |
|
27619 | this.timelineToDatetimeMappings = {};
|
27620 |
|
27621 | if (playlist.segments && playlist.segments.length && playlist.segments[0].dateTimeObject) {
|
27622 | const firstSegment = playlist.segments[0];
|
27623 | const playlistTimestamp = firstSegment.dateTimeObject.getTime() / 1000;
|
27624 | this.timelineToDatetimeMappings[firstSegment.timeline] = -playlistTimestamp;
|
27625 | }
|
27626 | }
|
27627 | |
27628 |
|
27629 |
|
27630 |
|
27631 |
|
27632 |
|
27633 |
|
27634 |
|
27635 |
|
27636 |
|
27637 |
|
27638 |
|
27639 |
|
27640 |
|
27641 | saveSegmentTimingInfo({
|
27642 | segmentInfo,
|
27643 | shouldSaveTimelineMapping
|
27644 | }) {
|
27645 | const didCalculateSegmentTimeMapping = this.calculateSegmentTimeMapping_(segmentInfo, segmentInfo.timingInfo, shouldSaveTimelineMapping);
|
27646 | const segment = segmentInfo.segment;
|
27647 |
|
27648 | if (didCalculateSegmentTimeMapping) {
|
27649 | this.saveDiscontinuitySyncInfo_(segmentInfo);
|
27650 |
|
27651 |
|
27652 | if (!segmentInfo.playlist.syncInfo) {
|
27653 | segmentInfo.playlist.syncInfo = {
|
27654 | mediaSequence: segmentInfo.playlist.mediaSequence + segmentInfo.mediaIndex,
|
27655 | time: segment.start
|
27656 | };
|
27657 | }
|
27658 | }
|
27659 |
|
27660 | const dateTime = segment.dateTimeObject;
|
27661 |
|
27662 | if (segment.discontinuity && shouldSaveTimelineMapping && dateTime) {
|
27663 | this.timelineToDatetimeMappings[segment.timeline] = -(dateTime.getTime() / 1000);
|
27664 | }
|
27665 | }
|
27666 |
|
27667 | timestampOffsetForTimeline(timeline) {
|
27668 | if (typeof this.timelines[timeline] === 'undefined') {
|
27669 | return null;
|
27670 | }
|
27671 |
|
27672 | return this.timelines[timeline].time;
|
27673 | }
|
27674 |
|
27675 | mappingForTimeline(timeline) {
|
27676 | if (typeof this.timelines[timeline] === 'undefined') {
|
27677 | return null;
|
27678 | }
|
27679 |
|
27680 | return this.timelines[timeline].mapping;
|
27681 | }
|
27682 | |
27683 |
|
27684 |
|
27685 |
|
27686 |
|
27687 |
|
27688 |
|
27689 |
|
27690 |
|
27691 |
|
27692 |
|
27693 |
|
27694 |
|
27695 |
|
27696 |
|
27697 |
|
27698 |
|
27699 | calculateSegmentTimeMapping_(segmentInfo, timingInfo, shouldSaveTimelineMapping) {
|
27700 |
|
27701 | const segment = segmentInfo.segment;
|
27702 | const part = segmentInfo.part;
|
27703 | let mappingObj = this.timelines[segmentInfo.timeline];
|
27704 | let start;
|
27705 | let end;
|
27706 |
|
27707 | if (typeof segmentInfo.timestampOffset === 'number') {
|
27708 | mappingObj = {
|
27709 | time: segmentInfo.startOfSegment,
|
27710 | mapping: segmentInfo.startOfSegment - timingInfo.start
|
27711 | };
|
27712 |
|
27713 | if (shouldSaveTimelineMapping) {
|
27714 | this.timelines[segmentInfo.timeline] = mappingObj;
|
27715 | this.trigger('timestampoffset');
|
27716 | this.logger_(`time mapping for timeline ${segmentInfo.timeline}: ` + `[time: ${mappingObj.time}] [mapping: ${mappingObj.mapping}]`);
|
27717 | }
|
27718 |
|
27719 | start = segmentInfo.startOfSegment;
|
27720 | end = timingInfo.end + mappingObj.mapping;
|
27721 | } else if (mappingObj) {
|
27722 | start = timingInfo.start + mappingObj.mapping;
|
27723 | end = timingInfo.end + mappingObj.mapping;
|
27724 | } else {
|
27725 | return false;
|
27726 | }
|
27727 |
|
27728 | if (part) {
|
27729 | part.start = start;
|
27730 | part.end = end;
|
27731 | }
|
27732 |
|
27733 |
|
27734 |
|
27735 |
|
27736 |
|
27737 |
|
27738 | if (!segment.start || start < segment.start) {
|
27739 | segment.start = start;
|
27740 | }
|
27741 |
|
27742 | segment.end = end;
|
27743 | return true;
|
27744 | }
|
27745 | |
27746 |
|
27747 |
|
27748 |
|
27749 |
|
27750 |
|
27751 |
|
27752 |
|
27753 |
|
27754 |
|
27755 | saveDiscontinuitySyncInfo_(segmentInfo) {
|
27756 | const playlist = segmentInfo.playlist;
|
27757 | const segment = segmentInfo.segment;
|
27758 |
|
27759 |
|
27760 |
|
27761 | if (segment.discontinuity) {
|
27762 | this.discontinuities[segment.timeline] = {
|
27763 | time: segment.start,
|
27764 | accuracy: 0
|
27765 | };
|
27766 | } else if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
27767 |
|
27768 |
|
27769 | for (let i = 0; i < playlist.discontinuityStarts.length; i++) {
|
27770 | const segmentIndex = playlist.discontinuityStarts[i];
|
27771 | const discontinuity = playlist.discontinuitySequence + i + 1;
|
27772 | const mediaIndexDiff = segmentIndex - segmentInfo.mediaIndex;
|
27773 | const accuracy = Math.abs(mediaIndexDiff);
|
27774 |
|
27775 | if (!this.discontinuities[discontinuity] || this.discontinuities[discontinuity].accuracy > accuracy) {
|
27776 | let time;
|
27777 |
|
27778 | if (mediaIndexDiff < 0) {
|
27779 | time = segment.start - sumDurations({
|
27780 | defaultDuration: playlist.targetDuration,
|
27781 | durationList: playlist.segments,
|
27782 | startIndex: segmentInfo.mediaIndex,
|
27783 | endIndex: segmentIndex
|
27784 | });
|
27785 | } else {
|
27786 | time = segment.end + sumDurations({
|
27787 | defaultDuration: playlist.targetDuration,
|
27788 | durationList: playlist.segments,
|
27789 | startIndex: segmentInfo.mediaIndex + 1,
|
27790 | endIndex: segmentIndex
|
27791 | });
|
27792 | }
|
27793 |
|
27794 | this.discontinuities[discontinuity] = {
|
27795 | time,
|
27796 | accuracy
|
27797 | };
|
27798 | }
|
27799 | }
|
27800 | }
|
27801 | }
|
27802 |
|
27803 | dispose() {
|
27804 | this.trigger('dispose');
|
27805 | this.off();
|
27806 | }
|
27807 |
|
27808 | }
|
27809 |
|
27810 | |
27811 |
|
27812 |
|
27813 |
|
27814 |
|
27815 |
|
27816 |
|
27817 |
|
27818 |
|
27819 |
|
27820 | class TimelineChangeController extends videojs__default["default"].EventTarget {
|
27821 | constructor() {
|
27822 | super();
|
27823 | this.pendingTimelineChanges_ = {};
|
27824 | this.lastTimelineChanges_ = {};
|
27825 | }
|
27826 |
|
27827 | clearPendingTimelineChange(type) {
|
27828 | this.pendingTimelineChanges_[type] = null;
|
27829 | this.trigger('pendingtimelinechange');
|
27830 | }
|
27831 |
|
27832 | pendingTimelineChange({
|
27833 | type,
|
27834 | from,
|
27835 | to
|
27836 | }) {
|
27837 | if (typeof from === 'number' && typeof to === 'number') {
|
27838 | this.pendingTimelineChanges_[type] = {
|
27839 | type,
|
27840 | from,
|
27841 | to
|
27842 | };
|
27843 | this.trigger('pendingtimelinechange');
|
27844 | }
|
27845 |
|
27846 | return this.pendingTimelineChanges_[type];
|
27847 | }
|
27848 |
|
27849 | lastTimelineChange({
|
27850 | type,
|
27851 | from,
|
27852 | to
|
27853 | }) {
|
27854 | if (typeof from === 'number' && typeof to === 'number') {
|
27855 | this.lastTimelineChanges_[type] = {
|
27856 | type,
|
27857 | from,
|
27858 | to
|
27859 | };
|
27860 | delete this.pendingTimelineChanges_[type];
|
27861 | this.trigger('timelinechange');
|
27862 | }
|
27863 |
|
27864 | return this.lastTimelineChanges_[type];
|
27865 | }
|
27866 |
|
27867 | dispose() {
|
27868 | this.trigger('dispose');
|
27869 | this.pendingTimelineChanges_ = {};
|
27870 | this.lastTimelineChanges_ = {};
|
27871 | this.off();
|
27872 | }
|
27873 |
|
27874 | }
|
27875 |
|
27876 |
|
27877 | const workerCode = transform(getWorkerString(function () {
|
27878 | |
27879 |
|
27880 |
|
27881 |
|
27882 | |
27883 |
|
27884 |
|
27885 |
|
27886 |
|
27887 |
|
27888 | var Stream = function () {
|
27889 | function Stream() {
|
27890 | this.listeners = {};
|
27891 | }
|
27892 | |
27893 |
|
27894 |
|
27895 |
|
27896 |
|
27897 |
|
27898 |
|
27899 |
|
27900 |
|
27901 | var _proto = Stream.prototype;
|
27902 |
|
27903 | _proto.on = function on(type, listener) {
|
27904 | if (!this.listeners[type]) {
|
27905 | this.listeners[type] = [];
|
27906 | }
|
27907 |
|
27908 | this.listeners[type].push(listener);
|
27909 | }
|
27910 | |
27911 |
|
27912 |
|
27913 |
|
27914 |
|
27915 |
|
27916 |
|
27917 |
|
27918 | ;
|
27919 |
|
27920 | _proto.off = function off(type, listener) {
|
27921 | if (!this.listeners[type]) {
|
27922 | return false;
|
27923 | }
|
27924 |
|
27925 | var index = this.listeners[type].indexOf(listener);
|
27926 |
|
27927 |
|
27928 |
|
27929 |
|
27930 |
|
27931 |
|
27932 |
|
27933 |
|
27934 | this.listeners[type] = this.listeners[type].slice(0);
|
27935 | this.listeners[type].splice(index, 1);
|
27936 | return index > -1;
|
27937 | }
|
27938 | |
27939 |
|
27940 |
|
27941 |
|
27942 |
|
27943 |
|
27944 | ;
|
27945 |
|
27946 | _proto.trigger = function trigger(type) {
|
27947 | var callbacks = this.listeners[type];
|
27948 |
|
27949 | if (!callbacks) {
|
27950 | return;
|
27951 | }
|
27952 |
|
27953 |
|
27954 |
|
27955 |
|
27956 |
|
27957 | if (arguments.length === 2) {
|
27958 | var length = callbacks.length;
|
27959 |
|
27960 | for (var i = 0; i < length; ++i) {
|
27961 | callbacks[i].call(this, arguments[1]);
|
27962 | }
|
27963 | } else {
|
27964 | var args = Array.prototype.slice.call(arguments, 1);
|
27965 | var _length = callbacks.length;
|
27966 |
|
27967 | for (var _i = 0; _i < _length; ++_i) {
|
27968 | callbacks[_i].apply(this, args);
|
27969 | }
|
27970 | }
|
27971 | }
|
27972 | |
27973 |
|
27974 |
|
27975 | ;
|
27976 |
|
27977 | _proto.dispose = function dispose() {
|
27978 | this.listeners = {};
|
27979 | }
|
27980 | |
27981 |
|
27982 |
|
27983 |
|
27984 |
|
27985 |
|
27986 |
|
27987 |
|
27988 | ;
|
27989 |
|
27990 | _proto.pipe = function pipe(destination) {
|
27991 | this.on('data', function (data) {
|
27992 | destination.push(data);
|
27993 | });
|
27994 | };
|
27995 |
|
27996 | return Stream;
|
27997 | }();
|
27998 |
|
27999 |
|
28000 | |
28001 |
|
28002 |
|
28003 |
|
28004 |
|
28005 |
|
28006 |
|
28007 |
|
28008 |
|
28009 | function unpad(padded) {
|
28010 | return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
|
28011 | }
|
28012 |
|
28013 |
|
28014 | |
28015 |
|
28016 |
|
28017 |
|
28018 |
|
28019 |
|
28020 |
|
28021 |
|
28022 |
|
28023 |
|
28024 |
|
28025 |
|
28026 |
|
28027 |
|
28028 |
|
28029 |
|
28030 |
|
28031 |
|
28032 |
|
28033 |
|
28034 |
|
28035 |
|
28036 |
|
28037 |
|
28038 |
|
28039 |
|
28040 |
|
28041 |
|
28042 |
|
28043 |
|
28044 |
|
28045 |
|
28046 |
|
28047 |
|
28048 |
|
28049 |
|
28050 |
|
28051 |
|
28052 |
|
28053 | |
28054 |
|
28055 |
|
28056 |
|
28057 |
|
28058 |
|
28059 |
|
28060 | const precompute = function () {
|
28061 | const tables = [[[], [], [], [], []], [[], [], [], [], []]];
|
28062 | const encTable = tables[0];
|
28063 | const decTable = tables[1];
|
28064 | const sbox = encTable[4];
|
28065 | const sboxInv = decTable[4];
|
28066 | let i;
|
28067 | let x;
|
28068 | let xInv;
|
28069 | const d = [];
|
28070 | const th = [];
|
28071 | let x2;
|
28072 | let x4;
|
28073 | let x8;
|
28074 | let s;
|
28075 | let tEnc;
|
28076 | let tDec;
|
28077 |
|
28078 | for (i = 0; i < 256; i++) {
|
28079 | th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
|
28080 | }
|
28081 |
|
28082 | for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
|
28083 |
|
28084 | s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
|
28085 | s = s >> 8 ^ s & 255 ^ 99;
|
28086 | sbox[x] = s;
|
28087 | sboxInv[s] = x;
|
28088 |
|
28089 | x8 = d[x4 = d[x2 = d[x]]];
|
28090 | tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
|
28091 | tEnc = d[s] * 0x101 ^ s * 0x1010100;
|
28092 |
|
28093 | for (i = 0; i < 4; i++) {
|
28094 | encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
|
28095 | decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
|
28096 | }
|
28097 | }
|
28098 |
|
28099 |
|
28100 | for (i = 0; i < 5; i++) {
|
28101 | encTable[i] = encTable[i].slice(0);
|
28102 | decTable[i] = decTable[i].slice(0);
|
28103 | }
|
28104 |
|
28105 | return tables;
|
28106 | };
|
28107 |
|
28108 | let aesTables = null;
|
28109 | |
28110 |
|
28111 |
|
28112 |
|
28113 |
|
28114 |
|
28115 |
|
28116 |
|
28117 | class AES {
|
28118 | constructor(key) {
|
28119 | |
28120 |
|
28121 |
|
28122 |
|
28123 |
|
28124 |
|
28125 |
|
28126 |
|
28127 |
|
28128 |
|
28129 |
|
28130 |
|
28131 |
|
28132 |
|
28133 | if (!aesTables) {
|
28134 | aesTables = precompute();
|
28135 | }
|
28136 |
|
28137 |
|
28138 | 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()]];
|
28139 | let i;
|
28140 | let j;
|
28141 | let tmp;
|
28142 | const sbox = this._tables[0][4];
|
28143 | const decTable = this._tables[1];
|
28144 | const keyLen = key.length;
|
28145 | let rcon = 1;
|
28146 |
|
28147 | if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
|
28148 | throw new Error('Invalid aes key size');
|
28149 | }
|
28150 |
|
28151 | const encKey = key.slice(0);
|
28152 | const decKey = [];
|
28153 | this._key = [encKey, decKey];
|
28154 |
|
28155 | for (i = keyLen; i < 4 * keyLen + 28; i++) {
|
28156 | tmp = encKey[i - 1];
|
28157 |
|
28158 | if (i % keyLen === 0 || keyLen === 8 && i % keyLen === 4) {
|
28159 | tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
|
28160 |
|
28161 | if (i % keyLen === 0) {
|
28162 | tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
|
28163 | rcon = rcon << 1 ^ (rcon >> 7) * 283;
|
28164 | }
|
28165 | }
|
28166 |
|
28167 | encKey[i] = encKey[i - keyLen] ^ tmp;
|
28168 | }
|
28169 |
|
28170 |
|
28171 | for (j = 0; i; j++, i--) {
|
28172 | tmp = encKey[j & 3 ? i : i - 4];
|
28173 |
|
28174 | if (i <= 4 || j < 4) {
|
28175 | decKey[j] = tmp;
|
28176 | } else {
|
28177 | decKey[j] = decTable[0][sbox[tmp >>> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]];
|
28178 | }
|
28179 | }
|
28180 | }
|
28181 | |
28182 |
|
28183 |
|
28184 |
|
28185 |
|
28186 |
|
28187 |
|
28188 |
|
28189 |
|
28190 |
|
28191 |
|
28192 |
|
28193 |
|
28194 |
|
28195 |
|
28196 | decrypt(encrypted0, encrypted1, encrypted2, encrypted3, out, offset) {
|
28197 | const key = this._key[1];
|
28198 |
|
28199 | let a = encrypted0 ^ key[0];
|
28200 | let b = encrypted3 ^ key[1];
|
28201 | let c = encrypted2 ^ key[2];
|
28202 | let d = encrypted1 ^ key[3];
|
28203 | let a2;
|
28204 | let b2;
|
28205 | let c2;
|
28206 |
|
28207 | const nInnerRounds = key.length / 4 - 2;
|
28208 | let i;
|
28209 | let kIndex = 4;
|
28210 | const table = this._tables[1];
|
28211 |
|
28212 | const table0 = table[0];
|
28213 | const table1 = table[1];
|
28214 | const table2 = table[2];
|
28215 | const table3 = table[3];
|
28216 | const sbox = table[4];
|
28217 |
|
28218 | for (i = 0; i < nInnerRounds; i++) {
|
28219 | a2 = table0[a >>> 24] ^ table1[b >> 16 & 255] ^ table2[c >> 8 & 255] ^ table3[d & 255] ^ key[kIndex];
|
28220 | b2 = table0[b >>> 24] ^ table1[c >> 16 & 255] ^ table2[d >> 8 & 255] ^ table3[a & 255] ^ key[kIndex + 1];
|
28221 | c2 = table0[c >>> 24] ^ table1[d >> 16 & 255] ^ table2[a >> 8 & 255] ^ table3[b & 255] ^ key[kIndex + 2];
|
28222 | d = table0[d >>> 24] ^ table1[a >> 16 & 255] ^ table2[b >> 8 & 255] ^ table3[c & 255] ^ key[kIndex + 3];
|
28223 | kIndex += 4;
|
28224 | a = a2;
|
28225 | b = b2;
|
28226 | c = c2;
|
28227 | }
|
28228 |
|
28229 |
|
28230 | for (i = 0; i < 4; i++) {
|
28231 | out[(3 & -i) + offset] = sbox[a >>> 24] << 24 ^ sbox[b >> 16 & 255] << 16 ^ sbox[c >> 8 & 255] << 8 ^ sbox[d & 255] ^ key[kIndex++];
|
28232 | a2 = a;
|
28233 | a = b;
|
28234 | b = c;
|
28235 | c = d;
|
28236 | d = a2;
|
28237 | }
|
28238 | }
|
28239 |
|
28240 | }
|
28241 | |
28242 |
|
28243 |
|
28244 |
|
28245 | |
28246 |
|
28247 |
|
28248 |
|
28249 |
|
28250 |
|
28251 |
|
28252 |
|
28253 |
|
28254 | class AsyncStream extends Stream {
|
28255 | constructor() {
|
28256 | super(Stream);
|
28257 | this.jobs = [];
|
28258 | this.delay = 1;
|
28259 | this.timeout_ = null;
|
28260 | }
|
28261 | |
28262 |
|
28263 |
|
28264 |
|
28265 |
|
28266 |
|
28267 |
|
28268 | processJob_() {
|
28269 | this.jobs.shift()();
|
28270 |
|
28271 | if (this.jobs.length) {
|
28272 | this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
|
28273 | } else {
|
28274 | this.timeout_ = null;
|
28275 | }
|
28276 | }
|
28277 | |
28278 |
|
28279 |
|
28280 |
|
28281 |
|
28282 |
|
28283 |
|
28284 | push(job) {
|
28285 | this.jobs.push(job);
|
28286 |
|
28287 | if (!this.timeout_) {
|
28288 | this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
|
28289 | }
|
28290 | }
|
28291 |
|
28292 | }
|
28293 | |
28294 |
|
28295 |
|
28296 |
|
28297 |
|
28298 |
|
28299 |
|
28300 | |
28301 |
|
28302 |
|
28303 |
|
28304 |
|
28305 |
|
28306 | const ntoh = function (word) {
|
28307 | return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
|
28308 | };
|
28309 | |
28310 |
|
28311 |
|
28312 |
|
28313 |
|
28314 |
|
28315 |
|
28316 |
|
28317 |
|
28318 |
|
28319 |
|
28320 |
|
28321 |
|
28322 |
|
28323 |
|
28324 | const decrypt = function (encrypted, key, initVector) {
|
28325 |
|
28326 | const encrypted32 = new Int32Array(encrypted.buffer, encrypted.byteOffset, encrypted.byteLength >> 2);
|
28327 | const decipher = new AES(Array.prototype.slice.call(key));
|
28328 |
|
28329 | const decrypted = new Uint8Array(encrypted.byteLength);
|
28330 | const decrypted32 = new Int32Array(decrypted.buffer);
|
28331 |
|
28332 |
|
28333 | let init0;
|
28334 | let init1;
|
28335 | let init2;
|
28336 | let init3;
|
28337 | let encrypted0;
|
28338 | let encrypted1;
|
28339 | let encrypted2;
|
28340 | let encrypted3;
|
28341 |
|
28342 | let wordIx;
|
28343 |
|
28344 |
|
28345 | init0 = initVector[0];
|
28346 | init1 = initVector[1];
|
28347 | init2 = initVector[2];
|
28348 | init3 = initVector[3];
|
28349 |
|
28350 |
|
28351 | for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
|
28352 |
|
28353 |
|
28354 | encrypted0 = ntoh(encrypted32[wordIx]);
|
28355 | encrypted1 = ntoh(encrypted32[wordIx + 1]);
|
28356 | encrypted2 = ntoh(encrypted32[wordIx + 2]);
|
28357 | encrypted3 = ntoh(encrypted32[wordIx + 3]);
|
28358 |
|
28359 | decipher.decrypt(encrypted0, encrypted1, encrypted2, encrypted3, decrypted32, wordIx);
|
28360 |
|
28361 |
|
28362 | decrypted32[wordIx] = ntoh(decrypted32[wordIx] ^ init0);
|
28363 | decrypted32[wordIx + 1] = ntoh(decrypted32[wordIx + 1] ^ init1);
|
28364 | decrypted32[wordIx + 2] = ntoh(decrypted32[wordIx + 2] ^ init2);
|
28365 | decrypted32[wordIx + 3] = ntoh(decrypted32[wordIx + 3] ^ init3);
|
28366 |
|
28367 | init0 = encrypted0;
|
28368 | init1 = encrypted1;
|
28369 | init2 = encrypted2;
|
28370 | init3 = encrypted3;
|
28371 | }
|
28372 |
|
28373 | return decrypted;
|
28374 | };
|
28375 | |
28376 |
|
28377 |
|
28378 |
|
28379 |
|
28380 |
|
28381 |
|
28382 |
|
28383 |
|
28384 |
|
28385 |
|
28386 |
|
28387 |
|
28388 | class Decrypter {
|
28389 | constructor(encrypted, key, initVector, done) {
|
28390 | const step = Decrypter.STEP;
|
28391 | const encrypted32 = new Int32Array(encrypted.buffer);
|
28392 | const decrypted = new Uint8Array(encrypted.byteLength);
|
28393 | let i = 0;
|
28394 | this.asyncStream_ = new AsyncStream();
|
28395 |
|
28396 | this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
|
28397 |
|
28398 | for (i = step; i < encrypted32.length; i += step) {
|
28399 | initVector = new Uint32Array([ntoh(encrypted32[i - 4]), ntoh(encrypted32[i - 3]), ntoh(encrypted32[i - 2]), ntoh(encrypted32[i - 1])]);
|
28400 | this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
|
28401 | }
|
28402 |
|
28403 |
|
28404 | this.asyncStream_.push(function () {
|
28405 |
|
28406 | done(null, unpad(decrypted));
|
28407 | });
|
28408 | }
|
28409 | |
28410 |
|
28411 |
|
28412 |
|
28413 |
|
28414 |
|
28415 |
|
28416 | static get STEP() {
|
28417 |
|
28418 | return 32000;
|
28419 | }
|
28420 | |
28421 |
|
28422 |
|
28423 |
|
28424 |
|
28425 | decryptChunk_(encrypted, key, initVector, decrypted) {
|
28426 | return function () {
|
28427 | const bytes = decrypt(encrypted, key, initVector);
|
28428 | decrypted.set(bytes, encrypted.byteOffset);
|
28429 | };
|
28430 | }
|
28431 |
|
28432 | }
|
28433 |
|
28434 | var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
28435 | var win;
|
28436 |
|
28437 | if (typeof window !== "undefined") {
|
28438 | win = window;
|
28439 | } else if (typeof commonjsGlobal !== "undefined") {
|
28440 | win = commonjsGlobal;
|
28441 | } else if (typeof self !== "undefined") {
|
28442 | win = self;
|
28443 | } else {
|
28444 | win = {};
|
28445 | }
|
28446 |
|
28447 | var window_1 = win;
|
28448 |
|
28449 | var isArrayBufferView = function isArrayBufferView(obj) {
|
28450 | if (ArrayBuffer.isView === 'function') {
|
28451 | return ArrayBuffer.isView(obj);
|
28452 | }
|
28453 |
|
28454 | return obj && obj.buffer instanceof ArrayBuffer;
|
28455 | };
|
28456 |
|
28457 | var BigInt = window_1.BigInt || Number;
|
28458 | [BigInt('0x1'), BigInt('0x100'), BigInt('0x10000'), BigInt('0x1000000'), BigInt('0x100000000'), BigInt('0x10000000000'), BigInt('0x1000000000000'), BigInt('0x100000000000000'), BigInt('0x10000000000000000')];
|
28459 |
|
28460 | (function () {
|
28461 | var a = new Uint16Array([0xFFCC]);
|
28462 | var b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
28463 |
|
28464 | if (b[0] === 0xFF) {
|
28465 | return 'big';
|
28466 | }
|
28467 |
|
28468 | if (b[0] === 0xCC) {
|
28469 | return 'little';
|
28470 | }
|
28471 |
|
28472 | return 'unknown';
|
28473 | })();
|
28474 | |
28475 |
|
28476 |
|
28477 |
|
28478 |
|
28479 |
|
28480 |
|
28481 |
|
28482 |
|
28483 |
|
28484 |
|
28485 |
|
28486 | const createTransferableMessage = function (message) {
|
28487 | const transferable = {};
|
28488 | Object.keys(message).forEach(key => {
|
28489 | const value = message[key];
|
28490 |
|
28491 | if (isArrayBufferView(value)) {
|
28492 | transferable[key] = {
|
28493 | bytes: value.buffer,
|
28494 | byteOffset: value.byteOffset,
|
28495 | byteLength: value.byteLength
|
28496 | };
|
28497 | } else {
|
28498 | transferable[key] = value;
|
28499 | }
|
28500 | });
|
28501 | return transferable;
|
28502 | };
|
28503 |
|
28504 |
|
28505 | |
28506 |
|
28507 |
|
28508 |
|
28509 |
|
28510 |
|
28511 |
|
28512 | self.onmessage = function (event) {
|
28513 | const data = event.data;
|
28514 | const encrypted = new Uint8Array(data.encrypted.bytes, data.encrypted.byteOffset, data.encrypted.byteLength);
|
28515 | const key = new Uint32Array(data.key.bytes, data.key.byteOffset, data.key.byteLength / 4);
|
28516 | const iv = new Uint32Array(data.iv.bytes, data.iv.byteOffset, data.iv.byteLength / 4);
|
28517 |
|
28518 |
|
28519 | new Decrypter(encrypted, key, iv, function (err, bytes) {
|
28520 | self.postMessage(createTransferableMessage({
|
28521 | source: data.source,
|
28522 | decrypted: bytes
|
28523 | }), [bytes.buffer]);
|
28524 | });
|
28525 |
|
28526 | };
|
28527 | }));
|
28528 | var Decrypter = factory(workerCode);
|
28529 |
|
28530 |
|
28531 | |
28532 |
|
28533 |
|
28534 |
|
28535 |
|
28536 |
|
28537 | const audioTrackKind_ = properties => {
|
28538 | let kind = properties.default ? 'main' : 'alternative';
|
28539 |
|
28540 | if (properties.characteristics && properties.characteristics.indexOf('public.accessibility.describes-video') >= 0) {
|
28541 | kind = 'main-desc';
|
28542 | }
|
28543 |
|
28544 | return kind;
|
28545 | };
|
28546 | |
28547 |
|
28548 |
|
28549 |
|
28550 |
|
28551 |
|
28552 |
|
28553 |
|
28554 |
|
28555 |
|
28556 |
|
28557 | const stopLoaders = (segmentLoader, mediaType) => {
|
28558 | segmentLoader.abort();
|
28559 | segmentLoader.pause();
|
28560 |
|
28561 | if (mediaType && mediaType.activePlaylistLoader) {
|
28562 | mediaType.activePlaylistLoader.pause();
|
28563 | mediaType.activePlaylistLoader = null;
|
28564 | }
|
28565 | };
|
28566 | |
28567 |
|
28568 |
|
28569 |
|
28570 |
|
28571 |
|
28572 |
|
28573 |
|
28574 |
|
28575 |
|
28576 | const startLoaders = (playlistLoader, mediaType) => {
|
28577 |
|
28578 |
|
28579 | mediaType.activePlaylistLoader = playlistLoader;
|
28580 | playlistLoader.load();
|
28581 | };
|
28582 | |
28583 |
|
28584 |
|
28585 |
|
28586 |
|
28587 |
|
28588 |
|
28589 |
|
28590 |
|
28591 |
|
28592 |
|
28593 |
|
28594 |
|
28595 |
|
28596 |
|
28597 |
|
28598 | const onGroupChanged = (type, settings) => () => {
|
28599 | const {
|
28600 | segmentLoaders: {
|
28601 | [type]: segmentLoader,
|
28602 | main: mainSegmentLoader
|
28603 | },
|
28604 | mediaTypes: {
|
28605 | [type]: mediaType
|
28606 | }
|
28607 | } = settings;
|
28608 | const activeTrack = mediaType.activeTrack();
|
28609 | const activeGroup = mediaType.getActiveGroup();
|
28610 | const previousActiveLoader = mediaType.activePlaylistLoader;
|
28611 | const lastGroup = mediaType.lastGroup_;
|
28612 |
|
28613 | if (activeGroup && lastGroup && activeGroup.id === lastGroup.id) {
|
28614 | return;
|
28615 | }
|
28616 |
|
28617 | mediaType.lastGroup_ = activeGroup;
|
28618 | mediaType.lastTrack_ = activeTrack;
|
28619 | stopLoaders(segmentLoader, mediaType);
|
28620 |
|
28621 | if (!activeGroup || activeGroup.isMainPlaylist) {
|
28622 |
|
28623 | return;
|
28624 | }
|
28625 |
|
28626 | if (!activeGroup.playlistLoader) {
|
28627 | if (previousActiveLoader) {
|
28628 |
|
28629 |
|
28630 |
|
28631 |
|
28632 | mainSegmentLoader.resetEverything();
|
28633 | }
|
28634 |
|
28635 | return;
|
28636 | }
|
28637 |
|
28638 |
|
28639 | segmentLoader.resyncLoader();
|
28640 | startLoaders(activeGroup.playlistLoader, mediaType);
|
28641 | };
|
28642 | const onGroupChanging = (type, settings) => () => {
|
28643 | const {
|
28644 | segmentLoaders: {
|
28645 | [type]: segmentLoader
|
28646 | },
|
28647 | mediaTypes: {
|
28648 | [type]: mediaType
|
28649 | }
|
28650 | } = settings;
|
28651 | mediaType.lastGroup_ = null;
|
28652 | segmentLoader.abort();
|
28653 | segmentLoader.pause();
|
28654 | };
|
28655 | |
28656 |
|
28657 |
|
28658 |
|
28659 |
|
28660 |
|
28661 |
|
28662 |
|
28663 |
|
28664 |
|
28665 |
|
28666 |
|
28667 |
|
28668 |
|
28669 |
|
28670 | const onTrackChanged = (type, settings) => () => {
|
28671 | const {
|
28672 | mainPlaylistLoader,
|
28673 | segmentLoaders: {
|
28674 | [type]: segmentLoader,
|
28675 | main: mainSegmentLoader
|
28676 | },
|
28677 | mediaTypes: {
|
28678 | [type]: mediaType
|
28679 | }
|
28680 | } = settings;
|
28681 | const activeTrack = mediaType.activeTrack();
|
28682 | const activeGroup = mediaType.getActiveGroup();
|
28683 | const previousActiveLoader = mediaType.activePlaylistLoader;
|
28684 | const lastTrack = mediaType.lastTrack_;
|
28685 |
|
28686 | if (lastTrack && activeTrack && lastTrack.id === activeTrack.id) {
|
28687 | return;
|
28688 | }
|
28689 |
|
28690 | mediaType.lastGroup_ = activeGroup;
|
28691 | mediaType.lastTrack_ = activeTrack;
|
28692 | stopLoaders(segmentLoader, mediaType);
|
28693 |
|
28694 | if (!activeGroup) {
|
28695 |
|
28696 | return;
|
28697 | }
|
28698 |
|
28699 | if (activeGroup.isMainPlaylist) {
|
28700 |
|
28701 | if (!activeTrack || !lastTrack || activeTrack.id === lastTrack.id) {
|
28702 | return;
|
28703 | }
|
28704 |
|
28705 | const pc = settings.vhs.playlistController_;
|
28706 | const newPlaylist = pc.selectPlaylist();
|
28707 |
|
28708 | if (pc.media() === newPlaylist) {
|
28709 | return;
|
28710 | }
|
28711 |
|
28712 | mediaType.logger_(`track change. Switching main audio from ${lastTrack.id} to ${activeTrack.id}`);
|
28713 | mainPlaylistLoader.pause();
|
28714 | mainSegmentLoader.resetEverything();
|
28715 | pc.fastQualityChange_(newPlaylist);
|
28716 | return;
|
28717 | }
|
28718 |
|
28719 | if (type === 'AUDIO') {
|
28720 | if (!activeGroup.playlistLoader) {
|
28721 |
|
28722 |
|
28723 |
|
28724 | mainSegmentLoader.setAudio(true);
|
28725 |
|
28726 |
|
28727 | mainSegmentLoader.resetEverything();
|
28728 | return;
|
28729 | }
|
28730 |
|
28731 |
|
28732 |
|
28733 |
|
28734 | segmentLoader.setAudio(true);
|
28735 | mainSegmentLoader.setAudio(false);
|
28736 | }
|
28737 |
|
28738 | if (previousActiveLoader === activeGroup.playlistLoader) {
|
28739 |
|
28740 |
|
28741 |
|
28742 | startLoaders(activeGroup.playlistLoader, mediaType);
|
28743 | return;
|
28744 | }
|
28745 |
|
28746 | if (segmentLoader.track) {
|
28747 |
|
28748 | segmentLoader.track(activeTrack);
|
28749 | }
|
28750 |
|
28751 |
|
28752 | segmentLoader.resetEverything();
|
28753 | startLoaders(activeGroup.playlistLoader, mediaType);
|
28754 | };
|
28755 | const onError = {
|
28756 | |
28757 |
|
28758 |
|
28759 |
|
28760 |
|
28761 |
|
28762 |
|
28763 |
|
28764 |
|
28765 |
|
28766 |
|
28767 |
|
28768 |
|
28769 | AUDIO: (type, settings) => () => {
|
28770 | const {
|
28771 | mediaTypes: {
|
28772 | [type]: mediaType
|
28773 | },
|
28774 | excludePlaylist
|
28775 | } = settings;
|
28776 |
|
28777 | const activeTrack = mediaType.activeTrack();
|
28778 | const activeGroup = mediaType.activeGroup();
|
28779 | const id = (activeGroup.filter(group => group.default)[0] || activeGroup[0]).id;
|
28780 | const defaultTrack = mediaType.tracks[id];
|
28781 |
|
28782 | if (activeTrack === defaultTrack) {
|
28783 |
|
28784 |
|
28785 | excludePlaylist({
|
28786 | error: {
|
28787 | message: 'Problem encountered loading the default audio track.'
|
28788 | }
|
28789 | });
|
28790 | return;
|
28791 | }
|
28792 |
|
28793 | videojs__default["default"].log.warn('Problem encountered loading the alternate audio track.' + 'Switching back to default.');
|
28794 |
|
28795 | for (const trackId in mediaType.tracks) {
|
28796 | mediaType.tracks[trackId].enabled = mediaType.tracks[trackId] === defaultTrack;
|
28797 | }
|
28798 |
|
28799 | mediaType.onTrackChanged();
|
28800 | },
|
28801 |
|
28802 | |
28803 |
|
28804 |
|
28805 |
|
28806 |
|
28807 |
|
28808 |
|
28809 |
|
28810 |
|
28811 |
|
28812 |
|
28813 |
|
28814 | SUBTITLES: (type, settings) => () => {
|
28815 | const {
|
28816 | mediaTypes: {
|
28817 | [type]: mediaType
|
28818 | }
|
28819 | } = settings;
|
28820 | videojs__default["default"].log.warn('Problem encountered loading the subtitle track.' + 'Disabling subtitle track.');
|
28821 | const track = mediaType.activeTrack();
|
28822 |
|
28823 | if (track) {
|
28824 | track.mode = 'disabled';
|
28825 | }
|
28826 |
|
28827 | mediaType.onTrackChanged();
|
28828 | }
|
28829 | };
|
28830 | const setupListeners = {
|
28831 | |
28832 |
|
28833 |
|
28834 |
|
28835 |
|
28836 |
|
28837 |
|
28838 |
|
28839 |
|
28840 |
|
28841 |
|
28842 | AUDIO: (type, playlistLoader, settings) => {
|
28843 | if (!playlistLoader) {
|
28844 |
|
28845 | return;
|
28846 | }
|
28847 |
|
28848 | const {
|
28849 | tech,
|
28850 | requestOptions,
|
28851 | segmentLoaders: {
|
28852 | [type]: segmentLoader
|
28853 | }
|
28854 | } = settings;
|
28855 | playlistLoader.on('loadedmetadata', () => {
|
28856 | const media = playlistLoader.media();
|
28857 | segmentLoader.playlist(media, requestOptions);
|
28858 |
|
28859 |
|
28860 | if (!tech.paused() || media.endList && tech.preload() !== 'none') {
|
28861 | segmentLoader.load();
|
28862 | }
|
28863 | });
|
28864 | playlistLoader.on('loadedplaylist', () => {
|
28865 | segmentLoader.playlist(playlistLoader.media(), requestOptions);
|
28866 |
|
28867 | if (!tech.paused()) {
|
28868 | segmentLoader.load();
|
28869 | }
|
28870 | });
|
28871 | playlistLoader.on('error', onError[type](type, settings));
|
28872 | },
|
28873 |
|
28874 | |
28875 |
|
28876 |
|
28877 |
|
28878 |
|
28879 |
|
28880 |
|
28881 |
|
28882 |
|
28883 |
|
28884 |
|
28885 | SUBTITLES: (type, playlistLoader, settings) => {
|
28886 | const {
|
28887 | tech,
|
28888 | requestOptions,
|
28889 | segmentLoaders: {
|
28890 | [type]: segmentLoader
|
28891 | },
|
28892 | mediaTypes: {
|
28893 | [type]: mediaType
|
28894 | }
|
28895 | } = settings;
|
28896 | playlistLoader.on('loadedmetadata', () => {
|
28897 | const media = playlistLoader.media();
|
28898 | segmentLoader.playlist(media, requestOptions);
|
28899 | segmentLoader.track(mediaType.activeTrack());
|
28900 |
|
28901 |
|
28902 | if (!tech.paused() || media.endList && tech.preload() !== 'none') {
|
28903 | segmentLoader.load();
|
28904 | }
|
28905 | });
|
28906 | playlistLoader.on('loadedplaylist', () => {
|
28907 | segmentLoader.playlist(playlistLoader.media(), requestOptions);
|
28908 |
|
28909 | if (!tech.paused()) {
|
28910 | segmentLoader.load();
|
28911 | }
|
28912 | });
|
28913 | playlistLoader.on('error', onError[type](type, settings));
|
28914 | }
|
28915 | };
|
28916 | const initialize = {
|
28917 | |
28918 |
|
28919 |
|
28920 |
|
28921 |
|
28922 |
|
28923 |
|
28924 |
|
28925 |
|
28926 | 'AUDIO': (type, settings) => {
|
28927 | const {
|
28928 | vhs,
|
28929 | sourceType,
|
28930 | segmentLoaders: {
|
28931 | [type]: segmentLoader
|
28932 | },
|
28933 | requestOptions,
|
28934 | main: {
|
28935 | mediaGroups
|
28936 | },
|
28937 | mediaTypes: {
|
28938 | [type]: {
|
28939 | groups,
|
28940 | tracks,
|
28941 | logger_
|
28942 | }
|
28943 | },
|
28944 | mainPlaylistLoader
|
28945 | } = settings;
|
28946 | const audioOnlyMain = isAudioOnly(mainPlaylistLoader.main);
|
28947 |
|
28948 | if (!mediaGroups[type] || Object.keys(mediaGroups[type]).length === 0) {
|
28949 | mediaGroups[type] = {
|
28950 | main: {
|
28951 | default: {
|
28952 | default: true
|
28953 | }
|
28954 | }
|
28955 | };
|
28956 |
|
28957 | if (audioOnlyMain) {
|
28958 | mediaGroups[type].main.default.playlists = mainPlaylistLoader.main.playlists;
|
28959 | }
|
28960 | }
|
28961 |
|
28962 | for (const groupId in mediaGroups[type]) {
|
28963 | if (!groups[groupId]) {
|
28964 | groups[groupId] = [];
|
28965 | }
|
28966 |
|
28967 | for (const variantLabel in mediaGroups[type][groupId]) {
|
28968 | let properties = mediaGroups[type][groupId][variantLabel];
|
28969 | let playlistLoader;
|
28970 |
|
28971 | if (audioOnlyMain) {
|
28972 | logger_(`AUDIO group '${groupId}' label '${variantLabel}' is a main playlist`);
|
28973 | properties.isMainPlaylist = true;
|
28974 | playlistLoader = null;
|
28975 |
|
28976 | } else if (sourceType === 'vhs-json' && properties.playlists) {
|
28977 | playlistLoader = new PlaylistLoader(properties.playlists[0], vhs, requestOptions);
|
28978 | } else if (properties.resolvedUri) {
|
28979 | playlistLoader = new PlaylistLoader(properties.resolvedUri, vhs, requestOptions);
|
28980 |
|
28981 | } else if (properties.playlists && sourceType === 'dash') {
|
28982 | playlistLoader = new DashPlaylistLoader(properties.playlists[0], vhs, requestOptions, mainPlaylistLoader);
|
28983 | } else {
|
28984 |
|
28985 |
|
28986 | playlistLoader = null;
|
28987 | }
|
28988 |
|
28989 | properties = merge$1({
|
28990 | id: variantLabel,
|
28991 | playlistLoader
|
28992 | }, properties);
|
28993 | setupListeners[type](type, properties.playlistLoader, settings);
|
28994 | groups[groupId].push(properties);
|
28995 |
|
28996 | if (typeof tracks[variantLabel] === 'undefined') {
|
28997 | const track = new videojs__default["default"].AudioTrack({
|
28998 | id: variantLabel,
|
28999 | kind: audioTrackKind_(properties),
|
29000 | enabled: false,
|
29001 | language: properties.language,
|
29002 | default: properties.default,
|
29003 | label: variantLabel
|
29004 | });
|
29005 | tracks[variantLabel] = track;
|
29006 | }
|
29007 | }
|
29008 | }
|
29009 |
|
29010 |
|
29011 | segmentLoader.on('error', onError[type](type, settings));
|
29012 | },
|
29013 |
|
29014 | |
29015 |
|
29016 |
|
29017 |
|
29018 |
|
29019 |
|
29020 |
|
29021 |
|
29022 |
|
29023 | 'SUBTITLES': (type, settings) => {
|
29024 | const {
|
29025 | tech,
|
29026 | vhs,
|
29027 | sourceType,
|
29028 | segmentLoaders: {
|
29029 | [type]: segmentLoader
|
29030 | },
|
29031 | requestOptions,
|
29032 | main: {
|
29033 | mediaGroups
|
29034 | },
|
29035 | mediaTypes: {
|
29036 | [type]: {
|
29037 | groups,
|
29038 | tracks
|
29039 | }
|
29040 | },
|
29041 | mainPlaylistLoader
|
29042 | } = settings;
|
29043 |
|
29044 | for (const groupId in mediaGroups[type]) {
|
29045 | if (!groups[groupId]) {
|
29046 | groups[groupId] = [];
|
29047 | }
|
29048 |
|
29049 | for (const variantLabel in mediaGroups[type][groupId]) {
|
29050 | if (!vhs.options_.useForcedSubtitles && mediaGroups[type][groupId][variantLabel].forced) {
|
29051 |
|
29052 |
|
29053 |
|
29054 |
|
29055 |
|
29056 |
|
29057 |
|
29058 |
|
29059 | continue;
|
29060 | }
|
29061 |
|
29062 | let properties = mediaGroups[type][groupId][variantLabel];
|
29063 | let playlistLoader;
|
29064 |
|
29065 | if (sourceType === 'hls') {
|
29066 | playlistLoader = new PlaylistLoader(properties.resolvedUri, vhs, requestOptions);
|
29067 | } else if (sourceType === 'dash') {
|
29068 | const playlists = properties.playlists.filter(p => p.excludeUntil !== Infinity);
|
29069 |
|
29070 | if (!playlists.length) {
|
29071 | return;
|
29072 | }
|
29073 |
|
29074 | playlistLoader = new DashPlaylistLoader(properties.playlists[0], vhs, requestOptions, mainPlaylistLoader);
|
29075 | } else if (sourceType === 'vhs-json') {
|
29076 | playlistLoader = new PlaylistLoader(
|
29077 |
|
29078 | properties.playlists ? properties.playlists[0] : properties.resolvedUri, vhs, requestOptions);
|
29079 | }
|
29080 |
|
29081 | properties = merge$1({
|
29082 | id: variantLabel,
|
29083 | playlistLoader
|
29084 | }, properties);
|
29085 | setupListeners[type](type, properties.playlistLoader, settings);
|
29086 | groups[groupId].push(properties);
|
29087 |
|
29088 | if (typeof tracks[variantLabel] === 'undefined') {
|
29089 | const track = tech.addRemoteTextTrack({
|
29090 | id: variantLabel,
|
29091 | kind: 'subtitles',
|
29092 | default: properties.default && properties.autoselect,
|
29093 | language: properties.language,
|
29094 | label: variantLabel
|
29095 | }, false).track;
|
29096 | tracks[variantLabel] = track;
|
29097 | }
|
29098 | }
|
29099 | }
|
29100 |
|
29101 |
|
29102 | segmentLoader.on('error', onError[type](type, settings));
|
29103 | },
|
29104 |
|
29105 | |
29106 |
|
29107 |
|
29108 |
|
29109 |
|
29110 |
|
29111 |
|
29112 |
|
29113 |
|
29114 | 'CLOSED-CAPTIONS': (type, settings) => {
|
29115 | const {
|
29116 | tech,
|
29117 | main: {
|
29118 | mediaGroups
|
29119 | },
|
29120 | mediaTypes: {
|
29121 | [type]: {
|
29122 | groups,
|
29123 | tracks
|
29124 | }
|
29125 | }
|
29126 | } = settings;
|
29127 |
|
29128 | for (const groupId in mediaGroups[type]) {
|
29129 | if (!groups[groupId]) {
|
29130 | groups[groupId] = [];
|
29131 | }
|
29132 |
|
29133 | for (const variantLabel in mediaGroups[type][groupId]) {
|
29134 | const properties = mediaGroups[type][groupId][variantLabel];
|
29135 |
|
29136 | if (!/^(?:CC|SERVICE)/.test(properties.instreamId)) {
|
29137 | continue;
|
29138 | }
|
29139 |
|
29140 | const captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
|
29141 | let newProps = {
|
29142 | label: variantLabel,
|
29143 | language: properties.language,
|
29144 | instreamId: properties.instreamId,
|
29145 | default: properties.default && properties.autoselect
|
29146 | };
|
29147 |
|
29148 | if (captionServices[newProps.instreamId]) {
|
29149 | newProps = merge$1(newProps, captionServices[newProps.instreamId]);
|
29150 | }
|
29151 |
|
29152 | if (newProps.default === undefined) {
|
29153 | delete newProps.default;
|
29154 | }
|
29155 |
|
29156 |
|
29157 |
|
29158 | groups[groupId].push(merge$1({
|
29159 | id: variantLabel
|
29160 | }, properties));
|
29161 |
|
29162 | if (typeof tracks[variantLabel] === 'undefined') {
|
29163 | const track = tech.addRemoteTextTrack({
|
29164 | id: newProps.instreamId,
|
29165 | kind: 'captions',
|
29166 | default: newProps.default,
|
29167 | language: newProps.language,
|
29168 | label: newProps.label
|
29169 | }, false).track;
|
29170 | tracks[variantLabel] = track;
|
29171 | }
|
29172 | }
|
29173 | }
|
29174 | }
|
29175 | };
|
29176 |
|
29177 | const groupMatch = (list, media) => {
|
29178 | for (let i = 0; i < list.length; i++) {
|
29179 | if (playlistMatch(media, list[i])) {
|
29180 | return true;
|
29181 | }
|
29182 |
|
29183 | if (list[i].playlists && groupMatch(list[i].playlists, media)) {
|
29184 | return true;
|
29185 | }
|
29186 | }
|
29187 |
|
29188 | return false;
|
29189 | };
|
29190 | |
29191 |
|
29192 |
|
29193 |
|
29194 |
|
29195 |
|
29196 |
|
29197 |
|
29198 |
|
29199 |
|
29200 |
|
29201 |
|
29202 |
|
29203 |
|
29204 |
|
29205 |
|
29206 | const activeGroup = (type, settings) => track => {
|
29207 | const {
|
29208 | mainPlaylistLoader,
|
29209 | mediaTypes: {
|
29210 | [type]: {
|
29211 | groups
|
29212 | }
|
29213 | }
|
29214 | } = settings;
|
29215 | const media = mainPlaylistLoader.media();
|
29216 |
|
29217 | if (!media) {
|
29218 | return null;
|
29219 | }
|
29220 |
|
29221 | let variants = null;
|
29222 |
|
29223 | if (media.attributes[type]) {
|
29224 | variants = groups[media.attributes[type]];
|
29225 | }
|
29226 |
|
29227 | const groupKeys = Object.keys(groups);
|
29228 |
|
29229 | if (!variants) {
|
29230 |
|
29231 |
|
29232 |
|
29233 | if (type === 'AUDIO' && groupKeys.length > 1 && isAudioOnly(settings.main)) {
|
29234 | for (let i = 0; i < groupKeys.length; i++) {
|
29235 | const groupPropertyList = groups[groupKeys[i]];
|
29236 |
|
29237 | if (groupMatch(groupPropertyList, media)) {
|
29238 | variants = groupPropertyList;
|
29239 | break;
|
29240 | }
|
29241 | }
|
29242 |
|
29243 | } else if (groups.main) {
|
29244 | variants = groups.main;
|
29245 | } else if (groupKeys.length === 1) {
|
29246 | variants = groups[groupKeys[0]];
|
29247 | }
|
29248 | }
|
29249 |
|
29250 | if (typeof track === 'undefined') {
|
29251 | return variants;
|
29252 | }
|
29253 |
|
29254 | if (track === null || !variants) {
|
29255 |
|
29256 |
|
29257 | return null;
|
29258 | }
|
29259 |
|
29260 | return variants.filter(props => props.id === track.id)[0] || null;
|
29261 | };
|
29262 | const activeTrack = {
|
29263 | |
29264 |
|
29265 |
|
29266 |
|
29267 |
|
29268 |
|
29269 |
|
29270 |
|
29271 |
|
29272 |
|
29273 |
|
29274 |
|
29275 | AUDIO: (type, settings) => () => {
|
29276 | const {
|
29277 | mediaTypes: {
|
29278 | [type]: {
|
29279 | tracks
|
29280 | }
|
29281 | }
|
29282 | } = settings;
|
29283 |
|
29284 | for (const id in tracks) {
|
29285 | if (tracks[id].enabled) {
|
29286 | return tracks[id];
|
29287 | }
|
29288 | }
|
29289 |
|
29290 | return null;
|
29291 | },
|
29292 |
|
29293 | |
29294 |
|
29295 |
|
29296 |
|
29297 |
|
29298 |
|
29299 |
|
29300 |
|
29301 |
|
29302 |
|
29303 |
|
29304 |
|
29305 | SUBTITLES: (type, settings) => () => {
|
29306 | const {
|
29307 | mediaTypes: {
|
29308 | [type]: {
|
29309 | tracks
|
29310 | }
|
29311 | }
|
29312 | } = settings;
|
29313 |
|
29314 | for (const id in tracks) {
|
29315 | if (tracks[id].mode === 'showing' || tracks[id].mode === 'hidden') {
|
29316 | return tracks[id];
|
29317 | }
|
29318 | }
|
29319 |
|
29320 | return null;
|
29321 | }
|
29322 | };
|
29323 | const getActiveGroup = (type, {
|
29324 | mediaTypes
|
29325 | }) => () => {
|
29326 | const activeTrack_ = mediaTypes[type].activeTrack();
|
29327 |
|
29328 | if (!activeTrack_) {
|
29329 | return null;
|
29330 | }
|
29331 |
|
29332 | return mediaTypes[type].activeGroup(activeTrack_);
|
29333 | };
|
29334 | |
29335 |
|
29336 |
|
29337 |
|
29338 |
|
29339 |
|
29340 |
|
29341 |
|
29342 |
|
29343 |
|
29344 |
|
29345 |
|
29346 |
|
29347 |
|
29348 |
|
29349 |
|
29350 |
|
29351 |
|
29352 |
|
29353 |
|
29354 |
|
29355 |
|
29356 |
|
29357 | const setupMediaGroups = settings => {
|
29358 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(type => {
|
29359 | initialize[type](type, settings);
|
29360 | });
|
29361 | const {
|
29362 | mediaTypes,
|
29363 | mainPlaylistLoader,
|
29364 | tech,
|
29365 | vhs,
|
29366 | segmentLoaders: {
|
29367 | ['AUDIO']: audioSegmentLoader,
|
29368 | main: mainSegmentLoader
|
29369 | }
|
29370 | } = settings;
|
29371 |
|
29372 | ['AUDIO', 'SUBTITLES'].forEach(type => {
|
29373 | mediaTypes[type].activeGroup = activeGroup(type, settings);
|
29374 | mediaTypes[type].activeTrack = activeTrack[type](type, settings);
|
29375 | mediaTypes[type].onGroupChanged = onGroupChanged(type, settings);
|
29376 | mediaTypes[type].onGroupChanging = onGroupChanging(type, settings);
|
29377 | mediaTypes[type].onTrackChanged = onTrackChanged(type, settings);
|
29378 | mediaTypes[type].getActiveGroup = getActiveGroup(type, settings);
|
29379 | });
|
29380 |
|
29381 |
|
29382 | const audioGroup = mediaTypes.AUDIO.activeGroup();
|
29383 |
|
29384 | if (audioGroup) {
|
29385 | const groupId = (audioGroup.filter(group => group.default)[0] || audioGroup[0]).id;
|
29386 | mediaTypes.AUDIO.tracks[groupId].enabled = true;
|
29387 | mediaTypes.AUDIO.onGroupChanged();
|
29388 | mediaTypes.AUDIO.onTrackChanged();
|
29389 | const activeAudioGroup = mediaTypes.AUDIO.getActiveGroup();
|
29390 |
|
29391 |
|
29392 |
|
29393 | if (!activeAudioGroup.playlistLoader) {
|
29394 |
|
29395 | mainSegmentLoader.setAudio(true);
|
29396 | } else {
|
29397 |
|
29398 | mainSegmentLoader.setAudio(false);
|
29399 | audioSegmentLoader.setAudio(true);
|
29400 | }
|
29401 | }
|
29402 |
|
29403 | mainPlaylistLoader.on('mediachange', () => {
|
29404 | ['AUDIO', 'SUBTITLES'].forEach(type => mediaTypes[type].onGroupChanged());
|
29405 | });
|
29406 | mainPlaylistLoader.on('mediachanging', () => {
|
29407 | ['AUDIO', 'SUBTITLES'].forEach(type => mediaTypes[type].onGroupChanging());
|
29408 | });
|
29409 |
|
29410 | const onAudioTrackChanged = () => {
|
29411 | mediaTypes.AUDIO.onTrackChanged();
|
29412 | tech.trigger({
|
29413 | type: 'usage',
|
29414 | name: 'vhs-audio-change'
|
29415 | });
|
29416 | };
|
29417 |
|
29418 | tech.audioTracks().addEventListener('change', onAudioTrackChanged);
|
29419 | tech.remoteTextTracks().addEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
|
29420 | vhs.on('dispose', () => {
|
29421 | tech.audioTracks().removeEventListener('change', onAudioTrackChanged);
|
29422 | tech.remoteTextTracks().removeEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
|
29423 | });
|
29424 |
|
29425 | tech.clearTracks('audio');
|
29426 |
|
29427 | for (const id in mediaTypes.AUDIO.tracks) {
|
29428 | tech.audioTracks().addTrack(mediaTypes.AUDIO.tracks[id]);
|
29429 | }
|
29430 | };
|
29431 | |
29432 |
|
29433 |
|
29434 |
|
29435 |
|
29436 |
|
29437 |
|
29438 |
|
29439 |
|
29440 | const createMediaTypes = () => {
|
29441 | const mediaTypes = {};
|
29442 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(type => {
|
29443 | mediaTypes[type] = {
|
29444 | groups: {},
|
29445 | tracks: {},
|
29446 | activePlaylistLoader: null,
|
29447 | activeGroup: noop,
|
29448 | activeTrack: noop,
|
29449 | getActiveGroup: noop,
|
29450 | onGroupChanged: noop,
|
29451 | onTrackChanged: noop,
|
29452 | lastTrack_: null,
|
29453 | logger_: logger(`MediaGroups[${type}]`)
|
29454 | };
|
29455 | });
|
29456 | return mediaTypes;
|
29457 | };
|
29458 |
|
29459 | |
29460 |
|
29461 |
|
29462 |
|
29463 |
|
29464 |
|
29465 |
|
29466 |
|
29467 |
|
29468 |
|
29469 |
|
29470 | class SteeringManifest {
|
29471 | constructor() {
|
29472 | this.priority_ = [];
|
29473 | this.pathwayClones_ = new Map();
|
29474 | }
|
29475 |
|
29476 | set version(number) {
|
29477 |
|
29478 | if (number === 1) {
|
29479 | this.version_ = number;
|
29480 | }
|
29481 | }
|
29482 |
|
29483 | set ttl(seconds) {
|
29484 |
|
29485 | this.ttl_ = seconds || 300;
|
29486 | }
|
29487 |
|
29488 | set reloadUri(uri) {
|
29489 | if (uri) {
|
29490 |
|
29491 | this.reloadUri_ = resolveUrl(this.reloadUri_, uri);
|
29492 | }
|
29493 | }
|
29494 |
|
29495 | set priority(array) {
|
29496 |
|
29497 | if (array && array.length) {
|
29498 | this.priority_ = array;
|
29499 | }
|
29500 | }
|
29501 |
|
29502 | set pathwayClones(array) {
|
29503 |
|
29504 | if (array && array.length) {
|
29505 | this.pathwayClones_ = new Map(array.map(clone => [clone.ID, clone]));
|
29506 | }
|
29507 | }
|
29508 |
|
29509 | get version() {
|
29510 | return this.version_;
|
29511 | }
|
29512 |
|
29513 | get ttl() {
|
29514 | return this.ttl_;
|
29515 | }
|
29516 |
|
29517 | get reloadUri() {
|
29518 | return this.reloadUri_;
|
29519 | }
|
29520 |
|
29521 | get priority() {
|
29522 | return this.priority_;
|
29523 | }
|
29524 |
|
29525 | get pathwayClones() {
|
29526 | return this.pathwayClones_;
|
29527 | }
|
29528 |
|
29529 | }
|
29530 | |
29531 |
|
29532 |
|
29533 |
|
29534 |
|
29535 |
|
29536 |
|
29537 |
|
29538 |
|
29539 |
|
29540 |
|
29541 | class ContentSteeringController extends videojs__default["default"].EventTarget {
|
29542 | constructor(xhr, bandwidth) {
|
29543 | super();
|
29544 | this.currentPathway = null;
|
29545 | this.defaultPathway = null;
|
29546 | this.queryBeforeStart = false;
|
29547 | this.availablePathways_ = new Set();
|
29548 | this.steeringManifest = new SteeringManifest();
|
29549 | this.proxyServerUrl_ = null;
|
29550 | this.manifestType_ = null;
|
29551 | this.ttlTimeout_ = null;
|
29552 | this.request_ = null;
|
29553 | this.currentPathwayClones = new Map();
|
29554 | this.nextPathwayClones = new Map();
|
29555 | this.excludedSteeringManifestURLs = new Set();
|
29556 | this.logger_ = logger('Content Steering');
|
29557 | this.xhr_ = xhr;
|
29558 | this.getBandwidth_ = bandwidth;
|
29559 | }
|
29560 | |
29561 |
|
29562 |
|
29563 |
|
29564 |
|
29565 |
|
29566 |
|
29567 |
|
29568 | assignTagProperties(baseUrl, steeringTag) {
|
29569 | this.manifestType_ = steeringTag.serverUri ? 'HLS' : 'DASH';
|
29570 |
|
29571 | const steeringUri = steeringTag.serverUri || steeringTag.serverURL;
|
29572 |
|
29573 | if (!steeringUri) {
|
29574 | this.logger_(`steering manifest URL is ${steeringUri}, cannot request steering manifest.`);
|
29575 | this.trigger('error');
|
29576 | return;
|
29577 | }
|
29578 |
|
29579 |
|
29580 | if (steeringUri.startsWith('data:')) {
|
29581 | this.decodeDataUriManifest_(steeringUri.substring(steeringUri.indexOf(',') + 1));
|
29582 | return;
|
29583 | }
|
29584 |
|
29585 |
|
29586 | this.steeringManifest.reloadUri = resolveUrl(baseUrl, steeringUri);
|
29587 |
|
29588 | this.defaultPathway = steeringTag.pathwayId || steeringTag.defaultServiceLocation;
|
29589 |
|
29590 | this.queryBeforeStart = steeringTag.queryBeforeStart;
|
29591 | this.proxyServerUrl_ = steeringTag.proxyServerURL;
|
29592 |
|
29593 |
|
29594 |
|
29595 | if (this.defaultPathway && !this.queryBeforeStart) {
|
29596 | this.trigger('content-steering');
|
29597 | }
|
29598 | }
|
29599 | |
29600 |
|
29601 |
|
29602 |
|
29603 |
|
29604 |
|
29605 |
|
29606 |
|
29607 |
|
29608 |
|
29609 | requestSteeringManifest(initial) {
|
29610 | const reloadUri = this.steeringManifest.reloadUri;
|
29611 |
|
29612 | if (!reloadUri) {
|
29613 | return;
|
29614 | }
|
29615 |
|
29616 |
|
29617 |
|
29618 |
|
29619 | const uri = initial ? reloadUri : this.getRequestURI(reloadUri);
|
29620 |
|
29621 | if (!uri) {
|
29622 | this.logger_('No valid content steering manifest URIs. Stopping content steering.');
|
29623 | this.trigger('error');
|
29624 | this.dispose();
|
29625 | return;
|
29626 | }
|
29627 |
|
29628 | this.request_ = this.xhr_({
|
29629 | uri
|
29630 | }, (error, errorInfo) => {
|
29631 | if (error) {
|
29632 |
|
29633 |
|
29634 |
|
29635 |
|
29636 | if (errorInfo.status === 410) {
|
29637 | this.logger_(`manifest request 410 ${error}.`);
|
29638 | this.logger_(`There will be no more content steering requests to ${uri} this session.`);
|
29639 | this.excludedSteeringManifestURLs.add(uri);
|
29640 | return;
|
29641 | }
|
29642 |
|
29643 |
|
29644 |
|
29645 |
|
29646 | if (errorInfo.status === 429) {
|
29647 | const retrySeconds = errorInfo.responseHeaders['retry-after'];
|
29648 | this.logger_(`manifest request 429 ${error}.`);
|
29649 | this.logger_(`content steering will retry in ${retrySeconds} seconds.`);
|
29650 | this.startTTLTimeout_(parseInt(retrySeconds, 10));
|
29651 | return;
|
29652 | }
|
29653 |
|
29654 |
|
29655 |
|
29656 |
|
29657 |
|
29658 | this.logger_(`manifest failed to load ${error}.`);
|
29659 | this.startTTLTimeout_();
|
29660 | return;
|
29661 | }
|
29662 |
|
29663 | const steeringManifestJson = JSON.parse(this.request_.responseText);
|
29664 | this.assignSteeringProperties_(steeringManifestJson);
|
29665 | this.startTTLTimeout_();
|
29666 | });
|
29667 | }
|
29668 | |
29669 |
|
29670 |
|
29671 |
|
29672 |
|
29673 |
|
29674 |
|
29675 |
|
29676 | setProxyServerUrl_(steeringUrl) {
|
29677 | const steeringUrlObject = new window.URL(steeringUrl);
|
29678 | const proxyServerUrlObject = new window.URL(this.proxyServerUrl_);
|
29679 | proxyServerUrlObject.searchParams.set('url', encodeURI(steeringUrlObject.toString()));
|
29680 | return this.setSteeringParams_(proxyServerUrlObject.toString());
|
29681 | }
|
29682 | |
29683 |
|
29684 |
|
29685 |
|
29686 |
|
29687 |
|
29688 |
|
29689 | decodeDataUriManifest_(dataUri) {
|
29690 | const steeringManifestJson = JSON.parse(window.atob(dataUri));
|
29691 | this.assignSteeringProperties_(steeringManifestJson);
|
29692 | }
|
29693 | |
29694 |
|
29695 |
|
29696 |
|
29697 |
|
29698 |
|
29699 |
|
29700 |
|
29701 |
|
29702 |
|
29703 | setSteeringParams_(url) {
|
29704 | const urlObject = new window.URL(url);
|
29705 | const path = this.getPathway();
|
29706 | const networkThroughput = this.getBandwidth_();
|
29707 |
|
29708 | if (path) {
|
29709 | const pathwayKey = `_${this.manifestType_}_pathway`;
|
29710 | urlObject.searchParams.set(pathwayKey, path);
|
29711 | }
|
29712 |
|
29713 | if (networkThroughput) {
|
29714 | const throughputKey = `_${this.manifestType_}_throughput`;
|
29715 | urlObject.searchParams.set(throughputKey, networkThroughput);
|
29716 | }
|
29717 |
|
29718 | return urlObject.toString();
|
29719 | }
|
29720 | |
29721 |
|
29722 |
|
29723 |
|
29724 |
|
29725 |
|
29726 |
|
29727 | assignSteeringProperties_(steeringJson) {
|
29728 | this.steeringManifest.version = steeringJson.VERSION;
|
29729 |
|
29730 | if (!this.steeringManifest.version) {
|
29731 | this.logger_(`manifest version is ${steeringJson.VERSION}, which is not supported.`);
|
29732 | this.trigger('error');
|
29733 | return;
|
29734 | }
|
29735 |
|
29736 | this.steeringManifest.ttl = steeringJson.TTL;
|
29737 | this.steeringManifest.reloadUri = steeringJson['RELOAD-URI'];
|
29738 |
|
29739 | this.steeringManifest.priority = steeringJson['PATHWAY-PRIORITY'] || steeringJson['SERVICE-LOCATION-PRIORITY'];
|
29740 |
|
29741 |
|
29742 | this.steeringManifest.pathwayClones = steeringJson['PATHWAY-CLONES'];
|
29743 | this.nextPathwayClones = this.steeringManifest.pathwayClones;
|
29744 |
|
29745 |
|
29746 |
|
29747 |
|
29748 |
|
29749 |
|
29750 |
|
29751 |
|
29752 | if (!this.availablePathways_.size) {
|
29753 | this.logger_('There are no available pathways for content steering. Ending content steering.');
|
29754 | this.trigger('error');
|
29755 | this.dispose();
|
29756 | }
|
29757 |
|
29758 | const chooseNextPathway = pathwaysByPriority => {
|
29759 | for (const path of pathwaysByPriority) {
|
29760 | if (this.availablePathways_.has(path)) {
|
29761 | return path;
|
29762 | }
|
29763 | }
|
29764 |
|
29765 |
|
29766 | return [...this.availablePathways_][0];
|
29767 | };
|
29768 |
|
29769 | const nextPathway = chooseNextPathway(this.steeringManifest.priority);
|
29770 |
|
29771 | if (this.currentPathway !== nextPathway) {
|
29772 | this.currentPathway = nextPathway;
|
29773 | this.trigger('content-steering');
|
29774 | }
|
29775 | }
|
29776 | |
29777 |
|
29778 |
|
29779 |
|
29780 |
|
29781 |
|
29782 |
|
29783 | getPathway() {
|
29784 | return this.currentPathway || this.defaultPathway;
|
29785 | }
|
29786 | |
29787 |
|
29788 |
|
29789 |
|
29790 |
|
29791 |
|
29792 |
|
29793 |
|
29794 |
|
29795 |
|
29796 | getRequestURI(reloadUri) {
|
29797 | if (!reloadUri) {
|
29798 | return null;
|
29799 | }
|
29800 |
|
29801 | const isExcluded = uri => this.excludedSteeringManifestURLs.has(uri);
|
29802 |
|
29803 | if (this.proxyServerUrl_) {
|
29804 | const proxyURI = this.setProxyServerUrl_(reloadUri);
|
29805 |
|
29806 | if (!isExcluded(proxyURI)) {
|
29807 | return proxyURI;
|
29808 | }
|
29809 | }
|
29810 |
|
29811 | const steeringURI = this.setSteeringParams_(reloadUri);
|
29812 |
|
29813 | if (!isExcluded(steeringURI)) {
|
29814 | return steeringURI;
|
29815 | }
|
29816 |
|
29817 |
|
29818 | return null;
|
29819 | }
|
29820 | |
29821 |
|
29822 |
|
29823 |
|
29824 |
|
29825 |
|
29826 |
|
29827 |
|
29828 | startTTLTimeout_(ttl = this.steeringManifest.ttl) {
|
29829 |
|
29830 | const ttlMS = ttl * 1000;
|
29831 | this.ttlTimeout_ = window.setTimeout(() => {
|
29832 | this.requestSteeringManifest();
|
29833 | }, ttlMS);
|
29834 | }
|
29835 | |
29836 |
|
29837 |
|
29838 |
|
29839 |
|
29840 | clearTTLTimeout_() {
|
29841 | window.clearTimeout(this.ttlTimeout_);
|
29842 | this.ttlTimeout_ = null;
|
29843 | }
|
29844 | |
29845 |
|
29846 |
|
29847 |
|
29848 |
|
29849 | abort() {
|
29850 | if (this.request_) {
|
29851 | this.request_.abort();
|
29852 | }
|
29853 |
|
29854 | this.request_ = null;
|
29855 | }
|
29856 | |
29857 |
|
29858 |
|
29859 |
|
29860 |
|
29861 | dispose() {
|
29862 | this.off('content-steering');
|
29863 | this.off('error');
|
29864 | this.abort();
|
29865 | this.clearTTLTimeout_();
|
29866 | this.currentPathway = null;
|
29867 | this.defaultPathway = null;
|
29868 | this.queryBeforeStart = null;
|
29869 | this.proxyServerUrl_ = null;
|
29870 | this.manifestType_ = null;
|
29871 | this.ttlTimeout_ = null;
|
29872 | this.request_ = null;
|
29873 | this.excludedSteeringManifestURLs = new Set();
|
29874 | this.availablePathways_ = new Set();
|
29875 | this.steeringManifest = new SteeringManifest();
|
29876 | }
|
29877 | |
29878 |
|
29879 |
|
29880 |
|
29881 |
|
29882 |
|
29883 |
|
29884 | addAvailablePathway(pathway) {
|
29885 | if (pathway) {
|
29886 | this.availablePathways_.add(pathway);
|
29887 | }
|
29888 | }
|
29889 | |
29890 |
|
29891 |
|
29892 |
|
29893 |
|
29894 | clearAvailablePathways() {
|
29895 | this.availablePathways_.clear();
|
29896 | }
|
29897 | |
29898 |
|
29899 |
|
29900 |
|
29901 |
|
29902 | excludePathway(pathway) {
|
29903 | return this.availablePathways_.delete(pathway);
|
29904 | }
|
29905 | |
29906 |
|
29907 |
|
29908 |
|
29909 |
|
29910 |
|
29911 |
|
29912 |
|
29913 |
|
29914 | didDASHTagChange(baseURL, newTag) {
|
29915 | return !newTag && this.steeringManifest.reloadUri || newTag && (resolveUrl(baseURL, newTag.serverURL) !== this.steeringManifest.reloadUri || newTag.defaultServiceLocation !== this.defaultPathway || newTag.queryBeforeStart !== this.queryBeforeStart || newTag.proxyServerURL !== this.proxyServerUrl_);
|
29916 | }
|
29917 |
|
29918 | getAvailablePathways() {
|
29919 | return this.availablePathways_;
|
29920 | }
|
29921 |
|
29922 | }
|
29923 |
|
29924 | |
29925 |
|
29926 |
|
29927 | const ABORT_EARLY_EXCLUSION_SECONDS = 10;
|
29928 | let Vhs$1;
|
29929 |
|
29930 |
|
29931 | const loaderStats = ['mediaRequests', 'mediaRequestsAborted', 'mediaRequestsTimedout', 'mediaRequestsErrored', 'mediaTransferDuration', 'mediaBytesTransferred', 'mediaAppends'];
|
29932 |
|
29933 | const sumLoaderStat = function (stat) {
|
29934 | return this.audioSegmentLoader_[stat] + this.mainSegmentLoader_[stat];
|
29935 | };
|
29936 |
|
29937 | const shouldSwitchToMedia = function ({
|
29938 | currentPlaylist,
|
29939 | buffered,
|
29940 | currentTime,
|
29941 | nextPlaylist,
|
29942 | bufferLowWaterLine,
|
29943 | bufferHighWaterLine,
|
29944 | duration,
|
29945 | bufferBasedABR,
|
29946 | log
|
29947 | }) {
|
29948 |
|
29949 | if (!nextPlaylist) {
|
29950 | videojs__default["default"].log.warn('We received no playlist to switch to. Please check your stream.');
|
29951 | return false;
|
29952 | }
|
29953 |
|
29954 | const sharedLogLine = `allowing switch ${currentPlaylist && currentPlaylist.id || 'null'} -> ${nextPlaylist.id}`;
|
29955 |
|
29956 | if (!currentPlaylist) {
|
29957 | log(`${sharedLogLine} as current playlist is not set`);
|
29958 | return true;
|
29959 | }
|
29960 |
|
29961 |
|
29962 | if (nextPlaylist.id === currentPlaylist.id) {
|
29963 | return false;
|
29964 | }
|
29965 |
|
29966 |
|
29967 | const isBuffered = Boolean(findRange(buffered, currentTime).length);
|
29968 |
|
29969 |
|
29970 |
|
29971 |
|
29972 | if (!currentPlaylist.endList) {
|
29973 |
|
29974 |
|
29975 | if (!isBuffered && typeof currentPlaylist.partTargetDuration === 'number') {
|
29976 | log(`not ${sharedLogLine} as current playlist is live llhls, but currentTime isn't in buffered.`);
|
29977 | return false;
|
29978 | }
|
29979 |
|
29980 | log(`${sharedLogLine} as current playlist is live`);
|
29981 | return true;
|
29982 | }
|
29983 |
|
29984 | const forwardBuffer = timeAheadOf(buffered, currentTime);
|
29985 | const maxBufferLowWaterLine = bufferBasedABR ? Config.EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE : Config.MAX_BUFFER_LOW_WATER_LINE;
|
29986 |
|
29987 |
|
29988 | if (duration < maxBufferLowWaterLine) {
|
29989 | log(`${sharedLogLine} as duration < max low water line (${duration} < ${maxBufferLowWaterLine})`);
|
29990 | return true;
|
29991 | }
|
29992 |
|
29993 | const nextBandwidth = nextPlaylist.attributes.BANDWIDTH;
|
29994 | const currBandwidth = currentPlaylist.attributes.BANDWIDTH;
|
29995 |
|
29996 |
|
29997 | if (nextBandwidth < currBandwidth && (!bufferBasedABR || forwardBuffer < bufferHighWaterLine)) {
|
29998 | let logLine = `${sharedLogLine} as next bandwidth < current bandwidth (${nextBandwidth} < ${currBandwidth})`;
|
29999 |
|
30000 | if (bufferBasedABR) {
|
30001 | logLine += ` and forwardBuffer < bufferHighWaterLine (${forwardBuffer} < ${bufferHighWaterLine})`;
|
30002 | }
|
30003 |
|
30004 | log(logLine);
|
30005 | return true;
|
30006 | }
|
30007 |
|
30008 |
|
30009 |
|
30010 | if ((!bufferBasedABR || nextBandwidth > currBandwidth) && forwardBuffer >= bufferLowWaterLine) {
|
30011 | let logLine = `${sharedLogLine} as forwardBuffer >= bufferLowWaterLine (${forwardBuffer} >= ${bufferLowWaterLine})`;
|
30012 |
|
30013 | if (bufferBasedABR) {
|
30014 | logLine += ` and next bandwidth > current bandwidth (${nextBandwidth} > ${currBandwidth})`;
|
30015 | }
|
30016 |
|
30017 | log(logLine);
|
30018 | return true;
|
30019 | }
|
30020 |
|
30021 | log(`not ${sharedLogLine} as no switching criteria met`);
|
30022 | return false;
|
30023 | };
|
30024 | |
30025 |
|
30026 |
|
30027 |
|
30028 |
|
30029 |
|
30030 |
|
30031 |
|
30032 |
|
30033 |
|
30034 |
|
30035 | class PlaylistController extends videojs__default["default"].EventTarget {
|
30036 | constructor(options) {
|
30037 | super();
|
30038 | const {
|
30039 | src,
|
30040 | withCredentials,
|
30041 | tech,
|
30042 | bandwidth,
|
30043 | externVhs,
|
30044 | useCueTags,
|
30045 | playlistExclusionDuration,
|
30046 | enableLowInitialPlaylist,
|
30047 | sourceType,
|
30048 | cacheEncryptionKeys,
|
30049 | bufferBasedABR,
|
30050 | leastPixelDiffSelector,
|
30051 | captionServices
|
30052 | } = options;
|
30053 |
|
30054 | if (!src) {
|
30055 | throw new Error('A non-empty playlist URL or JSON manifest string is required');
|
30056 | }
|
30057 |
|
30058 | let {
|
30059 | maxPlaylistRetries
|
30060 | } = options;
|
30061 |
|
30062 | if (maxPlaylistRetries === null || typeof maxPlaylistRetries === 'undefined') {
|
30063 | maxPlaylistRetries = Infinity;
|
30064 | }
|
30065 |
|
30066 | Vhs$1 = externVhs;
|
30067 | this.bufferBasedABR = Boolean(bufferBasedABR);
|
30068 | this.leastPixelDiffSelector = Boolean(leastPixelDiffSelector);
|
30069 | this.withCredentials = withCredentials;
|
30070 | this.tech_ = tech;
|
30071 | this.vhs_ = tech.vhs;
|
30072 | this.sourceType_ = sourceType;
|
30073 | this.useCueTags_ = useCueTags;
|
30074 | this.playlistExclusionDuration = playlistExclusionDuration;
|
30075 | this.maxPlaylistRetries = maxPlaylistRetries;
|
30076 | this.enableLowInitialPlaylist = enableLowInitialPlaylist;
|
30077 |
|
30078 | if (this.useCueTags_) {
|
30079 | this.cueTagsTrack_ = this.tech_.addTextTrack('metadata', 'ad-cues');
|
30080 | this.cueTagsTrack_.inBandMetadataTrackDispatchType = '';
|
30081 | }
|
30082 |
|
30083 | this.requestOptions_ = {
|
30084 | withCredentials,
|
30085 | maxPlaylistRetries,
|
30086 | timeout: null
|
30087 | };
|
30088 | this.on('error', this.pauseLoading);
|
30089 | this.mediaTypes_ = createMediaTypes();
|
30090 | this.mediaSource = new window.MediaSource();
|
30091 | this.handleDurationChange_ = this.handleDurationChange_.bind(this);
|
30092 | this.handleSourceOpen_ = this.handleSourceOpen_.bind(this);
|
30093 | this.handleSourceEnded_ = this.handleSourceEnded_.bind(this);
|
30094 | this.mediaSource.addEventListener('durationchange', this.handleDurationChange_);
|
30095 |
|
30096 | this.mediaSource.addEventListener('sourceopen', this.handleSourceOpen_);
|
30097 | this.mediaSource.addEventListener('sourceended', this.handleSourceEnded_);
|
30098 |
|
30099 |
|
30100 | this.seekable_ = createTimeRanges();
|
30101 | this.hasPlayed_ = false;
|
30102 | this.syncController_ = new SyncController(options);
|
30103 | this.segmentMetadataTrack_ = tech.addRemoteTextTrack({
|
30104 | kind: 'metadata',
|
30105 | label: 'segment-metadata'
|
30106 | }, false).track;
|
30107 | this.decrypter_ = new Decrypter();
|
30108 | this.sourceUpdater_ = new SourceUpdater(this.mediaSource);
|
30109 | this.inbandTextTracks_ = {};
|
30110 | this.timelineChangeController_ = new TimelineChangeController();
|
30111 | this.keyStatusMap_ = new Map();
|
30112 | const segmentLoaderSettings = {
|
30113 | vhs: this.vhs_,
|
30114 | parse708captions: options.parse708captions,
|
30115 | useDtsForTimestampOffset: options.useDtsForTimestampOffset,
|
30116 | captionServices,
|
30117 | mediaSource: this.mediaSource,
|
30118 | currentTime: this.tech_.currentTime.bind(this.tech_),
|
30119 | seekable: () => this.seekable(),
|
30120 | seeking: () => this.tech_.seeking(),
|
30121 | duration: () => this.duration(),
|
30122 | hasPlayed: () => this.hasPlayed_,
|
30123 | goalBufferLength: () => this.goalBufferLength(),
|
30124 | bandwidth,
|
30125 | syncController: this.syncController_,
|
30126 | decrypter: this.decrypter_,
|
30127 | sourceType: this.sourceType_,
|
30128 | inbandTextTracks: this.inbandTextTracks_,
|
30129 | cacheEncryptionKeys,
|
30130 | sourceUpdater: this.sourceUpdater_,
|
30131 | timelineChangeController: this.timelineChangeController_,
|
30132 | exactManifestTimings: options.exactManifestTimings,
|
30133 | addMetadataToTextTrack: this.addMetadataToTextTrack.bind(this)
|
30134 | };
|
30135 |
|
30136 |
|
30137 |
|
30138 |
|
30139 | this.mainPlaylistLoader_ = this.sourceType_ === 'dash' ? new DashPlaylistLoader(src, this.vhs_, merge$1(this.requestOptions_, {
|
30140 | addMetadataToTextTrack: this.addMetadataToTextTrack.bind(this)
|
30141 | })) : new PlaylistLoader(src, this.vhs_, merge$1(this.requestOptions_, {
|
30142 | addDateRangesToTextTrack: this.addDateRangesToTextTrack_.bind(this)
|
30143 | }));
|
30144 | this.setupMainPlaylistLoaderListeners_();
|
30145 |
|
30146 |
|
30147 | this.mainSegmentLoader_ = new SegmentLoader(merge$1(segmentLoaderSettings, {
|
30148 | segmentMetadataTrack: this.segmentMetadataTrack_,
|
30149 | loaderType: 'main'
|
30150 | }), options);
|
30151 |
|
30152 | this.audioSegmentLoader_ = new SegmentLoader(merge$1(segmentLoaderSettings, {
|
30153 | loaderType: 'audio'
|
30154 | }), options);
|
30155 | this.subtitleSegmentLoader_ = new VTTSegmentLoader(merge$1(segmentLoaderSettings, {
|
30156 | loaderType: 'vtt',
|
30157 | featuresNativeTextTracks: this.tech_.featuresNativeTextTracks,
|
30158 | loadVttJs: () => new Promise((resolve, reject) => {
|
30159 | function onLoad() {
|
30160 | tech.off('vttjserror', onError);
|
30161 | resolve();
|
30162 | }
|
30163 |
|
30164 | function onError() {
|
30165 | tech.off('vttjsloaded', onLoad);
|
30166 | reject();
|
30167 | }
|
30168 |
|
30169 | tech.one('vttjsloaded', onLoad);
|
30170 | tech.one('vttjserror', onError);
|
30171 |
|
30172 | tech.addWebVttScript_();
|
30173 | })
|
30174 | }), options);
|
30175 |
|
30176 | const getBandwidth = () => {
|
30177 | return this.mainSegmentLoader_.bandwidth;
|
30178 | };
|
30179 |
|
30180 | this.contentSteeringController_ = new ContentSteeringController(this.vhs_.xhr, getBandwidth);
|
30181 | this.setupSegmentLoaderListeners_();
|
30182 |
|
30183 | if (this.bufferBasedABR) {
|
30184 | this.mainPlaylistLoader_.one('loadedplaylist', () => this.startABRTimer_());
|
30185 | this.tech_.on('pause', () => this.stopABRTimer_());
|
30186 | this.tech_.on('play', () => this.startABRTimer_());
|
30187 | }
|
30188 |
|
30189 |
|
30190 |
|
30191 |
|
30192 |
|
30193 |
|
30194 |
|
30195 |
|
30196 |
|
30197 | loaderStats.forEach(stat => {
|
30198 | this[stat + '_'] = sumLoaderStat.bind(this, stat);
|
30199 | });
|
30200 | this.logger_ = logger('pc');
|
30201 | this.triggeredFmp4Usage = false;
|
30202 |
|
30203 | if (this.tech_.preload() === 'none') {
|
30204 | this.loadOnPlay_ = () => {
|
30205 | this.loadOnPlay_ = null;
|
30206 | this.mainPlaylistLoader_.load();
|
30207 | };
|
30208 |
|
30209 | this.tech_.one('play', this.loadOnPlay_);
|
30210 | } else {
|
30211 | this.mainPlaylistLoader_.load();
|
30212 | }
|
30213 |
|
30214 | this.timeToLoadedData__ = -1;
|
30215 | this.mainAppendsToLoadedData__ = -1;
|
30216 | this.audioAppendsToLoadedData__ = -1;
|
30217 | const event = this.tech_.preload() === 'none' ? 'play' : 'loadstart';
|
30218 |
|
30219 | this.tech_.one(event, () => {
|
30220 | const timeToLoadedDataStart = Date.now();
|
30221 | this.tech_.one('loadeddata', () => {
|
30222 | this.timeToLoadedData__ = Date.now() - timeToLoadedDataStart;
|
30223 | this.mainAppendsToLoadedData__ = this.mainSegmentLoader_.mediaAppends;
|
30224 | this.audioAppendsToLoadedData__ = this.audioSegmentLoader_.mediaAppends;
|
30225 | });
|
30226 | });
|
30227 | }
|
30228 |
|
30229 | mainAppendsToLoadedData_() {
|
30230 | return this.mainAppendsToLoadedData__;
|
30231 | }
|
30232 |
|
30233 | audioAppendsToLoadedData_() {
|
30234 | return this.audioAppendsToLoadedData__;
|
30235 | }
|
30236 |
|
30237 | appendsToLoadedData_() {
|
30238 | const main = this.mainAppendsToLoadedData_();
|
30239 | const audio = this.audioAppendsToLoadedData_();
|
30240 |
|
30241 | if (main === -1 || audio === -1) {
|
30242 | return -1;
|
30243 | }
|
30244 |
|
30245 | return main + audio;
|
30246 | }
|
30247 |
|
30248 | timeToLoadedData_() {
|
30249 | return this.timeToLoadedData__;
|
30250 | }
|
30251 | |
30252 |
|
30253 |
|
30254 |
|
30255 |
|
30256 |
|
30257 |
|
30258 |
|
30259 | checkABR_(reason = 'abr') {
|
30260 | const nextPlaylist = this.selectPlaylist();
|
30261 |
|
30262 | if (nextPlaylist && this.shouldSwitchToMedia_(nextPlaylist)) {
|
30263 | this.switchMedia_(nextPlaylist, reason);
|
30264 | }
|
30265 | }
|
30266 |
|
30267 | switchMedia_(playlist, cause, delay) {
|
30268 | const oldMedia = this.media();
|
30269 | const oldId = oldMedia && (oldMedia.id || oldMedia.uri);
|
30270 | const newId = playlist && (playlist.id || playlist.uri);
|
30271 |
|
30272 | if (oldId && oldId !== newId) {
|
30273 | this.logger_(`switch media ${oldId} -> ${newId} from ${cause}`);
|
30274 | this.tech_.trigger({
|
30275 | type: 'usage',
|
30276 | name: `vhs-rendition-change-${cause}`
|
30277 | });
|
30278 | }
|
30279 |
|
30280 | this.mainPlaylistLoader_.media(playlist, delay);
|
30281 | }
|
30282 | |
30283 |
|
30284 |
|
30285 |
|
30286 |
|
30287 |
|
30288 |
|
30289 |
|
30290 |
|
30291 |
|
30292 |
|
30293 | switchMediaForDASHContentSteering_() {
|
30294 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(type => {
|
30295 | const mediaType = this.mediaTypes_[type];
|
30296 | const activeGroup = mediaType ? mediaType.activeGroup() : null;
|
30297 | const pathway = this.contentSteeringController_.getPathway();
|
30298 |
|
30299 | if (activeGroup && pathway) {
|
30300 |
|
30301 | const mediaPlaylists = activeGroup.length ? activeGroup[0].playlists : activeGroup.playlists;
|
30302 | const dashMediaPlaylists = mediaPlaylists.filter(p => p.attributes.serviceLocation === pathway);
|
30303 |
|
30304 | if (dashMediaPlaylists.length) {
|
30305 | this.mediaTypes_[type].activePlaylistLoader.media(dashMediaPlaylists[0]);
|
30306 | }
|
30307 | }
|
30308 | });
|
30309 | }
|
30310 | |
30311 |
|
30312 |
|
30313 |
|
30314 |
|
30315 |
|
30316 |
|
30317 | startABRTimer_() {
|
30318 | this.stopABRTimer_();
|
30319 | this.abrTimer_ = window.setInterval(() => this.checkABR_(), 250);
|
30320 | }
|
30321 | |
30322 |
|
30323 |
|
30324 |
|
30325 |
|
30326 |
|
30327 |
|
30328 | stopABRTimer_() {
|
30329 |
|
30330 |
|
30331 | if (this.tech_.scrubbing && this.tech_.scrubbing()) {
|
30332 | return;
|
30333 | }
|
30334 |
|
30335 | window.clearInterval(this.abrTimer_);
|
30336 | this.abrTimer_ = null;
|
30337 | }
|
30338 | |
30339 |
|
30340 |
|
30341 |
|
30342 |
|
30343 |
|
30344 |
|
30345 | getAudioTrackPlaylists_() {
|
30346 | const main = this.main();
|
30347 | const defaultPlaylists = main && main.playlists || [];
|
30348 |
|
30349 |
|
30350 |
|
30351 | if (!main || !main.mediaGroups || !main.mediaGroups.AUDIO) {
|
30352 | return defaultPlaylists;
|
30353 | }
|
30354 |
|
30355 | const AUDIO = main.mediaGroups.AUDIO;
|
30356 | const groupKeys = Object.keys(AUDIO);
|
30357 | let track;
|
30358 |
|
30359 | if (Object.keys(this.mediaTypes_.AUDIO.groups).length) {
|
30360 | track = this.mediaTypes_.AUDIO.activeTrack();
|
30361 | } else {
|
30362 |
|
30363 | const defaultGroup = AUDIO.main || groupKeys.length && AUDIO[groupKeys[0]];
|
30364 |
|
30365 | for (const label in defaultGroup) {
|
30366 | if (defaultGroup[label].default) {
|
30367 | track = {
|
30368 | label
|
30369 | };
|
30370 | break;
|
30371 | }
|
30372 | }
|
30373 | }
|
30374 |
|
30375 |
|
30376 | if (!track) {
|
30377 | return defaultPlaylists;
|
30378 | }
|
30379 |
|
30380 | const playlists = [];
|
30381 |
|
30382 |
|
30383 | for (const group in AUDIO) {
|
30384 | if (AUDIO[group][track.label]) {
|
30385 | const properties = AUDIO[group][track.label];
|
30386 |
|
30387 | if (properties.playlists && properties.playlists.length) {
|
30388 | playlists.push.apply(playlists, properties.playlists);
|
30389 | } else if (properties.uri) {
|
30390 | playlists.push(properties);
|
30391 | } else if (main.playlists.length) {
|
30392 |
|
30393 |
|
30394 |
|
30395 | for (let i = 0; i < main.playlists.length; i++) {
|
30396 | const playlist = main.playlists[i];
|
30397 |
|
30398 | if (playlist.attributes && playlist.attributes.AUDIO && playlist.attributes.AUDIO === group) {
|
30399 | playlists.push(playlist);
|
30400 | }
|
30401 | }
|
30402 | }
|
30403 | }
|
30404 | }
|
30405 |
|
30406 | if (!playlists.length) {
|
30407 | return defaultPlaylists;
|
30408 | }
|
30409 |
|
30410 | return playlists;
|
30411 | }
|
30412 | |
30413 |
|
30414 |
|
30415 |
|
30416 |
|
30417 |
|
30418 |
|
30419 |
|
30420 | setupMainPlaylistLoaderListeners_() {
|
30421 | this.mainPlaylistLoader_.on('loadedmetadata', () => {
|
30422 | const media = this.mainPlaylistLoader_.media();
|
30423 | const requestTimeout = media.targetDuration * 1.5 * 1000;
|
30424 |
|
30425 |
|
30426 | if (isLowestEnabledRendition(this.mainPlaylistLoader_.main, this.mainPlaylistLoader_.media())) {
|
30427 | this.requestOptions_.timeout = 0;
|
30428 | } else {
|
30429 | this.requestOptions_.timeout = requestTimeout;
|
30430 | }
|
30431 |
|
30432 |
|
30433 |
|
30434 | if (media.endList && this.tech_.preload() !== 'none') {
|
30435 | this.mainSegmentLoader_.playlist(media, this.requestOptions_);
|
30436 | this.mainSegmentLoader_.load();
|
30437 | }
|
30438 |
|
30439 | setupMediaGroups({
|
30440 | sourceType: this.sourceType_,
|
30441 | segmentLoaders: {
|
30442 | AUDIO: this.audioSegmentLoader_,
|
30443 | SUBTITLES: this.subtitleSegmentLoader_,
|
30444 | main: this.mainSegmentLoader_
|
30445 | },
|
30446 | tech: this.tech_,
|
30447 | requestOptions: this.requestOptions_,
|
30448 | mainPlaylistLoader: this.mainPlaylistLoader_,
|
30449 | vhs: this.vhs_,
|
30450 | main: this.main(),
|
30451 | mediaTypes: this.mediaTypes_,
|
30452 | excludePlaylist: this.excludePlaylist.bind(this)
|
30453 | });
|
30454 | this.triggerPresenceUsage_(this.main(), media);
|
30455 | this.setupFirstPlay();
|
30456 |
|
30457 | if (!this.mediaTypes_.AUDIO.activePlaylistLoader || this.mediaTypes_.AUDIO.activePlaylistLoader.media()) {
|
30458 | this.trigger('selectedinitialmedia');
|
30459 | } else {
|
30460 |
|
30461 |
|
30462 |
|
30463 | this.mediaTypes_.AUDIO.activePlaylistLoader.one('loadedmetadata', () => {
|
30464 | this.trigger('selectedinitialmedia');
|
30465 | });
|
30466 | }
|
30467 | });
|
30468 | this.mainPlaylistLoader_.on('loadedplaylist', () => {
|
30469 | if (this.loadOnPlay_) {
|
30470 | this.tech_.off('play', this.loadOnPlay_);
|
30471 | }
|
30472 |
|
30473 | let updatedPlaylist = this.mainPlaylistLoader_.media();
|
30474 |
|
30475 | if (!updatedPlaylist) {
|
30476 |
|
30477 | this.attachContentSteeringListeners_();
|
30478 | this.initContentSteeringController_();
|
30479 |
|
30480 |
|
30481 | this.excludeUnsupportedVariants_();
|
30482 | let selectedMedia;
|
30483 |
|
30484 | if (this.enableLowInitialPlaylist) {
|
30485 | selectedMedia = this.selectInitialPlaylist();
|
30486 | }
|
30487 |
|
30488 | if (!selectedMedia) {
|
30489 | selectedMedia = this.selectPlaylist();
|
30490 | }
|
30491 |
|
30492 | if (!selectedMedia || !this.shouldSwitchToMedia_(selectedMedia)) {
|
30493 | return;
|
30494 | }
|
30495 |
|
30496 | this.initialMedia_ = selectedMedia;
|
30497 | this.switchMedia_(this.initialMedia_, 'initial');
|
30498 |
|
30499 |
|
30500 |
|
30501 |
|
30502 |
|
30503 |
|
30504 | const haveJsonSource = this.sourceType_ === 'vhs-json' && this.initialMedia_.segments;
|
30505 |
|
30506 | if (!haveJsonSource) {
|
30507 | return;
|
30508 | }
|
30509 |
|
30510 | updatedPlaylist = this.initialMedia_;
|
30511 | }
|
30512 |
|
30513 | this.handleUpdatedMediaPlaylist(updatedPlaylist);
|
30514 | });
|
30515 | this.mainPlaylistLoader_.on('error', () => {
|
30516 | const error = this.mainPlaylistLoader_.error;
|
30517 | this.excludePlaylist({
|
30518 | playlistToExclude: error.playlist,
|
30519 | error
|
30520 | });
|
30521 | });
|
30522 | this.mainPlaylistLoader_.on('mediachanging', () => {
|
30523 | this.mainSegmentLoader_.abort();
|
30524 | this.mainSegmentLoader_.pause();
|
30525 | });
|
30526 | this.mainPlaylistLoader_.on('mediachange', () => {
|
30527 | const media = this.mainPlaylistLoader_.media();
|
30528 | const requestTimeout = media.targetDuration * 1.5 * 1000;
|
30529 |
|
30530 |
|
30531 | if (isLowestEnabledRendition(this.mainPlaylistLoader_.main, this.mainPlaylistLoader_.media())) {
|
30532 | this.requestOptions_.timeout = 0;
|
30533 | } else {
|
30534 | this.requestOptions_.timeout = requestTimeout;
|
30535 | }
|
30536 |
|
30537 | if (this.sourceType_ === 'dash') {
|
30538 |
|
30539 | this.mainPlaylistLoader_.load();
|
30540 | }
|
30541 |
|
30542 |
|
30543 |
|
30544 |
|
30545 |
|
30546 | this.mainSegmentLoader_.pause();
|
30547 | this.mainSegmentLoader_.playlist(media, this.requestOptions_);
|
30548 |
|
30549 | if (this.waitingForFastQualityPlaylistReceived_) {
|
30550 | this.runFastQualitySwitch_();
|
30551 | } else {
|
30552 | this.mainSegmentLoader_.load();
|
30553 | }
|
30554 |
|
30555 | this.tech_.trigger({
|
30556 | type: 'mediachange',
|
30557 | bubbles: true
|
30558 | });
|
30559 | });
|
30560 | this.mainPlaylistLoader_.on('playlistunchanged', () => {
|
30561 | const updatedPlaylist = this.mainPlaylistLoader_.media();
|
30562 |
|
30563 |
|
30564 |
|
30565 | if (updatedPlaylist.lastExcludeReason_ === 'playlist-unchanged') {
|
30566 | return;
|
30567 | }
|
30568 |
|
30569 | const playlistOutdated = this.stuckAtPlaylistEnd_(updatedPlaylist);
|
30570 |
|
30571 | if (playlistOutdated) {
|
30572 |
|
30573 |
|
30574 |
|
30575 |
|
30576 | this.excludePlaylist({
|
30577 | error: {
|
30578 | message: 'Playlist no longer updating.',
|
30579 | reason: 'playlist-unchanged'
|
30580 | }
|
30581 | });
|
30582 |
|
30583 | this.tech_.trigger('playliststuck');
|
30584 | }
|
30585 | });
|
30586 | this.mainPlaylistLoader_.on('renditiondisabled', () => {
|
30587 | this.tech_.trigger({
|
30588 | type: 'usage',
|
30589 | name: 'vhs-rendition-disabled'
|
30590 | });
|
30591 | });
|
30592 | this.mainPlaylistLoader_.on('renditionenabled', () => {
|
30593 | this.tech_.trigger({
|
30594 | type: 'usage',
|
30595 | name: 'vhs-rendition-enabled'
|
30596 | });
|
30597 | });
|
30598 | }
|
30599 | |
30600 |
|
30601 |
|
30602 |
|
30603 |
|
30604 |
|
30605 |
|
30606 |
|
30607 |
|
30608 |
|
30609 |
|
30610 | handleUpdatedMediaPlaylist(updatedPlaylist) {
|
30611 | if (this.useCueTags_) {
|
30612 | this.updateAdCues_(updatedPlaylist);
|
30613 | }
|
30614 |
|
30615 |
|
30616 |
|
30617 |
|
30618 |
|
30619 | this.mainSegmentLoader_.pause();
|
30620 | this.mainSegmentLoader_.playlist(updatedPlaylist, this.requestOptions_);
|
30621 |
|
30622 | if (this.waitingForFastQualityPlaylistReceived_) {
|
30623 | this.runFastQualitySwitch_();
|
30624 | }
|
30625 |
|
30626 | this.updateDuration(!updatedPlaylist.endList);
|
30627 |
|
30628 |
|
30629 |
|
30630 | if (!this.tech_.paused()) {
|
30631 | this.mainSegmentLoader_.load();
|
30632 |
|
30633 | if (this.audioSegmentLoader_) {
|
30634 | this.audioSegmentLoader_.load();
|
30635 | }
|
30636 | }
|
30637 | }
|
30638 | |
30639 |
|
30640 |
|
30641 |
|
30642 |
|
30643 |
|
30644 |
|
30645 | triggerPresenceUsage_(main, media) {
|
30646 | const mediaGroups = main.mediaGroups || {};
|
30647 | let defaultDemuxed = true;
|
30648 | const audioGroupKeys = Object.keys(mediaGroups.AUDIO);
|
30649 |
|
30650 | for (const mediaGroup in mediaGroups.AUDIO) {
|
30651 | for (const label in mediaGroups.AUDIO[mediaGroup]) {
|
30652 | const properties = mediaGroups.AUDIO[mediaGroup][label];
|
30653 |
|
30654 | if (!properties.uri) {
|
30655 | defaultDemuxed = false;
|
30656 | }
|
30657 | }
|
30658 | }
|
30659 |
|
30660 | if (defaultDemuxed) {
|
30661 | this.tech_.trigger({
|
30662 | type: 'usage',
|
30663 | name: 'vhs-demuxed'
|
30664 | });
|
30665 | }
|
30666 |
|
30667 | if (Object.keys(mediaGroups.SUBTITLES).length) {
|
30668 | this.tech_.trigger({
|
30669 | type: 'usage',
|
30670 | name: 'vhs-webvtt'
|
30671 | });
|
30672 | }
|
30673 |
|
30674 | if (Vhs$1.Playlist.isAes(media)) {
|
30675 | this.tech_.trigger({
|
30676 | type: 'usage',
|
30677 | name: 'vhs-aes'
|
30678 | });
|
30679 | }
|
30680 |
|
30681 | if (audioGroupKeys.length && Object.keys(mediaGroups.AUDIO[audioGroupKeys[0]]).length > 1) {
|
30682 | this.tech_.trigger({
|
30683 | type: 'usage',
|
30684 | name: 'vhs-alternate-audio'
|
30685 | });
|
30686 | }
|
30687 |
|
30688 | if (this.useCueTags_) {
|
30689 | this.tech_.trigger({
|
30690 | type: 'usage',
|
30691 | name: 'vhs-playlist-cue-tags'
|
30692 | });
|
30693 | }
|
30694 | }
|
30695 |
|
30696 | shouldSwitchToMedia_(nextPlaylist) {
|
30697 | const currentPlaylist = this.mainPlaylistLoader_.media() || this.mainPlaylistLoader_.pendingMedia_;
|
30698 | const currentTime = this.tech_.currentTime();
|
30699 | const bufferLowWaterLine = this.bufferLowWaterLine();
|
30700 | const bufferHighWaterLine = this.bufferHighWaterLine();
|
30701 | const buffered = this.tech_.buffered();
|
30702 | return shouldSwitchToMedia({
|
30703 | buffered,
|
30704 | currentTime,
|
30705 | currentPlaylist,
|
30706 | nextPlaylist,
|
30707 | bufferLowWaterLine,
|
30708 | bufferHighWaterLine,
|
30709 | duration: this.duration(),
|
30710 | bufferBasedABR: this.bufferBasedABR,
|
30711 | log: this.logger_
|
30712 | });
|
30713 | }
|
30714 | |
30715 |
|
30716 |
|
30717 |
|
30718 |
|
30719 |
|
30720 |
|
30721 |
|
30722 | setupSegmentLoaderListeners_() {
|
30723 | this.mainSegmentLoader_.on('bandwidthupdate', () => {
|
30724 |
|
30725 |
|
30726 | this.checkABR_('bandwidthupdate');
|
30727 | this.tech_.trigger('bandwidthupdate');
|
30728 | });
|
30729 | this.mainSegmentLoader_.on('timeout', () => {
|
30730 | if (this.bufferBasedABR) {
|
30731 |
|
30732 |
|
30733 |
|
30734 |
|
30735 | this.mainSegmentLoader_.load();
|
30736 | }
|
30737 | });
|
30738 |
|
30739 |
|
30740 | if (!this.bufferBasedABR) {
|
30741 | this.mainSegmentLoader_.on('progress', () => {
|
30742 | this.trigger('progress');
|
30743 | });
|
30744 | }
|
30745 |
|
30746 | this.mainSegmentLoader_.on('error', () => {
|
30747 | const error = this.mainSegmentLoader_.error();
|
30748 | this.excludePlaylist({
|
30749 | playlistToExclude: error.playlist,
|
30750 | error
|
30751 | });
|
30752 | });
|
30753 | this.mainSegmentLoader_.on('appenderror', () => {
|
30754 | this.error = this.mainSegmentLoader_.error_;
|
30755 | this.trigger('error');
|
30756 | });
|
30757 | this.mainSegmentLoader_.on('syncinfoupdate', () => {
|
30758 | this.onSyncInfoUpdate_();
|
30759 | });
|
30760 | this.mainSegmentLoader_.on('timestampoffset', () => {
|
30761 | this.tech_.trigger({
|
30762 | type: 'usage',
|
30763 | name: 'vhs-timestamp-offset'
|
30764 | });
|
30765 | });
|
30766 | this.audioSegmentLoader_.on('syncinfoupdate', () => {
|
30767 | this.onSyncInfoUpdate_();
|
30768 | });
|
30769 | this.audioSegmentLoader_.on('appenderror', () => {
|
30770 | this.error = this.audioSegmentLoader_.error_;
|
30771 | this.trigger('error');
|
30772 | });
|
30773 | this.mainSegmentLoader_.on('ended', () => {
|
30774 | this.logger_('main segment loader ended');
|
30775 | this.onEndOfStream();
|
30776 | });
|
30777 | this.mainSegmentLoader_.on('earlyabort', event => {
|
30778 |
|
30779 | if (this.bufferBasedABR) {
|
30780 | return;
|
30781 | }
|
30782 |
|
30783 | this.delegateLoaders_('all', ['abort']);
|
30784 | this.excludePlaylist({
|
30785 | error: {
|
30786 | message: 'Aborted early because there isn\'t enough bandwidth to complete ' + 'the request without rebuffering.'
|
30787 | },
|
30788 | playlistExclusionDuration: ABORT_EARLY_EXCLUSION_SECONDS
|
30789 | });
|
30790 | });
|
30791 |
|
30792 | const updateCodecs = () => {
|
30793 | if (!this.sourceUpdater_.hasCreatedSourceBuffers()) {
|
30794 | return this.tryToCreateSourceBuffers_();
|
30795 | }
|
30796 |
|
30797 | const codecs = this.getCodecsOrExclude_();
|
30798 |
|
30799 | if (!codecs) {
|
30800 | return;
|
30801 | }
|
30802 |
|
30803 | this.sourceUpdater_.addOrChangeSourceBuffers(codecs);
|
30804 | };
|
30805 |
|
30806 | this.mainSegmentLoader_.on('trackinfo', updateCodecs);
|
30807 | this.audioSegmentLoader_.on('trackinfo', updateCodecs);
|
30808 | this.mainSegmentLoader_.on('fmp4', () => {
|
30809 | if (!this.triggeredFmp4Usage) {
|
30810 | this.tech_.trigger({
|
30811 | type: 'usage',
|
30812 | name: 'vhs-fmp4'
|
30813 | });
|
30814 | this.triggeredFmp4Usage = true;
|
30815 | }
|
30816 | });
|
30817 | this.audioSegmentLoader_.on('fmp4', () => {
|
30818 | if (!this.triggeredFmp4Usage) {
|
30819 | this.tech_.trigger({
|
30820 | type: 'usage',
|
30821 | name: 'vhs-fmp4'
|
30822 | });
|
30823 | this.triggeredFmp4Usage = true;
|
30824 | }
|
30825 | });
|
30826 | this.audioSegmentLoader_.on('ended', () => {
|
30827 | this.logger_('audioSegmentLoader ended');
|
30828 | this.onEndOfStream();
|
30829 | });
|
30830 | }
|
30831 |
|
30832 | mediaSecondsLoaded_() {
|
30833 | return Math.max(this.audioSegmentLoader_.mediaSecondsLoaded + this.mainSegmentLoader_.mediaSecondsLoaded);
|
30834 | }
|
30835 | |
30836 |
|
30837 |
|
30838 |
|
30839 |
|
30840 | load() {
|
30841 | this.mainSegmentLoader_.load();
|
30842 |
|
30843 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
30844 | this.audioSegmentLoader_.load();
|
30845 | }
|
30846 |
|
30847 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
30848 | this.subtitleSegmentLoader_.load();
|
30849 | }
|
30850 | }
|
30851 | |
30852 |
|
30853 |
|
30854 |
|
30855 |
|
30856 |
|
30857 |
|
30858 |
|
30859 |
|
30860 |
|
30861 | fastQualityChange_(media = this.selectPlaylist()) {
|
30862 | if (media && media === this.mainPlaylistLoader_.media()) {
|
30863 | this.logger_('skipping fastQualityChange because new media is same as old');
|
30864 | return;
|
30865 | }
|
30866 |
|
30867 | this.switchMedia_(media, 'fast-quality');
|
30868 |
|
30869 |
|
30870 | this.waitingForFastQualityPlaylistReceived_ = true;
|
30871 | }
|
30872 |
|
30873 | runFastQualitySwitch_() {
|
30874 | this.waitingForFastQualityPlaylistReceived_ = false;
|
30875 |
|
30876 |
|
30877 |
|
30878 |
|
30879 |
|
30880 |
|
30881 | this.mainSegmentLoader_.pause();
|
30882 | this.mainSegmentLoader_.resetEverything(() => {
|
30883 | this.tech_.setCurrentTime(this.tech_.currentTime());
|
30884 | });
|
30885 | }
|
30886 | |
30887 |
|
30888 |
|
30889 |
|
30890 |
|
30891 | play() {
|
30892 | if (this.setupFirstPlay()) {
|
30893 | return;
|
30894 | }
|
30895 |
|
30896 | if (this.tech_.ended()) {
|
30897 | this.tech_.setCurrentTime(0);
|
30898 | }
|
30899 |
|
30900 | if (this.hasPlayed_) {
|
30901 | this.load();
|
30902 | }
|
30903 |
|
30904 | const seekable = this.tech_.seekable();
|
30905 |
|
30906 |
|
30907 | if (this.tech_.duration() === Infinity) {
|
30908 | if (this.tech_.currentTime() < seekable.start(0)) {
|
30909 | return this.tech_.setCurrentTime(seekable.end(seekable.length - 1));
|
30910 | }
|
30911 | }
|
30912 | }
|
30913 | |
30914 |
|
30915 |
|
30916 |
|
30917 |
|
30918 |
|
30919 | setupFirstPlay() {
|
30920 | const media = this.mainPlaylistLoader_.media();
|
30921 |
|
30922 |
|
30923 |
|
30924 |
|
30925 |
|
30926 | if (!media || this.tech_.paused() || this.hasPlayed_) {
|
30927 | return false;
|
30928 | }
|
30929 |
|
30930 |
|
30931 | if (!media.endList || media.start) {
|
30932 | const seekable = this.seekable();
|
30933 |
|
30934 | if (!seekable.length) {
|
30935 |
|
30936 |
|
30937 | return false;
|
30938 | }
|
30939 |
|
30940 | const seekableEnd = seekable.end(0);
|
30941 | let startPoint = seekableEnd;
|
30942 |
|
30943 | if (media.start) {
|
30944 | const offset = media.start.timeOffset;
|
30945 |
|
30946 | if (offset < 0) {
|
30947 | startPoint = Math.max(seekableEnd + offset, seekable.start(0));
|
30948 | } else {
|
30949 | startPoint = Math.min(seekableEnd, offset);
|
30950 | }
|
30951 | }
|
30952 |
|
30953 |
|
30954 | this.trigger('firstplay');
|
30955 |
|
30956 | this.tech_.setCurrentTime(startPoint);
|
30957 | }
|
30958 |
|
30959 | this.hasPlayed_ = true;
|
30960 |
|
30961 | this.load();
|
30962 | return true;
|
30963 | }
|
30964 | |
30965 |
|
30966 |
|
30967 |
|
30968 |
|
30969 |
|
30970 |
|
30971 | handleSourceOpen_() {
|
30972 |
|
30973 |
|
30974 |
|
30975 | this.tryToCreateSourceBuffers_();
|
30976 |
|
30977 |
|
30978 |
|
30979 | if (this.tech_.autoplay()) {
|
30980 | const playPromise = this.tech_.play();
|
30981 |
|
30982 |
|
30983 | if (typeof playPromise !== 'undefined' && typeof playPromise.then === 'function') {
|
30984 | playPromise.then(null, e => {});
|
30985 | }
|
30986 | }
|
30987 |
|
30988 | this.trigger('sourceopen');
|
30989 | }
|
30990 | |
30991 |
|
30992 |
|
30993 |
|
30994 |
|
30995 |
|
30996 |
|
30997 | handleSourceEnded_() {
|
30998 | if (!this.inbandTextTracks_.metadataTrack_) {
|
30999 | return;
|
31000 | }
|
31001 |
|
31002 | const cues = this.inbandTextTracks_.metadataTrack_.cues;
|
31003 |
|
31004 | if (!cues || !cues.length) {
|
31005 | return;
|
31006 | }
|
31007 |
|
31008 | const duration = this.duration();
|
31009 | cues[cues.length - 1].endTime = isNaN(duration) || Math.abs(duration) === Infinity ? Number.MAX_VALUE : duration;
|
31010 | }
|
31011 | |
31012 |
|
31013 |
|
31014 |
|
31015 |
|
31016 |
|
31017 |
|
31018 | handleDurationChange_() {
|
31019 | this.tech_.trigger('durationchange');
|
31020 | }
|
31021 | |
31022 |
|
31023 |
|
31024 |
|
31025 |
|
31026 |
|
31027 |
|
31028 |
|
31029 |
|
31030 |
|
31031 | onEndOfStream() {
|
31032 | let isEndOfStream = this.mainSegmentLoader_.ended_;
|
31033 |
|
31034 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
31035 | const mainMediaInfo = this.mainSegmentLoader_.getCurrentMediaInfo_();
|
31036 |
|
31037 | if (!mainMediaInfo || mainMediaInfo.hasVideo) {
|
31038 |
|
31039 |
|
31040 |
|
31041 | isEndOfStream = isEndOfStream && this.audioSegmentLoader_.ended_;
|
31042 | } else {
|
31043 |
|
31044 | isEndOfStream = this.audioSegmentLoader_.ended_;
|
31045 | }
|
31046 | }
|
31047 |
|
31048 | if (!isEndOfStream) {
|
31049 | return;
|
31050 | }
|
31051 |
|
31052 | this.stopABRTimer_();
|
31053 | this.sourceUpdater_.endOfStream();
|
31054 | }
|
31055 | |
31056 |
|
31057 |
|
31058 |
|
31059 |
|
31060 |
|
31061 |
|
31062 |
|
31063 | stuckAtPlaylistEnd_(playlist) {
|
31064 | const seekable = this.seekable();
|
31065 |
|
31066 | if (!seekable.length) {
|
31067 |
|
31068 | return false;
|
31069 | }
|
31070 |
|
31071 | const expired = this.syncController_.getExpiredTime(playlist, this.duration());
|
31072 |
|
31073 | if (expired === null) {
|
31074 | return false;
|
31075 | }
|
31076 |
|
31077 |
|
31078 |
|
31079 | const absolutePlaylistEnd = Vhs$1.Playlist.playlistEnd(playlist, expired);
|
31080 | const currentTime = this.tech_.currentTime();
|
31081 | const buffered = this.tech_.buffered();
|
31082 |
|
31083 | if (!buffered.length) {
|
31084 |
|
31085 | return absolutePlaylistEnd - currentTime <= SAFE_TIME_DELTA;
|
31086 | }
|
31087 |
|
31088 | const bufferedEnd = buffered.end(buffered.length - 1);
|
31089 |
|
31090 |
|
31091 | return bufferedEnd - currentTime <= SAFE_TIME_DELTA && absolutePlaylistEnd - bufferedEnd <= SAFE_TIME_DELTA;
|
31092 | }
|
31093 | |
31094 |
|
31095 |
|
31096 |
|
31097 |
|
31098 |
|
31099 |
|
31100 |
|
31101 |
|
31102 |
|
31103 |
|
31104 |
|
31105 |
|
31106 | excludePlaylist({
|
31107 | playlistToExclude = this.mainPlaylistLoader_.media(),
|
31108 | error = {},
|
31109 | playlistExclusionDuration
|
31110 | }) {
|
31111 |
|
31112 |
|
31113 |
|
31114 |
|
31115 | playlistToExclude = playlistToExclude || this.mainPlaylistLoader_.media();
|
31116 | playlistExclusionDuration = playlistExclusionDuration || error.playlistExclusionDuration || this.playlistExclusionDuration;
|
31117 |
|
31118 |
|
31119 | if (!playlistToExclude) {
|
31120 | this.error = error;
|
31121 |
|
31122 | if (this.mediaSource.readyState !== 'open') {
|
31123 | this.trigger('error');
|
31124 | } else {
|
31125 | this.sourceUpdater_.endOfStream('network');
|
31126 | }
|
31127 |
|
31128 | return;
|
31129 | }
|
31130 |
|
31131 | playlistToExclude.playlistErrors_++;
|
31132 | const playlists = this.mainPlaylistLoader_.main.playlists;
|
31133 | const enabledPlaylists = playlists.filter(isEnabled);
|
31134 | const isFinalRendition = enabledPlaylists.length === 1 && enabledPlaylists[0] === playlistToExclude;
|
31135 |
|
31136 |
|
31137 | if (playlists.length === 1 && playlistExclusionDuration !== Infinity) {
|
31138 | videojs__default["default"].log.warn(`Problem encountered with playlist ${playlistToExclude.id}. ` + 'Trying again since it is the only playlist.');
|
31139 | this.tech_.trigger('retryplaylist');
|
31140 |
|
31141 | return this.mainPlaylistLoader_.load(isFinalRendition);
|
31142 | }
|
31143 |
|
31144 | if (isFinalRendition) {
|
31145 |
|
31146 | if (this.main().contentSteering) {
|
31147 | const pathway = this.pathwayAttribute_(playlistToExclude);
|
31148 |
|
31149 | const reIncludeDelay = this.contentSteeringController_.steeringManifest.ttl * 1000;
|
31150 | this.contentSteeringController_.excludePathway(pathway);
|
31151 | this.excludeThenChangePathway_();
|
31152 | setTimeout(() => {
|
31153 | this.contentSteeringController_.addAvailablePathway(pathway);
|
31154 | }, reIncludeDelay);
|
31155 | return;
|
31156 | }
|
31157 |
|
31158 |
|
31159 |
|
31160 |
|
31161 |
|
31162 | let reincluded = false;
|
31163 | playlists.forEach(playlist => {
|
31164 |
|
31165 | if (playlist === playlistToExclude) {
|
31166 | return;
|
31167 | }
|
31168 |
|
31169 | const excludeUntil = playlist.excludeUntil;
|
31170 |
|
31171 | if (typeof excludeUntil !== 'undefined' && excludeUntil !== Infinity) {
|
31172 | reincluded = true;
|
31173 | delete playlist.excludeUntil;
|
31174 | }
|
31175 | });
|
31176 |
|
31177 | if (reincluded) {
|
31178 | videojs__default["default"].log.warn('Removing other playlists from the exclusion list because the last ' + 'rendition is about to be excluded.');
|
31179 |
|
31180 |
|
31181 |
|
31182 | this.tech_.trigger('retryplaylist');
|
31183 | }
|
31184 | }
|
31185 |
|
31186 |
|
31187 | let excludeUntil;
|
31188 |
|
31189 | if (playlistToExclude.playlistErrors_ > this.maxPlaylistRetries) {
|
31190 | excludeUntil = Infinity;
|
31191 | } else {
|
31192 | excludeUntil = Date.now() + playlistExclusionDuration * 1000;
|
31193 | }
|
31194 |
|
31195 | playlistToExclude.excludeUntil = excludeUntil;
|
31196 |
|
31197 | if (error.reason) {
|
31198 | playlistToExclude.lastExcludeReason_ = error.reason;
|
31199 | }
|
31200 |
|
31201 | this.tech_.trigger('excludeplaylist');
|
31202 | this.tech_.trigger({
|
31203 | type: 'usage',
|
31204 | name: 'vhs-rendition-excluded'
|
31205 | });
|
31206 |
|
31207 |
|
31208 |
|
31209 |
|
31210 | const nextPlaylist = this.selectPlaylist();
|
31211 |
|
31212 | if (!nextPlaylist) {
|
31213 | this.error = 'Playback cannot continue. No available working or supported playlists.';
|
31214 | this.trigger('error');
|
31215 | return;
|
31216 | }
|
31217 |
|
31218 | const logFn = error.internal ? this.logger_ : videojs__default["default"].log.warn;
|
31219 | const errorMessage = error.message ? ' ' + error.message : '';
|
31220 | logFn(`${error.internal ? 'Internal problem' : 'Problem'} encountered with playlist ${playlistToExclude.id}.` + `${errorMessage} Switching to playlist ${nextPlaylist.id}.`);
|
31221 |
|
31222 | if (nextPlaylist.attributes.AUDIO !== playlistToExclude.attributes.AUDIO) {
|
31223 | this.delegateLoaders_('audio', ['abort', 'pause']);
|
31224 | }
|
31225 |
|
31226 |
|
31227 | if (nextPlaylist.attributes.SUBTITLES !== playlistToExclude.attributes.SUBTITLES) {
|
31228 | this.delegateLoaders_('subtitle', ['abort', 'pause']);
|
31229 | }
|
31230 |
|
31231 | this.delegateLoaders_('main', ['abort', 'pause']);
|
31232 | const delayDuration = nextPlaylist.targetDuration / 2 * 1000 || 5 * 1000;
|
31233 | const shouldDelay = typeof nextPlaylist.lastRequest === 'number' && Date.now() - nextPlaylist.lastRequest <= delayDuration;
|
31234 |
|
31235 | return this.switchMedia_(nextPlaylist, 'exclude', isFinalRendition || shouldDelay);
|
31236 | }
|
31237 | |
31238 |
|
31239 |
|
31240 |
|
31241 |
|
31242 | pauseLoading() {
|
31243 | this.delegateLoaders_('all', ['abort', 'pause']);
|
31244 | this.stopABRTimer_();
|
31245 | }
|
31246 | |
31247 |
|
31248 |
|
31249 |
|
31250 |
|
31251 |
|
31252 |
|
31253 |
|
31254 |
|
31255 |
|
31256 |
|
31257 |
|
31258 |
|
31259 |
|
31260 |
|
31261 |
|
31262 | delegateLoaders_(filter, fnNames) {
|
31263 | const loaders = [];
|
31264 | const dontFilterPlaylist = filter === 'all';
|
31265 |
|
31266 | if (dontFilterPlaylist || filter === 'main') {
|
31267 | loaders.push(this.mainPlaylistLoader_);
|
31268 | }
|
31269 |
|
31270 | const mediaTypes = [];
|
31271 |
|
31272 | if (dontFilterPlaylist || filter === 'audio') {
|
31273 | mediaTypes.push('AUDIO');
|
31274 | }
|
31275 |
|
31276 | if (dontFilterPlaylist || filter === 'subtitle') {
|
31277 | mediaTypes.push('CLOSED-CAPTIONS');
|
31278 | mediaTypes.push('SUBTITLES');
|
31279 | }
|
31280 |
|
31281 | mediaTypes.forEach(mediaType => {
|
31282 | const loader = this.mediaTypes_[mediaType] && this.mediaTypes_[mediaType].activePlaylistLoader;
|
31283 |
|
31284 | if (loader) {
|
31285 | loaders.push(loader);
|
31286 | }
|
31287 | });
|
31288 | ['main', 'audio', 'subtitle'].forEach(name => {
|
31289 | const loader = this[`${name}SegmentLoader_`];
|
31290 |
|
31291 | if (loader && (filter === name || filter === 'all')) {
|
31292 | loaders.push(loader);
|
31293 | }
|
31294 | });
|
31295 | loaders.forEach(loader => fnNames.forEach(fnName => {
|
31296 | if (typeof loader[fnName] === 'function') {
|
31297 | loader[fnName]();
|
31298 | }
|
31299 | }));
|
31300 | }
|
31301 | |
31302 |
|
31303 |
|
31304 |
|
31305 |
|
31306 |
|
31307 |
|
31308 |
|
31309 | setCurrentTime(currentTime) {
|
31310 | const buffered = findRange(this.tech_.buffered(), currentTime);
|
31311 |
|
31312 | if (!(this.mainPlaylistLoader_ && this.mainPlaylistLoader_.media())) {
|
31313 |
|
31314 | return 0;
|
31315 | }
|
31316 |
|
31317 |
|
31318 |
|
31319 | if (!this.mainPlaylistLoader_.media().segments) {
|
31320 | return 0;
|
31321 | }
|
31322 |
|
31323 |
|
31324 | if (buffered && buffered.length) {
|
31325 | return currentTime;
|
31326 | }
|
31327 |
|
31328 |
|
31329 |
|
31330 | this.mainSegmentLoader_.pause();
|
31331 | this.mainSegmentLoader_.resetEverything();
|
31332 |
|
31333 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
31334 | this.audioSegmentLoader_.pause();
|
31335 | this.audioSegmentLoader_.resetEverything();
|
31336 | }
|
31337 |
|
31338 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
31339 | this.subtitleSegmentLoader_.pause();
|
31340 | this.subtitleSegmentLoader_.resetEverything();
|
31341 | }
|
31342 |
|
31343 |
|
31344 | this.load();
|
31345 | }
|
31346 | |
31347 |
|
31348 |
|
31349 |
|
31350 |
|
31351 |
|
31352 |
|
31353 | duration() {
|
31354 | if (!this.mainPlaylistLoader_) {
|
31355 | return 0;
|
31356 | }
|
31357 |
|
31358 | const media = this.mainPlaylistLoader_.media();
|
31359 |
|
31360 | if (!media) {
|
31361 |
|
31362 | return 0;
|
31363 | }
|
31364 |
|
31365 |
|
31366 |
|
31367 |
|
31368 |
|
31369 |
|
31370 |
|
31371 |
|
31372 |
|
31373 |
|
31374 |
|
31375 | if (!media.endList) {
|
31376 | return Infinity;
|
31377 | }
|
31378 |
|
31379 |
|
31380 |
|
31381 | if (this.mediaSource) {
|
31382 | return this.mediaSource.duration;
|
31383 | }
|
31384 |
|
31385 | return Vhs$1.Playlist.duration(media);
|
31386 | }
|
31387 | |
31388 |
|
31389 |
|
31390 |
|
31391 |
|
31392 |
|
31393 |
|
31394 | seekable() {
|
31395 | return this.seekable_;
|
31396 | }
|
31397 |
|
31398 | onSyncInfoUpdate_() {
|
31399 | let audioSeekable;
|
31400 |
|
31401 |
|
31402 |
|
31403 |
|
31404 |
|
31405 |
|
31406 |
|
31407 |
|
31408 |
|
31409 |
|
31410 |
|
31411 |
|
31412 |
|
31413 |
|
31414 |
|
31415 |
|
31416 |
|
31417 |
|
31418 |
|
31419 | if (!this.mainPlaylistLoader_) {
|
31420 | return;
|
31421 | }
|
31422 |
|
31423 | let media = this.mainPlaylistLoader_.media();
|
31424 |
|
31425 | if (!media) {
|
31426 | return;
|
31427 | }
|
31428 |
|
31429 | let expired = this.syncController_.getExpiredTime(media, this.duration());
|
31430 |
|
31431 | if (expired === null) {
|
31432 |
|
31433 | return;
|
31434 | }
|
31435 |
|
31436 | const main = this.mainPlaylistLoader_.main;
|
31437 | const mainSeekable = Vhs$1.Playlist.seekable(media, expired, Vhs$1.Playlist.liveEdgeDelay(main, media));
|
31438 |
|
31439 | if (mainSeekable.length === 0) {
|
31440 | return;
|
31441 | }
|
31442 |
|
31443 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
31444 | media = this.mediaTypes_.AUDIO.activePlaylistLoader.media();
|
31445 | expired = this.syncController_.getExpiredTime(media, this.duration());
|
31446 |
|
31447 | if (expired === null) {
|
31448 | return;
|
31449 | }
|
31450 |
|
31451 | audioSeekable = Vhs$1.Playlist.seekable(media, expired, Vhs$1.Playlist.liveEdgeDelay(main, media));
|
31452 |
|
31453 | if (audioSeekable.length === 0) {
|
31454 | return;
|
31455 | }
|
31456 | }
|
31457 |
|
31458 | let oldEnd;
|
31459 | let oldStart;
|
31460 |
|
31461 | if (this.seekable_ && this.seekable_.length) {
|
31462 | oldEnd = this.seekable_.end(0);
|
31463 | oldStart = this.seekable_.start(0);
|
31464 | }
|
31465 |
|
31466 | if (!audioSeekable) {
|
31467 |
|
31468 |
|
31469 | this.seekable_ = mainSeekable;
|
31470 | } else if (audioSeekable.start(0) > mainSeekable.end(0) || mainSeekable.start(0) > audioSeekable.end(0)) {
|
31471 |
|
31472 | this.seekable_ = mainSeekable;
|
31473 | } else {
|
31474 | this.seekable_ = 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)]]);
|
31475 | }
|
31476 |
|
31477 |
|
31478 | if (this.seekable_ && this.seekable_.length) {
|
31479 | if (this.seekable_.end(0) === oldEnd && this.seekable_.start(0) === oldStart) {
|
31480 | return;
|
31481 | }
|
31482 | }
|
31483 |
|
31484 | this.logger_(`seekable updated [${printableRange(this.seekable_)}]`);
|
31485 | this.tech_.trigger('seekablechanged');
|
31486 | }
|
31487 | |
31488 |
|
31489 |
|
31490 |
|
31491 |
|
31492 | updateDuration(isLive) {
|
31493 | if (this.updateDuration_) {
|
31494 | this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
|
31495 | this.updateDuration_ = null;
|
31496 | }
|
31497 |
|
31498 | if (this.mediaSource.readyState !== 'open') {
|
31499 | this.updateDuration_ = this.updateDuration.bind(this, isLive);
|
31500 | this.mediaSource.addEventListener('sourceopen', this.updateDuration_);
|
31501 | return;
|
31502 | }
|
31503 |
|
31504 | if (isLive) {
|
31505 | const seekable = this.seekable();
|
31506 |
|
31507 | if (!seekable.length) {
|
31508 | return;
|
31509 | }
|
31510 |
|
31511 |
|
31512 |
|
31513 |
|
31514 |
|
31515 |
|
31516 |
|
31517 |
|
31518 |
|
31519 |
|
31520 |
|
31521 |
|
31522 |
|
31523 |
|
31524 |
|
31525 |
|
31526 |
|
31527 |
|
31528 |
|
31529 |
|
31530 |
|
31531 |
|
31532 |
|
31533 |
|
31534 | if (isNaN(this.mediaSource.duration) || this.mediaSource.duration < seekable.end(seekable.length - 1)) {
|
31535 | this.sourceUpdater_.setDuration(seekable.end(seekable.length - 1));
|
31536 | }
|
31537 |
|
31538 | return;
|
31539 | }
|
31540 |
|
31541 | const buffered = this.tech_.buffered();
|
31542 | let duration = Vhs$1.Playlist.duration(this.mainPlaylistLoader_.media());
|
31543 |
|
31544 | if (buffered.length > 0) {
|
31545 | duration = Math.max(duration, buffered.end(buffered.length - 1));
|
31546 | }
|
31547 |
|
31548 | if (this.mediaSource.duration !== duration) {
|
31549 | this.sourceUpdater_.setDuration(duration);
|
31550 | }
|
31551 | }
|
31552 | |
31553 |
|
31554 |
|
31555 |
|
31556 |
|
31557 |
|
31558 | dispose() {
|
31559 | this.trigger('dispose');
|
31560 | this.decrypter_.terminate();
|
31561 | this.mainPlaylistLoader_.dispose();
|
31562 | this.mainSegmentLoader_.dispose();
|
31563 | this.contentSteeringController_.dispose();
|
31564 | this.keyStatusMap_.clear();
|
31565 |
|
31566 | if (this.loadOnPlay_) {
|
31567 | this.tech_.off('play', this.loadOnPlay_);
|
31568 | }
|
31569 |
|
31570 | ['AUDIO', 'SUBTITLES'].forEach(type => {
|
31571 | const groups = this.mediaTypes_[type].groups;
|
31572 |
|
31573 | for (const id in groups) {
|
31574 | groups[id].forEach(group => {
|
31575 | if (group.playlistLoader) {
|
31576 | group.playlistLoader.dispose();
|
31577 | }
|
31578 | });
|
31579 | }
|
31580 | });
|
31581 | this.audioSegmentLoader_.dispose();
|
31582 | this.subtitleSegmentLoader_.dispose();
|
31583 | this.sourceUpdater_.dispose();
|
31584 | this.timelineChangeController_.dispose();
|
31585 | this.stopABRTimer_();
|
31586 |
|
31587 | if (this.updateDuration_) {
|
31588 | this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
|
31589 | }
|
31590 |
|
31591 | this.mediaSource.removeEventListener('durationchange', this.handleDurationChange_);
|
31592 |
|
31593 | this.mediaSource.removeEventListener('sourceopen', this.handleSourceOpen_);
|
31594 | this.mediaSource.removeEventListener('sourceended', this.handleSourceEnded_);
|
31595 | this.off();
|
31596 | }
|
31597 | |
31598 |
|
31599 |
|
31600 |
|
31601 |
|
31602 |
|
31603 |
|
31604 | main() {
|
31605 | return this.mainPlaylistLoader_.main;
|
31606 | }
|
31607 | |
31608 |
|
31609 |
|
31610 |
|
31611 |
|
31612 |
|
31613 |
|
31614 | media() {
|
31615 |
|
31616 | return this.mainPlaylistLoader_.media() || this.initialMedia_;
|
31617 | }
|
31618 |
|
31619 | areMediaTypesKnown_() {
|
31620 | const usingAudioLoader = !!this.mediaTypes_.AUDIO.activePlaylistLoader;
|
31621 | const hasMainMediaInfo = !!this.mainSegmentLoader_.getCurrentMediaInfo_();
|
31622 |
|
31623 |
|
31624 | const hasAudioMediaInfo = !usingAudioLoader ? true : !!this.audioSegmentLoader_.getCurrentMediaInfo_();
|
31625 |
|
31626 | if (!hasMainMediaInfo || !hasAudioMediaInfo) {
|
31627 | return false;
|
31628 | }
|
31629 |
|
31630 | return true;
|
31631 | }
|
31632 |
|
31633 | getCodecsOrExclude_() {
|
31634 | const media = {
|
31635 | main: this.mainSegmentLoader_.getCurrentMediaInfo_() || {},
|
31636 | audio: this.audioSegmentLoader_.getCurrentMediaInfo_() || {}
|
31637 | };
|
31638 | const playlist = this.mainSegmentLoader_.getPendingSegmentPlaylist() || this.media();
|
31639 |
|
31640 | media.video = media.main;
|
31641 | const playlistCodecs = codecsForPlaylist(this.main(), playlist);
|
31642 | const codecs = {};
|
31643 | const usingAudioLoader = !!this.mediaTypes_.AUDIO.activePlaylistLoader;
|
31644 |
|
31645 | if (media.main.hasVideo) {
|
31646 | codecs.video = playlistCodecs.video || media.main.videoCodec || DEFAULT_VIDEO_CODEC;
|
31647 | }
|
31648 |
|
31649 | if (media.main.isMuxed) {
|
31650 | codecs.video += `,${playlistCodecs.audio || media.main.audioCodec || DEFAULT_AUDIO_CODEC}`;
|
31651 | }
|
31652 |
|
31653 | if (media.main.hasAudio && !media.main.isMuxed || media.audio.hasAudio || usingAudioLoader) {
|
31654 | codecs.audio = playlistCodecs.audio || media.main.audioCodec || media.audio.audioCodec || DEFAULT_AUDIO_CODEC;
|
31655 |
|
31656 | media.audio.isFmp4 = media.main.hasAudio && !media.main.isMuxed ? media.main.isFmp4 : media.audio.isFmp4;
|
31657 | }
|
31658 |
|
31659 |
|
31660 | if (!codecs.audio && !codecs.video) {
|
31661 | this.excludePlaylist({
|
31662 | playlistToExclude: playlist,
|
31663 | error: {
|
31664 | message: 'Could not determine codecs for playlist.'
|
31665 | },
|
31666 | playlistExclusionDuration: Infinity
|
31667 | });
|
31668 | return;
|
31669 | }
|
31670 |
|
31671 |
|
31672 | const supportFunction = (isFmp4, codec) => isFmp4 ? browserSupportsCodec(codec) : muxerSupportsCodec(codec);
|
31673 |
|
31674 | const unsupportedCodecs = {};
|
31675 | let unsupportedAudio;
|
31676 | ['video', 'audio'].forEach(function (type) {
|
31677 | if (codecs.hasOwnProperty(type) && !supportFunction(media[type].isFmp4, codecs[type])) {
|
31678 | const supporter = media[type].isFmp4 ? 'browser' : 'muxer';
|
31679 | unsupportedCodecs[supporter] = unsupportedCodecs[supporter] || [];
|
31680 | unsupportedCodecs[supporter].push(codecs[type]);
|
31681 |
|
31682 | if (type === 'audio') {
|
31683 | unsupportedAudio = supporter;
|
31684 | }
|
31685 | }
|
31686 | });
|
31687 |
|
31688 | if (usingAudioLoader && unsupportedAudio && playlist.attributes.AUDIO) {
|
31689 | const audioGroup = playlist.attributes.AUDIO;
|
31690 | this.main().playlists.forEach(variant => {
|
31691 | const variantAudioGroup = variant.attributes && variant.attributes.AUDIO;
|
31692 |
|
31693 | if (variantAudioGroup === audioGroup && variant !== playlist) {
|
31694 | variant.excludeUntil = Infinity;
|
31695 | }
|
31696 | });
|
31697 | this.logger_(`excluding audio group ${audioGroup} as ${unsupportedAudio} does not support codec(s): "${codecs.audio}"`);
|
31698 | }
|
31699 |
|
31700 |
|
31701 | if (Object.keys(unsupportedCodecs).length) {
|
31702 | const message = Object.keys(unsupportedCodecs).reduce((acc, supporter) => {
|
31703 | if (acc) {
|
31704 | acc += ', ';
|
31705 | }
|
31706 |
|
31707 | acc += `${supporter} does not support codec(s): "${unsupportedCodecs[supporter].join(',')}"`;
|
31708 | return acc;
|
31709 | }, '') + '.';
|
31710 | this.excludePlaylist({
|
31711 | playlistToExclude: playlist,
|
31712 | error: {
|
31713 | internal: true,
|
31714 | message
|
31715 | },
|
31716 | playlistExclusionDuration: Infinity
|
31717 | });
|
31718 | return;
|
31719 | }
|
31720 |
|
31721 |
|
31722 | if (this.sourceUpdater_.hasCreatedSourceBuffers() && !this.sourceUpdater_.canChangeType()) {
|
31723 | const switchMessages = [];
|
31724 | ['video', 'audio'].forEach(type => {
|
31725 | const newCodec = (parseCodecs(this.sourceUpdater_.codecs[type] || '')[0] || {}).type;
|
31726 | const oldCodec = (parseCodecs(codecs[type] || '')[0] || {}).type;
|
31727 |
|
31728 | if (newCodec && oldCodec && newCodec.toLowerCase() !== oldCodec.toLowerCase()) {
|
31729 | switchMessages.push(`"${this.sourceUpdater_.codecs[type]}" -> "${codecs[type]}"`);
|
31730 | }
|
31731 | });
|
31732 |
|
31733 | if (switchMessages.length) {
|
31734 | this.excludePlaylist({
|
31735 | playlistToExclude: playlist,
|
31736 | error: {
|
31737 | message: `Codec switching not supported: ${switchMessages.join(', ')}.`,
|
31738 | internal: true
|
31739 | },
|
31740 | playlistExclusionDuration: Infinity
|
31741 | });
|
31742 | return;
|
31743 | }
|
31744 | }
|
31745 |
|
31746 |
|
31747 |
|
31748 | return codecs;
|
31749 | }
|
31750 | |
31751 |
|
31752 |
|
31753 |
|
31754 |
|
31755 |
|
31756 |
|
31757 | tryToCreateSourceBuffers_() {
|
31758 |
|
31759 |
|
31760 | if (this.mediaSource.readyState !== 'open' || this.sourceUpdater_.hasCreatedSourceBuffers()) {
|
31761 | return;
|
31762 | }
|
31763 |
|
31764 | if (!this.areMediaTypesKnown_()) {
|
31765 | return;
|
31766 | }
|
31767 |
|
31768 | const codecs = this.getCodecsOrExclude_();
|
31769 |
|
31770 | if (!codecs) {
|
31771 | return;
|
31772 | }
|
31773 |
|
31774 | this.sourceUpdater_.createSourceBuffers(codecs);
|
31775 | const codecString = [codecs.video, codecs.audio].filter(Boolean).join(',');
|
31776 | this.excludeIncompatibleVariants_(codecString);
|
31777 | }
|
31778 | |
31779 |
|
31780 |
|
31781 |
|
31782 |
|
31783 | excludeUnsupportedVariants_() {
|
31784 | const playlists = this.main().playlists;
|
31785 | const ids = [];
|
31786 |
|
31787 |
|
31788 | Object.keys(playlists).forEach(key => {
|
31789 | const variant = playlists[key];
|
31790 |
|
31791 | if (ids.indexOf(variant.id) !== -1) {
|
31792 | return;
|
31793 | }
|
31794 |
|
31795 | ids.push(variant.id);
|
31796 | const codecs = codecsForPlaylist(this.main, variant);
|
31797 | const unsupported = [];
|
31798 |
|
31799 | if (codecs.audio && !muxerSupportsCodec(codecs.audio) && !browserSupportsCodec(codecs.audio)) {
|
31800 | unsupported.push(`audio codec ${codecs.audio}`);
|
31801 | }
|
31802 |
|
31803 | if (codecs.video && !muxerSupportsCodec(codecs.video) && !browserSupportsCodec(codecs.video)) {
|
31804 | unsupported.push(`video codec ${codecs.video}`);
|
31805 | }
|
31806 |
|
31807 | if (codecs.text && codecs.text === 'stpp.ttml.im1t') {
|
31808 | unsupported.push(`text codec ${codecs.text}`);
|
31809 | }
|
31810 |
|
31811 | if (unsupported.length) {
|
31812 | variant.excludeUntil = Infinity;
|
31813 | this.logger_(`excluding ${variant.id} for unsupported: ${unsupported.join(', ')}`);
|
31814 | }
|
31815 | });
|
31816 | }
|
31817 | |
31818 |
|
31819 |
|
31820 |
|
31821 |
|
31822 |
|
31823 |
|
31824 |
|
31825 |
|
31826 |
|
31827 |
|
31828 |
|
31829 |
|
31830 |
|
31831 |
|
31832 |
|
31833 | excludeIncompatibleVariants_(codecString) {
|
31834 | const ids = [];
|
31835 | const playlists = this.main().playlists;
|
31836 | const codecs = unwrapCodecList(parseCodecs(codecString));
|
31837 | const codecCount_ = codecCount(codecs);
|
31838 | const videoDetails = codecs.video && parseCodecs(codecs.video)[0] || null;
|
31839 | const audioDetails = codecs.audio && parseCodecs(codecs.audio)[0] || null;
|
31840 | Object.keys(playlists).forEach(key => {
|
31841 | const variant = playlists[key];
|
31842 |
|
31843 |
|
31844 | if (ids.indexOf(variant.id) !== -1 || variant.excludeUntil === Infinity) {
|
31845 | return;
|
31846 | }
|
31847 |
|
31848 | ids.push(variant.id);
|
31849 | const exclusionReasons = [];
|
31850 |
|
31851 | const variantCodecs = codecsForPlaylist(this.mainPlaylistLoader_.main, variant);
|
31852 | const variantCodecCount = codecCount(variantCodecs);
|
31853 |
|
31854 |
|
31855 | if (!variantCodecs.audio && !variantCodecs.video) {
|
31856 | return;
|
31857 | }
|
31858 |
|
31859 |
|
31860 |
|
31861 |
|
31862 | if (variantCodecCount !== codecCount_) {
|
31863 | exclusionReasons.push(`codec count "${variantCodecCount}" !== "${codecCount_}"`);
|
31864 | }
|
31865 |
|
31866 |
|
31867 |
|
31868 | if (!this.sourceUpdater_.canChangeType()) {
|
31869 | const variantVideoDetails = variantCodecs.video && parseCodecs(variantCodecs.video)[0] || null;
|
31870 | const variantAudioDetails = variantCodecs.audio && parseCodecs(variantCodecs.audio)[0] || null;
|
31871 |
|
31872 | if (variantVideoDetails && videoDetails && variantVideoDetails.type.toLowerCase() !== videoDetails.type.toLowerCase()) {
|
31873 | exclusionReasons.push(`video codec "${variantVideoDetails.type}" !== "${videoDetails.type}"`);
|
31874 | }
|
31875 |
|
31876 |
|
31877 | if (variantAudioDetails && audioDetails && variantAudioDetails.type.toLowerCase() !== audioDetails.type.toLowerCase()) {
|
31878 | exclusionReasons.push(`audio codec "${variantAudioDetails.type}" !== "${audioDetails.type}"`);
|
31879 | }
|
31880 | }
|
31881 |
|
31882 | if (exclusionReasons.length) {
|
31883 | variant.excludeUntil = Infinity;
|
31884 | this.logger_(`excluding ${variant.id}: ${exclusionReasons.join(' && ')}`);
|
31885 | }
|
31886 | });
|
31887 | }
|
31888 |
|
31889 | updateAdCues_(media) {
|
31890 | let offset = 0;
|
31891 | const seekable = this.seekable();
|
31892 |
|
31893 | if (seekable.length) {
|
31894 | offset = seekable.start(0);
|
31895 | }
|
31896 |
|
31897 | updateAdCues(media, this.cueTagsTrack_, offset);
|
31898 | }
|
31899 | |
31900 |
|
31901 |
|
31902 |
|
31903 |
|
31904 |
|
31905 |
|
31906 | goalBufferLength() {
|
31907 | const currentTime = this.tech_.currentTime();
|
31908 | const initial = Config.GOAL_BUFFER_LENGTH;
|
31909 | const rate = Config.GOAL_BUFFER_LENGTH_RATE;
|
31910 | const max = Math.max(initial, Config.MAX_GOAL_BUFFER_LENGTH);
|
31911 | return Math.min(initial + currentTime * rate, max);
|
31912 | }
|
31913 | |
31914 |
|
31915 |
|
31916 |
|
31917 |
|
31918 |
|
31919 |
|
31920 | bufferLowWaterLine() {
|
31921 | const currentTime = this.tech_.currentTime();
|
31922 | const initial = Config.BUFFER_LOW_WATER_LINE;
|
31923 | const rate = Config.BUFFER_LOW_WATER_LINE_RATE;
|
31924 | const max = Math.max(initial, Config.MAX_BUFFER_LOW_WATER_LINE);
|
31925 | const newMax = Math.max(initial, Config.EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE);
|
31926 | return Math.min(initial + currentTime * rate, this.bufferBasedABR ? newMax : max);
|
31927 | }
|
31928 |
|
31929 | bufferHighWaterLine() {
|
31930 | return Config.BUFFER_HIGH_WATER_LINE;
|
31931 | }
|
31932 |
|
31933 | addDateRangesToTextTrack_(dateRanges) {
|
31934 | createMetadataTrackIfNotExists(this.inbandTextTracks_, 'com.apple.streaming', this.tech_);
|
31935 | addDateRangeMetadata({
|
31936 | inbandTextTracks: this.inbandTextTracks_,
|
31937 | dateRanges
|
31938 | });
|
31939 | }
|
31940 |
|
31941 | addMetadataToTextTrack(dispatchType, metadataArray, videoDuration) {
|
31942 | const timestampOffset = this.sourceUpdater_.videoBuffer ? this.sourceUpdater_.videoTimestampOffset() : this.sourceUpdater_.audioTimestampOffset();
|
31943 |
|
31944 |
|
31945 |
|
31946 | createMetadataTrackIfNotExists(this.inbandTextTracks_, dispatchType, this.tech_);
|
31947 | addMetadata({
|
31948 | inbandTextTracks: this.inbandTextTracks_,
|
31949 | metadataArray,
|
31950 | timestampOffset,
|
31951 | videoDuration
|
31952 | });
|
31953 | }
|
31954 | |
31955 |
|
31956 |
|
31957 |
|
31958 |
|
31959 |
|
31960 |
|
31961 |
|
31962 | pathwayAttribute_(playlist) {
|
31963 | return playlist.attributes['PATHWAY-ID'] || playlist.attributes.serviceLocation;
|
31964 | }
|
31965 | |
31966 |
|
31967 |
|
31968 |
|
31969 |
|
31970 | initContentSteeringController_() {
|
31971 | const main = this.main();
|
31972 |
|
31973 | if (!main.contentSteering) {
|
31974 | return;
|
31975 | }
|
31976 |
|
31977 | for (const playlist of main.playlists) {
|
31978 | this.contentSteeringController_.addAvailablePathway(this.pathwayAttribute_(playlist));
|
31979 | }
|
31980 |
|
31981 | this.contentSteeringController_.assignTagProperties(main.uri, main.contentSteering);
|
31982 |
|
31983 | if (this.contentSteeringController_.queryBeforeStart) {
|
31984 |
|
31985 | this.contentSteeringController_.requestSteeringManifest(true);
|
31986 | return;
|
31987 | }
|
31988 |
|
31989 |
|
31990 | this.tech_.one('canplay', () => {
|
31991 | this.contentSteeringController_.requestSteeringManifest();
|
31992 | });
|
31993 | }
|
31994 | |
31995 |
|
31996 |
|
31997 |
|
31998 |
|
31999 | resetContentSteeringController_() {
|
32000 | this.contentSteeringController_.clearAvailablePathways();
|
32001 | this.contentSteeringController_.dispose();
|
32002 | this.initContentSteeringController_();
|
32003 | }
|
32004 | |
32005 |
|
32006 |
|
32007 |
|
32008 |
|
32009 | attachContentSteeringListeners_() {
|
32010 | this.contentSteeringController_.on('content-steering', this.excludeThenChangePathway_.bind(this));
|
32011 |
|
32012 | if (this.sourceType_ === 'dash') {
|
32013 | this.mainPlaylistLoader_.on('loadedplaylist', () => {
|
32014 | const main = this.main();
|
32015 |
|
32016 | const didDashTagChange = this.contentSteeringController_.didDASHTagChange(main.uri, main.contentSteering);
|
32017 |
|
32018 | const didPathwaysChange = () => {
|
32019 | const availablePathways = this.contentSteeringController_.getAvailablePathways();
|
32020 | const newPathways = [];
|
32021 |
|
32022 | for (const playlist of main.playlists) {
|
32023 | const serviceLocation = playlist.attributes.serviceLocation;
|
32024 |
|
32025 | if (serviceLocation) {
|
32026 | newPathways.push(serviceLocation);
|
32027 |
|
32028 | if (!availablePathways.has(serviceLocation)) {
|
32029 | return true;
|
32030 | }
|
32031 | }
|
32032 | }
|
32033 |
|
32034 |
|
32035 | if (!newPathways.length && availablePathways.size) {
|
32036 | return true;
|
32037 | }
|
32038 |
|
32039 | return false;
|
32040 | };
|
32041 |
|
32042 | if (didDashTagChange || didPathwaysChange()) {
|
32043 | this.resetContentSteeringController_();
|
32044 | }
|
32045 | });
|
32046 | }
|
32047 | }
|
32048 | |
32049 |
|
32050 |
|
32051 |
|
32052 |
|
32053 | excludeThenChangePathway_() {
|
32054 | const currentPathway = this.contentSteeringController_.getPathway();
|
32055 |
|
32056 | if (!currentPathway) {
|
32057 | return;
|
32058 | }
|
32059 |
|
32060 | this.handlePathwayClones_();
|
32061 | const main = this.main();
|
32062 | const playlists = main.playlists;
|
32063 | const ids = new Set();
|
32064 | let didEnablePlaylists = false;
|
32065 | Object.keys(playlists).forEach(key => {
|
32066 | const variant = playlists[key];
|
32067 | const pathwayId = this.pathwayAttribute_(variant);
|
32068 | const differentPathwayId = pathwayId && currentPathway !== pathwayId;
|
32069 | const steeringExclusion = variant.excludeUntil === Infinity && variant.lastExcludeReason_ === 'content-steering';
|
32070 |
|
32071 | if (steeringExclusion && !differentPathwayId) {
|
32072 | delete variant.excludeUntil;
|
32073 | delete variant.lastExcludeReason_;
|
32074 | didEnablePlaylists = true;
|
32075 | }
|
32076 |
|
32077 | const noExcludeUntil = !variant.excludeUntil && variant.excludeUntil !== Infinity;
|
32078 | const shouldExclude = !ids.has(variant.id) && differentPathwayId && noExcludeUntil;
|
32079 |
|
32080 | if (!shouldExclude) {
|
32081 | return;
|
32082 | }
|
32083 |
|
32084 | ids.add(variant.id);
|
32085 | variant.excludeUntil = Infinity;
|
32086 | variant.lastExcludeReason_ = 'content-steering';
|
32087 |
|
32088 | this.logger_(`excluding ${variant.id} for ${variant.lastExcludeReason_}`);
|
32089 | });
|
32090 |
|
32091 | if (this.contentSteeringController_.manifestType_ === 'DASH') {
|
32092 | Object.keys(this.mediaTypes_).forEach(key => {
|
32093 | const type = this.mediaTypes_[key];
|
32094 |
|
32095 | if (type.activePlaylistLoader) {
|
32096 | const currentPlaylist = type.activePlaylistLoader.media_;
|
32097 |
|
32098 | if (currentPlaylist && currentPlaylist.attributes.serviceLocation !== currentPathway) {
|
32099 | didEnablePlaylists = true;
|
32100 | }
|
32101 | }
|
32102 | });
|
32103 | }
|
32104 |
|
32105 | if (didEnablePlaylists) {
|
32106 | this.changeSegmentPathway_();
|
32107 | }
|
32108 | }
|
32109 | |
32110 |
|
32111 |
|
32112 |
|
32113 |
|
32114 |
|
32115 |
|
32116 |
|
32117 |
|
32118 |
|
32119 |
|
32120 |
|
32121 | handlePathwayClones_() {
|
32122 | const main = this.main();
|
32123 | const playlists = main.playlists;
|
32124 | const currentPathwayClones = this.contentSteeringController_.currentPathwayClones;
|
32125 | const nextPathwayClones = this.contentSteeringController_.nextPathwayClones;
|
32126 | const hasClones = currentPathwayClones && currentPathwayClones.size || nextPathwayClones && nextPathwayClones.size;
|
32127 |
|
32128 | if (!hasClones) {
|
32129 | return;
|
32130 | }
|
32131 |
|
32132 | for (const [id, clone] of currentPathwayClones.entries()) {
|
32133 | const newClone = nextPathwayClones.get(id);
|
32134 |
|
32135 | if (!newClone) {
|
32136 | this.mainPlaylistLoader_.updateOrDeleteClone(clone);
|
32137 | this.contentSteeringController_.excludePathway(id);
|
32138 | }
|
32139 | }
|
32140 |
|
32141 | for (const [id, clone] of nextPathwayClones.entries()) {
|
32142 | const oldClone = currentPathwayClones.get(id);
|
32143 |
|
32144 | if (!oldClone) {
|
32145 | const playlistsToClone = playlists.filter(p => {
|
32146 | return p.attributes['PATHWAY-ID'] === clone['BASE-ID'];
|
32147 | });
|
32148 | playlistsToClone.forEach(p => {
|
32149 | this.mainPlaylistLoader_.addClonePathway(clone, p);
|
32150 | });
|
32151 | this.contentSteeringController_.addAvailablePathway(id);
|
32152 | continue;
|
32153 | }
|
32154 |
|
32155 |
|
32156 | if (this.equalPathwayClones_(oldClone, clone)) {
|
32157 | continue;
|
32158 | }
|
32159 |
|
32160 |
|
32161 |
|
32162 | this.mainPlaylistLoader_.updateOrDeleteClone(clone, true);
|
32163 | this.contentSteeringController_.addAvailablePathway(id);
|
32164 | }
|
32165 |
|
32166 |
|
32167 | this.contentSteeringController_.currentPathwayClones = new Map(JSON.parse(JSON.stringify([...nextPathwayClones])));
|
32168 | }
|
32169 | |
32170 |
|
32171 |
|
32172 |
|
32173 |
|
32174 |
|
32175 |
|
32176 |
|
32177 |
|
32178 | equalPathwayClones_(a, b) {
|
32179 | if (a['BASE-ID'] !== b['BASE-ID'] || a.ID !== b.ID || a['URI-REPLACEMENT'].HOST !== b['URI-REPLACEMENT'].HOST) {
|
32180 | return false;
|
32181 | }
|
32182 |
|
32183 | const aParams = a['URI-REPLACEMENT'].PARAMS;
|
32184 | const bParams = b['URI-REPLACEMENT'].PARAMS;
|
32185 |
|
32186 |
|
32187 | for (const p in aParams) {
|
32188 | if (aParams[p] !== bParams[p]) {
|
32189 | return false;
|
32190 | }
|
32191 | }
|
32192 |
|
32193 | for (const p in bParams) {
|
32194 | if (aParams[p] !== bParams[p]) {
|
32195 | return false;
|
32196 | }
|
32197 | }
|
32198 |
|
32199 | return true;
|
32200 | }
|
32201 | |
32202 |
|
32203 |
|
32204 |
|
32205 |
|
32206 |
|
32207 | changeSegmentPathway_() {
|
32208 | const nextPlaylist = this.selectPlaylist();
|
32209 | this.pauseLoading();
|
32210 |
|
32211 | if (this.contentSteeringController_.manifestType_ === 'DASH') {
|
32212 | this.switchMediaForDASHContentSteering_();
|
32213 | }
|
32214 |
|
32215 | this.switchMedia_(nextPlaylist, 'content-steering');
|
32216 | }
|
32217 | |
32218 |
|
32219 |
|
32220 |
|
32221 |
|
32222 |
|
32223 |
|
32224 | excludeNonUsablePlaylistsByKeyId_() {
|
32225 | if (!this.mainPlaylistLoader_ || !this.mainPlaylistLoader_.main) {
|
32226 | return;
|
32227 | }
|
32228 |
|
32229 | let nonUsableKeyStatusCount = 0;
|
32230 | const NON_USABLE = 'non-usable';
|
32231 | this.mainPlaylistLoader_.main.playlists.forEach(playlist => {
|
32232 | const keyIdSet = this.mainPlaylistLoader_.getKeyIdSet(playlist);
|
32233 |
|
32234 | if (!keyIdSet || !keyIdSet.size) {
|
32235 | return;
|
32236 | }
|
32237 |
|
32238 | keyIdSet.forEach(key => {
|
32239 | const USABLE = 'usable';
|
32240 | const hasUsableKeyStatus = this.keyStatusMap_.has(key) && this.keyStatusMap_.get(key) === USABLE;
|
32241 | const nonUsableExclusion = playlist.lastExcludeReason_ === NON_USABLE && playlist.excludeUntil === Infinity;
|
32242 |
|
32243 | if (!hasUsableKeyStatus) {
|
32244 |
|
32245 | if (playlist.excludeUntil !== Infinity && playlist.lastExcludeReason_ !== NON_USABLE) {
|
32246 | playlist.excludeUntil = Infinity;
|
32247 | playlist.lastExcludeReason_ = NON_USABLE;
|
32248 | this.logger_(`excluding playlist ${playlist.id} because the key ID ${key} doesn't exist in the keyStatusMap or is not ${USABLE}`);
|
32249 | }
|
32250 |
|
32251 |
|
32252 | nonUsableKeyStatusCount++;
|
32253 | } else if (hasUsableKeyStatus && nonUsableExclusion) {
|
32254 | delete playlist.excludeUntil;
|
32255 | delete playlist.lastExcludeReason_;
|
32256 | this.logger_(`enabling playlist ${playlist.id} because key ID ${key} is ${USABLE}`);
|
32257 | }
|
32258 | });
|
32259 | });
|
32260 |
|
32261 | if (nonUsableKeyStatusCount >= this.mainPlaylistLoader_.main.playlists.length) {
|
32262 | this.mainPlaylistLoader_.main.playlists.forEach(playlist => {
|
32263 | const isNonHD = playlist && playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height < 720;
|
32264 | const excludedForNonUsableKey = playlist.excludeUntil === Infinity && playlist.lastExcludeReason_ === NON_USABLE;
|
32265 |
|
32266 | if (isNonHD && excludedForNonUsableKey) {
|
32267 |
|
32268 | delete playlist.excludeUntil;
|
32269 | videojs__default["default"].log.warn(`enabling non-HD playlist ${playlist.id} because all playlists were excluded due to ${NON_USABLE} key IDs`);
|
32270 | }
|
32271 | });
|
32272 | }
|
32273 | }
|
32274 | |
32275 |
|
32276 |
|
32277 |
|
32278 |
|
32279 |
|
32280 |
|
32281 |
|
32282 | addKeyStatus_(keyId, status) {
|
32283 | const isString = typeof keyId === 'string';
|
32284 | const keyIdHexString = isString ? keyId : bufferToHexString(keyId);
|
32285 | const formattedKeyIdString = keyIdHexString.slice(0, 32).toLowerCase();
|
32286 | this.logger_(`KeyStatus '${status}' with key ID ${formattedKeyIdString} added to the keyStatusMap`);
|
32287 | this.keyStatusMap_.set(formattedKeyIdString, status);
|
32288 | }
|
32289 | |
32290 |
|
32291 |
|
32292 |
|
32293 |
|
32294 |
|
32295 |
|
32296 |
|
32297 | updatePlaylistByKeyStatus(keyId, status) {
|
32298 | this.addKeyStatus_(keyId, status);
|
32299 |
|
32300 | if (!this.waitingForFastQualityPlaylistReceived_) {
|
32301 | this.excludeNonUsableThenChangePlaylist_();
|
32302 | }
|
32303 |
|
32304 |
|
32305 | this.mainPlaylistLoader_.off('loadedplaylist', this.excludeNonUsableThenChangePlaylist_.bind(this));
|
32306 | this.mainPlaylistLoader_.on('loadedplaylist', this.excludeNonUsableThenChangePlaylist_.bind(this));
|
32307 | }
|
32308 |
|
32309 | excludeNonUsableThenChangePlaylist_() {
|
32310 | this.excludeNonUsablePlaylistsByKeyId_();
|
32311 | this.fastQualityChange_();
|
32312 | }
|
32313 |
|
32314 | }
|
32315 |
|
32316 | |
32317 |
|
32318 |
|
32319 |
|
32320 |
|
32321 |
|
32322 |
|
32323 |
|
32324 |
|
32325 |
|
32326 |
|
32327 |
|
32328 |
|
32329 | const enableFunction = (loader, playlistID, changePlaylistFn) => enable => {
|
32330 | const playlist = loader.main.playlists[playlistID];
|
32331 | const incompatible = isIncompatible(playlist);
|
32332 | const currentlyEnabled = isEnabled(playlist);
|
32333 |
|
32334 | if (typeof enable === 'undefined') {
|
32335 | return currentlyEnabled;
|
32336 | }
|
32337 |
|
32338 | if (enable) {
|
32339 | delete playlist.disabled;
|
32340 | } else {
|
32341 | playlist.disabled = true;
|
32342 | }
|
32343 |
|
32344 | if (enable !== currentlyEnabled && !incompatible) {
|
32345 |
|
32346 | changePlaylistFn();
|
32347 |
|
32348 | if (enable) {
|
32349 | loader.trigger('renditionenabled');
|
32350 | } else {
|
32351 | loader.trigger('renditiondisabled');
|
32352 | }
|
32353 | }
|
32354 |
|
32355 | return enable;
|
32356 | };
|
32357 | |
32358 |
|
32359 |
|
32360 |
|
32361 |
|
32362 |
|
32363 |
|
32364 |
|
32365 |
|
32366 | class Representation {
|
32367 | constructor(vhsHandler, playlist, id) {
|
32368 | const {
|
32369 | playlistController_: pc
|
32370 | } = vhsHandler;
|
32371 | const qualityChangeFunction = pc.fastQualityChange_.bind(pc);
|
32372 |
|
32373 | if (playlist.attributes) {
|
32374 | const resolution = playlist.attributes.RESOLUTION;
|
32375 | this.width = resolution && resolution.width;
|
32376 | this.height = resolution && resolution.height;
|
32377 | this.bandwidth = playlist.attributes.BANDWIDTH;
|
32378 | this.frameRate = playlist.attributes['FRAME-RATE'];
|
32379 | }
|
32380 |
|
32381 | this.codecs = codecsForPlaylist(pc.main(), playlist);
|
32382 | this.playlist = playlist;
|
32383 |
|
32384 |
|
32385 | this.id = id;
|
32386 |
|
32387 |
|
32388 | this.enabled = enableFunction(vhsHandler.playlists, playlist.id, qualityChangeFunction);
|
32389 | }
|
32390 |
|
32391 | }
|
32392 | |
32393 |
|
32394 |
|
32395 |
|
32396 |
|
32397 |
|
32398 |
|
32399 |
|
32400 |
|
32401 | const renditionSelectionMixin = function (vhsHandler) {
|
32402 |
|
32403 | vhsHandler.representations = () => {
|
32404 | const main = vhsHandler.playlistController_.main();
|
32405 | const playlists = isAudioOnly(main) ? vhsHandler.playlistController_.getAudioTrackPlaylists_() : main.playlists;
|
32406 |
|
32407 | if (!playlists) {
|
32408 | return [];
|
32409 | }
|
32410 |
|
32411 | return playlists.filter(media => !isIncompatible(media)).map((e, i) => new Representation(vhsHandler, e, e.id));
|
32412 | };
|
32413 | };
|
32414 |
|
32415 | |
32416 |
|
32417 |
|
32418 |
|
32419 |
|
32420 |
|
32421 |
|
32422 |
|
32423 |
|
32424 |
|
32425 | const timerCancelEvents = ['seeking', 'seeked', 'pause', 'playing', 'error'];
|
32426 | |
32427 |
|
32428 |
|
32429 |
|
32430 | class PlaybackWatcher {
|
32431 | |
32432 |
|
32433 |
|
32434 |
|
32435 |
|
32436 |
|
32437 | constructor(options) {
|
32438 | this.playlistController_ = options.playlistController;
|
32439 | this.tech_ = options.tech;
|
32440 | this.seekable = options.seekable;
|
32441 | this.allowSeeksWithinUnsafeLiveWindow = options.allowSeeksWithinUnsafeLiveWindow;
|
32442 | this.liveRangeSafeTimeDelta = options.liveRangeSafeTimeDelta;
|
32443 | this.media = options.media;
|
32444 | this.consecutiveUpdates = 0;
|
32445 | this.lastRecordedTime = null;
|
32446 | this.checkCurrentTimeTimeout_ = null;
|
32447 | this.logger_ = logger('PlaybackWatcher');
|
32448 | this.logger_('initialize');
|
32449 |
|
32450 | const playHandler = () => this.monitorCurrentTime_();
|
32451 |
|
32452 | const canPlayHandler = () => this.monitorCurrentTime_();
|
32453 |
|
32454 | const waitingHandler = () => this.techWaiting_();
|
32455 |
|
32456 | const cancelTimerHandler = () => this.resetTimeUpdate_();
|
32457 |
|
32458 | const pc = this.playlistController_;
|
32459 | const loaderTypes = ['main', 'subtitle', 'audio'];
|
32460 | const loaderChecks = {};
|
32461 | loaderTypes.forEach(type => {
|
32462 | loaderChecks[type] = {
|
32463 | reset: () => this.resetSegmentDownloads_(type),
|
32464 | updateend: () => this.checkSegmentDownloads_(type)
|
32465 | };
|
32466 | pc[`${type}SegmentLoader_`].on('appendsdone', loaderChecks[type].updateend);
|
32467 |
|
32468 |
|
32469 |
|
32470 | pc[`${type}SegmentLoader_`].on('playlistupdate', loaderChecks[type].reset);
|
32471 |
|
32472 |
|
32473 |
|
32474 |
|
32475 | this.tech_.on(['seeked', 'seeking'], loaderChecks[type].reset);
|
32476 | });
|
32477 | |
32478 |
|
32479 |
|
32480 |
|
32481 |
|
32482 |
|
32483 |
|
32484 |
|
32485 |
|
32486 | const setSeekingHandlers = fn => {
|
32487 | ['main', 'audio'].forEach(type => {
|
32488 | pc[`${type}SegmentLoader_`][fn]('appended', this.seekingAppendCheck_);
|
32489 | });
|
32490 | };
|
32491 |
|
32492 | this.seekingAppendCheck_ = () => {
|
32493 | if (this.fixesBadSeeks_()) {
|
32494 | this.consecutiveUpdates = 0;
|
32495 | this.lastRecordedTime = this.tech_.currentTime();
|
32496 | setSeekingHandlers('off');
|
32497 | }
|
32498 | };
|
32499 |
|
32500 | this.clearSeekingAppendCheck_ = () => setSeekingHandlers('off');
|
32501 |
|
32502 | this.watchForBadSeeking_ = () => {
|
32503 | this.clearSeekingAppendCheck_();
|
32504 | setSeekingHandlers('on');
|
32505 | };
|
32506 |
|
32507 | this.tech_.on('seeked', this.clearSeekingAppendCheck_);
|
32508 | this.tech_.on('seeking', this.watchForBadSeeking_);
|
32509 | this.tech_.on('waiting', waitingHandler);
|
32510 | this.tech_.on(timerCancelEvents, cancelTimerHandler);
|
32511 | this.tech_.on('canplay', canPlayHandler);
|
32512 | |
32513 |
|
32514 |
|
32515 |
|
32516 |
|
32517 |
|
32518 |
|
32519 |
|
32520 |
|
32521 |
|
32522 |
|
32523 | this.tech_.one('play', playHandler);
|
32524 |
|
32525 | this.dispose = () => {
|
32526 | this.clearSeekingAppendCheck_();
|
32527 | this.logger_('dispose');
|
32528 | this.tech_.off('waiting', waitingHandler);
|
32529 | this.tech_.off(timerCancelEvents, cancelTimerHandler);
|
32530 | this.tech_.off('canplay', canPlayHandler);
|
32531 | this.tech_.off('play', playHandler);
|
32532 | this.tech_.off('seeking', this.watchForBadSeeking_);
|
32533 | this.tech_.off('seeked', this.clearSeekingAppendCheck_);
|
32534 | loaderTypes.forEach(type => {
|
32535 | pc[`${type}SegmentLoader_`].off('appendsdone', loaderChecks[type].updateend);
|
32536 | pc[`${type}SegmentLoader_`].off('playlistupdate', loaderChecks[type].reset);
|
32537 | this.tech_.off(['seeked', 'seeking'], loaderChecks[type].reset);
|
32538 | });
|
32539 |
|
32540 | if (this.checkCurrentTimeTimeout_) {
|
32541 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
32542 | }
|
32543 |
|
32544 | this.resetTimeUpdate_();
|
32545 | };
|
32546 | }
|
32547 | |
32548 |
|
32549 |
|
32550 |
|
32551 |
|
32552 |
|
32553 |
|
32554 | monitorCurrentTime_() {
|
32555 | this.checkCurrentTime_();
|
32556 |
|
32557 | if (this.checkCurrentTimeTimeout_) {
|
32558 | window.clearTimeout(this.checkCurrentTimeTimeout_);
|
32559 | }
|
32560 |
|
32561 |
|
32562 | this.checkCurrentTimeTimeout_ = window.setTimeout(this.monitorCurrentTime_.bind(this), 250);
|
32563 | }
|
32564 | |
32565 |
|
32566 |
|
32567 |
|
32568 |
|
32569 |
|
32570 |
|
32571 |
|
32572 |
|
32573 |
|
32574 |
|
32575 |
|
32576 | resetSegmentDownloads_(type) {
|
32577 | const loader = this.playlistController_[`${type}SegmentLoader_`];
|
32578 |
|
32579 | if (this[`${type}StalledDownloads_`] > 0) {
|
32580 | this.logger_(`resetting possible stalled download count for ${type} loader`);
|
32581 | }
|
32582 |
|
32583 | this[`${type}StalledDownloads_`] = 0;
|
32584 | this[`${type}Buffered_`] = loader.buffered_();
|
32585 | }
|
32586 | |
32587 |
|
32588 |
|
32589 |
|
32590 |
|
32591 |
|
32592 |
|
32593 |
|
32594 |
|
32595 |
|
32596 |
|
32597 |
|
32598 | checkSegmentDownloads_(type) {
|
32599 | const pc = this.playlistController_;
|
32600 | const loader = pc[`${type}SegmentLoader_`];
|
32601 | const buffered = loader.buffered_();
|
32602 | const isBufferedDifferent = isRangeDifferent(this[`${type}Buffered_`], buffered);
|
32603 | this[`${type}Buffered_`] = buffered;
|
32604 |
|
32605 |
|
32606 |
|
32607 | if (isBufferedDifferent) {
|
32608 | this.resetSegmentDownloads_(type);
|
32609 | return;
|
32610 | }
|
32611 |
|
32612 | this[`${type}StalledDownloads_`]++;
|
32613 | this.logger_(`found #${this[`${type}StalledDownloads_`]} ${type} appends that did not increase buffer (possible stalled download)`, {
|
32614 | playlistId: loader.playlist_ && loader.playlist_.id,
|
32615 | buffered: timeRangesToArray(buffered)
|
32616 | });
|
32617 |
|
32618 | if (this[`${type}StalledDownloads_`] < 10) {
|
32619 | return;
|
32620 | }
|
32621 |
|
32622 | this.logger_(`${type} loader stalled download exclusion`);
|
32623 | this.resetSegmentDownloads_(type);
|
32624 | this.tech_.trigger({
|
32625 | type: 'usage',
|
32626 | name: `vhs-${type}-download-exclusion`
|
32627 | });
|
32628 |
|
32629 | if (type === 'subtitle') {
|
32630 | return;
|
32631 | }
|
32632 |
|
32633 |
|
32634 |
|
32635 | pc.excludePlaylist({
|
32636 | error: {
|
32637 | message: `Excessive ${type} segment downloading detected.`
|
32638 | },
|
32639 | playlistExclusionDuration: Infinity
|
32640 | });
|
32641 | }
|
32642 | |
32643 |
|
32644 |
|
32645 |
|
32646 |
|
32647 |
|
32648 |
|
32649 |
|
32650 |
|
32651 | checkCurrentTime_() {
|
32652 | if (this.tech_.paused() || this.tech_.seeking()) {
|
32653 | return;
|
32654 | }
|
32655 |
|
32656 | const currentTime = this.tech_.currentTime();
|
32657 | const buffered = this.tech_.buffered();
|
32658 |
|
32659 | if (this.lastRecordedTime === currentTime && (!buffered.length || currentTime + SAFE_TIME_DELTA >= buffered.end(buffered.length - 1))) {
|
32660 |
|
32661 |
|
32662 |
|
32663 |
|
32664 |
|
32665 | return this.techWaiting_();
|
32666 | }
|
32667 |
|
32668 | if (this.consecutiveUpdates >= 5 && currentTime === this.lastRecordedTime) {
|
32669 | this.consecutiveUpdates++;
|
32670 | this.waiting_();
|
32671 | } else if (currentTime === this.lastRecordedTime) {
|
32672 | this.consecutiveUpdates++;
|
32673 | } else {
|
32674 | this.consecutiveUpdates = 0;
|
32675 | this.lastRecordedTime = currentTime;
|
32676 | }
|
32677 | }
|
32678 | |
32679 |
|
32680 |
|
32681 |
|
32682 |
|
32683 |
|
32684 |
|
32685 | resetTimeUpdate_() {
|
32686 | this.consecutiveUpdates = 0;
|
32687 | }
|
32688 | |
32689 |
|
32690 |
|
32691 |
|
32692 |
|
32693 |
|
32694 |
|
32695 |
|
32696 | fixesBadSeeks_() {
|
32697 | const seeking = this.tech_.seeking();
|
32698 |
|
32699 | if (!seeking) {
|
32700 | return false;
|
32701 | }
|
32702 |
|
32703 |
|
32704 |
|
32705 |
|
32706 |
|
32707 | const seekable = this.seekable();
|
32708 | const currentTime = this.tech_.currentTime();
|
32709 | const isAfterSeekableRange = this.afterSeekableWindow_(seekable, currentTime, this.media(), this.allowSeeksWithinUnsafeLiveWindow);
|
32710 | let seekTo;
|
32711 |
|
32712 | if (isAfterSeekableRange) {
|
32713 | const seekableEnd = seekable.end(seekable.length - 1);
|
32714 |
|
32715 | seekTo = seekableEnd;
|
32716 | }
|
32717 |
|
32718 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
32719 | const seekableStart = seekable.start(0);
|
32720 |
|
32721 |
|
32722 | seekTo = seekableStart + (
|
32723 |
|
32724 | seekableStart === seekable.end(0) ? 0 : SAFE_TIME_DELTA);
|
32725 | }
|
32726 |
|
32727 | if (typeof seekTo !== 'undefined') {
|
32728 | this.logger_(`Trying to seek outside of seekable at time ${currentTime} with ` + `seekable range ${printableRange(seekable)}. Seeking to ` + `${seekTo}.`);
|
32729 | this.tech_.setCurrentTime(seekTo);
|
32730 | return true;
|
32731 | }
|
32732 |
|
32733 | const sourceUpdater = this.playlistController_.sourceUpdater_;
|
32734 | const buffered = this.tech_.buffered();
|
32735 | const audioBuffered = sourceUpdater.audioBuffer ? sourceUpdater.audioBuffered() : null;
|
32736 | const videoBuffered = sourceUpdater.videoBuffer ? sourceUpdater.videoBuffered() : null;
|
32737 | const media = this.media();
|
32738 |
|
32739 |
|
32740 | const minAppendedDuration = media.partTargetDuration ? media.partTargetDuration : (media.targetDuration - TIME_FUDGE_FACTOR) * 2;
|
32741 |
|
32742 |
|
32743 | const bufferedToCheck = [audioBuffered, videoBuffered];
|
32744 |
|
32745 | for (let i = 0; i < bufferedToCheck.length; i++) {
|
32746 |
|
32747 | if (!bufferedToCheck[i]) {
|
32748 | continue;
|
32749 | }
|
32750 |
|
32751 | const timeAhead = timeAheadOf(bufferedToCheck[i], currentTime);
|
32752 |
|
32753 |
|
32754 | if (timeAhead < minAppendedDuration) {
|
32755 | return false;
|
32756 | }
|
32757 | }
|
32758 |
|
32759 | const nextRange = findNextRange(buffered, currentTime);
|
32760 |
|
32761 |
|
32762 | if (nextRange.length === 0) {
|
32763 | return false;
|
32764 | }
|
32765 |
|
32766 | seekTo = nextRange.start(0) + SAFE_TIME_DELTA;
|
32767 | this.logger_(`Buffered region starts (${nextRange.start(0)}) ` + ` just beyond seek point (${currentTime}). Seeking to ${seekTo}.`);
|
32768 | this.tech_.setCurrentTime(seekTo);
|
32769 | return true;
|
32770 | }
|
32771 | |
32772 |
|
32773 |
|
32774 |
|
32775 |
|
32776 |
|
32777 |
|
32778 | waiting_() {
|
32779 | if (this.techWaiting_()) {
|
32780 | return;
|
32781 | }
|
32782 |
|
32783 |
|
32784 | const currentTime = this.tech_.currentTime();
|
32785 | const buffered = this.tech_.buffered();
|
32786 | const currentRange = findRange(buffered, currentTime);
|
32787 |
|
32788 |
|
32789 |
|
32790 |
|
32791 |
|
32792 |
|
32793 |
|
32794 |
|
32795 | if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
|
32796 | this.resetTimeUpdate_();
|
32797 | this.tech_.setCurrentTime(currentTime);
|
32798 | 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.');
|
32799 |
|
32800 | this.tech_.trigger({
|
32801 | type: 'usage',
|
32802 | name: 'vhs-unknown-waiting'
|
32803 | });
|
32804 | return;
|
32805 | }
|
32806 | }
|
32807 | |
32808 |
|
32809 |
|
32810 |
|
32811 |
|
32812 |
|
32813 |
|
32814 |
|
32815 |
|
32816 |
|
32817 | techWaiting_() {
|
32818 | const seekable = this.seekable();
|
32819 | const currentTime = this.tech_.currentTime();
|
32820 |
|
32821 | if (this.tech_.seeking()) {
|
32822 |
|
32823 | return true;
|
32824 | }
|
32825 |
|
32826 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
32827 | const livePoint = seekable.end(seekable.length - 1);
|
32828 | this.logger_(`Fell out of live window at time ${currentTime}. Seeking to ` + `live point (seekable end) ${livePoint}`);
|
32829 | this.resetTimeUpdate_();
|
32830 | this.tech_.setCurrentTime(livePoint);
|
32831 |
|
32832 | this.tech_.trigger({
|
32833 | type: 'usage',
|
32834 | name: 'vhs-live-resync'
|
32835 | });
|
32836 | return true;
|
32837 | }
|
32838 |
|
32839 | const sourceUpdater = this.tech_.vhs.playlistController_.sourceUpdater_;
|
32840 | const buffered = this.tech_.buffered();
|
32841 | const videoUnderflow = this.videoUnderflow_({
|
32842 | audioBuffered: sourceUpdater.audioBuffered(),
|
32843 | videoBuffered: sourceUpdater.videoBuffered(),
|
32844 | currentTime
|
32845 | });
|
32846 |
|
32847 | if (videoUnderflow) {
|
32848 |
|
32849 |
|
32850 |
|
32851 |
|
32852 | this.resetTimeUpdate_();
|
32853 | this.tech_.setCurrentTime(currentTime);
|
32854 |
|
32855 | this.tech_.trigger({
|
32856 | type: 'usage',
|
32857 | name: 'vhs-video-underflow'
|
32858 | });
|
32859 | return true;
|
32860 | }
|
32861 |
|
32862 | const nextRange = findNextRange(buffered, currentTime);
|
32863 |
|
32864 | if (nextRange.length > 0) {
|
32865 | this.logger_(`Stopped at ${currentTime} and seeking to ${nextRange.start(0)}`);
|
32866 | this.resetTimeUpdate_();
|
32867 | this.skipTheGap_(currentTime);
|
32868 | return true;
|
32869 | }
|
32870 |
|
32871 |
|
32872 | return false;
|
32873 | }
|
32874 |
|
32875 | afterSeekableWindow_(seekable, currentTime, playlist, allowSeeksWithinUnsafeLiveWindow = false) {
|
32876 | if (!seekable.length) {
|
32877 |
|
32878 | return false;
|
32879 | }
|
32880 |
|
32881 | let allowedEnd = seekable.end(seekable.length - 1) + SAFE_TIME_DELTA;
|
32882 | const isLive = !playlist.endList;
|
32883 | const isLLHLS = typeof playlist.partTargetDuration === 'number';
|
32884 |
|
32885 | if (isLive && (isLLHLS || allowSeeksWithinUnsafeLiveWindow)) {
|
32886 | allowedEnd = seekable.end(seekable.length - 1) + playlist.targetDuration * 3;
|
32887 | }
|
32888 |
|
32889 | if (currentTime > allowedEnd) {
|
32890 | return true;
|
32891 | }
|
32892 |
|
32893 | return false;
|
32894 | }
|
32895 |
|
32896 | beforeSeekableWindow_(seekable, currentTime) {
|
32897 | if (seekable.length &&
|
32898 | seekable.start(0) > 0 && currentTime < seekable.start(0) - this.liveRangeSafeTimeDelta) {
|
32899 | return true;
|
32900 | }
|
32901 |
|
32902 | return false;
|
32903 | }
|
32904 |
|
32905 | videoUnderflow_({
|
32906 | videoBuffered,
|
32907 | audioBuffered,
|
32908 | currentTime
|
32909 | }) {
|
32910 |
|
32911 | if (!videoBuffered) {
|
32912 | return;
|
32913 | }
|
32914 |
|
32915 | let gap;
|
32916 |
|
32917 | if (videoBuffered.length && audioBuffered.length) {
|
32918 |
|
32919 |
|
32920 |
|
32921 | const lastVideoRange = findRange(videoBuffered, currentTime - 3);
|
32922 | const videoRange = findRange(videoBuffered, currentTime);
|
32923 | const audioRange = findRange(audioBuffered, currentTime);
|
32924 |
|
32925 | if (audioRange.length && !videoRange.length && lastVideoRange.length) {
|
32926 | gap = {
|
32927 | start: lastVideoRange.end(0),
|
32928 | end: audioRange.end(0)
|
32929 | };
|
32930 | }
|
32931 |
|
32932 | } else {
|
32933 | const nextRange = findNextRange(videoBuffered, currentTime);
|
32934 |
|
32935 |
|
32936 | if (!nextRange.length) {
|
32937 | gap = this.gapFromVideoUnderflow_(videoBuffered, currentTime);
|
32938 | }
|
32939 | }
|
32940 |
|
32941 | if (gap) {
|
32942 | this.logger_(`Encountered a gap in video from ${gap.start} to ${gap.end}. ` + `Seeking to current time ${currentTime}`);
|
32943 | return true;
|
32944 | }
|
32945 |
|
32946 | return false;
|
32947 | }
|
32948 | |
32949 |
|
32950 |
|
32951 |
|
32952 |
|
32953 |
|
32954 |
|
32955 |
|
32956 | skipTheGap_(scheduledCurrentTime) {
|
32957 | const buffered = this.tech_.buffered();
|
32958 | const currentTime = this.tech_.currentTime();
|
32959 | const nextRange = findNextRange(buffered, currentTime);
|
32960 | this.resetTimeUpdate_();
|
32961 |
|
32962 | if (nextRange.length === 0 || currentTime !== scheduledCurrentTime) {
|
32963 | return;
|
32964 | }
|
32965 |
|
32966 | this.logger_('skipTheGap_:', 'currentTime:', currentTime, 'scheduled currentTime:', scheduledCurrentTime, 'nextRange start:', nextRange.start(0));
|
32967 |
|
32968 | this.tech_.setCurrentTime(nextRange.start(0) + TIME_FUDGE_FACTOR);
|
32969 | this.tech_.trigger({
|
32970 | type: 'usage',
|
32971 | name: 'vhs-gap-skip'
|
32972 | });
|
32973 | }
|
32974 |
|
32975 | gapFromVideoUnderflow_(buffered, currentTime) {
|
32976 |
|
32977 |
|
32978 |
|
32979 |
|
32980 |
|
32981 |
|
32982 |
|
32983 |
|
32984 |
|
32985 |
|
32986 |
|
32987 |
|
32988 |
|
32989 |
|
32990 |
|
32991 |
|
32992 |
|
32993 |
|
32994 |
|
32995 |
|
32996 |
|
32997 |
|
32998 | const gaps = findGaps(buffered);
|
32999 |
|
33000 | for (let i = 0; i < gaps.length; i++) {
|
33001 | const start = gaps.start(i);
|
33002 | const end = gaps.end(i);
|
33003 |
|
33004 | if (currentTime - start < 4 && currentTime - start > 2) {
|
33005 | return {
|
33006 | start,
|
33007 | end
|
33008 | };
|
33009 | }
|
33010 | }
|
33011 |
|
33012 | return null;
|
33013 | }
|
33014 |
|
33015 | }
|
33016 |
|
33017 | const defaultOptions = {
|
33018 | errorInterval: 30,
|
33019 |
|
33020 | getSource(next) {
|
33021 | const tech = this.tech({
|
33022 | IWillNotUseThisInPlugins: true
|
33023 | });
|
33024 | const sourceObj = tech.currentSource_ || this.currentSource();
|
33025 | return next(sourceObj);
|
33026 | }
|
33027 |
|
33028 | };
|
33029 | |
33030 |
|
33031 |
|
33032 |
|
33033 |
|
33034 |
|
33035 |
|
33036 |
|
33037 | const initPlugin = function (player, options) {
|
33038 | let lastCalled = 0;
|
33039 | let seekTo = 0;
|
33040 | const localOptions = merge$1(defaultOptions, options);
|
33041 | player.ready(() => {
|
33042 | player.trigger({
|
33043 | type: 'usage',
|
33044 | name: 'vhs-error-reload-initialized'
|
33045 | });
|
33046 | });
|
33047 | |
33048 |
|
33049 |
|
33050 |
|
33051 |
|
33052 |
|
33053 |
|
33054 | const loadedMetadataHandler = function () {
|
33055 | if (seekTo) {
|
33056 | player.currentTime(seekTo);
|
33057 | }
|
33058 | };
|
33059 | |
33060 |
|
33061 |
|
33062 |
|
33063 |
|
33064 |
|
33065 |
|
33066 |
|
33067 | const setSource = function (sourceObj) {
|
33068 | if (sourceObj === null || sourceObj === undefined) {
|
33069 | return;
|
33070 | }
|
33071 |
|
33072 | seekTo = player.duration() !== Infinity && player.currentTime() || 0;
|
33073 | player.one('loadedmetadata', loadedMetadataHandler);
|
33074 | player.src(sourceObj);
|
33075 | player.trigger({
|
33076 | type: 'usage',
|
33077 | name: 'vhs-error-reload'
|
33078 | });
|
33079 | player.play();
|
33080 | };
|
33081 | |
33082 |
|
33083 |
|
33084 |
|
33085 |
|
33086 |
|
33087 |
|
33088 |
|
33089 | const errorHandler = function () {
|
33090 |
|
33091 |
|
33092 | if (Date.now() - lastCalled < localOptions.errorInterval * 1000) {
|
33093 | player.trigger({
|
33094 | type: 'usage',
|
33095 | name: 'vhs-error-reload-canceled'
|
33096 | });
|
33097 | return;
|
33098 | }
|
33099 |
|
33100 | if (!localOptions.getSource || typeof localOptions.getSource !== 'function') {
|
33101 | videojs__default["default"].log.error('ERROR: reloadSourceOnError - The option getSource must be a function!');
|
33102 | return;
|
33103 | }
|
33104 |
|
33105 | lastCalled = Date.now();
|
33106 | return localOptions.getSource.call(player, setSource);
|
33107 | };
|
33108 | |
33109 |
|
33110 |
|
33111 |
|
33112 |
|
33113 |
|
33114 |
|
33115 | const cleanupEvents = function () {
|
33116 | player.off('loadedmetadata', loadedMetadataHandler);
|
33117 | player.off('error', errorHandler);
|
33118 | player.off('dispose', cleanupEvents);
|
33119 | };
|
33120 | |
33121 |
|
33122 |
|
33123 |
|
33124 |
|
33125 |
|
33126 |
|
33127 |
|
33128 | const reinitPlugin = function (newOptions) {
|
33129 | cleanupEvents();
|
33130 | initPlugin(player, newOptions);
|
33131 | };
|
33132 |
|
33133 | player.on('error', errorHandler);
|
33134 | player.on('dispose', cleanupEvents);
|
33135 |
|
33136 |
|
33137 | player.reloadSourceOnError = reinitPlugin;
|
33138 | };
|
33139 | |
33140 |
|
33141 |
|
33142 |
|
33143 |
|
33144 |
|
33145 |
|
33146 |
|
33147 | const reloadSourceOnError = function (options) {
|
33148 | initPlugin(this, options);
|
33149 | };
|
33150 |
|
33151 | var version$4 = "3.10.0";
|
33152 |
|
33153 | var version$3 = "7.0.2";
|
33154 |
|
33155 | var version$2 = "1.3.0";
|
33156 |
|
33157 | var version$1 = "7.1.0";
|
33158 |
|
33159 | var version = "4.0.1";
|
33160 |
|
33161 | |
33162 |
|
33163 |
|
33164 |
|
33165 |
|
33166 |
|
33167 | const Vhs = {
|
33168 | PlaylistLoader,
|
33169 | Playlist,
|
33170 | utils,
|
33171 | STANDARD_PLAYLIST_SELECTOR: lastBandwidthSelector,
|
33172 | INITIAL_PLAYLIST_SELECTOR: lowestBitrateCompatibleVariantSelector,
|
33173 | lastBandwidthSelector,
|
33174 | movingAverageBandwidthSelector,
|
33175 | comparePlaylistBandwidth,
|
33176 | comparePlaylistResolution,
|
33177 | xhr: xhrFactory()
|
33178 | };
|
33179 |
|
33180 | Object.keys(Config).forEach(prop => {
|
33181 | Object.defineProperty(Vhs, prop, {
|
33182 | get() {
|
33183 | videojs__default["default"].log.warn(`using Vhs.${prop} is UNSAFE be sure you know what you are doing`);
|
33184 | return Config[prop];
|
33185 | },
|
33186 |
|
33187 | set(value) {
|
33188 | videojs__default["default"].log.warn(`using Vhs.${prop} is UNSAFE be sure you know what you are doing`);
|
33189 |
|
33190 | if (typeof value !== 'number' || value < 0) {
|
33191 | videojs__default["default"].log.warn(`value of Vhs.${prop} must be greater than or equal to 0`);
|
33192 | return;
|
33193 | }
|
33194 |
|
33195 | Config[prop] = value;
|
33196 | }
|
33197 |
|
33198 | });
|
33199 | });
|
33200 | const LOCAL_STORAGE_KEY = 'videojs-vhs';
|
33201 | |
33202 |
|
33203 |
|
33204 |
|
33205 |
|
33206 |
|
33207 |
|
33208 |
|
33209 | const handleVhsMediaChange = function (qualityLevels, playlistLoader) {
|
33210 | const newPlaylist = playlistLoader.media();
|
33211 | let selectedIndex = -1;
|
33212 |
|
33213 | for (let i = 0; i < qualityLevels.length; i++) {
|
33214 | if (qualityLevels[i].id === newPlaylist.id) {
|
33215 | selectedIndex = i;
|
33216 | break;
|
33217 | }
|
33218 | }
|
33219 |
|
33220 | qualityLevels.selectedIndex_ = selectedIndex;
|
33221 | qualityLevels.trigger({
|
33222 | selectedIndex,
|
33223 | type: 'change'
|
33224 | });
|
33225 | };
|
33226 | |
33227 |
|
33228 |
|
33229 |
|
33230 |
|
33231 |
|
33232 |
|
33233 |
|
33234 |
|
33235 | const handleVhsLoadedMetadata = function (qualityLevels, vhs) {
|
33236 | vhs.representations().forEach(rep => {
|
33237 | qualityLevels.addQualityLevel(rep);
|
33238 | });
|
33239 | handleVhsMediaChange(qualityLevels, vhs.playlists);
|
33240 | };
|
33241 |
|
33242 |
|
33243 |
|
33244 | Vhs.canPlaySource = function () {
|
33245 | return videojs__default["default"].log.warn('VHS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
|
33246 | };
|
33247 |
|
33248 | const emeKeySystems = (keySystemOptions, mainPlaylist, audioPlaylist) => {
|
33249 | if (!keySystemOptions) {
|
33250 | return keySystemOptions;
|
33251 | }
|
33252 |
|
33253 | let codecs = {};
|
33254 |
|
33255 | if (mainPlaylist && mainPlaylist.attributes && mainPlaylist.attributes.CODECS) {
|
33256 | codecs = unwrapCodecList(parseCodecs(mainPlaylist.attributes.CODECS));
|
33257 | }
|
33258 |
|
33259 | if (audioPlaylist && audioPlaylist.attributes && audioPlaylist.attributes.CODECS) {
|
33260 | codecs.audio = audioPlaylist.attributes.CODECS;
|
33261 | }
|
33262 |
|
33263 | const videoContentType = getMimeForCodec(codecs.video);
|
33264 | const audioContentType = getMimeForCodec(codecs.audio);
|
33265 |
|
33266 | const keySystemContentTypes = {};
|
33267 |
|
33268 | for (const keySystem in keySystemOptions) {
|
33269 | keySystemContentTypes[keySystem] = {};
|
33270 |
|
33271 | if (audioContentType) {
|
33272 | keySystemContentTypes[keySystem].audioContentType = audioContentType;
|
33273 | }
|
33274 |
|
33275 | if (videoContentType) {
|
33276 | keySystemContentTypes[keySystem].videoContentType = videoContentType;
|
33277 | }
|
33278 |
|
33279 |
|
33280 |
|
33281 |
|
33282 |
|
33283 |
|
33284 |
|
33285 | if (mainPlaylist.contentProtection && mainPlaylist.contentProtection[keySystem] && mainPlaylist.contentProtection[keySystem].pssh) {
|
33286 | keySystemContentTypes[keySystem].pssh = mainPlaylist.contentProtection[keySystem].pssh;
|
33287 | }
|
33288 |
|
33289 |
|
33290 |
|
33291 | if (typeof keySystemOptions[keySystem] === 'string') {
|
33292 | keySystemContentTypes[keySystem].url = keySystemOptions[keySystem];
|
33293 | }
|
33294 | }
|
33295 |
|
33296 | return merge$1(keySystemOptions, keySystemContentTypes);
|
33297 | };
|
33298 | |
33299 |
|
33300 |
|
33301 |
|
33302 |
|
33303 |
|
33304 |
|
33305 |
|
33306 |
|
33307 |
|
33308 | |
33309 |
|
33310 |
|
33311 |
|
33312 |
|
33313 |
|
33314 |
|
33315 |
|
33316 |
|
33317 |
|
33318 |
|
33319 |
|
33320 |
|
33321 |
|
33322 |
|
33323 | const getAllPsshKeySystemsOptions = (playlists, keySystems) => {
|
33324 | return playlists.reduce((keySystemsArr, playlist) => {
|
33325 | if (!playlist.contentProtection) {
|
33326 | return keySystemsArr;
|
33327 | }
|
33328 |
|
33329 | const keySystemsOptions = keySystems.reduce((keySystemsObj, keySystem) => {
|
33330 | const keySystemOptions = playlist.contentProtection[keySystem];
|
33331 |
|
33332 | if (keySystemOptions && keySystemOptions.pssh) {
|
33333 | keySystemsObj[keySystem] = {
|
33334 | pssh: keySystemOptions.pssh
|
33335 | };
|
33336 | }
|
33337 |
|
33338 | return keySystemsObj;
|
33339 | }, {});
|
33340 |
|
33341 | if (Object.keys(keySystemsOptions).length) {
|
33342 | keySystemsArr.push(keySystemsOptions);
|
33343 | }
|
33344 |
|
33345 | return keySystemsArr;
|
33346 | }, []);
|
33347 | };
|
33348 | |
33349 |
|
33350 |
|
33351 |
|
33352 |
|
33353 |
|
33354 |
|
33355 |
|
33356 |
|
33357 |
|
33358 |
|
33359 |
|
33360 |
|
33361 |
|
33362 |
|
33363 |
|
33364 |
|
33365 |
|
33366 |
|
33367 |
|
33368 |
|
33369 |
|
33370 |
|
33371 |
|
33372 |
|
33373 |
|
33374 | const waitForKeySessionCreation = ({
|
33375 | player,
|
33376 | sourceKeySystems,
|
33377 | audioMedia,
|
33378 | mainPlaylists
|
33379 | }) => {
|
33380 | if (!player.eme.initializeMediaKeys) {
|
33381 | return Promise.resolve();
|
33382 | }
|
33383 |
|
33384 |
|
33385 |
|
33386 |
|
33387 |
|
33388 |
|
33389 |
|
33390 |
|
33391 |
|
33392 | const playlists = audioMedia ? mainPlaylists.concat([audioMedia]) : mainPlaylists;
|
33393 | const keySystemsOptionsArr = getAllPsshKeySystemsOptions(playlists, Object.keys(sourceKeySystems));
|
33394 | const initializationFinishedPromises = [];
|
33395 | const keySessionCreatedPromises = [];
|
33396 |
|
33397 |
|
33398 |
|
33399 |
|
33400 |
|
33401 | keySystemsOptionsArr.forEach(keySystemsOptions => {
|
33402 | keySessionCreatedPromises.push(new Promise((resolve, reject) => {
|
33403 | player.tech_.one('keysessioncreated', resolve);
|
33404 | }));
|
33405 | initializationFinishedPromises.push(new Promise((resolve, reject) => {
|
33406 | player.eme.initializeMediaKeys({
|
33407 | keySystems: keySystemsOptions
|
33408 | }, err => {
|
33409 | if (err) {
|
33410 | reject(err);
|
33411 | return;
|
33412 | }
|
33413 |
|
33414 | resolve();
|
33415 | });
|
33416 | }));
|
33417 | });
|
33418 |
|
33419 |
|
33420 |
|
33421 |
|
33422 |
|
33423 |
|
33424 | return Promise.race([
|
33425 |
|
33426 |
|
33427 | Promise.all(initializationFinishedPromises),
|
33428 | Promise.race(keySessionCreatedPromises)]);
|
33429 | };
|
33430 | |
33431 |
|
33432 |
|
33433 |
|
33434 |
|
33435 |
|
33436 |
|
33437 |
|
33438 |
|
33439 |
|
33440 |
|
33441 |
|
33442 |
|
33443 |
|
33444 |
|
33445 |
|
33446 |
|
33447 |
|
33448 | const setupEmeOptions = ({
|
33449 | player,
|
33450 | sourceKeySystems,
|
33451 | media,
|
33452 | audioMedia
|
33453 | }) => {
|
33454 | const sourceOptions = emeKeySystems(sourceKeySystems, media, audioMedia);
|
33455 |
|
33456 | if (!sourceOptions) {
|
33457 | return false;
|
33458 | }
|
33459 |
|
33460 | player.currentSource().keySystems = sourceOptions;
|
33461 |
|
33462 |
|
33463 | if (sourceOptions && !player.eme) {
|
33464 | videojs__default["default"].log.warn('DRM encrypted source cannot be decrypted without a DRM plugin');
|
33465 | return false;
|
33466 | }
|
33467 |
|
33468 | return true;
|
33469 | };
|
33470 |
|
33471 | const getVhsLocalStorage = () => {
|
33472 | if (!window.localStorage) {
|
33473 | return null;
|
33474 | }
|
33475 |
|
33476 | const storedObject = window.localStorage.getItem(LOCAL_STORAGE_KEY);
|
33477 |
|
33478 | if (!storedObject) {
|
33479 | return null;
|
33480 | }
|
33481 |
|
33482 | try {
|
33483 | return JSON.parse(storedObject);
|
33484 | } catch (e) {
|
33485 |
|
33486 | return null;
|
33487 | }
|
33488 | };
|
33489 |
|
33490 | const updateVhsLocalStorage = options => {
|
33491 | if (!window.localStorage) {
|
33492 | return false;
|
33493 | }
|
33494 |
|
33495 | let objectToStore = getVhsLocalStorage();
|
33496 | objectToStore = objectToStore ? merge$1(objectToStore, options) : options;
|
33497 |
|
33498 | try {
|
33499 | window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(objectToStore));
|
33500 | } catch (e) {
|
33501 |
|
33502 |
|
33503 |
|
33504 |
|
33505 | return false;
|
33506 | }
|
33507 |
|
33508 | return objectToStore;
|
33509 | };
|
33510 | |
33511 |
|
33512 |
|
33513 |
|
33514 |
|
33515 |
|
33516 |
|
33517 |
|
33518 |
|
33519 |
|
33520 |
|
33521 |
|
33522 |
|
33523 |
|
33524 | const expandDataUri = dataUri => {
|
33525 | if (dataUri.toLowerCase().indexOf('data:application/vnd.videojs.vhs+json,') === 0) {
|
33526 | return JSON.parse(dataUri.substring(dataUri.indexOf(',') + 1));
|
33527 | }
|
33528 |
|
33529 |
|
33530 | return dataUri;
|
33531 | };
|
33532 | |
33533 |
|
33534 |
|
33535 |
|
33536 |
|
33537 |
|
33538 |
|
33539 |
|
33540 | const addOnRequestHook = (xhr, callback) => {
|
33541 | if (!xhr._requestCallbackSet) {
|
33542 | xhr._requestCallbackSet = new Set();
|
33543 | }
|
33544 |
|
33545 | xhr._requestCallbackSet.add(callback);
|
33546 | };
|
33547 | |
33548 |
|
33549 |
|
33550 |
|
33551 |
|
33552 |
|
33553 |
|
33554 |
|
33555 | const addOnResponseHook = (xhr, callback) => {
|
33556 | if (!xhr._responseCallbackSet) {
|
33557 | xhr._responseCallbackSet = new Set();
|
33558 | }
|
33559 |
|
33560 | xhr._responseCallbackSet.add(callback);
|
33561 | };
|
33562 | |
33563 |
|
33564 |
|
33565 |
|
33566 |
|
33567 |
|
33568 |
|
33569 |
|
33570 | const removeOnRequestHook = (xhr, callback) => {
|
33571 | if (!xhr._requestCallbackSet) {
|
33572 | return;
|
33573 | }
|
33574 |
|
33575 | xhr._requestCallbackSet.delete(callback);
|
33576 |
|
33577 | if (!xhr._requestCallbackSet.size) {
|
33578 | delete xhr._requestCallbackSet;
|
33579 | }
|
33580 | };
|
33581 | |
33582 |
|
33583 |
|
33584 |
|
33585 |
|
33586 |
|
33587 |
|
33588 |
|
33589 | const removeOnResponseHook = (xhr, callback) => {
|
33590 | if (!xhr._responseCallbackSet) {
|
33591 | return;
|
33592 | }
|
33593 |
|
33594 | xhr._responseCallbackSet.delete(callback);
|
33595 |
|
33596 | if (!xhr._responseCallbackSet.size) {
|
33597 | delete xhr._responseCallbackSet;
|
33598 | }
|
33599 | };
|
33600 | |
33601 |
|
33602 |
|
33603 |
|
33604 |
|
33605 | Vhs.supportsNativeHls = function () {
|
33606 | if (!document || !document.createElement) {
|
33607 | return false;
|
33608 | }
|
33609 |
|
33610 | const video = document.createElement('video');
|
33611 |
|
33612 | if (!videojs__default["default"].getTech('Html5').isSupported()) {
|
33613 | return false;
|
33614 | }
|
33615 |
|
33616 |
|
33617 | const canPlay = [
|
33618 | 'application/vnd.apple.mpegurl',
|
33619 | 'audio/mpegurl',
|
33620 | 'audio/x-mpegurl',
|
33621 | 'application/x-mpegurl',
|
33622 | 'video/x-mpegurl', 'video/mpegurl', 'application/mpegurl'];
|
33623 | return canPlay.some(function (canItPlay) {
|
33624 | return /maybe|probably/i.test(video.canPlayType(canItPlay));
|
33625 | });
|
33626 | }();
|
33627 |
|
33628 | Vhs.supportsNativeDash = function () {
|
33629 | if (!document || !document.createElement || !videojs__default["default"].getTech('Html5').isSupported()) {
|
33630 | return false;
|
33631 | }
|
33632 |
|
33633 | return /maybe|probably/i.test(document.createElement('video').canPlayType('application/dash+xml'));
|
33634 | }();
|
33635 |
|
33636 | Vhs.supportsTypeNatively = type => {
|
33637 | if (type === 'hls') {
|
33638 | return Vhs.supportsNativeHls;
|
33639 | }
|
33640 |
|
33641 | if (type === 'dash') {
|
33642 | return Vhs.supportsNativeDash;
|
33643 | }
|
33644 |
|
33645 | return false;
|
33646 | };
|
33647 | |
33648 |
|
33649 |
|
33650 |
|
33651 |
|
33652 |
|
33653 | Vhs.isSupported = function () {
|
33654 | return videojs__default["default"].log.warn('VHS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
|
33655 | };
|
33656 | |
33657 |
|
33658 |
|
33659 |
|
33660 |
|
33661 |
|
33662 |
|
33663 | Vhs.xhr.onRequest = function (callback) {
|
33664 | addOnRequestHook(Vhs.xhr, callback);
|
33665 | };
|
33666 | |
33667 |
|
33668 |
|
33669 |
|
33670 |
|
33671 |
|
33672 |
|
33673 | Vhs.xhr.onResponse = function (callback) {
|
33674 | addOnResponseHook(Vhs.xhr, callback);
|
33675 | };
|
33676 | |
33677 |
|
33678 |
|
33679 |
|
33680 |
|
33681 |
|
33682 |
|
33683 | Vhs.xhr.offRequest = function (callback) {
|
33684 | removeOnRequestHook(Vhs.xhr, callback);
|
33685 | };
|
33686 | |
33687 |
|
33688 |
|
33689 |
|
33690 |
|
33691 |
|
33692 |
|
33693 | Vhs.xhr.offResponse = function (callback) {
|
33694 | removeOnResponseHook(Vhs.xhr, callback);
|
33695 | };
|
33696 |
|
33697 | const Component = videojs__default["default"].getComponent('Component');
|
33698 | |
33699 |
|
33700 |
|
33701 |
|
33702 |
|
33703 |
|
33704 |
|
33705 |
|
33706 |
|
33707 |
|
33708 |
|
33709 | class VhsHandler extends Component {
|
33710 | constructor(source, tech, options) {
|
33711 | super(tech, options.vhs);
|
33712 |
|
33713 |
|
33714 | if (typeof options.initialBandwidth === 'number') {
|
33715 | this.options_.bandwidth = options.initialBandwidth;
|
33716 | }
|
33717 |
|
33718 | this.logger_ = logger('VhsHandler');
|
33719 |
|
33720 |
|
33721 | if (tech.options_ && tech.options_.playerId) {
|
33722 | const _player = videojs__default["default"].getPlayer(tech.options_.playerId);
|
33723 |
|
33724 | this.player_ = _player;
|
33725 | }
|
33726 |
|
33727 | this.tech_ = tech;
|
33728 | this.source_ = source;
|
33729 | this.stats = {};
|
33730 | this.ignoreNextSeekingEvent_ = false;
|
33731 | this.setOptions_();
|
33732 |
|
33733 | if (this.options_.overrideNative && tech.overrideNativeAudioTracks && tech.overrideNativeVideoTracks) {
|
33734 | tech.overrideNativeAudioTracks(true);
|
33735 | tech.overrideNativeVideoTracks(true);
|
33736 | } else if (this.options_.overrideNative && (tech.featuresNativeVideoTracks || tech.featuresNativeAudioTracks)) {
|
33737 |
|
33738 |
|
33739 | throw new Error('Overriding native VHS requires emulated tracks. ' + 'See https://git.io/vMpjB');
|
33740 | }
|
33741 |
|
33742 |
|
33743 |
|
33744 | this.on(document, ['fullscreenchange', 'webkitfullscreenchange', 'mozfullscreenchange', 'MSFullscreenChange'], event => {
|
33745 | const fullscreenElement = document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement;
|
33746 |
|
33747 | if (fullscreenElement && fullscreenElement.contains(this.tech_.el())) {
|
33748 | this.playlistController_.fastQualityChange_();
|
33749 | } else {
|
33750 |
|
33751 |
|
33752 |
|
33753 | this.playlistController_.checkABR_();
|
33754 | }
|
33755 | });
|
33756 | this.on(this.tech_, 'seeking', function () {
|
33757 | if (this.ignoreNextSeekingEvent_) {
|
33758 | this.ignoreNextSeekingEvent_ = false;
|
33759 | return;
|
33760 | }
|
33761 |
|
33762 | this.setCurrentTime(this.tech_.currentTime());
|
33763 | });
|
33764 | this.on(this.tech_, 'error', function () {
|
33765 |
|
33766 |
|
33767 | if (this.tech_.error() && this.playlistController_) {
|
33768 | this.playlistController_.pauseLoading();
|
33769 | }
|
33770 | });
|
33771 | this.on(this.tech_, 'play', this.play);
|
33772 | }
|
33773 | |
33774 |
|
33775 |
|
33776 |
|
33777 |
|
33778 |
|
33779 |
|
33780 |
|
33781 | setOptions_(options = {}) {
|
33782 | this.options_ = merge$1(this.options_, options);
|
33783 |
|
33784 | this.options_.withCredentials = this.options_.withCredentials || false;
|
33785 | this.options_.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions === false ? false : true;
|
33786 | this.options_.useDevicePixelRatio = this.options_.useDevicePixelRatio || false;
|
33787 | this.options_.useBandwidthFromLocalStorage = typeof this.source_.useBandwidthFromLocalStorage !== 'undefined' ? this.source_.useBandwidthFromLocalStorage : this.options_.useBandwidthFromLocalStorage || false;
|
33788 | this.options_.useForcedSubtitles = this.options_.useForcedSubtitles || false;
|
33789 | this.options_.useNetworkInformationApi = this.options_.useNetworkInformationApi || false;
|
33790 | this.options_.useDtsForTimestampOffset = this.options_.useDtsForTimestampOffset || false;
|
33791 | this.options_.customTagParsers = this.options_.customTagParsers || [];
|
33792 | this.options_.customTagMappers = this.options_.customTagMappers || [];
|
33793 | this.options_.cacheEncryptionKeys = this.options_.cacheEncryptionKeys || false;
|
33794 | this.options_.llhls = this.options_.llhls === false ? false : true;
|
33795 | this.options_.bufferBasedABR = this.options_.bufferBasedABR || false;
|
33796 |
|
33797 | if (typeof this.options_.playlistExclusionDuration !== 'number') {
|
33798 | this.options_.playlistExclusionDuration = 60;
|
33799 | }
|
33800 |
|
33801 | if (typeof this.options_.bandwidth !== 'number') {
|
33802 | if (this.options_.useBandwidthFromLocalStorage) {
|
33803 | const storedObject = getVhsLocalStorage();
|
33804 |
|
33805 | if (storedObject && storedObject.bandwidth) {
|
33806 | this.options_.bandwidth = storedObject.bandwidth;
|
33807 | this.tech_.trigger({
|
33808 | type: 'usage',
|
33809 | name: 'vhs-bandwidth-from-local-storage'
|
33810 | });
|
33811 | }
|
33812 |
|
33813 | if (storedObject && storedObject.throughput) {
|
33814 | this.options_.throughput = storedObject.throughput;
|
33815 | this.tech_.trigger({
|
33816 | type: 'usage',
|
33817 | name: 'vhs-throughput-from-local-storage'
|
33818 | });
|
33819 | }
|
33820 | }
|
33821 | }
|
33822 |
|
33823 |
|
33824 |
|
33825 | if (typeof this.options_.bandwidth !== 'number') {
|
33826 | this.options_.bandwidth = Config.INITIAL_BANDWIDTH;
|
33827 | }
|
33828 |
|
33829 |
|
33830 |
|
33831 | this.options_.enableLowInitialPlaylist = this.options_.enableLowInitialPlaylist && this.options_.bandwidth === Config.INITIAL_BANDWIDTH;
|
33832 |
|
33833 | ['withCredentials', 'useDevicePixelRatio', 'limitRenditionByPlayerDimensions', 'bandwidth', 'customTagParsers', 'customTagMappers', 'cacheEncryptionKeys', 'playlistSelector', 'initialPlaylistSelector', 'bufferBasedABR', 'liveRangeSafeTimeDelta', 'llhls', 'useForcedSubtitles', 'useNetworkInformationApi', 'useDtsForTimestampOffset', 'exactManifestTimings', 'leastPixelDiffSelector'].forEach(option => {
|
33834 | if (typeof this.source_[option] !== 'undefined') {
|
33835 | this.options_[option] = this.source_[option];
|
33836 | }
|
33837 | });
|
33838 | this.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions;
|
33839 | this.useDevicePixelRatio = this.options_.useDevicePixelRatio;
|
33840 | }
|
33841 |
|
33842 |
|
33843 | setOptions(options = {}) {
|
33844 | this.setOptions_(options);
|
33845 | }
|
33846 | |
33847 |
|
33848 |
|
33849 |
|
33850 |
|
33851 |
|
33852 |
|
33853 | src(src, type) {
|
33854 |
|
33855 | if (!src) {
|
33856 | return;
|
33857 | }
|
33858 |
|
33859 | this.setOptions_();
|
33860 |
|
33861 | this.options_.src = expandDataUri(this.source_.src);
|
33862 | this.options_.tech = this.tech_;
|
33863 | this.options_.externVhs = Vhs;
|
33864 | this.options_.sourceType = simpleTypeFromSourceType(type);
|
33865 |
|
33866 | this.options_.seekTo = time => {
|
33867 | this.tech_.setCurrentTime(time);
|
33868 | };
|
33869 |
|
33870 | this.playlistController_ = new PlaylistController(this.options_);
|
33871 | const playbackWatcherOptions = merge$1({
|
33872 | liveRangeSafeTimeDelta: SAFE_TIME_DELTA
|
33873 | }, this.options_, {
|
33874 | seekable: () => this.seekable(),
|
33875 | media: () => this.playlistController_.media(),
|
33876 | playlistController: this.playlistController_
|
33877 | });
|
33878 | this.playbackWatcher_ = new PlaybackWatcher(playbackWatcherOptions);
|
33879 | this.playlistController_.on('error', () => {
|
33880 | const player = videojs__default["default"].players[this.tech_.options_.playerId];
|
33881 | let error = this.playlistController_.error;
|
33882 |
|
33883 | if (typeof error === 'object' && !error.code) {
|
33884 | error.code = 3;
|
33885 | } else if (typeof error === 'string') {
|
33886 | error = {
|
33887 | message: error,
|
33888 | code: 3
|
33889 | };
|
33890 | }
|
33891 |
|
33892 | player.error(error);
|
33893 | });
|
33894 | const defaultSelector = this.options_.bufferBasedABR ? Vhs.movingAverageBandwidthSelector(0.55) : Vhs.STANDARD_PLAYLIST_SELECTOR;
|
33895 |
|
33896 |
|
33897 | this.playlistController_.selectPlaylist = this.selectPlaylist ? this.selectPlaylist.bind(this) : defaultSelector.bind(this);
|
33898 | this.playlistController_.selectInitialPlaylist = Vhs.INITIAL_PLAYLIST_SELECTOR.bind(this);
|
33899 |
|
33900 | this.playlists = this.playlistController_.mainPlaylistLoader_;
|
33901 | this.mediaSource = this.playlistController_.mediaSource;
|
33902 |
|
33903 |
|
33904 |
|
33905 | Object.defineProperties(this, {
|
33906 | selectPlaylist: {
|
33907 | get() {
|
33908 | return this.playlistController_.selectPlaylist;
|
33909 | },
|
33910 |
|
33911 | set(selectPlaylist) {
|
33912 | this.playlistController_.selectPlaylist = selectPlaylist.bind(this);
|
33913 | }
|
33914 |
|
33915 | },
|
33916 | throughput: {
|
33917 | get() {
|
33918 | return this.playlistController_.mainSegmentLoader_.throughput.rate;
|
33919 | },
|
33920 |
|
33921 | set(throughput) {
|
33922 | this.playlistController_.mainSegmentLoader_.throughput.rate = throughput;
|
33923 |
|
33924 |
|
33925 | this.playlistController_.mainSegmentLoader_.throughput.count = 1;
|
33926 | }
|
33927 |
|
33928 | },
|
33929 | bandwidth: {
|
33930 | get() {
|
33931 | let playerBandwidthEst = this.playlistController_.mainSegmentLoader_.bandwidth;
|
33932 | const networkInformation = window.navigator.connection || window.navigator.mozConnection || window.navigator.webkitConnection;
|
33933 | const tenMbpsAsBitsPerSecond = 10e6;
|
33934 |
|
33935 | if (this.options_.useNetworkInformationApi && networkInformation) {
|
33936 |
|
33937 |
|
33938 | const networkInfoBandwidthEstBitsPerSec = networkInformation.downlink * 1000 * 1000;
|
33939 |
|
33940 |
|
33941 |
|
33942 | if (networkInfoBandwidthEstBitsPerSec >= tenMbpsAsBitsPerSecond && playerBandwidthEst >= tenMbpsAsBitsPerSecond) {
|
33943 | playerBandwidthEst = Math.max(playerBandwidthEst, networkInfoBandwidthEstBitsPerSec);
|
33944 | } else {
|
33945 | playerBandwidthEst = networkInfoBandwidthEstBitsPerSec;
|
33946 | }
|
33947 | }
|
33948 |
|
33949 | return playerBandwidthEst;
|
33950 | },
|
33951 |
|
33952 | set(bandwidth) {
|
33953 | this.playlistController_.mainSegmentLoader_.bandwidth = bandwidth;
|
33954 |
|
33955 |
|
33956 |
|
33957 | this.playlistController_.mainSegmentLoader_.throughput = {
|
33958 | rate: 0,
|
33959 | count: 0
|
33960 | };
|
33961 | }
|
33962 |
|
33963 | },
|
33964 |
|
33965 | |
33966 |
|
33967 |
|
33968 |
|
33969 |
|
33970 |
|
33971 |
|
33972 |
|
33973 |
|
33974 | systemBandwidth: {
|
33975 | get() {
|
33976 | const invBandwidth = 1 / (this.bandwidth || 1);
|
33977 | let invThroughput;
|
33978 |
|
33979 | if (this.throughput > 0) {
|
33980 | invThroughput = 1 / this.throughput;
|
33981 | } else {
|
33982 | invThroughput = 0;
|
33983 | }
|
33984 |
|
33985 | const systemBitrate = Math.floor(1 / (invBandwidth + invThroughput));
|
33986 | return systemBitrate;
|
33987 | },
|
33988 |
|
33989 | set() {
|
33990 | videojs__default["default"].log.error('The "systemBandwidth" property is read-only');
|
33991 | }
|
33992 |
|
33993 | }
|
33994 | });
|
33995 |
|
33996 | if (this.options_.bandwidth) {
|
33997 | this.bandwidth = this.options_.bandwidth;
|
33998 | }
|
33999 |
|
34000 | if (this.options_.throughput) {
|
34001 | this.throughput = this.options_.throughput;
|
34002 | }
|
34003 |
|
34004 | Object.defineProperties(this.stats, {
|
34005 | bandwidth: {
|
34006 | get: () => this.bandwidth || 0,
|
34007 | enumerable: true
|
34008 | },
|
34009 | mediaRequests: {
|
34010 | get: () => this.playlistController_.mediaRequests_() || 0,
|
34011 | enumerable: true
|
34012 | },
|
34013 | mediaRequestsAborted: {
|
34014 | get: () => this.playlistController_.mediaRequestsAborted_() || 0,
|
34015 | enumerable: true
|
34016 | },
|
34017 | mediaRequestsTimedout: {
|
34018 | get: () => this.playlistController_.mediaRequestsTimedout_() || 0,
|
34019 | enumerable: true
|
34020 | },
|
34021 | mediaRequestsErrored: {
|
34022 | get: () => this.playlistController_.mediaRequestsErrored_() || 0,
|
34023 | enumerable: true
|
34024 | },
|
34025 | mediaTransferDuration: {
|
34026 | get: () => this.playlistController_.mediaTransferDuration_() || 0,
|
34027 | enumerable: true
|
34028 | },
|
34029 | mediaBytesTransferred: {
|
34030 | get: () => this.playlistController_.mediaBytesTransferred_() || 0,
|
34031 | enumerable: true
|
34032 | },
|
34033 | mediaSecondsLoaded: {
|
34034 | get: () => this.playlistController_.mediaSecondsLoaded_() || 0,
|
34035 | enumerable: true
|
34036 | },
|
34037 | mediaAppends: {
|
34038 | get: () => this.playlistController_.mediaAppends_() || 0,
|
34039 | enumerable: true
|
34040 | },
|
34041 | mainAppendsToLoadedData: {
|
34042 | get: () => this.playlistController_.mainAppendsToLoadedData_() || 0,
|
34043 | enumerable: true
|
34044 | },
|
34045 | audioAppendsToLoadedData: {
|
34046 | get: () => this.playlistController_.audioAppendsToLoadedData_() || 0,
|
34047 | enumerable: true
|
34048 | },
|
34049 | appendsToLoadedData: {
|
34050 | get: () => this.playlistController_.appendsToLoadedData_() || 0,
|
34051 | enumerable: true
|
34052 | },
|
34053 | timeToLoadedData: {
|
34054 | get: () => this.playlistController_.timeToLoadedData_() || 0,
|
34055 | enumerable: true
|
34056 | },
|
34057 | buffered: {
|
34058 | get: () => timeRangesToArray(this.tech_.buffered()),
|
34059 | enumerable: true
|
34060 | },
|
34061 | currentTime: {
|
34062 | get: () => this.tech_.currentTime(),
|
34063 | enumerable: true
|
34064 | },
|
34065 | currentSource: {
|
34066 | get: () => this.tech_.currentSource_,
|
34067 | enumerable: true
|
34068 | },
|
34069 | currentTech: {
|
34070 | get: () => this.tech_.name_,
|
34071 | enumerable: true
|
34072 | },
|
34073 | duration: {
|
34074 | get: () => this.tech_.duration(),
|
34075 | enumerable: true
|
34076 | },
|
34077 | main: {
|
34078 | get: () => this.playlists.main,
|
34079 | enumerable: true
|
34080 | },
|
34081 | playerDimensions: {
|
34082 | get: () => this.tech_.currentDimensions(),
|
34083 | enumerable: true
|
34084 | },
|
34085 | seekable: {
|
34086 | get: () => timeRangesToArray(this.tech_.seekable()),
|
34087 | enumerable: true
|
34088 | },
|
34089 | timestamp: {
|
34090 | get: () => Date.now(),
|
34091 | enumerable: true
|
34092 | },
|
34093 | videoPlaybackQuality: {
|
34094 | get: () => this.tech_.getVideoPlaybackQuality(),
|
34095 | enumerable: true
|
34096 | }
|
34097 | });
|
34098 | this.tech_.one('canplay', this.playlistController_.setupFirstPlay.bind(this.playlistController_));
|
34099 | this.tech_.on('bandwidthupdate', () => {
|
34100 | if (this.options_.useBandwidthFromLocalStorage) {
|
34101 | updateVhsLocalStorage({
|
34102 | bandwidth: this.bandwidth,
|
34103 | throughput: Math.round(this.throughput)
|
34104 | });
|
34105 | }
|
34106 | });
|
34107 | this.playlistController_.on('selectedinitialmedia', () => {
|
34108 |
|
34109 | renditionSelectionMixin(this);
|
34110 | });
|
34111 | this.playlistController_.sourceUpdater_.on('createdsourcebuffers', () => {
|
34112 | this.setupEme_();
|
34113 | });
|
34114 |
|
34115 |
|
34116 | this.on(this.playlistController_, 'progress', function () {
|
34117 | this.tech_.trigger('progress');
|
34118 | });
|
34119 |
|
34120 |
|
34121 | this.on(this.playlistController_, 'firstplay', function () {
|
34122 | this.ignoreNextSeekingEvent_ = true;
|
34123 | });
|
34124 | this.setupQualityLevels_();
|
34125 |
|
34126 |
|
34127 | if (!this.tech_.el()) {
|
34128 | return;
|
34129 | }
|
34130 |
|
34131 | this.mediaSourceUrl_ = window.URL.createObjectURL(this.playlistController_.mediaSource);
|
34132 | this.tech_.src(this.mediaSourceUrl_);
|
34133 | }
|
34134 |
|
34135 | createKeySessions_() {
|
34136 | const audioPlaylistLoader = this.playlistController_.mediaTypes_.AUDIO.activePlaylistLoader;
|
34137 | this.logger_('waiting for EME key session creation');
|
34138 | waitForKeySessionCreation({
|
34139 | player: this.player_,
|
34140 | sourceKeySystems: this.source_.keySystems,
|
34141 | audioMedia: audioPlaylistLoader && audioPlaylistLoader.media(),
|
34142 | mainPlaylists: this.playlists.main.playlists
|
34143 | }).then(() => {
|
34144 | this.logger_('created EME key session');
|
34145 | this.playlistController_.sourceUpdater_.initializedEme();
|
34146 | }).catch(err => {
|
34147 | this.logger_('error while creating EME key session', err);
|
34148 | this.player_.error({
|
34149 | message: 'Failed to initialize media keys for EME',
|
34150 | code: 3
|
34151 | });
|
34152 | });
|
34153 | }
|
34154 |
|
34155 | handleWaitingForKey_() {
|
34156 |
|
34157 |
|
34158 |
|
34159 |
|
34160 |
|
34161 |
|
34162 |
|
34163 |
|
34164 | this.logger_('waitingforkey fired, attempting to create any new key sessions');
|
34165 | this.createKeySessions_();
|
34166 | }
|
34167 | |
34168 |
|
34169 |
|
34170 |
|
34171 |
|
34172 |
|
34173 |
|
34174 |
|
34175 |
|
34176 |
|
34177 | setupEme_() {
|
34178 | const audioPlaylistLoader = this.playlistController_.mediaTypes_.AUDIO.activePlaylistLoader;
|
34179 | const didSetupEmeOptions = setupEmeOptions({
|
34180 | player: this.player_,
|
34181 | sourceKeySystems: this.source_.keySystems,
|
34182 | media: this.playlists.media(),
|
34183 | audioMedia: audioPlaylistLoader && audioPlaylistLoader.media()
|
34184 | });
|
34185 | this.player_.tech_.on('keystatuschange', e => {
|
34186 | this.playlistController_.updatePlaylistByKeyStatus(e.keyId, e.status);
|
34187 | });
|
34188 | this.handleWaitingForKey_ = this.handleWaitingForKey_.bind(this);
|
34189 | this.player_.tech_.on('waitingforkey', this.handleWaitingForKey_);
|
34190 |
|
34191 | if (!didSetupEmeOptions) {
|
34192 |
|
34193 | this.playlistController_.sourceUpdater_.initializedEme();
|
34194 | return;
|
34195 | }
|
34196 |
|
34197 | this.createKeySessions_();
|
34198 | }
|
34199 | |
34200 |
|
34201 |
|
34202 |
|
34203 |
|
34204 |
|
34205 |
|
34206 |
|
34207 | setupQualityLevels_() {
|
34208 | const player = videojs__default["default"].players[this.tech_.options_.playerId];
|
34209 |
|
34210 |
|
34211 | if (!player || !player.qualityLevels || this.qualityLevels_) {
|
34212 | return;
|
34213 | }
|
34214 |
|
34215 | this.qualityLevels_ = player.qualityLevels();
|
34216 | this.playlistController_.on('selectedinitialmedia', () => {
|
34217 | handleVhsLoadedMetadata(this.qualityLevels_, this);
|
34218 | });
|
34219 | this.playlists.on('mediachange', () => {
|
34220 | handleVhsMediaChange(this.qualityLevels_, this.playlists);
|
34221 | });
|
34222 | }
|
34223 | |
34224 |
|
34225 |
|
34226 |
|
34227 |
|
34228 | static version() {
|
34229 | return {
|
34230 | '@videojs/http-streaming': version$4,
|
34231 | 'mux.js': version$3,
|
34232 | 'mpd-parser': version$2,
|
34233 | 'm3u8-parser': version$1,
|
34234 | 'aes-decrypter': version
|
34235 | };
|
34236 | }
|
34237 | |
34238 |
|
34239 |
|
34240 |
|
34241 |
|
34242 | version() {
|
34243 | return this.constructor.version();
|
34244 | }
|
34245 |
|
34246 | canChangeType() {
|
34247 | return SourceUpdater.canChangeType();
|
34248 | }
|
34249 | |
34250 |
|
34251 |
|
34252 |
|
34253 |
|
34254 | play() {
|
34255 | this.playlistController_.play();
|
34256 | }
|
34257 | |
34258 |
|
34259 |
|
34260 |
|
34261 |
|
34262 | setCurrentTime(currentTime) {
|
34263 | this.playlistController_.setCurrentTime(currentTime);
|
34264 | }
|
34265 | |
34266 |
|
34267 |
|
34268 |
|
34269 |
|
34270 | duration() {
|
34271 | return this.playlistController_.duration();
|
34272 | }
|
34273 | |
34274 |
|
34275 |
|
34276 |
|
34277 |
|
34278 | seekable() {
|
34279 | return this.playlistController_.seekable();
|
34280 | }
|
34281 | |
34282 |
|
34283 |
|
34284 |
|
34285 |
|
34286 | dispose() {
|
34287 | if (this.playbackWatcher_) {
|
34288 | this.playbackWatcher_.dispose();
|
34289 | }
|
34290 |
|
34291 | if (this.playlistController_) {
|
34292 | this.playlistController_.dispose();
|
34293 | }
|
34294 |
|
34295 | if (this.qualityLevels_) {
|
34296 | this.qualityLevels_.dispose();
|
34297 | }
|
34298 |
|
34299 | if (this.tech_ && this.tech_.vhs) {
|
34300 | delete this.tech_.vhs;
|
34301 | }
|
34302 |
|
34303 | if (this.mediaSourceUrl_ && window.URL.revokeObjectURL) {
|
34304 | window.URL.revokeObjectURL(this.mediaSourceUrl_);
|
34305 | this.mediaSourceUrl_ = null;
|
34306 | }
|
34307 |
|
34308 | if (this.tech_) {
|
34309 | this.tech_.off('waitingforkey', this.handleWaitingForKey_);
|
34310 | }
|
34311 |
|
34312 | super.dispose();
|
34313 | }
|
34314 |
|
34315 | convertToProgramTime(time, callback) {
|
34316 | return getProgramTime({
|
34317 | playlist: this.playlistController_.media(),
|
34318 | time,
|
34319 | callback
|
34320 | });
|
34321 | }
|
34322 |
|
34323 |
|
34324 | seekToProgramTime(programTime, callback, pauseAfterSeek = true, retryCount = 2) {
|
34325 | return seekToProgramTime({
|
34326 | programTime,
|
34327 | playlist: this.playlistController_.media(),
|
34328 | retryCount,
|
34329 | pauseAfterSeek,
|
34330 | seekTo: this.options_.seekTo,
|
34331 | tech: this.options_.tech,
|
34332 | callback
|
34333 | });
|
34334 | }
|
34335 | |
34336 |
|
34337 |
|
34338 |
|
34339 |
|
34340 |
|
34341 | setupXhrHooks_() {
|
34342 | |
34343 |
|
34344 |
|
34345 |
|
34346 |
|
34347 | this.xhr.onRequest = callback => {
|
34348 | addOnRequestHook(this.xhr, callback);
|
34349 | };
|
34350 | |
34351 |
|
34352 |
|
34353 |
|
34354 |
|
34355 |
|
34356 |
|
34357 | this.xhr.onResponse = callback => {
|
34358 | addOnResponseHook(this.xhr, callback);
|
34359 | };
|
34360 | |
34361 |
|
34362 |
|
34363 |
|
34364 |
|
34365 |
|
34366 |
|
34367 | this.xhr.offRequest = callback => {
|
34368 | removeOnRequestHook(this.xhr, callback);
|
34369 | };
|
34370 | |
34371 |
|
34372 |
|
34373 |
|
34374 |
|
34375 |
|
34376 |
|
34377 | this.xhr.offResponse = callback => {
|
34378 | removeOnResponseHook(this.xhr, callback);
|
34379 | };
|
34380 |
|
34381 |
|
34382 |
|
34383 | this.player_.trigger('xhr-hooks-ready');
|
34384 | }
|
34385 |
|
34386 | }
|
34387 | |
34388 |
|
34389 |
|
34390 |
|
34391 |
|
34392 |
|
34393 |
|
34394 |
|
34395 |
|
34396 | const VhsSourceHandler = {
|
34397 | name: 'videojs-http-streaming',
|
34398 | VERSION: version$4,
|
34399 |
|
34400 | canHandleSource(srcObj, options = {}) {
|
34401 | const localOptions = merge$1(videojs__default["default"].options, options);
|
34402 | return VhsSourceHandler.canPlayType(srcObj.type, localOptions);
|
34403 | },
|
34404 |
|
34405 | handleSource(source, tech, options = {}) {
|
34406 | const localOptions = merge$1(videojs__default["default"].options, options);
|
34407 | tech.vhs = new VhsHandler(source, tech, localOptions);
|
34408 | tech.vhs.xhr = xhrFactory();
|
34409 | tech.vhs.setupXhrHooks_();
|
34410 | tech.vhs.src(source.src, source.type);
|
34411 | return tech.vhs;
|
34412 | },
|
34413 |
|
34414 | canPlayType(type, options) {
|
34415 | const simpleType = simpleTypeFromSourceType(type);
|
34416 |
|
34417 | if (!simpleType) {
|
34418 | return '';
|
34419 | }
|
34420 |
|
34421 | const overrideNative = VhsSourceHandler.getOverrideNative(options);
|
34422 | const supportsTypeNatively = Vhs.supportsTypeNatively(simpleType);
|
34423 | const canUseMsePlayback = !supportsTypeNatively || overrideNative;
|
34424 | return canUseMsePlayback ? 'maybe' : '';
|
34425 | },
|
34426 |
|
34427 | getOverrideNative(options = {}) {
|
34428 | const {
|
34429 | vhs = {}
|
34430 | } = options;
|
34431 | const defaultOverrideNative = !(videojs__default["default"].browser.IS_ANY_SAFARI || videojs__default["default"].browser.IS_IOS);
|
34432 | const {
|
34433 | overrideNative = defaultOverrideNative
|
34434 | } = vhs;
|
34435 | return overrideNative;
|
34436 | }
|
34437 |
|
34438 | };
|
34439 | |
34440 |
|
34441 |
|
34442 |
|
34443 |
|
34444 |
|
34445 |
|
34446 | const supportsNativeMediaSources = () => {
|
34447 | return browserSupportsCodec('avc1.4d400d,mp4a.40.2');
|
34448 | };
|
34449 |
|
34450 |
|
34451 | if (supportsNativeMediaSources()) {
|
34452 | videojs__default["default"].getTech('Html5').registerSourceHandler(VhsSourceHandler, 0);
|
34453 | }
|
34454 |
|
34455 | videojs__default["default"].VhsHandler = VhsHandler;
|
34456 | videojs__default["default"].VhsSourceHandler = VhsSourceHandler;
|
34457 | videojs__default["default"].Vhs = Vhs;
|
34458 |
|
34459 | if (!videojs__default["default"].use) {
|
34460 | videojs__default["default"].registerComponent('Vhs', Vhs);
|
34461 | }
|
34462 |
|
34463 | videojs__default["default"].options.vhs = videojs__default["default"].options.vhs || {};
|
34464 |
|
34465 | if (!videojs__default["default"].getPlugin || !videojs__default["default"].getPlugin('reloadSourceOnError')) {
|
34466 | videojs__default["default"].registerPlugin('reloadSourceOnError', reloadSourceOnError);
|
34467 | }
|
34468 |
|
34469 | exports.LOCAL_STORAGE_KEY = LOCAL_STORAGE_KEY;
|
34470 | exports.Vhs = Vhs;
|
34471 | exports.VhsHandler = VhsHandler;
|
34472 | exports.VhsSourceHandler = VhsSourceHandler;
|
34473 | exports.emeKeySystems = emeKeySystems;
|
34474 | exports.expandDataUri = expandDataUri;
|
34475 | exports.getAllPsshKeySystemsOptions = getAllPsshKeySystemsOptions;
|
34476 | exports.setupEmeOptions = setupEmeOptions;
|
34477 | exports.simpleTypeFromSourceType = simpleTypeFromSourceType;
|
34478 | exports.waitForKeySessionCreation = waitForKeySessionCreation;
|
34479 |
|
34480 | Object.defineProperty(exports, '__esModule', { value: true });
|
34481 |
|
34482 | }));
|