Emulate some of the old behavior of the original videojs.xhr in the videojs.Hls.xhr wrapper
Add a default timeout of 45 seconds for all requests Create a boolean `timedout` property on request in response to the ETIMEOUT error Create an error code for non-200 responses
Showing
1 changed file
with
24 additions
and
2 deletions
... | @@ -5,13 +5,35 @@ | ... | @@ -5,13 +5,35 @@ |
5 | * A wrapper for videojs.xhr that tracks bandwidth. | 5 | * A wrapper for videojs.xhr that tracks bandwidth. |
6 | */ | 6 | */ |
7 | videojs.Hls.xhr = function(options, callback) { | 7 | videojs.Hls.xhr = function(options, callback) { |
8 | var request = videojs.xhr(options, function(error, request) { | 8 | // Add a default timeout for all hls requests |
9 | if (request.response) { | 9 | options = videojs.mergeOptions({ |
10 | timeout: 45e3 | ||
11 | }, options); | ||
12 | |||
13 | var request = videojs.xhr(options, function(error, response) { | ||
14 | if (!error && request.response) { | ||
10 | request.responseTime = (new Date()).getTime(); | 15 | request.responseTime = (new Date()).getTime(); |
11 | request.roundTripTime = request.responseTime - request.requestTime; | 16 | request.roundTripTime = request.responseTime - request.requestTime; |
12 | request.bytesReceived = request.response.byteLength || request.response.length; | 17 | request.bytesReceived = request.response.byteLength || request.response.length; |
18 | if (!request.bandwidth) { | ||
13 | request.bandwidth = Math.floor((request.bytesReceived / request.roundTripTime) * 8 * 1000); | 19 | request.bandwidth = Math.floor((request.bytesReceived / request.roundTripTime) * 8 * 1000); |
14 | } | 20 | } |
21 | } | ||
22 | |||
23 | // videojs.xhr now uses a specific code on the error object to signal that a request has | ||
24 | // timed out errors of setting a boolean on the request object | ||
25 | if (error || request.timedout) { | ||
26 | request.timedout = request.timedout || (error.code === 'ETIMEDOUT'); | ||
27 | } else { | ||
28 | request.timedout = false; | ||
29 | } | ||
30 | |||
31 | // videojs.xhr no longer consider status codes outside of 200 and 0 (for file uris) to be | ||
32 | // errors but the old XHR did so emulate that behavior | ||
33 | if (!error && response.statusCode !== 200 && response.statusCode !== 0) { | ||
34 | error = new Error('XHR Failed with a response of: ' + | ||
35 | (request && (request.response || request.responseText))); | ||
36 | } | ||
15 | 37 | ||
16 | callback(error, request); | 38 | callback(error, request); |
17 | }); | 39 | }); | ... | ... |
-
Please register or sign in to post a comment