Fix how we determine whether we need to continue buffering
With the HTML5 tech buffered can return more than one region. We need to find the region that we are currently playing inside of and use the extent of that region's end-point to determine whether or not we need to buffer more segments
Showing
1 changed file
with
34 additions
and
3 deletions
... | @@ -739,6 +739,35 @@ videojs.Hls.prototype.stopCheckingBuffer_ = function() { | ... | @@ -739,6 +739,35 @@ videojs.Hls.prototype.stopCheckingBuffer_ = function() { |
739 | }; | 739 | }; |
740 | 740 | ||
741 | /** | 741 | /** |
742 | * Attempts to find the buffered TimeRange where playback is currently | ||
743 | * happening. Returns a new TimeRange with one or zero ranges. | ||
744 | */ | ||
745 | videojs.Hls.prototype.findCurrentBuffered_ = function() { | ||
746 | var | ||
747 | tech = this.tech_, | ||
748 | currentTime = tech.currentTime(), | ||
749 | buffered = this.tech_.buffered(), | ||
750 | i; | ||
751 | |||
752 | if (buffered && buffered.length) { | ||
753 | // Search for a range containing the play-head | ||
754 | for (i = 0;i < buffered.length; i++) { | ||
755 | if (buffered.start(i) <= currentTime && | ||
756 | buffered.end(i) >= currentTime) { | ||
757 | return videojs.createTimeRange(buffered.start(i), buffered.end(i)); | ||
758 | } | ||
759 | } | ||
760 | |||
761 | // Just return the first range if one was not found that contain | ||
762 | // the play-head | ||
763 | return videojs.createTimeRange(buffered.start(0), buffered.end(0)); | ||
764 | } | ||
765 | |||
766 | // Return an empty range if no ranges exist | ||
767 | return videojs.createTimeRange(); | ||
768 | }; | ||
769 | |||
770 | /** | ||
742 | * Determines whether there is enough video data currently in the buffer | 771 | * Determines whether there is enough video data currently in the buffer |
743 | * and downloads a new segment if the buffered time is less than the goal. | 772 | * and downloads a new segment if the buffered time is less than the goal. |
744 | * @param offset (optional) {number} the offset into the downloaded segment | 773 | * @param offset (optional) {number} the offset into the downloaded segment |
... | @@ -747,7 +776,8 @@ videojs.Hls.prototype.stopCheckingBuffer_ = function() { | ... | @@ -747,7 +776,8 @@ videojs.Hls.prototype.stopCheckingBuffer_ = function() { |
747 | videojs.Hls.prototype.fillBuffer = function(offset) { | 776 | videojs.Hls.prototype.fillBuffer = function(offset) { |
748 | var | 777 | var |
749 | tech = this.tech_, | 778 | tech = this.tech_, |
750 | buffered = this.tech_.buffered(), | 779 | currentTime = tech.currentTime(), |
780 | buffered = this.findCurrentBuffered_(), | ||
751 | bufferedTime = 0, | 781 | bufferedTime = 0, |
752 | segment, | 782 | segment, |
753 | segmentUri; | 783 | segmentUri; |
... | @@ -785,9 +815,10 @@ videojs.Hls.prototype.fillBuffer = function(offset) { | ... | @@ -785,9 +815,10 @@ videojs.Hls.prototype.fillBuffer = function(offset) { |
785 | return; | 815 | return; |
786 | } | 816 | } |
787 | 817 | ||
818 | // To determine how much is buffered, we need to find the buffered region we | ||
819 | // are currently playing in and measure it's length | ||
788 | if (buffered && buffered.length) { | 820 | if (buffered && buffered.length) { |
789 | // assuming a single, contiguous buffer region | 821 | bufferedTime = Math.max(0, buffered.end(0) - currentTime); |
790 | bufferedTime = tech.buffered().end(0) - tech.currentTime(); | ||
791 | } | 822 | } |
792 | 823 | ||
793 | // if there is plenty of content in the buffer and we're not | 824 | // if there is plenty of content in the buffer and we're not | ... | ... |
-
Please register or sign in to post a comment