Refactoring: Pull all fullscreen code out of Document and into its own helper class
[WebKit-https.git] / PerformanceTests / Media / media-source-loader.js
1 class MediaSourceLoader {
2     constructor(url)
3     {
4         this._url = url;
5         this.onload = null;
6         this.onerror = null;
7     }
8
9     loadManifest()
10     {
11         return new Promise((resolve, reject) => {
12             if (this._manifest) {
13                 resolve();
14                 return;
15             }
16
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();
23             }
24             request.onerror = (event) => {
25                 this.loadManifestFailed(event);
26                 reject(event);
27             }
28             request.send();
29         })
30     }
31
32     loadManifestSucceeded(event)
33     {
34         this._manifest = event.target.response;
35
36         if (!this._manifest || !this._manifest.url) {
37             if (this.onerror)
38                 this.onerror();
39             return;
40         }
41     }
42
43     loadManifestFailed()
44     {
45         if (this.onerror)
46             this.onerror();
47     }
48
49     loadMediaData()
50     {
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();
59                 }
60                 request.onerror = (event) => {
61                     this.loadMediaDataFailed(event);
62                     reject(event);
63                 }
64                 request.send();
65             });
66         });
67     }
68
69     loadMediaDataSucceeded(event)
70     {
71         this._mediaData = event.target.response;
72
73         if (this.onload)
74             this.onload();
75     }
76
77     loadMediaDataFailed()
78     {
79         if (this.onerror)
80             this.onerror();
81     }
82
83     get type()
84     {
85         return this._manifest ? this._manifest.type : "";
86     }
87
88     get duration()
89     {
90         if (!this._manifest)
91             return 0;
92         return this._manifest.media.reduce((duration, media) => { return duration + media.duration }, 0);
93     }
94
95     get initSegment()
96     {
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);
101     }
102
103     get mediaSegmentsLength()
104     {
105         if (!this._manifest || !this._manifest.media)
106             return 0;
107         return this._manifest.media.length;   
108     }
109
110     *mediaSegments()
111     {
112         if (!this._manifest || !this._manifest.media || !this._mediaData)
113             return;
114
115         for (var media of this._manifest.media)
116             yield this._mediaData.slice(media.offset, media.offset + media.size);
117     }
118
119     get everyMediaSegment()
120     {
121         if (!this._manifest || !this._manifest.media || !this._mediaData)
122             return null;
123
124         return this._mediaData.slice(this._manifest.media[0].offset);
125     }
126 };