Add some performance tests for various aspects of media loading and playback.
[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         return this._manifest ? this._manifest.duration : 0
91     }
92
93     get initSegment()
94     {
95         if (!this._manifest || !this._manifest.init || !this._mediaData)
96             return null;
97         var init = this._manifest.init;
98         return this._mediaData.slice(init.offset, init.offset + init.size);
99     }
100
101     get mediaSegmentsLength()
102     {
103         if (!this._manifest || !this._manifest.media)
104             return 0;
105         return this._manifest.media.length;   
106     }
107
108     *mediaSegments()
109     {
110         if (!this._manifest || !this._manifest.media || !this._mediaData)
111             return;
112
113         for (var media of this._manifest.media)
114             yield this._mediaData.slice(media.offset, media.offset + media.size);
115     }
116
117     get everyMediaSegment()
118     {
119         if (!this._manifest || !this._manifest.media || !this._mediaData)
120             return null;
121
122         return this._mediaData.slice(this._manifest.media[0].offset);
123     }
124 };