Files
VoelinClient/tests/interop/browser-peer.cjs
T
Claude e050148a32 Rename the project to Voelin; packages hold only the app, tsgw is a Linux server package
The product is Voelin (app id io.github.faumaray.Voelin), everywhere:
- crates tsc-* -> voelin-* (and voelin-fuzz), the desktop binary
  tsc-desktop -> voelin, the development CLI tsctl -> voelinctl;
- the Android app: applicationId, Kotlin package io.github.faumaray.voelin,
  VoelinApp, libvoelin_android.so (JNI class names follow);
- TSC_* environment variables -> VOELIN_*, Gradle properties tsc.* ->
  voelin.*, signing secrets VOELIN_SIGNING_*;
- the data/config/cache/state folder tsc -> voelin, the keyring service,
  thread names, the dev compose project;
- desktop file, metainfo, icon, Flatpak manifest, NSIS installer, docs.
The gateway keeps its name: bin tsgw (crate voelin-gateway), TSGW_* env
vars, the tsgw.v1+json protocol, the tsgw image. The window title and the
About page show "Voelin <version>" from voelin_platform::APP_NAME.

Packages (scripts/package.sh): the app only (voelin, voelin.exe, APKs,
Flatpak). voelinctl is a development tool and in no package. tsgw is for
servers and now its own Linux package, tsgw_<v>_amd64.deb and
tsgw-<v>-linux-x86_64.tar.gz, with a sandboxed systemd unit
(packaging/linux/tsgw.service; /etc/tsgw config, /var/lib/tsgw state) and
dependencies of its own (no GUI libraries). docs/gateway-admin.md has an
Install section.

Fixes:
- package-windows: makensis on Windows found no file through "D:/a/..."
  paths; pass Windows paths (cygpath -w).
- StartupWMClass is the app id, which the app sets as its X11 WM_CLASS
  (it was the binary name, so desktops could not match the window).
- Gradle wrapper download timeout 10 s -> 60 s (slow proxies).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-26 21:11:17 +00:00

199 lines
6.7 KiB
JavaScript

