1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | Object.defineProperty(exports, '__esModule', {
|
11 | value: true
|
12 | });
|
13 |
|
14 | var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
15 |
|
16 | var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
19 |
|
20 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
21 |
|
22 | function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
23 |
|
24 | var _resolveUrl = require('./resolve-url');
|
25 |
|
26 | var _resolveUrl2 = _interopRequireDefault(_resolveUrl);
|
27 |
|
28 | var _videoJs = require('video.js');
|
29 |
|
30 | var _playlistJs = require('./playlist.js');
|
31 |
|
32 | var _m3u8Parser = require('m3u8-parser');
|
33 |
|
34 | var _m3u8Parser2 = _interopRequireDefault(_m3u8Parser);
|
35 |
|
36 | var _globalWindow = require('global/window');
|
37 |
|
38 | var _globalWindow2 = _interopRequireDefault(_globalWindow);
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var updateSegments = function updateSegments(original, update, offset) {
|
56 | var result = update.slice();
|
57 |
|
58 | offset = offset || 0;
|
59 | var length = Math.min(original.length, update.length + offset);
|
60 |
|
61 | for (var i = offset; i < length; i++) {
|
62 | result[i - offset] = (0, _videoJs.mergeOptions)(original[i], result[i - offset]);
|
63 | }
|
64 | return result;
|
65 | };
|
66 |
|
67 | exports.updateSegments = updateSegments;
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | var updateMaster = function updateMaster(master, media) {
|
81 | var result = (0, _videoJs.mergeOptions)(master, {});
|
82 | var playlist = result.playlists.filter(function (p) {
|
83 | return p.uri === media.uri;
|
84 | })[0];
|
85 |
|
86 | if (!playlist) {
|
87 | return null;
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 | if (playlist.segments && media.segments && playlist.segments.length === media.segments.length && playlist.mediaSequence === media.mediaSequence) {
|
93 | return null;
|
94 | }
|
95 |
|
96 | var mergedPlaylist = (0, _videoJs.mergeOptions)(playlist, media);
|
97 |
|
98 |
|
99 | if (playlist.segments) {
|
100 | mergedPlaylist.segments = updateSegments(playlist.segments, media.segments, media.mediaSequence - playlist.mediaSequence);
|
101 | }
|
102 |
|
103 |
|
104 | mergedPlaylist.segments.forEach(function (segment) {
|
105 | if (!segment.resolvedUri) {
|
106 | segment.resolvedUri = (0, _resolveUrl2['default'])(mergedPlaylist.resolvedUri, segment.uri);
|
107 | }
|
108 | if (segment.key && !segment.key.resolvedUri) {
|
109 | segment.key.resolvedUri = (0, _resolveUrl2['default'])(mergedPlaylist.resolvedUri, segment.key.uri);
|
110 | }
|
111 | if (segment.map && !segment.map.resolvedUri) {
|
112 | segment.map.resolvedUri = (0, _resolveUrl2['default'])(mergedPlaylist.resolvedUri, segment.map.uri);
|
113 | }
|
114 | });
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | for (var i = 0; i < result.playlists.length; i++) {
|
120 | if (result.playlists[i].uri === media.uri) {
|
121 | result.playlists[i] = mergedPlaylist;
|
122 | }
|
123 | }
|
124 | result.playlists[media.uri] = mergedPlaylist;
|
125 |
|
126 | return result;
|
127 | };
|
128 |
|
129 | exports.updateMaster = updateMaster;
|
130 | var setupMediaPlaylists = function setupMediaPlaylists(master) {
|
131 |
|
132 | var i = master.playlists.length;
|
133 |
|
134 | while (i--) {
|
135 | var playlist = master.playlists[i];
|
136 |
|
137 | master.playlists[playlist.uri] = playlist;
|
138 | playlist.resolvedUri = (0, _resolveUrl2['default'])(master.uri, playlist.uri);
|
139 |
|
140 | if (!playlist.attributes) {
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | playlist.attributes = {};
|
147 |
|
148 | _videoJs.log.warn('Invalid playlist STREAM-INF detected. Missing BANDWIDTH attribute.');
|
149 | }
|
150 | }
|
151 | };
|
152 |
|
153 | exports.setupMediaPlaylists = setupMediaPlaylists;
|
154 | var resolveMediaGroupUris = function resolveMediaGroupUris(master) {
|
155 | ['AUDIO', 'SUBTITLES'].forEach(function (mediaType) {
|
156 | for (var groupKey in master.mediaGroups[mediaType]) {
|
157 | for (var labelKey in master.mediaGroups[mediaType][groupKey]) {
|
158 | var mediaProperties = master.mediaGroups[mediaType][groupKey][labelKey];
|
159 |
|
160 | if (mediaProperties.uri) {
|
161 | mediaProperties.resolvedUri = (0, _resolveUrl2['default'])(master.uri, mediaProperties.uri);
|
162 | }
|
163 | }
|
164 | }
|
165 | });
|
166 | };
|
167 |
|
168 | exports.resolveMediaGroupUris = resolveMediaGroupUris;
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | var refreshDelay = function refreshDelay(media, update) {
|
180 | var lastSegment = media.segments[media.segments.length - 1];
|
181 | var delay = undefined;
|
182 |
|
183 | if (update && lastSegment && lastSegment.duration) {
|
184 | delay = lastSegment.duration * 1000;
|
185 | } else {
|
186 |
|
187 |
|
188 | delay = (media.targetDuration || 10) * 500;
|
189 | }
|
190 | return delay;
|
191 | };
|
192 |
|
193 | exports.refreshDelay = refreshDelay;
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | var PlaylistLoader = (function (_EventTarget) {
|
205 | _inherits(PlaylistLoader, _EventTarget);
|
206 |
|
207 | function PlaylistLoader(srcUrl, hls, withCredentials) {
|
208 | var _this = this;
|
209 |
|
210 | _classCallCheck(this, PlaylistLoader);
|
211 |
|
212 | _get(Object.getPrototypeOf(PlaylistLoader.prototype), 'constructor', this).call(this);
|
213 |
|
214 | this.srcUrl = srcUrl;
|
215 | this.hls_ = hls;
|
216 | this.withCredentials = withCredentials;
|
217 |
|
218 | if (!this.srcUrl) {
|
219 | throw new Error('A non-empty playlist URL is required');
|
220 | }
|
221 |
|
222 |
|
223 | this.state = 'HAVE_NOTHING';
|
224 |
|
225 |
|
226 | this.on('mediaupdatetimeout', function () {
|
227 | if (_this.state !== 'HAVE_METADATA') {
|
228 |
|
229 | return;
|
230 | }
|
231 |
|
232 | _this.state = 'HAVE_CURRENT_METADATA';
|
233 |
|
234 | _this.request = _this.hls_.xhr({
|
235 | uri: (0, _resolveUrl2['default'])(_this.master.uri, _this.media().uri),
|
236 | withCredentials: _this.withCredentials
|
237 | }, function (error, req) {
|
238 |
|
239 | if (!_this.request) {
|
240 | return;
|
241 | }
|
242 |
|
243 | if (error) {
|
244 | return _this.playlistRequestError(_this.request, _this.media().uri, 'HAVE_METADATA');
|
245 | }
|
246 |
|
247 | _this.haveMetadata(_this.request, _this.media().uri);
|
248 | });
|
249 | });
|
250 | }
|
251 |
|
252 | _createClass(PlaylistLoader, [{
|
253 | key: 'playlistRequestError',
|
254 | value: function playlistRequestError(xhr, url, startingState) {
|
255 |
|
256 | this.request = null;
|
257 |
|
258 | if (startingState) {
|
259 | this.state = startingState;
|
260 | }
|
261 |
|
262 | this.error = {
|
263 | playlist: this.master.playlists[url],
|
264 | status: xhr.status,
|
265 | message: 'HLS playlist request error at URL: ' + url,
|
266 | responseText: xhr.responseText,
|
267 | code: xhr.status >= 500 ? 4 : 2
|
268 | };
|
269 |
|
270 | this.trigger('error');
|
271 | }
|
272 |
|
273 |
|
274 |
|
275 | }, {
|
276 | key: 'haveMetadata',
|
277 | value: function haveMetadata(xhr, url) {
|
278 | var _this2 = this;
|
279 |
|
280 |
|
281 | this.request = null;
|
282 | this.state = 'HAVE_METADATA';
|
283 |
|
284 | var parser = new _m3u8Parser2['default'].Parser();
|
285 |
|
286 | parser.push(xhr.responseText);
|
287 | parser.end();
|
288 | parser.manifest.uri = url;
|
289 |
|
290 |
|
291 | parser.manifest.attributes = parser.manifest.attributes || {};
|
292 |
|
293 |
|
294 | var update = updateMaster(this.master, parser.manifest);
|
295 |
|
296 | this.targetDuration = parser.manifest.targetDuration;
|
297 |
|
298 | if (update) {
|
299 | this.master = update;
|
300 | this.media_ = this.master.playlists[parser.manifest.uri];
|
301 | } else {
|
302 | this.trigger('playlistunchanged');
|
303 | }
|
304 |
|
305 |
|
306 | if (!this.media().endList) {
|
307 | _globalWindow2['default'].clearTimeout(this.mediaUpdateTimeout);
|
308 | this.mediaUpdateTimeout = _globalWindow2['default'].setTimeout(function () {
|
309 | _this2.trigger('mediaupdatetimeout');
|
310 | }, refreshDelay(this.media(), !!update));
|
311 | }
|
312 |
|
313 | this.trigger('loadedplaylist');
|
314 | }
|
315 |
|
316 | |
317 |
|
318 |
|
319 | }, {
|
320 | key: 'dispose',
|
321 | value: function dispose() {
|
322 | this.stopRequest();
|
323 | _globalWindow2['default'].clearTimeout(this.mediaUpdateTimeout);
|
324 | }
|
325 | }, {
|
326 | key: 'stopRequest',
|
327 | value: function stopRequest() {
|
328 | if (this.request) {
|
329 | var oldRequest = this.request;
|
330 |
|
331 | this.request = null;
|
332 | oldRequest.onreadystatechange = null;
|
333 | oldRequest.abort();
|
334 | }
|
335 | }
|
336 |
|
337 | |
338 |
|
339 |
|
340 |
|
341 |
|
342 | }, {
|
343 | key: 'enabledPlaylists_',
|
344 | value: function enabledPlaylists_() {
|
345 | return this.master.playlists.filter(_playlistJs.isEnabled).length;
|
346 | }
|
347 |
|
348 | |
349 |
|
350 |
|
351 |
|
352 |
|
353 | }, {
|
354 | key: 'isLowestEnabledRendition_',
|
355 | value: function isLowestEnabledRendition_() {
|
356 | if (this.master.playlists.length === 1) {
|
357 | return true;
|
358 | }
|
359 |
|
360 | var currentBandwidth = this.media().attributes.BANDWIDTH || Number.MAX_VALUE;
|
361 |
|
362 | return this.master.playlists.filter(function (playlist) {
|
363 | if (!(0, _playlistJs.isEnabled)(playlist)) {
|
364 | return false;
|
365 | }
|
366 |
|
367 | return (playlist.attributes.BANDWIDTH || 0) < currentBandwidth;
|
368 | }).length === 0;
|
369 | }
|
370 |
|
371 | |
372 |
|
373 |
|
374 |
|
375 |
|
376 | }, {
|
377 | key: 'isFinalRendition_',
|
378 | value: function isFinalRendition_() {
|
379 | return this.master.playlists.filter(_playlistJs.isEnabled).length === 1;
|
380 | }
|
381 |
|
382 | |
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | }, {
|
395 | key: 'media',
|
396 | value: function media(playlist) {
|
397 | var _this3 = this;
|
398 |
|
399 |
|
400 | if (!playlist) {
|
401 | return this.media_;
|
402 | }
|
403 |
|
404 |
|
405 | if (this.state === 'HAVE_NOTHING') {
|
406 | throw new Error('Cannot switch media playlist from ' + this.state);
|
407 | }
|
408 |
|
409 | var startingState = this.state;
|
410 |
|
411 |
|
412 |
|
413 | if (typeof playlist === 'string') {
|
414 | if (!this.master.playlists[playlist]) {
|
415 | throw new Error('Unknown playlist URI: ' + playlist);
|
416 | }
|
417 | playlist = this.master.playlists[playlist];
|
418 | }
|
419 |
|
420 | var mediaChange = !this.media_ || playlist.uri !== this.media_.uri;
|
421 |
|
422 |
|
423 | if (this.master.playlists[playlist.uri].endList) {
|
424 |
|
425 | if (this.request) {
|
426 | this.request.onreadystatechange = null;
|
427 | this.request.abort();
|
428 | this.request = null;
|
429 | }
|
430 | this.state = 'HAVE_METADATA';
|
431 | this.media_ = playlist;
|
432 |
|
433 |
|
434 | if (mediaChange) {
|
435 | this.trigger('mediachanging');
|
436 | this.trigger('mediachange');
|
437 | }
|
438 | return;
|
439 | }
|
440 |
|
441 |
|
442 | if (!mediaChange) {
|
443 | return;
|
444 | }
|
445 |
|
446 | this.state = 'SWITCHING_MEDIA';
|
447 |
|
448 |
|
449 | if (this.request) {
|
450 | if ((0, _resolveUrl2['default'])(this.master.uri, playlist.uri) === this.request.url) {
|
451 |
|
452 |
|
453 | return;
|
454 | }
|
455 | this.request.onreadystatechange = null;
|
456 | this.request.abort();
|
457 | this.request = null;
|
458 | }
|
459 |
|
460 |
|
461 | if (this.media_) {
|
462 | this.trigger('mediachanging');
|
463 | }
|
464 |
|
465 | this.request = this.hls_.xhr({
|
466 | uri: (0, _resolveUrl2['default'])(this.master.uri, playlist.uri),
|
467 | withCredentials: this.withCredentials
|
468 | }, function (error, req) {
|
469 |
|
470 | if (!_this3.request) {
|
471 | return;
|
472 | }
|
473 |
|
474 | if (error) {
|
475 | return _this3.playlistRequestError(_this3.request, playlist.uri, startingState);
|
476 | }
|
477 |
|
478 | _this3.haveMetadata(req, playlist.uri);
|
479 |
|
480 |
|
481 | if (startingState === 'HAVE_MASTER') {
|
482 | _this3.trigger('loadedmetadata');
|
483 | } else {
|
484 | _this3.trigger('mediachange');
|
485 | }
|
486 | });
|
487 | }
|
488 |
|
489 | |
490 |
|
491 |
|
492 | }, {
|
493 | key: 'pause',
|
494 | value: function pause() {
|
495 | this.stopRequest();
|
496 | _globalWindow2['default'].clearTimeout(this.mediaUpdateTimeout);
|
497 | if (this.state === 'HAVE_NOTHING') {
|
498 |
|
499 |
|
500 | this.started = false;
|
501 | }
|
502 |
|
503 | if (this.state === 'SWITCHING_MEDIA') {
|
504 |
|
505 |
|
506 |
|
507 | if (this.media_) {
|
508 | this.state = 'HAVE_METADATA';
|
509 | } else {
|
510 | this.state = 'HAVE_MASTER';
|
511 | }
|
512 | } else if (this.state === 'HAVE_CURRENT_METADATA') {
|
513 | this.state = 'HAVE_METADATA';
|
514 | }
|
515 | }
|
516 |
|
517 | |
518 |
|
519 |
|
520 | }, {
|
521 | key: 'load',
|
522 | value: function load(isFinalRendition) {
|
523 | var _this4 = this;
|
524 |
|
525 | _globalWindow2['default'].clearTimeout(this.mediaUpdateTimeout);
|
526 |
|
527 | var media = this.media();
|
528 |
|
529 | if (isFinalRendition) {
|
530 | var delay = media ? media.targetDuration / 2 * 1000 : 5 * 1000;
|
531 |
|
532 | this.mediaUpdateTimeout = _globalWindow2['default'].setTimeout(function () {
|
533 | return _this4.load();
|
534 | }, delay);
|
535 | return;
|
536 | }
|
537 |
|
538 | if (!this.started) {
|
539 | this.start();
|
540 | return;
|
541 | }
|
542 |
|
543 | if (media && !media.endList) {
|
544 | this.trigger('mediaupdatetimeout');
|
545 | } else {
|
546 | this.trigger('loadedplaylist');
|
547 | }
|
548 | }
|
549 |
|
550 | |
551 |
|
552 |
|
553 | }, {
|
554 | key: 'start',
|
555 | value: function start() {
|
556 | var _this5 = this;
|
557 |
|
558 | this.started = true;
|
559 |
|
560 |
|
561 | this.request = this.hls_.xhr({
|
562 | uri: this.srcUrl,
|
563 | withCredentials: this.withCredentials
|
564 | }, function (error, req) {
|
565 |
|
566 | if (!_this5.request) {
|
567 | return;
|
568 | }
|
569 |
|
570 |
|
571 | _this5.request = null;
|
572 |
|
573 | if (error) {
|
574 | _this5.error = {
|
575 | status: req.status,
|
576 | message: 'HLS playlist request error at URL: ' + _this5.srcUrl,
|
577 | responseText: req.responseText,
|
578 |
|
579 | code: 2
|
580 | };
|
581 | if (_this5.state === 'HAVE_NOTHING') {
|
582 | _this5.started = false;
|
583 | }
|
584 | return _this5.trigger('error');
|
585 | }
|
586 |
|
587 | var parser = new _m3u8Parser2['default'].Parser();
|
588 |
|
589 | parser.push(req.responseText);
|
590 | parser.end();
|
591 |
|
592 | _this5.state = 'HAVE_MASTER';
|
593 |
|
594 | parser.manifest.uri = _this5.srcUrl;
|
595 |
|
596 |
|
597 | if (parser.manifest.playlists) {
|
598 | _this5.master = parser.manifest;
|
599 |
|
600 | setupMediaPlaylists(_this5.master);
|
601 | resolveMediaGroupUris(_this5.master);
|
602 |
|
603 | _this5.trigger('loadedplaylist');
|
604 | if (!_this5.request) {
|
605 |
|
606 |
|
607 | _this5.media(parser.manifest.playlists[0]);
|
608 | }
|
609 | return;
|
610 | }
|
611 |
|
612 |
|
613 |
|
614 | _this5.master = {
|
615 | mediaGroups: {
|
616 | 'AUDIO': {},
|
617 | 'VIDEO': {},
|
618 | 'CLOSED-CAPTIONS': {},
|
619 | 'SUBTITLES': {}
|
620 | },
|
621 | uri: _globalWindow2['default'].location.href,
|
622 | playlists: [{
|
623 | uri: _this5.srcUrl
|
624 | }]
|
625 | };
|
626 | _this5.master.playlists[_this5.srcUrl] = _this5.master.playlists[0];
|
627 | _this5.master.playlists[0].resolvedUri = _this5.srcUrl;
|
628 |
|
629 |
|
630 | _this5.master.playlists[0].attributes = _this5.master.playlists[0].attributes || {};
|
631 | _this5.haveMetadata(req, _this5.srcUrl);
|
632 | return _this5.trigger('loadedmetadata');
|
633 | });
|
634 | }
|
635 | }]);
|
636 |
|
637 | return PlaylistLoader;
|
638 | })(_videoJs.EventTarget);
|
639 |
|
640 | exports['default'] = PlaylistLoader; |
\ | No newline at end of file |