summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-buffered.html
blob: 38cd1717eb23be2d6ef9dde3959aa79b48621193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>SourceBuffer.buffered test cases.</title>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>
    </head>
    <body>
        <div id="log"></div>
        <script>
            var subType = MediaSourceUtil.getSubType(MediaSourceUtil.AUDIO_ONLY_TYPE);

            var manifestFilenameA = subType + "/test-a-128k-44100Hz-1ch-manifest.json";
            var manifestFilenameB = subType + "/test-v-128k-320x240-30fps-10kfr-manifest.json";

            // Audio track expectations
            var expectationsA = {
              webm: "{ [0.000, 2.023) }",
              mp4: "{ [0.000, 2.043) }",
            };

            // Video track expectations
            var expectationsB = {
              webm: "{ [0.000, 2.001) }",
              mp4: "{ [0.067, 2.067) }",
            };

            // Audio and Video intersection expectations.
            // https://w3c.github.io/media-source/index.html#dom-sourcebuffer-buffered
            // When mediaSource.readyState is "ended", then set the end time on the last range in track ranges to highest end time.
            var expectationsC = {
              webm: ["{ [0.000, 2.001) }", "{ [0.000, 2.023) }"],
              mp4: ["{ [0.067, 2.043) }", "{ [0.067, 2.067) }"]
            };

            function mediaSourceDemuxedTest(callback, description)
            {
                mediasource_test(function(test, mediaElement, mediaSource)
                {
                    mediaElement.pause();
                    mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
                    mediaElement.addEventListener("ended", test.step_func_done());

                    MediaSourceUtil.fetchManifestAndData(test, manifestFilenameA, function(typeA, dataA)
                    {
                        MediaSourceUtil.fetchManifestAndData(test, manifestFilenameB, function(typeB, dataB)
                        {
                            mediaSource.addSourceBuffer(typeA);
                            mediaSource.addSourceBuffer(typeB);
                            assert_equals(mediaSource.sourceBuffers.length, 2);

                            callback(test, mediaElement, mediaSource, dataA, dataB);
                        });
                    });
                }, description);
            };

            function appendData(test, mediaSource, dataA, dataB, callback)
            {
                var sourceBufferA = mediaSource.sourceBuffers[0];
                var sourceBufferB = mediaSource.sourceBuffers[1];

                test.expectEvent(sourceBufferA, "update");
                test.expectEvent(sourceBufferA, "updateend");
                sourceBufferA.appendBuffer(dataA);

                test.expectEvent(sourceBufferB, "update");
                test.expectEvent(sourceBufferB, "updateend");
                sourceBufferB.appendBuffer(dataB);

                test.waitForExpectedEvents(function()
                {
                     callback();
                });
            }

            mediaSourceDemuxedTest(function(test, mediaElement, mediaSource, dataA, dataB) {
                test.expectEvent(mediaElement, "loadedmetadata");
                appendData(test, mediaSource, dataA, dataB, function()
                {
                    var expectedBeforeEndOfStreamIntersection = expectationsC[subType][0];
                    var expectedAfterEndOfStreamIntersection = expectationsC[subType][1];

                    assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
                    assertBufferedEquals(mediaSource.activeSourceBuffers[1], expectationsB[subType], "mediaSource.activeSourceBuffers[1]");
                    assertBufferedEquals(mediaElement, expectedBeforeEndOfStreamIntersection, "mediaElement.buffered");

                    mediaSource.endOfStream();

                    assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
                    assertBufferedEquals(mediaSource.activeSourceBuffers[1], expectationsB[subType], "mediaSource.activeSourceBuffers[1]");
                    assertBufferedEquals(mediaElement, expectedAfterEndOfStreamIntersection, "mediaElement.buffered");

                    test.done();
                });
            }, "Demuxed content with different lengths");

            mediasource_test(function(test, mediaElement, mediaSource)
            {
                mediaElement.pause();
                mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
                mediaElement.addEventListener("ended", test.step_func_done());

                MediaSourceUtil.fetchManifestAndData(test, subType + "/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr-manifest.json", function(type, data)
                {
                    var sourceBuffer = mediaSource.addSourceBuffer(type);
                    test.expectEvent(sourceBuffer, "update");
                    test.expectEvent(sourceBuffer, "updateend");
                    sourceBuffer.appendBuffer(data);

                    test.waitForExpectedEvents(function()
                    {
                        var expectationsAV = {
                            webm: ["{ [0.003, 2.004) }", "{ [0.003, 2.023) }"],
                            mp4: ["{ [0.067, 2.043) }", "{ [0.067, 2.067) }"],
                        };

                        var expectedBeforeEndOfStream = expectationsAV[subType][0];
                        var expectedAfterEndOfStream = expectationsAV[subType][1];

                        assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectedBeforeEndOfStream, "mediaSource.activeSourceBuffers[0]");
                        assertBufferedEquals(mediaElement, expectedBeforeEndOfStream, "mediaElement.buffered");

                        mediaSource.endOfStream();

                        assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectedAfterEndOfStream, "mediaSource.activeSourceBuffers[0]");
                        assertBufferedEquals(mediaElement, expectedAfterEndOfStream, "mediaElement.buffered");

                        test.done();
                    });
                });
            }, "Muxed content with different lengths");

            mediaSourceDemuxedTest(function(test, mediaElement, mediaSource, dataA, dataB) {
                var dataBSize = {
                    webm: 318,
                    mp4: 835,
                };
                test.expectEvent(mediaElement, "loadedmetadata");
                appendData(test, mediaSource, dataA, dataB.subarray(0, dataBSize[subType]), function()
                {
                    assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
                    assertBufferedEquals(mediaSource.activeSourceBuffers[1], "{ }", "mediaSource.activeSourceBuffers[1]");
                    assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");

                    mediaSource.endOfStream();

                    assertBufferedEquals(mediaSource.activeSourceBuffers[0], expectationsA[subType], "mediaSource.activeSourceBuffers[0]");
                    assertBufferedEquals(mediaSource.activeSourceBuffers[1], "{ }", "mediaSource.activeSourceBuffers[1]");
                    assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");

                    test.done();
                });
            }, "Demuxed content with an empty buffered range on one SourceBuffer");

            mediasource_test(function(test, mediaElement, mediaSource)
            {
                mediaElement.pause();
                mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
                mediaElement.addEventListener("ended", test.step_func_done());

                MediaSourceUtil.fetchManifestAndData(test, subType + "/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr-manifest.json", function(type, data)
                {
                    var sourceBuffer = mediaSource.addSourceBuffer(type);
                    test.expectEvent(mediaElement, "loadedmetadata");
                    test.expectEvent(sourceBuffer, "update");
                    test.expectEvent(sourceBuffer, "updateend");
                    sourceBuffer.appendBuffer(data.subarray(0, 4052));

                    test.waitForExpectedEvents(function()
                    {
                        assertBufferedEquals(mediaSource.activeSourceBuffers[0], "{ }", "mediaSource.activeSourceBuffers[0]");
                        assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");

                        mediaSource.endOfStream();

                        assertBufferedEquals(mediaSource.activeSourceBuffers[0], "{ }", "mediaSource.activeSourceBuffers[0]");
                        assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");

                        test.done();
                    });
                });
            }, "Muxed content empty buffered ranges.");

            mediasource_test(function(test, mediaElement, mediaSource)
            {
                mediaElement.pause();
                mediaElement.addEventListener("error", test.unreached_func("Unexpected event 'error'"));
                mediaElement.addEventListener("ended", test.step_func_done());

                var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_ONLY_TYPE);

                assertBufferedEquals(mediaSource.sourceBuffers[0], "{ }", "mediaSource.sourceBuffers[0]");
                assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");
                test.done();

            }, "Get buffered range when sourcebuffer is empty.");

            mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
            {
                var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);

                test.expectEvent(mediaElement, "loadedmetadata");
                test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
                sourceBuffer.appendBuffer(initSegment);
                test.waitForExpectedEvents(function()
                {
                    assertBufferedEquals(mediaSource.sourceBuffers[0], "{ }", "mediaSource.sourceBuffers[0]");
                    assertBufferedEquals(mediaSource.activeSourceBuffers[0], "{ }", "mediaSource.activeSourceBuffers[0]");
                    assertBufferedEquals(mediaElement, "{ }", "mediaElement.buffered");
                    test.done();
                });

            }, "Get buffered range when only init segment is appended.");

            mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
            {
                test.expectEvent(mediaSource.sourceBuffers, "removesourcebuffer", "SourceBuffer removed.");
                mediaSource.removeSourceBuffer(sourceBuffer);

                test.waitForExpectedEvents(function()
                {
                    assert_throws("InvalidStateError",
                        function() { sourceBuffer.buffered; },
                        "get sourceBuffer.buffered throws an exception for InvalidStateError.");
                    test.done();
                });
            }, "Get buffered range after removing sourcebuffer.");
        </script>
    </body>
</html>