Refactoring: Pull all fullscreen code out of Document and into its own helper class
[WebKit-https.git] / PerformanceTests / Media / MSERemoveCodedFrames.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="media-source-loader.js"></script>
5 <script src="../resources/runner.js"></script>
6 <script>
7 var loader;
8 var video;
9 var longMediaSegment;
10
11 function concatArrayBuffers() {
12     var byteLength = 0;
13     Array.prototype.forEach.call(arguments, arrayBuffer => {
14         if (!arrayBuffer.byteLength)
15             throw "Not an ArrayBuffer!";
16         byteLength += arrayBuffer.byteLength;
17     });
18
19
20     var view = new Uint8Array(byteLength);
21     var offset = 0;
22     Array.prototype.forEach.call(arguments, arrayBuffer => {
23         view.set(new Uint8Array(arrayBuffer), offset);
24         offset += arrayBuffer.byteLength;
25     });
26     return view.buffer;
27 }
28
29 function concatMediaData() {
30     return new Promise((resolve, reject) => {
31         var segments = new Array(100);
32         segments.fill(loader.everyMediaSegment);
33         longMediaSegment = concatArrayBuffers.apply(this, segments);
34         resolve(longMediaSegment);
35     });
36 }
37
38 window.addEventListener('load', () => {
39     PerfTestRunner.prepareToMeasureValuesAsync({
40         unit: 'ms',
41         done: function () {
42             if (video) {
43                 video.src = null;
44                 video.load();
45             }
46         }
47     });
48
49     loader = new MediaSourceLoader('test-fragmented-video.json');
50     loader.loadMediaData().then(concatMediaData).then(runTest);
51 });
52
53 function remove30SecondsAtATimeTillEmpty(sourceBuffer) {
54     return new Promise(resolve => {
55         var removeNext30Seconds = () => {
56             var start = sourceBuffer.buffered.start(0);
57             sourceBuffer.remove(start, start + 30)
58         }
59         sourceBuffer.onupdate = () => {
60             if (sourceBuffer.buffered.length == 0 || sourceBuffer.buffered.start(0) - sourceBuffer.buffered.end(0) == 0) {
61                 sourceBuffer.onupdate = null;
62                 resolve();
63                 return;
64             }
65             
66             removeNext30Seconds();
67         };
68         removeNext30Seconds();
69     });
70 }
71
72 function runTest() {   
73     video =  document.createElement('video');
74
75     loadMediaDataIntoVideo(video).then(sourceBuffer => {
76         startTime = PerfTestRunner.now();
77         return remove30SecondsAtATimeTillEmpty(sourceBuffer);
78     }).then(() => {
79         if (PerfTestRunner.measureValueAsync(PerfTestRunner.now() - startTime))
80             setTimeout(runTest, 0);
81     });
82 }
83
84 function loadMediaDataIntoVideo(video, segmentCount) {
85     return new Promise((resolve, reject) => {
86         var source = new MediaSource();
87         source.onsourceopen = () => {
88             source.onsourceopen = null;
89             source.duration = loader.duration * 100; 
90             var currentMediaSegment = 0;
91             var sourceBuffer = source.addSourceBuffer(loader.type);
92             sourceBuffer.mode = 'sequence';
93             sourceBuffer.appendBuffer(loader.initSegment);
94
95             var appendedMediaSegment = false;
96             sourceBuffer.onupdate = () => {
97
98                 if (appendedMediaSegment) {
99                     if (source.readyState !== 'ended') {
100                         source.endOfStream();
101                         sourceBuffer.onupdate = null;
102                         sourceBuffer.onerror = null;
103                         resolve(sourceBuffer);
104                     }
105                     return;
106                 }
107
108                 sourceBuffer.appendBuffer(longMediaSegment);
109                 appendedMediaSegment = true;
110             };
111             sourceBuffer.onerror = error => {
112                 reject(error);
113             };
114         };
115         video.src = URL.createObjectURL(source);
116     });
117 }
118 </script>
119 </head>
120 <body>
121 </body>
122 </html>