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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
<!DOCTYPE html>
<html>
<head>
<title>Test sequence of effects of errors
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/media-source/mediasource-util.js"></script>
</head>
<body>
</body>
<script>
'use strict';
function create_audio(t) {
const audio = document.createElement('audio');
audio.controls = true;
audio.watcher = new EventWatcher(
t, audio,
[
'loadstart',
'waiting',
'error',
'ended',
'loadedmetadata',
'canplay',
'volumechange',
'playing',
'pause',
]);
document.body.appendChild(audio);
return audio;
}
promise_test(async t => {
const audio = create_audio(t);
audio.src = '';
assert_equals(audio.error, null, 'initial error attribute');
// Queue a volumechange event on the media element task source.
audio.volume = 0;
// The dedicated media source failure steps are described as queued, but
// browsers do not make state changes asynchronously.
// https://github.com/whatwg/html/issues/11155
audio.onvolumechange = t.step_func(() => {
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
'error code');
// Queue a second volumechange. This arrives after the error event
// because the error event is queued immediately after the resource
// selection algorithm synchronous steps.
audio.volume = 1;
});
await audio.watcher.wait_for(
['volumechange', 'loadstart', 'error', 'volumechange']);
}, 'empty src attribute');
promise_test(async t => {
const audio = create_audio(t);
// src is such that "the result of encoding-parsing a URL" is failure.
audio.src = 'https://#fragment';
assert_equals(audio.error, null, 'initial error attribute');
// Queue a volumechange event on the media element task source.
audio.volume = 0;
// The dedicated media source failure steps are described as queued from
// parallel steps in the resource selection algorithm, but browsers do not
// make state changes asynchronously, and they queue the error event
// immediately after the resource selection algorithm synchronous steps.
// https://github.com/whatwg/html/issues/11155
audio.onvolumechange = t.step_func(() => {
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
'error code');
audio.volume = 1;
});
await audio.watcher.wait_for(
['volumechange', 'loadstart', 'error', 'volumechange']);
}, 'urlRecord failure');
let resource;
promise_test(async t => {
resource = await MediaSourceUtil.fetchResourceOfManifest(
t,
'/media-source/webm/test-a-128k-44100Hz-1ch-manifest.json');
}, 'fetch resource');
async function create_audio_with_source_buffer(t) {
const audio = create_audio(t);
audio.source = new MediaSource();
audio.source.watcher = new EventWatcher(t, audio.source, ['sourceopen']);
audio.src = URL.createObjectURL(audio.source);
await audio.watcher.wait_for('loadstart');
await audio.source.watcher.wait_for('sourceopen');
assert_implements_optional(MediaSource.isTypeSupported(resource.type),
`${resource.type} supported`);
audio.buffer = audio.source.addSourceBuffer(resource.type);
assert_equals(audio.buffer.mode, 'segments',
`${resource.type} buffer.mode`);
audio.buffer.watcher =
new EventWatcher(t, audio.buffer, ['updateend']);
return audio;
}
// While different browsers pass different HAVE_NOTHING subtests, the four
// subtests are helpful to identify the different interactions.
promise_test(async t => {
const audio = await create_audio_with_source_buffer(t);
assert_equals(audio.readyState, audio.HAVE_NOTHING, 'readyState');
// Queue a volumechange event on the media element task source to check that
// the event named 'error' is fired from the same task source.
audio.volume = 0;
audio.source.endOfStream("decode");
audio.volume = 1;
await audio.watcher.wait_for(['volumechange', 'error', 'volumechange']);
}, 'error event while HAVE_NOTHING');
// This subtest is arranged to demonstrate that the specification does not
// describe what browsers do. Please do not adjust implementations to make
// this pass as https://github.com/whatwg/html/issues/11155 proposes changing
// the spec.
promise_test(async t => {
const audio = await create_audio_with_source_buffer(t);
assert_equals(audio.readyState, audio.HAVE_NOTHING, 'readyState');
// Queue a volumechange event on the media element task source
audio.volume = 0;
audio.source.endOfStream("decode");
// The dedicated media source failure steps are described as queued so state
// would not change until the task runs.
await audio.watcher.wait_for('volumechange');
assert_equals(audio.error, null, 'error attribute');
await audio.watcher.wait_for('error');
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
'error code');
}, 'error attribute while HAVE_NOTHING');
// This subtest is arranged to demonstrate that the specification does not
// describe what browsers do. Please do not adjust implementations to make
// this pass as https://github.com/whatwg/html/issues/11155 proposes changing
// the spec.
promise_test(async t => {
const audio = await create_audio_with_source_buffer(t);
assert_equals(audio.readyState, audio.HAVE_NOTHING, 'readyState');
const play_promise = audio.play();
await audio.watcher.wait_for('waiting');
assert_false(audio.paused, 'paused attribute');
// 'error event while HAVE_NOTHING' checks the order of events.
audio.watcher.stop_watching();
// Queue a volumechange event on the media element task source to see
// whether the play promise is rejected from a task on same task source.
audio.volume = 0;
audio.source.endOfStream("decode");
audio.volume = 1;
const sequence = [];
const events_promise = new Promise(resolve => {
audio.onvolumechange = t.step_func(() => {
sequence.push('volumechange');
if (sequence.filter(_ => _ == 'volumechange').length == 2) {
resolve();
}
});
});
try {
await play_promise;
assert_unreached('promise should reject');
} catch {
sequence.push('rejection');
}
await events_promise;
assert_array_equals(sequence, ['volumechange', 'rejection', 'volumechange'],
'sequence');
}, 'play() promise while HAVE_NOTHING');
// This subtest is arranged to demonstrate inconsistencies between
// implementations. Please do not adjust implementations to make this pass as
// https://github.com/whatwg/html/issues/11155 proposes changing the spec.
promise_test(async t => {
const audio = await create_audio_with_source_buffer(t);
assert_equals(audio.readyState, audio.HAVE_NOTHING, 'readyState');
const play_promise = audio.play();
await audio.watcher.wait_for('waiting');
assert_false(audio.paused, 'paused attribute');
// 'error event while HAVE_NOTHING' checks the order of events.
audio.watcher.stop_watching();
// The resource selection algorithm describes the dedicated media source
// failure steps as queued and the event named "error" as fired
// synchronously from those steps.
// https://html.spec.whatwg.org/multipage/media.html#concept-media-load-algorithm
// That is not what browsers do, but, as described, the error event would
// arrive before the pending play promise rejection.
audio.source.endOfStream("decode");
const sequence = [];
const event_promise = new Promise(resolve => {
audio.onerror = t.step_func(() => {
sequence.push('event');
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
'error code on event');
resolve();
});
});
try {
await play_promise;
assert_unreached('promise should reject');
} catch {
sequence.push('rejection');
}
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
'error code on rejection');
await event_promise;
assert_array_equals(sequence, ['event', 'rejection'], 'sequence');
}, 'play() promise after error event while HAVE_NOTHING');
promise_test(async t => {
const audio = await create_audio_with_source_buffer(t);
// Truncate at the end of the metadata.
audio.buffer.appendBuffer(
resource.data.subarray(0, resource.cluster_start[0]));
await Promise.all([
audio.watcher.wait_for('loadedmetadata'),
audio.buffer.watcher.wait_for('updateend'),
]);
assert_equals(audio.readyState, audio.HAVE_METADATA, 'loadedmetadata');
const play_promise1 = audio.play();
await audio.watcher.wait_for('waiting');
assert_false(audio.paused, 'paused attribute');
let settled = 'NOT SETTLED';
play_promise1.catch(_ => _).then(_ => settled = _);
assert_equals(audio.error, null, 'error attribute');
// Trigger "If the media data is corrupted" in the media data processing
// steps list.
// https://html.spec.whatwg.org/multipage/media.html#media-data-processing-steps-list
audio.source.endOfStream("decode");
// The error event is described as firing synchronously during endOfStream(),
// but no browsers do this. https://github.com/whatwg/html/issues/11155
await audio.watcher.wait_for('error');
// The error attribute should be set synchronously, but this checked late
// just for Blink.
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_DECODE, 'error code');
// The end of stream algorithm does not change duration on error
// https://w3c.github.io/media-source/#dfn-end-of-stream
assert_equals(audio.duration, 2.023, 'duration');
// MEDIA_ERR_DECODE does not reject the pending play promise
// https://github.com/whatwg/html/issues/505#issuecomment-178046408
// because playback is sometimes possible after such errors,
// https://github.com/whatwg/html/pull/509#issuecomment-174967812
// as in the 'error after HAVE_FUTURE_DATA' subtest below.
// Trigger volumechange for media element task source tasks.
// Await 2 tasks to check that the play() promise is not about to be
// rejected.
// 2 is the number of tasks necessary to wait for the spurious promise
// rejection with Blink.
for (const i of Array(2).keys()) {
audio.volume = i % 2;
await audio.watcher.wait_for('volumechange');
}
assert_equals(settled, 'NOT SETTLED', 'play(() promise should not settle');
// Check that the promise is rejected when appropriate.
audio.pause();
const sequence = [];
const play_promise2 = new Promise(resolve => {
audio.onpause = () => {
sequence.push('pause');
if (sequence.filter(_ => _ == 'pause').length == 2) {
audio.onpause = null;
return;
}
resolve(audio.play());
audio.pause();
}
});
audio.onwaiting = () => {
sequence.push('waiting');
}
assert_true(audio.paused, 'paused attribute');
await Promise.all([
audio.watcher.wait_for(['pause', 'waiting', 'pause']),
play_promise1.catch(() => sequence.push('promise1')),
play_promise2.catch(() => sequence.push('promise2')),
]);
assert_array_equals(sequence,
['pause', 'promise1', 'waiting', 'pause', 'promise2'],
'sequence');
promise_rejects_dom(
t, 'AbortError', play_promise1, 'play promise rejection');
}, 'error event while HAVE_METADATA');
async function create_audio_with_full_resource(t) {
const audio = await create_audio_with_source_buffer(t);
// Just to reduce sound impacts
audio.volume = 0;
audio.buffer.appendBuffer(resource.data);
await Promise.all([
audio.watcher.wait_for(['volumechange', 'loadedmetadata', 'canplay']),
audio.buffer.watcher.wait_for('updateend'),
]);
assert_greater_than(audio.readyState, audio.HAVE_CURRENT_DATA,
'readyState');
assert_equals(audio.error, null, 'error attribute');
return audio;
}
promise_test(async t => {
const audio = await create_audio_with_full_resource(t);
audio.source.endOfStream("decode");
// The error event is specified to fire synchronously during endOfStream(),
// but no browsers do this. https://github.com/whatwg/html/issues/11155
await audio.watcher.wait_for('error');
// The error attribute should be set synchronously, but this is checked late
// just for Blink. It is checked synchronously in the 'error attribute
// after DECODE_ERROR' subtest below.
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_DECODE, 'error code');
const sequence = [];
const play_promise1 = audio.play().then(() => sequence.push('promise1'));
const play_promise2 = new Promise(resolve => {
audio.onplaying = () => {
sequence.push('event');
resolve(audio.play().then(() => sequence.push('promise2')));
};
});
assert_false(audio.paused, 'paused attribute');
await Promise.all([
audio.watcher.wait_for('playing'),
play_promise1,
play_promise2,
]);
assert_array_equals(sequence, ['event', 'promise1', 'promise2'], 'sequence');
}, 'error event after HAVE_FUTURE_DATA');
// This subtest could be merged into 'error event after HAVE_FUTURE_DATA' and
// 'error event while HAVE_METADATA' if/when Blink conforms with the synchronous
// attribute change.
promise_test(async t => {
const audio = await create_audio_with_full_resource(t);
audio.source.endOfStream("decode");
// The error attribute is set synchronously.
assert_equals(audio.error?.code, MediaError.MEDIA_ERR_DECODE, 'error code');
}, 'error attribute after DECODE_ERROR');
</script>
</html>
|