b22f4c3f by Steve Heffernan

Reorganized videojs-hls.js to make it a bit more readable and set it up for further simplification

1 parent 74ac9838
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 <script src="src/videojs-hls.js"></script> 16 <script src="src/videojs-hls.js"></script>
17 17
18 <!-- segment handling --> 18 <!-- segment handling -->
19 <script src="src/xhr.js"></script>
19 <script src="src/flv-tag.js"></script> 20 <script src="src/flv-tag.js"></script>
20 <script src="src/exp-golomb.js"></script> 21 <script src="src/exp-golomb.js"></script>
21 <script src="src/h264-stream.js"></script> 22 <script src="src/h264-stream.js"></script>
......
1 (function(videojs){
2 /**
3 * Creates and sends an XMLHttpRequest.
4 * TODO - expose video.js core's XHR and use that instead
5 *
6 * @param options {string | object} if this argument is a string, it
7 * is intrepreted as a URL and a simple GET request is
8 * inititated. If it is an object, it should contain a `url`
9 * property that indicates the URL to request and optionally a
10 * `method` which is the type of HTTP request to send.
11 * @param callback (optional) {function} a function to call when the
12 * request completes. If the request was not successful, the first
13 * argument will be falsey.
14 * @return {object} the XMLHttpRequest that was initiated.
15 */
16 videojs.Hls.xhr = function(url, callback) {
17 var
18 options = {
19 method: 'GET',
20 timeout: 45 * 1000
21 },
22 request,
23 abortTimeout;
24
25 if (typeof callback !== 'function') {
26 callback = function() {};
27 }
28
29 if (typeof url === 'object') {
30 options = videojs.util.mergeOptions(options, url);
31 url = options.url;
32 }
33
34 request = new window.XMLHttpRequest();
35 request.open(options.method, url);
36 request.url = url;
37
38 if (options.responseType) {
39 request.responseType = options.responseType;
40 }
41 if (options.withCredentials) {
42 request.withCredentials = true;
43 }
44 if (options.timeout) {
45 if (request.timeout === 0) {
46 request.timeout = options.timeout;
47 request.ontimeout = function() {
48 request.timedout = true;
49 };
50 } else {
51 // polyfill XHR2 by aborting after the timeout
52 abortTimeout = window.setTimeout(function() {
53 if (request.readyState !== 4) {
54 request.timedout = true;
55 request.abort();
56 }
57 }, options.timeout);
58 }
59 }
60
61 request.onreadystatechange = function() {
62 // wait until the request completes
63 if (this.readyState !== 4) {
64 return;
65 }
66
67 // clear outstanding timeouts
68 window.clearTimeout(abortTimeout);
69
70 // request timeout
71 if (request.timedout) {
72 return callback.call(this, 'timeout', url);
73 }
74
75 // request aborted or errored
76 if (this.status >= 400 || this.status === 0) {
77 return callback.call(this, true, url);
78 }
79
80 return callback.call(this, false, url);
81 };
82 request.send(null);
83 return request;
84 };
85
86 })(window.videojs);
...\ No newline at end of file ...\ No newline at end of file
...@@ -79,6 +79,7 @@ module.exports = function(config) { ...@@ -79,6 +79,7 @@ module.exports = function(config) {
79 '../node_modules/videojs-contrib-media-sources/src/videojs-media-sources.js', 79 '../node_modules/videojs-contrib-media-sources/src/videojs-media-sources.js',
80 '../test/karma-qunit-shim.js', 80 '../test/karma-qunit-shim.js',
81 '../src/videojs-hls.js', 81 '../src/videojs-hls.js',
82 '../src/xhr.js',
82 '../src/flv-tag.js', 83 '../src/flv-tag.js',
83 '../src/exp-golomb.js', 84 '../src/exp-golomb.js',
84 '../src/h264-stream.js', 85 '../src/h264-stream.js',
......
...@@ -43,6 +43,7 @@ module.exports = function(config) { ...@@ -43,6 +43,7 @@ module.exports = function(config) {
43 '../node_modules/videojs-contrib-media-sources/src/videojs-media-sources.js', 43 '../node_modules/videojs-contrib-media-sources/src/videojs-media-sources.js',
44 '../test/karma-qunit-shim.js', 44 '../test/karma-qunit-shim.js',
45 '../src/videojs-hls.js', 45 '../src/videojs-hls.js',
46 '../src/xhr.js',
46 '../src/flv-tag.js', 47 '../src/flv-tag.js',
47 '../src/exp-golomb.js', 48 '../src/exp-golomb.js',
48 '../src/h264-stream.js', 49 '../src/h264-stream.js',
......
...@@ -123,6 +123,7 @@ ...@@ -123,6 +123,7 @@
123 <script src="../../node_modules/video.js/dist/video-js/video.js"></script> 123 <script src="../../node_modules/video.js/dist/video-js/video.js"></script>
124 <script src="../../node_modules/videojs-contrib-media-sources/src/videojs-media-sources.js"></script> 124 <script src="../../node_modules/videojs-contrib-media-sources/src/videojs-media-sources.js"></script>
125 <script src="../../src/videojs-hls.js"></script> 125 <script src="../../src/videojs-hls.js"></script>
126 <script src="../../src/xhr.js"></script>
126 <script src="../../src/stream.js"></script> 127 <script src="../../src/stream.js"></script>
127 <script src="../../src/m3u8/m3u8-parser.js"></script> 128 <script src="../../src/m3u8/m3u8-parser.js"></script>
128 <script src="../../src/playlist-loader.js"></script> 129 <script src="../../src/playlist-loader.js"></script>
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
20 20
21 <!-- HLS plugin --> 21 <!-- HLS plugin -->
22 <script src="../src/videojs-hls.js"></script> 22 <script src="../src/videojs-hls.js"></script>
23 <script src="../src/xhr.js"></script>
23 <script src="../src/flv-tag.js"></script> 24 <script src="../src/flv-tag.js"></script>
24 <script src="../src/exp-golomb.js"></script> 25 <script src="../src/exp-golomb.js"></script>
25 <script src="../src/h264-stream.js"></script> 26 <script src="../src/h264-stream.js"></script>
......