playlist-loader.js
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* A state machine that manages the loading, caching, and updating of
* M3U8 playlists.
*/
(function(window) {
'use strict';
var
/* XXX COPIED REMOVE ME */
/**
* Constructs a new URI by interpreting a path relative to another
* URI.
* @param basePath {string} a relative or absolute URI
* @param path {string} a path part to combine with the base
* @return {string} a URI that is equivalent to composing `base`
* with `path`
* @see http://stackoverflow.com/questions/470832/getting-an-absolute-url-from-a-relative-one-ie6-issue
*/
resolveUrl = function(basePath, path) {
// use the base element to get the browser to handle URI resolution
var
oldBase = document.querySelector('base'),
docHead = document.querySelector('head'),
a = document.createElement('a'),
base = oldBase,
oldHref,
result;
// prep the document
if (oldBase) {
oldHref = oldBase.href;
} else {
base = docHead.appendChild(document.createElement('base'));
}
base.href = basePath;
a.href = path;
result = a.href;
// clean up
if (oldBase) {
oldBase.href = oldHref;
} else {
docHead.removeChild(base);
}
return result;
},
PlaylistLoader = function(url) {
var
loader = this,
request;
if (!url) {
throw new Error('A non-empty playlist URL is required');
}
loader.state = 'HAVE_NOTHING';
request = new window.XMLHttpRequest();
request.open('GET', url);
request.onreadystatechange = function() {
var parser = new videojs.m3u8.Parser();
parser.push(this.responseText);
if (parser.manifest.playlists) {
loader.master = parser.manifest;
} else {
// infer a master playlist if none was previously requested
loader.master = {
playlists: [parser.manifest]
};
}
loader.state = 'HAVE_MASTER';
return;
};
request.send(null);
};
window.videojs.hls.PlaylistLoader = PlaylistLoader;
})(window);