A person had to turn recording on before a problem for its report to say what led up to it, and nobody does that before a problem. Recording is now passive and always on: the page and the App keep a bounded history in memory (8,192 events or 8 MiB), and a report is exported on demand. There is no recording switch for a person any more. The page's diagnostic control is one button that downloads the report, and the App launcher's is one button that downloads the App report. The launcher state no longer carries a debug field, the route that enabled App recording is gone, and the App no longer adds a debug flag to the pages it opens. The App installs its recorder at its start, before anything can log; the Server binary does not, so it is unchanged. Every guard that kept an event from being recorded is removed, and audio capture and capture frame counters always count. What stays off by default is the work that costs something of its own: the page's per-frame gap loop and console echo, which `?debug=1` starts, and the App's window-loop lateness probe and shutdown progress lines, which `PIIK_DIAGNOSTIC_PROBES=1` starts. Reports say whether probes ran instead of whether recording was on. The copy describes the report in all five languages, and keys and the visual kind that lost their last user are removed. Tests now assert that history is recorded with nothing switched on, that the probes run only with the developer's flag, and keep every assertion on bounds, secret filtering and report contents.
476 lines
28 KiB
JavaScript
476 lines
28 KiB
JavaScript
// Actual ChannelTransport/BrowserEncodingPool comparison; synthetic source and local receivers only.
|
|
import { BrowserEncodingPool as ProbePool } from '../src/client/media/browser-encoding-pool';
|
|
import { ChannelTransport } from '../src/client/channel/transport';
|
|
import { applyVideoCaptureProfile as probeApplyProfile, QUALITY_RESOLUTIONS } from '../src/client/media/quality';
|
|
import { browserDebugProbes, exportBrowserDebug } from '../src/client/lib/debug';
|
|
|
|
const settings = new URLSearchParams(location.search);
|
|
const mode = settings.get('mode') || 'carrier', codec = settings.get('codec') || 'VP8';
|
|
const network = settings.get('network') === '1', automatic = settings.get('automatic') === '1';
|
|
const high = settings.get('high') === '1', av = settings.get('av') === '1';
|
|
const single = settings.get('single') === '1', late = settings.get('late') === '1';
|
|
const nativeSource = settings.get('nativeSource') === '1', background = settings.get('background') === '1';
|
|
const lifecycle = settings.get('lifecycle') === '1', shapedRate = Number(settings.get('rate') || 120000);
|
|
const relay = settings.get('relay') === '1';
|
|
const shortPulse = settings.get('shortPulse') === '1';
|
|
const repeatedPulses = settings.get('repeatedPulses') === '1', textured = settings.get('textured') === '1';
|
|
const displaySource = settings.get('displaySource') === '1';
|
|
const quietStart = settings.get('quietStart') === '1';
|
|
const customSize = settings.has('width') ? { width: Number(settings.get('width')), height: Number(settings.get('height')) } : null;
|
|
const selectedProfile = () => ({ resolution: customSize ?? QUALITY_RESOLUTIONS[high ? '1080p' : '480p'], maxFramerate: 30,
|
|
maxBitrate: customSize ? 12000000 : high ? 5000000 : 1700000,
|
|
degradationPreference: settings.get('preference') || (high ? 'maintain-resolution' : 'balanced'),
|
|
maxAudioBitrate: Number(settings.get('audioBitrate') || 128000) });
|
|
const productPool = mode === 'carrier' ? new ProbePool() : null;
|
|
const childEdges = [], peers = [], tracks = [], edges = [], monitors = [], audioMonitors = [];
|
|
const errors = [], sourcePulses = [], controls = [], visibility = [], productTrace = [], sourceFrames = [];
|
|
let sourceReader, sourceReading;
|
|
const displayClones = [];
|
|
if (displaySource) {
|
|
const clone = MediaStreamTrack.prototype.clone;
|
|
MediaStreamTrack.prototype.clone = function () {
|
|
const track = clone.call(this);
|
|
if (this.getSettings().displaySurface) displayClones.push(track);
|
|
return track;
|
|
};
|
|
}
|
|
const sourceTimes = new Map(), seenProductGroups = new Map();
|
|
let rawSource, drawing, productTraceTimer, audioContext, audioTrack, audioGain, upstreamPeer;
|
|
let producing = !quietStart, sourceFrame = 0, phase = 'startup';
|
|
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
const poolState = () => productPool ? {
|
|
members: [...productPool.members].map(m => ({ id: m.id, group: m.current?.producer.id,
|
|
pending: m.pending?.group.producer.id, selecting: m.pending?.selecting, budget: m.budget,
|
|
mixed: m.mixed, disabled: m.disabled, carrier: m.carrier, replacing: !!m.replacement,
|
|
output: m.output, queued: m.encoded.current?.frames.length,
|
|
waitingKey: m.encoded.current?.needKey, pendingFrames: m.encoded.pending?.queue.frames.length })),
|
|
groups: [...productPool.groups].map(g => ({ id: g.producer.id, budget: g.budget,
|
|
ready: g.ready, failed: g.failed, output: g.output }))
|
|
} : null;
|
|
window.probeProgress = () => ({ phase, sourceFrame, errors, pool: poolState(),
|
|
sourceSize: { width: rawSource?.getSettings().width, height: rawSource?.getSettings().height },
|
|
captured: sourceFrames.slice(-3), decoded: monitors.map(m => ({ role: m.role, last: m.samples.at(-1) })),
|
|
peers: peers.map(p => ({ connection: p.connectionState, ice: p.iceConnectionState })),
|
|
audio: audioMonitors.map(m => ({ role: m.role, attached: m.attached, pulses: m.pulses.length })) });
|
|
|
|
function watch(video, role) {
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = 640; canvas.height = 360;
|
|
const context = canvas.getContext('2d', { willReadFrequently: true }), samples = [];
|
|
let stopped = false;
|
|
const next = () => video.requestVideoFrameCallback((_time, metadata) => {
|
|
if (stopped) return;
|
|
if (nativeSource || displaySource) {
|
|
samples.push({ at: performance.now(), phase, id: metadata.presentedFrames, age: null,
|
|
width: video.videoWidth, height: video.videoHeight });
|
|
} else {
|
|
try {
|
|
context.drawImage(video, 0, 0, 640, 360);
|
|
const pixels = context.getImageData(0, 0, 640, 30).data;
|
|
let id = 0;
|
|
for (let bit = 0; bit < 12; bit++) {
|
|
const index = (15 * 640 + 20 + bit * 50) * 4;
|
|
if (pixels[index] + pixels[index + 1] + pixels[index + 2] > 384) id |= 1 << bit;
|
|
}
|
|
const now = performance.now(), created = sourceTimes.get(id);
|
|
samples.push({ at: now, phase, id, age: created === undefined ? null : now - created,
|
|
width: video.videoWidth, height: video.videoHeight });
|
|
} catch (error) { errors.push(String(error.message)); }
|
|
}
|
|
next();
|
|
});
|
|
next();
|
|
monitors.push({ role, samples, stop: () => { stopped = true; } });
|
|
}
|
|
|
|
function watchAudio(video, role) {
|
|
// Observe element-level played audio, not the earlier receiver track or physical speakers.
|
|
const played = video.captureStream();
|
|
const monitor = { role, pulses: [], attached: false, active: false,
|
|
stop() { this.active = false; played.getTracks().forEach(t => t.stop()); } };
|
|
audioMonitors.push(monitor);
|
|
const attach = () => {
|
|
if (monitor.attached || !played.getAudioTracks().length) return;
|
|
monitor.attached = monitor.active = true;
|
|
const source = audioContext.createMediaStreamSource(played), analyser = audioContext.createAnalyser();
|
|
analyser.fftSize = 512;
|
|
const silent = audioContext.createGain();
|
|
silent.gain.value = 0;
|
|
source.connect(analyser).connect(silent).connect(audioContext.destination);
|
|
const values = new Float32Array(analyser.fftSize);
|
|
let sounded = false;
|
|
const sample = () => {
|
|
if (!monitor.active) { source.disconnect(); analyser.disconnect(); silent.disconnect(); return; }
|
|
analyser.getFloatTimeDomainData(values);
|
|
const loud = values.some(value => Math.abs(value) > .01);
|
|
if (loud && !sounded) monitor.pulses.push({ at: performance.now(), phase });
|
|
sounded = loud;
|
|
requestAnimationFrame(sample);
|
|
};
|
|
sample();
|
|
};
|
|
played.addEventListener('addtrack', attach);
|
|
attach();
|
|
}
|
|
|
|
async function pair(role) {
|
|
const receive = new RTCPeerConnection({ iceServers: [] }), pending = [];
|
|
peers.push(receive);
|
|
let edge;
|
|
const peer = new ChannelTransport({
|
|
polite: false, iceServers: [], canReceiveVideoLayers: false, videoPool: role === 'U' ? undefined : productPool,
|
|
send(payload) {
|
|
if (payload.description) {
|
|
if (edge.answer) edge.signaling = edge.signaling.then(() => edge.answer(payload)).catch(error => errors.push(String(error)));
|
|
else edge.offer = payload;
|
|
} else if (payload.candidate && !(network && role === 'A')) {
|
|
if (receive.remoteDescription) receive.addIceCandidate(payload.candidate).catch(error => errors.push(String(error)));
|
|
else pending.push(payload.candidate);
|
|
}
|
|
},
|
|
receive() {}, connected() {}, base() {},
|
|
failed() { errors.push(role + ': transport failed'); },
|
|
outgoingFailed(flowId) { errors.push(role + ': output failed ' + flowId); },
|
|
});
|
|
if (role === 'U') upstreamPeer = peer;
|
|
peers.push(peer.pc);
|
|
const video = document.createElement('video'), received = new MediaStream();
|
|
video.autoplay = video.muted = video.playsInline = true;
|
|
video.width = 320; video.srcObject = received;
|
|
if (role !== 'U') document.body.append(video);
|
|
receive.ontrack = event => { received.addTrack(event.track); video.play().catch(() => {}); };
|
|
if (av && role !== 'U') video.addEventListener('loadeddata', () => watchAudio(video, role), { once: true });
|
|
if (!(network && role === 'A')) receive.onicecandidate = event => {
|
|
if (event.candidate) peer.signal({ candidate: event.candidate.toJSON() });
|
|
};
|
|
edge = { role, peer, send: peer.pc, receive, pending, received, signaling: Promise.resolve() };
|
|
edges.push(edge);
|
|
if (role !== 'U') childEdges.push(edge);
|
|
peer.setOutgoing(new Map([[role, screenMedia(selectedProfile())]]));
|
|
const deadline = performance.now() + 6000;
|
|
while (!edge.offer && performance.now() < deadline) await wait(20);
|
|
if (!edge.offer) throw Error('Product transport did not offer its media');
|
|
edge.sender = peer.pc.getSenders().find(sender => sender.track?.kind === 'video');
|
|
if (role !== 'U') watch(video, role);
|
|
return edge;
|
|
}
|
|
|
|
function screenMedia(profile) {
|
|
return { kind: 'screen', stream: new MediaStream([rawSource, ...(audioTrack ? [audioTrack] : [])]),
|
|
profile, audioBitrate: profile.maxAudioBitrate, paused: !rawSource.enabled,
|
|
codec: { primary: codec.toLowerCase(), vp8Fallback: false } };
|
|
}
|
|
|
|
async function connect(edge) {
|
|
const gathered = async pc => {
|
|
const end = performance.now() + 5000;
|
|
while (pc.iceGatheringState !== 'complete' && performance.now() < end) await wait(20);
|
|
};
|
|
if (network && edge.role === 'A') {
|
|
const firstPort = sdp => Number(sdp.match(/^a=candidate:\S+ 1 udp \d+ \S+ (\d+)/mi)?.[1]);
|
|
const proxySDP = (description, port) => ({ ...description, sdp: description.sdp.replace(/^a=candidate:.*\r?\n/gm, '')
|
|
.replace(/^a=mid:[^\r\n]+\r?$/gm, line => line.trimEnd() + '\r\na=candidate:proxy 1 udp 2130706431 127.0.0.1 ' + port + ' typ host\r') });
|
|
const proxy = await fetch('/shaper').then(r => r.json());
|
|
edge.answer = async payload => {
|
|
await gathered(edge.send);
|
|
const offer = { type: 'offer', sdp: edge.send.localDescription.sdp };
|
|
if (!offer.sdp.includes('a=end-of-candidates')) offer.sdp += 'a=end-of-candidates\r\n';
|
|
await edge.receive.setRemoteDescription(proxySDP(offer, proxy.ports.right));
|
|
await edge.receive.setLocalDescription(await edge.receive.createAnswer());
|
|
await gathered(edge.receive);
|
|
const answer = { type: 'answer', sdp: edge.receive.localDescription.sdp };
|
|
if (!answer.sdp.includes('a=end-of-candidates')) answer.sdp += 'a=end-of-candidates\r\n';
|
|
await fetch('/shaper?left=' + firstPort(offer.sdp) + '&right=' + firstPort(answer.sdp));
|
|
edge.peer.signal({ exchangeId: payload.exchangeId, description: proxySDP(answer, proxy.ports.left) });
|
|
};
|
|
} else {
|
|
edge.answer = async payload => {
|
|
await edge.receive.setRemoteDescription(payload.description);
|
|
for (const candidate of edge.pending.splice(0)) await edge.receive.addIceCandidate(candidate);
|
|
await edge.receive.setLocalDescription(await edge.receive.createAnswer());
|
|
edge.peer.signal({ exchangeId: payload.exchangeId, description: edge.receive.localDescription });
|
|
};
|
|
}
|
|
edge.signaling = edge.signaling.then(() => edge.answer(edge.offer));
|
|
await edge.signaling;
|
|
const deadline = performance.now() + 6000;
|
|
while (edge.send.connectionState !== 'connected' && performance.now() < deadline) await wait(30);
|
|
if (edge.send.connectionState !== 'connected') throw Error('Connection deadline');
|
|
}
|
|
|
|
const selectReport = report => Array.from(report.values())
|
|
.filter(r => ['outbound-rtp', 'inbound-rtp', 'remote-inbound-rtp', 'remote-outbound-rtp',
|
|
'candidate-pair', 'transport', 'local-candidate', 'remote-candidate'].includes(r.type))
|
|
.map(r => {
|
|
const fields = ['type', 'kind', 'id', 'localCandidateId', 'remoteCandidateId', 'selectedCandidatePairId',
|
|
'port', 'nominated', 'framesEncoded', 'framesDecoded', 'framesSent', 'framesReceived', 'bytesSent',
|
|
'bytesReceived', 'totalEncodeTime', 'totalDecodeTime', 'frameWidth', 'frameHeight', 'framesPerSecond',
|
|
'targetBitrate', 'qualityLimitationReason', 'qualityLimitationDurations', 'encoderImplementation', 'powerEfficientEncoder',
|
|
'qpSum', 'pliCount', 'nackCount', 'retransmittedBytesSent',
|
|
'packetsLost', 'jitter', 'jitterBufferDelay', 'jitterBufferEmittedCount', 'freezeCount',
|
|
'totalFreezesDuration', 'reportsSent', 'remoteTimestamp', 'currentRoundTripTime', 'availableOutgoingBitrate', 'state'];
|
|
return Object.fromEntries(fields.filter(key => r[key] !== undefined).map(key => [key, r[key]]));
|
|
});
|
|
async function statistics() {
|
|
const result = {};
|
|
for (const edge of edges) {
|
|
const report = await edge.send.getStats();
|
|
const metrics = edge.peer.outgoingMetrics(report, new Set([edge.role]))[0]?.metrics ?? {};
|
|
result[edge.role] = { audioParameters: edge.send.getSenders().find(sender => sender.track?.kind === 'audio')?.getParameters(),
|
|
send: selectReport(report),
|
|
receive: selectReport(await edge.receive.getStats()),
|
|
details: Object.fromEntries(['frameWidth', 'frameHeight', 'framesPerSecond', 'bitrateKbps',
|
|
'qualityLimitationReason', 'intervalFramesSent', 'intervalFramesEncoded']
|
|
.map(key => [key, metrics[key]])) };
|
|
}
|
|
if (productPool) {
|
|
for (const group of productPool.groups) if (!seenProductGroups.has(group.producer.id))
|
|
seenProductGroups.set(group.producer.id, { name: 'G' + seenProductGroups.size, group });
|
|
for (const entry of seenProductGroups.values()) {
|
|
const { name, group } = entry;
|
|
const report = await group.producer.report().catch(() => entry.report ?? group.report);
|
|
if (report) {
|
|
entry.report = report;
|
|
result[name] = { send: selectReport(report), receive: [], retired: !productPool.groups.has(group) };
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
async function snapshot() {
|
|
return { phase, at: performance.now(), stats: await statistics(),
|
|
process: await fetch('/process').then(r => r.json()),
|
|
...(network ? { shaper: await fetch('/shaper').then(r => r.json()) } : {}) };
|
|
}
|
|
|
|
async function run() {
|
|
if (av) {
|
|
audioContext = new AudioContext({ sampleRate: 48000 });
|
|
await audioContext.resume();
|
|
const destination = audioContext.createMediaStreamDestination(), tone = audioContext.createOscillator();
|
|
tone.frequency.value = 1000;
|
|
audioGain = audioContext.createGain(); audioGain.gain.value = 0;
|
|
tone.connect(audioGain).connect(destination); tone.start();
|
|
audioTrack = destination.stream.getAudioTracks()[0]; tracks.push(audioTrack);
|
|
}
|
|
const source = document.createElement('canvas');
|
|
source.width = customSize?.width ?? (high ? 1920 : 640); source.height = customSize?.height ?? (high ? 1080 : 360);
|
|
if (!nativeSource) document.body.prepend(source);
|
|
const ctx = source.getContext('2d');
|
|
ctx.scale(source.width / 640, source.height / 360);
|
|
if (displaySource) {
|
|
document.body.style.margin = '0';
|
|
source.style.cssText = 'position:fixed;inset:0;width:100vw;height:100vh;z-index:10';
|
|
}
|
|
rawSource = displaySource ? await new Promise((resolve, reject) => {
|
|
window.acquireDisplay = () => navigator.mediaDevices.getDisplayMedia({ audio: false, preferCurrentTab: true,
|
|
selfBrowserSurface: 'include', video: { width: source.width, height: source.height, frameRate: 30 } })
|
|
.then(stream => resolve(stream.getVideoTracks()[0]), reject);
|
|
}) : nativeSource ? (await navigator.mediaDevices.getUserMedia({
|
|
video: { width: source.width, height: source.height, frameRate: 30 }, audio: false
|
|
})).getVideoTracks()[0] : source.captureStream(0).getVideoTracks()[0];
|
|
rawSource.contentHint = 'motion'; tracks.push(rawSource);
|
|
if (displaySource) {
|
|
sourceReader = new MediaStreamTrackProcessor({ track: rawSource, maxBufferSize: 1 }).readable.getReader();
|
|
sourceReading = (async () => { for (;;) {
|
|
const { done, value } = await sourceReader.read(); if (done) break;
|
|
sourceFrames.push({ at: performance.now(), phase, width: value.displayWidth, height: value.displayHeight }); value.close();
|
|
} })().catch(error => errors.push(String(error)));
|
|
}
|
|
await probeApplyProfile(rawSource, selectedProfile());
|
|
const main = rawSource;
|
|
const connectChildren = async () => {
|
|
for (const role of single ? ['A'] : ['A', 'B']) await pair(role);
|
|
for (const edge of edges) if (edge.role !== 'U' && (!late || edge.role !== 'B')) await connect(edge);
|
|
};
|
|
if (!relay) await connectChildren();
|
|
if (!nativeSource) drawing = setInterval(() => {
|
|
if (!producing) return;
|
|
const id = sourceFrame++ % 4096;
|
|
sourceTimes.set(id, performance.now());
|
|
if (audioGain) {
|
|
audioGain.gain.setValueAtTime(id % 60 < 6 ? .1 : 0, audioContext.currentTime);
|
|
if (id % 60 === 0) sourcePulses.push({ id, at: performance.now() });
|
|
}
|
|
ctx.fillStyle = '#172b3a'; ctx.fillRect(0, 0, 640, 360);
|
|
if (textured) for (let y = 34; y < 320; y += 8) for (let x = 0; x < 640; x += 8) {
|
|
const n = (((x + sourceFrame * 7) >> 3) * 17 + (y >> 3) * 23) & 255;
|
|
ctx.fillStyle = `rgb(${n},${(n * 3) & 255},${(n * 7) & 255})`;
|
|
ctx.fillRect(x, y, 8, 8);
|
|
}
|
|
ctx.fillStyle = '#48c98b'; ctx.fillRect((sourceFrame * 11) % 540, 65, 100, 220);
|
|
ctx.fillStyle = '#f7b744'; ctx.font = '40px sans-serif'; ctx.fillText(String(id), 30, 335);
|
|
for (let bit = 0; bit < 12; bit++) {
|
|
ctx.fillStyle = id & (1 << bit) ? '#fff' : '#000'; ctx.fillRect(bit * 50, 0, 50, 30);
|
|
}
|
|
(relay ? main : rawSource).requestFrame?.();
|
|
}, 1000 / (lifecycle ? 60 : 30));
|
|
if (productPool) productTraceTimer = setInterval(() => productTrace.push({ at: performance.now(), phase, ...poolState() }), 500);
|
|
if (relay) {
|
|
const upstream = await pair('U');
|
|
await connect(upstream);
|
|
rawSource = upstream.received.getVideoTracks()[0];
|
|
audioTrack = upstream.received.getAudioTracks()[0];
|
|
if (!rawSource || av && !audioTrack) throw Error('Relay upstream did not supply its tracks');
|
|
rawSource.contentHint = 'motion';
|
|
await connectChildren();
|
|
}
|
|
if (late) { await wait(1000); await connect(edges.find(e => e.role === 'B')); }
|
|
await wait(1900);
|
|
if (productPool) {
|
|
const deadline = performance.now() + 12000;
|
|
let activated = null;
|
|
for (;;) {
|
|
const members = [...productPool.members];
|
|
// Compatible allocations share an owner; distinct BWE budgets must
|
|
// remain independent even when this synthetic scene encodes cheaply.
|
|
const sharing = members.length === childEdges.length && members.every(m => m.current && m.carrier && !m.replacement) &&
|
|
new Set(members.map(m => m.current)).size === new Set(members.map(m => m.budget)).size;
|
|
if (sharing) activated ??= performance.now();
|
|
else activated = null;
|
|
if (activated !== null && monitors.every(m => {
|
|
const last = m.samples.at(-1);
|
|
return last?.width > 16 && (nativeSource || displaySource ? last.at >= activated :
|
|
last.age !== null && (sourceTimes.get(last.id) ?? -1) >= activated);
|
|
})) break;
|
|
if (performance.now() >= deadline) throw Error('Product shared output did not become fresh');
|
|
await wait(30);
|
|
}
|
|
}
|
|
const descriptions = edges.map(e => ({ role: e.role,
|
|
extensions: e.send.localDescription?.sdp.match(/^a=extmap:.*$/gm),
|
|
parameters: e.sender.getParameters().encodings }));
|
|
const snapshots = [await snapshot()];
|
|
if (quietStart) {
|
|
const deadline = performance.now() + 10000;
|
|
while (!monitors.every(m => m.samples.some(s => s.width > 16))) {
|
|
if (performance.now() >= deadline) throw Error('Static display produced no first frame');
|
|
await wait(30);
|
|
}
|
|
controls.push({ action: 'static-first-frame', at: performance.now(), sourceFrame });
|
|
producing = true;
|
|
}
|
|
const observeNetwork = async (name, duration, pulse) => {
|
|
phase = name; controls.push({ action: name, at: performance.now(), pulse });
|
|
const until = performance.now() + duration;
|
|
do { await wait(Math.min(1000, Math.max(0, until - performance.now()))); snapshots.push(await snapshot()); }
|
|
while (performance.now() < until);
|
|
};
|
|
if (repeatedPulses) await observeNetwork('healthy', 30000);
|
|
else { phase = 'healthy'; await wait(shortPulse ? 45000 : 6000); snapshots.push(await snapshot()); }
|
|
if (lifecycle) {
|
|
let appliedProfile = selectedProfile();
|
|
const apply = async profile => {
|
|
const at = performance.now();
|
|
await probeApplyProfile(rawSource, profile);
|
|
await Promise.all(childEdges.map(async edge => {
|
|
edge.peer.setOutgoing(new Map([[edge.role, screenMedia(profile)]]));
|
|
await edge.peer.configureVideo(edge.role, profile);
|
|
}));
|
|
controls.push({ phase, profile, elapsedMs: performance.now() - at });
|
|
appliedProfile = profile;
|
|
};
|
|
const observe = async (name, duration = 6000) => {
|
|
phase = name; await wait(duration); snapshots.push(await snapshot());
|
|
if (name.startsWith('quiet-') || name.startsWith('paused-')) return;
|
|
for (const monitor of monitors) {
|
|
const last = monitor.samples.at(-1), ceiling = appliedProfile.resolution;
|
|
if (last?.phase !== name || (!displaySource && (last.age === null || last.age >= 1000)) ||
|
|
performance.now() - last.at >= 1000 || last.width > ceiling.width || last.height > ceiling.height)
|
|
throw Error(name + ': receiver did not deliver current frames within the requested ceiling');
|
|
}
|
|
};
|
|
const p60 = { ...selectedProfile(), resolution: QUALITY_RESOLUTIONS['720p'], maxFramerate: 60, maxBitrate: 5000000 };
|
|
phase = 'profile-720p60'; await apply(p60); await observe(phase);
|
|
producing = false; phase = 'quiet-profile-480p30';
|
|
await apply({ ...p60, resolution: QUALITY_RESOLUTIONS['480p'], maxFramerate: 30, maxBitrate: 3000000 });
|
|
await observe(phase, 2500);
|
|
producing = true; await observe('resumed-480p30');
|
|
rawSource.enabled = false; childEdges.forEach(edge => edge.peer.setOutgoing(new Map([[edge.role, screenMedia(appliedProfile)]])));
|
|
phase = 'paused-profile-720p30'; await apply({ ...p60, maxFramerate: 30 }); await observe(phase, 2000);
|
|
rawSource.enabled = true; childEdges.forEach(edge => edge.peer.setOutgoing(new Map([[edge.role, screenMedia(appliedProfile)]])));
|
|
await observe('resumed-720p30');
|
|
const oldSource = rawSource;
|
|
rawSource = source.captureStream(0).getVideoTracks()[0];
|
|
rawSource.contentHint = 'motion'; tracks.push(rawSource);
|
|
await probeApplyProfile(rawSource, appliedProfile);
|
|
const replaced = await Promise.all(childEdges.map(async edge => {
|
|
edge.peer.setOutgoing(new Map([[edge.role, screenMedia(appliedProfile)]]));
|
|
await edge.peer.configureVideo(edge.role, appliedProfile);
|
|
return edge.peer.sending.get(edge.role)?.senders.some(binding => binding.source === rawSource);
|
|
}));
|
|
controls.push({ action: 'source-replaced', replaced });
|
|
if (!replaced.every(Boolean)) throw Error('Product source replacement failed');
|
|
oldSource.stop(); await observe('replaced-source');
|
|
if (productPool && [...productPool.groups].some(group => group.source !== rawSource)) throw Error('Old source group survived replacement');
|
|
if (productPool) {
|
|
for (const group of [...productPool.groups]) group.producer.fail();
|
|
await observe('ordinary-after-producer-failure');
|
|
if (productPool.groups.size || childEdges.some(edge => edge.send.connectionState !== 'connected')) throw Error('Ordinary recovery did not retire the pool');
|
|
}
|
|
}
|
|
if (background) {
|
|
phase = 'hidden'; await fetch('/visibility?hidden=1');
|
|
visibility.push({ at: performance.now(), requested: 'hidden', actual: document.visibilityState });
|
|
if (document.visibilityState !== 'hidden') throw Error('Page was not actually hidden');
|
|
await wait(10000); snapshots.push(await snapshot());
|
|
phase = 'visible-again'; await fetch('/visibility?hidden=0');
|
|
visibility.push({ at: performance.now(), requested: 'visible', actual: document.visibilityState });
|
|
if (document.visibilityState !== 'visible') throw Error('Page was not actually restored');
|
|
await wait(6000); snapshots.push(await snapshot());
|
|
}
|
|
if (network) {
|
|
if (repeatedPulses) {
|
|
for (let pulse = 0; pulse < 3; pulse++) {
|
|
await fetch('/shaper?rate=' + shapedRate);
|
|
await observeNetwork('limited', 1000, pulse);
|
|
await fetch('/shaper?rate=0');
|
|
await observeNetwork('released', pulse < 2 ? 9000 : 75000, pulse);
|
|
}
|
|
} else {
|
|
await fetch('/shaper?rate=' + shapedRate);
|
|
phase = 'limited'; controls.push({ action: phase, at: performance.now() });
|
|
await wait(shortPulse ? 1000 : 14000); snapshots.push(await snapshot());
|
|
await fetch('/shaper?rate=0');
|
|
// --auto extends recovery observation; adaptation always belongs to the product.
|
|
phase = 'released'; controls.push({ action: phase, at: performance.now() });
|
|
await wait(automatic ? 40000 : 10000); snapshots.push(await snapshot());
|
|
}
|
|
if (displaySource) {
|
|
const healthy = sourceFrames.findLast(frame => frame.phase === 'healthy');
|
|
if (!healthy || sourceFrames.some(frame => ['limited', 'released'].includes(frame.phase) &&
|
|
(frame.width < healthy.width - 2 || frame.height < healthy.height - 2)))
|
|
throw Error('Sender adaptation reduced its display-capture input');
|
|
}
|
|
}
|
|
return { descriptions, snapshots, browser: navigator.userAgent, sourcePulses, sourceFrames,
|
|
audioMonitors: audioMonitors.map(({ role, pulses, attached }) => ({ role, pulses, attached })),
|
|
monitors: monitors.map(({ role, samples }) => ({ role, samples })) };
|
|
}
|
|
const configuration = { mode, codec, product: true, network, automatic, av, high, single, late, shortPulse, repeatedPulses, textured,
|
|
background, nativeSource, displaySource, quietStart, lifecycle, relay, shapedRate, profile: selectedProfile() };
|
|
try {
|
|
window.probeResult = { ...configuration, ...await run(), controls, visibility, productTrace, errors };
|
|
if (browserDebugProbes) {
|
|
const report = JSON.parse(await exportBrowserDebug());
|
|
window.probeResult.debug = report;
|
|
if (productPool && (!report.events.some(e => e.scope === 'encoding-pool' && e.event === 'sample') ||
|
|
!report.events.some(e => e.scope === 'webrtc' && e.event === 'stats' && e.details.role === 'pool-producer')))
|
|
throw Error('Debug report omitted the pool or producer evidence');
|
|
}
|
|
} catch (error) {
|
|
window.probeResult = { ...configuration, error: String(error?.stack || error?.message || error),
|
|
progress: window.probeProgress(), controls, visibility, productTrace, errors };
|
|
} finally {
|
|
clearInterval(drawing); clearInterval(productTraceTimer);
|
|
monitors.forEach(m => m.stop()); audioMonitors.forEach(m => m.stop());
|
|
childEdges.forEach(edge => edge.peer.close()); productPool?.dispose();
|
|
upstreamPeer?.close();
|
|
await sourceReader?.cancel();
|
|
await sourceReading;
|
|
tracks.forEach(t => t.stop()); peers.forEach(pc => pc.close());
|
|
await audioContext?.close();
|
|
if (displayClones.some(track => track.readyState !== 'ended')) errors.push('Sender retirement left a live display-capture clone');
|
|
window.probeDone = true;
|
|
}
|