Update ReadMe.md
[qtwebkit.git] / PerformanceTests / Media / media-source-loader.js
blob24144f75a88ff2bb99d04f2a4a8049d56f6635be
1 class MediaSourceLoader {
2 constructor(url)
4 this._url = url;
5 this.onload = null;
6 this.onerror = null;
9 loadManifest()
11 return new Promise((resolve, reject) => {
12 if (this._manifest) {
13 resolve();
14 return;
17 var request = new XMLHttpRequest();
18 request.open('GET', this._url, true);
19 request.responseType = 'json';
20 request.onload = (event) => {
21 this.loadManifestSucceeded(event);
22 resolve();
24 request.onerror = (event) => {
25 this.loadManifestFailed(event);
26 reject(event);
28 request.send();
32 loadManifestSucceeded(event)
34 this._manifest = event.target.response;
36 if (!this._manifest || !this._manifest.url) {
37 if (this.onerror)
38 this.onerror();
39 return;
43 loadManifestFailed()
45 if (this.onerror)
46 this.onerror();
49 loadMediaData()
51 return new Promise((resolve, reject) => {
52 this.loadManifest().then(() => {
53 var request = new XMLHttpRequest();
54 request.open('GET', this._manifest.url, true);
55 request.responseType = 'arraybuffer';
56 request.onload = (event) => {
57 this.loadMediaDataSucceeded(event);
58 resolve();
60 request.onerror = (event) => {
61 this.loadMediaDataFailed(event);
62 reject(event);
64 request.send();
65 });
66 });
69 loadMediaDataSucceeded(event)
71 this._mediaData = event.target.response;
73 if (this.onload)
74 this.onload();
77 loadMediaDataFailed()
79 if (this.onerror)
80 this.onerror();
83 get type()
85 return this._manifest ? this._manifest.type : "";
88 get duration()
90 if (!this._manifest)
91 return 0;
92 return this._manifest.media.reduce((duration, media) => { return duration + media.duration }, 0);
95 get initSegment()
97 if (!this._manifest || !this._manifest.init || !this._mediaData)
98 return null;
99 var init = this._manifest.init;
100 return this._mediaData.slice(init.offset, init.offset + init.size);
103 get mediaSegmentsLength()
105 if (!this._manifest || !this._manifest.media)
106 return 0;
107 return this._manifest.media.length;
110 *mediaSegments()
112 if (!this._manifest || !this._manifest.media || !this._mediaData)
113 return;
115 for (var media of this._manifest.media)
116 yield this._mediaData.slice(media.offset, media.offset + media.size);
119 get everyMediaSegment()
121 if (!this._manifest || !this._manifest.media || !this._mediaData)
122 return null;
124 return this._mediaData.slice(this._manifest.media[0].offset);