6e4ebe73 by David LaPalomento

Pull out XHR helper

Create a helper method for creating XMLHttpRequests and migrate downloadPlaylist to using it.
1 parent c30a0cfa
......@@ -94,6 +94,35 @@ var
}
},
xhr = function(url, callback) {
var
options = {
method: 'GET'
},
request;
if (typeof url === 'object') {
options = videojs.util.mergeOptions(options, url);
url = options.url;
}
request = new window.XMLHttpRequest();
request.open(options.method, url);
request.onreadystatechange = function() {
// wait until the request completes
if (this.readyState !== 4) {
return;
}
// request error
if (this.status >= 400 || this.status === 0) {
return callback.call(this, true, url);
}
return callback.call(this, false, url);
};
request.send(null);
return request;
},
/**
* TODO - Document this great feature.
*
......@@ -350,7 +379,7 @@ var
if (!playlist.segments ||
mediaSequence < (playlist.mediaSequence || 0) ||
mediaSequence > (playlist.mediaSequence || 0) + playlist.segments.length) {
downloadPlaylist(resolveUrl(srcUrl, playlist.uri));
xhr(resolveUrl(srcUrl, playlist.uri), downloadPlaylist);
} else {
player.hls.mediaIndex =
findCorrespondingMediaIndex(player.hls.mediaIndex,
......@@ -437,32 +466,26 @@ var
};
/**
* Download an M3U8 and update the current manifest object. If the provided
* URL is a master playlist, the default variant will be downloaded and
* parsed as well. Triggers `loadedmanifest` once for each playlist that is
* downloaded and `loadedmetadata` after at least one media playlist has
* been parsed. Whether multiple playlists were downloaded or not, when
* `loadedmetadata` fires a parsed or inferred master playlist object will
* be available as `player.hls.master`.
* Callback that is invoked when a playlist finishes
* downloading. If the response is a master playlist, the default
* variant will be downloaded and parsed as well. Triggers
* `loadedmanifest` once for each playlist that is downloaded and
* `loadedmetadata` after at least one media playlist has been
* parsed. Whether multiple playlists were downloaded or not, when
* `loadedmetadata` fires a parsed or inferred master playlist
* object will be available as `player.hls.master`.
*
* @param error {*} truthy if the request was not successful
* @param url {string} a URL to the M3U8 file to process
*/
downloadPlaylist = function(url) {
var xhr = new window.XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = function() {
downloadPlaylist = function(error, url) {
var i, parser, playlist, playlistUri, refreshDelay;
// wait until the request completes
if (xhr.readyState !== 4) {
return;
}
if (xhr.status >= 400 || this.status === 0) {
if (error) {
player.hls.error = {
status: xhr.status,
status: this.status,
message: 'HLS playlist request error at URL: ' + url,
code: (xhr.status >= 500) ? 4 : 2
code: (this.status >= 500) ? 4 : 2
};
player.trigger('error');
return;
......@@ -470,12 +493,12 @@ var
// readystate DONE
parser = new videojs.m3u8.Parser();
parser.push(xhr.responseText);
parser.push(this.responseText);
// master playlists
if (parser.manifest.playlists) {
player.hls.master = parser.manifest;
downloadPlaylist(resolveUrl(url, parser.manifest.playlists[0].uri));
xhr(resolveUrl(url, parser.manifest.playlists[0].uri), downloadPlaylist);
player.trigger('loadedmanifest');
return;
}
......@@ -525,7 +548,7 @@ var
// check the playlist for updates if EXT-X-ENDLIST isn't present
if (!parser.manifest.endList) {
window.setTimeout(function() {
downloadPlaylist(url);
xhr(url, downloadPlaylist);
}, refreshDelay);
}
......@@ -550,8 +573,6 @@ var
player.trigger('loadedmanifest');
};
xhr.send(null);
};
/**
* Determines whether there is enough video data currently in the buffer
......@@ -668,7 +689,7 @@ var
sourceBuffer.appendBuffer(segmentParser.getFlvHeader());
player.hls.mediaIndex = 0;
downloadPlaylist(srcUrl);
xhr(srcUrl, downloadPlaylist);
});
player.src([{
src: videojs.URL.createObjectURL(mediaSource),
......