1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { resolveUrl, resolveManifestRedirect } from './resolve-url';
|
9 | import videojs from 'video.js';
|
10 | import window from 'global/window';
|
11 | import logger from './util/logger';
|
12 | import {
|
13 | parseManifest,
|
14 | addPropertiesToMaster,
|
15 | masterForMedia,
|
16 | setupMediaPlaylist,
|
17 | forEachMediaGroup
|
18 | } from './manifest';
|
19 | import {getKnownPartCount} from './playlist.js';
|
20 |
|
21 | const { mergeOptions, EventTarget } = videojs;
|
22 |
|
23 | const addLLHLSQueryDirectives = (uri, media) => {
|
24 | if (media.endList || !media.serverControl) {
|
25 | return uri;
|
26 | }
|
27 |
|
28 | const parameters = {};
|
29 |
|
30 | if (media.serverControl.canBlockReload) {
|
31 | const {preloadSegment} = media;
|
32 |
|
33 | let nextMSN = media.mediaSequence + media.segments.length;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | if (preloadSegment) {
|
39 | const parts = preloadSegment.parts || [];
|
40 |
|
41 | const nextPart = getKnownPartCount(media) - 1;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | if (nextPart > -1 && nextPart !== (parts.length - 1)) {
|
47 |
|
48 |
|
49 | parameters._HLS_part = nextPart;
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | if (nextPart > -1 || parts.length) {
|
62 | nextMSN--;
|
63 | }
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 | parameters._HLS_msn = nextMSN;
|
69 | }
|
70 |
|
71 | if (media.serverControl && media.serverControl.canSkipUntil) {
|
72 |
|
73 |
|
74 | parameters._HLS_skip = (media.serverControl.canSkipDateranges ? 'v2' : 'YES');
|
75 | }
|
76 |
|
77 | if (Object.keys(parameters).length) {
|
78 | const parsedUri = new window.URL(uri);
|
79 |
|
80 | ['_HLS_skip', '_HLS_msn', '_HLS_part'].forEach(function(name) {
|
81 | if (!parameters.hasOwnProperty(name)) {
|
82 | return;
|
83 | }
|
84 |
|
85 | parsedUri.searchParams.set(name, parameters[name]);
|
86 | });
|
87 |
|
88 | uri = parsedUri.toString();
|
89 | }
|
90 |
|
91 | return uri;
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | export const updateSegment = (a, b) => {
|
104 | if (!a) {
|
105 | return b;
|
106 | }
|
107 |
|
108 | const result = mergeOptions(a, b);
|
109 |
|
110 |
|
111 |
|
112 | if (a.preloadHints && !b.preloadHints) {
|
113 | delete result.preloadHints;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 | if (a.parts && !b.parts) {
|
119 | delete result.parts;
|
120 |
|
121 |
|
122 |
|
123 | } else if (a.parts && b.parts) {
|
124 | for (let i = 0; i < b.parts.length; i++) {
|
125 | if (a.parts && a.parts[i]) {
|
126 | result.parts[i] = mergeOptions(a.parts[i], b.parts[i]);
|
127 | }
|
128 | }
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 | if (!a.skipped && b.skipped) {
|
134 | result.skipped = false;
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 | if (a.preload && !b.preload) {
|
140 | result.preload = false;
|
141 | }
|
142 |
|
143 | return result;
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | export const updateSegments = (original, update, offset) => {
|
162 | const oldSegments = original.slice();
|
163 | const newSegments = update.slice();
|
164 |
|
165 | offset = offset || 0;
|
166 | const result = [];
|
167 |
|
168 | let currentMap;
|
169 |
|
170 | for (let newIndex = 0; newIndex < newSegments.length; newIndex++) {
|
171 | const oldSegment = oldSegments[newIndex + offset];
|
172 | const newSegment = newSegments[newIndex];
|
173 |
|
174 | if (oldSegment) {
|
175 | currentMap = oldSegment.map || currentMap;
|
176 |
|
177 | result.push(updateSegment(oldSegment, newSegment));
|
178 | } else {
|
179 |
|
180 | if (currentMap && !newSegment.map) {
|
181 | newSegment.map = currentMap;
|
182 | }
|
183 |
|
184 | result.push(newSegment);
|
185 |
|
186 | }
|
187 | }
|
188 | return result;
|
189 | };
|
190 |
|
191 | export const resolveSegmentUris = (segment, baseUri) => {
|
192 |
|
193 |
|
194 | if (!segment.resolvedUri && segment.uri) {
|
195 | segment.resolvedUri = resolveUrl(baseUri, segment.uri);
|
196 | }
|
197 | if (segment.key && !segment.key.resolvedUri) {
|
198 | segment.key.resolvedUri = resolveUrl(baseUri, segment.key.uri);
|
199 | }
|
200 | if (segment.map && !segment.map.resolvedUri) {
|
201 | segment.map.resolvedUri = resolveUrl(baseUri, segment.map.uri);
|
202 | }
|
203 |
|
204 | if (segment.map && segment.map.key && !segment.map.key.resolvedUri) {
|
205 | segment.map.key.resolvedUri = resolveUrl(baseUri, segment.map.key.uri);
|
206 | }
|
207 | if (segment.parts && segment.parts.length) {
|
208 | segment.parts.forEach((p) => {
|
209 | if (p.resolvedUri) {
|
210 | return;
|
211 | }
|
212 | p.resolvedUri = resolveUrl(baseUri, p.uri);
|
213 | });
|
214 | }
|
215 |
|
216 | if (segment.preloadHints && segment.preloadHints.length) {
|
217 | segment.preloadHints.forEach((p) => {
|
218 | if (p.resolvedUri) {
|
219 | return;
|
220 | }
|
221 | p.resolvedUri = resolveUrl(baseUri, p.uri);
|
222 | });
|
223 | }
|
224 | };
|
225 |
|
226 | const getAllSegments = function(media) {
|
227 | const segments = media.segments || [];
|
228 | const preloadSegment = media.preloadSegment;
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | if (preloadSegment && preloadSegment.parts && preloadSegment.parts.length) {
|
234 |
|
235 |
|
236 |
|
237 | if (preloadSegment.preloadHints) {
|
238 | for (let i = 0; i < preloadSegment.preloadHints.length; i++) {
|
239 | if (preloadSegment.preloadHints[i].type === 'MAP') {
|
240 | return segments;
|
241 | }
|
242 | }
|
243 | }
|
244 |
|
245 | preloadSegment.duration = media.targetDuration;
|
246 | preloadSegment.preload = true;
|
247 |
|
248 | segments.push(preloadSegment);
|
249 | }
|
250 |
|
251 | return segments;
|
252 | };
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | export const isPlaylistUnchanged = (a, b) => a === b ||
|
258 | (a.segments && b.segments && a.segments.length === b.segments.length &&
|
259 | a.endList === b.endList &&
|
260 | a.mediaSequence === b.mediaSequence &&
|
261 | a.preloadSegment === b.preloadSegment);
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | export const updateMaster = (master, newMedia, unchangedCheck = isPlaylistUnchanged) => {
|
276 | const result = mergeOptions(master, {});
|
277 | const oldMedia = result.playlists[newMedia.id];
|
278 |
|
279 | if (!oldMedia) {
|
280 | return null;
|
281 | }
|
282 |
|
283 | if (unchangedCheck(oldMedia, newMedia)) {
|
284 | return null;
|
285 | }
|
286 |
|
287 | newMedia.segments = getAllSegments(newMedia);
|
288 |
|
289 | const mergedPlaylist = mergeOptions(oldMedia, newMedia);
|
290 |
|
291 |
|
292 | if (mergedPlaylist.preloadSegment && !newMedia.preloadSegment) {
|
293 | delete mergedPlaylist.preloadSegment;
|
294 | }
|
295 |
|
296 |
|
297 | if (oldMedia.segments) {
|
298 | if (newMedia.skip) {
|
299 | newMedia.segments = newMedia.segments || [];
|
300 |
|
301 |
|
302 | for (let i = 0; i < newMedia.skip.skippedSegments; i++) {
|
303 | newMedia.segments.unshift({skipped: true});
|
304 | }
|
305 | }
|
306 | mergedPlaylist.segments = updateSegments(
|
307 | oldMedia.segments,
|
308 | newMedia.segments,
|
309 | newMedia.mediaSequence - oldMedia.mediaSequence
|
310 | );
|
311 | }
|
312 |
|
313 |
|
314 | mergedPlaylist.segments.forEach((segment) => {
|
315 | resolveSegmentUris(segment, mergedPlaylist.resolvedUri);
|
316 | });
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | for (let i = 0; i < result.playlists.length; i++) {
|
322 | if (result.playlists[i].id === newMedia.id) {
|
323 | result.playlists[i] = mergedPlaylist;
|
324 | }
|
325 | }
|
326 | result.playlists[newMedia.id] = mergedPlaylist;
|
327 |
|
328 | result.playlists[newMedia.uri] = mergedPlaylist;
|
329 |
|
330 |
|
331 | forEachMediaGroup(master, (properties, mediaType, groupKey, labelKey) => {
|
332 | if (!properties.playlists) {
|
333 | return;
|
334 | }
|
335 | for (let i = 0; i < properties.playlists.length; i++) {
|
336 | if (newMedia.id === properties.playlists[i].id) {
|
337 | properties.playlists[i] = mergedPlaylist;
|
338 | }
|
339 | }
|
340 | });
|
341 |
|
342 | return result;
|
343 | };
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | export const refreshDelay = (media, update) => {
|
356 | const segments = media.segments || [];
|
357 | const lastSegment = segments[segments.length - 1];
|
358 | const lastPart = lastSegment && lastSegment.parts && lastSegment.parts[lastSegment.parts.length - 1];
|
359 | const lastDuration = lastPart && lastPart.duration || lastSegment && lastSegment.duration;
|
360 |
|
361 | if (update && lastDuration) {
|
362 | return lastDuration * 1000;
|
363 | }
|
364 |
|
365 |
|
366 |
|
367 | return (media.partTargetDuration || media.targetDuration || 10) * 500;
|
368 | };
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 | export default class PlaylistLoader extends EventTarget {
|
380 | constructor(src, vhs, options = { }) {
|
381 | super();
|
382 |
|
383 | if (!src) {
|
384 | throw new Error('A non-empty playlist URL or object is required');
|
385 | }
|
386 | this.logger_ = logger('PlaylistLoader');
|
387 |
|
388 | const { withCredentials = false, handleManifestRedirects = false } = options;
|
389 |
|
390 | this.src = src;
|
391 | this.vhs_ = vhs;
|
392 | this.withCredentials = withCredentials;
|
393 | this.handleManifestRedirects = handleManifestRedirects;
|
394 |
|
395 | const vhsOptions = vhs.options_;
|
396 |
|
397 | this.customTagParsers = (vhsOptions && vhsOptions.customTagParsers) || [];
|
398 | this.customTagMappers = (vhsOptions && vhsOptions.customTagMappers) || [];
|
399 | this.experimentalLLHLS = (vhsOptions && vhsOptions.experimentalLLHLS) || false;
|
400 |
|
401 |
|
402 | if (videojs.browser.IE_VERSION) {
|
403 | this.experimentalLLHLS = false;
|
404 | }
|
405 |
|
406 |
|
407 | this.state = 'HAVE_NOTHING';
|
408 |
|
409 |
|
410 | this.handleMediaupdatetimeout_ = this.handleMediaupdatetimeout_.bind(this);
|
411 | this.on('mediaupdatetimeout', this.handleMediaupdatetimeout_);
|
412 | }
|
413 |
|
414 | handleMediaupdatetimeout_() {
|
415 | if (this.state !== 'HAVE_METADATA') {
|
416 |
|
417 | return;
|
418 | }
|
419 | const media = this.media();
|
420 |
|
421 | let uri = resolveUrl(this.master.uri, media.uri);
|
422 |
|
423 | if (this.experimentalLLHLS) {
|
424 | uri = addLLHLSQueryDirectives(uri, media);
|
425 | }
|
426 | this.state = 'HAVE_CURRENT_METADATA';
|
427 |
|
428 | this.request = this.vhs_.xhr({
|
429 | uri,
|
430 | withCredentials: this.withCredentials
|
431 | }, (error, req) => {
|
432 |
|
433 | if (!this.request) {
|
434 | return;
|
435 | }
|
436 |
|
437 | if (error) {
|
438 | return this.playlistRequestError(this.request, this.media(), 'HAVE_METADATA');
|
439 | }
|
440 |
|
441 | this.haveMetadata({
|
442 | playlistString: this.request.responseText,
|
443 | url: this.media().uri,
|
444 | id: this.media().id
|
445 | });
|
446 | });
|
447 |
|
448 | }
|
449 |
|
450 | playlistRequestError(xhr, playlist, startingState) {
|
451 | const {
|
452 | uri,
|
453 | id
|
454 | } = playlist;
|
455 |
|
456 |
|
457 | this.request = null;
|
458 |
|
459 | if (startingState) {
|
460 | this.state = startingState;
|
461 | }
|
462 |
|
463 | this.error = {
|
464 | playlist: this.master.playlists[id],
|
465 | status: xhr.status,
|
466 | message: `HLS playlist request error at URL: ${uri}.`,
|
467 | responseText: xhr.responseText,
|
468 | code: (xhr.status >= 500) ? 4 : 2
|
469 | };
|
470 |
|
471 | this.trigger('error');
|
472 | }
|
473 |
|
474 | parseManifest_({url, manifestString}) {
|
475 | return parseManifest({
|
476 | onwarn: ({message}) => this.logger_(`m3u8-parser warn for ${url}: ${message}`),
|
477 | oninfo: ({message}) => this.logger_(`m3u8-parser info for ${url}: ${message}`),
|
478 | manifestString,
|
479 | customTagParsers: this.customTagParsers,
|
480 | customTagMappers: this.customTagMappers,
|
481 | experimentalLLHLS: this.experimentalLLHLS
|
482 | });
|
483 | }
|
484 |
|
485 | |
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 | haveMetadata({ playlistString, playlistObject, url, id }) {
|
498 |
|
499 | this.request = null;
|
500 | this.state = 'HAVE_METADATA';
|
501 |
|
502 | const playlist = playlistObject || this.parseManifest_({
|
503 | url,
|
504 | manifestString: playlistString
|
505 | });
|
506 |
|
507 | playlist.lastRequest = Date.now();
|
508 |
|
509 | setupMediaPlaylist({
|
510 | playlist,
|
511 | uri: url,
|
512 | id
|
513 | });
|
514 |
|
515 |
|
516 | const update = updateMaster(this.master, playlist);
|
517 |
|
518 | this.targetDuration = playlist.partTargetDuration || playlist.targetDuration;
|
519 |
|
520 | this.pendingMedia_ = null;
|
521 |
|
522 | if (update) {
|
523 | this.master = update;
|
524 | this.media_ = this.master.playlists[id];
|
525 | } else {
|
526 | this.trigger('playlistunchanged');
|
527 | }
|
528 |
|
529 | this.updateMediaUpdateTimeout_(refreshDelay(this.media(), !!update));
|
530 |
|
531 | this.trigger('loadedplaylist');
|
532 | }
|
533 |
|
534 | |
535 |
|
536 |
|
537 | dispose() {
|
538 | this.trigger('dispose');
|
539 | this.stopRequest();
|
540 | window.clearTimeout(this.mediaUpdateTimeout);
|
541 | window.clearTimeout(this.finalRenditionTimeout);
|
542 |
|
543 | this.off();
|
544 | }
|
545 |
|
546 | stopRequest() {
|
547 | if (this.request) {
|
548 | const oldRequest = this.request;
|
549 |
|
550 | this.request = null;
|
551 | oldRequest.onreadystatechange = null;
|
552 | oldRequest.abort();
|
553 | }
|
554 | }
|
555 |
|
556 | |
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 | media(playlist, shouldDelay) {
|
571 |
|
572 | if (!playlist) {
|
573 | return this.media_;
|
574 | }
|
575 |
|
576 |
|
577 | if (this.state === 'HAVE_NOTHING') {
|
578 | throw new Error('Cannot switch media playlist from ' + this.state);
|
579 | }
|
580 |
|
581 |
|
582 |
|
583 | if (typeof playlist === 'string') {
|
584 | if (!this.master.playlists[playlist]) {
|
585 | throw new Error('Unknown playlist URI: ' + playlist);
|
586 | }
|
587 | playlist = this.master.playlists[playlist];
|
588 | }
|
589 |
|
590 | window.clearTimeout(this.finalRenditionTimeout);
|
591 |
|
592 | if (shouldDelay) {
|
593 | const delay = ((playlist.partTargetDuration || playlist.targetDuration) / 2) * 1000 || 5 * 1000;
|
594 |
|
595 | this.finalRenditionTimeout =
|
596 | window.setTimeout(this.media.bind(this, playlist, false), delay);
|
597 | return;
|
598 | }
|
599 |
|
600 | const startingState = this.state;
|
601 | const mediaChange = !this.media_ || playlist.id !== this.media_.id;
|
602 | const masterPlaylistRef = this.master.playlists[playlist.id];
|
603 |
|
604 |
|
605 | if (masterPlaylistRef && masterPlaylistRef.endList ||
|
606 |
|
607 |
|
608 | (playlist.endList && playlist.segments.length)) {
|
609 |
|
610 |
|
611 | if (this.request) {
|
612 | this.request.onreadystatechange = null;
|
613 | this.request.abort();
|
614 | this.request = null;
|
615 | }
|
616 | this.state = 'HAVE_METADATA';
|
617 | this.media_ = playlist;
|
618 |
|
619 |
|
620 | if (mediaChange) {
|
621 | this.trigger('mediachanging');
|
622 |
|
623 | if (startingState === 'HAVE_MASTER') {
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 | this.trigger('loadedmetadata');
|
630 | } else {
|
631 | this.trigger('mediachange');
|
632 | }
|
633 | }
|
634 | return;
|
635 | }
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | this.updateMediaUpdateTimeout_(refreshDelay(playlist, true));
|
643 |
|
644 |
|
645 | if (!mediaChange) {
|
646 | return;
|
647 | }
|
648 |
|
649 | this.state = 'SWITCHING_MEDIA';
|
650 |
|
651 |
|
652 | if (this.request) {
|
653 | if (playlist.resolvedUri === this.request.url) {
|
654 |
|
655 |
|
656 | return;
|
657 | }
|
658 | this.request.onreadystatechange = null;
|
659 | this.request.abort();
|
660 | this.request = null;
|
661 | }
|
662 |
|
663 |
|
664 | if (this.media_) {
|
665 | this.trigger('mediachanging');
|
666 | }
|
667 |
|
668 | this.pendingMedia_ = playlist;
|
669 |
|
670 | this.request = this.vhs_.xhr({
|
671 | uri: playlist.resolvedUri,
|
672 | withCredentials: this.withCredentials
|
673 | }, (error, req) => {
|
674 |
|
675 | if (!this.request) {
|
676 | return;
|
677 | }
|
678 |
|
679 | playlist.lastRequest = Date.now();
|
680 |
|
681 | playlist.resolvedUri = resolveManifestRedirect(this.handleManifestRedirects, playlist.resolvedUri, req);
|
682 |
|
683 | if (error) {
|
684 | return this.playlistRequestError(this.request, playlist, startingState);
|
685 | }
|
686 |
|
687 | this.haveMetadata({
|
688 | playlistString: req.responseText,
|
689 | url: playlist.uri,
|
690 | id: playlist.id
|
691 | });
|
692 |
|
693 |
|
694 | if (startingState === 'HAVE_MASTER') {
|
695 | this.trigger('loadedmetadata');
|
696 | } else {
|
697 | this.trigger('mediachange');
|
698 | }
|
699 | });
|
700 | }
|
701 |
|
702 | |
703 |
|
704 |
|
705 | pause() {
|
706 | if (this.mediaUpdateTimeout) {
|
707 | window.clearTimeout(this.mediaUpdateTimeout);
|
708 | this.mediaUpdateTimeout = null;
|
709 | }
|
710 |
|
711 | this.stopRequest();
|
712 | if (this.state === 'HAVE_NOTHING') {
|
713 |
|
714 |
|
715 | this.started = false;
|
716 | }
|
717 |
|
718 | if (this.state === 'SWITCHING_MEDIA') {
|
719 |
|
720 |
|
721 |
|
722 | if (this.media_) {
|
723 | this.state = 'HAVE_METADATA';
|
724 | } else {
|
725 | this.state = 'HAVE_MASTER';
|
726 | }
|
727 | } else if (this.state === 'HAVE_CURRENT_METADATA') {
|
728 | this.state = 'HAVE_METADATA';
|
729 | }
|
730 | }
|
731 |
|
732 | |
733 |
|
734 |
|
735 | load(shouldDelay) {
|
736 | if (this.mediaUpdateTimeout) {
|
737 | window.clearTimeout(this.mediaUpdateTimeout);
|
738 | this.mediaUpdateTimeout = null;
|
739 | }
|
740 | const media = this.media();
|
741 |
|
742 | if (shouldDelay) {
|
743 | const delay = media ? ((media.partTargetDuration || media.targetDuration) / 2) * 1000 : 5 * 1000;
|
744 |
|
745 | this.mediaUpdateTimeout = window.setTimeout(() => {
|
746 | this.mediaUpdateTimeout = null;
|
747 | this.load();
|
748 | }, delay);
|
749 |
|
750 | return;
|
751 | }
|
752 |
|
753 | if (!this.started) {
|
754 | this.start();
|
755 | return;
|
756 | }
|
757 |
|
758 | if (media && !media.endList) {
|
759 | this.trigger('mediaupdatetimeout');
|
760 | } else {
|
761 | this.trigger('loadedplaylist');
|
762 | }
|
763 | }
|
764 |
|
765 | updateMediaUpdateTimeout_(delay) {
|
766 | if (this.mediaUpdateTimeout) {
|
767 | window.clearTimeout(this.mediaUpdateTimeout);
|
768 | this.mediaUpdateTimeout = null;
|
769 | }
|
770 |
|
771 |
|
772 | if (!this.media() || this.media().endList) {
|
773 | return;
|
774 | }
|
775 |
|
776 | this.mediaUpdateTimeout = window.setTimeout(() => {
|
777 | this.mediaUpdateTimeout = null;
|
778 | this.trigger('mediaupdatetimeout');
|
779 | this.updateMediaUpdateTimeout_(delay);
|
780 | }, delay);
|
781 | }
|
782 |
|
783 | |
784 |
|
785 |
|
786 | start() {
|
787 | this.started = true;
|
788 |
|
789 | if (typeof this.src === 'object') {
|
790 |
|
791 |
|
792 | if (!this.src.uri) {
|
793 | this.src.uri = window.location.href;
|
794 | }
|
795 |
|
796 |
|
797 |
|
798 | this.src.resolvedUri = this.src.uri;
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 | setTimeout(() => {
|
810 | this.setupInitialPlaylist(this.src);
|
811 | }, 0);
|
812 | return;
|
813 | }
|
814 |
|
815 |
|
816 | this.request = this.vhs_.xhr({
|
817 | uri: this.src,
|
818 | withCredentials: this.withCredentials
|
819 | }, (error, req) => {
|
820 |
|
821 | if (!this.request) {
|
822 | return;
|
823 | }
|
824 |
|
825 |
|
826 | this.request = null;
|
827 |
|
828 | if (error) {
|
829 | this.error = {
|
830 | status: req.status,
|
831 | message: `HLS playlist request error at URL: ${this.src}.`,
|
832 | responseText: req.responseText,
|
833 |
|
834 | code: 2
|
835 | };
|
836 | if (this.state === 'HAVE_NOTHING') {
|
837 | this.started = false;
|
838 | }
|
839 | return this.trigger('error');
|
840 | }
|
841 |
|
842 | this.src = resolveManifestRedirect(this.handleManifestRedirects, this.src, req);
|
843 |
|
844 | const manifest = this.parseManifest_({
|
845 | manifestString: req.responseText,
|
846 | url: this.src
|
847 | });
|
848 |
|
849 | this.setupInitialPlaylist(manifest);
|
850 | });
|
851 | }
|
852 |
|
853 | srcUri() {
|
854 | return typeof this.src === 'string' ? this.src : this.src.uri;
|
855 | }
|
856 |
|
857 | |
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 | setupInitialPlaylist(manifest) {
|
876 | this.state = 'HAVE_MASTER';
|
877 |
|
878 | if (manifest.playlists) {
|
879 | this.master = manifest;
|
880 | addPropertiesToMaster(this.master, this.srcUri());
|
881 |
|
882 |
|
883 |
|
884 | manifest.playlists.forEach((playlist) => {
|
885 | playlist.segments = getAllSegments(playlist);
|
886 |
|
887 | playlist.segments.forEach((segment) => {
|
888 | resolveSegmentUris(segment, playlist.resolvedUri);
|
889 | });
|
890 | });
|
891 | this.trigger('loadedplaylist');
|
892 | if (!this.request) {
|
893 |
|
894 |
|
895 | this.media(this.master.playlists[0]);
|
896 | }
|
897 | return;
|
898 | }
|
899 |
|
900 |
|
901 |
|
902 |
|
903 | const uri = this.srcUri() || window.location.href;
|
904 |
|
905 | this.master = masterForMedia(manifest, uri);
|
906 | this.haveMetadata({
|
907 | playlistObject: manifest,
|
908 | url: uri,
|
909 | id: this.master.playlists[0].id
|
910 | });
|
911 | this.trigger('loadedmetadata');
|
912 | }
|
913 |
|
914 | }
|