convert segmentXHR to use xhr helper
Showing
1 changed file
with
38 additions
and
44 deletions
... | @@ -639,68 +639,62 @@ var | ... | @@ -639,68 +639,62 @@ var |
639 | } | 639 | } |
640 | 640 | ||
641 | // request the next segment | 641 | // request the next segment |
642 | segmentXhr = new window.XMLHttpRequest(); | 642 | segmentXhr = xhr({ |
643 | segmentXhr.open('GET', segmentUri); | 643 | url: segmentUri, |
644 | segmentXhr.responseType = 'arraybuffer'; | 644 | responseType: 'arraybuffer' |
645 | segmentXhr.onreadystatechange = function() { | 645 | }, function(error, url) { |
646 | // wait until the request completes | ||
647 | if (this.readyState !== 4) { | ||
648 | return; | ||
649 | } | ||
650 | |||
651 | // the segment request is no longer outstanding | 646 | // the segment request is no longer outstanding |
652 | segmentXhr = null; | 647 | segmentXhr = null; |
653 | 648 | ||
654 | // trigger an error if the request was not successful | 649 | if (error) { |
655 | if (this.status >= 400) { | ||
656 | player.hls.error = { | 650 | player.hls.error = { |
657 | status: this.status, | 651 | status: this.status, |
658 | message: 'HLS segment request error at URL: ' + segmentUri, | 652 | message: 'HLS segment request error at URL: ' + url, |
659 | code: (this.status >= 500) ? 4 : 2 | 653 | code: (this.status >= 500) ? 4 : 2 |
660 | }; | 654 | }; |
661 | 655 | ||
662 | // try moving on to the next segment | 656 | // try moving on to the next segment |
663 | player.hls.mediaIndex++; | 657 | player.hls.mediaIndex++; |
664 | return; | 658 | return; |
665 | } | 659 | } else { |
666 | 660 | // stop processing if the request was aborted | |
667 | // stop processing if the request was aborted | 661 | if (!this.response) { |
668 | if (!this.response) { | 662 | return; |
669 | return; | 663 | } |
670 | } | ||
671 | |||
672 | // calculate the download bandwidth | ||
673 | player.hls.segmentXhrTime = (+new Date()) - startTime; | ||
674 | player.hls.bandwidth = (this.response.byteLength / player.hls.segmentXhrTime) * 8 * 1000; | ||
675 | 664 | ||
676 | // transmux the segment data from MP2T to FLV | 665 | // calculate the download bandwidth |
677 | segmentParser.parseSegmentBinaryData(new Uint8Array(this.response)); | 666 | player.hls.segmentXhrTime = (+new Date()) - startTime; |
678 | segmentParser.flushTags(); | 667 | player.hls.bandwidth = (this.response.byteLength / player.hls.segmentXhrTime) * 8 * 1000; |
668 | |||
669 | // transmux the segment data from MP2T to FLV | ||
670 | segmentParser.parseSegmentBinaryData(new Uint8Array(this.response)); | ||
671 | segmentParser.flushTags(); | ||
672 | |||
673 | // if we're refilling the buffer after a seek, scan through the muxed | ||
674 | // FLV tags until we find the one that is closest to the desired | ||
675 | // playback time | ||
676 | if (offset !== undefined && typeof offset === "number") { | ||
677 | while (segmentParser.getTags()[0].pts < offset) { | ||
678 | segmentParser.getNextTag(); | ||
679 | } | ||
680 | } | ||
679 | 681 | ||
680 | // if we're refilling the buffer after a seek, scan through the muxed | 682 | while (segmentParser.tagsAvailable()) { |
681 | // FLV tags until we find the one that is closest to the desired | 683 | // queue up the bytes to be appended to the SourceBuffer |
682 | // playback time | 684 | // the queue gives control back to the browser between tags |
683 | if (offset !== undefined && typeof offset === "number") { | 685 | // so that large segments don't cause a "hiccup" in playback |
684 | while (segmentParser.getTags()[0].pts < offset) { | 686 | tags.push(segmentParser.getNextTag().bytes); |
685 | segmentParser.getNextTag(); | ||
686 | } | 687 | } |
687 | } | ||
688 | 688 | ||
689 | while (segmentParser.tagsAvailable()) { | 689 | player.hls.mediaIndex++; |
690 | // queue up the bytes to be appended to the SourceBuffer | ||
691 | // the queue gives control back to the browser between tags | ||
692 | // so that large segments don't cause a "hiccup" in playback | ||
693 | tags.push(segmentParser.getNextTag().bytes); | ||
694 | } | ||
695 | 690 | ||
696 | player.hls.mediaIndex++; | 691 | // figure out what stream the next segment should be downloaded from |
692 | // with the updated bandwidth information | ||
693 | updateCurrentPlaylist(); | ||
694 | } | ||
695 | }); | ||
697 | 696 | ||
698 | // figure out what stream the next segment should be downloaded from | ||
699 | // with the updated bandwidth information | ||
700 | updateCurrentPlaylist(); | ||
701 | }; | ||
702 | startTime = +new Date(); | 697 | startTime = +new Date(); |
703 | segmentXhr.send(null); | ||
704 | }; | 698 | }; |
705 | 699 | ||
706 | // load the MediaSource into the player | 700 | // load the MediaSource into the player | ... | ... |
-
Please register or sign in to post a comment