// Shared owned-canvas/audio fixture for normal and kernel-NAT Browser acceptance. import assert from "node:assert/strict"; export function fixture(browser) { async function evaluate(index, expression) { const script = `(async () => JSON.stringify(await (${expression})))()`; const data = await browser(index, "eval", "-b", Buffer.from(script).toString("base64")); return data.result === undefined ? undefined : JSON.parse(data.result); } async function until(label, sample, predicate, timeout = 20_000) { const deadline = Date.now() + timeout; let value; do { value = await sample(); if (predicate(value)) return value; await new Promise(resolve => setTimeout(resolve, 200)); } while (Date.now() < deadline); throw new Error(`${label}: ${JSON.stringify(value)}`); } function captureProbe(name) { return `(() => { localStorage.setItem('piik:ui-lang', 'en'); localStorage.setItem('piik:ui-mode', 'text'); const peers = [], sockets = [], captures = [], contexts = [], protocol = [], channels = [], warnings = [], flowStates = [], audioSources = [], qualitySamples = [], restoredVideo = [], trackEvents = []; const trackObjects = new WeakMap(); const trackObjectId = track => { if (!trackObjects.has(track)) trackObjects.set(track, crypto.randomUUID()); return trackObjects.get(track); }; const trackState = track => ({ id: track.id, objectId: trackObjectId(track), kind: track.kind, readyState: track.readyState, enabled: track.enabled, muted: track.muted }); const observedStreams = new WeakSet(); const recordTrack = (pc, event, track, streams, mid) => { trackEvents.push({ time: Date.now(), pcIndex: peers.indexOf(pc), event, mid, track: trackState(track), streamIds: streams.map(stream => stream.id) }); if (trackEvents.length > 128) trackEvents.shift(); }; let qualityInstalled = false; let captureAudio = false; const conversationWS = Object.create(null); const warn = console.warn.bind(console); console.warn = (...args) => { warnings.push(args.map(value => String(value))); if (warnings.length > 20) warnings.shift(); warn(...args); }; const blockedPeers = new Set(), pairPeers = new Map(), flowBindings = new Map(), flowStreams = new Map(), senders = [], channelOwners = new Map(); let videoStall = null; let retainedSource = null; let blockedOffers = 0; const record = (direction, data) => { const event = JSON.parse(data); const command = event.input?.action?.command; const type = command?.type ?? ((event.output?.reply?.result?.type ?? event.result?.type) === 'history' ? 'history' : event.type); const room = command?.roomId ?? event.roomId; if (room && (type === 'chat' || type === 'history')) { const counts = conversationWS[room] ??= { sent: { chat: 0, history: 0 }, received: { chat: 0, history: 0 } }; counts[direction][type]++; } for (const pair of event.state?.pairs ?? []) { pairPeers.set(pair.id, pair.peerId); for (const flow of [...pair.incoming, ...pair.outgoing]) flowBindings.set(flow.id, flow); } if (event.type === 'signal') for (const track of event.payload.tracks ?? []) flowStreams.set(track.flowId, track.streamId); if (direction === 'sent' && event.type === 'flow_state') { flowStates.push({ flowId: event.flowId, state: event.state, time: Date.now() }); if (flowStates.length > 128) flowStates.shift(); } const flowId = event.flowId ?? event.input?.action?.flowId; protocol.push({ time: Date.now(), direction, type:event.type, room:event.roomId, request:event.requestId, flowId, publicationId: event.publicationId ?? command?.publicationId ?? flowBindings.get(flowId)?.publicationId, helperSessionId: event.helperSessionId ?? command?.helperSessionId, now: event.input?.now, nextRouteDeadline: event.output?.projection?.nextRouteDeadline, flows: event.output?.projection?.flows.map(({ id, publicationId, sender, receiver, inputFlowId, selected }) => ({ id, publicationId, sender, receiver, inputFlowId, selected })), action:event.input?.action?.type, revision:event.input?.revision ?? event.output?.revision, binding:event.bindingId ?? event.input?.bindingId ?? event.output?.bindingId, result:event.result?.type ?? event.output?.reply?.result?.type, code:event.code ?? event.result?.code ?? event.output?.reply?.result?.code, pair:event.pairId, ready:event.ready, state: typeof event.state === 'string' ? event.state : event.input?.action?.state }); if (protocol.length > 256) protocol.shift(); return event; }; const NativePC = window.RTCPeerConnection, NativeWS = window.WebSocket; const recordChannel = (pc, channel) => { channels.push(channel); const owned = channelOwners.get(pc) ?? []; owned.push(channel); channelOwners.set(pc, owned); return channel; }; window.RTCPeerConnection = class extends NativePC { constructor(...args) { super(...args); peers.push(this); this.addEventListener('datachannel', event => recordChannel(this, event.channel)); this.addEventListener('track', ({ track, streams, transceiver }) => { recordTrack(this, 'track', track, streams, transceiver.mid); track.addEventListener('ended', () => recordTrack(this, 'ended', track, streams, transceiver.mid), { once: true }); for (const stream of streams) if (!observedStreams.has(stream)) { observedStreams.add(stream); stream.addEventListener('removetrack', event => recordTrack(this, 'removetrack', event.track, [stream], this.getTransceivers().find(value => value.receiver.track === event.track)?.mid ?? null)); } }); } createDataChannel(...args) { return recordChannel(this, super.createDataChannel(...args)); } addTransceiver(track, options) { const transceiver = super.addTransceiver(track, options); senders.push({ pc: this, sender: transceiver.sender, id: crypto.randomUUID(), streams: (options?.streams ?? []).map(stream => stream.id) }); return transceiver; } addTrack(track, ...streams) { const sender = super.addTrack(track, ...streams); senders.push({ pc: this, sender, id: crypto.randomUUID(), streams: streams.map(stream => stream.id) }); return sender; } }; window.WebSocket = class extends NativeWS { constructor(...args) { super(...args); sockets.push(this); this.addEventListener('message', message => { const event = record('received', message.data); if (event.type === 'signal' && blockedPeers.has(event.fromSessionId)) { if (event.payload?.description?.type === 'offer') blockedOffers++; message.stopImmediatePropagation(); } }); } send(data) { const event = record('sent', data); if (event.type === 'signal' && blockedPeers.has(pairPeers.get(event.pairId))) return; const fault = videoStall; const binding = fault && event.type === 'signal' && event.payload?.description && pairPeers.get(event.pairId) === fault.peerId && event.payload.tracks?.find(track => flowBindings.get(track.flowId)?.publicationId === fault.publicationId && (!fault.candidateOnly || flowBindings.get(track.flowId)?.selected === false)); if (binding) { if (!fault.pending) { const output = senders.find(output => output.pc.localDescription?.sdp === event.payload.description.sdp && output.streams.includes(binding.streamId) && output.sender.track?.kind === 'video'); fault.pending = (async () => { if (!output || event.payload.description.type !== 'offer') throw new Error('Video stall requires its initial sender offer'); const parameters = output.sender.getParameters(); if (!parameters.encodings?.length) throw new Error('Video sender has no controllable encoding'); parameters.encodings.forEach(encoding => { encoding.active = false; }); await output.sender.setParameters(parameters); Object.assign(fault, { output, pairId: event.pairId, flowId: binding.flowId, startedAt: Date.now() }); })(); } // Install the real sender fault before its original offer can reach the receiver. void fault.pending.then(() => super.send(data)).catch(error => { fault.error = String(error); }); return; } return super.send(data); } }; const createAudioSource = AudioContext.prototype.createMediaStreamSource; AudioContext.prototype.createMediaStreamSource = function(stream) { const source = createAudioSource.call(this, stream); const track = stream.getAudioTracks()[0]; const owner = { trackId: track.id, trackObjectId: trackObjectId(track), context: this, connected: false }; audioSources.push(owner); const connect = source.connect.bind(source), disconnect = source.disconnect.bind(source); source.connect = (...args) => { owner.connected = true; return connect(...args); }; source.disconnect = (...args) => { owner.connected = false; return disconnect(...args); }; return source; }; const microphone = (stereo = false) => { const context = new AudioContext(); contexts.push(context); const frequencies = stereo ? [500, 1500] : [${330 + name.charCodeAt(0)}]; const channels = context.createChannelMerger(frequencies.length); const oscillators = frequencies.map((frequency, channel) => { const oscillator = context.createOscillator(); oscillator.frequency.value = frequency; oscillator.connect(channels, 0, channel); oscillator.start(); return oscillator; }); const gain = context.createGain(), destination = context.createMediaStreamDestination(); gain.gain.value = .05; channels.connect(gain); gain.connect(destination); const track = destination.stream.getAudioTracks()[0], stop = track.stop.bind(track); track.stop = () => { stop(); oscillators.forEach(oscillator => oscillator.stop()); void context.close(); }; void context.resume(); captures.push(destination.stream); return destination.stream; }; navigator.mediaDevices.getUserMedia = async options => { if (options.video) throw new Error('This gate only substitutes microphone capture'); return microphone(); }; navigator.mediaDevices.getDisplayMedia = async () => { const canvas = document.createElement('canvas'); canvas.width = 640; canvas.height = 360; const ctx = canvas.getContext('2d'); let frame = 0; const timer = setInterval(() => { ctx.fillStyle = 'hsl(' + (frame++ % 360) + ' 70% 40%)'; ctx.fillRect(0, 0, 640, 360); ctx.fillStyle = 'white'; ctx.font = '40px sans-serif'; ctx.fillText('${name} ' + frame, 40, 150); }, 100); const stream = canvas.captureStream(10); captures.push(stream); const track = stream.getVideoTracks()[0], stop = track.stop.bind(track); track.stop = () => { clearInterval(timer); stop(); }; if (captureAudio) for (const audio of microphone(true).getAudioTracks()) stream.addTrack(audio); return stream; }; window.__channelProbe = { peers, sockets, captures, contexts, clearProtocol() { protocol.length = 0; }, captureAudio(enabled) { captureAudio = enabled; }, async screenAudioSeparation(publicationId) { const stream = window.__channelGate.snapshot().streams.get(publicationId); if (!stream?.getAudioTracks().length) throw new Error('No received screen sound'); const context = new AudioContext(); try { const input = context.createMediaStreamSource(stream); const splitter = context.createChannelSplitter(2), sink = context.createGain(); sink.gain.value = 0; sink.connect(context.destination); input.connect(splitter); const analyzers = [0, 1].map(channel => { const analyzer = context.createAnalyser(); analyzer.fftSize = 8192; analyzer.smoothingTimeConstant = 0; splitter.connect(analyzer, channel); analyzer.connect(sink); return analyzer; }); await context.resume(); await new Promise(resolve => setTimeout(resolve, 1000)); return analyzers.map((analyzer, channel) => { const bins = new Float32Array(analyzer.frequencyBinCount); analyzer.getFloatFrequencyData(bins); const level = frequency => { const bin = Math.round(frequency * analyzer.fftSize / context.sampleRate); return Math.max(-120, ...bins.slice(bin - 1, bin + 2)); }; return level(channel === 0 ? 500 : 1500) - level(channel === 0 ? 1500 : 500); }); } finally { await context.close(); } }, async observeQuality() { if (qualityInstalled) return; const { P2pQualityProbe } = await import('/src/client/media/candidate-quality-probe.ts'); const observe = P2pQualityProbe.prototype.observe; const metrics = value => value && Object.fromEntries([ 'trackIdentifier', 'rtpStatsId', 'rtpSsrc', 'sampleTimestampMs', 'sampleWindowMs', 'frameWidth', 'frameHeight', 'framesPerSecond', 'intervalFramesDecoded', 'bitrateKbps', 'intervalFreezeCount', 'intervalFreezeDurationMs', 'intervalPauseCount', 'intervalPauseDurationMs', ].map(key => [key, value[key]])); P2pQualityProbe.prototype.observe = function(current, candidate) { const result = observe.call(this, current, candidate); qualitySamples.push({ time: Date.now(), current: metrics(current), candidate: metrics(candidate), result }); if (qualitySamples.length > 128) qualitySamples.shift(); return result; }; qualityInstalled = true; }, screen(publicationId) { const snapshot = window.__channelGate.snapshot(); const publication = snapshot.state.publications.find(value => value.id === publicationId); const streamIds = [...flowStreams].filter(([flowId]) => flowBindings.get(flowId)?.publicationId === publicationId).map(([, streamId]) => streamId); return { fullscreen: { enabled: document.fullscreenEnabled, hasFocus: document.hasFocus(), element: document.fullscreenElement && { tag: document.fullscreenElement.tagName, className: document.fullscreenElement.getAttribute('class') }, userActivation: navigator.userActivation && { active: navigator.userActivation.isActive, hasBeenActive: navigator.userActivation.hasBeenActive } }, publication: publication && { id: publication.id, roomId: publication.roomId, sessionId: publication.sessionId, kind: publication.kind, paused: publication.paused }, streamId: snapshot.streams.get(publicationId)?.id, inputs: (snapshot.screenInputs.get(publicationId) ?? []).map(input => { const element = [...document.querySelectorAll('video')].find(video => video.dataset.flowId === input.flowId); if (element) element.__allocationIdentity ??= crypto.randomUUID(); const rect = element?.getBoundingClientRect(); const hit = rect && document.elementFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2); const buttons = element?.parentElement?.querySelectorAll('.c3-mini-controls button'); const fullscreenControl = buttons?.item(buttons.length - 1); const pair = snapshot.state.pairs.find(pair => pair.incoming.some(flow => flow.id === input.flowId)); const flow = pair?.incoming.find(flow => flow.id === input.flowId); return { flowId: input.flowId, selected: input.selected, switchReady: input.switchReady, streamId: input.stream.id, pairId: pair?.id, peerId: pair?.peerId, inputFlowId: flow?.inputFlowId, active: input.stream.active, tracks: input.stream.getTracks().map(track => ({ ...trackState(track), receivers: peers.flatMap((pc, pcIndex) => pc.getTransceivers().filter(value => value.receiver.track === track) .map(value => ({ pcIndex, connectionState: pc.connectionState, mid: value.mid, direction: value.direction, currentDirection: value.currentDirection }))) })), audio: input.stream.getAudioTracks().map(track => track.id), audioObjects: input.stream.getAudioTracks().map(trackObjectId), video: input.stream.getVideoTracks().map(track => track.id), elementId: element?.__allocationIdentity, displayed: element?.dataset.selected, composited: element?.dataset.composited, muted: element?.muted, paused: element?.paused, fullscreen: !!element && document.fullscreenElement === element.parentElement, rect: rect && { x: rect.x, y: rect.y, width: rect.width, height: rect.height }, centerHit: hit && { tag: hit.tagName, className: hit.getAttribute('class'), isVideo: hit === element, buttonLabel: hit.closest('button')?.getAttribute('aria-label') }, fullscreenControl: fullscreenControl && { label: fullscreenControl.getAttribute('aria-label'), disabled: fullscreenControl.disabled || fullscreenControl.getAttribute('aria-disabled') === 'true' }, volume: element?.volume, width: element?.videoWidth, height: element?.videoHeight, frames: element?.getVideoPlaybackQuality().totalVideoFrames, time: element?.currentTime }; }), outputs: snapshot.state.pairs.flatMap(pair => pair.outgoing.filter(flow => flow.publicationId === publicationId).map(flow => { const streamId = flowStreams.get(flow.id); const owner = window.__channelGate.session.media.peers.get(pair.id)?.transport.sending.get(flow.id); return { flowId: flow.id, inputFlowId: flow.inputFlowId, selected: flow.selected, streamId, wrapperId: owner?.stream.id, pairId: pair.id, peerId: pair.peerId, senders: senders.filter(output => streamId && output.streams.includes(streamId) && output.sender.track) .map(output => { const transceiver = output.pc.getTransceivers().find(value => value.sender === output.sender); const binding = owner?.senders.find(binding => binding.sender === output.sender); return { id: output.id, trackId: output.sender.track.id, kind: output.sender.track.kind, track: trackState(output.sender.track), source: binding && trackState(binding.source), pcIndex: peers.indexOf(output.pc), connectionState: output.pc.connectionState, mid: transceiver?.mid, direction: transceiver?.direction, currentDirection: transceiver?.currentDirection }; }) }; })), flowStates, qualitySamples, restoredVideo, trackEvents: trackEvents.filter(event => event.streamIds.some(id => streamIds.includes(id))), audioOwners: audioSources.filter(owner => owner.connected && owner.context.state === 'running').map(owner => owner.trackObjectId) }; }, async decodedVideo(publicationId, flowId) { const track = window.__channelGate.snapshot().screenInputs.get(publicationId) ?.find(input => input.flowId === flowId)?.stream.getVideoTracks()[0]; const receiver = track && peers.filter(pc => pc.connectionState === 'connected') .flatMap(pc => pc.getReceivers()).find(receiver => receiver.track === track); if (!receiver) return null; const report = [...(await receiver.getStats()).values()] .find(report => report.type === 'inbound-rtp' && report.kind === 'video' && Number.isFinite(report.framesDecoded)); return report && { id: report.id, frames: report.framesDecoded, timestamp: report.timestamp, width: report.frameWidth, height: report.frameHeight }; }, async sourceStats(publicationId, retainPeerId) { const rows = [], incoming = [], statsErrors = []; let outputs = []; const snapshot = window.__channelGate.snapshot(); for (const pair of snapshot.state.pairs) { for (const flow of pair.incoming.filter(flow => flow.publicationId === publicationId)) { const input = snapshot.screenInputs.get(publicationId)?.find(input => input.flowId === flow.id); for (const track of input?.stream.getTracks() ?? []) { const pc = peers.find(pc => pc.connectionState === 'connected' && pc.getReceivers().some(receiver => receiver.track === track)); const receiver = pc?.getReceivers().find(receiver => receiver.track === track); if (!receiver) continue; const results = await Promise.allSettled([Promise.resolve().then(() => receiver.getStats()), Promise.resolve().then(() => pc.getStats())]); results.forEach((result, index) => { if (result.status === 'rejected') statsErrors.push({ flowId: flow.id, api: ['receiver.getStats', 'pc.getStats'][index], error: String(result.reason), connectionState: pc.connectionState }); }); const scoped = results[0].status === 'fulfilled' ? results[0].value : new Map(); const all = results[1].status === 'fulfilled' ? results[1].value : new Map(); for (const report of scoped.values()) if (report.type === 'inbound-rtp') incoming.push({ flowId: flow.id, pairId: pair.id, peerId: pair.peerId, inputFlowId: flow.inputFlowId, selected: flow.selected, streamId: input.stream.id, track: trackState(track), pcIndex: peers.indexOf(pc), statsId: report.id, timestamp: report.timestamp, mid: report.mid ?? null, ssrc: report.ssrc ?? null, kind: report.kind, codec: all.get(report.codecId)?.mimeType ?? null, packetsReceived: report.packetsReceived ?? null, bytesReceived: report.bytesReceived ?? null, packetsLost: report.packetsLost ?? null, framesReceived: report.framesReceived ?? null, framesDecoded: report.framesDecoded ?? null, keyFramesDecoded: report.keyFramesDecoded ?? null }); } } for (const flow of pair.outgoing.filter(flow => flow.publicationId === publicationId)) { const streamId = flowStreams.get(flow.id); for (const output of senders.filter(output => output.pc.connectionState === 'connected' && output.streams.includes(streamId) && output.sender.track?.kind === 'video')) { outputs.push({ publicationId, pair, flow, streamId, output, track: output.sender.track, statsIds: [] }); } } } if (retainPeerId !== undefined) { if (retainedSource?.publicationId !== publicationId || retainedSource?.pair.peerId !== retainPeerId) retainedSource = outputs.find(value => value.pair.peerId === retainPeerId && value.flow.selected) ?? null; outputs = retainedSource ? [retainedSource] : []; } for (const retained of outputs) { const { pair, flow, streamId, output, track, statsIds } = retained; const results = await Promise.allSettled([Promise.resolve().then(() => output.sender.getStats()), Promise.resolve().then(() => output.pc.getStats()), Promise.resolve().then(() => output.sender.getParameters())]); results.forEach((result, index) => { if (result.status === 'rejected') statsErrors.push({ flowId: flow.id, senderId: output.id, api: ['sender.getStats', 'pc.getStats', 'sender.getParameters'][index], error: String(result.reason), connectionState: output.pc.connectionState }); }); const scoped = results[0].status === 'fulfilled' ? results[0].value : new Map(); const all = results[1].status === 'fulfilled' ? results[1].value : new Map(); const parameters = results[2].status === 'fulfilled' ? results[2].value : null; const encodings = parameters?.encodings.map(({ rid, active, scaleResolutionDownBy, maxBitrate, maxFramerate }) => ({ rid: rid ?? null, active: active ?? null, scaleResolutionDownBy: scaleResolutionDownBy ?? null, maxBitrate: maxBitrate ?? null, maxFramerate: maxFramerate ?? null })) ?? null; const extensions = parameters?.headerExtensions.map(extension => extension.uri); const headerExtensions = extensions ? { transportWideCc: extensions.some(uri => uri.includes('transport-wide-cc')), mid: extensions.includes('urn:ietf:params:rtp-hdrext:sdes:mid'), rid: extensions.includes('urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id'), repairedRid: extensions.includes('urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id'), } : null; for (const report of scoped.values()) if (report.type === 'outbound-rtp' && report.kind === 'video' && output.sender.track === track && !statsIds.includes(report.id)) statsIds.push(report.id); for (const id of statsIds) { const report = all.get(id); if (!report) continue; const transport = all.get(report.transportId); const selectedPair = all.get(transport?.selectedCandidatePairId); const remote = all.get(report.remoteId), source = all.get(report.mediaSourceId); rows.push({ flowId: flow.id, pairId: pair.id, peerId: pair.peerId, selected: flow.selected, inputFlowId: flow.inputFlowId, streamId, track: trackState(track), senderTrack: output.sender.track ? trackState(output.sender.track) : null, pcIndex: peers.indexOf(output.pc), connectionState: output.pc.connectionState, iceConnectionState: output.pc.iceConnectionState, dtlsState: transport?.dtlsState ?? null, retired: !snapshot.state.pairs.some(pair => pair.outgoing.some(value => value.id === flow.id)), senderId: output.id, statsId: report.id, timestamp: report.timestamp, mid: report.mid ?? null, ssrc: report.ssrc ?? null, codec: all.get(report.codecId)?.mimeType ?? null, encodings, headerExtensions, rid: report.rid ?? null, framesEncoded: report.framesEncoded ?? null, framesSent: report.framesSent ?? null, targetBitrate: report.targetBitrate ?? null, frameWidth: report.frameWidth ?? null, frameHeight: report.frameHeight ?? null, bytesSent: report.bytesSent ?? null, packetsSent: report.packetsSent ?? null, retransmittedPacketsSent: report.retransmittedPacketsSent ?? null, retransmittedBytesSent: report.retransmittedBytesSent ?? null, qualityLimitationReason: report.qualityLimitationReason ?? null, encoderImplementation: report.encoderImplementation ?? null, keyFramesEncoded: report.keyFramesEncoded ?? null, pliCount: report.pliCount ?? null, nackCount: report.nackCount ?? null, availableOutgoingBitrate: selectedPair?.availableOutgoingBitrate ?? null, remoteInbound: remote ? { packetsLost: remote.packetsLost ?? null, fractionLost: remote.fractionLost ?? null, jitter: remote.jitter ?? null, roundTripTime: remote.roundTripTime ?? null, totalRoundTripTime: remote.totalRoundTripTime ?? null, roundTripTimeMeasurements: remote.roundTripTimeMeasurements ?? null, reportsReceived: remote.reportsReceived ?? null } : null, selectedPair: selectedPair ? { currentRoundTripTime: selectedPair.currentRoundTripTime ?? null, totalRoundTripTime: selectedPair.totalRoundTripTime ?? null, packetsSent: selectedPair.packetsSent ?? null, packetsReceived: selectedPair.packetsReceived ?? null, bytesSent: selectedPair.bytesSent ?? null, bytesReceived: selectedPair.bytesReceived ?? null } : null, mediaSource: source ? { trackIdentifier: source.trackIdentifier ?? null, frames: source.frames ?? null, framesPerSecond: source.framesPerSecond ?? null, width: source.width ?? null, height: source.height ?? null } : null }); } } return { sourceCollectedTime: Date.now(), rows, incoming, statsErrors, retained: retainPeerId !== undefined && retainedSource ? { flowId: retainedSource.flow.id, senderId: retainedSource.output.id, statsIds: retainedSource.statsIds, track: trackState(retainedSource.track), connectionState: retainedSource.output.pc.connectionState, missingStatsIds: retainedSource.statsIds.filter(id => !rows.some(row => row.statsId === id)) } : null }; }, blockPeer(sessionId) { blockedPeers.add(sessionId); }, unblockPeer(sessionId) { blockedPeers.delete(sessionId); }, async limitVideo(publicationId, peerId, maxFramerate, scaleResolutionDownBy) { const session = window.__channelGate.session; const pair = window.__channelGate.snapshot().state.pairs.find(pair => pair.peerId === peerId); const flow = pair?.outgoing.find(flow => flow.publicationId === publicationId && flow.selected); const transport = pair && session.media.peers.get(pair.id)?.transport; const owner = flow && transport?.sending.get(flow.id); const video = owner?.senders.find(binding => binding.source.kind === 'video'); const streamId = flow && flowStreams.get(flow.id); const output = video && senders.find(output => output.sender === video.sender); if (!output || output.pc.connectionState !== 'connected' || !owner.profile) return null; const settings = () => { const value = video.source.getSettings(); return { width: value.width, height: value.height, frameRate: value.frameRate }; }; const others = () => [...session.media.peers.values()].flatMap(peer => [...peer.transport.sending] .filter(([id, value]) => id !== flow.id && value.kind === 'screen' && value.profile) .map(([flowId, value]) => ({ flowId, profile: structuredClone(value.profile) }))); const before = { settings: settings(), profile: structuredClone(owner.profile), others: others() }; if (!before.settings.width || !before.settings.height) throw new Error('Controlled source has no capture dimensions'); const resolution = { width: Math.floor(before.settings.width / scaleResolutionDownBy), height: Math.floor(before.settings.height / scaleResolutionDownBy) }; const profile = { ...owner.profile, resolution, maxFramerate }; const setOutgoing = transport.setOutgoing; transport.setOutgoing = function(streams, resourcesReleased) { if (!streams.has(flow.id)) { this.setOutgoing = setOutgoing; return setOutgoing.call(this, streams, resourcesReleased); } const controlled = new Map(streams), value = controlled.get(flow.id); controlled.set(flow.id, { ...value, profile: { ...value.profile, resolution, maxFramerate } }); return setOutgoing.call(this, controlled, resourcesReleased); }; await transport.configureVideo(flow.id, profile); return { flowId: flow.id, pairId: pair.id, streamId, senderId: output.id, before, after: { settings: settings(), profile: structuredClone(owner.profile), others: others() } }; }, stallVideo(publicationId, peerId, candidateOnly = false) { videoStall = { publicationId, peerId, candidateOnly }; }, async restoreVideo() { const fault = videoStall; videoStall = null; await fault?.pending?.catch(() => {}); if (!fault?.output?.sender.track || fault.output.pc.connectionState === 'closed') return; const parameters = fault.output.sender.getParameters(); parameters.encodings.forEach(encoding => { encoding.active = true; }); await fault.output.sender.setParameters(parameters); restoredVideo.push({ publicationId: fault.publicationId, peerId: fault.peerId, flowId: fault.flowId, trackId: fault.output.sender.track.id, restoredAt: Date.now(), active: fault.output.sender.getParameters().encodings.map(encoding => encoding.active) }); }, closeDataChannels() { for (const channel of channels) channel.close(); }, async sample() { const inbound = [], outbound = []; for (const pc of peers) if (pc.connectionState === 'connected') { const report = await pc.getStats(); const identity = stat => ({ pc: peers.indexOf(pc), id: stat.id, mid: stat.mid, ssrc: stat.ssrc, codec: report.get(stat.codecId)?.mimeType }); for (const stat of report.values()) { if (stat.type === 'inbound-rtp') inbound.push({ ...identity(stat), kind: stat.kind, bytes: stat.bytesReceived, frames: stat.framesDecoded, framesReceived: stat.framesReceived, packets: stat.packetsReceived, keys: stat.keyFramesDecoded, plis: stat.pliCount, nacks: stat.nackCount, samples: stat.totalSamplesReceived, energy: stat.totalAudioEnergy }); if (stat.type === 'outbound-rtp') outbound.push({ ...identity(stat), kind: stat.kind, bytes: stat.bytesSent, frames: stat.framesEncoded, packets: stat.packetsSent, keys: stat.keyFramesEncoded }); } } const fault = videoStall, stalled = fault?.output; let stalledBytes = 0; if (stalled?.sender.track) for (const report of (await stalled.sender.getStats()).values()) { if (report.type === 'outbound-rtp' && report.kind === 'video') stalledBytes += report.bytesSent ?? 0; } return { inbound, outbound, protocol, conversationWS, warnings, blockedOffers, qualitySamples, restoredVideo, videoStall: fault && { publicationId: fault.publicationId, peerId: fault.peerId, pairId: fault.pairId, flowId: fault.flowId, startedAt: fault.startedAt, error: fault.error, bytes: stalledBytes, connected: stalled?.pc.connectionState === 'connected', roomOpen: stalled && channelOwners.get(stalled.pc)?.some(channel => channel.label === 'piik-room' && channel.readyState === 'open'), active: stalled?.sender.getParameters().encodings.map(encoding => encoding.active) }, connections: peers.map(pc => pc.connectionState), channels: channels.map(channel => ({ label: channel.label, state: channel.readyState })), videos: [...document.querySelectorAll('video')].map(video => ({ time: video.currentTime, frames: video.getVideoPlaybackQuality().totalVideoFrames, width: video.videoWidth, paused: video.paused })), captures: captures.flatMap(stream => stream.getTracks().map(track => ({ kind: track.kind, state: track.readyState }))), overflow: document.documentElement.scrollWidth > innerWidth, text: document.body.innerText.slice(-1800) }; } }; })()`; } const hook = "window.__channelGate"; const action = async (index, expression) => { try { return await evaluate(index, `${hook}.session.${expression}`); } catch (cause) { throw new Error(`Browser ${index} ${expression.split('(')[0]} failed`, { cause }); } }; const view = index => evaluate(index, `(() => { const value = ${hook}?.snapshot(); if (!value) return { connection: 'connecting', state: null }; return { sessionId: value.sessionId, connection: value.connection, self: value.self, state: value.state && { channel: value.state.channel, publications: value.state.publications, subscriptions: value.state.subscriptions, pairs: value.state.pairs, voice: value.state.voice, files: value.state.files }, streams: [...value.streams.keys()], error: value.error }; })()`); const picture = async (index, publicationId, observe) => { const value = await evaluate(index, `(() => { const stream = ${hook}.snapshot().streams.get(${JSON.stringify(publicationId)}); const video = [...document.querySelectorAll('video')].find(element => element.srcObject === stream); return { received: !!stream, frames: video?.getVideoPlaybackQuality().totalVideoFrames ?? 0, time: video?.currentTime ?? 0, live: stream?.getVideoTracks().some(track => track.readyState === 'live') ?? false }; })()`); await observe?.(value); return value; }; async function playing(index, publicationId, observe) { return until(`browser ${index} decodes ${publicationId}`, () => picture(index, publicationId, observe), sample => sample.live && sample.frames > 0); } async function progressing(index, publicationId, observe, minimumFrames = 1) { let before = await playing(index, publicationId, observe); return until(`browser ${index} advances ${publicationId}`, () => picture(index, publicationId, observe), sample => { const advanced = sample.live && sample.frames >= before.frames + minimumFrames && sample.time > before.time; // A repaired route can replace the player and reset its frame/time counters. if (sample.frames < before.frames || sample.time < before.time) before = sample; return advanced; }); } const publishScreen = (index, roomId) => action(index, `publish('screen', ${JSON.stringify(roomId)}, await navigator.mediaDevices.getDisplayMedia({video:true}))`); async function capturedScreen(index, roomId) { await action(index, `startScreen(${JSON.stringify(roomId)})`); const current = await until("captured screen is admitted", () => view(index), value => value.state.publications.some(publication => publication.sessionId === value.sessionId && publication.roomId === roomId && publication.kind === "screen")); return current.state.publications.find(publication => publication.sessionId === current.sessionId && publication.roomId === roomId && publication.kind === "screen").id; } const sample = index => evaluate(index, "window.__channelProbe.sample()"); async function audioProgressing(index, publicationId) { let previous = null; return until(`browser ${index} receives advancing audio ${publicationId}`, () => evaluate(index, `(async () => { const current = () => ${hook}.snapshot().streams.get(${JSON.stringify(publicationId)}); const track = current()?.getAudioTracks().find(track => track.readyState === 'live'); if (!track) return null; const pc = window.__channelProbe.peers.find(pc => pc.connectionState === 'connected' && pc.getReceivers().some(receiver => receiver.track === track)); const receiver = pc?.getReceivers().find(receiver => receiver.track === track); if (!receiver) return null; const stats = await receiver.getStats(); if (pc.connectionState !== 'connected' || !current()?.getAudioTracks().includes(track)) return null; return { trackId: track.id, reports: [...stats.values()] .filter(report => report.type === 'inbound-rtp' && report.kind === 'audio' && report.isRemote !== true) .map(report => ({ id: report.id, samples: report.totalSamplesReceived, energy: report.totalAudioEnergy })) }; })()`), current => { const advancing = current && previous?.trackId === current.trackId && current.reports.some(report => { const before = previous.reports.find(before => before.id === report.id); return before && Number.isFinite(report.samples) && Number.isFinite(report.energy) && report.samples > before.samples && report.energy > before.energy; }); previous = current; return advancing; }); } async function assertNoError(index, flow) { const current = await view(index); if (current.error === null) return; assert.fail(`${flow} left an unexpected error in browser ${index}: ${JSON.stringify({ error: current.error, protocol: (await sample(index)).protocol, })}`); } return { evaluate, until, captureProbe, hook, action, view, picture, playing, progressing, publishScreen, capturedScreen, sample, audioProgressing, assertNoError }; }