110 lines
4.5 KiB
HTML
110 lines
4.5 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<!-- MSE test (S022): a MediaSource with one SourceBuffer, fed mse-clip-mp4.js (fMP4, H.264 + AAC) or, with
|
|
?webm, mse-clip-webm.js (VP9 + Opus) in 32 KiB appendBuffer chunks. With ?split (S026) it uses two
|
|
SourceBuffers like YouTube: mse-clip-video.js (VP9 WebM, video only) is appended first, then after 2 s
|
|
mse-clip-audio.js (AAC fMP4, audio only). The clips are base64 scripts because file:// pages can't fetch() or
|
|
XHR other file:// URLs. window.mse holds the state for smoke asserts: stage, chunks appended, buffered end
|
|
(the smallest over the buffers), currentTime, readyState, videoWidth, errors. -->
|
|
<head><meta charset="utf-8"><title>MSE basic</title>
|
|
<style>
|
|
body { margin: 0; background: #1d3b2a; color: #fff; font: 20px sans-serif; text-align: center; }
|
|
video { display: block; margin: 20px auto; width: 640px; height: 360px; background: #000; }
|
|
#log { font: 16px monospace; white-space: pre; text-align: left; width: 640px; margin: 0 auto; }
|
|
</style></head>
|
|
<body>
|
|
<div id="head">MSE BASIC</div>
|
|
<video id="v" muted></video>
|
|
<div id="log"></div>
|
|
<script>
|
|
const webm = location.search.includes('webm');
|
|
const split = location.search.includes('split');
|
|
const tracks = split
|
|
? [{ file: 'mse-clip-video.js', mime: 'video/webm; codecs="vp9"' },
|
|
{ file: 'mse-clip-audio.js', mime: 'audio/mp4; codecs="mp4a.40.2"', delay: 2000 }]
|
|
: [{ file: webm ? 'mse-clip-webm.js' : 'mse-clip-mp4.js',
|
|
mime: webm ? 'video/webm; codecs="vp9, opus"' : 'video/mp4; codecs="avc1.4d401f, mp4a.40.2"' }];
|
|
const mse = window.mse = { stage: 'start', chunks: 0, buffered: 0, time: 0, errors: [] };
|
|
const log = (line) => { document.getElementById('log').textContent += line + '\n'; };
|
|
const fail = (what) => { mse.errors.push(String(what)); log('ERROR ' + what); };
|
|
|
|
function load(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);
|
|
});
|
|
}
|
|
|
|
function update() {
|
|
const v = document.getElementById('v');
|
|
mse.time = v.currentTime;
|
|
mse.readyState = v.readyState;
|
|
mse.videoWidth = v.videoWidth;
|
|
document.getElementById('head').textContent =
|
|
'MSE ' + mse.stage + ' chunks ' + mse.chunks + ' buffered ' + mse.buffered.toFixed(2) +
|
|
' time ' + mse.time.toFixed(2);
|
|
}
|
|
|
|
// Appends data to sb in 32 KiB chunks, one per updateend; resolves when all of it is in.
|
|
function append(sb, data) {
|
|
return new Promise((resolve) => {
|
|
let offset = 0;
|
|
const next = () => {
|
|
if (offset >= data.byteLength) {
|
|
sb.removeEventListener('updateend', next);
|
|
resolve();
|
|
return;
|
|
}
|
|
sb.appendBuffer(data.slice(offset, offset + 32768));
|
|
offset += 32768;
|
|
mse.chunks++;
|
|
};
|
|
sb.addEventListener('updateend', next);
|
|
next();
|
|
});
|
|
}
|
|
|
|
const bufferedEnd = (sb) => (sb.buffered.length ? sb.buffered.end(sb.buffered.length - 1) : 0);
|
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
async function run() {
|
|
if (!window.MediaSource) { mse.stage = 'no MediaSource'; return; }
|
|
mse.supported = tracks.every((t) => MediaSource.isTypeSupported(t.mime));
|
|
for (const t of tracks) log('isTypeSupported ' + MediaSource.isTypeSupported(t.mime) + ' ' + t.mime);
|
|
for (const t of tracks) t.data = await load(t.file);
|
|
const v = document.getElementById('v');
|
|
const ms = new MediaSource();
|
|
ms.addEventListener('sourceopen', async () => {
|
|
mse.stage = 'open';
|
|
log('sourceopen readyState ' + ms.readyState);
|
|
try {
|
|
for (const t of tracks) {
|
|
t.sb = ms.addSourceBuffer(t.mime);
|
|
t.sb.addEventListener('error', () => fail('SourceBuffer error ' + t.mime));
|
|
}
|
|
} catch (e) { fail(e); return; }
|
|
for (const t of tracks) {
|
|
if (t.delay) {
|
|
await sleep(t.delay);
|
|
log('before ' + t.mime + ': readyState ' + v.readyState);
|
|
}
|
|
await append(t.sb, t.data);
|
|
}
|
|
mse.buffered = Math.min(...tracks.map((t) => bufferedEnd(t.sb)));
|
|
ms.endOfStream();
|
|
mse.stage = 'ended';
|
|
log('endOfStream buffered ' + mse.buffered + ' duration ' + ms.duration);
|
|
v.play().then(() => log('playing'), fail);
|
|
});
|
|
ms.addEventListener('sourceended', () => log('sourceended'));
|
|
v.addEventListener('error', () => fail('video error ' + (v.error && v.error.message)));
|
|
v.src = URL.createObjectURL(ms);
|
|
setInterval(update, 250);
|
|
}
|
|
run().catch(fail);
|
|
</script>
|
|
</body>
|
|
</html>
|