Nacre/tests/smoke/pages/mse-spec.html

304 lines
13 KiB
HTML

<!doctype html>
<html>
<!-- MSE spec cases (S027), run one after another on mse-clip-mp4.js (fMP4 H.264 + AAC, 5 s) and, for changeType,
mse-clip-webm.js. window.spec.results maps each case to true or to the reason it failed; window.spec.done
turns true at the end. Cases: attach (event order, revoke right after src), duration (setter, durationchange,
below-buffered throws), ranges (video.buffered/seekable from the MediaSource, activeSourceBuffers),
remove (remove + re-append), removeSourceBuffer (then a new buffer), changeType, abort mid-append, ended then
append reopens, sequence mode, detach (load() closes the source), leak (drops a MediaSource and churns the GC; the
collection itself is checked in the browser log by tests/smoke/mse-leak-check.sh). -->
<head><meta charset="utf-8"><title>MSE spec</title>
<style>
body { margin: 0; background: #1d2b3b; color: #fff; font: 18px sans-serif; }
video { display: block; margin: 12px auto; width: 480px; height: 270px; background: #000; }
#log { font: 14px monospace; white-space: pre; width: 900px; margin: 0 auto; }
</style></head>
<body>
<video id="v" muted></video>
<div id="log"></div>
<script>
const MP4 = 'video/mp4; codecs="avc1.4d401f, mp4a.40.2"';
const WEBM = 'video/webm; codecs="vp9, opus"';
const spec = window.spec = { results: {}, done: false, events: [] };
const log = (line) => { document.getElementById('log').textContent += line + '\n'; };
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
function loadClip(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = url;
script.onload = () => resolve(Uint8Array.from(atob(window.MSE_CLIP), (c) => c.charCodeAt(0)).buffer);
script.onerror = () => reject('load ' + url);
document.head.appendChild(script);
});
}
// Resolves with the event's name when `target` fires one of `names`, or 'timeout'.
function next(target, names, ms = 10000) {
return new Promise((resolve) => {
const done = (e) => { names.forEach((n) => target.removeEventListener(n, done)); clearTimeout(t); resolve(e.type); };
const t = setTimeout(() => { names.forEach((n) => target.removeEventListener(n, done)); resolve('timeout'); }, ms);
names.forEach((n) => target.addEventListener(n, done));
});
}
function check(cond, what) { if (!cond) throw new Error(what); }
function throwsName(fn) { try { fn(); return 'none'; } catch (e) { return e.name; } }
const end = (ranges) => (ranges.length ? ranges.end(ranges.length - 1) : 0);
const start = (ranges) => (ranges.length ? ranges.start(0) : -1);
// Records every event of these names on target into spec.events, as "<prefix>:<type>".
function record(target, prefix, names) {
names.forEach((n) => target.addEventListener(n, () => spec.events.push(prefix + ':' + n)));
}
async function append(sb, data) {
sb.appendBuffer(data);
const ev = await next(sb, ['updateend']);
check(ev === 'updateend', 'append: ' + ev);
}
// A fresh MediaSource attached to the page's video; resolves once open.
async function open(video, revoke) {
const ms = new MediaSource();
const opened = next(ms, ['sourceopen']);
video.src = URL.createObjectURL(ms);
if (revoke) URL.revokeObjectURL(video.src);
check(await opened === 'sourceopen', 'no sourceopen');
return ms;
}
function detach(video) {
video.removeAttribute('src');
video.load();
}
const cases = {
async attach(v, mp4) {
const ms = new MediaSource();
record(ms, 'ms', ['sourceopen', 'sourceended', 'sourceclose']);
check(ms.readyState === 'closed', 'initial ' + ms.readyState);
check(isNaN(ms.duration), 'closed duration ' + ms.duration);
const opened = next(ms, ['sourceopen']);
v.src = URL.createObjectURL(ms);
URL.revokeObjectURL(v.src); // as many players do; attaching must still work
check(ms.readyState === 'closed', 'open before attach');
check(await opened === 'sourceopen', 'no sourceopen');
check(ms.readyState === 'open', 'after sourceopen ' + ms.readyState);
check(throwsName(() => ms.addSourceBuffer('')) === 'TypeError', 'empty type');
check(throwsName(() => ms.addSourceBuffer('video/x-nothing')) === 'NotSupportedError', 'bad type');
const sb = ms.addSourceBuffer(MP4);
check(ms.sourceBuffers.length === 1 && ms.sourceBuffers[0] === sb, 'sourceBuffers');
const order = [];
['updatestart', 'update', 'updateend'].forEach((n) => sb.addEventListener(n, () => order.push(n)));
sb.appendBuffer(mp4);
check(sb.updating, 'not updating after appendBuffer');
check(throwsName(() => sb.appendBuffer(mp4)) === 'InvalidStateError', 'second append while updating');
await next(sb, ['updateend']);
check(order.join() === 'updatestart,update,updateend', 'order ' + order.join());
detach(v);
await sleep(100);
check(spec.events.join() === 'ms:sourceopen,ms:sourceclose', 'events ' + spec.events.join());
},
async duration(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
await append(sb, mp4);
const buffered = end(sb.buffered);
check(buffered > 4, 'buffered ' + buffered);
const changed = next(v, ['durationchange']);
ms.duration = 100;
check(ms.duration === 100, 'ms.duration ' + ms.duration);
check(await changed === 'durationchange', 'no durationchange');
check(v.duration === 100, 'video.duration ' + v.duration);
check(throwsName(() => { ms.duration = 1; }) === 'InvalidStateError', 'below buffered');
check(throwsName(() => { ms.duration = -1; }) === 'TypeError', 'negative');
ms.endOfStream();
check(ms.readyState === 'ended', 'readyState ' + ms.readyState);
await next(ms, ['sourceended']);
await sleep(200);
check(Math.abs(ms.duration - buffered) < 0.1, 'duration after endOfStream ' + ms.duration + ' vs ' + buffered);
check(throwsName(() => { ms.duration = 50; }) === 'InvalidStateError', 'set while ended');
detach(v);
},
async ranges(v, mp4) {
const ms = await open(v);
const active = [];
ms.activeSourceBuffers.addEventListener('addsourcebuffer', () => active.push('add'));
const sb = ms.addSourceBuffer(MP4);
check(v.buffered.length === 0, 'buffered before append');
await append(sb, mp4);
await sleep(100);
check(ms.activeSourceBuffers.length === 1, 'activeSourceBuffers ' + ms.activeSourceBuffers.length);
check(active.join() === 'add', 'active events ' + active.join());
check(v.buffered.length >= 1, 'video.buffered empty');
check(Math.abs(end(v.buffered) - end(sb.buffered)) < 0.1, 'video.buffered ' + end(v.buffered));
check(end(v.seekable) === ms.duration, 'seekable ' + end(v.seekable) + ' duration ' + ms.duration);
ms.duration = Infinity;
check(Math.abs(end(v.seekable) - end(v.buffered)) < 0.01, 'live seekable ' + end(v.seekable));
ms.setLiveSeekableRange(0, 20);
check(end(v.seekable) === 20, 'live seekable range ' + end(v.seekable));
ms.clearLiveSeekableRange();
detach(v);
},
async remove(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
await append(sb, mp4);
const before = end(sb.buffered);
check(throwsName(() => sb.remove(3, 2)) === 'TypeError', 'end before start');
sb.remove(0, 3);
check(sb.updating, 'not updating during remove');
check(await next(sb, ['updateend']) === 'updateend', 'remove updateend');
check(start(sb.buffered) >= 1, 'buffered start after remove ' + start(sb.buffered));
check(end(sb.buffered) === before, 'buffered end after remove ' + end(sb.buffered));
await append(sb, mp4);
check(start(sb.buffered) < 0.1 && end(sb.buffered) === before, 're-append ' + start(sb.buffered));
detach(v);
},
async removeSourceBuffer(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
await append(sb, mp4);
const removed = next(ms.sourceBuffers, ['removesourcebuffer']);
ms.removeSourceBuffer(sb);
check(ms.sourceBuffers.length === 0, 'still listed');
check(await removed === 'removesourcebuffer', 'no removesourcebuffer');
check(throwsName(() => sb.appendBuffer(mp4)) === 'InvalidStateError', 'append to removed');
check(throwsName(() => ms.removeSourceBuffer(sb)) === 'NotFoundError', 'remove twice');
const sb2 = ms.addSourceBuffer(MP4);
await append(sb2, mp4);
check(end(sb2.buffered) > 4, 'new buffer ' + end(sb2.buffered));
detach(v);
},
async changeType(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
await append(sb, mp4);
check(throwsName(() => sb.changeType('')) === 'TypeError', 'empty');
check(throwsName(() => sb.changeType('video/x-nothing')) === 'NotSupportedError', 'unsupported');
check(throwsName(() => sb.changeType(MP4)) === 'none', 'same type');
// gst_mse can't switch containers (documented limit).
check(throwsName(() => sb.changeType(WEBM)) === 'NotSupportedError', 'other container');
sb.remove(0, Infinity);
await next(sb, ['updateend']);
await append(sb, mp4);
check(end(sb.buffered) > 4, 'append after changeType ' + end(sb.buffered));
detach(v);
},
async abort(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
const order = [];
['updatestart', 'update', 'updateend', 'abort'].forEach((n) => sb.addEventListener(n, () => order.push(n)));
sb.appendBuffer(mp4);
sb.abort();
check(!sb.updating, 'updating after abort');
check(sb.appendWindowEnd === Infinity, 'append window reset');
await sleep(500);
check(order.join() === 'updatestart,abort,updateend', 'order ' + order.join());
await append(sb, mp4);
check(end(sb.buffered) > 4, 'append after abort ' + end(sb.buffered));
detach(v);
},
async reopen(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
// The first two fragments (ftyp, moov, 2 x moof+mdat end at byte 43652): endOfStream() doesn't reset the
// parser, so a cut inside a box would make the next append continue that box (an append error).
await append(sb, mp4.slice(0, 43652));
ms.endOfStream();
check(ms.readyState === 'ended', 'not ended');
check(throwsName(() => ms.endOfStream()) === 'InvalidStateError', 'endOfStream twice');
await next(ms, ['sourceended']);
const reopened = next(ms, ['sourceopen']);
sb.appendBuffer(mp4);
check(ms.readyState === 'open', 'append did not reopen: ' + ms.readyState);
check(await reopened === 'sourceopen', 'no sourceopen');
await next(sb, ['updateend']);
check(end(sb.buffered) > 4, 'buffered ' + end(sb.buffered));
detach(v);
},
async sequence(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
check(sb.mode === 'segments', 'mode ' + sb.mode);
sb.mode = 'sequence';
check(sb.mode === 'sequence', 'mode ' + sb.mode);
await append(sb, mp4);
check(end(sb.buffered) > 4, 'buffered ' + end(sb.buffered));
sb.timestampOffset = 10;
check(sb.timestampOffset === 10, 'timestampOffset ' + sb.timestampOffset);
detach(v);
},
async detach(v, mp4) {
const ms = await open(v);
const sb = ms.addSourceBuffer(MP4);
await append(sb, mp4);
const closed = next(ms, ['sourceclose']);
const removed = next(ms.sourceBuffers, ['removesourcebuffer']);
detach(v);
check(ms.readyState === 'closed', 'readyState ' + ms.readyState);
check(isNaN(ms.duration), 'duration ' + ms.duration);
check(ms.sourceBuffers.length === 0 && ms.activeSourceBuffers.length === 0, 'lists not empty');
check(await closed === 'sourceclose', 'no sourceclose');
check(await removed === 'removesourcebuffer', 'no removesourcebuffer');
check(throwsName(() => ms.addSourceBuffer(MP4)) === 'InvalidStateError', 'add while closed');
// A closed MediaSource can be attached again.
const opened = next(ms, ['sourceopen']);
v.src = URL.createObjectURL(ms);
check(await opened === 'sourceopen', 'no sourceopen on reattach');
detach(v);
},
// Drops an attached-then-detached MediaSource and churns the GC for 10 s. Servo runs neither
// FinalizationRegistry callbacks nor clears WeakRefs, so the page can't see the collection; the browser logs
// "MSE: MediaSource <id> created/freed" and tests/smoke/mse-leak-check.sh checks that this one (the
// second-to-last created) was freed before the sentinel (the last, kept alive) was created.
async leak(v, mp4) {
await (async () => {
const ms = await open(v, true);
const sb = ms.addSourceBuffer(MP4);
await append(sb, mp4);
detach(v);
})();
for (let i = 0; i < 40; i++) {
let junk = [];
for (let j = 0; j < 64; j++) junk.push(new ArrayBuffer(1 << 20));
junk = null;
await sleep(250);
}
window.sentinel = new MediaSource();
},
};
async function run() {
const v = document.getElementById('v');
if (!window.MediaSource) { spec.results.exists = 'no MediaSource'; spec.done = true; return; }
const mp4 = await loadClip('mse-clip-mp4.js');
for (const [name, fn] of Object.entries(cases)) {
try {
await fn(v, mp4);
spec.results[name] = true;
} catch (e) {
spec.results[name] = String(e && e.message || e);
}
log(name + ': ' + spec.results[name]);
await sleep(100);
}
spec.done = true;
log('done');
}
run().catch((e) => { spec.results.run = String(e); spec.done = true; });
</script>
</body>
</html>