d8ff8610 by Katrina Ellison Geltman Committed by David LaPalomento

Add support for #EXT-X-BYTERANGE tag

Ports #451 to the videojs-contrib-hls development branch
1 parent 425d7a72
......@@ -894,7 +894,7 @@ videojs.HlsHandler.prototype.fillBuffer = function(mediaIndex) {
// we have entered a state where we are fetching the same segment,
// try to walk forward
if (this.lastSegmentLoaded_ &&
this.lastSegmentLoaded_ === this.playlistUriToUrl(segment.uri)) {
this.lastSegmentLoaded_ === segment) {
return this.fillBuffer(mediaIndex + 1);
}
......@@ -961,6 +961,29 @@ videojs.HlsHandler.prototype.playlistUriToUrl = function(segmentRelativeUrl) {
return playListUrl;
};
/* Turns segment byterange into a string suitable for use in
* HTTP Range requests
*/
videojs.HlsHandler.prototype.byterangeStr = function(byterange) {
var byterangeStart, byterangeEnd;
// `byterangeEnd` is one less than `offset + length` because the HTTP range
// header uses inclusive ranges
byterangeEnd = byterange.offset + byterange.length - 1;
byterangeStart = byterange.offset;
return "bytes=" + byterangeStart + "-" + byterangeEnd;
};
/* Defines headers for use in the xhr request for a particular segment.
*/
videojs.HlsHandler.prototype.segmentXhrHeaders = function(segment) {
var headers = {};
if ('byterange' in segment) {
headers['Range'] = this.byterangeStr(segment.byterange);
}
return headers;
};
/*
* Sets `bandwidth`, `segmentXhrTime`, and appends to the `bytesReceived.
* Expects an object with:
......@@ -1054,7 +1077,8 @@ videojs.HlsHandler.prototype.loadSegment = function(segmentInfo) {
// Set xhr timeout to 150% of the segment duration to allow us
// some time to switch renditions in the event of a catastrophic
// decrease in network performance or a server issue.
timeout: (segment.duration * 1.5) * 1000
timeout: (segment.duration * 1.5) * 1000,
headers: this.segmentXhrHeaders(segment)
}, function(error, request) {
// This is a timeout of a previously aborted segment request
// so simply ignore it
......@@ -1085,7 +1109,7 @@ videojs.HlsHandler.prototype.loadSegment = function(segmentInfo) {
return;
}
self.lastSegmentLoaded_ = segmentInfo.uri;
self.lastSegmentLoaded_ = segment;
self.setBandwidth(request);
if (segment.key) {
......