Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。
fix(media): improve connection recovery and capture compatibility (#498)
AAA066e218d84a2
diff --git a/.github/pull_request_template.md b/.github/pull_request_template.mdindex d9483b83..19614dca 100644--- a/.github/pull_request_template.md+++ b/.github/pull_request_template.md@@ -6,7 +6,7 @@## Release notes-<!-- Required for product-changing PRs; omit this section for standalone website/docs/maintenance work. Public copy, automatically published in GitHub Releases. Keep this exact heading; use ### for subsections. Describe user benefits, fixes and required upgrade actions in concise Chinese and English. Omit internal logs, audit prompts and unverified claims. 产品改动需填写公开的中英文更新说明;仅官网、文档或外围维护可删去此节。 -->+<!-- Required for product-changing PRs; omit this section for standalone website/docs/maintenance work. Public copy, automatically published in GitHub Releases. Keep this exact heading; use ### for subsections. Describe user benefits, fixes and required upgrade actions in concise English followed by Chinese. Omit internal logs, audit prompts and unverified claims. 产品改动需填写公开更新说明,先英文、后中文;仅官网、文档或外围维护可删去此节。 --><!-- Follow docs/standards/versioning.md#release-notes. Keep the public Release title Piik vX.Y.Z.An optional editorial opening uses a short **bold title** and one plain paragraph explaining its concretediff --git a/docs/README.md b/docs/README.mdindex a34ac1fb..492a5111 100644--- a/docs/README.md+++ b/docs/README.md@@ -11,7 +11,7 @@ This page indexes developer and maintainer references.| Task | Guide || --- | --- || Try or change the source code | [Run from source](#run-from-source) |-| Find a directory or the module responsible for a behavior | [Repository layout and module map](./standards/engineering.md#repository-layout) |+| Find a module, interface, lifecycle or source entry point | [Engineering maps and source index](./standards/engineering.md#repository-layout) || Find the shared product, design and lifecycle rules | [Standards index](./standards/README.md) || Deploy or configure a service | [Self-hosting](./operations/self-hosting.md) · [Configuration](./standards/configuration.md) · [Service management](./operations/service-management.md) || Build App packages and native capture | [App development](../cmd/piik-app/DEVELOPMENT.md) |diff --git a/docs/adr/0004-peer-assisted-media-experiment.md b/docs/adr/0004-peer-assisted-media-experiment.mdindex 1bb84d59..b13c51e8 100644--- a/docs/adr/0004-peer-assisted-media-experiment.md+++ b/docs/adr/0004-peer-assisted-media-experiment.md@@ -84,7 +84,7 @@ Peer relay is part of the deployed Browser route owned by ADR-0005. This ADRretains only the experiment evidence used to evaluate that route; its historicalpolicy and gates do not independently authorize current behavior.-The current accepted endpoint rule is one server-authoritative downstream+The current ordinary endpoint rule is one server-authoritative downstreamcapacity for every non-server endpoint, default `2` and statically configurableas `1`, `2`, or `3`; upstream receive is free and role or user agent does notcreate an exception. ADR-0005 owns the implemented and deployed Browser route@@ -113,8 +113,9 @@ Negative:- ADR-0001 remains the direct browser P2P baseline.- ADR-0005 owns current automatic routing and capacity invariants.- ADR-0007 owns path-quality evidence and representation behavior.-- A future native shared-encode sender or relay requires its own decision and- evidence; this experiment does not pre-approve it.+- [ADR-0013](./0013-embedded-node-local-media.md) and+ [ADR-0014](./0014-browser-node-local-encoding-pool.md) own accepted encoding+ reuse; this experiment does not define that behavior.## Referencesdiff --git a/docs/adr/0005-automatic-hybrid-media-routing.md b/docs/adr/0005-automatic-hybrid-media-routing.mdindex eb13f9f9..52087b02 100644--- a/docs/adr/0005-automatic-hybrid-media-routing.md+++ b/docs/adr/0005-automatic-hybrid-media-routing.md@@ -4,7 +4,7 @@- Date: 2026-08-20- Last updated: 2026-08-30-The current candidate replaces this ADR's external LiveKit room/token lifecycle+ADR-0013 replaces this ADR's external LiveKit room/token lifecyclewith admitted in-process SFU resources under [ADR-0013](./0013-embedded-node-local-media.md).Exact physical subscription closure now releases its reservation. The graph,capacity and serial-operation decisions below remain; external-service details@@ -58,7 +58,7 @@ One transition-overlap reservation may temporarily raise a producer toauthorized. SFU ingress and subscription egress are independent server resourcesand are not inferred from endpoint child counts.-The room-voice candidate adds an explicit, session-scoped relay volunteer+Room voice adds an explicit, session-scoped relay volunteeroverride under the [endpoint capacity contract](../standards/routing-transport.md#endpoint-and-server-capacity).It retains ordinary caps and one serial overlap reservation, but a capablevolunteer's admitted fanout may reach 20, with one extra transition slot.@@ -84,9 +84,9 @@ relay or a relay with reduced capacity accepts no new children; only itsdeterministic overflow children are reassigned. A relay whose ingress fails isitself reparented while its descendants remain attached.-### Room Media Lifetime (Candidate)+### Room Media Lifetime-The room-voice candidate keeps this graph at room lifetime, with independent+Room voice keeps this graph at room lifetime, with independentpicture generations and voice publications. Its existing serial operation alsoowns video attachment and composite picture/voice readiness; physical facetshave distinct identities so retiring one cannot remove the others. Thediff --git a/docs/adr/0009-optional-nat-prediction.md b/docs/adr/0009-optional-nat-prediction.mdindex 25f44c04..fe3d023f 100644--- a/docs/adr/0009-optional-nat-prediction.md+++ b/docs/adr/0009-optional-nat-prediction.md@@ -30,8 +30,8 @@ connection acquisition reuses the route controller's existing opportunity ledger2. An enabled Site derives ports 3479 and 3480 from its first ordinary STUNauthority on UDP 3478. Public-link App mode supplies three ordinary publicSTUN destinations, reusing the same set for its prediction survey; disabling- prediction retains ordinary discovery redundancy. Pure LAN mode supplies- none. The same accepted share policy and ICE configuration cover every+ prediction retains ordinary discovery redundancy. LAN mode uses its local+ STUN endpoint without a prediction survey. The same accepted share policy and ICE configuration cover everyBrowser or Native P2P edge. SFU PeerConnections remain unchanged.3. For one ICE gathering generation, the adapter observes only UDP `srflx`candidates belonging to the exact three-destination survey set. Browserdiff --git a/docs/adr/0010-cross-platform-client-runtime.md b/docs/adr/0010-cross-platform-client-runtime.mdindex 3e5d2873..f670bf34 100644--- a/docs/adr/0010-cross-platform-client-runtime.md+++ b/docs/adr/0010-cross-platform-client-runtime.md@@ -44,7 +44,7 @@ topologies and makes a saved Site unavailable while another room source runs.3. Discovery returns a per-process `instanceToken`. The WebSocket subprotocolechoes it so the Browser connects to the process it discovered. This value ispublic process identity, not authentication. The listener accepts the current- Local Host origin and the one user-saved Site origin. Origin and Host+ Local Host origin and individually approved Site origins. Origin and Hostvalidation plus the Browser's local-network permission own this boundary.4. Native control starts with a strict `hello` handshake. Up to two participantcontrol sessions may coexist; each retains its own share and media lifetime.@@ -139,8 +139,8 @@ topologies and makes a saved Site unavailable while another room source runs.added. Only a consumed App-launch marker travels in the URL fragment.Exact target identity travels over loopback.9. The App uses the system Browser as its only UI. It remains available to the- saved Site before a room mode is chosen. Its persistent launcher offers Local- and public link, shows the approved Site and allows revocation. Starting a+ approved Sites before a room mode is chosen. Its persistent launcher offers Local+ and public link, lists approved Sites and allows independent revocation. Starting aLocal/public room does not disable that Site's native access or approval UI;neither visiting the launcher nor approving a Site creates a room. Command-lineentry selectors remain automation inputs rather than the normal interface. An@@ -154,18 +154,17 @@ topologies and makes a saved Site unavailable while another room source runs.A participant on an approved Site consumes that Site'sconfigured STUN survey. Both feed the same connection-local predictionadapter; Native additionally owns its UDP socket and best-effort port mapping.-11. One App configuration owns the optional Site origin and an optional,+11. One App configuration owns approved Site origins and an optional,user-chosen Local access password. A blank value leaves the Local site open;a value gates that site through the existing SiteAccess authority. TheApp bootstraps a selected page through a fragment consumed beforeauthentication; the page retains only the non-secret App opt-in at itsorigin. Friends use the existing room invitation grant. No App-specific- room authorization system is added. Site replacement/revocation uses this- same configuration owner, immediately denies new control requests from the- old origin and cancels its existing sessions. Retirement is bounded and does- not wait for unrelated Local sessions. Cleanup failure never restores the- revoked permission; concurrent approval pages must review a changed Site- before replacing it.+ room authorization system is added. Adding a Site preserves other approvals.+ Revoking an origin through that same configuration owner immediately denies+ its new control requests and cancels only its existing sessions. Retirement+ is bounded and does not wait for unrelated Local or Site sessions. Cleanup+ failure never restores revoked permission.12. Local authority shutdown first ends every in-memory room through the current`room-closed` path, then closes signaling and HTTP. The App cancels theserver context, calls that end-then-close sequence under one bounded@@ -179,8 +178,7 @@ topologies and makes a saved Site unavailable while another room source runs.Git revision and an explicit supported target. The Go binary embeds thatrelease's built Browser assets and the package `REVISION` records the samerevision; a revision mismatch fails rather than loading a stale private- contract. App-scoped pull requests build every- target and must start the assembled Local authority, pass `/healthz`, and+ contract. App package acceptance must start the assembled Local authority, pass `/healthz`, andstop it cleanly before the candidate is accepted.14. The root package manifest is the single dependency contract for the Browserbundle and repository tooling. No host installs packages to run Piik, sodiff --git a/docs/adr/0013-embedded-node-local-media.md b/docs/adr/0013-embedded-node-local-media.mdindex 2a14d143..366e5ff7 100644--- a/docs/adr/0013-embedded-node-local-media.md+++ b/docs/adr/0013-embedded-node-local-media.md@@ -48,9 +48,10 @@ acceptance. Mature simulcast implementations provide the starting model.forwarding every downstream request. Do not require server transcoding orrecursive subtree demand to ship a forwarding SFU.8. Embed library-level STUN, preserving Binding and the three NAT-survey- destinations without enabling TURN allocations. Evaluate an embedded SFU- library through a narrow adapter. `inlivedev/sfu` is a candidate, not an- irrevocable selection or authorization for a deep fork.+ destinations without enabling TURN allocations. Embed Pion transport and+ LiveKit media-core components through the shared forwarding adapter. The+ [library evaluation](../research/embedded-media.md) retains the candidate+ comparison and selection rationale.9. Pure Browser peers retain normal WebRTC capture/senders and can consume thesame H264/VP8 output. No Browser encoded-passthrough promise, new codec familyor application transport is introduced by this architecture.diff --git a/docs/adr/0014-browser-node-local-encoding-pool.md b/docs/adr/0014-browser-node-local-encoding-pool.mdindex e967df77..7e9f8107 100644--- a/docs/adr/0014-browser-node-local-encoding-pool.md+++ b/docs/adr/0014-browser-node-local-encoding-pool.md@@ -14,7 +14,9 @@ producer's encoded payload/type and exposed codec metadata, with the outgoingstream's RTP clock. It avoids making one downstreamconnection the shared encoding authority. Small carrier encodes and localtransport/decoder work remain real costs. Measured savings and recovery vary-by codec; the prototype is not production acceptance.+by codec. The initial prototype established feasibility; the subsequent+implementation evidence and its physical limits remain in the linked research+and [verification status](../verification-status.md).This decision extends [ADR-0007](./0007-path-isolated-representation-quality.md)and [ADR-0013](./0013-embedded-node-local-media.md) for eligible Browser outputs.diff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.mdindex a72cb2ec..b674a690 100644--- a/docs/guide/troubleshooting.md+++ b/docs/guide/troubleshooting.md@@ -19,6 +19,7 @@ Start at the first thing that fails:| Source picker is missing or sharing will not start | [Source and capture checks](#starting-a-share). || Page opens, but the picture will not connect | [Media connection](#when-video-will-not-connect). || Picture blurs or stutters | [Picture quality](#picture-blurs-stutters-or-stops). |+| Phone fullscreen or playback controls are missing | [Fullscreen and controls](#fullscreen-or-playback-after-sleep). || Picture works, but no game/movie sound | [Source sound](#picture-but-no-source-sound). || Cannot hear a friend, or cannot use the microphone | [Room voice](#cannot-hear-someone-speaking) and [permissions](#microphone-or-remote-controls-are-unavailable). || Gamepad does not respond | [Gamepad checks](#gamepads). |@@ -390,10 +391,19 @@ for current setup details.### Fullscreen or playback after sleep+- **Playback controls are hidden:** tap the picture to bring them back. During+ remote control, use the drawer tab at the TV's bottom edge.+- **Fullscreen is missing or rejected:** open the invitation in a standalone+ browser, such as Chrome on Android or Safari on iPhone, instead of a chat app's+ embedded browser. **Theater mode** expands playback within the page when the+ browser does not offer screen fullscreen.+- **Piik controls disappear in fullscreen:** the browser has opened its native+ video player. Exit fullscreen and use **Theater mode** to keep Piik's playback+ controls, chat overlay and remote input on the page.- **Phone fullscreen has black bars:** video preserves its proportions. Try- landscape orientation, or select **Fill** or **Stretch** under **Playback settings**.-- **Fullscreen button does not work:** use the playback bar's **Fullscreen** button.- System video fullscreen depends on the browser; see [viewing controls](./getting-started.md#join-a-friends-room).+ landscape, or use **Switch picture fit** on the playback bar to select **Fill**+ or **Stretch**. Exit the browser's native video player to use Piik's picture-fit+ controls. See [viewing controls](./getting-started.md#join-a-friends-room).- **Sharing stops after sleep or suspension:** wake the device and return to thesharing tab. Start sharing again if needed; browser and OS suspension caninterrupt capture or playback.diff --git a/docs/guide/troubleshooting.zh-CN.md b/docs/guide/troubleshooting.zh-CN.mdindex f584c6e6..4fb76b8f 100644--- a/docs/guide/troubleshooting.zh-CN.md+++ b/docs/guide/troubleshooting.zh-CN.md@@ -19,8 +19,10 @@ outline: 2| 没有选源器,或选好来源后无法开播 | [来源与采集检查](#开始分享)。 || 网页能打开,但画面连接不上 | [媒体连接](#画面连接不上)。 || 画面模糊或卡顿 | [画质检查](#画面模糊卡顿或中断)。 |+| 手机找不到全屏或播控按钮 | [全屏与播控](#全屏或休眠后播放异常)。 || 有画面,但听不到游戏/影片声音 | [来源声音](#有画面但没有来源声音)。 || 听不到朋友,或无法开麦 | [房间语音](#听不到说话声)与[浏览器权限](#无法使用麦克风或远程控制)。 |+| 手柄没有响应 | [手柄排查](#手柄没有响应)。 |对应症状的简单检查无效后,再看[进阶网络排查](./network.zh-CN.md)。每次只改一项,方便对照。@@ -323,10 +325,14 @@ Windows 需要用户授权;其他采集要求显示边框时,边框仍会保### 全屏或休眠后播放异常-- **手机全屏仍有黑边:** 画面默认保持原始比例,可尝试横屏,或在 **播放设置** 中选择- **等比铺满** 或 **拉伸铺满**。-- **全屏按钮无效:** 使用播放栏的 **全屏观看** 按钮。系统视频全屏由浏览器提供,- 见[观看操作](./getting-started.zh-CN.md#加入朋友的房间)。+- **播控按钮隐藏了:** 轻点画面即可重新呼出;正在远控时,使用电视底边的抽屉拉手。+- **没有全屏按钮或点击失败:** 将邀请链接复制到独立浏览器打开,例如安卓 Chrome 或+ iPhone Safari,避免使用聊天软件的内置浏览器。不支持系统全屏时,可用 **剧场模式** 放大网页内的画面。+- **全屏后 Piik 播控不见了:** 浏览器进入了自带的视频播放器。退出全屏、改用 **剧场模式**,+ 即可保留 Piik 播控、弹幕和远控操作。+- **手机全屏仍有黑边:** 画面默认保持原始比例,可尝试横屏,或用播控上的 **切换画面适配**+ 切换 **等比铺满** 或 **拉伸铺满**。如果已进入浏览器自带的视频播放器,先退出全屏再使用+ Piik 的画面适配。见[观看操作](./getting-started.zh-CN.md#加入朋友的房间)。- **休眠或挂起后分享中断:** 唤醒设备,回到分享标签页,必要时重新开始分享。浏览器或系统挂起可能中断采集和播放。diff --git a/docs/project-memory.md b/docs/project-memory.mdindex b8ee1885..316a6554 100644--- a/docs/project-memory.md+++ b/docs/project-memory.md@@ -1,6 +1,6 @@# Project Memory-Last updated: 2026-10-23+Last updated: 2026-10-24Piik is private, low-latency screen sharing for one Host and up to 20authenticated friends. The current product surface is Web Host, Web Viewer, and@@ -23,23 +23,13 @@ Host authority belongs to a tab, with one separate persistent origin resumehint. The App admits two independent native control sessions under its oneloopback service.-The current source embeds Binding-only STUN and optional SFU forwarding in-the Hosted Go process. Native and SFU use one shared Pion/LiveKit media adapter;-Native parents reuse suitable encoded outputs and derive a missing lower output-only for direct-child demand. Source-owned groups retain the highest needed-output and lower fallbacks, while each child receives its selected output.-Native Host SFU publication reuses the encoded source directly; its loopback-Browser stream supplies local preview and Browser quality candidates. Eligible-Browser capture and relay now share-independent local WebRTC producers for compatible direct children under ADR-0014;-unsupported APIs and failed pooling retain ordinary senders. Windows, macOS and-Linux producers share one encoded-frame-boundary and Host quality controls. Source replacement retains route and-connection identity; lower-output adaptation preserves the original and higher-sibling outputs. [Status](./status.md) owns candidate and release readiness;-runtime release descriptors own deployed identity.-The Local launcher keeps an optional user-chosen site-access password; leaving it-blank keeps the self-contained site open.+The Hosted Go process embeds Binding-only STUN and optional SFU forwarding.+Native and SFU use one shared Pion/LiveKit media adapter. Native parents reuse+encoded outputs and derive missing lower outputs from direct-child demand;+adaptation preserves the source and higher sibling outputs. Eligible Browser+capture and relay share local WebRTC producers under ADR-0014, with ordinary+senders as fallback. Platform producers share the encoded-frame boundary and+Host quality controls. Source replacement preserves route and connection identity.## Product Map@@ -53,9 +43,10 @@ blank keeps the self-contained site open.- [Presentation and lifecycle](./standards/presentation-lifecycle.md) owns Host andViewer workflows, playback authority, roster, state, and Browser lifecycle.-Non-obvious decisions live in [ADRs](./adr/); reproducible evidence and platform-limits live in [research](./research/). Source code and tests own self-evident-implementation and routine UI detail.+[ADRs](./adr/) own architecture decisions; [research](./research/) owns measurements+and platform limits.+[Engineering maps and source index](./standards/engineering.md#source-index)+locate implementation owners and their module, interface and lifecycle boundaries.## Core Invariants@@ -64,7 +55,8 @@ implementation and routine UI detail.through replacement/deletion, with separate invitation rotation or revocation.Hosted operators may opt into empty-room expiry and capacity reclamation;the default retains rooms indefinitely. Hosted defaults to SQLite, with explicit- process-memory opt-out; App Local remains process-only. Site access has a separate 24-hour idle+ process-memory opt-out; App Local remains process-only, with an optional site-access+ password. Site access has a separate 24-hour idlecookie lifetime within one service run. Production persists SQLite roomauthority; live participants, routes, and media remain process-only.- Media is automatic and distributed. Ordinary peers are STUN-only and prefer@@ -85,12 +77,12 @@ implementation and routine UI detail.- Browser WebRTC and the shared Pion/LiveKit media adapter own congestion control,media adaptation, pacing and layer selection. Parent demand is local to directchildren and never a room-wide minimum or topology rebalance. Current source- defaults persistent native-edge local P2P convergence- on, with a pre-share Host opt-out. SFU quality work is limited to multi-root- Host fanout relief and each Viewer must prove its own candidate non-regression- before commit. A persistently limited edge with no different Peer parent may- use one same-parent connection regeneration through that same operation; its- exact post-commit sender identity damps only another same-edge regeneration.+ defaults persistent native-edge local P2P convergence on, with a pre-share+ Host opt-out. SFU quality work is limited to multi-root Host fanout relief;+ each Viewer must prove candidate non-regression before commit. One bounded+ same-parent regeneration may repair a persistently limited edge through+ that same operation; the [routing owner](./standards/routing-transport.md)+ defines its eligibility and damping.No weighted score, all-pairs probe, general parent-wide prediction, orperiodic rebalance is accepted.- Browser video uses `motion`; each share chooses H.264 with VP8 fallback through@@ -104,13 +96,11 @@ implementation and routine UI detail.## Current Snapshot-Room voice integrates normal App/Server/Web rather than a-standalone demo. It keeps room authority and one route controller while picture-generations and explicitly joined voice publications have separate-lifetimes. Optional source commentary belongs to the share and can run-independently of room voice. [Status](./status.md#current-work)-indexes delivery and [room media](./standards/routing-transport.md#room-media)-owns the new behavior.+Room voice shares room authority and one route controller with picture sharing.+Picture generations and explicitly joined voice publications have separate+lifetimes. Optional source commentary belongs to the share and runs independently+of room voice. [Room media](./standards/routing-transport.md#room-media) owns+these lifecycles.Optional attended input follows the committed media route back to the nativeHost. One grant covers keyboard/mouse and an optional gamepad. Control starts@@ -122,18 +112,12 @@ Viewer playback preferences and the combined interaction window remain localpresentation owners under the existing 2.x compatibility boundary.The current contract tuple is owned by [status](./status.md#accepted-release-contract).-Embedded SFU SDP/ICE and demand travel over the-authenticated room WebSocket, without an external room service or media token.-Committed first-frame readiness, bounded candidate-relative progress, durable-room authority and the H.264/VP8 sender gate remain. [Status](./status.md)-indexes source and deployment evidence; protocol rollback requires-matching Browser and App artifacts, not only an application symlink.+Embedded SFU SDP/ICE and demand use the authenticated room WebSocket.+[Deployment](./deployment.md) owns rollback with matching Browser and App artifacts.Unfinished work is owned by the [TODO ledger](./todo.md).-[Verification status](./verification-status.md) summarizes material evidence-boundaries. Environment and initial-services are owned by [configuration](./standards/configuration.md) and-[self-hosting](./operations/self-hosting.md); application release is owned by-[deployment](./deployment.md), with exact deployed identity retained in the-release descriptor and deployment record.-Git history and pull requests own completed timelines.+[Status](./status.md) indexes delivery; [verification status](./verification-status.md)+summarizes evidence boundaries. [Configuration](./standards/configuration.md)+and [self-hosting](./operations/self-hosting.md) own environment and services.+Release descriptors and deployment records own exact deployed identity;+Git and pull requests own completed history.diff --git a/docs/research/advanced-peer-distribution.md b/docs/research/advanced-peer-distribution.mdindex f91757f5..779bb14f 100644--- a/docs/research/advanced-peer-distribution.md+++ b/docs/research/advanced-peer-distribution.md@@ -246,8 +246,10 @@ foreign-frame ownership, not replacement of a sender's own frame payload.It does not reject an independent local producer. The later[node-local producer/carrier experiments](./browser-local-encoding-pool.md)establish a working standard-transform prototype with separate findings and-remaining quality/accounting obligations. Product media still uses normal-Browser senders and the verified optional App fanout pending that acceptance.+remaining quality/accounting obligations. Those September 9 results preceded the+accepted Browser pool in+[ADR-0014](../adr/0014-browser-node-local-encoding-pool.md), which owns the current+optional Browser reuse composition.A relevant future input API is Chromium's experimental[`RTCEncodedSource` implementation](https://chromium-review.googlesource.com/c/chromium/src/+/8097283),diff --git a/docs/research/browser-local-encoding-pool.md b/docs/research/browser-local-encoding-pool.mdindex ad25b36c..68dc4e68 100644--- a/docs/research/browser-local-encoding-pool.md+++ b/docs/research/browser-local-encoding-pool.md@@ -101,9 +101,12 @@ around fallback was 271 ms for H264 and 80 ms for VP8. Source-wide fallbacktherefore has a measurable handoff cost, even without a resolution drop orconnection failure. This does not establish low-end hardware capacity.-An unmatched negotiated codec is instead connection-local. A Chrome 154-late-Viewer control reproduced all members losing pooling when only the new-Viewer advertised different codec parameters. Scoped fallback now preserves+An unmatched negotiated codec or sender attachment failure is connection-local.+Delayed attachment rejection controls retain the healthy sibling's producer,+restore only the failing member's ordinary sender, and admit a later member to+that same producer. A Chrome 154 late-Viewer control reproduced all members+losing pooling when only the new Viewer advertised different codec parameters.+Scoped fallback now preservesthe first Viewer's producer and delivers fresh pictures to both Viewers inVP8 1080p and H264 4K controls. Unit counterfactuals cover admission and activecodec changes; producer failure retains its existing source-wide fallback.diff --git a/docs/research/browser-screen-audio-quality.md b/docs/research/browser-screen-audio-quality.mdindex e931d038..7833d1ce 100644--- a/docs/research/browser-screen-audio-quality.md+++ b/docs/research/browser-screen-audio-quality.md@@ -107,7 +107,7 @@ Two controls constrain the replacement design:live bitrate changes. This does not diagnose any field interruption or changethe average bitrate controls, stereo format or route model.-The [media extension proposal](./native-client-media.md#audio-and-ui-proposal)+The [numeric audio rationale](./native-client-media.md#numeric-audio-and-ui-rationale)owns the UI and contract rationale. The candidate enforces a Browser sender'snegotiated Opus limit, and admits unchanged Native/SFU forwarding only when thereceiver's declared limit accommodates the full shared format. A codec-validdiff --git a/docs/research/camera-and-microphone.md b/docs/research/camera-and-microphone.mdindex 5f5c17e0..a2d7a27c 100644--- a/docs/research/camera-and-microphone.md+++ b/docs/research/camera-and-microphone.md@@ -9,8 +9,11 @@ The current source restores optional commentary under **Sharing settings →Sound → Include my microphone**, independently of room voice. The stable Browseroutput and Native mixer described below have a current UI consumer. Commentarystarts off for each new share; enabling room voice does not change it. Both may-remain enabled with a duplicate-speech warning. Earlier capture evidence does not-establish acceptance of this restored combined UI; [TODO](../todo.md#now) owns that check.+remain enabled under the [current microphone contract](../standards/media-quality.md#capture-and-controls).+The normal-page room-voice gate verified simultaneous room microphone and source+commentary delivery, commentary retirement on share stop, and the next share's+default-off state with synthetic devices. This does not establish physical echo+or device acceptance.The existing source picker offers Browser, Camera, Window and Screen in thatorder, defaulting to Window when an App is detected. Without an App, it showsdiff --git a/docs/research/cross-platform-client-runtime.md b/docs/research/cross-platform-client-runtime.mdindex 9981b741..4acf03df 100644--- a/docs/research/cross-platform-client-runtime.md+++ b/docs/research/cross-platform-client-runtime.md@@ -108,13 +108,12 @@ compare actual capture, audio, H.264/VP8 sender implementation, 1080p cadence,relay, background behavior, and resource use on every target platform. Theresult selects one UI runtime; it does not create parallel products.-## Current Evidence+## Historical Package Evidence-The runs below are pre-port evidence: they exercised the package that bundled a-Node runtime and an extracted application tree. Their capture, discovery, media,-and network results still apply; every package composition, startup, and-shutdown claim must be re-verified on the single-binary App before that build-is accepted.+The runs below exercised the earlier package that bundled Node and an extracted+application tree. They retain their measured revision's results. The current+single-binary composition is described above; [verification status](../verification-status.md)+owns current acceptance and platform coverage.On Windows with Chrome for Testing 151.0.7922.138:@@ -327,21 +326,13 @@ The evidence supports direct manual downloads with an optional release-notelink and inline major-upgrade guidance. The accepted behavior belongs to[versioning](../standards/versioning.md#major-upgrade-guidance). Historicalclients still follow their released behavior; a notice cannot substitute for-actual App/Site compatibility checks. The scope of a future 2.0 break remains-undecided.--## Remaining Gates--- Run the exact clean-revision package on macOS; Linux amd64 is proved.-- Prove a physical second-device LAN Viewer on desktop, Android, and iOS.-- Prove first-run Browser local-network permission outside CDP automation.-- Record no-STUN mDNS behavior on ordinary and AP-isolated LANs.-- Exercise the native Host on a configured Site and representative Browsers;- the local SFU bridge is proved in isolation, while full Site recovery,- representative audio, and non-Windows capture remain undefined.-- Exercise one-link Browser media on a physical second device; the current gate- proves the complete public control path and independent Pion media, not- decoded Browser frames.+actual App/Site compatibility checks.++## Current Follow-up++[TODO](../todo.md) owns remaining work; [verification status](../verification-status.md)+owns current device and network coverage. The historical runs above do not form+a second acceptance ledger.## Sourcesdiff --git a/docs/research/nat-traversal.md b/docs/research/nat-traversal.mdindex 9ca97f84..66fb56a9 100644--- a/docs/research/nat-traversal.md+++ b/docs/research/nat-traversal.md@@ -148,7 +148,8 @@ supports an opt-in mechanism, not a default or a participant-wide classifier.Ordinary STUN remains the discovery service. A Site may enable same-hostSTUN-only listeners on 3479 and 3480. Public-link App mode supplies three ordinarypublic destinations and reuses them for prediction, so disabling prediction-does not remove discovery redundancy; pure LAN mode supplies none.+does not remove discovery redundancy. LAN mode uses its local STUN endpoint+without a prediction survey.Current pages automatically request that capability on P2P-first sites. Theserver's accepted share policy keeps Host, Viewer and relay behavior aligned;the published 2.x field still accepts older pages' per-share preferences.@@ -275,9 +276,9 @@ On 2026-09-01, Chrome 151 on the flagship Windows network gathered one `srflx`candidate independently from the self-hosted endpoint, Xiaomi, Bilibili, andCloudflare within a five-second bound. `stun.qq.com:3478` produced none in thesame ordered run. Candidate events and local candidate stats preserved the exact-STUN `url`, but the destinations supplied no selected-path benefit. They remain-research evidence only; the product has no independent or third-party STUN-configuration.+STUN `url`, but that run established no selected-path benefit. The current+[STUN configuration](../standards/configuration.md#piik-app-configuration)+is independent of this dated reachability probe.A second real-Chrome loopback rewrote only one remote candidate foundation to`sp1`, established the data channel, and read `sp1` from the selected remote@@ -511,16 +512,15 @@ Mature implementations provide a concrete comparison:and random-port paths. Its implementation is evidence that these are realmechanisms, not evidence that replacing Piik's media stack improves coverage.-An isolated Go prototype now compares ordinary ICE, the current eight-candidate+An isolated Go prototype compared ordinary ICE, the current eight-candidateprediction policy, an ordered first tuple, a coordinated eight-socket pool andordinary ICE over eight preallocated sockets. The pool authenticates generation-specific STUN transactions, closes its losing sockets and passes its actualwinning socket to stock Pion WebRTC. Success requires bidirectional SRTP payloaddelivery and reverse RTCP feedback. All strategies share a four-second overalldeadline; no relay, OS socket or external network is used in these controls.-The retained implementation is commit `807c7390` on-`spike/magicsock-assessment`, under `scripts/magicsock-probe/native_punch.go`-and its tests; it is not part of the product module.+The prototype is retired; the current transport and retry owners are described+in [routing and transport](../standards/routing-transport.md).In Pion's sequential endpoint-dependent NAT fixture, coordinated first sendsconnect the otherwise timing-sensitive bilateral tuple. Three unrelated mappings@@ -725,31 +725,15 @@ recovery loop or a Browser fork to enable this option.### Integration Cost And Recovery Controls-**Current candidate excludes Magicsock; optional-helper adoption needs a matched gain.**-This composition requires Native App transport at both ends of the enhanced-edge. Those participants can still use the existing Browser UI and local media-bridge; Native transport does not require a separate desktop interface. A-Browser-only endpoint continues to use ordinary WebRTC.--The background-App proposal is an optional transport helper for the same Web-workflow. The existing standby and saved Site approval can reduce repeated-setup, but do not remove first-use authorization, Browser local-network-permissions, or the integration costs below. Reassess adoption against that-optional-helper scope, preserving ordinary ICE when the helper is absent or-unavailable. The controls established no additional reachable NAT class;-background availability alone is not reachability evidence or acceptance of-DERP media fallback. Add it to the release candidate only after a matched benefit-and the integration boundaries below are established.--The isolated experiment remains at `cc6f9d0d` on-`spike/magicsock-assessment`, outside Piik's production dependency graph.-The `scripts/magicsock-probe/README.md` and `scripts/tailcat-probe/README.md`-files at that commit own rerun commands, fixture boundaries and opt-in-dependency patches. The complete earlier experimental-observations remain in `git show 82e3f253:docs/research/nat-traversal.md`.-Its internal package is retired, not a current 2.0 test deliverable.--#### Current Stable Direct-Transport Control+**Magicsock integration is not planned.** The tested composition required+Native App transport at both ends and established no additional reachable NAT+class worth its integration cost. Piik retains WebRTC with its existing+NAT preparation and virtual-LAN support.++The experiment has been removed. The dated controls below record the decision's+technical basis, not a maintained module or a future integration checklist.++#### October 12 Direct-Transport ControlOn October 12, 2026, the isolated raw-Bind module was refreshed to[Tailscale v1.104.1](https://github.com/tailscale/tailscale/releases/tag/v1.104.1)@@ -774,9 +758,8 @@ Raw Bind also consumed bare inner STUN, requiring a test-only packet prefix.After peer removal it could still deliver UDP without peer identity; rawreception is not authentication. A product adapter would need supportedframing and existing room/cryptographic authorization, not just another socket.-The refreshed full probe suite and longer-window matrix passed, including-controls that expect connection failure. They do not establish a new reason-to include Magicsock in this candidate.+The refreshed probe suite and longer-window matrix passed, including controls+that expect connection failure. These results did not establish a product benefit.#### Reachability And Cost@@ -803,8 +786,7 @@ Native mapping, prediction and dual-stack controls are retained#### Integration Boundaries-The experimental composition exposed costs that a future adoption must address-without another retry owner, timeout extension or silent relay policy change:+The experimental composition exposed these integration costs:- **Relay policy:** the reviewed upstream API has no direct-only media policyretaining discovery. An isolated overlay filtered encrypted data in both@@ -840,7 +822,7 @@ without another retry owner, timeout extension or silent relay policy change:and delayed end-of-candidates. Its multi-mux also needed a correction fornewly enumerated child addresses. These remain experimental dependency changes.-#### Scope Of The Retained Evidence+#### Scope Of The Historical EvidenceThe isolated overlays exercised real Pion/LiveKit forwarding, H264/VP8 and Opus,Browser decoding, Native control and room authorization. Bounded rendered checks@@ -853,8 +835,8 @@ candidates withheld, while ordinary Web edges retained ICE.Those checks established local composition and ownership behavior. They didnot establish physical capture, difficult-network benefit, broad mobile supportor sustained field reliability. The experiment's detailed intermediate controls,-failed counterfactuals and package evidence remain in Git rather than this-candidate's work ledger. Independent ordinary-media findings are retained here+failed counterfactuals and package evidence belong to historical research.+Independent ordinary-media findings are retained hereand in their existing owners.## Ordinary ICE And Media Recovery@@ -906,8 +888,50 @@ while keeping the Browser connection, instead reported connected without freshpictures in a counterfactual. The tests therefore support standard ICE restart,not unilateral reconstruction or a claim of physical roaming coverage.+### Candidate Generations Across App Control++A controlled Browser-Host/Native-Viewer restart on 2026-10-24 reproduced lost+trickle candidates. The room WebSocket delivered a restart offer before the App+WebSocket delivered an already-written completion from the previous gathering.+The Viewer had replaced its candidate batch on the SDP exchange, so the old+completion closed the new batch and all three new survey candidates were lost.+The Go writer's existing current-owner check correctly protected unwritten+events; it could not recall an event already delivered to the socket.++Native Viewer now reuses the Browser candidate emitter across SDP exchanges.+Each Native collector captures Pion's actual ICE username fragment and includes+it on ordinary, survey and gateway candidates. Completion is conveyed for that+generation rather than as an unscoped null, following+[RFC 8838](https://www.rfc-editor.org/rfc/rfc8838.html#section-13) and the+[WebRTC candidate algorithm](https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-addicecandidate).+The delayed-event control retains new candidates with prediction enabled or+disabled. The real H264/VP8 receiver control checks initial and restarted+candidates against the corresponding SDP while preserving its encoded source+and downstream connection. No new generation field, retry owner or timeout is+introduced.++The same restart control exposed a second queue inside pinned Pion ICE: replacing+its handler does not discard candidates and unscoped completion already queued+by the previous gathering. Holding the old callback across restart delivered both+generations to the new handler. Native collectors now read the candidate's own+ICE fragment before accepting it and associate completion with that ordered+source generation. A real Pion control retains every new-generation candidate+and excludes the old ones. Trickled answers also omit Pion's embedded+`end-of-candidates`: only the merged collector can complete the additional STUN+and gateway observations. H264 and VP8 retained-source renegotiation exercise+that SDP path.+### Recovery Ownership And Forwarding+A same-connection Native ICE restart also reproduced an input stall: Pion's+asynchronous `connecting` callback arrived after transport and SCTP readiness had+recovered, leaving the input bridge unavailable. State projections now check the+current PeerConnection at consumption. Historical loss still clears permission;+the recovered bridge requests fresh readiness. The control delays that real+callback until after one input packet round trip, then verifies another fresh+handshake and packet delivery after release. Host grant controls retain their+existing revocation behavior.+A real Viewer-owner fixture retained sockets and external conditions throughinterruption and replacement. It exposed recovery counts being renewed with anew Native/Browser backend, permitting an extra request. The corrected budget@@ -976,12 +1000,19 @@ attempts also do not represent four full 20-second product operation timeouts.The correction keeps Pion's ordinary UDP mux and uses the existing Pion STUNclient for each observation. Only concurrent in-flight queries to the same-destination are shared; completed addresses are not cached. Pion owns request-transaction matching and retransmission within the existing five-second survey-bound. A caller can stop waiting independently; connection retirement ends discovery.+destination within one gathering are shared; completed addresses are not cached.+Pion owns request transaction matching and retransmission within the existing+five-second survey bound. Gathering retirement cancels its observations;+retaining the socket does not retain those transactions for a successor.ICE restart retains its socket. The separate overlap correction below isolatesreplacement connections without adding a retry loop, periodic probe or new transport.+A retained-socket delayed-response control found that connection-wide in-flight+sharing could still supply a retired query's mapping to a new gathering. The+old response was valid for its transaction but described the previous public+port. Query coalescing now belongs to each survey, so restart sends a fresh+Binding request while preserving ordinary duplicate-destination sharing.+The same review reproduced acceptance of an unmatched STUN response in the olduniversal cache path. Stock STUN transactions reject it and recover a droppedBinding response. Native edge replacement and receiver ICE restart now querydiff --git a/docs/research/native-client-media.md b/docs/research/native-client-media.mdindex 1223b93e..339cc271 100644--- a/docs/research/native-client-media.md+++ b/docs/research/native-client-media.md@@ -16,6 +16,15 @@ and exclusion retained the second with under 0.04 of the excluded tone. Targetexit retired capture in about 42 ms; attempting the stale identity failed.No raw audio was saved. The extended capability probe completed in 300 ms.+On 2026-10-24, a held optional audio query reproduced the helper exceeding Go's+three-second discovery deadline and losing otherwise available video capability.+Windows now runs optional audio queries independently alongside video+and adapter discovery, with a shared one-second wait budget starting at probe+entry. Each worker owns its COM apartment and results inside the one-shot+probe process; live audio startup keeps its existing budget. Held-query controls+retain video output, and a real helper through Go discovery completed in 163 ms+with video and all three audio capabilities available.+A parent/child follow-up used a silent selected parent with a 1900 Hz child,plus an independent 2900 Hz process. Process-scoped reference amplitudes wereabout 1799; exclusion retained the independent tone at 1800 and reduced the@@ -489,6 +498,17 @@ intermittent platform cause and physical-device limits remain unproven.## Native H.264 Motion Quality+Windows hardware initialization requests a single temporal layer where supported+and low-latency mode where writable. Microsoft's+[hardware encoder requirements](https://learn.microsoft.com/en-us/windows/win32/medfound/h-264-video-encoder#certified-hardware-encoder)+classify these properties as optional. Chromium's+[Media Foundation encoder](https://chromium.googlesource.com/chromium/src/+/main/media/gpu/windows/media_foundation_video_encode_accelerator_win.cc)+explicitly sets even one temporal layer for Intel and conditionally sets low-latency+mode. Piik uses the property capabilities without a vendor or version branch;+an absent optional property or readback no longer rejects an otherwise usable+encoder. Required CBR, bitrate and GOP checks, Constrained Baseline bitstream+admission, actual H.264 output and Auto's measured frame cadence retain their owners.+Issue [#432](https://github.com/TNTcraftHIM/Piik/issues/432) reports more visibleblocking with native H.264 than VP8 on v1.6.7. Review of its attached Browserreport confirms 720p30 in both comparison windows, roughly 11 Mbps received,@@ -608,14 +628,13 @@ a corrected SDP interpretation, not evidence that all Firefox sharing lost audio## Media Capability Extension Assessment-Planning direction reviewed 2026-10-09 for 2.0; the design below is a proposal,-not a replacement for the current contract or profile decision above. The owner-reopened numeric media requests and capability negotiation as one coherent-extension. The published resolution enums are enforced-by both signaling readers and Native control, while capture admission separately-bounds dimensions. Native capture, forwarding and SFU declarations also admit-specific encoded formats. Removing one validator would leave these consumers-inconsistent.+The October 9 review developed the numeric media extension later published in+2.0. The rationale and dated implementation checks below retain that transition;+[media quality](../standards/media-quality.md) and [versioning](../standards/versioning.md)+own the current contract. The preceding 1.x resolution enums were enforced by+both signaling readers and Native control, while capture admission separately+bounded dimensions. Coordinating those consumers preserved resource admission+when the public request became numeric.Source review also distinguishes output geometry from a quality limit. Thepublished Windows path fits content into the selected landscape raster with a@@ -691,11 +710,11 @@ App discovery advertises size, pixel-count, macroblock-rate, frame-rate and bitrsize/rate combinations as operation failures without retiring the controlsession. Removing an enum is not permission to allocate arbitrary media resources.-The Windows H.264 UI gate switches from 1080p60 to the 4K ceiling, then applies-a non-preset 960x600 ceiling during the live share. Its earlier 30 fps run-observed fitted output without replacing the Viewer media object. The gate now-expects the 60 fps control and needs rerunning. The captured window is smaller-than 4K, so this verifies settings/lifecycle, not physical 4K capture.+The initial Windows H.264 UI gate switched from 1080p60 to the 4K ceiling, then+applied a non-preset 960x600 ceiling during the live share. Its 30 fps run+observed fitted output without replacing the Viewer media object. The later+60 fps acceptance is recorded below. The captured window was smaller than 4K,+so this verified settings/lifecycle, not physical 4K capture.Preset changes, paused updates, source recovery/replacement and subsequentsharing still pass. Value-based comparison prevents an identical deserializedsize from turning an audio-only edit into a video update. Numeric preference@@ -1002,20 +1021,20 @@ inherently require a major; breaking an existing public contract does. Evaluatethe Browser/App/Server and mixed-peer flows before freezing a replacementcontract; avoid a general compatibility framework.-### Audio And UI Proposal+### Numeric Audio And UI Rationale[#462](https://github.com/TNTcraftHIM/Piik/pull/462) requests 256/320 kbps Opus.The values are permitted by [RFC 7587](https://www.rfc-editor.org/rfc/rfc7587.html#section-6.1),whose receive-rate parameter spans 6,000..510,000 bit/s. This is a codec boundary,-not a recommended default or proof of audible benefit. The current preset names-also travel in authentication and Native commands: adding its two enum values-causes the actual v1.9.2 readers to terminate those sessions. The PR's compatible-release claim is therefore incorrect even though its audio use case is valid.--The 2.0 candidate uses one numeric audio-bitrate ceiling through capture, sender,-receiver and SFU; derive existing preset labels locally. Align receive admission-with codec support instead of the largest UI shortcut. Keep the existing default-while comparing actual Opus output and listening results for higher ceilings.+not a recommended default or proof of audible benefit. The v1.9.2 preset names+also traveled in authentication and Native commands: adding its two enum values+caused those readers to terminate their sessions. That extension therefore+required the later contract change despite its valid audio use case.++The published 2.0 extension uses one numeric audio-bitrate ceiling through+capture, sender, receiver and SFU, with preset labels derived locally. Receive+admission follows codec support separately from UI shortcuts. The default was+retained; actual Opus output and listening quality remain distinct evidence.The [numeric audio controls](./browser-screen-audio-quality.md#numeric-bitrate-assessment)establish local 256/320 kbps encoding and Browser reception, with separatereceive-limit enforcement and a 320 kbps implementation ceiling. Full-range@@ -1037,14 +1056,12 @@ supports a simple/default entry with further controls; Piik need not reproduceOBS's full output configuration. Existing visual/tooltip owners still apply,including narrow screens, both themes and visual mode.-Start with numeric audio through the existing pipeline, then portrait/ultrawide-geometry and 4K as a second concrete use case. A 4K path must review all platform-capture/decoder bounds, encoded-frame limits and SFU admission. Admission and output must be tested-at the actual profile, not inferred from a codec list. Check live changes,-late/limited Viewers and relays, failure cleanup and resource rejection before-freezing the 2.0 contract. Prefer a clear 1.x mismatch over a general legacy-adapter. Future presets inside supported numeric semantics need no new enum;-new formats still require actual implementation and an advertised capability.+The implementation proceeded through numeric audio, aspect-preserving geometry+and larger picture ceilings. The controls above record live changes, mixed+receivers, cleanup and bounded rejection at their measured profiles. The 2.0+contract retained an explicit 1.x mismatch. Future presets within its numeric+semantics need no new enum; new formats still require an implementation and+advertised capability under the current versioning owner.## Implementation Boundarydiff --git a/docs/research/native-sender.md b/docs/research/native-sender.mdindex 5947a775..e187423c 100644--- a/docs/research/native-sender.md+++ b/docs/research/native-sender.md@@ -5,7 +5,10 @@- Status: historical evidence; obsolete sender implementation deletedCurrent product scope is owned by [media quality](../standards/media-quality.md).-[ADR-0006](../adr/0006-fixed-high-native-sender-canary.md) owns the stop line.+[ADR-0006](../adr/0006-fixed-high-native-sender-canary.md) records the retired+canary stop line. Native App adoption and subsequent evidence are owned by+[ADR-0010](../adr/0010-cross-platform-client-runtime.md) and+[Native App media](./native-client-media.md).## Conclusion@@ -26,7 +29,8 @@ The retained measurements prove:It does not prove two-Viewer product fanout, heterogeneous congestion feedback,loss/retransmission correctness, audio synchronization, reconnect isolation,public networks, endurance, packaging, update/signing, cross-platform behavior,-or distribution licensing. Native remains outside the current release.+or distribution licensing. These were the limits of the August 27 canary,+before the later Native App integration.## Candidate Shape@@ -44,8 +48,9 @@ opaque window targetEach PeerConnection must keep independent ICE, DTLS-SRTP, SSRC/sequence/TWCC,pacing, RTCP, congestion, and retransmission state. PLI/FIR from any child may becoalesced into one shared SPS/PPS/IDR request, but a weak child's bitrate request-cannot silently become a room-wide minimum policy. A third Viewer must wait-under the same endpoint capacity as Web Hosts.+cannot silently become a room-wide minimum policy. That canary required a third+Viewer to wait under its then-current endpoint capacity; current placement is owned by+[routing and transport](../standards/routing-transport.md).Local target identity, HWND/PID, process creation time, adapter/MFT identity,PCM, and hardware diagnostics never enter signaling. The helper revalidates the@@ -112,9 +117,11 @@ frames and 500 Opus packets to a Chrome Viewer. The exact helper PID and adapterLUID matched nonzero `VideoEncode`. This is a one-Viewer functional proof, not aresource or product release claim.-## Remaining Gates+## Historical Canary Gates-Before native product consideration, one current-wire candidate must prove:+The August 27 canary required the following evidence before product consideration.+Later implementation and current acceptance are recorded in Native App media+and verification status, rather than this retired checklist:1. current room/Host authorization and source-generation cleanup;2. exact emitted H.264 profile negotiation and unmodified Chrome, Edge, Firefox,@@ -138,8 +145,9 @@ make a diagnostic gate pass.Pion is MIT and coder/websocket is ISC. The Windows APIs use installed hardwareMFTs; vendor codec binaries are not redistributed. Direct NVENC would require aseparate NVIDIA Video Codec SDK and redistribution review. H.264 patent and-project licensing remain unresolved before executable distribution. No-proprietary or GPL/AGPL implementation code was copied.+project licensing were unresolved at that canary boundary. Current distribution+terms and packaged notices are owned by the repository license and dependency+provenance. No proprietary or GPL/AGPL implementation code was copied.## Primary Sourcesdiff --git a/docs/research/network-lab.md b/docs/research/network-lab.mdindex dc98852e..ffb012af 100644--- a/docs/research/network-lab.md+++ b/docs/research/network-lab.md@@ -244,7 +244,9 @@ build the current binaries withThe four Browser/App directions exercise initial playback, a quiet source andrepainting, stop/reshare, either page reloading, joining before sharing, andreplacement during unfinished preparation. The last case releases real old SDP-after the new share is playing. The SFU-only case covers the same steady+after the new share is playing. Browser senders also receive a normal Viewer+ICE-restart request; both Browser and App receivers must recover on the same+playback connection. The SFU-only case covers the same steadylifecycle, and the two fallback cases delay real Peer signaling for 15 secondsto exercise SFU commitment followed by direct continuation. Video-frame callbackscheck that the working route keeps presenting during that continuation.diff --git a/docs/research/node-local-adaptation.md b/docs/research/node-local-adaptation.mdindex 1dd5c7ea..b194f47b 100644--- a/docs/research/node-local-adaptation.md+++ b/docs/research/node-local-adaptation.md@@ -1,10 +1,10 @@# Node-Local Adaptive Encoded Reuse-Reviewed: 2026-09-07. [ADR-0013](../adr/0013-embedded-node-local-media.md) now owns+Initial review: 2026-09-07. [ADR-0013](../adr/0013-embedded-node-local-media.md) ownsthe accepted model and highest-demand envelope. Codec backends and timing-values still need implementation evidence. ADR-0007 remains the runtime contract.-The [design proposal](./node-local-media-design.md) narrows implementation from-the measurements below; it does not reopen the agreed topology/reuse model.+values below describe that initial candidate. [Node-local media design](./node-local-media-design.md)+records the subsequent published implementation and recovery evidence;+[media quality](../standards/media-quality.md) owns current behavior.## Agreed Model@@ -27,7 +27,7 @@ server-side derivation needs real decoding/encoding capacity. Pure Browserpeers can consume normal WebRTC outputs and benefit from capable upstreams,but have no generic cross-PeerConnection encoded-forwarding API.-## Existing Building Blocks And Gaps+## Initial Building Blocks And Gaps- `mediaedge.Source` already forwards encoded RTP to multiple Pion edges.- Native GCC observations currently drive route evidence, not the shared@@ -145,8 +145,8 @@ retire their resources immediately; warm retention cannot revive a retired sourc| Pion mediadevices | Go codec-controller and raw-frame broadcast interface reference | Cgo/native codecs still exist; not a reason to replace the working capture adapters || GStreamer `tee`/queues and codec elements | Shared decode/frame flow and isolated output branches | `webrtcsink` currently encodes per consumer, so it does not directly solve shared derived output |-No candidate has yet demonstrated the complete local derivation, reuse and-recovery contract. The current inLive probe proves forwarding only. A small SFU+At the initial review, no candidate had demonstrated the complete local derivation,+reuse and recovery contract. The inLive probe proved forwarding only. A small SFUlibrary does not by itself replace a transcoder. Do not add FFmpeg/GStreamer orchange the current Native codec stack on documentation claims alone.diff --git a/docs/research/node-local-media-design.md b/docs/research/node-local-media-design.mdindex 3133ef9b..47d310c0 100644--- a/docs/research/node-local-media-design.md+++ b/docs/research/node-local-media-design.md@@ -356,8 +356,9 @@ or three on 3478/3479/3480 when prediction is enabled. `STUN_LISTEN_HOST`defaults to `0.0.0.0` and is separate from HTTP binding. Existing `STUN_URLS`remain advertised discovery addresses: their DNS may intentionally differfrom the Web origin, so they must not be resolved as local bind addresses.-The Local App constructor supplies no listeners; using public discovery-does not turn an App into a public STUN service.+Local LAN mode binds its Binding-only STUN listener on the selected LAN address+and App UDP port, including routed virtual LANs. Public-link mode uses external+discovery destinations instead; it does not expose a public App STUN service.The application binds HTTP and every required UDP listener before opening theroom database or accepting signaling. Failed startup returns all sockets;diff --git a/docs/research/realtime-quality-adaptation.md b/docs/research/realtime-quality-adaptation.mdindex 50b608a9..73ffcaa1 100644--- a/docs/research/realtime-quality-adaptation.md+++ b/docs/research/realtime-quality-adaptation.md@@ -1,13 +1,19 @@# Realtime Screen-Share Quality Evidence-- Reviewed: 2026-08-30+- Initial review: 2026-08-30; dated follow-ups record subsequent evidence- Scope: Browser game capture, codecs, startup adaptation, relay, and LiveKit-- Status: current evidence; product behavior is owned by+- Status: historical Browser/LiveKit SDK measurements and subsequent implementation evidence; current product behavior is owned by[media quality](../standards/media-quality.md) and[ADR-0007](../adr/0007-path-isolated-representation-quality.md)## Findings+The initial findings below describe the August Browser and external LiveKit+composition. [ADR-0013](../adr/0013-embedded-node-local-media.md) replaced that+service/SDK boundary with embedded media components. The later+[recovery ownership](#recovery-ownership) section distinguishes current paths;+SDK options below are retained measurement inputs.+- Browser settings are ceilings and content intent, not delivered floors.WebRTC owns each direct/relay edge's congestion adaptation; LiveKit owns SFUrepresentations and subscriber forwarding.@@ -295,7 +301,7 @@ not justify removing protection for H.264, other browsers or cold recovery.The [WebRTC statistics model](https://www.w3.org/TR/webrtc-stats/#dom-rtcoutboundrtpstreamstats-framesencoded)defines this counter per RTP stream, independently of a capture-track lifetime.-## LiveKit Representation Evidence+## Historical LiveKit SDK Representation EvidencePinned LiveKit client 2.22.0, server 1.13.5, and Chrome 151/153 showed thatdefault screen-share publication can expose original and lower representations,@@ -320,7 +326,7 @@ connections. This is an A/B input, not evidence to raise video priority.Forcing a lower encoding permanently consumed the same Host-to-SFU congestionbudget and reduced HIGH performance. Publishing only HIGH abandoned constrained-subscribers. Current ownership is therefore:+subscribers. The measured SDK composition used:- one Host publication for all SFU subscribers;- no custom `screenShareSimulcastLayers` and no backup codec;@@ -332,7 +338,7 @@ A LiveKit H.264 publication may use more than one hardware encoder for itsrepresentations. Reducing Host network copies to one publication does not byitself prove lower Host encode cost.-Production room 8489 separated an SFU delivery problem from route churn. SFU+An August SFU study separated an SFU delivery problem from route churn. SFUroots showed synchronized freeze windows and frequent 1920-to-960 representationchanges before any quality-convergence operation existed, while a brief directPeer Viewer remained near 1080p and full cadence. Later, the Host publication@@ -400,13 +406,10 @@ in [background capture research](./browser-background-capture.md), not Webkeepalive features.The Media Capture specification copies a track's constraints when it is cloned,-then lets each clone change constraints independently. Current Host Peer senders-trust those copied constraints when constructed, while the SFU publisher applies-the same profile again immediately after creating an equivalent clone. Removing-only that initial duplicate application should preserve constraints; clearing-clone constraints altogether would change the source/sink contract and requires-an A/B across live profile changes, sibling pressure, source replacement and-same-edge regeneration.+then lets each clone change constraints independently. At the initial review,+Host Peer senders trusted those copied constraints while the SDK SFU publisher+applied the same profile again. That observation did not establish a safe+constraint-removal change; the later embedded publisher owns its current path.Web content has no supported API for raising capture, encoder, renderer-process,GPU, or operating-system scheduling priority. WebRTC sender `priority` allocates@@ -417,8 +420,8 @@ does not change page visibility. Silent audio, animation loops, or command-linethrottling flags are therefore not product keepalive mechanisms.The mature resource choices remain bounded Browser P2P copies, one bounded SFU-publication when its accepted route condition applies, or a future native/shared-encoder. LiveKit Dynacast can stop unused SFU representations; it cannot combine+publication when its accepted route condition applies, or native/shared+encoding. LiveKit Dynacast can stop unused SFU representations; it cannot combineindependent P2P encoders.### Current Pipeline Screening@@ -557,7 +560,7 @@ low-cadence sources, measuring latency, loss, recovery and retained dimensions.The [Native QP comparison](./webrtc-encoder-pool.md#budget-restoration) tunesthat existing encoder owner; Browser internals expose a different API boundary.-Production room 4521 supplied a separate network/source boundary. It used one+A P2P study isolated a separate network/source boundary. It used onestable Host-to-Viewer P2P edge with no reparent or SFU activity, yet Chromiumreported `bandwidth` for 112 consecutive sender windows, estimated only0.42-1.78 Mbps available outgoing bandwidth, and adapted through 960p, 640p and@@ -747,7 +750,7 @@ into a scalar score or infer unconnected path quality. The single SFUpublication does not imply one quality node: Host ingress is generation-scoped,and every Viewer subscription needs its own candidate proof.-The 2026-08-27 room-6020 canary observed 21 successful Peer candidates, all+The 2026-08-27 canary observed 21 successful Peer candidates, allwithin two seconds, and 19 failed candidates with a 15-second median. Reusing theexisting five-second no-progress window for each background Peer candidatetherefore removes measured queue tail without adding a new threshold; candidates@@ -775,7 +778,7 @@ not copied. Overcast and End System Multicast demonstrate gradual measuredoverlay parent changes; their periodic probes, scalar metrics, publishedpercentages, and tuning loops are not adopted for this Browser product.-## Open Evidence+## Evidence Limits- Real games and sustained CPU/GPU contention across weaker Hosts and operatingsystems.@@ -793,10 +796,10 @@ percentages, and tuning loops are not adopted for this Browser product.- Controlled and production correlation of Host SFU active representations,subscribed-quality changes, keyframe reacquisition, and Viewer freezes beforechanging Dynacast policy or its delay.-- Current-Browser A/B of the duplicate initial SFU clone constraint application,- source-only versus independently constrained clones across live profile- changes, and default two-layer screen-share publication versus any proposed- representation or priority change.++The earlier SDK clone-constraint and representation comparisons belong to that+retired composition. Current pending work is owned by [TODO](../todo.md), and+[verification status](../verification-status.md) owns acceptance limits.## Primary Sourcesdiff --git a/docs/research/room-interactions.md b/docs/research/room-interactions.mdindex 6bd6aa2c..5597644e 100644--- a/docs/research/room-interactions.md+++ b/docs/research/room-interactions.md@@ -252,9 +252,9 @@ danmaku or reaction effects. Current behavior remains the no-history contract ab## Audio Boundary-The published Host microphone remains commentary mixed into the shared source.Text, reactions and danmaku introduce no microphone capture or additional media-peers. Room voice is an isolated prototype, not a published capability.+peers. Published room voice and optional Host source commentary follow the+[current microphone contract](../standards/media-quality.md#capture-and-controls).### Room Voice Transport Assessment@@ -263,9 +263,9 @@ room controller for voice and picture, with companion Browser audio connectionson its selected edges and an embedded Opus SFU adapter on the existing UDP mux.The earlier experiments below remain design evidence, not the distributableimplementation. Room voice now opens when the Host joins and closes when the-Host leaves. Room admission replaces per-person voice permissions. All current-microphone controls use room voice; pausing local listening leaves the microphone-on. Source sound stays with the picture. This follows the independent media+Host leaves. Room admission replaces per-person voice permissions. Pausing local+listening leaves the room-voice microphone on. Source sound stays with the picture.+This follows the independent mediasource model in [LiveKit](https://docs.livekit.io/intro/basics/rooms-participants-tracks/)and the [RTP synchronization boundary](https://www.rfc-editor.org/rfc/rfc8834.html#section-12.2.3):sharing a route does not guarantee synchronized playback across independent@@ -281,8 +281,10 @@ NAT success rates or 21-person all-speaking audio capacity.older checkout and records its revision, checking that an older Viewer retainspicture playback without subscribing to voice events.Product owners are [room voice](../standards/rooms-access.md#room-voice) and-[room media](../standards/routing-transport.md#room-media); the public release-does not yet include this candidate.+[room media](../standards/routing-transport.md#room-media). This integration shipped+in v2.4.0; the dated experiments below describe its earlier design evidence.++#### Historical Prototype AssessmentReviewed 2026-10-19. The owner requested optional self-hosted voice and assessmentof App-hosted voice. The local prototype forwards independent Opus publications,@@ -396,8 +398,10 @@ keeps `Host -> A -> B` video decoding while B's direct voice ICE is denied;lifting that denial and explicitly rejoining restores voice. A connected videoroute therefore cannot be displayed as proof of an available voice endpoint.-Voice integration also needs one Host microphone publication, rather than both-commentary and room voice carrying the same capture. System-audio capture can+The earlier prototype trial used one Host microphone publication, moving it out+of commentary. The current dual-microphone behavior is owned by+[media quality](../standards/media-quality.md#capture-and-controls), not this+historical trial. System-audio capture canrecapture received voices even with headphones; reuse supported audio exclusionand validate simultaneous capture/playback before making an echo-free claim.In the synthetic sharing trial, moving the Host microphone out of commentary@@ -804,8 +808,8 @@ admission counts Viewers rather than device types. The current Browsercollector uses one standard-mapped controller per page and reports at up to 60 Hz.No new media route, port or multiple screen publisher is required. Slot capacityfollows the backend and game, including physical controllers already in use;-it is not the room capacity. Real-game recognition and concurrent-player-acceptance remain in [TODO](../todo.md#now).+it is not the room capacity. Remaining Linux/macOS desktop and game-recognition+checks are tracked in [TODO](../todo.md#parked-product-work).### Window Capture And Game Input@@ -858,6 +862,16 @@ release during loss. Keep the first prototype on the existing ordered channel;desktop acceptance must not be presented as competitive-game readiness. Theseare input semantics and acceptance work, not reasons to replace the media pipeline.+Local component timing on 2026-10-24 used Windows and headless Chrome 155.+For 200 samples, Browser input dispatch and AES-GCM-to-send each had a 0.1 ms+95th percentile; ordered Browser SCTP loopback round-trip was 0.8 ms. Across+100 ten-point mouse bursts, the latest batched point reached send in 5.1 ms+at the 95th percentile, matching the short timer rather than a rendering-frame+wait. Encrypted Pion delivery through Host authorization to a recorded input+sink was 0.13 ms at the 95th percentile. No slow point appeared in these local+components. End-to-end timing still needs target execution, Native capture and+encoding, remote transport and physical display response in the same trial.+The reviewed implementation uses WGC capture and the documented Windows inputAPI, without game-memory access, injected game code or an input driver.[SendInput](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput)diff --git a/docs/research/server-consolidation.md b/docs/research/server-consolidation.mdindex 9a76c68e..b91d0e94 100644--- a/docs/research/server-consolidation.md+++ b/docs/research/server-consolidation.md@@ -14,7 +14,8 @@ reachability, and lifecycle policy. Within that scope `protocol` owns wire typesstrict decoding, and shared scalars; `config` owns environment validation plusthe Hosted and Local compositions; `room` owns room authority, participants, andthe SQLite database; `route` decides graph transitions without I/O; `signal`-executes signaling and SFU effects under one lock; `sfu` owns admission and+serializes room/signaling authority and commits SFU intent, then runs slow+media effects outside that lock with exact lifetime checks; `sfu` owns admission andembedded media through the shared Pion/LiveKit adapter; `app` owns HTTPcomposition, site access, static assets, and lifecycle; and `webassets` carriesthe embedded Browser bundle.diff --git a/docs/research/webrtc-encoder-pool.md b/docs/research/webrtc-encoder-pool.mdindex ac59a42c..54fc5343 100644--- a/docs/research/webrtc-encoder-pool.md+++ b/docs/research/webrtc-encoder-pool.md@@ -1,19 +1,18 @@# WebRTC Adaptation And Encoder Reuse-Reviewed: 2026-09-08. Source-backed review and synthetic VP8/H264 feasibility probes,-not an accepted engine replacement. [ADR-0013](../adr/0013-embedded-node-local-media.md)-owns the direct-child/shared-output model. [Status](../status.md) owns current-adoption; this investigation asks whether retaining mature encoder-control while sharing compatible encoding work is smaller and more complete-than extending the current Native fixed-output candidate.+Initial review: 2026-09-08. The early review and probes below compare the fixed-output+candidate with mature encoder adaptation. The later [Windows product attachment](#windows-product-attachment)+and [compatible output groups](#compatible-output-groups) record the published integration.+[ADR-0013](../adr/0013-embedded-node-local-media.md) owns the direct-child/shared-output+model; [media quality](../standards/media-quality.md) owns current behavior.## Pinned Baseline-The installed Chrome is 152.0.7977.82. Its [DEPS][chromium-deps] pins libwebrtc+The initial review used Chrome 152.0.7977.82. Its [DEPS][chromium-deps] pins libwebrtcto `6f37672d358475cd17544121a12494da454d85fb`; WebRTC references below use thatrevision, not a floating upstream branch. Source shows available mechanisms,not which field trials or hardware encoder a particular runtime selected.-The Piik comparison uses the `3c192ddd` candidate, not production.+The initial Piik comparison used the `3c192ddd` candidate, not production.## Actual Control Ownership@@ -66,7 +65,7 @@ independent version of that resizing path. The [simulcast encoder adapter][simulcoordinates per-output codecs, rates, scaling and keyframes; it is not a poolacross independent PeerConnections. Adding a third preset cannot establish parity.-## The Current Native Gap+## Fixed-Output Native Gap (2026-09-08)`nativecapture.ScreenShareOutputs` constructs original plus half-size outputsat the same FPS. `mediaedge.Source.BeginFrame` chooses activation and lowers the@@ -77,7 +76,7 @@ overload but do not automatically reduce the requested encoding work.Pion supplies transport; LiveKit media components supply BWE, forwarding,allocation, recovery and pacing. They do not supply the omitted libwebrtc raw-source/encoder resource-adaptation chain. The current source-RTP cost adjustment+source/encoder resource-adaptation chain. That candidate's source-RTP cost adjustmentalso is not the full `RtpVideoSender` overhead/protection calculation. These areconcrete missing integrations, not evidence that either library is defective.The existing constrained-codec recovery evidence remains valid within its scope,@@ -225,7 +224,7 @@ contain no frame payloads or private identifiers.- **The shared-encoder abstraction passes a narrow executable feasibilitycheck when physical correction state shares that owner.** It reuses realencoding, retains stock spatial adaptation and can split/rejoin safely in- this VP8 case. No engine replacement has been selected or integrated.+ this VP8 case. At that point, no engine replacement had been selected or integrated.- The group invokes correction on actual frames, with rate-write deduplication;stock VSE does so on rate/configuration/FPS updates. The algorithm is reused,but this cadence change must not be described as an otherwise identical move.@@ -295,7 +294,7 @@ neither this one-output replay nor a no-extra-hop result silently changes theaccepted envelope policy. The pool probe also has occasional split/rejoin work,so its healthy case is close to one encode per frame, not an unconditional bound.-### Minimal Integration Direction+### Initial Integration DirectionKeep the existing Pion/LiveKit transport, estimator, pacer and projection peredge. Codec-only `VideoStreamEncoder::OnBitrateUpdated` accepts an external@@ -660,9 +659,9 @@ or Chrome comparison. The 0.24 fps result belongs only to this helper. Keepoverload acceptance open without changing stock thresholds or adding a customCPU controller.-## Replacement And Preservation Map+## Initial Replacement And Preservation Map-| Current owner | Treatment if a pool-backed engine is proved |+| Owner at the initial review | Planned treatment || --- | --- || Go room/controller, embedded STUN, SFU service lifecycle | Preserve. A codec experiment does not alter authority, NAT policy or single-operation routing. || LiveKit forwarding/BWE/projection and Pion transport | Keep for existing encoded forwarding. Replace a Native endpoint owner only if the new library actually assumes that responsibility; never run two controllers on one edge. |@@ -675,28 +674,19 @@ Native room sessions, native ICE improvements and mixed Browser/App relay.Changing transport libraries does not automatically preserve these APIs. Nounrelated page rewrite or platform-adapter deletion is justified by this review.-## Integration Gates--1. Reuse the now-executed native VSE/VP8 boundary; do not repeat a mock-only- factory probe or fetch a second full engine to show buffer reuse. Validate- hardware H264 and physical-resource feedback with the existing capture adapter.-2. Measure source replacement, overload, real scheduling, perceptual quality,- transport overhead and actual CPU/GPU cost. Preserve correction/drop/resource- feedback with explicit owners before accepting the product integration.- The delayed-codec counterexample above means the per-sender factory cache- cannot be selected as-is; evaluate shared complete encoding pipelines before- adding another resource/membership coordination layer.-3. Connect the separately verified encoded bypass and shared lower derivation- through that same representation owner. If preserving stock- control requires substantial VSE/source patches or a custom rate/quality- policy, report that cost instead of calling it a small factory adapter.-4. The narrow pass permits an isolated implementation candidate, not release- approval. Select the integration through an ADR refinement after resolving- the remaining owners. Preserve the current candidate; never merge it solely- to manufacture a new branch base. Follow- the stable executable paths and serialized physical workload rules in- CONTRIBUTING. The quiet-source update defect has real WGC acceptance and the- owner closed the Win10 display report; distinct game failures require fresh evidence.+## Initial Integration Gate Record++The September 8 review required a complete shared encoding pipeline after the+per-sender cache failed the delayed-codec control. It also required received+encoded bypass, missing-output derivation, budget conversion, safe switching+and source retirement to retain their existing owners. The Windows product+attachment and compatible-output sections above record the resulting integration.++That initial feasibility pass was not release approval. Current device,+overload, perceptual-quality and public-network limits remain owned by+[verification status](../verification-status.md); [TODO](../todo.md) owns pending+work. The retained experiments and negative controls explain the selected+boundary without prescribing another completed migration.[chromium-deps]: https://chromium.googlesource.com/chromium/src/+/refs/tags/152.0.7977.82/DEPS[chromium-hint]: https://chromium.googlesource.com/chromium/src/+/refs/tags/152.0.7977.82/third_party/blink/renderer/modules/peerconnection/media_stream_video_webrtc_sink.ccdiff --git a/docs/standards/configuration.md b/docs/standards/configuration.mdindex 5b268690..43eec207 100644--- a/docs/standards/configuration.md+++ b/docs/standards/configuration.md@@ -20,7 +20,7 @@ service secret store or an untracked access-restricted environment file.| Variable | Contract || --- | --- |-| `PIIK_ENV` | `development` or `production`, default `development`; required for every server deployment: `production` enables production-only validation and the `Secure` site-access cookie. |+| `PIIK_ENV` | `development` or `production`, default `development`; set `production` for server deployments to enable production validation and Secure site-access cookies for configured HTTPS destinations. || `LISTEN_HOST` | Defaults to `0.0.0.0`; bare-metal production normally uses `127.0.0.1`. || `PORT` | Positive TCP port, default `8787`; the tracked release wrapper supports only that default. || `PUBLIC_BASE_URL` | Exact public HTTP(S) origin; production requires HTTPS. |diff --git a/docs/standards/documentation.md b/docs/standards/documentation.mdindex 7a7f98d7..ce69a46f 100644--- a/docs/standards/documentation.md+++ b/docs/standards/documentation.md@@ -13,7 +13,7 @@ verification procedures stay in [CONTRIBUTING.md](../../CONTRIBUTING.md).| Universal constraints and navigation | `AGENTS.md` | Short mandatory boundaries and links; no procedure manual || Reusable agent methods | `.agents/skills/*/SKILL.md` | Task triggers and methods under project constraints; link to domain rules and delivery rather than repeat them || Branches, review, validation, PRs, releases and cleanup | `CONTRIBUTING.md` | Executable workflow |-| Module boundaries, interface discipline and ablation | [Engineering](./engineering.md) | Current responsibility map and shared coding rules |+| Module boundaries, interface discipline and ablation | [Engineering](./engineering.md) | Module, contract, lifecycle and source navigation; shared coding rules || Names, terminology and voice | [Naming and copy](./naming.md) | Product/tool names, role labels and Chinese/English writing conventions || Translation contributions | [Translation guide](../guide/translating.md) and its Chinese counterpart | Catalog editing, new-language registration and translation review || Illustration, palette, motion and accessible layout | [Visual language](./visual-language.md) | Shared Browser/App design grammar |@@ -57,6 +57,8 @@ Introductions and the general guide link to those procedures rather than duplica2. Update the existing owner first. Add a new owner only for a distinct topicwith real consumers; give it an entry in the documentation map. Deletereplaced normative text elsewhere and link to the owner.+ When an entry point moves or a cross-module responsibility changes, update+ the engineering index and its links to the relevant verification entry points.3. Keep current owners consistent. Update memory/status only when their compactsnapshot materially changes; put unresolved work in TODO. Checkpoint andintegration timing follows CONTRIBUTING, not an automatic extra commit fordiff --git a/docs/standards/engineering.md b/docs/standards/engineering.mdindex 1dbc47bb..319c04e6 100644--- a/docs/standards/engineering.md+++ b/docs/standards/engineering.md@@ -53,6 +53,8 @@ and checks together; a smaller root alone does not justify another abstraction.| `src/client/pages`, `lib`, `media`, `native` | Browser orchestration and endpoint adapters; bind current media and retire owned resources || `src/client/media/viewer-presentation.ts`, `ui/media-status.ts` | Playback facts and their derived status projection; UI indicators do not create recovery or quality policy || `src/client/components`, `locales`, `ui` | Shared interaction, visual primitives and localized copy; same product interface for Server and App |+| `src/client/room-voice` | Room voice session and Browser endpoint; membership and audio resources outlive the interaction window independently of picture sharing |+| `src/client/remote-control` | Attended input sessions and forwarding over the committed media route; native input authority stays in `internal/app/remoteinput` || `internal/server/webassets` | One built Browser bundle embedded by both binaries; Node/Vite are development tooling |Names follow [naming](./naming.md). Architecture decisions explain why these@@ -74,6 +76,7 @@ Keep this check outside room startup, authorization and media lifecycles.| Room HTTP and Browser/Server signaling | `src/shared/protocol.ts` and `internal/server/protocol`; HTTP dispatch in `internal/server/app`, WS dispatch in `signal` || Site access HTTP | `src/client/lib/api.ts` and `internal/server/app/json.go`; cookies and access behavior belong to [rooms/access](./rooms-access.md) || Browser/App discovery and control | `src/client/native/wire.ts`, `internal/app/loopback/protocol.go`, `internal/app/nativecontrol/wire.go` |+| Remote input data channel | [Browser input protocol](../../src/client/remote-control/protocol.ts), [native input validation](../../internal/app/remoteinput/manager.go) and [channel routing](../../internal/media/inputroute); admission remains with the existing room and native control owners || App/capture sidecar | `internal/app/nativecapture` and each platform capture adapter; probe/commands and encoded-frame envelope are distinct formats || Configuration and persistence | `internal/server/config`, `internal/app/config`, `internal/server/room/database.go`; operator semantics in [configuration](./configuration.md) || Release and update metadata | Existing packaging scripts, `src/client/lib/release-update.ts`, `cmd/piik-server/release.go`; identity/compatibility in [versioning](./versioning.md), operations in [deployment](../deployment.md) |@@ -131,35 +134,52 @@ instead of mirroring it.| Browser Viewer route | `pages/ViewerPage.tsx` binds one parent peer (Browser or Native) and its relay children | parent, connection and message identity, then playback facts | a new viewer generation retires the old binding; relay children follow `activateChildren` revisions || Browser relay fanout | `webrtc/viewer-relay.ts` prepares a child before it is needed | `activateChildren(revision, childPeerIds)` commits the prepared set | replaced or emptied sets disconnect the child peers they own || Browser local encoding pool | `webrtc/host-peer.ts` attaches a pool carrier when capture and APIs support it ([ADR-0014](../adr/0014-browser-node-local-encoding-pool.md)) | the carrier binding owns its pooled producer while attached | source change or disposal releases the carrier; unsupported APIs keep ordinary senders |+| Browser/App control connection | `native/client-connection.ts` discovers lazily when the page requests App capability | one acquisition and exact `NativeClient` instance; the page owns eligibility and failure policy | explicit release, page/session retirement or App disconnect; late discovery closes only its own result |+| Room voice | `room-voice/session.ts` binds the admitted room session and its Browser endpoint | authoritative membership and the current publication/connection identity under [room media](./routing-transport.md#room-media) | leave, room retirement or session close releases owned audio; closing the interaction window does not leave voice |+| Floating room tool | `FloatingPanel.tsx` presents the tool; `ui/panel-window.ts` opens a supported detached window | the current window request and document own presentation, not the room session | close returns the tool to the page; opener/session retirement closes even a late open under [presentation lifecycle](./presentation-lifecycle.md) || App native session | the loopback service admits native control sessions and the Web UI selects the native path | session identity plus the explicit media-path choice | session end, share end or process exit retires that edge and its capture sidecar |+| Native ICE gathering | one gathering observes addresses on its connection's retained socket | candidates retain their source ICE generation; one collector joins Pion, STUN and gateway completion | ICE restart retires previous observations and excludes stale queued output; connection close retires the socket || Server room authority | `signal` authenticates one Host or Viewer command | `room` writes durably before the in-memory commit; the signaling lock serializes mutations | explicit replacement or deletion, grant rotation or revocation, or process exit for memory-mode rooms || Server route and SFU | `signal` feeds `route` one serial child operation; `sfu/admission.go` accepts one bounded publication | one committed graph per controller and the forwarding publication | superseded assignments retire inside the same operation; deactivate retires the publication |## Source IndexStart a change at the owning entry point, then follow the interfaces listed-above rather than searching the tree.+above rather than searching the tree. NAT policy stays in [routing](./routing-transport.md),+visual rules in [visual language](./visual-language.md), and website behavior and publication+in [introduction design](./public-introduction.md) and the [website runbook](../operations/website.md).| Change | Start at || --- | --- |-| Host workflow, share lifecycle, notices | `src/client/pages/HostPage.tsx`, `src/client/pages/host-page-notices.ts` |-| Viewer workflow, playback, status projection | `src/client/pages/ViewerPage.tsx`, `src/client/media/viewer-presentation.ts`, `src/client/ui/media-status.ts` |-| Received viewer metrics and expiry | `src/client/media/viewer-quality-evidence.ts` owns evidence rules; `viewer-quality-evidence-store.ts` owns received entries and timers for Host and relay views |-| Browser peer and route mechanics | `src/client/webrtc/host-peer.ts`, `viewer-peer.ts`, `viewer-relay.ts`, `src/client/media/host-sfu-route.ts`, `viewer-sfu-route.ts`, `route-transition.ts` |-| Capture, profiles and sender parameters | `src/client/media/quality.ts`, `src/client/media/browser-encoding-pool.ts`, `browser-encoding-output.ts` |-| SFU endpoints | `src/client/sfu/publisher.ts`, `subscriber.ts`, `peer.ts`, `internal/server/sfu` |-| Localized copy and failure facts | `src/client/locales`, `src/client/ui/copy.ts`, `src/client/ui/media-failure.ts` |-| Native bridge, capture and media edges | `src/client/native`, `internal/app/loopback`, `internal/app/nativecontrol`, `nativehost`, `nativeviewer`, `nativecapture` |-| Room authority, persistence and graph | `internal/server/signal`, `internal/server/room`, `internal/server/route` |-| Shared wire contract | `src/shared/protocol.ts`, `internal/server/protocol`, `tests/fixtures/wire-samples.json` |-| Diagnostics and logging | `src/client/lib/debug.ts`, `internal/diagnostics`, [configuration](./configuration.md#diagnostics) |-| Configuration and deployment | `internal/server/config`, `internal/app/config`, [configuration](./configuration.md), [deployment](../deployment.md) |+| Host workflow, share lifecycle, notices | [src/client/pages/HostPage.tsx](../../src/client/pages/HostPage.tsx), [src/client/pages/host-page-notices.ts](../../src/client/pages/host-page-notices.ts) |+| Viewer workflow, playback, status projection | [src/client/pages/ViewerPage.tsx](../../src/client/pages/ViewerPage.tsx), [src/client/media/viewer-presentation.ts](../../src/client/media/viewer-presentation.ts), [src/client/ui/media-status.ts](../../src/client/ui/media-status.ts) |+| Received viewer metrics and expiry | [src/client/media/viewer-quality-evidence.ts](../../src/client/media/viewer-quality-evidence.ts) owns evidence rules; [src/client/media/viewer-quality-evidence-store.ts](../../src/client/media/viewer-quality-evidence-store.ts) owns received entries and timers for Host and relay views |+| Browser peer and route mechanics | [src/client/webrtc/host-peer.ts](../../src/client/webrtc/host-peer.ts), [src/client/webrtc/viewer-peer.ts](../../src/client/webrtc/viewer-peer.ts), [src/client/webrtc/viewer-relay.ts](../../src/client/webrtc/viewer-relay.ts), [src/client/media/host-sfu-route.ts](../../src/client/media/host-sfu-route.ts), [src/client/media/viewer-sfu-route.ts](../../src/client/media/viewer-sfu-route.ts), [src/client/media/route-transition.ts](../../src/client/media/route-transition.ts) |+| Capture, profiles and sender parameters | [src/client/media/quality.ts](../../src/client/media/quality.ts), [src/client/media/browser-encoding-pool.ts](../../src/client/media/browser-encoding-pool.ts), [src/client/media/browser-encoding-output.ts](../../src/client/media/browser-encoding-output.ts) |+| SFU endpoints | [src/client/sfu/publisher.ts](../../src/client/sfu/publisher.ts), [src/client/sfu/subscriber.ts](../../src/client/sfu/subscriber.ts), [src/client/sfu/peer.ts](../../src/client/sfu/peer.ts), [internal/server/sfu](../../internal/server/sfu) |+| Localized copy and failure facts | [src/client/locales](../../src/client/locales), [src/client/ui/copy.ts](../../src/client/ui/copy.ts), [src/client/ui/media-failure.ts](../../src/client/ui/media-failure.ts) |+| Native control connection and media adapters | [src/client/native/client-connection.ts](../../src/client/native/client-connection.ts), [src/client/native](../../src/client/native), [internal/app/loopback](../../internal/app/loopback), [internal/app/nativecontrol](../../internal/app/nativecontrol), [internal/app/nativehost](../../internal/app/nativehost), [internal/app/nativeviewer](../../internal/app/nativeviewer), [internal/app/nativecapture](../../internal/app/nativecapture) |+| Room authority, persistence and graph | [internal/server/signal](../../internal/server/signal), [internal/server/room](../../internal/server/room), [internal/server/route](../../internal/server/route) |+| Room signaling intent and interaction delivery | [src/client/lib/signaling.ts](../../src/client/lib/signaling.ts), [src/client/lib/room-interactions.ts](../../src/client/lib/room-interactions.ts); admission and transient delivery follow [rooms and access](./rooms-access.md) |+| Shared wire contract | [src/shared/protocol.ts](../../src/shared/protocol.ts), [internal/server/protocol](../../internal/server/protocol), [tests/fixtures/wire-samples.json](../../tests/fixtures/wire-samples.json) |+| Diagnostics and logging | [src/client/lib/debug.ts](../../src/client/lib/debug.ts), [internal/diagnostics](../../internal/diagnostics), [configuration](./configuration.md#diagnostics) |+| Configuration and deployment | [internal/server/config](../../internal/server/config), [internal/app/config](../../internal/app/config), [configuration](./configuration.md), [deployment](../deployment.md) |+| ICE sockets, gateway mapping and candidate gathering | [internal/app/mediaedge/ice_socket.go](../../internal/app/mediaedge/ice_socket.go), [internal/app/mediaedge/candidate_gathering.go](../../internal/app/mediaedge/candidate_gathering.go), [internal/app/mediaedge/stun.go](../../internal/app/mediaedge/stun.go), [internal/app/portmapping](../../internal/app/portmapping) |+| NAT evidence and App punch preparation | [src/client/webrtc/nat-prediction.ts](../../src/client/webrtc/nat-prediction.ts), [src/shared/nat-candidate.ts](../../src/shared/nat-candidate.ts), [src/client/native/native-punch.ts](../../src/client/native/native-punch.ts), [internal/app/mediaedge/punch.go](../../internal/app/mediaedge/punch.go), [internal/server/stun/server.go](../../internal/server/stun/server.go); controls and execution belong to the [network laboratory](../research/network-lab.md) |+| Route scheduling, retry opportunities and deadlines | [internal/server/route/operation.go](../../internal/server/route/operation.go), [internal/server/route/opportunities.go](../../internal/server/route/opportunities.go), [internal/server/route/candidates.go](../../internal/server/route/candidates.go); routing decisions stay under [ADR-0005](../adr/0005-automatic-hybrid-media-routing.md) |+| Picture attachment and retained room edges | [internal/server/route/publication.go](../../internal/server/route/publication.go), [internal/server/route/graph.go](../../internal/server/route/graph.go); separate picture and voice lifetimes follow [room media](./routing-transport.md#room-media) |+| Room voice session, endpoint and roster | [src/client/room-voice/session.ts](../../src/client/room-voice/session.ts), [src/client/room-voice/browser-endpoint.ts](../../src/client/room-voice/browser-endpoint.ts), [src/client/room-voice/participant-presentation.ts](../../src/client/room-voice/participant-presentation.ts), [src/client/room-voice/RoomVoicePanel.tsx](../../src/client/room-voice/RoomVoicePanel.tsx) |+| Remote input and committed-route forwarding | [src/client/remote-control/input-session.ts](../../src/client/remote-control/input-session.ts), [src/client/remote-control/input-route.ts](../../src/client/remote-control/input-route.ts), [src/client/remote-control/protocol.ts](../../src/client/remote-control/protocol.ts), [internal/app/remoteinput](../../internal/app/remoteinput) |+| Interaction window and detached presentation | [src/client/components/living/RoomInteractions.tsx](../../src/client/components/living/RoomInteractions.tsx), [src/client/components/living/FloatingPanel.tsx](../../src/client/components/living/FloatingPanel.tsx), [src/client/ui/panel-window.ts](../../src/client/ui/panel-window.ts), [src/client/components/living/use-anchored-popover.ts](../../src/client/components/living/use-anchored-popover.ts) |+| Visual primitives, guidance and preview | [src/client/ui/palette.css](../../src/client/ui/palette.css), [src/client/ui/theme.ts](../../src/client/ui/theme.ts), [src/client/ui/icons.tsx](../../src/client/ui/icons.tsx), [src/client/components/living/primitives.tsx](../../src/client/components/living/primitives.tsx), [src/client/components/living/Tooltip.tsx](../../src/client/components/living/Tooltip.tsx), [src/client/pages/ControlsPreview.tsx](../../src/client/pages/ControlsPreview.tsx) |+| Public website and film presentation | [site/index.html](../../site/index.html), [site/main.js](../../site/main.js), [site/styles.css](../../site/styles.css), [site/films](../../site/films), [scripts/build-website.mjs](../../scripts/build-website.mjs) |## Implementation Rules- Reuse a current owner, standard API or mature dependency before adding a- mechanism. Extract around a responsibility shared by real callers; file size- alone does not justify wrappers, a generic manager or a framework.+ mechanism. Extract a complete responsibility for its real consumers, keeping+ resource acquisition, replacement and retirement together. File size alone+ does not justify wrappers, a generic manager or a framework.Remove superseded paths in the same change; retain no unused legacy copy.Keep test-only state seeding and setup in test harnesses.Do not export mutable resources solely for tests; preserve the production@@ -182,8 +202,10 @@ above rather than searching the tree.operation/resource identity before changing current state. Cancellation,failure, missing observation and success must retain distinct meanings.Queued events retain physical-owner validity until the final serialized write;- reused logical IDs do not identify a replacement instance. New subscriptions- follow the old owner's retirement, including repeated cleanup callers.+ readiness commits also require the resource's current state. Reused logical+ IDs do not identify a replacement instance. A child's attachment failure does+ not retire its shared producer. New subscriptions follow the old owner's+ retirement, including repeated cleanup callers.Closing a Browser connection may leave its API promises pending. Its ownercancels the corresponding waits so a serialized successor never depends oncompletion from a retired connection; late results still need identity checks.@@ -204,8 +226,9 @@ above rather than searching the tree.- Keep decisions separate from effects. Respect documented lock ordering andrevalidate after unlocked work. Local native capability never grants roomauthority; the shared Go service remains authoritative.- Injected callbacks still execute in their caller's context; include their- blocking I/O in the caller's lock and latency analysis.+ Injected callbacks still execute in their caller's context. Synchronous+ authority hooks must not hide blocking I/O or release the caller's lock;+ schedule slow work at an explicit, owned effect boundary.- Validate input at the boundary, keep domain error meanings stable, and mapthem into shared localized UI. [Versioning](./versioning.md) determines whichextensions are compatible; an optional field is not automatically compatible.diff --git a/docs/standards/media-quality.md b/docs/standards/media-quality.mdindex 81026398..426e6ff7 100644--- a/docs/standards/media-quality.md+++ b/docs/standards/media-quality.md@@ -434,7 +434,7 @@ lower video output; each outbound PeerConnection owns its RTP identity andtransport feedback. When an edge ispersistently degraded, the existing quality operation may prepare a BrowserWebRTC sender from the stable local bridge as that edge's candidate. The old-edge stays live until the Viewer proves the candidate is better; a failed+edge stays live until the Viewer completes the route owner's relative quality proof; a failedcandidate rolls back, and a later operation may return the edge to the sharednative source. This gives one difficult path stock per-sender adaptation withoutlowering the shared representation for healthy paths. It adds no quality score,diff --git a/docs/standards/naming.md b/docs/standards/naming.mdindex e321048d..b4cebc4c 100644--- a/docs/standards/naming.md+++ b/docs/standards/naming.md@@ -36,8 +36,10 @@ Keep metadata and ordinary prose as the continuous sentence.Keep the public brand line consistent in the homepage, film ending and READMEopening, beside a plain screen-sharing description. Scene titles should namethe action or benefit; do not turn every section into another slogan. Games, drawings, photos and-movie nights show the range of screen sharing without implying voice chat,-shared input or synchronized playback controls. Each locale must read naturally+movie nights show the range of screen sharing. Scenes may demonstrate room+voice and attended remote control when they show the required participation and+permission; sharing alone does not enable either. Do not imply synchronized+playback controls. Each locale must read naturallyas a complete sequence, without depending on a pun from the other language.Set the register by the reader's task:diff --git a/docs/standards/rooms-access.md b/docs/standards/rooms-access.mdindex 1bfc7fab..08194faa 100644--- a/docs/standards/rooms-access.md+++ b/docs/standards/rooms-access.md@@ -73,7 +73,7 @@ Three independent authorities exist:active. It authorizes room creation, Host role, and code-only Viewerattempts; it is not a room credential, and Viewer-grant admission cannotcreate or renew it. Cookie transport attributes follow the configured- destination: HTTPS retains the Secure host-prefixed cookie, while a+ production destination: HTTPS retains the Secure host-prefixed cookie, while aseparately configured HTTP origin, such as App loopback, uses its HTTP cookie. Login,renewal, room creation and WebSocket admission use that same selection.Cookie signatures use a random process key independent of the password;@@ -154,6 +154,11 @@ page's conversation, but refresh, leaving or authority loss clears it. There isno history replay or offline outbox. HTTPS/WSS protects transport; chat is notend-to-end encrypted, and the site operator can access live message contents.+A send-confirmation timeout releases the send control without retransmitting.+A late reply on the same connection can still settle the latest submitted send;+another submitted send or reconnection retires that confirmation identity. Only confirmation+of the matching chat text clears its draft.+[Presentation and lifecycle](./presentation-lifecycle.md#product-surface) ownsthe distinction between a room session and its current media publication.[Interaction research](../research/room-interactions.md) records delivery bounds,diff --git a/docs/standards/routing-transport.md b/docs/standards/routing-transport.mdindex c28b8298..544ec7e7 100644--- a/docs/standards/routing-transport.md+++ b/docs/standards/routing-transport.md@@ -68,6 +68,9 @@ sharing. The server-validated route policy remains fixed for that share generatiThese are route-policy gates, not new routing algorithms. Changing anypolicy requires stopping the current share and starting another generation.+Voice can establish the room graph before a picture share, using the site's+default route policy. Starting a share applies its Host-selected policy to that+same room controller; voice has no separate policy or route planner.## Viewer Route Choice@@ -131,9 +134,13 @@ A candidate carrying picture and voice commits only after both decoded-pictureproof and the required voice transports connect. A silent voice-only candidateuses actual transport connection, without requiring samples or a video frame.Video-only attachment to an already committed room edge uses its own fresh-physical identity and decoded-frame proof. Retired facet callbacks cannot-invalidate a replacement. Stop, pause and failure statuses describe the affected-consumer; a voice failure does not become a video error.+physical identity and decoded-frame proof. Failed attachment evidence ends when+an involved endpoint session changes; proven picture and room transports remain.+Retired facet callbacks cannot+invalidate a replacement. Reusing a room reservation waits for its previous+picture facet's physical close and reader join; full retirement joins those+facets before releasing admission. Stop, pause and failure statuses describe the+affected consumer; a voice failure does not become a video error.When current decoded-picture proof still establishes a healthy committed path,a failed companion voice transport is recreated on that same path withoutinvalidating picture readiness or consuming a topology candidate. Without that@@ -161,10 +168,21 @@ without discarding its subtree; departure and capacity changes move only affectechildren. ADR-0005 owns filtering, ordering, cursor, revision, reservation, androllback mechanics.-Reauthentication rebinds retained media and restores eligible direct opportunities-behind healthy SFU edges through that same continuation owner. The existing-session-scoped ledger bounds these attempts; a repeated update of the same-session does not reopen them.+Reauthentication rebinds retained media. Newly eligible parents and session+opportunities restore direct convergence behind healthy SFU edges through that+same continuation owner. The existing session-scoped ledger bounds these+attempts; repeated capacity or session updates do not reopen spent opportunities.++Trickled candidates and their completion retain the gathering's ICE identity+across SDP exchanges. A delayed completion cannot close a newer gathering;+ordinary renegotiation does not create a new ICE generation. Address observations+belong to that gathering, even when ICE restart retains the physical socket.+The merged collector owns completion across ordinary and supplemental candidates.++Transport readiness uses the current PeerConnection state when consumed,+including after Native or SFU event queues. Historical transport loss+still retires input permission; a recovered connection requests a fresh input+handshake rather than restoring the previous grant.Five seconds is a scheduling window only while another bounded candidate or SFUfallback remains. It advances a silent Peer candidate instead of delaying the@@ -202,9 +220,9 @@ resets it; replacing an unconnected backend cannot renew recovery indefinitely.Capable Native receivers also renegotiate that connection while retaining theencoded source, local playback and downstream edges when its codec/audio shapeis unchanged. Pion owns ICE restart; each actual gathering owns fresh supplemental-candidates. Native STUN observations share only an in-flight transaction, never-a completed address across gatherings. Discovery uses the existing media socket;-retiring one gathering does not cancel another or close healthy media.+candidates. Native STUN observations share an in-flight transaction only within+one gathering. Discovery uses the existing media socket; retiring one gathering+does not cancel another or close healthy media.Each actual gathering also refreshes a previously successful gateway mapping;the requested lease does not prove that the gateway retained its state. OrdinarySDP renegotiation does not refresh mappings, and this adds no background poller.diff --git a/docs/standards/visual-language.md b/docs/standards/visual-language.mdindex b0c8e721..b43cc740 100644--- a/docs/standards/visual-language.md+++ b/docs/standards/visual-language.md@@ -253,7 +253,8 @@ on press, and spring back over 260 ms on release. Selected and unselected statesuse the same motion; selection changes only their colour and outline.Inputs and option tiles share softer corners. Press feedback uses the sharedmotion values in `button.css` and releases the lower shadow. Captions inside-sliding tracks retain a smaller 1 px lift without moving the track. Keep icon,+sliding tracks stay centred on their selection surface during hover and focus;+presses retain the shared feedback. Keep icon,text and control dimensions intact: scaling the whole button shifts off-centre contentssideways and changes their stroke weight. Do not add a separate icon displacement.Keep filled buttons' border and face in the same colour state; a bright inset@@ -381,7 +382,7 @@ changing the badge does not restyle the UI, wordmark or illustrations.| A camera source | `camera` identifies the alternative picture source. Source/playback sound retains the speaker metaphor. || Room text and reactions | `chat` opens the conversation, `danmaku` controls its optional picture overlay, `smile` identifies reactions and `send` submits the chosen content. These do not represent media transport or voice. || Room voice | `microphone` / `microphoneOff` identify input state; `headphones` / `headphonesOff` pause or resume room-voice listening, while `speaker` / `speakerOff` mute one person locally. Input, playback and forwarding remain independent. Toggle names stay fixed. The microphone's green fill shows measured input after gain, not the configured gain percentage. Participant rows keep avatar, name and microphone aligned; the status symbol has an accessible name and shared tooltip instead of a repeated on/off sentence. Measured speech fills that symbol and colours its outline green, without adding a connection-like dot. An enabled input alone is not speaking evidence. Opening the channel and opening a microphone stay separate actions. The Host has labelled open/close-room actions, with `stop` and danger styling for closing everyone's voice; a Viewer's `door` leaves only their own voice participation. Joined membership does not use a network-strength symbol. |-| Experimental attended desktop control | `mouse` identifies keyboard/mouse control, distinct from sharing or playback. Permission copy stays literal; an active control marker describes the grant, not media connectivity. |+| Attended desktop control | `mouse` identifies keyboard/mouse control, distinct from sharing or playback. Permission copy stays literal; an active control marker describes the grant, not media connectivity. || Floating utility placement | `grip` marks a draggable title bar; corner handles resize the window. Closing a panel hides its presentation, not its room session. || Server forwarding media | A server on the media path; opening a Site alone does not imply SFU |diff --git a/docs/status.md b/docs/status.mdindex 424a4820..12555e16 100644--- a/docs/status.md+++ b/docs/status.md@@ -1,6 +1,6 @@# Current Status-Last updated: 2026-10-23+Last updated: 2026-10-24This is the compact execution/deployment index. Product modules own behavior,[verification status](./verification-status.md) summarizes material evidence limits,@@ -18,15 +18,6 @@ and Git/PRs own completed history.corresponding authority. Explicit memory mode and App Local end rooms atprocess exit. Site access retains its separate 24-hour idle lifetime withinone service run; service restart requires site-password entry again.-- Browser/App entry lifecycle and current-edge reconnect ownership repairs- are implemented. Browser node-local pooling and detailed local Debug export- remain part of the accepted media surface. Product modules own their behavior;- [configuration](./standards/configuration.md#diagnostics) owns Debug activation,- report contents and disclosure.-- Shared feedback composition is implemented: generic loading uses the wink- mascot, concrete media waits use their owning comic, and waiting captions are- secondary to literal status. Website reduced-motion preference and player- tooltip placement follow the same presentation owners.- Camera sharing and room voice are accepted product features. Source audiofollows the picture; room voice owns conversation microphones under thesame route controller. Optional Host commentary belongs to the share's audio@@ -35,158 +26,72 @@ and Git/PRs own completed history.[capture assessment](./research/camera-and-microphone.md) owns platform behaviorand platform limits.-Website delivery includes a bilingual reader documentation center generated-from the repository's guides alongside the homepage, with search and detailed-troubleshooting separate from the homepage FAQ.-## Accepted Interaction Phase-The accepted [room-interaction design](./research/room-interactions.md) includes-the verified media repairs. It adds authenticated text and reactions-over existing signaling, with optional local chat overlay and a shared floating-panel. Its opted-in room session can outlive a publication; capture, quality and-route owners remain unchanged. Optional Windows screen-audio exclusion uses the-existing source/mixer boundary. Hosted Server-also accepts an optional SFU-only policy through the same route controller.-Windows native capture and thumbnails share automatic HDR-to-SDR conversion.-The [fidelity assessment](./research/media-fidelity.md#hdr-to-sdr) owns its evidence-and the distinction from Browser capture and end-to-end HDR.-Room voice supplies Host and Viewer speech separately from source audio.-Release artifacts and operator deployment records own delivery completion;-[TODO](./todo.md#now) owns remaining work.+Text chat, reactions, room voice and attended remote control are accepted product+features. They use existing room authority and the shared route controller;+picture, voice and input grants retain their separate lifetimes. The+[room-interaction design](./research/room-interactions.md) records the design;+[product standards](./standards/README.md) own current behavior.+Release artifacts and operator records own delivery completion.## Current Work-The current 2.x release line retains attended keyboard/mouse control, Viewer-playback settings and the interaction window, with the maintenance media and lifecycle-repairs. The accepted maintenance phase preserves the published protocols and-route owners; release artifacts and operator records establish publication and-postflight completion. Room voice integrates normal App/Server/Web, with the same-room graph and serial route operation, separate picture and voice lifetimes, explicit pre-share room creation-and Host-owned room voice activation. Normal Browser checks cover a three-participant Peer-relay and embedded SFU, duplex audio, local-mute isolation, picture replacement,-detached UI and capture retirement. Optional voluntary Peer fanout and local-picture rotation are included; frp uses the documented existing UDP configuration.-Windows x64 Server packaging uses the existing-release inventory; Docker images remain Linux-only.-App Local invitations now use HTTPS on the selected LAN address; the Host keeps-its loopback entry. Hosted Server also supports optional direct HTTPS with a-temporary local certificate or supplied certificate files. Both use the same-room authority and require the browser to accept the certificate.-Channel availability derives from Host membership and uses one-room microphone for Host and Viewers. Local normal-page acceptance covers both-P2P and SFU, independent listening, picture continuity and voice-facet recovery.-Controlled-network voice cases and platform package checks have passed; the-release artifacts and operator records establish publication and deployment.-Shared keyboard/mouse and optional Windows ViGEmBus virtual gamepads-use the normal App/Web input and authorization surfaces. Host and Viewers see-granted collaborators on the couch through a share-scoped presence projection.-The Windows adapter-calls the installed driver directly; App packages contain no optional input SDK-or driver. Linux uinput passed-an isolated real-kernel gate; desktop permissions and game recognition remain-unverified. The macOS virtual-HID-implementation needs its Apple entitlement and a macOS build/runtime check.-Co-play and the maintenance changes form one accepted integration phase;-release artifacts establish publication and the platform limits above remain.-The website uses the selected bilingual film A with on-demand loading and-shared product playback controls; the final card stays visible after playback.-[TODO](./todo.md) owns unfinished work and explicit deferrals;-release artifacts and operator records own exact delivery results.--2.0 separates numeric media requests from presets and bounded runtime admission,-retains successfully applied quality settings, and lets App serve an approved-Site independently of Local/public room startup. It also adds detached chat on-supporting desktop Browsers. Media, room authority and P2P-first routing keep-their existing owners; [product standards](./standards/README.md) define the-accepted behavior.+The maintenance line preserves published protocols, compatibility and route+ownership. [TODO](./todo.md) is the only unfinished-work ledger; Git and PRs own+completed fixes. [Verification status](./verification-status.md) owns material+acceptance and platform boundaries.++Room voice, shared keyboard/mouse and optional Windows virtual gamepads use the+normal App/Web interfaces. App packages contain no optional input SDK or driver.+Linux uinput has an isolated kernel check; desktop permission and game-recognition+coverage is pending. macOS virtual HID needs an Apple entitlement and physical+build/runtime checks. The [input assessment](./research/room-interactions.md#remote-co-play)+owns platform details.++App Local invitations use HTTPS on the selected LAN address, with a loopback+entry for the Host. Hosted Server supports direct HTTPS with temporary or+operator-supplied certificates. The [HTTPS runbook](./operations/self-hosting.md)+owns setup. Virtual LANs and frp use the existing room and UDP media paths.+The website uses the selected bilingual film A, loaded on demand with shared+product playback controls and a persistent final card.Optional coordinated Native UDP preparation augments the existing retry owner.The [network laboratory](./research/network-lab.md) covers all Browser/Apppairings, recovery, SFU policies and successful two-App preparation throughsequential kernel NAT. These results do not establish a public success rate.-Magicsock remains excluded: its [controlled comparison](./research/nat-traversal.md#integration-cost-and-recovery-controls)-established no additional reachability worth its integration costs.+Magicsock integration is not planned; its [controlled comparison](./research/nat-traversal.md#integration-cost-and-recovery-controls)+records the technical basis.The [media assessment](./research/native-client-media.md#media-capability-extension-assessment)owns bounded output and live-control evidence. Additional codecs, surrounddelivery and end-to-end HDR remain outside the published phase.-2.0 was published after final acceptance without a separate field beta.-Immutable release artifacts and operator records own exact delivery-and postflight; [TODO](./todo.md#now) owns remaining product work. Hosted room-retention stays indefinite by default. Demo uses one hour without authenticated-participants; the private deployment keeps the default.-## Deployment-Source, package identity, GitHub repository and local worktree paths use Piik.-Runtime code keeps only the Piik wire labels and stored keys.The immutable release descriptor, runtime `REVISION` and operator deploymentrecord own exact identity and postflight results.-The existing deployment uses one Go process for Web, room authority/signaling,-Binding-only STUN on UDP 3478/3479/3480 and optional SFU on UDP 7882. nginx owns-HTTPS. Participants, routes and media remain process-only. This private service-is not a public demonstration.+The private deployment uses one Go process for Web, room authority/signaling,+STUN and optional SFU, with nginx for HTTPS. The+[self-hosting runbook](./operations/self-hosting.md) owns listeners and network+configuration.The separate US [public demo](https://demo.piik.tv) is deployed with open siteentry, persistent room authority and P2P-only media. Cloudflare fronts HTTP/WSSand caches static bundles; STUN uses a separate DNS-only hostname. Caddy ownsorigin HTTPS, and the standard systemd service runs the published Server package.-Public DNS,-HTTPS, runtime assets, all three STUN listeners and a bounded browser P2P-sharing/stop check passed; the browser check used synthetic video on one network.-The operator record owns exact identity and postflight evidence.--[TODO](./todo.md) owns unfinished work. The owner declared the first-public release on 2026-09-12. Source is public, main protection is active, and-`piik.tv` runs on GitHub Pages behind Cloudflare with HTTPS. The private service-remains a separate deployment. Release descriptors and GitHub deployment records-identify published artifacts and website revisions.--GitHub owns the original Server and registered App packages. Gitee copies them-without rebuilding; mirror completion requires anonymous checksum verification.-Mirror reads retry complete transfers; ambiguous uploads are reconciled by-inventory before another attempt. Public-repository CI-and native packaging passed. App entry, update links-and bounded two-build interoperability have acceptance evidence. Accepted-product changes publish automatically; standalone website and documentation-changes validate and deploy the website without creating App/Server versions.-GitHub/Gitee release comparison, mirror publishing and PR-sourced notes follow-[versioning](./standards/versioning.md). [Verification status](./verification-status.md#device-evidence-boundary)-summarizes material evidence limits; shared lessons live in-[engineering](./standards/engineering.md).--## Media Evidence And Limits--- Compatible Native consumers share one complete stock WebRTC encoding and- adaptation pipeline per local output. Different weak demands stay independent;- suitable received media can be forwarded without decoding/re-encoding.- Pion/LiveKit retain transport, estimation, pacing and forwarding ownership.-- Windows H264/VP8 have bounded encoding/adaptation evidence. Actual Native- relay checks cover compatible sharing, weak-budget separation and rejoining,- source retirement and large encoded-frame delivery. These results do not- establish a universal encoder count, CPU saving or Browser-equivalent quality.-- Windows Host acceptance covers live and paused settings, quiet-source- preparation/restore, source changes and subsequent sharing. The owner- confirmed Windows 10 whole-display capture resolved. Specific new game/device- failures should be reopened from new evidence.-- Native Host publication through embedded SFU retains video/audio; Browser to- App fanout and App-exit fallback have bounded physical checks. Two- independent Native sessions have simultaneous media/retirement evidence.-- The Linux runtime container has non-root/read-only, memory/SQLite, diagnostic- export, STUN and listener-lifecycle evidence. This does not establish every- operator's proxy/firewall or public-media configuration.-- The independent Browser producer in ADR-0014 is integrated.- Bounded H264/VP8 checks cover direct and received-track sharing, weak-child- isolation/recovery, live/quiet settings, pause, source change and ordinary- fallback. Its native allocation and complete-frame output replace the earlier- payload-only Worker path. Fewer full encodes are established; total CPU gains- depend on codec/hardware. [Browser research](./research/browser-local-encoding-pool.md)- owns these measurements and failed controls. The owner accepted this scoped- Browser composition for release; actual deployment identity stays in the- runtime descriptor.-- Browser-controlled recovery and extreme CPU-overload limits remain in- [encoder research](./research/webrtc-encoder-pool.md). Windows App and Browser- are this phase's primary targets; macOS/Linux physical capture remains deferred.+Demo expires rooms after one hour without authenticated participants; the private+deployment retains the default of no expiry. The operator record owns exact+identity and postflight evidence. `piik.tv` runs on GitHub Pages behind+Cloudflare; website delivery includes a bilingual documentation center built+from repository guides.++GitHub owns original Server and App packages; Gitee mirrors them without+rebuilding and verifies anonymous downloads against their checksums. Server+packages cover Linux x64/ARM64 and Windows x64; Docker images are Linux-only.+App packages cover Windows x64 and macOS/Linux x64/ARM64.+Accepted product changes publish through the release workflow; standalone+website/documentation changes deploy the site without creating App/Server+versions. [Versioning](./standards/versioning.md) owns release identity, notes,+compatibility and mirror publication.diff --git a/docs/todo.md b/docs/todo.mdindex 32440ff4..a3dd9023 100644--- a/docs/todo.md+++ b/docs/todo.md@@ -1,37 +1,49 @@# Current TODO Ledger-Last reviewed: 2026-10-23+Last reviewed: 2026-10-24This ledger contains unfinished work and explicit owner holds. Product modulesown behavior; research and Git/PRs own evidence and completed history.## Now+- [ ] **Remote-control end-to-end latency.** Measure input-to-visible-response+ on an attended target, covering OS execution, Native capture/encoding, remote+ transport and display in the same trial. The+ [input assessment](./research/room-interactions.md#window-capture-and-game-input)+ owns completed local component measurements.- [ ] **Windows window-sharing black screen (#495).** Investigate[#495](https://github.com/TNTcraftHIM/Piik/issues/495): Windows 10, Chrome 155,- Piik v2.3.0; App window sharing is black while full-screen sharing works.- The attachment shows the Host selecting its own Chrome window, then a black- preview after sharing starts. This path works in local Windows 11 / Chrome 155- checks. A same-attempt App/Web report from the affected device is needed to- distinguish capture output from decoding. Keep this investigation separate- from the release; it is not covered by the macOS #492 crop fix.+ Piik v2.3.0; native Chrome/Edge window sharing fails while QQ and Browser+ capture work. The latest report uses VP8: transport connects, but capture+ remains `starting` with no video frames. Obtain the App failure stage or compare+ the existing Intel/NVIDIA adapter choices with VP8 fixed on that device.+- [ ] **Intel native H.264 startup (#452).** Follow the+ [new v2.4.0 report](https://github.com/TNTcraftHIM/Piik/issues/452): explicit H.264+ fails while VP8 works on Intel Arc B580 / Intel Graphics. The Browser report+ records App startup rejection; an App diagnostic is needed to identify its+ native failure stage.- [ ] **Return LiveKit to an upstream pin.** Follow[upstream #5016](https://github.com/livekit/livekit/pull/5016). Once upstreamsupplies the allocator input Piik needs, verify recovery and remove thetemporary replacement. [Dependency provenance](../internal/thirdparty/README.md#local-encoder-probe-demand)owns the patch and removal boundary.+## Next++- [ ] **Release headlines in App and Server update checks.** Keep the prepared+ implementation on the feature branch for a later feature release. It consumes+ the existing [headline contract](./standards/versioning.md#release-notes).+- [ ] **Android App release scope.** Keep the Local-sharing APK prototype on+ the feature branch. Complete phone, tablet and TV acceptance, hosted-site+ integration and release packaging before including Android in a public release.+## Held By Owner+- [ ] **QR invitations.** Deferred from the maintenance release on 2026-10-24.+ Keep the local QR implementation on the feature branch for later acceptance.- [ ] **Additional collaboration cursors.** Deferred by the owner. Keep theshared system mouse; do not add multi-cursor or pointer-ownership visualization.-- [ ] **Separate accompaniment sharing.** Deferred on 2026-10-20 and excluded- from this candidate. Preserve the unfinished prototype on- `spike/audio-accompaniment` (`5e385add`); it is not a working feature.- Sharing a music application with source audio remains available.-- [ ] **Remote-control latency and responsiveness.** Scheduled after release.- Compare input delivery, Host execution, capture/encoding and playback on the- same endpoints and network before changing budgets or buffering.- [ ] **Persistent-loss estimator replacement.** Deferred on 2026-10-18.Keep mature congestion control and accept quality reduction under sustainedhigh loss; concrete accounting/recovery defects remain ordinary bug fixes.@@ -39,7 +51,7 @@ own behavior; research and Git/PRs own evidence and completed history.- [ ] **Multiple remote-control screens.** Deferred on 2026-10-17. Keep oneshared source; Viewer screen switching and simultaneous screens are later work.- [ ] **32-bit Windows/Linux Apps.** Deferred on 2026-10-02. No package or- website option is planned; preserve the isolated Windows x86 experiment in Git.+ website option is planned.- [ ] **Windows ARM64 App.** Deferred on 2026-10-03. Keep Windows downloads x64-only.- [ ] **Alternative public-invitation providers.** Deferred on 2026-10-16.Keep Cloudflare Quick Tunnel. The [provider assessment](./research/cross-platform-client-runtime.md#alternative-invitation-providers)@@ -74,14 +86,7 @@ own behavior; research and Git/PRs own evidence and completed history.- **Phone as a Host microphone.** Revisit only for a concrete use case thatjustifies the [pairing and input boundary](./research/camera-and-microphone.md#phone-microphone-assessment).- **Mobile controlled endpoints.** Separate scope from desktop keyboard/mouse- control and the co-play candidate above.-- **Android App assessment.** Evaluate native phone screen sharing and source- audio beyond the existing mobile Browser features. Start with Android's- [MediaProjection](https://developer.android.com/media/grow/media-projection)- permission/foreground-service lifecycle and- [playback-capture limits](https://developer.android.com/media/platform/av-capture),- then assess reuse of the Go room/transport core. This is separate from the- current voice candidate; a website wrapper alone does not add screen capture.+ control and desktop co-play.- **Multiple simultaneous sharers.** Define publication/subscription ownershipbefore prototyping. The [party-mode assessment](./research/room-interactions.md#multiple-sharers-assessment-not-implemented)owns the proposal; the current one-Host contract remains.diff --git a/internal/app/app.go b/internal/app/app.goindex f6fe4d38..7d2ea4d3 100644--- a/internal/app/app.go+++ b/internal/app/app.go@@ -199,7 +199,7 @@ func Run(ctx context.Context, options Options) (returnedErr error) {return err}}- return runLauncher(ctx, options, configPath, config, control, input, func(selected *Options, config appconfig.Config) error {+ return runLauncher(ctx, options, configPath, config, control, func(selected *Options, config appconfig.Config) error {diagnosticsMu.Lock()defer diagnosticsMu.Unlock()if ctx.Err() != nil {@@ -247,7 +247,6 @@ func runLauncher(configPath string,config appconfig.Config,control *loopback.Server,- input *remoteinput.Manager,configureDiagnostics func(*Options, appconfig.Config) error,) (returnedErr error) {automatic := explicitMode(options)@@ -339,7 +338,11 @@ func runLauncher(} else if initialConfig.Site != "" && !options.Local && !options.Link {// A starred Site changes only the opening page. --site also remains an// explicit approval shortcut; the launcher can start a Local room later.- openSite(initialConfig.Site, options)+ site := initialConfig.Site+ if !automatic {+ site = startupSite+ }+ openSite(site, options)}// An interactive room is a child of the resident App. Its failure must not// retire an unrelated Site's native sessions or its approval page. Explicitdiff --git a/internal/app/app_test.go b/internal/app/app_test.goindex 979a3eb6..0e474765 100644--- a/internal/app/app_test.go+++ b/internal/app/app_test.go@@ -205,7 +205,7 @@ func TestLauncherRetainsItsRuntimeFailureDuringCancellation(t *testing.T) {joined := falsego func() {done <- runLauncher(ctx, Options{Port: listener.Addr().(*net.TCPAddr).Port, console: console},- configPath, config, control, nil, func(*Options, appconfig.Config) error { return nil })+ configPath, config, control, func(*Options, appconfig.Config) error { return nil })}()defer func() {cancel()@@ -311,7 +311,7 @@ func TestLauncherRetainsItsRuntimeFailureDuringCancellation(t *testing.T) {func TestHungBrowserHandoffDoesNotBlockLauncherOrSiteExit(t *testing.T) {t.Setenv("TERM", "dumb")- for _, mode := range []string{"launcher", "site", "starred-site", "revoked-site"} {+ for _, mode := range []string{"launcher", "site", "starred-site", "starred-second-site", "revoked-site"} {t.Run(mode, func(t *testing.T) {ctx, cancel := context.WithCancel(t.Context())defer cancel()@@ -338,14 +338,20 @@ func TestHungBrowserHandoffDoesNotBlockLauncherOrSiteExit(t *testing.T) {config.Site = "https://share.example"}}+ if mode == "starred-second-site" {+ config.SetApprovedSites([]string{"https://first.example", "https://share.example"})+ if err := launcher.SaveStartupSite(configPath, "https://share.example"); err != nil {+ t.Fatal(err)+ }+ }done := make(chan error, 1)go func() {if mode == "site" {options.SiteSet = trueconfig.Site = "https://share.example"- done <- runLauncher(ctx, options, configPath, config, control, nil, func(*Options, appconfig.Config) error { return nil })+ done <- runLauncher(ctx, options, configPath, config, control, func(*Options, appconfig.Config) error { return nil })} else {- done <- runLauncher(ctx, options, configPath, config, control, nil, func(*Options, appconfig.Config) error { return nil })+ done <- runLauncher(ctx, options, configPath, config, control, func(*Options, appconfig.Config) error { return nil })}}()select {@@ -359,7 +365,7 @@ func TestHungBrowserHandoffDoesNotBlockLauncherOrSiteExit(t *testing.T) {if model.view.entry == "" {t.Fatal("manual entry was not available while browser opening hung")}- if mode == "site" || mode == "starred-site" {+ if mode == "site" || mode == "starred-site" || mode == "starred-second-site" {if model.view.entry != launchURL("https://share.example") {t.Fatalf("App did not open the selected Site: %s", model.view.entry)}@@ -425,9 +431,9 @@ func TestBrowserOpenFailureKeepsTheAppAvailable(t *testing.T) {if mode == "site" {options.SiteSet = trueconfig.Site = "https://share.example"- done <- runLauncher(ctx, options, configPath, config, control, nil, func(*Options, appconfig.Config) error { return nil })+ done <- runLauncher(ctx, options, configPath, config, control, func(*Options, appconfig.Config) error { return nil })} else {- done <- runLauncher(ctx, options, configPath, config, control, nil,+ done <- runLauncher(ctx, options, configPath, config, control,func(*Options, appconfig.Config) error { return nil })}}()diff --git a/internal/app/console.go b/internal/app/console.goindex 424dcb12..beb1b857 100644--- a/internal/app/console.go+++ b/internal/app/console.go@@ -121,11 +121,11 @@ var consoleCopy = map[string][3]string{"error": {"Details", "详情", "!"},"exit": {"quit", "退出", "↪"},"openFailed": {"Could not open the browser; use the address above.", "无法打开浏览器,请使用上方地址。", "! ↗"},- "log": {"Log", "日志", ""},- "export": {"Export diagnostics", "导出诊断", ""},- "exporting": {"Saving diagnostics", "正在保存诊断", ""},- "exported": {"Saved", "已保存", ""},- "exportFailed": {"Could not save diagnostics", "无法保存诊断", ""},+ "log": {"Log", "日志", "LOG"},+ "export": {"Export diagnostics", "导出诊断", "ZIP"},+ "exporting": {"Saving diagnostics", "正在保存诊断", "ZIP ..."},+ "exported": {"Saved", "已保存", "ZIP +"},+ "exportFailed": {"Could not save diagnostics", "无法保存诊断", "ZIP !"},}func newConsole(cancel context.CancelFunc, machine bool) *console {@@ -517,52 +517,10 @@ func (model consoleModel) content(styled bool) string {}func consoleVisualToken(key string) string {- switch key {- case "local":- return "□"- case "link":- return "↗"- case "site":- return "@"- case "setup":- return "?"- case "starting":- return "…"- case "ready":- return "✓"- case "stopping":- return "→"- case "stopped":- return "○"- case "failed", "error":- return "!"- case "openFailed":- return "! ↗"- case "open":- return "○"- case "password":- return "*"- case "entry":- return "□"- case "launcher":- return "☆"- case "invite":- return "↗"- case "exit":- return "↪"- case "log":- return "LOG"- case "export":- return "ZIP"- case "exporting":- return "ZIP ..."- case "exported":- return "ZIP +"- case "exportFailed":- return "ZIP !"- default:- return "?"+ if entry, known := consoleCopy[key]; known {+ return entry[2]}+ return "?"}func consoleTV(frame int, animated, compact bool) string {diff --git a/internal/app/console_test.go b/internal/app/console_test.goindex a4577bc5..b7fc8263 100644--- a/internal/app/console_test.go+++ b/internal/app/console_test.go@@ -34,6 +34,9 @@ func TestConsoleLocalizesFailureAtRenderWithoutLosingCause(t *testing.T) {if !errors.Is(model.view.problem, cause) {t.Fatal("console lost original cause")}+ if language == "vis" && !strings.Contains(content, "\n!\n\n!\n"+problem.Error()) {+ t.Fatalf("known failure was not shown as a visual error: %s", content)+ }}model.language = "en"model.view.problem = errors.New("unclassified technical failure")diff --git a/internal/app/loopback/server.go b/internal/app/loopback/server.goindex ba1b7964..27b71f28 100644--- a/internal/app/loopback/server.go+++ b/internal/app/loopback/server.go@@ -49,8 +49,8 @@ type ControlSession interface {Close() error}-// ControlEvent retains a physical owner's validity through asynchronous queues.-// Current must only inspect ownership; it runs under the WebSocket write lock.+// ControlEvent revalidates its owner or current transport fact after queues.+// Current must be read-only; it runs under the WebSocket write lock.type ControlEvent struct {Value anyCurrent func() booldiff --git a/internal/app/mediaedge/candidate_gathering.go b/internal/app/mediaedge/candidate_gathering.goindex fbce646d..d2e3539f 100644--- a/internal/app/mediaedge/candidate_gathering.go+++ b/internal/app/mediaedge/candidate_gathering.go@@ -14,16 +14,18 @@ const ()type localCandidateGathering struct {- socket *iceSocket- servers []webrtc.ICEServer- mappedPort int- prepareMapping func() int- emit func(*webrtc.ICECandidateInit)- ctx context.Context- cancel context.CancelFunc- once sync.Once+ socket *iceSocket+ servers []webrtc.ICEServer+ mappedPort int+ prepareMapping func() int+ usernameFragment string+ emit func(*webrtc.ICECandidateInit)+ ctx context.Context+ cancel context.CancelFunc+ once sync.Oncemu sync.Mutex+ pionGeneration stringpionDone boolsupplementalDone boolendSent bool@@ -35,24 +37,43 @@ func newLocalCandidateGathering(socket *iceSocket,servers []webrtc.ICEServer,prepareMapping func() int,+ usernameFragment string,emit func(*webrtc.ICECandidateInit),) *localCandidateGathering {ctx, cancel := context.WithCancel(socket.ctx)surveyServers := stunServers(servers)return &localCandidateGathering{socket: socket, servers: surveyServers, prepareMapping: prepareMapping, emit: emit,- ctx: ctx, cancel: cancel, supplementalDone: len(surveyServers) == 0,+ usernameFragment: usernameFragment,+ ctx: ctx, cancel: cancel, supplementalDone: len(surveyServers) == 0,mappedCandidates: make(map[string]struct{}),}}func (gathering *localCandidateGathering) addPion(candidate *webrtc.ICECandidate) {if candidate != nil {- value := candidate.ToJSON()- gathering.emitCandidate(&value)+ value, err := candidate.ToICE()+ if err != nil {+ return+ }+ fragment, _ := value.GetExtension("ufrag")+ gathering.mu.Lock()+ gathering.pionGeneration = fragment.Value+ gathering.mu.Unlock()+ if fragment.Value != gathering.usernameFragment {+ return+ }+ message := candidate.ToJSON()+ gathering.emitCandidate(&message)return}gathering.mu.Lock()+ // Pion delivers its queued candidates in order, but a new handler can+ // receive the previous generation's unscoped completion after restart.+ if gathering.pionGeneration != gathering.usernameFragment {+ gathering.mu.Unlock()+ return+ }gathering.pionDone = truesendEnd := gathering.finishLocked()gathering.mu.Unlock()@@ -178,6 +199,7 @@ func (gathering *localCandidateGathering) emitCandidate(candidate *webrtc.ICECanclosed := gathering.closedgathering.mu.Unlock()if !closed && gathering.emit != nil {+ candidate.UsernameFragment = &gathering.usernameFragmentgathering.emit(candidate)}}diff --git a/internal/app/mediaedge/candidate_gathering_test.go b/internal/app/mediaedge/candidate_gathering_test.goindex f7a2d9ac..15c18369 100644--- a/internal/app/mediaedge/candidate_gathering_test.go+++ b/internal/app/mediaedge/candidate_gathering_test.go@@ -33,6 +33,44 @@ func localSurveyServer(t *testing.T) string {return "stun:" + listener.LocalAddr().String()}+func firstPionCandidate(t *testing.T) (*webrtc.ICECandidate, string) {+ t.Helper()+ settings := webrtc.SettingEngine{}+ settings.SetIncludeLoopbackCandidate(true)+ settings.SetIPFilter(func(ip net.IP) bool { return ip.IsLoopback() })+ connection, err := webrtc.NewAPI(webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = connection.Close() })+ if _, err = connection.CreateDataChannel("gathering", nil); err != nil {+ t.Fatal(err)+ }+ candidates := make(chan *webrtc.ICECandidate, 1)+ connection.OnICECandidate(func(candidate *webrtc.ICECandidate) {+ if candidate != nil {+ select {+ case candidates <- candidate:+ default:+ }+ }+ })+ offer, err := connection.CreateOffer(nil)+ if err != nil {+ t.Fatal(err)+ }+ if err = connection.SetLocalDescription(offer); err != nil {+ t.Fatal(err)+ }+ select {+ case candidate := <-candidates:+ return candidate, descriptionUfrag(t, offer)+ case <-time.After(time.Second):+ t.Fatal("Pion did not gather its loopback candidate")+ return nil, ""+ }+}+func TestCandidatesTrickleBeforeGatewayMappingCompletes(t *testing.T) {for _, closeFirst := range []bool{false, true} {name := "mapping-finishes"@@ -46,30 +84,31 @@ func TestCandidatesTrickleBeforeGatewayMappingCompletes(t *testing.T) {}t.Cleanup(func() { _ = engine.Close() })socket := testICESocket(t, engine)+ pionCandidate, fragment := firstPionCandidate(t)mapping := make(chan int, 1)t.Cleanup(func() { close(mapping) })started := make(chan struct{})candidates := make(chan *webrtc.ICECandidateInit, 8)gathering := newLocalCandidateGathering(socket,[]webrtc.ICEServer{{URLs: []string{localSurveyServer(t)}}},- func() int { close(started); return <-mapping },+ func() int { close(started); return <-mapping }, fragment,func(candidate *webrtc.ICECandidateInit) { candidates <- candidate },)t.Cleanup(gathering.close)gathering.start()waitSignal(t, started, "mapping start")- gathering.addPion(&webrtc.ICECandidate{- Foundation: "host", Component: 1, Protocol: webrtc.ICEProtocolUDP,- Address: "127.0.0.1", Port: uint16(socket.localPort), Typ: webrtc.ICECandidateTypeHost,- })+ gathering.addPion(pionCandidate)gathering.addPion(nil)for received := 0; received < 2; received++ {select {case candidate := <-candidates:- if candidate == nil || (!strings.Contains(candidate.Candidate, "candidate:host ") &&+ if candidate == nil || (!strings.Contains(candidate.Candidate, " typ host") &&!strings.Contains(candidate.Candidate, "candidate:ns1 ")) {t.Fatalf("expected ordinary candidate before mapping: %+v", candidate)}+ if candidate.UsernameFragment == nil || *candidate.UsernameFragment != fragment {+ t.Fatal("ordinary candidate lost its gathering generation")+ }case <-time.After(time.Second):t.Fatal("ordinary ICE waited for the gateway mapping")}@@ -98,6 +137,9 @@ func TestCandidatesTrickleBeforeGatewayMappingCompletes(t *testing.T) {(candidate == nil || !strings.Contains(candidate.Candidate, want)) {t.Fatalf("expected %s after mapping: %+v", want, candidate)}+ if candidate != nil && (candidate.UsernameFragment == nil || *candidate.UsernameFragment != fragment) {+ t.Fatal("gateway candidate lost its gathering generation")+ }case <-time.After(time.Second):t.Fatalf("missing %s after mapping", want)}@@ -173,7 +215,7 @@ func TestSurveyCandidateIdentitySurvivesResponseOrder(t *testing.T) {candidates := make(chan *webrtc.ICECandidateInit, 4)gathering := newLocalCandidateGathering(socket, []webrtc.ICEServer{{URLs: []string{"turn:unused.invalid:3478", "stun:" + slow.String(), "stun:" + fast.String(),- }}}, nil, func(candidate *webrtc.ICECandidateInit) { candidates <- candidate })+ }}}, nil, "gathering", func(candidate *webrtc.ICECandidateInit) { candidates <- candidate })t.Cleanup(gathering.close)gathering.start()for _, want := range []struct{ foundation, port string }{{"candidate:ns2", "40003"}, {"candidate:ns1", "40000"}} {@@ -220,10 +262,12 @@ func TestSurveyPreservesDestinationsWithDuplicateMapping(t *testing.T) {want["candidate:ns"+strconv.Itoa(index+1)] = strconv.Itoa(port)}candidates := make(chan *webrtc.ICECandidateInit, 5)- gathering := newLocalCandidateGathering(socket, []webrtc.ICEServer{{URLs: urls}}, nil,+ pionCandidate, fragment := firstPionCandidate(t)+ gathering := newLocalCandidateGathering(socket, []webrtc.ICEServer{{URLs: urls}}, nil, fragment,func(candidate *webrtc.ICECandidateInit) { candidates <- candidate })t.Cleanup(gathering.close)gathering.start()+ gathering.addPion(pionCandidate)gathering.addPion(nil)for {select {@@ -234,6 +278,9 @@ func TestSurveyPreservesDestinationsWithDuplicateMapping(t *testing.T) {}return}+ if !strings.HasPrefix(candidate.Candidate, "candidate:ns") {+ continue+ }fields := strings.Fields(candidate.Candidate)if want[fields[0]] != fields[5] || (!releaseSurvey.Load() && fields[0] != "candidate:ns2") {t.Fatalf("unexpected destination observation: %s", candidate.Candidate)diff --git a/internal/app/mediaedge/candidate_generation_test.go b/internal/app/mediaedge/candidate_generation_test.gonew file mode 100644index 00000000..685ffb2c--- /dev/null+++ b/internal/app/mediaedge/candidate_generation_test.go@@ -0,0 +1,273 @@+package mediaedge++import (+ "net"+ "strconv"+ "strings"+ "sync"+ "testing"+ "time"++ "github.com/pion/webrtc/v4"+)++func descriptionUfrag(t *testing.T, description webrtc.SessionDescription) string {+ t.Helper()+ parsed, err := description.Unmarshal()+ if err != nil {+ t.Fatal(err)+ }+ for _, media := range parsed.MediaDescriptions {+ if value, ok := media.Attribute("ice-ufrag"); ok {+ return value+ }+ }+ t.Fatal("description has no ICE username fragment")+ return ""+}++func TestPionQueuedCandidatesKeepTheirGatheringGeneration(t *testing.T) {+ settings := webrtc.SettingEngine{}+ settings.SetIncludeLoopbackCandidate(true)+ settings.SetIPFilter(func(ip net.IP) bool { return ip.IsLoopback() })+ connection, err := webrtc.NewAPI(webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = connection.Close() })+ if _, err = connection.CreateDataChannel("gathering", nil); err != nil {+ t.Fatal(err)+ }+ entered, release := make(chan struct{}), make(chan struct{})+ t.Cleanup(func() {+ select {+ case <-release:+ default:+ close(release)+ }+ })+ var once sync.Once+ connection.OnICECandidate(func(*webrtc.ICECandidate) {+ once.Do(func() { close(entered); <-release })+ })+ offer, err := connection.CreateOffer(nil)+ if err != nil {+ t.Fatal(err)+ }+ if err = connection.SetLocalDescription(offer); err != nil {+ t.Fatal(err)+ }+ previous := descriptionUfrag(t, offer)+ waitSignal(t, entered, "held Pion candidate callback")+ remote, err := webrtc.NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = remote.Close() })+ if err = remote.SetRemoteDescription(offer); err != nil {+ t.Fatal(err)+ }+ answer, err := remote.CreateAnswer(nil)+ if err != nil {+ t.Fatal(err)+ }+ if err = remote.SetLocalDescription(answer); err != nil {+ t.Fatal(err)+ }+ if err = connection.SetRemoteDescription(answer); err != nil {+ t.Fatal(err)+ }+ // Let the old generation queue its completion behind the held callback.+ time.Sleep(50 * time.Millisecond)+ next, err := connection.CreateOffer(&webrtc.OfferOptions{ICERestart: true})+ if err != nil {+ t.Fatal(err)+ }+ current := descriptionUfrag(t, next)+ engine, err := NewEngine(EngineOptions{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = engine.Close() })+ emitted := make(chan *webrtc.ICECandidateInit, 32)+ gathering := newLocalCandidateGathering(testICESocket(t, engine), nil, nil, current,+ func(candidate *webrtc.ICECandidateInit) { emitted <- candidate })+ t.Cleanup(gathering.close)+ raw := make(chan *webrtc.ICECandidate, 32)+ connection.OnICECandidate(func(candidate *webrtc.ICECandidate) {+ gathering.addPion(candidate)+ raw <- candidate+ })+ if err = connection.SetLocalDescription(next); err != nil {+ t.Fatal(err)+ }+ close(release)+ currentCandidates, completions := 0, 0+ deadline := time.After(time.Second)+ for completions < 2 {+ select {+ case candidate := <-raw:+ if candidate == nil {+ completions+++ } else if strings.Contains(candidate.ToJSON().Candidate, " ufrag "+current) {+ currentCandidates+++ }+ case <-deadline:+ t.Fatal("Pion did not drain both gathering generations")+ }+ }+ if current == previous || currentCandidates == 0 {+ t.Fatal("the callback queue did not exercise an ICE restart")+ }+ gotCandidates, gotEnd := 0, false+ for {+ select {+ case candidate := <-emitted:+ if candidate == nil {+ if gotCandidates == 0 || gotEnd {+ t.Fatal("old completion ended the current gathering")+ }+ gotEnd = true+ } else {+ if gotEnd || strings.Contains(candidate.Candidate, " ufrag "+previous) ||+ candidate.UsernameFragment == nil || *candidate.UsernameFragment != current {+ t.Fatal("queued old Pion candidate entered the current gathering")+ }+ gotCandidates+++ }+ default:+ if !gotEnd || gotCandidates != currentCandidates {+ t.Fatalf("current gathering lost candidates or completion: candidates=%d/%d, end=%v", gotCandidates, currentCandidates, gotEnd)+ }+ return+ }+ }+}++func TestNativeRemoteCandidatesRespectJSONGeneration(t *testing.T) {+ for _, receiverSide := range []bool{false, true} {+ name := "sender-queued-before-answer"+ if receiverSide {+ name = "receiver-after-ICE-restart"+ }+ t.Run(name, func(t *testing.T) {+ engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = engine.Close() })+ remote, err := webrtc.NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = remote.Close() })+ var connection *webrtc.PeerConnection+ var add func(*webrtc.ICECandidateInit) error+ var applyAnswer func()+ var previous, current string+ if receiverSide {+ if _, err = remote.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo,+ webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}); err != nil {+ t.Fatal(err)+ }+ offer, err := remote.CreateOffer(nil)+ if err != nil {+ t.Fatal(err)+ }+ if err = remote.SetLocalDescription(offer); err != nil {+ t.Fatal(err)+ }+ receiver, answer, err := engine.NewReceiver(ReceiverOptions{Offer: offer, EdgeCapacity: 1})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = receiver.Close() })+ if err = remote.SetRemoteDescription(answer); err != nil {+ t.Fatal(err)+ }+ previous = descriptionUfrag(t, offer)+ offer, err = remote.CreateOffer(&webrtc.OfferOptions{ICERestart: true})+ if err != nil {+ t.Fatal(err)+ }+ if err = remote.SetLocalDescription(offer); err != nil {+ t.Fatal(err)+ }+ if _, reused, err := receiver.Renegotiate(offer, nil); err != nil || !reused {+ t.Fatalf("receiver restart failed: reused=%v, error=%v", reused, err)+ }+ current = descriptionUfrag(t, offer)+ connection, add = receiver.connection, receiver.AddRemoteCandidate+ } else {+ source, err := engine.NewSource("vp8", 1, 1, nil)+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = source.Close() })+ edge, err := engine.NewEdge(source, EdgeOptions{ConnectionID: "candidate-generation"})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = edge.Close() })+ offer, err := edge.CreateOffer()+ if err != nil {+ t.Fatal(err)+ }+ if err = remote.SetRemoteDescription(offer); err != nil {+ t.Fatal(err)+ }+ answer, err := remote.CreateAnswer(nil)+ if err != nil {+ t.Fatal(err)+ }+ current, previous = descriptionUfrag(t, answer), "retired_generation"+ connection, add = edge.connection, edge.AddRemoteCandidate+ applyAnswer = func() {+ if err := edge.SetAnswer(answer, ""); err != nil {+ t.Fatal(err)+ }+ }+ }+ if previous == current {+ t.Fatal("ICE restart retained its username fragment")+ }+ ports := []int{}+ for _, fragment := range []*string{&previous, ¤t, nil} {+ listener, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = listener.Close() })+ port := listener.LocalAddr().(*net.UDPAddr).Port+ ports = append(ports, port)+ // Standards-shaped JSON fragment, deliberately absent from the SDP line.+ if err := add(&webrtc.ICECandidateInit{UsernameFragment: fragment,+ Candidate: "candidate:probe 1 udp 2122260223 127.0.0.1 " + strconv.Itoa(port) + " typ host"}); err != nil {+ t.Fatal(err)+ }+ }+ if applyAnswer != nil {+ applyAnswer()+ }+ currentSeen, legacySeen := false, false+ until := time.Now().Add(250 * time.Millisecond)+ for time.Now().Before(until) {+ for _, stat := range connection.GetStats() {+ candidate, ok := stat.(webrtc.ICECandidateStats)+ if !ok || candidate.Type != webrtc.StatsTypeRemoteCandidate {+ continue+ }+ if int(candidate.Port) == ports[0] {+ t.Fatal("retired JSON username fragment entered the current ICE checklist")+ }+ currentSeen = currentSeen || int(candidate.Port) == ports[1]+ legacySeen = legacySeen || int(candidate.Port) == ports[2]+ }+ time.Sleep(time.Millisecond)+ }+ if !currentSeen || !legacySeen {+ t.Fatalf("positive controls not admitted: current=%v legacy=%v", currentSeen, legacySeen)+ }+ })+ }+}diff --git a/internal/app/mediaedge/edge.go b/internal/app/mediaedge/edge.goindex 54179dab..4483c803 100644--- a/internal/app/mediaedge/edge.go+++ b/internal/app/mediaedge/edge.go@@ -20,8 +20,10 @@ import (const maxPendingCandidates = 64type EdgeEvents struct {- LocalCandidate func(*webrtc.ICECandidateInit)- ConnectionState func(webrtc.PeerConnectionState, *SelectedPair)+ LocalCandidate func(*webrtc.ICECandidateInit)+ // Historical loss still retires input permission. Media projections must+ // check isCurrentState again after any event queues.+ ConnectionState func(state webrtc.PeerConnectionState, pair *SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool)}type EdgeOptions struct {@@ -159,8 +161,13 @@ func (engine *Engine) NewEdge(source *Source, options EdgeOptions) (_ *Edge, err}}if !options.Local {+ parameters, parametersErr := connection.SCTP().Transport().ICETransport().GetLocalParameters()+ if parametersErr != nil {+ _ = edge.Close()+ return nil, parametersErr+ }edge.localCandidates = newLocalCandidateGathering(- socket, options.ICEServers, prepareMapping, options.Events.LocalCandidate,+ socket, options.ICEServers, prepareMapping, parameters.UsernameFragment, options.Events.LocalCandidate,)connection.OnICECandidate(func(candidate *webrtc.ICECandidate) {edge.localCandidates.addPion(candidate)@@ -175,11 +182,12 @@ func (engine *Engine) NewEdge(source *Source, options EdgeOptions) (_ *Edge, err})}connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {+ isCurrentState := func(expected webrtc.PeerConnectionState) bool { return connection.ConnectionState() == expected }debug := slog.Default().Enabled(engine.ctx, slog.LevelDebug)if debug {log.Debug("piik-client", "event", "media-connection-state", "direction", "outbound", "local", options.Local, "state", state.String())}- if state == webrtc.PeerConnectionStateConnected {+ if state == webrtc.PeerConnectionStateConnected && isCurrentState(state) {if err := edge.transport.SetConnected(); err != nil {if debug {log.Debug("piik-client", "event", "media-activation-failed", diagnostics.Error(err))@@ -190,7 +198,7 @@ func (engine *Engine) NewEdge(source *Source, options EdgeOptions) (_ *Edge, erredge.source.RequestRecoveryFrame()}var selected *SelectedPair- if state == webrtc.PeerConnectionStateConnected && (edge.events.ConnectionState != nil || debug) {+ if state == webrtc.PeerConnectionStateConnected && isCurrentState(state) && (edge.events.ConnectionState != nil || debug) {if pair, pairErr := edge.SelectedPair(); pairErr == nil {selected = &pairif debug {@@ -200,7 +208,7 @@ func (engine *Engine) NewEdge(source *Source, options EdgeOptions) (_ *Edge, err}}if edge.events.ConnectionState != nil {- edge.events.ConnectionState(state, selected)+ edge.events.ConnectionState(state, selected, isCurrentState)}})return edge, nil@@ -342,7 +350,7 @@ func (edge *Edge) SetAnswer(answer webrtc.SessionDescription, offerID string) erfor _, candidate := range pending {// Candidates are disposable edge input. The connection state callback// remains the authority for a real media failure.- if err := edge.connection.AddICECandidate(candidate); err != nil {+ if err := forwarding.AddICECandidate(edge.connection, candidate); err != nil {slog.Debug("media-candidate-rejected", "connectionId", diagnostics.ID(edge.connectionID), "stage", "drain", diagnostics.Error(err))}}@@ -380,7 +388,7 @@ func (edge *Edge) AddRemoteCandidate(candidate *webrtc.ICECandidateInit) error {// A candidate may become stale between validation and delivery. Dropping// that one edge input keeps the shared control session alive; ICE state// events still report whether the edge itself can connect.- if err := edge.connection.AddICECandidate(value); err != nil {+ if err := forwarding.AddICECandidate(edge.connection, value); err != nil {slog.Debug("media-candidate-rejected", "connectionId", diagnostics.ID(edge.connectionID), "stage", "apply", diagnostics.Error(err))}return nildiff --git a/internal/app/mediaedge/edge_test.go b/internal/app/mediaedge/edge_test.goindex b9ad9494..efbbbb4b 100644--- a/internal/app/mediaedge/edge_test.go+++ b/internal/app/mediaedge/edge_test.go@@ -243,7 +243,7 @@ func TestShutdownClosesSiblingTransportsBeforeWaitingForWrites(t *testing.T) {for index := range closed {_, err := engine.NewEdge(source, EdgeOptions{ConnectionID: "shutdown-" + owner + string(rune('a'+index)),- Events: EdgeEvents{ConnectionState: func(state webrtc.PeerConnectionState, _ *SelectedPair) {+ Events: EdgeEvents{ConnectionState: func(state webrtc.PeerConnectionState, _ *SelectedPair, _ func(webrtc.PeerConnectionState) bool) {if state == webrtc.PeerConnectionStateClosed {closed[index] <- struct{}{}}@@ -636,7 +636,7 @@ func TestBandwidthObserverReceivesTransportFeedbackWithoutPacing(t *testing.T) {}sample, ok := edge.QualitySample(time.Now())if !ok || sample.State != "healthy" || sample.Reason == nil ||- *sample.Reason != "none" || sample.IntervalFramesEncoded == 0 ||+ *sample.Reason != "none" || sample.IntervalFramesSent == 0 ||sample.RTPStatsID == "" || sample.TrackIdentifier == "" {t.Fatalf("quality sample = %+v, %v", sample, ok)}@@ -775,7 +775,7 @@ func connectedReceiver(t.Helper()connected := make(chan struct{}, 1)edge, err := engine.NewEdge(source, EdgeOptions{ConnectionID: connectionID, Events: EdgeEvents{- ConnectionState: func(state webrtc.PeerConnectionState, _ *SelectedPair) {+ ConnectionState: func(state webrtc.PeerConnectionState, _ *SelectedPair, _ func(webrtc.PeerConnectionState) bool) {if state == webrtc.PeerConnectionStateConnected {select {case connected <- struct{}{}:@@ -935,3 +935,62 @@ func waitPacket(t *testing.T, packets <-chan *rtp.Packet, edges ...*Edge) *rtp.Preturn nil}}++// Connection callbacks and the final event writer can run after Pion has moved+// on. Retain the real callback until the PC closes, then consume its projection.+func TestEdgeDelayedConnectionStateIsNotCurrent(t *testing.T) {+ engine, err := NewEngine(EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = engine.Close() })+ source, err := engine.NewSource("vp8", 2, 2, nil)+ if err != nil {+ t.Fatal(err)+ }+ connected := make(chan func(webrtc.PeerConnectionState) bool, 1)+ release := make(chan struct{})+ delivered := make(chan bool, 1)+ var releaseOnce sync.Once+ resume := func() { releaseOnce.Do(func() { close(release) }) }+ t.Cleanup(resume)+ edge, err := engine.NewEdge(source, EdgeOptions{ConnectionID: "delayed-state", Events: EdgeEvents{+ ConnectionState: func(state webrtc.PeerConnectionState, _ *SelectedPair, current func(webrtc.PeerConnectionState) bool) {+ if state == webrtc.PeerConnectionStateConnected {+ connected <- current+ <-release+ delivered <- current(state)+ }+ },+ }})+ if err != nil {+ t.Fatal(err)+ }+ receiver := newReceiver(t)+ t.Cleanup(func() { _ = receiver.Close() })+ connectEdgeToReceiver(t, edge, receiver)+ var current func(webrtc.PeerConnectionState) bool+ select {+ case current = <-connected:+ case <-time.After(5 * time.Second):+ t.Fatal("no connected Native callback")+ }+ if !current(webrtc.PeerConnectionStateConnected) {+ t.Fatal("live connection was not current")+ }+ if err = edge.Close(); err != nil {+ t.Fatal(err)+ }+ resume()+ select {+ case valid := <-delivered:+ if valid {+ t.Fatal("delayed connected event survived PC closure")+ }+ case <-time.After(5 * time.Second):+ t.Fatal("Native callback did not resume")+ }+ if !current(webrtc.PeerConnectionStateClosed) {+ t.Fatal("current-state predicate retained a historical snapshot")+ }+}diff --git a/internal/app/mediaedge/engine_test.go b/internal/app/mediaedge/engine_test.goindex 8400a707..a36dbb39 100644--- a/internal/app/mediaedge/engine_test.go+++ b/internal/app/mediaedge/engine_test.go@@ -139,7 +139,7 @@ func TestICESocketSTUNUsesOnePortForBothFamilies(t *testing.T) {t.Fatal(err)}t.Cleanup(func() { _ = server.Close() })- mapped, err := socket.stunMapping(t.Context(), listener.LocalAddr().(*net.UDPAddr))+ mapped, err := socket.querySTUN(t.Context(), listener.LocalAddr().(*net.UDPAddr))if err != nil {t.Fatal(err)}diff --git a/internal/app/mediaedge/ice_socket.go b/internal/app/mediaedge/ice_socket.goindex c117a205..e11826ca 100644--- a/internal/app/mediaedge/ice_socket.go+++ b/internal/app/mediaedge/ice_socket.go@@ -29,7 +29,6 @@ type iceSocket struct {mux *ice.UDPMuxDefaultlocalPort intportMapping *portmapping.Mapping- stunQueries singleflight.Groupctx context.Contextcancel context.CancelFunccloseOnce sync.Once@@ -95,7 +94,7 @@ func (socket *iceSocket) addPunchedPeer(connection *webrtc.PeerConnection) errorreturn nil}mid := "0"- return connection.AddICECandidate(webrtc.ICECandidateInit{SDPMid: &mid,+ return forwarding.AddICECandidate(connection, webrtc.ICECandidateInit{SDPMid: &mid,Candidate: fmt.Sprintf("candidate:sp0 1 udp 1694498815 %s %d typ srflx", socket.punchedPeer.IP, socket.punchedPeer.Port),})}@@ -134,6 +133,8 @@ func (socket *iceSocket) surveySTUN(defer cancel()// One survey retains its resolver even while canceled workers are settling.resolver := net.DefaultResolver+ // Duplicate destinations may share an observation only in this gathering.+ var queries singleflight.Groupresults := make(chan mappedAddress)var pending sync.WaitGroupindex := 0@@ -158,7 +159,7 @@ func (socket *iceSocket) surveySTUN(}address := net.UDPAddrFromAddrPort(netip.AddrPortFrom(addresses[0], uint16(uri.Port)))started := time.Now()- mapped, err := socket.stunMapping(surveyContext, address)+ mapped, err := socket.stunMapping(surveyContext, &queries, address)if err != nil || mapped == nil || mapped.IP.To4() == nil ||mapped.Port < 1 || mapped.Port > 65_535 {slog.DebugContext(surveyContext, "nat-survey", "event", "binding-failed", "serverPort", address.Port,diff --git a/internal/app/mediaedge/publication.go b/internal/app/mediaedge/publication.goindex a0d0cbfd..e3525b9e 100644--- a/internal/app/mediaedge/publication.go+++ b/internal/app/mediaedge/publication.go@@ -88,10 +88,16 @@ func (engine *Engine) NewPublication(source *Source, options EdgeOptions) (*Publpublication.transport = transportpublication.signaling.connection = transport.PCpublication.signaling.audioSource = options.Audio- publication.signaling.localCandidates = newLocalCandidateGathering(socket, options.ICEServers, nil, options.Events.LocalCandidate)+ parameters, err := transport.PC.SCTP().Transport().ICETransport().GetLocalParameters()+ if err != nil {+ _ = publication.Close()+ return nil, err+ }+ publication.signaling.localCandidates = newLocalCandidateGathering(socket, options.ICEServers, nil, parameters.UsernameFragment, options.Events.LocalCandidate)transport.PC.OnICECandidate(publication.signaling.localCandidates.addPion)transport.PC.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {- if state == webrtc.PeerConnectionStateConnected {+ isCurrentState := func(expected webrtc.PeerConnectionState) bool { return transport.PC.ConnectionState() == expected }+ if state == webrtc.PeerConnectionStateConnected && isCurrentState(state) {if connectErr := transport.SetConnected(); connectErr != nil {_ = transport.PC.Close()return@@ -99,7 +105,7 @@ func (engine *Engine) NewPublication(source *Source, options EdgeOptions) (*Publsource.RequestRecoveryFrame()}if options.Events.ConnectionState != nil {- options.Events.ConnectionState(state, nil)+ options.Events.ConnectionState(state, nil, isCurrentState)}})engine.mu.Lock()diff --git a/internal/app/mediaedge/quality.go b/internal/app/mediaedge/quality.goindex 8caff36b..49c85ce3 100644--- a/internal/app/mediaedge/quality.go+++ b/internal/app/mediaedge/quality.go@@ -18,7 +18,7 @@ type QualitySample struct {TrackIdentifier stringState stringReason *string- IntervalFramesEncoded uint64+ IntervalFramesSent uint64FramesPerSecond float64BitrateKbps float64AvailableOutgoingKbps float64@@ -57,16 +57,16 @@ func (edge *Edge) QualitySample(now time.Time) (QualitySample, bool) {seconds := window.Seconds()bitrate := float64(bytes*8) / secondssample := QualitySample{- SampleTimestampMs: now.UnixMilli(),- SampleWindowMs: window.Milliseconds(),- RTPStatsID: edge.connection.ID(),- TrackIdentifier: edge.transport.Output.ID(),- State: "unknown",- IntervalFramesEncoded: frames,- FramesPerSecond: float64(frames) / seconds,- BitrateKbps: bitrate / 1000,- Width: uint32(format >> 32),- Height: uint32(format),+ SampleTimestampMs: now.UnixMilli(),+ SampleWindowMs: window.Milliseconds(),+ RTPStatsID: edge.connection.ID(),+ TrackIdentifier: edge.transport.Output.ID(),+ State: "unknown",+ IntervalFramesSent: frames,+ FramesPerSecond: float64(frames) / seconds,+ BitrateKbps: bitrate / 1000,+ Width: uint32(format >> 32),+ Height: uint32(format),}if slog.Default().Enabled(edge.engine.ctx, slog.LevelDebug) {defer func() {diff --git a/internal/app/mediaedge/quality_test.go b/internal/app/mediaedge/quality_test.goindex 4ba81254..410a919c 100644--- a/internal/app/mediaedge/quality_test.go+++ b/internal/app/mediaedge/quality_test.go@@ -97,7 +97,7 @@ func TestQualitySampleUsesAllocationNotPacketRate(t *testing.T) {sample, ok := edge.QualitySample(started.Add(2 * time.Second))if !ok || sample.State != "healthy" || sample.Reason == nil ||- *sample.Reason != "none" || sample.IntervalFramesEncoded != 60 ||+ *sample.Reason != "none" || sample.IntervalFramesSent != 60 ||sample.FramesPerSecond != 30 || sample.BitrateKbps != 4_000 ||sample.AvailableOutgoingKbps != 100 {t.Fatalf("quality sample = %+v, %v", sample, ok)diff --git a/internal/app/mediaedge/receiver.go b/internal/app/mediaedge/receiver.goindex ec5d63eb..174c543b 100644--- a/internal/app/mediaedge/receiver.go+++ b/internal/app/mediaedge/receiver.go@@ -2,7 +2,6 @@ package mediaedgeimport ("errors"- "io""log/slog""slices""strconv"@@ -20,7 +19,7 @@ import (type ReceiverEvents struct {// The final consumer must check current again after any event queues.LocalCandidate func(candidate *webrtc.ICECandidateInit, current func() bool)- ConnectionState func(state webrtc.PeerConnectionState, pair *SelectedPair, current func() bool)+ ConnectionState func(state webrtc.PeerConnectionState, pair *SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool)DataChannel func(*webrtc.DataChannel)}@@ -142,6 +141,9 @@ func (engine *Engine) NewReceiver(options ReceiverOptions) (_ *Receiver, _ webrt}})connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {+ isCurrentState := func(expected webrtc.PeerConnectionState) bool {+ return receiver.active() && connection.ConnectionState() == expected+ }if !receiver.active() {return}@@ -150,7 +152,7 @@ func (engine *Engine) NewReceiver(options ReceiverOptions) (_ *Receiver, _ webrtslog.Debug("piik-client", "event", "media-connection-state", "direction", "inbound", "state", state.String())}var selected *SelectedPair- if state == webrtc.PeerConnectionStateConnected && (receiver.events.ConnectionState != nil || debug) {+ if state == webrtc.PeerConnectionStateConnected && isCurrentState(state) && (receiver.events.ConnectionState != nil || debug) {if pair, pairErr := receiver.SelectedPair(); pairErr == nil {selected = &pairif debug {@@ -160,7 +162,7 @@ func (engine *Engine) NewReceiver(options ReceiverOptions) (_ *Receiver, _ webrt}}if receiver.events.ConnectionState != nil {- receiver.events.ConnectionState(state, selected, receiver.active)+ receiver.events.ConnectionState(state, selected, isCurrentState)}})connection.OnTrack(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {@@ -220,6 +222,10 @@ func (receiver *Receiver) Renegotiate(offer webrtc.SessionDescription, servers [}func (receiver *Receiver) beginGathering() {+ parameters, err := receiver.connection.SCTP().Transport().ICETransport().GetLocalParameters()+ if err != nil {+ return+ }// Serialize candidate delivery with collector replacement, without holding// the receiver state lock across the caller's event delivery.receiver.candidateMu.Lock()@@ -236,7 +242,7 @@ func (receiver *Receiver) beginGathering() {defer receiver.mu.Unlock()return !receiver.closed && receiver.localCandidates == gathering}- gathering = newLocalCandidateGathering(receiver.socket, receiver.iceServers, receiver.prepareMapping,+ gathering = newLocalCandidateGathering(receiver.socket, receiver.iceServers, receiver.prepareMapping, parameters.UsernameFragment,func(candidate *webrtc.ICECandidateInit) {receiver.candidateMu.Lock()defer receiver.candidateMu.Unlock()@@ -286,10 +292,14 @@ func (receiver *Receiver) createAnswer() (webrtc.SessionDescription, error) {if err = receiver.connection.SetLocalDescription(answer); err != nil {return webrtc.SessionDescription{}, err}- if receiver.connection.LocalDescription() == nil {+ local := receiver.connection.LocalDescription()+ if local == nil {return webrtc.SessionDescription{}, errors.New("native media receiver produced no SDP answer")}- return *receiver.connection.LocalDescription(), nil+ // This is a trickled answer: Pion can finish before the extra STUN/mapping+ // observations. Only the merged collector may announce their completion.+ return webrtc.SessionDescription{Type: local.Type,+ SDP: strings.ReplaceAll(local.SDP, "a=end-of-candidates\r\n", "")}, nil}func (receiver *Receiver) AddRemoteCandidate(candidate *webrtc.ICECandidateInit) error {@@ -306,7 +316,7 @@ func (receiver *Receiver) AddRemoteCandidate(candidate *webrtc.ICECandidateInit)return nil}receiver.mu.Unlock()- _ = receiver.connection.AddICECandidate(value)+ _ = forwarding.AddICECandidate(receiver.connection, value)return nil}@@ -378,9 +388,6 @@ func (receiver *Receiver) consumeTrack(track *webrtc.TrackRemote, _ *webrtc.RTPRfor {packet, _, err := forwarding.ReadRTP(track)if err != nil {- if !errors.Is(err, io.EOF) {- return- }return}// Pion completes all bindings; each edge owns its write failure.diff --git a/internal/app/mediaedge/receiver_test.go b/internal/app/mediaedge/receiver_test.goindex 3ca8ae47..fa4bd09f 100644--- a/internal/app/mediaedge/receiver_test.go+++ b/internal/app/mediaedge/receiver_test.go@@ -423,6 +423,7 @@ func testReceiverForwarding(t *testing.T, codec string) {var nativeReceiver *Receivervar upstreamCandidates []*webrtc.ICECandidateInitvar receiverCandidates []*webrtc.ICECandidateInit+ var gatheredCandidates []*webrtc.ICECandidateInitvar candidateMu sync.MutexremoteReady := falsecompleted := make(chan struct{}, 4)@@ -471,6 +472,7 @@ func testReceiverForwarding(t *testing.T, codec string) {return}candidateMu.Lock()+ gatheredCandidates = append(gatheredCandidates, candidate)if strings.Contains(candidate.Candidate, "candidate:ns") {surveyCandidates++}@@ -596,6 +598,22 @@ func testReceiverForwarding(t *testing.T, codec string) {}}waitSignal(t, completed, "initial receiver gathering")+ checkCandidateGeneration := func(description webrtc.SessionDescription) {+ t.Helper()+ fragment := descriptionUfrag(t, description)+ candidateMu.Lock()+ defer candidateMu.Unlock()+ if len(gatheredCandidates) == 0 {+ t.Fatal("receiver gathering produced no candidates")+ }+ for _, candidate := range gatheredCandidates {+ if candidate.UsernameFragment == nil || *candidate.UsernameFragment != fragment {+ t.Fatalf("receiver candidate lost its actual SDP generation: %+v", candidate)+ }+ }+ gatheredCandidates = nil+ }+ checkCandidateGeneration(answer)firstGatheringCurrent := <-gatheringCurrentif !firstGatheringCurrent() {t.Fatal("current receiver gathering lost delivery authority")@@ -624,6 +642,9 @@ func testReceiverForwarding(t *testing.T, codec string) {if renegotiateErr != nil || !reused {t.Fatalf("receiver renegotiation: reused=%v, %v", reused, renegotiateErr)}+ if strings.Contains(answer.SDP, "a=end-of-candidates") {+ t.Fatal("trickle answer bypassed the merged candidate completion owner")+ }if err = upstream.SetRemoteDescription(answer); err != nil {t.Fatal(err)}@@ -644,6 +665,7 @@ func testReceiverForwarding(t *testing.T, codec string) {}if restart {waitSignal(t, completed, "restarted receiver gathering")+ checkCandidateGeneration(answer)if firstGatheringCurrent() || !(<-gatheringCurrent)() {t.Fatal("queued candidates did not retain their gathering owner")}diff --git a/internal/app/mediaedge/stun.go b/internal/app/mediaedge/stun.goindex f91c4eb3..2dfd071c 100644--- a/internal/app/mediaedge/stun.go+++ b/internal/app/mediaedge/stun.go@@ -7,20 +7,17 @@ import ("github.com/TNTcraftHIM/Piik/internal/diagnostics""github.com/pion/stun/v4"+ "golang.org/x/sync/singleflight")-// Only in-flight discovery is shared. Completed observations must not survive-// into a later ICE gathering: the socket can outlive its public NAT mapping.-func (socket *iceSocket) stunMapping(ctx context.Context, server *net.UDPAddr) (*stun.XORMappedAddress, error) {+// A gathering owns its in-flight observations; retaining the media socket does+// not let a successor reuse an earlier transaction's delayed mapping response.+func (socket *iceSocket) stunMapping(ctx context.Context, queries *singleflight.Group, server *net.UDPAddr) (*stun.XORMappedAddress, error) {if err := ctx.Err(); err != nil {return nil, err}- result := socket.stunQueries.DoChan(server.String(), func() (any, error) {- // A retiring gathering must not cancel its successor's discovery. The- // connection owns the transaction; callers keep their own wait deadline.- queryContext, cancel := context.WithTimeout(socket.ctx, stunSurveyTimeout)- defer cancel()- return socket.querySTUN(queryContext, server)+ result := queries.DoChan(server.String(), func() (any, error) {+ return socket.querySTUN(ctx, server)})select {case <-ctx.Done():diff --git a/internal/app/mediaedge/stun_test.go b/internal/app/mediaedge/stun_test.goindex f4beaa41..d1bb2663 100644--- a/internal/app/mediaedge/stun_test.go+++ b/internal/app/mediaedge/stun_test.go@@ -218,3 +218,72 @@ func TestConcurrentSurveyCancellationAndEngineRetirement(t *testing.T) {_ = engine.Close()waitSignal(t, stopped, "Engine shutdown during discovery")}++func TestRestartedGatheringDoesNotReuseRetiredSTUNTransaction(t *testing.T) {+ engine, err := NewEngine(EngineOptions{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = engine.Close() })+ socket := testICESocket(t, engine)+ firstRequest := make(chan struct{})+ freshRequest := make(chan struct{}, 1)+ releaseOld := make(chan struct{})+ t.Cleanup(func() {+ select {+ case <-releaseOld:+ default:+ close(releaseOld)+ }+ })+ var previous [12]byte+ started := false+ server := bindingServer(t, func(request *stun.Message, sender *net.UDPAddr, listener *net.UDPConn) {+ if !started {+ started, previous = true, request.TransactionID+ close(firstRequest)+ go func() {+ <-releaseOld+ answerBinding(request, sender, listener, 40000)+ }()+ return+ }+ if request.TransactionID == previous {+ return // Retransmissions still belong to the retired observation.+ }+ if sender.Port != socket.localPort {+ t.Error("ICE restart replaced the physical media socket")+ }+ select {+ case freshRequest <- struct{}{}:+ default:+ }+ answerBinding(request, sender, listener, 40001)+ })+ servers := []webrtc.ICEServer{{URLs: []string{"stun:" + server.String()}}}+ previousGathering := newLocalCandidateGathering(socket, servers, nil, "previous",+ func(*webrtc.ICECandidateInit) { t.Error("retired gathering delivered a candidate") })+ t.Cleanup(previousGathering.close)+ previousGathering.start()+ waitSignal(t, firstRequest, "previous gathering's Binding request")+ previousGathering.close()+ candidates := make(chan *webrtc.ICECandidateInit, 4)+ nextGathering := newLocalCandidateGathering(socket, servers, nil, "current",+ func(candidate *webrtc.ICECandidateInit) { candidates <- candidate })+ t.Cleanup(nextGathering.close)+ nextGathering.start()+ select {+ case <-freshRequest:+ case <-time.After(250 * time.Millisecond):+ }+ close(releaseOld)+ select {+ case candidate := <-candidates:+ if candidate == nil || strings.Fields(candidate.Candidate)[5] != "40001" ||+ candidate.UsernameFragment == nil || *candidate.UsernameFragment != "current" {+ t.Fatalf("new gathering inherited the retired transaction's old mapping: %+v", candidate)+ }+ case <-time.After(time.Second):+ t.Fatal("new gathering waited for the retired transaction")+ }+}diff --git a/internal/app/nativecapture/stream.go b/internal/app/nativecapture/stream.goindex ee7f83c2..2e58523f 100644--- a/internal/app/nativecapture/stream.go+++ b/internal/app/nativecapture/stream.go@@ -134,6 +134,7 @@ type Stream struct {controlMu sync.Mutexclosed boolcloseOnce sync.Once+ failure error // Published before done; read only after process retirement.readMu sync.MutexreadEnd sync.Oncelogger *slog.Logger@@ -530,6 +531,12 @@ func startStreamWithEnvironment(waitErr := command.Wait()stopParent()_ = trace.Close()+ stage, hresult := captureFailureCodes(stderr.Bytes())+ if waitErr != nil && (stage != "" || hresult != "") {+ // Preserve validated failure codes and the exit cause for diagnostics+ // and errors.Is/As, never the helper's raw detail.+ stream.failure = fmt.Errorf("native capture failed (stage=%s, hresult=%s): %w", stage, hresult, waitErr)+ }logCaptureFailure(ctx, waitErr, command.ProcessState.ExitCode(), stderr.Bytes())logger.DebugContext(ctx, "piik-client", "event", "capture-process-ended", "mode", mode,"durationMs", time.Since(started).Milliseconds(), "exitCode", command.ProcessState.ExitCode(),@@ -544,7 +551,18 @@ func logCaptureFailure(ctx context.Context, err error, exitCode int, stderr []byif err == nil || !slog.Default().Enabled(ctx, slog.LevelDebug) {return}- stage, hresult := "", ""+ stage, hresult := captureFailureCodes(stderr)+ canceled := ctx.Err() != nil+ // A real child failure may race owner cancellation. Keep its fixed codes,+ // while ordinary cancellation without a reported failure stays silent.+ if canceled && stage == "" && hresult == "" {+ return+ }+ slog.DebugContext(ctx, "piik-client", "event", "capture-process-failed",+ "exitCode", exitCode, "stage", stage, "hresult", hresult, "canceled", canceled)+}++func captureFailureCodes(stderr []byte) (stage, hresult string) {for _, line := range strings.Split(string(stderr), "\n") {key, value, _ := strings.Cut(strings.TrimSuffix(line, "\r"), "=")switch {@@ -554,14 +572,7 @@ func logCaptureFailure(ctx context.Context, err error, exitCode int, stderr []byhresult = strings.ToLower(value)}}- canceled := ctx.Err() != nil- // A real child failure may race owner cancellation. Keep its fixed codes,- // while ordinary cancellation without a reported failure stays silent.- if canceled && stage == "" && hresult == "" {- return- }- slog.DebugContext(ctx, "piik-client", "event", "capture-process-failed",- "exitCode", exitCode, "stage", stage, "hresult", hresult, "canceled", canceled)+ return}func (stream *Stream) Read() (Frame, error) {@@ -710,5 +721,5 @@ func (stream *Stream) Close() error {stream.cancel()<-drained})- return nil+ return stream.failure}diff --git a/internal/app/nativecapture/stream_test.go b/internal/app/nativecapture/stream_test.goindex d56ae361..42ae41f2 100644--- a/internal/app/nativecapture/stream_test.go+++ b/internal/app/nativecapture/stream_test.go@@ -8,6 +8,7 @@ import ("io""log/slog""os"+ "os/exec""path/filepath""strconv""strings"@@ -83,6 +84,10 @@ func TestCaptureStopHelper(t *testing.T) {if mode == "" {return}+ if mode == "startup-stage" {+ _, _ = io.WriteString(os.Stderr, "result=window-capture-failed\nstage=mft-output-timeout\nhresult=0x80004005\ndetail=private window title C:\\private\\capture.exe\n")+ os.Exit(2)+ }if err := writeFrame(os.Stdout, Frame{Kind: FrameStatus, Data: []byte("ready")}); err != nil {os.Exit(2)}@@ -109,6 +114,32 @@ func TestCaptureStopHelper(t *testing.T) {os.Exit(0)}+func TestCaptureFailureSurvivesProcessRetirementWithoutDebugLogging(t *testing.T) {+ executable, err := os.Executable()+ if err != nil {+ t.Fatal(err)+ }+ stream, err := startStreamWithEnvironment(t.Context(), executable,+ []string{"-test.run=^TestCaptureStopHelper$"}, []string{"PIIK_CAPTURE_STOP_FIXTURE=startup-stage"})+ if err != nil {+ t.Fatal(err)+ }+ if _, err := stream.Read(); err == nil {+ t.Fatal("failed process produced a ready frame")+ }+ err = stream.Close()+ if err == nil || !strings.Contains(err.Error(), "mft-output-timeout") || !strings.Contains(err.Error(), "0x80004005") {+ t.Fatalf("process retirement lost native failure stage: %v", err)+ }+ if strings.Contains(err.Error(), "private") {+ t.Fatalf("native failure exposed raw stderr: %v", err)+ }+ var exit *exec.ExitError+ if !errors.As(err, &exit) || exit.ExitCode() != 2 {+ t.Fatalf("native failure lost its process exit cause: %v", err)+ }+}+func TestCaptureStopUsesOneBoundedRetirementPath(t *testing.T) {executable, err := os.Executable()if err != nil {diff --git a/internal/app/nativecontrol/session.go b/internal/app/nativecontrol/session.goindex 73fdfed0..34e06c31 100644--- a/internal/app/nativecontrol/session.go+++ b/internal/app/nativecontrol/session.go@@ -714,7 +714,7 @@ func (session *Session) startShare(EncoderIndex: request.EncoderIndex,Profile: profile.Video,},- MicrophoneMixing: request.MicrophoneMixing && session.capabilities.Microphone,+ MicrophoneMixing: request.MicrophoneMixing,ExcludeAudio: request.ExcludeAudio,Profile: profile,EdgeCapacity: request.EdgeCapacity,@@ -1046,22 +1046,7 @@ func viewerEventMessage(event nativeviewer.Event) any {NatTraversalPath: event.NatTraversalPath,}case "edge-quality":- quality := event.Quality- return edgeQualityEvent{- eventEnvelope: base,- SampleTimestampMs: quality.SampleTimestampMs,- SampleWindowMs: quality.SampleWindowMs,- RTPStatsID: quality.RTPStatsID,- TrackIdentifier: quality.TrackIdentifier,- State: quality.State,- Reason: quality.Reason,- IntervalFramesEncoded: quality.IntervalFramesEncoded,- FramesPerSecond: quality.FramesPerSecond,- BitrateKbps: quality.BitrateKbps,- AvailableOutgoingKbps: quality.AvailableOutgoingKbps,- Width: quality.Width,- Height: quality.Height,- }+ return qualityEvent(base, event.Quality)default:return edgeStateEvent{eventEnvelope: base, State: event.State}}@@ -1099,27 +1084,30 @@ func eventMessage(event nativehost.Event) any {NatTraversalPath: event.NatTraversalPath,}case "edge-quality":- quality := event.Quality- return edgeQualityEvent{- eventEnvelope: base,- SampleTimestampMs: quality.SampleTimestampMs,- SampleWindowMs: quality.SampleWindowMs,- RTPStatsID: quality.RTPStatsID,- TrackIdentifier: quality.TrackIdentifier,- State: quality.State,- Reason: quality.Reason,- IntervalFramesEncoded: quality.IntervalFramesEncoded,- FramesPerSecond: quality.FramesPerSecond,- BitrateKbps: quality.BitrateKbps,- AvailableOutgoingKbps: quality.AvailableOutgoingKbps,- Width: quality.Width,- Height: quality.Height,- }+ return qualityEvent(base, event.Quality)default:return captureStateEvent{eventEnvelope: base, State: event.State}}}+func qualityEvent(base eventEnvelope, quality *mediaedge.QualitySample) edgeQualityEvent {+ return edgeQualityEvent{+ eventEnvelope: base,+ SampleTimestampMs: quality.SampleTimestampMs,+ SampleWindowMs: quality.SampleWindowMs,+ RTPStatsID: quality.RTPStatsID,+ TrackIdentifier: quality.TrackIdentifier,+ State: quality.State,+ Reason: quality.Reason,+ IntervalFramesSent: quality.IntervalFramesSent,+ FramesPerSecond: quality.FramesPerSecond,+ BitrateKbps: quality.BitrateKbps,+ AvailableOutgoingKbps: quality.AvailableOutgoingKbps,+ Width: quality.Width,+ Height: quality.Height,+ }+}+func pionICEServers(values []iceServer) ([]webrtc.ICEServer, error) {if len(values) > maxICEServers {return nil, protocolViolation("native ICE server list is too large")diff --git a/internal/app/nativecontrol/session_test.go b/internal/app/nativecontrol/session_test.goindex 2a9d8ad6..1b5d50bb 100644--- a/internal/app/nativecontrol/session_test.go+++ b/internal/app/nativecontrol/session_test.go@@ -279,14 +279,15 @@ func runQuietCaptureFixture(directory string) {}func TestMicrophoneOptInPreservesLegacyAndOrdersStateBeforeCompletion(t *testing.T) {- for _, optIn := range []bool{false, true} {- t.Run(strconv.FormatBool(optIn), func(t *testing.T) {+ for _, capabilities := range []struct{ optIn, microphone bool }{{false, true}, {true, true}, {true, false}} {+ optIn, microphone := capabilities.optIn, capabilities.microphone+ t.Run(strconv.FormatBool(optIn)+"/microphone="+strconv.FormatBool(microphone), func(t *testing.T) {t.Setenv("PIIK_QUIET_CAPTURE_FIXTURE", t.TempDir())executable, err := os.Executable()if err != nil {t.Fatal(err)}- session := New(executable, nativecapture.Capabilities{VideoCapture: true, SoftwareVP8: true, Microphone: true}, false, nil)+ session := New(executable, nativecapture.Capabilities{VideoCapture: true, SoftwareVP8: true, Microphone: microphone}, false, nil)defer session.Close()request := startShareRequest{Version: 10, ID: "request_start", Type: "start-share", ShareID: "share_123456",Source: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"},@@ -310,6 +311,9 @@ func TestMicrophoneOptInPreservesLegacyAndOrdersStateBeforeCompletion(t *testing} else if _, failed := result.(requestFailedResponse); err != nil || !failed {t.Fatalf("volume accepted without an audio source: %v %#v", err, result)}+ if !microphone {+ return // Source gain must work without requesting an unavailable device.+ }result, err = session.Handle(t.Context(), []byte(`{"version":10,"id":"request_mic","type":"set-microphone","shareId":"share_123456","enabled":true,"volume":1}`))if !optIn {if _, failed := result.(requestFailedResponse); err != nil || !failed {diff --git a/internal/app/nativecontrol/wire.go b/internal/app/nativecontrol/wire.goindex 1ab1676f..e8986e08 100644--- a/internal/app/nativecontrol/wire.go+++ b/internal/app/nativecontrol/wire.go@@ -375,7 +375,7 @@ type edgeQualityEvent struct {TrackIdentifier string `json:"trackIdentifier"`State string `json:"state"`Reason *string `json:"reason"`- IntervalFramesEncoded uint64 `json:"intervalFramesEncoded"`+ IntervalFramesSent uint64 `json:"intervalFramesEncoded"` // Published v10 name for output frames.FramesPerSecond float64 `json:"framesPerSecond"`BitrateKbps float64 `json:"bitrateKbps"`AvailableOutgoingKbps float64 `json:"availableOutgoingKbps"`diff --git a/internal/app/nativehost/audio_mix_test.go b/internal/app/nativehost/audio_mix_test.goindex 318963bd..2235e304 100644--- a/internal/app/nativehost/audio_mix_test.go+++ b/internal/app/nativehost/audio_mix_test.go@@ -5,6 +5,7 @@ import ("encoding/binary""os""path/filepath"+ "strings""testing""time"@@ -111,6 +112,8 @@ func TestAudioExclusionFailureMutesOnlySourceAndNeverFallsBack(t *testing.T) {}if err := session.ReplaceSource(attempt, true, excluded); err == nil {t.Fatal("failed exclusion was accepted", fault)+ } else if fault == "PIIK_VIDEO_FAIL" && !strings.Contains(err.Error(), "mft-set-output-type") {+ t.Fatalf("replacement lost the native failure stage: %v", err)}if session.SourceAudio() == nil || *session.SourceAudio() || session.audioStream != nil || session.stream != video {t.Fatal("failed exclusion retained unfiltered audio or retired video", fault)diff --git a/internal/app/nativehost/audio_test.go b/internal/app/nativehost/audio_test.goindex 89353d74..8db318ad 100644--- a/internal/app/nativehost/audio_test.go+++ b/internal/app/nativehost/audio_test.go@@ -7,6 +7,7 @@ import ("io""os""path/filepath"+ "strings""sync/atomic""testing""time"@@ -58,6 +59,7 @@ func runAudioRecoveryCapture() {}audio := microphone || (len(os.Args) > 1 && os.Args[1] == "--capture-audio")if !audio && os.Getenv("PIIK_VIDEO_FAIL") == "1" {+ _, _ = io.WriteString(os.Stderr, "stage=mft-set-output-type\nhresult=0xc00d36b4\ndetail=private capture target\n")os.Exit(2)}if !microphone && audio && os.Args[2] == "exclude" {@@ -132,6 +134,30 @@ func runAudioRecoveryCapture() {}}+func TestCaptureStartupReturnsNativeFailureCodesWithoutPrivateDetail(t *testing.T) {+ t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")+ t.Setenv("PIIK_VIDEO_FAIL", "1")+ executable, err := os.Executable()+ if err != nil {+ t.Fatal(err)+ }+ video := nativecapture.VideoOptions{Codec: "h264", Profile: audioRecoveryProfile(),+ Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}}+ session, err := Start(t.Context(), Options{ShareID: "startup-failure", CaptureProcess: executable,+ Video: video, Profile: QualityProfile{Video: video.Profile, AudioBitrate: 64_000}, EdgeCapacity: 1,+ BindAddress: "127.0.0.1:0"})+ if session != nil {+ _ = session.Close()+ t.Fatal("failed capture started a share")+ }+ if err == nil || !strings.Contains(err.Error(), "mft-set-output-type") || !strings.Contains(err.Error(), "0xc00d36b4") {+ t.Fatalf("startup lost native machine codes: %v", err)+ }+ if strings.Contains(err.Error(), "private") {+ t.Fatalf("startup exposed private helper detail: %v", err)+ }+}+func TestAudioStartupDoesNotRequireSamplesOrStopVideo(t *testing.T) {t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "audio-recovery")executable, err := os.Executable()diff --git a/internal/app/nativehost/publication.go b/internal/app/nativehost/publication.goindex bb29b6ab..7dee87e8 100644--- a/internal/app/nativehost/publication.go+++ b/internal/app/nativehost/publication.go@@ -35,12 +35,12 @@ func (session *Session) PreparePublication(generation, connectionID string, servCurrent: current,PublicationGeneration: generation, ConnectionID: connectionID, Candidate: candidate})},- ConnectionState: func(state webrtc.PeerConnectionState, _ *mediaedge.SelectedPair) {+ ConnectionState: func(state webrtc.PeerConnectionState, _ *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {if state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed {session.remoteInput.RevokeEdge(connectionID)}session.emit(Event{Type: "publication-state", ShareID: session.shareID,- Current: current,+ Current: func() bool { return current() && isCurrentState(state) },PublicationGeneration: generation, ConnectionID: connectionID, State: state.String()})},},diff --git a/internal/app/nativehost/session.go b/internal/app/nativehost/session.goindex 98e7e0cd..22525206 100644--- a/internal/app/nativehost/session.go+++ b/internal/app/nativehost/session.go@@ -178,10 +178,10 @@ func Start(parent context.Context, options Options) (*Session, error) {}if err != nil {cancel()- _ = stream.Close()+ captureErr := stream.Close()<-videoDonesession.retire()- return nil, err+ return nil, errors.Join(err, captureErr)}// Start the optional audio helper only after video initialization, avoiding// concurrent platform source enumeration while continuing to drain video.@@ -294,17 +294,19 @@ func (session *Session) prepareEdge(ConnectionID: connectionID, Candidate: candidate,})},- ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair) {+ ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {if state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed {session.remoteInput.RevokeEdge(connectionID)}session.emit(Event{- Type: "edge-state", ShareID: session.shareID,+ Current: func() bool { return isCurrentState(state) },+ Type: "edge-state", ShareID: session.shareID,ConnectionID: connectionID, State: state.String(),})if pair != nil {session.emit(Event{- Type: "edge-path", ShareID: session.shareID,+ Current: func() bool { return isCurrentState(state) },+ Type: "edge-path", ShareID: session.shareID,ConnectionID: connectionID,LocalType: pair.Local.String(),RemoteType: pair.Remote.String(),@@ -644,8 +646,7 @@ func (session *Session) prepareVideo(allowQuiet,)if err != nil {- _ = replacement.Close()- return nil, CaptureState{}, err+ return nil, CaptureState{}, errors.Join(err, replacement.Close())}if options.RemoteInput && !state.RemoteInput {_ = replacement.Close()diff --git a/internal/app/nativeviewer/input_bridge_test.go b/internal/app/nativeviewer/input_bridge_test.goindex 20699470..f700bc33 100644--- a/internal/app/nativeviewer/input_bridge_test.go+++ b/internal/app/nativeviewer/input_bridge_test.go@@ -1,7 +1,10 @@package nativeviewerimport (+ "context"+ "log/slog""strings"+ "sync""testing""time"@@ -10,9 +13,44 @@ import ("github.com/pion/webrtc/v4")+// Delay the real Native callback while Pion and SCTP recover independently.+type delayedInputStateHandler struct {+ entered chan struct{}+ release chan struct{}+ once sync.Once+}++func (h *delayedInputStateHandler) Enabled(context.Context, slog.Level) bool { return true }+func (h *delayedInputStateHandler) Handle(_ context.Context, record slog.Record) error {+ var event, direction, state string+ record.Attrs(func(a slog.Attr) bool {+ switch a.Key {+ case "event":+ event = a.Value.String()+ case "direction":+ direction = a.Value.String()+ case "state":+ state = a.Value.String()+ }+ return true+ })+ if event == "media-connection-state" && direction == "inbound" && state == "connecting" {+ h.once.Do(func() { close(h.entered); <-h.release })+ }+ return nil+}+func (h *delayedInputStateHandler) WithAttrs([]slog.Attr) slog.Handler { return h }+func (h *delayedInputStateHandler) WithGroup(string) slog.Handler { return h }+// Exercise the actual native receiver and local playback edge, not a second// Browser-only path. Both local playback and relay children use the media tree.func TestInputChannelUsesPlaybackAndRelay(t *testing.T) {+ previous := slog.Default()+ delayed := &delayedInputStateHandler{entered: make(chan struct{}), release: make(chan struct{})}+ slog.SetDefault(slog.New(delayed))+ var releaseOnce sync.Once+ resume := func() { releaseOnce.Do(func() { close(delayed.release) }) }+ t.Cleanup(func() { resume(); slog.SetDefault(previous) })peer := func() *webrtc.PeerConnection {t.Helper()p, err := webrtc.NewPeerConnection(webrtc.Configuration{})@@ -127,6 +165,49 @@ func TestInputChannelUsesPlaybackAndRelay(t *testing.T) {t.Fatal(err)}waitMessage(requests, packet)+ // Restart the same Native PC, but delay its transient loss callback until+ // the current connection and input handshake have recovered.+ offer, err = upstream.CreateOffer(&webrtc.OfferOptions{ICERestart: true})+ if err != nil {+ t.Fatal(err)+ }+ offer = localDescription(upstream, offer)+ result, err = session.AcceptOffer("upstream", offer, nil, true, false)+ if err != nil {+ t.Fatal(err)+ }+ if !result.Reused {+ t.Fatal("restart replaced the Native receiver")+ }+ if err = upstream.SetRemoteDescription(result.Answer); err != nil {+ t.Fatal(err)+ }+ select {+ case <-delayed.entered:+ case <-time.After(5 * time.Second):+ t.Fatal("Native connecting callback was not delayed")+ }+ waitMessage(requests, `{"ready":true}`)+ if err = remote.SendText(`{"ready":true}`); err != nil {+ t.Fatal(err)+ }+ waitMessage(states, `{"ready":true}`)+ if err = local.SendText(packet); err != nil {+ t.Fatal(err)+ }+ waitMessage(requests, packet)+ resume()+ waitMessage(states, `{"ready":false}`)+ waitMessage(requests, `{"ready":true}`)+ if err = remote.SendText(`{"ready":true}`); err != nil {+ t.Fatal(err)+ }+ waitMessage(states, `{"ready":true}`)+ if err = local.SendText(packet); err != nil {+ t.Fatal(err)+ }+ waitMessage(requests, packet)+if err = session.UpdateProfile(nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 30, Bitrate: 2_000_000, Preference: "balanced"}); err != nil {t.Fatal(err)}diff --git a/internal/app/nativeviewer/session.go b/internal/app/nativeviewer/session.goindex a7b4a233..33384b8c 100644--- a/internal/app/nativeviewer/session.go+++ b/internal/app/nativeviewer/session.go@@ -188,18 +188,25 @@ func (session *Session) AcceptOffer(ConnectionID: connectionID, Candidate: candidate,})},- ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, current func() bool) {+ ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {if input != nil {- input.SetUpAvailable(state == webrtc.PeerConnectionStateConnected)+ // Historical loss still releases grants; a recovered transport+ // then requests fresh readiness rather than restoring permission.+ if state != webrtc.PeerConnectionStateConnected {+ input.SetUpAvailable(false)+ }+ if isCurrentState(webrtc.PeerConnectionStateConnected) {+ input.SetUpAvailable(true)+ }}session.emit(Event{- Current: current,+ Current: func() bool { return isCurrentState(state) },Type: "edge-state", ShareID: session.shareID,ConnectionID: connectionID, State: state.String(),})if pair != nil {session.emit(Event{- Current: current,+ Current: func() bool { return isCurrentState(state) },Type: "edge-path", ShareID: session.shareID,ConnectionID: connectionID,LocalType: pair.Local.String(),@@ -308,17 +315,19 @@ func (session *Session) prepareEdge(ConnectionID: connectionID, Candidate: candidate,})},- ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair) {+ ConnectionState: func(state webrtc.PeerConnectionState, pair *mediaedge.SelectedPair, isCurrentState func(webrtc.PeerConnectionState) bool) {if receiver.input != nil && (state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed) {receiver.input.LoseDown(connectionID, nil)}session.emit(Event{- Type: "edge-state", ShareID: session.shareID,+ Current: func() bool { return isCurrentState(state) },+ Type: "edge-state", ShareID: session.shareID,ConnectionID: connectionID, State: state.String(),})if pair != nil {session.emit(Event{- Type: "edge-path", ShareID: session.shareID,+ Current: func() bool { return isCurrentState(state) },+ Type: "edge-path", ShareID: session.shareID,ConnectionID: connectionID,LocalType: pair.Local.String(),RemoteType: pair.Remote.String(),diff --git a/internal/diagnostics/logging.go b/internal/diagnostics/logging.goindex 94bfc741..bc74b417 100644--- a/internal/diagnostics/logging.go+++ b/internal/diagnostics/logging.go@@ -204,11 +204,13 @@ func (writer *LineWriter) Close() error {func PionLoggerFactory(fields ...any) logging.LoggerFactory {factory := logging.NewDefaultLoggerFactory()+ // Dependency ICE detail belongs to opt-in diagnostics, including Pion's+ // error-level candidate observations. Never bypass redaction via stderr.+ writer := Writer("pion")+ writer.logger = writer.logger.With(fields...)+ factory.Writer = writerif slog.Default().Enabled(context.Background(), slog.LevelDebug) {factory.DefaultLogLevel = logging.LogLevelDebug- writer := Writer("pion")- writer.logger = writer.logger.With(fields...)- factory.Writer = writer}return factory}diff --git a/internal/diagnostics/recorder_test.go b/internal/diagnostics/recorder_test.goindex cc1a1209..46526514 100644--- a/internal/diagnostics/recorder_test.go+++ b/internal/diagnostics/recorder_test.go@@ -301,6 +301,50 @@ func TestRecorderCloseAndFailures(t *testing.T) {}}+func TestPionDependencyLogsRespectDiagnosticOptIn(t *testing.T) {+ recorder, err := Open(t.TempDir(), "client", "test")+ if err != nil {+ t.Fatal(err)+ }+ defer recorder.Close()+ stderr, err := os.CreateTemp(t.TempDir(), "stderr")+ if err != nil {+ t.Fatal(err)+ }+ previousLogger, previousStderr := slog.Default(), os.Stderr+ os.Stderr = stderr+ defer func() {+ os.Stderr = previousStderr+ slog.SetDefault(previousLogger)+ _ = stderr.Close()+ }()+ const secret = "must-not-appear-in-report"+ var normal bytes.Buffer+ slog.SetDefault(slog.New(slog.NewTextHandler(&normal, nil)))+ PionLoggerFactory().NewLogger("pc").Errorf("dropping candidate with ufrag %s because it doesn't match", secret)+ slog.Error("capture process failed", Error(errors.New("capture process stopped unexpectedly")))+ slog.SetDefault(recorder.Logger())+ PionLoggerFactory().NewLogger("pc").Errorf("dropping candidate with ufrag %s because it doesn't match", secret)+ output, err := os.ReadFile(stderr.Name())+ if err != nil {+ t.Fatal(err)+ }+ if len(output) != 0 {+ t.Fatal("dependency details bypassed the opt-in diagnostic writer through stderr")+ }+ if strings.Contains(normal.String(), "dropping candidate") || strings.Contains(normal.String(), secret) ||+ !strings.Contains(normal.String(), "capture process stopped unexpectedly") {+ t.Fatal("normal logging lost the owning failure or exposed ICE details")+ }+ data, err := os.ReadFile(recorder.LogPath())+ if err != nil {+ t.Fatal(err)+ }+ if bytes.Contains(data, []byte(secret)) || !bytes.Contains(data, []byte("dropping candidate with ufrag [redacted]")) {+ t.Fatal("Debug logging lost the dependency cause or retained its ICE credential")+ }+}+func TestDependencyDiagnosticsPreserveCausesWithoutSecrets(t *testing.T) {recorder, err := Open(t.TempDir(), "client", "test")if err != nil {diff --git a/internal/media/forwarding/ice.go b/internal/media/forwarding/ice.goindex 85c99f01..281d6cce 100644--- a/internal/media/forwarding/ice.go+++ b/internal/media/forwarding/ice.go@@ -1,11 +1,43 @@package forwardingimport (+ "errors"+ "strings"+"github.com/pion/ice/v4""github.com/pion/transport/v5""github.com/pion/transport/v5/stdnet"+ "github.com/pion/webrtc/v4"+ "github.com/pion/webrtc/v4/pkg/rtcerr")+// AddICECandidate preserves the standard JSON generation at Pion's boundary.+// Pion checks the SDP-line ufrag extension, but ignores UsernameFragment itself.+func AddICECandidate(connection *webrtc.PeerConnection, candidate webrtc.ICECandidateInit) error {+ // Keep Pion's missing-description and end-of-gathering lifecycle handling.+ if connection.RemoteDescription() == nil || candidate.Candidate == "" {+ return connection.AddICECandidate(candidate)+ }+ parsed, err := ice.UnmarshalCandidate(strings.TrimPrefix(candidate.Candidate, "candidate:"))+ if err != nil {+ if errors.Is(err, ice.ErrUnknownCandidateTyp) || errors.Is(err, ice.ErrDetermineNetworkType) {+ return connection.AddICECandidate(candidate)+ }+ return &rtcerr.OperationError{Err: err}+ }+ if candidate.UsernameFragment != nil {+ fragment := *candidate.UsernameFragment+ if fields := strings.Fields(fragment); len(fields) != 1 || fields[0] != fragment {+ return &rtcerr.OperationError{Err: errors.New("ICE username fragment is not an SDP token")}+ }+ if err = parsed.AddExtension(ice.CandidateExtension{Key: "ufrag", Value: fragment}); err != nil {+ return &rtcerr.OperationError{Err: err}+ }+ candidate.Candidate = "candidate:" + parsed.Marshal()+ }+ return connection.AddICECandidate(candidate)+}+// NewUDPMux retains Pion's socket and candidate policy, but reads current local// addresses at each gathering. Its default stdnet snapshot otherwise survives// ICE restart (and every new SFU connection sharing that mux).diff --git a/internal/media/forwarding/ice_test.go b/internal/media/forwarding/ice_test.gonew file mode 100644index 00000000..95049d6e--- /dev/null+++ b/internal/media/forwarding/ice_test.go@@ -0,0 +1,62 @@+package forwarding++import (+ "errors"+ "testing"++ "github.com/pion/webrtc/v4"+ "github.com/pion/webrtc/v4/pkg/rtcerr"+)++func TestCandidateContentErrorsRemainSeparateFromConnectionLifecycle(t *testing.T) {+ connection, err := webrtc.NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = connection.Close() })+ invalid := webrtc.ICECandidateInit{Candidate: "malformed"}+ var invalidState *rtcerr.InvalidStateError+ if err := AddICECandidate(connection, invalid); !errors.As(err, &invalidState) {+ t.Fatalf("missing description lost its lifecycle error: %v", err)+ }+ remote, err := webrtc.NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = remote.Close() })+ if _, err = remote.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {+ t.Fatal(err)+ }+ offer, err := remote.CreateOffer(nil)+ if err != nil {+ t.Fatal(err)+ }+ if err = connection.SetRemoteDescription(offer); err != nil {+ t.Fatal(err)+ }+ const valid = "candidate:probe 1 udp 2122260223 127.0.0.1 49152 typ host"+ fragment := "not a token"+ for _, candidate := range []webrtc.ICECandidateInit{+ invalid,+ {Candidate: "malformed", UsernameFragment: new("current")},+ {Candidate: valid, UsernameFragment: &fragment},+ } {+ var operation *rtcerr.OperationError+ if err := AddICECandidate(connection, candidate); !errors.As(err, &operation) {+ t.Fatalf("candidate content did not retain a disposable operation error: %v", err)+ }+ }+ // Invalid input cannot poison a later valid candidate or gathering completion.+ for _, candidate := range []webrtc.ICECandidateInit{{Candidate: valid}, {}} {+ if err := AddICECandidate(connection, candidate); err != nil {+ t.Fatalf("valid candidate following rejection: %v", err)+ }+ }+ if err := connection.Close(); err != nil {+ t.Fatal(err)+ }+ var operation *rtcerr.OperationError+ if err := AddICECandidate(connection, webrtc.ICECandidateInit{Candidate: valid}); err == nil || errors.As(err, &operation) {+ t.Fatalf("closed transport was classified as disposable content: %v", err)+ }+}diff --git a/internal/media/forwarding/packet_buffer.go b/internal/media/forwarding/packet_buffer.goindex ebd6d514..12345c06 100644--- a/internal/media/forwarding/packet_buffer.go+++ b/internal/media/forwarding/packet_buffer.go@@ -1,6 +1,30 @@package forwarding-import "github.com/pion/transport/v5/packetio"+import (+ "io"++ "github.com/livekit/livekit-server/pkg/sfu/buffer"+ "github.com/pion/transport/v5/packetio"+)++// packetBufferFactory uses LiveKit's claimed video RTCP reader and preserves+// stock Pion SRTP/SRTCP bounds for all remaining streams, including audio.+func packetBufferFactory(factory *buffer.Factory) func(packetio.BufferPacketType, uint32) io.ReadWriteCloser {+ return func(kind packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser {+ if kind == packetio.RTCPBufferPacket {+ if reader := factory.GetRTCPReader(ssrc); reader != nil {+ return reader+ }+ }+ input := &packetBuffer{Buffer: packetio.NewBuffer()}+ limit := 1_000_000+ if kind == packetio.RTCPBufferPacket {+ limit = 100_000+ }+ input.SetLimitSize(limit)+ return input+ }+}// packetBuffer adapts Pion's bounded packet queue to its SRTP BufferFactory's// io.ReadWriteCloser contract, retaining optional packet attributes.diff --git a/internal/media/forwarding/publication.go b/internal/media/forwarding/publication.goindex d86fdd1b..9fbd7f5e 100644--- a/internal/media/forwarding/publication.go+++ b/internal/media/forwarding/publication.go@@ -23,7 +23,6 @@ import ("github.com/pion/interceptor""github.com/pion/rtcp""github.com/pion/rtp"- "github.com/pion/transport/v5/packetio""github.com/pion/webrtc/v4")@@ -107,20 +106,7 @@ func NewPublication(options TransportOptions) (_ *Publication, err error) {if slog.Default().Enabled(context.Background(), slog.LevelDebug) {settings.LoggerFactory = diagnostics.PionLoggerFactory("connectionId", diagnostics.ID(options.ConnectionID))}- settings.BufferFactory = func(kind packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser {- if kind == packetio.RTCPBufferPacket {- if reader := factory.GetRTCPReader(ssrc); reader != nil {- return reader- }- }- input := &packetBuffer{Buffer: packetio.NewBuffer()}- limit := 1_000_000- if kind == packetio.RTCPBufferPacket {- limit = 100_000- }- input.SetLimitSize(limit)- return input- }+ settings.BufferFactory = packetBufferFactory(factory)registry := &interceptor.Registry{}if err = configureRTCPReports(registry, func(ssrc uint32) *sfu.DownTrack {for _, track := range publication.tracks {diff --git a/internal/media/forwarding/transport.go b/internal/media/forwarding/transport.goindex 265fc6ef..55fd536f 100644--- a/internal/media/forwarding/transport.go+++ b/internal/media/forwarding/transport.go@@ -24,7 +24,6 @@ import ("github.com/pion/interceptor""github.com/pion/rtcp""github.com/pion/rtp"- "github.com/pion/transport/v5/packetio""github.com/pion/webrtc/v4")@@ -96,21 +95,7 @@ func NewTransport(options TransportOptions) (_ *Transport, err error) {if slog.Default().Enabled(context.Background(), slog.LevelDebug) {settings.LoggerFactory = diagnostics.PionLoggerFactory("connectionId", diagnostics.ID(options.ConnectionID))}- settings.BufferFactory = func(kind packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser {- if kind == packetio.RTCPBufferPacket {- if reader := factory.GetRTCPReader(ssrc); reader != nil {- return reader- }- }- // Preserve stock Pion SRTP/SRTCP bounds for unclaimed streams, including audio.- input := &packetBuffer{Buffer: packetio.NewBuffer()}- limit := 1_000_000- if kind == packetio.RTCPBufferPacket {- limit = 100_000- }- input.SetLimitSize(limit)- return input- }+ settings.BufferFactory = packetBufferFactory(factory)registry := &interceptor.Registry{}// LiveKit records actual paced sends and owns this extension's sequence space.if err = media.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{diff --git a/internal/server/protocol/types.go b/internal/server/protocol/types.goindex 9bd60706..a1348d93 100644--- a/internal/server/protocol/types.go+++ b/internal/server/protocol/types.go@@ -37,7 +37,7 @@ var (var (degradationPreferences = []string{"maintain-resolution", "balanced", "maintain-framerate"}mediaRoutePhases = []string{"prepare", "active"}- routeDemandReasons = []string{"join", "edge-unavailable", "parent-departed", "capacity-reduction", "sfu-bootstrap", "direct-convergence", "quality-convergence", "root-convergence"}+ routeDemandReasons = []string{"join", "edge-unavailable", "parent-departed", "capacity-reduction", "sfu-bootstrap", "direct-convergence", "quality-convergence", "root-convergence", "publication-attach"}routeDiagnosticFinalRoutes = []string{"direct", "sfu", "waiting", "failed"}routeDiagnosticBuckets = []string{"none", "stale", "endpoint-capacity", "sfu-admission", "candidate-failed", "first-frame-timeout", "operation-deadline", "aborted"}routeDiagnosticStages = []string{"admission", "first-frame", "quality-proof"}@@ -772,8 +772,8 @@ func (v *RouteDiagnosticChild) UnmarshalJSON(data []byte) error {if !inRangeInt(v.Ordinal, 1, MaxViewersPerRoomLimit) {return errors.New("route diagnostic ordinal is out of range")}- if !inRangeInt(v.EffectiveCapacity, 0, MaxEndpointMediaCopyCapacity) ||- !inRangeInt(v.ChildCount, 0, MaxEndpointMediaCopyCapacity) {+ if !inRangeInt(v.EffectiveCapacity, 0, MaxViewersPerRoomLimit) ||+ !inRangeInt(v.ChildCount, 0, MaxViewersPerRoomLimit) {return errors.New("route diagnostic capacity is out of range")}for _, duration := range []*Int{v.DemandAgeMs, v.QueueWaitMs, v.CandidateStartMs,diff --git a/internal/server/route/controller.go b/internal/server/route/controller.goindex eba72797..b1495ab6 100644--- a/internal/server/route/controller.go+++ b/internal/server/route/controller.go@@ -480,14 +480,6 @@ func (c *Controller) UpsertParticipant(input ParticipantInput, nowMs *int64) []*if input.Role == protocol.RoleViewer {released = append(released, c.SetViewerRoutePreference(input.PeerID, input.SessionID, input.RoutePreference, nowMs)...)}- if previousSessionID != input.SessionID {- // Reauthentication opens this session's opportunities even when SFU- // media survived. Restore child and parent opportunities in the same- // bounded continuation owner, without changing healthy media.- for peerID := range c.upstreamByViewer.All() {- c.restoreDirectContinuation(peerID)- }- }return released}// New participants append to the scheduling order.@@ -531,7 +523,6 @@ func (c *Controller) SetViewerRoutePreference(peerID, sessionID string, preferenc.directContinuations.Delete(peerID)c.clearSfuBootstrapForDemand(peerID)c.touchFacts()- c.restoreDirectContinuation(peerID)c.debug("viewer-route-preference", "participant", c.debugPeer(peerID), "preference", preference)if nowMs != nil {c.recordDemand(peerID, *nowMs, c.routeDemandReason(peerID))diff --git a/internal/server/route/controller_test.go b/internal/server/route/controller_test.goindex 25323de1..fb89e494 100644--- a/internal/server/route/controller_test.go+++ b/internal/server/route/controller_test.go@@ -376,6 +376,14 @@ func TestNeverTreatsOneDegradedHostEdgeAsSfuFanoutPressure(t *testing.T) {routes.hydrateEdge(A, peerEdge(HOST, "a_from_host"))routes.hydrateHostPublication("publication_generation_12345678", res("publication_resource"), "")routes.hydrateEdge(B, sfuEdge("publication_generation_12345678", "b_from_sfu", "b_subscription"))+ // Isolate quality convergence: this established SFU relay has already tried+ // its own Peer opportunities, while remaining eligible to supply A.+ for _, plan := range routes.buildCandidates(B, false) {+ if plan.Tuple.Kind == UpstreamPeer {+ routes.consumeCandidateOpportunity(B, plan)+ }+ }+observePersistentDegraded(t, routes, A, "a_from_host", 99)observePersistentSfuHealthy(t, routes, "publication_generation_12345678", routes.Snapshot().Revision, 103)operation := must(t, routes.Reconcile(104).Operation)@@ -1136,6 +1144,14 @@ func TestAcceptsSfuFedPeerParentFromDecodedProgressWithoutFreezeStats(t *testingroutes.hydrateEdge(A, peerEdge(HOST, "a_from_host"))routes.hydrateHostPublication("publication_generation_12345678", res("publication_resource"), "")routes.hydrateEdge(B, sfuEdge("publication_generation_12345678", "b_from_sfu", "b_subscription"))+ // Isolate quality convergence: this established SFU relay has already tried+ // its own Peer opportunities, while remaining eligible to supply A.+ for _, plan := range routes.buildCandidates(B, false) {+ if plan.Tuple.Kind == UpstreamPeer {+ routes.consumeCandidateOpportunity(B, plan)+ }+ }+revision := routes.Snapshot().RevisionobservePersistentSfuHealthy(t, routes, "publication_generation_12345678", revision, 100)metrics := qualityMetrics()@@ -1772,6 +1788,15 @@ func TestAdvancesExactDirectConvergenceFailuresWithoutFailingSfu(t *testing.T) {addViewer(routes, peerID, 1, nil)routes.hydrateEdge(peerID, sfuEdge("publication_1", peerID+"_sfu", peerID+"_subscription"))}+ // These fixture SFU relays already exhausted their own upstream opportunities.+ for _, peerID := range []string{B, C} {+ for _, plan := range routes.buildCandidates(peerID, false) {+ if plan.Tuple.Kind == UpstreamPeer {+ routes.consumeCandidateOpportunity(peerID, plan)+ }+ }+ }+addViewer(routes, A, 0, ms(0))acquisition := must(t, routes.Reconcile(0).Operation)headStartTuple := acquisition.Candidates[0].Tuple@@ -1808,6 +1833,15 @@ func TestRoundRobinsBackgroundDirectConvergenceBetweenSfuViewers(t *testing.T) {routes.hydrateEdge(peerID, sfuEdge("publication", peerID+"_sfu", peerID+"_subscription"))}+ // These fixture SFU relays already exhausted their own upstream opportunities.+ for _, peerID := range []string{B, C} {+ for _, plan := range routes.buildCandidates(peerID, false) {+ if plan.Tuple.Kind == UpstreamPeer {+ routes.consumeCandidateOpportunity(peerID, plan)+ }+ }+ }+nowMs := int64(0)for _, peerID := range []string{A, D} {addViewer(routes, peerID, 0, ms(nowMs))diff --git a/internal/server/route/debug_test.go b/internal/server/route/debug_test.goindex fca92ed2..72da5780 100644--- a/internal/server/route/debug_test.go+++ b/internal/server/route/debug_test.go@@ -1,6 +1,71 @@package route-import "testing"+import (+ "encoding/json"+ "os"+ "testing"++ "github.com/TNTcraftHIM/Piik/internal/server/protocol"+)++func TestDiagnosticReadersAcceptCurrentRoomProducer(t *testing.T) {+ routes := newController(1, Options{})+ addViewer(routes, A, 20, nil)+ routes.SetRelayVolunteer(A, sessionOf(A), true, nil)+ routes.hydrateEdge(A, peerEdge(HOST, "host_a"))+ for _, peer := range []string{B, C, D, E} {+ addViewer(routes, peer, 0, nil)+ routes.hydrateEdge(peer, peerEdge(A, peer+"_edge"))+ }+ routes.SetVideoPublication("new_video", false, 1)+ // Replacement preserves the room edges and prepares one video attachment.+ routes.SetVideoPublication("replacement_video", false, 2)+ routes.Reconcile(3)+ encoded, err := protocol.EncodeServerMessage(protocol.RouteDiagnosticSnapshotMessage{+ Type: "route-diagnostic-snapshot", Snapshot: routes.RouteDiagnosticSnapshot(3),+ })+ if err != nil {+ t.Fatal(err)+ }+ if _, err := protocol.DecodeServerMessage(encoded); err != nil {+ t.Fatalf("current producer rejected: %v", err)+ }+ for _, field := range []string{"effectiveCapacity", "childCount"} {+ var invalid map[string]any+ if err := json.Unmarshal(encoded, &invalid); err != nil {+ t.Fatal(err)+ }+ child := invalid["snapshot"].(map[string]any)["children"].([]any)[0].(map[string]any)+ child[field] = protocol.MaxViewersPerRoomLimit + 1+ raw, err := json.Marshal(invalid)+ if err != nil {+ t.Fatal(err)+ }+ if _, err := protocol.DecodeServerMessage(raw); err == nil {+ t.Fatalf("diagnostic %s exceeded the room resource bound", field)+ }+ }+ // The same production output is replayed by the TypeScript wire fixture.+ raw, err := os.ReadFile("../../../tests/fixtures/wire-samples.json")+ if err != nil {+ t.Fatal(err)+ }+ var fixture struct {+ ServerMessages []struct{ Name, JSON string }+ }+ if err := json.Unmarshal(raw, &fixture); err != nil {+ t.Fatal(err)+ }+ for _, sample := range fixture.ServerMessages {+ if sample.Name == "room-relay-video-attachment-diagnostic" {+ if sample.JSON != string(encoded) {+ t.Fatal("cross-language fixture drifted from the actual room producer")+ }+ return+ }+ }+ t.Fatal("current room diagnostic is missing from the cross-language fixture")+}func TestControllerDebugRequiresInjectedSinkAndRoom(t *testing.T) {var kinds, events []stringdiff --git a/internal/server/route/graph.go b/internal/server/route/graph.goindex 7e1a3df3..7ea9b6a6 100644--- a/internal/server/route/graph.go+++ b/internal/server/route/graph.go@@ -145,14 +145,25 @@ func (c *Controller) parentRouteRetired(edge *CommittedEdge) bool {// rebindCommittedSession preserves the reconnecting peer's edges and// publication, updating them to its new session ID.func (c *Controller) rebindCommittedSession(peerID, sessionID string) {- if ownEdge, ok := c.upstreamByViewer.Get(peerID); ok {- ownEdge.ChildSessionID = sessionID- }- for _, edge := range c.upstreamByViewer.Values() {- if edge.Kind != UpstreamPeer || edge.ParentPeerID != peerID {- continue+ for childPeerID, edge := range c.upstreamByViewer.All() {+ involved := false+ if childPeerID == peerID {+ edge.ChildSessionID = sessionID+ involved = true+ }+ if edge.Kind == UpstreamPeer && edge.ParentPeerID == peerID {+ edge.ParentSessionID = sessionID+ involved = true+ }+ // A failed optional attachment belongs to its endpoint sessions. Proven+ // video and the retained room transport survive reauthentication.+ if edge.Kind == UpstreamSfu && peerID == c.hostPeerID && c.hostPublication != nil &&+ edge.PublicationGeneration == c.hostPublication.Generation {+ involved = true+ }+ if involved && edge.VideoGeneration != "" && !edge.VideoReady {+ edge.VideoGeneration = ""}- edge.ParentSessionID = sessionID}if peerID == c.hostPeerID && c.hostPublication != nil {c.hostPublication.HostSessionID = sessionIDdiff --git a/internal/server/route/operation.go b/internal/server/route/operation.goindex 5eb43cc6..5fee4cbb 100644--- a/internal/server/route/operation.go+++ b/internal/server/route/operation.go@@ -8,19 +8,17 @@ import (// validation is the private validateOrAdvance result.type validation struct {- released []*Resource- exhaustedChildPeerID string- expired bool- consumedGuard bool+ released []*Resource+ exhaustedPeerIDs []string+ expired bool+ consumedGuard bool}// failedPeerIDsFrom deduplicates while preserving argument order.func failedPeerIDsFrom(results ...validation) []string {ids := make([]string, 0, len(results))for _, result := range results {- if result.exhaustedChildPeerID != "" {- ids = append(ids, result.exhaustedChildPeerID)- }+ ids = append(ids, result.exhaustedPeerIDs...)}return dedupeStrings(ids)}@@ -82,6 +80,11 @@ func (c *Controller) Reconcile(nowMs int64) ReconcileResult {FailedPeerIDs: failedPeerIDs, Released: released}}+ // Refresh current parent/session opportunities; the ledger preserves spent attempts.+ for peerID := range c.upstreamByViewer.All() {+ c.restoreDirectContinuation(peerID)+ }+// The loop bound is a count, not an order.for remaining := c.participants.Len(); remaining > 0; remaining-- {bootstrap := c.takeSfuBootstrapCarrier(nowMs, &failedPeerIDs)@@ -375,11 +378,11 @@ func (c *Controller) SkipCurrentCandidate(guard CandidateCursorGuard, nowMs int6}}if op.reason == DemandPublicationAttach {- c.finishPublicationAttach(op, false)- return BeginResult{Accepted: true, FailedPeerIDs: []string{op.childPeerID}, Released: validation.released}+ failed := c.finishPublicationAttach(op, false)+ return BeginResult{Accepted: true, FailedPeerIDs: failed, Released: validation.released}}if bucket == RejectionCandidateFailed {- if isAvailabilityOperation(op.reason) {+ if isAvailabilityOperation(op.reason) || op.reason == DemandDirectConvergence {c.consumeCandidateOpportunity(op.childPeerID, op.candidates[op.cursor])}c.promoteNextDirectWithinHeadStart(op, nowMs)@@ -818,9 +821,9 @@ func (c *Controller) validateOrAdvance(nowMs int64, failedGuard *CandidateGuard)}result.expired = trueif failedBootstrapDemand != "" {- result.exhaustedChildPeerID = failedBootstrapDemand+ result.exhaustedPeerIDs = []string{failedBootstrapDemand}} else if exhausted {- result.exhaustedChildPeerID = op.demandPeerID+ result.exhaustedPeerIDs = []string{op.demandPeerID}}return result}@@ -921,9 +924,9 @@ func (c *Controller) validateOrAdvance(nowMs int64, failedGuard *CandidateGuard)}c.blockAndClear(op, !backgroundConvergence && !sfuBootstrap, &result.released, c.revision != activeRevisionAtStart)if failedBootstrapDemand != "" {- result.exhaustedChildPeerID = failedBootstrapDemand+ result.exhaustedPeerIDs = []string{failedBootstrapDemand}} else if exhausted {- result.exhaustedChildPeerID = op.demandPeerID+ result.exhaustedPeerIDs = []string{op.demandPeerID}c.rememberUnavailableSfuBootstrap(op.demandPeerID)}}@@ -1490,6 +1493,11 @@ func (c *Controller) consumeDirectContinuationCandidate(op *operation) {if op.reason != DemandDirectConvergence || tuple == nil || tuple.Kind != UpstreamPeer {return}+ // Unstarted candidates invalidated by changing facts retain their opportunity.+ plan := op.planAt(op.cursor)+ if op.current != nil && op.current.connectionAttempt == nil && plan != nil {+ c.consumeCandidateOpportunity(op.childPeerID, *plan)+ }continuation, ok := c.directContinuations.Get(op.childPeerID)if !ok {return@@ -1499,7 +1507,6 @@ func (c *Controller) consumeDirectContinuationCandidate(op *operation) {return}continuation.parentPeerIDs = slices.Delete(continuation.parentPeerIDs, index, index+1)- plan := op.planAt(op.cursor)if op.current != nil && op.current.connectionAttempt != nil &&plan != nil &&c.candidateOpportunityAvailable(op.childPeerID, *plan) {diff --git a/internal/server/route/publication.go b/internal/server/route/publication.goindex 6efa907d..fc4c82b5 100644--- a/internal/server/route/publication.go+++ b/internal/server/route/publication.go@@ -155,11 +155,11 @@ func (c *Controller) beginPublicationAttach(input BeginInput, op *operation, chereturn BeginResult{Accepted: true, Operation: c.operationSnapshot(), Released: checked.released}}-// One attempt per exact edge/publication is sufficient for optional attachment.+// Optional attachment gets one attempt per edge, publication and endpoint sessions.// Failure records an unavailable publication, without consuming or resetting// the healthy room transport's NAT opportunity. A new edge or publication gets-// its own attempt; unrelated reconciles never retry this failed attachment.-func (c *Controller) finishPublicationAttach(op *operation, ready bool) {+// its own attempt, as do new endpoint sessions; unrelated reconciles never retry it.+func (c *Controller) finishPublicationAttach(op *operation, ready bool) []string {edge, _ := c.upstreamByViewer.Get(op.childPeerID)edge.VideoGeneration, edge.VideoConnectionID, edge.VideoReady = op.videoGeneration, "", readyif ready {@@ -172,6 +172,24 @@ func (c *Controller) finishPublicationAttach(op *operation, ready bool) {}c.operation = nilc.touchFacts()+ if ready {+ return nil+ }+ // Dependents cannot attach this picture until their upstream recovers.+ // Report that terminal result without retiring voice or spending their own+ // untried attachment. A proven picture ends the affected branch.+ failed := []string{op.childPeerID}+ for index := 0; index < len(failed); index++ {+ for _, peerID := range c.childrenOf(failed[index]) {+ child, _ := c.participants.Get(peerID)+ edge, _ := c.upstreamByViewer.Get(peerID)+ if child != nil && child.sessionIs(edge.ChildSessionID) && !child.departureConfirmed &&+ edge.Usable && edge.PhysicalActive && !c.sourceVideoReady(peerID) {+ failed = append(failed, peerID)+ }+ }+ }+ return failed}func (c *Controller) validatePublicationAttach(op *operation, nowMs int64, failed *CandidateGuard) validation {@@ -184,8 +202,7 @@ func (c *Controller) validatePublicationAttach(op *operation, nowMs int64, faileresult.consumedGuard = failed != nil && op.current != nil && c.guardMatches(*failed, op, op.current)result.expired = nowMs >= op.deadlineAtMsif result.expired || result.consumedGuard {- c.finishPublicationAttach(op, false)- result.exhaustedChildPeerID = op.childPeerID+ result.exhaustedPeerIDs = c.finishPublicationAttach(op, false)}return result}diff --git a/internal/server/route/publication_failure_test.go b/internal/server/route/publication_failure_test.gonew file mode 100644index 00000000..cc5f6460--- /dev/null+++ b/internal/server/route/publication_failure_test.go@@ -0,0 +1,61 @@+package route++import "testing"++func TestFailedPublicationAttachmentReportsItsDependentViewers(t *testing.T) {+ for _, failure := range []string{"transport", "timeout", "rejected"} {+ t.Run(failure, func(t *testing.T) {+ routes := voiceTree(t)+ before := routes.Snapshot()+ routes.SetVideoPublication("share", false, 10)+ var failed []string+ if failure == "rejected" {+ op := must(t, routes.Reconcile(11).Operation)+ failed = routes.SkipCurrentCandidate(cursorGuard(op), 12, RejectionCandidateFailed).FailedPeerIDs+ } else {+ guard := beginVideoAttach(t, routes, 11, A, "video-a")+ if failure == "transport" {+ failed = routes.CandidateFailed(guard, 12).FailedPeerIDs+ } else {+ failed = routes.OperationExpired(routes.Operation().DeadlineAtMs).FailedPeerIDs+ }+ }+ eqSlice(t, failed, []string{A, B})+ for _, child := range []string{A, B} {+ previous, _ := before.UpstreamByViewer.Get(child)+ edge := edgeOf(t, routes, child)+ eq(t, edge.ConnectionID, previous.ConnectionID)+ eq(t, edge.Usable && edge.PhysicalActive, true)+ eq(t, edge.VideoReady, false)+ }+ settled := routes.Reconcile(40_000)+ noOperation(t, settled.Operation)+ eq(t, len(settled.FailedPeerIDs), 0)+ // A failed picture does not poison the voice graph or a later share.+ routes.SetVideoPublication("replacement", false, 40_001)+ a := beginVideoAttach(t, routes, 40_002, A, "replacement-video-a")+ eq(t, routes.CandidateReady(a, 40_003, nil, CandidateProof{VideoGeneration: "replacement"}).Committed, true)+ b := beginVideoAttach(t, routes, 40_004, B, "replacement-video-b")+ eq(t, routes.CandidateReady(b, 40_005, nil, CandidateProof{VideoGeneration: "replacement"}).Committed, true)+ })+ }+}++func TestPublicationAttachmentFailureDoesNotFailIndependentBranches(t *testing.T) {+ routes := newController(2, Options{})+ routes.SetVideoPublication("", false, 0)+ for _, peerID := range []string{A, B, C, D} {+ addViewer(routes, peerID, 1, nil)+ }+ routes.hydrateEdge(A, peerEdge(HOST, "voice-a"))+ routes.hydrateEdge(B, peerEdge(A, "voice-b"))+ routes.hydrateEdge(C, peerEdge(B, "voice-c"))+ routes.hydrateEdge(D, peerEdge(HOST, "voice-d"))+ routes.SetVideoPublication("share", false, 1)+ a := beginVideoAttach(t, routes, 2, A, "video-a")+ eqSlice(t, routes.CandidateFailed(a, 3).FailedPeerIDs, []string{A, B, C})+ d := beginVideoAttach(t, routes, 4, D, "video-d")+ eq(t, routes.CandidateReady(d, 5, nil, CandidateProof{VideoGeneration: "share"}).Committed, true)+ noOperation(t, routes.Reconcile(6).Operation)+ eq(t, edgeOf(t, routes, D).VideoReady, true)+}diff --git a/internal/server/route/publication_test.go b/internal/server/route/publication_test.goindex 7657e4df..54905416 100644--- a/internal/server/route/publication_test.go+++ b/internal/server/route/publication_test.go@@ -220,3 +220,82 @@ func TestAttachedVideoQualityUsesPhysicalVideoIdentityAndNativeProof(t *testing.childPeerID: A, connectionID: "replacement", state: SenderQualityHealthy, acceptedAtMs: 19,}).Accepted, false)}++func TestFailedOptionalPictureReopensOnlyForNewEndpointSession(t *testing.T) {+ for _, endpoint := range []string{"child", "parent", "host"} {+ t.Run(endpoint, func(t *testing.T) {+ routes := voiceTree(t)+ routes.SetVideoPublication("share", false, 10)+ failedChild := B+ if endpoint == "host" {+ failedChild = A+ } else {+ a := beginVideoAttach(t, routes, 11, A, "video-a")+ eq(t, routes.CandidateReady(a, 12, nil, CandidateProof{VideoGeneration: "share"}).Committed, true)+ }+ failed := beginVideoAttach(t, routes, 13, failedChild, "failed-video")+ routes.CandidateFailed(failed, 14)+ before := routes.Snapshot()+ changedPeer := A+ if endpoint == "child" {+ changedPeer = B+ } else if endpoint == "host" {+ changedPeer = HOST+ }+ participant, _ := routes.participants.Get(changedPeer)+ input := viewerInput(changedPeer, participant.sessionID, 1)+ if endpoint == "host" {+ input = hostInput(participant.sessionID, 1)+ }+ routes.UpsertParticipant(input, ms(15))+ noOperation(t, routes.Reconcile(16).Operation)+ eq(t, edgeOf(t, routes, failedChild).VideoGeneration, "share")+ input.SessionID = "new_endpoint_session"+ routes.UpsertParticipant(input, ms(17))+ retry := beginVideoAttach(t, routes, 18, failedChild, "recovered-video")+ retry.ChildSessionID = edgeOf(t, routes, failedChild).ChildSessionID+ eq(t, routes.CandidateReady(retry, 19, nil, CandidateProof{VideoGeneration: "share"}).Committed, true)+ if endpoint == "host" {+ b := beginVideoAttach(t, routes, 20, B, "video-b")+ eq(t, routes.CandidateReady(b, 21, nil, CandidateProof{VideoGeneration: "share"}).Committed, true)+ } else {+ eq(t, edgeOf(t, routes, A).VideoConnectionID, "video-a")+ }+ for _, peerID := range []string{A, B} {+ previous, _ := before.UpstreamByViewer.Get(peerID)+ eq(t, edgeOf(t, routes, peerID).ConnectionID, previous.ConnectionID)+ eq(t, edgeOf(t, routes, peerID).Usable, true)+ }+ // Further endpoint sessions retain proven physical video without another attach.+ input.SessionID = "healthy_endpoint_session"+ routes.UpsertParticipant(input, ms(22))+ noOperation(t, routes.Reconcile(23).Operation)+ eq(t, edgeOf(t, routes, failedChild).VideoConnectionID, "recovered-video")+ })+ }+}++func TestHostSessionReopensFailedSfuPictureWithoutReplacingHealthyPicture(t *testing.T) {+ routes := newController(1, Options{SfuEnabled: true, SfuOnly: true})+ routes.SetVideoPublication("", false, 0)+ addViewer(routes, A, 0, nil)+ addViewer(routes, B, 0, nil)+ routes.hydrateHostPublication("publication", res("publication"), "publisher")+ routes.hydrateEdge(A, sfuEdge("publication", "voice-a", "subscription-a"))+ routes.hydrateEdge(B, sfuEdge("publication", "voice-b", "subscription-b"))+ routes.SetVideoPublication("share", false, 1)+ a := beginVideoAttach(t, routes, 2, A, "failed-a")+ routes.CandidateFailed(a, 3)+ b := beginVideoAttach(t, routes, 4, B, "healthy-b")+ eq(t, routes.CandidateReady(b, 5, nil, CandidateProof{VideoGeneration: "share"}).Committed, true)+ host, _ := routes.participants.Get(HOST)+ routes.UpsertParticipant(hostInput(host.sessionID, 1), ms(6))+ noOperation(t, routes.Reconcile(7).Operation)+ routes.UpsertParticipant(hostInput("replacement-host", 1), ms(8))+ a = beginVideoAttach(t, routes, 9, A, "recovered-a")+ eq(t, routes.CandidateReady(a, 10, nil, CandidateProof{VideoGeneration: "share"}).Committed, true)+ noOperation(t, routes.Reconcile(11).Operation)+ eq(t, edgeOf(t, routes, A).ConnectionID, "voice-a")+ eq(t, edgeOf(t, routes, B).VideoConnectionID, "healthy-b")+ eq(t, routes.Publication().HostSessionID, "replacement-host")+}diff --git a/internal/server/route/session_opportunity_test.go b/internal/server/route/session_opportunity_test.goindex 32826b15..599dc9fa 100644--- a/internal/server/route/session_opportunity_test.go+++ b/internal/server/route/session_opportunity_test.go@@ -27,7 +27,7 @@ func TestRetainedEdgeFailurePreservesNewSessionOpportunity(t *testing.T) {edge := edgeOf(t, routes, A)eq(t, edge.Usable, true)eq(t, edge.ConnectionID, "retained_connection")- if upstream == UpstreamSfu && changed != "none" {+ if upstream == UpstreamSfu {eq(t, must(t, routes.Reconcile(0).Operation).Reason, DemandDirectConvergence)} else {noOperation(t, routes.Reconcile(0).Operation)@@ -119,3 +119,53 @@ func TestRebuiltDirectConnectionConsumesCurrentSessionOpportunity(t *testing.T)}, ms(4)), true)eq(t, hasTuple(must(t, routes.Reconcile(4).Operation), isPeer), false)}++func TestFreshRelayOpportunityBehindHealthySfu(t *testing.T) {+ for _, voiceOnly := range []bool{false, true} {+ for _, nat := range []bool{false, true} {+ t.Run(fmt.Sprintf("voice=%v/nat=%v", voiceOnly, nat), func(t *testing.T) {+ routes := newController(1, Options{SfuEnabled: true, NatPredictionEnabled: nat})+ addViewer(routes, A, 0, nil)+ addViewer(routes, B, 0, nil)+ routes.hydrateHostPublication("publication", res("publication"), "publisher")+ routes.hydrateEdge(A, sfuEdge("publication", "retained_sfu", "subscription_a"))+ routes.hydrateEdge(B, sfuEdge("publication", "parent_sfu", "subscription_b"))+ if voiceOnly {+ routes.SetVideoPublication("", false, 0)+ routes.SetVoiceListeners([]string{HOST, A, B}, 0)+ }+ noOperation(t, routes.Reconcile(0).Operation)+ attempts := 1+ if nat {+ attempts = 4+ }+ for attempt := 1; attempt <= attempts; attempt++ {+ now := int64(attempt * 3)+ routes.SetEffectiveCapacity(B, sessionOf(B), 1, ms(now))+ op := must(t, routes.Reconcile(now).Operation)+ eq(t, op.Reason, DemandDirectConvergence)+ eq(t, op.ChildPeerID, A)+ eq(t, op.Candidates[0].Tuple, peerTuple(B))+ prepared := beginOperation(t, routes, BeginInput{NowMs: now, ConnectionID: fmt.Sprintf("fresh_%d", attempt), Reservation: direct()})+ routes.CandidateFailed(guardFor(A, prepared.Current.Revision, prepared.Current.ConnectionID), now+1)+ eq(t, edgeOf(t, routes, A).ConnectionID, "retained_sfu")+ eq(t, edgeOf(t, routes, A).Usable, true)+ }+ routes.SetEffectiveCapacity(B, sessionOf(B), 2, ms(20))+ noOperation(t, routes.Reconcile(20).Operation)+ routes.UpsertParticipant(viewerInput(B, "fresh_parent_session", 2), ms(21))+ op := must(t, routes.Reconcile(22).Operation)+ eq(t, op.ChildPeerID, A)+ eq(t, op.Candidates[0].Tuple, peerTuple(B))+ if voiceOnly {+ commitRoomPeer(t, routes, 23, "new_peer")+ } else {+ commitCurrent(t, routes, 23, "new_peer")+ }+ routes.SetEffectiveCapacity(B, "fresh_parent_session", 3, ms(26))+ routes.Reconcile(26)+ eq(t, edgeOf(t, routes, A).ConnectionID, "new_peer")+ })+ }+ }+}diff --git a/internal/server/route/viewer_preference_test.go b/internal/server/route/viewer_preference_test.goindex 0486f313..1e3e7505 100644--- a/internal/server/route/viewer_preference_test.go+++ b/internal/server/route/viewer_preference_test.go@@ -1,6 +1,7 @@package routeimport (+ "fmt""testing""github.com/TNTcraftHIM/Piik/internal/server/protocol"@@ -165,6 +166,7 @@ func TestViewerRoutePreferenceP2PExhaustionThenAuto(t *testing.T) {routes.hydrateHostPublication("publication", res("publication"), "publisher")routes.hydrateEdge(A, sfuEdge("publication", "a_sfu", "a_subscription"))routes.hydrateEdge(B, sfuEdge("publication", "b_sfu", "b_subscription"))+ routes.SetViewerRoutePreference(B, sessionOf(B), protocol.ViewerRouteSfu, ms(0))routes.SetViewerRoutePreference(A, sessionOf(A), protocol.ViewerRoutePeer, ms(0))result := routes.Reconcile(1)noOperation(t, result.Operation)@@ -176,3 +178,22 @@ func TestViewerRoutePreferenceP2PExhaustionThenAuto(t *testing.T) {eq(t, op.ChildPeerID, A)eqSlice(t, tuples(op), []CandidateTuple{sfuTuple(PublicationReuse)})}++func TestFreshRelayRespectsSfuPolicy(t *testing.T) {+ for _, siteOnly := range []bool{false, true} {+ t.Run(fmt.Sprintf("siteOnly=%v", siteOnly), func(t *testing.T) {+ routes := newController(1, Options{SfuEnabled: true, SfuOnly: siteOnly})+ addViewer(routes, A, 0, nil)+ addViewer(routes, B, 0, nil)+ routes.hydrateHostPublication("publication", res("publication"), "publisher")+ routes.hydrateEdge(A, sfuEdge("publication", "a_sfu", "subscription_a"))+ routes.hydrateEdge(B, sfuEdge("publication", "b_sfu", "subscription_b"))+ if !siteOnly {+ routes.SetViewerRoutePreference(A, sessionOf(A), protocol.ViewerRouteSfu, ms(0))+ }+ routes.SetEffectiveCapacity(B, sessionOf(B), 1, ms(1))+ noOperation(t, routes.Reconcile(2).Operation)+ eq(t, edgeOf(t, routes, A).ConnectionID, "a_sfu")+ })+ }+}diff --git a/internal/server/sfu/media.go b/internal/server/sfu/media.goindex 44b91f34..04d67baa 100644--- a/internal/server/sfu/media.go+++ b/internal/server/sfu/media.go@@ -195,19 +195,7 @@ func (media *Media) PreparePublication(fence ResourceFence, connectionID string,}publication.emit(MediaEvent{Candidate: value})})- pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {- publication.mu.Lock()- input := publication.input- publication.mu.Unlock()- if input != nil {- input.SetUpAvailable(state == webrtc.PeerConnectionStateConnected)- }- if state == webrtc.PeerConnectionStateFailed {- publication.fail(errors.New("SFU publisher transport failed"))- return- }- publication.emit(MediaEvent{State: state})- })+ pc.OnConnectionStateChange(publication.connectionStateChanged)pc.OnTrack(publication.consumeTrack)pc.OnDataChannel(func(channel *webrtc.DataChannel) {publication.mu.Lock()@@ -224,6 +212,27 @@ func (media *Media) PreparePublication(fence ResourceFence, connectionID string,return nil}+func (publication *mediaPublication) connectionStateChanged(state webrtc.PeerConnectionState) {+ publication.mu.Lock()+ input := publication.input+ publication.mu.Unlock()+ if input != nil {+ // Historical loss retires grants even if this PC has recovered. Recovery+ // requests a fresh handshake; it must never restore the previous grant.+ if state != webrtc.PeerConnectionStateConnected {+ input.SetUpAvailable(false)+ }+ if state != webrtc.PeerConnectionStateFailed && publication.pc.ConnectionState() == webrtc.PeerConnectionStateConnected {+ input.SetUpAvailable(true)+ }+ }+ if state == webrtc.PeerConnectionStateFailed {+ publication.fail(errors.New("SFU publisher transport failed"))+ return+ }+ publication.emit(MediaEvent{State: publication.pc.ConnectionState()})+}+func (publication *mediaPublication) wakeDemand() {select {case publication.demandWake <- struct{}{}:@@ -401,7 +410,7 @@ func (media *Media) AddPublisherICE(fence ResourceFence, connectionID string, caif err != nil {return err}- return publication.pc.AddICECandidate(candidate)+ return forwarding.AddICECandidate(publication.pc, candidate)}func (media *Media) UpdatePublication(fence ResourceFence, connectionID string, metadata PublicationMedia) error {@@ -629,16 +638,7 @@ func (media *Media) PrepareSubscriber(ctx context.Context, fence SubscriptionFenpublication.emitSubscriber(subscriber, MediaEvent{Candidate: value})})transport.PC.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {- if input != nil && (state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed) {- input.LoseDown(fence.ViewerPeerID+":"+connectionID, nil)- }- if state == webrtc.PeerConnectionStateConnected {- if err := transport.SetConnected(); err != nil {- state = webrtc.PeerConnectionStateFailed- }- }- publication.emitSubscriber(subscriber, MediaEvent{State: state})- publication.wakeDemand()+ publication.subscriberStateChanged(subscriber, state)})offer, err := transport.PC.CreateOffer(nil)if err == nil {@@ -650,6 +650,31 @@ func (media *Media) PrepareSubscriber(ctx context.Context, fence SubscriptionFenreturn offer, nil}+func (publication *mediaPublication) subscriberStateChanged(subscriber *mediaSubscription, state webrtc.PeerConnectionState) {+ publication.mu.Lock()+ current := !publication.closed && publication.failure == nil && publication.subscribers[subscriber.key] == subscriber && subscriber.ctx.Err() == nil+ input := publication.input+ publication.mu.Unlock()+ if !current {+ return+ }+ if input != nil && (state == webrtc.PeerConnectionStateDisconnected || state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed) {+ input.LoseDown(subscriber.key.peerID+":"+subscriber.key.connectionID, nil)+ }+ // Failed still belongs to the route's terminal failure owner. Other queued+ // states describe current readiness, never permission to attach an old PC.+ if state != webrtc.PeerConnectionStateFailed {+ state = subscriber.transport.PC.ConnectionState()+ }+ if state == webrtc.PeerConnectionStateConnected {+ if err := subscriber.transport.SetConnected(); err != nil {+ state = webrtc.PeerConnectionStateFailed+ }+ }+ publication.emitSubscriber(subscriber, MediaEvent{State: state})+ publication.wakeDemand()+}+func (media *Media) subscriber(fence SubscriptionFence, connectionID string) (*mediaSubscription, error) {publication, err := media.publication(fence.ResourceFence, "")if err != nil {@@ -702,7 +727,7 @@ func (media *Media) AddSubscriberICE(fence SubscriptionFence, connectionID strinif err != nil {return err}- return subscriber.transport.PC.AddICECandidate(candidate)+ return forwarding.AddICECandidate(subscriber.transport.PC, candidate)}func (media *Media) CloseSubscription(fence SubscriptionFence, connectionID string) error {diff --git a/internal/server/sfu/media_test.go b/internal/server/sfu/media_test.goindex 5ef40aee..b1dc916c 100644--- a/internal/server/sfu/media_test.go+++ b/internal/server/sfu/media_test.go@@ -2,6 +2,7 @@ package sfuimport ("context"+ "errors""fmt""net""strings"@@ -11,6 +12,7 @@ import ("time""github.com/TNTcraftHIM/Piik/internal/media/forwarding"+ "github.com/TNTcraftHIM/Piik/internal/media/inputroute"livekitsfu "github.com/livekit/livekit-server/pkg/sfu""github.com/pion/interceptor""github.com/pion/rtp"@@ -63,6 +65,9 @@ func TestPublicationAdmitsSupportedBitrateCeilings(t *testing.T) {t.Fatal(err)}pc := publication.pc+ if err := service.AddPublisherICE(fence, "publisher_12345678", webrtc.ICECandidateInit{}); !errors.Is(err, webrtc.ErrNoRemoteDescription) {+ t.Fatalf("candidate before remote SDP lost its lifecycle error: %v", err)+ }for _, bitrate := range []uint32{5_000_000, 100_000_001, 240_000_000} {metadata.Formats[0].Bitrate = bitrateif err := service.UpdatePublication(fence, "publisher_12345678", metadata); err != nil {@@ -166,7 +171,13 @@ func runMediaFanout(t *testing.T, codec string, layerCount int) {}return *pc.LocalDescription()}- service := NewMedia(MediaOptions{Settings: settings})+ stateEvents := make(chan MediaEvent, 128)+ service := NewMedia(MediaOptions{Settings: settings, Events: func(event MediaEvent) {+ select {+ case stateEvents <- event:+ default:+ }+ }})defer service.Close()fence := ResourceFence{RoomID: "1234", ShareGeneration: "share_123456", PublicationGeneration: "publication_123456"}admission := NewAdmission(AdmissionOptions{IngressCapacity: 1, EgressCapacity: 2})@@ -221,6 +232,10 @@ func runMediaFanout(t *testing.T, codec string, layerCount int) {t.Fatal("retired subscription kept waiting for input")}publisher := newPeer()+ requests := make(chan string, 32)+ inputChannel, err := publisher.CreateDataChannel(inputroute.Label, nil)+ check(err)+ inputChannel.OnMessage(func(message webrtc.DataChannelMessage) { requests <- string(message.Data) })capability := webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90_000}videoData := livekitsfu.VP8KeyFrame8x8if codec == "h264" {@@ -321,6 +336,7 @@ func runMediaFanout(t *testing.T, codec string, layerCount int) {{ResourceFence: fence, ViewerPeerID: "viewer_abcdef"},}var transports [2]*forwarding.Transport+ inputStates := make(chan string, 32)for index, subscription := range fences {if !admission.ReserveSubscription(subscription) {t.Fatal("subscription admission failed")@@ -337,6 +353,11 @@ func runMediaFanout(t *testing.T, codec string, layerCount int) {}offer = *physical.transport.PC.LocalDescription()receiver := newPeer()+ if index == 0 {+ receiver.OnDataChannel(func(channel *webrtc.DataChannel) {+ channel.OnMessage(func(message webrtc.DataChannelMessage) { inputStates <- string(message.Data) })+ })+ }receiver.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {for {packet, _, readErr := track.ReadRTP()@@ -406,6 +427,40 @@ func runMediaFanout(t *testing.T, codec string, layerCount int) {if len(publisher.GetSenders()) != 2 || len(publication.source.GetDownTracks()) != 2 {t.Fatal("one video/audio publication did not share its source")}+ if codec == "vp8" && layerCount == 1 {+ waitInput := func(messages <-chan string, wanted string) {+ t.Helper()+ for {+ select {+ case message := <-messages:+ if message == wanted {+ return+ }+ case <-ctx.Done():+ t.Fatalf("missing input message %s", wanted)+ }+ }+ }+ check(inputChannel.SendText(`{"ready":true}`))+ waitInput(inputStates, `{"ready":true}`)+ // Pion dispatches state callbacks asynchronously. A Connecting callback+ // may resume after this same physical PC has already recovered.+ for len(requests) > 0 {+ <-requests+ }+ publication.connectionStateChanged(webrtc.PeerConnectionStateConnecting)+ waitInput(inputStates, `{"ready":false}`)+ select {+ case message := <-requests:+ if message != `{"ready":true}` {+ t.Fatalf("fresh input handshake = %s", message)+ }+ case <-time.After(time.Second):+ t.Fatal("late loss left recovered SFU input unavailable")+ }+ check(inputChannel.SendText(`{"ready":true}`))+ waitInput(inputStates, `{"ready":true}`)+ }publication.mu.Lock()bound := 0for _, active := range publication.layers {@@ -445,6 +500,57 @@ func runMediaFanout(t *testing.T, codec string, layerCount int) {if service.AddSubscriberICE(fences[0], "subscriber_123456", webrtc.ICECandidateInit{}) == nil {t.Fatal("retired media accepted signaling")}+ if codec == "vp8" && layerCount == 1 {+ // Hold the existing owner lock across a real Connected -> Closed change.+ // Both Pion callbacks queue behind it; old Connected must not attach a+ // physically closed transport when it eventually acquires the owner.+ offer, err := service.PrepareSubscriber(ctx, fences[0], "delayed_state")+ check(err)+ delayed, err := service.subscriber(fences[0], "delayed_state")+ check(err)+ <-webrtc.GatheringCompletePromise(delayed.transport.PC)+ offer = *delayed.transport.PC.LocalDescription()+ receiver := newPeer()+ check(receiver.SetRemoteDescription(offer))+ answer, err := receiver.CreateAnswer(nil)+ check(err)+ answer = waitGathering(receiver, answer)+ publication.mu.Lock()+ err = delayed.transport.PC.SetRemoteDescription(answer)+ for err == nil && delayed.transport.PC.ConnectionState() != webrtc.PeerConnectionStateConnected {+ select {+ case <-time.After(time.Millisecond):+ case <-ctx.Done():+ err = ctx.Err()+ }+ }+ if err == nil {+ err = delayed.transport.PC.Close()+ }+ publication.mu.Unlock()+ check(err)+ closedEvents := 0+ for closedEvents < 3 { // Connecting, Connected and Closed were all held.+ select {+ case event := <-stateEvents:+ if event.ConnectionID != "delayed_state" || event.State == webrtc.PeerConnectionStateUnknown {+ continue+ }+ if event.State == webrtc.PeerConnectionStateConnected {+ t.Fatal("late Connected reactivated a closed SFU subscriber")+ }+ if event.State == webrtc.PeerConnectionStateClosed {+ closedEvents+++ }+ case <-ctx.Done():+ t.Fatal("delayed subscriber callbacks did not settle")+ }+ }+ if len(publication.source.GetDownTracks()) != 1 {+ t.Fatal("closed SFU subscriber acquired source membership")+ }+ check(service.CloseSubscription(fences[0], "delayed_state"))+ }check(service.ClosePublication(fence))check(service.ClosePublication(fence))if publication.pc.ConnectionState() != webrtc.PeerConnectionStateClosed ||diff --git a/internal/server/sfu/voice.go b/internal/server/sfu/voice.goindex d3fd4386..7cf2e179 100644--- a/internal/server/sfu/voice.go+++ b/internal/server/sfu/voice.go@@ -49,13 +49,16 @@ type VoiceMediaEvent struct {Description *VoiceDescriptionCandidate *webrtc.ICECandidateInitState webrtc.PeerConnectionState+ // Recheck transport evidence after the receiver acquires its authority lock.+ IsCurrentState func() bool}-// VoiceRuntime leaves the existing video runtime and its implementations unchanged.+// VoiceRuntime commits plans and retirement synchronously. The returned work+// performs SDP or physical close outside the caller's authority mutex.type VoiceRuntime interface {- ConfigureVoice(VoiceConnectionPlan, func(VoiceMediaEvent)) error+ ConfigureVoice(VoiceConnectionPlan, func(VoiceMediaEvent)) (func() error, error)SignalVoice(room, peer, session, connection string, signal VoiceSignal) error- CloseVoice(room, peer, session, connection string) error+ CloseVoice(room, peer, session, connection string) func() error}type voiceKey struct{ room, peer, session, connection string }@@ -100,21 +103,27 @@ func normalizeVoicePlan(plan VoiceConnectionPlan) (VoiceConnectionPlan, error) {return plan, nil}-func (media *Media) ConfigureVoice(plan VoiceConnectionPlan, emit func(VoiceMediaEvent)) error {+// ConfigureVoice commits publication authority before returning SDP work. The+// caller runs that work outside its authority lock; it uses the current plan.+func (media *Media) ConfigureVoice(plan VoiceConnectionPlan, emit func(VoiceMediaEvent)) (func() error, error) {plan, err := normalizeVoicePlan(plan)if err != nil {- return err+ return nil, err}if emit == nil {- return errors.New("voice callback is required")+ return nil, errors.New("voice callback is required")}v := media.voicev.mu.Lock()if v.closed {v.mu.Unlock()- return errors.New("voice media is closed")+ return nil, errors.New("voice media is closed")}c := v.connections[plan.key()]+ if c != nil && c.closed {+ v.mu.Unlock()+ return nil, errors.New("voice connection is retiring")+ }if c == nil {// One committed connection per member. The room's one transition may// replace both the Host junction and its first child connection.@@ -126,12 +135,12 @@ func (media *Media) ConfigureVoice(plan VoiceConnectionPlan, emit func(VoiceMedi}if roomCount >= maxVoicePublications+2 {v.mu.Unlock()- return errors.New("voice connection capacity is exhausted")+ return nil, errors.New("voice connection capacity is exhausted")}c, err = newVoiceConnection(v, plan, emit)if err != nil {v.mu.Unlock()- return err+ return nil, err}v.connections[plan.key()] = c}@@ -150,7 +159,7 @@ func (media *Media) ConfigureVoice(plan VoiceConnectionPlan, emit func(VoiceMedi}}v.mu.Unlock()- return c.negotiate(false)+ return func() error { return c.negotiate(false) }, nil}func (media *Media) SignalVoice(room, peer, session, connection string, signal VoiceSignal) error {@@ -164,25 +173,29 @@ func (media *Media) SignalVoice(room, peer, session, connection string, signal Vreturn c.signal(signal)}-// A delayed retirement cannot close another connection or a replacement session.-func (media *Media) CloseVoice(room, peer, session, connection string) error {+// CloseVoice synchronously revokes delivery, retaining the capacity charge until+// the caller runs the returned physical close and reader join outside authority.+func (media *Media) CloseVoice(room, peer, session, connection string) func() error {v := media.voicev.mu.Lock()c := v.connections[voiceKey{room, peer, session, connection}]if c != nil {- delete(v.connections, c.key)+ c.retireLocked()}v.mu.Unlock()if c == nil {return nil}- return c.close()+ return c.close}func (v *voiceMedia) close() error {v.mu.Lock()v.closed = trueconnections := v.connections+ for _, c := range connections {+ c.retireLocked()+ }v.connections = make(map[voiceKey]*voiceConnection)v.mu.Unlock()var err errordiff --git a/internal/server/sfu/voice_connection.go b/internal/server/sfu/voice_connection.goindex ba895899..acbf78c5 100644--- a/internal/server/sfu/voice_connection.go+++ b/internal/server/sfu/voice_connection.go@@ -10,6 +10,7 @@ import ("github.com/pion/interceptor""github.com/pion/rtp""github.com/pion/webrtc/v4"+ "github.com/pion/webrtc/v4/pkg/rtcerr")var voiceCodec = webrtc.RTPCodecCapability{@@ -154,6 +155,12 @@ func (c *voiceConnection) dispatch(event VoiceMediaEvent) {c.owner.mu.Unlock()if current {event.RoomID, event.PeerID, event.SessionID, event.ConnectionID = c.key.room, c.key.peer, c.key.session, c.key.connection+ state := event.State+ event.IsCurrentState = func() bool {+ c.owner.mu.Lock()+ defer c.owner.mu.Unlock()+ return !c.closed && c.owner.connections[c.key] == c && c.pc.ConnectionState() == state+ }emit(event)}}@@ -349,7 +356,7 @@ func (c *voiceConnection) signal(signal VoiceSignal) error {c.candidates = append(c.candidates, *signal.Candidate)return nil}- return c.pc.AddICECandidate(*signal.Candidate)+ return c.addCandidate(*signal.Candidate)}answer := signal.Descriptionif c.pending == nil || answer.Revision != c.pending.description.Revision || answer.SDP.Type != webrtc.SDPTypeAnswer ||@@ -372,7 +379,9 @@ func (c *voiceConnection) signal(signal VoiceSignal) error {c.remote = remotec.owner.mu.Unlock()for _, candidate := range c.candidates {- if err := c.pc.AddICECandidate(candidate); err != nil {+ // Candidates are disposable input, not the SDP transaction. A rejected+ // candidate must not strand an already-applied answer in pending.+ if err := c.addCandidate(candidate); err != nil {return err}}@@ -382,6 +391,15 @@ func (c *voiceConnection) signal(signal VoiceSignal) error {return c.offer(false)}+func (c *voiceConnection) addCandidate(candidate webrtc.ICECandidateInit) error {+ err := forwarding.AddICECandidate(c.pc, candidate)+ var candidateError *rtcerr.OperationError+ if errors.As(err, &candidateError) {+ return nil+ }+ return err+}+func (c *voiceConnection) startReader() bool {c.owner.mu.Lock()defer c.owner.mu.Unlock()@@ -460,17 +478,27 @@ func (c *voiceConnection) consume(track *webrtc.TrackRemote, receiver *webrtc.RT}}+// retireLocked revokes packet delivery before physical close can block.+func (c *voiceConnection) retireLocked() {+ c.closed = true+ for _, out := range c.outgoing {+ out.disable()+ }+}+func (c *voiceConnection) close() error {c.closeOnce.Do(func() {c.owner.mu.Lock()- c.closed = true- for _, out := range c.outgoing {- out.disable()- }+ c.retireLocked()c.owner.mu.Unlock()close(c.stop)c.closeErr = c.pc.Close()c.readers.Wait()+ c.owner.mu.Lock()+ if c.owner.connections[c.key] == c {+ delete(c.owner.connections, c.key)+ }+ c.owner.mu.Unlock()})return c.closeErr}diff --git a/internal/server/sfu/voice_test.go b/internal/server/sfu/voice_test.goindex d48080bb..2fe6dc4b 100644--- a/internal/server/sfu/voice_test.go+++ b/internal/server/sfu/voice_test.go@@ -237,7 +237,13 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {}// Pause the first answer to prove a forged publication cannot consume it.a.hold.Store(true)- if err = service.ConfigureVoice(aPlan, a.event); err != nil {+ connected := make(chan VoiceMediaEvent, 1)+ if err = configureVoiceForTest(service, aPlan, func(event VoiceMediaEvent) {+ if event.State == webrtc.PeerConnectionStateConnected {+ connected <- event+ }+ a.event(event)+ }); err != nil {t.Fatal(err)}var first VoiceDescription@@ -250,16 +256,25 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {if err = a.answer(first, true); err != nil {t.Fatal(err)}- if err = service.ConfigureVoice(bPlan, b.event); err != nil {+ if err = configureVoiceForTest(service, bPlan, b.event); err != nil {t.Fatal(err)}wait(func() bool { return a.count(0xb1) >= 3 && b.count(0xa1) >= 3 && b.count(0xa2) >= 3 })+ var connectedEvent VoiceMediaEvent+ select {+ case connectedEvent = <-connected:+ case <-ctx.Done():+ t.Fatal(ctx.Err())+ }+ if connectedEvent.IsCurrentState == nil || !connectedEvent.IsCurrentState() {+ t.Fatal("live voice transport did not provide current connection evidence")+ }if a.count(0xa1) != 0 || b.count(0xb1) != 0 {t.Fatal("voice was echoed to its incoming connection")}// A late close from a previous session cannot touch the current physical PC.- if err = service.CloseVoice(aPlan.RoomID, aPlan.PeerID, "old_session", aPlan.ConnectionID); err != nil {+ if err = closeVoiceForTest(service, aPlan.RoomID, aPlan.PeerID, "old_session", aPlan.ConnectionID); err != nil {t.Fatal(err)}before := b.count(0xa1)@@ -273,7 +288,7 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {a.hold.Store(true)revoked := aPlanrevoked.Publish = []VoicePublication{a2}- if err = service.ConfigureVoice(revoked, a.event); err != nil {+ if err = configureVoiceForTest(service, revoked, a.event); err != nil {t.Fatal(err)}var pending VoiceDescription@@ -298,12 +313,12 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {// Reusing a grant must reuse its empty media section, not grow SDP forever.for range 2 {- if err = service.ConfigureVoice(aPlan, a.event); err != nil {+ if err = configureVoiceForTest(service, aPlan, a.event); err != nil {t.Fatal(err)}before = b.count(0xa1)wait(func() bool { return b.count(0xa1) >= before+3 })- if err = service.ConfigureVoice(revoked, a.event); err != nil {+ if err = configureVoiceForTest(service, revoked, a.event); err != nil {t.Fatal(err)}wait(func() bool {@@ -333,7 +348,7 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {}withoutA2 := bPlanwithoutA2.Subscribe = []VoicePublication{a1}- if err = service.ConfigureVoice(withoutA2, b.event); err != nil {+ if err = configureVoiceForTest(service, withoutA2, b.event); err != nil {t.Fatal(err)}beforeB = a.count(0xb1)@@ -344,7 +359,7 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {if b.count(0xa2) != stopped {t.Fatal("revoked subscription kept receiving before SDP acknowledgement")}- if err = service.ConfigureVoice(bPlan, b.event); err != nil {+ if err = configureVoiceForTest(service, bPlan, b.event); err != nil {t.Fatal(err)}b.hold.Store(false)@@ -357,9 +372,12 @@ func TestVoiceJunctionDuplexAuthorizationAndExactRetirement(t *testing.T) {a.sources[a2.ID].active.Store(false)beforeB = a.count(0xb1)wait(func() bool { return a.count(0xb1) >= beforeB+3 })- if err = service.CloseVoice(aPlan.RoomID, aPlan.PeerID, aPlan.SessionID, aPlan.ConnectionID); err != nil {+ if err = closeVoiceForTest(service, aPlan.RoomID, aPlan.PeerID, aPlan.SessionID, aPlan.ConnectionID); err != nil {t.Fatal(err)}+ if connectedEvent.IsCurrentState() {+ t.Fatal("queued connection evidence survived physical voice retirement")+ }if err = service.SignalVoice(aPlan.RoomID, aPlan.PeerID, aPlan.SessionID, aPlan.ConnectionID, VoiceSignal{Renegotiate: true}); err == nil {t.Fatal("closed connection accepted signaling")}@@ -379,7 +397,7 @@ func TestVoiceFullRoomAdmitsBothPublicationReplacementFacets(t *testing.T) {}})configure := func(peer, connection string) error {- return service.ConfigureVoice(VoiceConnectionPlan{+ return configureVoiceForTest(service, VoiceConnectionPlan{RoomID: "1234", PeerID: peer, SessionID: "session_" + peer, ConnectionID: connection,}, func(VoiceMediaEvent) {})}@@ -404,25 +422,74 @@ func TestVoiceFullRoomAdmitsBothPublicationReplacementFacets(t *testing.T) {}}+func TestVoiceQueuedCandidateFailureDoesNotPoisonNegotiation(t *testing.T) {+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)+ defer cancel()+ service := NewMedia(MediaOptions{})+ t.Cleanup(func() { _ = service.Close() })+ plan := VoiceConnectionPlan{RoomID: "1234", PeerID: "peer", SessionID: "session", ConnectionID: "connection"}+ peer := newVoiceFixturePeer(t, ctx, service, webrtc.SettingEngine{}, plan, nil)+ peer.hold.Store(true)+ if err := configureVoiceForTest(service, plan, peer.event); err != nil {+ t.Fatal(err)+ }+ var first VoiceDescription+ select {+ case first = <-peer.held:+ case <-ctx.Done():+ t.Fatal(ctx.Err())+ }+ // One disposable candidate must not leave an already-applied SDP answer+ // pending forever or block later changes to this admitted connection.+ if err := service.SignalVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID,+ VoiceSignal{Candidate: &webrtc.ICECandidateInit{Candidate: "candidate:malformed"}}); err != nil {+ t.Fatal(err)+ }+ if err := peer.answer(first, false); err != nil {+ t.Fatalf("valid answer was rejected by a queued candidate: %v", err)+ }+ for _, candidate := range []string{"candidate:malformed", "candidate:control 1 udp 2122260223 127.0.0.1 9999 typ host"} {+ if err := service.SignalVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID,+ VoiceSignal{Candidate: &webrtc.ICECandidateInit{Candidate: candidate}}); err != nil {+ t.Fatalf("live candidate prevented continued signaling: %v", err)+ }+ }+ if err := service.SignalVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID,+ VoiceSignal{Renegotiate: true}); err != nil {+ t.Fatal(err)+ }+ select {+ case next := <-peer.held:+ if next.Revision <= first.Revision {+ t.Fatal("renegotiation did not advance")+ }+ if err := peer.answer(next, false); err != nil {+ t.Fatal(err)+ }+ case <-ctx.Done():+ t.Fatal("queued candidate blocked later negotiation")+ }+}+func TestVoicePlanBoundsAndMediaShutdown(t *testing.T) {service := NewMedia(MediaOptions{})plan := VoiceConnectionPlan{RoomID: "1234", PeerID: "peer", SessionID: "session", ConnectionID: "connection"}for i := range maxVoicePublications + 1 {plan.Publish = append(plan.Publish, VoicePublication{ID: fmt.Sprint("voice_", i), PeerID: "speaker"})}- if err := service.ConfigureVoice(plan, func(VoiceMediaEvent) {}); err == nil {+ if err := configureVoiceForTest(service, plan, func(VoiceMediaEvent) {}); err == nil {t.Fatal("oversized plan allocated a connection")}plan.Publish = slices.Clone(plan.Publish[:1])plan.Subscribe = slices.Clone(plan.Publish)- if err := service.ConfigureVoice(plan, func(VoiceMediaEvent) {}); err == nil {+ if err := configureVoiceForTest(service, plan, func(VoiceMediaEvent) {}); err == nil {t.Fatal("looping publication was admitted in both directions")}plan.Publish, plan.Subscribe = nil, nilclosed := make(chan error, 1)- if err := service.ConfigureVoice(plan, func(event VoiceMediaEvent) {+ if err := configureVoiceForTest(service, plan, func(event VoiceMediaEvent) {if event.Description != nil {- closed <- service.CloseVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID)+ closed <- closeVoiceForTest(service, plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID)}}); err != nil {t.Fatal(err)@@ -436,13 +503,13 @@ func TestVoicePlanBoundsAndMediaShutdown(t *testing.T) {t.Fatal("voice callback could not retire its own connection")}plan.ConnectionID = "shutdown_connection"- if err := service.ConfigureVoice(plan, func(VoiceMediaEvent) {}); err != nil {+ if err := configureVoiceForTest(service, plan, func(VoiceMediaEvent) {}); err != nil {t.Fatal(err)}if err := service.Close(); err != nil {t.Fatal(err)}- if err := service.ConfigureVoice(plan, func(VoiceMediaEvent) {}); err == nil {+ if err := configureVoiceForTest(service, plan, func(VoiceMediaEvent) {}); err == nil {t.Fatal("closed media admitted voice")}service.voice.mu.Lock()@@ -467,7 +534,7 @@ func TestVoiceSilentTransportAcceptsLatePublicationOnSameConnection(t *testing.Ta := newVoiceFixturePeer(t, ctx, service, settings, aPlan, map[string]byte{pub.ID: 0x71})b := newVoiceFixturePeer(t, ctx, service, settings, bPlan, nil)for _, peer := range []*voiceFixturePeer{a, b} {- if err := service.ConfigureVoice(peer.plan, peer.event); err != nil {+ if err := configureVoiceForTest(service, peer.plan, peer.event); err != nil {t.Fatal(err)}}@@ -499,10 +566,10 @@ func TestVoiceSilentTransportAcceptsLatePublicationOnSameConnection(t *testing.Tt.Fatal("silent transport did not reserve exactly one audio slot")}aPlan.Publish, bPlan.Subscribe = []VoicePublication{pub}, []VoicePublication{pub}- if err := service.ConfigureVoice(aPlan, a.event); err != nil {+ if err := configureVoiceForTest(service, aPlan, a.event); err != nil {t.Fatal(err)}- if err := service.ConfigureVoice(bPlan, b.event); err != nil {+ if err := configureVoiceForTest(service, bPlan, b.event); err != nil {t.Fatal(err)}wait(func() bool { return b.count(0x71) >= 3 })@@ -513,3 +580,130 @@ func TestVoiceSilentTransportAcceptsLatePublicationOnSameConnection(t *testing.Tt.Fatal("late microphone replaced the admitted connection or missed its idle slot")}}++func configureVoiceForTest(service *Media, plan VoiceConnectionPlan, emit func(VoiceMediaEvent)) error {+ work, err := service.ConfigureVoice(plan, emit)+ if err == nil && work != nil {+ err = work()+ }+ return err+}+func closeVoiceForTest(service *Media, room, peer, session, connection string) error {+ if work := service.CloseVoice(room, peer, session, connection); work != nil {+ return work()+ }+ return nil+}++func TestVoiceDeferredWorkUsesCurrentPlanAndCannotReviveRetiredConnection(t *testing.T) {+ service := NewMedia(MediaOptions{})+ t.Cleanup(func() { _ = service.Close() })+ plan := VoiceConnectionPlan{RoomID: "room", PeerID: "peer", SessionID: "session", ConnectionID: "connection"}+ emit := func(VoiceMediaEvent) {}+ oldWork, err := service.ConfigureVoice(plan, emit)+ if err != nil {+ t.Fatal(err)+ }+ latest := plan+ latest.Publish = []VoicePublication{{ID: "latest", PeerID: "peer"}}+ newWork, err := service.ConfigureVoice(latest, emit)+ if err != nil {+ t.Fatal(err)+ }+ if err := newWork(); err != nil {+ t.Fatal(err)+ }+ if err := oldWork(); err != nil {+ t.Fatal(err)+ }+ service.voice.mu.Lock()+ c := service.voice.connections[plan.key()]+ if !slices.Equal(c.plan.Publish, latest.Publish) {+ t.Error("old work replaced current publication authority")+ }+ service.voice.mu.Unlock()+ closeOld := service.CloseVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID)+ if !c.closed {+ t.Fatal("retirement did not revoke delivery synchronously")+ }+ if err := oldWork(); err == nil {+ t.Fatal("retired negotiation succeeded")+ }+ service.voice.mu.Lock()+ if service.voice.connections[plan.key()] != c {+ t.Error("retirement lost its physical capacity charge")+ }+ service.voice.mu.Unlock()+ if _, err := service.ConfigureVoice(latest, emit); err == nil {+ t.Fatal("configuration reused a retiring physical key")+ }+ if err := closeOld(); err != nil {+ t.Fatal(err)+ }+ replacementWork, err := service.ConfigureVoice(latest, emit)+ if err != nil {+ t.Fatal(err)+ }+ if err := closeOld(); err != nil {+ t.Fatal(err)+ }+ if err := replacementWork(); err != nil {+ t.Fatalf("old physical close retired replacement: %v", err)+ }+}++func TestVoiceRetirementKeepsPhysicalCapacityChargedUntilReadersJoin(t *testing.T) {+ service := NewMedia(MediaOptions{})+ var held []*voiceConnection+ defer func() {+ for _, c := range held {+ if c != nil {+ c.readers.Done()+ }+ }+ _ = service.Close()+ }()+ var closeFirst func() error+ for i := range maxVoicePublications + 2 {+ plan := VoiceConnectionPlan{RoomID: "room", PeerID: "peer", SessionID: "session", ConnectionID: fmt.Sprint("connection", i)}+ if _, err := service.ConfigureVoice(plan, func(VoiceMediaEvent) {}); err != nil {+ t.Fatal(err)+ }+ service.voice.mu.Lock()+ c := service.voice.connections[plan.key()]+ c.readers.Add(1) // Hold the existing reader-join boundary of physical close.+ service.voice.mu.Unlock()+ held = append(held, c)+ work := service.CloseVoice(plan.RoomID, plan.PeerID, plan.SessionID, plan.ConnectionID)+ if i == 0 {+ closeFirst = work+ }+ }+ extra := VoiceConnectionPlan{RoomID: "room", PeerID: "peer", SessionID: "session", ConnectionID: "extra"}+ if _, err := service.ConfigureVoice(extra, func(VoiceMediaEvent) {}); err == nil {+ t.Fatal("retiring physical connections escaped room capacity")+ }+ done := make(chan error, 1)+ go func() { done <- closeFirst() }()+ select {+ case err := <-done:+ t.Fatalf("close escaped reader join: %v", err)+ case <-time.After(20 * time.Millisecond):+ }+ if _, err := service.ConfigureVoice(extra, func(VoiceMediaEvent) {}); err == nil {+ t.Fatal("incomplete physical close released room capacity")+ }+ held[0].readers.Done()+ held[0] = nil+ select {+ case err := <-done:+ if err != nil {+ t.Fatal(err)+ }+ case <-time.After(time.Second):+ t.Fatal("physical close did not settle")+ }+ if _, err := service.ConfigureVoice(extra, func(VoiceMediaEvent) {}); err != nil {+ t.Fatalf("completed close retained room capacity: %v", err)+ }+}diff --git a/internal/server/signal/router.go b/internal/server/signal/router.goindex 6698a405..98ffd74d 100644--- a/internal/server/signal/router.go+++ b/internal/server/signal/router.go@@ -188,7 +188,7 @@ type router struct {resourceWaiters ordered.Map[string, struct{}]sfuDrainTasks ordered.Map[sfu.SubscriptionFence, *sfuDrainTask]// inflight counts the goroutines that will re-acquire mu: the pump- // drivers, fresh SFU config issues and drains.+ // drivers, fresh SFU config issues, voice negotiations and drains.inflight intclosing bool}diff --git a/internal/server/signal/router_sfu.go b/internal/server/signal/router_sfu.goindex 551e4118..dc993da8 100644--- a/internal/server/signal/router_sfu.go+++ b/internal/server/signal/router_sfu.go@@ -15,14 +15,15 @@ const (drainPublication drainKind = "publication"drainSubscription drainKind = "subscription"drainVideoPublication drainKind = "video-publication"+ drainVoice drainKind = "voice")// sfuDrainTask owns one asynchronous exact physical close.type sfuDrainTask struct {- kind drainKind- fence sfu.SubscriptionFence- operation *drainOperation- closeVideo func() error+ kind drainKind+ fence sfu.SubscriptionFence+ operation *drainOperation+ closePhysical func() error}// drainOperation is the promise runSfuDrain tracked: done is closed after err@@ -134,6 +135,9 @@ func (r *router) sendFreshSfuConfig(participant authenticatedRouteParticipant) {if publication == nil || !publication.PhysicalActive {return}+ if publication.Resource != nil && r.sfuVideoRetiring(resourceFence(publication.Resource)) {+ return+ }connectionID := publication.ConnectionIDrevision := snapshot.Revisionif participant.role == protocol.RoleViewer {@@ -200,6 +204,10 @@ func (r *router) prepareSfuVideoAttachment(roomID string, operation *route.Opera!hasChild || child.SessionID != operation.ChildSessionID {return}+ if r.sfuVideoRetiring(resourceFence(publication.Resource)) {+ r.resourceWaiters.Set(roomID, struct{}{})+ return+ }config := protocol.SfuConfigMessage{Type: "sfu-config", Revision: protocol.Int(operation.Current.Revision),PublicationGeneration: publication.Generation}config.ConnectionID = publication.ConnectionID@@ -250,11 +258,22 @@ func (r *router) retireSfuVideoPublication(fence sfu.ResourceFence, connectionIDreturn}key := sfu.SubscriptionFence{ResourceFence: fence, ConnectionID: connectionID}- task := &sfuDrainTask{kind: drainVideoPublication, fence: key, closeVideo: closeVideo}+ task := &sfuDrainTask{kind: drainVideoPublication, fence: key, closePhysical: closeVideo}r.sfuDrainTasks.Set(key, task)r.runSfuDrain(key, task)}+// The logical room reservation also owns its retiring picture facet. Do not+// issue a replacement until its exact physical close and reader join settle.+func (r *router) sfuVideoRetiring(fence sfu.ResourceFence) bool {+ for _, task := range r.sfuDrainTasks.All() {+ if task.kind == drainVideoPublication && task.fence.ResourceFence == fence {+ return true+ }+ }+ return false+}+// scheduleSfuPublicationDrain deletes sibling subscription tasks during// the live iteration.func (r *router) scheduleSfuPublicationDrain(fence sfu.ResourceFence) {@@ -302,16 +321,28 @@ func (r *router) runSfuDrain(key sfu.SubscriptionFence, task *sfuDrainTask) *drareturn operation}task.operation = operation+ var videoDrains []*drainOperation+ if task.kind == drainPublication {+ for siblingKey, sibling := range r.sfuDrainTasks.All() {+ if sibling.kind == drainVideoPublication && sibling.fence.ResourceFence == task.fence.ResourceFence {+ videoDrains = append(videoDrains, r.runSfuDrain(siblingKey, sibling))+ }+ }+ }r.inflight++go func() {var err error- if task.kind == drainVideoPublication {- err = task.closeVideo()+ if task.kind == drainVideoPublication || task.kind == drainVoice {+ err = task.closePhysical()} else if task.kind == drainPublication {err = fallback.media.ClosePublication(task.fence.ResourceFence)} else {err = fallback.media.CloseSubscription(task.fence, task.fence.ConnectionID)}+ for _, retired := range videoDrains {+ <-retired.done+ err = errors.Join(err, retired.err)+ }r.mu.Lock()defer r.mu.Unlock()r.inflight--@@ -338,7 +369,16 @@ func (r *router) settleSfuDrain(key sfu.SubscriptionFence, task *sfuDrainTask, efallback.admission.CompleteSubscriptionDrain(task.fence)}r.sfuDrainTasks.Delete(key)- r.wakeResourceWaiters()+ if task.kind != drainVoice {+ r.wakeResourceWaiters()+ if task.kind == drainVideoPublication && !r.closing {+ if rm, _ := r.rooms.Get(task.fence.RoomID); rm != nil && rm.controller != nil {+ r.prepareSfuVideoAttachment(task.fence.RoomID, rm.controller.Operation())+ }+ }+ } else if !r.closing {+ r.hooks.routesChanged(task.fence.RoomID)+ }return nil}}diff --git a/internal/server/signal/router_sfu_room_media_test.go b/internal/server/signal/router_sfu_room_media_test.goindex fdb9a0f9..75509d43 100644--- a/internal/server/signal/router_sfu_room_media_test.go+++ b/internal/server/signal/router_sfu_room_media_test.go@@ -216,3 +216,280 @@ func TestSfuLogicalSubscriptionDrainClosesPendingPhysicalVideo(t *testing.T) {t.Fatal("logical drain did not cancel physical video preparation")}}++type heldVideoRetirement struct {+ *sfu.Media+ started chan struct{}+ release chan struct{}+ oldClose func() error+}++type heldPublicationCreation struct {+ *sfu.Media+ oldConnection string+ entered, release, accepting, finish chan struct{}+}++func (m *heldPublicationCreation) PreparePublication(fence sfu.ResourceFence, connection string, metadata sfu.PublicationMedia) error {+ if connection == m.oldConnection {+ close(m.entered)+ <-m.release+ }+ return m.Media.PreparePublication(fence, connection, metadata)+}++func (m *heldPublicationCreation) AcceptPublisherOffer(_ sfu.ResourceFence, connection string, _ webrtc.SessionDescription) (webrtc.SessionDescription, error) {+ if connection == m.oldConnection {+ close(m.accepting)+ <-m.finish+ }+ return webrtc.SessionDescription{Type: webrtc.SDPTypeAnswer, SDP: "answer"}, nil+}++func (*heldPublicationCreation) PrepareSubscriber(context.Context, sfu.SubscriptionFence, string) (webrtc.SessionDescription, error) {+ return webrtc.SessionDescription{Type: webrtc.SDPTypeOffer, SDP: "offer"}, nil+}++func TestSfuPublicationCreationCannotOccupyReplacementRoomShare(t *testing.T) {+ h := newRouterHarness(t, routerHarnessOptions{capacity: 1, withSfu: true})+ created := h.createRoom()+ host, first, second := h.establishSfuRoom(created)+ h.settle()+ media := &heldPublicationCreation{Media: sfu.NewMedia(sfu.MediaOptions{}), entered: make(chan struct{}), release: make(chan struct{}), accepting: make(chan struct{}), finish: make(chan struct{})}+ defer media.Close()+ finished := false+ released := false+ defer func() {+ if !finished {+ close(media.finish)+ }+ if !released {+ close(media.release)+ }+ }()+ var message protocol.SfuSignalMessage+ var fence sfu.ResourceFence+ h.locked(func() {+ rm, _ := h.router.rooms.Get(created.RoomID)+ rm.roomMedia, rm.mediaGeneration = true, "room_media_12345678"+ rm.controller.SetVideoPublication(testShareGeneration, false, h.router.now())+ rm.controller.SetVoiceListeners([]string{host.peerID, first.peerID, second.peerID}, h.router.now())+ publication := rm.controller.Publication()+ fence = resourceFence(publication.Resource)+ media.oldConnection = publication.ConnectionID+ h.router.sfu.media = media+ h.sent = map[string][]protocol.ServerMessage{}+ message = protocol.SfuSignalMessage{Type: "sfu-signal", Kind: "description", Revision: protocol.Int(rm.controller.Revision()),+ PublicationGeneration: publication.Generation, ConnectionID: publication.ConnectionID,+ Description: &protocol.SessionDescription{Type: "offer", SDP: "offer"},+ Media: &protocol.SfuMedia{Codec: "vp8", Layers: []protocol.SfuLayer{{Width: 640, Height: 360, Bitrate: 500_000}}}}+ })+ done := make(chan struct{})+ go func() { h.locked(func() { h.router.handleSfuSignal(host, message) }); close(done) }()+ select {+ case <-media.entered:+ case <-time.After(defaultWait):+ t.Fatal("old publication creation did not start")+ }+ stopping, stopped := make(chan struct{}), make(chan struct{})+ go func() {+ close(stopping)+ h.locked(func() {+ h.router.setVideoPublication(created.RoomID, "", false)+ h.router.setVideoPublication(created.RoomID, "video_latest_12345678", false)+ })+ close(stopped)+ }()+ <-stopping+ // Give the replacement the opportunity to overtake the held constructor.+ // Its observable result, rather than lock availability, is asserted below.+ select {+ case <-stopped:+ case <-time.After(50 * time.Millisecond):+ }+ close(media.release)+ released = true+ select {+ case <-media.accepting:+ case <-time.After(defaultWait):+ t.Fatal("old SDP did not start")+ }+ select {+ case <-stopped:+ case <-time.After(defaultWait):+ t.Fatal("room share did not replace")+ }+ h.settle()+ var latest protocol.SfuConfigMessage+ h.locked(func() {+ rm, _ := h.router.rooms.Get(created.RoomID)+ if resourceFence(rm.controller.Publication().Resource) != fence {+ t.Fatal("voice room reservation changed across picture replacement")+ }+ for _, sent := range h.sent[host.sessionID] {+ if config, ok := sent.(protocol.SfuConfigMessage); ok {+ latest = config+ }+ }+ if latest.ConnectionID == "" || latest.ConnectionID == message.ConnectionID {+ t.Fatal("latest share was not configured")+ }+ next := message+ next.ConnectionID, next.Revision = latest.ConnectionID, latest.Revision+ h.router.handleSfuSignal(host, next)+ if !media.HasPublication(fence, latest.ConnectionID) {+ t.Fatal("late old creation occupied the current share's room reservation")+ }+ answered := false+ for _, sent := range h.sent[host.sessionID] {+ if reply, ok := sent.(protocol.SfuSignalMessage); ok && reply.ConnectionID == latest.ConnectionID && reply.Description != nil {+ answered = true+ }+ }+ if !answered {+ t.Fatal("latest share offer failed behind the old occupant")+ }+ })+ // The old SDP completion must not retire the newly accepted physical owner.+ close(media.finish)+ finished = true+ select {+ case <-done:+ case <-time.After(defaultWait):+ t.Fatal("old SDP completion did not settle")+ }+ h.settle()+ if !media.HasPublication(fence, latest.ConnectionID) {+ t.Fatal("old completion retired the current share")+ }+}++func (m *heldVideoRetirement) RetirePublication(fence sfu.ResourceFence, connection string) func() error {+ work := m.Media.RetirePublication(fence, connection)+ if work == nil {+ return nil+ }+ m.oldClose = work+ return func() error {+ close(m.started)+ <-m.release+ return work()+ }+}++func TestRoomSfuVideoReplacementWaitsForPhysicalRetirement(t *testing.T) {+ for _, intent := range []string{"newest-share", "stopped", "abandoned"} {+ t.Run(intent, func(t *testing.T) {+ stop := intent != "newest-share"+ h := newRouterHarness(t, routerHarnessOptions{capacity: 1, withSfu: true})+ created := h.createRoom()+ host, first, second := h.establishSfuRoom(created)+ h.settle()+ media := &heldVideoRetirement{Media: sfu.NewMedia(sfu.MediaOptions{}), started: make(chan struct{}), release: make(chan struct{})}+ defer media.Close()+ released := false+ defer func() {+ if !released {+ close(media.release)+ }+ }()+ metadata := sfu.PublicationMedia{Codec: "vp8", RIDs: []string{""}, Formats: []forwarding.LayerFormat{{Width: 640, Height: 360, Bitrate: 500_000}}}+ var fence sfu.ResourceFence+ var latestID string+ var fullDrain *drainOperation+ h.locked(func() {+ rm, _ := h.router.rooms.Get(created.RoomID)+ rm.roomMedia, rm.mediaGeneration = true, "room_media_12345678"+ rm.controller.SetVideoPublication(testShareGeneration, false, h.router.now())+ rm.controller.SetVoiceListeners([]string{host.peerID, first.peerID, second.peerID}, h.router.now())+ publication := rm.controller.Publication()+ fence = resourceFence(publication.Resource)+ if err := media.PreparePublication(fence, publication.ConnectionID, metadata); err != nil {+ t.Fatal(err)+ }+ h.router.sfu.media = media+ h.sent = map[string][]protocol.ServerMessage{}+ h.router.setVideoPublication(created.RoomID, "", false)+ h.router.setVideoPublication(created.RoomID, "video_first_12345678", false)+ })+ select {+ case <-media.started:+ case <-time.After(defaultWait):+ t.Fatal("physical retirement did not start")+ }+ h.locked(func() {+ rm, _ := h.router.rooms.Get(created.RoomID)+ if stop {+ h.router.setVideoPublication(created.RoomID, "", false)+ } else {+ h.router.setVideoPublication(created.RoomID, "video_latest_12345678", false)+ latestID = rm.controller.Publication().ConnectionID+ h.router.sendFreshSfuConfig(host)+ }+ for _, messages := range h.sent {+ for _, message := range messages {+ if _, ok := message.(protocol.SfuConfigMessage); ok {+ t.Fatal("new picture allocated before old physical join")+ }+ }+ }+ if !h.admission.HasPublication(fence) || rm.controller.Publication() == nil {+ t.Fatal("picture retirement discarded voice admission")+ }+ if intent == "abandoned" {+ h.router.deleteRoom(created.RoomID)+ task, _ := h.router.sfuDrainTasks.Get(sfu.SubscriptionFence{ResourceFence: fence})+ fullDrain = task.operation+ }+ })+ if fullDrain != nil {+ select {+ case <-fullDrain.done:+ t.Fatal("room drain released admission before retired picture joined")+ case <-time.After(50 * time.Millisecond):+ }+ }+ close(media.release)+ released = true+ h.settle()+ h.locked(func() {+ if intent == "abandoned" {+ if h.admission.Usage().Ingress != 0 || h.router.sfuDrainTasks.Len() != 0 {+ t.Fatal("room admission did not release after every physical join")+ }+ return+ }+ rm, _ := h.router.rooms.Get(created.RoomID)+ configs := 0+ for _, message := range h.sent[host.sessionID] {+ if config, ok := message.(protocol.SfuConfigMessage); ok {+ configs+++ if stop || config.ConnectionID != latestID {+ t.Fatal("retirement resumed superseded picture intent")+ }+ }+ }+ if !stop && configs == 0 {+ t.Fatal("latest picture did not resume after physical join")+ }+ if stop && configs != 0 {+ t.Fatal("retirement resurrected stopped picture")+ }+ if rm.controller.Publication() == nil || !h.admission.HasPublication(fence) {+ t.Fatal("voice admission did not survive picture handoff")+ }+ if !stop {+ if err := media.PreparePublication(fence, latestID, metadata); err != nil {+ t.Fatal(err)+ }+ if err := media.oldClose(); err != nil {+ t.Fatal(err)+ }+ if !media.HasPublication(fence, latestID) {+ t.Fatal("late old close removed current picture")+ }+ }+ })+ })+ }+}diff --git a/internal/server/signal/router_sfu_signal.go b/internal/server/signal/router_sfu_signal.goindex eaece889..189f208c 100644--- a/internal/server/signal/router_sfu_signal.go+++ b/internal/server/signal/router_sfu_signal.go@@ -3,6 +3,7 @@ package signalimport ("context""encoding/json"+ "errors""time""github.com/TNTcraftHIM/Piik/internal/diagnostics"@@ -11,6 +12,7 @@ import ("github.com/TNTcraftHIM/Piik/internal/server/route""github.com/TNTcraftHIM/Piik/internal/server/sfu""github.com/pion/webrtc/v4"+ "github.com/pion/webrtc/v4/pkg/rtcerr")func (r *router) sfuSignalAuthorized(participant authenticatedRouteParticipant, message protocol.SfuSignalMessage) (sfu.SubscriptionFence, bool) {@@ -43,6 +45,9 @@ func (r *router) sfuSignalAuthorized(participant authenticatedRouteParticipant,if int64(message.Revision) > latestRevision {return fence, false}+ if r.sfuVideoRetiring(fence.ResourceFence) {+ return fence, false+ }if participant.role == protocol.RoleHost {publication := snapshot.HostPublicationif publication != nil && publication.PhysicalActive && publication.Generation == message.PublicationGeneration &&@@ -97,12 +102,25 @@ func (r *router) handleSfuSignal(participant authenticatedRouteParticipant, messreturn}media := r.sfu.media- // Each session's input loop serializes SDP/ICE. Preparation itself is bounded+ // Each session's input loop serializes SDP/ICE. SDP negotiation is bounded// by the route operation and runs outside the authority mutex.var description webrtc.SessionDescriptionvar err errornewPublication := false+ // Register the physical owner before releasing authority. Retirement must+ // see this handle even when the SDP effect has not started yet.+ if message.Kind == "description" && participant.role == protocol.RoleHost &&+ message.Description.Type == "offer" && !media.HasPublication(fence.ResourceFence, message.ConnectionID) {+ if message.Media == nil {+ return+ }+ err = media.PreparePublication(fence.ResourceFence, message.ConnectionID, sfuPublicationMedia(message.Media))+ newPublication = err == nil+ }r.io(func() {+ if err != nil {+ return+ }switch message.Kind {case "media":if participant.role == protocol.RoleHost {@@ -114,17 +132,7 @@ func (r *router) handleSfuSignal(participant authenticatedRouteParticipant, messif incoming.Type != webrtc.SDPTypeOffer {return}- if !media.HasPublication(fence.ResourceFence, message.ConnectionID) {- if message.Media == nil {- return- }- metadata := sfuPublicationMedia(message.Media)- err = media.PreparePublication(fence.ResourceFence, message.ConnectionID, metadata)- newPublication = err == nil- }- if err == nil {- description, err = media.AcceptPublisherOffer(fence.ResourceFence, message.ConnectionID, incoming)- }+ description, err = media.AcceptPublisherOffer(fence.ResourceFence, message.ConnectionID, incoming)} else if incoming.Type == webrtc.SDPTypeAnswer {err = media.ApplySubscriberAnswer(fence, message.ConnectionID, incoming)}@@ -155,6 +163,10 @@ func (r *router) handleSfuSignal(participant authenticatedRouteParticipant, messr.debug(participant.roomID, "sfu-signal-failed","participant", r.debugPeer(participant.roomID, participant.peerID),"role", participant.role, "kind", message.Kind, diagnostics.Error(err))+ var candidateError *rtcerr.OperationError+ if message.Kind == "candidate" && errors.As(err, &candidateError) {+ return+ }r.failSfuConnection(participant, message)return}diff --git a/internal/server/signal/router_sfu_signal_test.go b/internal/server/signal/router_sfu_signal_test.goindex e5e92f4b..7d3498db 100644--- a/internal/server/signal/router_sfu_signal_test.go+++ b/internal/server/signal/router_sfu_signal_test.go@@ -4,6 +4,7 @@ import ("context""encoding/json""net"+ "strings""sync/atomic""testing""time"@@ -231,6 +232,35 @@ func TestEmbeddedSfuAuthenticatedWebSocketCarriesActualRTP(t *testing.T) {if !waitFor(t, 3*time.Second, func() bool { return received.Load() >= 3 }) {t.Fatal("authenticated signaling did not produce forwarded video RTP")}+ for _, peer := range []struct {+ client *testClient+ pc *webrtc.PeerConnection+ config protocol.SfuConfigMessage+ }{{host, publisher, hostConfig}, {viewer, subscriber, viewerConfig}} {+ valid := ""+ for _, line := range strings.Split(peer.pc.LocalDescription().SDP, "\n") {+ if strings.HasPrefix(line, "a=candidate:") {+ valid = strings.TrimSpace(strings.TrimPrefix(line, "a:"))+ break+ }+ }+ if valid == "" {+ t.Fatal("positive candidate control is missing")+ }+ for _, candidate := range []string{"candidate:malformed", valid} {+ encoded, err := json.Marshal(protocol.SfuSignalMessage{Type: "sfu-signal", Kind: "candidate",+ Revision: peer.config.Revision, PublicationGeneration: peer.config.PublicationGeneration,+ ConnectionID: peer.config.ConnectionID, Candidate: &protocol.IceCandidate{Candidate: candidate}})+ check(err)+ peer.client.sendText(string(encoded))+ }+ }+ if !waitFor(t, time.Second, func() bool {+ return diagnostics.calledWith(h.room.RoomID, "sfu-signal-failed", map[string]any{"role": "host", "kind": "candidate"}) &&+ diagnostics.calledWith(h.room.RoomID, "sfu-signal-failed", map[string]any{"role": "viewer", "kind": "candidate"})+ }) {+ t.Fatal("candidate rejection was not processed on both physical facets")+ }if !waitFor(t, time.Second, func() bool {return diagnostics.calledWith(h.room.RoomID, "sfu-transport-state", map[string]any{"role": "host", "phase": "prepare", "revision": int64(hostConfig.Revision), "state": "connected",diff --git a/internal/server/signal/server.go b/internal/server/signal/server.goindex f3482924..4cb12fc4 100644--- a/internal/server/signal/server.go+++ b/internal/server/signal/server.go@@ -1087,6 +1087,14 @@ func (s *Server) settleHostShare(roomID, sessionID string, request authRequest)if shareGeneration == "" {shareGeneration = currentGeneration}+ // Persistent room sessions may reconnect after a stop overtook their+ // authentication request. The retained generation fences that publication;+ // only a fresh generation may start another one. Legacy sessions keep their+ // established authenticate-to-publish lifecycle.+ if request.roomSession && currentGeneration != "" &&+ shareGeneration == currentGeneration && current.routePolicy == nil {+ return ""+ }if shareGeneration == "" {shareGeneration = sessionID}@@ -1665,7 +1673,9 @@ func (s *Server) handleSfuPublisherQualityEvidence(sess *session, source *authen}func (s *Server) consumeSenderQualityBudget(sessionID string, nowMs int64) bool {- maximumReports := min(s.endpointMediaCopyCapacity+1, 3) * 2+ // Volunteers may send to the whole room. Preserve one serial preparation+ // and the existing double allowance without mirroring route ownership here.+ maximumReports := (protocol.MaxViewersPerRoomLimit + 1) * 2current := s.senderQualityRateBySession[sessionID]if current == nil || nowMs-current.startedAtMs >= protocol.ViewerQualityEvidenceIntervalMs {s.senderQualityRateBySession[sessionID] = &senderQualityRateWindow{startedAtMs: nowMs, count: 1}diff --git a/internal/server/signal/server_test.go b/internal/server/signal/server_test.goindex ab3efd05..956fd0b6 100644--- a/internal/server/signal/server_test.go+++ b/internal/server/signal/server_test.go@@ -45,6 +45,31 @@ const (defaultWait = 2 * time.Second)+func TestSenderQualityBudgetAdmitsRoomProducerCadence(t *testing.T) {+ s := &Server{endpointMediaCopyCapacity: 2, senderQualityRateBySession: map[string]*senderQualityRateWindow{}}+ // Browser and Native producers report each physical sender every two+ // seconds. A room may retain all Viewer senders plus one serial preparation.+ const senders = protocol.MaxViewersPerRoomLimit + 1+ for window := range 4 {+ now := int64(window * protocol.ViewerQualityEvidenceIntervalMs)+ for range senders {+ if !s.consumeSenderQualityBudget("producer-session", now) {+ t.Fatal("legal per-connection producer burst was rate limited")+ }+ }+ // Keep the existing double allowance for boundary timing, then reject+ // further traffic until the next observation window.+ for range senders {+ if !s.consumeSenderQualityBudget("producer-session", now) {+ t.Fatal("producer boundary allowance was lost")+ }+ }+ if s.consumeSenderQualityBudget("producer-session", now) {+ t.Fatal("sender evidence above the room budget was accepted")+ }+ }+}+func TestCreateRoomReleasesLockWhenDerivationOrCommitPanics(t *testing.T) {for _, password := range []string{"", "room-password"} {t.Run(password, func(t *testing.T) {@@ -306,6 +331,67 @@ func TestPersistentRoomSessionReconnectStaysMediaIdle(t *testing.T) {h.closeClient(resumed)}+func TestStoppedPublicationReauthenticationPreservesRoomSessionFence(t *testing.T) {+ for _, persistent := range []bool{true, false} {+ t.Run(map[bool]string{true: "room-session", false: "legacy"}[persistent], func(t *testing.T) {+ h := startHarness(t, harnessOptions{})+ host := openClient(t, h)+ const clientID, stoppedGeneration = "stopped-reconnect-host", "stopped_reconnect_generation"+ authenticate(t, host, h.room, protocol.RoleHost, clientID, 1, stoppedGeneration,+ presenceOptions{roomSession: persistent})+ viewer := openClient(t, h)+ authenticate(t, viewer, h.room, protocol.RoleViewer, "stopped-reconnect-viewer", 1, "", presenceOptions{})+ host.ignore("route-update")+ viewer.ignore("route-update")+ host.sendJSON(map[string]any{"type": "stop-sharing", "shareGeneration": stoppedGeneration})+ viewer.next("sharing-stopped")+ viewer.next("host-status")+ h.locked(func() {+ if h.server.activeHostShareGeneration(h.room.RoomID) != "" {+ t.Fatal("stopped publication remained active")+ }+ })+ // A delayed authenticate still carries the generation that was valid+ // when sent. It must not need a compensating client stop to stay idle.+ resumed := openClient(t, h)+ auth := authenticate(t, resumed, h.room, protocol.RoleHost, clientID, 1, stoppedGeneration,+ presenceOptions{roomSession: persistent})+ if !persistent {+ if auth.ShareGeneration == nil || *auth.ShareGeneration != stoppedGeneration {+ t.Fatal("legacy authenticate-to-publish behavior changed")+ }+ return+ }+ if auth.ShareGeneration != nil || auth.RouteAssignment.Upstream.Kind != "none" {+ t.Fatal("delayed authentication restored a stopped publication")+ }+ h.locked(func() {+ if h.server.activeHostShareGeneration(h.room.RoomID) != "" {+ t.Fatal("delayed authentication reacquired media authority")+ }+ if rm, _ := h.server.router.rooms.Get(h.room.RoomID); rm != nil && rm.controller != nil {+ t.Fatal("delayed authentication rebuilt source-less routes")+ }+ })+ const freshGeneration = "fresh_reconnect_generation"+ resumed.sendJSON(map[string]any{"type": "start-sharing", "shareGeneration": freshGeneration,+ "routePolicy": protocol.DefaultRoutePolicy})+ resumed.next("sharing-started")+ h.locked(func() {+ if h.server.activeHostShareGeneration(h.room.RoomID) != freshGeneration {+ t.Fatal("fresh publication could not start after idle reauthentication")+ }+ })+ active := openClient(t, h)+ activeAuth := authenticate(t, active, h.room, protocol.RoleHost, clientID, 1, freshGeneration,+ presenceOptions{roomSession: true})+ if activeAuth.ShareGeneration == nil || *activeAuth.ShareGeneration != freshGeneration {+ t.Fatal("active publication did not survive reauthentication")+ }+ })+ }+}+// ---------------------------------------------------------------------------// harness// ---------------------------------------------------------------------------diff --git a/internal/server/signal/voice.go b/internal/server/signal/voice.goindex aef403b3..f65de390 100644--- a/internal/server/signal/voice.go+++ b/internal/server/signal/voice.go@@ -42,6 +42,12 @@ type voiceEdge struct {send map[string][]stringconnected map[string]boolsfuPlan *sfu.VoiceConnectionPlan+ sfuWork *voiceNegotiation+}++type voiceNegotiation struct {+ run func() error+ done chan struct{}}func (s *Server) voiceRoom(roomID string) *roomVoice {diff --git a/internal/server/signal/voice_owner_test.go b/internal/server/signal/voice_owner_test.goindex 4fba5148..58fc025d 100644--- a/internal/server/signal/voice_owner_test.go+++ b/internal/server/signal/voice_owner_test.go@@ -1,9 +1,15 @@package signalimport (+ "context"+ "errors"+ "sync""testing"+ "time""github.com/TNTcraftHIM/Piik/internal/server/protocol"+ "github.com/TNTcraftHIM/Piik/internal/server/sfu"+ "github.com/TNTcraftHIM/Piik/internal/server/sfu/sfutest")func TestVoiceFailureReplacesOnlyCompanionOfHealthyPicture(t *testing.T) {@@ -261,3 +267,209 @@ func TestVoiceOwnersStopDropsVideoOnlyCandidateDemand(t *testing.T) {t.Fatal("video-only viewer retained voice candidate demand after share stop")}}++type heldVoiceMedia struct {+ *sfutest.FakeMedia+ negotiations chan sfu.VoiceConnectionPlan+ negotiateRelease, latestRelease chan struct{}+ closeStarted, closeRelease chan struct{}+ closeOnce sync.Once+}++func (m *heldVoiceMedia) ConfigureVoice(plan sfu.VoiceConnectionPlan, _ func(sfu.VoiceMediaEvent)) (func() error, error) {+ return func() error {+ m.negotiations <- plan+ if len(plan.Publish) == 0 {+ <-m.negotiateRelease+ return errors.New("held negotiation failed")+ }+ <-m.latestRelease+ return nil+ }, nil+}+func (*heldVoiceMedia) SignalVoice(string, string, string, string, sfu.VoiceSignal) error { return nil }+func (m *heldVoiceMedia) CloseVoice(string, string, string, string) func() error {+ return func() error { m.closeOnce.Do(func() { close(m.closeStarted) }); <-m.closeRelease; return nil }+}+func TestVoiceEffectsPreserveSynchronousAuthorityAndShutdownJoinsWork(t *testing.T) {+ media := &heldVoiceMedia{FakeMedia: sfutest.New(), negotiations: make(chan sfu.VoiceConnectionPlan, 8), negotiateRelease: make(chan struct{}), latestRelease: make(chan struct{}), closeStarted: make(chan struct{}), closeRelease: make(chan struct{})}+ var negotiateOnce, latestOnce, closeOnce sync.Once+ releaseNegotiation := func() { negotiateOnce.Do(func() { close(media.negotiateRelease) }) }+ releaseLatest := func() { latestOnce.Do(func() { close(media.latestRelease) }) }+ releaseClose := func() { closeOnce.Do(func() { close(media.closeRelease) }) }+ t.Cleanup(releaseNegotiation)+ t.Cleanup(releaseLatest)+ t.Cleanup(releaseClose)+ h := startHarness(t, harnessOptions{sfu: &SfuFallback{Media: media, Admission: sfu.NewAdmission(sfu.AdmissionOptions{IngressCapacity: 2, EgressCapacity: 20})}})+ edge := &voiceEdge{id: "physical", logicalID: "logical", child: "peer", childSession: "session", transport: "sfu", send: map[string][]string{}}+ var v *roomVoice+ h.locked(func() { v = h.server.voiceRoom(h.room.RoomID); v.connections[edge.logicalID] = edge })+ done := make(chan struct{})+ go func() {+ h.server.mu.Lock()+ defer h.server.mu.Unlock()+ h.server.configureVoiceJunction(h.room.RoomID, v, edge, nil)+ close(done)+ }()+ select {+ case <-done:+ case <-time.After(defaultWait):+ t.Fatal("synchronous voice projection waited for SDP work")+ }+ select {+ case <-media.negotiations:+ case <-time.After(defaultWait):+ t.Fatal("negotiation did not start")+ }+ created := make(chan error, 1)+ go func() { _, err := h.server.CreateRoom(protocol.CodeEntryOpen, nil, ""); created <- err }()+ select {+ case err := <-created:+ if err != nil {+ t.Fatal(err)+ }+ case <-time.After(defaultWait):+ t.Fatal("voice negotiation blocked another room")+ }+ var replacementPlan *sfu.VoiceConnectionPlan+ h.locked(func() {+ operation := edge.sfuWork+ for _, id := range []string{"superseded", "latest"} {+ edge.send[edge.child] = []string{id}+ h.server.configureVoiceJunction(h.room.RoomID, v, edge, []protocol.VoicePublication{{ID: id, PeerID: edge.child}})+ if edge.sfuWork != operation {+ t.Error("plan update started an overlapping negotiation worker")+ }+ }+ replacementPlan = edge.sfuPlan+ })+ releaseNegotiation()+ select {+ case plan := <-media.negotiations:+ if len(plan.Publish) != 1 || plan.Publish[0].ID != "latest" {+ t.Fatalf("queued work replayed a superseded publication: %+v", plan.Publish)+ }+ case <-time.After(defaultWait):+ t.Fatal("latest negotiation did not start")+ }+ h.locked(func() {+ if edge.sfuPlan != replacementPlan {+ t.Error("stale negotiation failure overwrote replacement plan")+ }+ })+ h.locked(func() { h.server.closeRoomVoice(h.room.RoomID) })+ select {+ case <-media.closeStarted:+ case <-time.After(defaultWait):+ t.Fatal("physical retirement did not start")+ }+ go func() { _, err := h.server.CreateRoom(protocol.CodeEntryOpen, nil, ""); created <- err }()+ select {+ case err := <-created:+ if err != nil {+ t.Fatal(err)+ }+ case <-time.After(defaultWait):+ t.Fatal("voice retirement blocked another room")+ }+ closed := make(chan error, 1)+ go func() {+ ctx, cancel := context.WithTimeout(context.Background(), defaultWait)+ defer cancel()+ closed <- h.server.Close(ctx)+ }()+ select {+ case err := <-closed:+ t.Fatalf("shutdown escaped held retirement: %v", err)+ case <-time.After(30 * time.Millisecond):+ }+ releaseClose()+ select {+ case err := <-closed:+ t.Fatalf("shutdown escaped in-flight negotiation: %v", err)+ case <-time.After(30 * time.Millisecond):+ }+ releaseLatest()+ select {+ case err := <-closed:+ if err != nil {+ t.Fatal(err)+ }+ case <-time.After(defaultWait):+ t.Fatal("shutdown did not join retirement")+ }+}++type capacityHeldVoiceMedia struct {+ *sfutest.FakeMedia+ mu sync.Mutex+ busy bool+ plans chan sfu.VoiceConnectionPlan+ release chan struct{}+}++func (m *capacityHeldVoiceMedia) ConfigureVoice(plan sfu.VoiceConnectionPlan, _ func(sfu.VoiceMediaEvent)) (func() error, error) {+ m.mu.Lock()+ defer m.mu.Unlock()+ if m.busy {+ return nil, errors.New("voice connection capacity is exhausted")+ }+ m.plans <- plan+ return nil, nil+}+func (*capacityHeldVoiceMedia) SignalVoice(string, string, string, string, sfu.VoiceSignal) error {+ return nil+}+func (m *capacityHeldVoiceMedia) CloseVoice(string, string, string, string) func() error {+ return func() error { <-m.release; m.mu.Lock(); m.busy = false; m.mu.Unlock(); return nil }+}+func TestVoiceRetirementRefreshesCurrentPlanAfterCapacityRelease(t *testing.T) {+ media := &capacityHeldVoiceMedia{FakeMedia: sfutest.New(), busy: true, plans: make(chan sfu.VoiceConnectionPlan, 32), release: make(chan struct{})}+ var releaseOnce sync.Once+ unblock := func() { releaseOnce.Do(func() { close(media.release) }) }+ t.Cleanup(unblock)+ h := startHarness(t, harnessOptions{sfuOnly: true, sfu: &SfuFallback{Media: media, Admission: sfu.NewAdmission(sfu.AdmissionOptions{IngressCapacity: 2, EgressCapacity: 20})}})+ host, viewer := openClient(t, h), openClient(t, h)+ authenticate(t, host, h.room, protocol.RoleHost, "capacity-host", -1, "", presenceOptions{roomSession: true, roomOnly: true})+ viewerID := authenticate(t, viewer, h.room, protocol.RoleViewer, "capacity-viewer", 0, "", presenceOptions{})+ for _, client := range []*testClient{host, viewer} {+ client.sendJSON(map[string]any{"type": "subscribe-room-voice"})+ nextVoiceState(t, client, nil)+ }+ voiceCommand(host, map[string]any{"action": "join"})+ nextVoiceState(t, host, func(state protocol.RoomVoiceStateMessage) bool { return state.Enabled })+ voiceCommand(viewer, map[string]any{"action": "join"})+ nextVoiceState(t, viewer, func(state protocol.RoomVoiceStateMessage) bool { return len(state.Connections) > 0 })+ // Keep the actual controller's current facets. Capacity is held by an older+ // retiring facet, and publication intent changes before its close completes.+ h.locked(func() {+ v := h.server.voices[h.room.RoomID]+ for _, edge := range v.connections {+ edge.pending = false+ edge.connected[edge.parent] = true+ edge.connected[edge.child] = true+ }+ h.server.commitVoiceCandidate(h.room.RoomID)+ h.server.syncRoomVoice(h.room.RoomID)+ h.server.closeVoiceEdge(h.room.RoomID, &voiceEdge{id: "retiring-capacity", child: "old-peer", childSession: "old-session", transport: "sfu"})+ })+ voiceCommand(viewer, map[string]any{"action": "publish"})+ state := nextVoiceState(t, viewer, func(state protocol.RoomVoiceStateMessage) bool { return len(state.Publications) == 1 })+ expected := state.Publications[0].ID+ unblock()+ deadline := time.After(defaultWait)+ for {+ select {+ case plan := <-media.plans:+ if plan.PeerID == viewerID.PeerID {+ for _, pub := range plan.Publish {+ if pub.ID == expected {+ return+ }+ }+ }+ case <-deadline:+ t.Fatal("completed retirement did not resume current publication intent")+ }+ }+}diff --git a/internal/server/signal/voice_plan.go b/internal/server/signal/voice_plan.goindex 882daca9..456fb486 100644--- a/internal/server/signal/voice_plan.go+++ b/internal/server/signal/voice_plan.go@@ -38,7 +38,7 @@ func (s *Server) syncRoomVoice(roomID string) {unavailable := s.projectVoiceFlows(roomID, v, pubs)for _, edge := range edges {if edge.transport == "sfu" {- s.configureVoiceJunction(roomID, edge, pubs)+ s.configureVoiceJunction(roomID, v, edge, pubs)}}members := make([]protocol.VoiceMember, 0, len(v.members))@@ -223,7 +223,7 @@ func (s *Server) voiceRuntime() sfu.VoiceRuntime {return runtime}-func (s *Server) configureVoiceJunction(roomID string, edge *voiceEdge, pubs []protocol.VoicePublication) {+func (s *Server) configureVoiceJunction(roomID string, v *roomVoice, edge *voiceEdge, pubs []protocol.VoicePublication) {runtime := s.voiceRuntime()if runtime == nil {return@@ -247,18 +247,78 @@ func (s *Server) configureVoiceJunction(roomID string, edge *voiceEdge, pubs []preturn}edge.sfuPlan = &plan- if err := runtime.ConfigureVoice(plan, s.onVoiceMediaEvent); err != nil {- edge.sfuPlan = nil- if sess, _ := s.voiceSession(roomID, edge.child); sess != nil {- s.voiceError(sess, "connection-unavailable")+ work, err := runtime.ConfigureVoice(plan, s.onVoiceMediaEvent)+ if err != nil {+ s.failVoicePlan(roomID, edge)+ return+ }+ if edge.sfuWork != nil {+ edge.sfuWork.run = work+ return+ }+ if work != nil {+ operation := &voiceNegotiation{run: work, done: make(chan struct{})}+ edge.sfuWork = operation+ s.router.inflight+++ go s.negotiateVoice(roomID, v, edge, operation)+ }+}++// The synchronous graph hook only commits intent. One worker per physical edge+// coalesces changes into the latest plan without yielding its caller's lock.+func (s *Server) negotiateVoice(roomID string, v *roomVoice, edge *voiceEdge, operation *voiceNegotiation) {+ s.mu.Lock()+ defer s.mu.Unlock()+ defer func() {+ edge.sfuWork = nil+ s.router.inflight--+ close(operation.done)+ }()+ for !s.closing && s.voices[roomID] == v && v.connections[edge.logicalID] == edge && edge.sfuPlan != nil && operation.run != nil {+ plan, run := edge.sfuPlan, operation.run+ var err error+ s.router.io(func() { err = run() })+ if s.voices[roomID] != v || v.connections[edge.logicalID] != edge {+ return+ }+ if edge.sfuPlan != plan {+ continue+ }+ if err != nil {+ s.failVoicePlan(roomID, edge)}+ return+ }+}++func (s *Server) failVoicePlan(roomID string, edge *voiceEdge) {+ edge.sfuPlan = nil+ if sess, _ := s.voiceSession(roomID, edge.child); sess != nil && sess.sessionID == edge.childSession {+ s.voiceError(sess, "connection-unavailable")}}func (s *Server) closeVoiceEdge(roomID string, edge *voiceEdge) {if edge.transport == "sfu" {if runtime := s.voiceRuntime(); runtime != nil {- _ = runtime.CloseVoice(roomID, edge.child, edge.childSession, edge.id)+ closePhysical := runtime.CloseVoice(roomID, edge.child, edge.childSession, edge.id)+ if operation := edge.sfuWork; operation != nil {+ closeTransport := closePhysical+ closePhysical = func() error {+ var err error+ if closeTransport != nil {+ err = closeTransport()+ }+ <-operation.done+ return err+ }+ }+ if closePhysical != nil {+ key := sfu.SubscriptionFence{ResourceFence: sfu.ResourceFence{RoomID: roomID, PublicationGeneration: edge.id}, ConnectionID: edge.id}+ task := &sfuDrainTask{kind: drainVoice, fence: key, closePhysical: closePhysical}+ s.router.sfuDrainTasks.Set(key, task)+ s.router.runSfuDrain(key, task)+ }}}}diff --git a/internal/server/signal/voice_signal.go b/internal/server/signal/voice_signal.goindex 31f9e71c..9c7dd2a6 100644--- a/internal/server/signal/voice_signal.go+++ b/internal/server/signal/voice_signal.go@@ -48,10 +48,11 @@ func (s *Server) handleRoomVoiceSignal(sess *session, auth *authenticatedSessionif err == nil {err = json.Unmarshal(encoded, &incoming)}+ plan := edge.sfuPlanif err == nil {- err = runtime.SignalVoice(auth.roomID, auth.peerID, sess.sessionID, edge.id, incoming)+ s.router.io(func() { err = runtime.SignalVoice(auth.roomID, auth.peerID, sess.sessionID, edge.id, incoming) })}- if err != nil {+ if err != nil && s.voiceConnection(auth.roomID, auth.peerID, sess.sessionID, edge.id) == edge && edge.sfuPlan == plan {s.voiceError(sess, "connection-unavailable")}return@@ -117,6 +118,9 @@ func (s *Server) onVoiceMediaEvent(event sfu.VoiceMediaEvent) {}}if event.State == webrtc.PeerConnectionStateConnected {+ if event.IsCurrentState != nil && !event.IsCurrentState() {+ return+ }edge.connected[""] = trues.commitVoiceCandidate(event.RoomID)s.syncRoomVoice(event.RoomID)diff --git a/internal/server/signal/voice_test.go b/internal/server/signal/voice_test.goindex a7eb7456..969ab31e 100644--- a/internal/server/signal/voice_test.go+++ b/internal/server/signal/voice_test.go@@ -9,6 +9,7 @@ import ("github.com/TNTcraftHIM/Piik/internal/server/protocol""github.com/TNTcraftHIM/Piik/internal/server/sfu""github.com/TNTcraftHIM/Piik/internal/server/sfu/sfutest"+ "github.com/pion/webrtc/v4")func TestVoiceListeningPresenceIsOptedInAndDoesNotChangeMedia(t *testing.T) {@@ -108,6 +109,45 @@ func TestVoiceOnlySfuPreparesWithoutVideoPublication(t *testing.T) {if len(state.Connections) != 1 || state.Connections[0].Transport != "sfu" || !state.Connections[0].Pending {t.Fatalf("invalid SFU preparation: %+v", state.Connections)}+ for _, client := range []*testClient{host, viewer} {+ own := nextVoiceState(t, client, func(state protocol.RoomVoiceStateMessage) bool { return len(state.Connections) > 0 })+ client.sendJSON(map[string]any{"type": "room-voice-transport", "connectionId": own.Connections[0].ConnectionID, "state": "connected"})+ }+ var events []sfu.VoiceMediaEvent+ if !waitFor(t, defaultWait, func() bool {+ ready := true+ h.locked(func() {+ events = nil+ for _, edge := range h.server.voices[h.room.RoomID].connections {+ ready = ready && edge.connected[edge.child]+ events = append(events, sfu.VoiceMediaEvent{RoomID: h.room.RoomID, PeerID: edge.child,+ SessionID: edge.childSession, ConnectionID: edge.id, State: webrtc.PeerConnectionStateConnected})+ }+ })+ return ready && len(events) == 2+ }) {+ t.Fatal("clients did not report both voice transports")+ }+ // The transport may leave Connected while its callback waits for Server.mu.+ // One stale facet must not combine with the other facet to commit the route.+ events[0].IsCurrentState = func() bool { return false }+ h.server.onVoiceMediaEvent(events[0])+ events[1].IsCurrentState = func() bool { return true }+ h.server.onVoiceMediaEvent(events[1])+ h.locked(func() {+ rm, _ := h.server.router.rooms.Get(h.room.RoomID)+ if rm.controller.Operation() == nil {+ t.Fatal("stale Connected callback committed a voice route")+ }+ })+ events[0].IsCurrentState = func() bool { return true }+ h.server.onVoiceMediaEvent(events[0])+ h.locked(func() {+ rm, _ := h.server.router.rooms.Get(h.room.RoomID)+ if rm.controller.Operation() != nil {+ t.Fatal("current Connected callback did not commit voice route")+ }+ })}func voiceCommand(client *testClient, command map[string]any) {diff --git a/licenses/README.md b/licenses/README.mdindex 90fb79ca..7af36670 100644--- a/licenses/README.md+++ b/licenses/README.md@@ -29,8 +29,9 @@ and the license files of the modules actually compiled into that binary; theApp package also carries the pinned Cloudflared notices. Missing licensetexts or changed pinned contents stop packaging.-LiveKit media-core packages are referenced as an unmodified, version-pinned Go-module under Apache-2.0. Original Piik adapters remain MIT; adapted upstream+LiveKit media-core packages are version-pinned under Apache-2.0. The+[scoped dependency repairs](../internal/thirdparty/README.md) record the retained+estimator corrections and temporary allocator input. Original Piik adapters remain MIT; adapted upstreamprobe orchestration retains its Apache-2.0 attribution in the source. Redistribution retainsthe module's license and applicable notices through the same Go notice collector.Any later copied or modified upstream file must retain its attribution and markdiff --git a/native/capture/linux/main.c b/native/capture/linux/main.cindex ead441b7..11885bf8 100644--- a/native/capture/linux/main.c+++ b/native/capture/linux/main.c@@ -557,6 +557,13 @@ static gboolean configure_encoder(VideoOutput *output) {return TRUE;}+static gboolean prepare_encoder(VideoOutput *output) {+ if (configure_encoder(output)) return TRUE;+ output->failed = TRUE;+ output->enabled = FALSE;+ return output->layer != output->run->original_output;+}+static gboolean supported_h264_profile(guint8 profile, guint8 constraints, guint8 level) {// RFC 6184 Table 5: equivalent Constrained Baseline tools, as in the Go reader.if (!((profile == 0x42 && (constraints & 0x4f) == 0x40) ||@@ -1037,7 +1044,7 @@ static GstFlowReturn video_sample(GstAppSink *sink, gpointer data) {"{\"state\":\"active\",\"codec\":\"h264\",\"hardwareOnly\":true,""\"profileLevelId\":\"%s\",\"width\":%u,\"height\":%u,""\"fps\":%u,\"restoreToken\":%s,\"outputs\":%s,\"remoteInput\":%s}",- nal.profile_level_id, run->profile.width, run->profile.height,+ nal.profile_level_id, width, height,run->profile.frame_rate, token, profiles,run->portal != NULL && run->portal->remote_input ? "true" : "false");g_free(token);@@ -1101,6 +1108,14 @@ static int run_pipeline(CaptureRun *run, const char *starting_status) {fail_run(run, "native media output closed");goto stopped;}+ for (guint index = 0; index < run->output_count; ++index) {+ if (run->outputs[index].failed && !write_frame(6, 0, (guint8)index, 0, 0, 0, 0,+ (const guint8 *)"hardware encoder configuration failed",+ strlen("hardware encoder configuration failed"))) {+ fail_run(run, "native media output closed");+ goto stopped;+ }+ }GstStateChangeReturn state =gst_element_set_state(run->pipeline, GST_STATE_PLAYING);if (state == GST_STATE_CHANGE_FAILURE) {@@ -1268,19 +1283,18 @@ static int capture_video(int count, char **values) {char *name = g_strdup_printf("encoder%u", index);output->encoder = gst_bin_get_by_name(GST_BIN(pipeline), name);g_free(name);- if (output->encoder == NULL || !configure_encoder(output)) {- fprintf(stderr, "Piik capture unavailable: hardware encoder is incompatible\n");- write_frame(6, 0, (guint8)index, 0, 0, 0, 0,- (const guint8 *)"hardware encoder configuration failed",- strlen("hardware encoder configuration failed"));- goto cleanup;- }+ if (output->encoder == NULL) goto cleanup;output->branch = GST_ELEMENT(gst_object_get_parent(GST_OBJECT(output->encoder)));name = g_strdup_printf("gate%u", index);output->gate = gst_bin_get_by_name(GST_BIN(pipeline), name);g_free(name);if (output->branch == NULL || output->branch == pipeline || output->gate == NULL) goto cleanup;if (!isolate_output(output)) goto cleanup;+ if (!prepare_encoder(output)) {+ fprintf(stderr, "Piik capture unavailable: hardware encoder is incompatible\n");+ goto cleanup;+ }+ if (output->failed) continue;name = g_strdup_printf("output%u", index);GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), name);g_free(name);diff --git a/native/capture/linux/main.test.c b/native/capture/linux/main.test.cindex 683f49e9..ad496885 100644--- a/native/capture/linux/main.test.c+++ b/native/capture/linux/main.test.c@@ -59,6 +59,16 @@ static void check_encoder_admission_and_control(void) {g_object_get(encoder, "bitrate", &applied, NULL);g_assert_cmpuint(applied, ==, 1000000);g_assert_null(run.failure);+ // The same rejected codec setting retires an optional slot independently,+ // but cannot admit a run whose required original encoder failed.+ run.original_output = 1;+ run.outputs[0].profile.bit_rate = 50000001;+ run.outputs[0].enabled = TRUE;+ g_assert_true(prepare_encoder(&run.outputs[0]));+ g_assert_true(run.outputs[0].failed);+ g_assert_false(run.outputs[0].enabled);+ run.original_output = 0;+ g_assert_false(prepare_encoder(&run.outputs[0]));g_mutex_clear(&run.lock);gst_object_unref(encoder);}@@ -226,8 +236,8 @@ static void check_encoder_timestamp_segment(GstClockTime offset) {g_assert_no_error(error);GstElement *source = gst_bin_get_by_name(GST_BIN(pipeline), "source");GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "output");- CaptureRun run = {.active = TRUE, .last_input_timestamp = GST_CLOCK_TIME_NONE,- .profile = {.frame_rate = 30}};+ CaptureRun run = {.last_input_timestamp = GST_CLOCK_TIME_NONE,+ .profile = {.width = 1280, .height = 720, .frame_rate = 30}};g_mutex_init(&run.lock);VideoOutput output = {.run = &run, .enabled = TRUE,.activation_timestamp = GST_CLOCK_TIME_NONE, .key_timestamp = GST_CLOCK_TIME_NONE,@@ -269,7 +279,7 @@ static void check_encoder_timestamp_segment(GstClockTime offset) {char *bytes = NULL;gsize size = 0;g_assert_true(g_file_get_contents(frame_path, &bytes, &size, NULL));- g_assert_cmpuint(size, ==, 64 + sizeof(h264));+ g_assert_cmpuint(size, >, 96 + sizeof(h264));g_assert_cmpint(bytes[5], ==, 5);g_assert_cmpuint(read_be((guint8 *)bytes + 8, 8), ==, 3 * GST_SECOND / 100);g_assert_cmpint(bytes[37], ==, 2);@@ -278,6 +288,8 @@ static void check_encoder_timestamp_segment(GstClockTime offset) {g_assert_cmpuint(read_be((guint8 *)bytes + 56, 2), ==, 640);g_assert_cmpuint(read_be((guint8 *)bytes + 58, 2), ==, 480);g_assert_cmpmem(bytes + 64, sizeof(h264), h264, sizeof(h264));+ g_assert_cmpint(bytes[64 + sizeof(h264) + 5], ==, 3);+ g_assert_nonnull(strstr(bytes + 96 + sizeof(h264), "\"width\":640,\"height\":480"));g_assert_cmpuint(output.activation_timestamp, ==, 3 * GST_SECOND);g_assert_nonnull(request);GstClockTime pts, running_time;diff --git a/native/capture/windows/README.md b/native/capture/windows/README.mdindex 8115ce8c..6f13e9be 100644--- a/native/capture/windows/README.md+++ b/native/capture/windows/README.md@@ -78,9 +78,10 @@ not a second product MFT implementation.Shared local outputs use stock WebRTC source/encoder adaptation. H264 keepsnative NV12 surfaces, while WebRTC VP8 reads back into I420. The existing captureand output mailboxes retain source/generation ownership; adapted sizes and-timestamps cross the same media envelope. Hardware low-latency mode stays on;-the old fixed one-frame VBV override is removed so later bitrate increases are-not trapped by the original buffer limit.+timestamps cross the same media envelope. The encoder requests one temporal+layer where supported and low-latency mode where writable; these optional+properties do not replace the actual output and Auto cadence checks.+VBV sizing stays with the encoder, allowing bitrate increases within the active profile.The build caches the pinned WebRTC SDK and matching official MSVC linker/runtimelibraries under `build/encoder-pool`. A newer installed MSVC keeps its own linkerdiff --git a/native/capture/windows/h264_encoder.cpp b/native/capture/windows/h264_encoder.cppindex 1273180b..aad9e30d 100644--- a/native/capture/windows/h264_encoder.cpp+++ b/native/capture/windows/h264_encoder.cpp@@ -265,19 +265,6 @@ UINT32 ReadU32(ICodecAPI* codec, const GUID& key, const std::string& name) {return result;}-bool ReadBool(ICodecAPI* codec, const GUID& key, const std::string& name) {- VARIANT value;- VariantInit(&value);- Check(codec->GetValue(&key, &value), name + "-readback");- if (value.vt != VT_BOOL) {- VariantClear(&value);- Fail(name + "-readback-type", "codec property did not return VT_BOOL");- }- bool result = value.boolVal != VARIANT_FALSE;- VariantClear(&value);- return result;-}-void SetU32(ICodecAPI* codec, const GUID& key, UINT32 expected,const std::string& name) {RequireProperty(codec, key, name);@@ -298,26 +285,6 @@ void SetU32(ICodecAPI* codec, const GUID& key, UINT32 expected,}}-void SetBool(ICodecAPI* codec, const GUID& key, bool expected,- const std::string& name) {- RequireProperty(codec, key, name);- VARIANT value;- VariantInit(&value);- value.vt = VT_BOOL;- value.boolVal = expected ? VARIANT_TRUE : VARIANT_FALSE;- HRESULT set_result = codec->SetValue(&key, &value);- VariantClear(&value);- if (set_result != S_OK) {- if (FAILED(set_result)) {- Check(set_result, name + "-set");- }- Fail(name + "-readonly", "codec property rejected the requested value");- }- if (ReadBool(codec, key, name) != expected) {- Fail(name + "-weakened", "codec property readback changed the requested value");- }-}-void SetOptionalU32(ICodecAPI* codec, const GUID& key, UINT32 value) {if (codec->IsSupported(&key) != S_OK) return;VARIANT requested;@@ -328,12 +295,26 @@ void SetOptionalU32(ICodecAPI* codec, const GUID& key, UINT32 value) {VariantClear(&requested);}+void SetOptionalBool(ICodecAPI* codec, const GUID& key, bool value) {+ if (codec->IsModifiable(&key) != S_OK) return;+ VARIANT requested;+ VariantInit(&requested);+ requested.vt = VT_BOOL;+ requested.boolVal = value ? VARIANT_TRUE : VARIANT_FALSE;+ (void)codec->SetValue(&key, &requested);+ VariantClear(&requested);+}+void ConfigureCodec(ICodecAPI* codec,const VideoProfile& profile) {SetU32(codec, CODECAPI_AVEncCommonRateControlMode,eAVEncCommonRateControlMode_CBR, "codec-cbr");- SetBool(codec, CODECAPI_AVLowLatencyMode, true, "codec-low-latency");+ // Temporal layers and low-latency mode are optional hardware properties.+ // Explicitly request our one temporal layer where supported (some MFTs need+ // even this default set), without making optional readback an encode gate.+ SetOptionalU32(codec, CODECAPI_AVEncVideoTemporalLayerCount, 1);+ SetOptionalBool(codec, CODECAPI_AVLowLatencyMode, true);SetU32(codec, CODECAPI_AVEncCommonMeanBitRate, profile.bit_rate,"codec-mean-bitrate");SetU32(codec, CODECAPI_AVEncMPVGOPSize, profile.gop_frames(), "codec-gop");@@ -442,7 +423,6 @@ void ValidateCodecReadback(const VideoProfile& profile) {if (ReadU32(codec, CODECAPI_AVEncCommonRateControlMode, "codec-cbr") !=eAVEncCommonRateControlMode_CBR ||- !ReadBool(codec, CODECAPI_AVLowLatencyMode, "codec-low-latency") ||ReadU32(codec, CODECAPI_AVEncCommonMeanBitRate,"codec-mean-bitrate") != profile.bit_rate ||ReadU32(codec, CODECAPI_AVEncMPVGOPSize, "codec-gop") !=diff --git a/native/capture/windows/main.cpp b/native/capture/windows/main.cppindex 16ee25c5..a98f765a 100644--- a/native/capture/windows/main.cpp+++ b/native/capture/windows/main.cpp@@ -53,6 +53,7 @@#include <atomic>#include <chrono>#include <cmath>+#include <condition_variable>#include <cstdint>#include <cwctype>#include <exception>@@ -1207,7 +1208,68 @@ UINT32 WindowsBuild() {return version.dwBuildNumber;}-void WriteCapabilityProbe() {+// Optional WASAPI activation must not consume the helper's entire discovery+// budget. These three workers belong to this one-shot probe process. Each owns+// its COM apartment and shared results even if a driver returns after Snapshot.+class AudioCapabilityProbe final {+ public:+ using Clock = std::chrono::steady_clock;+ struct Capabilities final {+ bool process = false;+ bool system = false;+ bool exclusion = false;+ };+ explicit AudioCapabilityProbe(+ Clock::time_point deadline = Clock::now() + std::chrono::seconds(1),+ std::function<bool(bool)> process = piik::capture::ProcessAudioAvailable,+ std::function<bool()> system = piik::capture::SystemAudioAvailable)+ : state_(std::make_shared<State>()), deadline_(deadline) {+ Start([process](const std::shared_ptr<State>& state) {+ const bool available = process(false);+ std::lock_guard lock(state->mutex);+ state->capabilities.process = available;+ });+ Start([process = std::move(process)](const std::shared_ptr<State>& state) {+ const bool available = process(true);+ std::lock_guard lock(state->mutex);+ state->capabilities.exclusion = available;+ });+ Start([system = std::move(system)](const std::shared_ptr<State>& state) {+ const bool available = system();+ std::lock_guard lock(state->mutex);+ state->capabilities.system = available;+ });+ }++ Capabilities Snapshot() const {+ std::unique_lock lock(state_->mutex);+ state_->changed.wait_until(lock, deadline_, [state = state_]() { return state->pending == 0; });+ return state_->capabilities;+ }++ private:+ struct State final {+ std::mutex mutex;+ std::condition_variable changed;+ Capabilities capabilities;+ unsigned pending = 3;+ };+ void Start(std::function<void(const std::shared_ptr<State>&)> query) {+ std::thread([state = state_, query = std::move(query)]() {+ const HRESULT apartment = CoInitializeEx(nullptr, COINIT_MULTITHREADED);+ if (SUCCEEDED(apartment)) {+ try { query(state); } catch (...) { /* Optional capabilities remain unavailable. */ }+ CoUninitialize();+ }+ { std::lock_guard lock(state->mutex); --state->pending; }+ state->changed.notify_one();+ }).detach();+ }+ const std::shared_ptr<State> state_;+ const Clock::time_point deadline_;+};++void WriteCapabilityProbe(AudioCapabilityProbe audio = AudioCapabilityProbe()) {const UINT32 build = WindowsBuild();bool window_capture = false;try {@@ -1220,9 +1282,6 @@ void WriteCapabilityProbe() {} catch (const winrt::hresult_error&) {window_capture = false;}- const bool process_audio = piik::capture::ProcessAudioAvailable();- const bool system_audio = piik::capture::SystemAudioAvailable();- const bool process_audio_exclusion = piik::capture::ProcessAudioAvailable(true);std::vector<Adapter> adapters = EnumerateAdapters();std::ostringstream output;@@ -1232,10 +1291,6 @@ void WriteCapabilityProbe() {<< ",\"captureBorderControl\":"<< (window_capture && piik::capture::CaptureBorder::Supported() ? "true" : "false")<< ",\"microphone\":true,\"softwareVP8\":true"- << ",\"processAudio\":"- << (process_audio ? "true" : "false")- << ",\"processAudioExclusion\":" << (process_audio_exclusion ? "true" : "false")- << ",\"systemAudio\":" << (system_audio ? "true" : "false")<< ",\"adapters\":[";for (size_t adapter_index = 0; adapter_index < adapters.size();++adapter_index) {@@ -1262,7 +1317,10 @@ void WriteCapabilityProbe() {}output << "]}";}- output << "]}";+ const auto capabilities = audio.Snapshot();+ output << "],\"processAudio\":" << (capabilities.process ? "true" : "false")+ << ",\"processAudioExclusion\":" << (capabilities.exclusion ? "true" : "false")+ << ",\"systemAudio\":" << (capabilities.system ? "true" : "false") << '}';std::cout << output.str();if (!std::cout.good()) {Fail("capability-output", "could not write the capability result");@@ -1486,7 +1544,7 @@ class OutputWorker final {OutputWorker(UINT8 layer, VideoProfile profile, OutputKind kind, ID3D11Device* device,ProtocolWriter& writer, Factory create,std::unique_ptr<VideoEncoder> initial, bool active,- std::function<void(const EncodedAccessUnit&)> on_output,+ std::function<void(const AdaptiveAccessUnit&)> on_output,std::function<void(std::exception_ptr)> on_failure): layer_(layer), profile_(profile), kind_(kind), device_(device), writer_(writer),create_(std::move(create)), initial_(std::move(initial)),@@ -1586,7 +1644,7 @@ class OutputWorker final {access_unit.bytes.data(), static_cast<DWORD>(access_unit.bytes.size()),layer_, static_cast<UINT16>(output->width),static_cast<UINT16>(output->height)), "capture-video-output");- if (on_output_) on_output_(access_unit);+ if (on_output_) on_output_(*output);}} catch (...) {if (mailbox_.Fail()) on_failure_(std::current_exception());@@ -1604,7 +1662,7 @@ class OutputWorker final {ProtocolWriter& writer_;Factory create_;std::unique_ptr<VideoEncoder> initial_;- std::function<void(const EncodedAccessUnit&)> on_output_;+ std::function<void(const AdaptiveAccessUnit&)> on_output_;std::function<void(std::exception_ptr)> on_failure_;using Mailbox = piik::capture::OutputMailbox<CaptureInput>;Mailbox mailbox_;@@ -1641,16 +1699,16 @@ void WriteVideoStarting(ProtocolWriter& writer, const ProductArguments& argument}void WriteVideoActive(ProtocolWriter& writer, const ProductArguments& arguments,- bool hardware, const EncodedAccessUnit& output) {+ bool hardware, const AdaptiveAccessUnit& output) {std::ostringstream status;status << "{\"state\":\"active\",\"hardwareOnly\":" << (hardware ? "true" : "false")<< ",\"codec\":" << JSONString(hardware ? "h264" : "vp8");if (hardware) {- const auto profile = InspectAnnexB(output.bytes).profile_level_id;+ const auto profile = InspectAnnexB(output.access_unit.bytes).profile_level_id;if (!profile) Fail("capture-active-profile", "first H264 output did not carry an SPS profile");status << ",\"profileLevelId\":" << JSONString(*profile);}- status << ",\"width\":" << arguments.profile.width << ",\"height\":" << arguments.profile.height+ status << ",\"width\":" << output.width << ",\"height\":" << output.height<< ",\"fps\":" << arguments.profile.frame_rate;AppendOutputProfiles(status, arguments.outputs);status << '}';@@ -1660,7 +1718,7 @@ void WriteVideoActive(ProtocolWriter& writer, const ProductArguments& arguments,std::vector<std::unique_ptr<OutputWorker>> CreateOutputWorkers(const ProductArguments& arguments, const Adapter& adapter, const DeviceContext& device,ProtocolWriter& writer, VideoEncoderSelection encoder,- const std::function<void(const EncodedAccessUnit&)>& on_active,+ const std::function<void(const AdaptiveAccessUnit&)>& on_active,const std::function<void(size_t, std::exception_ptr)>& on_failure) {const bool hardware = encoder.kind == OutputKind::h264;const UINT encoder_index = arguments.mft_index;@@ -1681,7 +1739,7 @@ std::vector<std::unique_ptr<OutputWorker>> CreateOutputWorkers(workers.push_back(std::make_unique<OutputWorker>(static_cast<UINT8>(layer), profile, encoder.kind, device.device.Get(), writer,std::move(create), original ? std::move(encoder.initial) : nullptr, capture && layer < 2,- [original, on_active](const EncodedAccessUnit& output) { if (original) on_active(output); },+ [original, on_active](const AdaptiveAccessUnit& output) { if (original) on_active(output); },[layer, on_failure](std::exception_ptr error) { on_failure(layer, error); }));}return workers;@@ -1819,7 +1877,7 @@ void RunEncodedVideo(ProductArguments arguments) {std::atomic<bool> active{false};std::atomic<size_t> failed{0};auto workers = CreateOutputWorkers(arguments, adapter, device, writer, std::move(encoder),- [&](const EncodedAccessUnit& output) {+ [&](const AdaptiveAccessUnit& output) {if (!active.exchange(true)) WriteVideoActive(writer, arguments, hardware, output);},[&](size_t layer, std::exception_ptr error) {@@ -2003,7 +2061,7 @@ void RunVideoCapture(ProductArguments arguments) {const DWORD control_wait_ms = static_cast<DWORD>((frame_duration + 9'999) / 10'000);WriteVideoStarting(writer, arguments, adapter, encoder);workers = CreateOutputWorkers(arguments, adapter, device, writer, std::move(encoder),- [&](const EncodedAccessUnit& output) {+ [&](const AdaptiveAccessUnit& output) {if (!active_status_written.exchange(true)) WriteVideoActive(writer, arguments, hardware, output);},[&](size_t layer, std::exception_ptr error) {diff --git a/native/capture/windows/output_worker.test.cpp b/native/capture/windows/output_worker.test.cppindex 6cedca3f..0f5f6d25 100644--- a/native/capture/windows/output_worker.test.cpp+++ b/native/capture/windows/output_worker.test.cpp@@ -71,7 +71,7 @@ void CheckWorkerFailureGeneration(ID3D11Device* device, bool replace, bool activ// still rebuilds it from the same factory.auto prepared = create(VideoProfile{});OutputWorker worker(0, VideoProfile{}, OutputKind::h264, device, writer,- create, std::move(prepared), active, [&](const EncodedAccessUnit&) { output.set_value(); },+ create, std::move(prepared), active, [&](const AdaptiveAccessUnit&) { output.set_value(); },[&](std::exception_ptr error) {try { std::rethrow_exception(error); }catch (const GateFailure& actual) { assert(actual.result() == E_FAIL); }@@ -135,7 +135,11 @@ void CheckWorkerDimensions(ID3D11Device* device, SIZE content, SIZE expected) {OutputWorker worker(0, VideoProfile{}, OutputKind::h264, device, writer,[](const VideoProfile&) -> std::unique_ptr<VideoEncoder> {return std::make_unique<FixtureEncoder>(nullptr, std::shared_future<void>{});- }, nullptr, true, [&](const EncodedAccessUnit&) { output.set_value(); },+ }, nullptr, true, [&](const AdaptiveAccessUnit& actual) {+ assert(actual.width == static_cast<UINT32>(expected.cx));+ assert(actual.height == static_cast<UINT32>(expected.cy));+ output.set_value();+ },[&](std::exception_ptr error) { output.set_exception(error); });auto frame = std::make_shared<CaptureInput>();frame->width = static_cast<UINT32>(content.cx);@@ -176,13 +180,15 @@ void CheckH264ProfileCompatibility() {ProtocolWriter writer(write);ProductArguments arguments;arguments.outputs = {profile};- EncodedAccessUnit output{0, true, {0, 0, 0, 1, 0x67, 0x42, 0xe0, 0x1f}};+ AdaptiveAccessUnit output{{0, true, {0, 0, 0, 1, 0x67, 0x42, 0xe0, 0x1f}}, 640, 480, 333'333};WriteVideoActive(writer, arguments, true, output);std::array<char, 4096> bytes{};DWORD count = 0;assert(ReadFile(read, bytes.data(), static_cast<DWORD>(bytes.size()), &count, nullptr));const std::string status(bytes.data(), count);assert(status.find("\"profileLevelId\":\"42e01f\"") != std::string::npos);+ assert(status.find("\"width\":640,\"height\":480") != std::string::npos);+ assert(arguments.profile.width == 1280 && arguments.profile.height == 720);assert(status.find("42c01f") == std::string::npos);CloseHandle(write);CloseHandle(read);@@ -447,6 +453,74 @@ void CheckDecodedDisplayArea(ID3D11Device* device) {RECT{16, 8, 3856, 2168}).Get());}+void CheckAudioCapabilityIsolation() {+ using Clock = AudioCapabilityProbe::Clock;+ // Each real worker must own its COM apartment; quick completion must not+ // wait out the optional budget or conflate the independently queried flags.+ const auto apartment = []() {+ APTTYPE type;+ APTTYPEQUALIFIER qualifier;+ assert(SUCCEEDED(CoGetApartmentType(&type, &qualifier)) && type == APTTYPE_MTA);+ };+ const auto start = Clock::now();+ AudioCapabilityProbe quick(start + std::chrono::seconds(5),+ [apartment](bool exclude) { apartment(); return !exclude; },+ [apartment]() { apartment(); return true; });+ const auto supported = quick.Snapshot();+ assert(Clock::now() - start < std::chrono::seconds(1));+ assert(supported.process && supported.system && !supported.exclusion);++ auto include_release = std::make_shared<std::promise<void>>();+ auto include_released = include_release->get_future().share();+ AudioCapabilityProbe include_slow(Clock::now() + std::chrono::milliseconds(30),+ [include_released](bool exclude) { if (!exclude) include_released.wait(); return true; },+ []() { return true; });+ const auto partial = include_slow.Snapshot();+ assert(!partial.process && partial.exclusion && partial.system);+ include_release->set_value();+ const auto include_deadline = Clock::now() + std::chrono::seconds(2);+ while (!include_slow.Snapshot().process) {+ assert(Clock::now() < include_deadline);+ std::this_thread::sleep_for(std::chrono::milliseconds(1));+ }++ auto release = std::make_shared<std::promise<void>>();+ auto released = release->get_future().share();+ AudioCapabilityProbe slow(Clock::now() + std::chrono::milliseconds(30),+ [released](bool) { released.wait(); return true; },+ [released]() { released.wait(); return true; });+ const auto snapshot = slow.Snapshot();+ assert(!snapshot.process && !snapshot.system && !snapshot.exclusion);+ {+ Runtime runtime;+ std::ostringstream output;+ const auto previous = std::cout.rdbuf(output.rdbuf());+ try { WriteCapabilityProbe(slow); }+ catch (...) { std::cout.rdbuf(previous); release->set_value(); throw; }+ std::cout.rdbuf(previous);+ const auto payload = output.str();+ // The actual production serializer still emits video/encoder discovery+ // while all optional API queries remain blocked past their shared budget.+ assert(payload.find("\"videoCapture\":") != std::string::npos);+ assert(payload.find("\"adapters\":[") != std::string::npos);+ assert(payload.find("\"processAudio\":false") != std::string::npos);+ assert(payload.find("\"systemAudio\":false") != std::string::npos);+ assert(payload.find("\"processAudioExclusion\":false") != std::string::npos);+ }+ // The returned snapshot is independent of shared late results. Release the+ // controlled APIs and observe their successful completion before this fixture+ // ends, without requiring the production probe to join a blocked OS call.+ release->set_value();+ const auto deadline = Clock::now() + std::chrono::seconds(2);+ for (;;) {+ const auto current = slow.Snapshot();+ if (current.process && current.system && current.exclusion) break;+ assert(Clock::now() < deadline);+ std::this_thread::sleep_for(std::chrono::milliseconds(1));+ }+ assert(!snapshot.process && !snapshot.system && !snapshot.exclusion);+}+int main() {VideoProfile profile{2560, 1440, 60, 12'000'000};ValidateVideoProfile(profile);@@ -496,6 +570,7 @@ int main() {CheckEncoderCandidates();CheckH264ProfileCompatibility();CheckPrimaryFailureDetails();+ CheckAudioCapabilityIsolation();CheckOutputSampleAlignment();ComPtr<ID3D11Device> device;Check(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_WARP, nullptr, 0, nullptr, 0,diff --git a/native/probes/encoder-pool/h264_encoder_adapter.cpp b/native/probes/encoder-pool/h264_encoder_adapter.cppindex b4e0f538..0dbfda72 100644--- a/native/probes/encoder-pool/h264_encoder_adapter.cpp+++ b/native/probes/encoder-pool/h264_encoder_adapter.cpp@@ -6,6 +6,7 @@#include <cmath>#include <iostream>#include <limits>+#include <stdexcept>#include <utility>#include <vector>@@ -58,9 +59,6 @@ class HardwareEncoder final : public webrtc::VideoEncoder {profile_.height = config->height;profile_.frame_rate = config->maxFramerate;profile_.bit_rate = config->startBitrate * 1000;- if (profile_.h264_level() > static_cast<UINT32>(maximum_level_)) {- return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;- }try {runtime_ = std::make_unique<native::Runtime>();auto adapters = native::EnumerateAdapters();@@ -181,6 +179,16 @@ class HardwareEncoder final : public webrtc::VideoEncoder {const auto access_unit = encoder_->Encode(texture.Get(), static_cast<UINT64>(frame.timestamp_us()) * 10,key_frame_pending_);+ // LiveEncoder lets MF select the level and validates the emitted profile.+ // Constrain the actual SPS to this WebRTC encoder's negotiated level.+ const auto summary = native::InspectAnnexB(access_unit.bytes);+ if (summary.profile_level_id) {+ const auto profile = webrtc::ParseH264ProfileLevelId(+ summary.profile_level_id->c_str());+ if (!profile || profile->level > maximum_level_) {+ throw std::runtime_error("hardware H264 exceeds the negotiated level");+ }+ }key_frame_pending_ = false;webrtc::EncodedImage image;image.SetEncodedData(webrtc::EncodedImageBuffer::Create(@@ -276,15 +284,8 @@ class HardwareFactory final : public webrtc::VideoEncoderFactory {std::optional<webrtc::Resolution> resolution) const override {if (resolution && !ValidDimensions(resolution->width, resolution->height)) return {};const auto profile = webrtc::ParseSdpForH264ProfileLevelId(format.parameters);- native::VideoProfile minimum;- minimum.frame_rate = 1;- if (resolution) {- minimum.width = resolution->width;- minimum.height = resolution->height;- }const bool supported = format.IsSameCodec(HardwareFormat()) &&- (!scalability_mode || *scalability_mode == "L1T1") && profile &&- minimum.h264_level() <= static_cast<UINT32>(profile->level);+ (!scalability_mode || *scalability_mode == "L1T1") && profile.has_value();return {supported, supported};}diff --git a/package-lock.json b/package-lock.jsonindex 8ecb999c..f0e6ee2a 100644--- a/package-lock.json+++ b/package-lock.json@@ -25,7 +25,7 @@"vite": "8.2.4","vitest": "4.1.12","ws": "8.21.3",- "zod": "4.4.3"+ "zod": "4.6.6"},"engines": {"node": ">=24.0.0 <27"@@ -1912,9 +1912,9 @@}},"node_modules/zod": {- "version": "4.4.3",- "resolved": "https://registry.npmjs.org/zod/-/zod-4.4.3.tgz",- "integrity": "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ==",+ "version": "4.6.6",+ "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.6.tgz",+ "integrity": "sha512-4UmzQUF5fxvfcYHlbjBmejU+w+hbDnWFx4aS8kBx7nPBHcO2SqPbpAG8snbdg+Xse4pRefR98hgjFgIMCI/UhA==","dev": true,"license": "MIT","funding": {diff --git a/package.json b/package.jsonindex 187ab86a..9622c283 100644--- a/package.json+++ b/package.json@@ -59,6 +59,6 @@"vite": "8.2.4","vitest": "4.1.12","ws": "8.21.3",- "zod": "4.4.3"+ "zod": "4.6.6"}}diff --git a/scripts/app-media-gate.ts b/scripts/app-media-gate.tsindex a4b29632..bdb13e71 100644--- a/scripts/app-media-gate.ts+++ b/scripts/app-media-gate.ts@@ -356,7 +356,7 @@ async function browserMediaGate(input: {rejectRequest(new Error("Native request timed out: " + type));}, 8_000);pending.set(id, { resolve: resolveRequest, reject: rejectRequest, timer });- socket!.send(JSON.stringify({ version: 10, id, type, ...fields }));+ socket!.send(JSON.stringify({ version: input.protocol, id, type, ...fields }));});};socket.onmessage = (event) => {diff --git a/scripts/browser-gate-harness.ts b/scripts/browser-gate-harness.tsindex 72b8c0a3..3c8cd6ea 100644--- a/scripts/browser-gate-harness.ts+++ b/scripts/browser-gate-harness.ts@@ -550,7 +550,11 @@ async function removeGateProfile(profile: string): Promise<boolean> {try {while (pending.length > 0) {const current = pending.pop()!;- const stat = await lstat(current);+ const stat = await lstat(current).catch(error => {+ if (current === profile && (error as NodeJS.ErrnoException).code === "ENOENT") return null;+ throw error;+ });+ if (!stat) return true;if (stat.isSymbolicLink()) return false;if (stat.isDirectory()) {pending.push(diff --git a/scripts/browser-local-pool-probe.ts b/scripts/browser-local-pool-probe.tsindex 026a4c14..a464b17e 100644--- a/scripts/browser-local-pool-probe.ts+++ b/scripts/browser-local-pool-probe.ts@@ -1,4 +1,5 @@import { createServer } from 'node:http';+import type { ChildProcessWithoutNullStreams } from 'node:child_process';import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';import { createHash } from 'node:crypto';import { build } from 'esbuild';@@ -45,7 +46,12 @@ const body = Buffer.from(bundle.outputFiles[0].contents);let cdp: CdpConnection | null = null;let page: Awaited<ReturnType<typeof createPage>> | undefined;let originalTarget: string | undefined, backgroundTarget: string | undefined;-const shaper = network ? await createPoolShaper() : null;+let shaper: Awaited<ReturnType<typeof createPoolShaper>> | null = null;+let profile: string | null = null;+let chrome: ChildProcessWithoutNullStreams | null = null;+let port = 0, debugPort = 0;+let result: Record<string, unknown> = { mode, codec, product: true, network, automatic, high, av, single, late, shortPulse, repeatedPulses, textured,+ background, nativeSource, displaySource, quietStart, lifecycle, relay, shapedRate: rate, preference };const server = createServer((request, response) => {const url = new URL(request.url!, 'http://127.0.0.1');if (url.pathname === '/visibility' && cdp && page) {@@ -77,19 +83,19 @@ const server = createServer((request, response) => {response.end(url.pathname === '/probe.js' ? body :'<!doctype html><meta charset=utf-8><title>Browser local pool</title><script type=module src=/probe.js></script>');});-await new Promise<void>(r => server.listen(0, '127.0.0.1', r));-const port = (server.address() as { port: number }).port, debugPort = await reservePort();-const profile = await mkdtemp(join(tmpdir(), 'piik-client-media-'));-const chrome = await launchChrome(process.env.CHROME_PATH || 'C:/Program Files/Google/Chrome/Application/chrome.exe', debugPort, profile,- ['--headless=new', '--no-first-run', '--autoplay-policy=no-user-gesture-required',- ...(displaySource ? [`--window-size=${size?.width ?? 1920},${size?.height ?? 1200}`, '--force-device-scale-factor=1',- '--auto-accept-this-tab-capture', '--auto-select-tab-capture-source-by-title=Browser local pool'] : []),- ...(background ? [] : ['--disable-background-timer-throttling', '--disable-renderer-backgrounding']),- ...(nativeSource ? ['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'] : [])]);-chrome.stdout.resume(); chrome.stderr.resume();-let result: Record<string, unknown> = { mode, codec, product: true, network, automatic, high, av, single, late, shortPulse, repeatedPulses, textured,- background, nativeSource, displaySource, quietStart, lifecycle, relay, shapedRate: rate, preference };try {+ shaper = network ? await createPoolShaper() : null;+ await new Promise<void>((resolve, reject) => { server.once('error', reject); server.listen(0, '127.0.0.1', resolve); });+ port = (server.address() as { port: number }).port;+ debugPort = await reservePort();+ profile = await mkdtemp(join(tmpdir(), 'piik-client-media-'));+ chrome = await launchChrome(process.env.CHROME_PATH || 'C:/Program Files/Google/Chrome/Application/chrome.exe', debugPort, profile,+ ['--headless=new', '--no-first-run', '--autoplay-policy=no-user-gesture-required',+ ...(displaySource ? [`--window-size=${size?.width ?? 1920},${size?.height ?? 1200}`, '--force-device-scale-factor=1',+ '--auto-accept-this-tab-capture', '--auto-select-tab-capture-source-by-title=Browser local pool'] : []),+ ...(background ? [] : ['--disable-background-timer-throttling', '--disable-renderer-backgrounding']),+ ...(nativeSource ? ['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'] : [])]);+ chrome.stdout.resume(); chrome.stderr.resume();const version = await waitForVersion(debugPort, chrome);cdp = await CdpConnection.connect(version.webSocketDebuggerUrl, Date.now() + 5000);page = await createPage(cdp, 'about:blank');@@ -113,8 +119,11 @@ try {if (cdp && page) result.partial = await evaluate(cdp, page, 'window.probeResult || window.probeProgress?.()', Date.now() + 3000).catch(() => null);} finally {const clean = await cleanupRun({ cdp, native: null, chrome,- server: { close: () => new Promise<void>((resolve, reject) => server.close(e => e ? reject(e) : resolve())) },- profile, ports: [port, debugPort] });+ server: { close: () => new Promise<void>((resolve, reject) => {+ if (!server.listening) { resolve(); return; }+ server.close(e => e ? reject(e) : resolve());+ }) },+ profile, ports: [port, debugPort].filter(Boolean) });if (shaper) { result.shaper = shaper.stats(); await shaper.close(); }result.probeSha256 = createHash('sha256').update(body).digest('hex');result.cleanup = clean;diff --git a/scripts/media-lifecycle-gate.ts b/scripts/media-lifecycle-gate.tsindex e5062959..1cd24696 100644--- a/scripts/media-lifecycle-gate.ts+++ b/scripts/media-lifecycle-gate.ts@@ -119,7 +119,8 @@ for (let iteration = 0; iteration < repeats && !signal.aborted; iteration++) forfor (let index = 0; index < 2; index++) endpoints.push(new Endpoint(null,await mkdtemp(join(tmpdir(), "piik-client-media-")), signal, env));versions = await Promise.all(endpoints.map((endpoint, index) => endpoint.call<typeof versions[number]>("start", {- origin: index ? viewerOrigin : origin, app: index ? scenario.viewer : scenario.host })));+ origin: index ? viewerOrigin : origin, app: index ? scenario.viewer : scenario.host,+ loopbackMedia: scenario.sfu || scenario.fallback === true })));result.versions = versions;await endpoints[0]!.call("navigate", { url: origin + (scenario.host ? "/#piik-client=1" : "/") });const share = () => endpoints[0]!.call<string>("share", { sfuOnly: scenario.sfu });@@ -146,6 +147,18 @@ for (let iteration = 0; iteration < repeats && !signal.aborted; iteration++) forcheckContinuity(frozen, (await sample()).viewer);throw new Error("Frozen source incorrectly passed");}+ if (!scenario.host && !scenario.sfu) {+ stage("ice-restart");+ const before = await sample();+ if (!await endpoints[1]!.call("restart-upstream")) throw new Error("No committed Peer route to restart");+ await waitForSample(sample, row => row.host.events.filter(event => event.type === "restart-request").length >+ before.host.events.filter(event => event.type === "restart-request").length && (!scenario.viewer ||+ row.viewer.controls.filter(command => command.type === "receive-offer").length >+ before.viewer.controls.filter(command => command.type === "receive-offer").length), 10_000);+ const restarted = await media("same-edge-recovered");+ if (restarted.playbackPeerId !== received.playbackPeerId) throw new Error("ICE restart replaced the playback connection");+ received = restarted;+ }stage("quiet-source"); await endpoints[0]!.call("freeze-picture");await delay(6000, undefined, { signal });const quiet = await sample();diff --git a/scripts/network-lab/endpoint.ts b/scripts/network-lab/endpoint.tsindex 081c9c79..9ea642df 100644--- a/scripts/network-lab/endpoint.ts+++ b/scripts/network-lab/endpoint.ts@@ -33,7 +33,7 @@ export async function endpointWorker(): Promise<void> {};try {for await (const line of createInterface({ input: process.stdin })) {- const request = JSON.parse(line) as { id: number; action: string; origin?: string; app?: boolean;+ const request = JSON.parse(line) as { id: number; action: string; origin?: string; app?: boolean; loopbackMedia?: boolean;url?: string; output?: string; delayMs?: number; load?: boolean; ipv6?: boolean; prediction?: boolean; sfuOnly?: boolean; peer?: { address: string; port: number } };try {let result: unknown;@@ -61,6 +61,8 @@ export async function endpointWorker(): Promise<void> {chrome = await launchChrome(process.env.CHROME_PATH!, debugPort, profile, ["--headless=new", "--no-sandbox", "--disable-dev-shm-usage", "--no-first-run", "--no-default-browser-check","--no-proxy-server", "--autoplay-policy=no-user-gesture-required", "--disable-background-timer-throttling",+ // The lifecycle SFU binds only loopback; namespace trials keep ordinary ICE interfaces.+ ...(request.loopbackMedia ? ["--allow-loopback-in-peer-connection"] : []),"--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream","--disable-renderer-backgrounding", "--disable-backgrounding-occluded-windows",`--unsafely-treat-insecure-origin-as-secure=${origin}`,@@ -119,6 +121,8 @@ export async function endpointWorker(): Promise<void> {} else if (request.action === "reset-playback-progress") {await evaluate(cdp!, page!, "networkLab.resetPlaybackProgress()", Date.now() + 5000);result = true;+ } else if (request.action === "restart-upstream") {+ result = await evaluate(cdp!, page!, "networkLab.restartUpstream()", Date.now() + 5000);} else if (request.action === "freeze-picture") {result = await evaluate(cdp!, page!, "networkLab.freezeCapture()", Date.now() + 5000);} else if (request.action === "resume-picture") {diff --git a/scripts/network-lab/page.ts b/scripts/network-lab/page.tsindex 9e1675a9..f0adea15 100644--- a/scripts/network-lab/page.ts+++ b/scripts/network-lab/page.ts@@ -3,7 +3,7 @@export const pageProbe = String.raw`(() => {localStorage.setItem('piik:ui-lang','en'); localStorage.setItem('piik:ui-mode','text');const peers=[], sockets=[], events=[], errors=[], controls=[];- let route=null,natPrediction=null,voice=null,freezeCapture=()=>{},resumeCapture=()=>{},peerSignalDelay=0,peerSignalReleaseAt=0;+ let route=null,upstream=null,natPrediction=null,voice=null,freezeCapture=()=>{},resumeCapture=()=>{},peerSignalDelay=0,peerSignalReleaseAt=0;let observedVideo=null,frameCallback=0,renderedFrames=0,lastFrameAt=0,maxFrameGapMs=0;const observeVideo=video=>{if(video===observedVideo)return;@@ -31,10 +31,16 @@ export const pageProbe = String.raw`(() => {this.addEventListener('message', event=>{let message; try{message=JSON.parse(event.data);}catch{return;}if(typeof message.routePolicy?.natPrediction==='boolean')natPrediction=message.routePolicy.natPrediction;- if(message.type==='route-update'&&message.phase==='active')route=message.assignment.upstream.kind;+ if(message.type==='route-update'&&message.phase==='active'){+ upstream=message.assignment.upstream;route=upstream.kind;+ }if(message.type==='room-voice-state')voice=message;const value={at:performance.now(),type:message.type,state:message.state,reason:message.reason,scope,phase:message.phase,route:message.assignment?.upstream?.kind,connectionId:message.connectionId};+ if(message.type==='signal'&&message.payload?.kind==='description'){+ value.fromPeerId=message.fromPeerId;value.connectionId=message.payload.connectionId;+ value.descriptionType=message.payload.description.type;+ }if(message.type==='punch-prepared')value.predictionStep=message.prediction?.step;if(message.type==='punch-complete')value.connected=message.connected;for(const key of ['localType','remoteType','natTraversalPath','sampleWindowMs','intervalFramesEncoded',@@ -89,6 +95,13 @@ export const pageProbe = String.raw`(() => {}return {energy,samples,playing:audio.length,publications:voice?.publications.length||0};},freezeCapture:()=>freezeCapture(),resumeCapture:()=>resumeCapture(),delayPeerSignals:ms=>{peerSignalDelay=ms;peerSignalReleaseAt=0;},+ restartUpstream:()=>{+ const socket=sockets.findLast(s=>!s.url.includes('/control')&&s.readyState===WS.OPEN);+ const offer=events.findLast(event=>event.type==='signal'&&event.fromPeerId===upstream?.peerId&&event.descriptionType==='offer');+ if(!socket||upstream?.kind!=='peer'||!offer)return false;+ socket.send(JSON.stringify({type:'restart-request',targetPeerId:upstream.peerId,+ connectionId:offer.connectionId,rebuild:false}));return true;+ },resetPlaybackProgress:()=>{renderedFrames=0;lastFrameAt=performance.now();maxFrameGapMs=0;},async signalProbe(){const socket=sockets.findLast(s=>!s.url.includes('/control')&&s.readyState===WS.OPEN);if(!socket)return false;diff --git a/scripts/peer-assisted-benchmark.ts b/scripts/peer-assisted-benchmark.tsindex 737043c6..a61ab2a6 100644--- a/scripts/peer-assisted-benchmark.ts+++ b/scripts/peer-assisted-benchmark.ts@@ -602,9 +602,6 @@ function parseBenchmarkConfig("BENCHMARK_QUALITY_SMOKE requires a selected viewer count of at least 3",);}- if (environment.BENCHMARK_CANARY && environment.BENCHMARK_CANARY !== "none") {- throw new Error("BENCHMARK_CANARY is obsolete; this gate covers topology and relay-loss recovery");- }const recoveryText = environment.BENCHMARK_RECOVERY_VIEWERS?.trim();const recoveryViewerCount = recoveryText ? Number(recoveryText) : null;if (diff --git a/scripts/release-version.mjs b/scripts/release-version.mjsindex 70c9e14d..7c1e61f1 100644--- a/scripts/release-version.mjs+++ b/scripts/release-version.mjs@@ -27,6 +27,7 @@ const nonProductPaths = ["scripts/release-*.mjs", "scripts/publish-release.mjs", "scripts/publish-container.mjs", "scripts/mirror-release.mjs","scripts/*-gate.*", "scripts/*-probe.*", "scripts/browser-*", "scripts/encoded-*","scripts/*-gate-endpoint.ts", "scripts/embedded-sfu-page.ts", "scripts/peer-assisted-benchmark.ts",+ "scripts/network-lab.ts", "scripts/network-lab/**","cmd/piik-peer-gate/**",].map((path) => `:(top,glob,exclude)${path}`);diff --git a/site/assets/brand-motion.css b/site/assets/brand-motion.cssindex eac556d8..a291a17e 100644--- a/site/assets/brand-motion.css+++ b/site/assets/brand-motion.css@@ -64,7 +64,7 @@}/* Drawing paints on the main thread. Release composited gestures only afterits last stroke ends, so a busy page cannot wink on an unfinished shell. */-.lr-brand-mark:is(.is-animated, .is-once).is-drawing :is(.lr-brand-face, .lr-brand-sparkles) { visibility: hidden; }+:where(html:not([data-reduced-motion])) .lr-brand-mark:is(.is-animated, .is-once).is-drawing :is(.lr-brand-face, .lr-brand-sparkles) { visibility: hidden; }.lr-brand-mark .lr-brand-antenna-motion { transform-origin: 16px 8px; }.lr-brand-mark:is(.is-animated.is-loop-intro, .is-once):not(.is-drawing) .lr-brand-antenna-motion { animation: lr-brand-antenna-wiggle 1.6s ease-in-out both; }.lr-brand-mark.is-animated.is-loop-active .lr-brand-antenna-motion { animation: lr-brand-antenna-wiggle 3.6s ease-in-out infinite; }@@ -128,6 +128,9 @@.lr-brand-sparkles { stroke: #d08a22; }html[data-theme="dark"] .lr-brand-sparkles { stroke: #f4c14d; }+/* The catalogue/website motion preference shares the system's static shell. */+html[data-reduced-motion] .lr-brand-mark:is(.is-animated, .is-once) .lr-brand-shell :where([pathLength]) { stroke-dashoffset: 0; }+@media (prefers-reduced-motion: reduce) {.lr-brand-mark:is(.is-animated, .is-once) .lr-brand-antenna-motion,.lr-brand-mark:is(.is-animated, .is-once) .lr-brand-shell :where([pathLength]) {diff --git a/site/assets/brand.js b/site/assets/brand.jsindex 6f9c1638..099f436a 100644--- a/site/assets/brand.js+++ b/site/assets/brand.js@@ -1,3 +1,5 @@+import { bindSvgReplayOnPointerEnter, replaySvgAnimations } from '../../src/client/ui/animation.ts';+// Shared geometry for the website, film and original demonstration scenes.export function mascotMarkup(id, fill = 'none', animated = false) {const part = animated ? 'lr-brand' : 'mascot';@@ -20,20 +22,30 @@ export function brandMarkup(id) {return `<svg class="mascot lr-brand-mark is-once is-drawing" viewBox="0 0 32 32" aria-hidden="true">${mascotMarkup(id, 'none', true)}</svg><span>Piik<span class="identity-dot">.</span></span>`;}+export function bindBrandMotion(graphic) {+ const feet = graphic.querySelector('.lr-brand-feet');+ const finishDrawing = event => {+ if (event.animationName === 'lr-brand-draw') graphic.classList.remove('is-drawing');+ };+ feet.addEventListener('animationend', finishDrawing);+ const beginDrawing = () => {+ graphic.classList.add('is-drawing');+ // Commit the hidden gesture before restarting the drawing timeline.+ void graphic.getBoundingClientRect();+ };+ const stopReplay = bindSvgReplayOnPointerEnter(graphic, beginDrawing);+ // SSR may finish drawing before hydration attaches the completion listener.+ beginDrawing();+ replaySvgAnimations(graphic);+ return () => {+ feet.removeEventListener('animationend', finishDrawing);+ stopReplay();+ };+}+export function mountBrands() {document.querySelectorAll('[data-brand]').forEach((link, index) => {link.innerHTML = brandMarkup(`brand-${index}`);- const graphic = link.querySelector('.lr-brand-mark');- graphic.querySelector('.lr-brand-feet').addEventListener('animationend', event => {- if (event.animationName === 'lr-brand-draw') graphic.classList.remove('is-drawing');- });- const replay = () => {- graphic.classList.add('is-drawing');- // Commit the hidden gesture before restarting the drawing timeline.- void graphic.getBoundingClientRect();- graphic.getAnimations({ subtree: true }).forEach(animation => { animation.currentTime = 0; });- };- link.addEventListener('pointerenter', replay);- link.addEventListener('focus', replay);+ bindBrandMotion(link.querySelector('.lr-brand-mark'));});}diff --git a/site/docs/.vitepress/theme/index.js b/site/docs/.vitepress/theme/index.jsindex e4ce0d67..81c8a55f 100644--- a/site/docs/.vitepress/theme/index.js+++ b/site/docs/.vitepress/theme/index.js@@ -1,13 +1,25 @@import DefaultTheme from 'vitepress/theme-without-fonts';import { useData, withBase } from 'vitepress';-import { h, provide } from 'vue';+import { h, onBeforeUnmount, onMounted, provide, ref } from 'vue';import { useStorage } from '@vueuse/core';import { THEME_STORAGE_KEY } from '../../../../src/client/ui/theme.ts';-import { brandMarkup } from '../../../assets/brand.js';+import { bindBrandMotion, brandMarkup } from '../../../assets/brand.js';import { initialLanguage, rememberLanguage } from '../../../assets/language.js';import '../../../assets/brand.css';import './style.css';+const DocumentationBrand = {+ setup() {+ const element = ref();+ let stopMotion;+ onMounted(() => {+ stopMotion = bindBrandMotion(element.value.querySelector('.lr-brand-mark'));+ });+ onBeforeUnmount(() => stopMotion?.());+ return () => h('span', { ref: element, class: 'identity', innerHTML: brandMarkup('docs-brand') });+ },+};+export default {extends: DefaultTheme,setup() {@@ -24,8 +36,7 @@ export default {Layout() {const { lang } = useData();return h(DefaultTheme.Layout, null, {- 'nav-bar-title-before': () => h('span', { class: 'identity',- innerHTML: brandMarkup('docs-brand') }),+ 'nav-bar-title-before': () => h(DocumentationBrand),'doc-bottom': () => h('p', { class: 'docs-license' }, [h('a', { href: withBase('/third-party-licenses.txt') },lang.value === 'zh-CN' ? '第三方软件许可' : 'Third-party licenses'),diff --git a/site/index.html b/site/index.htmlindex 7f37f71c..afdd63c6 100644--- a/site/index.html+++ b/site/index.html@@ -948,8 +948,8 @@<span lang="en">Reduce motion</span><span lang="zh-CN">减少动态</span></label><p id="display-motion-help">- <span lang="en">Keep illustrations still and stop rotating captions.</span- ><span lang="zh-CN">插画保持静止,短句停止轮换。</span>+ <span lang="en">Keep illustrations still.</span+ ><span lang="zh-CN">插画保持静止。</span></p><p id="display-motion-system" hidden><span lang="en">Enabled by your system setting.</span><span lang="zh-CN">已跟随系统减少动态。</span>diff --git a/site/main.js b/site/main.jsindex b7545e35..c990da97 100644--- a/site/main.js+++ b/site/main.js@@ -68,11 +68,13 @@ const film = document.getElementById('film-preview');const filmFrame = document.getElementById('website-film');const filmLink = document.querySelector('[data-film-link]');const welcomeLines = document.getElementById('welcome-lines')?.content.children;+const selectedWelcome = new Map();function syncWelcome() {if (!welcomeLines) return;const element = document.getElementById('welcome-line');const pool = Array.from(welcomeLines).filter(line => line.lang === root.lang);- const line = createTextCycle(pool)();+ if (!selectedWelcome.has(root.lang)) selectedWelcome.set(root.lang, createTextCycle(pool)());+ const line = selectedWelcome.get(root.lang);element.hidden = pool.length === 0;element.replaceChildren(...(line ? [line.cloneNode(true)] : []));}diff --git a/src/client/components/living/CaptureSourcePicker.tsx b/src/client/components/living/CaptureSourcePicker.tsxindex f553e470..4df5adbe 100644--- a/src/client/components/living/CaptureSourcePicker.tsx+++ b/src/client/components/living/CaptureSourcePicker.tsx@@ -25,7 +25,7 @@ const SOURCE_ICONS = {export type NativeSourceList =| { kind: "browser" }- | { kind: "loading" }+ | { kind: "loading"; appDetected?: boolean }| { kind: "unavailable" }| { kind: "incompatible" }| { kind: "unsupported" }@@ -39,6 +39,11 @@ export type NativeSourceList =captureBorderControl?: boolean;};+export function nativeAppDetected(sources: NativeSourceList | null): boolean {+ return sources?.kind === "loading" ? sources.appDetected === true+ : sources !== null && sources.kind !== "browser" && sources.kind !== "unavailable";+}+export function CaptureSourcePicker({nativeSources,onBrowser,@@ -90,8 +95,7 @@ export function CaptureSourcePicker({const [shareAudio, setShareAudio] = useState(initialAudio);const [showCaptureBorder, setShowCaptureBorder] = useState(initialShowCaptureBorder);const [excludeAudio, setExcludeAudio] = useState(initialExcludeAudio);- const appDetected = nativeSources.kind === "ready" || nativeSources.kind === "failed" ||- nativeSources.kind === "unsupported" || nativeSources.kind === "incompatible";+ const appDetected = nativeAppDetected(nativeSources);const tabs = SOURCE_TABS.filter((value) => appDetected || value === "browser" || value === "camera");const preferredTab = tab ?? initialTab;const tabAvailable = (value: SourceTab) => value === "camera" ? cameraAvailable@@ -379,7 +383,11 @@ export function CaptureSourcePicker({<AppConnectionButton sourcePicker onConnected={onAppConnected} />)}- {issueKey ? (+ {nativeTab && nativeSources.kind === "loading" ? (+ <span className="lr-source-picker-status" role="status" aria-label={t("host.sourcePicker.loading")}>+ <Glyph name="refresh" className="lr-spin" size={18} />{!vis && t("host.sourcePicker.loading")}+ </span>+ ) : issueKey ? (<Pill icon="alert" label={t(issueKey)} comic="warning" />) : browserCapture || activeTab === "camera" ? null : sources.length === 0 ? (<spandiff --git a/src/client/components/living/Pawn.tsx b/src/client/components/living/Pawn.tsxindex 86104d65..084029ef 100644--- a/src/client/components/living/Pawn.tsx+++ b/src/client/components/living/Pawn.tsx@@ -26,26 +26,28 @@ export function PersonShape({ eyes = true, gaze = 0, eyeClassName, host }: {}function PawnAdornment({ voice, collaboration, host }: PawnState) {- return <g className="lr-person-status" color="var(--person-ink, #263b43)" fill="none"+ return <g className="lr-person-status" data-voice={voice ? "" : undefined}+ data-deafened={voice?.deafened || undefined} data-microphone={voice?.microphone || undefined}+ data-collaboration={collaboration || undefined} color="var(--person-ink, #263b43)" fill="none"stroke="currentColor" strokeLinecap="round" strokeLinejoin="round">- {voice ? <>- {voice.deafened ? <g>+ <g className="lr-person-headset">+ <g className="lr-person-neckband"><path d="M15 28q0-5 9-5t9 5" strokeWidth="1.6" />- <path d="M15 27v5m18-5v5" strokeWidth="3.4" />- </g> : <g>+ </g>+ <g className="lr-person-headband"><path d={host ? "M14.8 15v-2a9.2 9.2 0 0 1 3.5-7.2m11.4 0a9.2 9.2 0 0 1 3.5 7.2v2": "M14.8 15v-2a9.2 9.2 0 0 1 18.4 0v2"} strokeWidth="1.6" />- <path d="M14.8 13v5m18.4-5v5" strokeWidth="3.4" />- </g>}- <g transform={`translate(33 ${voice.deafened ? 28 : 17})`}>- <path d={voice.microphone- ? voice.deafened ? "M0 0v-4q0-3-4-3h-2" : "M0 0v1.5q0 3-4 3h-2"- : "M0 0l2-8"} strokeWidth="1.4" />- <circle cx={voice.microphone ? -6 : 2} cy={voice.microphone ? voice.deafened ? -7 : 4.5 : -8}- r="1.2" fill="currentColor" stroke="none" /></g>- </> : null}- {collaboration ? <g transform="translate(16.5 32.5) rotate(-45 7.5 7.5)"><Glyph name="mouse" size={15} /></g> : null}+ <path className="lr-person-earcup-left" d="M14.8 13v5" strokeWidth="3.4" />+ <path className="lr-person-earcup-right" d="M33.2 13v5" strokeWidth="3.4" />+ <g className="lr-person-mic-position">+ <path className="lr-person-mic-open lr-person-mic-head" d="M0 0v1.5q0 3-4 3h-2" strokeWidth="1.4" />+ <path className="lr-person-mic-open lr-person-mic-neck" d="M0 0v-4q0-3-4-3h-2" strokeWidth="1.4" />+ <path className="lr-person-mic-folded" d="M0 0l2-8" strokeWidth="1.4" />+ <circle className="lr-person-mic-tip" r="1.2" fill="currentColor" stroke="none" />+ </g>+ </g>+ <g className="lr-person-collaboration"><g transform="translate(16.5 32.5) rotate(-45 7.5 7.5)"><Glyph name="mouse" size={15} /></g></g></g>;}diff --git a/src/client/components/living/PlaybackControls.tsx b/src/client/components/living/PlaybackControls.tsxindex 85597137..752f55bf 100644--- a/src/client/components/living/PlaybackControls.tsx+++ b/src/client/components/living/PlaybackControls.tsx@@ -73,7 +73,6 @@ export function PlaybackControls({}};const toggleFullscreen = () => {- if (theaterMode) return;const video = videoRef.current as FullscreenVideo | null;if (!video) return;setFullscreenFailed(false);@@ -202,7 +201,7 @@ export function PlaybackControlsView({</button>;return <>- <div id={controlsId} ref={barRef} className={`lr-playback lr-host-share-controls lr-media-toolbar lr-media-controls${visibility?.hidden ? " is-hidden" : ""}`}+ <div id={controlsId} ref={barRef} className={`lr-playback lr-media-toolbar lr-media-controls${visibility?.hidden ? " is-hidden" : ""}`}role="group" aria-label={t("playback.controls")}><div className="lr-playback-main"><span className="lr-playback-watch">@@ -249,7 +248,7 @@ export function PlaybackControlsView({</div><span className="lr-playback-window" role="group" aria-label={t("playback.windowControls")}>{displayActions}- {fullscreen.supported && !theaterMode ? (+ {fullscreen.supported ? (<Btn icon={fullscreen.active ? "contract" : "expand"}draw="playback-fullscreen"title={fullscreenFailed ? "playback.fullscreenFailed" : fullscreen.active ? "playback.exitFullscreen"diff --git a/src/client/components/living/ShareFailureHelp.tsx b/src/client/components/living/ShareFailureHelp.tsxindex a4082ea5..58bfe514 100644--- a/src/client/components/living/ShareFailureHelp.tsx+++ b/src/client/components/living/ShareFailureHelp.tsx@@ -18,16 +18,21 @@ export function ShareFailureHelp({ status, label, code, checks }: {const titleId = useId();const [open, setOpen] = useState(false);const [copied, setCopied] = useState<"idle" | "done" | "failed">("idle");+ const copyRequest = useRef<object | null>(null);const hint = (status.tooltip ?? status.comic)!;const guide = useProjectLink(`/docs/${lang === "zh" ? "zh/" : ""}troubleshooting.html`,`https://github.com/TNTcraftHIM/Piik/blob/main/docs/guide/troubleshooting${lang === "zh" ? ".zh-CN" : ""}.md`, open);return <><StatusIndicator status={status} label={label} onActivate={() => {+ copyRequest.current = null;setCopied("idle");setOpen(true);dialog.current?.showModal();}} />- <dialog ref={dialog} className="lr-share-help" aria-labelledby={titleId} onClose={() => setOpen(false)}>+ <dialog ref={dialog} className="lr-share-help" aria-labelledby={titleId} onClose={() => {+ copyRequest.current = null;+ setOpen(false);+ }}>{open && <><header><h2 id={titleId}>{vis && <Glyph name="alert" size={20} />}<span className={vis ? "visually-hidden" : undefined}>{t("shareHelp.title")}</span></h2><button type="button" className="lr-btn is-dense is-quiet" aria-label={t("common.close")} autoFocus onClick={() => dialog.current?.close()}>@@ -50,8 +55,10 @@ export function ShareFailureHelp({ status, label, code, checks }: {<button type="button" className="lr-btn" aria-label={t(copied === "done" ? "shareHelp.copied" : "shareHelp.copy")} onClick={() => {// Deliberately excludes URLs, invitation credentials and raw exceptions.const version = import.meta.env.VITE_PIIK_VERSION ?? "development";+ const request = copyRequest.current = {};void Promise.resolve().then(() => navigator.clipboard.writeText(`Piik Web ${version}\n${label}\n${code}`)).then(- () => setCopied("done"), () => setCopied("failed"));+ () => { if (copyRequest.current === request) setCopied("done"); },+ () => { if (copyRequest.current === request) setCopied("failed"); });}}><Glyph name={copied === "done" ? "check" : "copy"} size={16} />{!vis && t(copied === "done" ? "shareHelp.copied" : "shareHelp.copy")}</button></nav><span role="status">{copied === "failed" ? t("shareHelp.copyFailed") : ""}</span>diff --git a/src/client/components/living/ViewerPlaybackSettings.tsx b/src/client/components/living/ViewerPlaybackSettings.tsxindex 83c02a9b..e64ee256 100644--- a/src/client/components/living/ViewerPlaybackSettings.tsx+++ b/src/client/components/living/ViewerPlaybackSettings.tsx@@ -58,7 +58,7 @@ export function ViewerPlaybackSettings({ route, relayVolunteer, overlaySettings,: preference === "auto" ? "hint-route-auto" : preference === "p2p" ? "hint-route-p2p" : "hint-route-sfu"}hintText={t(locked ?? `playback.routeHint.${preference}`)}onClick={() => { if (route.preference !== preference) route.onChange(preference); }}>- {vis && <Glyph name={preference === "auto" ? "branch" : preference === "p2p" ? "link" : "server"} size={17} />}+ {vis && <Glyph name={preference === "auto" ? "branch" : preference === "p2p" ? "network" : "server"} size={17} />}<Cap k={`playback.route.${preference}`} /></Chip>;})}</RowGroup>diff --git a/src/client/components/living/pawn.css b/src/client/components/living/pawn.cssindex 98d1bacf..210d4e16 100644--- a/src/client/components/living/pawn.css+++ b/src/client/components/living/pawn.css@@ -1,9 +1,39 @@.lr-person { overflow: visible; }.lr-person-body { transform-origin: 24px 48px; }.lr-person-eyes { transform-origin: 24px 14.1px; }+/* Keep the same SVG parts mounted so a second toggle reverses the gesture. */+.lr-person-headset, .lr-person-collaboration { opacity: 0; }+.lr-person-status[data-voice] .lr-person-headset,+.lr-person-status[data-collaboration] .lr-person-collaboration { opacity: 1; }+.lr-person-collaboration { transform: translateY(2px) scale(.94); transform-origin: 24px 40px; }+.lr-person-status[data-collaboration] .lr-person-collaboration { transform: none; }+.lr-person-neckband { opacity: 0; transform: translateY(-14px); }+.lr-person-status[data-deafened] .lr-person-neckband { opacity: 1; transform: none; }+.lr-person-headband { opacity: 1; }+.lr-person-status[data-deafened] .lr-person-headband { opacity: 0; transform: translateY(14px); }+.lr-person-status[data-deafened] .lr-person-earcup-left { transform: translate(.2px, 14px); }+.lr-person-status[data-deafened] .lr-person-earcup-right { transform: translate(-.2px, 14px); }+.lr-person-mic-position { transform: translate(33px, 17px); --mic-fold-angle: 140deg; }+.lr-person-status[data-deafened] .lr-person-mic-position { transform: translate(33px, 28px); --mic-fold-angle: 50deg; }+.lr-person-mic-open { transform: rotate(var(--mic-fold-angle)); opacity: 0; }+.lr-person-status[data-microphone] .lr-person-mic-head { transform: none; opacity: 1; }+.lr-person-status[data-microphone][data-deafened] .lr-person-mic-head { opacity: 0; }+.lr-person-status[data-microphone][data-deafened] .lr-person-mic-neck { transform: none; opacity: 1; }+.lr-person-status[data-microphone] .lr-person-mic-folded { transform: rotate(calc(-1 * var(--mic-fold-angle))); opacity: 0; }+.lr-person-mic-tip { transform: translate(2px, -8px); }+.lr-person-status[data-microphone] .lr-person-mic-tip { transform: translate(-6px, 4.5px); }+.lr-person-status[data-microphone][data-deafened] .lr-person-mic-tip { transform: translate(-6px, -7px); }@media (prefers-reduced-motion: no-preference) {- .lr-person[data-motion] .lr-person-body { animation: lr-person-lean var(--person-duration) ease-in-out infinite; }- .lr-person[data-motion] .lr-person-eyes { animation: lr-person-eyes var(--person-duration) ease-in-out infinite; }+ :where(html:not([data-reduced-motion])) {+ .lr-person-headset, .lr-person-collaboration { transition: opacity 180ms ease, transform 220ms ease-out; }+ .lr-person-headband, .lr-person-neckband,+ .lr-person-earcup-left, .lr-person-earcup-right, .lr-person-mic-position,+ .lr-person-mic-open, .lr-person-mic-folded, .lr-person-mic-tip {+ transition: transform 260ms ease-in-out, opacity 180ms ease;+ }+ .lr-person[data-motion] .lr-person-body { animation: lr-person-lean var(--person-duration) ease-in-out infinite; }+ .lr-person[data-motion] .lr-person-eyes { animation: lr-person-eyes var(--person-duration) ease-in-out infinite; }+ }}@keyframes lr-person-lean {0%, 36%, 43%, 100% { transform: none; }diff --git a/src/client/components/living/segmented-control.css b/src/client/components/living/segmented-control.cssindex 976f71a1..62de60b5 100644--- a/src/client/components/living/segmented-control.css+++ b/src/client/components/living/segmented-control.css@@ -65,8 +65,9 @@.lr-toggle input:focus-visible + span { outline: 3px solid var(--action); outline-offset: 3px; }:is(.lr-toggle, .lr-lang) button.is-selected,.lr-toggle input:checked + span { color: var(--control-selected-ink); }+/* Labels stay centred on the track's selection surface, including sticky touch hover. */:is(.lr-toggle, .lr-lang) button:is(:hover, :focus-visible):not(:disabled),-.lr-toggle input:enabled:is(:hover, :focus-visible) + span { transform: translateY(-1px); }+.lr-toggle input:enabled:is(:hover, :focus-visible) + span { transform: none; }:is(.lr-toggle, .lr-lang) button:is(:hover, :focus-visible):not(:disabled):not(.is-selected),.lr-toggle input:enabled:not(:checked):is(:hover, :focus-visible) + span { background: var(--control-hover); color: var(--ink); }:is(.lr-toggle, .lr-lang) button:active:not(:disabled),diff --git a/src/client/components/living/video-fullscreen.ts b/src/client/components/living/video-fullscreen.tsindex 15fbadea..313ab381 100644--- a/src/client/components/living/video-fullscreen.ts+++ b/src/client/components/living/video-fullscreen.ts@@ -3,6 +3,8 @@ import { leavePictureInPicture } from "./use-picture-in-picture";export type FullscreenVideo = HTMLVideoElement & {webkitEnterFullscreen?: () => void;webkitExitFullscreen?: () => void;+ webkitEnterFullScreen?: () => void;+ webkitExitFullScreen?: () => void;webkitSupportsFullscreen?: boolean;webkitDisplayingFullscreen?: boolean;webkitPresentationMode?: string;@@ -11,11 +13,12 @@ export type FullscreenVideo = HTMLVideoElement & {export function videoFullscreenState(video: FullscreenVideo) {const screen = video.parentElement;const pageSupported = Boolean(document.fullscreenEnabled && screen?.requestFullscreen);- const nativeSupported = typeof video.webkitEnterFullscreen === "function";+ const nativeSupported = typeof (video.webkitEnterFullscreen ?? video.webkitEnterFullScreen) === "function";const nativeActive = Boolean(video.webkitDisplayingFullscreen || video.webkitPresentationMode === "fullscreen");return {supported: pageSupported || nativeSupported,- ready: pageSupported || (nativeSupported && video.readyState >= 1 && video.webkitSupportsFullscreen === true),+ // Native players need not expose Safari's optional readiness property.+ ready: pageSupported || (nativeSupported && video.readyState >= 1 && video.webkitSupportsFullscreen !== false),active: Boolean(screen && document.fullscreenElement === screen) || nativeActive,nativeActive,};@@ -29,13 +32,13 @@ export async function toggleVideoFullscreen(video: FullscreenVideo): Promise<voiif (screen && document.fullscreenElement === screen) {await document.exitFullscreen();} else if (state.nativeActive) {- video.webkitExitFullscreen?.();+ (video.webkitExitFullscreen ?? video.webkitExitFullScreen)?.call(video);} else if (state.ready) {if (document.fullscreenEnabled && screen?.requestFullscreen) {await screen.requestFullscreen();await leavePictureInPicture(video).catch(() => undefined);} else {- video.webkitEnterFullscreen?.();+ (video.webkitEnterFullscreen ?? video.webkitEnterFullScreen)?.call(video);}}}diff --git a/src/client/lib/debug-webrtc.ts b/src/client/lib/debug-webrtc.tsindex 7dcb49c3..dbc9afc1 100644--- a/src/client/lib/debug-webrtc.ts+++ b/src/client/lib/debug-webrtc.ts@@ -1,4 +1,4 @@-import { browserDebugEnabled, debugError, debugEvent } from "./debug";+import { browserDebugEnabled, debugError, debugEvent, registerDebugCollector } from "./debug";import { isPredictedCandidateFoundation } from "../../shared/nat-candidate";const connections = new WeakMap<RTCPeerConnection, object>();@@ -9,9 +9,15 @@ const statsTypes = new Set([// Observe existing connection owners and their existing statistics samples.// These listeners add no polling or transport actions.-export function observeDebugConnection(connection: RTCPeerConnection, identity: object): void {- if (!browserDebugEnabled) return;+export function observeDebugConnection(connection: RTCPeerConnection, identity: object): () => void {+ if (!browserDebugEnabled) return () => {};connections.set(connection, identity);+ const live = () => connection.connectionState !== "closed";+ const release = registerDebugCollector("webrtc", async () => {+ if (!live()) return;+ const report = await connection.getStats();+ if (live()) debugRtcStats(connection, report);+ });const transports = new Set<RTCDtlsTransport>();const snapshot = (event: string) => {try {@@ -36,6 +42,19 @@ export function observeDebugConnection(connection: RTCPeerConnection, identity:errorCode: event.errorCode, errorText: event.errorText, address: event.address, port: event.port, url: event.url });});snapshot("created");+ return release;+}++export function observeDebugPlayback(video: HTMLVideoElement, identity: object): () => void {+ return registerDebugCollector("playback", () => {+ const quality = video.getVideoPlaybackQuality?.();+ debugEvent("playback", "snapshot", { ...identity,+ width: video.videoWidth, height: video.videoHeight, readyState: video.readyState,+ paused: video.paused, ended: video.ended, visibility: document.visibilityState,+ totalVideoFrames: quality?.totalVideoFrames ?? null,+ droppedVideoFrames: quality?.droppedVideoFrames ?? null,+ });+ });}export function debugRtcStats(connection: RTCPeerConnection, report: RTCStatsReport): void {diff --git a/src/client/lib/debug.ts b/src/client/lib/debug.tsindex a5c20bba..782f973e 100644--- a/src/client/lib/debug.ts+++ b/src/client/lib/debug.ts@@ -147,6 +147,16 @@ function snapshot(): BrowserDebugEvent[] {return structuredClone(events.map(({ record }) => record));}+const exportCollectors = new Set<{ name: string; collect: () => void | Promise<void> }>();++// Live resource owners supply a current snapshot only when a report is requested.+export function registerDebugCollector(name: string, collect: () => void | Promise<void>): () => void {+ if (!browserDebugEnabled) return () => {};+ const entry = { name, collect };+ exportCollectors.add(entry);+ return () => { exportCollectors.delete(entry); };+}+export async function exportBrowserDebug(): Promise<string> {const buildEnvironment = (import.meta as ImportMeta & { env?: Record<string, string> }).env;const collectorErrors: object[] = [];@@ -167,6 +177,16 @@ export async function exportBrowserDebug(): Promise<string> {} finally { clearTimeout(timer); }}} catch (error) { collectorErrors.push({ collector: "environment", error: errorDetails(error) }); }+ await Promise.all([...exportCollectors].map(async ({ name, collect }) => {+ let timer: ReturnType<typeof setTimeout> | undefined;+ try {+ await Promise.race([+ Promise.resolve().then(collect),+ new Promise<never>((_, reject) => { timer = setTimeout(() => reject(new Error("Diagnostic snapshot timed out")), 1_000); }),+ ]);+ } catch (error) { collectorErrors.push({ collector: name, error: errorDetails(error) }); }+ finally { clearTimeout(timer); }+ }));const history = snapshot();for (const record of history) {if (record.event === "collector-failed") collectorErrors.push({ at: record.at, ...record.details });diff --git a/src/client/lib/room-interactions.ts b/src/client/lib/room-interactions.tsindex 3f5ef4f7..69b54130 100644--- a/src/client/lib/room-interactions.ts+++ b/src/client/lib/room-interactions.ts@@ -42,6 +42,7 @@ export class RoomInteractionSession {private cooldownTimer: ReturnType<typeof setTimeout> | undefined;private effectsTimer: ReturnType<typeof setTimeout> | undefined;private overlayVisible = false;+ private requestId: string | null = null;private closed = false;private subscriptionStartedAt: number | null = null;private serverOffset = 0;@@ -85,7 +86,8 @@ export class RoomInteractionSession {return true;}if (event.type === "room-interaction-rejected") {- if (event.requestId === this.state.pending?.requestId) {+ if (event.requestId === this.requestId) {+ this.requestId = null;clearTimeout(this.pendingTimer);this.update({ pending: null, error: event.reason });}@@ -102,7 +104,8 @@ export class RoomInteractionSession {// lifetime beyond one full effect, and never replay an expired effect.const beganAt = Math.min(now, message.occurredAt - this.serverOffset);const own = message.sender.peerId === this.state.peerId;- const confirmsPending = own && message.requestId === this.state.pending?.requestId;+ const confirmsPending = own && message.requestId === this.requestId;+ if (confirmsPending) this.requestId = null;const confirmed = confirmsPending ? message : this.state.confirmed;const pending = confirmsPending ? null : this.state.pending;if (!pending) clearTimeout(this.pendingTimer);@@ -144,6 +147,9 @@ export class RoomInteractionSession {this.update({ error: "offline" });return false;}+ // The timeout releases the send control, not this request's identity.+ // A late result may settle it until another submission or connection replaces it.+ this.requestId = requestId;this.update({ pending: { requestId, payload }, coolingDown: true, error: null });this.pendingTimer = setTimeout(() => this.update({ pending: null, error: "unconfirmed" }), 5000);this.cooldownTimer = setTimeout(() => this.update({ coolingDown: false }), INTERACTION_INTERVAL_MS);@@ -188,6 +194,7 @@ export class RoomInteractionSession {}disconnected() {+ this.requestId = null;this.subscriptionStartedAt = null;this.serverOffset = 0;clearTimeout(this.pendingTimer);diff --git a/src/client/lib/signaling.ts b/src/client/lib/signaling.tsindex 069122f8..dede2a64 100644--- a/src/client/lib/signaling.ts+++ b/src/client/lib/signaling.ts@@ -196,8 +196,12 @@ export class SignalingClient {}setViewerRoutePreference(preference: ViewerRoutePreference): boolean {- return this.identity.role === "viewer" && this.identity.routePreference !== undefined &&- this.send({ type: "set-viewer-route-preference", preference });+ if (this.identity.role !== "viewer" || this.identity.routePreference === undefined ||+ !this.send({ type: "set-viewer-route-preference", preference })) return false;+ // Replies describe applied policy; reconnect retains the personal request,+ // including when its acknowledgement is lost or an older reply arrives late.+ this.identity.routePreference = preference;+ return true;}get relayVolunteerRequest(): Readonly<{ enabled: boolean; capacity: number }> | null {@@ -502,9 +506,6 @@ export class SignalingClient {this.flushAudioPresence();this.flushCoPlayPresence();}- if (message.type === "viewer-route-preference" && this.identity.role === "viewer") {- this.identity.routePreference = message.preference;- }if ((message.type === "sharing-started" || message.type === "sharing-start-failed") &&this.pendingShareGeneration === message.shareGeneration) {this.pendingShareGeneration = null;diff --git a/src/client/locales/en.ts b/src/client/locales/en.tsindex d41b0061..9c7ec3ae 100644--- a/src/client/locales/en.ts+++ b/src/client/locales/en.ts@@ -65,7 +65,7 @@ export const en: Record<CopyKey, string> = {"remote.mode.room": "Room Viewers","remote.mode.password": "Password","remote.help.ask": "Decide whether to allow each control request.",- "remote.help.room": "Any room Viewer can take control when no one else has it.",+ "remote.help.room": "Room Viewers can take control without individual approval.","remote.help.password": "Viewers can control this computer after entering the control password.","remote.policyLifetime": "Applies to this shared source only. Changing sources or stopping turns remote control off.","remote.password": "Control password",diff --git a/src/client/locales/zh.ts b/src/client/locales/zh.tsindex aa01a27a..e57487ef 100644--- a/src/client/locales/zh.ts+++ b/src/client/locales/zh.ts@@ -65,7 +65,7 @@ export const zh = {"remote.mode.room": "房间观众","remote.mode.password": "控制密码","remote.help.ask": "收到请求后,由你决定是否允许控制。",- "remote.help.room": "无人控制时,房间观众可直接操作电脑。",+ "remote.help.room": "房间观众无需逐次批准,即可操作电脑。","remote.help.password": "观众输入控制密码后,可操作这台电脑。","remote.policyLifetime": "仅当前分享来源有效;换源或停播后关闭远控。","remote.password": "控制密码",diff --git a/src/client/media/browser-encoding-pool.ts b/src/client/media/browser-encoding-pool.tsindex 8736cb8f..2551d98b 100644--- a/src/client/media/browser-encoding-pool.ts+++ b/src/client/media/browser-encoding-pool.ts@@ -274,8 +274,8 @@ export class BrowserEncodingPool {member.encoderStats = createStatsAccumulator(); member.previousOutput = undefined;member.carrier = true;member.replacement = member.configureCarrier().then((applied) => {- if (!member.disposed && !applied) this.failSource(member.source);- }).catch(() => this.failSource(member.source)).finally(() => { member.replacement = undefined; });+ if (!applied) this.fallback(member);+ }).catch(() => this.fallback(member)).finally(() => { member.replacement = undefined; });this.prune();}, (metadata) => {if (member.disposed || member.pending !== pending) return false;diff --git a/src/client/media/browser-encoding-producer.ts b/src/client/media/browser-encoding-producer.tsindex 4f034315..e0ed1418 100644--- a/src/client/media/browser-encoding-producer.ts+++ b/src/client/media/browser-encoding-producer.ts@@ -20,6 +20,7 @@ export class BrowserEncodingProducer {readonly id = createOpaqueId();private send: RTCPeerConnection | null = null;private receive: RTCPeerConnection | null = null;+ private readonly releaseDebug: Array<() => void> = [];private input: MediaStreamTrack | null = null;private videoSender: RTCRtpSender | null = null;private video: HTMLVideoElement | null = null;@@ -62,8 +63,8 @@ export class BrowserEncodingProducer {track.addEventListener("ended", this.fail);const send = this.send = new RTCPeerConnection({ iceServers: [], encodedInsertableStreams: true } as RTCConfiguration);const receive = this.receive = new RTCPeerConnection({ iceServers: [] });- observeDebugConnection(send, { producerId: this.id, role: "pool-producer" });- observeDebugConnection(receive, { producerId: this.id, role: "pool-local-receiver" });+ this.releaseDebug.push(observeDebugConnection(send, { producerId: this.id, role: "pool-producer" }));+ this.releaseDebug.push(observeDebugConnection(receive, { producerId: this.id, role: "pool-local-receiver" }));const transceiver = send.addTransceiver(track, { direction: "sendonly", streams: [new MediaStream([track])] });this.videoSender = transceiver.sender;const streams = encodedStreams(transceiver.sender), writer = streams.writable.getWriter();@@ -180,6 +181,8 @@ export class BrowserEncodingProducer {dispose(): void {if (this.disposed) return;this.disposed = true;+ for (const release of this.releaseDebug) release();+ this.releaseDebug.length = 0;this.clearStartupTimer();debugEvent("encoding-pool", "producer-retired", { producerId: this.id });this.source.removeEventListener("ended", this.fail);diff --git a/src/client/native/client-connection.ts b/src/client/native/client-connection.tsnew file mode 100644index 00000000..d403d08f--- /dev/null+++ b/src/client/native/client-connection.ts@@ -0,0 +1,73 @@+import type { NativeClient } from "./client";++// One page/session owns one lazy App control connection. Acquisition and+// retirement share the same identity, including results arriving after release.+export class NativeClientConnection {+ private client: NativeClient | null = null;+ private pending: { promise: Promise<NativeClient | null>; controller: AbortController } | null = null;+ private stopWatching: (() => void) | null = null;++ constructor(private readonly connect: (signal: AbortSignal) => Promise<NativeClient | null>,+ private readonly disconnected: () => void = () => {}) {}++ get current(): NativeClient | null { return this.client; }++ acquire(beforeConnect?: Promise<void>): Promise<NativeClient | null> {+ if (this.pending) return this.pending.promise;+ if (!beforeConnect && this.client) return Promise.resolve(this.client);+ const controller = new AbortController();+ const discovery: Promise<NativeClient | null> = beforeConnect+ ? beforeConnect.then(() => {+ if (this.pending !== connecting) return null;+ return this.client ?? this.connect(controller.signal);+ })+ : this.connect(controller.signal);+ const promise = discovery.then(client => {+ if (this.pending !== connecting) {+ client?.close();+ return null;+ }+ this.pending = null;+ if (client) this.adopt(client);+ return this.client;+ }).finally(() => {+ if (this.pending === connecting) this.pending = null;+ });+ const connecting = { promise, controller };+ this.pending = connecting;+ return promise;+ }++ adopt(client: NativeClient): void {+ if (this.client === client) return;+ this.release();+ this.client = client;+ this.stopWatching = client.onClose(() => {+ if (this.client !== client) return;+ this.client = null;+ this.stopWatching = null;+ this.cancelDiscovery();+ this.disconnected();+ });+ }++ discard(client: NativeClient): void {+ if (this.client === client) this.release();+ else client.close();+ }++ release(): void {+ this.cancelDiscovery();+ this.stopWatching?.();+ this.stopWatching = null;+ const client = this.client;+ this.client = null;+ client?.close();+ }++ private cancelDiscovery(): void {+ const pending = this.pending;+ this.pending = null;+ pending?.controller.abort();+ }+}diff --git a/src/client/native/client.ts b/src/client/native/client.tsindex 7337de05..1e3d532d 100644--- a/src/client/native/client.ts+++ b/src/client/native/client.ts@@ -56,10 +56,14 @@ interface NativeDiscoveryOptions {signal?: AbortSignal;}+function isLoopbackPage(): boolean {+ return ["localhost", "127.0.0.1", "[::1]"].includes(window.location.hostname);+}+async function ungrantedLocalPermission(): Promise<PermissionState | null> {// App Local already runs on loopback. Its permission can say "prompt" even// though loopback-to-loopback requests do not need consent.- if (["localhost", "127.0.0.1", "[::1]"].includes(window.location.hostname)) return null;+ if (isLoopbackPage()) return null;for (const name of ["loopback-network", "local-network-access"]) {try {const { state } = await navigator.permissions.query({ name: name as PermissionName });@@ -165,7 +169,7 @@ async function discoverNative<T>(const response = await fetch(`http://127.0.0.1:${port}/${path}`, {cache: "no-store",signal: signal ? AbortSignal.any([signal, controller.signal]) : controller.signal,- targetAddressSpace: "loopback",+ ...(isLoopbackPage() ? {} : { targetAddressSpace: "loopback" }),} as RequestInit);if (!response.ok) throw new Error("Piik App discovery was not accepted");const body: unknown = await response.json();@@ -210,7 +214,7 @@ export async function notifyNativePresentation(headers: { "Content-Type": "application/json" },body: JSON.stringify({ language }),signal,- targetAddressSpace: "loopback",+ ...(isLoopbackPage() ? {} : { targetAddressSpace: "loopback" }),} as RequestInit);}@@ -341,7 +345,7 @@ export class NativeClient {...shareInput,...(excludeAudio ? { excludeAudio } : {}),// Keep a stable output for source-audio toggles during sharing.- ...(this.health.nativeMedia.microphone ? { microphoneMixing: true } : {}),+ ...(this.health.nativeMedia.microphone || this.health.nativeMedia.shareVolume ? { microphoneMixing: true } : {}),...(this.health.nativeMedia.remoteInput ? { remoteInput: true } : {}),...(this.health.nativeMedia.captureBorderControl ? { showCaptureBorder } : {}),},diff --git a/src/client/native/media-bridge.ts b/src/client/native/media-bridge.tsindex 026b9d93..5ca6c993 100644--- a/src/client/native/media-bridge.ts+++ b/src/client/native/media-bridge.ts@@ -52,6 +52,7 @@ export class NativeMediaBridge {private disconnectTimer: number | null = null;private completeNegotiation: (() => void) | null = null;private rejectNegotiation: ((error: Error) => void) | null = null;+ private releaseDebug: (() => void) | undefined;constructor(private readonly shareId: string,@@ -65,7 +66,7 @@ export class NativeMediaBridge {try {this.stream = new MediaStream();this.peer = peer = new RTCPeerConnection();- observeDebugConnection(this.peer, { connectionId: this.connectionId, sourceConnectionId, shareId, role: "native-preview" });+ this.releaseDebug = observeDebugConnection(this.peer, { connectionId: this.connectionId, sourceConnectionId, shareId, role: "native-preview" });} catch (error) {peer?.close();debugError("native-bridge", "initialization-failed", error);@@ -129,6 +130,7 @@ export class NativeMediaBridge {dispose(): void {if (this.disposed) return;this.disposed = true;+ this.releaseDebug?.();this.remoteInput.dispose();this.lifetime.abort();this.rejectNegotiation?.(new NativeMediaBridgeError("Native media bridge failed"));diff --git a/src/client/native/native-sender-peer.ts b/src/client/native/native-sender-peer.tsindex d93f762b..c05b58a8 100644--- a/src/client/native/native-sender-peer.ts+++ b/src/client/native/native-sender-peer.ts@@ -101,8 +101,8 @@ export class NativeSenderPeer implements HostMediaPeer {onQuality: (quality) => {const known = quality.state !== "unknown";const format = this.source?.format?.();- const width = quality.width || format?.width || null;- const height = quality.height || format?.height || null;+ const width = quality.width || null;+ const height = quality.height || null;this.snapshot = {...this.snapshot,metrics: {@@ -113,10 +113,8 @@ export class NativeSenderPeer implements HostMediaPeer {trackIdentifier: quality.trackIdentifier,videoEncodingCount: 1,activeVideoEncodingCount: this.isConnected() ? 1 : 0,- captureWidth: width,- captureHeight: height,- captureFramesPerSecond: quality.framesPerSecond,- mediaSourceFramesPerSecond: quality.framesPerSecond,+ captureWidth: format?.width || null,+ captureHeight: format?.height || null,bitrateKbps: quality.bitrateKbps,availableOutgoingKbps: known? quality.availableOutgoingKbps@@ -132,7 +130,8 @@ export class NativeSenderPeer implements HostMediaPeer {codecProfile: null,codecParameters: null,powerEfficientEncoder: this.source ? null : codec === "h264",- intervalFramesEncoded: quality.intervalFramesEncoded,+ // The published wire name counts this output's forwarded RTP frames.+ intervalFramesSent: quality.intervalFramesEncoded,qualityLimitationReason: quality.reason,nativeEdgeQualityState: quality.state,},diff --git a/src/client/native/native-viewer-peer.ts b/src/client/native/native-viewer-peer.tsindex 5a3cc996..b03b5c5d 100644--- a/src/client/native/native-viewer-peer.ts+++ b/src/client/native/native-viewer-peer.ts@@ -21,7 +21,7 @@ import {import {iceServersWithNatPrediction,isNativeNatSurveyCandidate,- NatPredictionCandidateBatch,+ NatPredictionCandidateEmitter,natPredictionSurveyUrls,type SignalCandidate,} from "../webrtc/nat-prediction";@@ -236,7 +236,7 @@ class NativeViewerPeer implements ViewerMediaPeer {private parentPeerId: string | null = null;private connectionId: string | null = null;private bridge: NativeMediaBridge | null = null;- private localCandidates: { batch: NatPredictionCandidateBatch; iceConfig: IceConfig } | null = null;+ private localCandidates: { emitter: NatPredictionCandidateEmitter; iceConfig: IceConfig } | null = null;private unsubscribe: (() => void) | null = null;private unsubscribeClose: (() => void) | null = null;private snapshot: PeerSnapshot | null = null;@@ -381,7 +381,7 @@ class NativeViewerPeer implements ViewerMediaPeer {this.disposed = true;this.clearTimers();this.disposeBridge();- this.localCandidates?.batch.discard();+ this.localCandidates?.emitter.discard();this.localCandidates = null;this.unsubscribe?.();this.unsubscribe = null;@@ -411,7 +411,8 @@ class NativeViewerPeer implements ViewerMediaPeer {if (identityChanged) {const previous = this.connectionId;this.disposeBridge();- this.localCandidates?.batch.discard();+ this.localCandidates?.emitter.discard();+ this.localCandidates = null;if (previous) {await this.client.closeReceiver(this.sessionId, previous).catch(() => undefined,@@ -422,7 +423,6 @@ class NativeViewerPeer implements ViewerMediaPeer {} else if (!retainSource) {this.disposeBridge();}- this.localCandidates?.batch.discard();if (!retainSource) {this.state = "connecting";this.failureNotified = false;@@ -469,10 +469,12 @@ class NativeViewerPeer implements ViewerMediaPeer {candidate,});};- const candidates = predictionEnabled- ? new NatPredictionCandidateBatch(sendCandidate)- : null;- this.localCandidates = candidates ? { batch: candidates, iceConfig: nativeIceConfig } : null;+ if (!this.localCandidates) {+ this.localCandidates = {+ emitter: new NatPredictionCandidateEmitter(this.natPredictionEnabled, sendCandidate),+ iceConfig: nativeIceConfig,+ };+ } else this.localCandidates.iceConfig = nativeIceConfig;this.emit();const pending = { bridge: retainSource ? this.bridge : null, bridgeFailed: false };this.pendingOffer = pending;@@ -578,13 +580,9 @@ class NativeViewerPeer implements ViewerMediaPeer {}if (event.type === "edge-candidate") {if (this.localCandidates) {- const { batch, iceConfig } = this.localCandidates;- if (event.candidate) {- batch.add(- event.candidate,- isNativeNatSurveyCandidate(event.candidate, iceConfig.iceServers, iceConfig.natPredictionStunUrls),- );- } else batch.complete();+ const { emitter, iceConfig } = this.localCandidates;+ emitter.addSignal(event.candidate,+ isNativeNatSurveyCandidate(event.candidate, iceConfig.iceServers, iceConfig.natPredictionStunUrls));} else if (this.parentPeerId && this.connectionId) {this.events.sendSignal(this.parentPeerId, {kind: "candidate",diff --git a/src/client/native/wire.ts b/src/client/native/wire.tsindex 6bd87768..011f42d0 100644--- a/src/client/native/wire.ts+++ b/src/client/native/wire.ts@@ -1,6 +1,6 @@import { z } from "zod";import { remoteControlModeSchema, remoteInputPlatformSchema, remoteShortcutKeySchema } from "../remote-control/protocol";-import { sfuMediaSchema, positiveUint32Schema, qualityResolutionSchema, punchPredictionSchema,+import { iceCandidateSchema, sfuMediaSchema, positiveUint32Schema, qualityResolutionSchema, punchPredictionSchema,nullableSafeEvidenceNumber, nullableSafeEvidenceInteger } from "../../shared/protocol";export const NATIVE_CLIENT_PROTOCOL = 10;@@ -42,7 +42,7 @@ export const nativeHealthSchema = nativeDiscoveryIdentitySchema.extend({maxVideoFramerate: z.number().positive().max(Number.MAX_SAFE_INTEGER).optional(),maxVideoBitrate: positiveUint32Schema.optional(),receiverReuse: z.boolean().default(false),- maxRelayChildren: z.number().int().min(1).max(20).optional(),+ maxRelayChildren: positiveUint32Schema.optional(),remoteInput: z.boolean().optional(),remoteStopShortcut: z.boolean().optional(),remoteCoPlay: z.boolean().optional(),@@ -123,15 +123,6 @@ const nativeAdapterSchema = z.strict();export type NativeAdapter = z.infer<typeof nativeAdapterSchema>;-const candidateSchema = z- .object({- candidate: z.string().max(4096),- sdpMid: z.string().max(128).nullable().optional(),- sdpMLineIndex: z.number().int().min(0).max(255).nullable().optional(),- usernameFragment: z.string().max(256).nullable().optional(),- })- .strict();-const responseBase = {version: z.literal(NATIVE_CLIENT_PROTOCOL),id: opaqueIdentifierSchema,@@ -347,7 +338,7 @@ export const nativeEventSchema = z.discriminatedUnion("type", [type: z.literal("publication-candidate"),publicationGeneration: opaqueIdentifierSchema,connectionId: opaqueIdentifierSchema,- candidate: candidateSchema.nullable(),+ candidate: iceCandidateSchema.nullable(),}).strict(),z@@ -378,7 +369,7 @@ export const nativeEventSchema = z.discriminatedUnion("type", [...eventBase,type: z.literal("edge-candidate"),connectionId: opaqueIdentifierSchema,- candidate: candidateSchema.nullable(),+ candidate: iceCandidateSchema.nullable(),}).strict(),z@@ -456,4 +447,4 @@ export const nativeEventSchema = z.discriminatedUnion("type", []);export type NativeClientEvent = z.infer<typeof nativeEventSchema>;-export type NativeIceCandidate = z.infer<typeof candidateSchema>;+export type NativeIceCandidate = z.infer<typeof iceCandidateSchema>;diff --git a/src/client/pages/ControlsPreview.tsx b/src/client/pages/ControlsPreview.tsxindex 21b26531..4703a47c 100644--- a/src/client/pages/ControlsPreview.tsx+++ b/src/client/pages/ControlsPreview.tsx@@ -82,7 +82,7 @@ export function ControlsPreview() {const [invalid, setInvalid] = useState(false);const [sourceOpen, setSourceOpen] = useState(displayPreview);const [sourceRevision, setSourceRevision] = useState(0);- const [sourceState, setSourceState] = useState<NativeSourceList["kind"] | "empty">("ready");+ const [sourceState, setSourceState] = useState<NativeSourceList["kind"] | "refreshing" | "empty">("ready");const [captureBorderAvailable, setCaptureBorderAvailable] = useState(true);const [showCaptureBorder, setShowCaptureBorder] = useState(false);const [roomCode, setRoomCode] = useState("6020");@@ -279,9 +279,9 @@ export function ControlsPreview() {<section id="source-preview" className="cp-section"><header className="cp-section-head"><span className="cp-number">06</span><div><h2>{en ? "Choose a picture" : "挑一块画面。"}</h2><p>{en ? "The actual source selector, with sample windows and cameras. Selection only updates this preview." : "正式的画面选择器,放入了示例窗口和摄像头;选择只影响这张预览。"}</p></div></header>- <div className="cp-tools">{(["ready", "loading", "unavailable", "incompatible", "unsupported", "failed", "empty"] as const).map((value, index) => <Chip key={value}+ <div className="cp-tools">{(["ready", "loading", "refreshing", "unavailable", "incompatible", "unsupported", "failed", "empty"] as const).map((value, index) => <Chip key={value}title={value} selected={sourceState === value} onClick={() => { setSourceState(value); setSourceOpen(true); }}>- {en ? ["Available", "Loading", "App unavailable", "Update needed", "Capture unavailable", "Read failed", "Empty list"][index] : ["正常", "读取中", "App 未连接", "需要更新", "无法采集", "读取失败", "空列表"][index]}</Chip>)}+ {en ? ["Available", "Loading", "Refreshing", "App unavailable", "Update needed", "Capture unavailable", "Read failed", "Empty list"][index] : ["正常", "读取中", "刷新中", "App 未连接", "需要更新", "无法采集", "读取失败", "空列表"][index]}</Chip>)}<SwitchItem checked={captureBorderAvailable} onChange={setCaptureBorderAvailable}label={en ? "Windows border control" : "Windows 边框控制"} /><a className="lr-btn" href="/__tooltip-preview?source=display#source-preview" onClick={() => {@@ -294,7 +294,8 @@ export function ControlsPreview() {<div className="cp-stage"><StageTv hasEntry label={t("host.sourcePicker.title")}><img className="cp-poster" src={POSTER} alt="" />{sourceOpen ? <CaptureSourcePicker key={sourceRevision} nativeSources={sourceState === "empty" ? { ...SOURCES, sources: [], captureBorderControl: captureBorderAvailable }- : sourceState === "ready" ? { ...SOURCES, captureBorderControl: captureBorderAvailable } : { kind: sourceState }}+ : sourceState === "ready" ? { ...SOURCES, captureBorderControl: captureBorderAvailable }+ : sourceState === "refreshing" ? { kind: "loading", appDetected: true } : { kind: sourceState }}initialShowCaptureBorder={showCaptureBorder}initialTab={displayPreview ? "display" : "window"}onBrowser={() => { setSourceOpen(false); notify(); }}@@ -302,7 +303,7 @@ export function ControlsPreview() {loadCameras={previewCameras}onAppConnected={() => setSourceState("ready")}onNative={(_target, _audio, showBorder) => { setShowCaptureBorder(showBorder); setSourceOpen(false); notify(); }}- onPreview={previewSource} onRefresh={() => setSourceState("ready")} onCancel={() => setSourceOpen(false)} />+ onPreview={previewSource} onRefresh={() => setSourceState("refreshing")} onCancel={() => setSourceOpen(false)} />: <div className="cp-stage-action"><Btn icon="cast" tone="primary" title="host.start" cap="host.start" hint="hint-share-start" onClick={() => setSourceOpen(true)} /></div>}</StageTv></div></section>diff --git a/src/client/pages/HostPage.tsx b/src/client/pages/HostPage.tsxindex e279f0ea..4e864d81 100644--- a/src/client/pages/HostPage.tsx+++ b/src/client/pages/HostPage.tsx@@ -44,6 +44,7 @@ import { browserCaptureDevices } from "../media/capture-devices";import { ShareMicrophoneSettings } from "../components/living/ShareMicrophoneSettings";import {CaptureSourcePicker,+ nativeAppDetected,type NativeSourceList,} from "../components/living/CaptureSourcePicker";import { PawnDetail } from "../components/living/PawnDetail";@@ -104,6 +105,7 @@ import { readPreferredQuality, savePreferredQuality } from "../lib/quality-prefeimport { AppQualityPreference } from "../native/quality-preference";import { createOpaqueId } from "../lib/opaque-id";import { debugError, debugEvent, debugOperation } from "../lib/debug";+import { observeDebugPlayback } from "../lib/debug-webrtc";import {defaultHostDisplayName,readDisplayName,@@ -177,6 +179,7 @@ import type {} from "../types";import { HostPeer, type HostMediaPeer } from "../webrtc/host-peer";import { NativeClient, NativeCompatibilityError, NativeRequestError } from "../native/client";+import { NativeClientConnection } from "../native/client-connection";import {NativeSenderPeer,shouldUseBrowserQualityCandidate,@@ -407,7 +410,7 @@ export function HostPage({const [nativeActive, setNativeActive] = useState(false);const [remoteControlState, setRemoteControlState] = useState<RemoteControlState | null>(null);const remoteStopShortcut = useCallback(async (key?: number) => {- const client = nativeClientRef.current;+ const client = nativeConnection.current;const shareId = nativeShareGenerationRef.current;if (!client || !shareId) throw new Error("Native share is unavailable");return client.remoteStopShortcut(shareId, key);@@ -630,22 +633,21 @@ export function HostPage({);const codecProbeAbortRef = useRef<AbortController | null>(null);const sharingPausedRef = useRef(false);+ const sharingPauseOperationRef = useRef<object | null>(null);const retiringStreamRef = useRef<MediaStream | null>(null);const hostSfuRouteRef = useRef<HostSfuRoute | null>(null);- const nativeClientRef = useRef<NativeClient | null>(null);- const nativeClientConnectRef = useRef<{ promise: Promise<NativeClient | null>; controller: AbortController } | null>(null);+ const [nativeConnection] = useState(() => new NativeClientConnection(connectNativeClient, handleNativeClientClosed));const nativeShareGenerationRef = useRef<string | null>(null);const serverShareGenerationRef = useRef<string | null>(null);const nativeMediaBridgeRef = useRef<NativeMediaBridge | null>(null);const nativeInputRoute = useMemo(() => new InputRoute(() => {}), []);const nativeMediaIngressRef = useRef<NativeMediaIngress | null>(null);const nativeEventCleanupRef = useRef<(() => void) | null>(null);- const nativeClientCloseCleanupRef = useRef<(() => void) | null>(null);const nativeModeRef = useRef(false);const nativeSourceAudioRef = useRef<boolean | undefined>(undefined);const nativeSourceSelectionRef = useRef<Extract<ShareSourceSelection, { kind: "native" }> | null>(null);const loadAudioApplications = useCallback(async () => {- const client = nativeClientRef.current;+ const client = nativeConnection.current;if (!client || !nativeModeRef.current) throw new Error("Piik App is unavailable");return client.sources();}, []);@@ -699,6 +701,15 @@ export function HostPage({}}, [stream]);+ useEffect(() => {+ const video = videoRef.current;+ if (!stream || !video) return;+ return observeDebugPlayback(video, {+ role: "host-preview", generation: activeGenerationRef.current,+ source: nativeModeRef.current ? "native" : "browser",+ });+ }, [stream]);+useEffect(() => {let senderQualitySuspended = false;const resetSenderQualityAuthority = () => {@@ -762,6 +773,7 @@ export function HostPage({generationRef.current += 1;roomMutationRef.current = null;sourceSwitchRef.current = null;+ sharingPauseOperationRef.current = null;qualityChangeRef.current = null;pendingQualityChangeRef.current = null;codecProbeAbortRef.current?.abort();@@ -790,12 +802,7 @@ export function HostPage({nativeSourceRequestRef.current = null;nativeSourcePathRef.current = null;disposeNativeShare();- nativeClientCloseCleanupRef.current?.();- nativeClientCloseCleanupRef.current = null;- nativeClientConnectRef.current?.controller.abort();- nativeClientConnectRef.current = null;- nativeClientRef.current?.close();- nativeClientRef.current = null;+ nativeConnection.release();hostAudioRef.current?.dispose();hostAudioRef.current = null;streamRef.current?.getTracks().forEach((track) => track.stop());@@ -868,7 +875,7 @@ export function HostPage({onDisconnected, onStats,};if (nativeModeRef.current) {- const client = nativeClientRef.current;+ const client = nativeConnection.current;const shareId = nativeShareGenerationRef.current;const ice = iceConfigRef.current;if (!client || !shareId || !ice) throw new Error("Native publication source is unavailable");@@ -927,6 +934,7 @@ export function HostPage({function disposeResources(notifyServer: boolean, keepRoomSession = false): void {closeCaptureSourcePicker();+ sharingPauseOperationRef.current = null;sourceSwitchRef.current = null;qualityChangeRef.current = null;pendingQualityChangeRef.current = null;@@ -1312,7 +1320,7 @@ export function HostPage({await nativeShareCleanupRef.current;await nativePreviewTailRef.current;if (!isCurrentShare(generation, shareGeneration)) return null;- if (nativeClientRef.current !== client) throw new NativeRequestError("unavailable", "start-share");+ if (nativeConnection.current !== client) throw new NativeRequestError("unavailable", "start-share");// Acquire the required Browser peer before starting capture. Pending// cancellation owns this bridge, but still closes control to abort startup.bridge = new NativeMediaBridge(@@ -1338,7 +1346,7 @@ export function HostPage({audioAvailableRef.current = false;nativeSourceSelectionRef.current = selection;nativeEventCleanup = client.onEvent((event) => {- if (event.shareId !== shareGeneration || !isCurrentShare(generation, shareGeneration) || nativeClientRef.current !== client) return;+ if (event.shareId !== shareGeneration || !isCurrentShare(generation, shareGeneration) || nativeConnection.current !== client) return;if (event.type === "remote-control-state") {setRemoteControlState(event);signalRef.current?.setCoPlayGrants(event.coPlay ? event.requests.flatMap(request =>@@ -1379,16 +1387,16 @@ export function HostPage({if (!isCurrentShare(generation, shareGeneration)) {if (nativeMediaBridgeRef.current === bridge) nativeMediaBridgeRef.current = null;bridge.dispose();- await client.stopShare(shareGeneration).catch(() => discardNativeClient(client));+ await client.stopShare(shareGeneration).catch(() => nativeConnection.discard(client));return null;}- if (nativeClientRef.current !== client) throw new NativeRequestError("unavailable", "start-share");+ if (nativeConnection.current !== client) throw new NativeRequestError("unavailable", "start-share");nativeSourceAudioRef.current ??= started.sourceAudio ?? started.audio;audioAvailableRef.current = nativeSourceAudioRef.current || audioAvailableRef.current;videoCodecRef.current = manualVideoCodecPreference(started.codec);// Register ownership before waiting for the local bridge. A native edge// may fail immediately after becoming ready.- ownNativeClient(client);+ nativeConnection.adopt(client);nativeShareGenerationRef.current = shareGeneration;nativeModeRef.current = true;if (client.health.nativeMedia.remoteInput) await client.setRemoteInputPeers(shareGeneration, remoteInputPeersRef.current);@@ -1404,7 +1412,7 @@ export function HostPage({nativeEventCleanup?.();if (nativeEventCleanupRef.current === nativeEventCleanup) nativeEventCleanupRef.current = null;if (- nativeClientRef.current === client &&+ nativeConnection.current === client &&nativeShareGenerationRef.current === shareGeneration) {disposeNativeShare();@@ -1414,72 +1422,37 @@ export function HostPage({if (shareStarted) {nativeShareCleanupRef.current = client.stopShare(shareGeneration)- .catch(() => discardNativeClient(client));+ .catch(() => nativeConnection.discard(client));} else {- discardNativeClient(client);+ nativeConnection.discard(client);}}throw error;}}- function ownNativeClient(client: NativeClient): void {- if (nativeClientRef.current === client) return;- nativeClientCloseCleanupRef.current?.();- nativeClientRef.current = client;- nativeClientCloseCleanupRef.current = client.onClose(() => {- if (nativeClientRef.current !== client) return;- nativeClientRef.current = null;- nativeClientConnectRef.current?.controller.abort();- nativeClientConnectRef.current = null;- nativeClientCloseCleanupRef.current = null;- if (nativeSourceRequestRef.current) {- nativeSourcePathRef.current = null;- setNativeSources({ kind: "unavailable" });- }- if (nativeMediaIngressRef.current) {- recoverBrowserFanout(nativeMediaIngressRef.current);- } else if (nativeModeRef.current && activeGenerationRef.current !== null) {- endSharing({ key: "native.fail.controlDisconnected", failureCode: "app/control/disconnected" }, true, "route-failed", "bad");- }- });- }-- function discardNativeClient(client: NativeClient): void {- if (nativeClientRef.current === client) {- nativeClientCloseCleanupRef.current?.();- nativeClientCloseCleanupRef.current = null;- nativeClientRef.current = null;- nativeClientConnectRef.current?.controller.abort();- nativeClientConnectRef.current = null;+ function handleNativeClientClosed(): void {+ if (nativeSourceRequestRef.current) {+ nativeSourcePathRef.current = null;+ setNativeSources({ kind: "unavailable" });+ }+ if (nativeMediaIngressRef.current) {+ recoverBrowserFanout(nativeMediaIngressRef.current);+ } else if (nativeModeRef.current && activeGenerationRef.current !== null) {+ endSharing({ key: "native.fail.controlDisconnected", failureCode: "app/control/disconnected" }, true, "route-failed", "bad");}- client.close();}- function acquireNativeClient(): Promise<NativeClient | null> {- if (nativeClientConnectRef.current) return nativeClientConnectRef.current.promise;- const controller = new AbortController();- const promise: Promise<NativeClient | null> = nativeShareCleanupRef.current.then(() =>- nativeClientConnectRef.current === connecting- ? nativeClientRef.current ?? NativeClient.connect({ signal: controller.signal })- : null,- ).catch((error) => {+ function connectNativeClient(signal: AbortSignal): Promise<NativeClient | null> {+ return NativeClient.connect({ signal }).catch((error) => {debugError("native", "discovery-failed", error);if (error instanceof NativeCompatibilityError) throw error;return null;- }).then((client) => {- if (nativeClientConnectRef.current !== connecting) {- client?.close();- return null;- }- if (client) ownNativeClient(client);- return client;- }).finally(() => {- if (nativeClientConnectRef.current === connecting) nativeClientConnectRef.current = null;});- const connecting = { promise, controller };- nativeClientConnectRef.current = connecting;- return promise;+ }++ function acquireNativeClient(): Promise<NativeClient | null> {+ return nativeConnection.acquire(nativeShareCleanupRef.current);}function releaseUnusedNativeClient(): void {@@ -1487,10 +1460,7 @@ export function HostPage({// needs to keep it after startup. Merely visiting the page owns no session.if (nativeSourceRequestRef.current || nativeShareGenerationRef.current ||(activeGenerationRef.current !== null && roomMutationRef.current !== null)) return;- nativeClientConnectRef.current?.controller.abort();- nativeClientConnectRef.current = null;- const client = nativeClientRef.current;- if (client) discardNativeClient(client);+ nativeConnection.release();}function closeCaptureSourcePicker(): void {@@ -1513,7 +1483,7 @@ export function HostPage({setNativeSources({ kind: "browser" });return;}- setNativeSources({ kind: "loading" });+ setNativeSources(previous => ({ kind: "loading", appDetected: nativeAppDetected(previous) }));let client: NativeClient | null;try {@@ -1550,7 +1520,7 @@ export function HostPage({codec,client.health.nativeMedia.softwareVP8,);- if (nativeSourceRequestRef.current !== request || nativeClientRef.current !== client) {+ if (nativeSourceRequestRef.current !== request || nativeConnection.current !== client) {return;}if (!path) {@@ -1568,10 +1538,10 @@ export function HostPage({captureBorderControl: client.health.nativeMedia.captureBorderControl,});} catch (error) {- if (nativeSourceRequestRef.current !== request || nativeClientRef.current !== client) return;+ if (nativeSourceRequestRef.current !== request || nativeConnection.current !== client) return;debugError("native", "source-list-failed", error);if (!nativeShareGenerationRef.current) {- discardNativeClient(client);+ nativeConnection.discard(client);}setNativeSources({ kind: "failed" });}@@ -1584,10 +1554,10 @@ export function HostPage({): Promise<void> {if (!launchedByClient || videoCodecRef.current.primary !== "h264" ||!routePolicyRef.current.topologyOptimization) return;- const client = nativeClientRef.current;+ const client = nativeConnection.current;if (!client || !isCurrentShare(generation, shareGeneration)) return;await nativeShareCleanupRef.current;- if (nativeClientRef.current !== client || !isCurrentShare(generation, shareGeneration)) return;+ if (nativeConnection.current !== client || !isCurrentShare(generation, shareGeneration)) return;const ingress = new NativeMediaIngress(shareGeneration, client, () => {recoverBrowserFanout(ingress);}, () => qualitySettingsRef.current);@@ -1633,12 +1603,12 @@ export function HostPage({target: NativeCaptureTarget,signal?: AbortSignal,): Promise<string | null> {- const client = nativeClientRef.current;+ const client = nativeConnection.current;const request = nativeSourceRequestRef.current;if (!client || !request || nativeSources?.kind !== "ready") return null;const owns = () => !signal?.aborted &&nativeSourceRequestRef.current === request &&- nativeClientRef.current === client;+ nativeConnection.current === client;// Keep thumbnails from filling the same control queue used to start media.const preview = nativePreviewTailRef.current.then(async () => {if (!owns()) return null;@@ -1660,7 +1630,7 @@ export function HostPage({excludeAudio?: NativeCaptureTarget,): void {if (nativeSources?.kind !== "ready") return;- const client = nativeClientRef.current;+ const client = nativeConnection.current;const path = nativeSourcePathRef.current;if (!client || !path) return;setShowCaptureBorder(showCaptureBorder);@@ -1673,7 +1643,7 @@ export function HostPage({function disposeNativeShare(expectedShare = nativeShareGenerationRef.current): void {if (nativeShareGenerationRef.current !== expectedShare) return;- const client = nativeClientRef.current;+ const client = nativeConnection.current;const shareGeneration = nativeShareGenerationRef.current;const ingress = nativeMediaIngressRef.current;nativeMediaIngressRef.current = null;@@ -1697,7 +1667,7 @@ export function HostPage({nativeShareCleanupRef.current = (ingress? client.stopReceive(shareGeneration): client.stopShare(shareGeneration))- .catch(() => discardNativeClient(client));+ .catch(() => nativeConnection.discard(client));releaseUnusedNativeClient();}@@ -1838,7 +1808,7 @@ export function HostPage({screenAudioBitrate(previousProfile.maxAudioBitrate) !==screenAudioBitrate(nextProfile.maxAudioBitrate);try {- const nativeClient = nativeClientRef.current;+ const nativeClient = nativeConnection.current;const nativeShareGeneration = nativeShareGenerationRef.current;const nativeUpdate = nativeModeRef.current? nativeClient && nativeShareGeneration@@ -1979,7 +1949,7 @@ export function HostPage({shareVolumeChangeRef.current = token;const volume = muted ? 0 : level;const update = nativeModeRef.current && shareId- ? nativeClientRef.current!.setShareVolume(shareId, volume)+ ? nativeConnection.current!.setShareVolume(shareId, volume): Promise.resolve(hostAudioRef.current?.setVolume(volume));void update.then(() => {if (isCurrentGeneration(generation) && shareVolumeChangeRef.current === token) setShareVolume({ level, muted });@@ -1990,7 +1960,7 @@ export function HostPage({function toggleSharingPause(): void {const activeStream = streamRef.current;- const nativeClient = nativeClientRef.current;+ const nativeClient = nativeConnection.current;const nativeShareGeneration = nativeShareGenerationRef.current;if (phase !== "live" ||@@ -2001,16 +1971,20 @@ export function HostPage({if (nativeModeRef.current && nativeClient && nativeShareGeneration) {const generation = activeGenerationRef.current;if (generation === null) return;+ const operation = {};+ sharingPauseOperationRef.current = operation;+ const owns = () => isCurrentGeneration(generation) && sharingPauseOperationRef.current === operation;const nextPaused = !sharingPausedRef.current;void nativeClient.setPaused(nativeShareGeneration, nextPaused)- .then(() => {- if (!isCurrentGeneration(generation)) return;+ .then(async () => {+ if (!owns()) return;const sent = signalRef.current?.setSharingPaused(nextPaused) === true;if (!sent && !nextPaused) {// Mirror the Browser branch: a resume the server did not hear// rolls back to paused and restores the wire intent.- void nativeClient.setPaused(nativeShareGeneration, true);+ await nativeClient.setPaused(nativeShareGeneration, true);+ if (!owns()) return;signalRef.current?.confirmSharingPaused();setNoticeErrorKey("host.pause.signalRecovering", "signal-recovering", undefined, "warn");return;@@ -2032,7 +2006,7 @@ export function HostPage({}}).catch((error: unknown) => {- if (isCurrentGeneration(generation)) {+ if (owns()) {setNoticeError(error, "connection");}});@@ -2144,7 +2118,7 @@ export function HostPage({generation: number,): boolean {const stream = streamRef.current;- const nativeClient = nativeClientRef.current;+ const nativeClient = nativeConnection.current;const nativeShareGeneration = nativeShareGenerationRef.current;const iceConfig = iceConfigRef.current;const signal = signalRef.current;@@ -2277,7 +2251,7 @@ export function HostPage({return;}const activeStream = streamRef.current;- const nativeClient = nativeClientRef.current;+ const nativeClient = nativeConnection.current;const nativeShareGeneration = nativeShareGenerationRef.current;const useNative = nativeClient !== null && nativeShareGeneration !== null;const iceConfig = iceConfigRef.current;@@ -2524,8 +2498,9 @@ export function HostPage({if (message.shareGeneration !== shareGenerationRef.current) {return;}+ sharingPauseOperationRef.current = null;const activeStream = streamRef.current;- const nativeClient = nativeClientRef.current;+ const nativeClient = nativeConnection.current;const nativeShareGeneration = nativeShareGenerationRef.current;if (nativeModeRef.current && nativeClient && nativeShareGeneration) {void nativeClient@@ -2675,7 +2650,7 @@ export function HostPage({if (signalRef.current !== signal || !isCurrentRoomAuthority(activeRoom)) return;if (message.type === "remote-input-peers") {remoteInputPeersRef.current = message.peers;- const client = nativeClientRef.current, shareId = nativeShareGenerationRef.current;+ const client = nativeConnection.current, shareId = nativeShareGenerationRef.current;if (client?.health.nativeMedia.remoteInput && nativeModeRef.current && shareId) {void client.setRemoteInputPeers(shareId, message.peers).catch(error => debugError("native", "input-endpoints", error));}@@ -3029,7 +3004,7 @@ export function HostPage({generation === null ||!shareGeneration ||!isCurrentGeneration(generation) ||- nativeClientRef.current !== client ||+ nativeConnection.current !== client ||sourceSwitchRef.current ||qualityChangeRef.current) {@@ -3048,7 +3023,7 @@ export function HostPage({if (!isCurrentGeneration(generation) ||sourceSwitchRef.current !== token ||- nativeClientRef.current !== client+ nativeConnection.current !== client) return;// Preserve an exclusion request even on failure: the App mutes source// audio before replacing it, and the next picker must not default to all.@@ -3064,7 +3039,7 @@ export function HostPage({if (!isCurrentGeneration(generation) ||sourceSwitchRef.current !== token ||- nativeClientRef.current !== client+ nativeConnection.current !== client) {return;}@@ -3084,7 +3059,7 @@ export function HostPage({if (!isCurrentGeneration(generation) ||sourceSwitchRef.current !== token ||- nativeClientRef.current !== client+ nativeConnection.current !== client) {return;}@@ -3109,7 +3084,7 @@ export function HostPage({if (generation === null || commentaryOperationRef.current) return;const operation = {}; commentaryOperationRef.current = operation; setCommentaryBusy(true);try {- const client = nativeClientRef.current, shareId = nativeShareGenerationRef.current;+ const client = nativeConnection.current, shareId = nativeShareGenerationRef.current;if (nativeActive && client && shareId) await client.setMicrophone(shareId, enabled, input.volume, input.deviceId);else if (hostAudioRef.current) await hostAudioRef.current.setMicrophone(enabled, input);else throw new Error("Shared audio is unavailable");@@ -3129,7 +3104,7 @@ export function HostPage({function changeCommentaryVolume(volume: number): void {setCommentaryInput(previous => ({ ...previous, volume }));const generation = activeGenerationRef.current;- const client = nativeClientRef.current, shareId = nativeShareGenerationRef.current;+ const client = nativeConnection.current, shareId = nativeShareGenerationRef.current;if (nativeActive && client && shareId) void client.setMicrophoneVolume(shareId, volume).catch(error => {if (generation !== null && isCurrentGeneration(generation)) setNoticeError(error, "sourceAudio");});@@ -3250,7 +3225,7 @@ export function HostPage({if (ingress) {try {if (ingress.hasAudio !== (captured.getAudioTracks().length > 0)) {- const client = nativeClientRef.current;+ const client = nativeConnection.current;if (!client) throw new Error("Native media ingress is unavailable");const replacement = new NativeMediaIngress(ingress.shareId, client, () => {recoverBrowserFanout(replacement);@@ -3281,6 +3256,10 @@ export function HostPage({recoverBrowserFanout(ingress);}}+ if (!isCurrentGeneration(generation) || sourceSwitchRef.current !== token) {+ captured.getTracks().forEach((track) => track.stop());+ return;+ }const activeSfuRoute = hostSfuRouteRef.current;const provisional = hostProvisionalChildRef.current;const [replacements, , sfuReplaced] = await Promise.all([@@ -3727,9 +3706,9 @@ export function HostPage({useDocumentTitle([room?.roomId, titleContent.label, hostStatus.titleMarker], titleContent.variations,`${lang}:${vis}:${hostStatus.titleFrameKey}:${room?.roomId ?? ""}`);const audioBitrateCeiling = Math.min(MAX_AUDIO_SEND_BITRATE, nativeActive- ? nativeClientRef.current?.health.nativeMedia.maxAudioBitrate ?? MAX_AUDIO_SEND_BITRATE+ ? nativeConnection.current?.health.nativeMedia.maxAudioBitrate ?? MAX_AUDIO_SEND_BITRATE: MAX_AUDIO_SEND_BITRATE);- const videoLimits = videoCaptureLimits(nativeActive ? nativeClientRef.current?.health.nativeMedia : undefined);+ const videoLimits = videoCaptureLimits(nativeActive ? nativeConnection.current?.health.nativeMedia : undefined);return (<div className="lr-app">@@ -3774,7 +3753,7 @@ export function HostPage({) : null}{stream && <PlaybackControls videoRef={videoRef} stream={stream}sharingAudio={{ ...shareVolume, available: nativeActive- ? !!nativeClientRef.current?.health.nativeMedia.shareVolume : hostAudioRef.current?.commentaryAvailable === true,+ ? !!nativeConnection.current?.health.nativeMedia.shareVolume : hostAudioRef.current?.commentaryAvailable === true,onChange: changeShareVolume }}audioAvailable={audioAvailableRef.current}canPlay={phase === "live" && !nativeSources && !sharingPaused && !localPreviewPaused}@@ -3928,24 +3907,24 @@ export function HostPage({gamepad={remoteControlState.gamepad}simultaneous={remoteControlState.simultaneous}onSimultaneous={remoteControlState.simultaneous !== undefined ? async enabled => {- const client = nativeClientRef.current;+ const client = nativeConnection.current;const shareId = nativeShareGenerationRef.current;if (!client || !shareId) throw new Error("Native share is unavailable");await client.setRemoteControlSimultaneous(shareId, enabled);} : undefined}- onShortcut={nativeClientRef.current?.health.nativeMedia.remoteStopShortcut ? remoteStopShortcut : undefined}+ onShortcut={nativeConnection.current?.health.nativeMedia.remoteStopShortcut ? remoteStopShortcut : undefined}disabled={sharingPaused || switchingSource}requests={remoteControlState.requests.map(request => {return { ...request, name: viewers.find(viewer => viewer.peerId === request.peerId)?.label ?? t("common.name.viewerDefault") };})}onPolicy={async (mode, password) => {- const client = nativeClientRef.current;+ const client = nativeConnection.current;const shareId = nativeShareGenerationRef.current;if (!client || !shareId) throw new Error("Native share is unavailable");await client.setRemoteControlPolicy(shareId, mode, password);}}onDecide={async (id, allow) => {- const client = nativeClientRef.current;+ const client = nativeConnection.current;const shareId = nativeShareGenerationRef.current;if (!client || !shareId) throw new Error("Native share is unavailable");await client.setRemoteControl(shareId, id, allow);@@ -4046,7 +4025,7 @@ export function HostPage({</div></>}audio={<>- {phase === "live" && (!nativeActive || nativeClientRef.current?.health.nativeMedia.microphone) ?+ {phase === "live" && (!nativeActive || nativeConnection.current?.health.nativeMedia.microphone) ?<ShareMicrophoneSettings enabled={commentary}browser={!nativeActive}available={nativeActive || hostAudioRef.current?.commentaryAvailable === true}@@ -4056,12 +4035,12 @@ export function HostPage({volume={commentaryInput.volume} onVolume={changeCommentaryVolume}deviceId={commentaryInput.deviceId}onDevice={deviceId => { void changeCommentary(true, { ...commentaryInput, deviceId }); }}- loadDevices={() => nativeActive && nativeClientRef.current- ? nativeClientRef.current.microphones() : browserCaptureDevices("audioinput")}+ loadDevices={() => nativeActive && nativeConnection.current+ ? nativeConnection.current.microphones() : browserCaptureDevices("audioinput")}voiceProcessing={nativeActive ? undefined : commentaryInput.voiceProcessing}onVoiceProcessing={voiceProcessing => { void changeCommentary(true, { ...commentaryInput, voiceProcessing }); }} /> : null}{phase === "live" && nativeActive && nativeSourceSelectionRef.current?.target.kind === "display" &&- nativeClientRef.current?.health.nativeMedia.microphone && nativeClientRef.current.health.nativeMedia.processAudioExclusion ? (+ nativeConnection.current?.health.nativeMedia.microphone && nativeConnection.current.health.nativeMedia.processAudioExclusion ? (<div className="lr-door-group lr-source-audio-settings"><SwitchItem checked={nativeSourceSelectionRef.current.audio}label={t("host.sourcePicker.systemAudio")} hint={nativeSourceSelectionRef.current.audio ? "hint-stop-audio" : "hint-share-audio"}diff --git a/src/client/pages/PawnAppearancePreview.tsx b/src/client/pages/PawnAppearancePreview.tsxindex b7fb703c..8c0d0901 100644--- a/src/client/pages/PawnAppearancePreview.tsx+++ b/src/client/pages/PawnAppearancePreview.tsx@@ -1,3 +1,4 @@+import { useState } from "react";import { PawnSvg, type PawnState } from "../components/living/Pawn";import { participantColor } from "../components/living/participant-color";import { SwitchItem } from "../components/living/primitives";@@ -21,6 +22,11 @@ export function PawnAppearancePreview({ guides, onGuides }: {}) {const { lang } = useCopy();const en = lang === "en";+ const [voice, setVoice] = useState(true);+ const [microphone, setMicrophone] = useState(true);+ const [deafened, setDeafened] = useState(false);+ const [collaboration, setCollaboration] = useState(true);+ const [host, setHost] = useState(false);return <div id="pawn-appearance-preview" className="cp-pawn-preview" data-guides={guides || undefined}><div className="cp-tools"><h3>{en ? "Participant states" : "人物状态"}</h3>@@ -28,6 +34,22 @@ export function PawnAppearancePreview({ guides, onGuides }: {</div><p>{en ? "The microphone boom unfolds when unmuted and folds up when muted. The mouse uses the remote-control toolbar symbol.": "开麦时伸出麦克风杆,闭麦时折起。鼠标复用播控同款图标。"}</p>+ <div className="cp-pawn-motion">+ <div className="cp-tools">+ <SwitchItem checked={voice} onChange={setVoice} label={en ? "Voice" : "语音"} />+ <SwitchItem checked={microphone} onChange={setMicrophone} label={en ? "Microphone" : "麦克风"} />+ <SwitchItem checked={deafened} onChange={setDeafened} label={en ? "Deafened" : "闭耳机"} />+ <SwitchItem checked={collaboration} onChange={setCollaboration} label={en ? "Collaborating" : "协作中"} />+ <SwitchItem checked={host} onChange={setHost} label={en ? "Host" : "房主"} />+ </div>+ <div className="cp-pawn-motion-figures">+ {[false, true].map(large => <figure key={String(large)} className={large ? "is-large" : undefined}>+ <span className="cp-pawn-figure"><PawnSvg color={participantColor(SAMPLE_ID)} identity={SAMPLE_ID}+ host={host} voice={voice ? { microphone, deafened } : undefined} collaboration={collaboration} /></span>+ <figcaption>{large ? "2×" : "48px"}</figcaption>+ </figure>)}+ </div>+ </div><div className="cp-pawn-samples">{SAMPLES.map(({ label, state }, index) => <figure key={index}><span className="cp-pawn-figure"><PawnSvg color={participantColor(SAMPLE_ID)} identity={SAMPLE_ID} {...state} /></span>diff --git a/src/client/pages/StatusPreviewPage.tsx b/src/client/pages/StatusPreviewPage.tsxindex 0e0c445a..09400255 100644--- a/src/client/pages/StatusPreviewPage.tsx+++ b/src/client/pages/StatusPreviewPage.tsx@@ -20,6 +20,7 @@ import { composeDocumentTitle, useDocumentTitle } from "../ui/document-title";import { HOST_STATUS_SCENARIOS, STATUS_SCENARIOS } from "./status-preview-scenarios";import "./status-preview.css";import { usePreviewAnchor } from "./use-preview-anchor";+import { usePreviewReducedMotion } from "./use-preview-reduced-motion";function StatusMark({ status }: { status: StatusDescriptor }) {const { t, vis } = useCopy();@@ -36,6 +37,7 @@ export function StatusPreviewPage() {const { lang, vis, t, titleFrames } = useCopy();const [selected, setSelected] = useState("playing");const [reducedMotion, setReducedMotion] = useState(false);+ usePreviewReducedMotion(reducedMotion);const [couchView, setCouchView] = useState<"host" | "viewer">("viewer");const [loadingLabel, setLoadingLabel] = useState<CopyKey | "">("client.launch.starting");const scenario = STATUS_SCENARIOS.find((item) => item.id === selected)!;diff --git a/src/client/pages/TooltipPreviewPage.tsx b/src/client/pages/TooltipPreviewPage.tsxindex af2b8544..7a96b92b 100644--- a/src/client/pages/TooltipPreviewPage.tsx+++ b/src/client/pages/TooltipPreviewPage.tsx@@ -12,6 +12,7 @@ import { replaySvgAnimations } from "../ui/animation";import { MetricCells } from "../components/living/Metrics";import { PREVIEW_METRICS } from "./preview-metrics";import { usePreviewAnchor } from "./use-preview-anchor";+import { usePreviewReducedMotion } from "./use-preview-reduced-motion";const EXAMPLES: { label: string; kind: HintKind | ComicKind; tone: ComicTone; motion: ComicMotion; text: CopyKey }[] = [{ label: "说明 / Neutral", kind: "hint-volume", tone: "off", motion: "demo", text: "playback.volume" },@@ -61,6 +62,7 @@ export function TooltipPreviewPage() {const { t, lang } = useCopy();const en = lang === "en";const [reducedMotion, setReducedMotion] = useState(false);+ usePreviewReducedMotion(reducedMotion);return (<div className="lr-app" data-comic-reduced-motion={reducedMotion || undefined}><AppHeader led={<LedStrip state="live" label={t("state.signal.connected")} comic="signal-connected" />} />diff --git a/src/client/pages/ViewerPage.tsx b/src/client/pages/ViewerPage.tsxindex 0eb22f23..4e9cb7cd 100644--- a/src/client/pages/ViewerPage.tsx+++ b/src/client/pages/ViewerPage.tsx@@ -130,6 +130,7 @@ import {type ViewerRelayPeerFactory,} from "../webrtc/viewer-relay";import { NativeClient } from "../native/client";+import { NativeClientConnection } from "../native/client-connection";import {NativeCapableViewerPeer,} from "../native/native-viewer-peer";@@ -504,38 +505,19 @@ export function ViewerPage({});setRelayChildEvidence(relayChildEvidenceStore.getSnapshot());let sfuTransportConnected = false;- let nativeClientConnection: { promise: Promise<NativeClient | null>; controller: AbortController } | null = null;+ const nativeConnection = new NativeClientConnection(signal =>+ NativeClient.connect({ waitForPermission: false, signal }).catch(() => null));let nativeViewerAvailable = true;const nativeViewerSessionId = createOpaqueId();const releaseNativeClient = (): void => {- const connecting = nativeClientConnection;- nativeClientConnection = null;- connecting?.controller.abort();- void connecting?.promise.then((client) => client?.close());+ nativeConnection.release();};const acquireNativeClient = async (): Promise<NativeClient | null> => {if (!active || !clientActivationRef.current || !nativeViewerAvailable) return null;- if (!nativeClientConnection) {- const controller = new AbortController();- const promise = NativeClient.connect({ waitForPermission: false, signal: controller.signal }).catch(() => null).then((client) => {- if (!client || !active || nativeClientConnection !== connecting) {- if (nativeClientConnection === connecting) nativeClientConnection = null;- client?.close();- return null;- }- client.onClose(() => {- if (nativeClientConnection === connecting) nativeClientConnection = null;- });- return client;- });- const connecting = { promise, controller };- nativeClientConnection = connecting;- }- const connecting = nativeClientConnection;- const client = await connecting.promise;- return active && nativeViewerAvailable && nativeClientConnection === connecting ? client : null;+ const client = await nativeConnection.acquire();+ return active && nativeViewerAvailable && nativeConnection.current === client ? client : null;};function createViewerMediaPeer(diff --git a/src/client/pages/pawn-appearance-preview.css b/src/client/pages/pawn-appearance-preview.cssindex 5a8c91bf..43ad4f3e 100644--- a/src/client/pages/pawn-appearance-preview.css+++ b/src/client/pages/pawn-appearance-preview.css@@ -5,6 +5,11 @@.cp-pawn-preview figure { display: grid; justify-items: center; align-content: start; gap: 6px; margin: 0; min-width: 0; }.cp-pawn-figure { display: grid; place-items: center; width: 56px; height: 64px; border-radius: 12px; background: var(--couch); }.cp-pawn-figure .lr-person { width: 48px; height: 56px; }+.cp-pawn-motion { margin-bottom: 24px; }+.cp-pawn-motion > .cp-tools { flex-wrap: wrap; }+.cp-pawn-motion-figures { display: flex; justify-content: center; align-items: end; gap: 32px; margin-top: 16px; }+.cp-pawn-motion-figures .is-large .cp-pawn-figure { width: 120px; height: 128px; }+.cp-pawn-motion-figures .is-large .lr-person { width: 96px; height: 112px; }.cp-pawn-preview figcaption { color: var(--muted); font-size: 12px; line-height: 1.5; text-align: center; text-wrap: balance; }.cp-pawn-preview figcaption > span { display: block; word-break: keep-all; }.cp-pawn-headphone-comparison { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 24px; max-width: 680px; margin: 0 auto; }diff --git a/src/client/pages/use-preview-reduced-motion.ts b/src/client/pages/use-preview-reduced-motion.tsnew file mode 100644index 00000000..6f7785a4--- /dev/null+++ b/src/client/pages/use-preview-reduced-motion.ts@@ -0,0 +1,9 @@+import { useLayoutEffect } from "react";++/** One preview choice drives both control motion and the existing comic poses. */+export function usePreviewReducedMotion(reduced: boolean) {+ useLayoutEffect(() => {+ document.documentElement.toggleAttribute("data-reduced-motion", reduced);+ return () => document.documentElement.removeAttribute("data-reduced-motion");+ }, [reduced]);+}diff --git a/src/client/remote-control/input-session.ts b/src/client/remote-control/input-session.tsindex b5ea649a..501822bc 100644--- a/src/client/remote-control/input-session.ts+++ b/src/client/remote-control/input-session.ts@@ -24,6 +24,7 @@ export class RemoteInputSession {private capabilitiesRequested = false;private pointer: PointerInput | null = null;private pointerTimer: ReturnType<typeof setTimeout> | null = null;+ private nextPointerAt = 0;private wheelFraction = 0;constructor(private readonly channel: InputTransport, private readonly changed: (state: RemoteInputState | null) => void) {@@ -33,8 +34,13 @@ export class RemoteInputSession {}private set(state: RemoteInputState | null): void {- this.clearPointer();- this.wheelFraction = 0;+ // Capability updates do not retire input belonging to the same active grant.+ if (state?.state !== "active" || this.state?.state !== "active" ||+ state.grant !== this.state.grant || state.intent !== this.state.intent) {+ this.clearPointer();+ this.nextPointerAt = 0;+ this.wheelFraction = 0;+ }this.state = state;this.changed(state);}@@ -48,7 +54,10 @@ export class RemoteInputSession {private readonly flushPointer = (): void => {const pointer = this.pointer;this.clearPointer();- if (pointer) this.sendInput(pointer);+ if (pointer) {+ this.nextPointerAt = performance.now() + 4;+ this.sendInput(pointer);+ }};private readonly message = (event: MessageEvent): void => {@@ -124,9 +133,13 @@ export class RemoteInputSession {this.pointer = event.type === "relative" && this.pointer?.type === "relative"? { type: "relative", dx: this.pointer.dx + event.dx, dy: this.pointer.dy + event.dy }: event;- // Bound motion batching independently of rendering: a slow picture must- // not slow input too. The timer exists only while motion is pending.- if (this.pointerTimer === null) this.pointerTimer = setTimeout(this.flushPointer, 4);+ // Send idle motion immediately; batch only inside the last send's interval.+ // This remains independent of rendering and owns no idle timer.+ if (this.pointerTimer === null) {+ const delay = this.nextPointerAt - performance.now();+ if (delay <= 0) this.flushPointer();+ else this.pointerTimer = setTimeout(this.flushPointer, delay);+ }return;}if (event.type === "reset") { this.clearPointer(); this.wheelFraction = 0; }diff --git a/src/client/room-voice/RoomVoicePanel.tsx b/src/client/room-voice/RoomVoicePanel.tsxindex 344bae47..80e03141 100644--- a/src/client/room-voice/RoomVoicePanel.tsx+++ b/src/client/room-voice/RoomVoicePanel.tsx@@ -55,7 +55,7 @@ export function RoomVoiceControls({ session }: Props) {<AudioVolumeMenu volume={state.input.volume} onVolume={volume => session.setVolume(volume)}label="host.microphone.volume" icon="microphone" disabled={microphoneUnavailable || state.capturePending}><Btn icon={state.microphone ? "microphone" : "microphoneOff"} title="host.device.microphone"- size="compact" hint={microphoneUnavailable ? host ? "hint-voice-host-microphone-unavailable" : "hint-voice-microphone-unavailable"+ size="compact" draw="voice-microphone" hint={microphoneUnavailable ? host ? "hint-voice-host-microphone-unavailable" : "hint-voice-microphone-unavailable": state.capturePending ? "hint-close" : undefined}hintText={microphoneUnavailable ? "voice.error.microphoneUnavailable" : "voice.captureCancel"}pressed={state.microphone} busy={state.capturePending} disabled={!state.connected || microphoneUnavailable}@@ -68,12 +68,12 @@ export function RoomVoiceControls({ session }: Props) {<AudioVolumeMenu volume={state.listeningVolume} onVolume={volume => session.setListeningVolume(volume)}label="voice.listeningVolume" icon="headphones"><Btn icon={state.deafened ? "headphonesOff" : "headphones"} title="voice.listening"- size="compact"+ size="compact" draw="voice-listening"pressed={!state.deafened} onClick={() => session.deafen(!state.deafened)} /></AudioVolumeMenu></div>}<Btn icon={!joined ? "headphones" : host ? "stop" : "door"}- title={actionTitle} cap={actionTitle} tone={joined && host ? "danger" : undefined} size="compact"+ title={actionTitle} cap={actionTitle} tone={joined && host ? "danger" : undefined} size="compact" draw="voice-membership"hint={actionHint} hintText={actionExplanation}disabled={!state.connected || !state.room || !joined && !host && !state.room.enabled}onClick={() => { if (!joined) session.resumeAudio(); session.command({ action: joined ? "leave" : "join" }); }} />@@ -138,16 +138,16 @@ export function RoomVoicePanel({ session, names, onSelectParticipant }: Props &<PawnSvg color={participantColor(member.peerId)} identity={member.peerId} host={member.peerId === state.room?.hostPeerId} /></button></Tooltip> : <PawnSvg color={participantColor(member.peerId)} identity={member.peerId} host={member.peerId === state.room?.hostPeerId} />}<Tooltip className="lr-voice-name-tip" overflow={{ text: member.label, selector: ".lr-voice-name" }}><b><span className="lr-voice-name">{member.label}</span>{member.peerId === state.peerId && <span className={vis ? "visually-hidden" : undefined}>{`, ${t("voice.self")}`}</span>}</b></Tooltip>- <Tooltip kind={member.joined ? member.publicationId ? "hint-voice-microphone-on" : "hint-voice-microphone-off" : "hint-voice-join"}+ <Tooltip toggleOnClick kind={member.joined ? member.publicationId ? "hint-voice-microphone-on" : "hint-voice-microphone-off" : "hint-voice-join"}text={vis ? undefined : t(status.label)}>- <span className="lr-voice-member-mic" data-speaking={speaking || undefined} tabIndex={0} role="img"+ <button type="button" className="lr-voice-member-mic" data-speaking={speaking || undefined}aria-label={speaking ? `${t(status.label)} · ${t("voice.speaking")}` : t(status.label)}><Glyph name={status.icon} size={20} />{member.publicationId && <MicrophoneLevel level={levels.get(member.peerId) ?? 0} />}- </span>+ </button></Tooltip>- {status.appearance?.deafened && <Tooltip kind="hint-voice-deafen" text={vis ? undefined : t("voice.listeningPaused")}>- <span className="lr-voice-member-mic" tabIndex={0} role="img" aria-label={t("voice.listeningPaused")}><Glyph name="headphonesOff" size={18} /></span>+ {status.appearance?.deafened && <Tooltip toggleOnClick kind="hint-voice-deafen" text={vis ? undefined : t("voice.listeningPaused")}>+ <button type="button" className="lr-voice-member-mic" aria-label={t("voice.listeningPaused")}><Glyph name="headphonesOff" size={18} /></button></Tooltip>}</div>{member.peerId !== state.peerId && me?.joined && member.joined && <div className="lr-voice-member-controls">@@ -155,7 +155,7 @@ export function RoomVoicePanel({ session, names, onSelectParticipant }: Props &<AudioVolumeMenu volume={state.peerVolumes.get(member.peerId) ?? 1} onVolume={volume => session.setPeerVolume(member.peerId, volume)}label="voice.personVolume" icon="speaker" heading={member.label}><Btn icon={state.muted.has(member.peerId) ? "speakerOff" : "speaker"} size="dense"- title="voice.listenPerson"+ title="voice.listenPerson" draw="voice-person-listening"pressed={!state.muted.has(member.peerId)} onClick={() => session.muteVoice(member.peerId)} /></AudioVolumeMenu></div>}diff --git a/src/client/room-voice/browser-endpoint.ts b/src/client/room-voice/browser-endpoint.tsindex 3090d2a6..9b7c638b 100644--- a/src/client/room-voice/browser-endpoint.ts+++ b/src/client/room-voice/browser-endpoint.ts@@ -20,6 +20,7 @@ export interface VoiceConnectionMetric {type VoiceSignal = Extract<ClientMessage, { type: "room-voice-signal" }>;interface Connection {+ releaseDebug: () => void;config: VoiceConnection;pc: RTCPeerConnection;lifetime: AbortController;@@ -172,6 +173,7 @@ export class BrowserVoiceEndpoint {const prediction = config.transport === "peer" && !!this.iceConfig.natPredictionStunUrls?.length;const pc = new RTCPeerConnection({ iceServers: config.transport === "sfu" ? [] : iceServersWithNatPrediction(this.iceConfig.iceServers, prediction, this.iceConfig.natPredictionStunUrls) });const connection: Connection = { config, pc, lifetime: new AbortController(),+ releaseDebug: observeDebugConnection(pc, { connectionId: config.connectionId, role: "room-voice" }),candidates: new NatPredictionCandidateEmitter(prediction, candidate => {if (!candidate || !this.current(connection) || /\sTCP\s/i.test(candidate.candidate)) return;if (connection.localCandidates.length >= 64) { this.report(connection, "failed"); return; }@@ -180,7 +182,6 @@ export class BrowserVoiceEndpoint {queuedCandidates: [], localCandidates: [], senders: new Map(), remote: new Map(), dirty: this.isOfferer(config),requested: false, descriptionSent: false, offerRevision: 0, remoteRevision: 0, reported: null, metrics: new Map() };this.connections.set(config.connectionId, connection);- observeDebugConnection(pc, { connectionId: config.connectionId, role: "room-voice" });pc.onicecandidate = event => { if (this.current(connection)) connection.candidates.add(event.candidate); };pc.onicegatheringstatechange = () => { if (this.current(connection) && pc.iceGatheringState === "complete") connection.candidates.gatheringComplete(); };pc.onnegotiationneeded = () => { if (this.current(connection)) { connection.dirty = true; this.changed(); } };@@ -408,6 +409,7 @@ export class BrowserVoiceEndpoint {}private retire(connection: Connection): void {+ connection.releaseDebug();this.connections.delete(connection.config.connectionId);connection.metrics.clear();connection.lifetime.abort(); connection.candidates.discard(); connection.pc.close();diff --git a/src/client/room-voice/room-voice.css b/src/client/room-voice/room-voice.cssindex ad50259d..84ba8b59 100644--- a/src/client/room-voice/room-voice.css+++ b/src/client/room-voice/room-voice.css@@ -2,7 +2,7 @@.lr-voice-settings { display: grid; align-content: start; gap: 16px; }.lr-voice-panel-heading { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; color: var(--muted); font-size: 12px; }.lr-voice-mic-level { position: absolute; left: 50%; top: 50%; width: var(--control-icon-size); height: var(--control-icon-size); transform: translate(-50%, -50%); pointer-events: none; fill: var(--live); }-.lr-voice-member-mic { --control-icon-size: 20px; position: relative; display: inline-flex; flex: none; align-items: center; justify-content: center; width: 24px; height: 24px; border-radius: 6px; color: var(--muted); transition: color 140ms ease; }+.lr-voice-member-mic { --control-icon-size: 20px; position: relative; display: inline-flex; flex: none; align-items: center; justify-content: center; width: 24px; height: 24px; padding: 0; border: 0; border-radius: 6px; background: transparent; color: var(--muted); cursor: default; transition: color 140ms ease; }.lr-voice-member-mic[data-speaking] { color: var(--live); }.lr-voice-hint { display: flex; align-items: flex-start; gap: 6px; margin: 0 0 12px; color: var(--muted); font-size: 12px; line-height: 1.7; }.lr-voice-hint > svg, .lr-voice-control-error > svg { flex: none; margin-top: 2px; }@@ -25,3 +25,6 @@.lr-voice-toolbar { display: flex; flex: 1; align-items: center; gap: 6px; min-width: 0; }.lr-voice-toolbar > .lr-comic-tip-wrap { flex: none; margin-left: auto; }.lr-voice-quick-controls { display: flex; flex: none; gap: 6px; align-items: center; }+@media (prefers-reduced-motion: no-preference) {+ :where(html:not([data-reduced-motion])) .lr-voice-mic-level > rect { transition: y 140ms linear, height 140ms linear; }+}diff --git a/src/client/sfu/peer.ts b/src/client/sfu/peer.tsindex bcc03b4a..9f1b68a1 100644--- a/src/client/sfu/peer.ts+++ b/src/client/sfu/peer.ts@@ -30,6 +30,7 @@ export class SfuPeer {"kind" | "description" | "media"> | null = null;private readonly lifetime = new AbortController();+ private readonly releaseDebug: () => void;private get closed(): boolean { return this.lifetime.signal.aborted; }private signalTail: Promise<void> = Promise.resolve();@@ -37,7 +38,7 @@ export class SfuPeer {private config: SfuConnectionConfig,private readonly events: SfuPeerEvents,) {- observeDebugConnection(this.pc, { connectionId: config.connectionId,+ this.releaseDebug = observeDebugConnection(this.pc, { connectionId: config.connectionId,publicationGeneration: config.publicationGeneration, role: "sfu" });this.pc.onicecandidate = ({ candidate }) => {if (this.closed || !candidate || candidate.protocol === "tcp") return;@@ -156,6 +157,7 @@ export class SfuPeer {close(): void {if (this.closed) return;+ this.releaseDebug();this.lifetime.abort();this.pc.onicecandidate = null;this.pc.onconnectionstatechange = null;diff --git a/src/client/styles.css b/src/client/styles.cssindex 97148a03..45adc759 100644--- a/src/client/styles.css+++ b/src/client/styles.css@@ -204,13 +204,7 @@ ul { margin: 0; padding: 0; list-style: none; }/* ---------- room layout ---------- */-/* overflow-x: clip keeps invisible absolutely-positioned decor (the resting- opacity:0 comic-tip panels, up to 320px wide and centered on triggers near- the viewport edge) out of the document's scrollable overflow — at narrow- widths they otherwise inflate scrollWidth and open a blank horizontal- gutter. clip (unlike hidden) creates no scroll container and leaves- overflow-y: visible untouched. Open panels are edge-aligned via- is-start/is-end, so nothing visible is cut in practice. */+/* Clip horizontal decoration without creating a scrolling container. */.lr-app { min-height: 100dvh; display: flex; flex-direction: column; overflow-x: clip; }.lr-room {flex: 1;@@ -287,7 +281,7 @@ body.lr-theater-open .lr-top { visibility: hidden; }source-picker panels retain their content-driven size; theater is explicit. */@media (orientation: landscape) and (max-height: 500px) {.lr-room { --tv-viewport-reserve: 128px; }- .lr-room:has(.lr-host-share-controls) { --tv-viewport-reserve: 200px; }+ .lr-room:has(.lr-host-controls) { --tv-viewport-reserve: 200px; }.lr-room:not(.is-theater) .lr-tv:has(.lr-playback):not(:has(.has-entry)) {width: min(960px, 100%, calc(max(168px, 100svh - var(--tv-viewport-reserve)) * 16 / 9 + 30px));}@@ -869,7 +863,6 @@ body.lr-theater-open .lr-top { visibility: hidden; }cursor: text;font-family: var(--mono);font-size: 11px;- outline: 0;text-align: left;user-select: text;}diff --git a/src/client/webrtc/host-peer.ts b/src/client/webrtc/host-peer.tsindex 02cb317c..29777cfb 100644--- a/src/client/webrtc/host-peer.ts+++ b/src/client/webrtc/host-peer.ts@@ -100,6 +100,7 @@ export class HostPeer {| AudioSenderParameterReadback| null = null;private readonly lifetime = new AbortController();+ private readonly releaseDebug: () => void;private get disposed(): boolean { return this.lifetime.signal.aborted; }private profileRevision = 0;private negotiationEpoch = 0;@@ -154,7 +155,7 @@ export class HostPeer {stopSenderVideoTrack(this.senderVideoTrack);throw error;}- observeDebugConnection(this.connection, { connectionId, peerId, role: "send" });+ this.releaseDebug = observeDebugConnection(this.connection, { connectionId, peerId, role: "send" });this.localIceCandidates = new NatPredictionCandidateEmitter(this.natPredictionEnabled,(candidate) => this.sendIceCandidate(candidate),@@ -519,6 +520,7 @@ export class HostPeer {if (this.disposed) {return;}+ this.releaseDebug();this.lifetime.abort();retireSenderQualityEvidence(this.connectionId);this.nextNegotiationEpoch();diff --git a/src/client/webrtc/nat-prediction.ts b/src/client/webrtc/nat-prediction.tsindex 25a616cf..331a7d4c 100644--- a/src/client/webrtc/nat-prediction.ts+++ b/src/client/webrtc/nat-prediction.ts@@ -329,12 +329,12 @@ export class NatPredictionCandidateBatch {this.send(candidate);}- complete(): void {+ complete(end: SignalCandidate = null): void {if (this.completed) {return;}this.trySendPredictions();- this.send(null);+ this.send(end);this.completed = true;}@@ -367,10 +367,10 @@ function signalCandidate(candidate: RTCIceCandidate): ConcreteSignalCandidate {};}-/** Own one local ICE candidate generation for any Browser P2P role. */+/** Own local ICE generations across Browser and Native trickle delivery. */export class NatPredictionCandidateEmitter {private batch: NatPredictionCandidateBatch | null = null;- private usernameFragment: string | null = null;+ private generation: ConcreteSignalCandidate | null = null;private endSent = false;private surveyUrls = new Set<string>();@@ -392,67 +392,70 @@ export class NatPredictionCandidateEmitter {}add(candidate: RTCIceCandidate | null): void {- if (!this.enabled) {- this.send(candidate ? signalCandidate(candidate) : null);- return;- }- if (!candidate) {- this.completeBatch();- if (!this.endSent) {- this.send(null);- this.endSent = true;- }+ const serverUrl = (+ candidate as (RTCIceCandidate & { readonly url?: string | null }) | null+ )?.url;+ const candidateUrl = serverUrl ? normalizedStunUrl(serverUrl) : null;+ this.addSignal(candidate ? signalCandidate(candidate) : null,+ candidateUrl !== null && this.surveyUrls.has(candidateUrl));+ }++ /** Native trickle shares the same ICE-generation owner as Browser trickle. */+ addSignal(next: SignalCandidate, predictionEligible: boolean): void {+ if (!next || next.candidate === "") {+ // A scoped end can arrive after a newer generation started. Forward it+ // without completing that generation's prediction observations.+ if (next?.usernameFragment && this.generation?.usernameFragment &&+ next.usernameFragment !== this.generation.usernameFragment) {+ this.send(next);+ } else this.completeBatch(next);return;}- const next = signalCandidate(candidate);if (this.batch &&- this.usernameFragment &&+ this.generation?.usernameFragment &&next.usernameFragment &&- next.usernameFragment !== this.usernameFragment+ next.usernameFragment !== this.generation.usernameFragment) {// An ICE restart retires old observations; an unscoped end marker here// would end the new remote generation before its candidates arrive.this.discard();}if (!this.batch) {- this.usernameFragment = next.usernameFragment ?? null;+ this.generation = next;this.endSent = false;this.batch = new NatPredictionCandidateBatch(this.send);}- const serverUrl = (- candidate as RTCIceCandidate & { readonly url?: string | null }- ).url;- const candidateUrl = serverUrl- ? normalizedStunUrl(serverUrl)- : null;- this.batch.add(- next,- candidateUrl !== null && this.surveyUrls.has(candidateUrl),- );+ this.batch.add(next, this.enabled && predictionEligible);}gatheringComplete(): void {- if (this.enabled) {- this.completeBatch();- }+ this.completeBatch();}discard(): void {this.batch?.discard();this.batch = null;- this.usernameFragment = null;+ this.generation = null;this.endSent = false;}- private completeBatch(): void {+ private completeBatch(end: SignalCandidate = null): void {if (!this.batch) {+ if (!this.endSent && end?.usernameFragment) {+ this.send(end);+ this.generation = end;+ this.endSent = true;+ }return;}- this.batch.complete();+ // An unscoped null applies to the latest remote ICE generation. Keep a+ // delayed end attached to the candidates that actually finished gathering.+ const marker = end?.usernameFragment ? end : this.generation?.usernameFragment+ ? { ...this.generation, candidate: "" } : null;+ this.batch.complete(marker);this.batch = null;- this.usernameFragment = null;this.endSent = true;}}diff --git a/src/client/webrtc/viewer-peer.ts b/src/client/webrtc/viewer-peer.tsindex 13fbb2b6..5d68193a 100644--- a/src/client/webrtc/viewer-peer.ts+++ b/src/client/webrtc/viewer-peer.ts@@ -91,6 +91,7 @@ export class ViewerPeer implements ViewerMediaPeer {getRemoteInput(): RTCDataChannel | null { return this.remoteInput.current; }private connection: RTCPeerConnection | null = null;private connectionLifetime: AbortController | null = null;+ private releaseDebug: (() => void) | null = null;private connectionId: string | null = null;private parentPeerId: string | null = null;private remoteStream = new MediaStream();@@ -285,7 +286,7 @@ export class ViewerPeer implements ViewerMediaPeer {this.currentIceConfig.natPredictionStunUrls,),});- observeDebugConnection(connection, { connectionId, parentPeerId, role: "receive" });+ this.releaseDebug = observeDebugConnection(connection, { connectionId, parentPeerId, role: "receive" });const localIceCandidates = new NatPredictionCandidateEmitter(this.natPredictionEnabled,(candidate) => {@@ -734,6 +735,8 @@ export class ViewerPeer implements ViewerMediaPeer {}private disposeConnection(): void {+ this.releaseDebug?.();+ this.releaseDebug = null;this.remoteInput.dispose();this.clearDisconnectTimer();this.clearInitialConnectionTimer();diff --git a/src/shared/protocol.ts b/src/shared/protocol.tsindex 257b0f48..4d859982 100644--- a/src/shared/protocol.ts+++ b/src/shared/protocol.ts@@ -116,6 +116,17 @@ const activeMediaRouteUpstreamSchema = z.discriminatedUnion("kind", [sfuMediaRouteUpstreamSchema,]);+export const viewerPresenceEntrySchema = z+ .object({+ role: z.literal("viewer"),+ peerId: opaqueIdSchema,+ displayName: displayNameSchema,+ upstream: mediaRouteUpstreamSchema,+ mediaReady: z.literal(true).optional(),+ })+ .strict();+export type ViewerPresenceEntry = z.infer<typeof viewerPresenceEntrySchema>;+export const participantPresenceEntrySchema = z.discriminatedUnion("role", [z.object({@@ -125,31 +136,12 @@ export const participantPresenceEntrySchema = z.discriminatedUnion("role", [upstream: z.object({ kind: z.literal("none") }).strict(),}).strict(),- z- .object({- role: z.literal("viewer"),- peerId: opaqueIdSchema,- displayName: displayNameSchema,- upstream: mediaRouteUpstreamSchema,- mediaReady: z.literal(true).optional(),- })- .strict(),+ viewerPresenceEntrySchema,]);export type ParticipantPresenceEntry = z.infer<typeof participantPresenceEntrySchema>;-export const viewerPresenceEntrySchema = z- .object({- role: z.literal("viewer"),- peerId: opaqueIdSchema,- displayName: displayNameSchema,- upstream: mediaRouteUpstreamSchema,- mediaReady: z.literal(true).optional(),- })- .strict();-export type ViewerPresenceEntry = z.infer<typeof viewerPresenceEntrySchema>;-const tokenSchema = z.string().min(32)@@ -333,7 +325,7 @@ const sessionDescriptionSchema = z}).strict();-const iceCandidateSchema = z+export const iceCandidateSchema = z.object({candidate: z.string().max(4096),sdpMid: z.string().max(128).nullable().optional(),@@ -493,6 +485,7 @@ export const routeDemandReasonSchema = z.enum(["direct-convergence","quality-convergence","root-convergence",+ "publication-attach",]);export type RouteDemandReason = z.infer<typeof routeDemandReasonSchema>;@@ -566,12 +559,12 @@ const routeDiagnosticChildSchema = z.number().int().min(0)- .max(MAX_ENDPOINT_MEDIA_COPY_CAPACITY),+ .max(MAX_VIEWERS_PER_ROOM_LIMIT),childCount: z.number().int().min(0)- .max(MAX_ENDPOINT_MEDIA_COPY_CAPACITY),+ .max(MAX_VIEWERS_PER_ROOM_LIMIT),demandAgeMs: routeDiagnosticDurationSchema,queueWaitMs: routeDiagnosticDurationSchema,candidateStartMs: routeDiagnosticDurationSchema,diff --git a/tests/browser-debug.test.ts b/tests/browser-debug.test.tsindex 2001fe65..10025336 100644--- a/tests/browser-debug.test.ts+++ b/tests/browser-debug.test.ts@@ -20,6 +20,56 @@ afterEach(() => {});describe("opt-in Browser diagnostics", () => {+ it("collects current preview receive and playback evidence only at export, until owner retirement", async () => {+ browser();+ const debug = await import("../src/client/lib/debug");+ const rtc = await import("../src/client/lib/debug-webrtc");+ let decoded = 0;+ const connection = Object.assign(new EventTarget(), {+ connectionState: "connected", getSenders: () => [], getReceivers: () => [],+ getStats: vi.fn(async () => new Map([["video", {+ id: "video", type: "inbound-rtp", bytesReceived: 1234, framesDecoded: decoded,+ }]])),+ });+ const releaseConnection = rtc.observeDebugConnection(connection as unknown as RTCPeerConnection,+ { role: "native-preview", generation: 3 });+ const releasePlayback = rtc.observeDebugPlayback({+ videoWidth: 854, videoHeight: 480, readyState: 4, paused: false, ended: false,+ getVideoPlaybackQuality: () => ({ totalVideoFrames: 7, droppedVideoFrames: 1 }),+ } as unknown as HTMLVideoElement, { role: "host-preview", generation: 3 });+ expect(connection.getStats).not.toHaveBeenCalled();+ decoded = 8;+ const report = JSON.parse(await debug.exportBrowserDebug());+ expect(report.partial).toBe(false);+ expect(report.events).toContainEqual(expect.objectContaining({ event: "stats", details: expect.objectContaining({+ role: "native-preview", generation: 3,+ stats: [expect.objectContaining({ framesDecoded: 8, bytesReceived: 1234 })],+ }) }));+ expect(report.events).toContainEqual(expect.objectContaining({ event: "snapshot", details: expect.objectContaining({+ role: "host-preview", generation: 3, width: 854, height: 480, totalVideoFrames: 7, droppedVideoFrames: 1,+ }) }));+ connection.connectionState = "closed";+ releaseConnection(); // Browser close() need not dispatch a state-change event.+ releasePlayback();+ const retired = JSON.parse(await debug.exportBrowserDebug());+ expect(retired.events.filter((event: { event: string }) => event.event === "stats" || event.event === "snapshot")).toHaveLength(2);+ expect(connection.getStats).toHaveBeenCalledOnce();+ });++ it("bounds a stalled export snapshot without changing its media owner", async () => {+ browser();+ vi.useFakeTimers();+ const debug = await import("../src/client/lib/debug");+ const release = debug.registerDebugCollector("preview", () => new Promise(() => {}));+ const exporting = debug.exportBrowserDebug();+ await vi.advanceTimersByTimeAsync(1_000);+ const report = JSON.parse(await exporting);+ expect(report.partial).toBe(true);+ expect(report.collectorErrors).toContainEqual(expect.objectContaining({ collector: "preview" }));+ release();+ expect(vi.getTimerCount()).toBe(0);+ });+it("keeps explicit diagnostics through room and App entry without copying source credentials", async () => {const page = browser("?debug=1&token=source-secret");const { withBrowserDebug } = await import("../src/client/lib/debug");diff --git a/tests/browser-encoding-pool.test.ts b/tests/browser-encoding-pool.test.tsindex 3fb71e34..f340ed0c 100644--- a/tests/browser-encoding-pool.test.ts+++ b/tests/browser-encoding-pool.test.ts@@ -19,7 +19,7 @@ function stats(row: Record<string, unknown>, negotiated: RTCRtpCodec = codec): R]) as unknown as RTCStatsReport;}-function member(budget = 2_000_000) {+function member(budget = 2_000_000, configureCarrier = async () => true) {const demand = { budget, codec: { ...codec } as RTCRtpCodec };let producerId: string | null = null;const select = vi.fn((id: string, _requestKey: () => Promise<void>, selected: () => void,@@ -37,7 +37,7 @@ function member(budget = 2_000_000) {const connection = { connectionState: "connected", getStats: async () => stats({ targetBitrate: demand.budget }, demand.codec) };const fatal = vi.fn();const handle = pool.create(source, {} as RTCRtpSender, connection as RTCPeerConnection, profile,- encoded, async () => true, async () => undefined, fatal)!;+ encoded, configureCarrier, async () => undefined, fatal)!;return { demand, select, cancelSelection, fatal, handle, encoded, producerId: () => producerId };}@@ -86,6 +86,31 @@ afterEach(() => {});describe("Browser encoding pool ownership", () => {+ it.each(["false", "rejected"])("isolates a connection's %s carrier attachment from its shared source", async (outcome) => {+ const healthy = member();+ let finish!: (applied: boolean) => void, reject!: (error: Error) => void;+ const configure = vi.fn().mockImplementationOnce(() => new Promise<boolean>((yes, no) => { finish = yes; reject = no; }))+ .mockResolvedValue(true);+ const failed = member(2_000_000, configure);+ await sample(4);+ const producer = [...producers.keys()][0]!;+ expect(failed.producerId()).toBe(producer.id);++ if (outcome === "false") finish(false);+ else reject(new Error("connection sender retired"));+ await vi.advanceTimersByTimeAsync(0);+ expect(failed.encoded.passthrough).toHaveBeenCalledOnce();+ expect(configure).toHaveBeenCalledTimes(2);+ expect(failed.fatal).not.toHaveBeenCalled();+ expect(healthy.encoded.passthrough).not.toHaveBeenCalled();+ expect(healthy.producerId()).toBe(producer.id);+ expect(producer.dispose).not.toHaveBeenCalled();+ const later = member();+ expect(later.handle).not.toBeNull();+ await sample(3);+ expect(later.producerId()).toBe(producer.id);+ });+it.each(["joining", "active", "changed"])("isolates codec fallback for a %s member from healthy siblings", async (state) => {const healthy = member();let limited = state === "joining" ? null : member();diff --git a/tests/browser-gate-harness.test.ts b/tests/browser-gate-harness.test.tsindex 6baf4345..e1fc1a6f 100644--- a/tests/browser-gate-harness.test.ts+++ b/tests/browser-gate-harness.test.ts@@ -1,5 +1,6 @@-import { expect, it } from "vitest";+import { expect, it, vi } from "vitest";import { join } from "node:path";+import { tmpdir } from "node:os";import { launchChrome } from "../scripts/browser-gate-harness";import { parseVideoGateSize } from "../scripts/media-gate-geometry";@@ -12,7 +13,63 @@ it("accepts bounded numeric gate sizes and rejects malformed or oversized source}});+it.each(["Chrome", "shaper"])("cleans local pool startup after %s fails", async phase => {+ const { createServer } = await import("node:http");+ const servers: ReturnType<typeof createServer>[] = [];+ const cleanup = vi.fn(async (resources: { server: { close(): Promise<void> } }) => {+ await resources.server.close();+ return { browserExited: true, nativeExited: true, serverClosed: true, portsClosed: true, profileRemoved: true };+ });+ vi.doMock("node:http", () => ({ createServer: (...args: Parameters<typeof createServer>) => {+ const server = createServer(...args); servers.push(server); return server;+ } }));+ vi.doMock("node:fs/promises", () => ({ mkdir: vi.fn(), writeFile: vi.fn(), mkdtemp: async () => "fixture-profile" }));+ vi.doMock("esbuild", () => ({ build: async () => ({ outputFiles: [{ contents: Buffer.from("fixture") }] }) }));+ vi.doMock("../scripts/browser-pool-shaper.mjs", () => ({ createPoolShaper: async () => { throw new Error("shaper startup failed"); } }));+ vi.doMock("../scripts/browser-gate-harness", async () => ({+ ...await vi.importActual<typeof import("../scripts/browser-gate-harness")>("../scripts/browser-gate-harness"),+ cleanupRun: cleanup,+ launchChrome: async () => { throw Object.assign(new Error("missing Chrome"), { code: "ENOENT" }); },+ }));+ const argv = process.argv, exitCode = process.exitCode;+ const output = vi.spyOn(console, "log").mockImplementation(() => {});+ try {+ process.argv = [process.execPath, "browser-local-pool-probe.ts", "carrier", ...(phase === "shaper" ? ["--network"] : [])];+ vi.resetModules();+ await import("../scripts/browser-local-pool-probe");+ expect(cleanup).toHaveBeenCalledOnce();+ expect(servers.every(server => !server.listening)).toBe(true);+ expect(process.exitCode).toBe(1);+ } finally {+ for (const server of servers) if (server.listening) await new Promise<void>(resolve => server.close(() => resolve()));+ process.argv = argv; process.exitCode = exitCode; output.mockRestore();+ for (const module of ["node:http", "node:fs/promises", "esbuild", "../scripts/browser-gate-harness", "../scripts/browser-pool-shaper.mjs"]) vi.doUnmock(module);+ vi.resetModules();+ }+});+it("reports a missing browser executable through awaited startup", async () => {await expect(launchChrome(join(import.meta.dirname, "missing-browser.exe"), 9222,join(import.meta.dirname, "unused-profile"))).rejects.toMatchObject({ code: "ENOENT" });});++it("accepts repeated retirement of an absent Linux gate profile", async () => {+ const platform = Object.getOwnPropertyDescriptor(process, "platform")!;+ const lstat = vi.fn().mockRejectedValue(Object.assign(new Error("absent"), { code: "ENOENT" }));+ const rm = vi.fn();+ vi.doMock("node:fs/promises", () => ({ lstat, rm }));+ try {+ Object.defineProperty(process, "platform", { value: "linux" });+ vi.resetModules();+ const { cleanupRun } = await import("../scripts/browser-gate-harness");+ const resources = { cdp: null, native: null, chrome: null, server: null,+ profile: join(tmpdir(), "piik-client-media-AbCd12"), ports: [] };+ expect((await cleanupRun(resources)).profileRemoved).toBe(true);+ expect((await cleanupRun(resources)).profileRemoved).toBe(true);+ expect(rm).not.toHaveBeenCalled();+ } finally {+ Object.defineProperty(process, "platform", platform);+ vi.doUnmock("node:fs/promises");+ vi.resetModules();+ }+});diff --git a/tests/fixtures/wire-samples.json b/tests/fixtures/wire-samples.jsonindex a90d3c7a..472b3578 100644--- a/tests/fixtures/wire-samples.json+++ b/tests/fixtures/wire-samples.json@@ -1558,6 +1558,10 @@{"name": "co-play-participants-cleared","json": "{\"type\":\"co-play-presence\",\"participants\":[]}"+ },+ {+ "name": "room-relay-video-attachment-diagnostic",+ "json": "{\"type\":\"route-diagnostic-snapshot\",\"snapshot\":{\"children\":[{\"ordinal\":1,\"parent\":{\"kind\":\"host\"},\"effectiveCapacity\":20,\"childCount\":4,\"demandAgeMs\":null,\"queueWaitMs\":null,\"candidateStartMs\":null,\"firstDecodedFrameMs\":null,\"finalMs\":null,\"finalRoute\":\"direct\",\"rejectionBucket\":\"none\",\"quality\":null},{\"ordinal\":2,\"parent\":{\"kind\":\"viewer\",\"ordinal\":1},\"effectiveCapacity\":0,\"childCount\":0,\"demandAgeMs\":null,\"queueWaitMs\":null,\"candidateStartMs\":null,\"firstDecodedFrameMs\":null,\"finalMs\":null,\"finalRoute\":\"direct\",\"rejectionBucket\":\"none\",\"quality\":null},{\"ordinal\":3,\"parent\":{\"kind\":\"viewer\",\"ordinal\":1},\"effectiveCapacity\":0,\"childCount\":0,\"demandAgeMs\":null,\"queueWaitMs\":null,\"candidateStartMs\":null,\"firstDecodedFrameMs\":null,\"finalMs\":null,\"finalRoute\":\"direct\",\"rejectionBucket\":\"none\",\"quality\":null},{\"ordinal\":4,\"parent\":{\"kind\":\"viewer\",\"ordinal\":1},\"effectiveCapacity\":0,\"childCount\":0,\"demandAgeMs\":null,\"queueWaitMs\":null,\"candidateStartMs\":null,\"firstDecodedFrameMs\":null,\"finalMs\":null,\"finalRoute\":\"direct\",\"rejectionBucket\":\"none\",\"quality\":null},{\"ordinal\":5,\"parent\":{\"kind\":\"viewer\",\"ordinal\":1},\"effectiveCapacity\":0,\"childCount\":0,\"demandAgeMs\":null,\"queueWaitMs\":null,\"candidateStartMs\":null,\"firstDecodedFrameMs\":null,\"finalMs\":null,\"finalRoute\":\"direct\",\"rejectionBucket\":\"none\",\"quality\":null}],\"operation\":{\"childOrdinal\":1,\"reason\":\"publication-attach\",\"stage\":\"admission\",\"cursor\":0,\"candidateCount\":1}}}"}]}diff --git a/tests/host-peer-tracks.test.ts b/tests/host-peer-tracks.test.tsindex 8fec1bf3..0d75e9f3 100644--- a/tests/host-peer-tracks.test.ts+++ b/tests/host-peer-tracks.test.ts@@ -1045,8 +1045,8 @@ describe("HostPeer source replacement", () => {signal.candidate?.candidate.startsWith("candidate:base"),),).toHaveLength(3);- expect(candidateSignals.at(-1)?.candidate).toBeNull();- expect(candidateSignals.filter((signal) => signal.candidate === null)).toHaveLength(1);+ expect(candidateSignals.at(-1)?.candidate).toMatchObject({ candidate: "", usernameFragment: "test" });+ expect(candidateSignals.filter((signal) => signal.candidate?.candidate === "")).toHaveLength(1);peer.dispose();});diff --git a/tests/host-quality-ownership.test.ts b/tests/host-quality-ownership.test.tsindex ded86ee6..964d1ad4 100644--- a/tests/host-quality-ownership.test.ts+++ b/tests/host-quality-ownership.test.ts@@ -8,8 +8,10 @@ import {type QualitySettings,} from "../src/client/media/quality";import { NativeSenderPeer } from "../src/client/native/native-sender-peer";-import { NativeCompatibilityError, NativeRequestError } from "../src/client/native/client";+import { NativeCompatibilityError, NativeRequestError, type NativeClient } from "../src/client/native/client";+import { NativeClientConnection } from "../src/client/native/client-connection";import { nativeCaptureTargetKey } from "../src/client/native/capture-selection";+import { nativeAppDetected, type NativeSourceList } from "../src/client/components/living/CaptureSourcePicker";import { NativeMediaBridgeError } from "../src/client/native/media-bridge";import { InputRoute } from "../src/client/remote-control/input-route";import { reconcileBoundedMediaChildren } from "../src/client/webrtc/media-assignment";@@ -25,7 +27,7 @@ const source = ts.createSourceFile("HostPage.tsx", readFileSync(const owners = new Set(["changeQuality", "commitQuality", "rememberQuality", "loadAppQualityPreference", "handleSignalMessage","switchSource", "watchCaptureEnd", "switchNativeSource", "finishSourceSwitch", "replaceBrowserStream", "recoverBrowserFanout", "disposeNativeShare","acquireNativeClient", "requestSharing", "startNativeShare", "startBrowserNativeIngress",- "ownNativeClient", "discardNativeClient", "releaseUnusedNativeClient", "closeCaptureSourcePicker",+ "connectNativeClient", "handleNativeClientClosed", "releaseUnusedNativeClient", "closeCaptureSourcePicker","openCaptureSourcePicker", "startBrowserShareFromPicker", "startNativeShareFromPicker","startSharing", "connectRoomSignal", "createReplacementRoom", "beginRoomMutation", "finishRoomMutation", "setCaptureError", "toggleSharingPause","startPeer", "reconcileHostChildren", "setNotice", "setNoticeKey", "setStatusNotice", "setNoticeError", "setNoticeErrorKey", "endSharing", "disposeResources", "hostTerminationKey", "copyRoomLink", "isCurrentRoomAuthority",@@ -62,7 +64,7 @@ function ingress() {return { updateProfile: vi.fn(async (_profile: QualitySettings) => true), dispose: vi.fn() };}-function fixture(launchedByClient = true) {+function fixture(launchedByClient = true, nativeConnected = true) {let physical: QualitySettings = original;const track = { stop: vi.fn() };const stream = { getTracks: () => [track] };@@ -77,15 +79,14 @@ function fixture(launchedByClient = true) {captureOptions: vi.fn(async () => []), sources: vi.fn(async () => []),stopReceive: vi.fn(async () => undefined), stopShare: vi.fn(async () => undefined) };const route = { updateProfile: vi.fn(async () => true), resyncAuthoritative: vi.fn(async (): Promise<void> => undefined) };+ let context: ReturnType<typeof createContext>;const state = {debugError, debugEvent, debugOperation, NativeCompatibilityError, NativeRequestError, NativeMediaBridgeError, isCapturePermissionFailure, hostFailureCode, hostActionErrorKey, hostServerErrorKey, DOMException, AbortController,launchedByClient, isClientActivated: vi.fn(() => launchedByClient), hostRoomSessionAvailable: false,NativeClient: { connect: vi.fn(async (_options?: { signal: AbortSignal }): Promise<typeof client | null> => null) },- nativeClientConnectRef: ref<{ promise: Promise<typeof client | null>; controller: AbortController } | null>(null),- ownNativeClient: vi.fn(), setJoiningRoom: vi.fn(), startSharing: vi.fn(), openCaptureSourcePicker: vi.fn(),+ setJoiningRoom: vi.fn(), startSharing: vi.fn(), openCaptureSourcePicker: vi.fn(),videoCodecRef: ref({ primary: "h264" }),videoCodecModeRef: ref("h264"), MAX_ENDPOINT_MEDIA_CHILDREN: 2,- nativeClientCloseCleanupRef: ref<(() => void) | null>(null),sourcePickerReturnRef: ref<{ id: string; restore: boolean } | null>(null),nativeSourceRequestRef: ref<object | null>(null), nativeSourcePathRef: ref<unknown>(null),setNativeSources: vi.fn(), setShowCaptureBorder: vi.fn(), defaultNativeCapturePath: () => ({ adapterIndex: 0, encoderIndex: 0 }),@@ -103,10 +104,12 @@ function fixture(launchedByClient = true) {qualityPreferenceEditedRef: ref(false), pendingAppQualityPreferenceRef: ref<QualitySettings | null>(null),activeGenerationRef: ref<number | null>(1), streamRef: ref<typeof stream | null>(stream), sourceSwitchRef: ref<object | null>(null),nativeSourceAudioRef: ref<boolean | undefined>(undefined), audioAvailableRef: ref(false), setMicrophoneEnabled: vi.fn(),- nativeSourceSelectionRef: ref<Record<string, unknown> | null>(null), nativeCaptureTargetKey,- nativeModeRef: ref(false), nativeClientRef: ref<typeof client | null>(client), nativeShareGenerationRef: ref<string | null>("share"),+ nativeSourceSelectionRef: ref<Record<string, unknown> | null>(null), nativeCaptureTargetKey, nativeAppDetected,+ nativeModeRef: ref(false), nativeConnection: new NativeClientConnection(signal => context.connectNativeClient(signal),+ () => context.handleNativeClientClosed()), nativeShareGenerationRef: ref<string | null>("share"),hostAudioRef: ref<{ sourceStream: MediaStream } | null>(null),shareVolumeChangeRef: ref<object | null>(null), setShareVolume: vi.fn(),+ sharingPauseOperationRef: ref<object | null>(null),nativeMediaIngressRef: ref<ReturnType<typeof ingress> | null>(null), nativeMediaBridgeRef: ref(null),nativeInputRoute: new InputRoute(() => {}),nativeEventCleanupRef: ref(null), nativeShareCleanupRef: ref(Promise.resolve()),@@ -131,12 +134,13 @@ function fixture(launchedByClient = true) {ensureHostSfuRoute: () => route,reconcileBoundedMediaChildren, activeHostChildPeerIdsRef: ref<string[]>([]),};- const context = createContext({ ...state,+ context = createContext({ ...state,isCurrentGeneration: (generation: number) => state.activeGenerationRef.current === generation,isCurrentShare: (generation: number, share: string) =>state.activeGenerationRef.current === generation && state.shareGenerationRef.current === share,});runInContext(executable, context);+ if (nativeConnected) state.nativeConnection.adopt(client as unknown as NativeClient);const start = context.startSharing;const startChild = context.startPeer;const openPicker = context.openCaptureSourcePicker;@@ -185,7 +189,102 @@ function useRealResourceDisposal(current: ReturnType<typeof fixture>): void {current.disposeResources.mockImplementation(current.dispose);}+describe("Native sharing pause ownership", () => {+ function pauseFixture(paused = true) {+ const current = fixture();+ useRealResourceDisposal(current);+ current.nativeModeRef.current = true;+ const setPaused = vi.fn(async (_share: string, _paused: boolean) => {});+ Object.assign(current.client, { setPaused });+ const signal = { setSharingPaused: vi.fn(() => true), confirmSharingPaused: vi.fn(), setCoPlayGrants: vi.fn(), stop: vi.fn() };+ Object.assign(current.context, {+ sharingPausedRef: ref(paused), setSharingPaused: vi.fn(), setMediaPaused: vi.fn(() => true),+ signalRef: ref(signal),+ });+ return { ...current, setPaused, signal };+ }++ it.each([false, true])("commits the current Native pause change from paused=%s", async paused => {+ const current = pauseFixture(paused);+ current.context.toggleSharingPause();+ await vi.waitFor(() => expect(current.context.setSharingPaused).toHaveBeenCalledWith(!paused));+ expect(current.setPaused).toHaveBeenCalledExactlyOnceWith("share", !paused);+ expect(current.signal.setSharingPaused).toHaveBeenCalledExactlyOnceWith(!paused);+ expect(current.context.sharingPausedRef.current).toBe(!paused);+ });++ it.each(["success", "failure", "retired"])("ignores a superseded Native resume %s", async outcome => {+ const current = pauseFixture();+ const pending = deferred<void>();+ current.setPaused.mockReturnValueOnce(pending.promise);+ current.context.toggleSharingPause();+ if (outcome === "retired") {+ current.activeGenerationRef.current = null;+ current.dispose(false);+ } else {+ current.context.handleSignalMessage({ type: "pause-sharing-source", shareGeneration: "share" }, 1, false, null);+ expect(current.setPaused).toHaveBeenLastCalledWith("share", true);+ }+ current.context.setSharingPaused.mockClear();+ current.setNoticeValue.mockClear();+ if (outcome === "failure") pending.reject(new Error("old resume failed"));+ else pending.resolve();+ await new Promise(resolve => setTimeout(resolve, 0));+ expect(current.signal.setSharingPaused).not.toHaveBeenCalled();+ expect(current.context.setSharingPaused).not.toHaveBeenCalled();+ expect(current.setNoticeValue).not.toHaveBeenCalled();+ expect(current.sharingPauseOperationRef.current).toBeNull();+ });++ it("does not publish an old resume rollback after authoritative pause takes over", async () => {+ const current = pauseFixture();+ const rollback = deferred<void>();+ current.signal.setSharingPaused.mockReturnValue(false);+ current.setPaused.mockResolvedValueOnce().mockReturnValueOnce(rollback.promise);+ current.context.toggleSharingPause();+ await vi.waitFor(() => expect(current.setPaused).toHaveBeenCalledTimes(2));+ current.context.handleSignalMessage({ type: "pause-sharing-source", shareGeneration: "share" }, 1, false, null);+ current.signal.confirmSharingPaused.mockClear();+ current.setNoticeValue.mockClear();+ rollback.resolve();+ await new Promise(resolve => setTimeout(resolve, 0));+ expect(current.signal.confirmSharingPaused).not.toHaveBeenCalled();+ expect(current.setNoticeValue).not.toHaveBeenCalled();+ });+});+describe("Camera replacement ownership", () => {+ it.each([true, false])("does not apply a retired ingress replacement to a new share: %s", async result => {+ const current = fixture();+ const pending = deferred<boolean>();+ const oldTrack = { stop: vi.fn() }, capturedTrack = { stop: vi.fn() };+ const previous = { getTracks: () => [oldTrack], getVideoTracks: () => [oldTrack] };+ const captured = { getTracks: () => [capturedTrack], getVideoTracks: () => [capturedTrack], getAudioTracks: () => [] };+ const optional = { hasAudio: false, replaceStream: vi.fn(() => pending.promise) };+ const token = {};+ Object.assign(current.context, {+ streamRef: ref(previous), nativeMediaIngressRef: ref(optional), sourceSwitchRef: ref(token),+ retiringStreamRef: ref(null), sharingPausedRef: ref(false),+ setMediaPaused: vi.fn(), setStream: vi.fn(), watchCaptureEnd: vi.fn(),+ });+ const replacing = current.context.replaceBrowserStream(captured, 1, token);+ expect(optional.replaceStream).toHaveBeenCalledWith(captured);+ const replacementPeer = { replaceStream: vi.fn(async () => true) };+ const replacementRoute = { replaceStream: vi.fn(async () => true) };+ current.activeGenerationRef.current = 2;+ current.context.sourceSwitchRef.current = {};+ current.context.nativeMediaIngressRef.current = null;+ current.peersRef.current.clear();+ current.peersRef.current.set("replacement", replacementPeer as never);+ current.context.hostSfuRouteRef.current = replacementRoute;+ pending.resolve(result);+ await replacing;+ expect(replacementPeer.replaceStream).not.toHaveBeenCalled();+ expect(replacementRoute.replaceStream).not.toHaveBeenCalled();+ expect(capturedTrack.stop).toHaveBeenCalledOnce();+ expect(oldTrack.stop).toHaveBeenCalledOnce();+ });+it("keeps a screen Stop authoritative while a camera picker is pending", async () => {const current = fixture();let ended!: () => void;@@ -234,8 +333,8 @@ describe("Camera replacement ownership", () => {});describe("Host room ownership", () => {- function roomFixture() {- const current = fixture();+ function roomFixture(nativeConnected = true) {+ const current = fixture(true, nativeConnected);const room = { roomId: "1234", hostToken: "host-token", canonicalUrl: "https://example.test/r/1234", codeEntryPolicy: "open", inviteUrl: null };const signals: Signal[] = [];class Signal {@@ -306,7 +405,7 @@ describe("Host room ownership", () => {it.each(["INVALID_TOKEN", "HOST_ALREADY_CONNECTED", "room-closed", "terminated", "access-required"])("retires idle native selection and its late results on %s", async event => {for (const stage of ["ready", "discovery", "enumeration"]) {- const current = roomFixture();+ const current = roomFixture(stage !== "discovery");useRealResourceDisposal(current);current.context.phase = "idle";current.activeGenerationRef.current = null;@@ -315,7 +414,6 @@ describe("Host room ownership", () => {const discovery = deferred<typeof current.client>();const enumeration = deferred<never[]>();if (stage === "discovery") {- current.nativeClientRef.current = null;current.NativeClient.connect.mockReturnValueOnce(discovery.promise);} else if (stage === "enumeration") current.client.sources.mockReturnValueOnce(enumeration.promise);const signal = current.connect();@@ -330,11 +428,10 @@ describe("Host room ownership", () => {else signal.events.onMessage(event === "room-closed" ? { type: event } : { type: "error", code: event });expect(current.nativeSourceRequestRef.current).toBeNull();expect(current.nativeSourcePathRef.current).toBeNull();- expect(current.nativeClientConnectRef.current).toBeNull();discovery.resolve(current.client);enumeration.resolve([]);await selecting;- expect(current.nativeClientRef.current).toBeNull();+ expect(current.nativeConnection.current).toBeNull();expect(current.client.close).toHaveBeenCalledOnce();expect(current.setNativeSources).toHaveBeenLastCalledWith(null);expect(current.createRoom).not.toHaveBeenCalled();@@ -1005,8 +1102,6 @@ describe("Host quality ownership", () => {])("retains the known %s failure when retiring native sharing", async (kind, key, failureCode, comic) => {const current = fixture();current.generationRef.current = 1;- current.nativeClientRef.current = null;- current.context.ownNativeClient(current.client);await current.startNative();if (kind === "control") current.client.onClose.mock.calls[0]![0]();else if (kind === "media") current.NativeMediaBridge.mock.calls[0]![2]();@@ -1020,7 +1115,7 @@ describe("Host quality ownership", () => {it("retains connecting children during reconciliation and rebuilds them after reauthentication", async () => {const current = fixture(false);- current.nativeClientRef.current = null;+ current.nativeConnection.release();current.peer.isConnected.mockReturnValue(false);const healthy = { ...current.peer, isConnected: vi.fn(() => true) };const replacement = { ...current.peer, start: vi.fn(async () => true) };@@ -1049,7 +1144,7 @@ describe("Host quality ownership", () => {it("opens the same source picker for Browser and App without probing localhost on the Browser path", async () => {const browser = fixture(false);- browser.nativeClientRef.current = null;+ browser.nativeConnection.release();browser.requestShare();expect(browser.startSharing).not.toHaveBeenCalled();expect(browser.openCaptureSourcePicker).toHaveBeenCalledOnce();@@ -1058,7 +1153,7 @@ describe("Host quality ownership", () => {expect(browser.NativeClient.connect).not.toHaveBeenCalled();const current = fixture();- current.nativeClientRef.current = null;+ current.nativeConnection.release();current.routePolicyRef.current.topologyOptimization = true;const discovery = deferred<typeof current.client | null>();current.NativeClient.connect.mockReturnValue(discovery.promise);@@ -1073,7 +1168,7 @@ describe("Host quality ownership", () => {current.NativeClient.connect.mockResolvedValue(current.client);await expect(current.discover()).resolves.toBe(current.client);expect(current.NativeClient.connect).toHaveBeenCalledTimes(2);- current.nativeClientRef.current = current.client;+ current.nativeConnection.adopt(current.client as unknown as NativeClient);current.requestShare();expect(current.openCaptureSourcePicker).toHaveBeenCalledTimes(2);});@@ -1090,22 +1185,34 @@ describe("Host quality ownership", () => {expect(current.setNativeSources).toHaveBeenLastCalledWith(expect.objectContaining({ kind: "ready" }));expect(current.client.sources).toHaveBeenCalledOnce();+ let sources: NativeSourceList | null = current.setNativeSources.mock.lastCall![0];+ current.setNativeSources.mockImplementation((next: NativeSourceList | null | ((previous: NativeSourceList | null) => NativeSourceList)) => {+ sources = typeof next === "function" ? next(sources) : next;+ });+ const enumeration = deferred<never[]>();+ current.client.sources.mockReturnValueOnce(enumeration.promise);+ const refresh = current.openPicker();+ await vi.waitFor(() => expect(current.client.sources).toHaveBeenCalledTimes(2));+ expect(sources).toEqual({ kind: "loading", appDetected: true });+ enumeration.resolve([]);+ await refresh;+ expect(sources).toMatchObject({ kind: "ready", sources: [] });+current.context.phase = "live";await current.openPicker();expect(current.setNativeSources).toHaveBeenLastCalledWith({ kind: "browser" });- expect(current.client.sources).toHaveBeenCalledOnce();+ expect(current.client.sources).toHaveBeenCalledTimes(2);});it("shows App incompatibility in the picker and clears it on a successful refresh", async () => {const current = fixture();current.context.phase = "idle";- current.nativeClientRef.current = null;+ current.nativeConnection.release();current.nativeShareGenerationRef.current = null;current.NativeClient.connect.mockRejectedValueOnce(new NativeCompatibilityError(8));await current.openPicker();expect(current.setNativeSources).toHaveBeenLastCalledWith({ kind: "incompatible" });- expect(current.nativeClientConnectRef.current).toBeNull();- expect(current.nativeClientRef.current).toBeNull();+ expect(current.nativeConnection.current).toBeNull();expect(current.client.sources).not.toHaveBeenCalled();current.NativeClient.connect.mockResolvedValue(current.client);await current.openPicker();@@ -1117,7 +1224,7 @@ describe("Host quality ownership", () => {it("distinguishes absent App, missing capture capability, incompatible codec and an empty source list", async () => {const absent = fixture();absent.context.phase = "idle";- absent.nativeClientRef.current = null;+ absent.nativeConnection.release();await absent.openPicker();expect(absent.setNativeSources).toHaveBeenLastCalledWith({ kind: "unavailable" });@@ -1164,13 +1271,12 @@ describe("Host quality ownership", () => {expect(current.setNativeSources).toHaveBeenLastCalledWith({ kind: "failed" });expect(current.client.close).not.toHaveBeenCalled();expect(current.client.stopShare).not.toHaveBeenCalled();- expect(current.nativeClientRef.current).toBe(current.client);+ expect(current.nativeConnection.current).toBe(current.client);});it("ignores a cancelled discovery mismatch after another picker owns a compatible App", async () => {- const current = fixture();+ const current = fixture(true, false);current.context.phase = "idle";- current.nativeClientRef.current = null;current.nativeShareGenerationRef.current = null;const discovery = deferred<void>();current.NativeClient.connect.mockReturnValueOnce(discovery.promise.then(() => {@@ -1186,7 +1292,7 @@ describe("Host quality ownership", () => {await obsolete;expect(current.setNativeSources).not.toHaveBeenCalled();expect(current.client.close).not.toHaveBeenCalled();- expect(current.nativeClientRef.current).toBe(current.client);+ expect(current.nativeConnection.current).toBe(current.client);current.closePicker();});@@ -1201,7 +1307,7 @@ describe("Host quality ownership", () => {const starting = current.start();await Promise.resolve();expect(current.client.startShare).not.toHaveBeenCalled();- current.nativeClientRef.current = null;+ current.nativeConnection.release();cleanup.resolve();await starting;expect(current.client.startShare).not.toHaveBeenCalled();@@ -1213,9 +1319,8 @@ describe("Host quality ownership", () => {});it("retires discovery after picker cancellation and allows a later attempt", async () => {- const current = fixture();+ const current = fixture(true, false);current.context.phase = "idle";- current.nativeClientRef.current = null;current.nativeShareGenerationRef.current = null;const discovery = deferred<typeof current.client>();current.NativeClient.connect.mockReturnValueOnce(discovery.promise);@@ -1229,7 +1334,7 @@ describe("Host quality ownership", () => {await opening;expect(current.client.close).toHaveBeenCalledOnce();expect(current.client.sources).not.toHaveBeenCalled();- expect(current.nativeClientRef.current).toBeNull();+ expect(current.nativeConnection.current).toBeNull();const replacement = { ...current.client, close: vi.fn() };current.NativeClient.connect.mockResolvedValue(replacement);@@ -1315,7 +1420,7 @@ describe("Host quality ownership", () => {listing.resolve(undefined as never);await old;expect(current.client.close).not.toHaveBeenCalled();- expect(current.nativeClientRef.current).toBe(current.client);+ expect(current.nativeConnection.current).toBe(current.client);expect(current.setNativeSources).not.toHaveBeenCalled();current.closePicker();});diff --git a/tests/nat-prediction.test.ts b/tests/nat-prediction.test.tsindex 07c4e5ee..008e52a1 100644--- a/tests/nat-prediction.test.ts+++ b/tests/nat-prediction.test.ts@@ -451,7 +451,7 @@ describe("NAT prediction ICE adapter", () => {expect(sent.some((value) => value?.candidate.startsWith("candidate:sp")),).toBe(false);- expect(sent.filter((value) => value !== null)).toHaveLength(3);+ expect(sent.filter((value) => value?.candidate)).toHaveLength(3);});it("retires an unfinished generation without ending the restarted ICE session", () => {@@ -491,7 +491,23 @@ describe("NAT prediction ICE adapter", () => {emitter.gatheringComplete();emitter.add(null);- expect(sent.filter((value) => value === null)).toHaveLength(1);- expect(sent.at(-1)).toBeNull();+ expect(sent.filter((value) => value?.candidate === "")).toHaveLength(1);+ expect(sent.at(-1)).toMatchObject({ candidate: "", usernameFragment: "restarted" });+ expect(sent).not.toContain(null);+ });++ it("keeps a delayed scoped end from completing another generation", () => {+ const sent: SignalCandidate[] = [];+ const emitter = new NatPredictionCandidateEmitter(true, value => sent.push(value));+ const emit = (port: number) => emitter.addSignal({ ...candidate(port)!, usernameFragment: "current" }, true);+ emit(40_000);+ emitter.addSignal({ candidate: "", usernameFragment: "previous" }, false);+ emit(40_003);+ emit(40_006);+ emitter.gatheringComplete();+ emitter.add(null);+ expect(sent.filter(value => value?.candidate.startsWith("candidate:sp") ||+ value?.candidate.startsWith("candidate:sm"))).toHaveLength(MAX_NAT_PREDICTION_CANDIDATES);+ expect(sent.filter(value => value?.candidate === "").map(value => value?.usernameFragment)).toEqual(["previous", "current"]);});});diff --git a/tests/native-client.test.ts b/tests/native-client.test.tsindex ff91ca1f..25e1bb4e 100644--- a/tests/native-client.test.ts+++ b/tests/native-client.test.ts@@ -7,6 +7,7 @@ import { AppQualityPreference } from "../src/client/native/quality-preference";import {nativeEventSchema,nativeHealthSchema,+ nativeRelayCapacity,NATIVE_CLIENT_PROTOCOL,NATIVE_CLIENT_PORT_START,NATIVE_CLIENT_PORT_END,@@ -32,6 +33,39 @@ describe("native App private wire", () => {nativeMedia: { video: true, hardwareH264: true },};+ it.each(["localhost", "127.0.0.1", "[::1]"])("discovers and updates presentation from %s without an address-space hint", async hostname => {+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL(`http://${hostname}:8787/`) });+ const query = vi.fn().mockResolvedValue({ state: "prompt" });+ vi.stubGlobal("navigator", { permissions: { query } });+ const fetcher = vi.fn((url: string, init: RequestInit) => {+ if ("targetAddressSpace" in init) throw new TypeError("loopback is not a valid IPAddressSpace enum value");+ return Promise.resolve(new Response(JSON.stringify(url.endsWith("connect-discovery")+ ? { ...health, connectURL: "http://127.0.0.1:54321/client" } : health)));+ });+ vi.stubGlobal("fetch", fetcher);+ await expect(discoverNativeHealth({ waitForPermission: false })).resolves.toMatchObject({ port: health.port });+ await expect(discoverNativeConnect()).resolves.toContain("http://127.0.0.1:54321/client?");+ await notifyNativePresentation("en", new AbortController().signal);+ expect(query).not.toHaveBeenCalled();+ expect(fetcher).toHaveBeenLastCalledWith(`http://127.0.0.1:${health.port}/presentation`, expect.objectContaining({ method: "POST" }));+ expect(fetcher.mock.calls.every(([, init]) => !("targetAddressSpace" in init))).toBe(true);+ });++ it("retains the address-space hint for hosted discovery and presentation without a no-hint fallback", async () => {+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });+ vi.stubGlobal("navigator", { permissions: { query: vi.fn().mockResolvedValue({ state: "granted" }) } });+ const fetcher = vi.fn(async (_url: string, _init: RequestInit) => new Response(JSON.stringify(health)));+ vi.stubGlobal("fetch", fetcher);+ await notifyNativePresentation("en", new AbortController().signal);+ expect(fetcher.mock.calls.every(call => (call[1] as RequestInit & { targetAddressSpace: string }).targetAddressSpace === "loopback")).toBe(true);+ expect(fetcher).toHaveBeenLastCalledWith(`http://127.0.0.1:${health.port}/presentation`, expect.objectContaining({ targetAddressSpace: "loopback" }));+ fetcher.mockImplementation(() => { throw new TypeError("Unsupported IPAddressSpace"); });+ fetcher.mockClear();+ await expect(discoverNativeHealth()).resolves.toBeNull();+ expect(fetcher).toHaveBeenCalledTimes(NATIVE_CLIENT_PORT_END - NATIVE_CLIENT_PORT_START + 1);+ expect(fetcher.mock.calls.every(call => (call[1] as RequestInit & { targetAddressSpace: string }).targetAddressSpace === "loopback")).toBe(true);+ });+it("requires the controlled App platform in remote-control state", () => {const event = { version: NATIVE_CLIENT_PROTOCOL, type: "remote-control-state", shareId: "share_123456", mode: "off", requests: [] };expect(nativeEventSchema.safeParse(event).success).toBe(false);@@ -97,20 +131,22 @@ describe("native App private wire", () => {expect(nativeHealthSchema.parse({ ...health, nativeMedia }).nativeMedia).toMatchObject(nativeMedia);const stronger = { ...nativeMedia, maxVideoResolution: { width: 32768, height: 32768 },maxVideoPixels: 32768 ** 2, maxVideoFramerate: 120, maxVideoBitrate: 100_000_000,- maxAudioBitrate: 960_000 };- expect(nativeHealthSchema.parse({ ...health, nativeMedia: stronger }).nativeMedia).toMatchObject(stronger);+ maxAudioBitrate: 960_000, maxRelayChildren: 32 };+ const strongerHealth = nativeHealthSchema.parse({ ...health, nativeMedia: stronger });+ expect(strongerHealth.nativeMedia).toMatchObject(stronger);+ expect(nativeRelayCapacity(20, strongerHealth)).toBe(20);expect(nativeHealthSchema.safeParse({ ...health, nativeMedia: { ...nativeMedia, maxVideoPixels: 0 } }).success).toBe(false);expect(nativeHealthSchema.safeParse({ ...health, nativeMedia: { ...nativeMedia, maxVideoMacroblocksPerSecond: -1 } }).success).toBe(false);expect(nativeHealthSchema.parse({ ...health, nativeMedia: { ...nativeMedia, maxRelayChildren: 20 } }).nativeMedia.maxRelayChildren).toBe(20);- for (const maxRelayChildren of [0, 21, 1.5]) {+ for (const maxRelayChildren of [0, 0x1_0000_0000, 1.5]) {expect(nativeHealthSchema.safeParse({ ...health, nativeMedia: { ...nativeMedia, maxRelayChildren } }).success).toBe(false);}});it.each(["opening", "hello", "close"])("retires App acquisition during %s without waiting for its timeout", async stage => {vi.useFakeTimers();- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify(health))));const sockets: Socket[] = [];class Socket extends EventTarget {@@ -172,7 +208,7 @@ describe("native App private wire", () => {}}vi.stubGlobal("WebSocket", Socket);- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify(health))));const controller = new AbortController();const client = (await NativeClient.connect({ signal: controller.signal }))!;@@ -271,7 +307,7 @@ describe("native App private wire", () => {);it("prefers a compatible App on a later port after observing an incompatible App", async () => {- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });const fetcher = vi.fn(async (url: string) => {const port = Number(new URL(url).port);return new Response(JSON.stringify({ ...health, port,@@ -287,7 +323,7 @@ describe("native App private wire", () => {it("keeps discovery alive while local access takes longer than 400 ms", async () => {vi.useFakeTimers();- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async (url: string, options: RequestInit) => {if (new URL(url).port !== String(health.port)) throw new TypeError("No listener");await new Promise<void>((resolve, reject) => {@@ -306,7 +342,7 @@ describe("native App private wire", () => {});it("finds a later App without waiting for a silent port and aborts the unused probe", async () => {- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });let silentSignal: AbortSignal | undefined;vi.stubGlobal("fetch", vi.fn(async (url: string, options: RequestInit) => {const port = Number(new URL(url).port);@@ -325,7 +361,7 @@ describe("native App private wire", () => {it("bounds all silent discovery ports with one deadline", async () => {vi.useFakeTimers();- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });const signals: AbortSignal[] = [];vi.stubGlobal("fetch", vi.fn(async (_url: string, options: RequestInit) => {signals.push(options.signal!);@@ -342,7 +378,7 @@ describe("native App private wire", () => {});it("identifies an incompatible App even when that protocol uses different token or media metadata", async () => {- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({...health, protocol: NATIVE_CLIENT_PROTOCOL + 1,instanceToken: { format: "different-protocol" }, nativeMedia: "different-protocol",@@ -353,7 +389,7 @@ describe("native App private wire", () => {});it("treats denied, absent and malformed endpoints as ordinary Browser-only operation", async () => {- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });const malformed = [{ ...health, service: "another-service" },{ ...health, protocol: "8" },@@ -443,6 +479,7 @@ describe("native App private wire", () => {vi.stubGlobal("window", {setTimeout: globalThis.setTimeout,clearTimeout: globalThis.clearTimeout,+ location: new URL("https://piik.example/"),});vi.stubGlobal("fetch", vi.fn(async () =>new Response(JSON.stringify({@@ -573,7 +610,7 @@ describe("native App private wire", () => {}}vi.stubGlobal("WebSocket", CaptureSocket);- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({...health, nativeMedia: { ...health.nativeMedia, captureBorderControl: supported, remoteInput: supported },}))));@@ -624,7 +661,7 @@ describe("native App private wire", () => {}}vi.stubGlobal("WebSocket", Socket);- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ ...health,nativeMedia: { ...health.nativeMedia, microphone: true, ...(supported ? { processAudioExclusion: true } : {}) },}))));@@ -650,7 +687,12 @@ describe("native App private wire", () => {} finally { client.close(); }});- it.each([false, true])("gates microphone mixing and coalesces volume without delaying Stop (%s)", async (supported) => {+ it.each([+ { microphone: false, shareVolume: false },+ { microphone: true, shareVolume: true },+ { microphone: false, shareVolume: true },+ ])("gates audio mixing and coalesces volume without delaying Stop (%j)", async ({ microphone, shareVolume }) => {+ const supported = microphone || shareVolume;const requests: Record<string, unknown>[] = [];let socket!: Socket;class Socket extends EventTarget {@@ -677,9 +719,9 @@ describe("native App private wire", () => {}}vi.stubGlobal("WebSocket", Socket);- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({- ...health, nativeMedia: { ...health.nativeMedia, ...(supported ? { microphone: true, shareVolume: true } : {}) },+ ...health, nativeMedia: { ...health.nativeMedia, microphone, shareVolume },}))));const client = (await NativeClient.connect())!;try {@@ -696,17 +738,22 @@ describe("native App private wire", () => {expect(requests.some(request => request.type === "set-share-volume")).toBe(false);return;}- expect(await client.microphones()).toEqual([{ id: "headset", label: "USB Headset" }]);+ if (microphone) expect(await client.microphones()).toEqual([{ id: "headset", label: "USB Headset" }]);+ else await expect(client.microphones()).rejects.toThrow();const volume = client.setShareVolume("share_123456", .5);expect(client.setShareVolume("share_123456", 2)).toBe(volume);const shareVolumeRequest = requests.at(-1)!;- const microphoneVolume = client.setMicrophoneVolume("share_123456", .4);- expect(client.setMicrophoneVolume("share_123456", .9)).toBe(microphoneVolume);+ const microphoneVolume = microphone ? client.setMicrophoneVolume("share_123456", .4) : null;+ if (microphone) expect(client.setMicrophoneVolume("share_123456", .9)).toBe(microphoneVolume);const microphoneVolumeRequest = requests.at(-1)!;socket.ack(shareVolumeRequest, "share-volume-set");await vi.waitFor(() => expect(requests.at(-1)).toMatchObject({ type: "set-share-volume", volume: 2 }));socket.ack(requests.at(-1)!, "share-volume-set");await volume;+ if (!microphone) {+ expect(requests.some(request => request.type === "set-microphone")).toBe(false);+ return;+ }socket.ack(microphoneVolumeRequest, "microphone-set");await vi.waitFor(() => expect(requests.at(-1)).toMatchObject({ type: "set-microphone", volume: .9 }));socket.ack(requests.at(-1)!, "microphone-set");@@ -751,7 +798,7 @@ describe("native App private wire", () => {}}vi.stubGlobal("WebSocket", Socket);- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({...health, nativeMedia: { ...health.nativeMedia, ...(supported ? { receiverReuse: true } : {}) },}))));@@ -956,7 +1003,7 @@ describe("native App private wire", () => {}}vi.stubGlobal("WebSocket", Socket);- vi.stubGlobal("window", { setTimeout, clearTimeout });+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });vi.stubGlobal("fetch", vi.fn(async () => Response.json(health)));const client = (await NativeClient.connect())!;const received = vi.fn();diff --git a/tests/native-media-bridge.test.ts b/tests/native-media-bridge.test.tsindex da5c6a28..92eb583e 100644--- a/tests/native-media-bridge.test.ts+++ b/tests/native-media-bridge.test.ts@@ -10,6 +10,7 @@ import type { NativeClientEvent } from "../src/client/native/wire";import { debugError, debugEvent } from "../src/client/lib/debug";vi.mock("../src/client/lib/debug", () => ({+ registerDebugCollector: vi.fn(() => vi.fn()),browserDebugEnabled: true,debugEvent: vi.fn(),debugError: vi.fn(),diff --git a/tests/native-sender-peer.test.ts b/tests/native-sender-peer.test.tsindex 52a9c706..0b897972 100644--- a/tests/native-sender-peer.test.ts+++ b/tests/native-sender-peer.test.ts@@ -14,7 +14,11 @@ import type { NativeClientEvent } from "../src/client/native/wire";import type { PeerSnapshot } from "../src/client/types";describe("native Host peer quality", () => {- it.each(["h264", "vp8"] as const)("maps %s Pion quality windows into the existing sender evidence", async (codec) => {+ it.each([+ { codec: "h264", mode: "capture" },+ { codec: "vp8", mode: "capture" },+ { codec: "h264", mode: "relay" },+ ] as const)("maps $codec $mode output into sender evidence without inventing capture statistics", async ({ codec, mode }) => {invalidateSenderQualityEvidence();let listener: (event: NativeClientEvent) => void = () => undefined;const control: NativeEdgeControl = {@@ -36,6 +40,10 @@ describe("native Host peer quality", () => {}),};const snapshots: PeerSnapshot[] = [];+ const source = mode === "relay" ? {+ connectionId: "upstream_123456", getProfile: () => DEFAULT_QUALITY_SETTINGS,+ format: () => ({ width: 1920, height: 1080 }),+ } : undefined;const peer = new NativeSenderPeer("viewer_123456","edge_12345678",@@ -48,9 +56,11 @@ describe("native Host peer quality", () => {onUpdate: (snapshot) => snapshots.push(snapshot),},codec,+ source,);expect(await peer.start()).toBe(true);- expect(control.updateShare).not.toHaveBeenCalled();+ if (source) expect(control.updateShare).toHaveBeenCalledOnce();+ else expect(control.updateShare).not.toHaveBeenCalled();expect(shouldUseBrowserQualityCandidate(peer, {childPeerId: "viewer_123456",connectionId: "candidate_123456",@@ -104,7 +114,16 @@ describe("native Host peer quality", () => {codec: `video/${codec.toUpperCase()}`,codecProfile: null,codecParameters: null,- powerEfficientEncoder: codec === "h264",+ powerEfficientEncoder: source ? null : codec === "h264",+ captureWidth: source ? 1920 : null,+ captureHeight: source ? 1080 : null,+ captureFramesPerSecond: null,+ mediaSourceFramesPerSecond: null,+ intervalFramesEncoded: null,+ intervalFramesSent: 60,+ framesPerSecond: 30,+ frameWidth: 1280,+ frameHeight: 720,});expect(senderQualityEvidenceFromSnapshot(snapshots.at(-1)!, 7)).toMatchObject({state: "degraded",@@ -112,8 +131,14 @@ describe("native Host peer quality", () => {reason: "bandwidth",bitrateKbps: 3_000,availableOutgoingKbps: 1_000,+ captureFramesPerSecond: null,+ mediaSourceFramesPerSecond: null,},});+ listener({ ...quality, sampleTimestampMs: 14_000, intervalFramesEncoded: 0,+ framesPerSecond: 0, width: 0, height: 0, state: "unknown", reason: null });+ expect(snapshots.at(-1)?.metrics).toMatchObject({ frameWidth: null, frameHeight: null,+ captureWidth: source ? 1920 : null, captureHeight: source ? 1080 : null });peer.dispose();expect(senderQualityEvidenceFromSnapshot(snapshots.at(-1)!, 7)).toMatchObject({state: "unknown",diff --git a/tests/native-viewer-peer.test.ts b/tests/native-viewer-peer.test.tsindex 3772b865..bd922b0a 100644--- a/tests/native-viewer-peer.test.ts+++ b/tests/native-viewer-peer.test.ts@@ -115,6 +115,56 @@ it("uses the offer's STUN destinations for prediction while the next ICE config}});+it.each([false, true])("keeps restarted Native candidates after a delayed previous gathering end (prediction=%s)", async (prediction) => {+ vi.useFakeTimers();+ vi.stubGlobal("window", globalThis);+ vi.stubGlobal("MediaStream", class { getTracks() { return []; } });+ vi.stubGlobal("RTCPeerConnection", class { close() {} });+ vi.spyOn(NativeMediaBridge.prototype, "start").mockResolvedValue({} as MediaStream);+ let event!: (event: NativeClientEvent) => void;+ const result = { answer: { type: "answer" as const, sdp: "answer" }, audio: false, codec: "vp8" as const, reused: true };+ const receiveOffer = vi.fn(async () => result);+ const client = {+ health: { nativeMedia: { receiverReuse: true } }, receiveOffer,+ onEvent: (listener: typeof event) => { event = listener; return () => {}; },+ onClose: () => () => {}, closeReceiver: vi.fn(async () => {}), closeEdge: vi.fn(async () => {}),+ } as unknown as NativeClient;+ const sent: SignalPayload[] = [];+ const peer = new NativeCapableViewerPeer({+ iceServers: [{ urls: "stun:site.example:3478" }],+ natPredictionStunUrls: ["stun:site.example:3479", "stun:site.example:3480"],+ }, {+ sendSignal: (_parent, payload) => { sent.push(payload); return true; },+ sendRestartRequest: () => true, onStream: () => {}, onUpdate: () => {},+ }, { natPredictionEnabled: prediction }, async () => client, () => {}, "session", 2);+ const offer = (fragment: string): SignalPayload => ({+ kind: "description", connectionId: "current", description: { type: "offer",+ sdp: `v=0\r\na=ice-ufrag:${fragment}\r\nm=video 9 UDP/TLS/RTP/SAVPF 96\r\na=sendonly\r\na=rtpmap:96 VP8/90000\r\n` },+ });+ const emit = (candidate: Extract<NativeClientEvent, { type: "edge-candidate" }>["candidate"]) =>+ event({ version: 10, type: "edge-candidate", shareId: "session", connectionId: "current", candidate });+ try {+ await peer.acceptSignal("parent", offer("first"));+ emit({ candidate: "candidate:ns1 1 udp 1 203.0.113.7 40000 typ srflx", usernameFragment: "previous" });+ let finish!: () => void;+ receiveOffer.mockImplementationOnce(() => new Promise(resolve => { finish = () => resolve(result); }));+ const restarting = peer.acceptSignal("parent", offer("restarted"));+ await vi.waitFor(() => expect(receiveOffer).toHaveBeenCalledTimes(2));+ // The room WS delivered the new offer before an old event already written+ // by the independent App WS. The same physical receiver survives restart.+ emit(null);+ for (let index = 1; index <= 3; index++) {+ emit({ candidate: `candidate:ns${index} 1 udp 1 203.0.113.7 ${50000 + index * 3} typ srflx`, usernameFragment: "next" });+ }+ finish();+ await restarting;+ const candidates = sent.flatMap(payload => payload.kind === "candidate" ? [payload.candidate] : []);+ expect(candidates.filter(candidate => candidate?.candidate && candidate.usernameFragment === "next")).toHaveLength(prediction ? 11 : 3);+ expect(candidates.find(candidate => candidate?.candidate === "")).toMatchObject({ usernameFragment: "previous" });+ expect(candidates).not.toContain(null);+ } finally { peer.dispose(); }+});+it("retires queued candidates when disposed during their delivery", async () => {vi.stubGlobal("window", globalThis);vi.stubGlobal("MediaStream", class { getTracks() { return []; } });diff --git a/tests/protocol-fixture.test.ts b/tests/protocol-fixture.test.tsindex 642cba7f..60c822b4 100644--- a/tests/protocol-fixture.test.ts+++ b/tests/protocol-fixture.test.ts@@ -208,4 +208,15 @@ describe("shared wire fixture", () => {);},);++ it("rejects diagnostic fanout beyond the room resource bound", () => {+ const sample = fixture.serverMessages.find(+ sample => sample.name === "room-relay-video-attachment-diagnostic",+ )!;+ for (const field of ["effectiveCapacity", "childCount"]) {+ const message = JSON.parse(sample.json);+ message.snapshot.children[0][field] = MAX_VIEWERS_PER_ROOM_LIMIT + 1;+ expect(serverMessageSchema.safeParse(message).success).toBe(false);+ }+ });});diff --git a/tests/release-automation.test.ts b/tests/release-automation.test.tsindex 7e34f6a7..65330715 100644--- a/tests/release-automation.test.ts+++ b/tests/release-automation.test.ts@@ -174,6 +174,7 @@ describe("release automation", () => {"scripts/build-website.mjs", "scripts/check-docs.mjs", "scripts/check-container.mjs", "scripts/markdown-slug.mjs", "scripts/release-notes.mjs","scripts/publish-container.mjs","scripts/app-gate-endpoint.ts", "scripts/client-gate-endpoint.ts",+ "scripts/network-lab.ts", "scripts/network-lab/endpoint.ts", "scripts/network-lab/stun/main.go","tests/room.test.ts", ".agents/skills/ponytail/SKILL.md", ".github/workflows/website.yml",".github/workflows/gitee-mirror.yml", "scripts/release-mirror-source.mjs"]) {mkdirSync(dirname(join(root, path)), { recursive: true });diff --git a/tests/remote-input.test.ts b/tests/remote-input.test.tsindex c5ff5146..e7bbb801 100644--- a/tests/remote-input.test.ts+++ b/tests/remote-input.test.ts@@ -356,3 +356,72 @@ it("flushes coalesced motion before buttons and discards it when control ends",vi.useRealTimers();}});++it("sends idle motion immediately and only batches samples inside the send interval", () => {+ vi.useFakeTimers();+ const channel = new Channel();+ const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});+ const grant = "b".repeat(64);+ try {+ channel.receive({ platform: "windows", mode: "ask", state: "ready" });+ session.request();+ const intent = channel.sent.at(-1)?.intent;+ channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant });+ const before = channel.sent.length;+ session.input({ type: "move", x: .1, y: .2 });+ expect(channel.sent.slice(before)).toEqual([{ type: "move", x: .1, y: .2, grant }]);+ expect(vi.getTimerCount()).toBe(0);+ vi.advanceTimersByTime(1);+ session.input({ type: "move", x: .3, y: .4 });+ vi.advanceTimersByTime(1);+ session.input({ type: "move", x: .5, y: .6 });+ expect(channel.sent).toHaveLength(before + 1);+ vi.advanceTimersByTime(2);+ expect(channel.sent.slice(before)).toEqual([+ { type: "move", x: .1, y: .2, grant }, { type: "move", x: .5, y: .6, grant },+ ]);+ expect(vi.getTimerCount()).toBe(0);+ vi.advanceTimersByTime(12);+ session.input({ type: "relative", dx: 2, dy: -3 });+ expect(channel.sent.at(-1)).toEqual({ type: "relative", dx: 2, dy: -3, grant });+ expect(vi.getTimerCount()).toBe(0);+ } finally {+ session.dispose();+ vi.useRealTimers();+ }+});++it("keeps queued motion and wheel precision across updates to the same active grant", () => {+ vi.useFakeTimers();+ const channel = new Channel();+ const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});+ const grant = "b".repeat(64);+ try {+ channel.receive({ platform: "windows", mode: "room", state: "ready", coPlay: true, capabilities: ["coPlay", "text"] });+ session.request();+ const intent = channel.sent.at(-1)?.intent;+ const active = { platform: "windows", mode: "room", state: "active", intent, grant, coPlay: true };+ channel.receive({ ...active, capabilities: ["coPlay", "text"] });+ session.input({ type: "move", x: .1, y: .2 });+ session.input({ type: "move", x: .3, y: .4 });+ session.scroll(.5, 0);+ // Another controller joining disables committed text, not this player's motion.+ channel.receive({ ...active, capabilities: ["coPlay"] });+ vi.advanceTimersByTime(4);+ expect(channel.sent.at(-1)).toEqual({ type: "move", x: .3, y: .4, grant });+ session.scroll(.5, 0);+ expect(channel.sent.at(-1)).toEqual({ type: "wheel", delta: -1, grant });+ // Suspension still discards motion and precision belonging to the old activity.+ session.input({ type: "move", x: .9, y: .9 });+ const beforeSuspend = channel.sent.length;+ channel.receive({ ...active, state: "suspended", capabilities: ["coPlay"] });+ vi.advanceTimersByTime(4);+ expect(channel.sent).toHaveLength(beforeSuspend);+ channel.receive({ ...active, capabilities: ["coPlay"] });+ session.scroll(.75, 0);+ expect(channel.sent).toHaveLength(beforeSuspend);+ } finally {+ session.dispose();+ vi.useRealTimers();+ }+});diff --git a/tests/room-interactions.test.ts b/tests/room-interactions.test.tsindex acbe5c5d..c6d08a66 100644--- a/tests/room-interactions.test.ts+++ b/tests/room-interactions.test.ts@@ -117,6 +117,7 @@ test("data needs explicit readiness, waits for server echo, and has no offline rexpect(session.getSnapshot().confirmed?.payload).toEqual({ kind: "chat", text: "Hi" });vi.advanceTimersByTime(800);expect(session.send({ kind: "reaction", reaction: "tomato", targetPeerId: "other_peer_1234" })).toBe(true);+ const retired = session.getSnapshot().pending!;session.disconnected();expect(session.getSnapshot()).toMatchObject({ ready: false, pending: null, error: "unconfirmed", reactions: [] });const count = send.mock.calls.length;@@ -124,6 +125,10 @@ test("data needs explicit readiness, waits for server echo, and has no offline rexpect(send.mock.calls.slice(count)).toEqual([[{ type: "subscribe-room-interactions" }]]);expect(session.getSnapshot().messages).toHaveLength(1);expect(session.getSnapshot().error).toBe("unconfirmed");+ session.receive({ type: "room-interactions-ready", serverTime: Date.now() });+ session.receive({ ...message("retired_echo"), requestId: retired.requestId, payload: retired.payload,+ sender: { peerId: "self_peer_1234", role: "host", displayName: "Host" } });+ expect(session.getSnapshot()).toMatchObject({ confirmed: null, pending: null, error: "unconfirmed" });session.close();expect(vi.getTimerCount()).toBe(0);});@@ -157,10 +162,20 @@ test("rejection and timeout leave delivery unconfirmed without automatic retriesexpect(session.getSnapshot()).toMatchObject({ error: "rate-limited", pending: null, confirmed: null });vi.advanceTimersByTime(800);session.send({ kind: "chat", text: "Try me" });+ const pending = session.getSnapshot().pending!;const count = send.mock.calls.length;vi.advanceTimersByTime(6000);expect(session.getSnapshot()).toMatchObject({ error: "unconfirmed", pending: null, confirmed: null });expect(send).toHaveBeenCalledTimes(count);+ const echoed = { ...message("late_confirmed"), requestId: pending.requestId, payload: pending.payload,+ sender: { peerId: "self_peer_1234", role: "viewer" as const, displayName: "Me" } };+ session.receive(echoed);+ expect(session.getSnapshot()).toMatchObject({ error: null, pending: null, confirmed: { id: "late_confirmed" } });+ session.send({ kind: "chat", text: "Next attempt" });+ const rejected = session.getSnapshot().pending!;+ vi.advanceTimersByTime(5000);+ session.receive({ type: "room-interaction-rejected", requestId: rejected.requestId, reason: "busy" });+ expect(session.getSnapshot()).toMatchObject({ error: "busy", pending: null });session.close();});diff --git a/tests/signaling-admission.test.ts b/tests/signaling-admission.test.tsindex 67a44f53..38f032ae 100644--- a/tests/signaling-admission.test.ts+++ b/tests/signaling-admission.test.ts@@ -145,7 +145,7 @@ describe("room authentication and site admission", () => {host.signal.stop();});- it("restores Viewer route preference after discovery and reconnects with the acknowledged choice", async () => {+ it("restores Viewer route preference after discovery without replacing personal intent with a rejected effective choice", async () => {const { signal, sockets, events } = fixture("viewer");const wire = JSON.parse(readFileSync(new URL("./fixtures/wire-samples.json", import.meta.url), "utf8"));const authenticated = JSON.parse(wire.serverMessages.find(@@ -163,11 +163,35 @@ describe("room authentication and site admission", () => {sockets[0]!.dispatchEvent(Object.assign(new Event("close"), { code: 1006 }));await vi.advanceTimersByTimeAsync(800);sockets[1]!.dispatchEvent(new Event("open"));- expect(JSON.parse(sockets[1]!.send.mock.calls[0]![0])).toMatchObject({ type: "authenticate", routePreference: "p2p" });+ expect(JSON.parse(sockets[1]!.send.mock.calls[0]![0])).toMatchObject({ type: "authenticate", routePreference: "sfu" });expect(events.onTerminated).not.toHaveBeenCalled();signal.stop();});+ it.each([true, false])("retains the latest submitted Viewer route choice across socket loss and a late %s acknowledgement", async accepted => {+ const { signal, sockets } = fixture("viewer");+ const wire = JSON.parse(readFileSync(new URL("./fixtures/wire-samples.json", import.meta.url), "utf8"));+ const authenticated = JSON.parse(wire.serverMessages.find(+ (sample: { name: string }) => sample.name === "authenticated-viewer-minimal").json);+ signal.enableViewerRouteSelection("auto");+ sockets[0]!.receive(authenticated);+ sockets[0]!.receive({ type: "viewer-route-preference", preference: "auto", accepted: true });+ expect(signal.setViewerRoutePreference("sfu")).toBe(true);+ sockets[0]!.dispatchEvent(Object.assign(new Event("close"), { code: 1006 }));+ await vi.advanceTimersByTimeAsync(800);+ sockets[1]!.dispatchEvent(new Event("open"));+ expect(JSON.parse(sockets[1]!.send.mock.calls[0]![0])).toMatchObject({ routePreference: "sfu" });+ sockets[1]!.receive(authenticated);+ // Authentication makes controls available before its preference response.+ expect(signal.setViewerRoutePreference("p2p")).toBe(true);+ sockets[1]!.receive({ type: "viewer-route-preference", preference: accepted ? "sfu" : "auto", accepted });+ sockets[1]!.dispatchEvent(Object.assign(new Event("close"), { code: 1006 }));+ await vi.advanceTimersByTimeAsync(800);+ sockets[2]!.dispatchEvent(new Event("open"));+ expect(JSON.parse(sockets[2]!.send.mock.calls[0]![0])).toMatchObject({ routePreference: "p2p" });+ signal.stop();+ });+it("skips an unknown reaction without interrupting chat, media messages or a pending send", () => {const { signal, sockets, events } = fixture();const interactions = signal.interactions!;diff --git a/tests/video-fullscreen.test.ts b/tests/video-fullscreen.test.tsindex 61d6ac20..1d11483f 100644--- a/tests/video-fullscreen.test.ts+++ b/tests/video-fullscreen.test.ts@@ -85,4 +85,30 @@ describe("Video fullscreen", () => {await expect(toggleVideoFullscreen(standard.element)).rejects.toBe(rejected);expect(standard.page.exitPictureInPicture).not.toHaveBeenCalled();});++ it("uses native FullScreen methods without requiring a Safari-only capability flag", async () => {+ const { video, element } = player(true);+ delete video.webkitEnterFullscreen;+ delete video.webkitExitFullscreen;+ delete video.webkitSupportsFullscreen;+ element.webkitEnterFullScreen = vi.fn(function (this: FullscreenVideo) {+ expect(this).toBe(element);+ this.webkitDisplayingFullscreen = true;+ });+ element.webkitExitFullScreen = vi.fn(function (this: FullscreenVideo) {+ expect(this).toBe(element);+ this.webkitDisplayingFullscreen = false;+ });+ expect(videoFullscreenState(element)).toMatchObject({ supported: true, ready: false });+ await toggleVideoFullscreen(element);+ expect(element.webkitEnterFullScreen).not.toHaveBeenCalled();+ video.readyState = 1;+ expect(videoFullscreenState(element).ready).toBe(true);+ await toggleVideoFullscreen(element);+ expect(element.webkitEnterFullScreen).toHaveBeenCalledOnce();+ expect(videoFullscreenState(element).nativeActive).toBe(true);+ await toggleVideoFullscreen(element);+ expect(element.webkitExitFullScreen).toHaveBeenCalledOnce();+ expect(videoFullscreenState(element).active).toBe(false);+ });});diff --git a/tests/viewer-native-client-ownership.test.ts b/tests/viewer-native-client-ownership.test.tsindex 8eca262c..2b506a49 100644--- a/tests/viewer-native-client-ownership.test.ts+++ b/tests/viewer-native-client-ownership.test.ts@@ -3,12 +3,13 @@ import { createContext, runInContext } from "node:vm";import ts from "typescript";import { expect, it, vi } from "vitest";import { NativeCompatibilityError } from "../src/client/native/client";+import { NativeClientConnection } from "../src/client/native/client-connection";// Exercise the page's connection owner without mounting media or room signaling.const source = ts.createSourceFile("ViewerPage.tsx", readFileSync(new URL("../src/client/pages/ViewerPage.tsx", import.meta.url), "utf8",), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);-const owners = new Set(["nativeClientConnection", "nativeViewerAvailable", "acquireNativeClient", "releaseNativeClient"]);+const owners = new Set(["nativeConnection", "nativeViewerAvailable", "acquireNativeClient", "releaseNativeClient"]);const declarations: string[] = [];let denyAccess = "";function collect(node: ts.Node): void {@@ -44,7 +45,7 @@ function control() {function fixture(launchedByClient = true) {const connect = vi.fn(async (_options: { signal: AbortSignal; waitForPermission: boolean }): Promise<ReturnType<typeof control> | null> => null);const context = createContext({- clientActivationRef: { current: launchedByClient }, NativeClient: { connect }, AbortController,+ clientActivationRef: { current: launchedByClient }, NativeClient: { connect }, NativeClientConnection, AbortController,messageAuthority: { invalidate: vi.fn() }, setAssignedRoute: vi.fn(),clearViewerSfuRoute: vi.fn(), clearPeerState: vi.fn(), clearParticipantPresence: vi.fn(),dispatchPresentation: vi.fn(),diff --git a/tests/viewer-peer-generation.test.ts b/tests/viewer-peer-generation.test.tsindex db3d440f..d140988a 100644--- a/tests/viewer-peer-generation.test.ts+++ b/tests/viewer-peer-generation.test.ts@@ -575,7 +575,8 @@ describe("ViewerPeer connection generations", () => {signal.candidate?.candidate.startsWith("candidate:base"),),).toHaveLength(3);- expect(candidates.filter((signal) => signal.candidate === null)).toHaveLength(1);+ expect(candidates.filter((signal) => signal.candidate?.candidate === "")).toHaveLength(1);+ expect(candidates.at(-1)?.candidate).toMatchObject({ usernameFragment: "test" });peer.dispose();});