Files
Piik/scripts/app-browser-fanout-gate.ts
T
AAA cdd503a98d feat: improve feedback and App lifecycle reliability (#404)
Waiting feedback and tooltips now share consistent presentation lifecycles, while App shutdown and native-media operation failures stay within their owning session or output. This phase also consolidates the project standards and publishes searchable English/Chinese reader documentation alongside the homepage.

Validation: repository hygiene and Markdown links; TypeScript, 971 Web tests and production build; website/documentation build; Go tests, vet and Windows/Linux builds with Windows capture checks; desktop/mobile UI and documentation review. Linux race, container and three-platform packaging checks run in the release pipeline. Physical macOS/Linux capture and broader network coverage remain outside this acceptance.

The owner approved v1.4.0 publication. Existing empty-source, Windows URL-handler and public-invitation timeout reports remain tracked; this release makes no claim to resolve those device/network reports. The Windows capture-border request is next-phase work.

## Release notes

### 中文

这是一次以稳定性和交互一致性为重点的功能更新。等待与提示更有反馈,App 启动、原生分享和连接恢复的边界处理得到改善,使用与部署指南也有了统一的文档中心。

#### 新增与改进

- 欢迎语、等待短句和六类可轮换的标签页标题共用一致的显示规则。等待短句从一开始就出现,并在停留时轮换;明确的错误与操作提示保持固定。
- 房间准入等漫画采用更克制的小人物与循环动效。改进 tooltip 的定位与指向,减少对播放器的遮挡;状态、图标和文案保持相同含义。
- 新增中英文文档中心,提供搜索、手机目录、使用教程、问题排查与自部署指南。官网 FAQ 聚焦开始使用前的问题,详细排查统一进入文档中心。

#### 修复

- 使用 `--local` 启动不再清空已保存的 Piik 站点地址。
- 打开系统浏览器不再阻塞 App 的退出流程;改进临时公网邀请、终端和诊断导出在关闭时的清理及错误反馈。
- 单条原生媒体连接的操作失败不再被误判为整个会话的协议错误;重复停止分享保持幂等。
- 修复连接替换、转发停止和异常退出时的部分资源清理问题,避免迟到结果影响当前连接。
- Windows 原生采集中,单个输出的编码错误只结束对应输出;共享采集源本身的错误仍会结束分享。

#### 升级与已知边界

本次保持现有协议版本与配置格式。更新 App 或自托管 Server 后,重新打开页面即可使用对应的新界面。

个别设备的空窗口列表、Windows 浏览器启动异常,以及 Linux 编码输出的故障隔离仍在跟进。macOS/Linux 的物理采集和更广泛的公网网络场景仍需实机验证;本次不宣称这些问题已经解决,也不承诺所有网络都能直连。

### English

This feature update focuses on stability and consistent feedback. Waiting states are easier to follow, App startup and native sharing handle several failure cases more reliably, and the new documentation center brings usage and deployment guides together.

#### New and improved

- Welcome lines, waiting captions and six groups of rotating tab titles follow one presentation lifecycle. Waiting captions appear immediately and rotate while visible; actionable errors and instructions remain fixed.
- Room-admission comics use restrained characters and looping motion. Tooltips point more accurately to their controls and interfere less with the player, with consistent meanings across illustrations, icons and copy.
- A new English/Chinese documentation center includes search, mobile navigation, usage guides, troubleshooting and self-hosting. The homepage FAQ covers questions before getting started and links to the documentation for detailed help.

#### Fixes

- Starting with `--local` no longer clears a saved Piik site address.
- Opening the system browser no longer blocks App shutdown. Temporary public invitations, the terminal and diagnostic exports handle cleanup and exit errors more consistently.
- An operation failure on one native media connection no longer becomes a protocol failure for the entire session. Repeated stop-sharing requests are safe.
- Fixed cleanup cases during connection replacement, relay shutdown and unexpected exits, preventing late results from affecting the current connection.
- On Windows, an encoder failure for one native output stops that output only. A failure of the shared capture source still ends sharing.

#### Updating and known limits

Protocol versions and configuration formats are unchanged. After updating the App or your self-hosted Server, reopen the page to use its updated interface.

Empty source lists on some devices, Windows browser-launch failures and Linux output-failure isolation remain under investigation. Physical macOS/Linux capture and broader public-network coverage still need device testing; this update does not claim to resolve those cases or guarantee a direct connection on every network.
2026-09-16 13:13:06 +08:00

298 lines
17 KiB
TypeScript

import { spawn, spawnSync, type ChildProcessWithoutNullStreams } from "node:child_process";
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import {
CdpConnection, cleanupRun, createPage, evaluate, fetchJsonBefore, launchChrome, reservePort,
waitForSample, waitForVersion, type PageHandle,
} from "./browser-gate-harness";
import { decodeAppEndpoint } from "./app-gate-endpoint";
const ROOT = resolve(import.meta.dirname, "..");
const BUILD_ROOT = join(ROOT, "build/go-check");
// A documentation-only LAN address: it makes the room server publish a LAN
// invitation the gate rewrites to its loopback origins, exactly as the packaged
// App does on a real network.
const LAN_ADDRESS = "192.0.2.1";
const initialAudio = process.env.PIIK_CLIENT_GATE_AUDIO !== "false";
const probe = String.raw`(() => {
localStorage.setItem('piik:ui-lang', 'en');
localStorage.setItem('piik:ui-mode', 'text');
const pcs = [], requests = [], sockets = [], errors = [];
window.addEventListener('unhandledrejection', (e) => errors.push(String(e.reason)));
const codecPreferences = RTCRtpTransceiver.prototype.setCodecPreferences;
RTCRtpTransceiver.prototype.setCodecPreferences = function(value) {
try { return codecPreferences.call(this,value); }
catch (error) { errors.push(String(error)); throw error; }
};
const PC = RTCPeerConnection, WS = WebSocket;
window.RTCPeerConnection = class extends PC { constructor(...args) { super(...args); pcs.push(this); } };
window.WebSocket = class extends WS {
constructor(...args) { super(...args); if (String(args[0]).includes('/control')) {
sockets.push(this);
} }
send(raw) { if (this.url.includes('/control')) { const value = JSON.parse(raw); requests.push(value.type); } super.send(raw); }
};
let audio = ${initialAudio}, captures = 0;
navigator.mediaDevices.getDisplayMedia = async () => {
captures++;
const canvas = document.createElement('canvas'); canvas.width = 1920; canvas.height = 1080;
const context = canvas.getContext('2d'); let frame = 0;
const draw = () => { context.fillStyle = '#14242a'; context.fillRect(0,0,1920,1080);
context.fillStyle = '#68dab3'; context.fillRect((frame++ * 17)%1800,100,120,400);
context.fillStyle = '#ffffff'; context.font = '80px sans-serif'; context.fillText(String(frame),100,700); };
draw(); const timer = setInterval(draw, 1000/30);
const stream = canvas.captureStream(30);
const track = stream.getVideoTracks()[0]; const stop = track.stop.bind(track);
track.stop = () => { clearInterval(timer); stop(); };
if (audio) {
const ac = new AudioContext(), osc = ac.createOscillator(), dest = ac.createMediaStreamDestination();
osc.frequency.value = 440; osc.connect(dest); osc.start(); await ac.resume();
const sound = dest.stream.getAudioTracks()[0], stopSound = sound.stop.bind(sound);
let soundStopped=false;
sound.stop = () => { if (soundStopped) return; soundStopped=true; osc.stop(); void ac.close(); stopSound(); };
stream.addTrack(sound);
}
return stream;
};
window.fanoutGate = {
audio(value) { audio = value; },
breakIngress() { const pc = pcs.find((p) => p.connectionState === 'connected' && p.getSenders().some((s) => s.track?.kind === 'video'));
pc?.close(); },
async sample() {
let outbound = 0, encoded = 0, received = 0;
const videoStats=[], feedback=[];
for (const pc of pcs) { if (pc.connectionState === 'closed') continue;
const stats = await pc.getStats(); stats.forEach((s) => {
if (s.type==='remote-inbound-rtp') feedback.push({kind:s.kind,lost:s.packetsLost,fraction:s.fractionLost,rtt:s.roundTripTime,reports:s.reportsReceived});
if (s.type==='candidate-pair' && s.nominated) feedback.push({rtt:s.currentRoundTripTime,bwe:s.availableOutgoingBitrate,sent:s.bytesSent,received:s.bytesReceived});
if (s.type === 'outbound-rtp' && s.kind === 'video' && s.framesEncoded > 0) { outbound++; encoded += s.framesEncoded;
const source=stats.get(s.mediaSourceId);
videoStats.push({width:s.frameWidth,height:s.frameHeight,fps:s.framesPerSecond,reason:s.qualityLimitationReason,
sourceFps:source?.framesPerSecond,target:s.targetBitrate,encoder:s.encoderImplementation}); }
if (s.type === 'inbound-rtp' && s.kind === 'video') {
received += s.framesDecoded || 0;
feedback.push({inbound:true,fps:s.framesPerSecond,receivedFrames:s.framesReceived,decodedFrames:s.framesDecoded,
dropped:s.framesDropped,lost:s.packetsLost,received:s.packetsReceived,nack:s.nackCount,pli:s.pliCount,
decodeTime:s.totalDecodeTime,buffer:s.jitterBufferDelay,bufferEmitted:s.jitterBufferEmittedCount});
}
}); }
const video = document.querySelector('video');
return { outbound, encoded, received, captures, videoStats, feedback, width: video?.videoWidth || 0,
requests, errors, peers: pcs.map((p) => ({state:p.connectionState, tracks:p.getSenders().map((s)=>s.track?.kind)})),
frames: video?.getVideoPlaybackQuality().totalVideoFrames || 0,
sharing: Boolean(document.querySelector('button[aria-label="Stop sharing"]')),
captureLive: Boolean(video?.srcObject?.getVideoTracks()[0]?.readyState === 'live'),
capturesEnabled: video?.srcObject?.getVideoTracks()[0]?.enabled,
receiveRequests: requests.filter((r) => r === 'receive-offer').length,
edgeRequests: requests.filter((r) => r === 'prepare-edge').length,
controlOpen: sockets.some((s) => s.readyState === WS.OPEN),
codecSelected: [...document.querySelectorAll('button.lr-chip.is-selected')].map((b) => b.textContent),
audio: video?.srcObject?.getAudioTracks().length || 0,
buttons: [...document.querySelectorAll('.lr-host-share-slot button')].map((b) => b.getAttribute('aria-label')) };
}
};
})()`;
function build(command: string, args: string[]): void {
const result = spawnSync(command, args, { cwd: ROOT, encoding: "utf8", windowsHide: true });
if (result.error) throw result.error;
if (result.status !== 0) {
throw new Error((result.stderr || result.stdout).trim() || command + " failed");
}
}
// startRoomServer builds and starts the Piik server the Browser pages and
// both Apps share. PIIK_ENV stays development because production
// requires an HTTPS public origin and STUN; the Web assets are
// embedded either way.
async function startRoomServer(
port: number,
workingDirectory: string,
): Promise<ChildProcessWithoutNullStreams> {
const go = process.env.PIIK_GO?.trim() || "go";
await mkdir(BUILD_ROOT, { recursive: true });
// The server embeds the Vite output, so the Web build precedes the Go build.
build(process.env.ComSpec || "cmd.exe", ["/d", "/s", "/c", "npm run build:web"]);
const binary = join(BUILD_ROOT, "piik-server.exe");
build(go, ["build", "-trimpath", "-o", binary, "./cmd/piik-server"]);
const origins = [`http://localhost:${port}`, `http://127.0.0.1:${port}`, `http://${LAN_ADDRESS}:${port}`];
const child = spawn(binary, [], {
// The gate profile has no .env, and the environment is explicit: the
// configuration this measurement runs against is these five values, never
// the repository's development file or the operator's shell.
cwd: workingDirectory, stdio: "pipe", windowsHide: true,
env: {
PATH: process.env.PATH,
SystemRoot: process.env.SystemRoot,
PIIK_ENV: "development",
ROOM_DATABASE_PATH: ":memory:",
PORT: String(port),
LISTEN_HOST: "127.0.0.1",
PUBLIC_BASE_URL: `http://${LAN_ADDRESS}:${port}`,
ALLOWED_ORIGINS: origins.join(","),
},
});
child.stdout.resume();
child.stderr.resume();
await waitForSample(
(deadline) => fetchJsonBefore<{ status: string }>(`http://127.0.0.1:${port}/healthz`, deadline),
(value) => value.status === "ok",
20000,
);
return child;
}
async function main(): Promise<void> {
if (process.env.PIIK_CLIENT_BROWSER_FANOUT_GATE !== "true") throw new Error("Browser fanout gate was not enabled");
const chromePath = process.env.CHROME_PATH;
if (!chromePath) throw new Error("CHROME_PATH is required");
const profile = await mkdtemp(join(tmpdir(), "piik-client-media-"));
const appPort = await reservePort(), debugPort = await reservePort();
const origin = `http://localhost:${appPort}`;
let app: ChildProcessWithoutNullStreams | null = null;
let relayApp: ChildProcessWithoutNullStreams | null = null;
let chrome: ChildProcessWithoutNullStreams | null = null;
let cdp: CdpConnection | null = null;
let server: ChildProcessWithoutNullStreams | null = null;
let nativePort = 0;
let relayPort = 0;
const pages: PageHandle[] = [];
const checks: Record<string, unknown> = {};
let stage = "startup", error: string | null = null;
try {
server = await startRoomServer(appPort, profile);
app = spawn(process.env.PIIK_CLIENT_EXE || join(BUILD_ROOT, "piik-app.exe"), [
"--site", origin, "--config", join(profile, "client.json"),
"--capture-process", join(BUILD_ROOT, "piik-capture.exe"),
], { windowsHide: true, stdio: "pipe", env: { ...process.env, PIIK_CLIENT_GATE_NO_BROWSER: "true" } });
app.stderr.resume();
let output = "";
app.stdout.on("data", (chunk: Buffer) => {
output += chunk.toString();
if (!nativePort && output.includes("\n")) nativePort = decodeAppEndpoint(output.split("\n")[0]!).port;
});
await waitForSample(async () => nativePort, (value) => value > 0, 15000);
chrome = launchChrome(chromePath, debugPort, profile, [
"--headless=new",
"--no-first-run", "--no-default-browser-check", "--no-proxy-server",
"--autoplay-policy=no-user-gesture-required", "--disable-background-timer-throttling",
"--disable-renderer-backgrounding", "--disable-backgrounding-occluded-windows",
]);
chrome.stdout.resume(); chrome.stderr.resume();
const version = await waitForVersion(debugPort, chrome);
checks.browser = version.Browser;
cdp = await CdpConnection.connect(version.webSocketDebuggerUrl, Date.now() + 10000);
await cdp.call("Browser.setPermission", { origin, permission: {name:"loopback-network"}, setting:"granted" }, undefined, Date.now()+5000);
const host = await createPage(cdp, `${origin}/#piik-client=1`, probe);
pages.push(host);
const read = async (page: PageHandle) => {
const sample = await evaluate<any>(cdp!, page, "fanoutGate.sample()", Date.now()+5000);
if (page === host) checks.lastHost = sample;
return sample;
};
const click = async (page: PageHandle, selector: string) => {
await waitForSample((deadline) => evaluate<boolean>(cdp!, page,
`Boolean(document.querySelector(${JSON.stringify(selector)}))`, deadline), Boolean, 10000);
await evaluate(cdp!, page, `document.querySelector(${JSON.stringify(selector)}).click()`, Date.now()+5000);
};
stage = "share";
await click(host, 'button[aria-controls="host-advanced-door"]');
stage = "share-codec";
await click(host, 'button[aria-label^="H264"]');
stage = "share-picker";
await click(host, 'button.lr-tv-big.is-action');
stage = "share-browser-capture";
await click(host, 'button[data-source-tab="browser"]');
await click(host, '.lr-source-option.is-browser');
stage = "share-ingress";
await waitForSample(() => read(host), (s) => s.sharing && s.receiveRequests === 1 && s.outbound === 1, 30000);
let invite = await evaluate<string>(cdp, host, "document.querySelector('.lr-invite-url').value", Date.now()+5000);
const url = new URL(invite); url.host = `localhost:${appPort}`; invite = url.toString();
const relayOrigin = `http://127.0.0.1:${appPort}`;
relayApp = spawn(process.env.PIIK_CLIENT_EXE || join(BUILD_ROOT,"piik-app.exe"), [
"--site", relayOrigin, "--config", join(profile,"relay-client.json"),
"--capture-process", join(BUILD_ROOT,"piik-capture.exe"),
], { windowsHide:true, stdio:"pipe", env:{...process.env,PIIK_CLIENT_GATE_NO_BROWSER:"true"} });
relayApp.stderr.resume(); let relayOutput="";
relayApp.stdout.on("data", (chunk:Buffer) => {
relayOutput+=chunk.toString();
if (!relayPort && relayOutput.includes("\n")) relayPort=decodeAppEndpoint(relayOutput.split("\n")[0]!).port;
});
await waitForSample(async()=>relayPort,(value)=>value>0,15000);
const viewers: PageHandle[] = [];
for (let index=0; index<2; index++) {
stage = `viewer-${index+1}`;
const context = await cdp.call<{browserContextId: string}>("Target.createBrowserContext", {}, undefined, Date.now()+5000);
await cdp.call("Browser.setPermission", { origin:index===0?relayOrigin:origin, permission: {name:"loopback-network"}, setting:"granted", browserContextId: context.browserContextId }, undefined, Date.now()+5000);
const viewerURL = index===0 ? invite.replace(origin,relayOrigin)+"&piik-client=1" : invite;
const viewer = await createPage(cdp, viewerURL, probe, true, context.browserContextId);
viewers.push(viewer);
pages.push(viewer);
await waitForSample(() => read(viewer), (s) => s.width > 0 && s.frames >= 30, 30000);
}
stage = "shared-encode";
await waitForSample(() => read(viewers[0]!), (s) => s.width===1920 && s.outbound===0, 30000);
await waitForSample(() => read(viewers[1]!), (s) => s.width===1920 && s.outbound===0, 30000);
checks.host = await read(host);
checks.viewers = await Promise.all(viewers.map(read));
if ((checks.host as any).outbound !== 1 || !(checks.viewers as any[]).every((v) => v.outbound === 0)) {
throw new Error("Shared encode count did not match the Native fanout");
}
const beforeSteady = await Promise.all(viewers.map(read));
const steadyStart = Date.now();
await waitForSample(async () => Date.now()-steadyStart, (elapsed) => elapsed>=5000, 6000);
const afterSteady = await Promise.all(viewers.map(read));
checks.viewerFps = afterSteady.map((sample,index) => (sample.received-beforeSteady[index].received)*1000/(Date.now()-steadyStart));
if (!(checks.viewerFps as number[]).every((fps) => fps>=27)) throw new Error("Viewer delivery did not sustain the 30 fps source");
stage = "quality";
await click(host, 'button[aria-label="720p"]');
await waitForSample(() => read(viewers[0]!), (s) => s.width === 1280, 20000);
checks.liveQuality = true;
stage = "pause-resume";
await click(host, 'button[aria-label="Pause sharing"]');
await waitForSample(() => read(host), (s) => s.capturesEnabled === false, 5000);
await click(host, 'button[aria-label="Resume sharing"]');
const before = await read(viewers[0]!);
await waitForSample(() => read(viewers[0]!), (s) => s.frames >= before.frames+20, 10000);
checks.pauseResume = true;
stage = "source-audio-change";
await evaluate(cdp, host, `fanoutGate.audio(${!initialAudio})`, Date.now()+5000);
await click(host, 'button[aria-label="Switch source"]');
await waitForSample(() => read(host), (s) => s.receiveRequests === 2 && s.captures === 2 && s.sharing, 15000);
const beforeSource = await read(viewers[1]!);
await waitForSample(() => read(viewers[1]!), (s) => s.width > 0 && s.frames >= beforeSource.frames+20 && s.audio===Number(!initialAudio), 25000);
checks.sourceAudioChange = true;
stage = "app-exit";
app.stdin.write("\n");
await waitForSample(() => read(host), (s) => !s.controlOpen && s.sharing && s.captureLive && s.outbound === 2, 25000);
for (const viewer of viewers) {
const prior = await read(viewer);
await waitForSample(() => read(viewer), (s) => s.width > 0 && s.frames >= prior.frames+15, 30000);
}
checks.appExitRecovery = true;
checks.finalHost = await read(host);
if ((checks.finalHost as any).errors.length > 0) throw new Error("Browser reported an unhandled media error");
await mkdir(BUILD_ROOT, {recursive:true});
const screenshot = await cdp.call<{data:string}>("Page.captureScreenshot", {format:"png"}, host.sessionId, Date.now()+5000);
await writeFile(join(BUILD_ROOT,"browser-fanout.png"), Buffer.from(screenshot.data,"base64"));
await click(host, 'button[aria-label="Stop sharing"]');
stage = "done";
} catch (caught) {
error = caught instanceof Error ? caught.message : String(caught);
if (cdp) checks.atFailure = await Promise.all(pages.map((page) =>
evaluate(cdp!,page,"fanoutGate.sample()",Date.now()+5000).catch(()=>null)));
} finally {
const relayCleanup = await cleanupRun({cdp:null,native:relayApp,chrome:null,server:null,profile:null,ports:relayPort?[relayPort]:[]});
const cleanup = await cleanupRun({cdp, native:app, chrome, server, profile, ports:[appPort,debugPort,...(nativePort?[nativePort]:[])]});
const result = {passed: error===null && Object.values(cleanup).every(Boolean) && relayCleanup.nativeExited && relayCleanup.portsClosed, stage, checks, error, cleanup};
await mkdir(BUILD_ROOT,{recursive:true});
await writeFile(join(BUILD_ROOT,"browser-fanout.json"), JSON.stringify(result,null,2));
process.stdout.write(JSON.stringify(result)+"\n");
if (!result.passed) process.exitCode=1;
}
}
await main();