9dde5c62 by jrivera

Added documentation for hls.xhr.beforeRequest with examples

1 parent 277bea49
...@@ -19,6 +19,7 @@ Play back HLS with video.js, even where it's not natively supported. ...@@ -19,6 +19,7 @@ Play back HLS with video.js, even where it's not natively supported.
19 - [hls.bandwidth](#hlsbandwidth) 19 - [hls.bandwidth](#hlsbandwidth)
20 - [hls.bytesReceived](#hlsbytesreceived) 20 - [hls.bytesReceived](#hlsbytesreceived)
21 - [hls.selectPlaylist](#hlsselectplaylist) 21 - [hls.selectPlaylist](#hlsselectplaylist)
22 - [hls.xhr](#hlsxhr)
22 - [Events](#events) 23 - [Events](#events)
23 - [loadedmetadata](#loadedmetadata) 24 - [loadedmetadata](#loadedmetadata)
24 - [loadedplaylist](#loadedplaylist) 25 - [loadedplaylist](#loadedplaylist)
...@@ -194,6 +195,44 @@ segment is downloaded. You can override this function to provide your ...@@ -194,6 +195,44 @@ segment is downloaded. You can override this function to provide your
194 adaptive streaming logic. You must, however, be sure to return a valid 195 adaptive streaming logic. You must, however, be sure to return a valid
195 media playlist object that is present in `player.hls.master`. 196 media playlist object that is present in `player.hls.master`.
196 197
198 #### hls.xhr
199 Type: `function`
200
201 The xhr function that is used by HLS internally is exposed on the per-
202 player `hls` object. While it is possible, we do not recommend replacing
203 the function with your own implementation. Instead, the `xhr` provides
204 the ability to specify a `beforeRequest` function that will be called
205 with an object containing the options that will be used to create the
206 xhr request.
207
208 Example:
209 ```javascript
210 player.hls.xhr.beforeRequest = function(options) {
211 options.uri = options.uri.replace('example.com', 'foo.com');
212
213 return options;
214 };
215 ```
216
217 The global `videojs.Hls` also exposes an `xhr` property. Specifying a
218 `beforeRequest` function on that will allow you to intercept the options
219 for *all* requests in every player on a page.
220
221 Example
222 ```javascript
223 videojs.Hls.xhr.beforeRequest = function(options) {
224 /*
225 * Modifications to requests that will affect every player.
226 */
227
228 return options;
229 };
230 ```
231
232 For information on the type of options that you can modify see the
233 documentation at [https://github.com/Raynos/xhr](https://github.com/Raynos/xhr).
234
235
197 ### Events 236 ### Events
198 Standard HTML video events are handled by video.js automatically and 237 Standard HTML video events are handled by video.js automatically and
199 are triggered on the player object. In addition, there are a couple 238 are triggered on the player object. In addition, there are a couple
......
...@@ -3366,17 +3366,15 @@ QUnit.test('selectPlaylist does not fail if getComputedStyle returns null', func ...@@ -3366,17 +3366,15 @@ QUnit.test('selectPlaylist does not fail if getComputedStyle returns null', func
3366 QUnit.test('Allows specifying the beforeRequest functionon the player', function() { 3366 QUnit.test('Allows specifying the beforeRequest functionon the player', function() {
3367 let beforeRequestCalled = false; 3367 let beforeRequestCalled = false;
3368 3368
3369 this.player.ready(function() {
3370 this.hls.xhr.beforeRequest = function() {
3371 beforeRequestCalled = true;
3372 };
3373 });
3374 this.player.src({ 3369 this.player.src({
3375 src: 'master.m3u8', 3370 src: 'master.m3u8',
3376 type: 'application/vnd.apple.mpegurl' 3371 type: 'application/vnd.apple.mpegurl'
3377 }); 3372 });
3378
3379 openMediaSource(this.player, this.clock); 3373 openMediaSource(this.player, this.clock);
3374
3375 this.player.hls.xhr.beforeRequest = function() {
3376 beforeRequestCalled = true;
3377 };
3380 // master 3378 // master
3381 standardXHRResponse(this.requests.shift()); 3379 standardXHRResponse(this.requests.shift());
3382 // media 3380 // media
...@@ -3396,6 +3394,9 @@ QUnit.test('Allows specifying the beforeRequest function globally', function() { ...@@ -3396,6 +3394,9 @@ QUnit.test('Allows specifying the beforeRequest function globally', function() {
3396 src: 'master.m3u8', 3394 src: 'master.m3u8',
3397 type: 'application/vnd.apple.mpegurl' 3395 type: 'application/vnd.apple.mpegurl'
3398 }); 3396 });
3397 openMediaSource(this.player, this.clock);
3398 // master
3399 standardXHRResponse(this.requests.shift());
3399 3400
3400 QUnit.ok(beforeRequestCalled, 'beforeRequest was called'); 3401 QUnit.ok(beforeRequestCalled, 'beforeRequest was called');
3401 3402
...@@ -3414,13 +3415,11 @@ QUnit.test('Allows overriding the global beforeRequest function', function() { ...@@ -3414,13 +3415,11 @@ QUnit.test('Allows overriding the global beforeRequest function', function() {
3414 src: 'master.m3u8', 3415 src: 'master.m3u8',
3415 type: 'application/vnd.apple.mpegurl' 3416 type: 'application/vnd.apple.mpegurl'
3416 }); 3417 });
3417 this.player.ready(function() {
3418 this.hls.xhr.beforeRequest = function() {
3419 beforeLocalRequestCalled++;
3420 };
3421 });
3422
3423 openMediaSource(this.player, this.clock); 3418 openMediaSource(this.player, this.clock);
3419
3420 this.player.hls.xhr.beforeRequest = function() {
3421 beforeLocalRequestCalled++;
3422 };
3424 // master 3423 // master
3425 standardXHRResponse(this.requests.shift()); 3424 standardXHRResponse(this.requests.shift());
3426 // media 3425 // media
......