5aec30b9 by Tom Johnson Committed by Gary Katsevman

encapsultate bandwidth routine

1 parent de317697
......@@ -58,6 +58,8 @@
haveMetadata = function(error, xhr, url) {
var parser, refreshDelay, update;
loader.bandwidth = request.bandwidth || xhr.bandwidth;
// any in-flight request is now finished
request = null;
......
......@@ -409,12 +409,20 @@ videojs.Hls.prototype.fillBuffer = function(offset) {
this.loadSegment(segmentUri, offset);
};
// Encapsulate the setBandwidth routine for future expansion
videojs.Hls.prototype.setBandwidthByXHR = function(xhr) {
var tech = this;
// calculate the download bandwidth
tech.segmentXhrTime = xhr.roundTripTime;
tech.bandwidth = xhr.bandwidth;
tech.bytesReceived += xhr.bytesReceived;
};
videojs.Hls.prototype.loadSegment = function(segmentUri, offset) {
var
tech = this,
player = this.player(),
settings = player.options().hls || {},
startTime = +new Date();
settings = player.options().hls || {};
// request the next segment
this.segmentXhr_ = videojs.Hls.xhr({
......@@ -448,10 +456,7 @@ videojs.Hls.prototype.loadSegment = function(segmentUri, offset) {
return;
}
// calculate the download bandwidth
tech.segmentXhrTime = (+new Date()) - startTime;
tech.bandwidth = (this.response.byteLength / tech.segmentXhrTime) * 8 * 1000;
tech.bytesReceived += this.response.byteLength;
tech.setBandwidthByXHR(this);
// package up all the work to append the segment
// if the segment is the start of a timestamp discontinuity,
......
......@@ -34,6 +34,7 @@
request = new window.XMLHttpRequest();
request.open(options.method, url);
request.url = url;
request.requestTime = new Date().getTime();
if (options.responseType) {
request.responseType = options.responseType;
......@@ -69,6 +70,13 @@
return callback.call(this, true, url);
}
if (this.response) {
this.responseTime = new Date().getTime();
this.roundTripTime = this.responseTime - this.requestTime;
this.bytesReceived = this.response.byteLength || this.response.length;
this.bandwidth = parseInt((this.bytesReceived / this.roundTripTime) * 8 * 1000,10);
}
return callback.call(this, false, url);
};
request.send(null);
......