5aec30b9 by Tom Johnson Committed by Gary Katsevman

encapsultate bandwidth routine

1 parent de317697
...@@ -58,6 +58,8 @@ ...@@ -58,6 +58,8 @@
58 haveMetadata = function(error, xhr, url) { 58 haveMetadata = function(error, xhr, url) {
59 var parser, refreshDelay, update; 59 var parser, refreshDelay, update;
60 60
61 loader.bandwidth = request.bandwidth || xhr.bandwidth;
62
61 // any in-flight request is now finished 63 // any in-flight request is now finished
62 request = null; 64 request = null;
63 65
......
...@@ -409,12 +409,20 @@ videojs.Hls.prototype.fillBuffer = function(offset) { ...@@ -409,12 +409,20 @@ videojs.Hls.prototype.fillBuffer = function(offset) {
409 this.loadSegment(segmentUri, offset); 409 this.loadSegment(segmentUri, offset);
410 }; 410 };
411 411
412 // Encapsulate the setBandwidth routine for future expansion
413 videojs.Hls.prototype.setBandwidthByXHR = function(xhr) {
414 var tech = this;
415 // calculate the download bandwidth
416 tech.segmentXhrTime = xhr.roundTripTime;
417 tech.bandwidth = xhr.bandwidth;
418 tech.bytesReceived += xhr.bytesReceived;
419 };
420
412 videojs.Hls.prototype.loadSegment = function(segmentUri, offset) { 421 videojs.Hls.prototype.loadSegment = function(segmentUri, offset) {
413 var 422 var
414 tech = this, 423 tech = this,
415 player = this.player(), 424 player = this.player(),
416 settings = player.options().hls || {}, 425 settings = player.options().hls || {};
417 startTime = +new Date();
418 426
419 // request the next segment 427 // request the next segment
420 this.segmentXhr_ = videojs.Hls.xhr({ 428 this.segmentXhr_ = videojs.Hls.xhr({
...@@ -448,10 +456,7 @@ videojs.Hls.prototype.loadSegment = function(segmentUri, offset) { ...@@ -448,10 +456,7 @@ videojs.Hls.prototype.loadSegment = function(segmentUri, offset) {
448 return; 456 return;
449 } 457 }
450 458
451 // calculate the download bandwidth 459 tech.setBandwidthByXHR(this);
452 tech.segmentXhrTime = (+new Date()) - startTime;
453 tech.bandwidth = (this.response.byteLength / tech.segmentXhrTime) * 8 * 1000;
454 tech.bytesReceived += this.response.byteLength;
455 460
456 // package up all the work to append the segment 461 // package up all the work to append the segment
457 // if the segment is the start of a timestamp discontinuity, 462 // if the segment is the start of a timestamp discontinuity,
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
34 request = new window.XMLHttpRequest(); 34 request = new window.XMLHttpRequest();
35 request.open(options.method, url); 35 request.open(options.method, url);
36 request.url = url; 36 request.url = url;
37 request.requestTime = new Date().getTime();
37 38
38 if (options.responseType) { 39 if (options.responseType) {
39 request.responseType = options.responseType; 40 request.responseType = options.responseType;
...@@ -69,6 +70,13 @@ ...@@ -69,6 +70,13 @@
69 return callback.call(this, true, url); 70 return callback.call(this, true, url);
70 } 71 }
71 72
73 if (this.response) {
74 this.responseTime = new Date().getTime();
75 this.roundTripTime = this.responseTime - this.requestTime;
76 this.bytesReceived = this.response.byteLength || this.response.length;
77 this.bandwidth = parseInt((this.bytesReceived / this.roundTripTime) * 8 * 1000,10);
78 }
79
72 return callback.call(this, false, url); 80 return callback.call(this, false, url);
73 }; 81 };
74 request.send(null); 82 request.send(null);
......