// Headless Chromium as the remote WebRTC peer for
// crates/voelin-stream/tests/browser_interop.rs (see README.md here).
//
// Reads one JSON command per line on stdin and writes one JSON reply per line
// on stdout; logs go to stderr. Commands (`op`):
// answer {sdp} answer an offer, wait for ICE gathering -> {sdp}
// offer {codec, trickle} send canvas video + oscillator audio; offer with
// `codec` preferred -> {sdp} or {unsupported};
// with `trickle` the SDP has no candidates
// candidates candidates gathered so far -> {candidates: [{candidate, sdpMid, sdpMLineIndex}]}
// accept {sdp} apply the answer to our offer
// candidate {candidate, sdpMid, sdpMLineIndex} add a remote candidate
// stats connection state, inbound/outbound RTP counters,
// frames shown by a <video> element
// quit
'use strict';
const readline = require('node:readline');
const { chromium } = require('playwright');
// Runs in the page.
const PAGE_SCRIPT = `
function gathered(pc, ms) {
return new Promise((resolve) => {
if (pc.iceGatheringState === 'complete') return resolve();
pc.addEventListener('icegatheringstatechange', () => {
if (pc.iceGatheringState === 'complete') resolve();
});
setTimeout(resolve, ms);
});
}
window.peer = {
pc: null,
timers: [],
shownFrames: 0,
localCandidates: [],
reset() {
if (this.pc) this.pc.close();
this.timers.forEach(clearInterval);
this.timers = [];
this.shownFrames = 0;
this.localCandidates = [];
this.pc = new RTCPeerConnection();
this.pc.addEventListener('icecandidate', (e) => {
if (e.candidate && e.candidate.candidate) this.localCandidates.push(e.candidate.toJSON());
});
this.pc.addEventListener('connectionstatechange', () => {
console.log('connection ' + this.pc.connectionState);
});
return this.pc;
},
// Count frames a <video> element actually presents (needs decodable video).
show(track) {
const video = document.createElement('video');
video.muted = true;
video.autoplay = true;
video.srcObject = new MediaStream([track]);
document.body.appendChild(video);
const count = () => {
this.shownFrames++;
video.requestVideoFrameCallback(count);
};
video.requestVideoFrameCallback(count);
video.play().catch((e) => console.log('play: ' + e));
},
async answer({ sdp }) {
const pc = this.reset();
pc.addEventListener('track', (e) => {
if (e.track.kind === 'video') this.show(e.track);
});
await pc.setRemoteDescription({ type: 'offer', sdp });
await pc.setLocalDescription(await pc.createAnswer());
await gathered(pc, 3000);
return { sdp: pc.localDescription.sdp };
},
async offer({ codec, trickle }) {
const pc = this.reset();
const canvas = document.createElement('canvas');
canvas.width = 320;
canvas.height = 240;
const ctx = canvas.getContext('2d');
let n = 0;
this.timers.push(setInterval(() => {
n++;
ctx.fillStyle = 'hsl(' + ((n * 7) % 360) + ',60%,50%)';
ctx.fillRect(0, 0, 320, 240);
ctx.fillStyle = '#fff';
ctx.fillText(String(n), 10 + (n % 200), 20 + (n % 150));
}, 33));
const video = canvas.captureStream(30).getVideoTracks()[0];
const audioCtx = new AudioContext();
const osc = audioCtx.createOscillator();
const dest = audioCtx.createMediaStreamDestination();
osc.connect(dest);
osc.start();
const audio = dest.stream.getAudioTracks()[0];
const stream = new MediaStream([video, audio]);
const transceiver = pc.addTransceiver(video, { direction: 'sendonly', streams: [stream] });
pc.addTransceiver(audio, { direction: 'sendonly', streams: [stream] });
if (codec) {
const all = RTCRtpSender.getCapabilities('video').codecs;
const wanted = all.filter((c) => c.mimeType.toLowerCase() === 'video/' + codec.toLowerCase());
if (wanted.length === 0) return { unsupported: codec };
transceiver.setCodecPreferences([...wanted, ...all.filter((c) => !wanted.includes(c))]);
}
await pc.setLocalDescription(await pc.createOffer());
if (!trickle) await gathered(pc, 3000);
const sdp = trickle ? pc.localDescription.sdp.split('\\r\\n').filter((l) => !l.startsWith('a=candidate')).join('\\r\\n') : pc.localDescription.sdp;
return { sdp };
},
async candidates() {
await gathered(this.pc, 3000);
return { candidates: this.localCandidates };
},
async accept({ sdp }) {
await this.pc.setRemoteDescription({ type: 'answer', sdp });
return { ok: true };
},
async candidate({ candidate, sdpMid, sdpMLineIndex }) {
await this.pc.addIceCandidate({ candidate, sdpMid, sdpMLineIndex });
return { ok: true };
},
async stats() {
const report = await this.pc.getStats();
const codecs = {};
const inbound = {};
const outbound = {};
report.forEach((s) => {
if (s.type === 'codec') codecs[s.id] = s.mimeType;
if (s.type === 'inbound-rtp') inbound[s.kind] = s;
if (s.type === 'outbound-rtp') outbound[s.kind] = s;
});
const pick = (s, keys) => {
const o = { codec: codecs[s.codecId] };
keys.forEach((k) => (o[k] = s[k]));
return o;
};
const out = { connectionState: this.pc.connectionState, shownFrames: this.shownFrames, inbound: {}, outbound: {} };
for (const k in inbound) {
out.inbound[k] = pick(inbound[k], ['packetsReceived', 'bytesReceived', 'packetsLost', 'framesReceived', 'framesDecoded', 'keyFramesDecoded', 'pliCount', 'totalSamplesReceived']);
}
for (const k in outbound) {
out.outbound[k] = pick(outbound[k], ['packetsSent', 'bytesSent', 'framesEncoded', 'keyFramesEncoded', 'pliCount']);
}
return out;
},
async quit() {
if (this.pc) this.pc.close();
return { ok: true };
},
};
`;
async function main() {
const browser = await chromium.launch({
headless: true,
args: [
// Real host candidates instead of mDNS names (no getUserMedia permission here).
'--disable-features=WebRtcHideLocalIpsWithMdns',
'--autoplay-policy=no-user-gesture-required',
],
});
const page = await browser.newPage();
page.on('console', (m) => process.stderr.write('[page] ' + m.text() + '\n'));
await page.setContent('<!doctype html><html><body></body></html>');
await page.addScriptTag({ content: PAGE_SCRIPT });
process.stdout.write(JSON.stringify({ ready: true, userAgent: await page.evaluate(() => navigator.userAgent) }) + '\n');
const lines = readline.createInterface({ input: process.stdin });
for await (const line of lines) {
if (!line.trim()) continue;
const cmd = JSON.parse(line);
let reply;
try {
reply = await page.evaluate(([op, arg]) => window.peer[op](arg), [cmd.op, cmd]);
} catch (e) {
reply = { error: String(e) };
}
process.stdout.write(JSON.stringify(reply) + '\n');
if (cmd.op === 'quit') break;
}
await browser.close();
}
main().catch((e) => {
process.stderr.write(String(e && e.stack ? e.stack : e) + '\n');
process.exit(1);
});