Files
CyberGril/samples/companion/tools/record_graph.html
T

90 lines
4.1 KiB
HTML

<!doctype html>
<html>
<head><meta charset="utf-8"><title>HEL-717 remote-stream graph</title></head>
<body>
<video id="talk" muted playsinline autoplay></video>
<canvas id="cv" width="960" height="720"></canvas>
<script>
// Replicates the product playback graph exactly:
// remote WebRTC stream -> <video> (muted, drawn to canvas via rAF)
// -> MediaStreamSource -> context.destination + MediaStreamDestination
// MediaRecorder over canvas.captureStream(25) + destination track (zero clock warm).
const events = [];
function mark(name, detail={}) { events.push({at: performance.now(), event: name, ...detail}); }
const talk = document.getElementById('talk');
const cv = document.getElementById('cv');
const ctx = cv.getContext('2d');
function draw() {
ctx.fillStyle = '#101010'; ctx.fillRect(0, 0, 960, 720);
if (talk.readyState >= 2) {
const s = Math.min(960 / talk.videoWidth, 640 / talk.videoHeight);
ctx.drawImage(talk, (960 - talk.videoWidth * s) / 2, 0, talk.videoWidth * s, talk.videoHeight * s);
}
ctx.fillStyle = '#fff'; ctx.font = '18px sans-serif';
ctx.fillText(`${(performance.now() / 1000).toFixed(3)}s · remote-stream graph`, 12, 705);
requestAnimationFrame(draw);
}
draw();
window.diagRun = async function () {
const actx = new AudioContext();
const dest = actx.createMediaStreamDestination();
const analyser = actx.createAnalyser();
const clock = actx.createConstantSource(); clock.offset.value = 0;
clock.connect(dest); clock.connect(actx.destination); clock.start();
const stream = new MediaStream();
talk.srcObject = stream;
const pc = new RTCPeerConnection({iceServers: []});
pc.ontrack = e => stream.addTrack(e.track);
pc.addTransceiver('audio', {direction: 'recvonly'});
pc.addTransceiver('video', {direction: 'recvonly'});
await pc.setLocalDescription(await pc.createOffer());
await new Promise((res, rej) => {
const to = setTimeout(() => rej(new Error('ice timeout')), 8000);
const check = () => { if (pc.iceGatheringState === 'complete') { clearTimeout(to); res(); } };
pc.addEventListener('icegatheringstatechange', check); check();
});
mark('offer-sent');
const answer = await (await fetch('/lt/offer', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(pc.localDescription)})).json();
await pc.setRemoteDescription(answer);
await talk.play();
await new Promise((res, rej) => {
const to = setTimeout(() => rej(new Error('media timeout')), 10000);
const check = () => { if (talk.readyState >= 2) { clearTimeout(to); res(); } };
talk.addEventListener('loadeddata', check, {once: true}); check();
});
mark('media-ready');
const src = actx.createMediaStreamSource(stream);
src.connect(actx.destination); src.connect(dest); src.connect(analyser);
// recorder
const recStream = cv.captureStream(25);
dest.stream.getAudioTracks().forEach(t => recStream.addTrack(t));
const chunks = [];
const rec = new MediaRecorder(recStream, {mimeType: 'video/webm;codecs=vp8,opus'});
rec.ondataavailable = e => chunks.push(e.data);
const stopped = new Promise(r => rec.onstop = r);
rec.start(1000);
mark('recorder-start');
const wav = await (await fetch('/fixed.wav')).arrayBuffer();
for (const [index, idle] of [0, 15000, 1000, 1000].entries()) {
await new Promise(r => setTimeout(r, idle));
const form = new FormData();
form.append('sessionid', answer.sessionid);
form.append('file', new Blob([wav]), 'utterance.wav');
mark('humanaudio-post', {round: index + 1, idle});
const rr = await fetch('/lt/humanaudio', {method: 'POST', body: form});
mark('humanaudio-ack', {status: rr.status});
await new Promise(r => setTimeout(r, 9000));
}
rec.stop(); await stopped;
recStream.getVideoTracks().forEach(t => t.stop());
await pc.close();
await fetch('/lt/hel714/close', {method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({sessionid:answer.sessionid})});
await actx.close();
const blob = new Blob(chunks, {type: 'video/webm'});
window.diagResult = {blobUrl: URL.createObjectURL(blob), events};
document.title = 'done';
};
</script>
</body>
</html>