forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-source-loader.js
126 lines (109 loc) · 3.1 KB
/
media-source-loader.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class MediaSourceLoader {
constructor(url)
{
this._url = url;
this.onload = null;
this.onerror = null;
}
loadManifest()
{
return new Promise((resolve, reject) => {
if (this._manifest) {
resolve();
return;
}
var request = new XMLHttpRequest();
request.open('GET', this._url, true);
request.responseType = 'json';
request.onload = (event) => {
this.loadManifestSucceeded(event);
resolve();
}
request.onerror = (event) => {
this.loadManifestFailed(event);
reject(event);
}
request.send();
})
}
loadManifestSucceeded(event)
{
this._manifest = event.target.response;
if (!this._manifest || !this._manifest.url) {
if (this.onerror)
this.onerror();
return;
}
}
loadManifestFailed()
{
if (this.onerror)
this.onerror();
}
loadMediaData()
{
return new Promise((resolve, reject) => {
this.loadManifest().then(() => {
var request = new XMLHttpRequest();
request.open('GET', this._manifest.url, true);
request.responseType = 'arraybuffer';
request.onload = (event) => {
this.loadMediaDataSucceeded(event);
resolve();
}
request.onerror = (event) => {
this.loadMediaDataFailed(event);
reject(event);
}
request.send();
});
});
}
loadMediaDataSucceeded(event)
{
this._mediaData = event.target.response;
if (this.onload)
this.onload();
}
loadMediaDataFailed()
{
if (this.onerror)
this.onerror();
}
get type()
{
return this._manifest ? this._manifest.type : "";
}
get duration()
{
if (!this._manifest)
return 0;
return this._manifest.media.reduce((duration, media) => { return duration + media.duration }, 0);
}
get initSegment()
{
if (!this._manifest || !this._manifest.init || !this._mediaData)
return null;
var init = this._manifest.init;
return this._mediaData.slice(init.offset, init.offset + init.size);
}
get mediaSegmentsLength()
{
if (!this._manifest || !this._manifest.media)
return 0;
return this._manifest.media.length;
}
*mediaSegments()
{
if (!this._manifest || !this._manifest.media || !this._mediaData)
return;
for (var media of this._manifest.media)
yield this._mediaData.slice(media.offset, media.offset + media.size);
}
get everyMediaSegment()
{
if (!this._manifest || !this._manifest.media || !this._mediaData)
return null;
return this._mediaData.slice(this._manifest.media[0].offset);
}
};