289 lines
12 KiB
HTML
289 lines
12 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<!-- MSE player cases (S028), run one after another on mse-clip-mp4.js (fMP4 H.264 + AAC, 5 s: ftyp+moov, then five
|
|
moof+mdat fragments of about 1 s). window.player.results maps each case to true or to the reason it failed;
|
|
window.player.done turns true at the end. window.player.maxGap is the longest stall of a 20 ms timer while the
|
|
cases ran (the script thread must never block on attach or appends).
|
|
Cases: attach (the page keeps running while the player starts: a timer ticks between src= and sourceopen),
|
|
metadata (loadedmetadata after the initialization segment only, readyState HAVE_METADATA, then HAVE_ENOUGH_DATA
|
|
after the media), seekBuffered (seek inside the buffered range: seeking, seeked), seekUnbuffered (seek past the
|
|
buffered range waits for an append, then seeked), rate (playbackRate 2 plays about twice as fast), ended
|
|
(endOfStream, play to the end: ended), decodeError (endOfStream("decode") after metadata: MediaError 3,
|
|
sourceended), garbage (garbage after the initialization segment: SourceBuffer error then updateend, MediaError 3),
|
|
garbageFirst (garbage before any initialization segment: MediaError 4, sourceclose). ?only=<case>,... runs
|
|
only those. -->
|
|
<head><meta charset="utf-8"><title>MSE player</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 player = window.player = { results: {}, done: false, maxGap: 0, gaps: {}, events: {} };
|
|
const log = (line) => { document.getElementById('log').textContent += line + '\n'; };
|
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
// The longest gap between ticks of a 20 ms timer: a blocked script thread shows up here.
|
|
let lastTick = performance.now();
|
|
setInterval(() => {
|
|
const now = performance.now();
|
|
player.maxGap = Math.max(player.maxGap, Math.round(now - lastTick));
|
|
lastTick = now;
|
|
}, 20);
|
|
|
|
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);
|
|
});
|
|
}
|
|
|
|
// Splits an fMP4 file into its initialization segment (everything before the first moof) and its media
|
|
// segments (each moof with the boxes up to the next moof).
|
|
function segments(buffer) {
|
|
const view = new DataView(buffer);
|
|
const starts = [];
|
|
for (let i = 0; i < buffer.byteLength; i += view.getUint32(i)) {
|
|
const type = String.fromCharCode(...new Uint8Array(buffer, i + 4, 4));
|
|
if (type === 'moof') starts.push(i);
|
|
}
|
|
starts.push(buffer.byteLength);
|
|
return {
|
|
init: buffer.slice(0, starts[0]),
|
|
media: starts.slice(0, -1).map((s, k) => buffer.slice(s, starts[k + 1])),
|
|
};
|
|
}
|
|
|
|
// 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));
|
|
});
|
|
}
|
|
|
|
// Resolves true once cond() holds, polling every 50 ms, or false after ms.
|
|
async function until(cond, ms = 10000) {
|
|
for (const t0 = performance.now(); performance.now() - t0 < ms; await sleep(50)) {
|
|
if (cond()) return true;
|
|
}
|
|
return cond();
|
|
}
|
|
|
|
function check(cond, what) { if (!cond) throw new Error(what); }
|
|
const end = (ranges) => (ranges.length ? ranges.end(ranges.length - 1) : 0);
|
|
|
|
async function append(sb, data) {
|
|
sb.appendBuffer(data);
|
|
const ev = await next(sb, ['updateend']);
|
|
check(ev === 'updateend', 'append: ' + ev);
|
|
}
|
|
|
|
async function open(video) {
|
|
const ms = new MediaSource();
|
|
const opened = next(ms, ['sourceopen']);
|
|
video.src = URL.createObjectURL(ms);
|
|
check(await opened === 'sourceopen', 'no sourceopen');
|
|
return ms;
|
|
}
|
|
|
|
// A source with the whole clip appended (and the stream ended unless keepOpen).
|
|
async function full(v, clip, keepOpen) {
|
|
const ms = await open(v);
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
await append(sb, clip.init);
|
|
for (const m of clip.media) await append(sb, m);
|
|
if (!keepOpen) ms.endOfStream();
|
|
return { ms, sb };
|
|
}
|
|
|
|
function detach(video) {
|
|
video.removeAttribute('src');
|
|
video.load();
|
|
}
|
|
|
|
const cases = {
|
|
async attach(v, clip) {
|
|
const ms = new MediaSource();
|
|
let ticks = 0;
|
|
const timer = setInterval(() => ticks++, 10);
|
|
const opened = next(ms, ['sourceopen']);
|
|
const t0 = performance.now();
|
|
v.src = URL.createObjectURL(ms);
|
|
const srcSet = performance.now() - t0;
|
|
check(await opened === 'sourceopen', 'no sourceopen');
|
|
clearInterval(timer);
|
|
check(srcSet < 200, 'setting src took ' + Math.round(srcSet) + ' ms');
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
await append(sb, clip.init);
|
|
log('attach: src= ' + srcSet.toFixed(1) + ' ms, ' + ticks + ' ticks before sourceopen');
|
|
},
|
|
|
|
async metadata(v, clip) {
|
|
const ms = await open(v);
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
check(v.readyState === 0, 'readyState before init ' + v.readyState);
|
|
const meta = next(v, ['loadedmetadata']);
|
|
await append(sb, clip.init);
|
|
check(await meta === 'loadedmetadata', 'no loadedmetadata after the init segment');
|
|
check(v.readyState === 1, 'readyState after init ' + v.readyState);
|
|
const through = next(v, ['canplaythrough'], 15000);
|
|
for (const m of clip.media) await append(sb, m);
|
|
ms.endOfStream();
|
|
check(await through === 'canplaythrough', 'no canplaythrough');
|
|
check(v.readyState === 4, 'readyState after media ' + v.readyState);
|
|
check(v.videoWidth > 0 || await until(() => v.videoWidth > 0, 5000), 'videoWidth ' + v.videoWidth);
|
|
},
|
|
|
|
async seekBuffered(v, clip) {
|
|
await full(v, clip);
|
|
await until(() => v.readyState >= 2);
|
|
const seeking = next(v, ['seeking'], 2000);
|
|
const seeked = next(v, ['seeked'], 10000);
|
|
v.currentTime = 2.5;
|
|
check(v.seeking, 'not seeking right after currentTime=');
|
|
check(await seeking === 'seeking', 'no seeking');
|
|
check(await seeked === 'seeked', 'no seeked');
|
|
check(Math.abs(v.currentTime - 2.5) < 0.1, 'currentTime ' + v.currentTime);
|
|
},
|
|
|
|
async seekUnbuffered(v, clip) {
|
|
const ms = await open(v);
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
await append(sb, clip.init);
|
|
await append(sb, clip.media[0]);
|
|
ms.duration = 5;
|
|
const buffered = end(v.buffered);
|
|
check(buffered > 0.5 && buffered < 2, 'buffered ' + buffered);
|
|
const seeked = next(v, ['seeked'], 1000);
|
|
v.currentTime = 3.5;
|
|
check(await seeked === 'timeout', 'seeked before the data was there');
|
|
check(v.seeking, 'stopped seeking');
|
|
check(v.readyState <= 1, 'readyState while waiting ' + v.readyState);
|
|
const later = next(v, ['seeked'], 15000);
|
|
for (const m of clip.media.slice(1)) await append(sb, m);
|
|
ms.endOfStream();
|
|
check(await later === 'seeked', 'no seeked after the append');
|
|
check(Math.abs(v.currentTime - 3.5) < 0.2, 'currentTime ' + v.currentTime);
|
|
},
|
|
|
|
async rate(v, clip) {
|
|
await full(v, clip);
|
|
check(await until(() => v.readyState >= 3), 'readyState ' + v.readyState);
|
|
v.playbackRate = 2;
|
|
const ratechange = next(v, ['ratechange'], 2000);
|
|
check(await ratechange === 'ratechange', 'no ratechange');
|
|
await v.play();
|
|
await until(() => v.currentTime > 0.2, 5000);
|
|
const t0 = v.currentTime, w0 = performance.now();
|
|
await sleep(1500);
|
|
const speed = (v.currentTime - t0) / ((performance.now() - w0) / 1000);
|
|
v.pause();
|
|
log('rate: speed ' + speed.toFixed(2));
|
|
check(speed > 1.5 && speed < 2.6, 'speed ' + speed.toFixed(2));
|
|
},
|
|
|
|
async ended(v, clip) {
|
|
const { ms } = await full(v, clip);
|
|
check(ms.readyState === 'ended', 'readyState ' + ms.readyState);
|
|
const ended = next(v, ['ended'], 15000);
|
|
v.currentTime = Math.max(0, v.duration - 1);
|
|
await v.play();
|
|
check(await ended === 'ended', 'no ended');
|
|
check(v.ended, 'v.ended false');
|
|
},
|
|
|
|
async decodeError(v, clip) {
|
|
const ms = await open(v);
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
const meta = next(v, ['loadedmetadata']);
|
|
await append(sb, clip.init);
|
|
check(await meta === 'loadedmetadata', 'no loadedmetadata');
|
|
const error = next(v, ['error']);
|
|
const ended = next(ms, ['sourceended']);
|
|
ms.endOfStream('decode');
|
|
check(await ended === 'sourceended', 'no sourceended');
|
|
check(await error === 'error', 'no error on the element');
|
|
check(v.error && v.error.code === 3, 'error code ' + (v.error && v.error.code));
|
|
},
|
|
|
|
async garbage(v, clip) {
|
|
const ms = await open(v);
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
await append(sb, clip.init);
|
|
await until(() => v.readyState >= 1, 5000);
|
|
const order = [];
|
|
['error', 'updateend'].forEach((n) => sb.addEventListener(n, () => order.push(n)));
|
|
const error = next(v, ['error']);
|
|
const junk = new Uint8Array(4096);
|
|
for (let i = 0; i < junk.length; i++) junk[i] = (i * 151 + 7) & 255;
|
|
sb.appendBuffer(junk);
|
|
await next(sb, ['updateend']);
|
|
check(order.join() === 'error,updateend', 'SourceBuffer events ' + order.join());
|
|
check(await error === 'error', 'no error on the element');
|
|
check(v.error && v.error.code === 3, 'error code ' + (v.error && v.error.code));
|
|
check(ms.readyState === 'ended', 'MediaSource ' + ms.readyState);
|
|
},
|
|
|
|
async garbageFirst(v) {
|
|
const ms = await open(v);
|
|
const sb = ms.addSourceBuffer(MP4);
|
|
const error = next(v, ['error']);
|
|
const closed = next(ms, ['sourceclose']);
|
|
const junk = new Uint8Array(4096);
|
|
for (let i = 0; i < junk.length; i++) junk[i] = (i * 151 + 7) & 255;
|
|
sb.appendBuffer(junk);
|
|
check(await error === 'error', 'no error on the element');
|
|
check(v.error && v.error.code === 4, 'error code ' + (v.error && v.error.code));
|
|
check(await closed === 'sourceclose', 'no sourceclose');
|
|
check(ms.sourceBuffers.length === 0, 'sourceBuffers ' + ms.sourceBuffers.length);
|
|
},
|
|
};
|
|
|
|
(async () => {
|
|
const clip = segments(await loadClip('mse-clip-mp4.js'));
|
|
log('clip: init ' + clip.init.byteLength + ' bytes, ' + clip.media.length + ' media segments');
|
|
// ?only=a,b runs just those cases (for debugging).
|
|
const only = new URLSearchParams(location.search).get('only');
|
|
for (const [name, run] of Object.entries(cases)) {
|
|
if (only && !only.split(',').includes(name)) continue;
|
|
// A fresh element per case: errors stick to an element until its next load.
|
|
const old = document.getElementById('v');
|
|
const v = document.createElement('video');
|
|
v.id = 'v';
|
|
v.muted = true;
|
|
old.replaceWith(v);
|
|
detach(old);
|
|
// Per case: the longest timer stall, and the element's events with currentTime.
|
|
const gapBefore = player.maxGap;
|
|
player.maxGap = 0;
|
|
const events = player.events[name] = [];
|
|
['loadedmetadata', 'loadeddata', 'canplay', 'canplaythrough', 'seeking', 'seeked', 'waiting', 'playing',
|
|
'ended', 'error', 'durationchange'].forEach((n) =>
|
|
v.addEventListener(n, () => events.push(n + '@' + v.currentTime.toFixed(2))));
|
|
try {
|
|
await run(v, clip);
|
|
player.results[name] = true;
|
|
} catch (e) {
|
|
player.results[name] = String(e && e.message || e);
|
|
}
|
|
player.gaps[name] = player.maxGap;
|
|
player.maxGap = Math.max(player.maxGap, gapBefore);
|
|
log(name + ': ' + player.results[name] + ' (max gap ' + player.gaps[name] + ' ms)');
|
|
detach(v);
|
|
await sleep(200);
|
|
}
|
|
log('max timer gap: ' + player.maxGap + ' ms');
|
|
player.done = true;
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|