2 <!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
4 <script src=
"/resources/testharness.js"></script>
5 <script src=
"/resources/testharnessreport.js"></script>
6 <script src=
"mediasource-util.js"></script>
8 function ErrorTest(testFunction
, description
)
10 mediasource_test(function(test
, mediaElement
, mediaSource
)
12 var segmentInfo
= MediaSourceUtil
.SEGMENT_INFO
;
15 assert_unreached("No segment info compatible with this MediaSource implementation.");
19 var sourceBuffer
= mediaSource
.addSourceBuffer(segmentInfo
.type
);
20 MediaSourceUtil
.loadBinaryData(test
, segmentInfo
.url
, function(mediaData
)
22 testFunction(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
);
27 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
29 var mediaSegment
= MediaSourceUtil
.extractSegmentData(mediaData
, segmentInfo
.media
[0]);
31 test
.expectEvent(sourceBuffer
, "error", "sourceBuffer error.");
32 test
.expectEvent(sourceBuffer
, "updateend", "mediaSegment append ended.");
33 test
.expectEvent(mediaElement
, "error", "mediaElement error.");
34 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
35 test
.expectEvent(mediaSource
, "sourceclose", "mediaSource closed.");
36 sourceBuffer
.appendBuffer(mediaSegment
);
38 test
.waitForExpectedEvents(function()
40 assert_true(mediaElement
.error
!= null);
41 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_SRC_NOT_SUPPORTED
);
43 assert_equals(mediaSource
.sourceBuffers
.length
, 0);
44 assert_equals(mediaSource
.readyState
, "closed");
47 }, "Appending media segment before the first initialization segment.");
49 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
51 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
53 // Fail if the append error algorithm occurs, since the decode
54 // error will be provided by us directly via endOfStream().
55 sourceBuffer
.addEventListener("error", test
.unreached_func("'error' should not be fired on sourceBuffer"));
57 test
.expectEvent(mediaElement
, "error", "mediaElement error.");
58 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
59 test
.expectEvent(mediaSource
, "sourceclose", "mediaSource closed.");
61 mediaSource
.endOfStream("decode");
63 test
.waitForExpectedEvents(function()
65 assert_true(mediaElement
.error
!= null);
66 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_SRC_NOT_SUPPORTED
);
68 assert_equals(mediaSource
.sourceBuffers
.length
, 0);
69 assert_equals(mediaSource
.readyState
, "closed");
71 // Give a short time for a broken implementation to errantly fire
72 // "error" on sourceBuffer.
73 test
.step_timeout(test
.step_func_done(), 0);
75 }, "Signaling 'decode' error via endOfStream() before initialization segment has been appended.");
77 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
79 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
81 // Fail if the append error algorithm occurs, since the network
82 // error will be provided by us directly via endOfStream().
83 sourceBuffer
.addEventListener("error", test
.unreached_func("'error' should not be fired on sourceBuffer"));
85 test
.expectEvent(mediaElement
, "error", "mediaElement error.");
86 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
87 test
.expectEvent(mediaSource
, "sourceclose", "mediaSource closed.");
89 mediaSource
.endOfStream("network");
91 test
.waitForExpectedEvents(function()
93 assert_true(mediaElement
.error
!= null);
94 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_SRC_NOT_SUPPORTED
);
96 assert_equals(mediaSource
.sourceBuffers
.length
, 0);
97 assert_equals(mediaSource
.readyState
, "closed");
99 // Give a short time for a broken implementation to errantly fire
100 // "error" on sourceBuffer.
101 test
.step_timeout(test
.step_func_done(), 0);
103 }, "Signaling 'network' error via endOfStream() before initialization segment has been appended.");
105 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
107 var initSegment
= MediaSourceUtil
.extractSegmentData(mediaData
, segmentInfo
.init
);
108 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
110 // Fail if the append error algorithm occurs, since the decode
111 // error will be provided by us directly via endOfStream().
112 sourceBuffer
.addEventListener("error", test
.unreached_func("'error' should not be fired on sourceBuffer"));
114 test
.expectEvent(sourceBuffer
, "updateend", "mediaSegment append ended.");
115 test
.expectEvent(mediaElement
, "loadedmetadata", "mediaElement metadata.");
116 sourceBuffer
.appendBuffer(initSegment
);
118 test
.waitForExpectedEvents(function()
120 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_METADATA
);
122 test
.expectEvent(mediaElement
, "error", "mediaElement error.");
123 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
124 mediaSource
.endOfStream("decode");
127 test
.waitForExpectedEvents(function()
129 assert_true(mediaElement
.error
!= null);
130 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_DECODE
);
131 assert_equals(mediaSource
.readyState
, "ended");
133 // Give a short time for a broken implementation to errantly fire
134 // "error" on sourceBuffer.
135 test
.step_timeout(test
.step_func_done(), 0);
138 }, "Signaling 'decode' error via endOfStream() after initialization segment has been appended and the HTMLMediaElement has reached HAVE_METADATA.");
140 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
142 var initSegment
= MediaSourceUtil
.extractSegmentData(mediaData
, segmentInfo
.init
);
143 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
145 // Fail if the append error algorithm occurs, since the network
146 // error will be provided by us directly via endOfStream().
147 sourceBuffer
.addEventListener("error", test
.unreached_func("'error' should not be fired on sourceBuffer"));
149 test
.expectEvent(sourceBuffer
, "updateend", "mediaSegment append ended.");
150 test
.expectEvent(mediaElement
, "loadedmetadata", "mediaElement metadata.");
151 sourceBuffer
.appendBuffer(initSegment
);
153 test
.waitForExpectedEvents(function()
155 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_METADATA
);
156 test
.expectEvent(mediaElement
, "error", "mediaElement error.");
157 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
158 mediaSource
.endOfStream("network");
161 test
.waitForExpectedEvents(function()
163 assert_true(mediaElement
.error
!= null);
164 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_NETWORK
);
165 assert_equals(mediaSource
.readyState
, "ended");
167 // Give a short time for a broken implementation to errantly fire
168 // "error" on sourceBuffer.
169 test
.step_timeout(test
.step_func_done(), 0);
171 }, "Signaling 'network' error via endOfStream() after initialization segment has been appended and the HTMLMediaElement has reached HAVE_METADATA.");
173 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
175 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
177 var initSegment
= MediaSourceUtil
.extractSegmentData(mediaData
, segmentInfo
.init
);
178 test
.expectEvent(sourceBuffer
, "updateend", "mediaSegment append ended.");
179 test
.expectEvent(mediaElement
, "loadedmetadata", "mediaElement metadata.");
180 sourceBuffer
.appendBuffer(initSegment
);
182 test
.waitForExpectedEvents(function()
184 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_METADATA
);
185 var mediaSegment
= MediaSourceUtil
.extractSegmentData(mediaData
, segmentInfo
.media
[0]);
186 var index
= segmentInfo
.init
.size
+ (mediaSegment
.length
- 1) / 2;
187 // Corrupt the media data from index of mediaData, so it can signal 'decode' error.
188 // Here use mediaSegment to replace the original mediaData[index, index + mediaSegment.length]
189 mediaData
.set(mediaSegment
, index
);
191 test
.expectEvent(sourceBuffer
, "error", "sourceBuffer error.");
192 test
.expectEvent(sourceBuffer
, "updateend", "mediaSegment append ended.");
193 test
.expectEvent(mediaElement
, "error", "mediaElement error.");
194 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
195 sourceBuffer
.appendBuffer(mediaData
);
198 test
.waitForExpectedEvents(function()
200 assert_true(mediaElement
.error
!= null);
201 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_DECODE
);
204 }, "Signaling 'decode' error via segment parser loop algorithm after initialization segment has been appended.");
206 ErrorTest(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
208 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
210 var mediaSegment
= MediaSourceUtil
.extractSegmentData(mediaData
, segmentInfo
.media
[0]);
211 var index
= segmentInfo
.init
.size
+ (mediaSegment
.length
- 1) / 2;
212 // Corrupt the media data from index of mediaData, so it can signal 'decode' error.
213 // Here use mediaSegment to replace the original mediaData[index, index + mediaSegment.length]
214 mediaData
.set(mediaSegment
, index
);
216 // Depending on implementation, mediaElement transition to
217 // HAVE_METADATA and dispatching 'loadedmetadata' may occur, since the
218 // initialization segment is uncorrupted and forms the initial part of
219 // the appended bytes. The segment parser loop continues and
220 // eventually should observe decode error. Other implementations may
221 // delay such transition until some larger portion of the append's
222 // parsing is completed or until the media element is configured to
223 // handle the playback of media with the associated metadata (which may
224 // not occur in this case before the MSE append error algorithm executes.)
225 // So we cannot reliably expect the lack or presence of
226 // 'loadedmetadata' before the MSE append error algortihm executes in
227 // this case; similarly, mediaElement's resulting readyState may be
228 // either HAVE_NOTHING or HAVE_METADATA after the append error
229 // algorithm executes, and the resulting MediaError code would
230 // respectively be MEDIA_ERR_SRC_NOT_SUPPORTED or MEDIA_ERR_DECODE.
232 mediaElement
.addEventListener("loadedmetadata", test
.step_func(() => { loaded
= true; }));
234 mediaElement
.addEventListener("error", test
.step_func(() => { errored
= true; }));
236 test
.expectEvent(sourceBuffer
, "error", "sourceBuffer error.");
237 test
.expectEvent(sourceBuffer
, "updateend", "mediaSegment append ended.");
238 test
.expectEvent(mediaSource
, "sourceended", "mediaSource ended.");
239 sourceBuffer
.appendBuffer(mediaData
);
241 let verifyFinalState
= test
.step_func(function() {
243 assert_greater_than(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
244 assert_true(mediaElement
.error
!= null);
245 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_DECODE
);
248 assert_equals(mediaElement
.readyState
, HTMLMediaElement
.HAVE_NOTHING
);
249 assert_true(mediaElement
.error
!= null);
250 assert_equals(mediaElement
.error
.code
, MediaError
.MEDIA_ERR_SRC_NOT_SUPPORTED
);
255 let awaitMediaElementError
= test
.step_func(function() {
257 test
.step_timeout(awaitMediaElementError
, 100);
263 test
.waitForExpectedEvents(function()
265 // Not all implementations will reliably fire a "loadedmetadata"
266 // event, so we use custom logic to verify mediaElement state based
267 // on whether or not "loadedmetadata" was ever fired. But first
268 // we must ensure "error" was fired on the mediaElement.
269 awaitMediaElementError();
272 }, "Signaling 'decode' error via segment parser loop algorithm of append containing init plus corrupted media segment.");