summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-errors.html
blob: 3df1dedcc2ba0826eff0b2266a90aed1ac8794d8 (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
234
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="mediasource-util.js"></script>
<script>
    function ErrorTest(testFunction, description)
    {
        mediasource_test(function(test, mediaElement, mediaSource)
        {
            var segmentInfo = MediaSourceUtil.SEGMENT_INFO;

            if (!segmentInfo) {
                assert_unreached("No segment info compatible with this MediaSource implementation.");
                return;
            }

            var sourceBuffer = mediaSource.addSourceBuffer(segmentInfo.type);
            MediaSourceUtil.loadBinaryData(test, segmentInfo.url, function(mediaData)
            {
                testFunction(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData);
            });
        }, description);
    }

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

        test.expectEvent(sourceBuffer, "error", "sourceBuffer error.");
        test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
        test.expectEvent(mediaElement, "error", "mediaElement error.");
        test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
        test.expectEvent(mediaSource, "sourceclose", "mediaSource closed.");
        sourceBuffer.appendBuffer(mediaSegment);

        test.waitForExpectedEvents(function()
        {
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED);

            assert_equals(mediaSource.sourceBuffers.length, 0);
            assert_equals(mediaSource.readyState, "closed");
            test.done();
        });
    }, "Appending media segment before the first initialization segment.");

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);

        // Fail if the append error algorithm occurs, since the decode
        // error will be provided by us directly via endOfStream().
        sourceBuffer.addEventListener("error", test.unreached_func("'error' should not be fired on sourceBuffer"));

        test.expectEvent(mediaElement, "error", "mediaElement error.");
        test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
        test.expectEvent(mediaSource, "sourceclose", "mediaSource closed.");

        mediaSource.endOfStream("decode");

        test.waitForExpectedEvents(function()
        {
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED);

            assert_equals(mediaSource.sourceBuffers.length, 0);
            assert_equals(mediaSource.readyState, "closed");

            // Give a short time for a broken implementation to errantly fire
            // "error" on sourceBuffer.
            test.step_timeout(test.step_func_done(), 0);
        });
    }, "Signaling 'decode' error via endOfStream() before initialization segment has been appended.");

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);

        // Fail if the append error algorithm occurs, since the network
        // error will be provided by us directly via endOfStream().
        sourceBuffer.addEventListener("error", test.unreached_func("'error' should not be fired on sourceBuffer"));

        test.expectEvent(mediaElement, "error", "mediaElement error.");
        test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
        test.expectEvent(mediaSource, "sourceclose", "mediaSource closed.");

        mediaSource.endOfStream("network");

        test.waitForExpectedEvents(function()
        {
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED);

            assert_equals(mediaSource.sourceBuffers.length, 0);
            assert_equals(mediaSource.readyState, "closed");

            // Give a short time for a broken implementation to errantly fire
            // "error" on sourceBuffer.
            test.step_timeout(test.step_func_done(), 0);
        });
    }, "Signaling 'network' error via endOfStream() before initialization segment has been appended.");

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
        assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);

        // Fail if the append error algorithm occurs, since the decode
        // error will be provided by us directly via endOfStream().
        sourceBuffer.addEventListener("error", test.unreached_func("'error' should not be fired on sourceBuffer"));

        test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
        test.expectEvent(mediaElement, "loadedmetadata", "mediaElement metadata.");
        sourceBuffer.appendBuffer(initSegment);

        test.waitForExpectedEvents(function()
        {
            assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_METADATA);

            test.expectEvent(mediaElement, "error", "mediaElement error.");
            test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
            mediaSource.endOfStream("decode");
        });

        test.waitForExpectedEvents(function()
        {
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_DECODE);
            assert_equals(mediaSource.readyState, "ended");

            // Give a short time for a broken implementation to errantly fire
            // "error" on sourceBuffer.
            test.step_timeout(test.step_func_done(), 0);
        });

    }, "Signaling 'decode' error via endOfStream() after initialization segment has been appended and the HTMLMediaElement has reached HAVE_METADATA.");

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
        assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);

        // Fail if the append error algorithm occurs, since the network
        // error will be provided by us directly via endOfStream().
        sourceBuffer.addEventListener("error", test.unreached_func("'error' should not be fired on sourceBuffer"));

        test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
        test.expectEvent(mediaElement, "loadedmetadata", "mediaElement metadata.");
        sourceBuffer.appendBuffer(initSegment);

        test.waitForExpectedEvents(function()
        {
            assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_METADATA);
            test.expectEvent(mediaElement, "error", "mediaElement error.");
            test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
            mediaSource.endOfStream("network");
        });

        test.waitForExpectedEvents(function()
        {
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_NETWORK);
            assert_equals(mediaSource.readyState, "ended");

            // Give a short time for a broken implementation to errantly fire
            // "error" on sourceBuffer.
            test.step_timeout(test.step_func_done(), 0);
        });
    }, "Signaling 'network' error via endOfStream() after initialization segment has been appended and the HTMLMediaElement has reached HAVE_METADATA.");

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);

        var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
        test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
        test.expectEvent(mediaElement, "loadedmetadata", "mediaElement metadata.");
        sourceBuffer.appendBuffer(initSegment);

        test.waitForExpectedEvents(function()
        {
            assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_METADATA);
            var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);
            var index = segmentInfo.init.size + (mediaSegment.length - 1) / 2;
            // Corrupt the media data from index of mediaData, so it can signal 'decode' error.
            // Here use mediaSegment to replace the original mediaData[index, index + mediaSegment.length]
            mediaData.set(mediaSegment, index);

            test.expectEvent(sourceBuffer, "error", "sourceBuffer error.");
            test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
            test.expectEvent(mediaElement, "error", "mediaElement error.");
            test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
            sourceBuffer.appendBuffer(mediaData);
        });

        test.waitForExpectedEvents(function()
        {
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_DECODE);
            test.done();
        });
    }, "Signaling 'decode' error via segment parser loop algorithm after initialization segment has been appended.");

    ErrorTest(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
    {
        assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);

        // Fail if the append error algorithm occurs, since the network
        // error will be provided by us directly via endOfStream().
        mediaElement.addEventListener("loadedmetadata", test.unreached_func("'loadedmetadata' should not be fired on mediaElement"));

        var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);
        var index = segmentInfo.init.size + (mediaSegment.length - 1) / 2;
        // Corrupt the media data from index of mediaData, so it can signal 'decode' error.
        // Here use mediaSegment to replace the original mediaData[index, index + mediaSegment.length]
        mediaData.set(mediaSegment, index);

        test.expectEvent(sourceBuffer, "error", "sourceBuffer error.");
        test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
        test.expectEvent(mediaElement, "error", "mediaElement error.");
        test.expectEvent(mediaSource, "sourceended", "mediaSource ended.");
        sourceBuffer.appendBuffer(mediaData);

        test.waitForExpectedEvents(function()
        {
            assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_NOTHING);
            assert_true(mediaElement.error != null);
            assert_equals(mediaElement.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED);
            test.done();
        });
    }, "Signaling 'decode' error via segment parser loop algorithm.");
</script>