Pull out XHR helper
Create a helper method for creating XMLHttpRequests and migrate downloadPlaylist to using it.
Showing
1 changed file
with
47 additions
and
26 deletions
... | @@ -94,6 +94,35 @@ var | ... | @@ -94,6 +94,35 @@ var |
94 | } | 94 | } |
95 | }, | 95 | }, |
96 | 96 | ||
97 | xhr = function(url, callback) { | ||
98 | var | ||
99 | options = { | ||
100 | method: 'GET' | ||
101 | }, | ||
102 | request; | ||
103 | if (typeof url === 'object') { | ||
104 | options = videojs.util.mergeOptions(options, url); | ||
105 | url = options.url; | ||
106 | } | ||
107 | request = new window.XMLHttpRequest(); | ||
108 | request.open(options.method, url); | ||
109 | request.onreadystatechange = function() { | ||
110 | // wait until the request completes | ||
111 | if (this.readyState !== 4) { | ||
112 | return; | ||
113 | } | ||
114 | |||
115 | // request error | ||
116 | if (this.status >= 400 || this.status === 0) { | ||
117 | return callback.call(this, true, url); | ||
118 | } | ||
119 | |||
120 | return callback.call(this, false, url); | ||
121 | }; | ||
122 | request.send(null); | ||
123 | return request; | ||
124 | }, | ||
125 | |||
97 | /** | 126 | /** |
98 | * TODO - Document this great feature. | 127 | * TODO - Document this great feature. |
99 | * | 128 | * |
... | @@ -350,7 +379,7 @@ var | ... | @@ -350,7 +379,7 @@ var |
350 | if (!playlist.segments || | 379 | if (!playlist.segments || |
351 | mediaSequence < (playlist.mediaSequence || 0) || | 380 | mediaSequence < (playlist.mediaSequence || 0) || |
352 | mediaSequence > (playlist.mediaSequence || 0) + playlist.segments.length) { | 381 | mediaSequence > (playlist.mediaSequence || 0) + playlist.segments.length) { |
353 | downloadPlaylist(resolveUrl(srcUrl, playlist.uri)); | 382 | xhr(resolveUrl(srcUrl, playlist.uri), downloadPlaylist); |
354 | } else { | 383 | } else { |
355 | player.hls.mediaIndex = | 384 | player.hls.mediaIndex = |
356 | findCorrespondingMediaIndex(player.hls.mediaIndex, | 385 | findCorrespondingMediaIndex(player.hls.mediaIndex, |
... | @@ -437,32 +466,26 @@ var | ... | @@ -437,32 +466,26 @@ var |
437 | }; | 466 | }; |
438 | 467 | ||
439 | /** | 468 | /** |
440 | * Download an M3U8 and update the current manifest object. If the provided | 469 | * Callback that is invoked when a playlist finishes |
441 | * URL is a master playlist, the default variant will be downloaded and | 470 | * downloading. If the response is a master playlist, the default |
442 | * parsed as well. Triggers `loadedmanifest` once for each playlist that is | 471 | * variant will be downloaded and parsed as well. Triggers |
443 | * downloaded and `loadedmetadata` after at least one media playlist has | 472 | * `loadedmanifest` once for each playlist that is downloaded and |
444 | * been parsed. Whether multiple playlists were downloaded or not, when | 473 | * `loadedmetadata` after at least one media playlist has been |
445 | * `loadedmetadata` fires a parsed or inferred master playlist object will | 474 | * parsed. Whether multiple playlists were downloaded or not, when |
446 | * be available as `player.hls.master`. | 475 | * `loadedmetadata` fires a parsed or inferred master playlist |
476 | * object will be available as `player.hls.master`. | ||
447 | * | 477 | * |
478 | * @param error {*} truthy if the request was not successful | ||
448 | * @param url {string} a URL to the M3U8 file to process | 479 | * @param url {string} a URL to the M3U8 file to process |
449 | */ | 480 | */ |
450 | downloadPlaylist = function(url) { | 481 | downloadPlaylist = function(error, url) { |
451 | var xhr = new window.XMLHttpRequest(); | ||
452 | xhr.open('GET', url); | ||
453 | xhr.onreadystatechange = function() { | ||
454 | var i, parser, playlist, playlistUri, refreshDelay; | 482 | var i, parser, playlist, playlistUri, refreshDelay; |
455 | 483 | ||
456 | // wait until the request completes | 484 | if (error) { |
457 | if (xhr.readyState !== 4) { | ||
458 | return; | ||
459 | } | ||
460 | |||
461 | if (xhr.status >= 400 || this.status === 0) { | ||
462 | player.hls.error = { | 485 | player.hls.error = { |
463 | status: xhr.status, | 486 | status: this.status, |
464 | message: 'HLS playlist request error at URL: ' + url, | 487 | message: 'HLS playlist request error at URL: ' + url, |
465 | code: (xhr.status >= 500) ? 4 : 2 | 488 | code: (this.status >= 500) ? 4 : 2 |
466 | }; | 489 | }; |
467 | player.trigger('error'); | 490 | player.trigger('error'); |
468 | return; | 491 | return; |
... | @@ -470,12 +493,12 @@ var | ... | @@ -470,12 +493,12 @@ var |
470 | 493 | ||
471 | // readystate DONE | 494 | // readystate DONE |
472 | parser = new videojs.m3u8.Parser(); | 495 | parser = new videojs.m3u8.Parser(); |
473 | parser.push(xhr.responseText); | 496 | parser.push(this.responseText); |
474 | 497 | ||
475 | // master playlists | 498 | // master playlists |
476 | if (parser.manifest.playlists) { | 499 | if (parser.manifest.playlists) { |
477 | player.hls.master = parser.manifest; | 500 | player.hls.master = parser.manifest; |
478 | downloadPlaylist(resolveUrl(url, parser.manifest.playlists[0].uri)); | 501 | xhr(resolveUrl(url, parser.manifest.playlists[0].uri), downloadPlaylist); |
479 | player.trigger('loadedmanifest'); | 502 | player.trigger('loadedmanifest'); |
480 | return; | 503 | return; |
481 | } | 504 | } |
... | @@ -525,7 +548,7 @@ var | ... | @@ -525,7 +548,7 @@ var |
525 | // check the playlist for updates if EXT-X-ENDLIST isn't present | 548 | // check the playlist for updates if EXT-X-ENDLIST isn't present |
526 | if (!parser.manifest.endList) { | 549 | if (!parser.manifest.endList) { |
527 | window.setTimeout(function() { | 550 | window.setTimeout(function() { |
528 | downloadPlaylist(url); | 551 | xhr(url, downloadPlaylist); |
529 | }, refreshDelay); | 552 | }, refreshDelay); |
530 | } | 553 | } |
531 | 554 | ||
... | @@ -550,8 +573,6 @@ var | ... | @@ -550,8 +573,6 @@ var |
550 | 573 | ||
551 | player.trigger('loadedmanifest'); | 574 | player.trigger('loadedmanifest'); |
552 | }; | 575 | }; |
553 | xhr.send(null); | ||
554 | }; | ||
555 | 576 | ||
556 | /** | 577 | /** |
557 | * Determines whether there is enough video data currently in the buffer | 578 | * Determines whether there is enough video data currently in the buffer |
... | @@ -668,7 +689,7 @@ var | ... | @@ -668,7 +689,7 @@ var |
668 | sourceBuffer.appendBuffer(segmentParser.getFlvHeader()); | 689 | sourceBuffer.appendBuffer(segmentParser.getFlvHeader()); |
669 | 690 | ||
670 | player.hls.mediaIndex = 0; | 691 | player.hls.mediaIndex = 0; |
671 | downloadPlaylist(srcUrl); | 692 | xhr(srcUrl, downloadPlaylist); |
672 | }); | 693 | }); |
673 | player.src([{ | 694 | player.src([{ |
674 | src: videojs.URL.createObjectURL(mediaSource), | 695 | src: videojs.URL.createObjectURL(mediaSource), | ... | ... |
-
Please register or sign in to post a comment