ac179c3e2710430c2be4a88698d9174b44c88833
[WebKit-https.git] / LayoutTests / http / tests / media / media-source / mediasource-buffered.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <script src="/w3c/resources/testharness.js"></script>
5         <script src="/w3c/resources/testharnessreport.js"></script>
6         <script src="mediasource-util.js"></script>
7     </head>
8     <body>
9         <div id="log"></div>
10         <script>
11             var subType = MediaSourceUtil.getSubType(MediaSourceUtil.AUDIO_ONLY_TYPE);
12
13             var manifestFilenameA = subType + "/test-a-128k-44100Hz-1ch-manifest.json";
14             var manifestFilenameB = subType + "/test-v-128k-320x240-30fps-10kfr-manifest.json";
15
16             var expectationsA = {
17               webm: "{ [0.000, 2.022) }",
18               mp4: "{ [0.000, 2.043) }",
19             };
20
21             var expectationsB = {
22               webm: "{ [0.000, 2.000) }",
23               mp4: "{ [0.000, 2.000) }",
24             };
25
26             function mediaSourceDemuxedTest(callback, description)
27             {
28                 mediasource_test(function(test, mediaElement, mediaSource)
29                 {
30                     mediaElement.pause();
31                     test.failOnEvent(mediaElement, 'error');
32                     test.endOnEvent(mediaElement, 'ended');
33
34                     MediaSourceUtil.fetchManifestAndData(test, manifestFilenameA, function(typeA, dataA)
35                     {
36                         MediaSourceUtil.fetchManifestAndData(test, manifestFilenameB, function(typeB, dataB)
37                         {
38                             mediaSource.addSourceBuffer(typeA);
39                             mediaSource.addSourceBuffer(typeB);
40                             assert_equals(mediaSource.sourceBuffers.length, 2);
41
42                             callback(test, mediaElement, mediaSource, dataA, dataB);
43                         });
44                     });
45                 }, description);
46             };
47
48             function appendData(test, mediaSource, dataA, dataB, callback)
49             {
50                 var sourceBufferA = mediaSource.sourceBuffers[0];
51                 var sourceBufferB = mediaSource.sourceBuffers[1];
52
53                 test.expectEvent(sourceBufferA, "update");
54                 test.expectEvent(sourceBufferA, "updateend");
55                 sourceBufferA.appendBuffer(dataA);
56
57                 test.expectEvent(sourceBufferB, "update");
58                 test.expectEvent(sourceBufferB, "updateend");
59                 sourceBufferB.appendBuffer(dataB);
60
61                 test.waitForExpectedEvents(function()
62                 {
63                      callback();
64                 });
65             }
66
67             mediaSourceDemuxedTest(function(test, mediaElement, mediaSource, dataA, dataB) {
68                 appendData(test, mediaSource, dataA, dataB, function()
69                 {
70                     assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
71                     assertBufferedEquals(mediaSource.activeSourceBuffers[1], expectationsB[subType], "mediaSource.activeSourceBuffers[1]");
72                     assertBufferedEquals(mediaElement, expectationsB[subType], "mediaElement.buffered");
73
74                     mediaSource.endOfStream();
75
76                     assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
77                     assertBufferedEquals(mediaSource.activeSourceBuffers[1], expectationsB[subType], "mediaSource.activeSourceBuffers[1]");
78                     assertBufferedEquals(mediaElement, expectationsA[subType], "mediaElement.buffered");
79
80                     test.done();
81                 });
82             }, "Demuxed content with different lengths");
83
84             mediasource_test(function(test, mediaElement, mediaSource)
85             {
86                 mediaElement.pause();
87                 test.failOnEvent(mediaElement, 'error');
88                 test.endOnEvent(mediaElement, 'ended');
89
90                 MediaSourceUtil.fetchManifestAndData(test, subType + "/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr-manifest.json", function(type, data)
91                 {
92                     var sourceBuffer = mediaSource.addSourceBuffer(type);
93                     test.expectEvent(sourceBuffer, "update");
94                     test.expectEvent(sourceBuffer, "updateend");
95                     sourceBuffer.appendBuffer(data);
96
97                     test.waitForExpectedEvents(function()
98                     {
99                         var expectationsAV = {
100                             webm: ["{ [0.000, 2.003) }", "{ [0.000, 2.022) }"],
101                             mp4: ["{ [0.000, 2.000) }", "{ [0.000, 2.043) }"],
102                         };
103
104                         var expectedBeforeEndOfStream = expectationsAV[subType][0];
105                         var expectedAfterEndOfStream = expectationsAV[subType][1];
106
107                         assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectedBeforeEndOfStream, "mediaSource.activeSourceBuffers[0]");
108                         assertBufferedEquals(mediaElement, expectedBeforeEndOfStream, "mediaElement.buffered");
109
110                         mediaSource.endOfStream();
111
112                         assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectedAfterEndOfStream, "mediaSource.activeSourceBuffers[0]");
113                         assertBufferedEquals(mediaElement, expectedAfterEndOfStream, "mediaElement.buffered");
114
115                         test.done();
116                     });
117                 });
118             }, "Muxed tracks with different lengths");
119
120             mediaSourceDemuxedTest(function(test, mediaElement, mediaSource, dataA, dataB) {
121                 var dataBSize = {
122                     webm: 318,
123                     mp4: 856,
124                 };
125                 appendData(test, mediaSource, dataA, dataB.subarray(0, dataBSize[subType]), function()
126                 {
127                     assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
128                     assertBufferedEquals(mediaSource.activeSourceBuffers[1], "{ }", "mediaSource.activeSourceBuffers[1]");
129                     assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");
130
131                     mediaSource.endOfStream();
132
133                     assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
134                     assertBufferedEquals(mediaSource.activeSourceBuffers[1], "{ }", "mediaSource.activeSourceBuffers[1]");
135                     assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");
136
137                     test.done();
138                 });
139             }, "Demuxed content with an empty buffered range on one SourceBuffer");
140
141             mediasource_test(function(test, mediaElement, mediaSource)
142             {
143                 mediaElement.pause();
144                 test.failOnEvent(mediaElement, 'error');
145                 test.endOnEvent(mediaElement, 'ended');
146
147                 MediaSourceUtil.fetchManifestAndData(test, subType + "/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr-manifest.json", function(type, data)
148                 {
149                     var sourceBuffer = mediaSource.addSourceBuffer(type);
150                     test.expectEvent(sourceBuffer, "update");
151                     test.expectEvent(sourceBuffer, "updateend");
152                     sourceBuffer.appendBuffer(data.subarray(0, 4052));
153
154                     test.waitForExpectedEvents(function()
155                     {
156                         assertBufferedEquals(mediaSource.activeSourceBuffers[0], "{ }", "mediaSource.activeSourceBuffers[0]");
157                         assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");
158
159                         mediaSource.endOfStream();
160
161                         assertBufferedEquals(mediaSource.activeSourceBuffers[0], "{ }", "mediaSource.activeSourceBuffers[0]");
162                         assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");
163
164                         test.done();
165                     });
166                 });
167             }, "Muxed content empty buffered ranges.");
168
169         </script>
170     </body>
171 </html>