1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | 'use strict';
|
8 |
|
9 | Object.defineProperty(exports, '__esModule', { value: true });
|
10 |
|
11 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
12 |
|
13 | var URLToolkit = _interopDefault(require('url-toolkit'));
|
14 | var window$1 = _interopDefault(require('global/window'));
|
15 | var videojs = _interopDefault(require('video.js'));
|
16 | var m3u8Parser = require('m3u8-parser');
|
17 | var document = _interopDefault(require('global/document'));
|
18 | var mpdParser = require('mpd-parser');
|
19 | var mp4Inspector = _interopDefault(require('mux.js/lib/tools/mp4-inspector'));
|
20 | var mp4probe = _interopDefault(require('mux.js/lib/mp4/probe'));
|
21 | var CaptionParser = _interopDefault(require('mux.js/lib/mp4/caption-parser'));
|
22 | var tsInspector = _interopDefault(require('mux.js/lib/tools/ts-inspector.js'));
|
23 | var aesDecrypter = require('aes-decrypter');
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var resolveUrl = function resolveUrl(baseURL, relativeURL) {
|
30 |
|
31 | if (/^[a-z]+:/i.test(relativeURL)) {
|
32 | return relativeURL;
|
33 | }
|
34 |
|
35 |
|
36 | if (!/\/\//i.test(baseURL)) {
|
37 | baseURL = URLToolkit.buildAbsoluteURL(window$1.location.href, baseURL);
|
38 | }
|
39 |
|
40 | return URLToolkit.buildAbsoluteURL(baseURL, relativeURL);
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | var resolveManifestRedirect = function resolveManifestRedirect(handleManifestRedirect, url, req) {
|
55 |
|
56 |
|
57 |
|
58 | if (handleManifestRedirect && req.responseURL && url !== req.responseURL) {
|
59 | return req.responseURL;
|
60 | }
|
61 |
|
62 | return url;
|
63 | };
|
64 |
|
65 | var classCallCheck = function (instance, Constructor) {
|
66 | if (!(instance instanceof Constructor)) {
|
67 | throw new TypeError("Cannot call a class as a function");
|
68 | }
|
69 | };
|
70 |
|
71 | var createClass = function () {
|
72 | function defineProperties(target, props) {
|
73 | for (var i = 0; i < props.length; i++) {
|
74 | var descriptor = props[i];
|
75 | descriptor.enumerable = descriptor.enumerable || false;
|
76 | descriptor.configurable = true;
|
77 | if ("value" in descriptor) descriptor.writable = true;
|
78 | Object.defineProperty(target, descriptor.key, descriptor);
|
79 | }
|
80 | }
|
81 |
|
82 | return function (Constructor, protoProps, staticProps) {
|
83 | if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
84 | if (staticProps) defineProperties(Constructor, staticProps);
|
85 | return Constructor;
|
86 | };
|
87 | }();
|
88 |
|
89 | var get = function get(object, property, receiver) {
|
90 | if (object === null) object = Function.prototype;
|
91 | var desc = Object.getOwnPropertyDescriptor(object, property);
|
92 |
|
93 | if (desc === undefined) {
|
94 | var parent = Object.getPrototypeOf(object);
|
95 |
|
96 | if (parent === null) {
|
97 | return undefined;
|
98 | } else {
|
99 | return get(parent, property, receiver);
|
100 | }
|
101 | } else if ("value" in desc) {
|
102 | return desc.value;
|
103 | } else {
|
104 | var getter = desc.get;
|
105 |
|
106 | if (getter === undefined) {
|
107 | return undefined;
|
108 | }
|
109 |
|
110 | return getter.call(receiver);
|
111 | }
|
112 | };
|
113 |
|
114 | var inherits = function (subClass, superClass) {
|
115 | if (typeof superClass !== "function" && superClass !== null) {
|
116 | throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
117 | }
|
118 |
|
119 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
120 | constructor: {
|
121 | value: subClass,
|
122 | enumerable: false,
|
123 | writable: true,
|
124 | configurable: true
|
125 | }
|
126 | });
|
127 | if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
128 | };
|
129 |
|
130 | var possibleConstructorReturn = function (self, call) {
|
131 | if (!self) {
|
132 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
133 | }
|
134 |
|
135 | return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
136 | };
|
137 |
|
138 | var slicedToArray = function () {
|
139 | function sliceIterator(arr, i) {
|
140 | var _arr = [];
|
141 | var _n = true;
|
142 | var _d = false;
|
143 | var _e = undefined;
|
144 |
|
145 | try {
|
146 | for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
147 | _arr.push(_s.value);
|
148 |
|
149 | if (i && _arr.length === i) break;
|
150 | }
|
151 | } catch (err) {
|
152 | _d = true;
|
153 | _e = err;
|
154 | } finally {
|
155 | try {
|
156 | if (!_n && _i["return"]) _i["return"]();
|
157 | } finally {
|
158 | if (_d) throw _e;
|
159 | }
|
160 | }
|
161 |
|
162 | return _arr;
|
163 | }
|
164 |
|
165 | return function (arr, i) {
|
166 | if (Array.isArray(arr)) {
|
167 | return arr;
|
168 | } else if (Symbol.iterator in Object(arr)) {
|
169 | return sliceIterator(arr, i);
|
170 | } else {
|
171 | throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
172 | }
|
173 | };
|
174 | }();
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | var mergeOptions = videojs.mergeOptions,
|
185 | EventTarget = videojs.EventTarget,
|
186 | log = videojs.log;
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | var forEachMediaGroup = function forEachMediaGroup(master, callback) {
|
199 | ['AUDIO', 'SUBTITLES'].forEach(function (mediaType) {
|
200 | for (var groupKey in master.mediaGroups[mediaType]) {
|
201 | for (var labelKey in master.mediaGroups[mediaType][groupKey]) {
|
202 | var mediaProperties = master.mediaGroups[mediaType][groupKey][labelKey];
|
203 |
|
204 | callback(mediaProperties, mediaType, groupKey, labelKey);
|
205 | }
|
206 | }
|
207 | });
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | var updateSegments = function updateSegments(original, update, offset) {
|
226 | var result = update.slice();
|
227 |
|
228 | offset = offset || 0;
|
229 | var length = Math.min(original.length, update.length + offset);
|
230 |
|
231 | for (var i = offset; i < length; i++) {
|
232 | result[i - offset] = mergeOptions(original[i], result[i - offset]);
|
233 | }
|
234 | return result;
|
235 | };
|
236 |
|
237 | var resolveSegmentUris = function resolveSegmentUris(segment, baseUri) {
|
238 | if (!segment.resolvedUri) {
|
239 | segment.resolvedUri = resolveUrl(baseUri, segment.uri);
|
240 | }
|
241 | if (segment.key && !segment.key.resolvedUri) {
|
242 | segment.key.resolvedUri = resolveUrl(baseUri, segment.key.uri);
|
243 | }
|
244 | if (segment.map && !segment.map.resolvedUri) {
|
245 | segment.map.resolvedUri = resolveUrl(baseUri, segment.map.uri);
|
246 | }
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | var updateMaster = function updateMaster(master, media) {
|
262 | var result = mergeOptions(master, {});
|
263 | var playlist = result.playlists[media.id];
|
264 |
|
265 | if (!playlist) {
|
266 | return null;
|
267 | }
|
268 |
|
269 |
|
270 |
|
271 | if (playlist.segments && media.segments && playlist.segments.length === media.segments.length && playlist.endList === media.endList && playlist.mediaSequence === media.mediaSequence) {
|
272 | return null;
|
273 | }
|
274 |
|
275 | var mergedPlaylist = mergeOptions(playlist, media);
|
276 |
|
277 |
|
278 | if (playlist.segments) {
|
279 | mergedPlaylist.segments = updateSegments(playlist.segments, media.segments, media.mediaSequence - playlist.mediaSequence);
|
280 | }
|
281 |
|
282 |
|
283 | mergedPlaylist.segments.forEach(function (segment) {
|
284 | resolveSegmentUris(segment, mergedPlaylist.resolvedUri);
|
285 | });
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | for (var i = 0; i < result.playlists.length; i++) {
|
291 | if (result.playlists[i].id === media.id) {
|
292 | result.playlists[i] = mergedPlaylist;
|
293 | }
|
294 | }
|
295 | result.playlists[media.id] = mergedPlaylist;
|
296 |
|
297 | result.playlists[media.uri] = mergedPlaylist;
|
298 |
|
299 | return result;
|
300 | };
|
301 |
|
302 | var createPlaylistID = function createPlaylistID(index, uri) {
|
303 | return index + '-' + uri;
|
304 | };
|
305 |
|
306 | var setupMediaPlaylists = function setupMediaPlaylists(master) {
|
307 |
|
308 | var i = master.playlists.length;
|
309 |
|
310 | while (i--) {
|
311 | var playlist = master.playlists[i];
|
312 |
|
313 | playlist.resolvedUri = resolveUrl(master.uri, playlist.uri);
|
314 | playlist.id = createPlaylistID(i, playlist.uri);
|
315 |
|
316 | master.playlists[playlist.id] = playlist;
|
317 |
|
318 | master.playlists[playlist.uri] = playlist;
|
319 |
|
320 | if (!playlist.attributes) {
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | playlist.attributes = {};
|
327 |
|
328 | log.warn('Invalid playlist STREAM-INF detected. Missing BANDWIDTH attribute.');
|
329 | }
|
330 | }
|
331 | };
|
332 |
|
333 | var resolveMediaGroupUris = function resolveMediaGroupUris(master) {
|
334 | forEachMediaGroup(master, function (properties) {
|
335 | if (properties.uri) {
|
336 | properties.resolvedUri = resolveUrl(master.uri, properties.uri);
|
337 | }
|
338 | });
|
339 | };
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 | var refreshDelay = function refreshDelay(media, update) {
|
352 | var lastSegment = media.segments[media.segments.length - 1];
|
353 | var delay = void 0;
|
354 |
|
355 | if (update && lastSegment && lastSegment.duration) {
|
356 | delay = lastSegment.duration * 1000;
|
357 | } else {
|
358 |
|
359 |
|
360 | delay = (media.targetDuration || 10) * 500;
|
361 | }
|
362 | return delay;
|
363 | };
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | var PlaylistLoader = function (_EventTarget) {
|
376 | inherits(PlaylistLoader, _EventTarget);
|
377 |
|
378 | function PlaylistLoader(srcUrl, hls) {
|
379 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
380 | classCallCheck(this, PlaylistLoader);
|
381 |
|
382 | var _this = possibleConstructorReturn(this, (PlaylistLoader.__proto__ || Object.getPrototypeOf(PlaylistLoader)).call(this));
|
383 |
|
384 | var _options$withCredenti = options.withCredentials,
|
385 | withCredentials = _options$withCredenti === undefined ? false : _options$withCredenti,
|
386 | _options$handleManife = options.handleManifestRedirects,
|
387 | handleManifestRedirects = _options$handleManife === undefined ? false : _options$handleManife;
|
388 |
|
389 |
|
390 | _this.srcUrl = srcUrl;
|
391 | _this.hls_ = hls;
|
392 | _this.withCredentials = withCredentials;
|
393 | _this.handleManifestRedirects = handleManifestRedirects;
|
394 |
|
395 | var hlsOptions = hls.options_;
|
396 |
|
397 | _this.customTagParsers = hlsOptions && hlsOptions.customTagParsers || [];
|
398 | _this.customTagMappers = hlsOptions && hlsOptions.customTagMappers || [];
|
399 |
|
400 | if (!_this.srcUrl) {
|
401 | throw new Error('A non-empty playlist URL is required');
|
402 | }
|
403 |
|
404 |
|
405 | _this.state = 'HAVE_NOTHING';
|
406 |
|
407 |
|
408 | _this.on('mediaupdatetimeout', function () {
|
409 | if (_this.state !== 'HAVE_METADATA') {
|
410 |
|
411 | return;
|
412 | }
|
413 |
|
414 | _this.state = 'HAVE_CURRENT_METADATA';
|
415 |
|
416 | _this.request = _this.hls_.xhr({
|
417 | uri: resolveUrl(_this.master.uri, _this.media().uri),
|
418 | withCredentials: _this.withCredentials
|
419 | }, function (error, req) {
|
420 |
|
421 | if (!_this.request) {
|
422 | return;
|
423 | }
|
424 |
|
425 | if (error) {
|
426 | return _this.playlistRequestError(_this.request, _this.media(), 'HAVE_METADATA');
|
427 | }
|
428 |
|
429 | _this.haveMetadata(_this.request, _this.media().uri, _this.media().id);
|
430 | });
|
431 | });
|
432 | return _this;
|
433 | }
|
434 |
|
435 | createClass(PlaylistLoader, [{
|
436 | key: 'playlistRequestError',
|
437 | value: function playlistRequestError(xhr, playlist, startingState) {
|
438 | var uri = playlist.uri,
|
439 | id = playlist.id;
|
440 |
|
441 |
|
442 |
|
443 | this.request = null;
|
444 |
|
445 | if (startingState) {
|
446 | this.state = startingState;
|
447 | }
|
448 |
|
449 | this.error = {
|
450 | playlist: this.master.playlists[id],
|
451 | status: xhr.status,
|
452 | message: 'HLS playlist request error at URL: ' + uri + '.',
|
453 | responseText: xhr.responseText,
|
454 | code: xhr.status >= 500 ? 4 : 2
|
455 | };
|
456 |
|
457 | this.trigger('error');
|
458 | }
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | }, {
|
464 | key: 'haveMetadata',
|
465 | value: function haveMetadata(xhr, url, id) {
|
466 | var _this2 = this;
|
467 |
|
468 |
|
469 | this.request = null;
|
470 | this.state = 'HAVE_METADATA';
|
471 |
|
472 | var parser = new m3u8Parser.Parser();
|
473 |
|
474 |
|
475 | this.customTagParsers.forEach(function (customParser) {
|
476 | return parser.addParser(customParser);
|
477 | });
|
478 |
|
479 |
|
480 | this.customTagMappers.forEach(function (mapper) {
|
481 | return parser.addTagMapper(mapper);
|
482 | });
|
483 |
|
484 | parser.push(xhr.responseText);
|
485 | parser.end();
|
486 | parser.manifest.uri = url;
|
487 | parser.manifest.id = id;
|
488 |
|
489 |
|
490 | parser.manifest.attributes = parser.manifest.attributes || {};
|
491 |
|
492 |
|
493 | var update = updateMaster(this.master, parser.manifest);
|
494 |
|
495 | this.targetDuration = parser.manifest.targetDuration;
|
496 |
|
497 | if (update) {
|
498 | this.master = update;
|
499 | this.media_ = this.master.playlists[id];
|
500 | } else {
|
501 | this.trigger('playlistunchanged');
|
502 | }
|
503 |
|
504 |
|
505 | if (!this.media().endList) {
|
506 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
507 | this.mediaUpdateTimeout = window$1.setTimeout(function () {
|
508 | _this2.trigger('mediaupdatetimeout');
|
509 | }, refreshDelay(this.media(), !!update));
|
510 | }
|
511 |
|
512 | this.trigger('loadedplaylist');
|
513 | }
|
514 |
|
515 | |
516 |
|
517 |
|
518 |
|
519 | }, {
|
520 | key: 'dispose',
|
521 | value: function dispose() {
|
522 | this.trigger('dispose');
|
523 | this.stopRequest();
|
524 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
525 | window$1.clearTimeout(this.finalRenditionTimeout);
|
526 | this.off();
|
527 | }
|
528 | }, {
|
529 | key: 'stopRequest',
|
530 | value: function stopRequest() {
|
531 | if (this.request) {
|
532 | var oldRequest = this.request;
|
533 |
|
534 | this.request = null;
|
535 | oldRequest.onreadystatechange = null;
|
536 | oldRequest.abort();
|
537 | }
|
538 | }
|
539 |
|
540 | |
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 | }, {
|
556 | key: 'media',
|
557 | value: function media(playlist, isFinalRendition) {
|
558 | var _this3 = this;
|
559 |
|
560 |
|
561 | if (!playlist) {
|
562 | return this.media_;
|
563 | }
|
564 |
|
565 |
|
566 | if (this.state === 'HAVE_NOTHING') {
|
567 | throw new Error('Cannot switch media playlist from ' + this.state);
|
568 | }
|
569 |
|
570 |
|
571 |
|
572 | if (typeof playlist === 'string') {
|
573 | if (!this.master.playlists[playlist]) {
|
574 | throw new Error('Unknown playlist URI: ' + playlist);
|
575 | }
|
576 | playlist = this.master.playlists[playlist];
|
577 | }
|
578 |
|
579 | window$1.clearTimeout(this.finalRenditionTimeout);
|
580 |
|
581 | if (isFinalRendition) {
|
582 | var delay = playlist.targetDuration / 2 * 1000 || 5 * 1000;
|
583 |
|
584 | this.finalRenditionTimeout = window$1.setTimeout(this.media.bind(this, playlist, false), delay);
|
585 | return;
|
586 | }
|
587 |
|
588 | var startingState = this.state;
|
589 | var mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
590 |
|
591 |
|
592 | if (this.master.playlists[playlist.id].endList) {
|
593 |
|
594 | if (this.request) {
|
595 | this.request.onreadystatechange = null;
|
596 | this.request.abort();
|
597 | this.request = null;
|
598 | }
|
599 | this.state = 'HAVE_METADATA';
|
600 | this.media_ = playlist;
|
601 |
|
602 |
|
603 | if (mediaChange) {
|
604 | this.trigger('mediachanging');
|
605 | this.trigger('mediachange');
|
606 | }
|
607 | return;
|
608 | }
|
609 |
|
610 |
|
611 | if (!mediaChange) {
|
612 | return;
|
613 | }
|
614 |
|
615 | this.state = 'SWITCHING_MEDIA';
|
616 |
|
617 |
|
618 | if (this.request) {
|
619 | if (playlist.resolvedUri === this.request.url) {
|
620 |
|
621 |
|
622 | return;
|
623 | }
|
624 | this.request.onreadystatechange = null;
|
625 | this.request.abort();
|
626 | this.request = null;
|
627 | }
|
628 |
|
629 |
|
630 | if (this.media_) {
|
631 | this.trigger('mediachanging');
|
632 | }
|
633 |
|
634 | this.request = this.hls_.xhr({
|
635 | uri: playlist.resolvedUri,
|
636 | withCredentials: this.withCredentials
|
637 | }, function (error, req) {
|
638 |
|
639 | if (!_this3.request) {
|
640 | return;
|
641 | }
|
642 |
|
643 | playlist.resolvedUri = resolveManifestRedirect(_this3.handleManifestRedirects, playlist.resolvedUri, req);
|
644 |
|
645 | if (error) {
|
646 | return _this3.playlistRequestError(_this3.request, playlist, startingState);
|
647 | }
|
648 |
|
649 | _this3.haveMetadata(req, playlist.uri, playlist.id);
|
650 |
|
651 |
|
652 | if (startingState === 'HAVE_MASTER') {
|
653 | _this3.trigger('loadedmetadata');
|
654 | } else {
|
655 | _this3.trigger('mediachange');
|
656 | }
|
657 | });
|
658 | }
|
659 |
|
660 | |
661 |
|
662 |
|
663 |
|
664 | }, {
|
665 | key: 'pause',
|
666 | value: function pause() {
|
667 | this.stopRequest();
|
668 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
669 | if (this.state === 'HAVE_NOTHING') {
|
670 |
|
671 |
|
672 | this.started = false;
|
673 | }
|
674 |
|
675 | if (this.state === 'SWITCHING_MEDIA') {
|
676 |
|
677 |
|
678 |
|
679 | if (this.media_) {
|
680 | this.state = 'HAVE_METADATA';
|
681 | } else {
|
682 | this.state = 'HAVE_MASTER';
|
683 | }
|
684 | } else if (this.state === 'HAVE_CURRENT_METADATA') {
|
685 | this.state = 'HAVE_METADATA';
|
686 | }
|
687 | }
|
688 |
|
689 | |
690 |
|
691 |
|
692 |
|
693 | }, {
|
694 | key: 'load',
|
695 | value: function load(isFinalRendition) {
|
696 | var _this4 = this;
|
697 |
|
698 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
699 |
|
700 | var media = this.media();
|
701 |
|
702 | if (isFinalRendition) {
|
703 | var delay = media ? media.targetDuration / 2 * 1000 : 5 * 1000;
|
704 |
|
705 | this.mediaUpdateTimeout = window$1.setTimeout(function () {
|
706 | return _this4.load();
|
707 | }, delay);
|
708 | return;
|
709 | }
|
710 |
|
711 | if (!this.started) {
|
712 | this.start();
|
713 | return;
|
714 | }
|
715 |
|
716 | if (media && !media.endList) {
|
717 | this.trigger('mediaupdatetimeout');
|
718 | } else {
|
719 | this.trigger('loadedplaylist');
|
720 | }
|
721 | }
|
722 |
|
723 | |
724 |
|
725 |
|
726 |
|
727 | }, {
|
728 | key: 'start',
|
729 | value: function start() {
|
730 | var _this5 = this;
|
731 |
|
732 | this.started = true;
|
733 |
|
734 |
|
735 | this.request = this.hls_.xhr({
|
736 | uri: this.srcUrl,
|
737 | withCredentials: this.withCredentials
|
738 | }, function (error, req) {
|
739 |
|
740 | if (!_this5.request) {
|
741 | return;
|
742 | }
|
743 |
|
744 |
|
745 | _this5.request = null;
|
746 |
|
747 | if (error) {
|
748 | _this5.error = {
|
749 | status: req.status,
|
750 | message: 'HLS playlist request error at URL: ' + _this5.srcUrl + '.',
|
751 | responseText: req.responseText,
|
752 |
|
753 | code: 2
|
754 | };
|
755 | if (_this5.state === 'HAVE_NOTHING') {
|
756 | _this5.started = false;
|
757 | }
|
758 | return _this5.trigger('error');
|
759 | }
|
760 |
|
761 | var parser = new m3u8Parser.Parser();
|
762 |
|
763 |
|
764 | _this5.customTagParsers.forEach(function (customParser) {
|
765 | return parser.addParser(customParser);
|
766 | });
|
767 |
|
768 |
|
769 | _this5.customTagMappers.forEach(function (mapper) {
|
770 | return parser.addTagMapper(mapper);
|
771 | });
|
772 |
|
773 | parser.push(req.responseText);
|
774 | parser.end();
|
775 |
|
776 | _this5.state = 'HAVE_MASTER';
|
777 |
|
778 | _this5.srcUrl = resolveManifestRedirect(_this5.handleManifestRedirects, _this5.srcUrl, req);
|
779 |
|
780 | parser.manifest.uri = _this5.srcUrl;
|
781 |
|
782 |
|
783 | if (parser.manifest.playlists) {
|
784 | _this5.master = parser.manifest;
|
785 |
|
786 | setupMediaPlaylists(_this5.master);
|
787 | resolveMediaGroupUris(_this5.master);
|
788 |
|
789 | _this5.trigger('loadedplaylist');
|
790 | if (!_this5.request) {
|
791 |
|
792 |
|
793 | _this5.media(parser.manifest.playlists[0]);
|
794 | }
|
795 | return;
|
796 | }
|
797 |
|
798 | var id = createPlaylistID(0, _this5.srcUrl);
|
799 |
|
800 |
|
801 |
|
802 | _this5.master = {
|
803 | mediaGroups: {
|
804 | 'AUDIO': {},
|
805 | 'VIDEO': {},
|
806 | 'CLOSED-CAPTIONS': {},
|
807 | 'SUBTITLES': {}
|
808 | },
|
809 | uri: window$1.location.href,
|
810 | playlists: [{
|
811 | uri: _this5.srcUrl,
|
812 | id: id,
|
813 | resolvedUri: _this5.srcUrl,
|
814 |
|
815 |
|
816 | attributes: {}
|
817 | }]
|
818 | };
|
819 | _this5.master.playlists[id] = _this5.master.playlists[0];
|
820 |
|
821 | _this5.master.playlists[_this5.srcUrl] = _this5.master.playlists[0];
|
822 |
|
823 | _this5.haveMetadata(req, _this5.srcUrl, id);
|
824 | return _this5.trigger('loadedmetadata');
|
825 | });
|
826 | }
|
827 | }]);
|
828 | return PlaylistLoader;
|
829 | }(EventTarget);
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 | var createTimeRange = videojs.createTimeRange;
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 | var backwardDuration = function backwardDuration(playlist, endSequence) {
|
848 | var result = 0;
|
849 | var i = endSequence - playlist.mediaSequence;
|
850 |
|
851 |
|
852 | var segment = playlist.segments[i];
|
853 |
|
854 |
|
855 |
|
856 | if (segment) {
|
857 | if (typeof segment.start !== 'undefined') {
|
858 | return { result: segment.start, precise: true };
|
859 | }
|
860 | if (typeof segment.end !== 'undefined') {
|
861 | return {
|
862 | result: segment.end - segment.duration,
|
863 | precise: true
|
864 | };
|
865 | }
|
866 | }
|
867 | while (i--) {
|
868 | segment = playlist.segments[i];
|
869 | if (typeof segment.end !== 'undefined') {
|
870 | return { result: result + segment.end, precise: true };
|
871 | }
|
872 |
|
873 | result += segment.duration;
|
874 |
|
875 | if (typeof segment.start !== 'undefined') {
|
876 | return { result: result + segment.start, precise: true };
|
877 | }
|
878 | }
|
879 | return { result: result, precise: false };
|
880 | };
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 | var forwardDuration = function forwardDuration(playlist, endSequence) {
|
890 | var result = 0;
|
891 | var segment = void 0;
|
892 | var i = endSequence - playlist.mediaSequence;
|
893 |
|
894 |
|
895 |
|
896 | for (; i < playlist.segments.length; i++) {
|
897 | segment = playlist.segments[i];
|
898 | if (typeof segment.start !== 'undefined') {
|
899 | return {
|
900 | result: segment.start - result,
|
901 | precise: true
|
902 | };
|
903 | }
|
904 |
|
905 | result += segment.duration;
|
906 |
|
907 | if (typeof segment.end !== 'undefined') {
|
908 | return {
|
909 | result: segment.end - result,
|
910 | precise: true
|
911 | };
|
912 | }
|
913 | }
|
914 |
|
915 | return { result: -1, precise: false };
|
916 | };
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 | var intervalDuration = function intervalDuration(playlist, endSequence, expired) {
|
932 | var backward = void 0;
|
933 | var forward = void 0;
|
934 |
|
935 | if (typeof endSequence === 'undefined') {
|
936 | endSequence = playlist.mediaSequence + playlist.segments.length;
|
937 | }
|
938 |
|
939 | if (endSequence < playlist.mediaSequence) {
|
940 | return 0;
|
941 | }
|
942 |
|
943 |
|
944 | backward = backwardDuration(playlist, endSequence);
|
945 | if (backward.precise) {
|
946 |
|
947 |
|
948 |
|
949 | return backward.result;
|
950 | }
|
951 |
|
952 |
|
953 |
|
954 | forward = forwardDuration(playlist, endSequence);
|
955 | if (forward.precise) {
|
956 |
|
957 |
|
958 | return forward.result;
|
959 | }
|
960 |
|
961 |
|
962 | return backward.result + expired;
|
963 | };
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 | var duration = function duration(playlist, endSequence, expired) {
|
981 | if (!playlist) {
|
982 | return 0;
|
983 | }
|
984 |
|
985 | if (typeof expired !== 'number') {
|
986 | expired = 0;
|
987 | }
|
988 |
|
989 |
|
990 |
|
991 | if (typeof endSequence === 'undefined') {
|
992 |
|
993 | if (playlist.totalDuration) {
|
994 | return playlist.totalDuration;
|
995 | }
|
996 |
|
997 |
|
998 | if (!playlist.endList) {
|
999 | return window$1.Infinity;
|
1000 | }
|
1001 | }
|
1002 |
|
1003 |
|
1004 | return intervalDuration(playlist, endSequence, expired);
|
1005 | };
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 | var sumDurations = function sumDurations(playlist, startIndex, endIndex) {
|
1019 | var durations = 0;
|
1020 |
|
1021 | if (startIndex > endIndex) {
|
1022 | var _ref = [endIndex, startIndex];
|
1023 | startIndex = _ref[0];
|
1024 | endIndex = _ref[1];
|
1025 | }
|
1026 |
|
1027 | if (startIndex < 0) {
|
1028 | for (var i = startIndex; i < Math.min(0, endIndex); i++) {
|
1029 | durations += playlist.targetDuration;
|
1030 | }
|
1031 | startIndex = 0;
|
1032 | }
|
1033 |
|
1034 | for (var _i = startIndex; _i < endIndex; _i++) {
|
1035 | durations += playlist.segments[_i].duration;
|
1036 | }
|
1037 |
|
1038 | return durations;
|
1039 | };
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 | var safeLiveIndex = function safeLiveIndex(playlist, liveEdgePadding) {
|
1061 | if (!playlist.segments.length) {
|
1062 | return 0;
|
1063 | }
|
1064 |
|
1065 | var i = playlist.segments.length;
|
1066 | var lastSegmentDuration = playlist.segments[i - 1].duration || playlist.targetDuration;
|
1067 | var safeDistance = typeof liveEdgePadding === 'number' ? liveEdgePadding : lastSegmentDuration + playlist.targetDuration * 2;
|
1068 |
|
1069 | if (safeDistance === 0) {
|
1070 | return i;
|
1071 | }
|
1072 |
|
1073 | var distanceFromEnd = 0;
|
1074 |
|
1075 | while (i--) {
|
1076 | distanceFromEnd += playlist.segments[i].duration;
|
1077 |
|
1078 | if (distanceFromEnd >= safeDistance) {
|
1079 | break;
|
1080 | }
|
1081 | }
|
1082 |
|
1083 | return Math.max(0, i);
|
1084 | };
|
1085 |
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 |
|
1104 |
|
1105 | var playlistEnd = function playlistEnd(playlist, expired, useSafeLiveEnd, liveEdgePadding) {
|
1106 | if (!playlist || !playlist.segments) {
|
1107 | return null;
|
1108 | }
|
1109 | if (playlist.endList) {
|
1110 | return duration(playlist);
|
1111 | }
|
1112 |
|
1113 | if (expired === null) {
|
1114 | return null;
|
1115 | }
|
1116 |
|
1117 | expired = expired || 0;
|
1118 |
|
1119 | var endSequence = useSafeLiveEnd ? safeLiveIndex(playlist, liveEdgePadding) : playlist.segments.length;
|
1120 |
|
1121 | return intervalDuration(playlist, playlist.mediaSequence + endSequence, expired);
|
1122 | };
|
1123 |
|
1124 |
|
1125 |
|
1126 |
|
1127 |
|
1128 |
|
1129 |
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
1139 |
|
1140 |
|
1141 | var seekable = function seekable(playlist, expired, liveEdgePadding) {
|
1142 | var useSafeLiveEnd = true;
|
1143 | var seekableStart = expired || 0;
|
1144 | var seekableEnd = playlistEnd(playlist, expired, useSafeLiveEnd, liveEdgePadding);
|
1145 |
|
1146 | if (seekableEnd === null) {
|
1147 | return createTimeRange();
|
1148 | }
|
1149 | return createTimeRange(seekableStart, seekableEnd);
|
1150 | };
|
1151 |
|
1152 | var isWholeNumber = function isWholeNumber(num) {
|
1153 | return num - Math.floor(num) === 0;
|
1154 | };
|
1155 |
|
1156 | var roundSignificantDigit = function roundSignificantDigit(increment, num) {
|
1157 |
|
1158 | if (isWholeNumber(num)) {
|
1159 | return num + increment * 0.1;
|
1160 | }
|
1161 |
|
1162 | var numDecimalDigits = num.toString().split('.')[1].length;
|
1163 |
|
1164 | for (var i = 1; i <= numDecimalDigits; i++) {
|
1165 | var scale = Math.pow(10, i);
|
1166 | var temp = num * scale;
|
1167 |
|
1168 | if (isWholeNumber(temp) || i === numDecimalDigits) {
|
1169 | return (temp + increment) / scale;
|
1170 | }
|
1171 | }
|
1172 | };
|
1173 |
|
1174 | var ceilLeastSignificantDigit = roundSignificantDigit.bind(null, 1);
|
1175 | var floorLeastSignificantDigit = roundSignificantDigit.bind(null, -1);
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 | var getMediaInfoForTime = function getMediaInfoForTime(playlist, currentTime, startIndex, startTime) {
|
1189 | var i = void 0;
|
1190 | var segment = void 0;
|
1191 | var numSegments = playlist.segments.length;
|
1192 |
|
1193 | var time = currentTime - startTime;
|
1194 |
|
1195 | if (time < 0) {
|
1196 |
|
1197 |
|
1198 | if (startIndex > 0) {
|
1199 | for (i = startIndex - 1; i >= 0; i--) {
|
1200 | segment = playlist.segments[i];
|
1201 | time += floorLeastSignificantDigit(segment.duration);
|
1202 | if (time > 0) {
|
1203 | return {
|
1204 | mediaIndex: i,
|
1205 | startTime: startTime - sumDurations(playlist, startIndex, i)
|
1206 | };
|
1207 | }
|
1208 | }
|
1209 | }
|
1210 |
|
1211 |
|
1212 | return {
|
1213 | mediaIndex: 0,
|
1214 | startTime: currentTime
|
1215 | };
|
1216 | }
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 | if (startIndex < 0) {
|
1222 | for (i = startIndex; i < 0; i++) {
|
1223 | time -= playlist.targetDuration;
|
1224 | if (time < 0) {
|
1225 | return {
|
1226 | mediaIndex: 0,
|
1227 | startTime: currentTime
|
1228 | };
|
1229 | }
|
1230 | }
|
1231 | startIndex = 0;
|
1232 | }
|
1233 |
|
1234 |
|
1235 |
|
1236 | for (i = startIndex; i < numSegments; i++) {
|
1237 | segment = playlist.segments[i];
|
1238 | time -= ceilLeastSignificantDigit(segment.duration);
|
1239 | if (time < 0) {
|
1240 | return {
|
1241 | mediaIndex: i,
|
1242 | startTime: startTime + sumDurations(playlist, startIndex, i)
|
1243 | };
|
1244 | }
|
1245 | }
|
1246 |
|
1247 |
|
1248 | return {
|
1249 | mediaIndex: numSegments - 1,
|
1250 | startTime: currentTime
|
1251 | };
|
1252 | };
|
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 | var isBlacklisted = function isBlacklisted(playlist) {
|
1262 | return playlist.excludeUntil && playlist.excludeUntil > Date.now();
|
1263 | };
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 |
|
1271 |
|
1272 |
|
1273 | var isIncompatible = function isIncompatible(playlist) {
|
1274 | return playlist.excludeUntil && playlist.excludeUntil === Infinity;
|
1275 | };
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 |
|
1283 |
|
1284 | var isEnabled = function isEnabled(playlist) {
|
1285 | var blacklisted = isBlacklisted(playlist);
|
1286 |
|
1287 | return !playlist.disabled && !blacklisted;
|
1288 | };
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 |
|
1297 | var isDisabled = function isDisabled(playlist) {
|
1298 | return playlist.disabled;
|
1299 | };
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 | var isAes = function isAes(media) {
|
1307 | for (var i = 0; i < media.segments.length; i++) {
|
1308 | if (media.segments[i].key) {
|
1309 | return true;
|
1310 | }
|
1311 | }
|
1312 | return false;
|
1313 | };
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 | var isFmp4 = function isFmp4(media) {
|
1321 | for (var i = 0; i < media.segments.length; i++) {
|
1322 | if (media.segments[i].map) {
|
1323 | return true;
|
1324 | }
|
1325 | }
|
1326 | return false;
|
1327 | };
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 | var hasAttribute = function hasAttribute(attr, playlist) {
|
1341 | return playlist.attributes && playlist.attributes[attr];
|
1342 | };
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 |
|
1355 |
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 | var estimateSegmentRequestTime = function estimateSegmentRequestTime(segmentDuration, bandwidth, playlist) {
|
1361 | var bytesReceived = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
1362 |
|
1363 | if (!hasAttribute('BANDWIDTH', playlist)) {
|
1364 | return NaN;
|
1365 | }
|
1366 |
|
1367 | var size = segmentDuration * playlist.attributes.BANDWIDTH;
|
1368 |
|
1369 | return (size - bytesReceived * 8) / bandwidth;
|
1370 | };
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 |
|
1377 | var isLowestEnabledRendition = function isLowestEnabledRendition(master, media) {
|
1378 | if (master.playlists.length === 1) {
|
1379 | return true;
|
1380 | }
|
1381 |
|
1382 | var currentBandwidth = media.attributes.BANDWIDTH || Number.MAX_VALUE;
|
1383 |
|
1384 | return master.playlists.filter(function (playlist) {
|
1385 | if (!isEnabled(playlist)) {
|
1386 | return false;
|
1387 | }
|
1388 |
|
1389 | return (playlist.attributes.BANDWIDTH || 0) < currentBandwidth;
|
1390 | }).length === 0;
|
1391 | };
|
1392 |
|
1393 |
|
1394 | var Playlist = {
|
1395 | duration: duration,
|
1396 | seekable: seekable,
|
1397 | safeLiveIndex: safeLiveIndex,
|
1398 | getMediaInfoForTime: getMediaInfoForTime,
|
1399 | isEnabled: isEnabled,
|
1400 | isDisabled: isDisabled,
|
1401 | isBlacklisted: isBlacklisted,
|
1402 | isIncompatible: isIncompatible,
|
1403 | playlistEnd: playlistEnd,
|
1404 | isAes: isAes,
|
1405 | isFmp4: isFmp4,
|
1406 | hasAttribute: hasAttribute,
|
1407 | estimateSegmentRequestTime: estimateSegmentRequestTime,
|
1408 | isLowestEnabledRendition: isLowestEnabledRendition
|
1409 | };
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 | var videojsXHR = videojs.xhr,
|
1416 | mergeOptions$1 = videojs.mergeOptions;
|
1417 |
|
1418 |
|
1419 | var xhrFactory = function xhrFactory() {
|
1420 | var xhr = function XhrFunction(options, callback) {
|
1421 |
|
1422 | options = mergeOptions$1({
|
1423 | timeout: 45e3
|
1424 | }, options);
|
1425 |
|
1426 |
|
1427 |
|
1428 | var beforeRequest = XhrFunction.beforeRequest || videojs.Hls.xhr.beforeRequest;
|
1429 |
|
1430 | if (beforeRequest && typeof beforeRequest === 'function') {
|
1431 | var newOptions = beforeRequest(options);
|
1432 |
|
1433 | if (newOptions) {
|
1434 | options = newOptions;
|
1435 | }
|
1436 | }
|
1437 |
|
1438 | var request = videojsXHR(options, function (error, response) {
|
1439 | var reqResponse = request.response;
|
1440 |
|
1441 | if (!error && reqResponse) {
|
1442 | request.responseTime = Date.now();
|
1443 | request.roundTripTime = request.responseTime - request.requestTime;
|
1444 | request.bytesReceived = reqResponse.byteLength || reqResponse.length;
|
1445 | if (!request.bandwidth) {
|
1446 | request.bandwidth = Math.floor(request.bytesReceived / request.roundTripTime * 8 * 1000);
|
1447 | }
|
1448 | }
|
1449 |
|
1450 | if (response.headers) {
|
1451 | request.responseHeaders = response.headers;
|
1452 | }
|
1453 |
|
1454 |
|
1455 |
|
1456 |
|
1457 | if (error && error.code === 'ETIMEDOUT') {
|
1458 | request.timedout = true;
|
1459 | }
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 | if (!error && !request.aborted && response.statusCode !== 200 && response.statusCode !== 206 && response.statusCode !== 0) {
|
1465 | error = new Error('XHR Failed with a response of: ' + (request && (reqResponse || request.responseText)));
|
1466 | }
|
1467 |
|
1468 | callback(error, request);
|
1469 | });
|
1470 | var originalAbort = request.abort;
|
1471 |
|
1472 | request.abort = function () {
|
1473 | request.aborted = true;
|
1474 | return originalAbort.apply(request, arguments);
|
1475 | };
|
1476 | request.uri = options.uri;
|
1477 | request.requestTime = Date.now();
|
1478 | return request;
|
1479 | };
|
1480 |
|
1481 | return xhr;
|
1482 | };
|
1483 |
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
|
1491 | var byterangeStr = function byterangeStr(byterange) {
|
1492 | var byterangeStart = void 0;
|
1493 | var byterangeEnd = void 0;
|
1494 |
|
1495 |
|
1496 |
|
1497 | byterangeEnd = byterange.offset + byterange.length - 1;
|
1498 | byterangeStart = byterange.offset;
|
1499 | return 'bytes=' + byterangeStart + '-' + byterangeEnd;
|
1500 | };
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 |
|
1507 |
|
1508 | var segmentXhrHeaders = function segmentXhrHeaders(segment) {
|
1509 | var headers = {};
|
1510 |
|
1511 | if (segment.byterange) {
|
1512 | headers.Range = byterangeStr(segment.byterange);
|
1513 | }
|
1514 | return headers;
|
1515 | };
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 |
|
1521 |
|
1522 |
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 | var textRange = function textRange(range, i) {
|
1528 | return range.start(i) + '-' + range.end(i);
|
1529 | };
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 |
|
1537 | var formatHexString = function formatHexString(e, i) {
|
1538 | var value = e.toString(16);
|
1539 |
|
1540 | return '00'.substring(0, 2 - value.length) + value + (i % 2 ? ' ' : '');
|
1541 | };
|
1542 | var formatAsciiString = function formatAsciiString(e) {
|
1543 | if (e >= 0x20 && e < 0x7e) {
|
1544 | return String.fromCharCode(e);
|
1545 | }
|
1546 | return '.';
|
1547 | };
|
1548 |
|
1549 |
|
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 | var createTransferableMessage = function createTransferableMessage(message) {
|
1560 | var transferable = {};
|
1561 |
|
1562 | Object.keys(message).forEach(function (key) {
|
1563 | var value = message[key];
|
1564 |
|
1565 | if (ArrayBuffer.isView(value)) {
|
1566 | transferable[key] = {
|
1567 | bytes: value.buffer,
|
1568 | byteOffset: value.byteOffset,
|
1569 | byteLength: value.byteLength
|
1570 | };
|
1571 | } else {
|
1572 | transferable[key] = value;
|
1573 | }
|
1574 | });
|
1575 |
|
1576 | return transferable;
|
1577 | };
|
1578 |
|
1579 |
|
1580 |
|
1581 |
|
1582 |
|
1583 | var initSegmentId = function initSegmentId(initSegment) {
|
1584 | var byterange = initSegment.byterange || {
|
1585 | length: Infinity,
|
1586 | offset: 0
|
1587 | };
|
1588 |
|
1589 | return [byterange.length, byterange.offset, initSegment.resolvedUri].join(',');
|
1590 | };
|
1591 |
|
1592 |
|
1593 |
|
1594 |
|
1595 | var segmentKeyId = function segmentKeyId(key) {
|
1596 | return key.resolvedUri;
|
1597 | };
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 | var hexDump = function hexDump(data) {
|
1603 | var bytes = Array.prototype.slice.call(data);
|
1604 | var step = 16;
|
1605 | var result = '';
|
1606 | var hex = void 0;
|
1607 | var ascii = void 0;
|
1608 |
|
1609 | for (var j = 0; j < bytes.length / step; j++) {
|
1610 | hex = bytes.slice(j * step, j * step + step).map(formatHexString).join('');
|
1611 | ascii = bytes.slice(j * step, j * step + step).map(formatAsciiString).join('');
|
1612 | result += hex + ' ' + ascii + '\n';
|
1613 | }
|
1614 |
|
1615 | return result;
|
1616 | };
|
1617 |
|
1618 | var tagDump = function tagDump(_ref) {
|
1619 | var bytes = _ref.bytes;
|
1620 | return hexDump(bytes);
|
1621 | };
|
1622 |
|
1623 | var textRanges = function textRanges(ranges) {
|
1624 | var result = '';
|
1625 | var i = void 0;
|
1626 |
|
1627 | for (i = 0; i < ranges.length; i++) {
|
1628 | result += textRange(ranges, i) + ' ';
|
1629 | }
|
1630 | return result;
|
1631 | };
|
1632 |
|
1633 | var utils = Object.freeze({
|
1634 | createTransferableMessage: createTransferableMessage,
|
1635 | initSegmentId: initSegmentId,
|
1636 | segmentKeyId: segmentKeyId,
|
1637 | hexDump: hexDump,
|
1638 | tagDump: tagDump,
|
1639 | textRanges: textRanges
|
1640 | });
|
1641 |
|
1642 |
|
1643 |
|
1644 |
|
1645 |
|
1646 | var SEGMENT_END_FUDGE_PERCENT = 0.25;
|
1647 |
|
1648 |
|
1649 |
|
1650 |
|
1651 |
|
1652 |
|
1653 |
|
1654 |
|
1655 |
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 | var playerTimeToProgramTime = function playerTimeToProgramTime(playerTime, segment) {
|
1664 | if (!segment.dateTimeObject) {
|
1665 |
|
1666 |
|
1667 | return null;
|
1668 | }
|
1669 |
|
1670 | var transmuxerPrependedSeconds = segment.videoTimingInfo.transmuxerPrependedSeconds;
|
1671 | var transmuxedStart = segment.videoTimingInfo.transmuxedPresentationStart;
|
1672 |
|
1673 |
|
1674 | var startOfSegment = transmuxedStart + transmuxerPrependedSeconds;
|
1675 | var offsetFromSegmentStart = playerTime - startOfSegment;
|
1676 |
|
1677 | return new Date(segment.dateTimeObject.getTime() + offsetFromSegmentStart * 1000);
|
1678 | };
|
1679 |
|
1680 | var originalSegmentVideoDuration = function originalSegmentVideoDuration(videoTimingInfo) {
|
1681 | return videoTimingInfo.transmuxedPresentationEnd - videoTimingInfo.transmuxedPresentationStart - videoTimingInfo.transmuxerPrependedSeconds;
|
1682 | };
|
1683 |
|
1684 |
|
1685 |
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 | var findSegmentForProgramTime = function findSegmentForProgramTime(programTime, playlist) {
|
1692 |
|
1693 |
|
1694 |
|
1695 |
|
1696 | var dateTimeObject = void 0;
|
1697 |
|
1698 | try {
|
1699 | dateTimeObject = new Date(programTime);
|
1700 | } catch (e) {
|
1701 | return null;
|
1702 | }
|
1703 |
|
1704 | if (!playlist || !playlist.segments || playlist.segments.length === 0) {
|
1705 | return null;
|
1706 | }
|
1707 |
|
1708 | var segment = playlist.segments[0];
|
1709 |
|
1710 | if (dateTimeObject < segment.dateTimeObject) {
|
1711 |
|
1712 | return null;
|
1713 | }
|
1714 |
|
1715 | for (var i = 0; i < playlist.segments.length - 1; i++) {
|
1716 | segment = playlist.segments[i];
|
1717 |
|
1718 | var nextSegmentStart = playlist.segments[i + 1].dateTimeObject;
|
1719 |
|
1720 | if (dateTimeObject < nextSegmentStart) {
|
1721 | break;
|
1722 | }
|
1723 | }
|
1724 |
|
1725 | var lastSegment = playlist.segments[playlist.segments.length - 1];
|
1726 | var lastSegmentStart = lastSegment.dateTimeObject;
|
1727 | var lastSegmentDuration = lastSegment.videoTimingInfo ? originalSegmentVideoDuration(lastSegment.videoTimingInfo) : lastSegment.duration + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT;
|
1728 | var lastSegmentEnd = new Date(lastSegmentStart.getTime() + lastSegmentDuration * 1000);
|
1729 |
|
1730 | if (dateTimeObject > lastSegmentEnd) {
|
1731 |
|
1732 | return null;
|
1733 | }
|
1734 |
|
1735 | if (dateTimeObject > lastSegmentStart) {
|
1736 | segment = lastSegment;
|
1737 | }
|
1738 |
|
1739 | return {
|
1740 | segment: segment,
|
1741 | estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : Playlist.duration(playlist, playlist.mediaSequence + playlist.segments.indexOf(segment)),
|
1742 |
|
1743 |
|
1744 |
|
1745 |
|
1746 | type: segment.videoTimingInfo ? 'accurate' : 'estimate'
|
1747 | };
|
1748 | };
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 | var findSegmentForPlayerTime = function findSegmentForPlayerTime(time, playlist) {
|
1757 |
|
1758 |
|
1759 |
|
1760 |
|
1761 |
|
1762 | if (!playlist || !playlist.segments || playlist.segments.length === 0) {
|
1763 | return null;
|
1764 | }
|
1765 |
|
1766 | var segmentEnd = 0;
|
1767 | var segment = void 0;
|
1768 |
|
1769 | for (var i = 0; i < playlist.segments.length; i++) {
|
1770 | segment = playlist.segments[i];
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 | segmentEnd = segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationEnd : segmentEnd + segment.duration;
|
1779 |
|
1780 | if (time <= segmentEnd) {
|
1781 | break;
|
1782 | }
|
1783 | }
|
1784 |
|
1785 | var lastSegment = playlist.segments[playlist.segments.length - 1];
|
1786 |
|
1787 | if (lastSegment.videoTimingInfo && lastSegment.videoTimingInfo.transmuxedPresentationEnd < time) {
|
1788 |
|
1789 | return null;
|
1790 | }
|
1791 |
|
1792 | if (time > segmentEnd) {
|
1793 |
|
1794 |
|
1795 |
|
1796 | if (time > segmentEnd + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT) {
|
1797 |
|
1798 |
|
1799 |
|
1800 | return null;
|
1801 | }
|
1802 |
|
1803 | segment = lastSegment;
|
1804 | }
|
1805 |
|
1806 | return {
|
1807 | segment: segment,
|
1808 | estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : segmentEnd - segment.duration,
|
1809 |
|
1810 |
|
1811 | type: segment.videoTimingInfo ? 'accurate' : 'estimate'
|
1812 | };
|
1813 | };
|
1814 |
|
1815 |
|
1816 |
|
1817 |
|
1818 |
|
1819 |
|
1820 |
|
1821 |
|
1822 |
|
1823 |
|
1824 |
|
1825 | var getOffsetFromTimestamp = function getOffsetFromTimestamp(comparisonTimeStamp, programTime) {
|
1826 | var segmentDateTime = void 0;
|
1827 | var programDateTime = void 0;
|
1828 |
|
1829 | try {
|
1830 | segmentDateTime = new Date(comparisonTimeStamp);
|
1831 | programDateTime = new Date(programTime);
|
1832 | } catch (e) {
|
1833 |
|
1834 | }
|
1835 |
|
1836 | var segmentTimeEpoch = segmentDateTime.getTime();
|
1837 | var programTimeEpoch = programDateTime.getTime();
|
1838 |
|
1839 | return (programTimeEpoch - segmentTimeEpoch) / 1000;
|
1840 | };
|
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 | var verifyProgramDateTimeTags = function verifyProgramDateTimeTags(playlist) {
|
1848 | if (!playlist.segments || playlist.segments.length === 0) {
|
1849 | return false;
|
1850 | }
|
1851 |
|
1852 | for (var i = 0; i < playlist.segments.length; i++) {
|
1853 | var segment = playlist.segments[i];
|
1854 |
|
1855 | if (!segment.dateTimeObject) {
|
1856 | return false;
|
1857 | }
|
1858 | }
|
1859 |
|
1860 | return true;
|
1861 | };
|
1862 |
|
1863 |
|
1864 |
|
1865 |
|
1866 |
|
1867 |
|
1868 |
|
1869 |
|
1870 |
|
1871 |
|
1872 |
|
1873 |
|
1874 |
|
1875 |
|
1876 |
|
1877 |
|
1878 | var getProgramTime = function getProgramTime(_ref) {
|
1879 | var playlist = _ref.playlist,
|
1880 | _ref$time = _ref.time,
|
1881 | time = _ref$time === undefined ? undefined : _ref$time,
|
1882 | callback = _ref.callback;
|
1883 |
|
1884 |
|
1885 | if (!callback) {
|
1886 | throw new Error('getProgramTime: callback must be provided');
|
1887 | }
|
1888 |
|
1889 | if (!playlist || time === undefined) {
|
1890 | return callback({
|
1891 | message: 'getProgramTime: playlist and time must be provided'
|
1892 | });
|
1893 | }
|
1894 |
|
1895 | var matchedSegment = findSegmentForPlayerTime(time, playlist);
|
1896 |
|
1897 | if (!matchedSegment) {
|
1898 | return callback({
|
1899 | message: 'valid programTime was not found'
|
1900 | });
|
1901 | }
|
1902 |
|
1903 | if (matchedSegment.type === 'estimate') {
|
1904 | return callback({
|
1905 | message: 'Accurate programTime could not be determined.' + ' Please seek to e.seekTime and try again',
|
1906 | seekTime: matchedSegment.estimatedStart
|
1907 | });
|
1908 | }
|
1909 |
|
1910 | var programTimeObject = {
|
1911 | mediaSeconds: time
|
1912 | };
|
1913 | var programTime = playerTimeToProgramTime(time, matchedSegment.segment);
|
1914 |
|
1915 | if (programTime) {
|
1916 | programTimeObject.programDateTime = programTime.toISOString();
|
1917 | }
|
1918 |
|
1919 | return callback(null, programTimeObject);
|
1920 | };
|
1921 |
|
1922 |
|
1923 |
|
1924 |
|
1925 |
|
1926 |
|
1927 |
|
1928 |
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 |
|
1936 | var seekToProgramTime = function seekToProgramTime(_ref2) {
|
1937 | var programTime = _ref2.programTime,
|
1938 | playlist = _ref2.playlist,
|
1939 | _ref2$retryCount = _ref2.retryCount,
|
1940 | retryCount = _ref2$retryCount === undefined ? 2 : _ref2$retryCount,
|
1941 | seekTo = _ref2.seekTo,
|
1942 | _ref2$pauseAfterSeek = _ref2.pauseAfterSeek,
|
1943 | pauseAfterSeek = _ref2$pauseAfterSeek === undefined ? true : _ref2$pauseAfterSeek,
|
1944 | tech = _ref2.tech,
|
1945 | callback = _ref2.callback;
|
1946 |
|
1947 |
|
1948 | if (!callback) {
|
1949 | throw new Error('seekToProgramTime: callback must be provided');
|
1950 | }
|
1951 |
|
1952 | if (typeof programTime === 'undefined' || !playlist || !seekTo) {
|
1953 | return callback({
|
1954 | message: 'seekToProgramTime: programTime, seekTo and playlist must be provided'
|
1955 | });
|
1956 | }
|
1957 |
|
1958 | if (!playlist.endList && !tech.hasStarted_) {
|
1959 | return callback({
|
1960 | message: 'player must be playing a live stream to start buffering'
|
1961 | });
|
1962 | }
|
1963 |
|
1964 | if (!verifyProgramDateTimeTags(playlist)) {
|
1965 | return callback({
|
1966 | message: 'programDateTime tags must be provided in the manifest ' + playlist.resolvedUri
|
1967 | });
|
1968 | }
|
1969 |
|
1970 | var matchedSegment = findSegmentForProgramTime(programTime, playlist);
|
1971 |
|
1972 |
|
1973 | if (!matchedSegment) {
|
1974 | return callback({
|
1975 | message: programTime + ' was not found in the stream'
|
1976 | });
|
1977 | }
|
1978 |
|
1979 | var segment = matchedSegment.segment;
|
1980 | var mediaOffset = getOffsetFromTimestamp(segment.dateTimeObject, programTime);
|
1981 |
|
1982 | if (matchedSegment.type === 'estimate') {
|
1983 |
|
1984 | if (retryCount === 0) {
|
1985 | return callback({
|
1986 | message: programTime + ' is not buffered yet. Try again'
|
1987 | });
|
1988 | }
|
1989 |
|
1990 | seekTo(matchedSegment.estimatedStart + mediaOffset);
|
1991 |
|
1992 | tech.one('seeked', function () {
|
1993 | seekToProgramTime({
|
1994 | programTime: programTime,
|
1995 | playlist: playlist,
|
1996 | retryCount: retryCount - 1,
|
1997 | seekTo: seekTo,
|
1998 | pauseAfterSeek: pauseAfterSeek,
|
1999 | tech: tech,
|
2000 | callback: callback
|
2001 | });
|
2002 | });
|
2003 |
|
2004 | return;
|
2005 | }
|
2006 |
|
2007 |
|
2008 |
|
2009 |
|
2010 | var seekToTime = segment.start + mediaOffset;
|
2011 | var seekedCallback = function seekedCallback() {
|
2012 | return callback(null, tech.currentTime());
|
2013 | };
|
2014 |
|
2015 |
|
2016 | tech.one('seeked', seekedCallback);
|
2017 |
|
2018 | if (pauseAfterSeek) {
|
2019 | tech.pause();
|
2020 | }
|
2021 | seekTo(seekToTime);
|
2022 | };
|
2023 |
|
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 | var TIME_FUDGE_FACTOR = 1 / 30;
|
2033 |
|
2034 |
|
2035 |
|
2036 |
|
2037 |
|
2038 | var SAFE_TIME_DELTA = TIME_FUDGE_FACTOR * 3;
|
2039 | var filterRanges = function filterRanges(timeRanges, predicate) {
|
2040 | var results = [];
|
2041 | var i = void 0;
|
2042 |
|
2043 | if (timeRanges && timeRanges.length) {
|
2044 |
|
2045 | for (i = 0; i < timeRanges.length; i++) {
|
2046 | if (predicate(timeRanges.start(i), timeRanges.end(i))) {
|
2047 | results.push([timeRanges.start(i), timeRanges.end(i)]);
|
2048 | }
|
2049 | }
|
2050 | }
|
2051 |
|
2052 | return videojs.createTimeRanges(results);
|
2053 | };
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 |
|
2061 |
|
2062 | var findRange = function findRange(buffered, time) {
|
2063 | return filterRanges(buffered, function (start, end) {
|
2064 | return start - SAFE_TIME_DELTA <= time && end + SAFE_TIME_DELTA >= time;
|
2065 | });
|
2066 | };
|
2067 |
|
2068 |
|
2069 |
|
2070 |
|
2071 |
|
2072 |
|
2073 |
|
2074 | var findNextRange = function findNextRange(timeRanges, time) {
|
2075 | return filterRanges(timeRanges, function (start) {
|
2076 | return start - TIME_FUDGE_FACTOR >= time;
|
2077 | });
|
2078 | };
|
2079 |
|
2080 |
|
2081 |
|
2082 |
|
2083 |
|
2084 |
|
2085 | var findGaps = function findGaps(buffered) {
|
2086 | if (buffered.length < 2) {
|
2087 | return videojs.createTimeRanges();
|
2088 | }
|
2089 |
|
2090 | var ranges = [];
|
2091 |
|
2092 | for (var i = 1; i < buffered.length; i++) {
|
2093 | var start = buffered.end(i - 1);
|
2094 | var end = buffered.start(i);
|
2095 |
|
2096 | ranges.push([start, end]);
|
2097 | }
|
2098 |
|
2099 | return videojs.createTimeRanges(ranges);
|
2100 | };
|
2101 |
|
2102 |
|
2103 |
|
2104 |
|
2105 |
|
2106 |
|
2107 |
|
2108 | var printableRange = function printableRange(range) {
|
2109 | var strArr = [];
|
2110 |
|
2111 | if (!range || !range.length) {
|
2112 | return '';
|
2113 | }
|
2114 |
|
2115 | for (var i = 0; i < range.length; i++) {
|
2116 | strArr.push(range.start(i) + ' => ' + range.end(i));
|
2117 | }
|
2118 |
|
2119 | return strArr.join(', ');
|
2120 | };
|
2121 |
|
2122 |
|
2123 |
|
2124 |
|
2125 |
|
2126 |
|
2127 |
|
2128 |
|
2129 |
|
2130 |
|
2131 |
|
2132 |
|
2133 |
|
2134 |
|
2135 |
|
2136 | var timeUntilRebuffer = function timeUntilRebuffer(buffered, currentTime) {
|
2137 | var playbackRate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
2138 |
|
2139 | var bufferedEnd = buffered.length ? buffered.end(buffered.length - 1) : 0;
|
2140 |
|
2141 | return (bufferedEnd - currentTime) / playbackRate;
|
2142 | };
|
2143 |
|
2144 |
|
2145 |
|
2146 |
|
2147 |
|
2148 |
|
2149 | var timeRangesToArray = function timeRangesToArray(timeRanges) {
|
2150 | var timeRangesList = [];
|
2151 |
|
2152 | for (var i = 0; i < timeRanges.length; i++) {
|
2153 | timeRangesList.push({
|
2154 | start: timeRanges.start(i),
|
2155 | end: timeRanges.end(i)
|
2156 | });
|
2157 | }
|
2158 |
|
2159 | return timeRangesList;
|
2160 | };
|
2161 |
|
2162 |
|
2163 |
|
2164 |
|
2165 |
|
2166 |
|
2167 |
|
2168 |
|
2169 |
|
2170 |
|
2171 |
|
2172 |
|
2173 |
|
2174 | var createTextTracksIfNecessary = function createTextTracksIfNecessary(sourceBuffer, mediaSource, segment) {
|
2175 | var player = mediaSource.player_;
|
2176 |
|
2177 |
|
2178 | if (segment.captions && segment.captions.length) {
|
2179 | if (!sourceBuffer.inbandTextTracks_) {
|
2180 | sourceBuffer.inbandTextTracks_ = {};
|
2181 | }
|
2182 |
|
2183 | for (var trackId in segment.captionStreams) {
|
2184 | if (!sourceBuffer.inbandTextTracks_[trackId]) {
|
2185 | player.tech_.trigger({ type: 'usage', name: 'hls-608' });
|
2186 | var track = player.textTracks().getTrackById(trackId);
|
2187 |
|
2188 | if (track) {
|
2189 |
|
2190 |
|
2191 |
|
2192 | sourceBuffer.inbandTextTracks_[trackId] = track;
|
2193 | } else {
|
2194 |
|
2195 |
|
2196 | sourceBuffer.inbandTextTracks_[trackId] = player.addRemoteTextTrack({
|
2197 | kind: 'captions',
|
2198 | id: trackId,
|
2199 | label: trackId
|
2200 | }, false).track;
|
2201 | }
|
2202 | }
|
2203 | }
|
2204 | }
|
2205 |
|
2206 | if (segment.metadata && segment.metadata.length && !sourceBuffer.metadataTrack_) {
|
2207 | sourceBuffer.metadataTrack_ = player.addRemoteTextTrack({
|
2208 | kind: 'metadata',
|
2209 | label: 'Timed Metadata'
|
2210 | }, false).track;
|
2211 | sourceBuffer.metadataTrack_.inBandMetadataTrackDispatchType = segment.metadata.dispatchType;
|
2212 | }
|
2213 | };
|
2214 |
|
2215 |
|
2216 |
|
2217 |
|
2218 |
|
2219 |
|
2220 |
|
2221 |
|
2222 |
|
2223 |
|
2224 |
|
2225 |
|
2226 |
|
2227 | var removeCuesFromTrack = function removeCuesFromTrack(start, end, track) {
|
2228 | var i = void 0;
|
2229 | var cue = void 0;
|
2230 |
|
2231 | if (!track) {
|
2232 | return;
|
2233 | }
|
2234 |
|
2235 | if (!track.cues) {
|
2236 | return;
|
2237 | }
|
2238 |
|
2239 | i = track.cues.length;
|
2240 |
|
2241 | while (i--) {
|
2242 | cue = track.cues[i];
|
2243 |
|
2244 |
|
2245 | if (cue.startTime <= end && cue.endTime >= start) {
|
2246 | track.removeCue(cue);
|
2247 | }
|
2248 | }
|
2249 | };
|
2250 |
|
2251 |
|
2252 |
|
2253 |
|
2254 |
|
2255 |
|
2256 |
|
2257 |
|
2258 |
|
2259 |
|
2260 |
|
2261 |
|
2262 | var deprecateOldCue = function deprecateOldCue(cue) {
|
2263 | Object.defineProperties(cue.frame, {
|
2264 | id: {
|
2265 | get: function get() {
|
2266 | videojs.log.warn('cue.frame.id is deprecated. Use cue.value.key instead.');
|
2267 | return cue.value.key;
|
2268 | }
|
2269 | },
|
2270 | value: {
|
2271 | get: function get() {
|
2272 | videojs.log.warn('cue.frame.value is deprecated. Use cue.value.data instead.');
|
2273 | return cue.value.data;
|
2274 | }
|
2275 | },
|
2276 | privateData: {
|
2277 | get: function get() {
|
2278 | videojs.log.warn('cue.frame.privateData is deprecated. Use cue.value.data instead.');
|
2279 | return cue.value.data;
|
2280 | }
|
2281 | }
|
2282 | });
|
2283 | };
|
2284 |
|
2285 | var durationOfVideo = function durationOfVideo(duration) {
|
2286 | var dur = void 0;
|
2287 |
|
2288 | if (isNaN(duration) || Math.abs(duration) === Infinity) {
|
2289 | dur = Number.MAX_VALUE;
|
2290 | } else {
|
2291 | dur = duration;
|
2292 | }
|
2293 | return dur;
|
2294 | };
|
2295 |
|
2296 |
|
2297 |
|
2298 |
|
2299 |
|
2300 |
|
2301 |
|
2302 |
|
2303 |
|
2304 | var addTextTrackData = function addTextTrackData(sourceHandler, captionArray, metadataArray) {
|
2305 | var Cue = window$1.WebKitDataCue || window$1.VTTCue;
|
2306 |
|
2307 | if (captionArray) {
|
2308 | captionArray.forEach(function (caption) {
|
2309 | var track = caption.stream;
|
2310 |
|
2311 | this.inbandTextTracks_[track].addCue(new Cue(caption.startTime + this.timestampOffset, caption.endTime + this.timestampOffset, caption.text));
|
2312 | }, sourceHandler);
|
2313 | }
|
2314 |
|
2315 | if (metadataArray) {
|
2316 | var videoDuration = durationOfVideo(sourceHandler.mediaSource_.duration);
|
2317 |
|
2318 | metadataArray.forEach(function (metadata) {
|
2319 | var time = metadata.cueTime + this.timestampOffset;
|
2320 |
|
2321 |
|
2322 |
|
2323 |
|
2324 |
|
2325 | if (typeof time !== 'number' || window$1.isNaN(time) || time < 0 || !(time < Infinity)) {
|
2326 | return;
|
2327 | }
|
2328 |
|
2329 | metadata.frames.forEach(function (frame) {
|
2330 | var cue = new Cue(time, time, frame.value || frame.url || frame.data || '');
|
2331 |
|
2332 | cue.frame = frame;
|
2333 | cue.value = frame;
|
2334 | deprecateOldCue(cue);
|
2335 |
|
2336 | this.metadataTrack_.addCue(cue);
|
2337 | }, this);
|
2338 | }, sourceHandler);
|
2339 |
|
2340 |
|
2341 |
|
2342 |
|
2343 | if (sourceHandler.metadataTrack_ && sourceHandler.metadataTrack_.cues && sourceHandler.metadataTrack_.cues.length) {
|
2344 | var cues = sourceHandler.metadataTrack_.cues;
|
2345 | var cuesArray = [];
|
2346 |
|
2347 |
|
2348 |
|
2349 | for (var i = 0; i < cues.length; i++) {
|
2350 | if (cues[i]) {
|
2351 | cuesArray.push(cues[i]);
|
2352 | }
|
2353 | }
|
2354 |
|
2355 |
|
2356 | var cuesGroupedByStartTime = cuesArray.reduce(function (obj, cue) {
|
2357 | var timeSlot = obj[cue.startTime] || [];
|
2358 |
|
2359 | timeSlot.push(cue);
|
2360 | obj[cue.startTime] = timeSlot;
|
2361 |
|
2362 | return obj;
|
2363 | }, {});
|
2364 |
|
2365 |
|
2366 | var sortedStartTimes = Object.keys(cuesGroupedByStartTime).sort(function (a, b) {
|
2367 | return Number(a) - Number(b);
|
2368 | });
|
2369 |
|
2370 |
|
2371 | sortedStartTimes.forEach(function (startTime, idx) {
|
2372 | var cueGroup = cuesGroupedByStartTime[startTime];
|
2373 | var nextTime = Number(sortedStartTimes[idx + 1]) || videoDuration;
|
2374 |
|
2375 |
|
2376 | cueGroup.forEach(function (cue) {
|
2377 | cue.endTime = nextTime;
|
2378 | });
|
2379 | });
|
2380 | }
|
2381 | }
|
2382 | };
|
2383 |
|
2384 | var win = typeof window !== 'undefined' ? window : {},
|
2385 | TARGET = typeof Symbol === 'undefined' ? '__target' : Symbol(),
|
2386 | SCRIPT_TYPE = 'application/javascript',
|
2387 | BlobBuilder = win.BlobBuilder || win.WebKitBlobBuilder || win.MozBlobBuilder || win.MSBlobBuilder,
|
2388 | URL = win.URL || win.webkitURL || URL && URL.msURL,
|
2389 | Worker = win.Worker;
|
2390 |
|
2391 |
|
2392 |
|
2393 |
|
2394 |
|
2395 |
|
2396 |
|
2397 |
|
2398 |
|
2399 | function shimWorker(filename, fn) {
|
2400 | return function ShimWorker(forceFallback) {
|
2401 | var o = this;
|
2402 |
|
2403 | if (!fn) {
|
2404 | return new Worker(filename);
|
2405 | } else if (Worker && !forceFallback) {
|
2406 |
|
2407 | var source = fn.toString().replace(/^function.+?{/, '').slice(0, -1),
|
2408 | objURL = createSourceObject(source);
|
2409 |
|
2410 | this[TARGET] = new Worker(objURL);
|
2411 | wrapTerminate(this[TARGET], objURL);
|
2412 | return this[TARGET];
|
2413 | } else {
|
2414 | var selfShim = {
|
2415 | postMessage: function postMessage(m) {
|
2416 | if (o.onmessage) {
|
2417 | setTimeout(function () {
|
2418 | o.onmessage({ data: m, target: selfShim });
|
2419 | });
|
2420 | }
|
2421 | }
|
2422 | };
|
2423 |
|
2424 | fn.call(selfShim);
|
2425 | this.postMessage = function (m) {
|
2426 | setTimeout(function () {
|
2427 | selfShim.onmessage({ data: m, target: o });
|
2428 | });
|
2429 | };
|
2430 | this.isThisThread = true;
|
2431 | }
|
2432 | };
|
2433 | }
|
2434 |
|
2435 | if (Worker) {
|
2436 | var testWorker,
|
2437 | objURL = createSourceObject('self.onmessage = function () {}'),
|
2438 | testArray = new Uint8Array(1);
|
2439 |
|
2440 | try {
|
2441 | testWorker = new Worker(objURL);
|
2442 |
|
2443 |
|
2444 | testWorker.postMessage(testArray, [testArray.buffer]);
|
2445 | } catch (e) {
|
2446 | Worker = null;
|
2447 | } finally {
|
2448 | URL.revokeObjectURL(objURL);
|
2449 | if (testWorker) {
|
2450 | testWorker.terminate();
|
2451 | }
|
2452 | }
|
2453 | }
|
2454 |
|
2455 | function createSourceObject(str) {
|
2456 | try {
|
2457 | return URL.createObjectURL(new Blob([str], { type: SCRIPT_TYPE }));
|
2458 | } catch (e) {
|
2459 | var blob = new BlobBuilder();
|
2460 | blob.append(str);
|
2461 | return URL.createObjectURL(blob.getBlob(type));
|
2462 | }
|
2463 | }
|
2464 |
|
2465 | function wrapTerminate(worker, objURL) {
|
2466 | if (!worker || !objURL) return;
|
2467 | var term = worker.terminate;
|
2468 | worker.objURL = objURL;
|
2469 | worker.terminate = function () {
|
2470 | if (worker.objURL) URL.revokeObjectURL(worker.objURL);
|
2471 | term.call(worker);
|
2472 | };
|
2473 | }
|
2474 |
|
2475 | var TransmuxWorker = new shimWorker("./transmuxer-worker.worker.js", function (window, document$$1) {
|
2476 | var self = this;
|
2477 | var transmuxerWorker = function () {
|
2478 |
|
2479 | |
2480 |
|
2481 |
|
2482 |
|
2483 |
|
2484 |
|
2485 |
|
2486 |
|
2487 |
|
2488 |
|
2489 | var Stream = function Stream() {
|
2490 | this.init = function () {
|
2491 | var listeners = {};
|
2492 | |
2493 |
|
2494 |
|
2495 |
|
2496 |
|
2497 |
|
2498 | this.on = function (type, listener) {
|
2499 | if (!listeners[type]) {
|
2500 | listeners[type] = [];
|
2501 | }
|
2502 | listeners[type] = listeners[type].concat(listener);
|
2503 | };
|
2504 | |
2505 |
|
2506 |
|
2507 |
|
2508 |
|
2509 |
|
2510 | this.off = function (type, listener) {
|
2511 | var index;
|
2512 | if (!listeners[type]) {
|
2513 | return false;
|
2514 | }
|
2515 | index = listeners[type].indexOf(listener);
|
2516 | listeners[type] = listeners[type].slice();
|
2517 | listeners[type].splice(index, 1);
|
2518 | return index > -1;
|
2519 | };
|
2520 | |
2521 |
|
2522 |
|
2523 |
|
2524 |
|
2525 | this.trigger = function (type) {
|
2526 | var callbacks, i, length, args;
|
2527 | callbacks = listeners[type];
|
2528 | if (!callbacks) {
|
2529 | return;
|
2530 | }
|
2531 |
|
2532 |
|
2533 |
|
2534 |
|
2535 | if (arguments.length === 2) {
|
2536 | length = callbacks.length;
|
2537 | for (i = 0; i < length; ++i) {
|
2538 | callbacks[i].call(this, arguments[1]);
|
2539 | }
|
2540 | } else {
|
2541 | args = [];
|
2542 | i = arguments.length;
|
2543 | for (i = 1; i < arguments.length; ++i) {
|
2544 | args.push(arguments[i]);
|
2545 | }
|
2546 | length = callbacks.length;
|
2547 | for (i = 0; i < length; ++i) {
|
2548 | callbacks[i].apply(this, args);
|
2549 | }
|
2550 | }
|
2551 | };
|
2552 | |
2553 |
|
2554 |
|
2555 | this.dispose = function () {
|
2556 | listeners = {};
|
2557 | };
|
2558 | };
|
2559 | };
|
2560 |
|
2561 | |
2562 |
|
2563 |
|
2564 |
|
2565 |
|
2566 |
|
2567 |
|
2568 |
|
2569 |
|
2570 | Stream.prototype.pipe = function (destination) {
|
2571 | this.on('data', function (data) {
|
2572 | destination.push(data);
|
2573 | });
|
2574 |
|
2575 | this.on('done', function (flushSource) {
|
2576 | destination.flush(flushSource);
|
2577 | });
|
2578 |
|
2579 | this.on('partialdone', function (flushSource) {
|
2580 | destination.partialFlush(flushSource);
|
2581 | });
|
2582 |
|
2583 | this.on('endedtimeline', function (flushSource) {
|
2584 | destination.endTimeline(flushSource);
|
2585 | });
|
2586 |
|
2587 | this.on('reset', function (flushSource) {
|
2588 | destination.reset(flushSource);
|
2589 | });
|
2590 |
|
2591 | return destination;
|
2592 | };
|
2593 |
|
2594 |
|
2595 |
|
2596 |
|
2597 |
|
2598 | Stream.prototype.push = function (data) {
|
2599 | this.trigger('data', data);
|
2600 | };
|
2601 |
|
2602 | Stream.prototype.flush = function (flushSource) {
|
2603 | this.trigger('done', flushSource);
|
2604 | };
|
2605 |
|
2606 | Stream.prototype.partialFlush = function (flushSource) {
|
2607 | this.trigger('partialdone', flushSource);
|
2608 | };
|
2609 |
|
2610 | Stream.prototype.endTimeline = function (flushSource) {
|
2611 | this.trigger('endedtimeline', flushSource);
|
2612 | };
|
2613 |
|
2614 | Stream.prototype.reset = function (flushSource) {
|
2615 | this.trigger('reset', flushSource);
|
2616 | };
|
2617 |
|
2618 | var stream = Stream;
|
2619 |
|
2620 | |
2621 |
|
2622 |
|
2623 |
|
2624 |
|
2625 |
|
2626 |
|
2627 |
|
2628 |
|
2629 |
|
2630 | var UINT32_MAX = Math.pow(2, 32) - 1;
|
2631 |
|
2632 | var box, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, sdtp, stbl, stsd, traf, trex, trun, types, MAJOR_BRAND, MINOR_VERSION, AVC1_BRAND, VIDEO_HDLR, AUDIO_HDLR, HDLR_TYPES, VMHD, SMHD, DREF, STCO, STSC, STSZ, STTS;
|
2633 |
|
2634 |
|
2635 | (function () {
|
2636 | var i;
|
2637 | types = {
|
2638 | avc1: [],
|
2639 | avcC: [],
|
2640 | btrt: [],
|
2641 | dinf: [],
|
2642 | dref: [],
|
2643 | esds: [],
|
2644 | ftyp: [],
|
2645 | hdlr: [],
|
2646 | mdat: [],
|
2647 | mdhd: [],
|
2648 | mdia: [],
|
2649 | mfhd: [],
|
2650 | minf: [],
|
2651 | moof: [],
|
2652 | moov: [],
|
2653 | mp4a: [],
|
2654 | mvex: [],
|
2655 | mvhd: [],
|
2656 | pasp: [],
|
2657 | sdtp: [],
|
2658 | smhd: [],
|
2659 | stbl: [],
|
2660 | stco: [],
|
2661 | stsc: [],
|
2662 | stsd: [],
|
2663 | stsz: [],
|
2664 | stts: [],
|
2665 | styp: [],
|
2666 | tfdt: [],
|
2667 | tfhd: [],
|
2668 | traf: [],
|
2669 | trak: [],
|
2670 | trun: [],
|
2671 | trex: [],
|
2672 | tkhd: [],
|
2673 | vmhd: []
|
2674 | };
|
2675 |
|
2676 |
|
2677 |
|
2678 | if (typeof Uint8Array === 'undefined') {
|
2679 | return;
|
2680 | }
|
2681 |
|
2682 | for (i in types) {
|
2683 | if (types.hasOwnProperty(i)) {
|
2684 | types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
|
2685 | }
|
2686 | }
|
2687 |
|
2688 | MAJOR_BRAND = new Uint8Array(['i'.charCodeAt(0), 's'.charCodeAt(0), 'o'.charCodeAt(0), 'm'.charCodeAt(0)]);
|
2689 | AVC1_BRAND = new Uint8Array(['a'.charCodeAt(0), 'v'.charCodeAt(0), 'c'.charCodeAt(0), '1'.charCodeAt(0)]);
|
2690 | MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
|
2691 | VIDEO_HDLR = new Uint8Array([0x00,
|
2692 | 0x00, 0x00, 0x00,
|
2693 | 0x00, 0x00, 0x00, 0x00,
|
2694 | 0x76, 0x69, 0x64, 0x65,
|
2695 | 0x00, 0x00, 0x00, 0x00,
|
2696 | 0x00, 0x00, 0x00, 0x00,
|
2697 | 0x00, 0x00, 0x00, 0x00,
|
2698 | 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00
|
2699 | ]);
|
2700 | AUDIO_HDLR = new Uint8Array([0x00,
|
2701 | 0x00, 0x00, 0x00,
|
2702 | 0x00, 0x00, 0x00, 0x00,
|
2703 | 0x73, 0x6f, 0x75, 0x6e,
|
2704 | 0x00, 0x00, 0x00, 0x00,
|
2705 | 0x00, 0x00, 0x00, 0x00,
|
2706 | 0x00, 0x00, 0x00, 0x00,
|
2707 | 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00
|
2708 | ]);
|
2709 | HDLR_TYPES = {
|
2710 | video: VIDEO_HDLR,
|
2711 | audio: AUDIO_HDLR
|
2712 | };
|
2713 | DREF = new Uint8Array([0x00,
|
2714 | 0x00, 0x00, 0x00,
|
2715 | 0x00, 0x00, 0x00, 0x01,
|
2716 | 0x00, 0x00, 0x00, 0x0c,
|
2717 | 0x75, 0x72, 0x6c, 0x20,
|
2718 | 0x00,
|
2719 | 0x00, 0x00, 0x01
|
2720 | ]);
|
2721 | SMHD = new Uint8Array([0x00,
|
2722 | 0x00, 0x00, 0x00,
|
2723 | 0x00, 0x00,
|
2724 | 0x00, 0x00
|
2725 | ]);
|
2726 | STCO = new Uint8Array([0x00,
|
2727 | 0x00, 0x00, 0x00,
|
2728 | 0x00, 0x00, 0x00, 0x00
|
2729 | ]);
|
2730 | STSC = STCO;
|
2731 | STSZ = new Uint8Array([0x00,
|
2732 | 0x00, 0x00, 0x00,
|
2733 | 0x00, 0x00, 0x00, 0x00,
|
2734 | 0x00, 0x00, 0x00, 0x00
|
2735 | ]);
|
2736 | STTS = STCO;
|
2737 | VMHD = new Uint8Array([0x00,
|
2738 | 0x00, 0x00, 0x01,
|
2739 | 0x00, 0x00,
|
2740 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
2741 | ]);
|
2742 | })();
|
2743 |
|
2744 | box = function box(type) {
|
2745 | var payload = [],
|
2746 | size = 0,
|
2747 | i,
|
2748 | result,
|
2749 | view;
|
2750 |
|
2751 | for (i = 1; i < arguments.length; i++) {
|
2752 | payload.push(arguments[i]);
|
2753 | }
|
2754 |
|
2755 | i = payload.length;
|
2756 |
|
2757 |
|
2758 | while (i--) {
|
2759 | size += payload[i].byteLength;
|
2760 | }
|
2761 | result = new Uint8Array(size + 8);
|
2762 | view = new DataView(result.buffer, result.byteOffset, result.byteLength);
|
2763 | view.setUint32(0, result.byteLength);
|
2764 | result.set(type, 4);
|
2765 |
|
2766 |
|
2767 | for (i = 0, size = 8; i < payload.length; i++) {
|
2768 | result.set(payload[i], size);
|
2769 | size += payload[i].byteLength;
|
2770 | }
|
2771 | return result;
|
2772 | };
|
2773 |
|
2774 | dinf = function dinf() {
|
2775 | return box(types.dinf, box(types.dref, DREF));
|
2776 | };
|
2777 |
|
2778 | esds = function esds(track) {
|
2779 | return box(types.esds, new Uint8Array([0x00,
|
2780 | 0x00, 0x00, 0x00,
|
2781 |
|
2782 |
|
2783 | 0x03,
|
2784 | 0x19,
|
2785 | 0x00, 0x00,
|
2786 | 0x00,
|
2787 |
|
2788 |
|
2789 | 0x04,
|
2790 | 0x11,
|
2791 | 0x40,
|
2792 | 0x15,
|
2793 | 0x00, 0x06, 0x00,
|
2794 | 0x00, 0x00, 0xda, 0xc0,
|
2795 | 0x00, 0x00, 0xda, 0xc0,
|
2796 |
|
2797 |
|
2798 | 0x05,
|
2799 | 0x02,
|
2800 |
|
2801 |
|
2802 | track.audioobjecttype << 3 | track.samplingfrequencyindex >>> 1, track.samplingfrequencyindex << 7 | track.channelcount << 3, 0x06, 0x01, 0x02
|
2803 | ]));
|
2804 | };
|
2805 |
|
2806 | ftyp = function ftyp() {
|
2807 | return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
|
2808 | };
|
2809 |
|
2810 | hdlr = function hdlr(type) {
|
2811 | return box(types.hdlr, HDLR_TYPES[type]);
|
2812 | };
|
2813 | mdat = function mdat(data) {
|
2814 | return box(types.mdat, data);
|
2815 | };
|
2816 | mdhd = function mdhd(track) {
|
2817 | var result = new Uint8Array([0x00,
|
2818 | 0x00, 0x00, 0x00,
|
2819 | 0x00, 0x00, 0x00, 0x02,
|
2820 | 0x00, 0x00, 0x00, 0x03,
|
2821 | 0x00, 0x01, 0x5f, 0x90,
|
2822 |
|
2823 | track.duration >>> 24 & 0xFF, track.duration >>> 16 & 0xFF, track.duration >>> 8 & 0xFF, track.duration & 0xFF,
|
2824 | 0x55, 0xc4,
|
2825 | 0x00, 0x00]);
|
2826 |
|
2827 |
|
2828 |
|
2829 |
|
2830 | if (track.samplerate) {
|
2831 | result[12] = track.samplerate >>> 24 & 0xFF;
|
2832 | result[13] = track.samplerate >>> 16 & 0xFF;
|
2833 | result[14] = track.samplerate >>> 8 & 0xFF;
|
2834 | result[15] = track.samplerate & 0xFF;
|
2835 | }
|
2836 |
|
2837 | return box(types.mdhd, result);
|
2838 | };
|
2839 | mdia = function mdia(track) {
|
2840 | return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
|
2841 | };
|
2842 | mfhd = function mfhd(sequenceNumber) {
|
2843 | return box(types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
|
2844 | (sequenceNumber & 0xFF000000) >> 24, (sequenceNumber & 0xFF0000) >> 16, (sequenceNumber & 0xFF00) >> 8, sequenceNumber & 0xFF
|
2845 | ]));
|
2846 | };
|
2847 | minf = function minf(track) {
|
2848 | return box(types.minf, track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD), dinf(), stbl(track));
|
2849 | };
|
2850 | moof = function moof(sequenceNumber, tracks) {
|
2851 | var trackFragments = [],
|
2852 | i = tracks.length;
|
2853 |
|
2854 | while (i--) {
|
2855 | trackFragments[i] = traf(tracks[i]);
|
2856 | }
|
2857 | return box.apply(null, [types.moof, mfhd(sequenceNumber)].concat(trackFragments));
|
2858 | };
|
2859 | |
2860 |
|
2861 |
|
2862 |
|
2863 |
|
2864 | moov = function moov(tracks) {
|
2865 | var i = tracks.length,
|
2866 | boxes = [];
|
2867 |
|
2868 | while (i--) {
|
2869 | boxes[i] = trak(tracks[i]);
|
2870 | }
|
2871 |
|
2872 | return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
|
2873 | };
|
2874 | mvex = function mvex(tracks) {
|
2875 | var i = tracks.length,
|
2876 | boxes = [];
|
2877 |
|
2878 | while (i--) {
|
2879 | boxes[i] = trex(tracks[i]);
|
2880 | }
|
2881 | return box.apply(null, [types.mvex].concat(boxes));
|
2882 | };
|
2883 | mvhd = function mvhd(duration) {
|
2884 | var bytes = new Uint8Array([0x00,
|
2885 | 0x00, 0x00, 0x00,
|
2886 | 0x00, 0x00, 0x00, 0x01,
|
2887 | 0x00, 0x00, 0x00, 0x02,
|
2888 | 0x00, 0x01, 0x5f, 0x90,
|
2889 | (duration & 0xFF000000) >> 24, (duration & 0xFF0000) >> 16, (duration & 0xFF00) >> 8, duration & 0xFF,
|
2890 | 0x00, 0x01, 0x00, 0x00,
|
2891 | 0x01, 0x00,
|
2892 | 0x00, 0x00,
|
2893 | 0x00, 0x00, 0x00, 0x00,
|
2894 | 0x00, 0x00, 0x00, 0x00,
|
2895 | 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,
|
2896 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2897 | 0xff, 0xff, 0xff, 0xff
|
2898 | ]);
|
2899 | return box(types.mvhd, bytes);
|
2900 | };
|
2901 |
|
2902 | sdtp = function sdtp(track) {
|
2903 | var samples = track.samples || [],
|
2904 | bytes = new Uint8Array(4 + samples.length),
|
2905 | flags,
|
2906 | i;
|
2907 |
|
2908 |
|
2909 |
|
2910 |
|
2911 | for (i = 0; i < samples.length; i++) {
|
2912 | flags = samples[i].flags;
|
2913 |
|
2914 | bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
|
2915 | }
|
2916 |
|
2917 | return box(types.sdtp, bytes);
|
2918 | };
|
2919 |
|
2920 | stbl = function stbl(track) {
|
2921 | return box(types.stbl, stsd(track), box(types.stts, STTS), box(types.stsc, STSC), box(types.stsz, STSZ), box(types.stco, STCO));
|
2922 | };
|
2923 |
|
2924 | (function () {
|
2925 | var videoSample, audioSample;
|
2926 |
|
2927 | stsd = function stsd(track) {
|
2928 |
|
2929 | return box(types.stsd, new Uint8Array([0x00,
|
2930 | 0x00, 0x00, 0x00,
|
2931 | 0x00, 0x00, 0x00, 0x01]), track.type === 'video' ? videoSample(track) : audioSample(track));
|
2932 | };
|
2933 |
|
2934 | videoSample = function videoSample(track) {
|
2935 | var sps = track.sps || [],
|
2936 | pps = track.pps || [],
|
2937 | sequenceParameterSets = [],
|
2938 | pictureParameterSets = [],
|
2939 | i,
|
2940 | avc1Box;
|
2941 |
|
2942 |
|
2943 | for (i = 0; i < sps.length; i++) {
|
2944 | sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
|
2945 | sequenceParameterSets.push(sps[i].byteLength & 0xFF);
|
2946 | sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i]));
|
2947 | }
|
2948 |
|
2949 |
|
2950 | for (i = 0; i < pps.length; i++) {
|
2951 | pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
|
2952 | pictureParameterSets.push(pps[i].byteLength & 0xFF);
|
2953 | pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
|
2954 | }
|
2955 |
|
2956 | avc1Box = [types.avc1, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2957 | 0x00, 0x01,
|
2958 | 0x00, 0x00,
|
2959 | 0x00, 0x00,
|
2960 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2961 | (track.width & 0xff00) >> 8, track.width & 0xff,
|
2962 | (track.height & 0xff00) >> 8, track.height & 0xff,
|
2963 | 0x00, 0x48, 0x00, 0x00,
|
2964 | 0x00, 0x48, 0x00, 0x00,
|
2965 | 0x00, 0x00, 0x00, 0x00,
|
2966 | 0x00, 0x01,
|
2967 | 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,
|
2968 | 0x00, 0x18,
|
2969 | 0x11, 0x11
|
2970 | ]), box(types.avcC, new Uint8Array([0x01,
|
2971 | track.profileIdc,
|
2972 | track.profileCompatibility,
|
2973 | track.levelIdc,
|
2974 | 0xff
|
2975 | ].concat([sps.length],
|
2976 | sequenceParameterSets,
|
2977 | [pps.length],
|
2978 | pictureParameterSets
|
2979 | ))), box(types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
|
2980 | 0x00, 0x2d, 0xc6, 0xc0,
|
2981 | 0x00, 0x2d, 0xc6, 0xc0
|
2982 | ]))];
|
2983 |
|
2984 | if (track.sarRatio) {
|
2985 | var hSpacing = track.sarRatio[0],
|
2986 | vSpacing = track.sarRatio[1];
|
2987 |
|
2988 | 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])));
|
2989 | }
|
2990 |
|
2991 | return box.apply(null, avc1Box);
|
2992 | };
|
2993 |
|
2994 | audioSample = function audioSample(track) {
|
2995 | return box(types.mp4a, new Uint8Array([
|
2996 |
|
2997 |
|
2998 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2999 | 0x00, 0x01,
|
3000 |
|
3001 |
|
3002 | 0x00, 0x00, 0x00, 0x00,
|
3003 | 0x00, 0x00, 0x00, 0x00,
|
3004 | (track.channelcount & 0xff00) >> 8, track.channelcount & 0xff,
|
3005 |
|
3006 | (track.samplesize & 0xff00) >> 8, track.samplesize & 0xff,
|
3007 | 0x00, 0x00,
|
3008 | 0x00, 0x00,
|
3009 |
|
3010 | (track.samplerate & 0xff00) >> 8, track.samplerate & 0xff, 0x00, 0x00
|
3011 |
|
3012 |
|
3013 | ]), esds(track));
|
3014 | };
|
3015 | })();
|
3016 |
|
3017 | tkhd = function tkhd(track) {
|
3018 | var result = new Uint8Array([0x00,
|
3019 | 0x00, 0x00, 0x07,
|
3020 | 0x00, 0x00, 0x00, 0x00,
|
3021 | 0x00, 0x00, 0x00, 0x00,
|
3022 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
3023 | 0x00, 0x00, 0x00, 0x00,
|
3024 | (track.duration & 0xFF000000) >> 24, (track.duration & 0xFF0000) >> 16, (track.duration & 0xFF00) >> 8, track.duration & 0xFF,
|
3025 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
3026 | 0x00, 0x00,
|
3027 | 0x00, 0x00,
|
3028 | 0x01, 0x00,
|
3029 | 0x00, 0x00,
|
3030 | 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,
|
3031 | (track.width & 0xFF00) >> 8, track.width & 0xFF, 0x00, 0x00,
|
3032 | (track.height & 0xFF00) >> 8, track.height & 0xFF, 0x00, 0x00
|
3033 | ]);
|
3034 |
|
3035 | return box(types.tkhd, result);
|
3036 | };
|
3037 |
|
3038 | |
3039 |
|
3040 |
|
3041 |
|
3042 | traf = function traf(track) {
|
3043 | var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable, dataOffset, upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
|
3044 |
|
3045 | trackFragmentHeader = box(types.tfhd, new Uint8Array([0x00,
|
3046 | 0x00, 0x00, 0x3a,
|
3047 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
3048 | 0x00, 0x00, 0x00, 0x01,
|
3049 | 0x00, 0x00, 0x00, 0x00,
|
3050 | 0x00, 0x00, 0x00, 0x00,
|
3051 | 0x00, 0x00, 0x00, 0x00
|
3052 | ]));
|
3053 |
|
3054 | upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / (UINT32_MAX + 1));
|
3055 | lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % (UINT32_MAX + 1));
|
3056 |
|
3057 | trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([0x01,
|
3058 | 0x00, 0x00, 0x00,
|
3059 |
|
3060 | upperWordBaseMediaDecodeTime >>> 24 & 0xFF, upperWordBaseMediaDecodeTime >>> 16 & 0xFF, upperWordBaseMediaDecodeTime >>> 8 & 0xFF, upperWordBaseMediaDecodeTime & 0xFF, lowerWordBaseMediaDecodeTime >>> 24 & 0xFF, lowerWordBaseMediaDecodeTime >>> 16 & 0xFF, lowerWordBaseMediaDecodeTime >>> 8 & 0xFF, lowerWordBaseMediaDecodeTime & 0xFF]));
|
3061 |
|
3062 |
|
3063 |
|
3064 |
|
3065 | dataOffset = 32 +
|
3066 | 20 +
|
3067 | 8 +
|
3068 | 16 +
|
3069 | 8 +
|
3070 | 8;
|
3071 |
|
3072 |
|
3073 | if (track.type === 'audio') {
|
3074 | trackFragmentRun = trun(track, dataOffset);
|
3075 | return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun);
|
3076 | }
|
3077 |
|
3078 |
|
3079 |
|
3080 |
|
3081 | sampleDependencyTable = sdtp(track);
|
3082 | trackFragmentRun = trun(track, sampleDependencyTable.length + dataOffset);
|
3083 | return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable);
|
3084 | };
|
3085 |
|
3086 | |
3087 |
|
3088 |
|
3089 |
|
3090 |
|
3091 | trak = function trak(track) {
|
3092 | track.duration = track.duration || 0xffffffff;
|
3093 | return box(types.trak, tkhd(track), mdia(track));
|
3094 | };
|
3095 |
|
3096 | trex = function trex(track) {
|
3097 | var result = new Uint8Array([0x00,
|
3098 | 0x00, 0x00, 0x00,
|
3099 | (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF,
|
3100 | 0x00, 0x00, 0x00, 0x01,
|
3101 | 0x00, 0x00, 0x00, 0x00,
|
3102 | 0x00, 0x00, 0x00, 0x00,
|
3103 | 0x00, 0x01, 0x00, 0x01
|
3104 | ]);
|
3105 |
|
3106 |
|
3107 |
|
3108 |
|
3109 | if (track.type !== 'video') {
|
3110 | result[result.length - 1] = 0x00;
|
3111 | }
|
3112 |
|
3113 | return box(types.trex, result);
|
3114 | };
|
3115 |
|
3116 | (function () {
|
3117 | var audioTrun, videoTrun, trunHeader;
|
3118 |
|
3119 |
|
3120 |
|
3121 |
|
3122 |
|
3123 | trunHeader = function trunHeader(samples, offset) {
|
3124 | var durationPresent = 0,
|
3125 | sizePresent = 0,
|
3126 | flagsPresent = 0,
|
3127 | compositionTimeOffset = 0;
|
3128 |
|
3129 |
|
3130 | if (samples.length) {
|
3131 | if (samples[0].duration !== undefined) {
|
3132 | durationPresent = 0x1;
|
3133 | }
|
3134 | if (samples[0].size !== undefined) {
|
3135 | sizePresent = 0x2;
|
3136 | }
|
3137 | if (samples[0].flags !== undefined) {
|
3138 | flagsPresent = 0x4;
|
3139 | }
|
3140 | if (samples[0].compositionTimeOffset !== undefined) {
|
3141 | compositionTimeOffset = 0x8;
|
3142 | }
|
3143 | }
|
3144 |
|
3145 | return [0x00,
|
3146 | 0x00, durationPresent | sizePresent | flagsPresent | compositionTimeOffset, 0x01,
|
3147 | (samples.length & 0xFF000000) >>> 24, (samples.length & 0xFF0000) >>> 16, (samples.length & 0xFF00) >>> 8, samples.length & 0xFF,
|
3148 | (offset & 0xFF000000) >>> 24, (offset & 0xFF0000) >>> 16, (offset & 0xFF00) >>> 8, offset & 0xFF
|
3149 | ];
|
3150 | };
|
3151 |
|
3152 | videoTrun = function videoTrun(track, offset) {
|
3153 | var bytes, samples, sample, i;
|
3154 |
|
3155 | samples = track.samples || [];
|
3156 | offset += 8 + 12 + 16 * samples.length;
|
3157 |
|
3158 | bytes = trunHeader(samples, offset);
|
3159 |
|
3160 | for (i = 0; i < samples.length; i++) {
|
3161 | sample = samples[i];
|
3162 | bytes = bytes.concat([(sample.duration & 0xFF000000) >>> 24, (sample.duration & 0xFF0000) >>> 16, (sample.duration & 0xFF00) >>> 8, sample.duration & 0xFF,
|
3163 | (sample.size & 0xFF000000) >>> 24, (sample.size & 0xFF0000) >>> 16, (sample.size & 0xFF00) >>> 8, sample.size & 0xFF,
|
3164 | sample.flags.isLeading << 2 | sample.flags.dependsOn, sample.flags.isDependedOn << 6 | sample.flags.hasRedundancy << 4 | sample.flags.paddingValue << 1 | sample.flags.isNonSyncSample, sample.flags.degradationPriority & 0xF0 << 8, sample.flags.degradationPriority & 0x0F,
|
3165 | (sample.compositionTimeOffset & 0xFF000000) >>> 24, (sample.compositionTimeOffset & 0xFF0000) >>> 16, (sample.compositionTimeOffset & 0xFF00) >>> 8, sample.compositionTimeOffset & 0xFF
|
3166 | ]);
|
3167 | }
|
3168 | return box(types.trun, new Uint8Array(bytes));
|
3169 | };
|
3170 |
|
3171 | audioTrun = function audioTrun(track, offset) {
|
3172 | var bytes, samples, sample, i;
|
3173 |
|
3174 | samples = track.samples || [];
|
3175 | offset += 8 + 12 + 8 * samples.length;
|
3176 |
|
3177 | bytes = trunHeader(samples, offset);
|
3178 |
|
3179 | for (i = 0; i < samples.length; i++) {
|
3180 | sample = samples[i];
|
3181 | bytes = bytes.concat([(sample.duration & 0xFF000000) >>> 24, (sample.duration & 0xFF0000) >>> 16, (sample.duration & 0xFF00) >>> 8, sample.duration & 0xFF,
|
3182 | (sample.size & 0xFF000000) >>> 24, (sample.size & 0xFF0000) >>> 16, (sample.size & 0xFF00) >>> 8, sample.size & 0xFF]);
|
3183 | }
|
3184 |
|
3185 | return box(types.trun, new Uint8Array(bytes));
|
3186 | };
|
3187 |
|
3188 | trun = function trun(track, offset) {
|
3189 | if (track.type === 'audio') {
|
3190 | return audioTrun(track, offset);
|
3191 | }
|
3192 |
|
3193 | return videoTrun(track, offset);
|
3194 | };
|
3195 | })();
|
3196 |
|
3197 | var mp4Generator = {
|
3198 | ftyp: ftyp,
|
3199 | mdat: mdat,
|
3200 | moof: moof,
|
3201 | moov: moov,
|
3202 | initSegment: function initSegment(tracks) {
|
3203 | var fileType = ftyp(),
|
3204 | movie = moov(tracks),
|
3205 | result;
|
3206 |
|
3207 | result = new Uint8Array(fileType.byteLength + movie.byteLength);
|
3208 | result.set(fileType);
|
3209 | result.set(movie, fileType.byteLength);
|
3210 | return result;
|
3211 | }
|
3212 | };
|
3213 |
|
3214 | |
3215 |
|
3216 |
|
3217 |
|
3218 |
|
3219 |
|
3220 |
|
3221 |
|
3222 |
|
3223 |
|
3224 | var groupNalsIntoFrames = function groupNalsIntoFrames(nalUnits) {
|
3225 | var i,
|
3226 | currentNal,
|
3227 | currentFrame = [],
|
3228 | frames = [];
|
3229 |
|
3230 |
|
3231 | frames.byteLength = 0;
|
3232 | frames.nalCount = 0;
|
3233 | frames.duration = 0;
|
3234 |
|
3235 | currentFrame.byteLength = 0;
|
3236 |
|
3237 | for (i = 0; i < nalUnits.length; i++) {
|
3238 | currentNal = nalUnits[i];
|
3239 |
|
3240 |
|
3241 | if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
|
3242 |
|
3243 |
|
3244 | if (currentFrame.length) {
|
3245 | currentFrame.duration = currentNal.dts - currentFrame.dts;
|
3246 |
|
3247 | frames.byteLength += currentFrame.byteLength;
|
3248 | frames.nalCount += currentFrame.length;
|
3249 | frames.duration += currentFrame.duration;
|
3250 | frames.push(currentFrame);
|
3251 | }
|
3252 | currentFrame = [currentNal];
|
3253 | currentFrame.byteLength = currentNal.data.byteLength;
|
3254 | currentFrame.pts = currentNal.pts;
|
3255 | currentFrame.dts = currentNal.dts;
|
3256 | } else {
|
3257 |
|
3258 | if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
|
3259 | currentFrame.keyFrame = true;
|
3260 | }
|
3261 | currentFrame.duration = currentNal.dts - currentFrame.dts;
|
3262 | currentFrame.byteLength += currentNal.data.byteLength;
|
3263 | currentFrame.push(currentNal);
|
3264 | }
|
3265 | }
|
3266 |
|
3267 |
|
3268 |
|
3269 | if (frames.length && (!currentFrame.duration || currentFrame.duration <= 0)) {
|
3270 | currentFrame.duration = frames[frames.length - 1].duration;
|
3271 | }
|
3272 |
|
3273 |
|
3274 |
|
3275 | frames.byteLength += currentFrame.byteLength;
|
3276 | frames.nalCount += currentFrame.length;
|
3277 | frames.duration += currentFrame.duration;
|
3278 |
|
3279 | frames.push(currentFrame);
|
3280 | return frames;
|
3281 | };
|
3282 |
|
3283 |
|
3284 |
|
3285 |
|
3286 |
|
3287 | var groupFramesIntoGops = function groupFramesIntoGops(frames) {
|
3288 | var i,
|
3289 | currentFrame,
|
3290 | currentGop = [],
|
3291 | gops = [];
|
3292 |
|
3293 |
|
3294 |
|
3295 | currentGop.byteLength = 0;
|
3296 | currentGop.nalCount = 0;
|
3297 | currentGop.duration = 0;
|
3298 | currentGop.pts = frames[0].pts;
|
3299 | currentGop.dts = frames[0].dts;
|
3300 |
|
3301 |
|
3302 | gops.byteLength = 0;
|
3303 | gops.nalCount = 0;
|
3304 | gops.duration = 0;
|
3305 | gops.pts = frames[0].pts;
|
3306 | gops.dts = frames[0].dts;
|
3307 |
|
3308 | for (i = 0; i < frames.length; i++) {
|
3309 | currentFrame = frames[i];
|
3310 |
|
3311 | if (currentFrame.keyFrame) {
|
3312 |
|
3313 |
|
3314 | if (currentGop.length) {
|
3315 | gops.push(currentGop);
|
3316 | gops.byteLength += currentGop.byteLength;
|
3317 | gops.nalCount += currentGop.nalCount;
|
3318 | gops.duration += currentGop.duration;
|
3319 | }
|
3320 |
|
3321 | currentGop = [currentFrame];
|
3322 | currentGop.nalCount = currentFrame.length;
|
3323 | currentGop.byteLength = currentFrame.byteLength;
|
3324 | currentGop.pts = currentFrame.pts;
|
3325 | currentGop.dts = currentFrame.dts;
|
3326 | currentGop.duration = currentFrame.duration;
|
3327 | } else {
|
3328 | currentGop.duration += currentFrame.duration;
|
3329 | currentGop.nalCount += currentFrame.length;
|
3330 | currentGop.byteLength += currentFrame.byteLength;
|
3331 | currentGop.push(currentFrame);
|
3332 | }
|
3333 | }
|
3334 |
|
3335 | if (gops.length && currentGop.duration <= 0) {
|
3336 | currentGop.duration = gops[gops.length - 1].duration;
|
3337 | }
|
3338 | gops.byteLength += currentGop.byteLength;
|
3339 | gops.nalCount += currentGop.nalCount;
|
3340 | gops.duration += currentGop.duration;
|
3341 |
|
3342 |
|
3343 | gops.push(currentGop);
|
3344 | return gops;
|
3345 | };
|
3346 |
|
3347 | |
3348 |
|
3349 |
|
3350 |
|
3351 |
|
3352 |
|
3353 |
|
3354 |
|
3355 |
|
3356 | var extendFirstKeyFrame = function extendFirstKeyFrame(gops) {
|
3357 | var currentGop;
|
3358 |
|
3359 | if (!gops[0][0].keyFrame && gops.length > 1) {
|
3360 |
|
3361 | currentGop = gops.shift();
|
3362 |
|
3363 | gops.byteLength -= currentGop.byteLength;
|
3364 | gops.nalCount -= currentGop.nalCount;
|
3365 |
|
3366 |
|
3367 |
|
3368 |
|
3369 | gops[0][0].dts = currentGop.dts;
|
3370 | gops[0][0].pts = currentGop.pts;
|
3371 | gops[0][0].duration += currentGop.duration;
|
3372 | }
|
3373 |
|
3374 | return gops;
|
3375 | };
|
3376 |
|
3377 | |
3378 |
|
3379 |
|
3380 |
|
3381 | var createDefaultSample = function createDefaultSample() {
|
3382 | return {
|
3383 | size: 0,
|
3384 | flags: {
|
3385 | isLeading: 0,
|
3386 | dependsOn: 1,
|
3387 | isDependedOn: 0,
|
3388 | hasRedundancy: 0,
|
3389 | degradationPriority: 0,
|
3390 | isNonSyncSample: 1
|
3391 | }
|
3392 | };
|
3393 | };
|
3394 |
|
3395 | |
3396 |
|
3397 |
|
3398 |
|
3399 |
|
3400 |
|
3401 |
|
3402 |
|
3403 | var sampleForFrame = function sampleForFrame(frame, dataOffset) {
|
3404 | var sample = createDefaultSample();
|
3405 |
|
3406 | sample.dataOffset = dataOffset;
|
3407 | sample.compositionTimeOffset = frame.pts - frame.dts;
|
3408 | sample.duration = frame.duration;
|
3409 | sample.size = 4 * frame.length;
|
3410 | sample.size += frame.byteLength;
|
3411 |
|
3412 | if (frame.keyFrame) {
|
3413 | sample.flags.dependsOn = 2;
|
3414 | sample.flags.isNonSyncSample = 0;
|
3415 | }
|
3416 |
|
3417 | return sample;
|
3418 | };
|
3419 |
|
3420 |
|
3421 | var generateSampleTable = function generateSampleTable(gops, baseDataOffset) {
|
3422 | var h,
|
3423 | i,
|
3424 | sample,
|
3425 | currentGop,
|
3426 | currentFrame,
|
3427 | dataOffset = baseDataOffset || 0,
|
3428 | samples = [];
|
3429 |
|
3430 | for (h = 0; h < gops.length; h++) {
|
3431 | currentGop = gops[h];
|
3432 |
|
3433 | for (i = 0; i < currentGop.length; i++) {
|
3434 | currentFrame = currentGop[i];
|
3435 |
|
3436 | sample = sampleForFrame(currentFrame, dataOffset);
|
3437 |
|
3438 | dataOffset += sample.size;
|
3439 |
|
3440 | samples.push(sample);
|
3441 | }
|
3442 | }
|
3443 | return samples;
|
3444 | };
|
3445 |
|
3446 |
|
3447 | var concatenateNalData = function concatenateNalData(gops) {
|
3448 | var h,
|
3449 | i,
|
3450 | j,
|
3451 | currentGop,
|
3452 | currentFrame,
|
3453 | currentNal,
|
3454 | dataOffset = 0,
|
3455 | nalsByteLength = gops.byteLength,
|
3456 | numberOfNals = gops.nalCount,
|
3457 | totalByteLength = nalsByteLength + 4 * numberOfNals,
|
3458 | data = new Uint8Array(totalByteLength),
|
3459 | view = new DataView(data.buffer);
|
3460 |
|
3461 |
|
3462 | for (h = 0; h < gops.length; h++) {
|
3463 | currentGop = gops[h];
|
3464 |
|
3465 |
|
3466 | for (i = 0; i < currentGop.length; i++) {
|
3467 | currentFrame = currentGop[i];
|
3468 |
|
3469 |
|
3470 | for (j = 0; j < currentFrame.length; j++) {
|
3471 | currentNal = currentFrame[j];
|
3472 |
|
3473 | view.setUint32(dataOffset, currentNal.data.byteLength);
|
3474 | dataOffset += 4;
|
3475 | data.set(currentNal.data, dataOffset);
|
3476 | dataOffset += currentNal.data.byteLength;
|
3477 | }
|
3478 | }
|
3479 | }
|
3480 | return data;
|
3481 | };
|
3482 |
|
3483 |
|
3484 | var generateSampleTableForFrame = function generateSampleTableForFrame(frame, baseDataOffset) {
|
3485 | var sample,
|
3486 | dataOffset = baseDataOffset || 0,
|
3487 | samples = [];
|
3488 |
|
3489 | sample = sampleForFrame(frame, dataOffset);
|
3490 | samples.push(sample);
|
3491 |
|
3492 | return samples;
|
3493 | };
|
3494 |
|
3495 |
|
3496 | var concatenateNalDataForFrame = function concatenateNalDataForFrame(frame) {
|
3497 | var i,
|
3498 | currentNal,
|
3499 | dataOffset = 0,
|
3500 | nalsByteLength = frame.byteLength,
|
3501 | numberOfNals = frame.length,
|
3502 | totalByteLength = nalsByteLength + 4 * numberOfNals,
|
3503 | data = new Uint8Array(totalByteLength),
|
3504 | view = new DataView(data.buffer);
|
3505 |
|
3506 |
|
3507 | for (i = 0; i < frame.length; i++) {
|
3508 | currentNal = frame[i];
|
3509 |
|
3510 | view.setUint32(dataOffset, currentNal.data.byteLength);
|
3511 | dataOffset += 4;
|
3512 | data.set(currentNal.data, dataOffset);
|
3513 | dataOffset += currentNal.data.byteLength;
|
3514 | }
|
3515 |
|
3516 | return data;
|
3517 | };
|
3518 |
|
3519 | var frameUtils = {
|
3520 | groupNalsIntoFrames: groupNalsIntoFrames,
|
3521 | groupFramesIntoGops: groupFramesIntoGops,
|
3522 | extendFirstKeyFrame: extendFirstKeyFrame,
|
3523 | generateSampleTable: generateSampleTable,
|
3524 | concatenateNalData: concatenateNalData,
|
3525 | generateSampleTableForFrame: generateSampleTableForFrame,
|
3526 | concatenateNalDataForFrame: concatenateNalDataForFrame
|
3527 | };
|
3528 |
|
3529 | |
3530 |
|
3531 |
|
3532 |
|
3533 |
|
3534 |
|
3535 | var highPrefix = [33, 16, 5, 32, 164, 27];
|
3536 | var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
|
3537 | var zeroFill = function zeroFill(count) {
|
3538 | var a = [];
|
3539 | while (count--) {
|
3540 | a.push(0);
|
3541 | }
|
3542 | return a;
|
3543 | };
|
3544 |
|
3545 | var makeTable = function makeTable(metaTable) {
|
3546 | return Object.keys(metaTable).reduce(function (obj, key) {
|
3547 | obj[key] = new Uint8Array(metaTable[key].reduce(function (arr, part) {
|
3548 | return arr.concat(part);
|
3549 | }, []));
|
3550 | return obj;
|
3551 | }, {});
|
3552 | };
|
3553 |
|
3554 |
|
3555 | var coneOfSilence = {
|
3556 | 96000: [highPrefix, [227, 64], zeroFill(154), [56]],
|
3557 | 88200: [highPrefix, [231], zeroFill(170), [56]],
|
3558 | 64000: [highPrefix, [248, 192], zeroFill(240), [56]],
|
3559 | 48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
|
3560 | 44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
|
3561 | 32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
|
3562 | 24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
|
3563 | 16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
|
3564 | 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]],
|
3565 | 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]],
|
3566 | 8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
|
3567 | };
|
3568 |
|
3569 | var silence = makeTable(coneOfSilence);
|
3570 |
|
3571 | |
3572 |
|
3573 |
|
3574 |
|
3575 |
|
3576 |
|
3577 | var ONE_SECOND_IN_TS = 90000,
|
3578 |
|
3579 |
|
3580 | secondsToVideoTs,
|
3581 | secondsToAudioTs,
|
3582 | videoTsToSeconds,
|
3583 | audioTsToSeconds,
|
3584 | audioTsToVideoTs,
|
3585 | videoTsToAudioTs,
|
3586 | metadataTsToSeconds;
|
3587 |
|
3588 | secondsToVideoTs = function secondsToVideoTs(seconds) {
|
3589 | return seconds * ONE_SECOND_IN_TS;
|
3590 | };
|
3591 |
|
3592 | secondsToAudioTs = function secondsToAudioTs(seconds, sampleRate) {
|
3593 | return seconds * sampleRate;
|
3594 | };
|
3595 |
|
3596 | videoTsToSeconds = function videoTsToSeconds(timestamp) {
|
3597 | return timestamp / ONE_SECOND_IN_TS;
|
3598 | };
|
3599 |
|
3600 | audioTsToSeconds = function audioTsToSeconds(timestamp, sampleRate) {
|
3601 | return timestamp / sampleRate;
|
3602 | };
|
3603 |
|
3604 | audioTsToVideoTs = function audioTsToVideoTs(timestamp, sampleRate) {
|
3605 | return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
|
3606 | };
|
3607 |
|
3608 | videoTsToAudioTs = function videoTsToAudioTs(timestamp, sampleRate) {
|
3609 | return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
|
3610 | };
|
3611 |
|
3612 | |
3613 |
|
3614 |
|
3615 |
|
3616 | metadataTsToSeconds = function metadataTsToSeconds(timestamp, timelineStartPts, keepOriginalTimestamps) {
|
3617 | return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
|
3618 | };
|
3619 |
|
3620 | var clock = {
|
3621 | ONE_SECOND_IN_TS: ONE_SECOND_IN_TS,
|
3622 | secondsToVideoTs: secondsToVideoTs,
|
3623 | secondsToAudioTs: secondsToAudioTs,
|
3624 | videoTsToSeconds: videoTsToSeconds,
|
3625 | audioTsToSeconds: audioTsToSeconds,
|
3626 | audioTsToVideoTs: audioTsToVideoTs,
|
3627 | videoTsToAudioTs: videoTsToAudioTs,
|
3628 | metadataTsToSeconds: metadataTsToSeconds
|
3629 | };
|
3630 |
|
3631 | |
3632 |
|
3633 |
|
3634 |
|
3635 |
|
3636 |
|
3637 |
|
3638 | |
3639 |
|
3640 |
|
3641 | var sumFrameByteLengths = function sumFrameByteLengths(array) {
|
3642 | var i,
|
3643 | currentObj,
|
3644 | sum = 0;
|
3645 |
|
3646 |
|
3647 | for (i = 0; i < array.length; i++) {
|
3648 | currentObj = array[i];
|
3649 | sum += currentObj.data.byteLength;
|
3650 | }
|
3651 |
|
3652 | return sum;
|
3653 | };
|
3654 |
|
3655 |
|
3656 |
|
3657 | var prefixWithSilence = function prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime) {
|
3658 | var baseMediaDecodeTimeTs,
|
3659 | frameDuration = 0,
|
3660 | audioGapDuration = 0,
|
3661 | audioFillFrameCount = 0,
|
3662 | audioFillDuration = 0,
|
3663 | silentFrame,
|
3664 | i,
|
3665 | firstFrame;
|
3666 |
|
3667 | if (!frames.length) {
|
3668 | return;
|
3669 | }
|
3670 |
|
3671 | baseMediaDecodeTimeTs = clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate);
|
3672 |
|
3673 | frameDuration = Math.ceil(clock.ONE_SECOND_IN_TS / (track.samplerate / 1024));
|
3674 |
|
3675 | if (audioAppendStartTs && videoBaseMediaDecodeTime) {
|
3676 |
|
3677 | audioGapDuration = baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime);
|
3678 |
|
3679 | audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
|
3680 | audioFillDuration = audioFillFrameCount * frameDuration;
|
3681 | }
|
3682 |
|
3683 |
|
3684 |
|
3685 | if (audioFillFrameCount < 1 || audioFillDuration > clock.ONE_SECOND_IN_TS / 2) {
|
3686 | return;
|
3687 | }
|
3688 |
|
3689 | silentFrame = silence[track.samplerate];
|
3690 |
|
3691 | if (!silentFrame) {
|
3692 |
|
3693 |
|
3694 | silentFrame = frames[0].data;
|
3695 | }
|
3696 |
|
3697 | for (i = 0; i < audioFillFrameCount; i++) {
|
3698 | firstFrame = frames[0];
|
3699 |
|
3700 | frames.splice(0, 0, {
|
3701 | data: silentFrame,
|
3702 | dts: firstFrame.dts - frameDuration,
|
3703 | pts: firstFrame.pts - frameDuration
|
3704 | });
|
3705 | }
|
3706 |
|
3707 | track.baseMediaDecodeTime -= Math.floor(clock.videoTsToAudioTs(audioFillDuration, track.samplerate));
|
3708 | };
|
3709 |
|
3710 |
|
3711 |
|
3712 |
|
3713 |
|
3714 | var trimAdtsFramesByEarliestDts = function trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts) {
|
3715 | if (track.minSegmentDts >= earliestAllowedDts) {
|
3716 | return adtsFrames;
|
3717 | }
|
3718 |
|
3719 |
|
3720 | track.minSegmentDts = Infinity;
|
3721 |
|
3722 | return adtsFrames.filter(function (currentFrame) {
|
3723 |
|
3724 | if (currentFrame.dts >= earliestAllowedDts) {
|
3725 | track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
|
3726 | track.minSegmentPts = track.minSegmentDts;
|
3727 | return true;
|
3728 | }
|
3729 |
|
3730 | return false;
|
3731 | });
|
3732 | };
|
3733 |
|
3734 |
|
3735 | var generateSampleTable$1 = function generateSampleTable(frames) {
|
3736 | var i,
|
3737 | currentFrame,
|
3738 | samples = [];
|
3739 |
|
3740 | for (i = 0; i < frames.length; i++) {
|
3741 | currentFrame = frames[i];
|
3742 | samples.push({
|
3743 | size: currentFrame.data.byteLength,
|
3744 | duration: 1024
|
3745 | });
|
3746 | }
|
3747 | return samples;
|
3748 | };
|
3749 |
|
3750 |
|
3751 | var concatenateFrameData = function concatenateFrameData(frames) {
|
3752 | var i,
|
3753 | currentFrame,
|
3754 | dataOffset = 0,
|
3755 | data = new Uint8Array(sumFrameByteLengths(frames));
|
3756 |
|
3757 | for (i = 0; i < frames.length; i++) {
|
3758 | currentFrame = frames[i];
|
3759 |
|
3760 | data.set(currentFrame.data, dataOffset);
|
3761 | dataOffset += currentFrame.data.byteLength;
|
3762 | }
|
3763 | return data;
|
3764 | };
|
3765 |
|
3766 | var audioFrameUtils = {
|
3767 | prefixWithSilence: prefixWithSilence,
|
3768 | trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
|
3769 | generateSampleTable: generateSampleTable$1,
|
3770 | concatenateFrameData: concatenateFrameData
|
3771 | };
|
3772 |
|
3773 | |
3774 |
|
3775 |
|
3776 |
|
3777 |
|
3778 |
|
3779 | var ONE_SECOND_IN_TS$1 = clock.ONE_SECOND_IN_TS;
|
3780 |
|
3781 | |
3782 |
|
3783 |
|
3784 |
|
3785 |
|
3786 | var collectDtsInfo = function collectDtsInfo(track, data) {
|
3787 | if (typeof data.pts === 'number') {
|
3788 | if (track.timelineStartInfo.pts === undefined) {
|
3789 | track.timelineStartInfo.pts = data.pts;
|
3790 | }
|
3791 |
|
3792 | if (track.minSegmentPts === undefined) {
|
3793 | track.minSegmentPts = data.pts;
|
3794 | } else {
|
3795 | track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
|
3796 | }
|
3797 |
|
3798 | if (track.maxSegmentPts === undefined) {
|
3799 | track.maxSegmentPts = data.pts;
|
3800 | } else {
|
3801 | track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
|
3802 | }
|
3803 | }
|
3804 |
|
3805 | if (typeof data.dts === 'number') {
|
3806 | if (track.timelineStartInfo.dts === undefined) {
|
3807 | track.timelineStartInfo.dts = data.dts;
|
3808 | }
|
3809 |
|
3810 | if (track.minSegmentDts === undefined) {
|
3811 | track.minSegmentDts = data.dts;
|
3812 | } else {
|
3813 | track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
|
3814 | }
|
3815 |
|
3816 | if (track.maxSegmentDts === undefined) {
|
3817 | track.maxSegmentDts = data.dts;
|
3818 | } else {
|
3819 | track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
|
3820 | }
|
3821 | }
|
3822 | };
|
3823 |
|
3824 | |
3825 |
|
3826 |
|
3827 |
|
3828 | var clearDtsInfo = function clearDtsInfo(track) {
|
3829 | delete track.minSegmentDts;
|
3830 | delete track.maxSegmentDts;
|
3831 | delete track.minSegmentPts;
|
3832 | delete track.maxSegmentPts;
|
3833 | };
|
3834 |
|
3835 | |
3836 |
|
3837 |
|
3838 |
|
3839 |
|
3840 |
|
3841 |
|
3842 |
|
3843 | var calculateTrackBaseMediaDecodeTime = function calculateTrackBaseMediaDecodeTime(track, keepOriginalTimestamps) {
|
3844 | var baseMediaDecodeTime,
|
3845 | scale,
|
3846 | minSegmentDts = track.minSegmentDts;
|
3847 |
|
3848 |
|
3849 | if (!keepOriginalTimestamps) {
|
3850 | minSegmentDts -= track.timelineStartInfo.dts;
|
3851 | }
|
3852 |
|
3853 |
|
3854 |
|
3855 | baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime;
|
3856 |
|
3857 |
|
3858 | baseMediaDecodeTime += minSegmentDts;
|
3859 |
|
3860 |
|
3861 | baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
|
3862 |
|
3863 | if (track.type === 'audio') {
|
3864 |
|
3865 |
|
3866 | scale = track.samplerate / ONE_SECOND_IN_TS$1;
|
3867 | baseMediaDecodeTime *= scale;
|
3868 | baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
|
3869 | }
|
3870 |
|
3871 | return baseMediaDecodeTime;
|
3872 | };
|
3873 |
|
3874 | var trackDecodeInfo = {
|
3875 | clearDtsInfo: clearDtsInfo,
|
3876 | calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
|
3877 | collectDtsInfo: collectDtsInfo
|
3878 | };
|
3879 |
|
3880 | |
3881 |
|
3882 |
|
3883 |
|
3884 |
|
3885 |
|
3886 |
|
3887 |
|
3888 |
|
3889 |
|
3890 |
|
3891 |
|
3892 |
|
3893 |
|
3894 |
|
3895 |
|
3896 |
|
3897 |
|
3898 | var USER_DATA_REGISTERED_ITU_T_T35 = 4,
|
3899 | RBSP_TRAILING_BITS = 128;
|
3900 |
|
3901 | |
3902 |
|
3903 |
|
3904 |
|
3905 |
|
3906 |
|
3907 |
|
3908 |
|
3909 | var parseSei = function parseSei(bytes) {
|
3910 | var i = 0,
|
3911 | result = {
|
3912 | payloadType: -1,
|
3913 | payloadSize: 0
|
3914 | },
|
3915 | payloadType = 0,
|
3916 | payloadSize = 0;
|
3917 |
|
3918 |
|
3919 | while (i < bytes.byteLength) {
|
3920 |
|
3921 | if (bytes[i] === RBSP_TRAILING_BITS) {
|
3922 | break;
|
3923 | }
|
3924 |
|
3925 |
|
3926 | while (bytes[i] === 0xFF) {
|
3927 | payloadType += 255;
|
3928 | i++;
|
3929 | }
|
3930 | payloadType += bytes[i++];
|
3931 |
|
3932 |
|
3933 | while (bytes[i] === 0xFF) {
|
3934 | payloadSize += 255;
|
3935 | i++;
|
3936 | }
|
3937 | payloadSize += bytes[i++];
|
3938 |
|
3939 |
|
3940 |
|
3941 | if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
|
3942 | result.payloadType = payloadType;
|
3943 | result.payloadSize = payloadSize;
|
3944 | result.payload = bytes.subarray(i, i + payloadSize);
|
3945 | break;
|
3946 | }
|
3947 |
|
3948 |
|
3949 | i += payloadSize;
|
3950 | payloadType = 0;
|
3951 | payloadSize = 0;
|
3952 | }
|
3953 |
|
3954 | return result;
|
3955 | };
|
3956 |
|
3957 |
|
3958 | var parseUserData = function parseUserData(sei) {
|
3959 |
|
3960 |
|
3961 | if (sei.payload[0] !== 181) {
|
3962 | return null;
|
3963 | }
|
3964 |
|
3965 |
|
3966 | if ((sei.payload[1] << 8 | sei.payload[2]) !== 49) {
|
3967 | return null;
|
3968 | }
|
3969 |
|
3970 |
|
3971 | if (String.fromCharCode(sei.payload[3], sei.payload[4], sei.payload[5], sei.payload[6]) !== 'GA94') {
|
3972 | return null;
|
3973 | }
|
3974 |
|
3975 |
|
3976 | if (sei.payload[7] !== 0x03) {
|
3977 | return null;
|
3978 | }
|
3979 |
|
3980 |
|
3981 |
|
3982 | return sei.payload.subarray(8, sei.payload.length - 1);
|
3983 | };
|
3984 |
|
3985 |
|
3986 | var parseCaptionPackets = function parseCaptionPackets(pts, userData) {
|
3987 | var results = [],
|
3988 | i,
|
3989 | count,
|
3990 | offset,
|
3991 | data;
|
3992 |
|
3993 |
|
3994 | if (!(userData[0] & 0x40)) {
|
3995 | return results;
|
3996 | }
|
3997 |
|
3998 |
|
3999 | count = userData[0] & 0x1f;
|
4000 | for (i = 0; i < count; i++) {
|
4001 | offset = i * 3;
|
4002 | data = {
|
4003 | type: userData[offset + 2] & 0x03,
|
4004 | pts: pts
|
4005 | };
|
4006 |
|
4007 |
|
4008 | if (userData[offset + 2] & 0x04) {
|
4009 | data.ccData = userData[offset + 3] << 8 | userData[offset + 4];
|
4010 | results.push(data);
|
4011 | }
|
4012 | }
|
4013 | return results;
|
4014 | };
|
4015 |
|
4016 | var discardEmulationPreventionBytes = function discardEmulationPreventionBytes(data) {
|
4017 | var length = data.byteLength,
|
4018 | emulationPreventionBytesPositions = [],
|
4019 | i = 1,
|
4020 | newLength,
|
4021 | newData;
|
4022 |
|
4023 |
|
4024 | while (i < length - 2) {
|
4025 | if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
|
4026 | emulationPreventionBytesPositions.push(i + 2);
|
4027 | i += 2;
|
4028 | } else {
|
4029 | i++;
|
4030 | }
|
4031 | }
|
4032 |
|
4033 |
|
4034 |
|
4035 | if (emulationPreventionBytesPositions.length === 0) {
|
4036 | return data;
|
4037 | }
|
4038 |
|
4039 |
|
4040 | newLength = length - emulationPreventionBytesPositions.length;
|
4041 | newData = new Uint8Array(newLength);
|
4042 | var sourceIndex = 0;
|
4043 |
|
4044 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
4045 | if (sourceIndex === emulationPreventionBytesPositions[0]) {
|
4046 |
|
4047 | sourceIndex++;
|
4048 |
|
4049 | emulationPreventionBytesPositions.shift();
|
4050 | }
|
4051 | newData[i] = data[sourceIndex];
|
4052 | }
|
4053 |
|
4054 | return newData;
|
4055 | };
|
4056 |
|
4057 |
|
4058 | var captionPacketParser = {
|
4059 | parseSei: parseSei,
|
4060 | parseUserData: parseUserData,
|
4061 | parseCaptionPackets: parseCaptionPackets,
|
4062 | discardEmulationPreventionBytes: discardEmulationPreventionBytes,
|
4063 | USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
|
4064 | };
|
4065 |
|
4066 |
|
4067 |
|
4068 |
|
4069 |
|
4070 |
|
4071 | var CaptionStream = function CaptionStream() {
|
4072 |
|
4073 | CaptionStream.prototype.init.call(this);
|
4074 |
|
4075 | this.captionPackets_ = [];
|
4076 |
|
4077 | this.ccStreams_ = [new Cea608Stream(0, 0),
|
4078 | new Cea608Stream(0, 1),
|
4079 | new Cea608Stream(1, 0),
|
4080 | new Cea608Stream(1, 1)
|
4081 | ];
|
4082 |
|
4083 | this.reset();
|
4084 |
|
4085 |
|
4086 | this.ccStreams_.forEach(function (cc) {
|
4087 | cc.on('data', this.trigger.bind(this, 'data'));
|
4088 | cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
|
4089 | cc.on('done', this.trigger.bind(this, 'done'));
|
4090 | }, this);
|
4091 | };
|
4092 |
|
4093 | CaptionStream.prototype = new stream();
|
4094 | CaptionStream.prototype.push = function (event) {
|
4095 | var sei, userData, newCaptionPackets;
|
4096 |
|
4097 |
|
4098 | if (event.nalUnitType !== 'sei_rbsp') {
|
4099 | return;
|
4100 | }
|
4101 |
|
4102 |
|
4103 | sei = captionPacketParser.parseSei(event.escapedRBSP);
|
4104 |
|
4105 |
|
4106 | if (sei.payloadType !== captionPacketParser.USER_DATA_REGISTERED_ITU_T_T35) {
|
4107 | return;
|
4108 | }
|
4109 |
|
4110 |
|
4111 | userData = captionPacketParser.parseUserData(sei);
|
4112 |
|
4113 |
|
4114 | if (!userData) {
|
4115 | return;
|
4116 | }
|
4117 |
|
4118 |
|
4119 |
|
4120 |
|
4121 |
|
4122 |
|
4123 |
|
4124 |
|
4125 |
|
4126 | if (event.dts < this.latestDts_) {
|
4127 |
|
4128 | this.ignoreNextEqualDts_ = true;
|
4129 | return;
|
4130 | } else if (event.dts === this.latestDts_ && this.ignoreNextEqualDts_) {
|
4131 | this.numSameDts_--;
|
4132 | if (!this.numSameDts_) {
|
4133 |
|
4134 | this.ignoreNextEqualDts_ = false;
|
4135 | }
|
4136 | return;
|
4137 | }
|
4138 |
|
4139 |
|
4140 | newCaptionPackets = captionPacketParser.parseCaptionPackets(event.pts, userData);
|
4141 | this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
|
4142 | if (this.latestDts_ !== event.dts) {
|
4143 | this.numSameDts_ = 0;
|
4144 | }
|
4145 | this.numSameDts_++;
|
4146 | this.latestDts_ = event.dts;
|
4147 | };
|
4148 |
|
4149 | CaptionStream.prototype.flushCCStreams = function (flushType) {
|
4150 | this.ccStreams_.forEach(function (cc) {
|
4151 | return flushType === 'flush' ? cc.flush() : cc.partialFlush();
|
4152 | }, this);
|
4153 | };
|
4154 |
|
4155 | CaptionStream.prototype.flushStream = function (flushType) {
|
4156 |
|
4157 | if (!this.captionPackets_.length) {
|
4158 | this.flushCCStreams(flushType);
|
4159 | return;
|
4160 | }
|
4161 |
|
4162 |
|
4163 |
|
4164 | this.captionPackets_.forEach(function (elem, idx) {
|
4165 | elem.presortIndex = idx;
|
4166 | });
|
4167 |
|
4168 |
|
4169 | this.captionPackets_.sort(function (a, b) {
|
4170 | if (a.pts === b.pts) {
|
4171 | return a.presortIndex - b.presortIndex;
|
4172 | }
|
4173 | return a.pts - b.pts;
|
4174 | });
|
4175 |
|
4176 | this.captionPackets_.forEach(function (packet) {
|
4177 | if (packet.type < 2) {
|
4178 |
|
4179 | this.dispatchCea608Packet(packet);
|
4180 | }
|
4181 |
|
4182 |
|
4183 | }, this);
|
4184 |
|
4185 | this.captionPackets_.length = 0;
|
4186 | this.flushCCStreams(flushType);
|
4187 | };
|
4188 |
|
4189 | CaptionStream.prototype.flush = function () {
|
4190 | return this.flushStream('flush');
|
4191 | };
|
4192 |
|
4193 |
|
4194 | CaptionStream.prototype.partialFlush = function () {
|
4195 | return this.flushStream('partialFlush');
|
4196 | };
|
4197 |
|
4198 | CaptionStream.prototype.reset = function () {
|
4199 | this.latestDts_ = null;
|
4200 | this.ignoreNextEqualDts_ = false;
|
4201 | this.numSameDts_ = 0;
|
4202 | this.activeCea608Channel_ = [null, null];
|
4203 | this.ccStreams_.forEach(function (ccStream) {
|
4204 | ccStream.reset();
|
4205 | });
|
4206 | };
|
4207 |
|
4208 |
|
4209 | |
4210 |
|
4211 |
|
4212 |
|
4213 |
|
4214 |
|
4215 |
|
4216 |
|
4217 |
|
4218 |
|
4219 | CaptionStream.prototype.dispatchCea608Packet = function (packet) {
|
4220 |
|
4221 | if (this.setsTextOrXDSActive(packet)) {
|
4222 | this.activeCea608Channel_[packet.type] = null;
|
4223 | } else if (this.setsChannel1Active(packet)) {
|
4224 | this.activeCea608Channel_[packet.type] = 0;
|
4225 | } else if (this.setsChannel2Active(packet)) {
|
4226 | this.activeCea608Channel_[packet.type] = 1;
|
4227 | }
|
4228 | if (this.activeCea608Channel_[packet.type] === null) {
|
4229 |
|
4230 |
|
4231 |
|
4232 | return;
|
4233 | }
|
4234 | this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
|
4235 | };
|
4236 |
|
4237 | CaptionStream.prototype.setsChannel1Active = function (packet) {
|
4238 | return (packet.ccData & 0x7800) === 0x1000;
|
4239 | };
|
4240 | CaptionStream.prototype.setsChannel2Active = function (packet) {
|
4241 | return (packet.ccData & 0x7800) === 0x1800;
|
4242 | };
|
4243 | CaptionStream.prototype.setsTextOrXDSActive = function (packet) {
|
4244 | return (packet.ccData & 0x7100) === 0x0100 || (packet.ccData & 0x78fe) === 0x102a || (packet.ccData & 0x78fe) === 0x182a;
|
4245 | };
|
4246 |
|
4247 |
|
4248 |
|
4249 |
|
4250 |
|
4251 |
|
4252 |
|
4253 |
|
4254 |
|
4255 |
|
4256 |
|
4257 |
|
4258 | var CHARACTER_TRANSLATION = {
|
4259 | 0x2a: 0xe1,
|
4260 | 0x5c: 0xe9,
|
4261 | 0x5e: 0xed,
|
4262 | 0x5f: 0xf3,
|
4263 | 0x60: 0xfa,
|
4264 | 0x7b: 0xe7,
|
4265 | 0x7c: 0xf7,
|
4266 | 0x7d: 0xd1,
|
4267 | 0x7e: 0xf1,
|
4268 | 0x7f: 0x2588,
|
4269 | 0x0130: 0xae,
|
4270 | 0x0131: 0xb0,
|
4271 | 0x0132: 0xbd,
|
4272 | 0x0133: 0xbf,
|
4273 | 0x0134: 0x2122,
|
4274 | 0x0135: 0xa2,
|
4275 | 0x0136: 0xa3,
|
4276 | 0x0137: 0x266a,
|
4277 | 0x0138: 0xe0,
|
4278 | 0x0139: 0xa0,
|
4279 | 0x013a: 0xe8,
|
4280 | 0x013b: 0xe2,
|
4281 | 0x013c: 0xea,
|
4282 | 0x013d: 0xee,
|
4283 | 0x013e: 0xf4,
|
4284 | 0x013f: 0xfb,
|
4285 | 0x0220: 0xc1,
|
4286 | 0x0221: 0xc9,
|
4287 | 0x0222: 0xd3,
|
4288 | 0x0223: 0xda,
|
4289 | 0x0224: 0xdc,
|
4290 | 0x0225: 0xfc,
|
4291 | 0x0226: 0x2018,
|
4292 | 0x0227: 0xa1,
|
4293 | 0x0228: 0x2a,
|
4294 | 0x0229: 0x27,
|
4295 | 0x022a: 0x2014,
|
4296 | 0x022b: 0xa9,
|
4297 | 0x022c: 0x2120,
|
4298 | 0x022d: 0x2022,
|
4299 | 0x022e: 0x201c,
|
4300 | 0x022f: 0x201d,
|
4301 | 0x0230: 0xc0,
|
4302 | 0x0231: 0xc2,
|
4303 | 0x0232: 0xc7,
|
4304 | 0x0233: 0xc8,
|
4305 | 0x0234: 0xca,
|
4306 | 0x0235: 0xcb,
|
4307 | 0x0236: 0xeb,
|
4308 | 0x0237: 0xce,
|
4309 | 0x0238: 0xcf,
|
4310 | 0x0239: 0xef,
|
4311 | 0x023a: 0xd4,
|
4312 | 0x023b: 0xd9,
|
4313 | 0x023c: 0xf9,
|
4314 | 0x023d: 0xdb,
|
4315 | 0x023e: 0xab,
|
4316 | 0x023f: 0xbb,
|
4317 | 0x0320: 0xc3,
|
4318 | 0x0321: 0xe3,
|
4319 | 0x0322: 0xcd,
|
4320 | 0x0323: 0xcc,
|
4321 | 0x0324: 0xec,
|
4322 | 0x0325: 0xd2,
|
4323 | 0x0326: 0xf2,
|
4324 | 0x0327: 0xd5,
|
4325 | 0x0328: 0xf5,
|
4326 | 0x0329: 0x7b,
|
4327 | 0x032a: 0x7d,
|
4328 | 0x032b: 0x5c,
|
4329 | 0x032c: 0x5e,
|
4330 | 0x032d: 0x5f,
|
4331 | 0x032e: 0x7c,
|
4332 | 0x032f: 0x7e,
|
4333 | 0x0330: 0xc4,
|
4334 | 0x0331: 0xe4,
|
4335 | 0x0332: 0xd6,
|
4336 | 0x0333: 0xf6,
|
4337 | 0x0334: 0xdf,
|
4338 | 0x0335: 0xa5,
|
4339 | 0x0336: 0xa4,
|
4340 | 0x0337: 0x2502,
|
4341 | 0x0338: 0xc5,
|
4342 | 0x0339: 0xe5,
|
4343 | 0x033a: 0xd8,
|
4344 | 0x033b: 0xf8,
|
4345 | 0x033c: 0x250c,
|
4346 | 0x033d: 0x2510,
|
4347 | 0x033e: 0x2514,
|
4348 | 0x033f: 0x2518
|
4349 | };
|
4350 |
|
4351 | var getCharFromCode = function getCharFromCode(code) {
|
4352 | if (code === null) {
|
4353 | return '';
|
4354 | }
|
4355 | code = CHARACTER_TRANSLATION[code] || code;
|
4356 | return String.fromCharCode(code);
|
4357 | };
|
4358 |
|
4359 |
|
4360 | var BOTTOM_ROW = 14;
|
4361 |
|
4362 |
|
4363 |
|
4364 | var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620, 0x1700, 0x1720, 0x1000, 0x1300, 0x1320, 0x1400, 0x1420];
|
4365 |
|
4366 |
|
4367 |
|
4368 | var createDisplayBuffer = function createDisplayBuffer() {
|
4369 | var result = [],
|
4370 | i = BOTTOM_ROW + 1;
|
4371 | while (i--) {
|
4372 | result.push('');
|
4373 | }
|
4374 | return result;
|
4375 | };
|
4376 |
|
4377 | var Cea608Stream = function Cea608Stream(field, dataChannel) {
|
4378 | Cea608Stream.prototype.init.call(this);
|
4379 |
|
4380 | this.field_ = field || 0;
|
4381 | this.dataChannel_ = dataChannel || 0;
|
4382 |
|
4383 | this.name_ = 'CC' + ((this.field_ << 1 | this.dataChannel_) + 1);
|
4384 |
|
4385 | this.setConstants();
|
4386 | this.reset();
|
4387 |
|
4388 | this.push = function (packet) {
|
4389 | var data, swap, char0, char1, text;
|
4390 |
|
4391 | data = packet.ccData & 0x7f7f;
|
4392 |
|
4393 |
|
4394 | if (data === this.lastControlCode_) {
|
4395 | this.lastControlCode_ = null;
|
4396 | return;
|
4397 | }
|
4398 |
|
4399 |
|
4400 | if ((data & 0xf000) === 0x1000) {
|
4401 | this.lastControlCode_ = data;
|
4402 | } else if (data !== this.PADDING_) {
|
4403 | this.lastControlCode_ = null;
|
4404 | }
|
4405 |
|
4406 | char0 = data >>> 8;
|
4407 | char1 = data & 0xff;
|
4408 |
|
4409 | if (data === this.PADDING_) {
|
4410 | return;
|
4411 | } else if (data === this.RESUME_CAPTION_LOADING_) {
|
4412 | this.mode_ = 'popOn';
|
4413 | } else if (data === this.END_OF_CAPTION_) {
|
4414 |
|
4415 |
|
4416 |
|
4417 |
|
4418 | this.mode_ = 'popOn';
|
4419 | this.clearFormatting(packet.pts);
|
4420 |
|
4421 | this.flushDisplayed(packet.pts);
|
4422 |
|
4423 |
|
4424 | swap = this.displayed_;
|
4425 | this.displayed_ = this.nonDisplayed_;
|
4426 | this.nonDisplayed_ = swap;
|
4427 |
|
4428 |
|
4429 | this.startPts_ = packet.pts;
|
4430 | } else if (data === this.ROLL_UP_2_ROWS_) {
|
4431 | this.rollUpRows_ = 2;
|
4432 | this.setRollUp(packet.pts);
|
4433 | } else if (data === this.ROLL_UP_3_ROWS_) {
|
4434 | this.rollUpRows_ = 3;
|
4435 | this.setRollUp(packet.pts);
|
4436 | } else if (data === this.ROLL_UP_4_ROWS_) {
|
4437 | this.rollUpRows_ = 4;
|
4438 | this.setRollUp(packet.pts);
|
4439 | } else if (data === this.CARRIAGE_RETURN_) {
|
4440 | this.clearFormatting(packet.pts);
|
4441 | this.flushDisplayed(packet.pts);
|
4442 | this.shiftRowsUp_();
|
4443 | this.startPts_ = packet.pts;
|
4444 | } else if (data === this.BACKSPACE_) {
|
4445 | if (this.mode_ === 'popOn') {
|
4446 | this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
|
4447 | } else {
|
4448 | this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
|
4449 | }
|
4450 | } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
|
4451 | this.flushDisplayed(packet.pts);
|
4452 | this.displayed_ = createDisplayBuffer();
|
4453 | } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
|
4454 | this.nonDisplayed_ = createDisplayBuffer();
|
4455 | } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
|
4456 | if (this.mode_ !== 'paintOn') {
|
4457 |
|
4458 |
|
4459 | this.flushDisplayed(packet.pts);
|
4460 | this.displayed_ = createDisplayBuffer();
|
4461 | }
|
4462 | this.mode_ = 'paintOn';
|
4463 | this.startPts_ = packet.pts;
|
4464 |
|
4465 |
|
4466 | } else if (this.isSpecialCharacter(char0, char1)) {
|
4467 |
|
4468 |
|
4469 |
|
4470 |
|
4471 | char0 = (char0 & 0x03) << 8;
|
4472 | text = getCharFromCode(char0 | char1);
|
4473 | this[this.mode_](packet.pts, text);
|
4474 | this.column_++;
|
4475 |
|
4476 |
|
4477 | } else if (this.isExtCharacter(char0, char1)) {
|
4478 |
|
4479 |
|
4480 |
|
4481 |
|
4482 |
|
4483 |
|
4484 | if (this.mode_ === 'popOn') {
|
4485 | this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
|
4486 | } else {
|
4487 | this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
|
4488 | }
|
4489 |
|
4490 |
|
4491 |
|
4492 |
|
4493 |
|
4494 | char0 = (char0 & 0x03) << 8;
|
4495 | text = getCharFromCode(char0 | char1);
|
4496 | this[this.mode_](packet.pts, text);
|
4497 | this.column_++;
|
4498 |
|
4499 |
|
4500 | } else if (this.isMidRowCode(char0, char1)) {
|
4501 |
|
4502 | this.clearFormatting(packet.pts);
|
4503 |
|
4504 |
|
4505 |
|
4506 | this[this.mode_](packet.pts, ' ');
|
4507 | this.column_++;
|
4508 |
|
4509 | if ((char1 & 0xe) === 0xe) {
|
4510 | this.addFormatting(packet.pts, ['i']);
|
4511 | }
|
4512 |
|
4513 | if ((char1 & 0x1) === 0x1) {
|
4514 | this.addFormatting(packet.pts, ['u']);
|
4515 | }
|
4516 |
|
4517 |
|
4518 | } else if (this.isOffsetControlCode(char0, char1)) {
|
4519 |
|
4520 |
|
4521 |
|
4522 |
|
4523 | this.column_ += char1 & 0x03;
|
4524 |
|
4525 |
|
4526 | } else if (this.isPAC(char0, char1)) {
|
4527 |
|
4528 |
|
4529 |
|
4530 | var row = ROWS.indexOf(data & 0x1f20);
|
4531 |
|
4532 |
|
4533 | if (this.mode_ === 'rollUp') {
|
4534 |
|
4535 |
|
4536 |
|
4537 | if (row - this.rollUpRows_ + 1 < 0) {
|
4538 | row = this.rollUpRows_ - 1;
|
4539 | }
|
4540 |
|
4541 | this.setRollUp(packet.pts, row);
|
4542 | }
|
4543 |
|
4544 | if (row !== this.row_) {
|
4545 |
|
4546 | this.clearFormatting(packet.pts);
|
4547 | this.row_ = row;
|
4548 | }
|
4549 |
|
4550 |
|
4551 | if (char1 & 0x1 && this.formatting_.indexOf('u') === -1) {
|
4552 | this.addFormatting(packet.pts, ['u']);
|
4553 | }
|
4554 |
|
4555 | if ((data & 0x10) === 0x10) {
|
4556 |
|
4557 |
|
4558 |
|
4559 |
|
4560 | this.column_ = ((data & 0xe) >> 1) * 4;
|
4561 | }
|
4562 |
|
4563 | if (this.isColorPAC(char1)) {
|
4564 |
|
4565 |
|
4566 |
|
4567 |
|
4568 | if ((char1 & 0xe) === 0xe) {
|
4569 | this.addFormatting(packet.pts, ['i']);
|
4570 | }
|
4571 | }
|
4572 |
|
4573 |
|
4574 | } else if (this.isNormalChar(char0)) {
|
4575 | if (char1 === 0x00) {
|
4576 | char1 = null;
|
4577 | }
|
4578 | text = getCharFromCode(char0);
|
4579 | text += getCharFromCode(char1);
|
4580 | this[this.mode_](packet.pts, text);
|
4581 | this.column_ += text.length;
|
4582 | }
|
4583 | };
|
4584 | };
|
4585 | Cea608Stream.prototype = new stream();
|
4586 |
|
4587 |
|
4588 | Cea608Stream.prototype.flushDisplayed = function (pts) {
|
4589 | var content = this.displayed_
|
4590 |
|
4591 | .map(function (row) {
|
4592 | try {
|
4593 | return row.trim();
|
4594 | } catch (e) {
|
4595 |
|
4596 |
|
4597 |
|
4598 |
|
4599 | console.error('Skipping malformed caption.');
|
4600 | return '';
|
4601 | }
|
4602 | })
|
4603 |
|
4604 | .join('\n')
|
4605 |
|
4606 | .replace(/^\n+|\n+$/g, '');
|
4607 |
|
4608 | if (content.length) {
|
4609 | this.trigger('data', {
|
4610 | startPts: this.startPts_,
|
4611 | endPts: pts,
|
4612 | text: content,
|
4613 | stream: this.name_
|
4614 | });
|
4615 | }
|
4616 | };
|
4617 |
|
4618 | |
4619 |
|
4620 |
|
4621 | Cea608Stream.prototype.reset = function () {
|
4622 | this.mode_ = 'popOn';
|
4623 |
|
4624 |
|
4625 |
|
4626 |
|
4627 | this.topRow_ = 0;
|
4628 | this.startPts_ = 0;
|
4629 | this.displayed_ = createDisplayBuffer();
|
4630 | this.nonDisplayed_ = createDisplayBuffer();
|
4631 | this.lastControlCode_ = null;
|
4632 |
|
4633 |
|
4634 | this.column_ = 0;
|
4635 | this.row_ = BOTTOM_ROW;
|
4636 | this.rollUpRows_ = 2;
|
4637 |
|
4638 |
|
4639 | this.formatting_ = [];
|
4640 | };
|
4641 |
|
4642 | |
4643 |
|
4644 |
|
4645 | Cea608Stream.prototype.setConstants = function () {
|
4646 |
|
4647 |
|
4648 |
|
4649 |
|
4650 |
|
4651 |
|
4652 |
|
4653 |
|
4654 |
|
4655 |
|
4656 |
|
4657 |
|
4658 | if (this.dataChannel_ === 0) {
|
4659 | this.BASE_ = 0x10;
|
4660 | this.EXT_ = 0x11;
|
4661 | this.CONTROL_ = (0x14 | this.field_) << 8;
|
4662 | this.OFFSET_ = 0x17;
|
4663 | } else if (this.dataChannel_ === 1) {
|
4664 | this.BASE_ = 0x18;
|
4665 | this.EXT_ = 0x19;
|
4666 | this.CONTROL_ = (0x1c | this.field_) << 8;
|
4667 | this.OFFSET_ = 0x1f;
|
4668 | }
|
4669 |
|
4670 |
|
4671 |
|
4672 |
|
4673 |
|
4674 | this.PADDING_ = 0x0000;
|
4675 |
|
4676 | this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
|
4677 | this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f;
|
4678 |
|
4679 | this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
|
4680 | this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
|
4681 | this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
|
4682 | this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d;
|
4683 |
|
4684 | this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29;
|
4685 |
|
4686 | this.BACKSPACE_ = this.CONTROL_ | 0x21;
|
4687 | this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
|
4688 | this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
|
4689 | };
|
4690 |
|
4691 | |
4692 |
|
4693 |
|
4694 |
|
4695 |
|
4696 |
|
4697 |
|
4698 |
|
4699 |
|
4700 |
|
4701 |
|
4702 | Cea608Stream.prototype.isSpecialCharacter = function (char0, char1) {
|
4703 | return char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f;
|
4704 | };
|
4705 |
|
4706 | |
4707 |
|
4708 |
|
4709 |
|
4710 |
|
4711 |
|
4712 |
|
4713 |
|
4714 |
|
4715 |
|
4716 |
|
4717 | Cea608Stream.prototype.isExtCharacter = function (char0, char1) {
|
4718 | return (char0 === this.EXT_ + 1 || char0 === this.EXT_ + 2) && char1 >= 0x20 && char1 <= 0x3f;
|
4719 | };
|
4720 |
|
4721 | |
4722 |
|
4723 |
|
4724 |
|
4725 |
|
4726 |
|
4727 |
|
4728 |
|
4729 |
|
4730 |
|
4731 |
|
4732 | Cea608Stream.prototype.isMidRowCode = function (char0, char1) {
|
4733 | return char0 === this.EXT_ && char1 >= 0x20 && char1 <= 0x2f;
|
4734 | };
|
4735 |
|
4736 | |
4737 |
|
4738 |
|
4739 |
|
4740 |
|
4741 |
|
4742 |
|
4743 |
|
4744 |
|
4745 |
|
4746 |
|
4747 | Cea608Stream.prototype.isOffsetControlCode = function (char0, char1) {
|
4748 | return char0 === this.OFFSET_ && char1 >= 0x21 && char1 <= 0x23;
|
4749 | };
|
4750 |
|
4751 | |
4752 |
|
4753 |
|
4754 |
|
4755 |
|
4756 |
|
4757 |
|
4758 |
|
4759 |
|
4760 |
|
4761 |
|
4762 | Cea608Stream.prototype.isPAC = function (char0, char1) {
|
4763 | return char0 >= this.BASE_ && char0 < this.BASE_ + 8 && char1 >= 0x40 && char1 <= 0x7f;
|
4764 | };
|
4765 |
|
4766 | |
4767 |
|
4768 |
|
4769 |
|
4770 |
|
4771 |
|
4772 |
|
4773 |
|
4774 |
|
4775 | Cea608Stream.prototype.isColorPAC = function (char1) {
|
4776 | return char1 >= 0x40 && char1 <= 0x4f || char1 >= 0x60 && char1 <= 0x7f;
|
4777 | };
|
4778 |
|
4779 | |
4780 |
|
4781 |
|
4782 |
|
4783 |
|
4784 |
|
4785 |
|
4786 |
|
4787 | Cea608Stream.prototype.isNormalChar = function (char) {
|
4788 | return char >= 0x20 && char <= 0x7f;
|
4789 | };
|
4790 |
|
4791 | |
4792 |
|
4793 |
|
4794 |
|
4795 |
|
4796 |
|
4797 |
|
4798 | Cea608Stream.prototype.setRollUp = function (pts, newBaseRow) {
|
4799 |
|
4800 | if (this.mode_ !== 'rollUp') {
|
4801 | this.row_ = BOTTOM_ROW;
|
4802 | this.mode_ = 'rollUp';
|
4803 |
|
4804 | this.flushDisplayed(pts);
|
4805 | this.nonDisplayed_ = createDisplayBuffer();
|
4806 | this.displayed_ = createDisplayBuffer();
|
4807 | }
|
4808 |
|
4809 | if (newBaseRow !== undefined && newBaseRow !== this.row_) {
|
4810 |
|
4811 | for (var i = 0; i < this.rollUpRows_; i++) {
|
4812 | this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
|
4813 | this.displayed_[this.row_ - i] = '';
|
4814 | }
|
4815 | }
|
4816 |
|
4817 | if (newBaseRow === undefined) {
|
4818 | newBaseRow = this.row_;
|
4819 | }
|
4820 |
|
4821 | this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
|
4822 | };
|
4823 |
|
4824 |
|
4825 |
|
4826 | Cea608Stream.prototype.addFormatting = function (pts, format) {
|
4827 | this.formatting_ = this.formatting_.concat(format);
|
4828 | var text = format.reduce(function (text, format) {
|
4829 | return text + '<' + format + '>';
|
4830 | }, '');
|
4831 | this[this.mode_](pts, text);
|
4832 | };
|
4833 |
|
4834 |
|
4835 |
|
4836 | Cea608Stream.prototype.clearFormatting = function (pts) {
|
4837 | if (!this.formatting_.length) {
|
4838 | return;
|
4839 | }
|
4840 | var text = this.formatting_.reverse().reduce(function (text, format) {
|
4841 | return text + '</' + format + '>';
|
4842 | }, '');
|
4843 | this.formatting_ = [];
|
4844 | this[this.mode_](pts, text);
|
4845 | };
|
4846 |
|
4847 |
|
4848 | Cea608Stream.prototype.popOn = function (pts, text) {
|
4849 | var baseRow = this.nonDisplayed_[this.row_];
|
4850 |
|
4851 |
|
4852 | baseRow += text;
|
4853 | this.nonDisplayed_[this.row_] = baseRow;
|
4854 | };
|
4855 |
|
4856 | Cea608Stream.prototype.rollUp = function (pts, text) {
|
4857 | var baseRow = this.displayed_[this.row_];
|
4858 |
|
4859 | baseRow += text;
|
4860 | this.displayed_[this.row_] = baseRow;
|
4861 | };
|
4862 |
|
4863 | Cea608Stream.prototype.shiftRowsUp_ = function () {
|
4864 | var i;
|
4865 |
|
4866 | for (i = 0; i < this.topRow_; i++) {
|
4867 | this.displayed_[i] = '';
|
4868 | }
|
4869 | for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
|
4870 | this.displayed_[i] = '';
|
4871 | }
|
4872 |
|
4873 | for (i = this.topRow_; i < this.row_; i++) {
|
4874 | this.displayed_[i] = this.displayed_[i + 1];
|
4875 | }
|
4876 |
|
4877 | this.displayed_[this.row_] = '';
|
4878 | };
|
4879 |
|
4880 | Cea608Stream.prototype.paintOn = function (pts, text) {
|
4881 | var baseRow = this.displayed_[this.row_];
|
4882 |
|
4883 | baseRow += text;
|
4884 | this.displayed_[this.row_] = baseRow;
|
4885 | };
|
4886 |
|
4887 |
|
4888 | var captionStream = {
|
4889 | CaptionStream: CaptionStream,
|
4890 | Cea608Stream: Cea608Stream
|
4891 | };
|
4892 |
|
4893 | |
4894 |
|
4895 |
|
4896 |
|
4897 |
|
4898 |
|
4899 |
|
4900 | var streamTypes = {
|
4901 | H264_STREAM_TYPE: 0x1B,
|
4902 | ADTS_STREAM_TYPE: 0x0F,
|
4903 | METADATA_STREAM_TYPE: 0x15
|
4904 | };
|
4905 |
|
4906 | var MAX_TS = 8589934592;
|
4907 |
|
4908 | var RO_THRESH = 4294967296;
|
4909 |
|
4910 | var TYPE_SHARED = 'shared';
|
4911 |
|
4912 | var handleRollover = function handleRollover(value, reference) {
|
4913 | var direction = 1;
|
4914 |
|
4915 | if (value > reference) {
|
4916 |
|
4917 |
|
4918 |
|
4919 |
|
4920 |
|
4921 |
|
4922 |
|
4923 | direction = -1;
|
4924 | }
|
4925 |
|
4926 |
|
4927 |
|
4928 | while (Math.abs(reference - value) > RO_THRESH) {
|
4929 | value += direction * MAX_TS;
|
4930 | }
|
4931 |
|
4932 | return value;
|
4933 | };
|
4934 |
|
4935 | var TimestampRolloverStream = function TimestampRolloverStream(type) {
|
4936 | var lastDTS, referenceDTS;
|
4937 |
|
4938 | TimestampRolloverStream.prototype.init.call(this);
|
4939 |
|
4940 |
|
4941 |
|
4942 |
|
4943 | this.type_ = type || TYPE_SHARED;
|
4944 |
|
4945 | this.push = function (data) {
|
4946 |
|
4947 |
|
4948 |
|
4949 | if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
|
4950 | return;
|
4951 | }
|
4952 |
|
4953 | if (referenceDTS === undefined) {
|
4954 | referenceDTS = data.dts;
|
4955 | }
|
4956 |
|
4957 | data.dts = handleRollover(data.dts, referenceDTS);
|
4958 | data.pts = handleRollover(data.pts, referenceDTS);
|
4959 |
|
4960 | lastDTS = data.dts;
|
4961 |
|
4962 | this.trigger('data', data);
|
4963 | };
|
4964 |
|
4965 | this.flush = function () {
|
4966 | referenceDTS = lastDTS;
|
4967 | this.trigger('done');
|
4968 | };
|
4969 |
|
4970 | this.endTimeline = function () {
|
4971 | this.flush();
|
4972 | this.trigger('endedtimeline');
|
4973 | };
|
4974 |
|
4975 | this.discontinuity = function () {
|
4976 | referenceDTS = void 0;
|
4977 | lastDTS = void 0;
|
4978 | };
|
4979 |
|
4980 | this.reset = function () {
|
4981 | this.discontinuity();
|
4982 | this.trigger('reset');
|
4983 | };
|
4984 | };
|
4985 |
|
4986 | TimestampRolloverStream.prototype = new stream();
|
4987 |
|
4988 | var timestampRolloverStream = {
|
4989 | TimestampRolloverStream: TimestampRolloverStream,
|
4990 | handleRollover: handleRollover
|
4991 | };
|
4992 |
|
4993 | var percentEncode = function percentEncode(bytes, start, end) {
|
4994 | var i,
|
4995 | result = '';
|
4996 | for (i = start; i < end; i++) {
|
4997 | result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
|
4998 | }
|
4999 | return result;
|
5000 | },
|
5001 |
|
5002 |
|
5003 |
|
5004 |
|
5005 | parseUtf8 = function parseUtf8(bytes, start, end) {
|
5006 | return decodeURIComponent(percentEncode(bytes, start, end));
|
5007 | },
|
5008 |
|
5009 |
|
5010 |
|
5011 |
|
5012 | parseIso88591 = function parseIso88591(bytes, start, end) {
|
5013 | return unescape(percentEncode(bytes, start, end));
|
5014 | },
|
5015 | parseSyncSafeInteger = function parseSyncSafeInteger(data) {
|
5016 | return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
|
5017 | },
|
5018 | tagParsers = {
|
5019 | TXXX: function TXXX(tag) {
|
5020 | var i;
|
5021 | if (tag.data[0] !== 3) {
|
5022 |
|
5023 | return;
|
5024 | }
|
5025 |
|
5026 | for (i = 1; i < tag.data.length; i++) {
|
5027 | if (tag.data[i] === 0) {
|
5028 |
|
5029 | tag.description = parseUtf8(tag.data, 1, i);
|
5030 |
|
5031 | tag.value = parseUtf8(tag.data, i + 1, tag.data.length).replace(/\0*$/, '');
|
5032 | break;
|
5033 | }
|
5034 | }
|
5035 | tag.data = tag.value;
|
5036 | },
|
5037 | WXXX: function WXXX(tag) {
|
5038 | var i;
|
5039 | if (tag.data[0] !== 3) {
|
5040 |
|
5041 | return;
|
5042 | }
|
5043 |
|
5044 | for (i = 1; i < tag.data.length; i++) {
|
5045 | if (tag.data[i] === 0) {
|
5046 |
|
5047 | tag.description = parseUtf8(tag.data, 1, i);
|
5048 | tag.url = parseUtf8(tag.data, i + 1, tag.data.length);
|
5049 | break;
|
5050 | }
|
5051 | }
|
5052 | },
|
5053 | PRIV: function PRIV(tag) {
|
5054 | var i;
|
5055 |
|
5056 | for (i = 0; i < tag.data.length; i++) {
|
5057 | if (tag.data[i] === 0) {
|
5058 |
|
5059 | tag.owner = parseIso88591(tag.data, 0, i);
|
5060 | break;
|
5061 | }
|
5062 | }
|
5063 | tag.privateData = tag.data.subarray(i + 1);
|
5064 | tag.data = tag.privateData;
|
5065 | }
|
5066 | },
|
5067 | _MetadataStream;
|
5068 |
|
5069 | _MetadataStream = function MetadataStream(options) {
|
5070 | var settings = {
|
5071 | debug: !!(options && options.debug),
|
5072 |
|
5073 |
|
5074 |
|
5075 |
|
5076 | descriptor: options && options.descriptor
|
5077 | },
|
5078 |
|
5079 |
|
5080 |
|
5081 | tagSize = 0,
|
5082 |
|
5083 |
|
5084 |
|
5085 | buffer = [],
|
5086 |
|
5087 |
|
5088 |
|
5089 | bufferSize = 0,
|
5090 | i;
|
5091 |
|
5092 | _MetadataStream.prototype.init.call(this);
|
5093 |
|
5094 |
|
5095 |
|
5096 | this.dispatchType = streamTypes.METADATA_STREAM_TYPE.toString(16);
|
5097 | if (settings.descriptor) {
|
5098 | for (i = 0; i < settings.descriptor.length; i++) {
|
5099 | this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
|
5100 | }
|
5101 | }
|
5102 |
|
5103 | this.push = function (chunk) {
|
5104 | var tag, frameStart, frameSize, frame, i, frameHeader;
|
5105 | if (chunk.type !== 'timed-metadata') {
|
5106 | return;
|
5107 | }
|
5108 |
|
5109 |
|
5110 |
|
5111 |
|
5112 | if (chunk.dataAlignmentIndicator) {
|
5113 | bufferSize = 0;
|
5114 | buffer.length = 0;
|
5115 | }
|
5116 |
|
5117 |
|
5118 | 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))) {
|
5119 | if (settings.debug) {
|
5120 |
|
5121 | console.log('Skipping unrecognized metadata packet');
|
5122 | }
|
5123 | return;
|
5124 | }
|
5125 |
|
5126 |
|
5127 |
|
5128 | buffer.push(chunk);
|
5129 | bufferSize += chunk.data.byteLength;
|
5130 |
|
5131 |
|
5132 | if (buffer.length === 1) {
|
5133 |
|
5134 |
|
5135 |
|
5136 |
|
5137 | tagSize = parseSyncSafeInteger(chunk.data.subarray(6, 10));
|
5138 |
|
5139 |
|
5140 |
|
5141 | tagSize += 10;
|
5142 | }
|
5143 |
|
5144 |
|
5145 | if (bufferSize < tagSize) {
|
5146 | return;
|
5147 | }
|
5148 |
|
5149 |
|
5150 | tag = {
|
5151 | data: new Uint8Array(tagSize),
|
5152 | frames: [],
|
5153 | pts: buffer[0].pts,
|
5154 | dts: buffer[0].dts
|
5155 | };
|
5156 | for (i = 0; i < tagSize;) {
|
5157 | tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
|
5158 | i += buffer[0].data.byteLength;
|
5159 | bufferSize -= buffer[0].data.byteLength;
|
5160 | buffer.shift();
|
5161 | }
|
5162 |
|
5163 |
|
5164 | frameStart = 10;
|
5165 | if (tag.data[5] & 0x40) {
|
5166 |
|
5167 | frameStart += 4;
|
5168 | frameStart += parseSyncSafeInteger(tag.data.subarray(10, 14));
|
5169 |
|
5170 |
|
5171 | tagSize -= parseSyncSafeInteger(tag.data.subarray(16, 20));
|
5172 | }
|
5173 |
|
5174 |
|
5175 |
|
5176 | do {
|
5177 |
|
5178 | frameSize = parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
|
5179 | if (frameSize < 1) {
|
5180 |
|
5181 | return console.log('Malformed ID3 frame encountered. Skipping metadata parsing.');
|
5182 | }
|
5183 | frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
|
5184 |
|
5185 | frame = {
|
5186 | id: frameHeader,
|
5187 | data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
|
5188 | };
|
5189 | frame.key = frame.id;
|
5190 | if (tagParsers[frame.id]) {
|
5191 | tagParsers[frame.id](frame);
|
5192 |
|
5193 |
|
5194 |
|
5195 | if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
|
5196 | var d = frame.data,
|
5197 | size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
|
5198 |
|
5199 | size *= 4;
|
5200 | size += d[7] & 0x03;
|
5201 | frame.timeStamp = size;
|
5202 |
|
5203 |
|
5204 |
|
5205 |
|
5206 | if (tag.pts === undefined && tag.dts === undefined) {
|
5207 | tag.pts = frame.timeStamp;
|
5208 | tag.dts = frame.timeStamp;
|
5209 | }
|
5210 | this.trigger('timestamp', frame);
|
5211 | }
|
5212 | }
|
5213 | tag.frames.push(frame);
|
5214 |
|
5215 | frameStart += 10;
|
5216 | frameStart += frameSize;
|
5217 | } while (frameStart < tagSize);
|
5218 | this.trigger('data', tag);
|
5219 | };
|
5220 | };
|
5221 | _MetadataStream.prototype = new stream();
|
5222 |
|
5223 | var metadataStream = _MetadataStream;
|
5224 |
|
5225 | var TimestampRolloverStream$1 = timestampRolloverStream.TimestampRolloverStream;
|
5226 |
|
5227 |
|
5228 | var _TransportPacketStream, _TransportParseStream, _ElementaryStream;
|
5229 |
|
5230 |
|
5231 | var MP2T_PACKET_LENGTH = 188,
|
5232 |
|
5233 |
|
5234 | SYNC_BYTE = 0x47;
|
5235 |
|
5236 | |
5237 |
|
5238 |
|
5239 |
|
5240 | _TransportPacketStream = function TransportPacketStream() {
|
5241 | var buffer = new Uint8Array(MP2T_PACKET_LENGTH),
|
5242 | bytesInBuffer = 0;
|
5243 |
|
5244 | _TransportPacketStream.prototype.init.call(this);
|
5245 |
|
5246 |
|
5247 |
|
5248 | |
5249 |
|
5250 |
|
5251 | this.push = function (bytes) {
|
5252 | var startIndex = 0,
|
5253 | endIndex = MP2T_PACKET_LENGTH,
|
5254 | everything;
|
5255 |
|
5256 |
|
5257 |
|
5258 | if (bytesInBuffer) {
|
5259 | everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
|
5260 | everything.set(buffer.subarray(0, bytesInBuffer));
|
5261 | everything.set(bytes, bytesInBuffer);
|
5262 | bytesInBuffer = 0;
|
5263 | } else {
|
5264 | everything = bytes;
|
5265 | }
|
5266 |
|
5267 |
|
5268 | while (endIndex < everything.byteLength) {
|
5269 |
|
5270 | if (everything[startIndex] === SYNC_BYTE && everything[endIndex] === SYNC_BYTE) {
|
5271 |
|
5272 |
|
5273 | this.trigger('data', everything.subarray(startIndex, endIndex));
|
5274 | startIndex += MP2T_PACKET_LENGTH;
|
5275 | endIndex += MP2T_PACKET_LENGTH;
|
5276 | continue;
|
5277 | }
|
5278 |
|
5279 |
|
5280 |
|
5281 | startIndex++;
|
5282 | endIndex++;
|
5283 | }
|
5284 |
|
5285 |
|
5286 |
|
5287 |
|
5288 | if (startIndex < everything.byteLength) {
|
5289 | buffer.set(everything.subarray(startIndex), 0);
|
5290 | bytesInBuffer = everything.byteLength - startIndex;
|
5291 | }
|
5292 | };
|
5293 |
|
5294 | |
5295 |
|
5296 |
|
5297 | this.flush = function () {
|
5298 |
|
5299 |
|
5300 |
|
5301 | if (bytesInBuffer === MP2T_PACKET_LENGTH && buffer[0] === SYNC_BYTE) {
|
5302 | this.trigger('data', buffer);
|
5303 | bytesInBuffer = 0;
|
5304 | }
|
5305 | this.trigger('done');
|
5306 | };
|
5307 |
|
5308 | this.endTimeline = function () {
|
5309 | this.flush();
|
5310 | this.trigger('endedtimeline');
|
5311 | };
|
5312 |
|
5313 | this.reset = function () {
|
5314 | bytesInBuffer = 0;
|
5315 | this.trigger('reset');
|
5316 | };
|
5317 | };
|
5318 | _TransportPacketStream.prototype = new stream();
|
5319 |
|
5320 | |
5321 |
|
5322 |
|
5323 |
|
5324 | _TransportParseStream = function TransportParseStream() {
|
5325 | var parsePsi, parsePat, parsePmt, self;
|
5326 | _TransportParseStream.prototype.init.call(this);
|
5327 | self = this;
|
5328 |
|
5329 | this.packetsWaitingForPmt = [];
|
5330 | this.programMapTable = undefined;
|
5331 |
|
5332 | parsePsi = function parsePsi(payload, psi) {
|
5333 | var offset = 0;
|
5334 |
|
5335 |
|
5336 |
|
5337 |
|
5338 |
|
5339 |
|
5340 |
|
5341 | if (psi.payloadUnitStartIndicator) {
|
5342 | offset += payload[offset] + 1;
|
5343 | }
|
5344 |
|
5345 | if (psi.type === 'pat') {
|
5346 | parsePat(payload.subarray(offset), psi);
|
5347 | } else {
|
5348 | parsePmt(payload.subarray(offset), psi);
|
5349 | }
|
5350 | };
|
5351 |
|
5352 | parsePat = function parsePat(payload, pat) {
|
5353 | pat.section_number = payload[7];
|
5354 | pat.last_section_number = payload[8];
|
5355 |
|
5356 |
|
5357 | self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
|
5358 | pat.pmtPid = self.pmtPid;
|
5359 | };
|
5360 |
|
5361 | |
5362 |
|
5363 |
|
5364 |
|
5365 |
|
5366 |
|
5367 |
|
5368 |
|
5369 | parsePmt = function parsePmt(payload, pmt) {
|
5370 | var sectionLength, tableEnd, programInfoLength, offset;
|
5371 |
|
5372 |
|
5373 |
|
5374 |
|
5375 |
|
5376 |
|
5377 | if (!(payload[5] & 0x01)) {
|
5378 | return;
|
5379 | }
|
5380 |
|
5381 |
|
5382 | self.programMapTable = {
|
5383 | video: null,
|
5384 | audio: null,
|
5385 | 'timed-metadata': {}
|
5386 | };
|
5387 |
|
5388 |
|
5389 | sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
|
5390 | tableEnd = 3 + sectionLength - 4;
|
5391 |
|
5392 |
|
5393 |
|
5394 | programInfoLength = (payload[10] & 0x0f) << 8 | payload[11];
|
5395 |
|
5396 |
|
5397 | offset = 12 + programInfoLength;
|
5398 | while (offset < tableEnd) {
|
5399 | var streamType = payload[offset];
|
5400 | var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2];
|
5401 |
|
5402 |
|
5403 |
|
5404 |
|
5405 | if (streamType === streamTypes.H264_STREAM_TYPE && self.programMapTable.video === null) {
|
5406 | self.programMapTable.video = pid;
|
5407 | } else if (streamType === streamTypes.ADTS_STREAM_TYPE && self.programMapTable.audio === null) {
|
5408 | self.programMapTable.audio = pid;
|
5409 | } else if (streamType === streamTypes.METADATA_STREAM_TYPE) {
|
5410 |
|
5411 | self.programMapTable['timed-metadata'][pid] = streamType;
|
5412 | }
|
5413 |
|
5414 |
|
5415 |
|
5416 | offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
|
5417 | }
|
5418 |
|
5419 |
|
5420 | pmt.programMapTable = self.programMapTable;
|
5421 | };
|
5422 |
|
5423 | |
5424 |
|
5425 |
|
5426 | this.push = function (packet) {
|
5427 | var result = {},
|
5428 | offset = 4;
|
5429 |
|
5430 | result.payloadUnitStartIndicator = !!(packet[1] & 0x40);
|
5431 |
|
5432 |
|
5433 | result.pid = packet[1] & 0x1f;
|
5434 | result.pid <<= 8;
|
5435 | result.pid |= packet[2];
|
5436 |
|
5437 |
|
5438 |
|
5439 |
|
5440 |
|
5441 |
|
5442 | if ((packet[3] & 0x30) >>> 4 > 0x01) {
|
5443 | offset += packet[offset] + 1;
|
5444 | }
|
5445 |
|
5446 |
|
5447 | if (result.pid === 0) {
|
5448 | result.type = 'pat';
|
5449 | parsePsi(packet.subarray(offset), result);
|
5450 | this.trigger('data', result);
|
5451 | } else if (result.pid === this.pmtPid) {
|
5452 | result.type = 'pmt';
|
5453 | parsePsi(packet.subarray(offset), result);
|
5454 | this.trigger('data', result);
|
5455 |
|
5456 |
|
5457 | while (this.packetsWaitingForPmt.length) {
|
5458 | this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
|
5459 | }
|
5460 | } else if (this.programMapTable === undefined) {
|
5461 |
|
5462 |
|
5463 | this.packetsWaitingForPmt.push([packet, offset, result]);
|
5464 | } else {
|
5465 | this.processPes_(packet, offset, result);
|
5466 | }
|
5467 | };
|
5468 |
|
5469 | this.processPes_ = function (packet, offset, result) {
|
5470 |
|
5471 | if (result.pid === this.programMapTable.video) {
|
5472 | result.streamType = streamTypes.H264_STREAM_TYPE;
|
5473 | } else if (result.pid === this.programMapTable.audio) {
|
5474 | result.streamType = streamTypes.ADTS_STREAM_TYPE;
|
5475 | } else {
|
5476 |
|
5477 |
|
5478 | result.streamType = this.programMapTable['timed-metadata'][result.pid];
|
5479 | }
|
5480 |
|
5481 | result.type = 'pes';
|
5482 | result.data = packet.subarray(offset);
|
5483 | this.trigger('data', result);
|
5484 | };
|
5485 | };
|
5486 | _TransportParseStream.prototype = new stream();
|
5487 | _TransportParseStream.STREAM_TYPES = {
|
5488 | h264: 0x1b,
|
5489 | adts: 0x0f
|
5490 | };
|
5491 |
|
5492 | |
5493 |
|
5494 |
|
5495 |
|
5496 |
|
5497 |
|
5498 |
|
5499 |
|
5500 | _ElementaryStream = function ElementaryStream() {
|
5501 | var self = this,
|
5502 |
|
5503 |
|
5504 |
|
5505 | video = {
|
5506 | data: [],
|
5507 | size: 0
|
5508 | },
|
5509 | audio = {
|
5510 | data: [],
|
5511 | size: 0
|
5512 | },
|
5513 | timedMetadata = {
|
5514 | data: [],
|
5515 | size: 0
|
5516 | },
|
5517 | programMapTable,
|
5518 | parsePes = function parsePes(payload, pes) {
|
5519 | var ptsDtsFlags;
|
5520 |
|
5521 |
|
5522 | pes.packetLength = 6 + (payload[4] << 8 | payload[5]);
|
5523 |
|
5524 |
|
5525 | pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0;
|
5526 |
|
5527 |
|
5528 |
|
5529 | ptsDtsFlags = payload[7];
|
5530 |
|
5531 |
|
5532 |
|
5533 |
|
5534 |
|
5535 |
|
5536 |
|
5537 |
|
5538 |
|
5539 | if (ptsDtsFlags & 0xC0) {
|
5540 |
|
5541 |
|
5542 |
|
5543 | pes.pts = (payload[9] & 0x0E) << 27 | (payload[10] & 0xFF) << 20 | (payload[11] & 0xFE) << 12 | (payload[12] & 0xFF) << 5 | (payload[13] & 0xFE) >>> 3;
|
5544 | pes.pts *= 4;
|
5545 | pes.pts += (payload[13] & 0x06) >>> 1;
|
5546 | pes.dts = pes.pts;
|
5547 | if (ptsDtsFlags & 0x40) {
|
5548 | pes.dts = (payload[14] & 0x0E) << 27 | (payload[15] & 0xFF) << 20 | (payload[16] & 0xFE) << 12 | (payload[17] & 0xFF) << 5 | (payload[18] & 0xFE) >>> 3;
|
5549 | pes.dts *= 4;
|
5550 | pes.dts += (payload[18] & 0x06) >>> 1;
|
5551 | }
|
5552 | }
|
5553 |
|
5554 |
|
5555 |
|
5556 | pes.data = payload.subarray(9 + payload[8]);
|
5557 | },
|
5558 |
|
5559 |
|
5560 | |
5561 |
|
5562 |
|
5563 | flushStream = function flushStream(stream$$1, type, forceFlush) {
|
5564 | var packetData = new Uint8Array(stream$$1.size),
|
5565 | event = {
|
5566 | type: type
|
5567 | },
|
5568 | i = 0,
|
5569 | offset = 0,
|
5570 | packetFlushable = false,
|
5571 | fragment;
|
5572 |
|
5573 |
|
5574 |
|
5575 | if (!stream$$1.data.length || stream$$1.size < 9) {
|
5576 | return;
|
5577 | }
|
5578 | event.trackId = stream$$1.data[0].pid;
|
5579 |
|
5580 |
|
5581 | for (i = 0; i < stream$$1.data.length; i++) {
|
5582 | fragment = stream$$1.data[i];
|
5583 |
|
5584 | packetData.set(fragment.data, offset);
|
5585 | offset += fragment.data.byteLength;
|
5586 | }
|
5587 |
|
5588 |
|
5589 | parsePes(packetData, event);
|
5590 |
|
5591 |
|
5592 |
|
5593 | packetFlushable = type === 'video' || event.packetLength <= stream$$1.size;
|
5594 |
|
5595 |
|
5596 | if (forceFlush || packetFlushable) {
|
5597 | stream$$1.size = 0;
|
5598 | stream$$1.data.length = 0;
|
5599 | }
|
5600 |
|
5601 |
|
5602 |
|
5603 | if (packetFlushable) {
|
5604 | self.trigger('data', event);
|
5605 | }
|
5606 | };
|
5607 |
|
5608 | _ElementaryStream.prototype.init.call(this);
|
5609 |
|
5610 | |
5611 |
|
5612 |
|
5613 |
|
5614 | this.push = function (data) {
|
5615 | ({
|
5616 | pat: function pat() {
|
5617 |
|
5618 |
|
5619 | },
|
5620 | pes: function pes() {
|
5621 | var stream$$1, streamType;
|
5622 |
|
5623 | switch (data.streamType) {
|
5624 | case streamTypes.H264_STREAM_TYPE:
|
5625 | case streamTypes.H264_STREAM_TYPE:
|
5626 | stream$$1 = video;
|
5627 | streamType = 'video';
|
5628 | break;
|
5629 | case streamTypes.ADTS_STREAM_TYPE:
|
5630 | stream$$1 = audio;
|
5631 | streamType = 'audio';
|
5632 | break;
|
5633 | case streamTypes.METADATA_STREAM_TYPE:
|
5634 | stream$$1 = timedMetadata;
|
5635 | streamType = 'timed-metadata';
|
5636 | break;
|
5637 | default:
|
5638 |
|
5639 | return;
|
5640 | }
|
5641 |
|
5642 |
|
5643 |
|
5644 | if (data.payloadUnitStartIndicator) {
|
5645 | flushStream(stream$$1, streamType, true);
|
5646 | }
|
5647 |
|
5648 |
|
5649 |
|
5650 | stream$$1.data.push(data);
|
5651 | stream$$1.size += data.data.byteLength;
|
5652 | },
|
5653 | pmt: function pmt() {
|
5654 | var event = {
|
5655 | type: 'metadata',
|
5656 | tracks: []
|
5657 | };
|
5658 |
|
5659 | programMapTable = data.programMapTable;
|
5660 |
|
5661 |
|
5662 | if (programMapTable.video !== null) {
|
5663 | event.tracks.push({
|
5664 | timelineStartInfo: {
|
5665 | baseMediaDecodeTime: 0
|
5666 | },
|
5667 | id: +programMapTable.video,
|
5668 | codec: 'avc',
|
5669 | type: 'video'
|
5670 | });
|
5671 | }
|
5672 | if (programMapTable.audio !== null) {
|
5673 | event.tracks.push({
|
5674 | timelineStartInfo: {
|
5675 | baseMediaDecodeTime: 0
|
5676 | },
|
5677 | id: +programMapTable.audio,
|
5678 | codec: 'adts',
|
5679 | type: 'audio'
|
5680 | });
|
5681 | }
|
5682 |
|
5683 | self.trigger('data', event);
|
5684 | }
|
5685 | })[data.type]();
|
5686 | };
|
5687 |
|
5688 | this.reset = function () {
|
5689 | video.size = 0;
|
5690 | video.data.length = 0;
|
5691 | audio.size = 0;
|
5692 | audio.data.length = 0;
|
5693 | this.trigger('reset');
|
5694 | };
|
5695 |
|
5696 | |
5697 |
|
5698 |
|
5699 |
|
5700 |
|
5701 |
|
5702 |
|
5703 |
|
5704 |
|
5705 | this.flushStreams_ = function () {
|
5706 |
|
5707 |
|
5708 | flushStream(video, 'video');
|
5709 | flushStream(audio, 'audio');
|
5710 | flushStream(timedMetadata, 'timed-metadata');
|
5711 | };
|
5712 |
|
5713 | this.flush = function () {
|
5714 | this.flushStreams_();
|
5715 | this.trigger('done');
|
5716 | };
|
5717 | };
|
5718 | _ElementaryStream.prototype = new stream();
|
5719 |
|
5720 | var m2ts = {
|
5721 | PAT_PID: 0x0000,
|
5722 | MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH,
|
5723 | TransportPacketStream: _TransportPacketStream,
|
5724 | TransportParseStream: _TransportParseStream,
|
5725 | ElementaryStream: _ElementaryStream,
|
5726 | TimestampRolloverStream: TimestampRolloverStream$1,
|
5727 | CaptionStream: captionStream.CaptionStream,
|
5728 | Cea608Stream: captionStream.Cea608Stream,
|
5729 | MetadataStream: metadataStream
|
5730 | };
|
5731 |
|
5732 | for (var type in streamTypes) {
|
5733 | if (streamTypes.hasOwnProperty(type)) {
|
5734 | m2ts[type] = streamTypes[type];
|
5735 | }
|
5736 | }
|
5737 |
|
5738 | var m2ts_1 = m2ts;
|
5739 |
|
5740 | var ONE_SECOND_IN_TS$2 = clock.ONE_SECOND_IN_TS;
|
5741 |
|
5742 | var _AdtsStream;
|
5743 |
|
5744 | var ADTS_SAMPLING_FREQUENCIES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
5745 |
|
5746 | |
5747 |
|
5748 |
|
5749 |
|
5750 |
|
5751 |
|
5752 |
|
5753 |
|
5754 | _AdtsStream = function AdtsStream(handlePartialSegments) {
|
5755 | var buffer,
|
5756 | frameNum = 0;
|
5757 |
|
5758 | _AdtsStream.prototype.init.call(this);
|
5759 |
|
5760 | this.push = function (packet) {
|
5761 | var i = 0,
|
5762 | frameLength,
|
5763 | protectionSkipBytes,
|
5764 | frameEnd,
|
5765 | oldBuffer,
|
5766 | sampleCount,
|
5767 | adtsFrameDuration;
|
5768 |
|
5769 | if (!handlePartialSegments) {
|
5770 | frameNum = 0;
|
5771 | }
|
5772 |
|
5773 | if (packet.type !== 'audio') {
|
5774 |
|
5775 | return;
|
5776 | }
|
5777 |
|
5778 |
|
5779 |
|
5780 | if (buffer) {
|
5781 | oldBuffer = buffer;
|
5782 | buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
|
5783 | buffer.set(oldBuffer);
|
5784 | buffer.set(packet.data, oldBuffer.byteLength);
|
5785 | } else {
|
5786 | buffer = packet.data;
|
5787 | }
|
5788 |
|
5789 |
|
5790 |
|
5791 | while (i + 5 < buffer.length) {
|
5792 |
|
5793 |
|
5794 | if (buffer[i] !== 0xFF || (buffer[i + 1] & 0xF6) !== 0xF0) {
|
5795 |
|
5796 |
|
5797 | i++;
|
5798 | continue;
|
5799 | }
|
5800 |
|
5801 |
|
5802 |
|
5803 | protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2;
|
5804 |
|
5805 |
|
5806 |
|
5807 | frameLength = (buffer[i + 3] & 0x03) << 11 | buffer[i + 4] << 3 | (buffer[i + 5] & 0xe0) >> 5;
|
5808 |
|
5809 | sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
|
5810 | adtsFrameDuration = sampleCount * ONE_SECOND_IN_TS$2 / ADTS_SAMPLING_FREQUENCIES[(buffer[i + 2] & 0x3c) >>> 2];
|
5811 |
|
5812 | frameEnd = i + frameLength;
|
5813 |
|
5814 |
|
5815 |
|
5816 | if (buffer.byteLength < frameEnd) {
|
5817 | return;
|
5818 | }
|
5819 |
|
5820 |
|
5821 | this.trigger('data', {
|
5822 | pts: packet.pts + frameNum * adtsFrameDuration,
|
5823 | dts: packet.dts + frameNum * adtsFrameDuration,
|
5824 | sampleCount: sampleCount,
|
5825 | audioobjecttype: (buffer[i + 2] >>> 6 & 0x03) + 1,
|
5826 | channelcount: (buffer[i + 2] & 1) << 2 | (buffer[i + 3] & 0xc0) >>> 6,
|
5827 | samplerate: ADTS_SAMPLING_FREQUENCIES[(buffer[i + 2] & 0x3c) >>> 2],
|
5828 | samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
|
5829 |
|
5830 | samplesize: 16,
|
5831 | data: buffer.subarray(i + 7 + protectionSkipBytes, frameEnd)
|
5832 | });
|
5833 |
|
5834 | frameNum++;
|
5835 |
|
5836 |
|
5837 | if (buffer.byteLength === frameEnd) {
|
5838 | buffer = undefined;
|
5839 | return;
|
5840 | }
|
5841 |
|
5842 |
|
5843 | buffer = buffer.subarray(frameEnd);
|
5844 | }
|
5845 | };
|
5846 |
|
5847 | this.flush = function () {
|
5848 | frameNum = 0;
|
5849 | this.trigger('done');
|
5850 | };
|
5851 |
|
5852 | this.reset = function () {
|
5853 | buffer = void 0;
|
5854 | this.trigger('reset');
|
5855 | };
|
5856 |
|
5857 | this.endTimeline = function () {
|
5858 | buffer = void 0;
|
5859 | this.trigger('endedtimeline');
|
5860 | };
|
5861 | };
|
5862 |
|
5863 | _AdtsStream.prototype = new stream();
|
5864 |
|
5865 | var adts = _AdtsStream;
|
5866 |
|
5867 | |
5868 |
|
5869 |
|
5870 |
|
5871 |
|
5872 |
|
5873 |
|
5874 | var ExpGolomb;
|
5875 |
|
5876 | |
5877 |
|
5878 |
|
5879 |
|
5880 | ExpGolomb = function ExpGolomb(workingData) {
|
5881 | var
|
5882 |
|
5883 | workingBytesAvailable = workingData.byteLength,
|
5884 |
|
5885 |
|
5886 |
|
5887 | workingWord = 0,
|
5888 |
|
5889 |
|
5890 |
|
5891 |
|
5892 | workingBitsAvailable = 0;
|
5893 |
|
5894 |
|
5895 | this.length = function () {
|
5896 | return 8 * workingBytesAvailable;
|
5897 | };
|
5898 |
|
5899 |
|
5900 | this.bitsAvailable = function () {
|
5901 | return 8 * workingBytesAvailable + workingBitsAvailable;
|
5902 | };
|
5903 |
|
5904 |
|
5905 | this.loadWord = function () {
|
5906 | var position = workingData.byteLength - workingBytesAvailable,
|
5907 | workingBytes = new Uint8Array(4),
|
5908 | availableBytes = Math.min(4, workingBytesAvailable);
|
5909 |
|
5910 | if (availableBytes === 0) {
|
5911 | throw new Error('no bytes available');
|
5912 | }
|
5913 |
|
5914 | workingBytes.set(workingData.subarray(position, position + availableBytes));
|
5915 | workingWord = new DataView(workingBytes.buffer).getUint32(0);
|
5916 |
|
5917 |
|
5918 | workingBitsAvailable = availableBytes * 8;
|
5919 | workingBytesAvailable -= availableBytes;
|
5920 | };
|
5921 |
|
5922 |
|
5923 | this.skipBits = function (count) {
|
5924 | var skipBytes;
|
5925 | if (workingBitsAvailable > count) {
|
5926 | workingWord <<= count;
|
5927 | workingBitsAvailable -= count;
|
5928 | } else {
|
5929 | count -= workingBitsAvailable;
|
5930 | skipBytes = Math.floor(count / 8);
|
5931 |
|
5932 | count -= skipBytes * 8;
|
5933 | workingBytesAvailable -= skipBytes;
|
5934 |
|
5935 | this.loadWord();
|
5936 |
|
5937 | workingWord <<= count;
|
5938 | workingBitsAvailable -= count;
|
5939 | }
|
5940 | };
|
5941 |
|
5942 |
|
5943 | this.readBits = function (size) {
|
5944 | var bits = Math.min(workingBitsAvailable, size),
|
5945 |
|
5946 |
|
5947 | valu = workingWord >>> 32 - bits;
|
5948 |
|
5949 | workingBitsAvailable -= bits;
|
5950 | if (workingBitsAvailable > 0) {
|
5951 | workingWord <<= bits;
|
5952 | } else if (workingBytesAvailable > 0) {
|
5953 | this.loadWord();
|
5954 | }
|
5955 |
|
5956 | bits = size - bits;
|
5957 | if (bits > 0) {
|
5958 | return valu << bits | this.readBits(bits);
|
5959 | }
|
5960 | return valu;
|
5961 | };
|
5962 |
|
5963 |
|
5964 | this.skipLeadingZeros = function () {
|
5965 | var leadingZeroCount;
|
5966 | for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
|
5967 | if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
|
5968 |
|
5969 | workingWord <<= leadingZeroCount;
|
5970 | workingBitsAvailable -= leadingZeroCount;
|
5971 | return leadingZeroCount;
|
5972 | }
|
5973 | }
|
5974 |
|
5975 |
|
5976 | this.loadWord();
|
5977 | return leadingZeroCount + this.skipLeadingZeros();
|
5978 | };
|
5979 |
|
5980 |
|
5981 | this.skipUnsignedExpGolomb = function () {
|
5982 | this.skipBits(1 + this.skipLeadingZeros());
|
5983 | };
|
5984 |
|
5985 |
|
5986 | this.skipExpGolomb = function () {
|
5987 | this.skipBits(1 + this.skipLeadingZeros());
|
5988 | };
|
5989 |
|
5990 |
|
5991 | this.readUnsignedExpGolomb = function () {
|
5992 | var clz = this.skipLeadingZeros();
|
5993 | return this.readBits(clz + 1) - 1;
|
5994 | };
|
5995 |
|
5996 |
|
5997 | this.readExpGolomb = function () {
|
5998 | var valu = this.readUnsignedExpGolomb();
|
5999 | if (0x01 & valu) {
|
6000 |
|
6001 | return 1 + valu >>> 1;
|
6002 | }
|
6003 | return -1 * (valu >>> 1);
|
6004 | };
|
6005 |
|
6006 |
|
6007 |
|
6008 | this.readBoolean = function () {
|
6009 | return this.readBits(1) === 1;
|
6010 | };
|
6011 |
|
6012 |
|
6013 | this.readUnsignedByte = function () {
|
6014 | return this.readBits(8);
|
6015 | };
|
6016 |
|
6017 | this.loadWord();
|
6018 | };
|
6019 |
|
6020 | var expGolomb = ExpGolomb;
|
6021 |
|
6022 | var _H264Stream, _NalByteStream;
|
6023 | var PROFILES_WITH_OPTIONAL_SPS_DATA;
|
6024 |
|
6025 | |
6026 |
|
6027 |
|
6028 | _NalByteStream = function NalByteStream() {
|
6029 | var syncPoint = 0,
|
6030 | i,
|
6031 | buffer;
|
6032 | _NalByteStream.prototype.init.call(this);
|
6033 |
|
6034 | |
6035 |
|
6036 |
|
6037 |
|
6038 |
|
6039 |
|
6040 |
|
6041 | this.push = function (data) {
|
6042 | var swapBuffer;
|
6043 |
|
6044 | if (!buffer) {
|
6045 | buffer = data.data;
|
6046 | } else {
|
6047 | swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
|
6048 | swapBuffer.set(buffer);
|
6049 | swapBuffer.set(data.data, buffer.byteLength);
|
6050 | buffer = swapBuffer;
|
6051 | }
|
6052 | var len = buffer.byteLength;
|
6053 |
|
6054 |
|
6055 |
|
6056 |
|
6057 |
|
6058 |
|
6059 |
|
6060 |
|
6061 |
|
6062 |
|
6063 |
|
6064 |
|
6065 | for (; syncPoint < len - 3; syncPoint++) {
|
6066 | if (buffer[syncPoint + 2] === 1) {
|
6067 |
|
6068 | i = syncPoint + 5;
|
6069 | break;
|
6070 | }
|
6071 | }
|
6072 |
|
6073 | while (i < len) {
|
6074 |
|
6075 |
|
6076 | switch (buffer[i]) {
|
6077 | case 0:
|
6078 |
|
6079 | if (buffer[i - 1] !== 0) {
|
6080 | i += 2;
|
6081 | break;
|
6082 | } else if (buffer[i - 2] !== 0) {
|
6083 | i++;
|
6084 | break;
|
6085 | }
|
6086 |
|
6087 |
|
6088 | if (syncPoint + 3 !== i - 2) {
|
6089 | this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
|
6090 | }
|
6091 |
|
6092 |
|
6093 | do {
|
6094 | i++;
|
6095 | } while (buffer[i] !== 1 && i < len);
|
6096 | syncPoint = i - 2;
|
6097 | i += 3;
|
6098 | break;
|
6099 | case 1:
|
6100 |
|
6101 | if (buffer[i - 1] !== 0 || buffer[i - 2] !== 0) {
|
6102 | i += 3;
|
6103 | break;
|
6104 | }
|
6105 |
|
6106 |
|
6107 | this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
|
6108 | syncPoint = i - 2;
|
6109 | i += 3;
|
6110 | break;
|
6111 | default:
|
6112 |
|
6113 |
|
6114 | i += 3;
|
6115 | break;
|
6116 | }
|
6117 | }
|
6118 |
|
6119 | buffer = buffer.subarray(syncPoint);
|
6120 | i -= syncPoint;
|
6121 | syncPoint = 0;
|
6122 | };
|
6123 |
|
6124 | this.reset = function () {
|
6125 | buffer = null;
|
6126 | syncPoint = 0;
|
6127 | this.trigger('reset');
|
6128 | };
|
6129 |
|
6130 | this.flush = function () {
|
6131 |
|
6132 | if (buffer && buffer.byteLength > 3) {
|
6133 | this.trigger('data', buffer.subarray(syncPoint + 3));
|
6134 | }
|
6135 |
|
6136 | buffer = null;
|
6137 | syncPoint = 0;
|
6138 | this.trigger('done');
|
6139 | };
|
6140 |
|
6141 | this.endTimeline = function () {
|
6142 | this.flush();
|
6143 | this.trigger('endedtimeline');
|
6144 | };
|
6145 | };
|
6146 | _NalByteStream.prototype = new stream();
|
6147 |
|
6148 |
|
6149 |
|
6150 |
|
6151 | PROFILES_WITH_OPTIONAL_SPS_DATA = {
|
6152 | 100: true,
|
6153 | 110: true,
|
6154 | 122: true,
|
6155 | 244: true,
|
6156 | 44: true,
|
6157 | 83: true,
|
6158 | 86: true,
|
6159 | 118: true,
|
6160 | 128: true,
|
6161 | 138: true,
|
6162 | 139: true,
|
6163 | 134: true
|
6164 | };
|
6165 |
|
6166 | |
6167 |
|
6168 |
|
6169 |
|
6170 | _H264Stream = function H264Stream() {
|
6171 | var nalByteStream = new _NalByteStream(),
|
6172 | self,
|
6173 | trackId,
|
6174 | currentPts,
|
6175 | currentDts,
|
6176 | discardEmulationPreventionBytes,
|
6177 | readSequenceParameterSet,
|
6178 | skipScalingList;
|
6179 |
|
6180 | _H264Stream.prototype.init.call(this);
|
6181 | self = this;
|
6182 |
|
6183 | |
6184 |
|
6185 |
|
6186 |
|
6187 |
|
6188 |
|
6189 |
|
6190 |
|
6191 |
|
6192 |
|
6193 |
|
6194 | this.push = function (packet) {
|
6195 | if (packet.type !== 'video') {
|
6196 | return;
|
6197 | }
|
6198 | trackId = packet.trackId;
|
6199 | currentPts = packet.pts;
|
6200 | currentDts = packet.dts;
|
6201 |
|
6202 | nalByteStream.push(packet);
|
6203 | };
|
6204 |
|
6205 | |
6206 |
|
6207 |
|
6208 |
|
6209 |
|
6210 |
|
6211 |
|
6212 |
|
6213 | nalByteStream.on('data', function (data) {
|
6214 | var event = {
|
6215 | trackId: trackId,
|
6216 | pts: currentPts,
|
6217 | dts: currentDts,
|
6218 | data: data
|
6219 | };
|
6220 |
|
6221 | switch (data[0] & 0x1f) {
|
6222 | case 0x05:
|
6223 | event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
|
6224 | break;
|
6225 | case 0x06:
|
6226 | event.nalUnitType = 'sei_rbsp';
|
6227 | event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
|
6228 | break;
|
6229 | case 0x07:
|
6230 | event.nalUnitType = 'seq_parameter_set_rbsp';
|
6231 | event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
|
6232 | event.config = readSequenceParameterSet(event.escapedRBSP);
|
6233 | break;
|
6234 | case 0x08:
|
6235 | event.nalUnitType = 'pic_parameter_set_rbsp';
|
6236 | break;
|
6237 | case 0x09:
|
6238 | event.nalUnitType = 'access_unit_delimiter_rbsp';
|
6239 | break;
|
6240 |
|
6241 | default:
|
6242 | break;
|
6243 | }
|
6244 |
|
6245 | self.trigger('data', event);
|
6246 | });
|
6247 | nalByteStream.on('done', function () {
|
6248 | self.trigger('done');
|
6249 | });
|
6250 | nalByteStream.on('partialdone', function () {
|
6251 | self.trigger('partialdone');
|
6252 | });
|
6253 | nalByteStream.on('reset', function () {
|
6254 | self.trigger('reset');
|
6255 | });
|
6256 | nalByteStream.on('endedtimeline', function () {
|
6257 | self.trigger('endedtimeline');
|
6258 | });
|
6259 |
|
6260 | this.flush = function () {
|
6261 | nalByteStream.flush();
|
6262 | };
|
6263 |
|
6264 | this.partialFlush = function () {
|
6265 | nalByteStream.partialFlush();
|
6266 | };
|
6267 |
|
6268 | this.reset = function () {
|
6269 | nalByteStream.reset();
|
6270 | };
|
6271 |
|
6272 | this.endTimeline = function () {
|
6273 | nalByteStream.endTimeline();
|
6274 | };
|
6275 |
|
6276 | |
6277 |
|
6278 |
|
6279 |
|
6280 |
|
6281 |
|
6282 |
|
6283 |
|
6284 |
|
6285 | skipScalingList = function skipScalingList(count, expGolombDecoder) {
|
6286 | var lastScale = 8,
|
6287 | nextScale = 8,
|
6288 | j,
|
6289 | deltaScale;
|
6290 |
|
6291 | for (j = 0; j < count; j++) {
|
6292 | if (nextScale !== 0) {
|
6293 | deltaScale = expGolombDecoder.readExpGolomb();
|
6294 | nextScale = (lastScale + deltaScale + 256) % 256;
|
6295 | }
|
6296 |
|
6297 | lastScale = nextScale === 0 ? lastScale : nextScale;
|
6298 | }
|
6299 | };
|
6300 |
|
6301 | |
6302 |
|
6303 |
|
6304 |
|
6305 |
|
6306 |
|
6307 |
|
6308 |
|
6309 | discardEmulationPreventionBytes = function discardEmulationPreventionBytes(data) {
|
6310 | var length = data.byteLength,
|
6311 | emulationPreventionBytesPositions = [],
|
6312 | i = 1,
|
6313 | newLength,
|
6314 | newData;
|
6315 |
|
6316 |
|
6317 | while (i < length - 2) {
|
6318 | if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
|
6319 | emulationPreventionBytesPositions.push(i + 2);
|
6320 | i += 2;
|
6321 | } else {
|
6322 | i++;
|
6323 | }
|
6324 | }
|
6325 |
|
6326 |
|
6327 |
|
6328 | if (emulationPreventionBytesPositions.length === 0) {
|
6329 | return data;
|
6330 | }
|
6331 |
|
6332 |
|
6333 | newLength = length - emulationPreventionBytesPositions.length;
|
6334 | newData = new Uint8Array(newLength);
|
6335 | var sourceIndex = 0;
|
6336 |
|
6337 | for (i = 0; i < newLength; sourceIndex++, i++) {
|
6338 | if (sourceIndex === emulationPreventionBytesPositions[0]) {
|
6339 |
|
6340 | sourceIndex++;
|
6341 |
|
6342 | emulationPreventionBytesPositions.shift();
|
6343 | }
|
6344 | newData[i] = data[sourceIndex];
|
6345 | }
|
6346 |
|
6347 | return newData;
|
6348 | };
|
6349 |
|
6350 | |
6351 |
|
6352 |
|
6353 |
|
6354 |
|
6355 |
|
6356 |
|
6357 |
|
6358 |
|
6359 | readSequenceParameterSet = function readSequenceParameterSet(data) {
|
6360 | var frameCropLeftOffset = 0,
|
6361 | frameCropRightOffset = 0,
|
6362 | frameCropTopOffset = 0,
|
6363 | frameCropBottomOffset = 0,
|
6364 | sarScale = 1,
|
6365 | expGolombDecoder,
|
6366 | profileIdc,
|
6367 | levelIdc,
|
6368 | profileCompatibility,
|
6369 | chromaFormatIdc,
|
6370 | picOrderCntType,
|
6371 | numRefFramesInPicOrderCntCycle,
|
6372 | picWidthInMbsMinus1,
|
6373 | picHeightInMapUnitsMinus1,
|
6374 | frameMbsOnlyFlag,
|
6375 | scalingListCount,
|
6376 | sarRatio,
|
6377 | aspectRatioIdc,
|
6378 | i;
|
6379 |
|
6380 | expGolombDecoder = new expGolomb(data);
|
6381 | profileIdc = expGolombDecoder.readUnsignedByte();
|
6382 | profileCompatibility = expGolombDecoder.readUnsignedByte();
|
6383 | levelIdc = expGolombDecoder.readUnsignedByte();
|
6384 | expGolombDecoder.skipUnsignedExpGolomb();
|
6385 |
|
6386 |
|
6387 | if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
|
6388 | chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
|
6389 | if (chromaFormatIdc === 3) {
|
6390 | expGolombDecoder.skipBits(1);
|
6391 | }
|
6392 | expGolombDecoder.skipUnsignedExpGolomb();
|
6393 | expGolombDecoder.skipUnsignedExpGolomb();
|
6394 | expGolombDecoder.skipBits(1);
|
6395 | if (expGolombDecoder.readBoolean()) {
|
6396 |
|
6397 | scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
|
6398 | for (i = 0; i < scalingListCount; i++) {
|
6399 | if (expGolombDecoder.readBoolean()) {
|
6400 |
|
6401 | if (i < 6) {
|
6402 | skipScalingList(16, expGolombDecoder);
|
6403 | } else {
|
6404 | skipScalingList(64, expGolombDecoder);
|
6405 | }
|
6406 | }
|
6407 | }
|
6408 | }
|
6409 | }
|
6410 |
|
6411 | expGolombDecoder.skipUnsignedExpGolomb();
|
6412 | picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
|
6413 |
|
6414 | if (picOrderCntType === 0) {
|
6415 | expGolombDecoder.readUnsignedExpGolomb();
|
6416 | } else if (picOrderCntType === 1) {
|
6417 | expGolombDecoder.skipBits(1);
|
6418 | expGolombDecoder.skipExpGolomb();
|
6419 | expGolombDecoder.skipExpGolomb();
|
6420 | numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
|
6421 | for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
|
6422 | expGolombDecoder.skipExpGolomb();
|
6423 | }
|
6424 | }
|
6425 |
|
6426 | expGolombDecoder.skipUnsignedExpGolomb();
|
6427 | expGolombDecoder.skipBits(1);
|
6428 |
|
6429 | picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
|
6430 | picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
|
6431 |
|
6432 | frameMbsOnlyFlag = expGolombDecoder.readBits(1);
|
6433 | if (frameMbsOnlyFlag === 0) {
|
6434 | expGolombDecoder.skipBits(1);
|
6435 | }
|
6436 |
|
6437 | expGolombDecoder.skipBits(1);
|
6438 | if (expGolombDecoder.readBoolean()) {
|
6439 |
|
6440 | frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
|
6441 | frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
|
6442 | frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
|
6443 | frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
|
6444 | }
|
6445 | if (expGolombDecoder.readBoolean()) {
|
6446 |
|
6447 | if (expGolombDecoder.readBoolean()) {
|
6448 |
|
6449 | aspectRatioIdc = expGolombDecoder.readUnsignedByte();
|
6450 | switch (aspectRatioIdc) {
|
6451 | case 1:
|
6452 | sarRatio = [1, 1];break;
|
6453 | case 2:
|
6454 | sarRatio = [12, 11];break;
|
6455 | case 3:
|
6456 | sarRatio = [10, 11];break;
|
6457 | case 4:
|
6458 | sarRatio = [16, 11];break;
|
6459 | case 5:
|
6460 | sarRatio = [40, 33];break;
|
6461 | case 6:
|
6462 | sarRatio = [24, 11];break;
|
6463 | case 7:
|
6464 | sarRatio = [20, 11];break;
|
6465 | case 8:
|
6466 | sarRatio = [32, 11];break;
|
6467 | case 9:
|
6468 | sarRatio = [80, 33];break;
|
6469 | case 10:
|
6470 | sarRatio = [18, 11];break;
|
6471 | case 11:
|
6472 | sarRatio = [15, 11];break;
|
6473 | case 12:
|
6474 | sarRatio = [64, 33];break;
|
6475 | case 13:
|
6476 | sarRatio = [160, 99];break;
|
6477 | case 14:
|
6478 | sarRatio = [4, 3];break;
|
6479 | case 15:
|
6480 | sarRatio = [3, 2];break;
|
6481 | case 16:
|
6482 | sarRatio = [2, 1];break;
|
6483 | case 255:
|
6484 | {
|
6485 | sarRatio = [expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte(), expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte()];
|
6486 | break;
|
6487 | }
|
6488 | }
|
6489 | if (sarRatio) {
|
6490 | sarScale = sarRatio[0] / sarRatio[1];
|
6491 | }
|
6492 | }
|
6493 | }
|
6494 | return {
|
6495 | profileIdc: profileIdc,
|
6496 | levelIdc: levelIdc,
|
6497 | profileCompatibility: profileCompatibility,
|
6498 | width: Math.ceil(((picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
|
6499 | height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2,
|
6500 | sarRatio: sarRatio
|
6501 | };
|
6502 | };
|
6503 | };
|
6504 | _H264Stream.prototype = new stream();
|
6505 |
|
6506 | var h264 = {
|
6507 | H264Stream: _H264Stream,
|
6508 | NalByteStream: _NalByteStream
|
6509 | };
|
6510 |
|
6511 | |
6512 |
|
6513 |
|
6514 |
|
6515 |
|
6516 |
|
6517 |
|
6518 |
|
6519 |
|
6520 | var ADTS_SAMPLING_FREQUENCIES$1 = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
6521 |
|
6522 | var isLikelyAacData = function isLikelyAacData(data) {
|
6523 | if (data[0] === 'I'.charCodeAt(0) && data[1] === 'D'.charCodeAt(0) && data[2] === '3'.charCodeAt(0)) {
|
6524 | return true;
|
6525 | }
|
6526 | return false;
|
6527 | };
|
6528 |
|
6529 | var parseSyncSafeInteger$1 = function parseSyncSafeInteger(data) {
|
6530 | return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
|
6531 | };
|
6532 |
|
6533 |
|
6534 |
|
6535 | var percentEncode$1 = function percentEncode(bytes, start, end) {
|
6536 | var i,
|
6537 | result = '';
|
6538 | for (i = start; i < end; i++) {
|
6539 | result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
|
6540 | }
|
6541 | return result;
|
6542 | };
|
6543 |
|
6544 |
|
6545 |
|
6546 | var parseIso88591$1 = function parseIso88591(bytes, start, end) {
|
6547 | return unescape(percentEncode$1(bytes, start, end));
|
6548 | };
|
6549 |
|
6550 | var parseId3TagSize = function parseId3TagSize(header, byteIndex) {
|
6551 | var returnSize = header[byteIndex + 6] << 21 | header[byteIndex + 7] << 14 | header[byteIndex + 8] << 7 | header[byteIndex + 9],
|
6552 | flags = header[byteIndex + 5],
|
6553 | footerPresent = (flags & 16) >> 4;
|
6554 |
|
6555 | if (footerPresent) {
|
6556 | return returnSize + 20;
|
6557 | }
|
6558 | return returnSize + 10;
|
6559 | };
|
6560 |
|
6561 | var parseAdtsSize = function parseAdtsSize(header, byteIndex) {
|
6562 | var lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
|
6563 | middle = header[byteIndex + 4] << 3,
|
6564 | highTwo = header[byteIndex + 3] & 0x3 << 11;
|
6565 |
|
6566 | return highTwo | middle | lowThree;
|
6567 | };
|
6568 |
|
6569 | var parseType = function parseType(header, byteIndex) {
|
6570 | if (header[byteIndex] === 'I'.charCodeAt(0) && header[byteIndex + 1] === 'D'.charCodeAt(0) && header[byteIndex + 2] === '3'.charCodeAt(0)) {
|
6571 | return 'timed-metadata';
|
6572 | } else if (header[byteIndex] & 0xff === 0xff && (header[byteIndex + 1] & 0xf0) === 0xf0) {
|
6573 | return 'audio';
|
6574 | }
|
6575 | return null;
|
6576 | };
|
6577 |
|
6578 | var parseSampleRate = function parseSampleRate(packet) {
|
6579 | var i = 0;
|
6580 |
|
6581 | while (i + 5 < packet.length) {
|
6582 | if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
|
6583 |
|
6584 |
|
6585 | i++;
|
6586 | continue;
|
6587 | }
|
6588 | return ADTS_SAMPLING_FREQUENCIES$1[(packet[i + 2] & 0x3c) >>> 2];
|
6589 | }
|
6590 |
|
6591 | return null;
|
6592 | };
|
6593 |
|
6594 | var parseAacTimestamp = function parseAacTimestamp(packet) {
|
6595 | var frameStart, frameSize, frame, frameHeader;
|
6596 |
|
6597 |
|
6598 | frameStart = 10;
|
6599 | if (packet[5] & 0x40) {
|
6600 |
|
6601 | frameStart += 4;
|
6602 | frameStart += parseSyncSafeInteger$1(packet.subarray(10, 14));
|
6603 | }
|
6604 |
|
6605 |
|
6606 |
|
6607 | do {
|
6608 |
|
6609 | frameSize = parseSyncSafeInteger$1(packet.subarray(frameStart + 4, frameStart + 8));
|
6610 | if (frameSize < 1) {
|
6611 | return null;
|
6612 | }
|
6613 | frameHeader = String.fromCharCode(packet[frameStart], packet[frameStart + 1], packet[frameStart + 2], packet[frameStart + 3]);
|
6614 |
|
6615 | if (frameHeader === 'PRIV') {
|
6616 | frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
|
6617 |
|
6618 | for (var i = 0; i < frame.byteLength; i++) {
|
6619 | if (frame[i] === 0) {
|
6620 | var owner = parseIso88591$1(frame, 0, i);
|
6621 | if (owner === 'com.apple.streaming.transportStreamTimestamp') {
|
6622 | var d = frame.subarray(i + 1);
|
6623 | var size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
|
6624 | size *= 4;
|
6625 | size += d[7] & 0x03;
|
6626 |
|
6627 | return size;
|
6628 | }
|
6629 | break;
|
6630 | }
|
6631 | }
|
6632 | }
|
6633 |
|
6634 | frameStart += 10;
|
6635 | frameStart += frameSize;
|
6636 | } while (frameStart < packet.byteLength);
|
6637 | return null;
|
6638 | };
|
6639 |
|
6640 | var utils = {
|
6641 | isLikelyAacData: isLikelyAacData,
|
6642 | parseId3TagSize: parseId3TagSize,
|
6643 | parseAdtsSize: parseAdtsSize,
|
6644 | parseType: parseType,
|
6645 | parseSampleRate: parseSampleRate,
|
6646 | parseAacTimestamp: parseAacTimestamp
|
6647 | };
|
6648 |
|
6649 |
|
6650 | var _AacStream;
|
6651 |
|
6652 | |
6653 |
|
6654 |
|
6655 |
|
6656 | _AacStream = function AacStream() {
|
6657 | var everything = new Uint8Array(),
|
6658 | timeStamp = 0;
|
6659 |
|
6660 | _AacStream.prototype.init.call(this);
|
6661 |
|
6662 | this.setTimestamp = function (timestamp) {
|
6663 | timeStamp = timestamp;
|
6664 | };
|
6665 |
|
6666 | this.push = function (bytes) {
|
6667 | var frameSize = 0,
|
6668 | byteIndex = 0,
|
6669 | bytesLeft,
|
6670 | chunk,
|
6671 | packet,
|
6672 | tempLength;
|
6673 |
|
6674 |
|
6675 |
|
6676 | if (everything.length) {
|
6677 | tempLength = everything.length;
|
6678 | everything = new Uint8Array(bytes.byteLength + tempLength);
|
6679 | everything.set(everything.subarray(0, tempLength));
|
6680 | everything.set(bytes, tempLength);
|
6681 | } else {
|
6682 | everything = bytes;
|
6683 | }
|
6684 |
|
6685 | while (everything.length - byteIndex >= 3) {
|
6686 | if (everything[byteIndex] === 'I'.charCodeAt(0) && everything[byteIndex + 1] === 'D'.charCodeAt(0) && everything[byteIndex + 2] === '3'.charCodeAt(0)) {
|
6687 |
|
6688 |
|
6689 |
|
6690 | if (everything.length - byteIndex < 10) {
|
6691 | break;
|
6692 | }
|
6693 |
|
6694 |
|
6695 | frameSize = utils.parseId3TagSize(everything, byteIndex);
|
6696 |
|
6697 |
|
6698 |
|
6699 |
|
6700 | if (byteIndex + frameSize > everything.length) {
|
6701 | break;
|
6702 | }
|
6703 | chunk = {
|
6704 | type: 'timed-metadata',
|
6705 | data: everything.subarray(byteIndex, byteIndex + frameSize)
|
6706 | };
|
6707 | this.trigger('data', chunk);
|
6708 | byteIndex += frameSize;
|
6709 | continue;
|
6710 | } else if ((everything[byteIndex] & 0xff) === 0xff && (everything[byteIndex + 1] & 0xf0) === 0xf0) {
|
6711 |
|
6712 |
|
6713 |
|
6714 | if (everything.length - byteIndex < 7) {
|
6715 | break;
|
6716 | }
|
6717 |
|
6718 | frameSize = utils.parseAdtsSize(everything, byteIndex);
|
6719 |
|
6720 |
|
6721 |
|
6722 | if (byteIndex + frameSize > everything.length) {
|
6723 | break;
|
6724 | }
|
6725 |
|
6726 | packet = {
|
6727 | type: 'audio',
|
6728 | data: everything.subarray(byteIndex, byteIndex + frameSize),
|
6729 | pts: timeStamp,
|
6730 | dts: timeStamp
|
6731 | };
|
6732 | this.trigger('data', packet);
|
6733 | byteIndex += frameSize;
|
6734 | continue;
|
6735 | }
|
6736 | byteIndex++;
|
6737 | }
|
6738 | bytesLeft = everything.length - byteIndex;
|
6739 |
|
6740 | if (bytesLeft > 0) {
|
6741 | everything = everything.subarray(byteIndex);
|
6742 | } else {
|
6743 | everything = new Uint8Array();
|
6744 | }
|
6745 | };
|
6746 |
|
6747 | this.reset = function () {
|
6748 | everything = new Uint8Array();
|
6749 | this.trigger('reset');
|
6750 | };
|
6751 |
|
6752 | this.endTimeline = function () {
|
6753 | everything = new Uint8Array();
|
6754 | this.trigger('endedtimeline');
|
6755 | };
|
6756 | };
|
6757 |
|
6758 | _AacStream.prototype = new stream();
|
6759 |
|
6760 | var aac = _AacStream;
|
6761 |
|
6762 | var H264Stream = h264.H264Stream;
|
6763 |
|
6764 | var isLikelyAacData$1 = utils.isLikelyAacData;
|
6765 | var ONE_SECOND_IN_TS$3 = clock.ONE_SECOND_IN_TS;
|
6766 |
|
6767 |
|
6768 | var AUDIO_PROPERTIES = ['audioobjecttype', 'channelcount', 'samplerate', 'samplingfrequencyindex', 'samplesize'];
|
6769 |
|
6770 | var VIDEO_PROPERTIES = ['width', 'height', 'profileIdc', 'levelIdc', 'profileCompatibility', 'sarRatio'];
|
6771 |
|
6772 |
|
6773 | var _VideoSegmentStream, _AudioSegmentStream, _Transmuxer, _CoalesceStream;
|
6774 |
|
6775 | |
6776 |
|
6777 |
|
6778 | var arrayEquals = function arrayEquals(a, b) {
|
6779 | var i;
|
6780 |
|
6781 | if (a.length !== b.length) {
|
6782 | return false;
|
6783 | }
|
6784 |
|
6785 |
|
6786 | for (i = 0; i < a.length; i++) {
|
6787 | if (a[i] !== b[i]) {
|
6788 | return false;
|
6789 | }
|
6790 | }
|
6791 |
|
6792 | return true;
|
6793 | };
|
6794 |
|
6795 | var generateVideoSegmentTimingInfo = function generateVideoSegmentTimingInfo(baseMediaDecodeTime, startDts, startPts, endDts, endPts, prependedContentDuration) {
|
6796 | var ptsOffsetFromDts = startPts - startDts,
|
6797 | decodeDuration = endDts - startDts,
|
6798 | presentationDuration = endPts - startPts;
|
6799 |
|
6800 |
|
6801 |
|
6802 |
|
6803 |
|
6804 | return {
|
6805 | start: {
|
6806 | dts: baseMediaDecodeTime,
|
6807 | pts: baseMediaDecodeTime + ptsOffsetFromDts
|
6808 | },
|
6809 | end: {
|
6810 | dts: baseMediaDecodeTime + decodeDuration,
|
6811 | pts: baseMediaDecodeTime + presentationDuration
|
6812 | },
|
6813 | prependedContentDuration: prependedContentDuration,
|
6814 | baseMediaDecodeTime: baseMediaDecodeTime
|
6815 | };
|
6816 | };
|
6817 |
|
6818 | |
6819 |
|
6820 |
|
6821 |
|
6822 |
|
6823 |
|
6824 |
|
6825 |
|
6826 |
|
6827 | _AudioSegmentStream = function AudioSegmentStream(track, options) {
|
6828 | var adtsFrames = [],
|
6829 | sequenceNumber = 0,
|
6830 | earliestAllowedDts = 0,
|
6831 | audioAppendStartTs = 0,
|
6832 | videoBaseMediaDecodeTime = Infinity;
|
6833 |
|
6834 | options = options || {};
|
6835 |
|
6836 | _AudioSegmentStream.prototype.init.call(this);
|
6837 |
|
6838 | this.push = function (data) {
|
6839 | trackDecodeInfo.collectDtsInfo(track, data);
|
6840 |
|
6841 | if (track) {
|
6842 | AUDIO_PROPERTIES.forEach(function (prop) {
|
6843 | track[prop] = data[prop];
|
6844 | });
|
6845 | }
|
6846 |
|
6847 |
|
6848 | adtsFrames.push(data);
|
6849 | };
|
6850 |
|
6851 | this.setEarliestDts = function (earliestDts) {
|
6852 | earliestAllowedDts = earliestDts - track.timelineStartInfo.baseMediaDecodeTime;
|
6853 | };
|
6854 |
|
6855 | this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
|
6856 | videoBaseMediaDecodeTime = baseMediaDecodeTime;
|
6857 | };
|
6858 |
|
6859 | this.setAudioAppendStart = function (timestamp) {
|
6860 | audioAppendStartTs = timestamp;
|
6861 | };
|
6862 |
|
6863 | this.flush = function () {
|
6864 | var frames, moof, mdat, boxes, frameDuration;
|
6865 |
|
6866 |
|
6867 | if (adtsFrames.length === 0) {
|
6868 | this.trigger('done', 'AudioSegmentStream');
|
6869 | return;
|
6870 | }
|
6871 |
|
6872 | frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
|
6873 | track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
|
6874 |
|
6875 | audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime);
|
6876 |
|
6877 |
|
6878 |
|
6879 | track.samples = audioFrameUtils.generateSampleTable(frames);
|
6880 |
|
6881 |
|
6882 | mdat = mp4Generator.mdat(audioFrameUtils.concatenateFrameData(frames));
|
6883 |
|
6884 | adtsFrames = [];
|
6885 |
|
6886 | moof = mp4Generator.moof(sequenceNumber, [track]);
|
6887 | boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
|
6888 |
|
6889 |
|
6890 | sequenceNumber++;
|
6891 |
|
6892 | boxes.set(moof);
|
6893 | boxes.set(mdat, moof.byteLength);
|
6894 |
|
6895 | trackDecodeInfo.clearDtsInfo(track);
|
6896 |
|
6897 | frameDuration = Math.ceil(ONE_SECOND_IN_TS$3 * 1024 / track.samplerate);
|
6898 |
|
6899 |
|
6900 |
|
6901 |
|
6902 |
|
6903 | if (frames.length) {
|
6904 | this.trigger('timingInfo', {
|
6905 | start: frames[0].pts,
|
6906 | end: frames[0].pts + frames.length * frameDuration
|
6907 | });
|
6908 | }
|
6909 | this.trigger('data', { track: track, boxes: boxes });
|
6910 | this.trigger('done', 'AudioSegmentStream');
|
6911 | };
|
6912 |
|
6913 | this.reset = function () {
|
6914 | trackDecodeInfo.clearDtsInfo(track);
|
6915 | adtsFrames = [];
|
6916 | this.trigger('reset');
|
6917 | };
|
6918 | };
|
6919 |
|
6920 | _AudioSegmentStream.prototype = new stream();
|
6921 |
|
6922 | |
6923 |
|
6924 |
|
6925 |
|
6926 |
|
6927 |
|
6928 |
|
6929 |
|
6930 |
|
6931 |
|
6932 |
|
6933 | _VideoSegmentStream = function VideoSegmentStream(track, options) {
|
6934 | var sequenceNumber = 0,
|
6935 | nalUnits = [],
|
6936 | gopsToAlignWith = [],
|
6937 | config,
|
6938 | pps;
|
6939 |
|
6940 | options = options || {};
|
6941 |
|
6942 | _VideoSegmentStream.prototype.init.call(this);
|
6943 |
|
6944 | delete track.minPTS;
|
6945 |
|
6946 | this.gopCache_ = [];
|
6947 |
|
6948 | |
6949 |
|
6950 |
|
6951 |
|
6952 |
|
6953 |
|
6954 |
|
6955 |
|
6956 | this.push = function (nalUnit) {
|
6957 | trackDecodeInfo.collectDtsInfo(track, nalUnit);
|
6958 |
|
6959 |
|
6960 | if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
|
6961 | config = nalUnit.config;
|
6962 | track.sps = [nalUnit.data];
|
6963 |
|
6964 | VIDEO_PROPERTIES.forEach(function (prop) {
|
6965 | track[prop] = config[prop];
|
6966 | }, this);
|
6967 | }
|
6968 |
|
6969 | if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
|
6970 | pps = nalUnit.data;
|
6971 | track.pps = [nalUnit.data];
|
6972 | }
|
6973 |
|
6974 |
|
6975 | nalUnits.push(nalUnit);
|
6976 | };
|
6977 |
|
6978 | |
6979 |
|
6980 |
|
6981 |
|
6982 | this.flush = function () {
|
6983 | var frames,
|
6984 | gopForFusion,
|
6985 | gops,
|
6986 | moof,
|
6987 | mdat,
|
6988 | boxes,
|
6989 | prependedContentDuration = 0,
|
6990 | firstGop,
|
6991 | lastGop;
|
6992 |
|
6993 |
|
6994 |
|
6995 | while (nalUnits.length) {
|
6996 | if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
|
6997 | break;
|
6998 | }
|
6999 | nalUnits.shift();
|
7000 | }
|
7001 |
|
7002 |
|
7003 | if (nalUnits.length === 0) {
|
7004 | this.resetStream_();
|
7005 | this.trigger('done', 'VideoSegmentStream');
|
7006 | return;
|
7007 | }
|
7008 |
|
7009 |
|
7010 |
|
7011 |
|
7012 | frames = frameUtils.groupNalsIntoFrames(nalUnits);
|
7013 | gops = frameUtils.groupFramesIntoGops(frames);
|
7014 |
|
7015 |
|
7016 |
|
7017 |
|
7018 |
|
7019 |
|
7020 |
|
7021 |
|
7022 |
|
7023 |
|
7024 |
|
7025 |
|
7026 |
|
7027 |
|
7028 |
|
7029 |
|
7030 |
|
7031 |
|
7032 |
|
7033 | if (!gops[0][0].keyFrame) {
|
7034 |
|
7035 | gopForFusion = this.getGopForFusion_(nalUnits[0], track);
|
7036 |
|
7037 | if (gopForFusion) {
|
7038 |
|
7039 |
|
7040 | prependedContentDuration = gopForFusion.duration;
|
7041 |
|
7042 | gops.unshift(gopForFusion);
|
7043 |
|
7044 |
|
7045 | gops.byteLength += gopForFusion.byteLength;
|
7046 | gops.nalCount += gopForFusion.nalCount;
|
7047 | gops.pts = gopForFusion.pts;
|
7048 | gops.dts = gopForFusion.dts;
|
7049 | gops.duration += gopForFusion.duration;
|
7050 | } else {
|
7051 |
|
7052 | gops = frameUtils.extendFirstKeyFrame(gops);
|
7053 | }
|
7054 | }
|
7055 |
|
7056 |
|
7057 | if (gopsToAlignWith.length) {
|
7058 | var alignedGops;
|
7059 |
|
7060 | if (options.alignGopsAtEnd) {
|
7061 | alignedGops = this.alignGopsAtEnd_(gops);
|
7062 | } else {
|
7063 | alignedGops = this.alignGopsAtStart_(gops);
|
7064 | }
|
7065 |
|
7066 | if (!alignedGops) {
|
7067 |
|
7068 | this.gopCache_.unshift({
|
7069 | gop: gops.pop(),
|
7070 | pps: track.pps,
|
7071 | sps: track.sps
|
7072 | });
|
7073 |
|
7074 |
|
7075 | this.gopCache_.length = Math.min(6, this.gopCache_.length);
|
7076 |
|
7077 |
|
7078 | nalUnits = [];
|
7079 |
|
7080 |
|
7081 | this.resetStream_();
|
7082 | this.trigger('done', 'VideoSegmentStream');
|
7083 | return;
|
7084 | }
|
7085 |
|
7086 |
|
7087 |
|
7088 | trackDecodeInfo.clearDtsInfo(track);
|
7089 |
|
7090 | gops = alignedGops;
|
7091 | }
|
7092 |
|
7093 | trackDecodeInfo.collectDtsInfo(track, gops);
|
7094 |
|
7095 |
|
7096 |
|
7097 | track.samples = frameUtils.generateSampleTable(gops);
|
7098 |
|
7099 |
|
7100 | mdat = mp4Generator.mdat(frameUtils.concatenateNalData(gops));
|
7101 |
|
7102 | track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
|
7103 |
|
7104 | this.trigger('processedGopsInfo', gops.map(function (gop) {
|
7105 | return {
|
7106 | pts: gop.pts,
|
7107 | dts: gop.dts,
|
7108 | byteLength: gop.byteLength
|
7109 | };
|
7110 | }));
|
7111 |
|
7112 | firstGop = gops[0];
|
7113 | lastGop = gops[gops.length - 1];
|
7114 |
|
7115 | this.trigger('segmentTimingInfo', generateVideoSegmentTimingInfo(track.baseMediaDecodeTime, firstGop.dts, firstGop.pts, lastGop.dts + lastGop.duration, lastGop.pts + lastGop.duration, prependedContentDuration));
|
7116 |
|
7117 | this.trigger('timingInfo', {
|
7118 | start: gops[0].pts,
|
7119 | end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
|
7120 | });
|
7121 |
|
7122 |
|
7123 | this.gopCache_.unshift({
|
7124 | gop: gops.pop(),
|
7125 | pps: track.pps,
|
7126 | sps: track.sps
|
7127 | });
|
7128 |
|
7129 |
|
7130 | this.gopCache_.length = Math.min(6, this.gopCache_.length);
|
7131 |
|
7132 |
|
7133 | nalUnits = [];
|
7134 |
|
7135 | this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
|
7136 | this.trigger('timelineStartInfo', track.timelineStartInfo);
|
7137 |
|
7138 | moof = mp4Generator.moof(sequenceNumber, [track]);
|
7139 |
|
7140 |
|
7141 |
|
7142 | boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
|
7143 |
|
7144 |
|
7145 | sequenceNumber++;
|
7146 |
|
7147 | boxes.set(moof);
|
7148 | boxes.set(mdat, moof.byteLength);
|
7149 |
|
7150 | this.trigger('data', { track: track, boxes: boxes });
|
7151 |
|
7152 | this.resetStream_();
|
7153 |
|
7154 |
|
7155 | this.trigger('done', 'VideoSegmentStream');
|
7156 | };
|
7157 |
|
7158 | this.reset = function () {
|
7159 | this.resetStream_();
|
7160 | nalUnits = [];
|
7161 | this.gopCache_.length = 0;
|
7162 | gopsToAlignWith.length = 0;
|
7163 | this.trigger('reset');
|
7164 | };
|
7165 |
|
7166 | this.resetStream_ = function () {
|
7167 | trackDecodeInfo.clearDtsInfo(track);
|
7168 |
|
7169 |
|
7170 |
|
7171 | config = undefined;
|
7172 | pps = undefined;
|
7173 | };
|
7174 |
|
7175 |
|
7176 |
|
7177 | this.getGopForFusion_ = function (nalUnit) {
|
7178 | var halfSecond = 45000,
|
7179 |
|
7180 |
|
7181 | allowableOverlap = 10000,
|
7182 |
|
7183 |
|
7184 | nearestDistance = Infinity,
|
7185 | dtsDistance,
|
7186 | nearestGopObj,
|
7187 | currentGop,
|
7188 | currentGopObj,
|
7189 | i;
|
7190 |
|
7191 |
|
7192 | for (i = 0; i < this.gopCache_.length; i++) {
|
7193 | currentGopObj = this.gopCache_[i];
|
7194 | currentGop = currentGopObj.gop;
|
7195 |
|
7196 |
|
7197 | if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) || !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
|
7198 | continue;
|
7199 | }
|
7200 |
|
7201 |
|
7202 | if (currentGop.dts < track.timelineStartInfo.dts) {
|
7203 | continue;
|
7204 | }
|
7205 |
|
7206 |
|
7207 | dtsDistance = nalUnit.dts - currentGop.dts - currentGop.duration;
|
7208 |
|
7209 |
|
7210 |
|
7211 | if (dtsDistance >= -allowableOverlap && dtsDistance <= halfSecond) {
|
7212 |
|
7213 |
|
7214 |
|
7215 | if (!nearestGopObj || nearestDistance > dtsDistance) {
|
7216 | nearestGopObj = currentGopObj;
|
7217 | nearestDistance = dtsDistance;
|
7218 | }
|
7219 | }
|
7220 | }
|
7221 |
|
7222 | if (nearestGopObj) {
|
7223 | return nearestGopObj.gop;
|
7224 | }
|
7225 | return null;
|
7226 | };
|
7227 |
|
7228 |
|
7229 |
|
7230 | this.alignGopsAtStart_ = function (gops) {
|
7231 | var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
|
7232 |
|
7233 | byteLength = gops.byteLength;
|
7234 | nalCount = gops.nalCount;
|
7235 | duration = gops.duration;
|
7236 | alignIndex = gopIndex = 0;
|
7237 |
|
7238 | while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
|
7239 | align = gopsToAlignWith[alignIndex];
|
7240 | gop = gops[gopIndex];
|
7241 |
|
7242 | if (align.pts === gop.pts) {
|
7243 | break;
|
7244 | }
|
7245 |
|
7246 | if (gop.pts > align.pts) {
|
7247 |
|
7248 |
|
7249 | alignIndex++;
|
7250 | continue;
|
7251 | }
|
7252 |
|
7253 |
|
7254 |
|
7255 | gopIndex++;
|
7256 | byteLength -= gop.byteLength;
|
7257 | nalCount -= gop.nalCount;
|
7258 | duration -= gop.duration;
|
7259 | }
|
7260 |
|
7261 | if (gopIndex === 0) {
|
7262 |
|
7263 | return gops;
|
7264 | }
|
7265 |
|
7266 | if (gopIndex === gops.length) {
|
7267 |
|
7268 | return null;
|
7269 | }
|
7270 |
|
7271 | alignedGops = gops.slice(gopIndex);
|
7272 | alignedGops.byteLength = byteLength;
|
7273 | alignedGops.duration = duration;
|
7274 | alignedGops.nalCount = nalCount;
|
7275 | alignedGops.pts = alignedGops[0].pts;
|
7276 | alignedGops.dts = alignedGops[0].dts;
|
7277 |
|
7278 | return alignedGops;
|
7279 | };
|
7280 |
|
7281 |
|
7282 |
|
7283 | this.alignGopsAtEnd_ = function (gops) {
|
7284 | var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
|
7285 |
|
7286 | alignIndex = gopsToAlignWith.length - 1;
|
7287 | gopIndex = gops.length - 1;
|
7288 | alignEndIndex = null;
|
7289 | matchFound = false;
|
7290 |
|
7291 | while (alignIndex >= 0 && gopIndex >= 0) {
|
7292 | align = gopsToAlignWith[alignIndex];
|
7293 | gop = gops[gopIndex];
|
7294 |
|
7295 | if (align.pts === gop.pts) {
|
7296 | matchFound = true;
|
7297 | break;
|
7298 | }
|
7299 |
|
7300 | if (align.pts > gop.pts) {
|
7301 | alignIndex--;
|
7302 | continue;
|
7303 | }
|
7304 |
|
7305 | if (alignIndex === gopsToAlignWith.length - 1) {
|
7306 |
|
7307 |
|
7308 |
|
7309 | alignEndIndex = gopIndex;
|
7310 | }
|
7311 |
|
7312 | gopIndex--;
|
7313 | }
|
7314 |
|
7315 | if (!matchFound && alignEndIndex === null) {
|
7316 | return null;
|
7317 | }
|
7318 |
|
7319 | var trimIndex;
|
7320 |
|
7321 | if (matchFound) {
|
7322 | trimIndex = gopIndex;
|
7323 | } else {
|
7324 | trimIndex = alignEndIndex;
|
7325 | }
|
7326 |
|
7327 | if (trimIndex === 0) {
|
7328 | return gops;
|
7329 | }
|
7330 |
|
7331 | var alignedGops = gops.slice(trimIndex);
|
7332 | var metadata = alignedGops.reduce(function (total, gop) {
|
7333 | total.byteLength += gop.byteLength;
|
7334 | total.duration += gop.duration;
|
7335 | total.nalCount += gop.nalCount;
|
7336 | return total;
|
7337 | }, { byteLength: 0, duration: 0, nalCount: 0 });
|
7338 |
|
7339 | alignedGops.byteLength = metadata.byteLength;
|
7340 | alignedGops.duration = metadata.duration;
|
7341 | alignedGops.nalCount = metadata.nalCount;
|
7342 | alignedGops.pts = alignedGops[0].pts;
|
7343 | alignedGops.dts = alignedGops[0].dts;
|
7344 |
|
7345 | return alignedGops;
|
7346 | };
|
7347 |
|
7348 | this.alignGopsWith = function (newGopsToAlignWith) {
|
7349 | gopsToAlignWith = newGopsToAlignWith;
|
7350 | };
|
7351 | };
|
7352 |
|
7353 | _VideoSegmentStream.prototype = new stream();
|
7354 |
|
7355 | |
7356 |
|
7357 |
|
7358 |
|
7359 |
|
7360 |
|
7361 |
|
7362 |
|
7363 | _CoalesceStream = function CoalesceStream(options, metadataStream) {
|
7364 |
|
7365 |
|
7366 |
|
7367 | this.numberOfTracks = 0;
|
7368 | this.metadataStream = metadataStream;
|
7369 |
|
7370 | options = options || {};
|
7371 |
|
7372 | if (typeof options.remux !== 'undefined') {
|
7373 | this.remuxTracks = !!options.remux;
|
7374 | } else {
|
7375 | this.remuxTracks = true;
|
7376 | }
|
7377 |
|
7378 | if (typeof options.keepOriginalTimestamps === 'boolean') {
|
7379 | this.keepOriginalTimestamps = options.keepOriginalTimestamps;
|
7380 | } else {
|
7381 | this.keepOriginalTimestamps = false;
|
7382 | }
|
7383 |
|
7384 | this.pendingTracks = [];
|
7385 | this.videoTrack = null;
|
7386 | this.pendingBoxes = [];
|
7387 | this.pendingCaptions = [];
|
7388 | this.pendingMetadata = [];
|
7389 | this.pendingBytes = 0;
|
7390 | this.emittedTracks = 0;
|
7391 |
|
7392 | _CoalesceStream.prototype.init.call(this);
|
7393 |
|
7394 |
|
7395 | this.push = function (output) {
|
7396 |
|
7397 |
|
7398 | if (output.text) {
|
7399 | return this.pendingCaptions.push(output);
|
7400 | }
|
7401 |
|
7402 | if (output.frames) {
|
7403 | return this.pendingMetadata.push(output);
|
7404 | }
|
7405 |
|
7406 |
|
7407 |
|
7408 |
|
7409 | this.pendingTracks.push(output.track);
|
7410 | this.pendingBytes += output.boxes.byteLength;
|
7411 |
|
7412 |
|
7413 |
|
7414 |
|
7415 |
|
7416 |
|
7417 |
|
7418 | if (output.track.type === 'video') {
|
7419 | this.videoTrack = output.track;
|
7420 | this.pendingBoxes.push(output.boxes);
|
7421 | }
|
7422 | if (output.track.type === 'audio') {
|
7423 | this.audioTrack = output.track;
|
7424 | this.pendingBoxes.unshift(output.boxes);
|
7425 | }
|
7426 | };
|
7427 | };
|
7428 |
|
7429 | _CoalesceStream.prototype = new stream();
|
7430 | _CoalesceStream.prototype.flush = function (flushSource) {
|
7431 | var offset = 0,
|
7432 | event = {
|
7433 | captions: [],
|
7434 | captionStreams: {},
|
7435 | metadata: [],
|
7436 | info: {}
|
7437 | },
|
7438 | caption,
|
7439 | id3,
|
7440 | initSegment,
|
7441 | timelineStartPts = 0,
|
7442 | i;
|
7443 |
|
7444 | if (this.pendingTracks.length < this.numberOfTracks) {
|
7445 | if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
|
7446 |
|
7447 |
|
7448 |
|
7449 | return;
|
7450 | } else if (this.remuxTracks) {
|
7451 |
|
7452 |
|
7453 | return;
|
7454 | } else if (this.pendingTracks.length === 0) {
|
7455 |
|
7456 |
|
7457 |
|
7458 |
|
7459 |
|
7460 |
|
7461 | this.emittedTracks++;
|
7462 |
|
7463 | if (this.emittedTracks >= this.numberOfTracks) {
|
7464 | this.trigger('done');
|
7465 | this.emittedTracks = 0;
|
7466 | }
|
7467 | return;
|
7468 | }
|
7469 | }
|
7470 |
|
7471 | if (this.videoTrack) {
|
7472 | timelineStartPts = this.videoTrack.timelineStartInfo.pts;
|
7473 | VIDEO_PROPERTIES.forEach(function (prop) {
|
7474 | event.info[prop] = this.videoTrack[prop];
|
7475 | }, this);
|
7476 | } else if (this.audioTrack) {
|
7477 | timelineStartPts = this.audioTrack.timelineStartInfo.pts;
|
7478 | AUDIO_PROPERTIES.forEach(function (prop) {
|
7479 | event.info[prop] = this.audioTrack[prop];
|
7480 | }, this);
|
7481 | }
|
7482 |
|
7483 | if (this.videoTrack || this.audioTrack) {
|
7484 | if (this.pendingTracks.length === 1) {
|
7485 | event.type = this.pendingTracks[0].type;
|
7486 | } else {
|
7487 | event.type = 'combined';
|
7488 | }
|
7489 |
|
7490 | this.emittedTracks += this.pendingTracks.length;
|
7491 |
|
7492 | initSegment = mp4Generator.initSegment(this.pendingTracks);
|
7493 |
|
7494 |
|
7495 | event.initSegment = new Uint8Array(initSegment.byteLength);
|
7496 |
|
7497 |
|
7498 |
|
7499 | event.initSegment.set(initSegment);
|
7500 |
|
7501 |
|
7502 | event.data = new Uint8Array(this.pendingBytes);
|
7503 |
|
7504 |
|
7505 | for (i = 0; i < this.pendingBoxes.length; i++) {
|
7506 | event.data.set(this.pendingBoxes[i], offset);
|
7507 | offset += this.pendingBoxes[i].byteLength;
|
7508 | }
|
7509 |
|
7510 |
|
7511 |
|
7512 | for (i = 0; i < this.pendingCaptions.length; i++) {
|
7513 | caption = this.pendingCaptions[i];
|
7514 | caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
|
7515 | caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
|
7516 |
|
7517 | event.captionStreams[caption.stream] = true;
|
7518 | event.captions.push(caption);
|
7519 | }
|
7520 |
|
7521 |
|
7522 |
|
7523 | for (i = 0; i < this.pendingMetadata.length; i++) {
|
7524 | id3 = this.pendingMetadata[i];
|
7525 | id3.cueTime = clock.metadataTsToSeconds(id3.pts, timelineStartPts, this.keepOriginalTimestamps);
|
7526 |
|
7527 | event.metadata.push(id3);
|
7528 | }
|
7529 |
|
7530 |
|
7531 |
|
7532 | event.metadata.dispatchType = this.metadataStream.dispatchType;
|
7533 |
|
7534 |
|
7535 | this.pendingTracks.length = 0;
|
7536 | this.videoTrack = null;
|
7537 | this.pendingBoxes.length = 0;
|
7538 | this.pendingCaptions.length = 0;
|
7539 | this.pendingBytes = 0;
|
7540 | this.pendingMetadata.length = 0;
|
7541 |
|
7542 |
|
7543 |
|
7544 |
|
7545 | this.trigger('data', event);
|
7546 |
|
7547 |
|
7548 |
|
7549 |
|
7550 | for (i = 0; i < event.captions.length; i++) {
|
7551 | caption = event.captions[i];
|
7552 |
|
7553 | this.trigger('caption', caption);
|
7554 | }
|
7555 |
|
7556 |
|
7557 |
|
7558 |
|
7559 | for (i = 0; i < event.metadata.length; i++) {
|
7560 | id3 = event.metadata[i];
|
7561 |
|
7562 | this.trigger('id3Frame', id3);
|
7563 | }
|
7564 | }
|
7565 |
|
7566 |
|
7567 | if (this.emittedTracks >= this.numberOfTracks) {
|
7568 | this.trigger('done');
|
7569 | this.emittedTracks = 0;
|
7570 | }
|
7571 | };
|
7572 |
|
7573 | _CoalesceStream.prototype.setRemux = function (val) {
|
7574 | this.remuxTracks = val;
|
7575 | };
|
7576 | |
7577 |
|
7578 |
|
7579 |
|
7580 |
|
7581 |
|
7582 | _Transmuxer = function Transmuxer(options) {
|
7583 | var self = this,
|
7584 | hasFlushed = true,
|
7585 | videoTrack,
|
7586 | audioTrack;
|
7587 |
|
7588 | _Transmuxer.prototype.init.call(this);
|
7589 |
|
7590 | options = options || {};
|
7591 | this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
|
7592 | this.transmuxPipeline_ = {};
|
7593 |
|
7594 | this.setupAacPipeline = function () {
|
7595 | var pipeline = {};
|
7596 | this.transmuxPipeline_ = pipeline;
|
7597 |
|
7598 | pipeline.type = 'aac';
|
7599 | pipeline.metadataStream = new m2ts_1.MetadataStream();
|
7600 |
|
7601 |
|
7602 | pipeline.aacStream = new aac();
|
7603 | pipeline.audioTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('audio');
|
7604 | pipeline.timedMetadataTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('timed-metadata');
|
7605 | pipeline.adtsStream = new adts();
|
7606 | pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
|
7607 | pipeline.headOfPipeline = pipeline.aacStream;
|
7608 |
|
7609 | pipeline.aacStream.pipe(pipeline.audioTimestampRolloverStream).pipe(pipeline.adtsStream);
|
7610 | pipeline.aacStream.pipe(pipeline.timedMetadataTimestampRolloverStream).pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
|
7611 |
|
7612 | pipeline.metadataStream.on('timestamp', function (frame) {
|
7613 | pipeline.aacStream.setTimestamp(frame.timeStamp);
|
7614 | });
|
7615 |
|
7616 | pipeline.aacStream.on('data', function (data) {
|
7617 | if (data.type === 'timed-metadata' && !pipeline.audioSegmentStream) {
|
7618 | audioTrack = audioTrack || {
|
7619 | timelineStartInfo: {
|
7620 | baseMediaDecodeTime: self.baseMediaDecodeTime
|
7621 | },
|
7622 | codec: 'adts',
|
7623 | type: 'audio'
|
7624 | };
|
7625 |
|
7626 | pipeline.coalesceStream.numberOfTracks++;
|
7627 | pipeline.audioSegmentStream = new _AudioSegmentStream(audioTrack, options);
|
7628 |
|
7629 | pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
|
7630 |
|
7631 |
|
7632 | pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
|
7633 | }
|
7634 |
|
7635 |
|
7636 | self.trigger('trackinfo', {
|
7637 | hasAudio: !!audioTrack,
|
7638 | hasVideo: !!videoTrack
|
7639 | });
|
7640 | });
|
7641 |
|
7642 |
|
7643 | pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
|
7644 |
|
7645 | pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
|
7646 | };
|
7647 |
|
7648 | this.setupTsPipeline = function () {
|
7649 | var pipeline = {};
|
7650 | this.transmuxPipeline_ = pipeline;
|
7651 |
|
7652 | pipeline.type = 'ts';
|
7653 | pipeline.metadataStream = new m2ts_1.MetadataStream();
|
7654 |
|
7655 |
|
7656 | pipeline.packetStream = new m2ts_1.TransportPacketStream();
|
7657 | pipeline.parseStream = new m2ts_1.TransportParseStream();
|
7658 | pipeline.elementaryStream = new m2ts_1.ElementaryStream();
|
7659 | pipeline.timestampRolloverStream = new m2ts_1.TimestampRolloverStream();
|
7660 | pipeline.adtsStream = new adts();
|
7661 | pipeline.h264Stream = new H264Stream();
|
7662 | pipeline.captionStream = new m2ts_1.CaptionStream();
|
7663 | pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
|
7664 | pipeline.headOfPipeline = pipeline.packetStream;
|
7665 |
|
7666 |
|
7667 | pipeline.packetStream.pipe(pipeline.parseStream).pipe(pipeline.elementaryStream).pipe(pipeline.timestampRolloverStream);
|
7668 |
|
7669 |
|
7670 |
|
7671 | pipeline.timestampRolloverStream.pipe(pipeline.h264Stream);
|
7672 |
|
7673 | pipeline.timestampRolloverStream.pipe(pipeline.adtsStream);
|
7674 |
|
7675 | pipeline.timestampRolloverStream.pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
|
7676 |
|
7677 |
|
7678 | pipeline.h264Stream.pipe(pipeline.captionStream).pipe(pipeline.coalesceStream);
|
7679 |
|
7680 | pipeline.elementaryStream.on('data', function (data) {
|
7681 | var i;
|
7682 |
|
7683 | if (data.type === 'metadata') {
|
7684 | i = data.tracks.length;
|
7685 |
|
7686 |
|
7687 | while (i--) {
|
7688 | if (!videoTrack && data.tracks[i].type === 'video') {
|
7689 | videoTrack = data.tracks[i];
|
7690 | videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
|
7691 | } else if (!audioTrack && data.tracks[i].type === 'audio') {
|
7692 | audioTrack = data.tracks[i];
|
7693 | audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
|
7694 | }
|
7695 | }
|
7696 |
|
7697 |
|
7698 | if (videoTrack && !pipeline.videoSegmentStream) {
|
7699 | pipeline.coalesceStream.numberOfTracks++;
|
7700 | pipeline.videoSegmentStream = new _VideoSegmentStream(videoTrack, options);
|
7701 |
|
7702 | pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
|
7703 |
|
7704 |
|
7705 |
|
7706 | if (audioTrack) {
|
7707 | audioTrack.timelineStartInfo = timelineStartInfo;
|
7708 |
|
7709 |
|
7710 |
|
7711 |
|
7712 | pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts);
|
7713 | }
|
7714 | });
|
7715 |
|
7716 | pipeline.videoSegmentStream.on('processedGopsInfo', self.trigger.bind(self, 'gopInfo'));
|
7717 | pipeline.videoSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'videoSegmentTimingInfo'));
|
7718 |
|
7719 | pipeline.videoSegmentStream.on('baseMediaDecodeTime', function (baseMediaDecodeTime) {
|
7720 | if (audioTrack) {
|
7721 | pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
|
7722 | }
|
7723 | });
|
7724 |
|
7725 | pipeline.videoSegmentStream.on('timingInfo', self.trigger.bind(self, 'videoTimingInfo'));
|
7726 |
|
7727 |
|
7728 | pipeline.h264Stream.pipe(pipeline.videoSegmentStream).pipe(pipeline.coalesceStream);
|
7729 | }
|
7730 |
|
7731 | if (audioTrack && !pipeline.audioSegmentStream) {
|
7732 |
|
7733 | pipeline.coalesceStream.numberOfTracks++;
|
7734 | pipeline.audioSegmentStream = new _AudioSegmentStream(audioTrack, options);
|
7735 |
|
7736 | pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
|
7737 |
|
7738 |
|
7739 | pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
|
7740 | }
|
7741 |
|
7742 |
|
7743 | self.trigger('trackinfo', {
|
7744 | hasAudio: !!audioTrack,
|
7745 | hasVideo: !!videoTrack
|
7746 | });
|
7747 | }
|
7748 | });
|
7749 |
|
7750 |
|
7751 | pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
|
7752 | pipeline.coalesceStream.on('id3Frame', function (id3Frame) {
|
7753 | id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
|
7754 |
|
7755 | self.trigger('id3Frame', id3Frame);
|
7756 | });
|
7757 | pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
|
7758 |
|
7759 | pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
|
7760 | };
|
7761 |
|
7762 |
|
7763 | this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
|
7764 | var pipeline = this.transmuxPipeline_;
|
7765 |
|
7766 | if (!options.keepOriginalTimestamps) {
|
7767 | this.baseMediaDecodeTime = baseMediaDecodeTime;
|
7768 | }
|
7769 |
|
7770 | if (audioTrack) {
|
7771 | audioTrack.timelineStartInfo.dts = undefined;
|
7772 | audioTrack.timelineStartInfo.pts = undefined;
|
7773 | trackDecodeInfo.clearDtsInfo(audioTrack);
|
7774 | if (!options.keepOriginalTimestamps) {
|
7775 | audioTrack.timelineStartInfo.baseMediaDecodeTime = baseMediaDecodeTime;
|
7776 | }
|
7777 | if (pipeline.audioTimestampRolloverStream) {
|
7778 | pipeline.audioTimestampRolloverStream.discontinuity();
|
7779 | }
|
7780 | }
|
7781 | if (videoTrack) {
|
7782 | if (pipeline.videoSegmentStream) {
|
7783 | pipeline.videoSegmentStream.gopCache_ = [];
|
7784 | }
|
7785 | videoTrack.timelineStartInfo.dts = undefined;
|
7786 | videoTrack.timelineStartInfo.pts = undefined;
|
7787 | trackDecodeInfo.clearDtsInfo(videoTrack);
|
7788 | pipeline.captionStream.reset();
|
7789 | if (!options.keepOriginalTimestamps) {
|
7790 | videoTrack.timelineStartInfo.baseMediaDecodeTime = baseMediaDecodeTime;
|
7791 | }
|
7792 | }
|
7793 |
|
7794 | if (pipeline.timestampRolloverStream) {
|
7795 | pipeline.timestampRolloverStream.discontinuity();
|
7796 | }
|
7797 | };
|
7798 |
|
7799 | this.setAudioAppendStart = function (timestamp) {
|
7800 | if (audioTrack) {
|
7801 | this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
|
7802 | }
|
7803 | };
|
7804 |
|
7805 | this.setRemux = function (val) {
|
7806 | var pipeline = this.transmuxPipeline_;
|
7807 |
|
7808 | options.remux = val;
|
7809 |
|
7810 | if (pipeline && pipeline.coalesceStream) {
|
7811 | pipeline.coalesceStream.setRemux(val);
|
7812 | }
|
7813 | };
|
7814 |
|
7815 | this.alignGopsWith = function (gopsToAlignWith) {
|
7816 | if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
|
7817 | this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
|
7818 | }
|
7819 | };
|
7820 |
|
7821 |
|
7822 | this.push = function (data) {
|
7823 | if (hasFlushed) {
|
7824 | var isAac = isLikelyAacData$1(data);
|
7825 |
|
7826 | if (isAac && this.transmuxPipeline_.type !== 'aac') {
|
7827 | this.setupAacPipeline();
|
7828 | } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
|
7829 | this.setupTsPipeline();
|
7830 | }
|
7831 | hasFlushed = false;
|
7832 | }
|
7833 | this.transmuxPipeline_.headOfPipeline.push(data);
|
7834 | };
|
7835 |
|
7836 |
|
7837 | this.flush = function () {
|
7838 | hasFlushed = true;
|
7839 |
|
7840 | this.transmuxPipeline_.headOfPipeline.flush();
|
7841 | };
|
7842 |
|
7843 | this.endTimeline = function () {
|
7844 | this.transmuxPipeline_.headOfPipeline.endTimeline();
|
7845 | };
|
7846 |
|
7847 | this.reset = function () {
|
7848 | if (this.transmuxPipeline_.headOfPipeline) {
|
7849 | this.transmuxPipeline_.headOfPipeline.reset();
|
7850 | }
|
7851 | };
|
7852 |
|
7853 |
|
7854 | this.resetCaptions = function () {
|
7855 | if (this.transmuxPipeline_.captionStream) {
|
7856 | this.transmuxPipeline_.captionStream.reset();
|
7857 | }
|
7858 | };
|
7859 | };
|
7860 | _Transmuxer.prototype = new stream();
|
7861 |
|
7862 | var transmuxer = {
|
7863 | Transmuxer: _Transmuxer,
|
7864 | VideoSegmentStream: _VideoSegmentStream,
|
7865 | AudioSegmentStream: _AudioSegmentStream,
|
7866 | AUDIO_PROPERTIES: AUDIO_PROPERTIES,
|
7867 | VIDEO_PROPERTIES: VIDEO_PROPERTIES,
|
7868 |
|
7869 | generateVideoSegmentTimingInfo: generateVideoSegmentTimingInfo
|
7870 | };
|
7871 |
|
7872 | var classCallCheck = function classCallCheck(instance, Constructor) {
|
7873 | if (!(instance instanceof Constructor)) {
|
7874 | throw new TypeError("Cannot call a class as a function");
|
7875 | }
|
7876 | };
|
7877 |
|
7878 | var createClass = function () {
|
7879 | function defineProperties(target, props) {
|
7880 | for (var i = 0; i < props.length; i++) {
|
7881 | var descriptor = props[i];
|
7882 | descriptor.enumerable = descriptor.enumerable || false;
|
7883 | descriptor.configurable = true;
|
7884 | if ("value" in descriptor) descriptor.writable = true;
|
7885 | Object.defineProperty(target, descriptor.key, descriptor);
|
7886 | }
|
7887 | }
|
7888 |
|
7889 | return function (Constructor, protoProps, staticProps) {
|
7890 | if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
7891 | if (staticProps) defineProperties(Constructor, staticProps);
|
7892 | return Constructor;
|
7893 | };
|
7894 | }();
|
7895 |
|
7896 | |
7897 |
|
7898 |
|
7899 |
|
7900 | |
7901 |
|
7902 |
|
7903 |
|
7904 |
|
7905 |
|
7906 |
|
7907 | var wireTransmuxerEvents = function wireTransmuxerEvents(self, transmuxer$$1) {
|
7908 | transmuxer$$1.on('data', function (segment) {
|
7909 |
|
7910 |
|
7911 |
|
7912 |
|
7913 | var initArray = segment.initSegment;
|
7914 |
|
7915 | segment.initSegment = {
|
7916 | data: initArray.buffer,
|
7917 | byteOffset: initArray.byteOffset,
|
7918 | byteLength: initArray.byteLength
|
7919 | };
|
7920 |
|
7921 | var typedArray = segment.data;
|
7922 |
|
7923 | segment.data = typedArray.buffer;
|
7924 | self.postMessage({
|
7925 | action: 'data',
|
7926 | segment: segment,
|
7927 | byteOffset: typedArray.byteOffset,
|
7928 | byteLength: typedArray.byteLength
|
7929 | }, [segment.data]);
|
7930 | });
|
7931 |
|
7932 | if (transmuxer$$1.captionStream) {
|
7933 | transmuxer$$1.captionStream.on('data', function (caption) {
|
7934 | self.postMessage({
|
7935 | action: 'caption',
|
7936 | data: caption
|
7937 | });
|
7938 | });
|
7939 | }
|
7940 |
|
7941 | transmuxer$$1.on('done', function (data) {
|
7942 | self.postMessage({ action: 'done' });
|
7943 | });
|
7944 |
|
7945 | transmuxer$$1.on('gopInfo', function (gopInfo) {
|
7946 | self.postMessage({
|
7947 | action: 'gopInfo',
|
7948 | gopInfo: gopInfo
|
7949 | });
|
7950 | });
|
7951 |
|
7952 | transmuxer$$1.on('videoSegmentTimingInfo', function (videoSegmentTimingInfo) {
|
7953 | self.postMessage({
|
7954 | action: 'videoSegmentTimingInfo',
|
7955 | videoSegmentTimingInfo: videoSegmentTimingInfo
|
7956 | });
|
7957 | });
|
7958 | };
|
7959 |
|
7960 | |
7961 |
|
7962 |
|
7963 |
|
7964 |
|
7965 |
|
7966 |
|
7967 |
|
7968 | var MessageHandlers = function () {
|
7969 | function MessageHandlers(self, options) {
|
7970 | classCallCheck(this, MessageHandlers);
|
7971 |
|
7972 | this.options = options || {};
|
7973 | this.self = self;
|
7974 | this.init();
|
7975 | }
|
7976 |
|
7977 | |
7978 |
|
7979 |
|
7980 |
|
7981 | createClass(MessageHandlers, [{
|
7982 | key: 'init',
|
7983 | value: function init() {
|
7984 | if (this.transmuxer) {
|
7985 | this.transmuxer.dispose();
|
7986 | }
|
7987 | this.transmuxer = new transmuxer.Transmuxer(this.options);
|
7988 | wireTransmuxerEvents(this.self, this.transmuxer);
|
7989 | }
|
7990 |
|
7991 | |
7992 |
|
7993 |
|
7994 |
|
7995 |
|
7996 |
|
7997 |
|
7998 | }, {
|
7999 | key: 'push',
|
8000 | value: function push(data) {
|
8001 |
|
8002 | var segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
|
8003 |
|
8004 | this.transmuxer.push(segment);
|
8005 | }
|
8006 |
|
8007 | |
8008 |
|
8009 |
|
8010 |
|
8011 |
|
8012 | }, {
|
8013 | key: 'reset',
|
8014 | value: function reset() {
|
8015 | this.init();
|
8016 | }
|
8017 |
|
8018 | |
8019 |
|
8020 |
|
8021 |
|
8022 |
|
8023 |
|
8024 |
|
8025 |
|
8026 | }, {
|
8027 | key: 'setTimestampOffset',
|
8028 | value: function setTimestampOffset(data) {
|
8029 | var timestampOffset = data.timestampOffset || 0;
|
8030 |
|
8031 | this.transmuxer.setBaseMediaDecodeTime(Math.round(timestampOffset * 90000));
|
8032 | }
|
8033 | }, {
|
8034 | key: 'setAudioAppendStart',
|
8035 | value: function setAudioAppendStart(data) {
|
8036 | this.transmuxer.setAudioAppendStart(Math.ceil(data.appendStart * 90000));
|
8037 | }
|
8038 |
|
8039 | |
8040 |
|
8041 |
|
8042 |
|
8043 |
|
8044 |
|
8045 |
|
8046 | }, {
|
8047 | key: 'flush',
|
8048 | value: function flush(data) {
|
8049 | this.transmuxer.flush();
|
8050 | }
|
8051 | }, {
|
8052 | key: 'resetCaptions',
|
8053 | value: function resetCaptions() {
|
8054 | this.transmuxer.resetCaptions();
|
8055 | }
|
8056 | }, {
|
8057 | key: 'alignGopsWith',
|
8058 | value: function alignGopsWith(data) {
|
8059 | this.transmuxer.alignGopsWith(data.gopsToAlignWith.slice());
|
8060 | }
|
8061 | }]);
|
8062 | return MessageHandlers;
|
8063 | }();
|
8064 |
|
8065 | |
8066 |
|
8067 |
|
8068 |
|
8069 |
|
8070 |
|
8071 |
|
8072 |
|
8073 | var TransmuxerWorker = function TransmuxerWorker(self) {
|
8074 | self.onmessage = function (event) {
|
8075 | if (event.data.action === 'init' && event.data.options) {
|
8076 | this.messageHandlers = new MessageHandlers(self, event.data.options);
|
8077 | return;
|
8078 | }
|
8079 |
|
8080 | if (!this.messageHandlers) {
|
8081 | this.messageHandlers = new MessageHandlers(self);
|
8082 | }
|
8083 |
|
8084 | if (event.data && event.data.action && event.data.action !== 'init') {
|
8085 | if (this.messageHandlers[event.data.action]) {
|
8086 | this.messageHandlers[event.data.action](event.data);
|
8087 | }
|
8088 | }
|
8089 | };
|
8090 | };
|
8091 |
|
8092 | var transmuxerWorker = new TransmuxerWorker(self);
|
8093 |
|
8094 | return transmuxerWorker;
|
8095 | }();
|
8096 | });
|
8097 |
|
8098 |
|
8099 |
|
8100 |
|
8101 |
|
8102 |
|
8103 |
|
8104 | var defaultCodecs = {
|
8105 | videoCodec: 'avc1',
|
8106 | videoObjectTypeIndicator: '.4d400d',
|
8107 |
|
8108 | audioProfile: '2'
|
8109 | };
|
8110 |
|
8111 |
|
8112 |
|
8113 |
|
8114 |
|
8115 |
|
8116 |
|
8117 |
|
8118 |
|
8119 | var translateLegacyCodecs = function translateLegacyCodecs(codecs) {
|
8120 | return codecs.map(function (codec) {
|
8121 | return codec.replace(/avc1\.(\d+)\.(\d+)/i, function (orig, profile, avcLevel) {
|
8122 | var profileHex = ('00' + Number(profile).toString(16)).slice(-2);
|
8123 | var avcLevelHex = ('00' + Number(avcLevel).toString(16)).slice(-2);
|
8124 |
|
8125 | return 'avc1.' + profileHex + '00' + avcLevelHex;
|
8126 | });
|
8127 | });
|
8128 | };
|
8129 |
|
8130 |
|
8131 |
|
8132 |
|
8133 |
|
8134 |
|
8135 | var parseCodecs = function parseCodecs() {
|
8136 | var codecs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
8137 |
|
8138 | var result = {
|
8139 | codecCount: 0
|
8140 | };
|
8141 | var parsed = void 0;
|
8142 |
|
8143 | result.codecCount = codecs.split(',').length;
|
8144 | result.codecCount = result.codecCount || 2;
|
8145 |
|
8146 |
|
8147 | parsed = /(^|\s|,)+(avc[13])([^ ,]*)/i.exec(codecs);
|
8148 | if (parsed) {
|
8149 | result.videoCodec = parsed[2];
|
8150 | result.videoObjectTypeIndicator = parsed[3];
|
8151 | }
|
8152 |
|
8153 |
|
8154 | result.audioProfile = /(^|\s|,)+mp4a.[0-9A-Fa-f]+\.([0-9A-Fa-f]+)/i.exec(codecs);
|
8155 | result.audioProfile = result.audioProfile && result.audioProfile[2];
|
8156 |
|
8157 | return result;
|
8158 | };
|
8159 |
|
8160 |
|
8161 |
|
8162 |
|
8163 |
|
8164 |
|
8165 |
|
8166 |
|
8167 |
|
8168 |
|
8169 | var mapLegacyAvcCodecs = function mapLegacyAvcCodecs(codecString) {
|
8170 | return codecString.replace(/avc1\.(\d+)\.(\d+)/i, function (match) {
|
8171 | return translateLegacyCodecs([match])[0];
|
8172 | });
|
8173 | };
|
8174 |
|
8175 |
|
8176 |
|
8177 |
|
8178 |
|
8179 |
|
8180 |
|
8181 |
|
8182 | var makeMimeTypeString = function makeMimeTypeString(type, container, codecs) {
|
8183 |
|
8184 |
|
8185 |
|
8186 | return type + '/' + container + '; codecs="' + codecs.filter(function (c) {
|
8187 | return !!c;
|
8188 | }).join(', ') + '"';
|
8189 | };
|
8190 |
|
8191 |
|
8192 |
|
8193 |
|
8194 |
|
8195 |
|
8196 | var getContainerType = function getContainerType(media) {
|
8197 |
|
8198 |
|
8199 |
|
8200 |
|
8201 | if (media.segments && media.segments.length && media.segments[0].map) {
|
8202 | return 'mp4';
|
8203 | }
|
8204 | return 'mp2t';
|
8205 | };
|
8206 |
|
8207 |
|
8208 |
|
8209 |
|
8210 |
|
8211 |
|
8212 |
|
8213 | var getCodecs = function getCodecs(media) {
|
8214 |
|
8215 |
|
8216 | var mediaAttributes = media.attributes || {};
|
8217 |
|
8218 | if (mediaAttributes.CODECS) {
|
8219 | return parseCodecs(mediaAttributes.CODECS);
|
8220 | }
|
8221 | return defaultCodecs;
|
8222 | };
|
8223 |
|
8224 | var audioProfileFromDefault = function audioProfileFromDefault(master, audioGroupId) {
|
8225 | if (!master.mediaGroups.AUDIO || !audioGroupId) {
|
8226 | return null;
|
8227 | }
|
8228 |
|
8229 | var audioGroup = master.mediaGroups.AUDIO[audioGroupId];
|
8230 |
|
8231 | if (!audioGroup) {
|
8232 | return null;
|
8233 | }
|
8234 |
|
8235 | for (var name in audioGroup) {
|
8236 | var audioType = audioGroup[name];
|
8237 |
|
8238 | if (audioType.default && audioType.playlists) {
|
8239 |
|
8240 | return parseCodecs(audioType.playlists[0].attributes.CODECS).audioProfile;
|
8241 | }
|
8242 | }
|
8243 |
|
8244 | return null;
|
8245 | };
|
8246 |
|
8247 |
|
8248 |
|
8249 |
|
8250 |
|
8251 |
|
8252 |
|
8253 |
|
8254 |
|
8255 |
|
8256 |
|
8257 |
|
8258 |
|
8259 |
|
8260 |
|
8261 | var mimeTypesForPlaylist = function mimeTypesForPlaylist(master, media) {
|
8262 | var containerType = getContainerType(media);
|
8263 | var codecInfo = getCodecs(media);
|
8264 | var mediaAttributes = media.attributes || {};
|
8265 |
|
8266 | var isMuxed = true;
|
8267 | var isMaat = false;
|
8268 |
|
8269 | if (!media) {
|
8270 |
|
8271 | return [];
|
8272 | }
|
8273 |
|
8274 | if (master.mediaGroups.AUDIO && mediaAttributes.AUDIO) {
|
8275 | var audioGroup = master.mediaGroups.AUDIO[mediaAttributes.AUDIO];
|
8276 |
|
8277 |
|
8278 | if (audioGroup) {
|
8279 | isMaat = true;
|
8280 |
|
8281 | isMuxed = false;
|
8282 |
|
8283 | for (var groupId in audioGroup) {
|
8284 |
|
8285 |
|
8286 |
|
8287 | if (!audioGroup[groupId].uri && !audioGroup[groupId].playlists) {
|
8288 | isMuxed = true;
|
8289 | break;
|
8290 | }
|
8291 | }
|
8292 | }
|
8293 | }
|
8294 |
|
8295 |
|
8296 |
|
8297 | if (isMaat && !codecInfo.audioProfile) {
|
8298 | if (!isMuxed) {
|
8299 |
|
8300 |
|
8301 |
|
8302 | codecInfo.audioProfile = audioProfileFromDefault(master, mediaAttributes.AUDIO);
|
8303 | }
|
8304 |
|
8305 | if (!codecInfo.audioProfile) {
|
8306 | videojs.log.warn('Multiple audio tracks present but no audio codec string is specified. ' + 'Attempting to use the default audio codec (mp4a.40.2)');
|
8307 | codecInfo.audioProfile = defaultCodecs.audioProfile;
|
8308 | }
|
8309 | }
|
8310 |
|
8311 |
|
8312 | var codecStrings = {};
|
8313 |
|
8314 | if (codecInfo.videoCodec) {
|
8315 | codecStrings.video = '' + codecInfo.videoCodec + codecInfo.videoObjectTypeIndicator;
|
8316 | }
|
8317 |
|
8318 | if (codecInfo.audioProfile) {
|
8319 | codecStrings.audio = 'mp4a.40.' + codecInfo.audioProfile;
|
8320 | }
|
8321 |
|
8322 |
|
8323 |
|
8324 | var justAudio = makeMimeTypeString('audio', containerType, [codecStrings.audio]);
|
8325 | var justVideo = makeMimeTypeString('video', containerType, [codecStrings.video]);
|
8326 | var bothVideoAudio = makeMimeTypeString('video', containerType, [codecStrings.video, codecStrings.audio]);
|
8327 |
|
8328 | if (isMaat) {
|
8329 | if (!isMuxed && codecStrings.video) {
|
8330 | return [justVideo, justAudio];
|
8331 | }
|
8332 |
|
8333 | if (!isMuxed && !codecStrings.video) {
|
8334 |
|
8335 |
|
8336 | return [justAudio, justAudio];
|
8337 | }
|
8338 |
|
8339 |
|
8340 |
|
8341 |
|
8342 | return [bothVideoAudio, justAudio];
|
8343 | }
|
8344 |
|
8345 |
|
8346 |
|
8347 | if (!codecStrings.video) {
|
8348 | return [justAudio];
|
8349 | }
|
8350 |
|
8351 |
|
8352 |
|
8353 | return [bothVideoAudio];
|
8354 | };
|
8355 |
|
8356 |
|
8357 |
|
8358 |
|
8359 |
|
8360 |
|
8361 |
|
8362 |
|
8363 |
|
8364 | var parseContentType = function parseContentType(type) {
|
8365 | var object = { type: '', parameters: {} };
|
8366 | var parameters = type.trim().split(';');
|
8367 |
|
8368 |
|
8369 | object.type = parameters.shift().trim();
|
8370 | parameters.forEach(function (parameter) {
|
8371 | var pair = parameter.trim().split('=');
|
8372 |
|
8373 | if (pair.length > 1) {
|
8374 | var name = pair[0].replace(/"/g, '').trim();
|
8375 | var value = pair[1].replace(/"/g, '').trim();
|
8376 |
|
8377 | object.parameters[name] = value;
|
8378 | }
|
8379 | });
|
8380 |
|
8381 | return object;
|
8382 | };
|
8383 |
|
8384 |
|
8385 |
|
8386 |
|
8387 |
|
8388 |
|
8389 |
|
8390 |
|
8391 | var isAudioCodec = function isAudioCodec(codec) {
|
8392 | return (/mp4a\.\d+.\d+/i.test(codec)
|
8393 | );
|
8394 | };
|
8395 |
|
8396 |
|
8397 |
|
8398 |
|
8399 |
|
8400 |
|
8401 |
|
8402 |
|
8403 | var isVideoCodec = function isVideoCodec(codec) {
|
8404 | return (/avc1\.[\da-f]+/i.test(codec)
|
8405 | );
|
8406 | };
|
8407 |
|
8408 |
|
8409 |
|
8410 |
|
8411 |
|
8412 |
|
8413 |
|
8414 |
|
8415 |
|
8416 |
|
8417 |
|
8418 |
|
8419 |
|
8420 |
|
8421 | var gopsSafeToAlignWith = function gopsSafeToAlignWith(buffer, currentTime, mapping) {
|
8422 | if (typeof currentTime === 'undefined' || currentTime === null || !buffer.length) {
|
8423 | return [];
|
8424 | }
|
8425 |
|
8426 |
|
8427 | var currentTimePts = Math.ceil((currentTime - mapping + 3) * 90000);
|
8428 |
|
8429 | var i = void 0;
|
8430 |
|
8431 | for (i = 0; i < buffer.length; i++) {
|
8432 | if (buffer[i].pts > currentTimePts) {
|
8433 | break;
|
8434 | }
|
8435 | }
|
8436 |
|
8437 | return buffer.slice(i);
|
8438 | };
|
8439 |
|
8440 |
|
8441 |
|
8442 |
|
8443 |
|
8444 |
|
8445 |
|
8446 |
|
8447 |
|
8448 |
|
8449 |
|
8450 |
|
8451 |
|
8452 |
|
8453 |
|
8454 | var updateGopBuffer = function updateGopBuffer(buffer, gops, replace) {
|
8455 | if (!gops.length) {
|
8456 | return buffer;
|
8457 | }
|
8458 |
|
8459 | if (replace) {
|
8460 |
|
8461 |
|
8462 |
|
8463 |
|
8464 | return gops.slice();
|
8465 | }
|
8466 |
|
8467 | var start = gops[0].pts;
|
8468 |
|
8469 | var i = 0;
|
8470 |
|
8471 | for (i; i < buffer.length; i++) {
|
8472 | if (buffer[i].pts >= start) {
|
8473 | break;
|
8474 | }
|
8475 | }
|
8476 |
|
8477 | return buffer.slice(0, i).concat(gops);
|
8478 | };
|
8479 |
|
8480 |
|
8481 |
|
8482 |
|
8483 |
|
8484 |
|
8485 |
|
8486 |
|
8487 |
|
8488 |
|
8489 |
|
8490 |
|
8491 |
|
8492 | var removeGopBuffer = function removeGopBuffer(buffer, start, end, mapping) {
|
8493 | var startPts = Math.ceil((start - mapping) * 90000);
|
8494 | var endPts = Math.ceil((end - mapping) * 90000);
|
8495 | var updatedBuffer = buffer.slice();
|
8496 |
|
8497 | var i = buffer.length;
|
8498 |
|
8499 | while (i--) {
|
8500 | if (buffer[i].pts <= endPts) {
|
8501 | break;
|
8502 | }
|
8503 | }
|
8504 |
|
8505 | if (i === -1) {
|
8506 |
|
8507 | return updatedBuffer;
|
8508 | }
|
8509 |
|
8510 | var j = i + 1;
|
8511 |
|
8512 | while (j--) {
|
8513 | if (buffer[j].pts <= startPts) {
|
8514 | break;
|
8515 | }
|
8516 | }
|
8517 |
|
8518 |
|
8519 | j = Math.max(j, 0);
|
8520 |
|
8521 | updatedBuffer.splice(j, i - j + 1);
|
8522 |
|
8523 | return updatedBuffer;
|
8524 | };
|
8525 |
|
8526 | var buffered = function buffered(videoBuffer, audioBuffer, audioDisabled) {
|
8527 | var start = null;
|
8528 | var end = null;
|
8529 | var arity = 0;
|
8530 | var extents = [];
|
8531 | var ranges = [];
|
8532 |
|
8533 |
|
8534 | if (!videoBuffer && !audioBuffer) {
|
8535 | return videojs.createTimeRange();
|
8536 | }
|
8537 |
|
8538 |
|
8539 | if (!videoBuffer) {
|
8540 | return audioBuffer.buffered;
|
8541 | }
|
8542 | if (!audioBuffer) {
|
8543 | return videoBuffer.buffered;
|
8544 | }
|
8545 |
|
8546 |
|
8547 | if (audioDisabled) {
|
8548 | return videoBuffer.buffered;
|
8549 | }
|
8550 |
|
8551 |
|
8552 | if (videoBuffer.buffered.length === 0 && audioBuffer.buffered.length === 0) {
|
8553 | return videojs.createTimeRange();
|
8554 | }
|
8555 |
|
8556 |
|
8557 |
|
8558 | var videoBuffered = videoBuffer.buffered;
|
8559 | var audioBuffered = audioBuffer.buffered;
|
8560 | var count = videoBuffered.length;
|
8561 |
|
8562 |
|
8563 | while (count--) {
|
8564 | extents.push({ time: videoBuffered.start(count), type: 'start' });
|
8565 | extents.push({ time: videoBuffered.end(count), type: 'end' });
|
8566 | }
|
8567 | count = audioBuffered.length;
|
8568 | while (count--) {
|
8569 | extents.push({ time: audioBuffered.start(count), type: 'start' });
|
8570 | extents.push({ time: audioBuffered.end(count), type: 'end' });
|
8571 | }
|
8572 |
|
8573 | extents.sort(function (a, b) {
|
8574 | return a.time - b.time;
|
8575 | });
|
8576 |
|
8577 |
|
8578 |
|
8579 | for (count = 0; count < extents.length; count++) {
|
8580 | if (extents[count].type === 'start') {
|
8581 | arity++;
|
8582 |
|
8583 |
|
8584 |
|
8585 | if (arity === 2) {
|
8586 | start = extents[count].time;
|
8587 | }
|
8588 | } else if (extents[count].type === 'end') {
|
8589 | arity--;
|
8590 |
|
8591 |
|
8592 |
|
8593 | if (arity === 1) {
|
8594 | end = extents[count].time;
|
8595 | }
|
8596 | }
|
8597 |
|
8598 |
|
8599 | if (start !== null && end !== null) {
|
8600 | ranges.push([start, end]);
|
8601 | start = null;
|
8602 | end = null;
|
8603 | }
|
8604 | }
|
8605 |
|
8606 | return videojs.createTimeRanges(ranges);
|
8607 | };
|
8608 |
|
8609 |
|
8610 |
|
8611 |
|
8612 |
|
8613 | var ONE_SECOND_IN_TS = 90000;
|
8614 |
|
8615 |
|
8616 |
|
8617 |
|
8618 |
|
8619 | var makeWrappedSourceBuffer = function makeWrappedSourceBuffer(mediaSource, mimeType) {
|
8620 | var sourceBuffer = mediaSource.addSourceBuffer(mimeType);
|
8621 | var wrapper = Object.create(null);
|
8622 |
|
8623 | wrapper.updating = false;
|
8624 | wrapper.realBuffer_ = sourceBuffer;
|
8625 |
|
8626 | var _loop = function _loop(key) {
|
8627 | if (typeof sourceBuffer[key] === 'function') {
|
8628 | wrapper[key] = function () {
|
8629 | return sourceBuffer[key].apply(sourceBuffer, arguments);
|
8630 | };
|
8631 | } else if (typeof wrapper[key] === 'undefined') {
|
8632 | Object.defineProperty(wrapper, key, {
|
8633 | get: function get$$1() {
|
8634 | return sourceBuffer[key];
|
8635 | },
|
8636 | set: function set$$1(v) {
|
8637 | return sourceBuffer[key] = v;
|
8638 | }
|
8639 | });
|
8640 | }
|
8641 | };
|
8642 |
|
8643 | for (var key in sourceBuffer) {
|
8644 | _loop(key);
|
8645 | }
|
8646 |
|
8647 | return wrapper;
|
8648 | };
|
8649 |
|
8650 |
|
8651 |
|
8652 |
|
8653 |
|
8654 |
|
8655 |
|
8656 |
|
8657 |
|
8658 |
|
8659 |
|
8660 |
|
8661 |
|
8662 |
|
8663 |
|
8664 | var VirtualSourceBuffer = function (_videojs$EventTarget) {
|
8665 | inherits(VirtualSourceBuffer, _videojs$EventTarget);
|
8666 |
|
8667 | function VirtualSourceBuffer(mediaSource, codecs) {
|
8668 | classCallCheck(this, VirtualSourceBuffer);
|
8669 |
|
8670 | var _this = possibleConstructorReturn(this, (VirtualSourceBuffer.__proto__ || Object.getPrototypeOf(VirtualSourceBuffer)).call(this, videojs.EventTarget));
|
8671 |
|
8672 | _this.timestampOffset_ = 0;
|
8673 | _this.pendingBuffers_ = [];
|
8674 | _this.bufferUpdating_ = false;
|
8675 |
|
8676 | _this.mediaSource_ = mediaSource;
|
8677 | _this.codecs_ = codecs;
|
8678 | _this.audioCodec_ = null;
|
8679 | _this.videoCodec_ = null;
|
8680 | _this.audioDisabled_ = false;
|
8681 | _this.appendAudioInitSegment_ = true;
|
8682 | _this.gopBuffer_ = [];
|
8683 | _this.timeMapping_ = 0;
|
8684 | _this.safeAppend_ = videojs.browser.IE_VERSION >= 11;
|
8685 |
|
8686 | var options = {
|
8687 | remux: false,
|
8688 | alignGopsAtEnd: _this.safeAppend_
|
8689 | };
|
8690 |
|
8691 | _this.codecs_.forEach(function (codec) {
|
8692 | if (isAudioCodec(codec)) {
|
8693 | _this.audioCodec_ = codec;
|
8694 | } else if (isVideoCodec(codec)) {
|
8695 | _this.videoCodec_ = codec;
|
8696 | }
|
8697 | });
|
8698 |
|
8699 |
|
8700 |
|
8701 | _this.transmuxer_ = new TransmuxWorker();
|
8702 | _this.transmuxer_.postMessage({ action: 'init', options: options });
|
8703 |
|
8704 | _this.transmuxer_.onmessage = function (event) {
|
8705 | if (event.data.action === 'data') {
|
8706 | return _this.data_(event);
|
8707 | }
|
8708 |
|
8709 | if (event.data.action === 'done') {
|
8710 | return _this.done_(event);
|
8711 | }
|
8712 |
|
8713 | if (event.data.action === 'gopInfo') {
|
8714 | return _this.appendGopInfo_(event);
|
8715 | }
|
8716 |
|
8717 | if (event.data.action === 'videoSegmentTimingInfo') {
|
8718 | return _this.videoSegmentTimingInfo_(event.data.videoSegmentTimingInfo);
|
8719 | }
|
8720 | };
|
8721 |
|
8722 |
|
8723 |
|
8724 | Object.defineProperty(_this, 'timestampOffset', {
|
8725 | get: function get$$1() {
|
8726 | return this.timestampOffset_;
|
8727 | },
|
8728 | set: function set$$1(val) {
|
8729 | if (typeof val === 'number' && val >= 0) {
|
8730 | this.timestampOffset_ = val;
|
8731 | this.appendAudioInitSegment_ = true;
|
8732 |
|
8733 |
|
8734 | this.gopBuffer_.length = 0;
|
8735 | this.timeMapping_ = 0;
|
8736 |
|
8737 |
|
8738 |
|
8739 | this.transmuxer_.postMessage({
|
8740 | action: 'setTimestampOffset',
|
8741 | timestampOffset: val
|
8742 | });
|
8743 | }
|
8744 | }
|
8745 | });
|
8746 |
|
8747 |
|
8748 | Object.defineProperty(_this, 'appendWindowStart', {
|
8749 | get: function get$$1() {
|
8750 | return (this.videoBuffer_ || this.audioBuffer_).appendWindowStart;
|
8751 | },
|
8752 | set: function set$$1(start) {
|
8753 | if (this.videoBuffer_) {
|
8754 | this.videoBuffer_.appendWindowStart = start;
|
8755 | }
|
8756 | if (this.audioBuffer_) {
|
8757 | this.audioBuffer_.appendWindowStart = start;
|
8758 | }
|
8759 | }
|
8760 | });
|
8761 |
|
8762 |
|
8763 | Object.defineProperty(_this, 'updating', {
|
8764 | get: function get$$1() {
|
8765 | return !!(this.bufferUpdating_ || !this.audioDisabled_ && this.audioBuffer_ && this.audioBuffer_.updating || this.videoBuffer_ && this.videoBuffer_.updating);
|
8766 | }
|
8767 | });
|
8768 |
|
8769 |
|
8770 |
|
8771 | Object.defineProperty(_this, 'buffered', {
|
8772 | get: function get$$1() {
|
8773 | return buffered(this.videoBuffer_, this.audioBuffer_, this.audioDisabled_);
|
8774 | }
|
8775 | });
|
8776 | return _this;
|
8777 | }
|
8778 |
|
8779 | |
8780 |
|
8781 |
|
8782 |
|
8783 |
|
8784 |
|
8785 |
|
8786 |
|
8787 |
|
8788 |
|
8789 | createClass(VirtualSourceBuffer, [{
|
8790 | key: 'data_',
|
8791 | value: function data_(event) {
|
8792 | var segment = event.data.segment;
|
8793 |
|
8794 |
|
8795 | segment.data = new Uint8Array(segment.data, event.data.byteOffset, event.data.byteLength);
|
8796 |
|
8797 | segment.initSegment = new Uint8Array(segment.initSegment.data, segment.initSegment.byteOffset, segment.initSegment.byteLength);
|
8798 |
|
8799 | createTextTracksIfNecessary(this, this.mediaSource_, segment);
|
8800 |
|
8801 |
|
8802 | this.pendingBuffers_.push(segment);
|
8803 | return;
|
8804 | }
|
8805 |
|
8806 | |
8807 |
|
8808 |
|
8809 |
|
8810 |
|
8811 |
|
8812 |
|
8813 |
|
8814 |
|
8815 |
|
8816 | }, {
|
8817 | key: 'done_',
|
8818 | value: function done_(event) {
|
8819 |
|
8820 | if (this.mediaSource_.readyState === 'closed') {
|
8821 | this.pendingBuffers_.length = 0;
|
8822 | return;
|
8823 | }
|
8824 |
|
8825 |
|
8826 |
|
8827 | this.processPendingSegments_();
|
8828 | return;
|
8829 | }
|
8830 | }, {
|
8831 | key: 'videoSegmentTimingInfo_',
|
8832 | value: function videoSegmentTimingInfo_(timingInfo) {
|
8833 | var timingInfoInSeconds = {
|
8834 | start: {
|
8835 | decode: timingInfo.start.dts / ONE_SECOND_IN_TS,
|
8836 | presentation: timingInfo.start.pts / ONE_SECOND_IN_TS
|
8837 | },
|
8838 | end: {
|
8839 | decode: timingInfo.end.dts / ONE_SECOND_IN_TS,
|
8840 | presentation: timingInfo.end.pts / ONE_SECOND_IN_TS
|
8841 | },
|
8842 | baseMediaDecodeTime: timingInfo.baseMediaDecodeTime / ONE_SECOND_IN_TS
|
8843 | };
|
8844 |
|
8845 | if (timingInfo.prependedContentDuration) {
|
8846 | timingInfoInSeconds.prependedContentDuration = timingInfo.prependedContentDuration / ONE_SECOND_IN_TS;
|
8847 | }
|
8848 |
|
8849 | this.trigger({
|
8850 | type: 'videoSegmentTimingInfo',
|
8851 | videoSegmentTimingInfo: timingInfoInSeconds
|
8852 | });
|
8853 | }
|
8854 |
|
8855 | |
8856 |
|
8857 |
|
8858 |
|
8859 |
|
8860 |
|
8861 |
|
8862 |
|
8863 |
|
8864 | }, {
|
8865 | key: 'createRealSourceBuffers_',
|
8866 | value: function createRealSourceBuffers_() {
|
8867 | var _this2 = this;
|
8868 |
|
8869 | var types = ['audio', 'video'];
|
8870 |
|
8871 | types.forEach(function (type) {
|
8872 |
|
8873 |
|
8874 | if (!_this2[type + 'Codec_']) {
|
8875 | return;
|
8876 | }
|
8877 |
|
8878 |
|
8879 | if (_this2[type + 'Buffer_']) {
|
8880 | return;
|
8881 | }
|
8882 |
|
8883 | var buffer = null;
|
8884 |
|
8885 |
|
8886 |
|
8887 | if (_this2.mediaSource_[type + 'Buffer_']) {
|
8888 | buffer = _this2.mediaSource_[type + 'Buffer_'];
|
8889 |
|
8890 |
|
8891 |
|
8892 |
|
8893 |
|
8894 |
|
8895 |
|
8896 |
|
8897 |
|
8898 |
|
8899 | buffer.updating = false;
|
8900 | } else {
|
8901 | var codecProperty = type + 'Codec_';
|
8902 | var mimeType = type + '/mp4;codecs="' + _this2[codecProperty] + '"';
|
8903 |
|
8904 | buffer = makeWrappedSourceBuffer(_this2.mediaSource_.nativeMediaSource_, mimeType);
|
8905 |
|
8906 | _this2.mediaSource_[type + 'Buffer_'] = buffer;
|
8907 | }
|
8908 |
|
8909 | _this2[type + 'Buffer_'] = buffer;
|
8910 |
|
8911 |
|
8912 | ['update', 'updatestart', 'updateend'].forEach(function (event) {
|
8913 | buffer.addEventListener(event, function () {
|
8914 |
|
8915 | if (type === 'audio' && _this2.audioDisabled_) {
|
8916 | return;
|
8917 | }
|
8918 |
|
8919 | if (event === 'updateend') {
|
8920 | _this2[type + 'Buffer_'].updating = false;
|
8921 | }
|
8922 |
|
8923 | var shouldTrigger = types.every(function (t) {
|
8924 |
|
8925 |
|
8926 | if (t === 'audio' && _this2.audioDisabled_) {
|
8927 | return true;
|
8928 | }
|
8929 |
|
8930 | if (type !== t && _this2[t + 'Buffer_'] && _this2[t + 'Buffer_'].updating) {
|
8931 | return false;
|
8932 | }
|
8933 | return true;
|
8934 | });
|
8935 |
|
8936 | if (shouldTrigger) {
|
8937 | return _this2.trigger(event);
|
8938 | }
|
8939 | });
|
8940 | });
|
8941 | });
|
8942 | }
|
8943 |
|
8944 | |
8945 |
|
8946 |
|
8947 |
|
8948 |
|
8949 |
|
8950 |
|
8951 |
|
8952 |
|
8953 |
|
8954 | }, {
|
8955 | key: 'appendBuffer',
|
8956 | value: function appendBuffer(segment) {
|
8957 |
|
8958 | this.bufferUpdating_ = true;
|
8959 |
|
8960 | if (this.audioBuffer_ && this.audioBuffer_.buffered.length) {
|
8961 | var audioBuffered = this.audioBuffer_.buffered;
|
8962 |
|
8963 | this.transmuxer_.postMessage({
|
8964 | action: 'setAudioAppendStart',
|
8965 | appendStart: audioBuffered.end(audioBuffered.length - 1)
|
8966 | });
|
8967 | }
|
8968 |
|
8969 | if (this.videoBuffer_) {
|
8970 | this.transmuxer_.postMessage({
|
8971 | action: 'alignGopsWith',
|
8972 | gopsToAlignWith: gopsSafeToAlignWith(this.gopBuffer_, this.mediaSource_.player_ ? this.mediaSource_.player_.currentTime() : null, this.timeMapping_)
|
8973 | });
|
8974 | }
|
8975 |
|
8976 | this.transmuxer_.postMessage({
|
8977 | action: 'push',
|
8978 |
|
8979 |
|
8980 |
|
8981 | data: segment.buffer,
|
8982 |
|
8983 |
|
8984 |
|
8985 | byteOffset: segment.byteOffset,
|
8986 | byteLength: segment.byteLength
|
8987 | }, [segment.buffer]);
|
8988 | this.transmuxer_.postMessage({ action: 'flush' });
|
8989 | }
|
8990 |
|
8991 | |
8992 |
|
8993 |
|
8994 |
|
8995 |
|
8996 |
|
8997 |
|
8998 |
|
8999 |
|
9000 |
|
9001 | }, {
|
9002 | key: 'appendGopInfo_',
|
9003 | value: function appendGopInfo_(event) {
|
9004 | this.gopBuffer_ = updateGopBuffer(this.gopBuffer_, event.data.gopInfo, this.safeAppend_);
|
9005 | }
|
9006 |
|
9007 | |
9008 |
|
9009 |
|
9010 |
|
9011 |
|
9012 |
|
9013 |
|
9014 |
|
9015 |
|
9016 | }, {
|
9017 | key: 'remove',
|
9018 | value: function remove(start, end) {
|
9019 | if (this.videoBuffer_) {
|
9020 | this.videoBuffer_.updating = true;
|
9021 | this.videoBuffer_.remove(start, end);
|
9022 | this.gopBuffer_ = removeGopBuffer(this.gopBuffer_, start, end, this.timeMapping_);
|
9023 | }
|
9024 | if (!this.audioDisabled_ && this.audioBuffer_) {
|
9025 | this.audioBuffer_.updating = true;
|
9026 | this.audioBuffer_.remove(start, end);
|
9027 | }
|
9028 |
|
9029 |
|
9030 | removeCuesFromTrack(start, end, this.metadataTrack_);
|
9031 |
|
9032 |
|
9033 | if (this.inbandTextTracks_) {
|
9034 | for (var track in this.inbandTextTracks_) {
|
9035 | removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
|
9036 | }
|
9037 | }
|
9038 | }
|
9039 |
|
9040 | |
9041 |
|
9042 |
|
9043 |
|
9044 |
|
9045 |
|
9046 |
|
9047 |
|
9048 | }, {
|
9049 | key: 'processPendingSegments_',
|
9050 | value: function processPendingSegments_() {
|
9051 | var sortedSegments = {
|
9052 | video: {
|
9053 | segments: [],
|
9054 | bytes: 0
|
9055 | },
|
9056 | audio: {
|
9057 | segments: [],
|
9058 | bytes: 0
|
9059 | },
|
9060 | captions: [],
|
9061 | metadata: []
|
9062 | };
|
9063 |
|
9064 | if (!this.pendingBuffers_.length) {
|
9065 |
|
9066 | this.trigger('updateend');
|
9067 | this.bufferUpdating_ = false;
|
9068 | return;
|
9069 | }
|
9070 |
|
9071 |
|
9072 |
|
9073 | sortedSegments = this.pendingBuffers_.reduce(function (segmentObj, segment) {
|
9074 | var type = segment.type;
|
9075 | var data = segment.data;
|
9076 | var initSegment = segment.initSegment;
|
9077 |
|
9078 | segmentObj[type].segments.push(data);
|
9079 | segmentObj[type].bytes += data.byteLength;
|
9080 |
|
9081 | segmentObj[type].initSegment = initSegment;
|
9082 |
|
9083 |
|
9084 | if (segment.captions) {
|
9085 | segmentObj.captions = segmentObj.captions.concat(segment.captions);
|
9086 | }
|
9087 |
|
9088 | if (segment.info) {
|
9089 | segmentObj[type].info = segment.info;
|
9090 | }
|
9091 |
|
9092 |
|
9093 | if (segment.metadata) {
|
9094 | segmentObj.metadata = segmentObj.metadata.concat(segment.metadata);
|
9095 | }
|
9096 |
|
9097 | return segmentObj;
|
9098 | }, sortedSegments);
|
9099 |
|
9100 |
|
9101 |
|
9102 | if (!this.videoBuffer_ && !this.audioBuffer_) {
|
9103 |
|
9104 |
|
9105 | if (sortedSegments.video.bytes === 0) {
|
9106 | this.videoCodec_ = null;
|
9107 | }
|
9108 | if (sortedSegments.audio.bytes === 0) {
|
9109 | this.audioCodec_ = null;
|
9110 | }
|
9111 |
|
9112 | this.createRealSourceBuffers_();
|
9113 | }
|
9114 |
|
9115 | if (sortedSegments.audio.info) {
|
9116 | this.mediaSource_.trigger({ type: 'audioinfo', info: sortedSegments.audio.info });
|
9117 | }
|
9118 | if (sortedSegments.video.info) {
|
9119 | this.mediaSource_.trigger({ type: 'videoinfo', info: sortedSegments.video.info });
|
9120 | }
|
9121 |
|
9122 | if (this.appendAudioInitSegment_) {
|
9123 | if (!this.audioDisabled_ && this.audioBuffer_) {
|
9124 | sortedSegments.audio.segments.unshift(sortedSegments.audio.initSegment);
|
9125 | sortedSegments.audio.bytes += sortedSegments.audio.initSegment.byteLength;
|
9126 | }
|
9127 | this.appendAudioInitSegment_ = false;
|
9128 | }
|
9129 |
|
9130 | var triggerUpdateend = false;
|
9131 |
|
9132 |
|
9133 | if (this.videoBuffer_ && sortedSegments.video.bytes) {
|
9134 | sortedSegments.video.segments.unshift(sortedSegments.video.initSegment);
|
9135 | sortedSegments.video.bytes += sortedSegments.video.initSegment.byteLength;
|
9136 | this.concatAndAppendSegments_(sortedSegments.video, this.videoBuffer_);
|
9137 | } else if (this.videoBuffer_ && (this.audioDisabled_ || !this.audioBuffer_)) {
|
9138 |
|
9139 |
|
9140 |
|
9141 |
|
9142 |
|
9143 |
|
9144 | triggerUpdateend = true;
|
9145 | }
|
9146 |
|
9147 |
|
9148 | addTextTrackData(this, sortedSegments.captions, sortedSegments.metadata);
|
9149 |
|
9150 | if (!this.audioDisabled_ && this.audioBuffer_) {
|
9151 | this.concatAndAppendSegments_(sortedSegments.audio, this.audioBuffer_);
|
9152 | }
|
9153 |
|
9154 | this.pendingBuffers_.length = 0;
|
9155 |
|
9156 | if (triggerUpdateend) {
|
9157 | this.trigger('updateend');
|
9158 | }
|
9159 |
|
9160 |
|
9161 | this.bufferUpdating_ = false;
|
9162 | }
|
9163 |
|
9164 | |
9165 |
|
9166 |
|
9167 |
|
9168 |
|
9169 |
|
9170 |
|
9171 |
|
9172 |
|
9173 | }, {
|
9174 | key: 'concatAndAppendSegments_',
|
9175 | value: function concatAndAppendSegments_(segmentObj, destinationBuffer) {
|
9176 | var offset = 0;
|
9177 | var tempBuffer = void 0;
|
9178 |
|
9179 | if (segmentObj.bytes) {
|
9180 | tempBuffer = new Uint8Array(segmentObj.bytes);
|
9181 |
|
9182 |
|
9183 | segmentObj.segments.forEach(function (segment) {
|
9184 | tempBuffer.set(segment, offset);
|
9185 | offset += segment.byteLength;
|
9186 | });
|
9187 |
|
9188 | try {
|
9189 | destinationBuffer.updating = true;
|
9190 | destinationBuffer.appendBuffer(tempBuffer);
|
9191 | } catch (error) {
|
9192 | if (this.mediaSource_.player_) {
|
9193 | this.mediaSource_.player_.error({
|
9194 | code: -3,
|
9195 | type: 'APPEND_BUFFER_ERR',
|
9196 | message: error.message,
|
9197 | originalError: error
|
9198 | });
|
9199 | }
|
9200 | }
|
9201 | }
|
9202 | }
|
9203 |
|
9204 | |
9205 |
|
9206 |
|
9207 |
|
9208 |
|
9209 |
|
9210 |
|
9211 | }, {
|
9212 | key: 'abort',
|
9213 | value: function abort() {
|
9214 | if (this.videoBuffer_) {
|
9215 | this.videoBuffer_.abort();
|
9216 | }
|
9217 | if (!this.audioDisabled_ && this.audioBuffer_) {
|
9218 | this.audioBuffer_.abort();
|
9219 | }
|
9220 | if (this.transmuxer_) {
|
9221 | this.transmuxer_.postMessage({ action: 'reset' });
|
9222 | }
|
9223 | this.pendingBuffers_.length = 0;
|
9224 | this.bufferUpdating_ = false;
|
9225 | }
|
9226 | }, {
|
9227 | key: 'dispose',
|
9228 | value: function dispose() {
|
9229 | if (this.transmuxer_) {
|
9230 | this.transmuxer_.terminate();
|
9231 | }
|
9232 | this.trigger('dispose');
|
9233 | this.off();
|
9234 | }
|
9235 | }]);
|
9236 | return VirtualSourceBuffer;
|
9237 | }(videojs.EventTarget);
|
9238 |
|
9239 |
|
9240 |
|
9241 |
|
9242 |
|
9243 |
|
9244 |
|
9245 |
|
9246 |
|
9247 |
|
9248 |
|
9249 |
|
9250 |
|
9251 |
|
9252 | var HtmlMediaSource = function (_videojs$EventTarget) {
|
9253 | inherits(HtmlMediaSource, _videojs$EventTarget);
|
9254 |
|
9255 | function HtmlMediaSource() {
|
9256 | classCallCheck(this, HtmlMediaSource);
|
9257 |
|
9258 | var _this = possibleConstructorReturn(this, (HtmlMediaSource.__proto__ || Object.getPrototypeOf(HtmlMediaSource)).call(this));
|
9259 |
|
9260 | var property = void 0;
|
9261 |
|
9262 | _this.nativeMediaSource_ = new window$1.MediaSource();
|
9263 |
|
9264 | for (property in _this.nativeMediaSource_) {
|
9265 | if (!(property in HtmlMediaSource.prototype) && typeof _this.nativeMediaSource_[property] === 'function') {
|
9266 | _this[property] = _this.nativeMediaSource_[property].bind(_this.nativeMediaSource_);
|
9267 | }
|
9268 | }
|
9269 |
|
9270 |
|
9271 |
|
9272 |
|
9273 | _this.duration_ = NaN;
|
9274 | Object.defineProperty(_this, 'duration', {
|
9275 | get: function get$$1() {
|
9276 | if (this.duration_ === Infinity) {
|
9277 | return this.duration_;
|
9278 | }
|
9279 | return this.nativeMediaSource_.duration;
|
9280 | },
|
9281 | set: function set$$1(duration) {
|
9282 | this.duration_ = duration;
|
9283 | if (duration !== Infinity) {
|
9284 | this.nativeMediaSource_.duration = duration;
|
9285 | return;
|
9286 | }
|
9287 | }
|
9288 | });
|
9289 | Object.defineProperty(_this, 'seekable', {
|
9290 | get: function get$$1() {
|
9291 | if (this.duration_ === Infinity) {
|
9292 | return videojs.createTimeRanges([[0, this.nativeMediaSource_.duration]]);
|
9293 | }
|
9294 | return this.nativeMediaSource_.seekable;
|
9295 | }
|
9296 | });
|
9297 |
|
9298 | Object.defineProperty(_this, 'readyState', {
|
9299 | get: function get$$1() {
|
9300 | return this.nativeMediaSource_.readyState;
|
9301 | }
|
9302 | });
|
9303 |
|
9304 | Object.defineProperty(_this, 'activeSourceBuffers', {
|
9305 | get: function get$$1() {
|
9306 | return this.activeSourceBuffers_;
|
9307 | }
|
9308 | });
|
9309 |
|
9310 |
|
9311 |
|
9312 | _this.sourceBuffers = [];
|
9313 |
|
9314 | _this.activeSourceBuffers_ = [];
|
9315 |
|
9316 | |
9317 |
|
9318 |
|
9319 |
|
9320 |
|
9321 |
|
9322 | _this.updateActiveSourceBuffers_ = function () {
|
9323 |
|
9324 | _this.activeSourceBuffers_.length = 0;
|
9325 |
|
9326 |
|
9327 |
|
9328 | if (_this.sourceBuffers.length === 1) {
|
9329 | var sourceBuffer = _this.sourceBuffers[0];
|
9330 |
|
9331 | sourceBuffer.appendAudioInitSegment_ = true;
|
9332 | sourceBuffer.audioDisabled_ = !sourceBuffer.audioCodec_;
|
9333 | _this.activeSourceBuffers_.push(sourceBuffer);
|
9334 | return;
|
9335 | }
|
9336 |
|
9337 |
|
9338 |
|
9339 |
|
9340 |
|
9341 | var disableCombined = false;
|
9342 | var disableAudioOnly = true;
|
9343 |
|
9344 |
|
9345 |
|
9346 | for (var i = 0; i < _this.player_.audioTracks().length; i++) {
|
9347 | var track = _this.player_.audioTracks()[i];
|
9348 |
|
9349 | if (track.enabled && track.kind !== 'main') {
|
9350 |
|
9351 |
|
9352 | disableCombined = true;
|
9353 | disableAudioOnly = false;
|
9354 | break;
|
9355 | }
|
9356 | }
|
9357 |
|
9358 | _this.sourceBuffers.forEach(function (sourceBuffer, index) {
|
9359 |
|
9360 |
|
9361 |
|
9362 |
|
9363 |
|
9364 | sourceBuffer.appendAudioInitSegment_ = true;
|
9365 |
|
9366 | if (sourceBuffer.videoCodec_ && sourceBuffer.audioCodec_) {
|
9367 |
|
9368 | sourceBuffer.audioDisabled_ = disableCombined;
|
9369 | } else if (sourceBuffer.videoCodec_ && !sourceBuffer.audioCodec_) {
|
9370 |
|
9371 |
|
9372 |
|
9373 | sourceBuffer.audioDisabled_ = true;
|
9374 | disableAudioOnly = false;
|
9375 | } else if (!sourceBuffer.videoCodec_ && sourceBuffer.audioCodec_) {
|
9376 |
|
9377 |
|
9378 |
|
9379 |
|
9380 |
|
9381 |
|
9382 | sourceBuffer.audioDisabled_ = index ? disableAudioOnly : !disableAudioOnly;
|
9383 | if (sourceBuffer.audioDisabled_) {
|
9384 | return;
|
9385 | }
|
9386 | }
|
9387 |
|
9388 | _this.activeSourceBuffers_.push(sourceBuffer);
|
9389 | });
|
9390 | };
|
9391 |
|
9392 | _this.onPlayerMediachange_ = function () {
|
9393 | _this.sourceBuffers.forEach(function (sourceBuffer) {
|
9394 | sourceBuffer.appendAudioInitSegment_ = true;
|
9395 | });
|
9396 | };
|
9397 |
|
9398 | _this.onHlsReset_ = function () {
|
9399 | _this.sourceBuffers.forEach(function (sourceBuffer) {
|
9400 | if (sourceBuffer.transmuxer_) {
|
9401 | sourceBuffer.transmuxer_.postMessage({ action: 'resetCaptions' });
|
9402 | }
|
9403 | });
|
9404 | };
|
9405 |
|
9406 | _this.onHlsSegmentTimeMapping_ = function (event) {
|
9407 | _this.sourceBuffers.forEach(function (buffer) {
|
9408 | return buffer.timeMapping_ = event.mapping;
|
9409 | });
|
9410 | };
|
9411 |
|
9412 |
|
9413 | ['sourceopen', 'sourceclose', 'sourceended'].forEach(function (eventName) {
|
9414 | this.nativeMediaSource_.addEventListener(eventName, this.trigger.bind(this));
|
9415 | }, _this);
|
9416 |
|
9417 |
|
9418 |
|
9419 | _this.on('sourceopen', function (event) {
|
9420 |
|
9421 | var video = document.querySelector('[src="' + _this.url_ + '"]');
|
9422 |
|
9423 | if (!video) {
|
9424 | return;
|
9425 | }
|
9426 |
|
9427 | _this.player_ = videojs(video.parentNode);
|
9428 |
|
9429 | if (!_this.player_) {
|
9430 | return;
|
9431 | }
|
9432 |
|
9433 |
|
9434 |
|
9435 | _this.player_.tech_.on('hls-reset', _this.onHlsReset_);
|
9436 |
|
9437 |
|
9438 |
|
9439 | _this.player_.tech_.on('hls-segment-time-mapping', _this.onHlsSegmentTimeMapping_);
|
9440 |
|
9441 | if (_this.player_.audioTracks && _this.player_.audioTracks()) {
|
9442 | _this.player_.audioTracks().on('change', _this.updateActiveSourceBuffers_);
|
9443 | _this.player_.audioTracks().on('addtrack', _this.updateActiveSourceBuffers_);
|
9444 | _this.player_.audioTracks().on('removetrack', _this.updateActiveSourceBuffers_);
|
9445 | }
|
9446 |
|
9447 | _this.player_.on('mediachange', _this.onPlayerMediachange_);
|
9448 | });
|
9449 |
|
9450 | _this.on('sourceended', function (event) {
|
9451 | var duration = durationOfVideo(_this.duration);
|
9452 |
|
9453 | for (var i = 0; i < _this.sourceBuffers.length; i++) {
|
9454 | var sourcebuffer = _this.sourceBuffers[i];
|
9455 | var cues = sourcebuffer.metadataTrack_ && sourcebuffer.metadataTrack_.cues;
|
9456 |
|
9457 | if (cues && cues.length) {
|
9458 | cues[cues.length - 1].endTime = duration;
|
9459 | }
|
9460 | }
|
9461 | });
|
9462 |
|
9463 |
|
9464 |
|
9465 | _this.on('sourceclose', function (event) {
|
9466 | this.sourceBuffers.forEach(function (sourceBuffer) {
|
9467 | if (sourceBuffer.transmuxer_) {
|
9468 | sourceBuffer.transmuxer_.terminate();
|
9469 | }
|
9470 | });
|
9471 |
|
9472 | this.sourceBuffers.length = 0;
|
9473 | if (!this.player_) {
|
9474 | return;
|
9475 | }
|
9476 |
|
9477 | if (this.player_.audioTracks && this.player_.audioTracks()) {
|
9478 | this.player_.audioTracks().off('change', this.updateActiveSourceBuffers_);
|
9479 | this.player_.audioTracks().off('addtrack', this.updateActiveSourceBuffers_);
|
9480 | this.player_.audioTracks().off('removetrack', this.updateActiveSourceBuffers_);
|
9481 | }
|
9482 |
|
9483 |
|
9484 |
|
9485 |
|
9486 |
|
9487 | if (this.player_.el_) {
|
9488 | this.player_.off('mediachange', this.onPlayerMediachange_);
|
9489 | }
|
9490 |
|
9491 | if (this.player_.tech_ && this.player_.tech_.el_) {
|
9492 | this.player_.tech_.off('hls-reset', this.onHlsReset_);
|
9493 | this.player_.tech_.off('hls-segment-time-mapping', this.onHlsSegmentTimeMapping_);
|
9494 | }
|
9495 | });
|
9496 | return _this;
|
9497 | }
|
9498 |
|
9499 | |
9500 |
|
9501 |
|
9502 |
|
9503 |
|
9504 |
|
9505 |
|
9506 |
|
9507 |
|
9508 | createClass(HtmlMediaSource, [{
|
9509 | key: 'addSeekableRange_',
|
9510 | value: function addSeekableRange_(start, end) {
|
9511 | var error = void 0;
|
9512 |
|
9513 | if (this.duration !== Infinity) {
|
9514 | error = new Error('MediaSource.addSeekableRange() can only be invoked ' + 'when the duration is Infinity');
|
9515 | error.name = 'InvalidStateError';
|
9516 | error.code = 11;
|
9517 | throw error;
|
9518 | }
|
9519 |
|
9520 | if (end > this.nativeMediaSource_.duration || isNaN(this.nativeMediaSource_.duration)) {
|
9521 | this.nativeMediaSource_.duration = end;
|
9522 | }
|
9523 | }
|
9524 |
|
9525 | |
9526 |
|
9527 |
|
9528 |
|
9529 |
|
9530 |
|
9531 |
|
9532 |
|
9533 | }, {
|
9534 | key: 'addSourceBuffer',
|
9535 | value: function addSourceBuffer(type) {
|
9536 | var buffer = void 0;
|
9537 | var parsedType = parseContentType(type);
|
9538 |
|
9539 |
|
9540 |
|
9541 | if (/^(video|audio)\/mp2t$/i.test(parsedType.type)) {
|
9542 | var codecs = [];
|
9543 |
|
9544 | if (parsedType.parameters && parsedType.parameters.codecs) {
|
9545 | codecs = parsedType.parameters.codecs.split(',');
|
9546 | codecs = translateLegacyCodecs(codecs);
|
9547 | codecs = codecs.filter(function (codec) {
|
9548 | return isAudioCodec(codec) || isVideoCodec(codec);
|
9549 | });
|
9550 | }
|
9551 |
|
9552 | if (codecs.length === 0) {
|
9553 | codecs = ['avc1.4d400d', 'mp4a.40.2'];
|
9554 | }
|
9555 |
|
9556 | buffer = new VirtualSourceBuffer(this, codecs);
|
9557 |
|
9558 | if (this.sourceBuffers.length !== 0) {
|
9559 |
|
9560 |
|
9561 |
|
9562 |
|
9563 |
|
9564 | this.sourceBuffers[0].createRealSourceBuffers_();
|
9565 | buffer.createRealSourceBuffers_();
|
9566 |
|
9567 |
|
9568 |
|
9569 | this.sourceBuffers[0].audioDisabled_ = true;
|
9570 | }
|
9571 | } else {
|
9572 |
|
9573 | buffer = this.nativeMediaSource_.addSourceBuffer(type);
|
9574 | }
|
9575 |
|
9576 | this.sourceBuffers.push(buffer);
|
9577 | return buffer;
|
9578 | }
|
9579 | }, {
|
9580 | key: 'dispose',
|
9581 | value: function dispose() {
|
9582 | this.trigger('dispose');
|
9583 | this.off();
|
9584 |
|
9585 | this.sourceBuffers.forEach(function (buffer) {
|
9586 | if (buffer.dispose) {
|
9587 | buffer.dispose();
|
9588 | }
|
9589 | });
|
9590 |
|
9591 | this.sourceBuffers.length = 0;
|
9592 | }
|
9593 | }]);
|
9594 | return HtmlMediaSource;
|
9595 | }(videojs.EventTarget);
|
9596 |
|
9597 |
|
9598 |
|
9599 |
|
9600 | var urlCount = 0;
|
9601 |
|
9602 |
|
9603 |
|
9604 |
|
9605 |
|
9606 |
|
9607 |
|
9608 |
|
9609 | videojs.mediaSources = {};
|
9610 |
|
9611 |
|
9612 |
|
9613 |
|
9614 |
|
9615 |
|
9616 |
|
9617 |
|
9618 | var open = function open(msObjectURL, swfId) {
|
9619 | var mediaSource = videojs.mediaSources[msObjectURL];
|
9620 |
|
9621 | if (mediaSource) {
|
9622 | mediaSource.trigger({ type: 'sourceopen', swfId: swfId });
|
9623 | } else {
|
9624 | throw new Error('Media Source not found (Video.js)');
|
9625 | }
|
9626 | };
|
9627 |
|
9628 |
|
9629 |
|
9630 |
|
9631 |
|
9632 |
|
9633 |
|
9634 | var supportsNativeMediaSources = function supportsNativeMediaSources() {
|
9635 | return !!window$1.MediaSource && !!window$1.MediaSource.isTypeSupported && window$1.MediaSource.isTypeSupported('video/mp4;codecs="avc1.4d400d,mp4a.40.2"');
|
9636 | };
|
9637 |
|
9638 |
|
9639 |
|
9640 |
|
9641 |
|
9642 |
|
9643 |
|
9644 |
|
9645 | var MediaSource = function MediaSource() {
|
9646 | this.MediaSource = {
|
9647 | open: open,
|
9648 | supportsNativeMediaSources: supportsNativeMediaSources
|
9649 | };
|
9650 |
|
9651 | if (supportsNativeMediaSources()) {
|
9652 | return new HtmlMediaSource();
|
9653 | }
|
9654 |
|
9655 | throw new Error('Cannot use create a virtual MediaSource for this video');
|
9656 | };
|
9657 |
|
9658 | MediaSource.open = open;
|
9659 | MediaSource.supportsNativeMediaSources = supportsNativeMediaSources;
|
9660 |
|
9661 |
|
9662 |
|
9663 |
|
9664 |
|
9665 |
|
9666 |
|
9667 | var URL$1 = {
|
9668 | |
9669 |
|
9670 |
|
9671 |
|
9672 |
|
9673 |
|
9674 |
|
9675 |
|
9676 | createObjectURL: function createObjectURL(object) {
|
9677 | var objectUrlPrefix = 'blob:vjs-media-source/';
|
9678 | var url = void 0;
|
9679 |
|
9680 |
|
9681 | if (object instanceof HtmlMediaSource) {
|
9682 | url = window$1.URL.createObjectURL(object.nativeMediaSource_);
|
9683 | object.url_ = url;
|
9684 | return url;
|
9685 | }
|
9686 |
|
9687 |
|
9688 | if (!(object instanceof HtmlMediaSource)) {
|
9689 | url = window$1.URL.createObjectURL(object);
|
9690 | object.url_ = url;
|
9691 | return url;
|
9692 | }
|
9693 |
|
9694 |
|
9695 |
|
9696 | url = objectUrlPrefix + urlCount;
|
9697 |
|
9698 | urlCount++;
|
9699 |
|
9700 |
|
9701 | videojs.mediaSources[url] = object;
|
9702 |
|
9703 | return url;
|
9704 | }
|
9705 | };
|
9706 |
|
9707 | videojs.MediaSource = MediaSource;
|
9708 | videojs.URL = URL$1;
|
9709 |
|
9710 | var EventTarget$1 = videojs.EventTarget,
|
9711 | mergeOptions$2 = videojs.mergeOptions;
|
9712 |
|
9713 |
|
9714 |
|
9715 |
|
9716 |
|
9717 |
|
9718 |
|
9719 |
|
9720 |
|
9721 |
|
9722 |
|
9723 |
|
9724 |
|
9725 |
|
9726 | var updateMaster$1 = function updateMaster$$1(oldMaster, newMaster) {
|
9727 | var noChanges = void 0;
|
9728 | var update = mergeOptions$2(oldMaster, {
|
9729 |
|
9730 | duration: newMaster.duration,
|
9731 | minimumUpdatePeriod: newMaster.minimumUpdatePeriod
|
9732 | });
|
9733 |
|
9734 |
|
9735 | for (var i = 0; i < newMaster.playlists.length; i++) {
|
9736 | var playlistUpdate = updateMaster(update, newMaster.playlists[i]);
|
9737 |
|
9738 | if (playlistUpdate) {
|
9739 | update = playlistUpdate;
|
9740 | } else {
|
9741 | noChanges = true;
|
9742 | }
|
9743 | }
|
9744 |
|
9745 |
|
9746 | forEachMediaGroup(newMaster, function (properties, type, group, label) {
|
9747 | if (properties.playlists && properties.playlists.length) {
|
9748 | var id = properties.playlists[0].id;
|
9749 | var _playlistUpdate = updateMaster(update, properties.playlists[0]);
|
9750 |
|
9751 | if (_playlistUpdate) {
|
9752 | update = _playlistUpdate;
|
9753 |
|
9754 | update.mediaGroups[type][group][label].playlists[0] = update.playlists[id];
|
9755 | noChanges = false;
|
9756 | }
|
9757 | }
|
9758 | });
|
9759 |
|
9760 | if (noChanges) {
|
9761 | return null;
|
9762 | }
|
9763 |
|
9764 | return update;
|
9765 | };
|
9766 |
|
9767 | var generateSidxKey = function generateSidxKey(sidxInfo) {
|
9768 |
|
9769 | var sidxByteRangeEnd = sidxInfo.byterange.offset + sidxInfo.byterange.length - 1;
|
9770 |
|
9771 | return sidxInfo.uri + '-' + sidxInfo.byterange.offset + '-' + sidxByteRangeEnd;
|
9772 | };
|
9773 |
|
9774 |
|
9775 |
|
9776 |
|
9777 |
|
9778 | var equivalentSidx = function equivalentSidx(a, b) {
|
9779 | var neitherMap = Boolean(!a.map && !b.map);
|
9780 |
|
9781 | var equivalentMap = neitherMap || Boolean(a.map && b.map && a.map.byterange.offset === b.map.byterange.offset && a.map.byterange.length === b.map.byterange.length);
|
9782 |
|
9783 | return equivalentMap && a.uri === b.uri && a.byterange.offset === b.byterange.offset && a.byterange.length === b.byterange.length;
|
9784 | };
|
9785 |
|
9786 |
|
9787 | var compareSidxEntry = function compareSidxEntry(playlists, oldSidxMapping) {
|
9788 | var newSidxMapping = {};
|
9789 |
|
9790 | for (var id in playlists) {
|
9791 | var playlist = playlists[id];
|
9792 | var currentSidxInfo = playlist.sidx;
|
9793 |
|
9794 | if (currentSidxInfo) {
|
9795 | var key = generateSidxKey(currentSidxInfo);
|
9796 |
|
9797 | if (!oldSidxMapping[key]) {
|
9798 | break;
|
9799 | }
|
9800 |
|
9801 | var savedSidxInfo = oldSidxMapping[key].sidxInfo;
|
9802 |
|
9803 | if (equivalentSidx(savedSidxInfo, currentSidxInfo)) {
|
9804 | newSidxMapping[key] = oldSidxMapping[key];
|
9805 | }
|
9806 | }
|
9807 | }
|
9808 |
|
9809 | return newSidxMapping;
|
9810 | };
|
9811 |
|
9812 |
|
9813 |
|
9814 |
|
9815 |
|
9816 |
|
9817 |
|
9818 |
|
9819 |
|
9820 |
|
9821 |
|
9822 | var filterChangedSidxMappings = function filterChangedSidxMappings(masterXml, srcUrl, clientOffset, oldSidxMapping) {
|
9823 |
|
9824 | var master = mpdParser.parse(masterXml, {
|
9825 | manifestUri: srcUrl,
|
9826 | clientOffset: clientOffset
|
9827 | });
|
9828 |
|
9829 | var videoSidx = compareSidxEntry(master.playlists, oldSidxMapping);
|
9830 | var mediaGroupSidx = videoSidx;
|
9831 |
|
9832 | forEachMediaGroup(master, function (properties, mediaType, groupKey, labelKey) {
|
9833 | if (properties.playlists && properties.playlists.length) {
|
9834 | var playlists = properties.playlists;
|
9835 |
|
9836 | mediaGroupSidx = mergeOptions$2(mediaGroupSidx, compareSidxEntry(playlists, oldSidxMapping));
|
9837 | }
|
9838 | });
|
9839 |
|
9840 | return mediaGroupSidx;
|
9841 | };
|
9842 |
|
9843 |
|
9844 | var requestSidx_ = function requestSidx_(sidxRange, playlist, xhr, options, finishProcessingFn) {
|
9845 | var sidxInfo = {
|
9846 |
|
9847 | uri: resolveManifestRedirect(options.handleManifestRedirects, sidxRange.resolvedUri),
|
9848 |
|
9849 | byterange: sidxRange.byterange,
|
9850 |
|
9851 | playlist: playlist
|
9852 | };
|
9853 |
|
9854 | var sidxRequestOptions = videojs.mergeOptions(sidxInfo, {
|
9855 | responseType: 'arraybuffer',
|
9856 | headers: segmentXhrHeaders(sidxInfo)
|
9857 | });
|
9858 |
|
9859 | return xhr(sidxRequestOptions, finishProcessingFn);
|
9860 | };
|
9861 |
|
9862 | var DashPlaylistLoader = function (_EventTarget) {
|
9863 | inherits(DashPlaylistLoader, _EventTarget);
|
9864 |
|
9865 |
|
9866 |
|
9867 |
|
9868 | function DashPlaylistLoader(srcUrlOrPlaylist, hls) {
|
9869 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
9870 | var masterPlaylistLoader = arguments[3];
|
9871 | classCallCheck(this, DashPlaylistLoader);
|
9872 |
|
9873 | var _this = possibleConstructorReturn(this, (DashPlaylistLoader.__proto__ || Object.getPrototypeOf(DashPlaylistLoader)).call(this));
|
9874 |
|
9875 | var _options$withCredenti = options.withCredentials,
|
9876 | withCredentials = _options$withCredenti === undefined ? false : _options$withCredenti,
|
9877 | _options$handleManife = options.handleManifestRedirects,
|
9878 | handleManifestRedirects = _options$handleManife === undefined ? false : _options$handleManife;
|
9879 |
|
9880 |
|
9881 | _this.hls_ = hls;
|
9882 | _this.withCredentials = withCredentials;
|
9883 | _this.handleManifestRedirects = handleManifestRedirects;
|
9884 |
|
9885 | if (!srcUrlOrPlaylist) {
|
9886 | throw new Error('A non-empty playlist URL or playlist is required');
|
9887 | }
|
9888 |
|
9889 |
|
9890 | _this.on('minimumUpdatePeriod', function () {
|
9891 | _this.refreshXml_();
|
9892 | });
|
9893 |
|
9894 |
|
9895 | _this.on('mediaupdatetimeout', function () {
|
9896 | _this.refreshMedia_(_this.media().id);
|
9897 | });
|
9898 |
|
9899 | _this.state = 'HAVE_NOTHING';
|
9900 | _this.loadedPlaylists_ = {};
|
9901 |
|
9902 |
|
9903 |
|
9904 | if (typeof srcUrlOrPlaylist === 'string') {
|
9905 | _this.srcUrl = srcUrlOrPlaylist;
|
9906 |
|
9907 |
|
9908 | _this.sidxMapping_ = {};
|
9909 | return possibleConstructorReturn(_this);
|
9910 | }
|
9911 |
|
9912 | _this.setupChildLoader(masterPlaylistLoader, srcUrlOrPlaylist);
|
9913 | return _this;
|
9914 | }
|
9915 |
|
9916 | createClass(DashPlaylistLoader, [{
|
9917 | key: 'setupChildLoader',
|
9918 | value: function setupChildLoader(masterPlaylistLoader, playlist) {
|
9919 | this.masterPlaylistLoader_ = masterPlaylistLoader;
|
9920 | this.childPlaylist_ = playlist;
|
9921 | }
|
9922 | }, {
|
9923 | key: 'dispose',
|
9924 | value: function dispose() {
|
9925 | this.trigger('dispose');
|
9926 | this.stopRequest();
|
9927 | this.loadedPlaylists_ = {};
|
9928 | window$1.clearTimeout(this.minimumUpdatePeriodTimeout_);
|
9929 | window$1.clearTimeout(this.mediaRequest_);
|
9930 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
9931 |
|
9932 | this.off();
|
9933 | }
|
9934 | }, {
|
9935 | key: 'hasPendingRequest',
|
9936 | value: function hasPendingRequest() {
|
9937 | return this.request || this.mediaRequest_;
|
9938 | }
|
9939 | }, {
|
9940 | key: 'stopRequest',
|
9941 | value: function stopRequest() {
|
9942 | if (this.request) {
|
9943 | var oldRequest = this.request;
|
9944 |
|
9945 | this.request = null;
|
9946 | oldRequest.onreadystatechange = null;
|
9947 | oldRequest.abort();
|
9948 | }
|
9949 | }
|
9950 | }, {
|
9951 | key: 'sidxRequestFinished_',
|
9952 | value: function sidxRequestFinished_(playlist, master, startingState, doneFn) {
|
9953 | var _this2 = this;
|
9954 |
|
9955 | return function (err, request) {
|
9956 |
|
9957 | if (!_this2.request) {
|
9958 | return;
|
9959 | }
|
9960 |
|
9961 |
|
9962 | _this2.request = null;
|
9963 |
|
9964 | if (err) {
|
9965 | _this2.error = {
|
9966 | status: request.status,
|
9967 | message: 'DASH playlist request error at URL: ' + playlist.uri,
|
9968 | response: request.response,
|
9969 |
|
9970 | code: 2
|
9971 | };
|
9972 | if (startingState) {
|
9973 | _this2.state = startingState;
|
9974 | }
|
9975 |
|
9976 | _this2.trigger('error');
|
9977 | return doneFn(master, null);
|
9978 | }
|
9979 |
|
9980 | var bytes = new Uint8Array(request.response);
|
9981 | var sidx = mp4Inspector.parseSidx(bytes.subarray(8));
|
9982 |
|
9983 | return doneFn(master, sidx);
|
9984 | };
|
9985 | }
|
9986 | }, {
|
9987 | key: 'media',
|
9988 | value: function media(playlist) {
|
9989 | var _this3 = this;
|
9990 |
|
9991 |
|
9992 | if (!playlist) {
|
9993 | return this.media_;
|
9994 | }
|
9995 |
|
9996 |
|
9997 | if (this.state === 'HAVE_NOTHING') {
|
9998 | throw new Error('Cannot switch media playlist from ' + this.state);
|
9999 | }
|
10000 |
|
10001 | var startingState = this.state;
|
10002 |
|
10003 |
|
10004 | if (typeof playlist === 'string') {
|
10005 | if (!this.master.playlists[playlist]) {
|
10006 | throw new Error('Unknown playlist URI: ' + playlist);
|
10007 | }
|
10008 | playlist = this.master.playlists[playlist];
|
10009 | }
|
10010 |
|
10011 | var mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
10012 |
|
10013 |
|
10014 | if (mediaChange && this.loadedPlaylists_[playlist.id] && this.loadedPlaylists_[playlist.id].endList) {
|
10015 | this.state = 'HAVE_METADATA';
|
10016 | this.media_ = playlist;
|
10017 |
|
10018 |
|
10019 | if (mediaChange) {
|
10020 | this.trigger('mediachanging');
|
10021 | this.trigger('mediachange');
|
10022 | }
|
10023 | return;
|
10024 | }
|
10025 |
|
10026 |
|
10027 | if (!mediaChange) {
|
10028 | return;
|
10029 | }
|
10030 |
|
10031 |
|
10032 | if (this.media_) {
|
10033 | this.trigger('mediachanging');
|
10034 | }
|
10035 |
|
10036 | if (!playlist.sidx) {
|
10037 |
|
10038 |
|
10039 | this.mediaRequest_ = window$1.setTimeout(this.haveMetadata.bind(this, { startingState: startingState, playlist: playlist }), 0);
|
10040 |
|
10041 |
|
10042 | return;
|
10043 | }
|
10044 |
|
10045 |
|
10046 | var oldMaster = void 0;
|
10047 | var sidxMapping = void 0;
|
10048 |
|
10049 |
|
10050 |
|
10051 | if (this.masterPlaylistLoader_) {
|
10052 | oldMaster = this.masterPlaylistLoader_.master;
|
10053 | sidxMapping = this.masterPlaylistLoader_.sidxMapping_;
|
10054 | } else {
|
10055 | oldMaster = this.master;
|
10056 | sidxMapping = this.sidxMapping_;
|
10057 | }
|
10058 |
|
10059 | var sidxKey = generateSidxKey(playlist.sidx);
|
10060 |
|
10061 | sidxMapping[sidxKey] = {
|
10062 | sidxInfo: playlist.sidx
|
10063 | };
|
10064 |
|
10065 | this.request = requestSidx_(playlist.sidx, playlist, this.hls_.xhr, { handleManifestRedirects: this.handleManifestRedirects }, this.sidxRequestFinished_(playlist, oldMaster, startingState, function (newMaster, sidx) {
|
10066 | if (!newMaster || !sidx) {
|
10067 | throw new Error('failed to request sidx');
|
10068 | }
|
10069 |
|
10070 |
|
10071 | sidxMapping[sidxKey].sidx = sidx;
|
10072 |
|
10073 |
|
10074 | _this3.haveMetadata({
|
10075 | startingState: startingState,
|
10076 | playlist: newMaster.playlists[playlist.id]
|
10077 | });
|
10078 | }));
|
10079 | }
|
10080 | }, {
|
10081 | key: 'haveMetadata',
|
10082 | value: function haveMetadata(_ref) {
|
10083 | var startingState = _ref.startingState,
|
10084 | playlist = _ref.playlist;
|
10085 |
|
10086 | this.state = 'HAVE_METADATA';
|
10087 | this.loadedPlaylists_[playlist.id] = playlist;
|
10088 | this.mediaRequest_ = null;
|
10089 |
|
10090 |
|
10091 | this.refreshMedia_(playlist.id);
|
10092 |
|
10093 |
|
10094 |
|
10095 | if (startingState === 'HAVE_MASTER') {
|
10096 | this.trigger('loadedmetadata');
|
10097 | } else {
|
10098 |
|
10099 | this.trigger('mediachange');
|
10100 | }
|
10101 | }
|
10102 | }, {
|
10103 | key: 'pause',
|
10104 | value: function pause() {
|
10105 | this.stopRequest();
|
10106 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
10107 | window$1.clearTimeout(this.minimumUpdatePeriodTimeout_);
|
10108 | if (this.state === 'HAVE_NOTHING') {
|
10109 |
|
10110 |
|
10111 | this.started = false;
|
10112 | }
|
10113 | }
|
10114 | }, {
|
10115 | key: 'load',
|
10116 | value: function load(isFinalRendition) {
|
10117 | var _this4 = this;
|
10118 |
|
10119 | window$1.clearTimeout(this.mediaUpdateTimeout);
|
10120 | window$1.clearTimeout(this.minimumUpdatePeriodTimeout_);
|
10121 |
|
10122 | var media = this.media();
|
10123 |
|
10124 | if (isFinalRendition) {
|
10125 | var delay = media ? media.targetDuration / 2 * 1000 : 5 * 1000;
|
10126 |
|
10127 | this.mediaUpdateTimeout = window$1.setTimeout(function () {
|
10128 | return _this4.load();
|
10129 | }, delay);
|
10130 | return;
|
10131 | }
|
10132 |
|
10133 |
|
10134 |
|
10135 | if (!this.started) {
|
10136 | this.start();
|
10137 | return;
|
10138 | }
|
10139 |
|
10140 | if (media && !media.endList) {
|
10141 | this.trigger('mediaupdatetimeout');
|
10142 | } else {
|
10143 | this.trigger('loadedplaylist');
|
10144 | }
|
10145 | }
|
10146 |
|
10147 | |
10148 |
|
10149 |
|
10150 |
|
10151 |
|
10152 |
|
10153 |
|
10154 | }, {
|
10155 | key: 'parseMasterXml',
|
10156 | value: function parseMasterXml() {
|
10157 | var master = mpdParser.parse(this.masterXml_, {
|
10158 | manifestUri: this.srcUrl,
|
10159 | clientOffset: this.clientOffset_,
|
10160 | sidxMapping: this.sidxMapping_
|
10161 | });
|
10162 |
|
10163 | master.uri = this.srcUrl;
|
10164 |
|
10165 |
|
10166 |
|
10167 |
|
10168 | for (var i = 0; i < master.playlists.length; i++) {
|
10169 | var phonyUri = 'placeholder-uri-' + i;
|
10170 |
|
10171 | master.playlists[i].uri = phonyUri;
|
10172 | }
|
10173 |
|
10174 |
|
10175 |
|
10176 | forEachMediaGroup(master, function (properties, mediaType, groupKey, labelKey) {
|
10177 | if (properties.playlists && properties.playlists.length) {
|
10178 | var _phonyUri = 'placeholder-uri-' + mediaType + '-' + groupKey + '-' + labelKey;
|
10179 | var id = createPlaylistID(0, _phonyUri);
|
10180 |
|
10181 | properties.playlists[0].uri = _phonyUri;
|
10182 | properties.playlists[0].id = id;
|
10183 |
|
10184 | master.playlists[id] = properties.playlists[0];
|
10185 | master.playlists[_phonyUri] = properties.playlists[0];
|
10186 | }
|
10187 | });
|
10188 |
|
10189 | setupMediaPlaylists(master);
|
10190 | resolveMediaGroupUris(master);
|
10191 |
|
10192 | return master;
|
10193 | }
|
10194 | }, {
|
10195 | key: 'start',
|
10196 | value: function start() {
|
10197 | var _this5 = this;
|
10198 |
|
10199 | this.started = true;
|
10200 |
|
10201 |
|
10202 |
|
10203 | if (this.masterPlaylistLoader_) {
|
10204 | this.mediaRequest_ = window$1.setTimeout(this.haveMaster_.bind(this), 0);
|
10205 | return;
|
10206 | }
|
10207 |
|
10208 |
|
10209 | this.request = this.hls_.xhr({
|
10210 | uri: this.srcUrl,
|
10211 | withCredentials: this.withCredentials
|
10212 | }, function (error, req) {
|
10213 |
|
10214 | if (!_this5.request) {
|
10215 | return;
|
10216 | }
|
10217 |
|
10218 |
|
10219 | _this5.request = null;
|
10220 |
|
10221 | if (error) {
|
10222 | _this5.error = {
|
10223 | status: req.status,
|
10224 | message: 'DASH playlist request error at URL: ' + _this5.srcUrl,
|
10225 | responseText: req.responseText,
|
10226 |
|
10227 | code: 2
|
10228 | };
|
10229 | if (_this5.state === 'HAVE_NOTHING') {
|
10230 | _this5.started = false;
|
10231 | }
|
10232 | return _this5.trigger('error');
|
10233 | }
|
10234 |
|
10235 | _this5.masterXml_ = req.responseText;
|
10236 |
|
10237 | if (req.responseHeaders && req.responseHeaders.date) {
|
10238 | _this5.masterLoaded_ = Date.parse(req.responseHeaders.date);
|
10239 | } else {
|
10240 | _this5.masterLoaded_ = Date.now();
|
10241 | }
|
10242 |
|
10243 | _this5.srcUrl = resolveManifestRedirect(_this5.handleManifestRedirects, _this5.srcUrl, req);
|
10244 |
|
10245 | _this5.syncClientServerClock_(_this5.onClientServerClockSync_.bind(_this5));
|
10246 | });
|
10247 | }
|
10248 |
|
10249 | |
10250 |
|
10251 |
|
10252 |
|
10253 |
|
10254 |
|
10255 |
|
10256 |
|
10257 | }, {
|
10258 | key: 'syncClientServerClock_',
|
10259 | value: function syncClientServerClock_(done) {
|
10260 | var _this6 = this;
|
10261 |
|
10262 | var utcTiming = mpdParser.parseUTCTiming(this.masterXml_);
|
10263 |
|
10264 |
|
10265 |
|
10266 | if (utcTiming === null) {
|
10267 | this.clientOffset_ = this.masterLoaded_ - Date.now();
|
10268 | return done();
|
10269 | }
|
10270 |
|
10271 | if (utcTiming.method === 'DIRECT') {
|
10272 | this.clientOffset_ = utcTiming.value - Date.now();
|
10273 | return done();
|
10274 | }
|
10275 |
|
10276 | this.request = this.hls_.xhr({
|
10277 | uri: resolveUrl(this.srcUrl, utcTiming.value),
|
10278 | method: utcTiming.method,
|
10279 | withCredentials: this.withCredentials
|
10280 | }, function (error, req) {
|
10281 |
|
10282 | if (!_this6.request) {
|
10283 | return;
|
10284 | }
|
10285 |
|
10286 | if (error) {
|
10287 |
|
10288 |
|
10289 | _this6.clientOffset_ = _this6.masterLoaded_ - Date.now();
|
10290 | return done();
|
10291 | }
|
10292 |
|
10293 | var serverTime = void 0;
|
10294 |
|
10295 | if (utcTiming.method === 'HEAD') {
|
10296 | if (!req.responseHeaders || !req.responseHeaders.date) {
|
10297 |
|
10298 |
|
10299 | serverTime = _this6.masterLoaded_;
|
10300 | } else {
|
10301 | serverTime = Date.parse(req.responseHeaders.date);
|
10302 | }
|
10303 | } else {
|
10304 | serverTime = Date.parse(req.responseText);
|
10305 | }
|
10306 |
|
10307 | _this6.clientOffset_ = serverTime - Date.now();
|
10308 |
|
10309 | done();
|
10310 | });
|
10311 | }
|
10312 | }, {
|
10313 | key: 'haveMaster_',
|
10314 | value: function haveMaster_() {
|
10315 | this.state = 'HAVE_MASTER';
|
10316 |
|
10317 | this.mediaRequest_ = null;
|
10318 |
|
10319 | if (!this.masterPlaylistLoader_) {
|
10320 | this.master = this.parseMasterXml();
|
10321 |
|
10322 |
|
10323 |
|
10324 | this.trigger('loadedplaylist');
|
10325 | } else if (!this.media_) {
|
10326 |
|
10327 |
|
10328 | this.media(this.childPlaylist_);
|
10329 | }
|
10330 | }
|
10331 |
|
10332 | |
10333 |
|
10334 |
|
10335 |
|
10336 |
|
10337 | }, {
|
10338 | key: 'onClientServerClockSync_',
|
10339 | value: function onClientServerClockSync_() {
|
10340 | var _this7 = this;
|
10341 |
|
10342 | this.haveMaster_();
|
10343 |
|
10344 | if (!this.hasPendingRequest() && !this.media_) {
|
10345 | this.media(this.master.playlists[0]);
|
10346 | }
|
10347 |
|
10348 |
|
10349 |
|
10350 |
|
10351 |
|
10352 |
|
10353 |
|
10354 | if (this.master && this.master.minimumUpdatePeriod) {
|
10355 | this.minimumUpdatePeriodTimeout_ = window$1.setTimeout(function () {
|
10356 | _this7.trigger('minimumUpdatePeriod');
|
10357 | }, this.master.minimumUpdatePeriod);
|
10358 | }
|
10359 | }
|
10360 |
|
10361 | |
10362 |
|
10363 |
|
10364 |
|
10365 |
|
10366 | }, {
|
10367 | key: 'refreshXml_',
|
10368 | value: function refreshXml_() {
|
10369 | var _this8 = this;
|
10370 |
|
10371 |
|
10372 |
|
10373 | this.request = this.hls_.xhr({
|
10374 | uri: this.srcUrl,
|
10375 | withCredentials: this.withCredentials
|
10376 | }, function (error, req) {
|
10377 |
|
10378 | if (!_this8.request) {
|
10379 | return;
|
10380 | }
|
10381 |
|
10382 |
|
10383 | _this8.request = null;
|
10384 |
|
10385 | if (error) {
|
10386 | _this8.error = {
|
10387 | status: req.status,
|
10388 | message: 'DASH playlist request error at URL: ' + _this8.srcUrl,
|
10389 | responseText: req.responseText,
|
10390 |
|
10391 | code: 2
|
10392 | };
|
10393 | if (_this8.state === 'HAVE_NOTHING') {
|
10394 | _this8.started = false;
|
10395 | }
|
10396 | return _this8.trigger('error');
|
10397 | }
|
10398 |
|
10399 | _this8.masterXml_ = req.responseText;
|
10400 |
|
10401 |
|
10402 | _this8.sidxMapping_ = filterChangedSidxMappings(_this8.masterXml_, _this8.srcUrl, _this8.clientOffset_, _this8.sidxMapping_);
|
10403 |
|
10404 | var master = _this8.parseMasterXml();
|
10405 | var updatedMaster = updateMaster$1(_this8.master, master);
|
10406 | var currentSidxInfo = _this8.media().sidx;
|
10407 |
|
10408 | if (updatedMaster) {
|
10409 | if (currentSidxInfo) {
|
10410 | var sidxKey = generateSidxKey(currentSidxInfo);
|
10411 |
|
10412 |
|
10413 | if (!_this8.sidxMapping_[sidxKey]) {
|
10414 | var playlist = _this8.media();
|
10415 |
|
10416 | _this8.request = requestSidx_(playlist.sidx, playlist, _this8.hls_.xhr, { handleManifestRedirects: _this8.handleManifestRedirects }, _this8.sidxRequestFinished_(playlist, master, _this8.state, function (newMaster, sidx) {
|
10417 | if (!newMaster || !sidx) {
|
10418 | throw new Error('failed to request sidx on minimumUpdatePeriod');
|
10419 | }
|
10420 |
|
10421 |
|
10422 | _this8.sidxMapping_[sidxKey].sidx = sidx;
|
10423 |
|
10424 | _this8.minimumUpdatePeriodTimeout_ = window$1.setTimeout(function () {
|
10425 | _this8.trigger('minimumUpdatePeriod');
|
10426 | }, _this8.master.minimumUpdatePeriod);
|
10427 |
|
10428 |
|
10429 | _this8.refreshMedia_(_this8.media().id);
|
10430 |
|
10431 | return;
|
10432 | }));
|
10433 | }
|
10434 | } else {
|
10435 |
|
10436 | _this8.master = updatedMaster;
|
10437 | }
|
10438 | }
|
10439 |
|
10440 | _this8.minimumUpdatePeriodTimeout_ = window$1.setTimeout(function () {
|
10441 | _this8.trigger('minimumUpdatePeriod');
|
10442 | }, _this8.master.minimumUpdatePeriod);
|
10443 | });
|
10444 | }
|
10445 |
|
10446 | |
10447 |
|
10448 |
|
10449 |
|
10450 |
|
10451 |
|
10452 | }, {
|
10453 | key: 'refreshMedia_',
|
10454 | value: function refreshMedia_(mediaID) {
|
10455 | var _this9 = this;
|
10456 |
|
10457 | if (!mediaID) {
|
10458 | throw new Error('refreshMedia_ must take a media id');
|
10459 | }
|
10460 |
|
10461 | var oldMaster = void 0;
|
10462 | var newMaster = void 0;
|
10463 |
|
10464 | if (this.masterPlaylistLoader_) {
|
10465 | oldMaster = this.masterPlaylistLoader_.master;
|
10466 | newMaster = this.masterPlaylistLoader_.parseMasterXml();
|
10467 | } else {
|
10468 | oldMaster = this.master;
|
10469 | newMaster = this.parseMasterXml();
|
10470 | }
|
10471 |
|
10472 | var updatedMaster = updateMaster$1(oldMaster, newMaster);
|
10473 |
|
10474 | if (updatedMaster) {
|
10475 | if (this.masterPlaylistLoader_) {
|
10476 | this.masterPlaylistLoader_.master = updatedMaster;
|
10477 | } else {
|
10478 | this.master = updatedMaster;
|
10479 | }
|
10480 | this.media_ = updatedMaster.playlists[mediaID];
|
10481 | } else {
|
10482 | this.media_ = newMaster.playlists[mediaID];
|
10483 | this.trigger('playlistunchanged');
|
10484 | }
|
10485 |
|
10486 | if (!this.media().endList) {
|
10487 | this.mediaUpdateTimeout = window$1.setTimeout(function () {
|
10488 | _this9.trigger('mediaupdatetimeout');
|
10489 | }, refreshDelay(this.media(), !!updatedMaster));
|
10490 | }
|
10491 |
|
10492 | this.trigger('loadedplaylist');
|
10493 | }
|
10494 | }]);
|
10495 | return DashPlaylistLoader;
|
10496 | }(EventTarget$1);
|
10497 |
|
10498 | var logger = function logger(source) {
|
10499 | if (videojs.log.debug) {
|
10500 | return videojs.log.debug.bind(videojs, 'VHS:', source + ' >');
|
10501 | }
|
10502 |
|
10503 | return function () {};
|
10504 | };
|
10505 |
|
10506 | function noop() {}
|
10507 |
|
10508 |
|
10509 |
|
10510 |
|
10511 |
|
10512 |
|
10513 |
|
10514 |
|
10515 |
|
10516 |
|
10517 |
|
10518 |
|
10519 |
|
10520 |
|
10521 |
|
10522 |
|
10523 |
|
10524 |
|
10525 |
|
10526 |
|
10527 | var SourceUpdater = function () {
|
10528 | function SourceUpdater(mediaSource, mimeType, type, sourceBufferEmitter) {
|
10529 | classCallCheck(this, SourceUpdater);
|
10530 |
|
10531 | this.callbacks_ = [];
|
10532 | this.pendingCallback_ = null;
|
10533 | this.timestampOffset_ = 0;
|
10534 | this.mediaSource = mediaSource;
|
10535 | this.processedAppend_ = false;
|
10536 | this.type_ = type;
|
10537 | this.mimeType_ = mimeType;
|
10538 | this.logger_ = logger('SourceUpdater[' + type + '][' + mimeType + ']');
|
10539 |
|
10540 | if (mediaSource.readyState === 'closed') {
|
10541 | mediaSource.addEventListener('sourceopen', this.createSourceBuffer_.bind(this, mimeType, sourceBufferEmitter));
|
10542 | } else {
|
10543 | this.createSourceBuffer_(mimeType, sourceBufferEmitter);
|
10544 | }
|
10545 | }
|
10546 |
|
10547 | createClass(SourceUpdater, [{
|
10548 | key: 'createSourceBuffer_',
|
10549 | value: function createSourceBuffer_(mimeType, sourceBufferEmitter) {
|
10550 | var _this = this;
|
10551 |
|
10552 | this.sourceBuffer_ = this.mediaSource.addSourceBuffer(mimeType);
|
10553 |
|
10554 | this.logger_('created SourceBuffer');
|
10555 |
|
10556 | if (sourceBufferEmitter) {
|
10557 | sourceBufferEmitter.trigger('sourcebufferadded');
|
10558 |
|
10559 | if (this.mediaSource.sourceBuffers.length < 2) {
|
10560 |
|
10561 |
|
10562 |
|
10563 |
|
10564 | sourceBufferEmitter.on('sourcebufferadded', function () {
|
10565 | _this.start_();
|
10566 | });
|
10567 | return;
|
10568 | }
|
10569 | }
|
10570 |
|
10571 | this.start_();
|
10572 | }
|
10573 | }, {
|
10574 | key: 'start_',
|
10575 | value: function start_() {
|
10576 | var _this2 = this;
|
10577 |
|
10578 | this.started_ = true;
|
10579 |
|
10580 |
|
10581 |
|
10582 | this.onUpdateendCallback_ = function () {
|
10583 | var pendingCallback = _this2.pendingCallback_;
|
10584 |
|
10585 | _this2.pendingCallback_ = null;
|
10586 | _this2.sourceBuffer_.removing = false;
|
10587 |
|
10588 | _this2.logger_('buffered [' + printableRange(_this2.buffered()) + ']');
|
10589 |
|
10590 | if (pendingCallback) {
|
10591 | pendingCallback();
|
10592 | }
|
10593 |
|
10594 | _this2.runCallback_();
|
10595 | };
|
10596 |
|
10597 | this.sourceBuffer_.addEventListener('updateend', this.onUpdateendCallback_);
|
10598 |
|
10599 | this.runCallback_();
|
10600 | }
|
10601 |
|
10602 | |
10603 |
|
10604 |
|
10605 |
|
10606 |
|
10607 |
|
10608 |
|
10609 | }, {
|
10610 | key: 'abort',
|
10611 | value: function abort(done) {
|
10612 | var _this3 = this;
|
10613 |
|
10614 | if (this.processedAppend_) {
|
10615 | this.queueCallback_(function () {
|
10616 | _this3.sourceBuffer_.abort();
|
10617 | }, done);
|
10618 | }
|
10619 | }
|
10620 |
|
10621 | |
10622 |
|
10623 |
|
10624 |
|
10625 |
|
10626 |
|
10627 |
|
10628 |
|
10629 | }, {
|
10630 | key: 'appendBuffer',
|
10631 | value: function appendBuffer(config, done) {
|
10632 | var _this4 = this;
|
10633 |
|
10634 | this.processedAppend_ = true;
|
10635 | this.queueCallback_(function () {
|
10636 | if (config.videoSegmentTimingInfoCallback) {
|
10637 | _this4.sourceBuffer_.addEventListener('videoSegmentTimingInfo', config.videoSegmentTimingInfoCallback);
|
10638 | }
|
10639 | _this4.sourceBuffer_.appendBuffer(config.bytes);
|
10640 | }, function () {
|
10641 | if (config.videoSegmentTimingInfoCallback) {
|
10642 | _this4.sourceBuffer_.removeEventListener('videoSegmentTimingInfo', config.videoSegmentTimingInfoCallback);
|
10643 | }
|
10644 | done();
|
10645 | });
|
10646 | }
|
10647 |
|
10648 | |
10649 |
|
10650 |
|
10651 |
|
10652 |
|
10653 |
|
10654 | }, {
|
10655 | key: 'buffered',
|
10656 | value: function buffered() {
|
10657 | if (!this.sourceBuffer_) {
|
10658 | return videojs.createTimeRanges();
|
10659 | }
|
10660 | return this.sourceBuffer_.buffered;
|
10661 | }
|
10662 |
|
10663 | |
10664 |
|
10665 |
|
10666 |
|
10667 |
|
10668 |
|
10669 |
|
10670 |
|
10671 |
|
10672 |
|
10673 | }, {
|
10674 | key: 'remove',
|
10675 | value: function remove(start, end) {
|
10676 | var _this5 = this;
|
10677 |
|
10678 | var done = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop;
|
10679 |
|
10680 | if (this.processedAppend_) {
|
10681 | this.queueCallback_(function () {
|
10682 | _this5.logger_('remove [' + start + ' => ' + end + ']');
|
10683 | _this5.sourceBuffer_.removing = true;
|
10684 | _this5.sourceBuffer_.remove(start, end);
|
10685 | }, done);
|
10686 | }
|
10687 | }
|
10688 |
|
10689 | |
10690 |
|
10691 |
|
10692 |
|
10693 |
|
10694 |
|
10695 | }, {
|
10696 | key: 'updating',
|
10697 | value: function updating() {
|
10698 |
|
10699 | return !this.sourceBuffer_ || this.sourceBuffer_.updating ||
|
10700 |
|
10701 | !!this.pendingCallback_ && this.pendingCallback_ !== noop;
|
10702 | }
|
10703 |
|
10704 | |
10705 |
|
10706 |
|
10707 |
|
10708 |
|
10709 |
|
10710 | }, {
|
10711 | key: 'timestampOffset',
|
10712 | value: function timestampOffset(offset) {
|
10713 | var _this6 = this;
|
10714 |
|
10715 | if (typeof offset !== 'undefined') {
|
10716 | this.queueCallback_(function () {
|
10717 | _this6.sourceBuffer_.timestampOffset = offset;
|
10718 | _this6.runCallback_();
|
10719 | });
|
10720 | this.timestampOffset_ = offset;
|
10721 | }
|
10722 | return this.timestampOffset_;
|
10723 | }
|
10724 |
|
10725 | |
10726 |
|
10727 |
|
10728 |
|
10729 | }, {
|
10730 | key: 'queueCallback_',
|
10731 | value: function queueCallback_(callback, done) {
|
10732 | this.callbacks_.push([callback.bind(this), done]);
|
10733 | this.runCallback_();
|
10734 | }
|
10735 |
|
10736 | |
10737 |
|
10738 |
|
10739 |
|
10740 | }, {
|
10741 | key: 'runCallback_',
|
10742 | value: function runCallback_() {
|
10743 | var callbacks = void 0;
|
10744 |
|
10745 | if (!this.updating() && this.callbacks_.length && this.started_) {
|
10746 | callbacks = this.callbacks_.shift();
|
10747 | this.pendingCallback_ = callbacks[1];
|
10748 | callbacks[0]();
|
10749 | }
|
10750 | }
|
10751 |
|
10752 | |
10753 |
|
10754 |
|
10755 |
|
10756 | }, {
|
10757 | key: 'dispose',
|
10758 | value: function dispose() {
|
10759 | var _this7 = this;
|
10760 |
|
10761 | var disposeFn = function disposeFn() {
|
10762 | if (_this7.sourceBuffer_ && _this7.mediaSource.readyState === 'open') {
|
10763 | _this7.sourceBuffer_.abort();
|
10764 | }
|
10765 | _this7.sourceBuffer_.removeEventListener('updateend', disposeFn);
|
10766 | };
|
10767 |
|
10768 | this.sourceBuffer_.removeEventListener('updateend', this.onUpdateendCallback_);
|
10769 | if (this.sourceBuffer_.removing) {
|
10770 | this.sourceBuffer_.addEventListener('updateend', disposeFn);
|
10771 | } else {
|
10772 | disposeFn();
|
10773 | }
|
10774 | }
|
10775 | }]);
|
10776 | return SourceUpdater;
|
10777 | }();
|
10778 |
|
10779 | var Config = {
|
10780 | GOAL_BUFFER_LENGTH: 30,
|
10781 | MAX_GOAL_BUFFER_LENGTH: 60,
|
10782 | GOAL_BUFFER_LENGTH_RATE: 1,
|
10783 |
|
10784 | INITIAL_BANDWIDTH: 4194304,
|
10785 |
|
10786 |
|
10787 | BANDWIDTH_VARIANCE: 1.2,
|
10788 |
|
10789 | BUFFER_LOW_WATER_LINE: 0,
|
10790 | MAX_BUFFER_LOW_WATER_LINE: 30,
|
10791 | BUFFER_LOW_WATER_LINE_RATE: 1
|
10792 | };
|
10793 |
|
10794 | var REQUEST_ERRORS = {
|
10795 | FAILURE: 2,
|
10796 | TIMEOUT: -101,
|
10797 | ABORTED: -102
|
10798 | };
|
10799 |
|
10800 |
|
10801 |
|
10802 |
|
10803 |
|
10804 |
|
10805 | var abortAll = function abortAll(activeXhrs) {
|
10806 | activeXhrs.forEach(function (xhr) {
|
10807 | xhr.abort();
|
10808 | });
|
10809 | };
|
10810 |
|
10811 |
|
10812 |
|
10813 |
|
10814 |
|
10815 |
|
10816 | var getRequestStats = function getRequestStats(request) {
|
10817 | return {
|
10818 | bandwidth: request.bandwidth,
|
10819 | bytesReceived: request.bytesReceived || 0,
|
10820 | roundTripTime: request.roundTripTime || 0
|
10821 | };
|
10822 | };
|
10823 |
|
10824 |
|
10825 |
|
10826 |
|
10827 |
|
10828 |
|
10829 |
|
10830 | var getProgressStats = function getProgressStats(progressEvent) {
|
10831 | var request = progressEvent.target;
|
10832 | var roundTripTime = Date.now() - request.requestTime;
|
10833 | var stats = {
|
10834 | bandwidth: Infinity,
|
10835 | bytesReceived: 0,
|
10836 | roundTripTime: roundTripTime || 0
|
10837 | };
|
10838 |
|
10839 | stats.bytesReceived = progressEvent.loaded;
|
10840 |
|
10841 |
|
10842 |
|
10843 | stats.bandwidth = Math.floor(stats.bytesReceived / stats.roundTripTime * 8 * 1000);
|
10844 |
|
10845 | return stats;
|
10846 | };
|
10847 |
|
10848 |
|
10849 |
|
10850 |
|
10851 |
|
10852 |
|
10853 |
|
10854 |
|
10855 | var handleErrors = function handleErrors(error, request) {
|
10856 | if (request.timedout) {
|
10857 | return {
|
10858 | status: request.status,
|
10859 | message: 'HLS request timed-out at URL: ' + request.uri,
|
10860 | code: REQUEST_ERRORS.TIMEOUT,
|
10861 | xhr: request
|
10862 | };
|
10863 | }
|
10864 |
|
10865 | if (request.aborted) {
|
10866 | return {
|
10867 | status: request.status,
|
10868 | message: 'HLS request aborted at URL: ' + request.uri,
|
10869 | code: REQUEST_ERRORS.ABORTED,
|
10870 | xhr: request
|
10871 | };
|
10872 | }
|
10873 |
|
10874 | if (error) {
|
10875 | return {
|
10876 | status: request.status,
|
10877 | message: 'HLS request errored at URL: ' + request.uri,
|
10878 | code: REQUEST_ERRORS.FAILURE,
|
10879 | xhr: request
|
10880 | };
|
10881 | }
|
10882 |
|
10883 | return null;
|
10884 | };
|
10885 |
|
10886 |
|
10887 |
|
10888 |
|
10889 |
|
10890 |
|
10891 |
|
10892 |
|
10893 |
|
10894 |
|
10895 | var handleKeyResponse = function handleKeyResponse(segment, finishProcessingFn) {
|
10896 | return function (error, request) {
|
10897 | var response = request.response;
|
10898 | var errorObj = handleErrors(error, request);
|
10899 |
|
10900 | if (errorObj) {
|
10901 | return finishProcessingFn(errorObj, segment);
|
10902 | }
|
10903 |
|
10904 | if (response.byteLength !== 16) {
|
10905 | return finishProcessingFn({
|
10906 | status: request.status,
|
10907 | message: 'Invalid HLS key at URL: ' + request.uri,
|
10908 | code: REQUEST_ERRORS.FAILURE,
|
10909 | xhr: request
|
10910 | }, segment);
|
10911 | }
|
10912 |
|
10913 | var view = new DataView(response);
|
10914 |
|
10915 | segment.key.bytes = new Uint32Array([view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12)]);
|
10916 | return finishProcessingFn(null, segment);
|
10917 | };
|
10918 | };
|
10919 |
|
10920 |
|
10921 |
|
10922 |
|
10923 |
|
10924 |
|
10925 |
|
10926 |
|
10927 |
|
10928 | var handleInitSegmentResponse = function handleInitSegmentResponse(segment, captionParser, finishProcessingFn) {
|
10929 | return function (error, request) {
|
10930 | var response = request.response;
|
10931 | var errorObj = handleErrors(error, request);
|
10932 |
|
10933 | if (errorObj) {
|
10934 | return finishProcessingFn(errorObj, segment);
|
10935 | }
|
10936 |
|
10937 |
|
10938 | if (response.byteLength === 0) {
|
10939 | return finishProcessingFn({
|
10940 | status: request.status,
|
10941 | message: 'Empty HLS segment content at URL: ' + request.uri,
|
10942 | code: REQUEST_ERRORS.FAILURE,
|
10943 | xhr: request
|
10944 | }, segment);
|
10945 | }
|
10946 |
|
10947 | segment.map.bytes = new Uint8Array(request.response);
|
10948 |
|
10949 |
|
10950 | if (captionParser && !captionParser.isInitialized()) {
|
10951 | captionParser.init();
|
10952 | }
|
10953 |
|
10954 | segment.map.timescales = mp4probe.timescale(segment.map.bytes);
|
10955 | segment.map.videoTrackIds = mp4probe.videoTrackIds(segment.map.bytes);
|
10956 |
|
10957 | return finishProcessingFn(null, segment);
|
10958 | };
|
10959 | };
|
10960 |
|
10961 |
|
10962 |
|
10963 |
|
10964 |
|
10965 |
|
10966 |
|
10967 |
|
10968 |
|
10969 |
|
10970 |
|
10971 | var handleSegmentResponse = function handleSegmentResponse(segment, captionParser, finishProcessingFn) {
|
10972 | return function (error, request) {
|
10973 | var response = request.response;
|
10974 | var errorObj = handleErrors(error, request);
|
10975 | var parsed = void 0;
|
10976 |
|
10977 | if (errorObj) {
|
10978 | return finishProcessingFn(errorObj, segment);
|
10979 | }
|
10980 |
|
10981 |
|
10982 | if (response.byteLength === 0) {
|
10983 | return finishProcessingFn({
|
10984 | status: request.status,
|
10985 | message: 'Empty HLS segment content at URL: ' + request.uri,
|
10986 | code: REQUEST_ERRORS.FAILURE,
|
10987 | xhr: request
|
10988 | }, segment);
|
10989 | }
|
10990 |
|
10991 | segment.stats = getRequestStats(request);
|
10992 |
|
10993 | if (segment.key) {
|
10994 | segment.encryptedBytes = new Uint8Array(request.response);
|
10995 | } else {
|
10996 | segment.bytes = new Uint8Array(request.response);
|
10997 | }
|
10998 |
|
10999 |
|
11000 |
|
11001 | if (captionParser && segment.map && segment.map.bytes) {
|
11002 |
|
11003 | if (!captionParser.isInitialized()) {
|
11004 | captionParser.init();
|
11005 | }
|
11006 |
|
11007 | parsed = captionParser.parse(segment.bytes, segment.map.videoTrackIds, segment.map.timescales);
|
11008 |
|
11009 | if (parsed && parsed.captions) {
|
11010 | segment.captionStreams = parsed.captionStreams;
|
11011 | segment.fmp4Captions = parsed.captions;
|
11012 | }
|
11013 | }
|
11014 |
|
11015 | return finishProcessingFn(null, segment);
|
11016 | };
|
11017 | };
|
11018 |
|
11019 |
|
11020 |
|
11021 |
|
11022 |
|
11023 |
|
11024 |
|
11025 |
|
11026 |
|
11027 | var decryptSegment = function decryptSegment(decrypter, segment, doneFn) {
|
11028 | var decryptionHandler = function decryptionHandler(event) {
|
11029 | if (event.data.source === segment.requestId) {
|
11030 | decrypter.removeEventListener('message', decryptionHandler);
|
11031 | var decrypted = event.data.decrypted;
|
11032 |
|
11033 | segment.bytes = new Uint8Array(decrypted.bytes, decrypted.byteOffset, decrypted.byteLength);
|
11034 | return doneFn(null, segment);
|
11035 | }
|
11036 | };
|
11037 |
|
11038 | decrypter.addEventListener('message', decryptionHandler);
|
11039 |
|
11040 | var keyBytes = void 0;
|
11041 |
|
11042 | if (segment.key.bytes.slice) {
|
11043 | keyBytes = segment.key.bytes.slice();
|
11044 | } else {
|
11045 | keyBytes = new Uint32Array(Array.prototype.slice.call(segment.key.bytes));
|
11046 | }
|
11047 |
|
11048 |
|
11049 |
|
11050 | decrypter.postMessage(createTransferableMessage({
|
11051 | source: segment.requestId,
|
11052 | encrypted: segment.encryptedBytes,
|
11053 | key: keyBytes,
|
11054 | iv: segment.key.iv
|
11055 | }), [segment.encryptedBytes.buffer, keyBytes.buffer]);
|
11056 | };
|
11057 |
|
11058 |
|
11059 |
|
11060 |
|
11061 |
|
11062 |
|
11063 |
|
11064 |
|
11065 |
|
11066 |
|
11067 |
|
11068 |
|
11069 | var waitForCompletion = function waitForCompletion(activeXhrs, decrypter, doneFn) {
|
11070 | var count = 0;
|
11071 | var didError = false;
|
11072 |
|
11073 | return function (error, segment) {
|
11074 | if (didError) {
|
11075 | return;
|
11076 | }
|
11077 |
|
11078 | if (error) {
|
11079 | didError = true;
|
11080 |
|
11081 | abortAll(activeXhrs);
|
11082 |
|
11083 |
|
11084 |
|
11085 |
|
11086 |
|
11087 |
|
11088 |
|
11089 |
|
11090 |
|
11091 |
|
11092 |
|
11093 |
|
11094 | return doneFn(error, segment);
|
11095 | }
|
11096 |
|
11097 | count += 1;
|
11098 |
|
11099 | if (count === activeXhrs.length) {
|
11100 |
|
11101 | segment.endOfAllRequests = Date.now();
|
11102 |
|
11103 | if (segment.encryptedBytes) {
|
11104 | return decryptSegment(decrypter, segment, doneFn);
|
11105 | }
|
11106 |
|
11107 | return doneFn(null, segment);
|
11108 | }
|
11109 | };
|
11110 | };
|
11111 |
|
11112 |
|
11113 |
|
11114 |
|
11115 |
|
11116 |
|
11117 |
|
11118 |
|
11119 |
|
11120 |
|
11121 |
|
11122 | var handleProgress = function handleProgress(segment, progressFn) {
|
11123 | return function (event) {
|
11124 | segment.stats = videojs.mergeOptions(segment.stats, getProgressStats(event));
|
11125 |
|
11126 |
|
11127 | if (!segment.stats.firstBytesReceivedAt && segment.stats.bytesReceived) {
|
11128 | segment.stats.firstBytesReceivedAt = Date.now();
|
11129 | }
|
11130 |
|
11131 | return progressFn(event, segment);
|
11132 | };
|
11133 | };
|
11134 |
|
11135 |
|
11136 |
|
11137 |
|
11138 |
|
11139 |
|
11140 |
|
11141 |
|
11142 |
|
11143 |
|
11144 |
|
11145 |
|
11146 |
|
11147 |
|
11148 |
|
11149 |
|
11150 |
|
11151 |
|
11152 |
|
11153 |
|
11154 |
|
11155 |
|
11156 |
|
11157 |
|
11158 |
|
11159 |
|
11160 |
|
11161 |
|
11162 |
|
11163 |
|
11164 |
|
11165 |
|
11166 |
|
11167 |
|
11168 |
|
11169 |
|
11170 |
|
11171 |
|
11172 |
|
11173 |
|
11174 |
|
11175 |
|
11176 |
|
11177 |
|
11178 |
|
11179 |
|
11180 |
|
11181 |
|
11182 |
|
11183 | var mediaSegmentRequest = function mediaSegmentRequest(xhr, xhrOptions, decryptionWorker, captionParser, segment, progressFn, doneFn) {
|
11184 | var activeXhrs = [];
|
11185 | var finishProcessingFn = waitForCompletion(activeXhrs, decryptionWorker, doneFn);
|
11186 |
|
11187 |
|
11188 | if (segment.key && !segment.key.bytes) {
|
11189 | var keyRequestOptions = videojs.mergeOptions(xhrOptions, {
|
11190 | uri: segment.key.resolvedUri,
|
11191 | responseType: 'arraybuffer'
|
11192 | });
|
11193 | var keyRequestCallback = handleKeyResponse(segment, finishProcessingFn);
|
11194 | var keyXhr = xhr(keyRequestOptions, keyRequestCallback);
|
11195 |
|
11196 | activeXhrs.push(keyXhr);
|
11197 | }
|
11198 |
|
11199 |
|
11200 | if (segment.map && !segment.map.bytes) {
|
11201 | var initSegmentOptions = videojs.mergeOptions(xhrOptions, {
|
11202 | uri: segment.map.resolvedUri,
|
11203 | responseType: 'arraybuffer',
|
11204 | headers: segmentXhrHeaders(segment.map)
|
11205 | });
|
11206 | var initSegmentRequestCallback = handleInitSegmentResponse(segment, captionParser, finishProcessingFn);
|
11207 | var initSegmentXhr = xhr(initSegmentOptions, initSegmentRequestCallback);
|
11208 |
|
11209 | activeXhrs.push(initSegmentXhr);
|
11210 | }
|
11211 |
|
11212 | var segmentRequestOptions = videojs.mergeOptions(xhrOptions, {
|
11213 | uri: segment.resolvedUri,
|
11214 | responseType: 'arraybuffer',
|
11215 | headers: segmentXhrHeaders(segment)
|
11216 | });
|
11217 | var segmentRequestCallback = handleSegmentResponse(segment, captionParser, finishProcessingFn);
|
11218 | var segmentXhr = xhr(segmentRequestOptions, segmentRequestCallback);
|
11219 |
|
11220 | segmentXhr.addEventListener('progress', handleProgress(segment, progressFn));
|
11221 | activeXhrs.push(segmentXhr);
|
11222 |
|
11223 | return function () {
|
11224 | return abortAll(activeXhrs);
|
11225 | };
|
11226 | };
|
11227 |
|
11228 |
|
11229 |
|
11230 |
|
11231 |
|
11232 |
|
11233 |
|
11234 |
|
11235 |
|
11236 |
|
11237 |
|
11238 |
|
11239 |
|
11240 | var safeGetComputedStyle = function safeGetComputedStyle(el, property) {
|
11241 | var result = void 0;
|
11242 |
|
11243 | if (!el) {
|
11244 | return '';
|
11245 | }
|
11246 |
|
11247 | result = window$1.getComputedStyle(el);
|
11248 | if (!result) {
|
11249 | return '';
|
11250 | }
|
11251 |
|
11252 | return result[property];
|
11253 | };
|
11254 |
|
11255 |
|
11256 |
|
11257 |
|
11258 |
|
11259 |
|
11260 |
|
11261 |
|
11262 | var stableSort = function stableSort(array, sortFn) {
|
11263 | var newArray = array.slice();
|
11264 |
|
11265 | array.sort(function (left, right) {
|
11266 | var cmp = sortFn(left, right);
|
11267 |
|
11268 | if (cmp === 0) {
|
11269 | return newArray.indexOf(left) - newArray.indexOf(right);
|
11270 | }
|
11271 | return cmp;
|
11272 | });
|
11273 | };
|
11274 |
|
11275 |
|
11276 |
|
11277 |
|
11278 |
|
11279 |
|
11280 |
|
11281 |
|
11282 |
|
11283 |
|
11284 |
|
11285 | var comparePlaylistBandwidth = function comparePlaylistBandwidth(left, right) {
|
11286 | var leftBandwidth = void 0;
|
11287 | var rightBandwidth = void 0;
|
11288 |
|
11289 | if (left.attributes.BANDWIDTH) {
|
11290 | leftBandwidth = left.attributes.BANDWIDTH;
|
11291 | }
|
11292 | leftBandwidth = leftBandwidth || window$1.Number.MAX_VALUE;
|
11293 | if (right.attributes.BANDWIDTH) {
|
11294 | rightBandwidth = right.attributes.BANDWIDTH;
|
11295 | }
|
11296 | rightBandwidth = rightBandwidth || window$1.Number.MAX_VALUE;
|
11297 |
|
11298 | return leftBandwidth - rightBandwidth;
|
11299 | };
|
11300 |
|
11301 |
|
11302 |
|
11303 |
|
11304 |
|
11305 |
|
11306 |
|
11307 |
|
11308 |
|
11309 |
|
11310 | var comparePlaylistResolution = function comparePlaylistResolution(left, right) {
|
11311 | var leftWidth = void 0;
|
11312 | var rightWidth = void 0;
|
11313 |
|
11314 | if (left.attributes.RESOLUTION && left.attributes.RESOLUTION.width) {
|
11315 | leftWidth = left.attributes.RESOLUTION.width;
|
11316 | }
|
11317 |
|
11318 | leftWidth = leftWidth || window$1.Number.MAX_VALUE;
|
11319 |
|
11320 | if (right.attributes.RESOLUTION && right.attributes.RESOLUTION.width) {
|
11321 | rightWidth = right.attributes.RESOLUTION.width;
|
11322 | }
|
11323 |
|
11324 | rightWidth = rightWidth || window$1.Number.MAX_VALUE;
|
11325 |
|
11326 |
|
11327 |
|
11328 | if (leftWidth === rightWidth && left.attributes.BANDWIDTH && right.attributes.BANDWIDTH) {
|
11329 | return left.attributes.BANDWIDTH - right.attributes.BANDWIDTH;
|
11330 | }
|
11331 | return leftWidth - rightWidth;
|
11332 | };
|
11333 |
|
11334 |
|
11335 |
|
11336 |
|
11337 |
|
11338 |
|
11339 |
|
11340 |
|
11341 |
|
11342 |
|
11343 |
|
11344 |
|
11345 |
|
11346 |
|
11347 |
|
11348 |
|
11349 |
|
11350 |
|
11351 | var simpleSelector = function simpleSelector(master, playerBandwidth, playerWidth, playerHeight, limitRenditionByPlayerDimensions) {
|
11352 |
|
11353 | var sortedPlaylistReps = master.playlists.map(function (playlist) {
|
11354 | var width = void 0;
|
11355 | var height = void 0;
|
11356 | var bandwidth = void 0;
|
11357 |
|
11358 | width = playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.width;
|
11359 | height = playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height;
|
11360 | bandwidth = playlist.attributes.BANDWIDTH;
|
11361 |
|
11362 | bandwidth = bandwidth || window$1.Number.MAX_VALUE;
|
11363 |
|
11364 | return {
|
11365 | bandwidth: bandwidth,
|
11366 | width: width,
|
11367 | height: height,
|
11368 | playlist: playlist
|
11369 | };
|
11370 | });
|
11371 |
|
11372 | stableSort(sortedPlaylistReps, function (left, right) {
|
11373 | return left.bandwidth - right.bandwidth;
|
11374 | });
|
11375 |
|
11376 |
|
11377 |
|
11378 | sortedPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
11379 | return !Playlist.isIncompatible(rep.playlist);
|
11380 | });
|
11381 |
|
11382 |
|
11383 |
|
11384 | var enabledPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
11385 | return Playlist.isEnabled(rep.playlist);
|
11386 | });
|
11387 |
|
11388 | if (!enabledPlaylistReps.length) {
|
11389 |
|
11390 |
|
11391 |
|
11392 | enabledPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
11393 | return !Playlist.isDisabled(rep.playlist);
|
11394 | });
|
11395 | }
|
11396 |
|
11397 |
|
11398 |
|
11399 | var bandwidthPlaylistReps = enabledPlaylistReps.filter(function (rep) {
|
11400 | return rep.bandwidth * Config.BANDWIDTH_VARIANCE < playerBandwidth;
|
11401 | });
|
11402 |
|
11403 | var highestRemainingBandwidthRep = bandwidthPlaylistReps[bandwidthPlaylistReps.length - 1];
|
11404 |
|
11405 |
|
11406 |
|
11407 | var bandwidthBestRep = bandwidthPlaylistReps.filter(function (rep) {
|
11408 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
11409 | })[0];
|
11410 |
|
11411 |
|
11412 | if (limitRenditionByPlayerDimensions === false) {
|
11413 | var _chosenRep = bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
|
11414 |
|
11415 | return _chosenRep ? _chosenRep.playlist : null;
|
11416 | }
|
11417 |
|
11418 |
|
11419 | var haveResolution = bandwidthPlaylistReps.filter(function (rep) {
|
11420 | return rep.width && rep.height;
|
11421 | });
|
11422 |
|
11423 |
|
11424 | stableSort(haveResolution, function (left, right) {
|
11425 | return left.width - right.width;
|
11426 | });
|
11427 |
|
11428 |
|
11429 | var resolutionBestRepList = haveResolution.filter(function (rep) {
|
11430 | return rep.width === playerWidth && rep.height === playerHeight;
|
11431 | });
|
11432 |
|
11433 | highestRemainingBandwidthRep = resolutionBestRepList[resolutionBestRepList.length - 1];
|
11434 |
|
11435 | var resolutionBestRep = resolutionBestRepList.filter(function (rep) {
|
11436 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
11437 | })[0];
|
11438 |
|
11439 | var resolutionPlusOneList = void 0;
|
11440 | var resolutionPlusOneSmallest = void 0;
|
11441 | var resolutionPlusOneRep = void 0;
|
11442 |
|
11443 |
|
11444 |
|
11445 | if (!resolutionBestRep) {
|
11446 | resolutionPlusOneList = haveResolution.filter(function (rep) {
|
11447 | return rep.width > playerWidth || rep.height > playerHeight;
|
11448 | });
|
11449 |
|
11450 |
|
11451 | resolutionPlusOneSmallest = resolutionPlusOneList.filter(function (rep) {
|
11452 | return rep.width === resolutionPlusOneList[0].width && rep.height === resolutionPlusOneList[0].height;
|
11453 | });
|
11454 |
|
11455 |
|
11456 |
|
11457 | highestRemainingBandwidthRep = resolutionPlusOneSmallest[resolutionPlusOneSmallest.length - 1];
|
11458 | resolutionPlusOneRep = resolutionPlusOneSmallest.filter(function (rep) {
|
11459 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
11460 | })[0];
|
11461 | }
|
11462 |
|
11463 |
|
11464 | var chosenRep = resolutionPlusOneRep || resolutionBestRep || bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
|
11465 |
|
11466 | return chosenRep ? chosenRep.playlist : null;
|
11467 | };
|
11468 |
|
11469 |
|
11470 |
|
11471 |
|
11472 |
|
11473 |
|
11474 |
|
11475 |
|
11476 |
|
11477 |
|
11478 |
|
11479 |
|
11480 |
|
11481 | var lastBandwidthSelector = function lastBandwidthSelector() {
|
11482 | var pixelRatio = this.useDevicePixelRatio ? window$1.devicePixelRatio || 1 : 1;
|
11483 |
|
11484 | return simpleSelector(this.playlists.master, this.systemBandwidth, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions);
|
11485 | };
|
11486 |
|
11487 |
|
11488 |
|
11489 |
|
11490 |
|
11491 |
|
11492 |
|
11493 |
|
11494 |
|
11495 |
|
11496 |
|
11497 |
|
11498 |
|
11499 |
|
11500 |
|
11501 |
|
11502 |
|
11503 |
|
11504 |
|
11505 |
|
11506 |
|
11507 |
|
11508 |
|
11509 |
|
11510 |
|
11511 |
|
11512 |
|
11513 |
|
11514 |
|
11515 | var minRebufferMaxBandwidthSelector = function minRebufferMaxBandwidthSelector(settings) {
|
11516 | var master = settings.master,
|
11517 | currentTime = settings.currentTime,
|
11518 | bandwidth = settings.bandwidth,
|
11519 | duration$$1 = settings.duration,
|
11520 | segmentDuration = settings.segmentDuration,
|
11521 | timeUntilRebuffer = settings.timeUntilRebuffer,
|
11522 | currentTimeline = settings.currentTimeline,
|
11523 | syncController = settings.syncController;
|
11524 |
|
11525 |
|
11526 |
|
11527 |
|
11528 | var compatiblePlaylists = master.playlists.filter(function (playlist) {
|
11529 | return !Playlist.isIncompatible(playlist);
|
11530 | });
|
11531 |
|
11532 |
|
11533 |
|
11534 | var enabledPlaylists = compatiblePlaylists.filter(Playlist.isEnabled);
|
11535 |
|
11536 | if (!enabledPlaylists.length) {
|
11537 |
|
11538 |
|
11539 |
|
11540 | enabledPlaylists = compatiblePlaylists.filter(function (playlist) {
|
11541 | return !Playlist.isDisabled(playlist);
|
11542 | });
|
11543 | }
|
11544 |
|
11545 | var bandwidthPlaylists = enabledPlaylists.filter(Playlist.hasAttribute.bind(null, 'BANDWIDTH'));
|
11546 |
|
11547 | var rebufferingEstimates = bandwidthPlaylists.map(function (playlist) {
|
11548 | var syncPoint = syncController.getSyncPoint(playlist, duration$$1, currentTimeline, currentTime);
|
11549 |
|
11550 |
|
11551 | var numRequests = syncPoint ? 1 : 2;
|
11552 | var requestTimeEstimate = Playlist.estimateSegmentRequestTime(segmentDuration, bandwidth, playlist);
|
11553 | var rebufferingImpact = requestTimeEstimate * numRequests - timeUntilRebuffer;
|
11554 |
|
11555 | return {
|
11556 | playlist: playlist,
|
11557 | rebufferingImpact: rebufferingImpact
|
11558 | };
|
11559 | });
|
11560 |
|
11561 | var noRebufferingPlaylists = rebufferingEstimates.filter(function (estimate) {
|
11562 | return estimate.rebufferingImpact <= 0;
|
11563 | });
|
11564 |
|
11565 |
|
11566 | stableSort(noRebufferingPlaylists, function (a, b) {
|
11567 | return comparePlaylistBandwidth(b.playlist, a.playlist);
|
11568 | });
|
11569 |
|
11570 | if (noRebufferingPlaylists.length) {
|
11571 | return noRebufferingPlaylists[0];
|
11572 | }
|
11573 |
|
11574 | stableSort(rebufferingEstimates, function (a, b) {
|
11575 | return a.rebufferingImpact - b.rebufferingImpact;
|
11576 | });
|
11577 |
|
11578 | return rebufferingEstimates[0] || null;
|
11579 | };
|
11580 |
|
11581 |
|
11582 |
|
11583 |
|
11584 |
|
11585 |
|
11586 |
|
11587 |
|
11588 |
|
11589 |
|
11590 |
|
11591 |
|
11592 | var lowestBitrateCompatibleVariantSelector = function lowestBitrateCompatibleVariantSelector() {
|
11593 |
|
11594 |
|
11595 | var playlists = this.playlists.master.playlists.filter(Playlist.isEnabled);
|
11596 |
|
11597 |
|
11598 | stableSort(playlists, function (a, b) {
|
11599 | return comparePlaylistBandwidth(a, b);
|
11600 | });
|
11601 |
|
11602 |
|
11603 |
|
11604 |
|
11605 |
|
11606 |
|
11607 | var playlistsWithVideo = playlists.filter(function (playlist) {
|
11608 | return parseCodecs(playlist.attributes.CODECS).videoCodec;
|
11609 | });
|
11610 |
|
11611 | return playlistsWithVideo[0] || null;
|
11612 | };
|
11613 |
|
11614 |
|
11615 |
|
11616 |
|
11617 |
|
11618 |
|
11619 |
|
11620 |
|
11621 |
|
11622 | var createCaptionsTrackIfNotExists = function createCaptionsTrackIfNotExists(inbandTextTracks, tech, captionStreams) {
|
11623 | for (var trackId in captionStreams) {
|
11624 | if (!inbandTextTracks[trackId]) {
|
11625 | tech.trigger({ type: 'usage', name: 'hls-608' });
|
11626 | var track = tech.textTracks().getTrackById(trackId);
|
11627 |
|
11628 | if (track) {
|
11629 |
|
11630 |
|
11631 |
|
11632 | inbandTextTracks[trackId] = track;
|
11633 | } else {
|
11634 |
|
11635 |
|
11636 | inbandTextTracks[trackId] = tech.addRemoteTextTrack({
|
11637 | kind: 'captions',
|
11638 | id: trackId,
|
11639 | label: trackId
|
11640 | }, false).track;
|
11641 | }
|
11642 | }
|
11643 | }
|
11644 | };
|
11645 |
|
11646 | var addCaptionData = function addCaptionData(_ref) {
|
11647 | var inbandTextTracks = _ref.inbandTextTracks,
|
11648 | captionArray = _ref.captionArray,
|
11649 | timestampOffset = _ref.timestampOffset;
|
11650 |
|
11651 | if (!captionArray) {
|
11652 | return;
|
11653 | }
|
11654 |
|
11655 | var Cue = window.WebKitDataCue || window.VTTCue;
|
11656 |
|
11657 | captionArray.forEach(function (caption) {
|
11658 | var track = caption.stream;
|
11659 | var startTime = caption.startTime;
|
11660 | var endTime = caption.endTime;
|
11661 |
|
11662 | if (!inbandTextTracks[track]) {
|
11663 | return;
|
11664 | }
|
11665 |
|
11666 | startTime += timestampOffset;
|
11667 | endTime += timestampOffset;
|
11668 |
|
11669 | inbandTextTracks[track].addCue(new Cue(startTime, endTime, caption.text));
|
11670 | });
|
11671 | };
|
11672 |
|
11673 |
|
11674 |
|
11675 |
|
11676 |
|
11677 |
|
11678 | var CHECK_BUFFER_DELAY = 500;
|
11679 |
|
11680 |
|
11681 |
|
11682 |
|
11683 |
|
11684 |
|
11685 |
|
11686 |
|
11687 |
|
11688 |
|
11689 |
|
11690 | var detectEndOfStream = function detectEndOfStream(playlist, mediaSource, segmentIndex) {
|
11691 | if (!playlist || !mediaSource) {
|
11692 | return false;
|
11693 | }
|
11694 |
|
11695 | var segments = playlist.segments;
|
11696 |
|
11697 |
|
11698 |
|
11699 | var appendedLastSegment = segmentIndex === segments.length;
|
11700 |
|
11701 |
|
11702 |
|
11703 |
|
11704 | return playlist.endList && mediaSource.readyState === 'open' && appendedLastSegment;
|
11705 | };
|
11706 |
|
11707 | var finite = function finite(num) {
|
11708 | return typeof num === 'number' && isFinite(num);
|
11709 | };
|
11710 |
|
11711 | var illegalMediaSwitch = function illegalMediaSwitch(loaderType, startingMedia, newSegmentMedia) {
|
11712 |
|
11713 |
|
11714 | if (loaderType !== 'main' || !startingMedia || !newSegmentMedia) {
|
11715 | return null;
|
11716 | }
|
11717 |
|
11718 | if (!newSegmentMedia.containsAudio && !newSegmentMedia.containsVideo) {
|
11719 | return 'Neither audio nor video found in segment.';
|
11720 | }
|
11721 |
|
11722 | if (startingMedia.containsVideo && !newSegmentMedia.containsVideo) {
|
11723 | 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.';
|
11724 | }
|
11725 |
|
11726 | if (!startingMedia.containsVideo && newSegmentMedia.containsVideo) {
|
11727 | 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.';
|
11728 | }
|
11729 |
|
11730 | return null;
|
11731 | };
|
11732 |
|
11733 |
|
11734 |
|
11735 |
|
11736 |
|
11737 |
|
11738 |
|
11739 |
|
11740 |
|
11741 |
|
11742 |
|
11743 |
|
11744 |
|
11745 |
|
11746 | var safeBackBufferTrimTime = function safeBackBufferTrimTime(seekable$$1, currentTime, targetDuration) {
|
11747 |
|
11748 |
|
11749 |
|
11750 |
|
11751 |
|
11752 | var trimTime = currentTime - 30;
|
11753 |
|
11754 | if (seekable$$1.length) {
|
11755 |
|
11756 |
|
11757 | trimTime = Math.max(trimTime, seekable$$1.start(0));
|
11758 | }
|
11759 |
|
11760 |
|
11761 |
|
11762 | var maxTrimTime = currentTime - targetDuration;
|
11763 |
|
11764 | return Math.min(maxTrimTime, trimTime);
|
11765 | };
|
11766 |
|
11767 | var segmentInfoString = function segmentInfoString(segmentInfo) {
|
11768 | var _segmentInfo$segment = segmentInfo.segment,
|
11769 | start = _segmentInfo$segment.start,
|
11770 | end = _segmentInfo$segment.end,
|
11771 | _segmentInfo$playlist = segmentInfo.playlist,
|
11772 | seq = _segmentInfo$playlist.mediaSequence,
|
11773 | id = _segmentInfo$playlist.id,
|
11774 | _segmentInfo$playlist2 = _segmentInfo$playlist.segments,
|
11775 | segments = _segmentInfo$playlist2 === undefined ? [] : _segmentInfo$playlist2,
|
11776 | index = segmentInfo.mediaIndex,
|
11777 | timeline = segmentInfo.timeline;
|
11778 |
|
11779 |
|
11780 | return ['appending [' + index + '] of [' + seq + ', ' + (seq + segments.length) + '] from playlist [' + id + ']', '[' + start + ' => ' + end + '] in timeline [' + timeline + ']'].join(' ');
|
11781 | };
|
11782 |
|
11783 |
|
11784 |
|
11785 |
|
11786 |
|
11787 |
|
11788 |
|
11789 |
|
11790 |
|
11791 | var SegmentLoader = function (_videojs$EventTarget) {
|
11792 | inherits(SegmentLoader, _videojs$EventTarget);
|
11793 |
|
11794 | function SegmentLoader(settings) {
|
11795 | classCallCheck(this, SegmentLoader);
|
11796 |
|
11797 |
|
11798 | var _this = possibleConstructorReturn(this, (SegmentLoader.__proto__ || Object.getPrototypeOf(SegmentLoader)).call(this));
|
11799 |
|
11800 | if (!settings) {
|
11801 | throw new TypeError('Initialization settings are required');
|
11802 | }
|
11803 | if (typeof settings.currentTime !== 'function') {
|
11804 | throw new TypeError('No currentTime getter specified');
|
11805 | }
|
11806 | if (!settings.mediaSource) {
|
11807 | throw new TypeError('No MediaSource specified');
|
11808 | }
|
11809 |
|
11810 | _this.bandwidth = settings.bandwidth;
|
11811 | _this.throughput = { rate: 0, count: 0 };
|
11812 | _this.roundTrip = NaN;
|
11813 | _this.resetStats_();
|
11814 | _this.mediaIndex = null;
|
11815 |
|
11816 |
|
11817 | _this.hasPlayed_ = settings.hasPlayed;
|
11818 | _this.currentTime_ = settings.currentTime;
|
11819 | _this.seekable_ = settings.seekable;
|
11820 | _this.seeking_ = settings.seeking;
|
11821 | _this.duration_ = settings.duration;
|
11822 | _this.mediaSource_ = settings.mediaSource;
|
11823 | _this.hls_ = settings.hls;
|
11824 | _this.loaderType_ = settings.loaderType;
|
11825 | _this.startingMedia_ = void 0;
|
11826 | _this.segmentMetadataTrack_ = settings.segmentMetadataTrack;
|
11827 | _this.goalBufferLength_ = settings.goalBufferLength;
|
11828 | _this.sourceType_ = settings.sourceType;
|
11829 | _this.inbandTextTracks_ = settings.inbandTextTracks;
|
11830 | _this.state_ = 'INIT';
|
11831 |
|
11832 |
|
11833 | _this.checkBufferTimeout_ = null;
|
11834 | _this.error_ = void 0;
|
11835 | _this.currentTimeline_ = -1;
|
11836 | _this.pendingSegment_ = null;
|
11837 | _this.mimeType_ = null;
|
11838 | _this.sourceUpdater_ = null;
|
11839 | _this.xhrOptions_ = null;
|
11840 |
|
11841 |
|
11842 | _this.activeInitSegmentId_ = null;
|
11843 | _this.initSegments_ = {};
|
11844 |
|
11845 |
|
11846 | _this.cacheEncryptionKeys_ = settings.cacheEncryptionKeys;
|
11847 | _this.keyCache_ = {};
|
11848 |
|
11849 |
|
11850 | if (_this.loaderType_ === 'main') {
|
11851 | _this.captionParser_ = new CaptionParser();
|
11852 | } else {
|
11853 | _this.captionParser_ = null;
|
11854 | }
|
11855 |
|
11856 | _this.decrypter_ = settings.decrypter;
|
11857 |
|
11858 |
|
11859 |
|
11860 |
|
11861 | _this.syncController_ = settings.syncController;
|
11862 | _this.syncPoint_ = {
|
11863 | segmentIndex: 0,
|
11864 | time: 0
|
11865 | };
|
11866 |
|
11867 | _this.triggerSyncInfoUpdate_ = function () {
|
11868 | return _this.trigger('syncinfoupdate');
|
11869 | };
|
11870 | _this.syncController_.on('syncinfoupdate', _this.triggerSyncInfoUpdate_);
|
11871 |
|
11872 | _this.mediaSource_.addEventListener('sourceopen', function () {
|
11873 | return _this.ended_ = false;
|
11874 | });
|
11875 |
|
11876 |
|
11877 | _this.fetchAtBuffer_ = false;
|
11878 |
|
11879 | _this.logger_ = logger('SegmentLoader[' + _this.loaderType_ + ']');
|
11880 |
|
11881 | Object.defineProperty(_this, 'state', {
|
11882 | get: function get$$1() {
|
11883 | return this.state_;
|
11884 | },
|
11885 | set: function set$$1(newState) {
|
11886 | if (newState !== this.state_) {
|
11887 | this.logger_(this.state_ + ' -> ' + newState);
|
11888 | this.state_ = newState;
|
11889 | }
|
11890 | }
|
11891 | });
|
11892 | return _this;
|
11893 | }
|
11894 |
|
11895 | |
11896 |
|
11897 |
|
11898 |
|
11899 |
|
11900 |
|
11901 |
|
11902 | createClass(SegmentLoader, [{
|
11903 | key: 'resetStats_',
|
11904 | value: function resetStats_() {
|
11905 | this.mediaBytesTransferred = 0;
|
11906 | this.mediaRequests = 0;
|
11907 | this.mediaRequestsAborted = 0;
|
11908 | this.mediaRequestsTimedout = 0;
|
11909 | this.mediaRequestsErrored = 0;
|
11910 | this.mediaTransferDuration = 0;
|
11911 | this.mediaSecondsLoaded = 0;
|
11912 | }
|
11913 |
|
11914 | |
11915 |
|
11916 |
|
11917 |
|
11918 | }, {
|
11919 | key: 'dispose',
|
11920 | value: function dispose() {
|
11921 | this.trigger('dispose');
|
11922 | this.state = 'DISPOSED';
|
11923 | this.pause();
|
11924 | this.abort_();
|
11925 | if (this.sourceUpdater_) {
|
11926 | this.sourceUpdater_.dispose();
|
11927 | }
|
11928 | this.resetStats_();
|
11929 | if (this.captionParser_) {
|
11930 | this.captionParser_.reset();
|
11931 | }
|
11932 |
|
11933 | if (this.checkBufferTimeout_) {
|
11934 | window$1.clearTimeout(this.checkBufferTimeout_);
|
11935 | }
|
11936 |
|
11937 | if (this.syncController_ && this.triggerSyncInfoUpdate_) {
|
11938 | this.syncController_.off('syncinfoupdate', this.triggerSyncInfoUpdate_);
|
11939 | }
|
11940 |
|
11941 | this.off();
|
11942 | }
|
11943 |
|
11944 | |
11945 |
|
11946 |
|
11947 |
|
11948 |
|
11949 | }, {
|
11950 | key: 'abort',
|
11951 | value: function abort() {
|
11952 | if (this.state !== 'WAITING') {
|
11953 | if (this.pendingSegment_) {
|
11954 | this.pendingSegment_ = null;
|
11955 | }
|
11956 | return;
|
11957 | }
|
11958 |
|
11959 | this.abort_();
|
11960 |
|
11961 |
|
11962 |
|
11963 |
|
11964 |
|
11965 | this.state = 'READY';
|
11966 |
|
11967 |
|
11968 |
|
11969 | if (!this.paused()) {
|
11970 | this.monitorBuffer_();
|
11971 | }
|
11972 | }
|
11973 |
|
11974 | |
11975 |
|
11976 |
|
11977 |
|
11978 |
|
11979 |
|
11980 | }, {
|
11981 | key: 'abort_',
|
11982 | value: function abort_() {
|
11983 | if (this.pendingSegment_) {
|
11984 | this.pendingSegment_.abortRequests();
|
11985 | }
|
11986 |
|
11987 |
|
11988 | this.pendingSegment_ = null;
|
11989 | }
|
11990 |
|
11991 | |
11992 |
|
11993 |
|
11994 |
|
11995 |
|
11996 |
|
11997 |
|
11998 | }, {
|
11999 | key: 'error',
|
12000 | value: function error(_error) {
|
12001 | if (typeof _error !== 'undefined') {
|
12002 | this.error_ = _error;
|
12003 | }
|
12004 |
|
12005 | this.pendingSegment_ = null;
|
12006 | return this.error_;
|
12007 | }
|
12008 | }, {
|
12009 | key: 'endOfStream',
|
12010 | value: function endOfStream() {
|
12011 | this.ended_ = true;
|
12012 | this.pause();
|
12013 | this.trigger('ended');
|
12014 | }
|
12015 |
|
12016 | |
12017 |
|
12018 |
|
12019 |
|
12020 |
|
12021 |
|
12022 |
|
12023 | }, {
|
12024 | key: 'buffered_',
|
12025 | value: function buffered_() {
|
12026 | if (!this.sourceUpdater_) {
|
12027 | return videojs.createTimeRanges();
|
12028 | }
|
12029 |
|
12030 | return this.sourceUpdater_.buffered();
|
12031 | }
|
12032 |
|
12033 | |
12034 |
|
12035 |
|
12036 |
|
12037 |
|
12038 |
|
12039 |
|
12040 |
|
12041 |
|
12042 |
|
12043 |
|
12044 | }, {
|
12045 | key: 'initSegment',
|
12046 | value: function initSegment(map) {
|
12047 | var set$$1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
12048 |
|
12049 | if (!map) {
|
12050 | return null;
|
12051 | }
|
12052 |
|
12053 | var id = initSegmentId(map);
|
12054 | var storedMap = this.initSegments_[id];
|
12055 |
|
12056 | if (set$$1 && !storedMap && map.bytes) {
|
12057 | this.initSegments_[id] = storedMap = {
|
12058 | resolvedUri: map.resolvedUri,
|
12059 | byterange: map.byterange,
|
12060 | bytes: map.bytes,
|
12061 | timescales: map.timescales,
|
12062 | videoTrackIds: map.videoTrackIds
|
12063 | };
|
12064 | }
|
12065 |
|
12066 | return storedMap || map;
|
12067 | }
|
12068 |
|
12069 | |
12070 |
|
12071 |
|
12072 |
|
12073 |
|
12074 |
|
12075 |
|
12076 |
|
12077 |
|
12078 |
|
12079 |
|
12080 | }, {
|
12081 | key: 'segmentKey',
|
12082 | value: function segmentKey(key) {
|
12083 | var set$$1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
12084 |
|
12085 | if (!key) {
|
12086 | return null;
|
12087 | }
|
12088 |
|
12089 | var id = segmentKeyId(key);
|
12090 | var storedKey = this.keyCache_[id];
|
12091 |
|
12092 |
|
12093 |
|
12094 | if (this.cacheEncryptionKeys_ && set$$1 && !storedKey && key.bytes) {
|
12095 | this.keyCache_[id] = storedKey = {
|
12096 | resolvedUri: key.resolvedUri,
|
12097 | bytes: key.bytes
|
12098 | };
|
12099 | }
|
12100 |
|
12101 | var result = {
|
12102 | resolvedUri: (storedKey || key).resolvedUri
|
12103 | };
|
12104 |
|
12105 | if (storedKey) {
|
12106 | result.bytes = storedKey.bytes;
|
12107 | }
|
12108 |
|
12109 | return result;
|
12110 | }
|
12111 |
|
12112 | |
12113 |
|
12114 |
|
12115 |
|
12116 |
|
12117 |
|
12118 |
|
12119 | }, {
|
12120 | key: 'couldBeginLoading_',
|
12121 | value: function couldBeginLoading_() {
|
12122 | return this.playlist_ && (
|
12123 |
|
12124 |
|
12125 |
|
12126 | this.sourceUpdater_ || this.mimeType_ && this.state === 'INIT') && !this.paused();
|
12127 | }
|
12128 |
|
12129 | |
12130 |
|
12131 |
|
12132 |
|
12133 | }, {
|
12134 | key: 'load',
|
12135 | value: function load() {
|
12136 |
|
12137 | this.monitorBuffer_();
|
12138 |
|
12139 |
|
12140 |
|
12141 | if (!this.playlist_) {
|
12142 | return;
|
12143 | }
|
12144 |
|
12145 |
|
12146 | this.syncController_.setDateTimeMapping(this.playlist_);
|
12147 |
|
12148 |
|
12149 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
12150 | return this.init_();
|
12151 | }
|
12152 |
|
12153 |
|
12154 |
|
12155 | if (!this.couldBeginLoading_() || this.state !== 'READY' && this.state !== 'INIT') {
|
12156 | return;
|
12157 | }
|
12158 |
|
12159 | this.state = 'READY';
|
12160 | }
|
12161 |
|
12162 | |
12163 |
|
12164 |
|
12165 |
|
12166 |
|
12167 |
|
12168 |
|
12169 |
|
12170 | }, {
|
12171 | key: 'init_',
|
12172 | value: function init_() {
|
12173 | this.state = 'READY';
|
12174 | this.sourceUpdater_ = new SourceUpdater(this.mediaSource_, this.mimeType_, this.loaderType_, this.sourceBufferEmitter_);
|
12175 | this.resetEverything();
|
12176 | return this.monitorBuffer_();
|
12177 | }
|
12178 |
|
12179 | |
12180 |
|
12181 |
|
12182 |
|
12183 |
|
12184 |
|
12185 | }, {
|
12186 | key: 'playlist',
|
12187 | value: function playlist(newPlaylist) {
|
12188 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
12189 |
|
12190 | if (!newPlaylist) {
|
12191 | return;
|
12192 | }
|
12193 |
|
12194 | var oldPlaylist = this.playlist_;
|
12195 | var segmentInfo = this.pendingSegment_;
|
12196 |
|
12197 | this.playlist_ = newPlaylist;
|
12198 | this.xhrOptions_ = options;
|
12199 |
|
12200 |
|
12201 |
|
12202 |
|
12203 |
|
12204 |
|
12205 |
|
12206 |
|
12207 | if (this.state === 'INIT') {
|
12208 | newPlaylist.syncInfo = {
|
12209 | mediaSequence: newPlaylist.mediaSequence,
|
12210 | time: 0
|
12211 | };
|
12212 | }
|
12213 |
|
12214 | var oldId = null;
|
12215 |
|
12216 | if (oldPlaylist) {
|
12217 | if (oldPlaylist.id) {
|
12218 | oldId = oldPlaylist.id;
|
12219 | } else if (oldPlaylist.uri) {
|
12220 | oldId = oldPlaylist.uri;
|
12221 | }
|
12222 | }
|
12223 |
|
12224 | this.logger_('playlist update [' + oldId + ' => ' + (newPlaylist.id || newPlaylist.uri) + ']');
|
12225 |
|
12226 |
|
12227 |
|
12228 | this.trigger('syncinfoupdate');
|
12229 |
|
12230 |
|
12231 |
|
12232 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
12233 | return this.init_();
|
12234 | }
|
12235 |
|
12236 | if (!oldPlaylist || oldPlaylist.uri !== newPlaylist.uri) {
|
12237 | if (this.mediaIndex !== null) {
|
12238 |
|
12239 |
|
12240 | this.resyncLoader();
|
12241 | }
|
12242 |
|
12243 |
|
12244 | return;
|
12245 | }
|
12246 |
|
12247 |
|
12248 |
|
12249 | var mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
12250 |
|
12251 | this.logger_('live window shift [' + mediaSequenceDiff + ']');
|
12252 |
|
12253 |
|
12254 |
|
12255 |
|
12256 | if (this.mediaIndex !== null) {
|
12257 | this.mediaIndex -= mediaSequenceDiff;
|
12258 | }
|
12259 |
|
12260 |
|
12261 |
|
12262 |
|
12263 | if (segmentInfo) {
|
12264 | segmentInfo.mediaIndex -= mediaSequenceDiff;
|
12265 |
|
12266 |
|
12267 |
|
12268 |
|
12269 | if (segmentInfo.mediaIndex >= 0) {
|
12270 | segmentInfo.segment = newPlaylist.segments[segmentInfo.mediaIndex];
|
12271 | }
|
12272 | }
|
12273 |
|
12274 | this.syncController_.saveExpiredSegmentInfo(oldPlaylist, newPlaylist);
|
12275 | }
|
12276 |
|
12277 | |
12278 |
|
12279 |
|
12280 |
|
12281 |
|
12282 |
|
12283 |
|
12284 | }, {
|
12285 | key: 'pause',
|
12286 | value: function pause() {
|
12287 | if (this.checkBufferTimeout_) {
|
12288 | window$1.clearTimeout(this.checkBufferTimeout_);
|
12289 |
|
12290 | this.checkBufferTimeout_ = null;
|
12291 | }
|
12292 | }
|
12293 |
|
12294 | |
12295 |
|
12296 |
|
12297 |
|
12298 |
|
12299 |
|
12300 | }, {
|
12301 | key: 'paused',
|
12302 | value: function paused() {
|
12303 | return this.checkBufferTimeout_ === null;
|
12304 | }
|
12305 |
|
12306 | |
12307 |
|
12308 |
|
12309 |
|
12310 |
|
12311 |
|
12312 |
|
12313 |
|
12314 |
|
12315 | }, {
|
12316 | key: 'mimeType',
|
12317 | value: function mimeType(_mimeType, sourceBufferEmitter) {
|
12318 | if (this.mimeType_) {
|
12319 | return;
|
12320 | }
|
12321 |
|
12322 | this.mimeType_ = _mimeType;
|
12323 | this.sourceBufferEmitter_ = sourceBufferEmitter;
|
12324 |
|
12325 |
|
12326 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
12327 | this.init_();
|
12328 | }
|
12329 | }
|
12330 |
|
12331 | |
12332 |
|
12333 |
|
12334 |
|
12335 |
|
12336 |
|
12337 | }, {
|
12338 | key: 'resetEverything',
|
12339 | value: function resetEverything(done) {
|
12340 | this.ended_ = false;
|
12341 | this.resetLoader();
|
12342 |
|
12343 |
|
12344 |
|
12345 |
|
12346 | this.remove(0, Infinity, done);
|
12347 |
|
12348 |
|
12349 | if (this.captionParser_) {
|
12350 | this.captionParser_.clearAllCaptions();
|
12351 | }
|
12352 | this.trigger('reseteverything');
|
12353 | }
|
12354 |
|
12355 | |
12356 |
|
12357 |
|
12358 |
|
12359 |
|
12360 |
|
12361 |
|
12362 | }, {
|
12363 | key: 'resetLoader',
|
12364 | value: function resetLoader() {
|
12365 | this.fetchAtBuffer_ = false;
|
12366 | this.resyncLoader();
|
12367 | }
|
12368 |
|
12369 | |
12370 |
|
12371 |
|
12372 |
|
12373 |
|
12374 | }, {
|
12375 | key: 'resyncLoader',
|
12376 | value: function resyncLoader() {
|
12377 | this.mediaIndex = null;
|
12378 | this.syncPoint_ = null;
|
12379 | this.abort();
|
12380 | }
|
12381 |
|
12382 | |
12383 |
|
12384 |
|
12385 |
|
12386 |
|
12387 |
|
12388 |
|
12389 |
|
12390 | }, {
|
12391 | key: 'remove',
|
12392 | value: function remove(start, end, done) {
|
12393 |
|
12394 |
|
12395 |
|
12396 | if (end === Infinity) {
|
12397 | end = this.duration_();
|
12398 | }
|
12399 |
|
12400 | if (this.sourceUpdater_) {
|
12401 | this.sourceUpdater_.remove(start, end, done);
|
12402 | }
|
12403 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
12404 |
|
12405 | if (this.inbandTextTracks_) {
|
12406 | for (var id in this.inbandTextTracks_) {
|
12407 | removeCuesFromTrack(start, end, this.inbandTextTracks_[id]);
|
12408 | }
|
12409 | }
|
12410 | }
|
12411 |
|
12412 | |
12413 |
|
12414 |
|
12415 |
|
12416 |
|
12417 |
|
12418 | }, {
|
12419 | key: 'monitorBuffer_',
|
12420 | value: function monitorBuffer_() {
|
12421 | if (this.checkBufferTimeout_) {
|
12422 | window$1.clearTimeout(this.checkBufferTimeout_);
|
12423 | }
|
12424 |
|
12425 | this.checkBufferTimeout_ = window$1.setTimeout(this.monitorBufferTick_.bind(this), 1);
|
12426 | }
|
12427 |
|
12428 | |
12429 |
|
12430 |
|
12431 |
|
12432 |
|
12433 |
|
12434 |
|
12435 | }, {
|
12436 | key: 'monitorBufferTick_',
|
12437 | value: function monitorBufferTick_() {
|
12438 | if (this.state === 'READY') {
|
12439 | this.fillBuffer_();
|
12440 | }
|
12441 |
|
12442 | if (this.checkBufferTimeout_) {
|
12443 | window$1.clearTimeout(this.checkBufferTimeout_);
|
12444 | }
|
12445 |
|
12446 | this.checkBufferTimeout_ = window$1.setTimeout(this.monitorBufferTick_.bind(this), CHECK_BUFFER_DELAY);
|
12447 | }
|
12448 |
|
12449 | |
12450 |
|
12451 |
|
12452 |
|
12453 |
|
12454 |
|
12455 |
|
12456 |
|
12457 |
|
12458 |
|
12459 | }, {
|
12460 | key: 'fillBuffer_',
|
12461 | value: function fillBuffer_() {
|
12462 | if (this.sourceUpdater_.updating()) {
|
12463 | return;
|
12464 | }
|
12465 |
|
12466 | if (!this.syncPoint_) {
|
12467 | this.syncPoint_ = this.syncController_.getSyncPoint(this.playlist_, this.duration_(), this.currentTimeline_, this.currentTime_());
|
12468 | }
|
12469 |
|
12470 |
|
12471 | var segmentInfo = this.checkBuffer_(this.buffered_(), this.playlist_, this.mediaIndex, this.hasPlayed_(), this.currentTime_(), this.syncPoint_);
|
12472 |
|
12473 | if (!segmentInfo) {
|
12474 | return;
|
12475 | }
|
12476 |
|
12477 | if (this.isEndOfStream_(segmentInfo.mediaIndex)) {
|
12478 | this.endOfStream();
|
12479 | return;
|
12480 | }
|
12481 |
|
12482 | if (segmentInfo.mediaIndex === this.playlist_.segments.length - 1 && this.mediaSource_.readyState === 'ended' && !this.seeking_()) {
|
12483 | return;
|
12484 | }
|
12485 |
|
12486 |
|
12487 |
|
12488 |
|
12489 |
|
12490 |
|
12491 |
|
12492 |
|
12493 |
|
12494 |
|
12495 |
|
12496 | if (segmentInfo.timeline !== this.currentTimeline_) {
|
12497 | this.syncController_.reset();
|
12498 | segmentInfo.timestampOffset = segmentInfo.startOfSegment;
|
12499 | if (this.captionParser_) {
|
12500 | this.captionParser_.clearAllCaptions();
|
12501 | }
|
12502 | }
|
12503 |
|
12504 | this.loadSegment_(segmentInfo);
|
12505 | }
|
12506 |
|
12507 | |
12508 |
|
12509 |
|
12510 |
|
12511 |
|
12512 |
|
12513 |
|
12514 |
|
12515 | }, {
|
12516 | key: 'isEndOfStream_',
|
12517 | value: function isEndOfStream_(mediaIndex) {
|
12518 | var playlist = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.playlist_;
|
12519 |
|
12520 | return detectEndOfStream(playlist, this.mediaSource_, mediaIndex) && !this.sourceUpdater_.updating();
|
12521 | }
|
12522 |
|
12523 | |
12524 |
|
12525 |
|
12526 |
|
12527 |
|
12528 |
|
12529 |
|
12530 |
|
12531 |
|
12532 |
|
12533 |
|
12534 |
|
12535 |
|
12536 | }, {
|
12537 | key: 'checkBuffer_',
|
12538 | value: function checkBuffer_(buffered, playlist, mediaIndex, hasPlayed, currentTime, syncPoint) {
|
12539 | var lastBufferedEnd = 0;
|
12540 | var startOfSegment = void 0;
|
12541 |
|
12542 | if (buffered.length) {
|
12543 | lastBufferedEnd = buffered.end(buffered.length - 1);
|
12544 | }
|
12545 |
|
12546 | var bufferedTime = Math.max(0, lastBufferedEnd - currentTime);
|
12547 |
|
12548 | if (!playlist.segments.length) {
|
12549 | return null;
|
12550 | }
|
12551 |
|
12552 |
|
12553 |
|
12554 | if (bufferedTime >= this.goalBufferLength_()) {
|
12555 | return null;
|
12556 | }
|
12557 |
|
12558 |
|
12559 |
|
12560 | if (!hasPlayed && bufferedTime >= 1) {
|
12561 | return null;
|
12562 | }
|
12563 |
|
12564 |
|
12565 |
|
12566 |
|
12567 |
|
12568 | if (syncPoint === null) {
|
12569 | mediaIndex = this.getSyncSegmentCandidate_(playlist);
|
12570 | return this.generateSegmentInfo_(playlist, mediaIndex, null, true);
|
12571 | }
|
12572 |
|
12573 |
|
12574 | if (mediaIndex !== null) {
|
12575 | var segment = playlist.segments[mediaIndex];
|
12576 |
|
12577 | startOfSegment = lastBufferedEnd;
|
12578 |
|
12579 | return this.generateSegmentInfo_(playlist, mediaIndex + 1, startOfSegment, false);
|
12580 | }
|
12581 |
|
12582 |
|
12583 |
|
12584 |
|
12585 | if (this.fetchAtBuffer_) {
|
12586 |
|
12587 | var mediaSourceInfo = Playlist.getMediaInfoForTime(playlist, lastBufferedEnd, syncPoint.segmentIndex, syncPoint.time);
|
12588 |
|
12589 | mediaIndex = mediaSourceInfo.mediaIndex;
|
12590 | startOfSegment = mediaSourceInfo.startTime;
|
12591 | } else {
|
12592 |
|
12593 | var _mediaSourceInfo = Playlist.getMediaInfoForTime(playlist, currentTime, syncPoint.segmentIndex, syncPoint.time);
|
12594 |
|
12595 | mediaIndex = _mediaSourceInfo.mediaIndex;
|
12596 | startOfSegment = _mediaSourceInfo.startTime;
|
12597 | }
|
12598 |
|
12599 | return this.generateSegmentInfo_(playlist, mediaIndex, startOfSegment, false);
|
12600 | }
|
12601 |
|
12602 | |
12603 |
|
12604 |
|
12605 |
|
12606 |
|
12607 |
|
12608 |
|
12609 |
|
12610 |
|
12611 |
|
12612 | }, {
|
12613 | key: 'getSyncSegmentCandidate_',
|
12614 | value: function getSyncSegmentCandidate_(playlist) {
|
12615 | var _this2 = this;
|
12616 |
|
12617 | if (this.currentTimeline_ === -1) {
|
12618 | return 0;
|
12619 | }
|
12620 |
|
12621 | var segmentIndexArray = playlist.segments.map(function (s, i) {
|
12622 | return {
|
12623 | timeline: s.timeline,
|
12624 | segmentIndex: i
|
12625 | };
|
12626 | }).filter(function (s) {
|
12627 | return s.timeline === _this2.currentTimeline_;
|
12628 | });
|
12629 |
|
12630 | if (segmentIndexArray.length) {
|
12631 | return segmentIndexArray[Math.min(segmentIndexArray.length - 1, 1)].segmentIndex;
|
12632 | }
|
12633 |
|
12634 | return Math.max(playlist.segments.length - 1, 0);
|
12635 | }
|
12636 | }, {
|
12637 | key: 'generateSegmentInfo_',
|
12638 | value: function generateSegmentInfo_(playlist, mediaIndex, startOfSegment, isSyncRequest) {
|
12639 | if (mediaIndex < 0 || mediaIndex >= playlist.segments.length) {
|
12640 | return null;
|
12641 | }
|
12642 |
|
12643 | var segment = playlist.segments[mediaIndex];
|
12644 |
|
12645 | return {
|
12646 | requestId: 'segment-loader-' + Math.random(),
|
12647 |
|
12648 | uri: segment.resolvedUri,
|
12649 |
|
12650 | mediaIndex: mediaIndex,
|
12651 |
|
12652 |
|
12653 | isSyncRequest: isSyncRequest,
|
12654 | startOfSegment: startOfSegment,
|
12655 |
|
12656 | playlist: playlist,
|
12657 |
|
12658 | bytes: null,
|
12659 |
|
12660 | encryptedBytes: null,
|
12661 |
|
12662 |
|
12663 | timestampOffset: null,
|
12664 |
|
12665 | timeline: segment.timeline,
|
12666 |
|
12667 | duration: segment.duration,
|
12668 |
|
12669 | segment: segment
|
12670 | };
|
12671 | }
|
12672 |
|
12673 | |
12674 |
|
12675 |
|
12676 |
|
12677 |
|
12678 |
|
12679 |
|
12680 |
|
12681 |
|
12682 |
|
12683 |
|
12684 | }, {
|
12685 | key: 'abortRequestEarly_',
|
12686 | value: function abortRequestEarly_(stats) {
|
12687 | if (this.hls_.tech_.paused() ||
|
12688 |
|
12689 |
|
12690 |
|
12691 | !this.xhrOptions_.timeout ||
|
12692 |
|
12693 | !this.playlist_.attributes.BANDWIDTH) {
|
12694 | return false;
|
12695 | }
|
12696 |
|
12697 |
|
12698 |
|
12699 |
|
12700 | if (Date.now() - (stats.firstBytesReceivedAt || Date.now()) < 1000) {
|
12701 | return false;
|
12702 | }
|
12703 |
|
12704 | var currentTime = this.currentTime_();
|
12705 | var measuredBandwidth = stats.bandwidth;
|
12706 | var segmentDuration = this.pendingSegment_.duration;
|
12707 |
|
12708 | var requestTimeRemaining = Playlist.estimateSegmentRequestTime(segmentDuration, measuredBandwidth, this.playlist_, stats.bytesReceived);
|
12709 |
|
12710 |
|
12711 |
|
12712 |
|
12713 | var timeUntilRebuffer$$1 = timeUntilRebuffer(this.buffered_(), currentTime, this.hls_.tech_.playbackRate()) - 1;
|
12714 |
|
12715 |
|
12716 |
|
12717 | if (requestTimeRemaining <= timeUntilRebuffer$$1) {
|
12718 | return false;
|
12719 | }
|
12720 |
|
12721 | var switchCandidate = minRebufferMaxBandwidthSelector({
|
12722 | master: this.hls_.playlists.master,
|
12723 | currentTime: currentTime,
|
12724 | bandwidth: measuredBandwidth,
|
12725 | duration: this.duration_(),
|
12726 | segmentDuration: segmentDuration,
|
12727 | timeUntilRebuffer: timeUntilRebuffer$$1,
|
12728 | currentTimeline: this.currentTimeline_,
|
12729 | syncController: this.syncController_
|
12730 | });
|
12731 |
|
12732 | if (!switchCandidate) {
|
12733 | return;
|
12734 | }
|
12735 |
|
12736 | var rebufferingImpact = requestTimeRemaining - timeUntilRebuffer$$1;
|
12737 |
|
12738 | var timeSavedBySwitching = rebufferingImpact - switchCandidate.rebufferingImpact;
|
12739 |
|
12740 | var minimumTimeSaving = 0.5;
|
12741 |
|
12742 |
|
12743 |
|
12744 |
|
12745 | if (timeUntilRebuffer$$1 <= TIME_FUDGE_FACTOR) {
|
12746 | minimumTimeSaving = 1;
|
12747 | }
|
12748 |
|
12749 | if (!switchCandidate.playlist || switchCandidate.playlist.uri === this.playlist_.uri || timeSavedBySwitching < minimumTimeSaving) {
|
12750 | return false;
|
12751 | }
|
12752 |
|
12753 |
|
12754 |
|
12755 |
|
12756 | this.bandwidth = switchCandidate.playlist.attributes.BANDWIDTH * Config.BANDWIDTH_VARIANCE + 1;
|
12757 | this.abort();
|
12758 | this.trigger('earlyabort');
|
12759 | return true;
|
12760 | }
|
12761 |
|
12762 | |
12763 |
|
12764 |
|
12765 |
|
12766 |
|
12767 |
|
12768 |
|
12769 |
|
12770 |
|
12771 |
|
12772 | }, {
|
12773 | key: 'handleProgress_',
|
12774 | value: function handleProgress_(event, simpleSegment) {
|
12775 | if (!this.pendingSegment_ || simpleSegment.requestId !== this.pendingSegment_.requestId || this.abortRequestEarly_(simpleSegment.stats)) {
|
12776 | return;
|
12777 | }
|
12778 |
|
12779 | this.trigger('progress');
|
12780 | }
|
12781 |
|
12782 | |
12783 |
|
12784 |
|
12785 |
|
12786 |
|
12787 |
|
12788 | }, {
|
12789 | key: 'loadSegment_',
|
12790 | value: function loadSegment_(segmentInfo) {
|
12791 | this.state = 'WAITING';
|
12792 | this.pendingSegment_ = segmentInfo;
|
12793 | this.trimBackBuffer_(segmentInfo);
|
12794 |
|
12795 | segmentInfo.abortRequests = mediaSegmentRequest(this.hls_.xhr, this.xhrOptions_, this.decrypter_, this.captionParser_, this.createSimplifiedSegmentObj_(segmentInfo),
|
12796 |
|
12797 | this.handleProgress_.bind(this), this.segmentRequestFinished_.bind(this));
|
12798 | }
|
12799 |
|
12800 | |
12801 |
|
12802 |
|
12803 |
|
12804 |
|
12805 |
|
12806 |
|
12807 |
|
12808 |
|
12809 | }, {
|
12810 | key: 'trimBackBuffer_',
|
12811 | value: function trimBackBuffer_(segmentInfo) {
|
12812 | var removeToTime = safeBackBufferTrimTime(this.seekable_(), this.currentTime_(), this.playlist_.targetDuration || 10);
|
12813 |
|
12814 |
|
12815 |
|
12816 |
|
12817 |
|
12818 |
|
12819 |
|
12820 | if (removeToTime > 0) {
|
12821 | this.remove(0, removeToTime);
|
12822 | }
|
12823 | }
|
12824 |
|
12825 | |
12826 |
|
12827 |
|
12828 |
|
12829 |
|
12830 |
|
12831 |
|
12832 |
|
12833 |
|
12834 |
|
12835 | }, {
|
12836 | key: 'createSimplifiedSegmentObj_',
|
12837 | value: function createSimplifiedSegmentObj_(segmentInfo) {
|
12838 | var segment = segmentInfo.segment;
|
12839 | var simpleSegment = {
|
12840 | resolvedUri: segment.resolvedUri,
|
12841 | byterange: segment.byterange,
|
12842 | requestId: segmentInfo.requestId
|
12843 | };
|
12844 |
|
12845 | if (segment.key) {
|
12846 |
|
12847 |
|
12848 | var iv = segment.key.iv || new Uint32Array([0, 0, 0, segmentInfo.mediaIndex + segmentInfo.playlist.mediaSequence]);
|
12849 |
|
12850 | simpleSegment.key = this.segmentKey(segment.key);
|
12851 | simpleSegment.key.iv = iv;
|
12852 | }
|
12853 |
|
12854 | if (segment.map) {
|
12855 | simpleSegment.map = this.initSegment(segment.map);
|
12856 | }
|
12857 |
|
12858 | return simpleSegment;
|
12859 | }
|
12860 |
|
12861 | |
12862 |
|
12863 |
|
12864 |
|
12865 |
|
12866 |
|
12867 |
|
12868 | }, {
|
12869 | key: 'segmentRequestFinished_',
|
12870 | value: function segmentRequestFinished_(error, simpleSegment) {
|
12871 |
|
12872 |
|
12873 | this.mediaRequests += 1;
|
12874 |
|
12875 | if (simpleSegment.stats) {
|
12876 | this.mediaBytesTransferred += simpleSegment.stats.bytesReceived;
|
12877 | this.mediaTransferDuration += simpleSegment.stats.roundTripTime;
|
12878 | }
|
12879 |
|
12880 |
|
12881 | if (!this.pendingSegment_) {
|
12882 | this.mediaRequestsAborted += 1;
|
12883 | return;
|
12884 | }
|
12885 |
|
12886 |
|
12887 |
|
12888 |
|
12889 |
|
12890 | if (simpleSegment.requestId !== this.pendingSegment_.requestId) {
|
12891 | return;
|
12892 | }
|
12893 |
|
12894 |
|
12895 | if (error) {
|
12896 | this.pendingSegment_ = null;
|
12897 | this.state = 'READY';
|
12898 |
|
12899 |
|
12900 |
|
12901 |
|
12902 | if (error.code === REQUEST_ERRORS.ABORTED) {
|
12903 | this.mediaRequestsAborted += 1;
|
12904 | return;
|
12905 | }
|
12906 |
|
12907 | this.pause();
|
12908 |
|
12909 |
|
12910 |
|
12911 |
|
12912 | if (error.code === REQUEST_ERRORS.TIMEOUT) {
|
12913 | this.mediaRequestsTimedout += 1;
|
12914 | this.bandwidth = 1;
|
12915 | this.roundTrip = NaN;
|
12916 | this.trigger('bandwidthupdate');
|
12917 | return;
|
12918 | }
|
12919 |
|
12920 |
|
12921 |
|
12922 | this.mediaRequestsErrored += 1;
|
12923 | this.error(error);
|
12924 | this.trigger('error');
|
12925 | return;
|
12926 | }
|
12927 |
|
12928 |
|
12929 |
|
12930 | this.bandwidth = simpleSegment.stats.bandwidth;
|
12931 | this.roundTrip = simpleSegment.stats.roundTripTime;
|
12932 |
|
12933 |
|
12934 |
|
12935 | if (simpleSegment.map) {
|
12936 | simpleSegment.map = this.initSegment(simpleSegment.map, true);
|
12937 | }
|
12938 |
|
12939 |
|
12940 | if (simpleSegment.key) {
|
12941 | this.segmentKey(simpleSegment.key, true);
|
12942 | }
|
12943 |
|
12944 | this.processSegmentResponse_(simpleSegment);
|
12945 | }
|
12946 |
|
12947 | |
12948 |
|
12949 |
|
12950 |
|
12951 |
|
12952 |
|
12953 |
|
12954 | }, {
|
12955 | key: 'processSegmentResponse_',
|
12956 | value: function processSegmentResponse_(simpleSegment) {
|
12957 | var segmentInfo = this.pendingSegment_;
|
12958 |
|
12959 | segmentInfo.bytes = simpleSegment.bytes;
|
12960 | if (simpleSegment.map) {
|
12961 | segmentInfo.segment.map.bytes = simpleSegment.map.bytes;
|
12962 | }
|
12963 |
|
12964 | segmentInfo.endOfAllRequests = simpleSegment.endOfAllRequests;
|
12965 |
|
12966 |
|
12967 | if (simpleSegment.fmp4Captions) {
|
12968 | createCaptionsTrackIfNotExists(this.inbandTextTracks_, this.hls_.tech_, simpleSegment.captionStreams);
|
12969 | addCaptionData({
|
12970 | inbandTextTracks: this.inbandTextTracks_,
|
12971 | captionArray: simpleSegment.fmp4Captions,
|
12972 |
|
12973 | timestampOffset: 0
|
12974 | });
|
12975 |
|
12976 |
|
12977 | if (this.captionParser_) {
|
12978 | this.captionParser_.clearParsedCaptions();
|
12979 | }
|
12980 | }
|
12981 |
|
12982 | this.handleSegment_();
|
12983 | }
|
12984 |
|
12985 | |
12986 |
|
12987 |
|
12988 |
|
12989 |
|
12990 |
|
12991 | }, {
|
12992 | key: 'handleSegment_',
|
12993 | value: function handleSegment_() {
|
12994 | var _this3 = this;
|
12995 |
|
12996 | if (!this.pendingSegment_) {
|
12997 | this.state = 'READY';
|
12998 | return;
|
12999 | }
|
13000 |
|
13001 | var segmentInfo = this.pendingSegment_;
|
13002 | var segment = segmentInfo.segment;
|
13003 | var timingInfo = this.syncController_.probeSegmentInfo(segmentInfo);
|
13004 |
|
13005 |
|
13006 |
|
13007 |
|
13008 | if (typeof this.startingMedia_ === 'undefined' && timingInfo && (
|
13009 |
|
13010 |
|
13011 | timingInfo.containsAudio || timingInfo.containsVideo)) {
|
13012 | this.startingMedia_ = {
|
13013 | containsAudio: timingInfo.containsAudio,
|
13014 | containsVideo: timingInfo.containsVideo
|
13015 | };
|
13016 | }
|
13017 |
|
13018 | var illegalMediaSwitchError = illegalMediaSwitch(this.loaderType_, this.startingMedia_, timingInfo);
|
13019 |
|
13020 | if (illegalMediaSwitchError) {
|
13021 | this.error({
|
13022 | message: illegalMediaSwitchError,
|
13023 | blacklistDuration: Infinity
|
13024 | });
|
13025 | this.trigger('error');
|
13026 | return;
|
13027 | }
|
13028 |
|
13029 | if (segmentInfo.isSyncRequest) {
|
13030 | this.trigger('syncinfoupdate');
|
13031 | this.pendingSegment_ = null;
|
13032 | this.state = 'READY';
|
13033 | return;
|
13034 | }
|
13035 |
|
13036 | if (segmentInfo.timestampOffset !== null && segmentInfo.timestampOffset !== this.sourceUpdater_.timestampOffset()) {
|
13037 |
|
13038 |
|
13039 |
|
13040 |
|
13041 | if (timingInfo && timingInfo.segmentTimestampInfo) {
|
13042 | var ptsStartTime = timingInfo.segmentTimestampInfo[0].ptsTime;
|
13043 | var dtsStartTime = timingInfo.segmentTimestampInfo[0].dtsTime;
|
13044 |
|
13045 | segmentInfo.timestampOffset -= ptsStartTime - dtsStartTime;
|
13046 | }
|
13047 |
|
13048 | this.sourceUpdater_.timestampOffset(segmentInfo.timestampOffset);
|
13049 |
|
13050 | this.trigger('timestampoffset');
|
13051 | }
|
13052 |
|
13053 | var timelineMapping = this.syncController_.mappingForTimeline(segmentInfo.timeline);
|
13054 |
|
13055 | if (timelineMapping !== null) {
|
13056 | this.trigger({
|
13057 | type: 'segmenttimemapping',
|
13058 | mapping: timelineMapping
|
13059 | });
|
13060 | }
|
13061 |
|
13062 | this.state = 'APPENDING';
|
13063 |
|
13064 |
|
13065 |
|
13066 | if (segment.map) {
|
13067 | var initId = initSegmentId(segment.map);
|
13068 |
|
13069 | if (!this.activeInitSegmentId_ || this.activeInitSegmentId_ !== initId) {
|
13070 | var initSegment = this.initSegment(segment.map);
|
13071 |
|
13072 | this.sourceUpdater_.appendBuffer({
|
13073 | bytes: initSegment.bytes
|
13074 | }, function () {
|
13075 | _this3.activeInitSegmentId_ = initId;
|
13076 | });
|
13077 | }
|
13078 | }
|
13079 |
|
13080 | segmentInfo.byteLength = segmentInfo.bytes.byteLength;
|
13081 | if (typeof segment.start === 'number' && typeof segment.end === 'number') {
|
13082 | this.mediaSecondsLoaded += segment.end - segment.start;
|
13083 | } else {
|
13084 | this.mediaSecondsLoaded += segment.duration;
|
13085 | }
|
13086 |
|
13087 | this.logger_(segmentInfoString(segmentInfo));
|
13088 |
|
13089 | this.sourceUpdater_.appendBuffer({
|
13090 | bytes: segmentInfo.bytes,
|
13091 | videoSegmentTimingInfoCallback: this.handleVideoSegmentTimingInfo_.bind(this, segmentInfo.requestId)
|
13092 | }, this.handleUpdateEnd_.bind(this));
|
13093 | }
|
13094 | }, {
|
13095 | key: 'handleVideoSegmentTimingInfo_',
|
13096 | value: function handleVideoSegmentTimingInfo_(requestId, event) {
|
13097 | if (!this.pendingSegment_ || requestId !== this.pendingSegment_.requestId) {
|
13098 | return;
|
13099 | }
|
13100 |
|
13101 | var segment = this.pendingSegment_.segment;
|
13102 |
|
13103 | if (!segment.videoTimingInfo) {
|
13104 | segment.videoTimingInfo = {};
|
13105 | }
|
13106 |
|
13107 | segment.videoTimingInfo.transmuxerPrependedSeconds = event.videoSegmentTimingInfo.prependedContentDuration || 0;
|
13108 | segment.videoTimingInfo.transmuxedPresentationStart = event.videoSegmentTimingInfo.start.presentation;
|
13109 | segment.videoTimingInfo.transmuxedPresentationEnd = event.videoSegmentTimingInfo.end.presentation;
|
13110 |
|
13111 | segment.videoTimingInfo.baseMediaDecodeTime = event.videoSegmentTimingInfo.baseMediaDecodeTime;
|
13112 | }
|
13113 |
|
13114 | |
13115 |
|
13116 |
|
13117 |
|
13118 |
|
13119 |
|
13120 |
|
13121 |
|
13122 | }, {
|
13123 | key: 'handleUpdateEnd_',
|
13124 | value: function handleUpdateEnd_() {
|
13125 | if (!this.pendingSegment_) {
|
13126 | this.state = 'READY';
|
13127 | if (!this.paused()) {
|
13128 | this.monitorBuffer_();
|
13129 | }
|
13130 | return;
|
13131 | }
|
13132 |
|
13133 | var segmentInfo = this.pendingSegment_;
|
13134 | var segment = segmentInfo.segment;
|
13135 | var isWalkingForward = this.mediaIndex !== null;
|
13136 |
|
13137 | this.pendingSegment_ = null;
|
13138 | this.recordThroughput_(segmentInfo);
|
13139 | this.addSegmentMetadataCue_(segmentInfo);
|
13140 |
|
13141 | this.state = 'READY';
|
13142 |
|
13143 | this.mediaIndex = segmentInfo.mediaIndex;
|
13144 | this.fetchAtBuffer_ = true;
|
13145 | this.currentTimeline_ = segmentInfo.timeline;
|
13146 |
|
13147 |
|
13148 |
|
13149 |
|
13150 |
|
13151 | this.trigger('syncinfoupdate');
|
13152 |
|
13153 |
|
13154 |
|
13155 |
|
13156 |
|
13157 | if (segment.end && this.currentTime_() - segment.end > segmentInfo.playlist.targetDuration * 3) {
|
13158 | this.resetEverything();
|
13159 | return;
|
13160 | }
|
13161 |
|
13162 |
|
13163 |
|
13164 | if (isWalkingForward) {
|
13165 | this.trigger('bandwidthupdate');
|
13166 | }
|
13167 | this.trigger('progress');
|
13168 |
|
13169 |
|
13170 |
|
13171 |
|
13172 | if (this.isEndOfStream_(segmentInfo.mediaIndex + 1, segmentInfo.playlist)) {
|
13173 | this.endOfStream();
|
13174 | }
|
13175 |
|
13176 | if (!this.paused()) {
|
13177 | this.monitorBuffer_();
|
13178 | }
|
13179 | }
|
13180 |
|
13181 | |
13182 |
|
13183 |
|
13184 |
|
13185 |
|
13186 |
|
13187 |
|
13188 |
|
13189 |
|
13190 |
|
13191 | }, {
|
13192 | key: 'recordThroughput_',
|
13193 | value: function recordThroughput_(segmentInfo) {
|
13194 | var rate = this.throughput.rate;
|
13195 |
|
13196 |
|
13197 | var segmentProcessingTime = Date.now() - segmentInfo.endOfAllRequests + 1;
|
13198 |
|
13199 | var segmentProcessingThroughput = Math.floor(segmentInfo.byteLength / segmentProcessingTime * 8 * 1000);
|
13200 |
|
13201 |
|
13202 |
|
13203 | this.throughput.rate += (segmentProcessingThroughput - rate) / ++this.throughput.count;
|
13204 | }
|
13205 |
|
13206 | |
13207 |
|
13208 |
|
13209 |
|
13210 |
|
13211 |
|
13212 |
|
13213 |
|
13214 |
|
13215 |
|
13216 | }, {
|
13217 | key: 'addSegmentMetadataCue_',
|
13218 | value: function addSegmentMetadataCue_(segmentInfo) {
|
13219 | if (!this.segmentMetadataTrack_) {
|
13220 | return;
|
13221 | }
|
13222 |
|
13223 | var segment = segmentInfo.segment;
|
13224 | var start = segment.start;
|
13225 | var end = segment.end;
|
13226 |
|
13227 |
|
13228 | if (!finite(start) || !finite(end)) {
|
13229 | return;
|
13230 | }
|
13231 |
|
13232 | removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
|
13233 |
|
13234 | var Cue = window$1.WebKitDataCue || window$1.VTTCue;
|
13235 | var value = {
|
13236 | custom: segment.custom,
|
13237 | dateTimeObject: segment.dateTimeObject,
|
13238 | dateTimeString: segment.dateTimeString,
|
13239 | bandwidth: segmentInfo.playlist.attributes.BANDWIDTH,
|
13240 | resolution: segmentInfo.playlist.attributes.RESOLUTION,
|
13241 | codecs: segmentInfo.playlist.attributes.CODECS,
|
13242 | byteLength: segmentInfo.byteLength,
|
13243 | uri: segmentInfo.uri,
|
13244 | timeline: segmentInfo.timeline,
|
13245 | playlist: segmentInfo.playlist.id,
|
13246 | start: start,
|
13247 | end: end
|
13248 | };
|
13249 | var data = JSON.stringify(value);
|
13250 | var cue = new Cue(start, end, data);
|
13251 |
|
13252 |
|
13253 |
|
13254 | cue.value = value;
|
13255 |
|
13256 | this.segmentMetadataTrack_.addCue(cue);
|
13257 | }
|
13258 | }]);
|
13259 | return SegmentLoader;
|
13260 | }(videojs.EventTarget);
|
13261 |
|
13262 | var uint8ToUtf8 = function uint8ToUtf8(uintArray) {
|
13263 | return decodeURIComponent(escape(String.fromCharCode.apply(null, uintArray)));
|
13264 | };
|
13265 |
|
13266 |
|
13267 |
|
13268 |
|
13269 |
|
13270 | var VTT_LINE_TERMINATORS = new Uint8Array('\n\n'.split('').map(function (char) {
|
13271 | return char.charCodeAt(0);
|
13272 | }));
|
13273 |
|
13274 |
|
13275 |
|
13276 |
|
13277 |
|
13278 |
|
13279 |
|
13280 |
|
13281 |
|
13282 | var VTTSegmentLoader = function (_SegmentLoader) {
|
13283 | inherits(VTTSegmentLoader, _SegmentLoader);
|
13284 |
|
13285 | function VTTSegmentLoader(settings) {
|
13286 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
13287 | classCallCheck(this, VTTSegmentLoader);
|
13288 |
|
13289 |
|
13290 |
|
13291 | var _this = possibleConstructorReturn(this, (VTTSegmentLoader.__proto__ || Object.getPrototypeOf(VTTSegmentLoader)).call(this, settings, options));
|
13292 |
|
13293 | _this.mediaSource_ = null;
|
13294 |
|
13295 | _this.subtitlesTrack_ = null;
|
13296 |
|
13297 | _this.featuresNativeTextTracks_ = settings.featuresNativeTextTracks;
|
13298 | return _this;
|
13299 | }
|
13300 |
|
13301 | |
13302 |
|
13303 |
|
13304 |
|
13305 |
|
13306 |
|
13307 |
|
13308 |
|
13309 | createClass(VTTSegmentLoader, [{
|
13310 | key: 'buffered_',
|
13311 | value: function buffered_() {
|
13312 | if (!this.subtitlesTrack_ || !this.subtitlesTrack_.cues.length) {
|
13313 | return videojs.createTimeRanges();
|
13314 | }
|
13315 |
|
13316 | var cues = this.subtitlesTrack_.cues;
|
13317 | var start = cues[0].startTime;
|
13318 | var end = cues[cues.length - 1].startTime;
|
13319 |
|
13320 | return videojs.createTimeRanges([[start, end]]);
|
13321 | }
|
13322 |
|
13323 | |
13324 |
|
13325 |
|
13326 |
|
13327 |
|
13328 |
|
13329 |
|
13330 |
|
13331 |
|
13332 |
|
13333 |
|
13334 | }, {
|
13335 | key: 'initSegment',
|
13336 | value: function initSegment(map) {
|
13337 | var set$$1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
13338 |
|
13339 | if (!map) {
|
13340 | return null;
|
13341 | }
|
13342 |
|
13343 | var id = initSegmentId(map);
|
13344 | var storedMap = this.initSegments_[id];
|
13345 |
|
13346 | if (set$$1 && !storedMap && map.bytes) {
|
13347 |
|
13348 |
|
13349 |
|
13350 |
|
13351 | var combinedByteLength = VTT_LINE_TERMINATORS.byteLength + map.bytes.byteLength;
|
13352 | var combinedSegment = new Uint8Array(combinedByteLength);
|
13353 |
|
13354 | combinedSegment.set(map.bytes);
|
13355 | combinedSegment.set(VTT_LINE_TERMINATORS, map.bytes.byteLength);
|
13356 |
|
13357 | this.initSegments_[id] = storedMap = {
|
13358 | resolvedUri: map.resolvedUri,
|
13359 | byterange: map.byterange,
|
13360 | bytes: combinedSegment
|
13361 | };
|
13362 | }
|
13363 |
|
13364 | return storedMap || map;
|
13365 | }
|
13366 |
|
13367 | |
13368 |
|
13369 |
|
13370 |
|
13371 |
|
13372 |
|
13373 |
|
13374 | }, {
|
13375 | key: 'couldBeginLoading_',
|
13376 | value: function couldBeginLoading_() {
|
13377 | return this.playlist_ && this.subtitlesTrack_ && !this.paused();
|
13378 | }
|
13379 |
|
13380 | |
13381 |
|
13382 |
|
13383 |
|
13384 |
|
13385 |
|
13386 |
|
13387 |
|
13388 | }, {
|
13389 | key: 'init_',
|
13390 | value: function init_() {
|
13391 | this.state = 'READY';
|
13392 | this.resetEverything();
|
13393 | return this.monitorBuffer_();
|
13394 | }
|
13395 |
|
13396 | |
13397 |
|
13398 |
|
13399 |
|
13400 |
|
13401 |
|
13402 |
|
13403 |
|
13404 |
|
13405 | }, {
|
13406 | key: 'track',
|
13407 | value: function track(_track) {
|
13408 | if (typeof _track === 'undefined') {
|
13409 | return this.subtitlesTrack_;
|
13410 | }
|
13411 |
|
13412 | this.subtitlesTrack_ = _track;
|
13413 |
|
13414 |
|
13415 |
|
13416 | if (this.state === 'INIT' && this.couldBeginLoading_()) {
|
13417 | this.init_();
|
13418 | }
|
13419 |
|
13420 | return this.subtitlesTrack_;
|
13421 | }
|
13422 |
|
13423 | |
13424 |
|
13425 |
|
13426 |
|
13427 |
|
13428 |
|
13429 | }, {
|
13430 | key: 'remove',
|
13431 | value: function remove(start, end) {
|
13432 | removeCuesFromTrack(start, end, this.subtitlesTrack_);
|
13433 | }
|
13434 |
|
13435 | |
13436 |
|
13437 |
|
13438 |
|
13439 |
|
13440 |
|
13441 |
|
13442 |
|
13443 |
|
13444 |
|
13445 | }, {
|
13446 | key: 'fillBuffer_',
|
13447 | value: function fillBuffer_() {
|
13448 | var _this2 = this;
|
13449 |
|
13450 | if (!this.syncPoint_) {
|
13451 | this.syncPoint_ = this.syncController_.getSyncPoint(this.playlist_, this.duration_(), this.currentTimeline_, this.currentTime_());
|
13452 | }
|
13453 |
|
13454 |
|
13455 | var segmentInfo = this.checkBuffer_(this.buffered_(), this.playlist_, this.mediaIndex, this.hasPlayed_(), this.currentTime_(), this.syncPoint_);
|
13456 |
|
13457 | segmentInfo = this.skipEmptySegments_(segmentInfo);
|
13458 |
|
13459 | if (!segmentInfo) {
|
13460 | return;
|
13461 | }
|
13462 |
|
13463 | if (this.syncController_.timestampOffsetForTimeline(segmentInfo.timeline) === null) {
|
13464 |
|
13465 |
|
13466 | var checkTimestampOffset = function checkTimestampOffset() {
|
13467 | _this2.state = 'READY';
|
13468 | if (!_this2.paused()) {
|
13469 |
|
13470 | _this2.monitorBuffer_();
|
13471 | }
|
13472 | };
|
13473 |
|
13474 | this.syncController_.one('timestampoffset', checkTimestampOffset);
|
13475 | this.state = 'WAITING_ON_TIMELINE';
|
13476 | return;
|
13477 | }
|
13478 |
|
13479 | this.loadSegment_(segmentInfo);
|
13480 | }
|
13481 |
|
13482 | |
13483 |
|
13484 |
|
13485 |
|
13486 |
|
13487 |
|
13488 |
|
13489 |
|
13490 |
|
13491 |
|
13492 |
|
13493 | }, {
|
13494 | key: 'skipEmptySegments_',
|
13495 | value: function skipEmptySegments_(segmentInfo) {
|
13496 | while (segmentInfo && segmentInfo.segment.empty) {
|
13497 | segmentInfo = this.generateSegmentInfo_(segmentInfo.playlist, segmentInfo.mediaIndex + 1, segmentInfo.startOfSegment + segmentInfo.duration, segmentInfo.isSyncRequest);
|
13498 | }
|
13499 | return segmentInfo;
|
13500 | }
|
13501 |
|
13502 | |
13503 |
|
13504 |
|
13505 |
|
13506 |
|
13507 |
|
13508 | }, {
|
13509 | key: 'handleSegment_',
|
13510 | value: function handleSegment_() {
|
13511 | var _this3 = this;
|
13512 |
|
13513 | if (!this.pendingSegment_ || !this.subtitlesTrack_) {
|
13514 | this.state = 'READY';
|
13515 | return;
|
13516 | }
|
13517 |
|
13518 | this.state = 'APPENDING';
|
13519 |
|
13520 | var segmentInfo = this.pendingSegment_;
|
13521 | var segment = segmentInfo.segment;
|
13522 |
|
13523 |
|
13524 | if (typeof window$1.WebVTT !== 'function' && this.subtitlesTrack_ && this.subtitlesTrack_.tech_) {
|
13525 |
|
13526 | var loadHandler = void 0;
|
13527 | var errorHandler = function errorHandler() {
|
13528 | _this3.subtitlesTrack_.tech_.off('vttjsloaded', loadHandler);
|
13529 | _this3.error({
|
13530 | message: 'Error loading vtt.js'
|
13531 | });
|
13532 | _this3.state = 'READY';
|
13533 | _this3.pause();
|
13534 | _this3.trigger('error');
|
13535 | };
|
13536 |
|
13537 | loadHandler = function loadHandler() {
|
13538 | _this3.subtitlesTrack_.tech_.off('vttjserror', errorHandler);
|
13539 | _this3.handleSegment_();
|
13540 | };
|
13541 |
|
13542 | this.state = 'WAITING_ON_VTTJS';
|
13543 | this.subtitlesTrack_.tech_.one('vttjsloaded', loadHandler);
|
13544 | this.subtitlesTrack_.tech_.one('vttjserror', errorHandler);
|
13545 |
|
13546 | return;
|
13547 | }
|
13548 |
|
13549 | segment.requested = true;
|
13550 |
|
13551 | try {
|
13552 | this.parseVTTCues_(segmentInfo);
|
13553 | } catch (e) {
|
13554 | this.error({
|
13555 | message: e.message
|
13556 | });
|
13557 | this.state = 'READY';
|
13558 | this.pause();
|
13559 | return this.trigger('error');
|
13560 | }
|
13561 |
|
13562 | this.updateTimeMapping_(segmentInfo, this.syncController_.timelines[segmentInfo.timeline], this.playlist_);
|
13563 |
|
13564 | if (segmentInfo.isSyncRequest) {
|
13565 | this.trigger('syncinfoupdate');
|
13566 | this.pendingSegment_ = null;
|
13567 | this.state = 'READY';
|
13568 | return;
|
13569 | }
|
13570 |
|
13571 | segmentInfo.byteLength = segmentInfo.bytes.byteLength;
|
13572 |
|
13573 | this.mediaSecondsLoaded += segment.duration;
|
13574 |
|
13575 | if (segmentInfo.cues.length) {
|
13576 |
|
13577 | this.remove(segmentInfo.cues[0].endTime, segmentInfo.cues[segmentInfo.cues.length - 1].endTime);
|
13578 | }
|
13579 |
|
13580 | segmentInfo.cues.forEach(function (cue) {
|
13581 | _this3.subtitlesTrack_.addCue(_this3.featuresNativeTextTracks_ ? new window$1.VTTCue(cue.startTime, cue.endTime, cue.text) : cue);
|
13582 | });
|
13583 |
|
13584 | this.handleUpdateEnd_();
|
13585 | }
|
13586 |
|
13587 | |
13588 |
|
13589 |
|
13590 |
|
13591 |
|
13592 |
|
13593 |
|
13594 |
|
13595 | }, {
|
13596 | key: 'parseVTTCues_',
|
13597 | value: function parseVTTCues_(segmentInfo) {
|
13598 | var decoder = void 0;
|
13599 | var decodeBytesToString = false;
|
13600 |
|
13601 | if (typeof window$1.TextDecoder === 'function') {
|
13602 | decoder = new window$1.TextDecoder('utf8');
|
13603 | } else {
|
13604 | decoder = window$1.WebVTT.StringDecoder();
|
13605 | decodeBytesToString = true;
|
13606 | }
|
13607 |
|
13608 | var parser = new window$1.WebVTT.Parser(window$1, window$1.vttjs, decoder);
|
13609 |
|
13610 | segmentInfo.cues = [];
|
13611 | segmentInfo.timestampmap = { MPEGTS: 0, LOCAL: 0 };
|
13612 |
|
13613 | parser.oncue = segmentInfo.cues.push.bind(segmentInfo.cues);
|
13614 | parser.ontimestampmap = function (map) {
|
13615 | return segmentInfo.timestampmap = map;
|
13616 | };
|
13617 | parser.onparsingerror = function (error) {
|
13618 | videojs.log.warn('Error encountered when parsing cues: ' + error.message);
|
13619 | };
|
13620 |
|
13621 | if (segmentInfo.segment.map) {
|
13622 | var mapData = segmentInfo.segment.map.bytes;
|
13623 |
|
13624 | if (decodeBytesToString) {
|
13625 | mapData = uint8ToUtf8(mapData);
|
13626 | }
|
13627 |
|
13628 | parser.parse(mapData);
|
13629 | }
|
13630 |
|
13631 | var segmentData = segmentInfo.bytes;
|
13632 |
|
13633 | if (decodeBytesToString) {
|
13634 | segmentData = uint8ToUtf8(segmentData);
|
13635 | }
|
13636 |
|
13637 | parser.parse(segmentData);
|
13638 | parser.flush();
|
13639 | }
|
13640 |
|
13641 | |
13642 |
|
13643 |
|
13644 |
|
13645 |
|
13646 |
|
13647 |
|
13648 |
|
13649 |
|
13650 |
|
13651 |
|
13652 |
|
13653 |
|
13654 |
|
13655 | }, {
|
13656 | key: 'updateTimeMapping_',
|
13657 | value: function updateTimeMapping_(segmentInfo, mappingObj, playlist) {
|
13658 | var segment = segmentInfo.segment;
|
13659 |
|
13660 | if (!mappingObj) {
|
13661 |
|
13662 |
|
13663 |
|
13664 | return;
|
13665 | }
|
13666 |
|
13667 | if (!segmentInfo.cues.length) {
|
13668 |
|
13669 |
|
13670 |
|
13671 | segment.empty = true;
|
13672 | return;
|
13673 | }
|
13674 |
|
13675 | var timestampmap = segmentInfo.timestampmap;
|
13676 | var diff = timestampmap.MPEGTS / 90000 - timestampmap.LOCAL + mappingObj.mapping;
|
13677 |
|
13678 | segmentInfo.cues.forEach(function (cue) {
|
13679 |
|
13680 | cue.startTime += diff;
|
13681 | cue.endTime += diff;
|
13682 | });
|
13683 |
|
13684 | if (!playlist.syncInfo) {
|
13685 | var firstStart = segmentInfo.cues[0].startTime;
|
13686 | var lastStart = segmentInfo.cues[segmentInfo.cues.length - 1].startTime;
|
13687 |
|
13688 | playlist.syncInfo = {
|
13689 | mediaSequence: playlist.mediaSequence + segmentInfo.mediaIndex,
|
13690 | time: Math.min(firstStart, lastStart - segment.duration)
|
13691 | };
|
13692 | }
|
13693 | }
|
13694 | }]);
|
13695 | return VTTSegmentLoader;
|
13696 | }(SegmentLoader);
|
13697 |
|
13698 |
|
13699 |
|
13700 |
|
13701 |
|
13702 |
|
13703 |
|
13704 |
|
13705 | var findAdCue = function findAdCue(track, mediaTime) {
|
13706 | var cues = track.cues;
|
13707 |
|
13708 | for (var i = 0; i < cues.length; i++) {
|
13709 | var cue = cues[i];
|
13710 |
|
13711 | if (mediaTime >= cue.adStartTime && mediaTime <= cue.adEndTime) {
|
13712 | return cue;
|
13713 | }
|
13714 | }
|
13715 | return null;
|
13716 | };
|
13717 |
|
13718 | var updateAdCues = function updateAdCues(media, track) {
|
13719 | var offset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
13720 |
|
13721 | if (!media.segments) {
|
13722 | return;
|
13723 | }
|
13724 |
|
13725 | var mediaTime = offset;
|
13726 | var cue = void 0;
|
13727 |
|
13728 | for (var i = 0; i < media.segments.length; i++) {
|
13729 | var segment = media.segments[i];
|
13730 |
|
13731 | if (!cue) {
|
13732 |
|
13733 |
|
13734 |
|
13735 |
|
13736 | cue = findAdCue(track, mediaTime + segment.duration / 2);
|
13737 | }
|
13738 |
|
13739 | if (cue) {
|
13740 | if ('cueIn' in segment) {
|
13741 |
|
13742 | cue.endTime = mediaTime;
|
13743 | cue.adEndTime = mediaTime;
|
13744 | mediaTime += segment.duration;
|
13745 | cue = null;
|
13746 | continue;
|
13747 | }
|
13748 |
|
13749 | if (mediaTime < cue.endTime) {
|
13750 |
|
13751 | mediaTime += segment.duration;
|
13752 | continue;
|
13753 | }
|
13754 |
|
13755 |
|
13756 | cue.endTime += segment.duration;
|
13757 | } else {
|
13758 | if ('cueOut' in segment) {
|
13759 | cue = new window$1.VTTCue(mediaTime, mediaTime + segment.duration, segment.cueOut);
|
13760 | cue.adStartTime = mediaTime;
|
13761 |
|
13762 |
|
13763 | cue.adEndTime = mediaTime + parseFloat(segment.cueOut);
|
13764 | track.addCue(cue);
|
13765 | }
|
13766 |
|
13767 | if ('cueOutCont' in segment) {
|
13768 |
|
13769 | var adOffset = void 0;
|
13770 | var adTotal = void 0;
|
13771 |
|
13772 |
|
13773 |
|
13774 |
|
13775 | var _segment$cueOutCont$s = segment.cueOutCont.split('/').map(parseFloat);
|
13776 |
|
13777 | var _segment$cueOutCont$s2 = slicedToArray(_segment$cueOutCont$s, 2);
|
13778 |
|
13779 | adOffset = _segment$cueOutCont$s2[0];
|
13780 | adTotal = _segment$cueOutCont$s2[1];
|
13781 |
|
13782 |
|
13783 | cue = new window$1.VTTCue(mediaTime, mediaTime + segment.duration, '');
|
13784 | cue.adStartTime = mediaTime - adOffset;
|
13785 | cue.adEndTime = cue.adStartTime + adTotal;
|
13786 | track.addCue(cue);
|
13787 | }
|
13788 | }
|
13789 | mediaTime += segment.duration;
|
13790 | }
|
13791 | };
|
13792 |
|
13793 |
|
13794 |
|
13795 |
|
13796 |
|
13797 | var tsprobe = tsInspector.inspect;
|
13798 |
|
13799 | var syncPointStrategies = [
|
13800 |
|
13801 |
|
13802 | {
|
13803 | name: 'VOD',
|
13804 | run: function run(syncController, playlist, duration$$1, currentTimeline, currentTime) {
|
13805 | if (duration$$1 !== Infinity) {
|
13806 | var syncPoint = {
|
13807 | time: 0,
|
13808 | segmentIndex: 0
|
13809 | };
|
13810 |
|
13811 | return syncPoint;
|
13812 | }
|
13813 | return null;
|
13814 | }
|
13815 | },
|
13816 |
|
13817 | {
|
13818 | name: 'ProgramDateTime',
|
13819 | run: function run(syncController, playlist, duration$$1, currentTimeline, currentTime) {
|
13820 | if (!syncController.datetimeToDisplayTime) {
|
13821 | return null;
|
13822 | }
|
13823 |
|
13824 | var segments = playlist.segments || [];
|
13825 | var syncPoint = null;
|
13826 | var lastDistance = null;
|
13827 |
|
13828 | currentTime = currentTime || 0;
|
13829 |
|
13830 | for (var i = 0; i < segments.length; i++) {
|
13831 | var segment = segments[i];
|
13832 |
|
13833 | if (segment.dateTimeObject) {
|
13834 | var segmentTime = segment.dateTimeObject.getTime() / 1000;
|
13835 | var segmentStart = segmentTime + syncController.datetimeToDisplayTime;
|
13836 | var distance = Math.abs(currentTime - segmentStart);
|
13837 |
|
13838 |
|
13839 |
|
13840 | if (lastDistance !== null && (distance === 0 || lastDistance < distance)) {
|
13841 | break;
|
13842 | }
|
13843 |
|
13844 | lastDistance = distance;
|
13845 | syncPoint = {
|
13846 | time: segmentStart,
|
13847 | segmentIndex: i
|
13848 | };
|
13849 | }
|
13850 | }
|
13851 | return syncPoint;
|
13852 | }
|
13853 | },
|
13854 |
|
13855 |
|
13856 | {
|
13857 | name: 'Segment',
|
13858 | run: function run(syncController, playlist, duration$$1, currentTimeline, currentTime) {
|
13859 | var segments = playlist.segments || [];
|
13860 | var syncPoint = null;
|
13861 | var lastDistance = null;
|
13862 |
|
13863 | currentTime = currentTime || 0;
|
13864 |
|
13865 | for (var i = 0; i < segments.length; i++) {
|
13866 | var segment = segments[i];
|
13867 |
|
13868 | if (segment.timeline === currentTimeline && typeof segment.start !== 'undefined') {
|
13869 | var distance = Math.abs(currentTime - segment.start);
|
13870 |
|
13871 |
|
13872 |
|
13873 | if (lastDistance !== null && lastDistance < distance) {
|
13874 | break;
|
13875 | }
|
13876 |
|
13877 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
13878 | lastDistance = distance;
|
13879 | syncPoint = {
|
13880 | time: segment.start,
|
13881 | segmentIndex: i
|
13882 | };
|
13883 | }
|
13884 | }
|
13885 | }
|
13886 | return syncPoint;
|
13887 | }
|
13888 | },
|
13889 |
|
13890 |
|
13891 | {
|
13892 | name: 'Discontinuity',
|
13893 | run: function run(syncController, playlist, duration$$1, currentTimeline, currentTime) {
|
13894 | var syncPoint = null;
|
13895 |
|
13896 | currentTime = currentTime || 0;
|
13897 |
|
13898 | if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
13899 | var lastDistance = null;
|
13900 |
|
13901 | for (var i = 0; i < playlist.discontinuityStarts.length; i++) {
|
13902 | var segmentIndex = playlist.discontinuityStarts[i];
|
13903 | var discontinuity = playlist.discontinuitySequence + i + 1;
|
13904 | var discontinuitySync = syncController.discontinuities[discontinuity];
|
13905 |
|
13906 | if (discontinuitySync) {
|
13907 | var distance = Math.abs(currentTime - discontinuitySync.time);
|
13908 |
|
13909 |
|
13910 |
|
13911 | if (lastDistance !== null && lastDistance < distance) {
|
13912 | break;
|
13913 | }
|
13914 |
|
13915 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
13916 | lastDistance = distance;
|
13917 | syncPoint = {
|
13918 | time: discontinuitySync.time,
|
13919 | segmentIndex: segmentIndex
|
13920 | };
|
13921 | }
|
13922 | }
|
13923 | }
|
13924 | }
|
13925 | return syncPoint;
|
13926 | }
|
13927 | },
|
13928 |
|
13929 |
|
13930 | {
|
13931 | name: 'Playlist',
|
13932 | run: function run(syncController, playlist, duration$$1, currentTimeline, currentTime) {
|
13933 | if (playlist.syncInfo) {
|
13934 | var syncPoint = {
|
13935 | time: playlist.syncInfo.time,
|
13936 | segmentIndex: playlist.syncInfo.mediaSequence - playlist.mediaSequence
|
13937 | };
|
13938 |
|
13939 | return syncPoint;
|
13940 | }
|
13941 | return null;
|
13942 | }
|
13943 | }];
|
13944 |
|
13945 | var SyncController = function (_videojs$EventTarget) {
|
13946 | inherits(SyncController, _videojs$EventTarget);
|
13947 |
|
13948 | function SyncController() {
|
13949 | classCallCheck(this, SyncController);
|
13950 |
|
13951 |
|
13952 |
|
13953 | var _this = possibleConstructorReturn(this, (SyncController.__proto__ || Object.getPrototypeOf(SyncController)).call(this));
|
13954 |
|
13955 | _this.inspectCache_ = undefined;
|
13956 |
|
13957 |
|
13958 | _this.timelines = [];
|
13959 | _this.discontinuities = [];
|
13960 | _this.datetimeToDisplayTime = null;
|
13961 |
|
13962 | _this.logger_ = logger('SyncController');
|
13963 | return _this;
|
13964 | }
|
13965 |
|
13966 | |
13967 |
|
13968 |
|
13969 |
|
13970 |
|
13971 |
|
13972 |
|
13973 |
|
13974 |
|
13975 |
|
13976 |
|
13977 |
|
13978 |
|
13979 |
|
13980 |
|
13981 |
|
13982 |
|
13983 | createClass(SyncController, [{
|
13984 | key: 'getSyncPoint',
|
13985 | value: function getSyncPoint(playlist, duration$$1, currentTimeline, currentTime) {
|
13986 | var syncPoints = this.runStrategies_(playlist, duration$$1, currentTimeline, currentTime);
|
13987 |
|
13988 | if (!syncPoints.length) {
|
13989 |
|
13990 |
|
13991 |
|
13992 | return null;
|
13993 | }
|
13994 |
|
13995 |
|
13996 |
|
13997 |
|
13998 | return this.selectSyncPoint_(syncPoints, { key: 'time', value: currentTime });
|
13999 | }
|
14000 |
|
14001 | |
14002 |
|
14003 |
|
14004 |
|
14005 |
|
14006 |
|
14007 |
|
14008 |
|
14009 |
|
14010 |
|
14011 |
|
14012 |
|
14013 | }, {
|
14014 | key: 'getExpiredTime',
|
14015 | value: function getExpiredTime(playlist, duration$$1) {
|
14016 | if (!playlist || !playlist.segments) {
|
14017 | return null;
|
14018 | }
|
14019 |
|
14020 | var syncPoints = this.runStrategies_(playlist, duration$$1, playlist.discontinuitySequence, 0);
|
14021 |
|
14022 |
|
14023 | if (!syncPoints.length) {
|
14024 | return null;
|
14025 | }
|
14026 |
|
14027 | var syncPoint = this.selectSyncPoint_(syncPoints, {
|
14028 | key: 'segmentIndex',
|
14029 | value: 0
|
14030 | });
|
14031 |
|
14032 |
|
14033 |
|
14034 | if (syncPoint.segmentIndex > 0) {
|
14035 | syncPoint.time *= -1;
|
14036 | }
|
14037 |
|
14038 | return Math.abs(syncPoint.time + sumDurations(playlist, syncPoint.segmentIndex, 0));
|
14039 | }
|
14040 |
|
14041 | |
14042 |
|
14043 |
|
14044 |
|
14045 |
|
14046 |
|
14047 |
|
14048 |
|
14049 |
|
14050 |
|
14051 |
|
14052 |
|
14053 |
|
14054 |
|
14055 |
|
14056 | }, {
|
14057 | key: 'runStrategies_',
|
14058 | value: function runStrategies_(playlist, duration$$1, currentTimeline, currentTime) {
|
14059 | var syncPoints = [];
|
14060 |
|
14061 |
|
14062 | for (var i = 0; i < syncPointStrategies.length; i++) {
|
14063 | var strategy = syncPointStrategies[i];
|
14064 | var syncPoint = strategy.run(this, playlist, duration$$1, currentTimeline, currentTime);
|
14065 |
|
14066 | if (syncPoint) {
|
14067 | syncPoint.strategy = strategy.name;
|
14068 | syncPoints.push({
|
14069 | strategy: strategy.name,
|
14070 | syncPoint: syncPoint
|
14071 | });
|
14072 | }
|
14073 | }
|
14074 |
|
14075 | return syncPoints;
|
14076 | }
|
14077 |
|
14078 | |
14079 |
|
14080 |
|
14081 |
|
14082 |
|
14083 |
|
14084 |
|
14085 |
|
14086 |
|
14087 |
|
14088 |
|
14089 |
|
14090 |
|
14091 |
|
14092 |
|
14093 |
|
14094 | }, {
|
14095 | key: 'selectSyncPoint_',
|
14096 | value: function selectSyncPoint_(syncPoints, target) {
|
14097 | var bestSyncPoint = syncPoints[0].syncPoint;
|
14098 | var bestDistance = Math.abs(syncPoints[0].syncPoint[target.key] - target.value);
|
14099 | var bestStrategy = syncPoints[0].strategy;
|
14100 |
|
14101 | for (var i = 1; i < syncPoints.length; i++) {
|
14102 | var newDistance = Math.abs(syncPoints[i].syncPoint[target.key] - target.value);
|
14103 |
|
14104 | if (newDistance < bestDistance) {
|
14105 | bestDistance = newDistance;
|
14106 | bestSyncPoint = syncPoints[i].syncPoint;
|
14107 | bestStrategy = syncPoints[i].strategy;
|
14108 | }
|
14109 | }
|
14110 |
|
14111 | this.logger_('syncPoint for [' + target.key + ': ' + target.value + '] chosen with strategy' + (' [' + bestStrategy + ']: [time:' + bestSyncPoint.time + ',') + (' segmentIndex:' + bestSyncPoint.segmentIndex + ']'));
|
14112 |
|
14113 | return bestSyncPoint;
|
14114 | }
|
14115 |
|
14116 | |
14117 |
|
14118 |
|
14119 |
|
14120 |
|
14121 |
|
14122 |
|
14123 |
|
14124 |
|
14125 | }, {
|
14126 | key: 'saveExpiredSegmentInfo',
|
14127 | value: function saveExpiredSegmentInfo(oldPlaylist, newPlaylist) {
|
14128 | var mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
14129 |
|
14130 |
|
14131 |
|
14132 | for (var i = mediaSequenceDiff - 1; i >= 0; i--) {
|
14133 | var lastRemovedSegment = oldPlaylist.segments[i];
|
14134 |
|
14135 | if (lastRemovedSegment && typeof lastRemovedSegment.start !== 'undefined') {
|
14136 | newPlaylist.syncInfo = {
|
14137 | mediaSequence: oldPlaylist.mediaSequence + i,
|
14138 | time: lastRemovedSegment.start
|
14139 | };
|
14140 | this.logger_('playlist refresh sync: [time:' + newPlaylist.syncInfo.time + ',' + (' mediaSequence: ' + newPlaylist.syncInfo.mediaSequence + ']'));
|
14141 | this.trigger('syncinfoupdate');
|
14142 | break;
|
14143 | }
|
14144 | }
|
14145 | }
|
14146 |
|
14147 | |
14148 |
|
14149 |
|
14150 |
|
14151 |
|
14152 |
|
14153 |
|
14154 | }, {
|
14155 | key: 'setDateTimeMapping',
|
14156 | value: function setDateTimeMapping(playlist) {
|
14157 | if (!this.datetimeToDisplayTime && playlist.segments && playlist.segments.length && playlist.segments[0].dateTimeObject) {
|
14158 | var playlistTimestamp = playlist.segments[0].dateTimeObject.getTime() / 1000;
|
14159 |
|
14160 | this.datetimeToDisplayTime = -playlistTimestamp;
|
14161 | }
|
14162 | }
|
14163 |
|
14164 | |
14165 |
|
14166 |
|
14167 |
|
14168 |
|
14169 | }, {
|
14170 | key: 'reset',
|
14171 | value: function reset() {
|
14172 | this.inspectCache_ = undefined;
|
14173 | }
|
14174 |
|
14175 | |
14176 |
|
14177 |
|
14178 |
|
14179 |
|
14180 |
|
14181 |
|
14182 |
|
14183 |
|
14184 | }, {
|
14185 | key: 'probeSegmentInfo',
|
14186 | value: function probeSegmentInfo(segmentInfo) {
|
14187 | var segment = segmentInfo.segment;
|
14188 | var playlist = segmentInfo.playlist;
|
14189 | var timingInfo = void 0;
|
14190 |
|
14191 | if (segment.map) {
|
14192 | timingInfo = this.probeMp4Segment_(segmentInfo);
|
14193 | } else {
|
14194 | timingInfo = this.probeTsSegment_(segmentInfo);
|
14195 | }
|
14196 |
|
14197 | if (timingInfo) {
|
14198 | if (this.calculateSegmentTimeMapping_(segmentInfo, timingInfo)) {
|
14199 | this.saveDiscontinuitySyncInfo_(segmentInfo);
|
14200 |
|
14201 |
|
14202 |
|
14203 | if (!playlist.syncInfo) {
|
14204 | playlist.syncInfo = {
|
14205 | mediaSequence: playlist.mediaSequence + segmentInfo.mediaIndex,
|
14206 | time: segment.start
|
14207 | };
|
14208 | }
|
14209 | }
|
14210 | }
|
14211 |
|
14212 | return timingInfo;
|
14213 | }
|
14214 |
|
14215 | |
14216 |
|
14217 |
|
14218 |
|
14219 |
|
14220 |
|
14221 |
|
14222 |
|
14223 |
|
14224 |
|
14225 | }, {
|
14226 | key: 'probeMp4Segment_',
|
14227 | value: function probeMp4Segment_(segmentInfo) {
|
14228 | var segment = segmentInfo.segment;
|
14229 |
|
14230 | var timescales = mp4probe.timescale(segment.map.bytes);
|
14231 |
|
14232 |
|
14233 | var compositionStartTime = mp4probe.compositionStartTime(timescales, segmentInfo.bytes);
|
14234 |
|
14235 | if (segmentInfo.timestampOffset !== null) {
|
14236 | segmentInfo.timestampOffset -= compositionStartTime;
|
14237 | }
|
14238 |
|
14239 | return {
|
14240 | start: compositionStartTime,
|
14241 | end: compositionStartTime + segment.duration
|
14242 | };
|
14243 | }
|
14244 |
|
14245 | |
14246 |
|
14247 |
|
14248 |
|
14249 |
|
14250 |
|
14251 |
|
14252 |
|
14253 |
|
14254 | }, {
|
14255 | key: 'probeTsSegment_',
|
14256 | value: function probeTsSegment_(segmentInfo) {
|
14257 | var timeInfo = tsprobe(segmentInfo.bytes, this.inspectCache_);
|
14258 | var segmentStartTime = void 0;
|
14259 | var segmentEndTime = void 0;
|
14260 | var segmentTimestampInfo = void 0;
|
14261 |
|
14262 | if (!timeInfo) {
|
14263 | return null;
|
14264 | }
|
14265 |
|
14266 | if (timeInfo.video && timeInfo.video.length === 2) {
|
14267 | this.inspectCache_ = timeInfo.video[1].dts;
|
14268 | segmentStartTime = timeInfo.video[0].dtsTime;
|
14269 | segmentEndTime = timeInfo.video[1].dtsTime;
|
14270 | segmentTimestampInfo = timeInfo.video;
|
14271 | } else if (timeInfo.audio && timeInfo.audio.length === 2) {
|
14272 | this.inspectCache_ = timeInfo.audio[1].dts;
|
14273 | segmentStartTime = timeInfo.audio[0].dtsTime;
|
14274 | segmentEndTime = timeInfo.audio[1].dtsTime;
|
14275 | segmentTimestampInfo = timeInfo.audio;
|
14276 | }
|
14277 |
|
14278 | var probedInfo = {
|
14279 | segmentTimestampInfo: segmentTimestampInfo,
|
14280 | start: segmentStartTime,
|
14281 | end: segmentEndTime,
|
14282 | containsVideo: timeInfo.video && timeInfo.video.length === 2,
|
14283 | containsAudio: timeInfo.audio && timeInfo.audio.length === 2
|
14284 | };
|
14285 |
|
14286 | return probedInfo;
|
14287 | }
|
14288 | }, {
|
14289 | key: 'timestampOffsetForTimeline',
|
14290 | value: function timestampOffsetForTimeline(timeline) {
|
14291 | if (typeof this.timelines[timeline] === 'undefined') {
|
14292 | return null;
|
14293 | }
|
14294 | return this.timelines[timeline].time;
|
14295 | }
|
14296 | }, {
|
14297 | key: 'mappingForTimeline',
|
14298 | value: function mappingForTimeline(timeline) {
|
14299 | if (typeof this.timelines[timeline] === 'undefined') {
|
14300 | return null;
|
14301 | }
|
14302 | return this.timelines[timeline].mapping;
|
14303 | }
|
14304 |
|
14305 | |
14306 |
|
14307 |
|
14308 |
|
14309 |
|
14310 |
|
14311 |
|
14312 |
|
14313 |
|
14314 |
|
14315 |
|
14316 |
|
14317 |
|
14318 | }, {
|
14319 | key: 'calculateSegmentTimeMapping_',
|
14320 | value: function calculateSegmentTimeMapping_(segmentInfo, timingInfo) {
|
14321 | var segment = segmentInfo.segment;
|
14322 | var mappingObj = this.timelines[segmentInfo.timeline];
|
14323 |
|
14324 | if (segmentInfo.timestampOffset !== null) {
|
14325 | mappingObj = {
|
14326 | time: segmentInfo.startOfSegment,
|
14327 | mapping: segmentInfo.startOfSegment - timingInfo.start
|
14328 | };
|
14329 | this.timelines[segmentInfo.timeline] = mappingObj;
|
14330 | this.trigger('timestampoffset');
|
14331 |
|
14332 | this.logger_('time mapping for timeline ' + segmentInfo.timeline + ': ' + ('[time: ' + mappingObj.time + '] [mapping: ' + mappingObj.mapping + ']'));
|
14333 |
|
14334 | segment.start = segmentInfo.startOfSegment;
|
14335 | segment.end = timingInfo.end + mappingObj.mapping;
|
14336 | } else if (mappingObj) {
|
14337 | segment.start = timingInfo.start + mappingObj.mapping;
|
14338 | segment.end = timingInfo.end + mappingObj.mapping;
|
14339 | } else {
|
14340 | return false;
|
14341 | }
|
14342 |
|
14343 | return true;
|
14344 | }
|
14345 |
|
14346 | |
14347 |
|
14348 |
|
14349 |
|
14350 |
|
14351 |
|
14352 |
|
14353 |
|
14354 |
|
14355 | }, {
|
14356 | key: 'saveDiscontinuitySyncInfo_',
|
14357 | value: function saveDiscontinuitySyncInfo_(segmentInfo) {
|
14358 | var playlist = segmentInfo.playlist;
|
14359 | var segment = segmentInfo.segment;
|
14360 |
|
14361 |
|
14362 |
|
14363 |
|
14364 | if (segment.discontinuity) {
|
14365 | this.discontinuities[segment.timeline] = {
|
14366 | time: segment.start,
|
14367 | accuracy: 0
|
14368 | };
|
14369 | } else if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
14370 |
|
14371 |
|
14372 | for (var i = 0; i < playlist.discontinuityStarts.length; i++) {
|
14373 | var segmentIndex = playlist.discontinuityStarts[i];
|
14374 | var discontinuity = playlist.discontinuitySequence + i + 1;
|
14375 | var mediaIndexDiff = segmentIndex - segmentInfo.mediaIndex;
|
14376 | var accuracy = Math.abs(mediaIndexDiff);
|
14377 |
|
14378 | if (!this.discontinuities[discontinuity] || this.discontinuities[discontinuity].accuracy > accuracy) {
|
14379 | var time = void 0;
|
14380 |
|
14381 | if (mediaIndexDiff < 0) {
|
14382 | time = segment.start - sumDurations(playlist, segmentInfo.mediaIndex, segmentIndex);
|
14383 | } else {
|
14384 | time = segment.end + sumDurations(playlist, segmentInfo.mediaIndex + 1, segmentIndex);
|
14385 | }
|
14386 |
|
14387 | this.discontinuities[discontinuity] = {
|
14388 | time: time,
|
14389 | accuracy: accuracy
|
14390 | };
|
14391 | }
|
14392 | }
|
14393 | }
|
14394 | }
|
14395 | }, {
|
14396 | key: 'dispose',
|
14397 | value: function dispose() {
|
14398 | this.trigger('dispose');
|
14399 | this.off();
|
14400 | }
|
14401 | }]);
|
14402 | return SyncController;
|
14403 | }(videojs.EventTarget);
|
14404 |
|
14405 | var Decrypter = new shimWorker("./decrypter-worker.worker.js", function (window, document$$1) {
|
14406 | var self = this;
|
14407 | var decrypterWorker = function () {
|
14408 |
|
14409 | |
14410 |
|
14411 |
|
14412 |
|
14413 |
|
14414 |
|
14415 |
|
14416 |
|
14417 | |
14418 |
|
14419 |
|
14420 |
|
14421 |
|
14422 |
|
14423 |
|
14424 | function unpad(padded) {
|
14425 | return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
|
14426 | }
|
14427 |
|
14428 | var classCallCheck = function classCallCheck(instance, Constructor) {
|
14429 | if (!(instance instanceof Constructor)) {
|
14430 | throw new TypeError("Cannot call a class as a function");
|
14431 | }
|
14432 | };
|
14433 |
|
14434 | var createClass = function () {
|
14435 | function defineProperties(target, props) {
|
14436 | for (var i = 0; i < props.length; i++) {
|
14437 | var descriptor = props[i];
|
14438 | descriptor.enumerable = descriptor.enumerable || false;
|
14439 | descriptor.configurable = true;
|
14440 | if ("value" in descriptor) descriptor.writable = true;
|
14441 | Object.defineProperty(target, descriptor.key, descriptor);
|
14442 | }
|
14443 | }
|
14444 |
|
14445 | return function (Constructor, protoProps, staticProps) {
|
14446 | if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
14447 | if (staticProps) defineProperties(Constructor, staticProps);
|
14448 | return Constructor;
|
14449 | };
|
14450 | }();
|
14451 |
|
14452 | var inherits = function inherits(subClass, superClass) {
|
14453 | if (typeof superClass !== "function" && superClass !== null) {
|
14454 | throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
14455 | }
|
14456 |
|
14457 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
14458 | constructor: {
|
14459 | value: subClass,
|
14460 | enumerable: false,
|
14461 | writable: true,
|
14462 | configurable: true
|
14463 | }
|
14464 | });
|
14465 | if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
14466 | };
|
14467 |
|
14468 | var possibleConstructorReturn = function possibleConstructorReturn(self, call) {
|
14469 | if (!self) {
|
14470 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
14471 | }
|
14472 |
|
14473 | return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
14474 | };
|
14475 |
|
14476 | |
14477 |
|
14478 |
|
14479 |
|
14480 |
|
14481 |
|
14482 |
|
14483 |
|
14484 |
|
14485 |
|
14486 |
|
14487 |
|
14488 |
|
14489 |
|
14490 |
|
14491 |
|
14492 |
|
14493 |
|
14494 |
|
14495 |
|
14496 |
|
14497 |
|
14498 |
|
14499 |
|
14500 |
|
14501 |
|
14502 |
|
14503 |
|
14504 |
|
14505 |
|
14506 |
|
14507 |
|
14508 |
|
14509 |
|
14510 |
|
14511 |
|
14512 |
|
14513 |
|
14514 |
|
14515 | |
14516 |
|
14517 |
|
14518 |
|
14519 |
|
14520 | var precompute = function precompute() {
|
14521 | var tables = [[[], [], [], [], []], [[], [], [], [], []]];
|
14522 | var encTable = tables[0];
|
14523 | var decTable = tables[1];
|
14524 | var sbox = encTable[4];
|
14525 | var sboxInv = decTable[4];
|
14526 | var i = void 0;
|
14527 | var x = void 0;
|
14528 | var xInv = void 0;
|
14529 | var d = [];
|
14530 | var th = [];
|
14531 | var x2 = void 0;
|
14532 | var x4 = void 0;
|
14533 | var x8 = void 0;
|
14534 | var s = void 0;
|
14535 | var tEnc = void 0;
|
14536 | var tDec = void 0;
|
14537 |
|
14538 |
|
14539 | for (i = 0; i < 256; i++) {
|
14540 | th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
|
14541 | }
|
14542 |
|
14543 | for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
|
14544 |
|
14545 | s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
|
14546 | s = s >> 8 ^ s & 255 ^ 99;
|
14547 | sbox[x] = s;
|
14548 | sboxInv[s] = x;
|
14549 |
|
14550 |
|
14551 | x8 = d[x4 = d[x2 = d[x]]];
|
14552 | tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
|
14553 | tEnc = d[s] * 0x101 ^ s * 0x1010100;
|
14554 |
|
14555 | for (i = 0; i < 4; i++) {
|
14556 | encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
|
14557 | decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
|
14558 | }
|
14559 | }
|
14560 |
|
14561 |
|
14562 | for (i = 0; i < 5; i++) {
|
14563 | encTable[i] = encTable[i].slice(0);
|
14564 | decTable[i] = decTable[i].slice(0);
|
14565 | }
|
14566 | return tables;
|
14567 | };
|
14568 | var aesTables = null;
|
14569 |
|
14570 | |
14571 |
|
14572 |
|
14573 |
|
14574 |
|
14575 |
|
14576 |
|
14577 |
|
14578 | var AES = function () {
|
14579 | function AES(key) {
|
14580 | classCallCheck(this, AES);
|
14581 |
|
14582 | |
14583 |
|
14584 |
|
14585 |
|
14586 |
|
14587 |
|
14588 |
|
14589 |
|
14590 |
|
14591 |
|
14592 |
|
14593 |
|
14594 |
|
14595 |
|
14596 | if (!aesTables) {
|
14597 | aesTables = precompute();
|
14598 | }
|
14599 |
|
14600 | 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()]];
|
14601 | var i = void 0;
|
14602 | var j = void 0;
|
14603 | var tmp = void 0;
|
14604 | var encKey = void 0;
|
14605 | var decKey = void 0;
|
14606 | var sbox = this._tables[0][4];
|
14607 | var decTable = this._tables[1];
|
14608 | var keyLen = key.length;
|
14609 | var rcon = 1;
|
14610 |
|
14611 | if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
|
14612 | throw new Error('Invalid aes key size');
|
14613 | }
|
14614 |
|
14615 | encKey = key.slice(0);
|
14616 | decKey = [];
|
14617 | this._key = [encKey, decKey];
|
14618 |
|
14619 |
|
14620 | for (i = keyLen; i < 4 * keyLen + 28; i++) {
|
14621 | tmp = encKey[i - 1];
|
14622 |
|
14623 |
|
14624 | if (i % keyLen === 0 || keyLen === 8 && i % keyLen === 4) {
|
14625 | tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
|
14626 |
|
14627 |
|
14628 | if (i % keyLen === 0) {
|
14629 | tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
|
14630 | rcon = rcon << 1 ^ (rcon >> 7) * 283;
|
14631 | }
|
14632 | }
|
14633 |
|
14634 | encKey[i] = encKey[i - keyLen] ^ tmp;
|
14635 | }
|
14636 |
|
14637 |
|
14638 | for (j = 0; i; j++, i--) {
|
14639 | tmp = encKey[j & 3 ? i : i - 4];
|
14640 | if (i <= 4 || j < 4) {
|
14641 | decKey[j] = tmp;
|
14642 | } else {
|
14643 | decKey[j] = decTable[0][sbox[tmp >>> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]];
|
14644 | }
|
14645 | }
|
14646 | }
|
14647 |
|
14648 | |
14649 |
|
14650 |
|
14651 |
|
14652 |
|
14653 |
|
14654 |
|
14655 |
|
14656 |
|
14657 |
|
14658 |
|
14659 |
|
14660 |
|
14661 |
|
14662 | AES.prototype.decrypt = function decrypt(encrypted0, encrypted1, encrypted2, encrypted3, out, offset) {
|
14663 | var key = this._key[1];
|
14664 |
|
14665 | var a = encrypted0 ^ key[0];
|
14666 | var b = encrypted3 ^ key[1];
|
14667 | var c = encrypted2 ^ key[2];
|
14668 | var d = encrypted1 ^ key[3];
|
14669 | var a2 = void 0;
|
14670 | var b2 = void 0;
|
14671 | var c2 = void 0;
|
14672 |
|
14673 |
|
14674 | var nInnerRounds = key.length / 4 - 2;
|
14675 | var i = void 0;
|
14676 | var kIndex = 4;
|
14677 | var table = this._tables[1];
|
14678 |
|
14679 |
|
14680 | var table0 = table[0];
|
14681 | var table1 = table[1];
|
14682 | var table2 = table[2];
|
14683 | var table3 = table[3];
|
14684 | var sbox = table[4];
|
14685 |
|
14686 |
|
14687 | for (i = 0; i < nInnerRounds; i++) {
|
14688 | a2 = table0[a >>> 24] ^ table1[b >> 16 & 255] ^ table2[c >> 8 & 255] ^ table3[d & 255] ^ key[kIndex];
|
14689 | b2 = table0[b >>> 24] ^ table1[c >> 16 & 255] ^ table2[d >> 8 & 255] ^ table3[a & 255] ^ key[kIndex + 1];
|
14690 | c2 = table0[c >>> 24] ^ table1[d >> 16 & 255] ^ table2[a >> 8 & 255] ^ table3[b & 255] ^ key[kIndex + 2];
|
14691 | d = table0[d >>> 24] ^ table1[a >> 16 & 255] ^ table2[b >> 8 & 255] ^ table3[c & 255] ^ key[kIndex + 3];
|
14692 | kIndex += 4;
|
14693 | a = a2;b = b2;c = c2;
|
14694 | }
|
14695 |
|
14696 |
|
14697 | for (i = 0; i < 4; i++) {
|
14698 | out[(3 & -i) + offset] = sbox[a >>> 24] << 24 ^ sbox[b >> 16 & 255] << 16 ^ sbox[c >> 8 & 255] << 8 ^ sbox[d & 255] ^ key[kIndex++];
|
14699 | a2 = a;a = b;b = c;c = d;d = a2;
|
14700 | }
|
14701 | };
|
14702 |
|
14703 | return AES;
|
14704 | }();
|
14705 |
|
14706 | |
14707 |
|
14708 |
|
14709 | |
14710 |
|
14711 |
|
14712 |
|
14713 |
|
14714 | var Stream = function () {
|
14715 | function Stream() {
|
14716 | classCallCheck(this, Stream);
|
14717 |
|
14718 | this.listeners = {};
|
14719 | }
|
14720 |
|
14721 | |
14722 |
|
14723 |
|
14724 |
|
14725 |
|
14726 |
|
14727 |
|
14728 |
|
14729 | Stream.prototype.on = function on(type, listener) {
|
14730 | if (!this.listeners[type]) {
|
14731 | this.listeners[type] = [];
|
14732 | }
|
14733 | this.listeners[type].push(listener);
|
14734 | };
|
14735 |
|
14736 | |
14737 |
|
14738 |
|
14739 |
|
14740 |
|
14741 |
|
14742 |
|
14743 |
|
14744 |
|
14745 | Stream.prototype.off = function off(type, listener) {
|
14746 | if (!this.listeners[type]) {
|
14747 | return false;
|
14748 | }
|
14749 |
|
14750 | var index = this.listeners[type].indexOf(listener);
|
14751 |
|
14752 | this.listeners[type].splice(index, 1);
|
14753 | return index > -1;
|
14754 | };
|
14755 |
|
14756 | |
14757 |
|
14758 |
|
14759 |
|
14760 |
|
14761 |
|
14762 |
|
14763 | Stream.prototype.trigger = function trigger(type) {
|
14764 | var callbacks = this.listeners[type];
|
14765 |
|
14766 | if (!callbacks) {
|
14767 | return;
|
14768 | }
|
14769 |
|
14770 |
|
14771 |
|
14772 |
|
14773 |
|
14774 | if (arguments.length === 2) {
|
14775 | var length = callbacks.length;
|
14776 |
|
14777 | for (var i = 0; i < length; ++i) {
|
14778 | callbacks[i].call(this, arguments[1]);
|
14779 | }
|
14780 | } else {
|
14781 | var args = Array.prototype.slice.call(arguments, 1);
|
14782 | var _length = callbacks.length;
|
14783 |
|
14784 | for (var _i = 0; _i < _length; ++_i) {
|
14785 | callbacks[_i].apply(this, args);
|
14786 | }
|
14787 | }
|
14788 | };
|
14789 |
|
14790 | |
14791 |
|
14792 |
|
14793 |
|
14794 | Stream.prototype.dispose = function dispose() {
|
14795 | this.listeners = {};
|
14796 | };
|
14797 | |
14798 |
|
14799 |
|
14800 |
|
14801 |
|
14802 |
|
14803 |
|
14804 |
|
14805 |
|
14806 | Stream.prototype.pipe = function pipe(destination) {
|
14807 | this.on('data', function (data) {
|
14808 | destination.push(data);
|
14809 | });
|
14810 | };
|
14811 |
|
14812 | return Stream;
|
14813 | }();
|
14814 |
|
14815 | |
14816 |
|
14817 |
|
14818 | |
14819 |
|
14820 |
|
14821 |
|
14822 |
|
14823 |
|
14824 |
|
14825 |
|
14826 | var AsyncStream = function (_Stream) {
|
14827 | inherits(AsyncStream, _Stream);
|
14828 |
|
14829 | function AsyncStream() {
|
14830 | classCallCheck(this, AsyncStream);
|
14831 |
|
14832 | var _this = possibleConstructorReturn(this, _Stream.call(this, Stream));
|
14833 |
|
14834 | _this.jobs = [];
|
14835 | _this.delay = 1;
|
14836 | _this.timeout_ = null;
|
14837 | return _this;
|
14838 | }
|
14839 |
|
14840 | |
14841 |
|
14842 |
|
14843 |
|
14844 |
|
14845 |
|
14846 | AsyncStream.prototype.processJob_ = function processJob_() {
|
14847 | this.jobs.shift()();
|
14848 | if (this.jobs.length) {
|
14849 | this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
|
14850 | } else {
|
14851 | this.timeout_ = null;
|
14852 | }
|
14853 | };
|
14854 |
|
14855 | |
14856 |
|
14857 |
|
14858 |
|
14859 |
|
14860 |
|
14861 | AsyncStream.prototype.push = function push(job) {
|
14862 | this.jobs.push(job);
|
14863 | if (!this.timeout_) {
|
14864 | this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
|
14865 | }
|
14866 | };
|
14867 |
|
14868 | return AsyncStream;
|
14869 | }(Stream);
|
14870 |
|
14871 | |
14872 |
|
14873 |
|
14874 |
|
14875 |
|
14876 |
|
14877 |
|
14878 | |
14879 |
|
14880 |
|
14881 |
|
14882 | var ntoh = function ntoh(word) {
|
14883 | return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
|
14884 | };
|
14885 |
|
14886 | |
14887 |
|
14888 |
|
14889 |
|
14890 |
|
14891 |
|
14892 |
|
14893 |
|
14894 |
|
14895 |
|
14896 |
|
14897 |
|
14898 |
|
14899 | var decrypt = function decrypt(encrypted, key, initVector) {
|
14900 |
|
14901 | var encrypted32 = new Int32Array(encrypted.buffer, encrypted.byteOffset, encrypted.byteLength >> 2);
|
14902 |
|
14903 | var decipher = new AES(Array.prototype.slice.call(key));
|
14904 |
|
14905 |
|
14906 | var decrypted = new Uint8Array(encrypted.byteLength);
|
14907 | var decrypted32 = new Int32Array(decrypted.buffer);
|
14908 |
|
14909 |
|
14910 |
|
14911 | var init0 = void 0;
|
14912 | var init1 = void 0;
|
14913 | var init2 = void 0;
|
14914 | var init3 = void 0;
|
14915 | var encrypted0 = void 0;
|
14916 | var encrypted1 = void 0;
|
14917 | var encrypted2 = void 0;
|
14918 | var encrypted3 = void 0;
|
14919 |
|
14920 |
|
14921 | var wordIx = void 0;
|
14922 |
|
14923 |
|
14924 |
|
14925 | init0 = initVector[0];
|
14926 | init1 = initVector[1];
|
14927 | init2 = initVector[2];
|
14928 | init3 = initVector[3];
|
14929 |
|
14930 |
|
14931 |
|
14932 | for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
|
14933 |
|
14934 |
|
14935 | encrypted0 = ntoh(encrypted32[wordIx]);
|
14936 | encrypted1 = ntoh(encrypted32[wordIx + 1]);
|
14937 | encrypted2 = ntoh(encrypted32[wordIx + 2]);
|
14938 | encrypted3 = ntoh(encrypted32[wordIx + 3]);
|
14939 |
|
14940 |
|
14941 | decipher.decrypt(encrypted0, encrypted1, encrypted2, encrypted3, decrypted32, wordIx);
|
14942 |
|
14943 |
|
14944 |
|
14945 | decrypted32[wordIx] = ntoh(decrypted32[wordIx] ^ init0);
|
14946 | decrypted32[wordIx + 1] = ntoh(decrypted32[wordIx + 1] ^ init1);
|
14947 | decrypted32[wordIx + 2] = ntoh(decrypted32[wordIx + 2] ^ init2);
|
14948 | decrypted32[wordIx + 3] = ntoh(decrypted32[wordIx + 3] ^ init3);
|
14949 |
|
14950 |
|
14951 | init0 = encrypted0;
|
14952 | init1 = encrypted1;
|
14953 | init2 = encrypted2;
|
14954 | init3 = encrypted3;
|
14955 | }
|
14956 |
|
14957 | return decrypted;
|
14958 | };
|
14959 |
|
14960 | |
14961 |
|
14962 |
|
14963 |
|
14964 |
|
14965 |
|
14966 |
|
14967 |
|
14968 |
|
14969 |
|
14970 |
|
14971 |
|
14972 | var Decrypter = function () {
|
14973 | function Decrypter(encrypted, key, initVector, done) {
|
14974 | classCallCheck(this, Decrypter);
|
14975 |
|
14976 | var step = Decrypter.STEP;
|
14977 | var encrypted32 = new Int32Array(encrypted.buffer);
|
14978 | var decrypted = new Uint8Array(encrypted.byteLength);
|
14979 | var i = 0;
|
14980 |
|
14981 | this.asyncStream_ = new AsyncStream();
|
14982 |
|
14983 |
|
14984 | this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
|
14985 | for (i = step; i < encrypted32.length; i += step) {
|
14986 | initVector = new Uint32Array([ntoh(encrypted32[i - 4]), ntoh(encrypted32[i - 3]), ntoh(encrypted32[i - 2]), ntoh(encrypted32[i - 1])]);
|
14987 | this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
|
14988 | }
|
14989 |
|
14990 | this.asyncStream_.push(function () {
|
14991 |
|
14992 | done(null, unpad(decrypted));
|
14993 | });
|
14994 | }
|
14995 |
|
14996 | |
14997 |
|
14998 |
|
14999 |
|
15000 |
|
15001 |
|
15002 | |
15003 |
|
15004 |
|
15005 | Decrypter.prototype.decryptChunk_ = function decryptChunk_(encrypted, key, initVector, decrypted) {
|
15006 | return function () {
|
15007 | var bytes = decrypt(encrypted, key, initVector);
|
15008 |
|
15009 | decrypted.set(bytes, encrypted.byteOffset);
|
15010 | };
|
15011 | };
|
15012 |
|
15013 | createClass(Decrypter, null, [{
|
15014 | key: 'STEP',
|
15015 | get: function get$$1() {
|
15016 |
|
15017 | return 32000;
|
15018 | }
|
15019 | }]);
|
15020 | return Decrypter;
|
15021 | }();
|
15022 |
|
15023 | |
15024 |
|
15025 |
|
15026 |
|
15027 | |
15028 |
|
15029 |
|
15030 |
|
15031 |
|
15032 |
|
15033 |
|
15034 |
|
15035 |
|
15036 |
|
15037 | var createTransferableMessage = function createTransferableMessage(message) {
|
15038 | var transferable = {};
|
15039 |
|
15040 | Object.keys(message).forEach(function (key) {
|
15041 | var value = message[key];
|
15042 |
|
15043 | if (ArrayBuffer.isView(value)) {
|
15044 | transferable[key] = {
|
15045 | bytes: value.buffer,
|
15046 | byteOffset: value.byteOffset,
|
15047 | byteLength: value.byteLength
|
15048 | };
|
15049 | } else {
|
15050 | transferable[key] = value;
|
15051 | }
|
15052 | });
|
15053 |
|
15054 | return transferable;
|
15055 | };
|
15056 |
|
15057 | |
15058 |
|
15059 |
|
15060 |
|
15061 |
|
15062 |
|
15063 |
|
15064 |
|
15065 | var DecrypterWorker = function DecrypterWorker(self) {
|
15066 | self.onmessage = function (event) {
|
15067 | var data = event.data;
|
15068 | var encrypted = new Uint8Array(data.encrypted.bytes, data.encrypted.byteOffset, data.encrypted.byteLength);
|
15069 | var key = new Uint32Array(data.key.bytes, data.key.byteOffset, data.key.byteLength / 4);
|
15070 | var iv = new Uint32Array(data.iv.bytes, data.iv.byteOffset, data.iv.byteLength / 4);
|
15071 |
|
15072 |
|
15073 | new Decrypter(encrypted, key, iv, function (err, bytes) {
|
15074 | self.postMessage(createTransferableMessage({
|
15075 | source: data.source,
|
15076 | decrypted: bytes
|
15077 | }), [bytes.buffer]);
|
15078 | });
|
15079 |
|
15080 | };
|
15081 | };
|
15082 |
|
15083 | var decrypterWorker = new DecrypterWorker(self);
|
15084 |
|
15085 | return decrypterWorker;
|
15086 | }();
|
15087 | });
|
15088 |
|
15089 |
|
15090 |
|
15091 |
|
15092 |
|
15093 |
|
15094 | var audioTrackKind_ = function audioTrackKind_(properties) {
|
15095 | var kind = properties.default ? 'main' : 'alternative';
|
15096 |
|
15097 | if (properties.characteristics && properties.characteristics.indexOf('public.accessibility.describes-video') >= 0) {
|
15098 | kind = 'main-desc';
|
15099 | }
|
15100 |
|
15101 | return kind;
|
15102 | };
|
15103 |
|
15104 |
|
15105 |
|
15106 |
|
15107 |
|
15108 |
|
15109 |
|
15110 |
|
15111 |
|
15112 |
|
15113 | var stopLoaders = function stopLoaders(segmentLoader, mediaType) {
|
15114 | segmentLoader.abort();
|
15115 | segmentLoader.pause();
|
15116 |
|
15117 | if (mediaType && mediaType.activePlaylistLoader) {
|
15118 | mediaType.activePlaylistLoader.pause();
|
15119 | mediaType.activePlaylistLoader = null;
|
15120 | }
|
15121 | };
|
15122 |
|
15123 |
|
15124 |
|
15125 |
|
15126 |
|
15127 |
|
15128 |
|
15129 |
|
15130 |
|
15131 |
|
15132 | var startLoaders = function startLoaders(playlistLoader, mediaType) {
|
15133 |
|
15134 |
|
15135 | mediaType.activePlaylistLoader = playlistLoader;
|
15136 | playlistLoader.load();
|
15137 | };
|
15138 |
|
15139 |
|
15140 |
|
15141 |
|
15142 |
|
15143 |
|
15144 |
|
15145 |
|
15146 |
|
15147 |
|
15148 |
|
15149 |
|
15150 |
|
15151 |
|
15152 |
|
15153 |
|
15154 | var onGroupChanged = function onGroupChanged(type, settings) {
|
15155 | return function () {
|
15156 | var _settings$segmentLoad = settings.segmentLoaders,
|
15157 | segmentLoader = _settings$segmentLoad[type],
|
15158 | mainSegmentLoader = _settings$segmentLoad.main,
|
15159 | mediaType = settings.mediaTypes[type];
|
15160 |
|
15161 | var activeTrack = mediaType.activeTrack();
|
15162 | var activeGroup = mediaType.activeGroup(activeTrack);
|
15163 | var previousActiveLoader = mediaType.activePlaylistLoader;
|
15164 |
|
15165 | stopLoaders(segmentLoader, mediaType);
|
15166 |
|
15167 | if (!activeGroup) {
|
15168 |
|
15169 | return;
|
15170 | }
|
15171 |
|
15172 | if (!activeGroup.playlistLoader) {
|
15173 | if (previousActiveLoader) {
|
15174 |
|
15175 |
|
15176 |
|
15177 |
|
15178 | mainSegmentLoader.resetEverything();
|
15179 | }
|
15180 | return;
|
15181 | }
|
15182 |
|
15183 |
|
15184 | segmentLoader.resyncLoader();
|
15185 |
|
15186 | startLoaders(activeGroup.playlistLoader, mediaType);
|
15187 | };
|
15188 | };
|
15189 |
|
15190 |
|
15191 |
|
15192 |
|
15193 |
|
15194 |
|
15195 |
|
15196 |
|
15197 |
|
15198 |
|
15199 |
|
15200 |
|
15201 |
|
15202 |
|
15203 |
|
15204 | var onTrackChanged = function onTrackChanged(type, settings) {
|
15205 | return function () {
|
15206 | var _settings$segmentLoad2 = settings.segmentLoaders,
|
15207 | segmentLoader = _settings$segmentLoad2[type],
|
15208 | mainSegmentLoader = _settings$segmentLoad2.main,
|
15209 | mediaType = settings.mediaTypes[type];
|
15210 |
|
15211 | var activeTrack = mediaType.activeTrack();
|
15212 | var activeGroup = mediaType.activeGroup(activeTrack);
|
15213 | var previousActiveLoader = mediaType.activePlaylistLoader;
|
15214 |
|
15215 | stopLoaders(segmentLoader, mediaType);
|
15216 |
|
15217 | if (!activeGroup) {
|
15218 |
|
15219 | return;
|
15220 | }
|
15221 |
|
15222 | if (!activeGroup.playlistLoader) {
|
15223 |
|
15224 |
|
15225 |
|
15226 | mainSegmentLoader.resetEverything();
|
15227 | return;
|
15228 | }
|
15229 |
|
15230 | if (previousActiveLoader === activeGroup.playlistLoader) {
|
15231 |
|
15232 |
|
15233 |
|
15234 | startLoaders(activeGroup.playlistLoader, mediaType);
|
15235 | return;
|
15236 | }
|
15237 |
|
15238 | if (segmentLoader.track) {
|
15239 |
|
15240 | segmentLoader.track(activeTrack);
|
15241 | }
|
15242 |
|
15243 |
|
15244 | segmentLoader.resetEverything();
|
15245 |
|
15246 | startLoaders(activeGroup.playlistLoader, mediaType);
|
15247 | };
|
15248 | };
|
15249 |
|
15250 | var onError = {
|
15251 | |
15252 |
|
15253 |
|
15254 |
|
15255 |
|
15256 |
|
15257 |
|
15258 |
|
15259 |
|
15260 |
|
15261 |
|
15262 |
|
15263 |
|
15264 | AUDIO: function AUDIO(type, settings) {
|
15265 | return function () {
|
15266 | var segmentLoader = settings.segmentLoaders[type],
|
15267 | mediaType = settings.mediaTypes[type],
|
15268 | blacklistCurrentPlaylist = settings.blacklistCurrentPlaylist;
|
15269 |
|
15270 |
|
15271 | stopLoaders(segmentLoader, mediaType);
|
15272 |
|
15273 |
|
15274 | var activeTrack = mediaType.activeTrack();
|
15275 | var activeGroup = mediaType.activeGroup();
|
15276 | var id = (activeGroup.filter(function (group) {
|
15277 | return group.default;
|
15278 | })[0] || activeGroup[0]).id;
|
15279 | var defaultTrack = mediaType.tracks[id];
|
15280 |
|
15281 | if (activeTrack === defaultTrack) {
|
15282 |
|
15283 |
|
15284 | blacklistCurrentPlaylist({
|
15285 | message: 'Problem encountered loading the default audio track.'
|
15286 | });
|
15287 | return;
|
15288 | }
|
15289 |
|
15290 | videojs.log.warn('Problem encountered loading the alternate audio track.' + 'Switching back to default.');
|
15291 |
|
15292 | for (var trackId in mediaType.tracks) {
|
15293 | mediaType.tracks[trackId].enabled = mediaType.tracks[trackId] === defaultTrack;
|
15294 | }
|
15295 |
|
15296 | mediaType.onTrackChanged();
|
15297 | };
|
15298 | },
|
15299 | |
15300 |
|
15301 |
|
15302 |
|
15303 |
|
15304 |
|
15305 |
|
15306 |
|
15307 |
|
15308 |
|
15309 |
|
15310 |
|
15311 | SUBTITLES: function SUBTITLES(type, settings) {
|
15312 | return function () {
|
15313 | var segmentLoader = settings.segmentLoaders[type],
|
15314 | mediaType = settings.mediaTypes[type];
|
15315 |
|
15316 |
|
15317 | videojs.log.warn('Problem encountered loading the subtitle track.' + 'Disabling subtitle track.');
|
15318 |
|
15319 | stopLoaders(segmentLoader, mediaType);
|
15320 |
|
15321 | var track = mediaType.activeTrack();
|
15322 |
|
15323 | if (track) {
|
15324 | track.mode = 'disabled';
|
15325 | }
|
15326 |
|
15327 | mediaType.onTrackChanged();
|
15328 | };
|
15329 | }
|
15330 | };
|
15331 |
|
15332 | var setupListeners = {
|
15333 | |
15334 |
|
15335 |
|
15336 |
|
15337 |
|
15338 |
|
15339 |
|
15340 |
|
15341 |
|
15342 |
|
15343 |
|
15344 | AUDIO: function AUDIO(type, playlistLoader, settings) {
|
15345 | if (!playlistLoader) {
|
15346 |
|
15347 | return;
|
15348 | }
|
15349 |
|
15350 | var tech = settings.tech,
|
15351 | requestOptions = settings.requestOptions,
|
15352 | segmentLoader = settings.segmentLoaders[type];
|
15353 |
|
15354 |
|
15355 | playlistLoader.on('loadedmetadata', function () {
|
15356 | var media = playlistLoader.media();
|
15357 |
|
15358 | segmentLoader.playlist(media, requestOptions);
|
15359 |
|
15360 |
|
15361 |
|
15362 | if (!tech.paused() || media.endList && tech.preload() !== 'none') {
|
15363 | segmentLoader.load();
|
15364 | }
|
15365 | });
|
15366 |
|
15367 | playlistLoader.on('loadedplaylist', function () {
|
15368 | segmentLoader.playlist(playlistLoader.media(), requestOptions);
|
15369 |
|
15370 |
|
15371 | if (!tech.paused()) {
|
15372 | segmentLoader.load();
|
15373 | }
|
15374 | });
|
15375 |
|
15376 | playlistLoader.on('error', onError[type](type, settings));
|
15377 | },
|
15378 | |
15379 |
|
15380 |
|
15381 |
|
15382 |
|
15383 |
|
15384 |
|
15385 |
|
15386 |
|
15387 |
|
15388 |
|
15389 | SUBTITLES: function SUBTITLES(type, playlistLoader, settings) {
|
15390 | var tech = settings.tech,
|
15391 | requestOptions = settings.requestOptions,
|
15392 | segmentLoader = settings.segmentLoaders[type],
|
15393 | mediaType = settings.mediaTypes[type];
|
15394 |
|
15395 |
|
15396 | playlistLoader.on('loadedmetadata', function () {
|
15397 | var media = playlistLoader.media();
|
15398 |
|
15399 | segmentLoader.playlist(media, requestOptions);
|
15400 | segmentLoader.track(mediaType.activeTrack());
|
15401 |
|
15402 |
|
15403 |
|
15404 | if (!tech.paused() || media.endList && tech.preload() !== 'none') {
|
15405 | segmentLoader.load();
|
15406 | }
|
15407 | });
|
15408 |
|
15409 | playlistLoader.on('loadedplaylist', function () {
|
15410 | segmentLoader.playlist(playlistLoader.media(), requestOptions);
|
15411 |
|
15412 |
|
15413 | if (!tech.paused()) {
|
15414 | segmentLoader.load();
|
15415 | }
|
15416 | });
|
15417 |
|
15418 | playlistLoader.on('error', onError[type](type, settings));
|
15419 | }
|
15420 | };
|
15421 |
|
15422 | var initialize = {
|
15423 | |
15424 |
|
15425 |
|
15426 |
|
15427 |
|
15428 |
|
15429 |
|
15430 |
|
15431 |
|
15432 | 'AUDIO': function AUDIO(type, settings) {
|
15433 | var hls = settings.hls,
|
15434 | sourceType = settings.sourceType,
|
15435 | segmentLoader = settings.segmentLoaders[type],
|
15436 | requestOptions = settings.requestOptions,
|
15437 | mediaGroups = settings.master.mediaGroups,
|
15438 | _settings$mediaTypes$ = settings.mediaTypes[type],
|
15439 | groups = _settings$mediaTypes$.groups,
|
15440 | tracks = _settings$mediaTypes$.tracks,
|
15441 | masterPlaylistLoader = settings.masterPlaylistLoader;
|
15442 |
|
15443 |
|
15444 |
|
15445 | if (!mediaGroups[type] || Object.keys(mediaGroups[type]).length === 0) {
|
15446 | mediaGroups[type] = { main: { default: { default: true } } };
|
15447 | }
|
15448 |
|
15449 | for (var groupId in mediaGroups[type]) {
|
15450 | if (!groups[groupId]) {
|
15451 | groups[groupId] = [];
|
15452 | }
|
15453 |
|
15454 |
|
15455 |
|
15456 |
|
15457 | for (var variantLabel in mediaGroups[type][groupId]) {
|
15458 | var properties = mediaGroups[type][groupId][variantLabel];
|
15459 | var playlistLoader = void 0;
|
15460 |
|
15461 | if (properties.resolvedUri) {
|
15462 | playlistLoader = new PlaylistLoader(properties.resolvedUri, hls, requestOptions);
|
15463 | } else if (properties.playlists && sourceType === 'dash') {
|
15464 | playlistLoader = new DashPlaylistLoader(properties.playlists[0], hls, requestOptions, masterPlaylistLoader);
|
15465 | } else {
|
15466 |
|
15467 |
|
15468 | playlistLoader = null;
|
15469 | }
|
15470 |
|
15471 | properties = videojs.mergeOptions({ id: variantLabel, playlistLoader: playlistLoader }, properties);
|
15472 |
|
15473 | setupListeners[type](type, properties.playlistLoader, settings);
|
15474 |
|
15475 | groups[groupId].push(properties);
|
15476 |
|
15477 | if (typeof tracks[variantLabel] === 'undefined') {
|
15478 | var track = new videojs.AudioTrack({
|
15479 | id: variantLabel,
|
15480 | kind: audioTrackKind_(properties),
|
15481 | enabled: false,
|
15482 | language: properties.language,
|
15483 | default: properties.default,
|
15484 | label: variantLabel
|
15485 | });
|
15486 |
|
15487 | tracks[variantLabel] = track;
|
15488 | }
|
15489 | }
|
15490 | }
|
15491 |
|
15492 |
|
15493 | segmentLoader.on('error', onError[type](type, settings));
|
15494 | },
|
15495 | |
15496 |
|
15497 |
|
15498 |
|
15499 |
|
15500 |
|
15501 |
|
15502 |
|
15503 |
|
15504 | 'SUBTITLES': function SUBTITLES(type, settings) {
|
15505 | var tech = settings.tech,
|
15506 | hls = settings.hls,
|
15507 | sourceType = settings.sourceType,
|
15508 | segmentLoader = settings.segmentLoaders[type],
|
15509 | requestOptions = settings.requestOptions,
|
15510 | mediaGroups = settings.master.mediaGroups,
|
15511 | _settings$mediaTypes$2 = settings.mediaTypes[type],
|
15512 | groups = _settings$mediaTypes$2.groups,
|
15513 | tracks = _settings$mediaTypes$2.tracks,
|
15514 | masterPlaylistLoader = settings.masterPlaylistLoader;
|
15515 |
|
15516 |
|
15517 | for (var groupId in mediaGroups[type]) {
|
15518 | if (!groups[groupId]) {
|
15519 | groups[groupId] = [];
|
15520 | }
|
15521 |
|
15522 | for (var variantLabel in mediaGroups[type][groupId]) {
|
15523 | if (mediaGroups[type][groupId][variantLabel].forced) {
|
15524 |
|
15525 |
|
15526 |
|
15527 |
|
15528 |
|
15529 |
|
15530 |
|
15531 |
|
15532 | continue;
|
15533 | }
|
15534 |
|
15535 | var properties = mediaGroups[type][groupId][variantLabel];
|
15536 |
|
15537 | var playlistLoader = void 0;
|
15538 |
|
15539 | if (sourceType === 'hls') {
|
15540 | playlistLoader = new PlaylistLoader(properties.resolvedUri, hls, requestOptions);
|
15541 | } else if (sourceType === 'dash') {
|
15542 | playlistLoader = new DashPlaylistLoader(properties.playlists[0], hls, requestOptions, masterPlaylistLoader);
|
15543 | }
|
15544 |
|
15545 | properties = videojs.mergeOptions({
|
15546 | id: variantLabel,
|
15547 | playlistLoader: playlistLoader
|
15548 | }, properties);
|
15549 |
|
15550 | setupListeners[type](type, properties.playlistLoader, settings);
|
15551 |
|
15552 | groups[groupId].push(properties);
|
15553 |
|
15554 | if (typeof tracks[variantLabel] === 'undefined') {
|
15555 | var track = tech.addRemoteTextTrack({
|
15556 | id: variantLabel,
|
15557 | kind: 'subtitles',
|
15558 | default: properties.default && properties.autoselect,
|
15559 | language: properties.language,
|
15560 | label: variantLabel
|
15561 | }, false).track;
|
15562 |
|
15563 | tracks[variantLabel] = track;
|
15564 | }
|
15565 | }
|
15566 | }
|
15567 |
|
15568 |
|
15569 | segmentLoader.on('error', onError[type](type, settings));
|
15570 | },
|
15571 | |
15572 |
|
15573 |
|
15574 |
|
15575 |
|
15576 |
|
15577 |
|
15578 |
|
15579 |
|
15580 | 'CLOSED-CAPTIONS': function CLOSEDCAPTIONS(type, settings) {
|
15581 | var tech = settings.tech,
|
15582 | mediaGroups = settings.master.mediaGroups,
|
15583 | _settings$mediaTypes$3 = settings.mediaTypes[type],
|
15584 | groups = _settings$mediaTypes$3.groups,
|
15585 | tracks = _settings$mediaTypes$3.tracks;
|
15586 |
|
15587 |
|
15588 | for (var groupId in mediaGroups[type]) {
|
15589 | if (!groups[groupId]) {
|
15590 | groups[groupId] = [];
|
15591 | }
|
15592 |
|
15593 | for (var variantLabel in mediaGroups[type][groupId]) {
|
15594 | var properties = mediaGroups[type][groupId][variantLabel];
|
15595 |
|
15596 |
|
15597 |
|
15598 | if (!properties.instreamId.match(/CC\d/)) {
|
15599 | continue;
|
15600 | }
|
15601 |
|
15602 |
|
15603 |
|
15604 | groups[groupId].push(videojs.mergeOptions({ id: variantLabel }, properties));
|
15605 |
|
15606 | if (typeof tracks[variantLabel] === 'undefined') {
|
15607 | var track = tech.addRemoteTextTrack({
|
15608 | id: properties.instreamId,
|
15609 | kind: 'captions',
|
15610 | default: properties.default && properties.autoselect,
|
15611 | language: properties.language,
|
15612 | label: variantLabel
|
15613 | }, false).track;
|
15614 |
|
15615 | tracks[variantLabel] = track;
|
15616 | }
|
15617 | }
|
15618 | }
|
15619 | }
|
15620 | };
|
15621 |
|
15622 |
|
15623 |
|
15624 |
|
15625 |
|
15626 |
|
15627 |
|
15628 |
|
15629 |
|
15630 |
|
15631 |
|
15632 |
|
15633 |
|
15634 |
|
15635 |
|
15636 | var activeGroup = function activeGroup(type, settings) {
|
15637 | return function (track) {
|
15638 | var masterPlaylistLoader = settings.masterPlaylistLoader,
|
15639 | groups = settings.mediaTypes[type].groups;
|
15640 |
|
15641 |
|
15642 | var media = masterPlaylistLoader.media();
|
15643 |
|
15644 | if (!media) {
|
15645 | return null;
|
15646 | }
|
15647 |
|
15648 | var variants = null;
|
15649 |
|
15650 | if (media.attributes[type]) {
|
15651 | variants = groups[media.attributes[type]];
|
15652 | }
|
15653 |
|
15654 | variants = variants || groups.main;
|
15655 |
|
15656 | if (typeof track === 'undefined') {
|
15657 | return variants;
|
15658 | }
|
15659 |
|
15660 | if (track === null) {
|
15661 |
|
15662 |
|
15663 | return null;
|
15664 | }
|
15665 |
|
15666 | return variants.filter(function (props) {
|
15667 | return props.id === track.id;
|
15668 | })[0] || null;
|
15669 | };
|
15670 | };
|
15671 |
|
15672 | var activeTrack = {
|
15673 | |
15674 |
|
15675 |
|
15676 |
|
15677 |
|
15678 |
|
15679 |
|
15680 |
|
15681 |
|
15682 |
|
15683 |
|
15684 |
|
15685 | AUDIO: function AUDIO(type, settings) {
|
15686 | return function () {
|
15687 | var tracks = settings.mediaTypes[type].tracks;
|
15688 |
|
15689 |
|
15690 | for (var id in tracks) {
|
15691 | if (tracks[id].enabled) {
|
15692 | return tracks[id];
|
15693 | }
|
15694 | }
|
15695 |
|
15696 | return null;
|
15697 | };
|
15698 | },
|
15699 | |
15700 |
|
15701 |
|
15702 |
|
15703 |
|
15704 |
|
15705 |
|
15706 |
|
15707 |
|
15708 |
|
15709 |
|
15710 |
|
15711 | SUBTITLES: function SUBTITLES(type, settings) {
|
15712 | return function () {
|
15713 | var tracks = settings.mediaTypes[type].tracks;
|
15714 |
|
15715 |
|
15716 | for (var id in tracks) {
|
15717 | if (tracks[id].mode === 'showing' || tracks[id].mode === 'hidden') {
|
15718 | return tracks[id];
|
15719 | }
|
15720 | }
|
15721 |
|
15722 | return null;
|
15723 | };
|
15724 | }
|
15725 | };
|
15726 |
|
15727 |
|
15728 |
|
15729 |
|
15730 |
|
15731 |
|
15732 |
|
15733 |
|
15734 |
|
15735 |
|
15736 |
|
15737 |
|
15738 |
|
15739 |
|
15740 |
|
15741 |
|
15742 |
|
15743 |
|
15744 |
|
15745 |
|
15746 |
|
15747 |
|
15748 |
|
15749 |
|
15750 |
|
15751 |
|
15752 |
|
15753 |
|
15754 |
|
15755 | var setupMediaGroups = function setupMediaGroups(settings) {
|
15756 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(function (type) {
|
15757 | initialize[type](type, settings);
|
15758 | });
|
15759 |
|
15760 | var mediaTypes = settings.mediaTypes,
|
15761 | masterPlaylistLoader = settings.masterPlaylistLoader,
|
15762 | tech = settings.tech,
|
15763 | hls = settings.hls;
|
15764 |
|
15765 |
|
15766 |
|
15767 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
15768 | mediaTypes[type].activeGroup = activeGroup(type, settings);
|
15769 | mediaTypes[type].activeTrack = activeTrack[type](type, settings);
|
15770 | mediaTypes[type].onGroupChanged = onGroupChanged(type, settings);
|
15771 | mediaTypes[type].onTrackChanged = onTrackChanged(type, settings);
|
15772 | });
|
15773 |
|
15774 |
|
15775 |
|
15776 | var audioGroup = mediaTypes.AUDIO.activeGroup();
|
15777 | var groupId = (audioGroup.filter(function (group) {
|
15778 | return group.default;
|
15779 | })[0] || audioGroup[0]).id;
|
15780 |
|
15781 | mediaTypes.AUDIO.tracks[groupId].enabled = true;
|
15782 | mediaTypes.AUDIO.onTrackChanged();
|
15783 |
|
15784 | masterPlaylistLoader.on('mediachange', function () {
|
15785 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
15786 | return mediaTypes[type].onGroupChanged();
|
15787 | });
|
15788 | });
|
15789 |
|
15790 |
|
15791 | var onAudioTrackChanged = function onAudioTrackChanged() {
|
15792 | mediaTypes.AUDIO.onTrackChanged();
|
15793 | tech.trigger({ type: 'usage', name: 'hls-audio-change' });
|
15794 | };
|
15795 |
|
15796 | tech.audioTracks().addEventListener('change', onAudioTrackChanged);
|
15797 | tech.remoteTextTracks().addEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
|
15798 |
|
15799 | hls.on('dispose', function () {
|
15800 | tech.audioTracks().removeEventListener('change', onAudioTrackChanged);
|
15801 | tech.remoteTextTracks().removeEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
|
15802 | });
|
15803 |
|
15804 |
|
15805 | tech.clearTracks('audio');
|
15806 |
|
15807 | for (var id in mediaTypes.AUDIO.tracks) {
|
15808 | tech.audioTracks().addTrack(mediaTypes.AUDIO.tracks[id]);
|
15809 | }
|
15810 | };
|
15811 |
|
15812 |
|
15813 |
|
15814 |
|
15815 |
|
15816 |
|
15817 |
|
15818 |
|
15819 |
|
15820 | var createMediaTypes = function createMediaTypes() {
|
15821 | var mediaTypes = {};
|
15822 |
|
15823 | ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(function (type) {
|
15824 | mediaTypes[type] = {
|
15825 | groups: {},
|
15826 | tracks: {},
|
15827 | activePlaylistLoader: null,
|
15828 | activeGroup: noop,
|
15829 | activeTrack: noop,
|
15830 | onGroupChanged: noop,
|
15831 | onTrackChanged: noop
|
15832 | };
|
15833 | });
|
15834 |
|
15835 | return mediaTypes;
|
15836 | };
|
15837 |
|
15838 |
|
15839 |
|
15840 |
|
15841 |
|
15842 | var ABORT_EARLY_BLACKLIST_SECONDS = 60 * 2;
|
15843 |
|
15844 | var Hls = void 0;
|
15845 |
|
15846 |
|
15847 |
|
15848 | var loaderStats = ['mediaRequests', 'mediaRequestsAborted', 'mediaRequestsTimedout', 'mediaRequestsErrored', 'mediaTransferDuration', 'mediaBytesTransferred'];
|
15849 | var sumLoaderStat = function sumLoaderStat(stat) {
|
15850 | return this.audioSegmentLoader_[stat] + this.mainSegmentLoader_[stat];
|
15851 | };
|
15852 | var shouldSwitchToMedia = function shouldSwitchToMedia(_ref) {
|
15853 | var currentPlaylist = _ref.currentPlaylist,
|
15854 | nextPlaylist = _ref.nextPlaylist,
|
15855 | forwardBuffer = _ref.forwardBuffer,
|
15856 | bufferLowWaterLine = _ref.bufferLowWaterLine,
|
15857 | duration$$1 = _ref.duration,
|
15858 | log = _ref.log;
|
15859 |
|
15860 |
|
15861 | if (!nextPlaylist) {
|
15862 | videojs.log.warn('We received no playlist to switch to. Please check your stream.');
|
15863 | return false;
|
15864 | }
|
15865 |
|
15866 |
|
15867 |
|
15868 |
|
15869 |
|
15870 | if (!currentPlaylist.endList) {
|
15871 | return true;
|
15872 | }
|
15873 |
|
15874 |
|
15875 |
|
15876 | if (duration$$1 < Config.MAX_BUFFER_LOW_WATER_LINE) {
|
15877 | return true;
|
15878 | }
|
15879 |
|
15880 |
|
15881 | if (nextPlaylist.attributes.BANDWIDTH < currentPlaylist.attributes.BANDWIDTH) {
|
15882 | return true;
|
15883 | }
|
15884 |
|
15885 |
|
15886 |
|
15887 | if (forwardBuffer >= bufferLowWaterLine) {
|
15888 | return true;
|
15889 | }
|
15890 |
|
15891 | return false;
|
15892 | };
|
15893 |
|
15894 |
|
15895 |
|
15896 |
|
15897 |
|
15898 |
|
15899 |
|
15900 |
|
15901 |
|
15902 |
|
15903 | var MasterPlaylistController = function (_videojs$EventTarget) {
|
15904 | inherits(MasterPlaylistController, _videojs$EventTarget);
|
15905 |
|
15906 | function MasterPlaylistController(options) {
|
15907 | classCallCheck(this, MasterPlaylistController);
|
15908 |
|
15909 | var _this = possibleConstructorReturn(this, (MasterPlaylistController.__proto__ || Object.getPrototypeOf(MasterPlaylistController)).call(this));
|
15910 |
|
15911 | var url = options.url,
|
15912 | handleManifestRedirects = options.handleManifestRedirects,
|
15913 | withCredentials = options.withCredentials,
|
15914 | tech = options.tech,
|
15915 | bandwidth = options.bandwidth,
|
15916 | externHls = options.externHls,
|
15917 | useCueTags = options.useCueTags,
|
15918 | blacklistDuration = options.blacklistDuration,
|
15919 | enableLowInitialPlaylist = options.enableLowInitialPlaylist,
|
15920 | cacheEncryptionKeys = options.cacheEncryptionKeys,
|
15921 | sourceType = options.sourceType;
|
15922 |
|
15923 |
|
15924 | if (!url) {
|
15925 | throw new Error('A non-empty playlist URL is required');
|
15926 | }
|
15927 |
|
15928 | Hls = externHls;
|
15929 |
|
15930 | _this.withCredentials = withCredentials;
|
15931 | _this.tech_ = tech;
|
15932 | _this.hls_ = tech.hls;
|
15933 | _this.sourceType_ = sourceType;
|
15934 | _this.useCueTags_ = useCueTags;
|
15935 | _this.blacklistDuration = blacklistDuration;
|
15936 | _this.enableLowInitialPlaylist = enableLowInitialPlaylist;
|
15937 | if (_this.useCueTags_) {
|
15938 | _this.cueTagsTrack_ = _this.tech_.addTextTrack('metadata', 'ad-cues');
|
15939 | _this.cueTagsTrack_.inBandMetadataTrackDispatchType = '';
|
15940 | }
|
15941 |
|
15942 | _this.requestOptions_ = {
|
15943 | withCredentials: withCredentials,
|
15944 | handleManifestRedirects: handleManifestRedirects,
|
15945 | timeout: null
|
15946 | };
|
15947 |
|
15948 | _this.mediaTypes_ = createMediaTypes();
|
15949 |
|
15950 | _this.mediaSource = new videojs.MediaSource();
|
15951 |
|
15952 |
|
15953 | _this.mediaSource.addEventListener('sourceopen', _this.handleSourceOpen_.bind(_this));
|
15954 |
|
15955 | _this.seekable_ = videojs.createTimeRanges();
|
15956 | _this.hasPlayed_ = false;
|
15957 |
|
15958 | _this.syncController_ = new SyncController(options);
|
15959 | _this.segmentMetadataTrack_ = tech.addRemoteTextTrack({
|
15960 | kind: 'metadata',
|
15961 | label: 'segment-metadata'
|
15962 | }, false).track;
|
15963 |
|
15964 | _this.decrypter_ = new Decrypter();
|
15965 | _this.inbandTextTracks_ = {};
|
15966 |
|
15967 | var segmentLoaderSettings = {
|
15968 | hls: _this.hls_,
|
15969 | mediaSource: _this.mediaSource,
|
15970 | currentTime: _this.tech_.currentTime.bind(_this.tech_),
|
15971 | seekable: function seekable$$1() {
|
15972 | return _this.seekable();
|
15973 | },
|
15974 | seeking: function seeking() {
|
15975 | return _this.tech_.seeking();
|
15976 | },
|
15977 | duration: function duration$$1() {
|
15978 | return _this.mediaSource.duration;
|
15979 | },
|
15980 | hasPlayed: function hasPlayed() {
|
15981 | return _this.hasPlayed_;
|
15982 | },
|
15983 | goalBufferLength: function goalBufferLength() {
|
15984 | return _this.goalBufferLength();
|
15985 | },
|
15986 | bandwidth: bandwidth,
|
15987 | syncController: _this.syncController_,
|
15988 | decrypter: _this.decrypter_,
|
15989 | sourceType: _this.sourceType_,
|
15990 | inbandTextTracks: _this.inbandTextTracks_,
|
15991 | cacheEncryptionKeys: cacheEncryptionKeys
|
15992 | };
|
15993 |
|
15994 | _this.masterPlaylistLoader_ = _this.sourceType_ === 'dash' ? new DashPlaylistLoader(url, _this.hls_, _this.requestOptions_) : new PlaylistLoader(url, _this.hls_, _this.requestOptions_);
|
15995 | _this.setupMasterPlaylistLoaderListeners_();
|
15996 |
|
15997 |
|
15998 |
|
15999 | _this.mainSegmentLoader_ = new SegmentLoader(videojs.mergeOptions(segmentLoaderSettings, {
|
16000 | segmentMetadataTrack: _this.segmentMetadataTrack_,
|
16001 | loaderType: 'main'
|
16002 | }), options);
|
16003 |
|
16004 |
|
16005 | _this.audioSegmentLoader_ = new SegmentLoader(videojs.mergeOptions(segmentLoaderSettings, {
|
16006 | loaderType: 'audio'
|
16007 | }), options);
|
16008 |
|
16009 | _this.subtitleSegmentLoader_ = new VTTSegmentLoader(videojs.mergeOptions(segmentLoaderSettings, {
|
16010 | loaderType: 'vtt',
|
16011 | featuresNativeTextTracks: _this.tech_.featuresNativeTextTracks
|
16012 | }), options);
|
16013 |
|
16014 | _this.setupSegmentLoaderListeners_();
|
16015 |
|
16016 |
|
16017 | loaderStats.forEach(function (stat) {
|
16018 | _this[stat + '_'] = sumLoaderStat.bind(_this, stat);
|
16019 | });
|
16020 |
|
16021 | _this.logger_ = logger('MPC');
|
16022 |
|
16023 | _this.masterPlaylistLoader_.load();
|
16024 | return _this;
|
16025 | }
|
16026 |
|
16027 | |
16028 |
|
16029 |
|
16030 |
|
16031 |
|
16032 |
|
16033 |
|
16034 |
|
16035 | createClass(MasterPlaylistController, [{
|
16036 | key: 'setupMasterPlaylistLoaderListeners_',
|
16037 | value: function setupMasterPlaylistLoaderListeners_() {
|
16038 | var _this2 = this;
|
16039 |
|
16040 | this.masterPlaylistLoader_.on('loadedmetadata', function () {
|
16041 | var media = _this2.masterPlaylistLoader_.media();
|
16042 | var requestTimeout = media.targetDuration * 1.5 * 1000;
|
16043 |
|
16044 |
|
16045 |
|
16046 | if (isLowestEnabledRendition(_this2.masterPlaylistLoader_.master, _this2.masterPlaylistLoader_.media())) {
|
16047 | _this2.requestOptions_.timeout = 0;
|
16048 | } else {
|
16049 | _this2.requestOptions_.timeout = requestTimeout;
|
16050 | }
|
16051 |
|
16052 |
|
16053 |
|
16054 | if (media.endList && _this2.tech_.preload() !== 'none') {
|
16055 | _this2.mainSegmentLoader_.playlist(media, _this2.requestOptions_);
|
16056 | _this2.mainSegmentLoader_.load();
|
16057 | }
|
16058 |
|
16059 | setupMediaGroups({
|
16060 | sourceType: _this2.sourceType_,
|
16061 | segmentLoaders: {
|
16062 | AUDIO: _this2.audioSegmentLoader_,
|
16063 | SUBTITLES: _this2.subtitleSegmentLoader_,
|
16064 | main: _this2.mainSegmentLoader_
|
16065 | },
|
16066 | tech: _this2.tech_,
|
16067 | requestOptions: _this2.requestOptions_,
|
16068 | masterPlaylistLoader: _this2.masterPlaylistLoader_,
|
16069 | hls: _this2.hls_,
|
16070 | master: _this2.master(),
|
16071 | mediaTypes: _this2.mediaTypes_,
|
16072 | blacklistCurrentPlaylist: _this2.blacklistCurrentPlaylist.bind(_this2)
|
16073 | });
|
16074 |
|
16075 | _this2.triggerPresenceUsage_(_this2.master(), media);
|
16076 |
|
16077 | try {
|
16078 | _this2.setupSourceBuffers_();
|
16079 | } catch (e) {
|
16080 | videojs.log.warn('Failed to create SourceBuffers', e);
|
16081 | return _this2.mediaSource.endOfStream('decode');
|
16082 | }
|
16083 | _this2.setupFirstPlay();
|
16084 |
|
16085 | if (!_this2.mediaTypes_.AUDIO.activePlaylistLoader || _this2.mediaTypes_.AUDIO.activePlaylistLoader.media()) {
|
16086 | _this2.trigger('selectedinitialmedia');
|
16087 | } else {
|
16088 |
|
16089 |
|
16090 |
|
16091 | _this2.mediaTypes_.AUDIO.activePlaylistLoader.one('loadedmetadata', function () {
|
16092 | _this2.trigger('selectedinitialmedia');
|
16093 | });
|
16094 | }
|
16095 | });
|
16096 |
|
16097 | this.masterPlaylistLoader_.on('loadedplaylist', function () {
|
16098 | var updatedPlaylist = _this2.masterPlaylistLoader_.media();
|
16099 |
|
16100 | if (!updatedPlaylist) {
|
16101 |
|
16102 |
|
16103 | _this2.excludeUnsupportedVariants_();
|
16104 |
|
16105 | var selectedMedia = void 0;
|
16106 |
|
16107 | if (_this2.enableLowInitialPlaylist) {
|
16108 | selectedMedia = _this2.selectInitialPlaylist();
|
16109 | }
|
16110 |
|
16111 | if (!selectedMedia) {
|
16112 | selectedMedia = _this2.selectPlaylist();
|
16113 | }
|
16114 |
|
16115 | _this2.initialMedia_ = selectedMedia;
|
16116 | _this2.masterPlaylistLoader_.media(_this2.initialMedia_);
|
16117 | return;
|
16118 | }
|
16119 |
|
16120 | if (_this2.useCueTags_) {
|
16121 | _this2.updateAdCues_(updatedPlaylist);
|
16122 | }
|
16123 |
|
16124 |
|
16125 |
|
16126 |
|
16127 |
|
16128 | _this2.mainSegmentLoader_.playlist(updatedPlaylist, _this2.requestOptions_);
|
16129 | _this2.updateDuration();
|
16130 |
|
16131 |
|
16132 |
|
16133 |
|
16134 | if (!_this2.tech_.paused()) {
|
16135 | _this2.mainSegmentLoader_.load();
|
16136 | if (_this2.audioSegmentLoader_) {
|
16137 | _this2.audioSegmentLoader_.load();
|
16138 | }
|
16139 | }
|
16140 |
|
16141 | if (!updatedPlaylist.endList) {
|
16142 | var addSeekableRange = function addSeekableRange() {
|
16143 | var seekable$$1 = _this2.seekable();
|
16144 |
|
16145 | if (seekable$$1.length !== 0) {
|
16146 | _this2.mediaSource.addSeekableRange_(seekable$$1.start(0), seekable$$1.end(0));
|
16147 | }
|
16148 | };
|
16149 |
|
16150 | if (_this2.duration() !== Infinity) {
|
16151 | var onDurationchange = function onDurationchange() {
|
16152 | if (_this2.duration() === Infinity) {
|
16153 | addSeekableRange();
|
16154 | } else {
|
16155 | _this2.tech_.one('durationchange', onDurationchange);
|
16156 | }
|
16157 | };
|
16158 |
|
16159 | _this2.tech_.one('durationchange', onDurationchange);
|
16160 | } else {
|
16161 | addSeekableRange();
|
16162 | }
|
16163 | }
|
16164 | });
|
16165 |
|
16166 | this.masterPlaylistLoader_.on('error', function () {
|
16167 | _this2.blacklistCurrentPlaylist(_this2.masterPlaylistLoader_.error);
|
16168 | });
|
16169 |
|
16170 | this.masterPlaylistLoader_.on('mediachanging', function () {
|
16171 | _this2.mainSegmentLoader_.abort();
|
16172 | _this2.mainSegmentLoader_.pause();
|
16173 | });
|
16174 |
|
16175 | this.masterPlaylistLoader_.on('mediachange', function () {
|
16176 | var media = _this2.masterPlaylistLoader_.media();
|
16177 | var requestTimeout = media.targetDuration * 1.5 * 1000;
|
16178 |
|
16179 |
|
16180 |
|
16181 | if (isLowestEnabledRendition(_this2.masterPlaylistLoader_.master, _this2.masterPlaylistLoader_.media())) {
|
16182 | _this2.requestOptions_.timeout = 0;
|
16183 | } else {
|
16184 | _this2.requestOptions_.timeout = requestTimeout;
|
16185 | }
|
16186 |
|
16187 |
|
16188 |
|
16189 |
|
16190 |
|
16191 | _this2.mainSegmentLoader_.playlist(media, _this2.requestOptions_);
|
16192 |
|
16193 | _this2.mainSegmentLoader_.load();
|
16194 |
|
16195 | _this2.tech_.trigger({
|
16196 | type: 'mediachange',
|
16197 | bubbles: true
|
16198 | });
|
16199 | });
|
16200 |
|
16201 | this.masterPlaylistLoader_.on('playlistunchanged', function () {
|
16202 | var updatedPlaylist = _this2.masterPlaylistLoader_.media();
|
16203 | var playlistOutdated = _this2.stuckAtPlaylistEnd_(updatedPlaylist);
|
16204 |
|
16205 | if (playlistOutdated) {
|
16206 |
|
16207 |
|
16208 |
|
16209 |
|
16210 | _this2.blacklistCurrentPlaylist({
|
16211 | message: 'Playlist no longer updating.'
|
16212 | });
|
16213 |
|
16214 | _this2.tech_.trigger('playliststuck');
|
16215 | }
|
16216 | });
|
16217 |
|
16218 | this.masterPlaylistLoader_.on('renditiondisabled', function () {
|
16219 | _this2.tech_.trigger({ type: 'usage', name: 'hls-rendition-disabled' });
|
16220 | });
|
16221 | this.masterPlaylistLoader_.on('renditionenabled', function () {
|
16222 | _this2.tech_.trigger({ type: 'usage', name: 'hls-rendition-enabled' });
|
16223 | });
|
16224 | }
|
16225 |
|
16226 | |
16227 |
|
16228 |
|
16229 |
|
16230 |
|
16231 |
|
16232 | }, {
|
16233 | key: 'triggerPresenceUsage_',
|
16234 | value: function triggerPresenceUsage_(master, media) {
|
16235 | var mediaGroups = master.mediaGroups || {};
|
16236 | var defaultDemuxed = true;
|
16237 | var audioGroupKeys = Object.keys(mediaGroups.AUDIO);
|
16238 |
|
16239 | for (var mediaGroup in mediaGroups.AUDIO) {
|
16240 | for (var label in mediaGroups.AUDIO[mediaGroup]) {
|
16241 | var properties = mediaGroups.AUDIO[mediaGroup][label];
|
16242 |
|
16243 | if (!properties.uri) {
|
16244 | defaultDemuxed = false;
|
16245 | }
|
16246 | }
|
16247 | }
|
16248 |
|
16249 | if (defaultDemuxed) {
|
16250 | this.tech_.trigger({ type: 'usage', name: 'hls-demuxed' });
|
16251 | }
|
16252 |
|
16253 | if (Object.keys(mediaGroups.SUBTITLES).length) {
|
16254 | this.tech_.trigger({ type: 'usage', name: 'hls-webvtt' });
|
16255 | }
|
16256 |
|
16257 | if (Hls.Playlist.isAes(media)) {
|
16258 | this.tech_.trigger({ type: 'usage', name: 'hls-aes' });
|
16259 | }
|
16260 |
|
16261 | if (Hls.Playlist.isFmp4(media)) {
|
16262 | this.tech_.trigger({ type: 'usage', name: 'hls-fmp4' });
|
16263 | }
|
16264 |
|
16265 | if (audioGroupKeys.length && Object.keys(mediaGroups.AUDIO[audioGroupKeys[0]]).length > 1) {
|
16266 | this.tech_.trigger({ type: 'usage', name: 'hls-alternate-audio' });
|
16267 | }
|
16268 |
|
16269 | if (this.useCueTags_) {
|
16270 | this.tech_.trigger({ type: 'usage', name: 'hls-playlist-cue-tags' });
|
16271 | }
|
16272 | }
|
16273 | |
16274 |
|
16275 |
|
16276 |
|
16277 |
|
16278 |
|
16279 |
|
16280 | }, {
|
16281 | key: 'setupSegmentLoaderListeners_',
|
16282 | value: function setupSegmentLoaderListeners_() {
|
16283 | var _this3 = this;
|
16284 |
|
16285 | this.mainSegmentLoader_.on('bandwidthupdate', function () {
|
16286 | var nextPlaylist = _this3.selectPlaylist();
|
16287 | var currentPlaylist = _this3.masterPlaylistLoader_.media();
|
16288 | var buffered = _this3.tech_.buffered();
|
16289 | var forwardBuffer = buffered.length ? buffered.end(buffered.length - 1) - _this3.tech_.currentTime() : 0;
|
16290 |
|
16291 | var bufferLowWaterLine = _this3.bufferLowWaterLine();
|
16292 |
|
16293 | if (shouldSwitchToMedia({
|
16294 | currentPlaylist: currentPlaylist,
|
16295 | nextPlaylist: nextPlaylist,
|
16296 | forwardBuffer: forwardBuffer,
|
16297 | bufferLowWaterLine: bufferLowWaterLine,
|
16298 | duration: _this3.duration(),
|
16299 | log: _this3.logger_
|
16300 | })) {
|
16301 | _this3.masterPlaylistLoader_.media(nextPlaylist);
|
16302 | }
|
16303 |
|
16304 | _this3.tech_.trigger('bandwidthupdate');
|
16305 | });
|
16306 | this.mainSegmentLoader_.on('progress', function () {
|
16307 | _this3.trigger('progress');
|
16308 | });
|
16309 |
|
16310 | this.mainSegmentLoader_.on('error', function () {
|
16311 | _this3.blacklistCurrentPlaylist(_this3.mainSegmentLoader_.error());
|
16312 | });
|
16313 |
|
16314 | this.mainSegmentLoader_.on('syncinfoupdate', function () {
|
16315 | _this3.onSyncInfoUpdate_();
|
16316 | });
|
16317 |
|
16318 | this.mainSegmentLoader_.on('timestampoffset', function () {
|
16319 | _this3.tech_.trigger({ type: 'usage', name: 'hls-timestamp-offset' });
|
16320 | });
|
16321 | this.audioSegmentLoader_.on('syncinfoupdate', function () {
|
16322 | _this3.onSyncInfoUpdate_();
|
16323 | });
|
16324 |
|
16325 | this.mainSegmentLoader_.on('ended', function () {
|
16326 | _this3.onEndOfStream();
|
16327 | });
|
16328 |
|
16329 | this.mainSegmentLoader_.on('earlyabort', function () {
|
16330 | _this3.blacklistCurrentPlaylist({
|
16331 | message: 'Aborted early because there isn\'t enough bandwidth to complete the ' + 'request without rebuffering.'
|
16332 | }, ABORT_EARLY_BLACKLIST_SECONDS);
|
16333 | });
|
16334 |
|
16335 | this.mainSegmentLoader_.on('reseteverything', function () {
|
16336 |
|
16337 |
|
16338 | _this3.tech_.trigger('hls-reset');
|
16339 | });
|
16340 |
|
16341 | this.mainSegmentLoader_.on('segmenttimemapping', function (event) {
|
16342 |
|
16343 |
|
16344 | _this3.tech_.trigger({
|
16345 | type: 'hls-segment-time-mapping',
|
16346 | mapping: event.mapping
|
16347 | });
|
16348 | });
|
16349 |
|
16350 | this.audioSegmentLoader_.on('ended', function () {
|
16351 | _this3.onEndOfStream();
|
16352 | });
|
16353 | }
|
16354 | }, {
|
16355 | key: 'mediaSecondsLoaded_',
|
16356 | value: function mediaSecondsLoaded_() {
|
16357 | return Math.max(this.audioSegmentLoader_.mediaSecondsLoaded + this.mainSegmentLoader_.mediaSecondsLoaded);
|
16358 | }
|
16359 |
|
16360 | |
16361 |
|
16362 |
|
16363 |
|
16364 | }, {
|
16365 | key: 'load',
|
16366 | value: function load() {
|
16367 | this.mainSegmentLoader_.load();
|
16368 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
16369 | this.audioSegmentLoader_.load();
|
16370 | }
|
16371 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
16372 | this.subtitleSegmentLoader_.load();
|
16373 | }
|
16374 | }
|
16375 |
|
16376 | |
16377 |
|
16378 |
|
16379 |
|
16380 |
|
16381 |
|
16382 |
|
16383 |
|
16384 | }, {
|
16385 | key: 'smoothQualityChange_',
|
16386 | value: function smoothQualityChange_() {
|
16387 | var media = this.selectPlaylist();
|
16388 |
|
16389 | if (media !== this.masterPlaylistLoader_.media()) {
|
16390 | this.masterPlaylistLoader_.media(media);
|
16391 |
|
16392 | this.mainSegmentLoader_.resetLoader();
|
16393 |
|
16394 | }
|
16395 | }
|
16396 |
|
16397 | |
16398 |
|
16399 |
|
16400 |
|
16401 |
|
16402 |
|
16403 |
|
16404 |
|
16405 |
|
16406 | }, {
|
16407 | key: 'fastQualityChange_',
|
16408 | value: function fastQualityChange_() {
|
16409 | var _this4 = this;
|
16410 |
|
16411 | var media = this.selectPlaylist();
|
16412 |
|
16413 | if (media === this.masterPlaylistLoader_.media()) {
|
16414 | return;
|
16415 | }
|
16416 |
|
16417 | this.masterPlaylistLoader_.media(media);
|
16418 |
|
16419 |
|
16420 |
|
16421 |
|
16422 |
|
16423 |
|
16424 |
|
16425 | this.mainSegmentLoader_.resetEverything(function () {
|
16426 |
|
16427 |
|
16428 | if (videojs.browser.IE_VERSION || videojs.browser.IS_EDGE) {
|
16429 | _this4.tech_.setCurrentTime(_this4.tech_.currentTime() + 0.04);
|
16430 | } else {
|
16431 | _this4.tech_.setCurrentTime(_this4.tech_.currentTime());
|
16432 | }
|
16433 | });
|
16434 |
|
16435 |
|
16436 | }
|
16437 |
|
16438 | |
16439 |
|
16440 |
|
16441 |
|
16442 | }, {
|
16443 | key: 'play',
|
16444 | value: function play() {
|
16445 | if (this.setupFirstPlay()) {
|
16446 | return;
|
16447 | }
|
16448 |
|
16449 | if (this.tech_.ended()) {
|
16450 | this.tech_.setCurrentTime(0);
|
16451 | }
|
16452 |
|
16453 | if (this.hasPlayed_) {
|
16454 | this.load();
|
16455 | }
|
16456 |
|
16457 | var seekable$$1 = this.tech_.seekable();
|
16458 |
|
16459 |
|
16460 |
|
16461 | if (this.tech_.duration() === Infinity) {
|
16462 | if (this.tech_.currentTime() < seekable$$1.start(0)) {
|
16463 | return this.tech_.setCurrentTime(seekable$$1.end(seekable$$1.length - 1));
|
16464 | }
|
16465 | }
|
16466 | }
|
16467 |
|
16468 | |
16469 |
|
16470 |
|
16471 |
|
16472 |
|
16473 | }, {
|
16474 | key: 'setupFirstPlay',
|
16475 | value: function setupFirstPlay() {
|
16476 | var _this5 = this;
|
16477 |
|
16478 | var media = this.masterPlaylistLoader_.media();
|
16479 |
|
16480 |
|
16481 |
|
16482 |
|
16483 |
|
16484 |
|
16485 | if (!media || this.tech_.paused() || this.hasPlayed_) {
|
16486 | return false;
|
16487 | }
|
16488 |
|
16489 |
|
16490 | if (!media.endList) {
|
16491 | var seekable$$1 = this.seekable();
|
16492 |
|
16493 | if (!seekable$$1.length) {
|
16494 |
|
16495 |
|
16496 | return false;
|
16497 | }
|
16498 |
|
16499 | if (videojs.browser.IE_VERSION && this.tech_.readyState() === 0) {
|
16500 |
|
16501 |
|
16502 | this.tech_.one('loadedmetadata', function () {
|
16503 | _this5.trigger('firstplay');
|
16504 | _this5.tech_.setCurrentTime(seekable$$1.end(0));
|
16505 | _this5.hasPlayed_ = true;
|
16506 | });
|
16507 |
|
16508 | return false;
|
16509 | }
|
16510 |
|
16511 |
|
16512 | this.trigger('firstplay');
|
16513 |
|
16514 | this.tech_.setCurrentTime(seekable$$1.end(0));
|
16515 | }
|
16516 |
|
16517 | this.hasPlayed_ = true;
|
16518 |
|
16519 | this.load();
|
16520 | return true;
|
16521 | }
|
16522 |
|
16523 | |
16524 |
|
16525 |
|
16526 |
|
16527 |
|
16528 |
|
16529 | }, {
|
16530 | key: 'handleSourceOpen_',
|
16531 | value: function handleSourceOpen_() {
|
16532 |
|
16533 |
|
16534 |
|
16535 | try {
|
16536 | this.setupSourceBuffers_();
|
16537 | } catch (e) {
|
16538 | videojs.log.warn('Failed to create Source Buffers', e);
|
16539 | return this.mediaSource.endOfStream('decode');
|
16540 | }
|
16541 |
|
16542 |
|
16543 |
|
16544 |
|
16545 | if (this.tech_.autoplay()) {
|
16546 | var playPromise = this.tech_.play();
|
16547 |
|
16548 |
|
16549 |
|
16550 | if (typeof playPromise !== 'undefined' && typeof playPromise.then === 'function') {
|
16551 | playPromise.then(null, function (e) {});
|
16552 | }
|
16553 | }
|
16554 |
|
16555 | this.trigger('sourceopen');
|
16556 | }
|
16557 |
|
16558 | |
16559 |
|
16560 |
|
16561 |
|
16562 |
|
16563 |
|
16564 |
|
16565 |
|
16566 |
|
16567 | }, {
|
16568 | key: 'onEndOfStream',
|
16569 | value: function onEndOfStream() {
|
16570 | var isEndOfStream = this.mainSegmentLoader_.ended_;
|
16571 |
|
16572 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
16573 |
|
16574 | if (!this.mainSegmentLoader_.startingMedia_ || this.mainSegmentLoader_.startingMedia_.containsVideo) {
|
16575 |
|
16576 |
|
16577 |
|
16578 | isEndOfStream = isEndOfStream && this.audioSegmentLoader_.ended_;
|
16579 | } else {
|
16580 |
|
16581 | isEndOfStream = this.audioSegmentLoader_.ended_;
|
16582 | }
|
16583 | }
|
16584 |
|
16585 | if (!isEndOfStream) {
|
16586 | return;
|
16587 | }
|
16588 |
|
16589 | this.logger_('calling mediaSource.endOfStream()');
|
16590 |
|
16591 |
|
16592 | try {
|
16593 | this.mediaSource.endOfStream();
|
16594 | } catch (e) {
|
16595 | videojs.log.warn('Failed to call media source endOfStream', e);
|
16596 | }
|
16597 | }
|
16598 |
|
16599 | |
16600 |
|
16601 |
|
16602 |
|
16603 |
|
16604 |
|
16605 | }, {
|
16606 | key: 'stuckAtPlaylistEnd_',
|
16607 | value: function stuckAtPlaylistEnd_(playlist) {
|
16608 | var seekable$$1 = this.seekable();
|
16609 |
|
16610 | if (!seekable$$1.length) {
|
16611 |
|
16612 | return false;
|
16613 | }
|
16614 |
|
16615 | var expired = this.syncController_.getExpiredTime(playlist, this.mediaSource.duration);
|
16616 |
|
16617 | if (expired === null) {
|
16618 | return false;
|
16619 | }
|
16620 |
|
16621 |
|
16622 |
|
16623 | var absolutePlaylistEnd = Hls.Playlist.playlistEnd(playlist, expired);
|
16624 | var currentTime = this.tech_.currentTime();
|
16625 | var buffered = this.tech_.buffered();
|
16626 |
|
16627 | if (!buffered.length) {
|
16628 |
|
16629 | return absolutePlaylistEnd - currentTime <= SAFE_TIME_DELTA;
|
16630 | }
|
16631 | var bufferedEnd = buffered.end(buffered.length - 1);
|
16632 |
|
16633 |
|
16634 |
|
16635 | return bufferedEnd - currentTime <= SAFE_TIME_DELTA && absolutePlaylistEnd - bufferedEnd <= SAFE_TIME_DELTA;
|
16636 | }
|
16637 |
|
16638 | |
16639 |
|
16640 |
|
16641 |
|
16642 |
|
16643 |
|
16644 |
|
16645 |
|
16646 |
|
16647 |
|
16648 |
|
16649 | }, {
|
16650 | key: 'blacklistCurrentPlaylist',
|
16651 | value: function blacklistCurrentPlaylist() {
|
16652 | var error = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
16653 | var blacklistDuration = arguments[1];
|
16654 |
|
16655 | var currentPlaylist = void 0;
|
16656 | var nextPlaylist = void 0;
|
16657 |
|
16658 |
|
16659 |
|
16660 |
|
16661 |
|
16662 | currentPlaylist = error.playlist || this.masterPlaylistLoader_.media();
|
16663 |
|
16664 | blacklistDuration = blacklistDuration || error.blacklistDuration || this.blacklistDuration;
|
16665 |
|
16666 |
|
16667 |
|
16668 | if (!currentPlaylist) {
|
16669 | this.error = error;
|
16670 |
|
16671 | try {
|
16672 | return this.mediaSource.endOfStream('network');
|
16673 | } catch (e) {
|
16674 | return this.trigger('error');
|
16675 | }
|
16676 | }
|
16677 |
|
16678 | var isFinalRendition = this.masterPlaylistLoader_.master.playlists.filter(isEnabled).length === 1;
|
16679 | var playlists = this.masterPlaylistLoader_.master.playlists;
|
16680 |
|
16681 | if (playlists.length === 1) {
|
16682 |
|
16683 | videojs.log.warn('Problem encountered with the current ' + 'HLS playlist. Trying again since it is the only playlist.');
|
16684 |
|
16685 | this.tech_.trigger('retryplaylist');
|
16686 | return this.masterPlaylistLoader_.load(isFinalRendition);
|
16687 | }
|
16688 |
|
16689 | if (isFinalRendition) {
|
16690 |
|
16691 |
|
16692 |
|
16693 |
|
16694 | videojs.log.warn('Removing all playlists from the blacklist because the last ' + 'rendition is about to be blacklisted.');
|
16695 | playlists.forEach(function (playlist) {
|
16696 | if (playlist.excludeUntil !== Infinity) {
|
16697 | delete playlist.excludeUntil;
|
16698 | }
|
16699 | });
|
16700 |
|
16701 |
|
16702 |
|
16703 | this.tech_.trigger('retryplaylist');
|
16704 | }
|
16705 |
|
16706 |
|
16707 | currentPlaylist.excludeUntil = Date.now() + blacklistDuration * 1000;
|
16708 | this.tech_.trigger('blacklistplaylist');
|
16709 | this.tech_.trigger({ type: 'usage', name: 'hls-rendition-blacklisted' });
|
16710 |
|
16711 |
|
16712 | nextPlaylist = this.selectPlaylist();
|
16713 | videojs.log.warn('Problem encountered with the current HLS playlist.' + (error.message ? ' ' + error.message : '') + ' Switching to another playlist.');
|
16714 |
|
16715 | return this.masterPlaylistLoader_.media(nextPlaylist, isFinalRendition);
|
16716 | }
|
16717 |
|
16718 | |
16719 |
|
16720 |
|
16721 |
|
16722 | }, {
|
16723 | key: 'pauseLoading',
|
16724 | value: function pauseLoading() {
|
16725 | this.mainSegmentLoader_.pause();
|
16726 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
16727 | this.audioSegmentLoader_.pause();
|
16728 | }
|
16729 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
16730 | this.subtitleSegmentLoader_.pause();
|
16731 | }
|
16732 | }
|
16733 |
|
16734 | |
16735 |
|
16736 |
|
16737 |
|
16738 |
|
16739 |
|
16740 |
|
16741 | }, {
|
16742 | key: 'setCurrentTime',
|
16743 | value: function setCurrentTime(currentTime) {
|
16744 | var buffered = findRange(this.tech_.buffered(), currentTime);
|
16745 |
|
16746 | if (!(this.masterPlaylistLoader_ && this.masterPlaylistLoader_.media())) {
|
16747 |
|
16748 | return 0;
|
16749 | }
|
16750 |
|
16751 |
|
16752 |
|
16753 | if (!this.masterPlaylistLoader_.media().segments) {
|
16754 | return 0;
|
16755 | }
|
16756 |
|
16757 |
|
16758 |
|
16759 |
|
16760 |
|
16761 | if (buffered && buffered.length) {
|
16762 | return currentTime;
|
16763 | }
|
16764 |
|
16765 |
|
16766 |
|
16767 | this.mainSegmentLoader_.resetEverything();
|
16768 | this.mainSegmentLoader_.abort();
|
16769 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
16770 | this.audioSegmentLoader_.resetEverything();
|
16771 | this.audioSegmentLoader_.abort();
|
16772 | }
|
16773 | if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
|
16774 | this.subtitleSegmentLoader_.resetEverything();
|
16775 | this.subtitleSegmentLoader_.abort();
|
16776 | }
|
16777 |
|
16778 |
|
16779 | this.load();
|
16780 | }
|
16781 |
|
16782 | |
16783 |
|
16784 |
|
16785 |
|
16786 |
|
16787 |
|
16788 | }, {
|
16789 | key: 'duration',
|
16790 | value: function duration$$1() {
|
16791 | if (!this.masterPlaylistLoader_) {
|
16792 | return 0;
|
16793 | }
|
16794 |
|
16795 | if (this.mediaSource) {
|
16796 | return this.mediaSource.duration;
|
16797 | }
|
16798 |
|
16799 | return Hls.Playlist.duration(this.masterPlaylistLoader_.media());
|
16800 | }
|
16801 |
|
16802 | |
16803 |
|
16804 |
|
16805 |
|
16806 |
|
16807 |
|
16808 | }, {
|
16809 | key: 'seekable',
|
16810 | value: function seekable$$1() {
|
16811 | return this.seekable_;
|
16812 | }
|
16813 | }, {
|
16814 | key: 'onSyncInfoUpdate_',
|
16815 | value: function onSyncInfoUpdate_() {
|
16816 | var audioSeekable = void 0;
|
16817 |
|
16818 | if (!this.masterPlaylistLoader_) {
|
16819 | return;
|
16820 | }
|
16821 |
|
16822 | var media = this.masterPlaylistLoader_.media();
|
16823 |
|
16824 | if (!media) {
|
16825 | return;
|
16826 | }
|
16827 |
|
16828 | var expired = this.syncController_.getExpiredTime(media, this.mediaSource.duration);
|
16829 |
|
16830 | if (expired === null) {
|
16831 |
|
16832 | return;
|
16833 | }
|
16834 |
|
16835 | var suggestedPresentationDelay = this.masterPlaylistLoader_.master.suggestedPresentationDelay;
|
16836 | var mainSeekable = Hls.Playlist.seekable(media, expired, suggestedPresentationDelay);
|
16837 |
|
16838 | if (mainSeekable.length === 0) {
|
16839 | return;
|
16840 | }
|
16841 |
|
16842 | if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
|
16843 | media = this.mediaTypes_.AUDIO.activePlaylistLoader.media();
|
16844 | expired = this.syncController_.getExpiredTime(media, this.mediaSource.duration);
|
16845 |
|
16846 | if (expired === null) {
|
16847 | return;
|
16848 | }
|
16849 |
|
16850 | audioSeekable = Hls.Playlist.seekable(media, expired, suggestedPresentationDelay);
|
16851 |
|
16852 | if (audioSeekable.length === 0) {
|
16853 | return;
|
16854 | }
|
16855 | }
|
16856 |
|
16857 | var oldEnd = void 0;
|
16858 | var oldStart = void 0;
|
16859 |
|
16860 | if (this.seekable_ && this.seekable_.length) {
|
16861 | oldEnd = this.seekable_.end(0);
|
16862 | oldStart = this.seekable_.start(0);
|
16863 | }
|
16864 |
|
16865 | if (!audioSeekable) {
|
16866 |
|
16867 |
|
16868 | this.seekable_ = mainSeekable;
|
16869 | } else if (audioSeekable.start(0) > mainSeekable.end(0) || mainSeekable.start(0) > audioSeekable.end(0)) {
|
16870 |
|
16871 | this.seekable_ = mainSeekable;
|
16872 | } else {
|
16873 | this.seekable_ = videojs.createTimeRanges([[audioSeekable.start(0) > mainSeekable.start(0) ? audioSeekable.start(0) : mainSeekable.start(0), audioSeekable.end(0) < mainSeekable.end(0) ? audioSeekable.end(0) : mainSeekable.end(0)]]);
|
16874 | }
|
16875 |
|
16876 |
|
16877 | if (this.seekable_ && this.seekable_.length) {
|
16878 | if (this.seekable_.end(0) === oldEnd && this.seekable_.start(0) === oldStart) {
|
16879 | return;
|
16880 | }
|
16881 | }
|
16882 |
|
16883 | this.logger_('seekable updated [' + printableRange(this.seekable_) + ']');
|
16884 |
|
16885 | this.tech_.trigger('seekablechanged');
|
16886 | }
|
16887 |
|
16888 | |
16889 |
|
16890 |
|
16891 |
|
16892 | }, {
|
16893 | key: 'updateDuration',
|
16894 | value: function updateDuration() {
|
16895 | var _this6 = this;
|
16896 |
|
16897 | var oldDuration = this.mediaSource.duration;
|
16898 | var newDuration = Hls.Playlist.duration(this.masterPlaylistLoader_.media());
|
16899 | var buffered = this.tech_.buffered();
|
16900 | var setDuration = function setDuration() {
|
16901 |
|
16902 |
|
16903 |
|
16904 | _this6.logger_('Setting duration from ' + _this6.mediaSource.duration + ' => ' + newDuration);
|
16905 | try {
|
16906 | _this6.mediaSource.duration = newDuration;
|
16907 | } catch (e) {
|
16908 | videojs.log.warn('Failed to set media source duration', e);
|
16909 | }
|
16910 | _this6.tech_.trigger('durationchange');
|
16911 |
|
16912 | _this6.mediaSource.removeEventListener('sourceopen', setDuration);
|
16913 | };
|
16914 |
|
16915 | if (buffered.length > 0) {
|
16916 | newDuration = Math.max(newDuration, buffered.end(buffered.length - 1));
|
16917 | }
|
16918 |
|
16919 |
|
16920 | if (oldDuration !== newDuration) {
|
16921 |
|
16922 | if (this.mediaSource.readyState !== 'open') {
|
16923 | this.mediaSource.addEventListener('sourceopen', setDuration);
|
16924 | } else {
|
16925 | setDuration();
|
16926 | }
|
16927 | }
|
16928 | }
|
16929 |
|
16930 | |
16931 |
|
16932 |
|
16933 |
|
16934 |
|
16935 | }, {
|
16936 | key: 'dispose',
|
16937 | value: function dispose() {
|
16938 | var _this7 = this;
|
16939 |
|
16940 | this.trigger('dispose');
|
16941 | if (this.decrypter_) {
|
16942 | this.decrypter_.terminate();
|
16943 | }
|
16944 | this.masterPlaylistLoader_.dispose();
|
16945 | this.mainSegmentLoader_.dispose();
|
16946 |
|
16947 | ['AUDIO', 'SUBTITLES'].forEach(function (type) {
|
16948 | var groups = _this7.mediaTypes_[type].groups;
|
16949 |
|
16950 | for (var id in groups) {
|
16951 | groups[id].forEach(function (group) {
|
16952 | if (group.playlistLoader) {
|
16953 | group.playlistLoader.dispose();
|
16954 | }
|
16955 | });
|
16956 | }
|
16957 | });
|
16958 |
|
16959 | this.audioSegmentLoader_.dispose();
|
16960 | this.subtitleSegmentLoader_.dispose();
|
16961 | this.off();
|
16962 |
|
16963 | if (this.mediaSource.dispose) {
|
16964 | this.mediaSource.dispose();
|
16965 | }
|
16966 | }
|
16967 |
|
16968 | |
16969 |
|
16970 |
|
16971 |
|
16972 |
|
16973 |
|
16974 | }, {
|
16975 | key: 'master',
|
16976 | value: function master() {
|
16977 | return this.masterPlaylistLoader_.master;
|
16978 | }
|
16979 |
|
16980 | |
16981 |
|
16982 |
|
16983 |
|
16984 |
|
16985 |
|
16986 | }, {
|
16987 | key: 'media',
|
16988 | value: function media() {
|
16989 |
|
16990 | return this.masterPlaylistLoader_.media() || this.initialMedia_;
|
16991 | }
|
16992 |
|
16993 | |
16994 |
|
16995 |
|
16996 |
|
16997 |
|
16998 |
|
16999 | }, {
|
17000 | key: 'setupSourceBuffers_',
|
17001 | value: function setupSourceBuffers_() {
|
17002 | var media = this.masterPlaylistLoader_.media();
|
17003 | var mimeTypes = void 0;
|
17004 |
|
17005 |
|
17006 |
|
17007 | if (!media || this.mediaSource.readyState !== 'open') {
|
17008 | return;
|
17009 | }
|
17010 |
|
17011 | mimeTypes = mimeTypesForPlaylist(this.masterPlaylistLoader_.master, media);
|
17012 | if (mimeTypes.length < 1) {
|
17013 | this.error = 'No compatible SourceBuffer configuration for the variant stream:' + media.resolvedUri;
|
17014 | return this.mediaSource.endOfStream('decode');
|
17015 | }
|
17016 |
|
17017 | this.configureLoaderMimeTypes_(mimeTypes);
|
17018 |
|
17019 |
|
17020 | this.excludeIncompatibleVariants_(media);
|
17021 | }
|
17022 | }, {
|
17023 | key: 'configureLoaderMimeTypes_',
|
17024 | value: function configureLoaderMimeTypes_(mimeTypes) {
|
17025 |
|
17026 |
|
17027 |
|
17028 |
|
17029 | var sourceBufferEmitter =
|
17030 |
|
17031 | mimeTypes.length > 1 &&
|
17032 |
|
17033 | mimeTypes[0].indexOf(',') === -1 &&
|
17034 |
|
17035 |
|
17036 | mimeTypes[0] !== mimeTypes[1] ?
|
17037 |
|
17038 | new videojs.EventTarget() :
|
17039 |
|
17040 |
|
17041 | null;
|
17042 |
|
17043 | this.mainSegmentLoader_.mimeType(mimeTypes[0], sourceBufferEmitter);
|
17044 | if (mimeTypes[1]) {
|
17045 | this.audioSegmentLoader_.mimeType(mimeTypes[1], sourceBufferEmitter);
|
17046 | }
|
17047 | }
|
17048 |
|
17049 | |
17050 |
|
17051 |
|
17052 |
|
17053 | }, {
|
17054 | key: 'excludeUnsupportedVariants_',
|
17055 | value: function excludeUnsupportedVariants_() {
|
17056 | this.master().playlists.forEach(function (variant) {
|
17057 | if (variant.attributes.CODECS && window$1.MediaSource && window$1.MediaSource.isTypeSupported && !window$1.MediaSource.isTypeSupported('video/mp4; codecs="' + mapLegacyAvcCodecs(variant.attributes.CODECS) + '"')) {
|
17058 | variant.excludeUntil = Infinity;
|
17059 | }
|
17060 | });
|
17061 | }
|
17062 |
|
17063 | |
17064 |
|
17065 |
|
17066 |
|
17067 |
|
17068 |
|
17069 |
|
17070 |
|
17071 |
|
17072 |
|
17073 |
|
17074 |
|
17075 |
|
17076 |
|
17077 |
|
17078 | }, {
|
17079 | key: 'excludeIncompatibleVariants_',
|
17080 | value: function excludeIncompatibleVariants_(media) {
|
17081 | var codecCount = 2;
|
17082 | var videoCodec = null;
|
17083 | var codecs = void 0;
|
17084 |
|
17085 | if (media.attributes.CODECS) {
|
17086 | codecs = parseCodecs(media.attributes.CODECS);
|
17087 | videoCodec = codecs.videoCodec;
|
17088 | codecCount = codecs.codecCount;
|
17089 | }
|
17090 |
|
17091 | this.master().playlists.forEach(function (variant) {
|
17092 | var variantCodecs = {
|
17093 | codecCount: 2,
|
17094 | videoCodec: null
|
17095 | };
|
17096 |
|
17097 | if (variant.attributes.CODECS) {
|
17098 | variantCodecs = parseCodecs(variant.attributes.CODECS);
|
17099 | }
|
17100 |
|
17101 |
|
17102 |
|
17103 | if (variantCodecs.codecCount !== codecCount) {
|
17104 | variant.excludeUntil = Infinity;
|
17105 | }
|
17106 |
|
17107 |
|
17108 |
|
17109 | if (variantCodecs.videoCodec !== videoCodec) {
|
17110 | variant.excludeUntil = Infinity;
|
17111 | }
|
17112 | });
|
17113 | }
|
17114 | }, {
|
17115 | key: 'updateAdCues_',
|
17116 | value: function updateAdCues_(media) {
|
17117 | var offset = 0;
|
17118 | var seekable$$1 = this.seekable();
|
17119 |
|
17120 | if (seekable$$1.length) {
|
17121 | offset = seekable$$1.start(0);
|
17122 | }
|
17123 |
|
17124 | updateAdCues(media, this.cueTagsTrack_, offset);
|
17125 | }
|
17126 |
|
17127 | |
17128 |
|
17129 |
|
17130 |
|
17131 |
|
17132 |
|
17133 | }, {
|
17134 | key: 'goalBufferLength',
|
17135 | value: function goalBufferLength() {
|
17136 | var currentTime = this.tech_.currentTime();
|
17137 | var initial = Config.GOAL_BUFFER_LENGTH;
|
17138 | var rate = Config.GOAL_BUFFER_LENGTH_RATE;
|
17139 | var max = Math.max(initial, Config.MAX_GOAL_BUFFER_LENGTH);
|
17140 |
|
17141 | return Math.min(initial + currentTime * rate, max);
|
17142 | }
|
17143 |
|
17144 | |
17145 |
|
17146 |
|
17147 |
|
17148 |
|
17149 |
|
17150 | }, {
|
17151 | key: 'bufferLowWaterLine',
|
17152 | value: function bufferLowWaterLine() {
|
17153 | var currentTime = this.tech_.currentTime();
|
17154 | var initial = Config.BUFFER_LOW_WATER_LINE;
|
17155 | var rate = Config.BUFFER_LOW_WATER_LINE_RATE;
|
17156 | var max = Math.max(initial, Config.MAX_BUFFER_LOW_WATER_LINE);
|
17157 |
|
17158 | return Math.min(initial + currentTime * rate, max);
|
17159 | }
|
17160 | }]);
|
17161 | return MasterPlaylistController;
|
17162 | }(videojs.EventTarget);
|
17163 |
|
17164 |
|
17165 |
|
17166 |
|
17167 |
|
17168 |
|
17169 |
|
17170 |
|
17171 |
|
17172 |
|
17173 |
|
17174 |
|
17175 |
|
17176 |
|
17177 | var enableFunction = function enableFunction(loader, playlistID, changePlaylistFn) {
|
17178 | return function (enable) {
|
17179 | var playlist = loader.master.playlists[playlistID];
|
17180 | var incompatible = isIncompatible(playlist);
|
17181 | var currentlyEnabled = isEnabled(playlist);
|
17182 |
|
17183 | if (typeof enable === 'undefined') {
|
17184 | return currentlyEnabled;
|
17185 | }
|
17186 |
|
17187 | if (enable) {
|
17188 | delete playlist.disabled;
|
17189 | } else {
|
17190 | playlist.disabled = true;
|
17191 | }
|
17192 |
|
17193 | if (enable !== currentlyEnabled && !incompatible) {
|
17194 |
|
17195 | changePlaylistFn();
|
17196 | if (enable) {
|
17197 | loader.trigger('renditionenabled');
|
17198 | } else {
|
17199 | loader.trigger('renditiondisabled');
|
17200 | }
|
17201 | }
|
17202 | return enable;
|
17203 | };
|
17204 | };
|
17205 |
|
17206 |
|
17207 |
|
17208 |
|
17209 |
|
17210 |
|
17211 |
|
17212 |
|
17213 |
|
17214 | var Representation = function Representation(hlsHandler, playlist, id) {
|
17215 | classCallCheck(this, Representation);
|
17216 | var mpc = hlsHandler.masterPlaylistController_,
|
17217 | smoothQualityChange = hlsHandler.options_.smoothQualityChange;
|
17218 |
|
17219 |
|
17220 | var changeType = smoothQualityChange ? 'smooth' : 'fast';
|
17221 | var qualityChangeFunction = mpc[changeType + 'QualityChange_'].bind(mpc);
|
17222 |
|
17223 |
|
17224 | if (playlist.attributes.RESOLUTION) {
|
17225 | var resolution = playlist.attributes.RESOLUTION;
|
17226 |
|
17227 | this.width = resolution.width;
|
17228 | this.height = resolution.height;
|
17229 | }
|
17230 |
|
17231 | this.bandwidth = playlist.attributes.BANDWIDTH;
|
17232 |
|
17233 |
|
17234 |
|
17235 | this.id = id;
|
17236 |
|
17237 |
|
17238 |
|
17239 | this.enabled = enableFunction(hlsHandler.playlists, playlist.id, qualityChangeFunction);
|
17240 | };
|
17241 |
|
17242 |
|
17243 |
|
17244 |
|
17245 |
|
17246 |
|
17247 |
|
17248 |
|
17249 |
|
17250 | var renditionSelectionMixin = function renditionSelectionMixin(hlsHandler) {
|
17251 | var playlists = hlsHandler.playlists;
|
17252 |
|
17253 |
|
17254 | hlsHandler.representations = function () {
|
17255 | if (!playlists || !playlists.master || !playlists.master.playlists) {
|
17256 | return [];
|
17257 | }
|
17258 | return playlists.master.playlists.filter(function (media) {
|
17259 | return !isIncompatible(media);
|
17260 | }).map(function (e, i) {
|
17261 | return new Representation(hlsHandler, e, e.id);
|
17262 | });
|
17263 | };
|
17264 | };
|
17265 |
|
17266 |
|
17267 |
|
17268 |
|
17269 |
|
17270 |
|
17271 |
|
17272 |
|
17273 |
|
17274 |
|
17275 |
|
17276 |
|
17277 | var timerCancelEvents = ['seeking', 'seeked', 'pause', 'playing', 'error'];
|
17278 |
|
17279 |
|
17280 |
|
17281 |
|
17282 |
|
17283 | var PlaybackWatcher = function () {
|
17284 | |
17285 |
|
17286 |
|
17287 |
|
17288 |
|
17289 | function PlaybackWatcher(options) {
|
17290 | var _this = this;
|
17291 |
|
17292 | classCallCheck(this, PlaybackWatcher);
|
17293 |
|
17294 | this.tech_ = options.tech;
|
17295 | this.seekable = options.seekable;
|
17296 | this.allowSeeksWithinUnsafeLiveWindow = options.allowSeeksWithinUnsafeLiveWindow;
|
17297 | this.media = options.media;
|
17298 |
|
17299 | this.consecutiveUpdates = 0;
|
17300 | this.lastRecordedTime = null;
|
17301 | this.timer_ = null;
|
17302 | this.checkCurrentTimeTimeout_ = null;
|
17303 | this.logger_ = logger('PlaybackWatcher');
|
17304 |
|
17305 | this.logger_('initialize');
|
17306 |
|
17307 | var canPlayHandler = function canPlayHandler() {
|
17308 | return _this.monitorCurrentTime_();
|
17309 | };
|
17310 | var waitingHandler = function waitingHandler() {
|
17311 | return _this.techWaiting_();
|
17312 | };
|
17313 | var cancelTimerHandler = function cancelTimerHandler() {
|
17314 | return _this.cancelTimer_();
|
17315 | };
|
17316 | var fixesBadSeeksHandler = function fixesBadSeeksHandler() {
|
17317 | return _this.fixesBadSeeks_();
|
17318 | };
|
17319 |
|
17320 | this.tech_.on('seekablechanged', fixesBadSeeksHandler);
|
17321 | this.tech_.on('waiting', waitingHandler);
|
17322 | this.tech_.on(timerCancelEvents, cancelTimerHandler);
|
17323 | this.tech_.on('canplay', canPlayHandler);
|
17324 |
|
17325 |
|
17326 | this.dispose = function () {
|
17327 | _this.logger_('dispose');
|
17328 | _this.tech_.off('seekablechanged', fixesBadSeeksHandler);
|
17329 | _this.tech_.off('waiting', waitingHandler);
|
17330 | _this.tech_.off(timerCancelEvents, cancelTimerHandler);
|
17331 | _this.tech_.off('canplay', canPlayHandler);
|
17332 | if (_this.checkCurrentTimeTimeout_) {
|
17333 | window$1.clearTimeout(_this.checkCurrentTimeTimeout_);
|
17334 | }
|
17335 | _this.cancelTimer_();
|
17336 | };
|
17337 | }
|
17338 |
|
17339 | |
17340 |
|
17341 |
|
17342 |
|
17343 |
|
17344 |
|
17345 |
|
17346 | createClass(PlaybackWatcher, [{
|
17347 | key: 'monitorCurrentTime_',
|
17348 | value: function monitorCurrentTime_() {
|
17349 | this.checkCurrentTime_();
|
17350 |
|
17351 | if (this.checkCurrentTimeTimeout_) {
|
17352 | window$1.clearTimeout(this.checkCurrentTimeTimeout_);
|
17353 | }
|
17354 |
|
17355 |
|
17356 | this.checkCurrentTimeTimeout_ = window$1.setTimeout(this.monitorCurrentTime_.bind(this), 250);
|
17357 | }
|
17358 |
|
17359 | |
17360 |
|
17361 |
|
17362 |
|
17363 |
|
17364 |
|
17365 |
|
17366 |
|
17367 | }, {
|
17368 | key: 'checkCurrentTime_',
|
17369 | value: function checkCurrentTime_() {
|
17370 | if (this.tech_.seeking() && this.fixesBadSeeks_()) {
|
17371 | this.consecutiveUpdates = 0;
|
17372 | this.lastRecordedTime = this.tech_.currentTime();
|
17373 | return;
|
17374 | }
|
17375 |
|
17376 | if (this.tech_.paused() || this.tech_.seeking()) {
|
17377 | return;
|
17378 | }
|
17379 |
|
17380 | var currentTime = this.tech_.currentTime();
|
17381 | var buffered = this.tech_.buffered();
|
17382 |
|
17383 | if (this.lastRecordedTime === currentTime && (!buffered.length || currentTime + SAFE_TIME_DELTA >= buffered.end(buffered.length - 1))) {
|
17384 |
|
17385 |
|
17386 |
|
17387 |
|
17388 |
|
17389 | return this.techWaiting_();
|
17390 | }
|
17391 |
|
17392 | if (this.consecutiveUpdates >= 5 && currentTime === this.lastRecordedTime) {
|
17393 | this.consecutiveUpdates++;
|
17394 | this.waiting_();
|
17395 | } else if (currentTime === this.lastRecordedTime) {
|
17396 | this.consecutiveUpdates++;
|
17397 | } else {
|
17398 | this.consecutiveUpdates = 0;
|
17399 | this.lastRecordedTime = currentTime;
|
17400 | }
|
17401 | }
|
17402 |
|
17403 | |
17404 |
|
17405 |
|
17406 |
|
17407 |
|
17408 |
|
17409 |
|
17410 | }, {
|
17411 | key: 'cancelTimer_',
|
17412 | value: function cancelTimer_() {
|
17413 | this.consecutiveUpdates = 0;
|
17414 |
|
17415 | if (this.timer_) {
|
17416 | this.logger_('cancelTimer_');
|
17417 | clearTimeout(this.timer_);
|
17418 | }
|
17419 |
|
17420 | this.timer_ = null;
|
17421 | }
|
17422 |
|
17423 | |
17424 |
|
17425 |
|
17426 |
|
17427 |
|
17428 |
|
17429 |
|
17430 | }, {
|
17431 | key: 'fixesBadSeeks_',
|
17432 | value: function fixesBadSeeks_() {
|
17433 | var seeking = this.tech_.seeking();
|
17434 |
|
17435 | if (!seeking) {
|
17436 | return false;
|
17437 | }
|
17438 |
|
17439 | var seekable = this.seekable();
|
17440 | var currentTime = this.tech_.currentTime();
|
17441 | var isAfterSeekableRange = this.afterSeekableWindow_(seekable, currentTime, this.media(), this.allowSeeksWithinUnsafeLiveWindow);
|
17442 | var seekTo = void 0;
|
17443 |
|
17444 | if (isAfterSeekableRange) {
|
17445 | var seekableEnd = seekable.end(seekable.length - 1);
|
17446 |
|
17447 |
|
17448 | seekTo = seekableEnd;
|
17449 | }
|
17450 |
|
17451 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
17452 | var seekableStart = seekable.start(0);
|
17453 |
|
17454 |
|
17455 |
|
17456 | seekTo = seekableStart + SAFE_TIME_DELTA;
|
17457 | }
|
17458 |
|
17459 | if (typeof seekTo !== 'undefined') {
|
17460 | this.logger_('Trying to seek outside of seekable at time ' + currentTime + ' with ' + ('seekable range ' + printableRange(seekable) + '. Seeking to ') + (seekTo + '.'));
|
17461 |
|
17462 | this.tech_.setCurrentTime(seekTo);
|
17463 | return true;
|
17464 | }
|
17465 |
|
17466 | return false;
|
17467 | }
|
17468 |
|
17469 | |
17470 |
|
17471 |
|
17472 |
|
17473 |
|
17474 |
|
17475 | }, {
|
17476 | key: 'waiting_',
|
17477 | value: function waiting_() {
|
17478 | if (this.techWaiting_()) {
|
17479 | return;
|
17480 | }
|
17481 |
|
17482 |
|
17483 | var currentTime = this.tech_.currentTime();
|
17484 | var buffered = this.tech_.buffered();
|
17485 | var currentRange = findRange(buffered, currentTime);
|
17486 |
|
17487 |
|
17488 |
|
17489 |
|
17490 |
|
17491 |
|
17492 |
|
17493 |
|
17494 |
|
17495 | if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
|
17496 | this.cancelTimer_();
|
17497 | this.tech_.setCurrentTime(currentTime);
|
17498 |
|
17499 | 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.');
|
17500 |
|
17501 |
|
17502 | this.tech_.trigger({ type: 'usage', name: 'hls-unknown-waiting' });
|
17503 | return;
|
17504 | }
|
17505 | }
|
17506 |
|
17507 | |
17508 |
|
17509 |
|
17510 |
|
17511 |
|
17512 |
|
17513 |
|
17514 |
|
17515 |
|
17516 | }, {
|
17517 | key: 'techWaiting_',
|
17518 | value: function techWaiting_() {
|
17519 | var seekable = this.seekable();
|
17520 | var currentTime = this.tech_.currentTime();
|
17521 |
|
17522 | if (this.tech_.seeking() && this.fixesBadSeeks_()) {
|
17523 |
|
17524 | return true;
|
17525 | }
|
17526 |
|
17527 | if (this.tech_.seeking() || this.timer_ !== null) {
|
17528 |
|
17529 | return true;
|
17530 | }
|
17531 |
|
17532 | if (this.beforeSeekableWindow_(seekable, currentTime)) {
|
17533 | var livePoint = seekable.end(seekable.length - 1);
|
17534 |
|
17535 | this.logger_('Fell out of live window at time ' + currentTime + '. Seeking to ' + ('live point (seekable end) ' + livePoint));
|
17536 | this.cancelTimer_();
|
17537 | this.tech_.setCurrentTime(livePoint);
|
17538 |
|
17539 |
|
17540 | this.tech_.trigger({ type: 'usage', name: 'hls-live-resync' });
|
17541 | return true;
|
17542 | }
|
17543 |
|
17544 | var buffered = this.tech_.buffered();
|
17545 | var nextRange = findNextRange(buffered, currentTime);
|
17546 |
|
17547 | if (this.videoUnderflow_(nextRange, buffered, currentTime)) {
|
17548 |
|
17549 |
|
17550 |
|
17551 |
|
17552 | this.cancelTimer_();
|
17553 | this.tech_.setCurrentTime(currentTime);
|
17554 |
|
17555 |
|
17556 | this.tech_.trigger({ type: 'usage', name: 'hls-video-underflow' });
|
17557 | return true;
|
17558 | }
|
17559 |
|
17560 |
|
17561 | if (nextRange.length > 0) {
|
17562 | var difference = nextRange.start(0) - currentTime;
|
17563 |
|
17564 | this.logger_('Stopped at ' + currentTime + ', setting timer for ' + difference + ', seeking ' + ('to ' + nextRange.start(0)));
|
17565 |
|
17566 | this.timer_ = setTimeout(this.skipTheGap_.bind(this), difference * 1000, currentTime);
|
17567 | return true;
|
17568 | }
|
17569 |
|
17570 |
|
17571 | return false;
|
17572 | }
|
17573 | }, {
|
17574 | key: 'afterSeekableWindow_',
|
17575 | value: function afterSeekableWindow_(seekable, currentTime, playlist) {
|
17576 | var allowSeeksWithinUnsafeLiveWindow = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
17577 |
|
17578 | if (!seekable.length) {
|
17579 |
|
17580 | return false;
|
17581 | }
|
17582 |
|
17583 | var allowedEnd = seekable.end(seekable.length - 1) + SAFE_TIME_DELTA;
|
17584 | var isLive = !playlist.endList;
|
17585 |
|
17586 | if (isLive && allowSeeksWithinUnsafeLiveWindow) {
|
17587 | allowedEnd = seekable.end(seekable.length - 1) + playlist.targetDuration * 3;
|
17588 | }
|
17589 |
|
17590 | if (currentTime > allowedEnd) {
|
17591 | return true;
|
17592 | }
|
17593 |
|
17594 | return false;
|
17595 | }
|
17596 | }, {
|
17597 | key: 'beforeSeekableWindow_',
|
17598 | value: function beforeSeekableWindow_(seekable, currentTime) {
|
17599 | if (seekable.length &&
|
17600 |
|
17601 | seekable.start(0) > 0 && currentTime < seekable.start(0) - SAFE_TIME_DELTA) {
|
17602 | return true;
|
17603 | }
|
17604 |
|
17605 | return false;
|
17606 | }
|
17607 | }, {
|
17608 | key: 'videoUnderflow_',
|
17609 | value: function videoUnderflow_(nextRange, buffered, currentTime) {
|
17610 | if (nextRange.length === 0) {
|
17611 |
|
17612 |
|
17613 | var gap = this.gapFromVideoUnderflow_(buffered, currentTime);
|
17614 |
|
17615 | if (gap) {
|
17616 | this.logger_('Encountered a gap in video from ' + gap.start + ' to ' + gap.end + '. ' + ('Seeking to current time ' + currentTime));
|
17617 |
|
17618 | return true;
|
17619 | }
|
17620 | }
|
17621 |
|
17622 | return false;
|
17623 | }
|
17624 |
|
17625 | |
17626 |
|
17627 |
|
17628 |
|
17629 |
|
17630 |
|
17631 |
|
17632 | }, {
|
17633 | key: 'skipTheGap_',
|
17634 | value: function skipTheGap_(scheduledCurrentTime) {
|
17635 | var buffered = this.tech_.buffered();
|
17636 | var currentTime = this.tech_.currentTime();
|
17637 | var nextRange = findNextRange(buffered, currentTime);
|
17638 |
|
17639 | this.cancelTimer_();
|
17640 |
|
17641 | if (nextRange.length === 0 || currentTime !== scheduledCurrentTime) {
|
17642 | return;
|
17643 | }
|
17644 |
|
17645 | this.logger_('skipTheGap_:', 'currentTime:', currentTime, 'scheduled currentTime:', scheduledCurrentTime, 'nextRange start:', nextRange.start(0));
|
17646 |
|
17647 |
|
17648 | this.tech_.setCurrentTime(nextRange.start(0) + TIME_FUDGE_FACTOR);
|
17649 |
|
17650 | this.tech_.trigger({ type: 'usage', name: 'hls-gap-skip' });
|
17651 | }
|
17652 | }, {
|
17653 | key: 'gapFromVideoUnderflow_',
|
17654 | value: function gapFromVideoUnderflow_(buffered, currentTime) {
|
17655 |
|
17656 |
|
17657 |
|
17658 |
|
17659 |
|
17660 |
|
17661 |
|
17662 |
|
17663 |
|
17664 |
|
17665 |
|
17666 |
|
17667 |
|
17668 |
|
17669 |
|
17670 |
|
17671 |
|
17672 |
|
17673 |
|
17674 |
|
17675 |
|
17676 |
|
17677 | var gaps = findGaps(buffered);
|
17678 |
|
17679 | for (var i = 0; i < gaps.length; i++) {
|
17680 | var start = gaps.start(i);
|
17681 | var end = gaps.end(i);
|
17682 |
|
17683 |
|
17684 | if (currentTime - start < 4 && currentTime - start > 2) {
|
17685 | return {
|
17686 | start: start,
|
17687 | end: end
|
17688 | };
|
17689 | }
|
17690 | }
|
17691 |
|
17692 | return null;
|
17693 | }
|
17694 | }]);
|
17695 | return PlaybackWatcher;
|
17696 | }();
|
17697 |
|
17698 | var defaultOptions = {
|
17699 | errorInterval: 30,
|
17700 | getSource: function getSource(next) {
|
17701 | var tech = this.tech({ IWillNotUseThisInPlugins: true });
|
17702 | var sourceObj = tech.currentSource_;
|
17703 |
|
17704 | return next(sourceObj);
|
17705 | }
|
17706 | };
|
17707 |
|
17708 |
|
17709 |
|
17710 |
|
17711 |
|
17712 |
|
17713 |
|
17714 |
|
17715 | var initPlugin = function initPlugin(player, options) {
|
17716 | var lastCalled = 0;
|
17717 | var seekTo = 0;
|
17718 | var localOptions = videojs.mergeOptions(defaultOptions, options);
|
17719 |
|
17720 | player.ready(function () {
|
17721 | player.trigger({ type: 'usage', name: 'hls-error-reload-initialized' });
|
17722 | });
|
17723 |
|
17724 | |
17725 |
|
17726 |
|
17727 |
|
17728 |
|
17729 |
|
17730 | var loadedMetadataHandler = function loadedMetadataHandler() {
|
17731 | if (seekTo) {
|
17732 | player.currentTime(seekTo);
|
17733 | }
|
17734 | };
|
17735 |
|
17736 | |
17737 |
|
17738 |
|
17739 |
|
17740 |
|
17741 |
|
17742 | var setSource = function setSource(sourceObj) {
|
17743 | if (sourceObj === null || sourceObj === undefined) {
|
17744 | return;
|
17745 | }
|
17746 | seekTo = player.duration() !== Infinity && player.currentTime() || 0;
|
17747 |
|
17748 | player.one('loadedmetadata', loadedMetadataHandler);
|
17749 |
|
17750 | player.src(sourceObj);
|
17751 | player.trigger({ type: 'usage', name: 'hls-error-reload' });
|
17752 | player.play();
|
17753 | };
|
17754 |
|
17755 | |
17756 |
|
17757 |
|
17758 |
|
17759 |
|
17760 |
|
17761 | var errorHandler = function errorHandler() {
|
17762 |
|
17763 |
|
17764 | if (Date.now() - lastCalled < localOptions.errorInterval * 1000) {
|
17765 | player.trigger({ type: 'usage', name: 'hls-error-reload-canceled' });
|
17766 | return;
|
17767 | }
|
17768 |
|
17769 | if (!localOptions.getSource || typeof localOptions.getSource !== 'function') {
|
17770 | videojs.log.error('ERROR: reloadSourceOnError - The option getSource must be a function!');
|
17771 | return;
|
17772 | }
|
17773 | lastCalled = Date.now();
|
17774 |
|
17775 | return localOptions.getSource.call(player, setSource);
|
17776 | };
|
17777 |
|
17778 | |
17779 |
|
17780 |
|
17781 |
|
17782 |
|
17783 | var cleanupEvents = function cleanupEvents() {
|
17784 | player.off('loadedmetadata', loadedMetadataHandler);
|
17785 | player.off('error', errorHandler);
|
17786 | player.off('dispose', cleanupEvents);
|
17787 | };
|
17788 |
|
17789 | |
17790 |
|
17791 |
|
17792 |
|
17793 |
|
17794 |
|
17795 | var reinitPlugin = function reinitPlugin(newOptions) {
|
17796 | cleanupEvents();
|
17797 | initPlugin(player, newOptions);
|
17798 | };
|
17799 |
|
17800 | player.on('error', errorHandler);
|
17801 | player.on('dispose', cleanupEvents);
|
17802 |
|
17803 |
|
17804 |
|
17805 | player.reloadSourceOnError = reinitPlugin;
|
17806 | };
|
17807 |
|
17808 |
|
17809 |
|
17810 |
|
17811 |
|
17812 |
|
17813 |
|
17814 | var reloadSourceOnError = function reloadSourceOnError(options) {
|
17815 | initPlugin(this, options);
|
17816 | };
|
17817 |
|
17818 | var version = "1.13.2";
|
17819 |
|
17820 |
|
17821 |
|
17822 |
|
17823 |
|
17824 |
|
17825 |
|
17826 |
|
17827 | var Hls$1 = {
|
17828 | PlaylistLoader: PlaylistLoader,
|
17829 | Playlist: Playlist,
|
17830 | Decrypter: aesDecrypter.Decrypter,
|
17831 | AsyncStream: aesDecrypter.AsyncStream,
|
17832 | decrypt: aesDecrypter.decrypt,
|
17833 | utils: utils,
|
17834 |
|
17835 | STANDARD_PLAYLIST_SELECTOR: lastBandwidthSelector,
|
17836 | INITIAL_PLAYLIST_SELECTOR: lowestBitrateCompatibleVariantSelector,
|
17837 | comparePlaylistBandwidth: comparePlaylistBandwidth,
|
17838 | comparePlaylistResolution: comparePlaylistResolution,
|
17839 |
|
17840 | xhr: xhrFactory()
|
17841 | };
|
17842 |
|
17843 |
|
17844 | ['GOAL_BUFFER_LENGTH', 'MAX_GOAL_BUFFER_LENGTH', 'GOAL_BUFFER_LENGTH_RATE', 'BUFFER_LOW_WATER_LINE', 'MAX_BUFFER_LOW_WATER_LINE', 'BUFFER_LOW_WATER_LINE_RATE', 'BANDWIDTH_VARIANCE'].forEach(function (prop) {
|
17845 | Object.defineProperty(Hls$1, prop, {
|
17846 | get: function get$$1() {
|
17847 | videojs.log.warn('using Hls.' + prop + ' is UNSAFE be sure you know what you are doing');
|
17848 | return Config[prop];
|
17849 | },
|
17850 | set: function set$$1(value) {
|
17851 | videojs.log.warn('using Hls.' + prop + ' is UNSAFE be sure you know what you are doing');
|
17852 |
|
17853 | if (typeof value !== 'number' || value < 0) {
|
17854 | videojs.log.warn('value of Hls.' + prop + ' must be greater than or equal to 0');
|
17855 | return;
|
17856 | }
|
17857 |
|
17858 | Config[prop] = value;
|
17859 | }
|
17860 | });
|
17861 | });
|
17862 |
|
17863 | var LOCAL_STORAGE_KEY = 'videojs-vhs';
|
17864 |
|
17865 | var simpleTypeFromSourceType = function simpleTypeFromSourceType(type) {
|
17866 | var mpegurlRE = /^(audio|video|application)\/(x-|vnd\.apple\.)?mpegurl/i;
|
17867 |
|
17868 | if (mpegurlRE.test(type)) {
|
17869 | return 'hls';
|
17870 | }
|
17871 |
|
17872 | var dashRE = /^application\/dash\+xml/i;
|
17873 |
|
17874 | if (dashRE.test(type)) {
|
17875 | return 'dash';
|
17876 | }
|
17877 |
|
17878 | return null;
|
17879 | };
|
17880 |
|
17881 |
|
17882 |
|
17883 |
|
17884 |
|
17885 |
|
17886 |
|
17887 |
|
17888 | var handleHlsMediaChange = function handleHlsMediaChange(qualityLevels, playlistLoader) {
|
17889 | var newPlaylist = playlistLoader.media();
|
17890 | var selectedIndex = -1;
|
17891 |
|
17892 | for (var i = 0; i < qualityLevels.length; i++) {
|
17893 | if (qualityLevels[i].id === newPlaylist.id) {
|
17894 | selectedIndex = i;
|
17895 | break;
|
17896 | }
|
17897 | }
|
17898 |
|
17899 | qualityLevels.selectedIndex_ = selectedIndex;
|
17900 | qualityLevels.trigger({
|
17901 | selectedIndex: selectedIndex,
|
17902 | type: 'change'
|
17903 | });
|
17904 | };
|
17905 |
|
17906 |
|
17907 |
|
17908 |
|
17909 |
|
17910 |
|
17911 |
|
17912 |
|
17913 | var handleHlsLoadedMetadata = function handleHlsLoadedMetadata(qualityLevels, hls) {
|
17914 | hls.representations().forEach(function (rep) {
|
17915 | qualityLevels.addQualityLevel(rep);
|
17916 | });
|
17917 | handleHlsMediaChange(qualityLevels, hls.playlists);
|
17918 | };
|
17919 |
|
17920 |
|
17921 |
|
17922 | Hls$1.canPlaySource = function () {
|
17923 | return videojs.log.warn('HLS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
|
17924 | };
|
17925 |
|
17926 | var emeKeySystems = function emeKeySystems(keySystemOptions, mainSegmentLoader, audioSegmentLoader) {
|
17927 | if (!keySystemOptions) {
|
17928 | return keySystemOptions;
|
17929 | }
|
17930 |
|
17931 | var videoMimeType = void 0;
|
17932 | var audioMimeType = void 0;
|
17933 |
|
17934 |
|
17935 |
|
17936 |
|
17937 | if (audioSegmentLoader.mimeType_) {
|
17938 | videoMimeType = mainSegmentLoader.mimeType_;
|
17939 | audioMimeType = audioSegmentLoader.mimeType_;
|
17940 |
|
17941 |
|
17942 |
|
17943 |
|
17944 |
|
17945 | } else {
|
17946 | var parsedMimeType = parseContentType(mainSegmentLoader.mimeType_);
|
17947 | var codecs = parsedMimeType.parameters.codecs.split(',');
|
17948 |
|
17949 | var audioCodec = void 0;
|
17950 | var videoCodec = void 0;
|
17951 |
|
17952 | codecs.forEach(function (codec) {
|
17953 | codec = codec.trim();
|
17954 |
|
17955 | if (isAudioCodec(codec)) {
|
17956 | audioCodec = codec;
|
17957 | } else if (isVideoCodec(codec)) {
|
17958 | videoCodec = codec;
|
17959 | }
|
17960 | });
|
17961 |
|
17962 | videoMimeType = parsedMimeType.type + '; codecs="' + videoCodec + '"';
|
17963 | audioMimeType = parsedMimeType.type.replace('video', 'audio') + '; codecs="' + audioCodec + '"';
|
17964 | }
|
17965 |
|
17966 |
|
17967 | var keySystemContentTypes = {};
|
17968 | var videoPlaylist = mainSegmentLoader.playlist_;
|
17969 |
|
17970 | for (var keySystem in keySystemOptions) {
|
17971 | keySystemContentTypes[keySystem] = {
|
17972 | audioContentType: audioMimeType,
|
17973 | videoContentType: videoMimeType
|
17974 | };
|
17975 |
|
17976 | if (videoPlaylist.contentProtection && videoPlaylist.contentProtection[keySystem] && videoPlaylist.contentProtection[keySystem].pssh) {
|
17977 | keySystemContentTypes[keySystem].pssh = videoPlaylist.contentProtection[keySystem].pssh;
|
17978 | }
|
17979 |
|
17980 |
|
17981 |
|
17982 | if (typeof keySystemOptions[keySystem] === 'string') {
|
17983 | keySystemContentTypes[keySystem].url = keySystemOptions[keySystem];
|
17984 | }
|
17985 | }
|
17986 |
|
17987 | return videojs.mergeOptions(keySystemOptions, keySystemContentTypes);
|
17988 | };
|
17989 |
|
17990 | var setupEmeOptions = function setupEmeOptions(hlsHandler) {
|
17991 | var mainSegmentLoader = hlsHandler.masterPlaylistController_.mainSegmentLoader_;
|
17992 | var audioSegmentLoader = hlsHandler.masterPlaylistController_.audioSegmentLoader_;
|
17993 |
|
17994 | var player = videojs.players[hlsHandler.tech_.options_.playerId];
|
17995 |
|
17996 | if (player.eme) {
|
17997 | var sourceOptions = emeKeySystems(hlsHandler.source_.keySystems, mainSegmentLoader, audioSegmentLoader);
|
17998 |
|
17999 | if (sourceOptions) {
|
18000 | player.currentSource().keySystems = sourceOptions;
|
18001 |
|
18002 |
|
18003 |
|
18004 | if (!(videojs.browser.IE_VERSION === 11) && player.eme.initializeMediaKeys) {
|
18005 | player.eme.initializeMediaKeys();
|
18006 | }
|
18007 | }
|
18008 | }
|
18009 | };
|
18010 |
|
18011 | var getVhsLocalStorage = function getVhsLocalStorage() {
|
18012 | if (!window.localStorage) {
|
18013 | return null;
|
18014 | }
|
18015 |
|
18016 | var storedObject = window.localStorage.getItem(LOCAL_STORAGE_KEY);
|
18017 |
|
18018 | if (!storedObject) {
|
18019 | return null;
|
18020 | }
|
18021 |
|
18022 | try {
|
18023 | return JSON.parse(storedObject);
|
18024 | } catch (e) {
|
18025 |
|
18026 | return null;
|
18027 | }
|
18028 | };
|
18029 |
|
18030 | var updateVhsLocalStorage = function updateVhsLocalStorage(options) {
|
18031 | if (!window.localStorage) {
|
18032 | return false;
|
18033 | }
|
18034 |
|
18035 | var objectToStore = getVhsLocalStorage();
|
18036 |
|
18037 | objectToStore = objectToStore ? videojs.mergeOptions(objectToStore, options) : options;
|
18038 |
|
18039 | try {
|
18040 | window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(objectToStore));
|
18041 | } catch (e) {
|
18042 |
|
18043 |
|
18044 |
|
18045 |
|
18046 | return false;
|
18047 | }
|
18048 |
|
18049 | return objectToStore;
|
18050 | };
|
18051 |
|
18052 |
|
18053 |
|
18054 |
|
18055 | Hls$1.supportsNativeHls = function () {
|
18056 | var video = document.createElement('video');
|
18057 |
|
18058 |
|
18059 | if (!videojs.getTech('Html5').isSupported()) {
|
18060 | return false;
|
18061 | }
|
18062 |
|
18063 |
|
18064 | var canPlay = [
|
18065 |
|
18066 | 'application/vnd.apple.mpegurl',
|
18067 |
|
18068 | 'audio/mpegurl',
|
18069 |
|
18070 | 'audio/x-mpegurl',
|
18071 |
|
18072 | 'application/x-mpegurl',
|
18073 |
|
18074 | 'video/x-mpegurl', 'video/mpegurl', 'application/mpegurl'];
|
18075 |
|
18076 | return canPlay.some(function (canItPlay) {
|
18077 | return (/maybe|probably/i.test(video.canPlayType(canItPlay))
|
18078 | );
|
18079 | });
|
18080 | }();
|
18081 |
|
18082 | Hls$1.supportsNativeDash = function () {
|
18083 | if (!videojs.getTech('Html5').isSupported()) {
|
18084 | return false;
|
18085 | }
|
18086 |
|
18087 | return (/maybe|probably/i.test(document.createElement('video').canPlayType('application/dash+xml'))
|
18088 | );
|
18089 | }();
|
18090 |
|
18091 | Hls$1.supportsTypeNatively = function (type) {
|
18092 | if (type === 'hls') {
|
18093 | return Hls$1.supportsNativeHls;
|
18094 | }
|
18095 |
|
18096 | if (type === 'dash') {
|
18097 | return Hls$1.supportsNativeDash;
|
18098 | }
|
18099 |
|
18100 | return false;
|
18101 | };
|
18102 |
|
18103 |
|
18104 |
|
18105 |
|
18106 |
|
18107 | Hls$1.isSupported = function () {
|
18108 | return videojs.log.warn('HLS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
|
18109 | };
|
18110 |
|
18111 | var Component = videojs.getComponent('Component');
|
18112 |
|
18113 |
|
18114 |
|
18115 |
|
18116 |
|
18117 |
|
18118 |
|
18119 |
|
18120 |
|
18121 |
|
18122 |
|
18123 |
|
18124 | var HlsHandler = function (_Component) {
|
18125 | inherits(HlsHandler, _Component);
|
18126 |
|
18127 | function HlsHandler(source, tech, options) {
|
18128 | classCallCheck(this, HlsHandler);
|
18129 |
|
18130 |
|
18131 |
|
18132 | var _this = possibleConstructorReturn(this, (HlsHandler.__proto__ || Object.getPrototypeOf(HlsHandler)).call(this, tech, options.hls));
|
18133 |
|
18134 | if (tech.options_ && tech.options_.playerId) {
|
18135 | var _player = videojs(tech.options_.playerId);
|
18136 |
|
18137 | if (!_player.hasOwnProperty('hls')) {
|
18138 | Object.defineProperty(_player, 'hls', {
|
18139 | get: function get$$1() {
|
18140 | videojs.log.warn('player.hls is deprecated. Use player.tech().hls instead.');
|
18141 | tech.trigger({ type: 'usage', name: 'hls-player-access' });
|
18142 | return _this;
|
18143 | },
|
18144 | configurable: true
|
18145 | });
|
18146 | }
|
18147 |
|
18148 |
|
18149 |
|
18150 |
|
18151 |
|
18152 |
|
18153 | _player.vhs = _this;
|
18154 |
|
18155 | _player.dash = _this;
|
18156 |
|
18157 | _this.player_ = _player;
|
18158 | }
|
18159 |
|
18160 | _this.tech_ = tech;
|
18161 | _this.source_ = source;
|
18162 | _this.stats = {};
|
18163 | _this.ignoreNextSeekingEvent_ = false;
|
18164 | _this.setOptions_();
|
18165 |
|
18166 | if (_this.options_.overrideNative && tech.overrideNativeAudioTracks && tech.overrideNativeVideoTracks) {
|
18167 | tech.overrideNativeAudioTracks(true);
|
18168 | tech.overrideNativeVideoTracks(true);
|
18169 | } else if (_this.options_.overrideNative && (tech.featuresNativeVideoTracks || tech.featuresNativeAudioTracks)) {
|
18170 |
|
18171 |
|
18172 | throw new Error('Overriding native HLS requires emulated tracks. ' + 'See https://git.io/vMpjB');
|
18173 | }
|
18174 |
|
18175 |
|
18176 |
|
18177 | _this.on(document, ['fullscreenchange', 'webkitfullscreenchange', 'mozfullscreenchange', 'MSFullscreenChange'], function (event) {
|
18178 | var fullscreenElement = document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement;
|
18179 |
|
18180 | if (fullscreenElement && fullscreenElement.contains(_this.tech_.el())) {
|
18181 | _this.masterPlaylistController_.smoothQualityChange_();
|
18182 | }
|
18183 | });
|
18184 |
|
18185 | _this.on(_this.tech_, 'seeking', function () {
|
18186 | if (this.ignoreNextSeekingEvent_) {
|
18187 | this.ignoreNextSeekingEvent_ = false;
|
18188 | return;
|
18189 | }
|
18190 |
|
18191 | this.setCurrentTime(this.tech_.currentTime());
|
18192 | });
|
18193 |
|
18194 | _this.on(_this.tech_, 'error', function () {
|
18195 | if (this.masterPlaylistController_) {
|
18196 | this.masterPlaylistController_.pauseLoading();
|
18197 | }
|
18198 | });
|
18199 |
|
18200 | _this.on(_this.tech_, 'play', _this.play);
|
18201 | return _this;
|
18202 | }
|
18203 |
|
18204 | createClass(HlsHandler, [{
|
18205 | key: 'setOptions_',
|
18206 | value: function setOptions_() {
|
18207 | var _this2 = this;
|
18208 |
|
18209 |
|
18210 | this.options_.withCredentials = this.options_.withCredentials || false;
|
18211 | this.options_.handleManifestRedirects = this.options_.handleManifestRedirects || false;
|
18212 | this.options_.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions === false ? false : true;
|
18213 | this.options_.useDevicePixelRatio = this.options_.useDevicePixelRatio || false;
|
18214 | this.options_.smoothQualityChange = this.options_.smoothQualityChange || false;
|
18215 | this.options_.useBandwidthFromLocalStorage = typeof this.source_.useBandwidthFromLocalStorage !== 'undefined' ? this.source_.useBandwidthFromLocalStorage : this.options_.useBandwidthFromLocalStorage || false;
|
18216 | this.options_.customTagParsers = this.options_.customTagParsers || [];
|
18217 | this.options_.customTagMappers = this.options_.customTagMappers || [];
|
18218 | this.options_.cacheEncryptionKeys = this.options_.cacheEncryptionKeys || false;
|
18219 |
|
18220 | if (typeof this.options_.blacklistDuration !== 'number') {
|
18221 | this.options_.blacklistDuration = 5 * 60;
|
18222 | }
|
18223 |
|
18224 | if (typeof this.options_.bandwidth !== 'number') {
|
18225 | if (this.options_.useBandwidthFromLocalStorage) {
|
18226 | var storedObject = getVhsLocalStorage();
|
18227 |
|
18228 | if (storedObject && storedObject.bandwidth) {
|
18229 | this.options_.bandwidth = storedObject.bandwidth;
|
18230 | this.tech_.trigger({ type: 'usage', name: 'hls-bandwidth-from-local-storage' });
|
18231 | }
|
18232 | if (storedObject && storedObject.throughput) {
|
18233 | this.options_.throughput = storedObject.throughput;
|
18234 | this.tech_.trigger({ type: 'usage', name: 'hls-throughput-from-local-storage' });
|
18235 | }
|
18236 | }
|
18237 | }
|
18238 |
|
18239 |
|
18240 | if (typeof this.options_.bandwidth !== 'number') {
|
18241 | this.options_.bandwidth = Config.INITIAL_BANDWIDTH;
|
18242 | }
|
18243 |
|
18244 |
|
18245 |
|
18246 | this.options_.enableLowInitialPlaylist = this.options_.enableLowInitialPlaylist && this.options_.bandwidth === Config.INITIAL_BANDWIDTH;
|
18247 |
|
18248 |
|
18249 | ['withCredentials', 'useDevicePixelRatio', 'limitRenditionByPlayerDimensions', 'bandwidth', 'smoothQualityChange', 'customTagParsers', 'customTagMappers', 'handleManifestRedirects', 'cacheEncryptionKeys'].forEach(function (option) {
|
18250 | if (typeof _this2.source_[option] !== 'undefined') {
|
18251 | _this2.options_[option] = _this2.source_[option];
|
18252 | }
|
18253 | });
|
18254 |
|
18255 | this.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions;
|
18256 | this.useDevicePixelRatio = this.options_.useDevicePixelRatio;
|
18257 | }
|
18258 | |
18259 |
|
18260 |
|
18261 |
|
18262 |
|
18263 |
|
18264 | }, {
|
18265 | key: 'src',
|
18266 | value: function src(_src, type) {
|
18267 | var _this3 = this;
|
18268 |
|
18269 |
|
18270 | if (!_src) {
|
18271 | return;
|
18272 | }
|
18273 | this.setOptions_();
|
18274 |
|
18275 | this.options_.url = this.source_.src;
|
18276 | this.options_.tech = this.tech_;
|
18277 | this.options_.externHls = Hls$1;
|
18278 | this.options_.sourceType = simpleTypeFromSourceType(type);
|
18279 |
|
18280 | this.options_.seekTo = function (time) {
|
18281 | _this3.tech_.setCurrentTime(time);
|
18282 | };
|
18283 |
|
18284 | this.masterPlaylistController_ = new MasterPlaylistController(this.options_);
|
18285 | this.playbackWatcher_ = new PlaybackWatcher(videojs.mergeOptions(this.options_, {
|
18286 | seekable: function seekable$$1() {
|
18287 | return _this3.seekable();
|
18288 | },
|
18289 | media: function media() {
|
18290 | return _this3.masterPlaylistController_.media();
|
18291 | }
|
18292 | }));
|
18293 |
|
18294 | this.masterPlaylistController_.on('error', function () {
|
18295 | var player = videojs.players[_this3.tech_.options_.playerId];
|
18296 |
|
18297 | player.error(_this3.masterPlaylistController_.error);
|
18298 | });
|
18299 |
|
18300 |
|
18301 |
|
18302 | this.masterPlaylistController_.selectPlaylist = this.selectPlaylist ? this.selectPlaylist.bind(this) : Hls$1.STANDARD_PLAYLIST_SELECTOR.bind(this);
|
18303 |
|
18304 | this.masterPlaylistController_.selectInitialPlaylist = Hls$1.INITIAL_PLAYLIST_SELECTOR.bind(this);
|
18305 |
|
18306 |
|
18307 | this.playlists = this.masterPlaylistController_.masterPlaylistLoader_;
|
18308 | this.mediaSource = this.masterPlaylistController_.mediaSource;
|
18309 |
|
18310 |
|
18311 |
|
18312 |
|
18313 | Object.defineProperties(this, {
|
18314 | selectPlaylist: {
|
18315 | get: function get$$1() {
|
18316 | return this.masterPlaylistController_.selectPlaylist;
|
18317 | },
|
18318 | set: function set$$1(selectPlaylist) {
|
18319 | this.masterPlaylistController_.selectPlaylist = selectPlaylist.bind(this);
|
18320 | }
|
18321 | },
|
18322 | throughput: {
|
18323 | get: function get$$1() {
|
18324 | return this.masterPlaylistController_.mainSegmentLoader_.throughput.rate;
|
18325 | },
|
18326 | set: function set$$1(throughput) {
|
18327 | this.masterPlaylistController_.mainSegmentLoader_.throughput.rate = throughput;
|
18328 |
|
18329 |
|
18330 | this.masterPlaylistController_.mainSegmentLoader_.throughput.count = 1;
|
18331 | }
|
18332 | },
|
18333 | bandwidth: {
|
18334 | get: function get$$1() {
|
18335 | return this.masterPlaylistController_.mainSegmentLoader_.bandwidth;
|
18336 | },
|
18337 | set: function set$$1(bandwidth) {
|
18338 | this.masterPlaylistController_.mainSegmentLoader_.bandwidth = bandwidth;
|
18339 |
|
18340 |
|
18341 |
|
18342 | this.masterPlaylistController_.mainSegmentLoader_.throughput = {
|
18343 | rate: 0,
|
18344 | count: 0
|
18345 | };
|
18346 | }
|
18347 | },
|
18348 | |
18349 |
|
18350 |
|
18351 |
|
18352 |
|
18353 |
|
18354 |
|
18355 |
|
18356 |
|
18357 | systemBandwidth: {
|
18358 | get: function get$$1() {
|
18359 | var invBandwidth = 1 / (this.bandwidth || 1);
|
18360 | var invThroughput = void 0;
|
18361 |
|
18362 | if (this.throughput > 0) {
|
18363 | invThroughput = 1 / this.throughput;
|
18364 | } else {
|
18365 | invThroughput = 0;
|
18366 | }
|
18367 |
|
18368 | var systemBitrate = Math.floor(1 / (invBandwidth + invThroughput));
|
18369 |
|
18370 | return systemBitrate;
|
18371 | },
|
18372 | set: function set$$1() {
|
18373 | videojs.log.error('The "systemBandwidth" property is read-only');
|
18374 | }
|
18375 | }
|
18376 | });
|
18377 |
|
18378 | if (this.options_.bandwidth) {
|
18379 | this.bandwidth = this.options_.bandwidth;
|
18380 | }
|
18381 | if (this.options_.throughput) {
|
18382 | this.throughput = this.options_.throughput;
|
18383 | }
|
18384 |
|
18385 | Object.defineProperties(this.stats, {
|
18386 | bandwidth: {
|
18387 | get: function get$$1() {
|
18388 | return _this3.bandwidth || 0;
|
18389 | },
|
18390 | enumerable: true
|
18391 | },
|
18392 | mediaRequests: {
|
18393 | get: function get$$1() {
|
18394 | return _this3.masterPlaylistController_.mediaRequests_() || 0;
|
18395 | },
|
18396 | enumerable: true
|
18397 | },
|
18398 | mediaRequestsAborted: {
|
18399 | get: function get$$1() {
|
18400 | return _this3.masterPlaylistController_.mediaRequestsAborted_() || 0;
|
18401 | },
|
18402 | enumerable: true
|
18403 | },
|
18404 | mediaRequestsTimedout: {
|
18405 | get: function get$$1() {
|
18406 | return _this3.masterPlaylistController_.mediaRequestsTimedout_() || 0;
|
18407 | },
|
18408 | enumerable: true
|
18409 | },
|
18410 | mediaRequestsErrored: {
|
18411 | get: function get$$1() {
|
18412 | return _this3.masterPlaylistController_.mediaRequestsErrored_() || 0;
|
18413 | },
|
18414 | enumerable: true
|
18415 | },
|
18416 | mediaTransferDuration: {
|
18417 | get: function get$$1() {
|
18418 | return _this3.masterPlaylistController_.mediaTransferDuration_() || 0;
|
18419 | },
|
18420 | enumerable: true
|
18421 | },
|
18422 | mediaBytesTransferred: {
|
18423 | get: function get$$1() {
|
18424 | return _this3.masterPlaylistController_.mediaBytesTransferred_() || 0;
|
18425 | },
|
18426 | enumerable: true
|
18427 | },
|
18428 | mediaSecondsLoaded: {
|
18429 | get: function get$$1() {
|
18430 | return _this3.masterPlaylistController_.mediaSecondsLoaded_() || 0;
|
18431 | },
|
18432 | enumerable: true
|
18433 | },
|
18434 | buffered: {
|
18435 | get: function get$$1() {
|
18436 | return timeRangesToArray(_this3.tech_.buffered());
|
18437 | },
|
18438 | enumerable: true
|
18439 | },
|
18440 | currentTime: {
|
18441 | get: function get$$1() {
|
18442 | return _this3.tech_.currentTime();
|
18443 | },
|
18444 | enumerable: true
|
18445 | },
|
18446 | currentSource: {
|
18447 | get: function get$$1() {
|
18448 | return _this3.tech_.currentSource_;
|
18449 | },
|
18450 | enumerable: true
|
18451 | },
|
18452 | currentTech: {
|
18453 | get: function get$$1() {
|
18454 | return _this3.tech_.name_;
|
18455 | },
|
18456 | enumerable: true
|
18457 | },
|
18458 | duration: {
|
18459 | get: function get$$1() {
|
18460 | return _this3.tech_.duration();
|
18461 | },
|
18462 | enumerable: true
|
18463 | },
|
18464 | master: {
|
18465 | get: function get$$1() {
|
18466 | return _this3.playlists.master;
|
18467 | },
|
18468 | enumerable: true
|
18469 | },
|
18470 | playerDimensions: {
|
18471 | get: function get$$1() {
|
18472 | return _this3.tech_.currentDimensions();
|
18473 | },
|
18474 | enumerable: true
|
18475 | },
|
18476 | seekable: {
|
18477 | get: function get$$1() {
|
18478 | return timeRangesToArray(_this3.tech_.seekable());
|
18479 | },
|
18480 | enumerable: true
|
18481 | },
|
18482 | timestamp: {
|
18483 | get: function get$$1() {
|
18484 | return Date.now();
|
18485 | },
|
18486 | enumerable: true
|
18487 | },
|
18488 | videoPlaybackQuality: {
|
18489 | get: function get$$1() {
|
18490 | return _this3.tech_.getVideoPlaybackQuality();
|
18491 | },
|
18492 | enumerable: true
|
18493 | }
|
18494 | });
|
18495 |
|
18496 | this.tech_.one('canplay', this.masterPlaylistController_.setupFirstPlay.bind(this.masterPlaylistController_));
|
18497 |
|
18498 | this.tech_.on('bandwidthupdate', function () {
|
18499 | if (_this3.options_.useBandwidthFromLocalStorage) {
|
18500 | updateVhsLocalStorage({
|
18501 | bandwidth: _this3.bandwidth,
|
18502 | throughput: Math.round(_this3.throughput)
|
18503 | });
|
18504 | }
|
18505 | });
|
18506 |
|
18507 | this.masterPlaylistController_.on('selectedinitialmedia', function () {
|
18508 |
|
18509 | renditionSelectionMixin(_this3);
|
18510 | setupEmeOptions(_this3);
|
18511 | });
|
18512 |
|
18513 |
|
18514 |
|
18515 | this.on(this.masterPlaylistController_, 'progress', function () {
|
18516 | this.tech_.trigger('progress');
|
18517 | });
|
18518 |
|
18519 |
|
18520 |
|
18521 | this.on(this.masterPlaylistController_, 'firstplay', function () {
|
18522 | this.ignoreNextSeekingEvent_ = true;
|
18523 | });
|
18524 |
|
18525 | this.setupQualityLevels_();
|
18526 |
|
18527 |
|
18528 |
|
18529 | if (!this.tech_.el()) {
|
18530 | return;
|
18531 | }
|
18532 |
|
18533 | this.tech_.src(videojs.URL.createObjectURL(this.masterPlaylistController_.mediaSource));
|
18534 | }
|
18535 |
|
18536 | |
18537 |
|
18538 |
|
18539 |
|
18540 |
|
18541 |
|
18542 |
|
18543 | }, {
|
18544 | key: 'setupQualityLevels_',
|
18545 | value: function setupQualityLevels_() {
|
18546 | var _this4 = this;
|
18547 |
|
18548 | var player = videojs.players[this.tech_.options_.playerId];
|
18549 |
|
18550 |
|
18551 |
|
18552 | if (!player || !player.qualityLevels || this.qualityLevels_) {
|
18553 | return;
|
18554 | }
|
18555 |
|
18556 | this.qualityLevels_ = player.qualityLevels();
|
18557 |
|
18558 | this.masterPlaylistController_.on('selectedinitialmedia', function () {
|
18559 | handleHlsLoadedMetadata(_this4.qualityLevels_, _this4);
|
18560 | });
|
18561 |
|
18562 | this.playlists.on('mediachange', function () {
|
18563 | handleHlsMediaChange(_this4.qualityLevels_, _this4.playlists);
|
18564 | });
|
18565 | }
|
18566 |
|
18567 | |
18568 |
|
18569 |
|
18570 |
|
18571 | }, {
|
18572 | key: 'play',
|
18573 | value: function play() {
|
18574 | this.masterPlaylistController_.play();
|
18575 | }
|
18576 |
|
18577 | |
18578 |
|
18579 |
|
18580 |
|
18581 | }, {
|
18582 | key: 'setCurrentTime',
|
18583 | value: function setCurrentTime(currentTime) {
|
18584 | this.masterPlaylistController_.setCurrentTime(currentTime);
|
18585 | }
|
18586 |
|
18587 | |
18588 |
|
18589 |
|
18590 |
|
18591 | }, {
|
18592 | key: 'duration',
|
18593 | value: function duration$$1() {
|
18594 | return this.masterPlaylistController_.duration();
|
18595 | }
|
18596 |
|
18597 | |
18598 |
|
18599 |
|
18600 |
|
18601 | }, {
|
18602 | key: 'seekable',
|
18603 | value: function seekable$$1() {
|
18604 | return this.masterPlaylistController_.seekable();
|
18605 | }
|
18606 |
|
18607 | |
18608 |
|
18609 |
|
18610 |
|
18611 | }, {
|
18612 | key: 'dispose',
|
18613 | value: function dispose() {
|
18614 | if (this.playbackWatcher_) {
|
18615 | this.playbackWatcher_.dispose();
|
18616 | }
|
18617 | if (this.masterPlaylistController_) {
|
18618 | this.masterPlaylistController_.dispose();
|
18619 | }
|
18620 | if (this.qualityLevels_) {
|
18621 | this.qualityLevels_.dispose();
|
18622 | }
|
18623 |
|
18624 | if (this.player_) {
|
18625 | delete this.player_.vhs;
|
18626 | delete this.player_.dash;
|
18627 | delete this.player_.hls;
|
18628 | }
|
18629 |
|
18630 | if (this.tech_ && this.tech_.hls) {
|
18631 | delete this.tech_.hls;
|
18632 | }
|
18633 |
|
18634 | get(HlsHandler.prototype.__proto__ || Object.getPrototypeOf(HlsHandler.prototype), 'dispose', this).call(this);
|
18635 | }
|
18636 | }, {
|
18637 | key: 'convertToProgramTime',
|
18638 | value: function convertToProgramTime(time, callback) {
|
18639 | return getProgramTime({
|
18640 | playlist: this.masterPlaylistController_.media(),
|
18641 | time: time,
|
18642 | callback: callback
|
18643 | });
|
18644 | }
|
18645 |
|
18646 |
|
18647 |
|
18648 | }, {
|
18649 | key: 'seekToProgramTime',
|
18650 | value: function seekToProgramTime$$1(programTime, callback) {
|
18651 | var pauseAfterSeek = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
18652 | var retryCount = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2;
|
18653 |
|
18654 | return seekToProgramTime({
|
18655 | programTime: programTime,
|
18656 | playlist: this.masterPlaylistController_.media(),
|
18657 | retryCount: retryCount,
|
18658 | pauseAfterSeek: pauseAfterSeek,
|
18659 | seekTo: this.options_.seekTo,
|
18660 | tech: this.options_.tech,
|
18661 | callback: callback
|
18662 | });
|
18663 | }
|
18664 | }]);
|
18665 | return HlsHandler;
|
18666 | }(Component);
|
18667 |
|
18668 |
|
18669 |
|
18670 |
|
18671 |
|
18672 |
|
18673 |
|
18674 |
|
18675 |
|
18676 |
|
18677 | var HlsSourceHandler = {
|
18678 | name: 'videojs-http-streaming',
|
18679 | VERSION: version,
|
18680 | canHandleSource: function canHandleSource(srcObj) {
|
18681 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
18682 |
|
18683 | var localOptions = videojs.mergeOptions(videojs.options, options);
|
18684 |
|
18685 | return HlsSourceHandler.canPlayType(srcObj.type, localOptions);
|
18686 | },
|
18687 | handleSource: function handleSource(source, tech) {
|
18688 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
18689 |
|
18690 | var localOptions = videojs.mergeOptions(videojs.options, options);
|
18691 |
|
18692 | tech.hls = new HlsHandler(source, tech, localOptions);
|
18693 | tech.hls.xhr = xhrFactory();
|
18694 |
|
18695 | tech.hls.src(source.src, source.type);
|
18696 | return tech.hls;
|
18697 | },
|
18698 | canPlayType: function canPlayType(type) {
|
18699 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
18700 |
|
18701 | var _videojs$mergeOptions = videojs.mergeOptions(videojs.options, options),
|
18702 | overrideNative = _videojs$mergeOptions.hls.overrideNative;
|
18703 |
|
18704 | var supportedType = simpleTypeFromSourceType(type);
|
18705 | var canUseMsePlayback = supportedType && (!Hls$1.supportsTypeNatively(supportedType) || overrideNative);
|
18706 |
|
18707 | return canUseMsePlayback ? 'maybe' : '';
|
18708 | }
|
18709 | };
|
18710 |
|
18711 | if (typeof videojs.MediaSource === 'undefined' || typeof videojs.URL === 'undefined') {
|
18712 | videojs.MediaSource = MediaSource;
|
18713 | videojs.URL = URL$1;
|
18714 | }
|
18715 |
|
18716 |
|
18717 | if (MediaSource.supportsNativeMediaSources()) {
|
18718 | videojs.getTech('Html5').registerSourceHandler(HlsSourceHandler, 0);
|
18719 | }
|
18720 |
|
18721 | videojs.HlsHandler = HlsHandler;
|
18722 | videojs.HlsSourceHandler = HlsSourceHandler;
|
18723 | videojs.Hls = Hls$1;
|
18724 | if (!videojs.use) {
|
18725 | videojs.registerComponent('Hls', Hls$1);
|
18726 | }
|
18727 | videojs.options.hls = videojs.options.hls || {};
|
18728 |
|
18729 | if (videojs.registerPlugin) {
|
18730 | videojs.registerPlugin('reloadSourceOnError', reloadSourceOnError);
|
18731 | } else {
|
18732 | videojs.plugin('reloadSourceOnError', reloadSourceOnError);
|
18733 | }
|
18734 |
|
18735 | exports.LOCAL_STORAGE_KEY = LOCAL_STORAGE_KEY;
|
18736 | exports.Hls = Hls$1;
|
18737 | exports.HlsHandler = HlsHandler;
|
18738 | exports.HlsSourceHandler = HlsSourceHandler;
|
18739 | exports.emeKeySystems = emeKeySystems;
|
18740 | exports.simpleTypeFromSourceType = simpleTypeFromSourceType;
|