Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

fix(media): stabilize reconnects and quality recovery

AAA42588f3a8b1d
diff --git a/.github/workflows/repository-hygiene.yml b/.github/workflows/repository-hygiene.yml
index cccd3e96..345a0275 100644
--- a/.github/workflows/repository-hygiene.yml
+++ b/.github/workflows/repository-hygiene.yml
@@ -151,7 +151,9 @@ jobs:
- name: Run isolated network laboratory
id: network_lab
if: github.event_name == 'workflow_dispatch' && inputs.network_lab != 'none' && inputs.network_lab != ''
- timeout-minutes: 45
+ # A paired full matrix includes 71 trials plus negative controls.
+ # Individual connection/recovery deadlines stay owned by the runner.
+ timeout-minutes: 60
env:
CHROME_PATH: /usr/bin/google-chrome
TC_LIB_DIR: ${{ github.workspace }}/build/network-lab/iproute2/netem
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 937ea45e..c2c785f8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -111,7 +111,7 @@ does not establish coverage.
The server core is Go, so its acceptance runs here. It builds the Web UI first when
that output is missing, because both binaries embed it. CI invokes these same
package commands rather than rebuilding their steps in YAML.
-- `npm run check:go-race` checks port mapping, media-edge and native-control
+- `npm run check:go-race` checks port mapping, media forwarding, media-edge and native-control
ownership, loopback delivery, service storage shutdown and scoped NAT dependencies with Go's race detector. It
requires a supported cgo toolchain; CI runs it on Linux.
- `npm run check:go-vuln` checks the App and Server entry points and their
@@ -138,6 +138,10 @@ does not establish coverage.
It requires a disposable Linux runner with namespace administration. Its own
routers contain all firewall and traffic-shaping changes; do not run it on a
production service or treat its model results as a public success rate.
+- `gate:media-lifecycle` reuses that laboratory's real Browser/App endpoints on
+ loopback for restart, replacement and P2P/SFU continuity. It needs Chrome and
+ current App/Server binaries, without namespace or firewall changes. Synthetic
+ capture keeps it separate from physical-device and real-NAT acceptance.
- On Windows, execute App/Server and Go test binaries only from stable
project build paths. Go's temporary compiler/cache files are not firewall
identities; do not run network tests with a changing temporary executable
diff --git a/docs/adr/0005-automatic-hybrid-media-routing.md b/docs/adr/0005-automatic-hybrid-media-routing.md
index 2fc2de6e..dbebe504 100644
--- a/docs/adr/0005-automatic-hybrid-media-routing.md
+++ b/docs/adr/0005-automatic-hybrid-media-routing.md
@@ -171,6 +171,10 @@ same serial scheduler starts the next operation after rollback and resource
release. Each operation retains its full original deadline; neither a failed
candidate nor a retry extends a running operation. Untried parents precede
repeat attempts, and waiting Viewers with fewer started Peer attempts go first.
+Operation expiry consumes only its visited candidate prefix. An untried suffix
+remains eligible after rollback; an untried SFU opportunity precedes repeated
+Peer attempts, without displacing a fresh Peer parent. Transport-connected
+progress cannot consume a fallback that was never attempted.
The foreground SFU window is unchanged. SFU commitment carries the same
remaining Peer budget into the existing round-robin direct continuations, not
another retry ledger. No usable SFU route is interrupted by these attempts.
diff --git a/docs/adr/0009-optional-nat-prediction.md b/docs/adr/0009-optional-nat-prediction.md
index 57a49bb5..d3b9279b 100644
--- a/docs/adr/0009-optional-nat-prediction.md
+++ b/docs/adr/0009-optional-nat-prediction.md
@@ -40,6 +40,9 @@ connection acquisition reuses the route controller's existing opportunity ledger
one media section and public address form an arithmetic port sequence, the
adapter appends at most eight bounded candidates outward from both sequence
endpoints: four above the high endpoint and four below the low endpoint.
+ Distinct components and exposed related addresses/ports stay in separate
+ groups; one public address does not establish a shared local socket.
+ Privacy-redacted bases remain bounded hypotheses, not verified socket identity.
Every host and observed srflx candidate keeps normal Trickle ICE timing; no ordinary
candidate is delayed, rejected, or replaced. A Browser that does not expose
candidate source URLs keeps ordinary ICE without prediction. Rejection of a
diff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.md
index 3cc9d9c5..be7393a4 100644
--- a/docs/guide/troubleshooting.md
+++ b/docs/guide/troubleshooting.md
@@ -270,7 +270,9 @@ policies can also block media. The message alone does not identify the cause.
STUN helps discover an address for direct connections; it does not relay the
picture. An unreachable STUN service can therefore leave chat working while
-media fails. On self-hosted sites, administrators should check that `STUN_URLS`
+media fails. App Public invite already uses multiple discovery services; one
+STUN error alone does not mean every path failed. On self-hosted sites,
+administrators should check that `STUN_URLS`
points to reachable UDP services; an HTTP reverse proxy does not forward STUN.
Compare the diagnostic reports from both peers before assigning the cause.
diff --git a/docs/guide/troubleshooting.zh-CN.md b/docs/guide/troubleshooting.zh-CN.md
index ed19e6c8..129676ac 100644
--- a/docs/guide/troubleshooting.zh-CN.md
+++ b/docs/guide/troubleshooting.zh-CN.md
@@ -224,7 +224,8 @@ NAT(网络地址转换)让多台设备共用一个上网地址。不同路
仅凭这条提示还不能确定具体原因。
STUN 帮助设备发现用于直连的地址,不负责转发画面。STUN 服务不可达时,
-也可能出现聊天正常、媒体连不上的情况。自部署管理员应检查 `STUN_URLS`
+也可能出现聊天正常、媒体连不上的情况。App 公网邀请已使用多个发现服务,
+单个 STUN 报错不代表全部线路失败。自部署管理员应检查 `STUN_URLS`
是否指向可达的 UDP 服务;HTTP 反向代理不能转发 STUN。
请结合双方同一次尝试的诊断报告判断原因。
diff --git a/docs/operations/github.md b/docs/operations/github.md
index a2eecdb9..e42e2d45 100644
--- a/docs/operations/github.md
+++ b/docs/operations/github.md
@@ -95,6 +95,12 @@ mirror success. A mirror failure stays visible in its own failed workflow; an
unfinished Gitee release remains a preview.
Rerun **Gitee mirror** to verify existing files and upload only missing ones.
+Read retries cover the complete JSON or attachment body, not just response
+headers. A partial attachment is discarded and its checksum starts again on
+the next bounded attempt. Uploads and other mutations are not blindly retried
+after an ambiguous timeout; rerunning inventories existing attachments first.
+Operation and package progress identify which transfer failed without recording
+tokens. A successful retry does not by itself identify the earlier network cause.
For a separate retry, dispatch it on `main` with the **original CI run ID**:
```sh
diff --git a/docs/research/cross-platform-client-runtime.md b/docs/research/cross-platform-client-runtime.md
index 58aa9466..2f5ac9be 100644
--- a/docs/research/cross-platform-client-runtime.md
+++ b/docs/research/cross-platform-client-runtime.md
@@ -220,6 +220,25 @@ for recoverable edge disconnects. Piik's four-start acquisition limit is not a
four-reconnect limit after readiness. Restarting the Quick Tunnel process would
allocate a new public address rather than repair the old invitation.
+### Alternative Invitation Providers
+
+Reviewed 2026-10-15. [frp](https://gofrp.org/en/docs/overview/) can expose HTTP(S)
+through a server the operator supplies; [cpolar](https://www.cpolar.com/docs)
+documents HTTPS tunnels, WebSocket forwarding and regional endpoints. Neither
+has been validated with Piik here. Their service location alone does not prove
+regional reliability, and HTTP tunneling does not provide a WebRTC media route.
+An alternative must preserve the public origin, TLS and signaling WebSocket;
+it also adds account/configuration or server ownership beyond Quick Tunnel's
+temporary, account-free link. Do not substitute its executable for cloudflared:
+the current process boundary expects Cloudflare's readiness and origin events.
+
+The existing Site path already separates public web hosting from App capture.
+Use a reachable Site when Quick Tunnel is unavailable, adding its SFU only when
+media fallback is needed. No second built-in provider is justified by current
+evidence. Ordinary STUN redundancy and its independent failure controls remain
+owned by [NAT research](./nat-traversal.md); changing the invitation provider
+does not repair blocked STUN or UDP.
+
## Major-Upgrade Delivery Assessment
Reviewed 2026-10-06 against released update checkers and simulated v2.0.0
diff --git a/docs/research/nat-traversal.md b/docs/research/nat-traversal.md
index c0e04a7a..97f04ae2 100644
--- a/docs/research/nat-traversal.md
+++ b/docs/research/nat-traversal.md
@@ -226,7 +226,13 @@ Packet-level loopback regressions cover reassignment, renewal, deletion,
cancellation and the existing PCPv6 combination. They do not establish physical
router coverage or a higher field connection-success rate.
-Piik bounds a mapping caller's wait to three seconds. Connection retirement
+Piik bounds a mapping caller's wait to three seconds. Optional PCPv6 operations
+have a two-second bound in both gateway adapters, leaving time to return a
+successful IPv4 mapping before that caller deadline. Joining the optional work
+at the same deadline could otherwise discard IPv4 success and suppress renewal.
+The regression uses the real budgets with a silent IPv6 gateway; IPv6-only
+mapping retains the caller's full budget. This trades away late optional IPv6
+responses, not ordinary IPv6 ICE candidates. Connection retirement
cancels both discovery and pending creation; a canceled owner cannot start a
later mapping request. Cleanup has its own deadline because it must still run
after cancellation. PCPv6 rollback can
diff --git a/docs/research/native-client-lifecycle.md b/docs/research/native-client-lifecycle.md
index 31da5b30..12c44a13 100644
--- a/docs/research/native-client-lifecycle.md
+++ b/docs/research/native-client-lifecycle.md
@@ -285,6 +285,14 @@ all game/driver recovery or the separate immediate share-start failure report.
Some focus changes did not remain foreground throughout a sampling window;
only recorded window states and receiver progress establish those transitions.
+On 2026-10-15, the actual Windows window-capture gate passed separately with
+Auto selecting H264 and with manual VP8. Both retained playback through live,
+paused and background profile changes, restored the source window, and shared
+again after source termination; all owned resources closed. This does not
+reproduce the new v2.0.0 `app/capture/failed` report. That key reports a failed
+Native source lifetime; the App's capture-process and share-ended diagnostics
+are needed to identify which capture, codec or stream operation failed.
+
## Keyframe Request Ownership
A direct Native source delivers a requested physical output through its
diff --git a/docs/research/network-lab.md b/docs/research/network-lab.md
index 37beb91f..8edef9b1 100644
--- a/docs/research/network-lab.md
+++ b/docs/research/network-lab.md
@@ -176,6 +176,14 @@ recovery ranged from 4.9 to 11.1 seconds, so 4.9 seconds is not a guaranteed bou
The SFU-only fixture verifies the locked server-media control and disabled
NAT policy; it does not attempt to enable a hidden P2P setting.
+The shared outgoing transport also needs RTX negotiation. A local UDP regression
+delays NACK feedback after one dropped video packet: primary-stream repair
+arrived after 16 later packets, but failed after 96, beyond Pion's default
+64-packet SRTP replay window. Offering the associated RTX codec lets LiveKit
+repair the delayed loss through its separate sequence space. A receiver declining
+RTX retains ordinary NACK within the primary replay window. This applies to
+Native and SFU outputs; it does not establish the cause of unmatched field stalls.
+
App Host checks must use the real H264 Browser-to-App ingress and App WAN sender;
launching an App alongside a Browser-only VP8 share is not an App-path test.
@@ -215,6 +223,49 @@ ends at the first sample with at least 15 decoded frames; sampling is every
500 ms. Recovery checks poll every second. These are sampled media-admission
times, not exact first-packet timestamps or one-way media latency.
+## Local Media Lifecycle Gate
+
+`npm run gate:media-lifecycle -- --repeat 2` reuses the process layer's endpoint,
+synthetic picture/tone, stats and cleanup on loopback, without Linux namespaces
+or administrator permissions. Set `CHROME_PATH`, run `npm run build:web`, and
+build the current binaries with
+`go build -o build/network-lab/bin/ ./cmd/piik-server ./cmd/piik-app` first.
+`--list` lists cases; `--cases browser-browser,app-app` selects a subset.
+
+The four Browser/App directions exercise initial playback, a quiet source and
+repainting, stop/reshare, either page reloading, joining before sharing, and
+replacement during unfinished preparation. The last case releases real old SDP
+after the new share is playing. The SFU-only case covers the same steady
+lifecycle, and the two fallback cases delay real Peer signaling for 15 seconds
+to exercise SFU commitment followed by direct continuation. Video-frame callbacks
+check that the working route keeps presenting during that continuation.
+
+These are real local UI, room and media paths, not simulated route messages.
+App senders use Browser H.264 ingress and Native fanout; App receivers use the
+Native receive/playback bridge. Each local App has its own approved origin, and
+the gate checks the exact control origin and current Native edge. SFU fallback
+uses Browser publication/subscription here. This gate does not validate physical
+Native capture/publication, real NATs or Internet connection success rates.
+
+Reports under ignored `build/media-lifecycle/` record source identity, binary
+hashes, Browser versions, stage samples, screenshots and resource cleanup.
+First-picture timing starts after the stage's navigation/share action completes;
+it is not end-to-end startup time. `--negative frozen-picture --cases browser-browser` freezes
+the source after playback; it must fail specifically for non-progressing decoded
+picture. `--negative cancel --cases browser-browser` cancels after playback.
+Both must exit unsuccessfully with complete cleanup; an unrelated setup failure
+is not a passing negative control.
+Real link impairment and sustained resolution recovery remain in the namespace
+and Pion transport gates, rather than interpreting a CDP throttle as UDP shaping.
+
+On 2026-10-15, all seven cases passed twice on Windows with Chrome
+154.0.8037.98, including cleanup. The four SFU-to-Peer continuation windows
+had maximum presented-frame gaps of 76--118 ms on loopback; these are not WAN
+latency guarantees. Frozen-picture and cancellation controls failed for their
+intended reasons and cleaned up. The route regression separately covers a connected
+Peer reaching its operation deadline before a first frame: untried fallback must
+survive that deadline, while each attempted route retains its finite budget.
+
## Sustained Quality Recovery
The [2026-10-14 comparison](https://github.com/TNTcraftHIM/Piik/actions/runs/38446966925)
diff --git a/docs/research/node-local-media-design.md b/docs/research/node-local-media-design.md
index 79aa4721..8257ba63 100644
--- a/docs/research/node-local-media-design.md
+++ b/docs/research/node-local-media-design.md
@@ -397,30 +397,68 @@ use the public provisional allocation primitives, with exact typed child state
read by its parent; do not parse DebugInfo or run a second multi-track allocator.
The candidate uses LiveKit SendSideBWE and Prober through their public APIs,
with the [scoped measurement repair](../../internal/thirdparty/README.md#livekit-send-side-bandwidth-estimation).
-Packet sending uses the pinned [LiveKit transport's send-side BWE default](https://github.com/livekit/livekit/blob/v1.13.6/pkg/rtc/transport.go),
-`NoQueue`: an asynchronous writer without a second bitrate throttle on already
-encoded packets. Admission stays capped by the existing packet cache bound;
-canceled probe tickets remain charged until actually dequeued. Congestion
-estimation, probes and codec/allocation budgets retain their existing owners.
+Packet timing uses the existing LiveKit SDK leaky-bucket pacer; the SFU writer
+retains RTP extensions, TWCC identity, probe accounting and packet-pool ownership.
+Native encoders emit whole access units, whereas the SFU's `NoQueue` path assumes
+input already paced by its publisher. Admission stays capped by the existing
+packet cache bound; canceled probe tickets remain charged until actually
+dequeued. Congestion estimation, probes and codec/allocation budgets retain their
+existing owners.
Adapted probe lifecycle code retains Apache-2.0 attribution. Probe
promotion requires the upstream validity criterion and non-congestion signal;
an empty or inconclusive attempt cannot promote the estimator's initial ceiling.
#### Send Queue Recovery
+Delayed loss repair needs negotiated RTX on both single-child forwarding and
+Native SFU publication. Reusing the primary sequence space causes sufficiently
+late retransmissions to fail SRTP replay checks. A real Pion simulcast control
+drops a packet, delivers 96 newer packets, then requests repair; it fails without
+RTX and passes with the shared codec registration. Simulcast repair also carries
+the matching MID and [repaired stream identifier](https://www.rfc-editor.org/rfc/rfc8852.html#section-3)
+on its repair SSRC. Removing those headers independently fails the same control.
+Pion and LiveKit continue to own replay protection and retransmission; no replay
+window relaxation or second repair loop is needed.
+
A bounded writer-stall control exposes a limitation in the former SFU queue:
after the estimate falls from 500 to 8 kbps, only three of 32 already-encoded
packets drain in two seconds. The upstream unthrottled writer drains the same
input once the writer resumes. A separate real Windows VP8 codec and
Native forwarding control reproduces stale decoded pictures after roughly one
second of ordinary UDP loss; the packet-count cap alone does not prevent this.
+The retained SDK queue also passes that stalled-writer control: its 200 ms
+drain target increases pacing when an old low estimate would trap queued frames.
+That target is not a hard latency bound.
The [ordinary media controls](./nat-traversal.md#recovery-ownership-and-forwarding)
retain the decoded-picture and address-change evidence boundary.
+The complementary healthy-network control emits 5 Mbps as 30 whole frames per
+second over a 12 Mbps link with a shallow 7.5 KB queue. `NoQueue` repeatedly loses
+parts of those bursts and reduces allocation despite sufficient average capacity;
+the paced control retains its high output and 5 Mbps committed budget. A 5 ms
+service interval still batches too much for this fixture; 3 ms passes with
+Pion's existing 1.5 pacing headroom. This follows the
+[WebRTC distinction between average frame rate and packet bursts](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/pacing/g3doc/index.md).
+Both queue controls must pass; correcting old-packet delay alone does not
+establish healthy Native sending.
+
+Allocation also distinguishes source measurements from changed capacity. A
+stable 1 Mbps connection must not drop its output merely because that source's
+measurement changes from 900 kbps to 1.05 Mbps, or because a lower output's
+measurement window becomes available first. The adapter now follows LiveKit's
+stable optimal/cooperative allocation paths; an actual lower committed capacity
+still constrains the allocation. The focused control checks both retention and
+real downgrades, while the frame-burst trace requires uninterrupted high output
+after startup. Native categorical quality reads those allocation restrictions
+and ordinary congestion, not the ratio of instantaneous traffic to capacity.
+
Congestion callbacks could also promote the estimator's unmeasured 100 Mbps
-initial capacity while a probe was pending. Congestion now only reduces the
-committed budget; a valid completed non-congesting probe owns increases.
-Publication allocation and encoder input use that same committed video capacity.
+initial capacity while a probe was pending. Outside a recovery probe, congestion
+can reduce the committed budget; a valid completed non-congesting probe owns
+increases. Congestion during sending or settling ends exploration without cutting
+the established media budget. Both single-child transport and the publication's
+encoder-facing budget retain this rule; the upstream publication allocator's
+guard alone does not protect a separate encoder consumer.
This follows the
[upstream distinction between congestion events and completed probes](https://github.com/livekit/livekit/blob/v1.13.6/pkg/sfu/streamallocator/streamallocator.go),
without introducing another estimator.
@@ -454,20 +492,51 @@ The explicit `TestTransportConstrainedDormantRecovery` virtual-network trace use
real Pion RTP/TWCC through 6 Mbps -> 500 kbps -> 6 Mbps shaping. The repaired
library restores sustained 1.20 Mbps video after release, with a budget assertion
that rejects the unmeasured initial capacity. Earlier passing recovery traces
-could promote 100 Mbps and do not establish correct budgeting. The SDK
-latency-aware queue requires an extra adapter and is not retained. Earlier
+could promote 100 Mbps and do not establish correct budgeting. Earlier
flaky comparisons also registered TWCC twice on the receiver, duplicating
feedback; those results do not rank the queues. Product receivers already use
the single default interceptor owner.
+Partial recovery exposed a second adapter omission: a rate-controlled local
+output can remain below its own ceiling without needing another spatial layer.
+Probe demand now includes that unmet current-output demand, including when no
+higher output exists. Raw forwarding cannot change its encoder and keeps the
+existing layer-selection rule. In a matched retained-connection control,
+6 Mbps -> 120 kbps -> 800 kbps -> 6 Mbps, the former adapter stays near 74 kbps
+through the partial restoration; the corrected adapter restores its 300 kbps
+output before the 1.2 Mbps output fits. Both eventually restore the higher output.
+The retained network gate checks the intermediate phase as well as final recovery.
+A separate two-child control uses real Windows VP8 encoding and Browser decoding:
+the constrained child recovers after release while the healthy child retains its
+original dimensions. These checks do not establish the cause of an unmatched
+physical Host's low-resolution session.
+
+One publication boundary remains reproducible: with two declared encodings but
+only the rate-controlled lowest requested, the original 6 Mbps -> 120 kbps ->
+6 Mbps control settled at about 215--230 kbps of a 300 kbps output. After RTX
+negotiation was repaired, four repeats on 2026-10-15 settled at 239--273 kbps;
+two still failed the fixture's sustained 250 kbps recovery observation without
+further probing. Repair improves delivery but does not establish consistent
+within-output recovery. The full SFU allocator probes spatial deficiency, so it
+stops exploring once that single active RID fits its
+reduced encoding. Diagnostics now retain this within-output limitation, but
+that does not repair its probe demand. A matched isolated library extension
+adds the local encoder's unmet demand to that allocator's state and existing
+padding probe, restoring about 295 kbps. No additional estimator, probe loop or
+fabricated layer bitrate is needed. The pinned and inspected upstream allocator
+have no such input; this experiment is not retained in product code and does
+not justify copying the complete allocator into Piik. Integration must preserve
+that single probe owner. This separate publication control does not diagnose a
+direct P2P field session.
+
Live audio changes exposed a related scope mismatch: Piik's TWCC tracks only
video, but the adapter deducted audio from that measured capacity and included
audio in expected probe usage. After restoring 320 kbps Opus, this could remove
an already recovered video layer on a healthy 6 Mbps link. The adapter now uses
the measured video capacity directly, matching the upstream allocator's
-video-only usage accounting. Native and SFU share this rule; quality reports
-compare video capacity with video payload while retaining total traffic in
-diagnostics. Opus rate and transport behavior are unchanged. The retained
+video-only usage accounting. Native and SFU share this rule; diagnostics retain
+video capacity and total traffic as separate facts. Opus rate and transport
+behavior are unchanged. The retained
network gate exercises video-only and real Opus 320/96/320 kbps cases, requiring
sustained high video and decoded audio after recovery. The former subtraction
fails a matched trace but can be masked by later probe traffic; a passing
diff --git a/docs/standards/engineering.md b/docs/standards/engineering.md
index 5c834193..ffd8c06e 100644
--- a/docs/standards/engineering.md
+++ b/docs/standards/engineering.md
@@ -302,6 +302,10 @@ after cancellation; a timeout constant or a close call alone proves no deadline.
Keep the shared resources production retains while changing the external
condition. New request/connection IDs alone do not prove that a dependency's
cached observations, failure state or resource lease have been refreshed.
+For media adaptation, include partial capacity restoration on the retained
+connection, not only recovery to a fast link. Check capture, encoded output,
+actual send and remote decode separately. A dependency's algorithm does not
+prove its adapter preserves assumptions about pacing, measurement or ownership.
For overlapping connections, preserve the actual shared local and remote
endpoints; giving each fixture an independent listener can hide demultiplexing
conflicts. Check both commit and rollback while the surviving media still flows.
diff --git a/docs/standards/media-quality.md b/docs/standards/media-quality.md
index 29d40a23..1458eaaf 100644
--- a/docs/standards/media-quality.md
+++ b/docs/standards/media-quality.md
@@ -221,7 +221,8 @@ ADR-0007 owns probe and negotiation mechanics.
Codec selection preserves the negotiated repair stream associated with the
selected primary codec. Native and SFU receivers use standard Pion RTX/NACK;
-codec restrictions must not strip recovery or disable SRTP replay protection.
+their shared outgoing transport also offers RTX, with LiveKit owning repair.
+Codec restrictions must not strip recovery or disable SRTP replay protection.
The Browser chooses the concrete encoder implementation. A reported H.264 codec
does not by itself prove hardware acceleration, and Web content cannot select a
@@ -341,6 +342,10 @@ directly and does not re-encode that preview through a Browser publisher.
Native parents and the SFU share the media forwarding adapter. Each Native
parent considers only its direct children and its admitted SFU publication.
+Recovery-probe traffic is exploration, not a reduction of committed media
+capacity. Allocation and encoder consumers retain the same budget through probe
+settling. A locally rate-controlled output has unmet demand within its current
+layer as well as demand for larger outputs; raw forwarding cannot synthesize it.
Compatible children reuse an available encoded output; a missing lower output
starts one shared decode/scale/encode group, while healthy forwarding retains
the original input without starting a decoder. The group produces the highest
@@ -464,7 +469,10 @@ quality metrics into media settings or a weighted/global route score.
Native categorical evidence includes the media framework's committed allocation
deficiency: fitting a reduced output into the available budget does not clear a
-continuing framework limitation. Publication evidence considers only requested
+continuing framework limitation, including a live encoder restricted within its
+current output. Source bitrate variation alone does not reduce a committed
+allocation or prove congestion; an unsuccessful recovery probe does not change
+the incumbent's quality category. Publication evidence considers only requested
encodings, so an intentionally inactive encoding is not a bandwidth failure.
Missing feedback or frame progress remains unknown. This category neither
infers a physical bottleneck nor ranks routes by configured quality ceilings.
diff --git a/docs/standards/routing-transport.md b/docs/standards/routing-transport.md
index 7e6b50c4..c365a262 100644
--- a/docs/standards/routing-transport.md
+++ b/docs/standards/routing-transport.md
@@ -112,6 +112,11 @@ Either exact preparing endpoint can report that progress, so a delayed Viewer
confirmation does not discard a transport already observed by its parent.
This cannot commit the route: first-frame readiness remains Viewer-owned, and
progress never extends the total operation deadline.
+If that deadline expires before later candidates are tried, rollback consumes
+only the visited prefix. The same scheduler may start a fresh bounded operation
+for the remaining opportunities; an untried SFU precedes a repeated Peer attempt,
+while a fresh Peer parent still has priority. A connected transport cannot spend
+another route's opportunity merely by failing to deliver its first frame.
Consequently a working SFU route can carry media while its one-candidate direct
convergence uses the full background operation, and peer-only acquisition does
not abandon its only possible route at the foreground boundary.
diff --git a/docs/standards/versioning.md b/docs/standards/versioning.md
index 7bb0bdb3..d4026329 100644
--- a/docs/standards/versioning.md
+++ b/docs/standards/versioning.md
@@ -149,6 +149,10 @@ Updates remain a manual action. Release prose stays on GitHub/Gitee; the App
does not fetch or render remote Markdown or add a second changelog store.
Direct download remains the primary action; reading notes is optional. Keep the
major warning inline, without a confirmation dialog or mandatory website detour.
+The same notice offers the official website's download section as a manual
+alternative, including the verified Gitee mirror. API response time does not
+measure package-download throughput; do not add speed probes or automatic
+provider switching to an otherwise valid update check.
Older installed Apps cannot acquire new warning UI from release metadata.
Their historical download behavior is accepted; do not split release feeds or
diff --git a/docs/status.md b/docs/status.md
index b5491cd5..8fd89d69 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -1,6 +1,6 @@
# Current Status
-Last updated: 2026-10-14
+Last updated: 2026-10-15
This is the compact execution/deployment index. Product modules own behavior,
[verification status](./verification-status.md) owns unresolved physical limits,
@@ -56,6 +56,10 @@ Release artifacts and operator deployment records own delivery completion;
## Current Work
+Post-release maintenance retains the existing protocol and route owners.
+[TODO](./todo.md#now) owns the remaining quality-recovery boundary; release
+artifacts and operator records own delivery and acceptance 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
@@ -76,8 +80,8 @@ limited decoders and sustained game/4K use retain the limits in
[verification status](./verification-status.md). Additional codecs, room voice,
surround delivery and end-to-end HDR remain outside this phase.
-The owner authorized publication after final acceptance, without a separate
-field beta. Immutable release artifacts and operator records own exact delivery
+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.
@@ -109,8 +113,10 @@ public release on 2026-09-12. Source is public, main protection is active, and
remains a separate deployment. Release descriptors and GitHub deployment records
identify published artifacts and website revisions.
-Matching Server and registered App packages are published on GitHub and Gitee;
-anonymous mirror downloads match the original checksums. Public-repository CI
+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
diff --git a/docs/todo.md b/docs/todo.md
index 9075a41f..20e6e3bb 100644
--- a/docs/todo.md
+++ b/docs/todo.md
@@ -1,6 +1,6 @@
# Current TODO Ledger
-Last reviewed: 2026-10-14
+Last reviewed: 2026-10-15
Explicit owner holds remain in force; an investigation does not itself
accept a new feature or architecture. A missing device or reporter trace blocks
@@ -9,14 +9,21 @@ Git/PRs own completed history.
## Now
-The 2.0 implementation scope is complete. Final package checks and delivery follow
-[the release workflow](../CONTRIBUTING.md#full-integration-and-release-workflow)
-and [GitHub operations](./operations/github.md); their exact results and PR
-disposition belong to the release/PR records. The owner authorized publication
-after bounded local acceptance, without a separate field beta. Broader device
-coverage below does not override the owner's platform deferrals or require a
-statistical NAT campaign. Do not describe those unmatched reports as fixed.
+The 2.0 maintenance line retains the published protocol and route owners.
+Release artifacts and operator records own exact delivery; remaining product
+work is listed below. Broader device coverage does not override the owner's
+platform deferrals or establish a diagnosis for unmatched reports.
+- [ ] **Native quality recovery.** Verify the retained-connection budget and
+ current-output demand repairs against the affected Host. Local paced-sending,
+ partial-recovery and stable-allocation controls pass; they do not prove the
+ live report resolved. The publication's sole active rate-controlled RID still
+ stops probing below its ceiling. An isolated extension restores it by exposing
+ codec demand to the existing allocator, but requires an upstream input absent
+ from the current dependency; choose a bounded integration without copying the
+ whole allocator, a second controller or fabricated source measurements.
+ [Media evidence](./research/node-local-media-design.md#send-queue-recovery)
+ owns the comparisons and remaining boundary.
## Held By Owner
- [ ] **Browser-internal encoder work.** Paused by the owner on 2026-10-01.
@@ -152,8 +159,11 @@ experiments and their limits.
obtain the next candidate/fallback result and final state; a rejected candidate
alone is not the share outcome. [Native media research](./research/native-client-media.md#current-boundary)
owns the hardware checks and limits.
-- [ ] **Share ends after entering a game.** Obtain version, capture path, codec
- and paired diagnostics. Locate the first capture/output, preview-bridge,
+- [ ] **Native sharing ends unexpectedly.** The new v2.0.0 `app/capture/failed`
+ report and earlier game-entry interruptions need matched App diagnostics.
+ The error key alone identifies neither a driver nor a timeout. The local
+ Windows lifecycle gate passes; it does not reproduce the affected setup.
+ Obtain capture path, codec and paired diagnostics. Locate the first capture/output, preview-bridge,
control or authority failure; distinguish source silence from target replacement,
display-mode change and device loss. [Capture research](./research/native-client-lifecycle.md#quiet-sources-and-viewer-recovery)
owns the quiet-source behavior; an encoder output deadline remains a candidate
@@ -243,14 +253,10 @@ restore reverse feedback. Keep standard ICE recovery while that boundary remains
Reconsider automatic storage only with a stable room-incarnation identity,
bounded retention and a clear delete control; reusable room codes must not
combine conversations. No new history service or wire field is authorized.
-8. **Direct OBS input (#436).** Reopen for a measured virtual-camera limitation.
- The [capture assessment](./research/camera-and-microphone.md) records raw-frame
- output and the WHIP boundary; virtual camera alone is not a second encode.
- Do not add an RTMP/WHIP listener without an accepted publication owner.
-9. **Managed Demo hosting.** The [provider assessment](./operations/website.md#managed-hosting-assessment)
+8. **Managed Demo hosting.** The [provider assessment](./operations/website.md#managed-hosting-assessment)
identifies UDP/storage requirements but establishes no cost saving. Keep the
existing P2P-only Demo; reopen for an accepted provider and operating boundary.
-10. **Phone as a Host microphone.** The [input assessment](./research/camera-and-microphone.md#phone-microphone-assessment)
+9. **Phone as a Host microphone.** The [input assessment](./research/camera-and-microphone.md#phone-microphone-assessment)
identifies pairing/revocation and native decoded-input ownership beyond the
device selector. Reopen for a concrete use case that justifies this boundary
and physical latency/echo/background checks. This does not reopen room voice.
diff --git a/internal/app/mediaedge/groups_test.go b/internal/app/mediaedge/groups_test.go
index 7b231563..32d90048 100644
--- a/internal/app/mediaedge/groups_test.go
+++ b/internal/app/mediaedge/groups_test.go
@@ -203,7 +203,10 @@ func TestOutputGroupsHonorOriginalTransportDemand(t *testing.T) {
output.SetBudget(200_000)
plan("limited budget", false)
output.SetBudget(2_000_000)
- if state := output.State(); state.Target != 0 || state.Prepare != 1 {
+ // SetReceiver reset the framework's acquired layer. Its acquisition target
+ // may aim at the original, but only the low layer is actually available for
+ // requesting a frame; preparation is separate from accepted forwarding.
+ if state := output.State(); state.Requested != 0 || state.Current > 0 || state.Prepare != 1 {
t.Fatalf("higher budget did not prepare the dormant original: %+v", state)
}
plan("higher budget preparation", true)
diff --git a/internal/app/mediaedge/quality.go b/internal/app/mediaedge/quality.go
index 354c24b7..8caff36b 100644
--- a/internal/app/mediaedge/quality.go
+++ b/internal/app/mediaedge/quality.go
@@ -85,9 +85,9 @@ func (edge *Edge) QualitySample(now time.Time) (QualitySample, bool) {
sample.AvailableOutgoingKbps = float64(target) / 1000
reason := "none"
sample.State = "healthy"
- // The estimator tracks video only. Keep total traffic in diagnostics, but
- // compare its capacity with the corresponding video payload.
- if edge.transport.Output.IsDeficient() || float64(target) < float64(videoBytes*8)/seconds {
+ // Allocation and ordinary congestion own the limitation. A source bitrate
+ // spike or an unsuccessful recovery probe is not itself a new media limit.
+ if edge.transport.BandwidthLimited() {
reason = "bandwidth"
sample.State = "degraded"
}
diff --git a/internal/app/mediaedge/quality_test.go b/internal/app/mediaedge/quality_test.go
index 2a64192e..4ba81254 100644
--- a/internal/app/mediaedge/quality_test.go
+++ b/internal/app/mediaedge/quality_test.go
@@ -61,7 +61,7 @@ func TestQualityDimensionsFollowAssignedSource(t *testing.T) {
edge.transport.Output.State(), available, rates)
}
-func TestQualitySampleUsesEstimatorCapacityAgainstEncodedPayload(t *testing.T) {
+func TestQualitySampleUsesAllocationNotPacketRate(t *testing.T) {
engine, err := NewEngine(EngineOptions{
BindAddress: "127.0.0.1:0", IncludeLoopback: true,
})
@@ -76,7 +76,8 @@ func TestQualitySampleUsesEstimatorCapacityAgainstEncodedPayload(t *testing.T) {
edge, receiver, _ := connectedReceiver(t, engine, source, "limited-edge")
t.Cleanup(func() { _ = receiver.Close() })
edge.targetBitrate = func() (int, bool) { return 100_000, true }
- // Encoded relay sources need no decoded dimensions to compare payload and capacity.
+ // Packet throughput can exceed a stale estimate without an allocation
+ // restriction. The counter window alone must not request route convergence.
started := time.Now()
if _, ok := edge.QualitySample(started); ok {
t.Fatal("first quality sample did not establish a baseline")
@@ -95,8 +96,8 @@ func TestQualitySampleUsesEstimatorCapacityAgainstEncodedPayload(t *testing.T) {
time.Sleep(time.Millisecond)
sample, ok := edge.QualitySample(started.Add(2 * time.Second))
- if !ok || sample.State != "degraded" || sample.Reason == nil ||
- *sample.Reason != "bandwidth" || sample.IntervalFramesEncoded != 60 ||
+ if !ok || sample.State != "healthy" || sample.Reason == nil ||
+ *sample.Reason != "none" || sample.IntervalFramesEncoded != 60 ||
sample.FramesPerSecond != 30 || sample.BitrateKbps != 4_000 ||
sample.AvailableOutgoingKbps != 100 {
t.Fatalf("quality sample = %+v, %v", sample, ok)
@@ -119,6 +120,7 @@ func TestQualitySampleUsesEstimatorCapacityAgainstEncodedPayload(t *testing.T) {
}{
{200_000, 100_000, 0, "degraded"},
{1_000_000, 900_000, 1, "healthy"},
+ {1_000_000, 1_050_000, 1, "healthy"},
} {
output.SetBudget(int64(check.budget))
edge.targetBitrate = func() (int, bool) { return check.budget, true }
diff --git a/internal/media/forwarding/codecs.go b/internal/media/forwarding/codecs.go
index 803852c8..1934e7ae 100644
--- a/internal/media/forwarding/codecs.go
+++ b/internal/media/forwarding/codecs.go
@@ -9,6 +9,23 @@ import (
"github.com/pion/webrtc/v4"
)
+// Each outgoing connection offers one primary video codec and Opus (111).
+// RTX keeps late repair outside the primary stream's SRTP replay window.
+func registerVideoCodec(media *webrtc.MediaEngine, codec webrtc.RTPCodecParameters) error {
+ if err := media.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
+ return err
+ }
+ repairPayload := webrtc.PayloadType(96)
+ if repairPayload == codec.PayloadType {
+ repairPayload++
+ }
+ return media.RegisterCodec(webrtc.RTPCodecParameters{
+ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeRTX,
+ ClockRate: codec.ClockRate, SDPFmtpLine: "apt=" + strconv.Itoa(int(codec.PayloadType))},
+ PayloadType: repairPayload,
+ }, webrtc.RTPCodecTypeVideo)
+}
+
// ReadRTP restores repaired packets using the negotiated codec. Pion v4.2.23
// can queue RTX before a RID track's first primary read initializes PayloadType,
// even though Codec is already known. Do not rewrite ordinary packets: their
diff --git a/internal/media/forwarding/output.go b/internal/media/forwarding/output.go
index 91cec7a7..ba30039f 100644
--- a/internal/media/forwarding/output.go
+++ b/internal/media/forwarding/output.go
@@ -10,6 +10,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
+ "github.com/livekit/livekit-server/pkg/sfu/bwe"
"github.com/livekit/protocol/livekit"
"github.com/pion/webrtc/v4"
)
@@ -27,6 +28,7 @@ type Output struct {
hasAllocation bool
closed bool
feedbackObserved func() bool
+ congestionState func() bwe.CongestionState
onActivity func()
onDemandChanged func()
}
@@ -51,17 +53,19 @@ func NewOutput(track *sfu.DownTrack, budget int64) *Output {
func (output *Output) SetBudget(budget int64) {
output.mu.Lock()
defer output.mu.Unlock()
- output.budget = max(budget, 0)
- output.reconcileLocked()
+ budget = max(budget, 0)
+ reduced := budget < output.budget
+ output.budget = budget
+ output.reconcileLocked(reduced)
}
func (output *Output) Reconcile() {
output.mu.Lock()
defer output.mu.Unlock()
- output.reconcileLocked()
+ output.reconcileLocked(false)
}
-func (output *Output) reconcileLocked() {
+func (output *Output) reconcileLocked(budgetReduced bool) {
defer output.notifyDemand()
if output.closed {
return
@@ -84,25 +88,49 @@ func (output *Output) reconcileLocked() {
break
}
}
- if !measured || (output.feedbackObserved != nil && !output.feedbackObserved()) {
+ congestion := bwe.CongestionStateNone
+ if output.congestionState != nil {
+ congestion = output.congestionState()
+ }
+ stable := !budgetReduced && output.hasAllocation && !output.IsDeficient() && congestion != bwe.CongestionStateCongested
+ if !measured || (output.feedbackObserved != nil && !output.feedbackObserved()) || stable {
if len(available) > 0 {
// Source history is not this new connection's capacity. Bootstrap
// through the library until video can establish real feedback;
// pausing against the initial estimate would prevent that feedback.
- output.allocation = output.AllocateOptimal(false, false)
+ // Stable streams also retain the library's optimal allocation, including
+ // while individual layers' bitrate windows are becoming available.
+ output.allocation = output.AllocateOptimal(false, congestion == bwe.CongestionStateEarlyWarning)
output.hasAllocation = true
}
return
}
output.ProvisionalAllocatePrepare()
remaining := output.budget
+ retained := buffer.InvalidLayer
+ if !budgetReduced && output.hasAllocation {
+ // Match the library's cooperative allocation: changed source measurements
+ // do not imply congestion. Retain a valid target; only a lower connection
+ // budget constrains it again. Larger outputs still need available headroom.
+ transition := output.ProvisionalAllocateGetCooperativeTransition(false)
+ if transition.From.IsValid() && transition.From == transition.To {
+ retained = transition.To
+ remaining -= output.BandwidthRequested()
+ } else {
+ output.ProvisionalAllocateReset()
+ }
+ }
maximum := output.MaxLayer()
controlled, ok := output.Receiver().(interface{ RateControlled(int32) bool })
keepLowest := ok && controlled.RateControlled(0) && slices.Contains(available, int32(0))
for spatial := int32(0); spatial <= maximum.Spatial; spatial++ {
for temporal := int32(0); temporal <= maximum.Temporal; temporal++ {
+ layer := buffer.VideoLayer{Spatial: spatial, Temporal: temporal}
+ if retained.IsValid() && !layer.GreaterThan(retained) {
+ continue
+ }
_, used := output.ProvisionalAllocate(remaining,
- buffer.VideoLayer{Spatial: spatial, Temporal: temporal}, !(keepLowest && spatial == 0), false)
+ layer, !(keepLowest && spatial == 0), false)
remaining -= used
}
}
@@ -156,13 +184,20 @@ func (output *Output) probeDemand() (int64, int64) {
return 0, 0
}
usage := output.BandwidthRequested()
+ // A live encoder can adapt below its current logical layer's ceiling. That
+ // unmet demand exists even without a higher layer; raw forwarding cannot
+ // consume it. Restore it before exploring a larger spatial output.
+ if ceiling := controlledLayerCeiling(output.Receiver(), output.allocation.TargetLayer.Spatial); ceiling > output.budget {
+ return max(ceiling-usage, 0), usage
+ }
if transition, available := output.GetNextHigherTransition(false); available {
return transition.BandwidthDelta, usage
}
// Dormant ceilings are desired demand only. They never enter the receiver's
// measured bitrate table or authorize forwarding an unavailable layer.
next, desired := int32(buffer.DefaultMaxLayerSpatial+1), int64(0)
- for _, layer := range buffer.GetVideoLayersForMimeType(output.Receiver().Mime(), output.Receiver().TrackInfo()) {
+ layers := buffer.GetVideoLayersForMimeType(output.Receiver().Mime(), output.Receiver().TrackInfo())
+ for _, layer := range layers {
if layer.SpatialLayer > output.allocation.TargetLayer.Spatial && layer.SpatialLayer <= output.MaxLayer().Spatial &&
layer.SpatialLayer < next && int64(layer.Bitrate) > output.budget {
next, desired = layer.SpatialLayer, int64(layer.Bitrate)
@@ -171,6 +206,28 @@ func (output *Output) probeDemand() (int64, int64) {
return max(desired-usage, 0), usage
}
+// BandwidthLimited includes a live encoder restricted inside its current layer.
+// Packet-rate variation alone is not an allocation or congestion decision.
+func (output *Output) BandwidthLimited() bool {
+ output.mu.Lock()
+ defer output.mu.Unlock()
+ return !output.closed && output.hasAllocation && (output.IsDeficient() ||
+ controlledLayerCeiling(output.Receiver(), output.allocation.TargetLayer.Spatial) > output.budget)
+}
+
+func controlledLayerCeiling(receiver sfu.TrackReceiver, spatial int32) int64 {
+ controlled, ok := receiver.(interface{ RateControlled(int32) bool })
+ if !ok || !controlled.RateControlled(spatial) {
+ return 0
+ }
+ for _, layer := range buffer.GetVideoLayersForMimeType(receiver.Mime(), receiver.TrackInfo()) {
+ if layer.SpatialLayer == spatial {
+ return int64(layer.Bitrate)
+ }
+ }
+ return 0
+}
+
func (output *Output) WriteRTP(packet *buffer.ExtPacket, layer int32) int32 {
return output.writeRTP(packet, layer, nil)
}
@@ -294,7 +351,7 @@ func (output *Output) replaceReceiver(receiver *outputReceiver) error {
output.mu.Lock()
output.current = -1
output.hasAllocation = false
- output.reconcileLocked()
+ output.reconcileLocked(false)
output.mu.Unlock()
return nil
}
diff --git a/internal/media/forwarding/output_test.go b/internal/media/forwarding/output_test.go
index 825f6494..2509a3bf 100644
--- a/internal/media/forwarding/output_test.go
+++ b/internal/media/forwarding/output_test.go
@@ -117,8 +117,8 @@ func TestOutputUsesLibraryAllocationAndExactLifetime(t *testing.T) {
if state := output.State(); !state.Paused || state.Target != -1 || state.Prepare != 0 || state.VideoBudget != 60_000 {
t.Fatalf("below-lowest budget cannot request codec adaptation: %+v", state)
}
- // A new child's seed cannot veto a warm source's first video. Once feedback
- // arrives, the same source must obey the real connection's allocation again.
+ // A new child's seed cannot veto a warm source's first video. Receiving
+ // feedback alone is not congestion; a committed reduction constrains it again.
observed := false
output.feedbackObserved = func() bool { return observed }
output.Reconcile()
@@ -127,14 +127,51 @@ func TestOutputUsesLibraryAllocationAndExactLifetime(t *testing.T) {
}
observed = true
output.Reconcile()
+ if state := output.State(); state.Paused {
+ t.Fatalf("first feedback reinterpreted the seed as a capacity reduction: %+v", state)
+ }
+ output.SetBudget(50_000)
if state := output.State(); !state.Paused {
- t.Fatalf("bootstrap bypassed observed raw-source limits: %+v", state)
+ t.Fatalf("bootstrap bypassed a committed capacity reduction: %+v", state)
}
+ output.SetBudget(60_000)
receiver.controlled = true
output.Reconcile()
if state := output.State(); state.Paused || state.Target != 0 {
t.Fatalf("live lowest codec could not continue bitrate adaptation: %+v", state)
}
+ // An adapting encoder can be constrained inside its current logical layer.
+ // Restore that layer's own ceiling before probing a higher spatial output,
+ // including when there is no higher output at all.
+ receiver.rates[0][0] = 50_000
+ output.Reconcile()
+ for _, maximum := range []int32{2, 0} {
+ output.SetMaxSpatialLayer(maximum)
+ if delta, usage := output.probeDemand(); delta != 50_000 || usage != 50_000 {
+ t.Fatalf("current codec recovery skipped (maximum=%d): delta=%d usage=%d", maximum, delta, usage)
+ }
+ if !output.BandwidthLimited() {
+ t.Fatalf("same-layer encoder restriction was reported as healthy (maximum=%d)", maximum)
+ }
+ }
+ output.SetBudget(100_000)
+ if delta, _ := output.probeDemand(); delta != 0 {
+ t.Fatalf("satisfied codec ceiling retained probe demand: %d", delta)
+ }
+ if output.BandwidthLimited() {
+ t.Fatal("satisfied codec ceiling retained a bandwidth restriction")
+ }
+ receiver.controlled = false
+ output.SetBudget(60_000)
+ if delta, _ := output.probeDemand(); delta != 0 {
+ t.Fatalf("raw forwarding invented same-layer codec demand: %d", delta)
+ }
+ if output.BandwidthLimited() {
+ t.Fatal("raw forwarding invented a local encoder restriction")
+ }
+ receiver.controlled = true
+ output.SetMaxSpatialLayer(2)
+ receiver.rates[0][0] = 100_000
output.SetBudget(0)
if state := output.State(); !state.Paused {
t.Fatalf("codec control overrode exhausted connection budget: %+v", state)
@@ -156,6 +193,44 @@ func TestOutputUsesLibraryAllocationAndExactLifetime(t *testing.T) {
if state := sibling.State(); state.Paused || state.Target != 2 || state.VideoBudget != 1_000_000 || receiver.rates[2][0] != 900_000 {
t.Fatalf("weak-child adaptation changed the shared high output: %+v", state)
}
+ // Frame complexity and RTP overhead can briefly exceed a nominal ceiling.
+ // A source measurement is not new evidence that this connection lost capacity.
+ // The layers' measurement windows can also become available independently.
+ receiver.rates[2][0] = 0
+ sibling.UpTrackBitrateAvailabilityChange()
+ if state := sibling.State(); state.Target != 2 {
+ t.Fatalf("unmeasured active layer was treated as unavailable: %+v", state)
+ }
+ receiver.rates[2][0] = 1_050_000
+ sibling.UpTrackBitrateReport(receiver.available, receiver.rates)
+ sibling.SetBudget(1_000_000)
+ if state := sibling.State(); state.Target != 2 || state.VideoBudget != 1_000_000 {
+ t.Fatalf("unchanged connection budget downgraded a healthy output: %+v", state)
+ }
+ sibling.SetBudget(1_020_000)
+ if state := sibling.State(); state.Target != 2 {
+ t.Fatalf("increased capacity downgraded an existing output: %+v", state)
+ }
+ sibling.SetBudget(800_000)
+ if state := sibling.State(); state.Target != 1 {
+ t.Fatalf("actual capacity reduction failed to constrain the output: %+v", state)
+ }
+ receiver.rates[1][0] = 850_000
+ sibling.UpTrackBitrateReport(receiver.available, receiver.rates)
+ sibling.SetBudget(800_000)
+ if state := sibling.State(); state.Target != 1 {
+ t.Fatalf("measured variation downgraded an already constrained output: %+v", state)
+ }
+ receiver.rates[1][0] = 300_000
+ receiver.rates[2][0] = 900_000
+ sibling.SetBudget(1_000_000)
+ if state := sibling.State(); state.Target != 2 {
+ t.Fatalf("increased capacity did not restore the high output: %+v", state)
+ }
+ sibling.SetMaxSpatialLayer(0)
+ if state := sibling.State(); state.Target != 0 {
+ t.Fatalf("stable allocation ignored a changed subscription limit: %+v", state)
+ }
receiver.rates[0][0] = 100_000
// A stopped upper output has no actual bitrate. Admission may prepare it,
// but the library must not select it until it is genuinely available again.
@@ -175,7 +250,9 @@ func TestOutputUsesLibraryAllocationAndExactLifetime(t *testing.T) {
t.Fatalf("preparation exceeded child maximum: %+v", state)
}
output.SetMaxSpatialLayer(2)
- output.current = 2 // Allocation-only fixture; the real RTP gate verifies emitted frames.
+ // Allocation-only fixture: put the accepted frame on a different layer so
+ // BeginFrame must fence it. The real RTP gate verifies emitted frames.
+ output.current = (output.State().Target + 1) % 3
before := output.State()
output.BeginFrame()
after := output.State()
diff --git a/internal/media/forwarding/publication.go b/internal/media/forwarding/publication.go
index 84c1922c..5846105c 100644
--- a/internal/media/forwarding/publication.go
+++ b/internal/media/forwarding/publication.go
@@ -28,6 +28,7 @@ import (
)
const publicationRIDExtension = "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
+const publicationRepairRIDExtension = "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
// Publication sends existing representations over one simulcast connection.
// LiveKit's allocator owns all track allocations and probes on the shared BWE.
@@ -66,9 +67,10 @@ func NewPublication(options TransportOptions) (_ *Publication, err error) {
Config: sendsidebwe.DefaultSendSideBWEConfig, Logger: log,
})}
publication.bandwidth.target.Store(int64(options.InitialBitrate))
- publication.pacer = &publicationPacer{headers: make(map[uint32]publicationHeaders), boundedPacer: newBoundedPacer(
- pacer.NewNoQueue(log, nil), log, options.Source.maxPackets,
+ publication.pacer = &publicationPacer{headers: make(map[uint32]publicationHeaders), boundedPacer: newPacedQueue(
+ log, options.Source.maxPackets, options.InitialBitrate,
)}
+ publication.bandwidth.pacer = publication.pacer.boundedPacer
publication.pacer.sender = pacer.NewBase(log, publication.bandwidth)
publication.allocator = streamallocator.NewStreamAllocator(streamallocator.StreamAllocatorParams{
Config: streamallocator.DefaultStreamAllocatorConfig, BWE: publication.bandwidth,
@@ -77,12 +79,12 @@ func NewPublication(options TransportOptions) (_ *Publication, err error) {
publication.allocator.Start()
media := &webrtc.MediaEngine{}
codec := options.Source.Codec()
- if err = media.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
+ if err = registerVideoCodec(media, codec); err != nil {
return nil, err
}
for _, uri := range []string{
"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
- "urn:ietf:params:rtp-hdrext:sdes:mid", publicationRIDExtension,
+ "urn:ietf:params:rtp-hdrext:sdes:mid", publicationRIDExtension, publicationRepairRIDExtension,
} {
if err = media.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: uri}, webrtc.RTPCodecTypeVideo); err != nil {
return nil, err
@@ -315,7 +317,11 @@ func (publication *Publication) Counters() PublicationCounters {
defer publication.mu.Unlock()
stats := PublicationCounters{Frames: make([]uint64, len(publication.counters)),
AudioBytes: publication.audioBytes.Load(), TargetBitrate: publication.bandwidth.target.Load(),
- Observed: publication.bandwidth.observed.Load(), Limited: publication.bandwidth.CongestionState() == bwe.CongestionStateCongested}
+ Observed: publication.bandwidth.observed.Load(), Limited: !publication.bandwidth.probing.Load() &&
+ publication.bandwidth.CongestionState() == bwe.CongestionStateCongested}
+ if publication.activeCount > 0 && controlledLayerCeiling(publication.source, 0) > publication.LowestLayerBudget() {
+ stats.Limited = true
+ }
for index := range publication.counters {
stats.Frames[index] = publication.counters[index].frames.Load()
stats.VideoBytes += publication.counters[index].bytes.Load()
@@ -487,11 +493,14 @@ type publicationTrack struct {
func (track *publicationTrack) ID() string { return string(track.id) }
func (track *publicationTrack) RID() string { return track.rid }
func (track *publicationTrack) Bind(context webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) {
- var extension, midExtension uint8
+ var extension, repairExtension, midExtension uint8
for _, header := range context.HeaderExtensions() {
if header.URI == publicationRIDExtension {
extension = uint8(header.ID)
}
+ if header.URI == publicationRepairRIDExtension {
+ repairExtension = uint8(header.ID)
+ }
if header.URI == "urn:ietf:params:rtp-hdrext:sdes:mid" {
midExtension = uint8(header.ID)
}
@@ -502,6 +511,10 @@ func (track *publicationTrack) Bind(context webrtc.TrackLocalContext) (webrtc.RT
track.pacer.headersMu.Lock()
track.pacer.headers[uint32(context.SSRC())] = publicationHeaders{rid: track.rid, extension: extension,
mid: track.transceiver.Mid(), midExtension: midExtension, counters: track.counters}
+ if ssrc := context.SSRCRetransmission(); ssrc != 0 {
+ track.pacer.headers[uint32(ssrc)] = publicationHeaders{rid: track.rid, extension: repairExtension,
+ mid: track.transceiver.Mid(), midExtension: midExtension, counters: track.counters}
+ }
track.pacer.headersMu.Unlock()
return track.DownTrack.Bind(context)
}
@@ -557,9 +570,11 @@ func (context publicationAudioContext) WriteStream() webrtc.TrackLocalWriter { r
// in this estimate; subtracting it here would understate the video budget.
type publicationBWE struct {
*sendsidebwe.SendSideBWE
+ pacer *boundedPacer
listener bwe.BWEListener
target atomic.Int64
observed atomic.Bool
+ probing atomic.Bool
}
func (bandwidth *publicationBWE) HandleTWCCFeedback(report *rtcp.TransportLayerCC) {
@@ -571,11 +586,26 @@ func (bandwidth *publicationBWE) SetBWEListener(listener bwe.BWEListener) {
bandwidth.listener = listener
bandwidth.SendSideBWE.SetBWEListener(bandwidth)
}
+
+func (bandwidth *publicationBWE) ProbeClusterStarting(info ccutils.ProbeClusterInfo) {
+ bandwidth.probing.Store(true)
+ bandwidth.SendSideBWE.ProbeClusterStarting(info)
+}
+
+func (bandwidth *publicationBWE) Reset() {
+ bandwidth.SendSideBWE.Reset()
+ bandwidth.probing.Store(false)
+}
+
func (bandwidth *publicationBWE) OnCongestionStateChange(from, to bwe.CongestionState, capacity int64) {
// As for an individual Transport, only a completed non-congesting probe
- // can raise committed capacity. All consumers use that same accepted value.
- if to == bwe.CongestionStateCongested && capacity > 0 && capacity < bandwidth.target.Load() {
+ // can raise capacity. The allocator ignores congestion during a probe; the
+ // encoder-facing budget must retain that same value through settling.
+ if !bandwidth.probing.Load() && to == bwe.CongestionStateCongested && capacity > 0 && capacity < bandwidth.target.Load() {
bandwidth.target.Store(capacity)
+ if bandwidth.pacer != nil {
+ bandwidth.pacer.SetBitrate(int(capacity))
+ }
}
bandwidth.listener.OnCongestionStateChange(from, to, bandwidth.target.Load())
}
@@ -583,6 +613,12 @@ func (bandwidth *publicationBWE) ProbeClusterFinalize() (ccutils.ProbeSignal, in
signal, capacity, done := bandwidth.SendSideBWE.ProbeClusterFinalize()
if done && signal == ccutils.ProbeSignalNotCongesting && capacity > bandwidth.target.Load() {
bandwidth.target.Store(capacity)
+ if bandwidth.pacer != nil {
+ bandwidth.pacer.SetBitrate(int(capacity))
+ }
+ }
+ if done {
+ bandwidth.probing.Store(false)
}
return signal, bandwidth.target.Load(), done
}
@@ -597,7 +633,7 @@ func (queue *publicationPacer) Enqueue(packet *pacer.Packet) {
queue.headersMu.Lock()
headers := queue.headers[packet.Header.SSRC]
queue.headersMu.Unlock()
- if headers.extension != 0 {
+ if headers.extension != 0 && headers.rid != "" {
_ = packet.Header.SetExtension(headers.extension, []byte(headers.rid))
}
if headers.midExtension != 0 {
diff --git a/internal/media/forwarding/publication_test.go b/internal/media/forwarding/publication_test.go
index 9dc2d857..64b5ae43 100644
--- a/internal/media/forwarding/publication_test.go
+++ b/internal/media/forwarding/publication_test.go
@@ -13,7 +13,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/bwe"
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
- "github.com/livekit/livekit-server/pkg/sfu/pacer"
+ packetpacer "github.com/livekit/mediatransportutil/pkg/pacer"
"github.com/livekit/protocol/logger"
"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/twcc"
@@ -31,7 +31,7 @@ func TestPublicationEmptyProbeCannotIncreaseCapacity(t *testing.T) {
})}
bandwidth.target.Store(target)
queue := &publicationPacer{boundedPacer: newBoundedPacer(
- pacer.NewNoQueue(log, nil), log, 500,
+ packetpacer.NewNoQueue(log), log, 500,
)}
defer queue.Stop()
info := ccutils.ProbeClusterInfo{Id: 1, CreatedAt: time.Now(), Goal: ccutils.ProbeClusterGoal{
@@ -125,6 +125,10 @@ func checkPublicationProbeCapacity(t *testing.T, received bool) {
} else if signal != ccutils.ProbeSignalInconclusive || capacity != target {
t.Fatalf("unacknowledged probe changed capacity: signal=%v committed=%d", signal, capacity)
}
+ bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateCongested, target/2)
+ if bandwidth.target.Load() != target/2 {
+ t.Fatal("finalized probe suppressed ordinary media congestion")
+ }
return
}
if time.Now().After(deadline) {
@@ -142,7 +146,9 @@ func (recorder *publicationCapacityRecorder) OnCongestionStateChange(_, _ bwe.Co
func TestPublicationCongestionUsesCommittedCapacity(t *testing.T) {
recorder := &publicationCapacityRecorder{}
- bandwidth := &publicationBWE{listener: recorder}
+ bandwidth := &publicationBWE{listener: recorder, SendSideBWE: sendsidebwe.NewSendSideBWE(sendsidebwe.SendSideBWEParams{
+ Config: sendsidebwe.DefaultSendSideBWEConfig, Logger: logger.GetDiscardLogger(),
+ })}
bandwidth.target.Store(1_200_000)
bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateEarlyWarning, 100_000_000)
bandwidth.OnCongestionStateChange(bwe.CongestionStateEarlyWarning, bwe.CongestionStateCongested, 100_000_000)
@@ -154,10 +160,28 @@ func TestPublicationCongestionUsesCommittedCapacity(t *testing.T) {
if bandwidth.target.Load() != 500_000 || recorder.capacity != 500_000 {
t.Fatalf("publication disagrees with its committed video budget: target=%d allocation=%d", bandwidth.target.Load(), recorder.capacity)
}
+ info := ccutils.ProbeClusterInfo{Id: 1, CreatedAt: time.Now()}
+ bandwidth.ProbeClusterStarting(info)
+ for _, ended := range []bool{false, true} {
+ if ended {
+ info.Result.EndTime = time.Now().UnixNano()
+ bandwidth.ProbeClusterDone(info)
+ }
+ bandwidth.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateCongested, 250_000)
+ if bandwidth.target.Load() != 500_000 || recorder.capacity != 500_000 {
+ t.Fatalf("probe congestion reached encoder/allocation budget (settling=%v): target=%d allocation=%d",
+ ended, bandwidth.target.Load(), recorder.capacity)
+ }
+ }
+ bandwidth.Reset()
+ bandwidth.OnCongestionStateChange(bwe.CongestionStateCongested, bwe.CongestionStateCongested, 250_000)
+ if bandwidth.target.Load() != 250_000 || recorder.capacity != 250_000 {
+ t.Fatal("retired publication probe suppressed ordinary congestion")
+ }
}
func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
- ctx, cancel := context.WithTimeout(t.Context(), 8*time.Second)
+ ctx, cancel := context.WithTimeout(t.Context(), 12*time.Second)
defer cancel()
check := func(err error) {
t.Helper()
@@ -193,12 +217,18 @@ func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
check(mediaEngine.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: publicationRIDExtension}, webrtc.RTPCodecTypeVideo))
check(mediaEngine.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: "urn:ietf:params:rtp-hdrext:sdes:mid"}, webrtc.RTPCodecTypeVideo))
registry := &interceptor.Registry{}
+ reports := &receiverReportGate{}
+ registry.Add(reports)
check(webrtc.RegisterDefaultInterceptors(mediaEngine, registry))
receiver, err := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(registry), webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
check(err)
defer receiver.Close()
var received [3]atomic.Uint32
var invalid atomic.Bool
+ var lastSequence, lowSSRC, missingSequence atomic.Uint32
+ var repaired atomic.Bool
+ deltaFrame := append([]byte(nil), sfu.VP8KeyFrame8x8...)
+ deltaFrame[0] |= 1
receiver.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
index := 2
if track.Kind() == webrtc.RTPCodecTypeVideo {
@@ -212,7 +242,7 @@ func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
}
}
for {
- packet, _, readErr := track.ReadRTP()
+ packet, attributes, readErr := ReadRTP(track)
if readErr != nil {
return
}
@@ -222,11 +252,18 @@ func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
if index < 2 {
var vp8 codecs.VP8Packet
payload, decodeErr := vp8.Unmarshal(packet.Payload)
- if decodeErr != nil || !bytes.Equal(payload, sfu.VP8KeyFrame8x8) {
+ if decodeErr != nil || !(bytes.Equal(payload, sfu.VP8KeyFrame8x8) || bytes.Equal(payload, deltaFrame)) {
invalid.Store(true)
return
}
}
+ if index == 0 {
+ lastSequence.Store(uint32(packet.SequenceNumber))
+ lowSSRC.Store(packet.SSRC)
+ if uint32(packet.SequenceNumber) == missingSequence.Load() && attributes.Get(webrtc.AttributeRtxPayloadType) != nil {
+ repaired.Store(true)
+ }
+ }
received[index].Add(1)
}
})
@@ -263,12 +300,13 @@ func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
t.Fatal("publication did not attach exactly one output per encoding")
}
frame := 0
+ frameData := sfu.VP8KeyFrame8x8
write := func() {
frame++
pts := time.Duration(frame) * time.Second / 30
check(source.BeginFrame(pts, time.Now()))
for layer := 0; layer < 2; layer++ {
- check(source.WriteFrame(layer, encoded.Frame{Data: sfu.VP8KeyFrame8x8, PTS: pts, Duration: time.Second / 30, Recovery: true}))
+ check(source.WriteFrame(layer, encoded.Frame{Data: frameData, PTS: pts, Duration: time.Second / 30, Recovery: bytes.Equal(frameData, sfu.VP8KeyFrame8x8)}))
}
check(audio.WriteSample(media.Sample{Data: []byte{0xf8, 0xff, 0xfe}, Duration: 20 * time.Millisecond}))
}
@@ -286,6 +324,48 @@ func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
if counters := publication.Counters(); counters.Frames[0] == 0 || counters.Frames[1] == 0 || counters.AudioBytes == 0 {
t.Fatalf("successful RTP writes were not accounted per encoding: %+v", counters)
}
+ // Lose a real simulcast packet, then delay its NACK beyond SRTP's replay
+ // window. The publishing path needs the same negotiated repair as P2P.
+ missingSequence.Store(uint32(uint16(lastSequence.Load() + 1)))
+ publication.pacer.mu.Lock()
+ publication.pacer.limit = 0
+ publication.pacer.mu.Unlock()
+ write()
+ for publication.pacer.stats().DroppedPackets < 2 {
+ select {
+ case <-time.After(time.Millisecond):
+ case <-ctx.Done():
+ t.Fatal("publication loss was not exercised")
+ }
+ }
+ publication.pacer.mu.Lock()
+ publication.pacer.limit = source.maxPackets
+ publication.pacer.mu.Unlock()
+ frameData = deltaFrame
+ for range 96 {
+ before := received[0].Load()
+ write()
+ for received[0].Load() == before {
+ select {
+ case <-time.After(time.Millisecond):
+ case <-ctx.Done():
+ t.Fatal("publication stopped before delayed feedback")
+ }
+ }
+ }
+ reports.allow.Store(true)
+ check(receiver.WriteRTCP([]rtcp.Packet{&rtcp.TransportLayerNack{
+ MediaSSRC: lowSSRC.Load(), Nacks: []rtcp.NackPair{{PacketID: uint16(missingSequence.Load())}},
+ }}))
+ for !repaired.Load() {
+ write()
+ select {
+ case <-time.After(10 * time.Millisecond):
+ case <-ctx.Done():
+ t.Fatal("publication did not recover delayed loss through negotiated RTX")
+ }
+ }
+ frameData = sfu.VP8KeyFrame8x8
// Once real bitrate measurements exist, one shared capacity update must
// constrain the sum of both video RID allocations.
for {
@@ -338,6 +418,17 @@ func TestPublicationSimulcastReuseAudioDemandAndClose(t *testing.T) {
if publication.Counters().Limited {
t.Fatal("publication stayed limited after requested low output recovered and high was deactivated")
}
+ // A live encoder can be constrained inside its sole requested RID. The
+ // inactive high RID and a fitting measured rate must not hide that demand.
+ source.SetLowestLayerRateControlled(true)
+ publication.bandwidth.target.Store(80_000)
+ if !publication.Counters().Limited {
+ t.Fatal("publication hid the current encoder's unmet 90 kbps ceiling")
+ }
+ publication.bandwidth.target.Store(1_000_000)
+ if publication.Counters().Limited {
+ t.Fatal("publication kept a limitation after the encoder ceiling was satisfied")
+ }
check(publication.SetActiveCount(2))
for received[1].Load() <= high {
write()
diff --git a/internal/media/forwarding/recovery_test.go b/internal/media/forwarding/recovery_test.go
index 733ad6ed..c502f45b 100644
--- a/internal/media/forwarding/recovery_test.go
+++ b/internal/media/forwarding/recovery_test.go
@@ -31,13 +31,28 @@ func TestTransportConstrainedDormantRecovery(t *testing.T) {
if os.Getenv("PIIK_TRANSPORT_RECOVERY") == "" {
t.Skip("set PIIK_TRANSPORT_RECOVERY=1 for the bounded virtual-network gate")
}
- t.Run("video-only", func(t *testing.T) { checkTransportRecovery(t, false) })
- t.Run("numeric-audio", func(t *testing.T) { checkTransportRecovery(t, true) })
+ for _, scenario := range []string{"video-only", "numeric-audio", "within-lowest-output", "encoded-frame-bursts"} {
+ t.Run(scenario, func(t *testing.T) { checkTransportRecovery(t, scenario) })
+ }
}
-func checkTransportRecovery(t *testing.T, withAudio bool) {
+func checkTransportRecovery(t *testing.T, scenario string) {
t.Helper()
- ctx, cancel := context.WithTimeout(t.Context(), 32*time.Second)
+ withAudio, recoverLowest := scenario == "numeric-audio", scenario == "within-lowest-output"
+ bursts := scenario == "encoded-frame-bursts"
+ duration, recoveredAt := 26*time.Second, 20*time.Second
+ lowBitrate, highBitrate, linkCeiling, recoveredBitrate := 300_000, 1_200_000, 6_000_000, 800_000
+ if recoverLowest {
+ duration, recoveredAt = 50*time.Second, 44*time.Second
+ }
+ if bursts {
+ // Average capacity comfortably fits the video, but a whole 5 Mbps/30 fps
+ // frame exceeds this link's queue. A healthy-link control catches missing
+ // pacing independently from congestion recovery.
+ duration, recoveredAt = 12*time.Second, 4*time.Second
+ lowBitrate, highBitrate, linkCeiling, recoveredBitrate = 1_200_000, 5_000_000, 12_000_000, 4_500_000
+ }
+ ctx, cancel := context.WithTimeout(t.Context(), duration+6*time.Second)
defer cancel()
check := func(err error) {
t.Helper()
@@ -52,7 +67,7 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
check(err)
receiveNet, err := vnet.NewNet(&vnet.NetConfig{StaticIPs: []string{"10.0.0.2"}})
check(err)
- link := vnet.NewTBFQueue(6_000_000, 2400, 7500)
+ link := vnet.NewTBFQueue(linkCeiling, 2400, 7500)
queue, err := vnet.NewQueue(receiveNet, link)
check(err)
defer queue.Close()
@@ -65,9 +80,12 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
RTCPFeedback: []webrtc.RTCPFeedback{{Type: "nack"}, {Type: "nack", Parameter: "pli"}, {Type: webrtc.TypeRTCPFBTransportCC}},
}, PayloadType: 96}
source, err := NewEncodedSource(SourceOptions{ID: "recovery", StreamID: "recovery", Codec: codec,
- Formats: []LayerFormat{{Width: 8, Height: 8, Bitrate: 300_000}, {Width: 16, Height: 16, Bitrate: 1_200_000}}, MaxPackets: 500})
+ Formats: []LayerFormat{{Width: 8, Height: 8, Bitrate: uint32(lowBitrate)}, {Width: 16, Height: 16, Bitrate: uint32(highBitrate)}}, MaxPackets: 500})
check(err)
defer source.Close()
+ // The generated lowest output follows the supplied codec budget, as Native
+ // capture does. Raw SFU forwarding cannot change an upstream encoder's rate.
+ source.SetLowestLayerRateControlled(true)
sendSettings := webrtc.SettingEngine{}
sendSettings.SetNet(sendNet)
sendSettings.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
@@ -101,7 +119,7 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
audioTrack = audio
}
transport, err := NewTransport(TransportOptions{Source: source.Source, Settings: sendSettings,
- ConnectionID: "recovery-child", InitialBitrate: 1_200_000, Audio: audioTrack})
+ ConnectionID: "recovery-child", InitialBitrate: highBitrate, Audio: audioTrack})
check(err)
defer transport.Close()
receiveSettings := webrtc.SettingEngine{}
@@ -190,8 +208,8 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
start, lastReport := time.Now(), time.Now()
var previousReceived, previousAudio uint64
var dormant bool
- var recoveredWindows, audioWindows int
- minimum := int64(1_200_000)
+ var recoveredWindows, audioWindows, lowestWindows int
+ minimum := int64(highBitrate)
frame := 0
for {
select {
@@ -213,16 +231,22 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
}
now := time.Now()
elapsed := now.Sub(start)
- if elapsed >= 26*time.Second {
+ if elapsed >= duration {
break
}
- capacity := 6_000_000
- if elapsed >= 4*time.Second && elapsed < 12*time.Second {
+ capacity := linkCeiling
+ if !bursts && elapsed >= 4*time.Second && elapsed < 12*time.Second {
capacity = 500_000
+ if recoverLowest {
+ capacity = 120_000
+ }
+ }
+ if recoverLowest && elapsed >= 12*time.Second && elapsed < 28*time.Second {
+ capacity = 800_000
}
link.SetRate(capacity)
state := transport.Output.State()
- if state.VideoBudget > 6_000_000 {
+ if state.VideoBudget > int64(linkCeiling) {
t.Fatalf("probe promoted capacity beyond this fixture's link: %+v", state)
}
_, observed := transport.TargetBitrate()
@@ -234,11 +258,11 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
pts := time.Duration(frame) * period
check(source.BeginFrame(pts, now))
transport.Output.BeginFrame()
- for layer, bitrate := range []int64{min(300_000, max(state.VideoBudget, 1)), 1_200_000} {
+ for layer, bitrate := range []int64{min(int64(lowBitrate), max(state.VideoBudget, 1)), int64(highBitrate)} {
if layer == 1 && !high {
continue
}
- data := make([]byte, max(len(sfu.VP8KeyFrame8x8), int(bitrate/8/30)))
+ data := make([]byte, max(len(sfu.VP8KeyFrame8x8), int(source.CodecBudget(layer, bitrate)/8/30)))
copy(data, sfu.VP8KeyFrame8x8)
check(source.WriteFrame(layer, encoded.Frame{Data: data, PTS: pts, Duration: period}))
}
@@ -251,13 +275,22 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
elapsed.Seconds(), capacity, state.VideoBudget, state.Target, state.Current, high, receivedBps,
transport.estimator.CongestionState(), transport.paddingReady, transport.probe.Id, transport.Egress())
transport.mu.Unlock()
- if elapsed >= 20*time.Second && state.Current == 1 && receivedBps > 800_000 {
+ if bursts && elapsed >= 2*time.Second && (state.Target != 1 || state.Current != 1) {
+ t.Fatalf("healthy capacity downgraded during source measurement: %+v", state)
+ }
+ if elapsed >= recoveredAt && state.Current == 1 && receivedBps > float64(recoveredBitrate) {
recoveredWindows++
}
+ // Partial recovery must restore the current output before the larger
+ // spatial output fits; only checking the final fast link misses this.
+ if recoverLowest && capacity == 800_000 && state.Current == 0 &&
+ state.VideoBudget >= 300_000 && state.VideoBudget < 1_200_000 && receivedBps > 250_000 {
+ lowestWindows++
+ }
decoded := audioDecoded.Load()
if withAudio {
t.Logf("audio decoded=%d errors=%d bitrate=%d", decoded-previousAudio, audioErrors.Load(), encoder.Bitrate())
- if elapsed >= 20*time.Second && decoded-previousAudio >= 35 {
+ if elapsed >= recoveredAt && decoded-previousAudio >= 35 {
audioWindows++
}
}
@@ -265,7 +298,10 @@ func checkTransportRecovery(t *testing.T, withAudio bool) {
lastReport, previousReceived = now, bytes
}
}
- if !dormant || minimum >= 1_200_000 || recoveredWindows < 5 || withAudio && (audioWindows < 5 || audioErrors.Load() != 0) {
+ if recoverLowest && lowestWindows < 5 {
+ t.Fatalf("current output remained degraded after partial recovery: windows=%d minimum=%d", lowestWindows, minimum)
+ }
+ if !bursts && (!dormant || minimum >= int64(highBitrate)) || recoveredWindows < 5 || withAudio && (audioWindows < 5 || audioErrors.Load() != 0) {
t.Fatalf("missing constrained/dormant/released recovery: dormant=%v minimum=%d videoWindows=%d audioWindows=%d audioErrors=%d state=%+v",
dormant, minimum, recoveredWindows, audioWindows, audioErrors.Load(), transport.Output.State())
}
diff --git a/internal/media/forwarding/transport.go b/internal/media/forwarding/transport.go
index edaa4490..de760aa6 100644
--- a/internal/media/forwarding/transport.go
+++ b/internal/media/forwarding/transport.go
@@ -18,6 +18,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
"github.com/livekit/livekit-server/pkg/sfu/pacer"
"github.com/livekit/livekit-server/pkg/sfu/streamallocator"
+ packetpacer "github.com/livekit/mediatransportutil/pkg/pacer"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/pion/interceptor"
@@ -74,7 +75,7 @@ func NewTransport(options TransportOptions) (_ *Transport, err error) {
}()
codec := options.Source.Codec()
media := &webrtc.MediaEngine{}
- if err = media.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
+ if err = registerVideoCodec(media, codec); err != nil {
return nil, err
}
if options.Audio != nil {
@@ -135,10 +136,7 @@ func NewTransport(options TransportOptions) (_ *Transport, err error) {
})
transport.estimator.SetBWEListener(transport)
transport.prober = ccutils.NewProber(ccutils.ProberParams{Listener: transport, Logger: log})
- // Match LiveKit's send-side BWE default: a reduced estimate must not throttle
- // already-encoded frames into a stale backlog after the writer recovers.
- transport.pacer = newBoundedPacer(pacer.NewNoQueue(log, nil),
- log, options.Source.maxPackets)
+ transport.pacer = newPacedQueue(log, options.Source.maxPackets, options.InitialBitrate)
transport.pacer.sender = pacer.NewBase(log, transport.estimator)
transport.pacer.SetPacerProbeObserverListener(transport)
track, err := sfu.NewDownTrack(sfu.DownTrackParams{
@@ -154,6 +152,7 @@ func NewTransport(options TransportOptions) (_ *Transport, err error) {
}
transport.Output = NewOutput(track, int64(options.InitialBitrate))
transport.Output.feedbackObserved = transport.observed.Load
+ transport.Output.congestionState = transport.estimator.CongestionState
track.Receiver().(*outputReceiver).output = transport.Output
transport.Output.onActivity = transport.mediaActivity
transport.Output.onDemandChanged = options.OnDemandChanged
@@ -279,6 +278,13 @@ func (transport *Transport) TargetBitrate() (int, bool) {
return int(transport.target.Load()), transport.observed.Load()
}
+func (transport *Transport) BandwidthLimited() bool {
+ transport.mu.Lock()
+ defer transport.mu.Unlock()
+ return transport.Output.BandwidthLimited() || (transport.probe.Id == ccutils.ProbeClusterIdInvalid &&
+ transport.estimator.CongestionState() == bwe.CongestionStateCongested)
+}
+
func (transport *Transport) RequiredActiveCount() int {
state := transport.Output.State()
highest := state.Target
@@ -301,6 +307,9 @@ func (transport *Transport) Egress() EgressStats {
}
func (transport *Transport) refreshBudgetLocked() {
+ if transport.probe.Id == ccutils.ProbeClusterIdInvalid || transport.probe.Result.EndTime != 0 {
+ transport.pacer.SetBitrate(int(transport.target.Load()))
+ }
transport.Output.SetBudget(transport.target.Load())
}
@@ -341,11 +350,13 @@ func (transport *Transport) OnTransportCCFeedback(_ *sfu.DownTrack, packet *rtcp
}
// HandleTWCCFeedback invokes this synchronously under the transport's event
-// lock. Congestion can reduce committed capacity; only a valid completed probe
-// can increase it. Warning/clear transitions can carry the estimator's initial
-// ceiling, especially while a probe is pending, rather than a new measurement.
+// lock. Like LiveKit's allocator, retain committed capacity while a recovery
+// probe is sending or settling; its congestion ends exploration in driveProbeLocked.
+// Outside that probe, congestion can reduce capacity; only a valid completed
+// probe can increase it. Warning/clear transitions are not capacity measurements.
func (transport *Transport) OnCongestionStateChange(_, state bwe.CongestionState, capacity int64) {
- if state == bwe.CongestionStateCongested && capacity > 0 && capacity < transport.target.Load() {
+ if transport.probe.Id == ccutils.ProbeClusterIdInvalid &&
+ state == bwe.CongestionStateCongested && capacity > 0 && capacity < transport.target.Load() {
transport.target.Store(capacity)
}
}
@@ -485,12 +496,14 @@ type EgressStats struct {
Packets, RTPBytes, DroppedPackets uint64
}
-// LiveKit's queue has no capacity limit. Only these bounded tickets enter it;
-// the original packet remains ours until a ticket sends or Stop retires it.
+// The SDK owns packet timing; bounded tickets keep original packet ownership
+// here until a ticket sends or Stop retires it. SFU's unpaced writer assumes
+// already-paced input, which does not hold for locally encoded whole frames.
type boundedPacer struct {
- pacer.Pacer
+ packetpacer.Pacer
sender *pacer.Base
recycle *pacer.Base
+ bitrate atomic.Int64
mu sync.Mutex
pending map[*pacedPacket]struct{}
limit int
@@ -501,11 +514,28 @@ type boundedPacer struct {
last atomic.Int64
}
-func newBoundedPacer(packetPacer pacer.Pacer, log logger.Logger, limit int) *boundedPacer {
+func newPacedQueue(log logger.Logger, limit, bitrate int) *boundedPacer {
+ // A 3 ms service interval spreads native frame bursts across shallow network
+ // queues; retain Pion's 1.5 pacing headroom. The SDK's 200 ms drain target
+ // prevents a stale low estimate from trapping already encoded frames for
+ // seconds; this is not a network latency promise.
+ queue := newBoundedPacer(packetpacer.NewPacerLeakyBucket(3*time.Millisecond,
+ int(float64(bitrate)*1.5), 200*time.Millisecond, log), log, limit)
+ queue.bitrate.Store(int64(bitrate))
+ return queue
+}
+
+func newBoundedPacer(packetPacer packetpacer.Pacer, log logger.Logger, limit int) *boundedPacer {
+ packetPacer.Start()
return &boundedPacer{Pacer: packetPacer, sender: pacer.NewBase(log, nil), recycle: pacer.NewBase(log, nil),
pending: make(map[*pacedPacket]struct{}), limit: limit}
}
+func (queue *boundedPacer) SetBitrate(bitrate int) {
+ queue.bitrate.Store(int64(bitrate))
+ queue.Pacer.SetBitrate(int(float64(bitrate) * 1.5))
+}
+
func (queue *boundedPacer) Enqueue(packet *pacer.Packet) {
queue.mu.Lock()
if queue.closed || len(queue.pending) >= queue.limit {
@@ -516,10 +546,11 @@ func (queue *boundedPacer) Enqueue(packet *pacer.Packet) {
}
ticket := &pacedPacket{owner: queue, packet: packet}
queue.pending[ticket] = struct{}{}
- proxy := pacer.PacketFactory.Get().(*pacer.Packet)
- // The library uses the byte count to pace; it never owns a reference to the
- // pooled payload/header that Stop can release before this ticket is visited.
- *proxy = pacer.Packet{HeaderSize: packet.HeaderSize + len(packet.Payload), WriteStream: ticket}
+ // The SDK reads the cloned header and payload length, then writes through the
+ // ticket. Only our sender reads payload bytes or assigns TWCC identity;
+ // cancellation can release its pools before the scheduling ticket is visited.
+ header := packet.Header.Clone()
+ proxy := &packetpacer.Packet{Header: &header, Payload: packet.Payload, Writer: ticket.WriteRTP}
queue.mu.Unlock()
queue.Pacer.Enqueue(proxy)
}
@@ -537,7 +568,7 @@ func (queue *boundedPacer) cancelProbes(id ccutils.ProbeClusterId) {
if ticket.packet != nil && ticket.packet.IsProbe && ticket.packet.ProbeClusterId == id {
packets = append(packets, ticket.packet)
ticket.packet = nil
- // The header-free proxy still occupies the library queue. Keep its
+ // The scheduling proxy still occupies the library queue. Keep its
// admission slot charged until that proxy is actually dequeued.
}
}
@@ -554,9 +585,11 @@ func (queue *boundedPacer) SetPacerProbeObserverListener(listener pacer.PacerPro
func (queue *boundedPacer) StartProbeCluster(info ccutils.ProbeClusterInfo) {
queue.sender.StartProbeCluster(info)
+ queue.Pacer.SetBitrate(int(float64(info.Goal.DesiredBps) * 1.5))
}
func (queue *boundedPacer) EndProbeCluster(id ccutils.ProbeClusterId) ccutils.ProbeClusterInfo {
+ queue.Pacer.SetBitrate(int(float64(queue.bitrate.Load()) * 1.5))
return queue.sender.EndProbeCluster(id)
}
diff --git a/internal/media/forwarding/transport_test.go b/internal/media/forwarding/transport_test.go
index a0d5ef4f..1e2e66fc 100644
--- a/internal/media/forwarding/transport_test.go
+++ b/internal/media/forwarding/transport_test.go
@@ -12,6 +12,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/bwe"
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
"github.com/livekit/livekit-server/pkg/sfu/pacer"
+ packetpacer "github.com/livekit/mediatransportutil/pkg/pacer"
"github.com/livekit/protocol/logger"
"github.com/pion/interceptor"
"github.com/pion/rtcp"
@@ -22,6 +23,16 @@ import (
)
func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
+ for _, repair := range []bool{true, false} {
+ name := "RTX"
+ if !repair {
+ name = "primary-only"
+ }
+ t.Run(name, func(t *testing.T) { testTransportVideoFeedbackAudioAndClose(t, repair) })
+ }
+}
+
+func testTransportVideoFeedbackAudioAndClose(t *testing.T, repair bool) {
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
check := func(err error) {
@@ -80,11 +91,19 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
t.Fatal("initial configured acquisition demand was lost")
}
mediaEngine := &webrtc.MediaEngine{}
- check(mediaEngine.RegisterDefaultCodecs())
+ if repair {
+ check(mediaEngine.RegisterDefaultCodecs())
+ } else {
+ check(mediaEngine.RegisterCodec(codec, webrtc.RTPCodecTypeVideo))
+ check(mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
+ RTPCodecCapability: audio.Codec(), PayloadType: 111,
+ }, webrtc.RTPCodecTypeAudio))
+ }
registry := &interceptor.Registry{}
- check(webrtc.RegisterDefaultInterceptors(mediaEngine, registry))
reports := &receiverReportGate{}
+ // Inner writer also gates NACKs emitted by the default generator itself.
registry.Add(reports)
+ check(webrtc.RegisterDefaultInterceptors(mediaEngine, registry))
receiver, err := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(registry),
webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
check(err)
@@ -93,13 +112,15 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
firstVideo := make(chan *rtp.Packet, 1)
paddingPackets := make(chan *rtp.Packet, 8)
var recoveryTimestamp atomic.Uint32
- recovered := make(chan struct{}, 1)
+ var missingSequence uint16
+ var videoSSRC uint32
+ recovered := make(chan bool, 1)
var readers sync.WaitGroup
receiver.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
readers.Add(1)
defer readers.Done()
for {
- packet, _, readErr := track.ReadRTP()
+ packet, attributes, readErr := track.ReadRTP()
if readErr != nil {
return
}
@@ -120,7 +141,7 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
}
if packet.Timestamp == recoveryTimestamp.Load() {
select {
- case recovered <- struct{}{}:
+ case recovered <- attributes.Get(webrtc.AttributeRtxPayloadType) != nil:
default:
}
}
@@ -171,6 +192,7 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
case packet := <-firstVideo:
firstFrame = true
recoveryTimestamp.Store(packet.Timestamp + 3000)
+ missingSequence, videoSSRC = packet.SequenceNumber+1, packet.SSRC
for len(keyFrames) > 0 {
<-keyFrames
}
@@ -188,8 +210,9 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
case <-time.After(time.Second):
t.Fatalf("first-frame PLI never reached source; output=%+v statsPLI=%d", transport.Output.State(), transport.Output.GetTrackStats().Plis)
}
- // Force exactly one local queue-admission loss. The following RTP packet
- // exposes its sequence gap to the receiver's normal NACK generator.
+ // Force one local loss, then delay its feedback beyond the receiver's stock
+ // SRTP replay window. Repair must use its negotiated RTX sequence space.
+ // A peer declining RTX still supports ordinary NACK within that window.
transport.pacer.mu.Lock()
transport.pacer.limit = 0
transport.pacer.mu.Unlock()
@@ -206,16 +229,48 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
transport.pacer.mu.Lock()
transport.pacer.limit = source.maxPackets
transport.pacer.mu.Unlock()
- for _, packet := range packetizer.Packetize(sfu.VP8KeyFrame8x8, 3000) {
- check(source.WriteRTP(0, packet))
- }
- select {
- case <-recovered:
- case <-ctx.Done():
- t.Fatal("local admission loss did not recover through library NACK")
+ deltaFrame := append([]byte(nil), sfu.VP8KeyFrame8x8...)
+ deltaFrame[0] |= 1
+ laterPackets := 96
+ if !repair {
+ laterPackets = 16
+ }
+ for range laterPackets {
+ before := videoPackets.Load()
+ for _, packet := range packetizer.Packetize(deltaFrame, 3000) {
+ check(source.WriteRTP(0, packet))
+ }
+ for videoPackets.Load() == before {
+ select {
+ case <-time.After(time.Millisecond):
+ case <-ctx.Done():
+ t.Fatal("media stopped while delaying loss feedback")
+ }
+ }
}
+ reports.allowNACK.Store(true)
+ check(receiver.WriteRTCP([]rtcp.Packet{&rtcp.TransportLayerNack{
+ MediaSSRC: videoSSRC, Nacks: []rtcp.NackPair{{PacketID: missingSequence}},
+ }}))
ticker := time.NewTicker(time.Second / 30)
defer ticker.Stop()
+ for repaired := false; !repaired; {
+ select {
+ case usedRTX := <-recovered:
+ if usedRTX != repair {
+ t.Fatalf("repair path disagrees with negotiation: RTX=%v", usedRTX)
+ }
+ repaired = true
+ case <-ticker.C:
+ // Pion checks its repair queue between primary reads. Keep the
+ // live delta stream flowing without superseding the missing keyframe.
+ for _, packet := range packetizer.Packetize(deltaFrame, 3000) {
+ check(source.WriteRTP(0, packet))
+ }
+ case <-ctx.Done():
+ t.Fatal("delayed local admission loss did not recover through library NACK")
+ }
+ }
for {
_, observed := transport.TargetBitrate()
if videoPackets.Load() >= 15 && audioPackets.Load() >= 15 && observed {
@@ -263,10 +318,10 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
case <-ctx.Done():
t.Fatal("padding packet did not arrive")
}
- // A muted library sender returns zero padding bytes despite a connected PC
+ // A stopped library writer returns zero padding bytes despite a connected PC
// and valid RR. Its probe must finish, and inconclusive feedback must not
// promote the BWE's internal initial-capacity sentinel into a codec budget.
- transport.Output.Mute(true)
+ transport.Output.StopWriteAndGetState()
before, _ := transport.TargetBitrate()
reports.allow.Store(true)
seenProbe := false
@@ -299,7 +354,8 @@ func TestTransportVideoFeedbackAudioAndClose(t *testing.T) {
type receiverReportGate struct {
interceptor.NoOp
- allow atomic.Bool
+ allow atomic.Bool
+ allowNACK atomic.Bool
}
func (gate *receiverReportGate) NewInterceptor(string) (interceptor.Interceptor, error) {
@@ -313,6 +369,9 @@ func (gate *receiverReportGate) BindRTCPWriter(writer interceptor.RTCPWriter) in
}
var filtered []rtcp.Packet
for _, packet := range packets {
+ if _, nack := packet.(*rtcp.TransportLayerNack); nack && !gate.allowNACK.Load() {
+ continue
+ }
if _, report := packet.(*rtcp.ReceiverReport); !report {
filtered = append(filtered, packet)
}
@@ -325,11 +384,11 @@ func (gate *receiverReportGate) BindRTCPWriter(writer interceptor.RTCPWriter) in
}
type heldPacketPacer struct {
- pacer.Pacer
- packets []*pacer.Packet
+ packetpacer.Pacer
+ packets []*packetpacer.Packet
}
-func (held *heldPacketPacer) Enqueue(packet *pacer.Packet) {
+func (held *heldPacketPacer) Enqueue(packet *packetpacer.Packet) {
held.packets = append(held.packets, packet)
}
@@ -358,6 +417,24 @@ func TestTransportDoesNotPromoteUnverifiedCongestionCapacity(t *testing.T) {
if bitrate, _ := transport.TargetBitrate(); bitrate != 500_000 {
t.Fatalf("clearing congestion replaced the committed reduction without a probe: %d", bitrate)
}
+ // A recovery probe deliberately exceeds the current media allocation. Its
+ // congestion, including late feedback while settling, cancels exploration
+ // without cutting the already committed capacity (LiveKit's allocator rule).
+ transport.probe = ccutils.ProbeClusterInfo{Id: 1}
+ for _, ended := range []bool{false, true} {
+ if ended {
+ transport.probe.Result.EndTime = 1
+ }
+ transport.OnCongestionStateChange(bwe.CongestionStateNone, bwe.CongestionStateCongested, 250_000)
+ if bitrate, _ := transport.TargetBitrate(); bitrate != 500_000 {
+ t.Fatalf("probe congestion cut committed media capacity (settling=%v): %d", ended, bitrate)
+ }
+ }
+ transport.probe = ccutils.ProbeClusterInfoInvalid
+ transport.OnCongestionStateChange(bwe.CongestionStateCongested, bwe.CongestionStateCongested, 250_000)
+ if bitrate, _ := transport.TargetBitrate(); bitrate != 250_000 {
+ t.Fatalf("post-probe media congestion did not reduce capacity: %d", bitrate)
+ }
}
type stalledPacketWriter struct {
@@ -436,7 +513,7 @@ func TestSendQueuesDrainAfterBandwidthReduction(t *testing.T) {
func TestBoundedPacerReleasesPacketsAndIsolatesBlockedQueue(t *testing.T) {
log := logger.GetDiscardLogger()
- sender := pacer.NewPassThrough(log, nil)
+ sender := packetpacer.NewPassThrough(log)
held := &heldPacketPacer{Pacer: sender}
queue := newBoundedPacer(held, log, 2)
defer queue.Stop()
@@ -468,7 +545,7 @@ func TestBoundedPacerReleasesPacketsAndIsolatesBlockedQueue(t *testing.T) {
queue.Stop()
queue.Stop()
queue.Enqueue(packet())
- // Upstream may still hold header-free tickets when it stops. Visiting one
+ // Upstream may still hold scheduling tickets when it stops. Visiting one
// after cancellation must neither send nor return the original a second time.
for _, proxy := range held.packets[1:] {
sender.Enqueue(proxy)
@@ -488,7 +565,7 @@ func TestBoundedPacerReleasesPacketsAndIsolatesBlockedQueue(t *testing.T) {
func TestBoundedPacerCanceledProbesRetainAdmission(t *testing.T) {
log := logger.GetDiscardLogger()
- sender := pacer.NewPassThrough(log, nil)
+ sender := packetpacer.NewPassThrough(log)
held := &heldPacketPacer{Pacer: sender}
queue := newBoundedPacer(held, log, 2)
defer queue.Stop()
@@ -530,7 +607,7 @@ func BenchmarkBoundedPacerAdmission(b *testing.B) {
b.Run(name, func(b *testing.B) {
var queue pacer.Pacer = pacer.NewPassThrough(log, nil)
if bounded {
- queue = newBoundedPacer(queue, log, 500)
+ queue = newBoundedPacer(packetpacer.NewPassThrough(log), log, 500)
}
defer queue.Stop()
writer := &countingPacketWriter{}
diff --git a/internal/server/route/availability_deadline_test.go b/internal/server/route/availability_deadline_test.go
new file mode 100644
index 00000000..76480c5d
--- /dev/null
+++ b/internal/server/route/availability_deadline_test.go
@@ -0,0 +1,75 @@
+package route
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestConnectedPeerDeadlinePreservesUntriedFallback(t *testing.T) {
+ for _, prediction := range []bool{false, true} {
+ t.Run(fmt.Sprintf("nat=%t", prediction), func(t *testing.T) {
+ routes := newController(2, Options{SfuEnabled: true, NatPredictionEnabled: prediction, OperationTimeoutMs: 20_000})
+ addViewer(routes, A, 0, nil)
+ operation := must(t, routes.Reconcile(0).Operation)
+ prepared := beginOperation(t, routes, BeginInput{NowMs: 1, ConnectionID: "connected_without_picture", Reservation: direct()})
+ guard := guardFor(A, prepared.Current.Revision, "connected_without_picture")
+ eq(t, routes.CandidateTransportConnected(guard, 100).Accepted, true)
+ eq(t, routes.OperationExpired(operation.WakeAtMs).Accepted, false)
+ result := routes.OperationExpired(operation.DeadlineAtMs)
+ eq(t, result.Accepted, true)
+ eqSlice(t, result.FailedPeerIDs, []string{})
+ noOperation(t, routes.Snapshot().Operation)
+ // Roll back before a fresh bounded operation. An attempted Peer must
+ // not consume or repeatedly jump ahead of an untried SFU route.
+ next := must(t, routes.Reconcile(operation.DeadlineAtMs+1).Operation)
+ eq(t, next.Candidates[next.Cursor].Tuple.Kind, UpstreamSfu)
+ eq(t, routes.CandidateReady(guard, operation.DeadlineAtMs+2, nil, CandidateProof{}).Accepted, false)
+ fallback := beginOperation(t, routes, BeginInput{NowMs: operation.DeadlineAtMs + 3,
+ ConnectionID: "fallback", PublicationGeneration: "publication", PublicationConnectionID: "publisher",
+ Reservation: sfuCreate("subscription", "publication")})
+ eq(t, routes.CandidateReady(guardFor(A, fallback.Current.Revision, "fallback"), operation.DeadlineAtMs+4, nil, CandidateProof{}).Committed, true)
+ eq(t, edgeOf(t, routes, A).Kind, UpstreamSfu)
+ })
+ }
+}
+
+func TestAvailabilityDeadlinesExhaustOnlyTheirBoundedOpportunities(t *testing.T) {
+ for _, prediction := range []bool{false, true} {
+ t.Run(fmt.Sprintf("nat=%t", prediction), func(t *testing.T) {
+ routes := newController(2, Options{SfuEnabled: true, NatPredictionEnabled: prediction, OperationTimeoutMs: 20_000})
+ addViewer(routes, A, 0, nil)
+ peerAttempts, sfuAttempts, now := 0, 0, int64(0)
+ for count := 0; count < 6; count++ {
+ op := routes.Reconcile(now).Operation
+ if op == nil {
+ break
+ }
+ input := BeginInput{NowMs: now + 1, ConnectionID: fmt.Sprintf("attempt_%d", count), Reservation: direct()}
+ kind := op.Candidates[op.Cursor].Tuple.Kind
+ if kind == UpstreamSfu {
+ sfuAttempts++
+ input.PublicationGeneration, input.PublicationConnectionID = "publication", "publisher"
+ input.Reservation = sfuCreate("subscription", "publication")
+ } else {
+ peerAttempts++
+ }
+ prepared := beginOperation(t, routes, input)
+ if kind == UpstreamPeer {
+ routes.CandidateTransportConnected(guardFor(A, prepared.Current.Revision, input.ConnectionID), now+2)
+ }
+ result := routes.OperationExpired(op.DeadlineAtMs)
+ if kind == UpstreamSfu {
+ eqLabels(t, result.Released, "subscription", "publication")
+ }
+ now = op.DeadlineAtMs + 1
+ }
+ wantPeerAttempts := 1
+ if prediction {
+ wantPeerAttempts = 4
+ }
+ eq(t, peerAttempts, wantPeerAttempts)
+ eq(t, sfuAttempts, 1)
+ noOperation(t, routes.Reconcile(now).Operation)
+ })
+ }
+}
diff --git a/internal/server/route/candidates.go b/internal/server/route/candidates.go
index b6afa2ab..78d6f39b 100644
--- a/internal/server/route/candidates.go
+++ b/internal/server/route/candidates.go
@@ -18,6 +18,15 @@ func (c *Controller) buildCandidates(childPeerID string, sfuOnly bool) []Candida
out = append(out, plan)
}
}
+ // Fresh Peer parents still lead. Once every eligible Peer has been tried,
+ // an unconsumed SFU opportunity must precede another NAT retry.
+ if len(out) > 1 && out[0].Tuple.Kind == UpstreamPeer && c.peerAttemptsStarted(childPeerID, out[0].Tuple.ParentPeerID) > 0 {
+ if index := slices.IndexFunc(out, func(plan CandidatePlan) bool { return plan.Tuple.Kind == UpstreamSfu }); index > 0 {
+ fallback := out[index]
+ copy(out[1:index+1], out[:index])
+ out[0] = fallback
+ }
+ }
return out
}
diff --git a/internal/server/route/operation.go b/internal/server/route/operation.go
index ce974257..bb267900 100644
--- a/internal/server/route/operation.go
+++ b/internal/server/route/operation.go
@@ -771,7 +771,7 @@ func (c *Controller) validateOrAdvance(nowMs int64, failedGuard *CandidateGuard)
if sfuBootstrap {
exhausted = failedBootstrapDemand != ""
} else {
- exhausted = !backgroundConvergence && !bootstrapAvailable && !c.hasRemainingNatOpportunity(op)
+ exhausted = !backgroundConvergence && !bootstrapAvailable && !c.hasRemainingAvailabilityOpportunity(op)
}
if bootstrapAvailable {
c.stageSfuBootstrap(op)
@@ -878,7 +878,7 @@ func (c *Controller) validateOrAdvance(nowMs int64, failedGuard *CandidateGuard)
if sfuBootstrap {
exhausted = failedBootstrapDemand != ""
} else {
- exhausted = !backgroundConvergence && !bootstrapAvailable && !c.hasRemainingNatOpportunity(op)
+ exhausted = !backgroundConvergence && !bootstrapAvailable && !c.hasRemainingAvailabilityOpportunity(op)
}
if bootstrapAvailable {
c.stageSfuBootstrap(op)
diff --git a/internal/server/route/opportunities.go b/internal/server/route/opportunities.go
index 96e2645d..a6207d8e 100644
--- a/internal/server/route/opportunities.go
+++ b/internal/server/route/opportunities.go
@@ -105,16 +105,8 @@ func (c *Controller) viewerAttemptsStarted(peerID string) int {
return started
}
-func (c *Controller) hasRemainingNatOpportunity(op *operation) bool {
- if !isAvailabilityOperation(op.reason) || !op.peerAttemptStarted {
- return false
- }
- for _, plan := range c.buildCandidates(op.childPeerID, false) {
- if plan.Tuple.Kind == UpstreamPeer {
- return true
- }
- }
- return false
+func (c *Controller) hasRemainingAvailabilityOpportunity(op *operation) bool {
+ return isAvailabilityOperation(op.reason) && len(c.buildCandidates(op.childPeerID, false)) > 0
}
// consumeCandidateOpportunity exhausts the key at its best rank.
@@ -185,7 +177,10 @@ func (c *Controller) consumeAvailabilityOperation(op *operation) {
if !isAvailabilityOperation(op.reason) {
return
}
- for _, plan := range op.candidates {
+ // A deadline can expire on a connected Peer before the suffix is reached.
+ // Consume only the visited prefix; an untried fallback remains eligible after
+ // rollback, under a new operation with its own unchanged deadline.
+ for _, plan := range op.candidates[:min(op.cursor+1, len(op.candidates))] {
if op.peerAttemptStarted && plan.Tuple.Kind == UpstreamPeer {
continue
}
diff --git a/internal/thirdparty/README.md b/internal/thirdparty/README.md
index 28778519..dc37cbe8 100644
--- a/internal/thirdparty/README.md
+++ b/internal/thirdparty/README.md
@@ -7,7 +7,7 @@ upstream tests and focused regressions are built and tested by
| Module | Source baseline | Local changes |
| --- | --- | --- |
-| `go-nat` | [netbirdio/go-nat at 6b2c8c5c74e8](https://github.com/netbirdio/go-nat/tree/6b2c8c5c74e8331ed41811cfd2fdc4c3dd8c3ff0) | `natpmp.go` uses request contexts, remembers the assigned external port and sends lifetime-zero deletion; regression tests cover the real client and PCPv6 combination |
+| `go-nat` | [netbirdio/go-nat at 6b2c8c5c74e8](https://github.com/netbirdio/go-nat/tree/6b2c8c5c74e8331ed41811cfd2fdc4c3dd8c3ff0) | NAT-PMP honors contexts, assigned ports and deletion; optional PCPv6 finishes inside Piik's mapping budget so it cannot withhold successful IPv4; regressions retain both gateway paths |
| `go-nat-pmp` | [jackpal/go-nat-pmp v1.0.2](https://github.com/jackpal/go-nat-pmp/tree/v1.0.2) | Context-aware request methods, deadline/cancellation through UDP I/O, adapted recorder/test caller, and a module manifest |
Upstream source and tests are retained, excluding examples and CI configuration.
diff --git a/internal/thirdparty/go-nat/nat_test.go b/internal/thirdparty/go-nat/nat_test.go
index 8a791698..8d49761d 100644
--- a/internal/thirdparty/go-nat/nat_test.go
+++ b/internal/thirdparty/go-nat/nat_test.go
@@ -1,3 +1,4 @@
+// Modified by Piik: exercise optional IPv6 against the real caller budget.
package nat
import (
@@ -270,28 +271,18 @@ func TestDualStackReportsPinholeLeftOpenWhenRollbackFails(t *testing.T) {
}
func TestDualStackMappingDoesNotWaitOutAnUnresponsiveIPv6Server(t *testing.T) {
- // Without a bound, the pinhole runs the full RFC 6887 retry schedule and a
- // perfectly good IPv4 mapping waits ~30s behind it.
- restore := pinholeTimeout
- pinholeTimeout = 50 * time.Millisecond
- t.Cleanup(func() { pinholeTimeout = restore })
-
ipv6 := &fakePCPMapper{unresponsive: true}
gateway := newNATWithPCPIPv6(&fakeNAT{port: 4242}, ipv6)
-
- done := make(chan struct{})
- go func() {
- defer close(done)
- port, err := gateway.AddPortMapping(context.Background(), "tcp", 1234, "test", time.Minute)
- if port != 4242 || err != nil {
- t.Errorf("AddPortMapping() = (%d, %v), want (4242, nil)", port, err)
- }
- }()
-
- select {
- case <-done:
- case <-time.After(5 * time.Second):
- t.Fatal("AddPortMapping is still waiting on the IPv6 pinhole")
+ // Use the production child timeout inside Piik's three-second mapping
+ // budget. A shorter test-only timeout hides the competing parent deadline.
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
+ defer cancel()
+ port, err := gateway.AddPortMapping(ctx, "tcp", 1234, "test", time.Minute)
+ if port != 4242 || err != nil {
+ t.Fatalf("AddPortMapping() = (%d, %v), want (4242, nil)", port, err)
+ }
+ if ctx.Err() != nil {
+ t.Fatal("optional IPv6 consumed the caller deadline before returning successful IPv4")
}
if got := gateway.(IPv6PinholeReporter).IPv6PinholeError(); !errors.Is(got, context.DeadlineExceeded) {
diff --git a/internal/thirdparty/go-nat/pcp/nat.go b/internal/thirdparty/go-nat/pcp/nat.go
index 66176e9e..2238cad2 100644
--- a/internal/thirdparty/go-nat/pcp/nat.go
+++ b/internal/thirdparty/go-nat/pcp/nat.go
@@ -1,3 +1,4 @@
+// Modified by Piik: finish optional IPv6 work inside the caller's mapping budget.
package pcp
import (
@@ -13,12 +14,11 @@ import (
// gives no context of its own.
const externalAddressTimeout = 10 * time.Second
-// pinholeTimeout bounds every best-effort IPv6 pinhole operation. The IPv4
-// mapping is what the caller asked for, and it waits on the pinhole so the
-// outcome can be reported, so an unresponsive IPv6 server must not hold it for
-// the full RFC 6887 §8.1.1 mapping schedule. It is a variable so tests can
-// shorten it.
-var pinholeTimeout = 5 * time.Second
+// pinholeTimeout bounds optional IPv6 mapping, health and cleanup work. Keep
+// it inside Piik's three-second mapping budget: joining a silent IPv6 request
+// at the parent deadline can hide an already successful IPv4 result. This is
+// the same local-gateway budget as PCP discovery. Tests may shorten it.
+var pinholeTimeout = 2 * time.Second
var (
// ErrNoNATFound is returned when no PCP-capable gateway can be found.
diff --git a/internal/thirdparty/go-nat/pcp/nat_test.go b/internal/thirdparty/go-nat/pcp/nat_test.go
index 5e51be8f..117aa83f 100644
--- a/internal/thirdparty/go-nat/pcp/nat_test.go
+++ b/internal/thirdparty/go-nat/pcp/nat_test.go
@@ -1,3 +1,4 @@
+// Modified by Piik: exercise optional IPv6 against the real caller budget.
package pcp
import (
@@ -156,9 +157,8 @@ func TestCheckServerHealthAnnouncesBothStacks(t *testing.T) {
}
}
-// A successful IPv4 mapping must not wait out an unresponsive IPv6 server.
-// Unbounded, the pinhole runs the full RFC 6887 §8.1.1 schedule and holds the
-// mapping the caller actually asked for behind it for about 30 seconds.
+// A successful IPv4 mapping must return before the caller's deadline even
+// when the optional IPv6 server is silent.
func TestAddPortMappingDoesNotWaitOutAnUnresponsiveIPv6Server(t *testing.T) {
newFakePCPServer(t, "udp4", "127.0.0.1:5351")
server6 := newFakePCPServer(t, "udp6", "[::1]:5351")
@@ -166,31 +166,22 @@ func TestAddPortMappingDoesNotWaitOutAnUnresponsiveIPv6Server(t *testing.T) {
server6.silent = true
server6.mu.Unlock()
- restore := pinholeTimeout
- pinholeTimeout = 50 * time.Millisecond
- t.Cleanup(func() { pinholeTimeout = restore })
-
client := NewClient(net.ParseIP("127.0.0.1"))
client.SetLocalIP(net.ParseIP("127.0.0.1"))
client6 := NewClient(net.ParseIP("::1"))
client6.SetLocalIP(net.ParseIP("::1"))
nat := &NAT{client: client, client6: client6}
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ // Match Piik's mapping budget without shortening the production child
+ // timeout: that would conceal the competing parent deadline.
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
-
- done := make(chan struct{})
- go func() {
- defer close(done)
- if _, err := nat.AddPortMapping(ctx, "tcp", 1234, "", time.Minute); err != nil {
- t.Errorf("AddPortMapping() error = %v, want the IPv4 mapping to succeed", err)
- }
- }()
-
- select {
- case <-done:
- case <-time.After(5 * time.Second):
- t.Fatal("AddPortMapping is still waiting on the IPv6 pinhole")
+ port, err := nat.AddPortMapping(ctx, "tcp", 1234, "", time.Minute)
+ if port != 1234 || err != nil {
+ t.Fatalf("AddPortMapping() = (%d, %v), want (1234, nil)", port, err)
+ }
+ if ctx.Err() != nil {
+ t.Fatal("optional IPv6 consumed the caller deadline before returning successful IPv4")
}
if err := nat.IPv6PinholeError(); !errors.Is(err, context.DeadlineExceeded) {
diff --git a/internal/thirdparty/go-nat/pcp_ipv6.go b/internal/thirdparty/go-nat/pcp_ipv6.go
index d90102c8..a2f66707 100644
--- a/internal/thirdparty/go-nat/pcp_ipv6.go
+++ b/internal/thirdparty/go-nat/pcp_ipv6.go
@@ -1,3 +1,4 @@
+// Modified by Piik: finish optional IPv6 work inside the caller's mapping budget.
package nat
import (
@@ -10,12 +11,11 @@ import (
"github.com/netbirdio/go-nat/pcp"
)
-// pinholeTimeout bounds every best-effort IPv6 pinhole operation. The IPv4
-// mapping is what the caller asked for, and it waits on the pinhole so the
-// outcome can be reported, so an unresponsive IPv6 server must not hold it for
-// the full RFC 6887 mapping retry schedule. It is a variable so tests can
-// shorten it.
-var pinholeTimeout = 5 * time.Second
+// pinholeTimeout bounds optional IPv6 mapping, health and cleanup work. Keep
+// it inside Piik's three-second mapping budget: joining a silent IPv6 request
+// at the parent deadline can hide an already successful IPv4 result. This is
+// the same local-gateway budget as PCP discovery. Tests may shorten it.
+var pinholeTimeout = 2 * time.Second
var (
// errPinholeRolledBack marks a pinhole that was opened and then closed
diff --git a/package.json b/package.json
index 7524a761..eefb5f6d 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,7 @@
"check:native": "node scripts/check-go.mjs --capture-only",
"gate:app-media": "tsx scripts/app-media-gate.ts",
"gate:network-lab": "tsx scripts/network-lab.ts",
+ "gate:media-lifecycle": "tsx scripts/media-lifecycle-gate.ts",
"gate:app-native-host": "tsx scripts/app-native-host-gate.ts",
"gate:app-browser-fanout": "tsx scripts/app-browser-fanout-gate.ts",
"gate:embedded-sfu": "tsx scripts/embedded-sfu-gate.ts",
diff --git a/scripts/check-go.mjs b/scripts/check-go.mjs
index 2d39b657..dc050543 100644
--- a/scripts/check-go.mjs
+++ b/scripts/check-go.mjs
@@ -222,6 +222,7 @@ if (mode === "--race") {
runGoTests(process.env.PIIK_GO?.trim() || "go", [
"./internal/app/portmapping", "./internal/app/mediaedge",
"./internal/app/nativecontrol", "./internal/app/loopback",
+ "./internal/media/forwarding",
"./internal/server/app",
"github.com/netbirdio/go-nat/...", "github.com/jackpal/go-nat-pmp",
], ["-race", "-count=1", "-timeout=2m"]);
diff --git a/scripts/media-lifecycle-gate.ts b/scripts/media-lifecycle-gate.ts
new file mode 100644
index 00000000..e5062959
--- /dev/null
+++ b/scripts/media-lifecycle-gate.ts
@@ -0,0 +1,201 @@
+import { execFileSync, spawn } from "node:child_process";
+import { createHash } from "node:crypto";
+import { createSocket } from "node:dgram";
+import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises";
+import { networkInterfaces, tmpdir } from "node:os";
+import { join, resolve } from "node:path";
+import { setTimeout as delay } from "node:timers/promises";
+import { parseArgs } from "node:util";
+import { cleanupRun, fetchJsonBefore, reservePort, waitForSample } from "./browser-gate-harness";
+import { Endpoint } from "./network-lab/endpoint-client";
+import type { MediaSample } from "./network-lab/page";
+import { checkAppPath, checkContinuity, hasPicture } from "./network-lab/scenarios";
+
+// Real UI/room/transport on loopback. NAT and bandwidth faults remain in the
+// namespace laboratory; this gate isolates lifecycle ordering from reachability.
+const { values } = parseArgs({ options: { cases: { type: "string" }, repeat: { type: "string", default: "1" },
+ negative: { type: "string" }, list: { type: "boolean" } } });
+if (values.negative && !["frozen-picture", "cancel"].includes(values.negative)) throw new Error("Unknown negative control");
+const cases = [
+ { id: "browser-browser", host: false, viewer: false, sfu: false },
+ { id: "app-browser", host: true, viewer: false, sfu: false },
+ { id: "browser-app", host: false, viewer: true, sfu: false },
+ { id: "app-app", host: true, viewer: true, sfu: false },
+ { id: "sfu-browser", host: false, viewer: false, sfu: true },
+ { id: "fallback-browser", host: false, viewer: false, sfu: false, fallback: true },
+ { id: "fallback-app", host: true, viewer: false, sfu: false, fallback: true },
+];
+const selected = values.cases ? values.cases.split(",").map(id => {
+ const item = cases.find(value => value.id === id);
+ if (!item) throw new Error(`Unknown lifecycle case: ${id}`);
+ return item;
+}) : cases;
+const repeats = Number(values.repeat);
+if (!Number.isInteger(repeats) || repeats < 1 || repeats > 20 || new Set(selected).size !== selected.length) throw new Error("Invalid repetition or duplicated case");
+if (values.list) { process.stdout.write(JSON.stringify(cases, null, 2) + "\n"); process.exit(0); }
+if (!process.env.CHROME_PATH) throw new Error("CHROME_PATH is required");
+const root = resolve(import.meta.dirname, ".."), suffix = process.platform === "win32" ? ".exe" : "";
+const localAddresses = Object.values(networkInterfaces()).flatMap(addresses => addresses?.map(value => value.address) ?? []);
+const output = join(root, "build/media-lifecycle", new Date().toISOString().replaceAll(/[:.]/g, "-"));
+await mkdir(output, { recursive: true });
+const git = (...args: string[]) => execFileSync("git", args, { cwd: root, encoding: "utf8", windowsHide: true }).trim();
+const binaries = await Promise.all(["server", "app"].map(async name => ({ name,
+ sha256: createHash("sha256").update(await readFile(join(root, `build/network-lab/bin/piik-${name}${suffix}`))).digest("hex") })));
+await writeFile(join(output, "environment.json"), JSON.stringify({ commit: git("rev-parse", "HEAD"),
+ status: git("status", "--short"), platform: process.platform, arch: process.arch, binaries, selected, repeats }, null, 2));
+const controller = new AbortController(), signal = controller.signal;
+const interrupt = () => controller.abort(new Error("Lifecycle gate cancelled"));
+process.once("SIGINT", interrupt); process.once("SIGTERM", interrupt);
+// Deliberately do not inherit operator/product settings or credentials.
+const env = { PATH: process.env.PATH, SystemRoot: process.env.SystemRoot, TEMP: process.env.TEMP,
+ TMP: process.env.TMP, HOME: process.env.HOME, USERPROFILE: process.env.USERPROFILE, CHROME_PATH: process.env.CHROME_PATH };
+const summaries: Array<Record<string, unknown>> = [];
+for (let iteration = 0; iteration < repeats && !signal.aborted; iteration++) for (const scenario of selected) {
+ if (signal.aborted) break;
+ const directory = join(output, `${scenario.id}-${iteration + 1}`); await mkdir(directory);
+ const port = await reservePort(), origin = `http://127.0.0.1:${port}`;
+ // Distinct approved origins prevent two local Apps from impersonating one
+ // another. Namespace trials already isolate their loopback services.
+ const viewerOrigin = scenario.host && scenario.viewer ? `http://localhost:${port}` : origin;
+ const udp = createSocket("udp4");
+ const mediaPort = await new Promise<number>((resolvePort, reject) => {
+ udp.once("error", reject); udp.bind(0, "127.0.0.1", () => { const port = udp.address().port; udp.close(() => resolvePort(port)); });
+ });
+ let serverLog = "";
+ const server = spawn(resolve(root, `build/network-lab/bin/piik-server${suffix}`), [], { cwd: directory, stdio: "pipe", windowsHide: true,
+ env: { ...env, PIIK_ENV: "development", PIIK_DEBUG: "route", ROOM_DATABASE_PATH: ":memory:",
+ LISTEN_HOST: "127.0.0.1", PORT: String(port), PUBLIC_BASE_URL: origin, ALLOWED_ORIGINS: [...new Set([origin, viewerOrigin])].join(","),
+ PIIK_LOG_DIR: join(directory, "logs"),
+ ...((scenario.sfu || scenario.fallback) ? { SFU_LISTEN_HOST: "127.0.0.1", SFU_UDP_PORT: String(mediaPort),
+ SFU_PUBLIC_IP: "127.0.0.1", SFU_ONLY: String(scenario.sfu) } : {}) } });
+ let serverFailure: Error | undefined;
+ server.on("error", error => { serverFailure = error; });
+ server.stdout.on("data", data => { serverLog = (serverLog + data).slice(-128_000); });
+ server.stderr.on("data", data => { serverLog = (serverLog + data).slice(-128_000); });
+ const endpoints: Endpoint[] = [];
+ let versions: Array<{ browser: string; app: boolean; appPort: number | null }> = [];
+ const result = { scenario: scenario.id, iteration: iteration + 1, passed: false, stage: "startup", error: "",
+ versions: [] as typeof versions,
+ stages: [] as Array<{ name: string; firstPictureMs: number; route: string | null; peer: number }>,
+ samples: [] as Array<{ stage: string; host: MediaSample; viewer: MediaSample }>, cleanup: [] as unknown[] };
+ const stage = (name: string) => { result.stage = name; process.stdout.write(JSON.stringify({ case: scenario.id, stage: name }) + "\n"); };
+ const sample = async () => {
+ signal.throwIfAborted();
+ const [host, viewer] = await Promise.all(endpoints.map(endpoint => endpoint.call<MediaSample>("sample")));
+ result.samples.push({ stage: result.stage, host: host!, viewer: viewer! });
+ return { host: host!, viewer: viewer! };
+ };
+ const media = async (name: string, previousPeer?: number, expectedRoute = scenario.sfu ? "sfu" : "peer") => {
+ stage(name); const started = performance.now();
+ let first: MediaSample | undefined, firstAt = 0;
+ while (performance.now() - started < 40_000) {
+ const { viewer } = await sample();
+ if (viewer.events.some(event => event.reason === "route-exhausted")) throw new Error("Route exhausted");
+ if (hasPicture(viewer) && viewer.route === expectedRoute && viewer.playbackPeerId !== previousPeer) {
+ if (!first || first.playbackPeerId !== viewer.playbackPeerId) { first = viewer; firstAt = performance.now(); }
+ else if (performance.now() - firstAt >= 3000) {
+ checkContinuity(first, viewer);
+ if (viewer.errors.length) throw new Error(`Viewer errors: ${viewer.errors.join("; ")}`);
+ const last = result.samples.at(-1)!;
+ for (const [index, page] of [last.host, viewer].entries()) {
+ const appPort = versions[index]?.appPort;
+ if (appPort && !page.controlOrigins.includes(`ws://127.0.0.1:${appPort}`)) throw new Error("Page did not attach its own App");
+ }
+ if (scenario.host && expectedRoute === "peer") checkAppPath(last.host, "prepare-edge", localAddresses);
+ if (scenario.viewer && expectedRoute === "peer") checkAppPath(viewer, "receive-offer", localAddresses);
+ result.stages.push({ name, firstPictureMs: firstAt - started, route: viewer.route, peer: viewer.playbackPeerId });
+ return viewer;
+ }
+ } else first = undefined;
+ await delay(250, undefined, { signal });
+ }
+ throw new Error(`No sustained ${expectedRoute} media during ${name}`);
+ };
+ try {
+ await waitForSample(deadline => {
+ if (serverFailure) throw serverFailure;
+ return fetchJsonBefore<{ status: string }>(`${origin}/healthz`, deadline);
+ }, value => value.status === "ok", 15_000);
+ for (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 })));
+ 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 });
+ const invitation = await share();
+ if (scenario.fallback) await endpoints[0]!.call("delay-peer-signals", { delayMs: 15000 });
+ await endpoints[1]!.call("navigate", { url: invitation.replace(origin, viewerOrigin) + (scenario.viewer ? "&piik-client=1" : "") });
+ let received = await media("initial", undefined, scenario.fallback ? "sfu" : undefined);
+ if (scenario.fallback) {
+ if (!result.samples.some(row => row.host.events.some(event => event.type === "test-signal-delayed"))) throw new Error("Signaling fault was not exercised");
+ await endpoints[1]!.call("reset-playback-progress");
+ received = await media("direct-continuation", received.playbackPeerId, "peer");
+ if (received.playbackProgress.renderedFrames < 30 || received.playbackProgress.maxFrameGapMs > 1500) {
+ throw new Error("Working SFU playback stalled during direct continuation");
+ }
+ }
+ if (values.negative === "cancel") {
+ stage("cancel-negative-control"); controller.abort(new Error("Lifecycle gate cancelled")); signal.throwIfAborted();
+ }
+ if (values.negative === "frozen-picture") {
+ stage("frozen-negative-control"); await endpoints[0]!.call("freeze-picture");
+ await delay(2000, undefined, { signal });
+ const frozen = (await sample()).viewer;
+ await delay(3000, undefined, { signal });
+ checkContinuity(frozen, (await sample()).viewer);
+ throw new Error("Frozen source incorrectly passed");
+ }
+ stage("quiet-source"); await endpoints[0]!.call("freeze-picture");
+ await delay(6000, undefined, { signal });
+ const quiet = await sample();
+ if (!quiet.host.sharing || quiet.viewer.playbackPeerId !== received.playbackPeerId) throw new Error("Quiet source retired a healthy route");
+ await endpoints[0]!.call("resume-picture");
+ const resumed = await media("source-resumed");
+ if (resumed.playbackPeerId !== received.playbackPeerId) throw new Error("Source repaint needed a new route");
+ stage("stop"); await endpoints[0]!.call("stop");
+ await share(); received = await media("reshare", received.playbackPeerId);
+ stage("viewer-reload"); await endpoints[1]!.call("reload");
+ received = await media("viewer-rejoined");
+ stage("host-reload"); await endpoints[0]!.call("reload"); await share();
+ received = await media("host-restarted", received.playbackPeerId);
+ stage("join-before-share"); await endpoints[0]!.call("stop"); await endpoints[1]!.call("reload");
+ await share(); await media("waiting-viewer-resumed");
+ if (!scenario.sfu) {
+ stage("replace-during-prepare"); await endpoints[0]!.call("stop");
+ await endpoints[0]!.call("delay-peer-signals", { delayMs: 8000 });
+ await share();
+ await waitForSample(sample, row => row.host.events.some(event => event.type === "test-signal-delayed" &&
+ event.at > row.host.at - 3000), 10_000);
+ await endpoints[0]!.call("stop");
+ await endpoints[0]!.call("delay-peer-signals", { delayMs: 0 });
+ await share(); received = await media("replacement-active");
+ // Release the old real SDP after the new share is active. Stale signaling
+ // must neither replace that route nor turn into a terminal page error.
+ await delay(8500, undefined, { signal });
+ const afterStale = await media("stale-signals-released");
+ if (received.playbackPeerId !== afterStale.playbackPeerId) throw new Error("Retired signaling replaced the current route");
+ }
+ await endpoints[0]!.call("stop"); result.passed = true;
+ } catch (error) { result.error = error instanceof Error ? error.message : String(error); }
+ finally {
+ await Promise.all(endpoints.map(async (endpoint, index) => {
+ await endpoint.call("screenshot", { output: join(directory, `${index}.png`) }, 5000).catch(() => {});
+ await writeFile(join(directory, `endpoint-${index}.json`), JSON.stringify(await endpoint.call("logs", {}, 5000).catch(String), null, 2));
+ result.cleanup.push(await endpoint.call("close", {}, 30_000).catch(error => ({ error: String(error) })));
+ }));
+ for (const endpoint of endpoints) result.cleanup.push(await cleanupRun({ chrome: null, native: null, cdp: null,
+ server: endpoint.process, profile: endpoint.profile, ports: [] }).catch(error => ({ error: String(error) })));
+ result.cleanup.push(await cleanupRun({ chrome: null, native: null, cdp: null, server, profile: null, ports: [port] }));
+ if (result.cleanup.some(item => !Object.values(item as object).every(flag => flag === true))) result.passed = false;
+ if (signal.aborted) { result.passed = false; result.error = "Cancelled"; }
+ await writeFile(join(directory, "result.json"), JSON.stringify(result, null, 2));
+ await writeFile(join(directory, "server.log"), serverLog);
+ const { samples: _samples, ...summary } = result; summaries.push(summary);
+ process.stdout.write(JSON.stringify(summary) + "\n");
+ }
+}
+process.off("SIGINT", interrupt); process.off("SIGTERM", interrupt);
+await writeFile(join(output, "summary.json"), JSON.stringify(summaries, null, 2));
+process.stdout.write(JSON.stringify({ report: output }) + "\n");
+if (signal.aborted || summaries.some(result => !result.passed)) process.exitCode = 1;
diff --git a/scripts/mirror-release.mjs b/scripts/mirror-release.mjs
index bb569c95..903e866b 100644
--- a/scripts/mirror-release.mjs
+++ b/scripts/mirror-release.mjs
@@ -24,33 +24,45 @@ function requireLatestSource() {
}
}
-async function fetchResponse(url, options, timeout) {
+async function fetchResponse(url, options, timeout, consume) {
const attempts = (options.method ?? "GET") === "GET" ? 3 : 1;
for (let attempt = 1; ; attempt++) {
+ const signal = AbortSignal.timeout(timeout);
+ let response;
try {
- const response = await fetch(url, { ...options, signal: AbortSignal.timeout(timeout) });
- if (attempt === attempts || ![408, 429, 500, 502, 503, 504].includes(response.status)) return response;
- await response.body?.cancel();
+ response = await fetch(url, { ...options, signal });
+ // Receiving headers is not a completed read. Keep JSON decoding and the
+ // entire anonymous package/hash verification inside the same retry bound.
+ if (attempt === attempts || ![408, 429, 500, 502, 503, 504].includes(response.status)) {
+ return await consume(response);
+ }
} catch (error) {
- if (attempt === attempts || !(error instanceof TypeError || error?.name === "TimeoutError")) throw error;
+ if (attempt === attempts || !(signal.aborted || error instanceof TypeError || error?.name === "TimeoutError")) {
+ throw new Error(`Gitee ${options.method ?? "GET"} ${new URL(url).pathname}: ${error.message}`);
+ }
+ } finally {
+ if (response?.body && !response.bodyUsed) await response.body.cancel().catch(() => {});
}
+ console.log(`Retry Gitee read ${new URL(url).pathname}: ${attempt + 1}/${attempts}`);
await delay(attempt * 1_000);
}
}
async function request(path, method = "GET", body) {
const multipart = body instanceof FormData;
+ console.log(`Gitee ${method} ${path}`);
// A timed-out upload may already exist. Retry only reads; a script rerun
// reconciles attachment identities before any subsequent write.
- const response = await fetchResponse(api + path, {
+ return fetchResponse(api + path, {
method, redirect: "error",
headers: { Accept: "application/json", Authorization: `Bearer ${token}`,
...(!body || multipart ? {} : { "Content-Type": "application/json" }) },
body: body ? multipart ? body : JSON.stringify(body) : undefined,
- }, multipart ? 180_000 : 20_000);
- if (response.status === 404 && method === "GET") return null;
- if (!response.ok) throw new Error(`Gitee ${method} ${path}: HTTP ${response.status}`);
- return response.json();
+ }, multipart ? 180_000 : 20_000, async response => {
+ if (response.status === 404 && method === "GET") return null;
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ return response.json();
+ });
}
// Verify anonymously: credentials are never forwarded to attachment redirects.
@@ -59,18 +71,20 @@ async function verifyDownload(uploaded, file) {
if (uploaded?.size !== file.size || uploaded.browser_download_url !== expectedURL) {
throw new Error(`Mirror attachment metadata mismatch: ${file.name}`);
}
- const response = await fetchResponse(expectedURL, {}, 180_000);
- if (!response.ok || !response.body) throw new Error(`Mirror attachment is not anonymously downloadable: ${file.name}`);
- const hash = createHash("sha256");
- let size = 0;
- for await (const chunk of response.body) {
- size += chunk.length;
- if (size > file.size) throw new Error(`Mirror attachment size mismatch: ${file.name}`);
- hash.update(chunk);
- }
- if (size !== file.size || hash.digest("hex") !== file.sha256) {
- throw new Error(`Mirror attachment checksum mismatch: ${file.name}`);
- }
+ console.log(`Verify Gitee package: ${file.name} (${file.size} bytes)`);
+ await fetchResponse(expectedURL, {}, 180_000, async response => {
+ if (!response.ok || !response.body) throw new Error(`Mirror attachment is not anonymously downloadable: ${file.name}`);
+ const hash = createHash("sha256");
+ let size = 0;
+ for await (const chunk of response.body) {
+ size += chunk.length;
+ if (size > file.size) throw new Error(`Mirror attachment size mismatch: ${file.name}`);
+ hash.update(chunk);
+ }
+ if (size !== file.size || hash.digest("hex") !== file.sha256) {
+ throw new Error(`Mirror attachment checksum mismatch: ${file.name}`);
+ }
+ });
}
if (option === "--dry-run") {
@@ -113,6 +127,7 @@ if (option === "--dry-run") {
let uploaded = attachments.find((asset) => asset.name === file.name);
if (!uploaded) {
if (!release.prerelease) throw new Error("Published mirror is incomplete; publish a corrected new version");
+ console.log(`Upload Gitee package: ${file.name} (${file.size} bytes)`);
const form = new FormData();
form.append("file", await openAsBlob(file.path), file.name);
uploaded = await request(`/releases/${release.id}/attach_files`, "POST", form);
diff --git a/scripts/network-lab.ts b/scripts/network-lab.ts
index 5a0c9220..0dfbc2d8 100644
--- a/scripts/network-lab.ts
+++ b/scripts/network-lab.ts
@@ -3,11 +3,11 @@ import { once } from "node:events";
import { access, mkdir, mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
-import { createInterface } from "node:readline";
import { parseArgs } from "node:util";
import { setTimeout as delay } from "node:timers/promises";
import { cleanupRun, withDeadline } from "./browser-gate-harness";
import { endpointWorker } from "./network-lab/endpoint";
+import { Endpoint } from "./network-lab/endpoint-client";
import { command, LabNetwork, SITE_IP, SITE_IPV6, SITE_ORIGIN } from "./network-lab/network";
import { checkAppPath, checkCalibration, checkContinuity, checkFaultHit, checkMapping, checkQualityRecovery, hasPicture, selectScenarios, summarizeTrials, type Scenario, type TrialSummary } from "./network-lab/scenarios";
import { echoWorker, type ProbeResult } from "./network-lab/udp";
@@ -21,46 +21,6 @@ const { values } = parseArgs({ options: { worker: { type: "string" }, suite: { t
prediction: { type: "string", default: "on" },
ipv6: { type: "boolean" }, list: { type: "boolean" }, verify: { type: "boolean" }, negative: { type: "string" } } });
-class Endpoint {
- private id = 0;
- private failure: Error | null = null;
- private readonly pending = new Map<number, { resolve: (value: unknown) => void; reject: (error: Error) => void }>();
- readonly process: ChildProcessWithoutNullStreams;
- constructor(namespace: string, readonly profile: string, private readonly signal: AbortSignal) {
- this.process = spawn("ip", ["netns", "exec", namespace, process.execPath, "--import", "tsx", ENTRY, "--worker", "endpoint"],
- { cwd: ROOT, env: { ...labEnvironment(), PIIK_NETWORK_LAB_PROFILE: profile }, stdio: "pipe" });
- this.process.stderr.on("data", data => process.stderr.write(data));
- this.process.stdin.on("error", error => this.fail(error));
- createInterface({ input: this.process.stdout }).on("line", line => {
- try {
- const reply = JSON.parse(line), pending = this.pending.get(reply.id);
- if (!pending) return;
- this.pending.delete(reply.id);
- if (reply.error) pending.reject(new Error(reply.error)); else pending.resolve(reply.result);
- } catch { this.fail(new Error("Invalid endpoint response")); }
- });
- this.process.on("error", error => this.fail(error));
- this.process.on("exit", (code, signal) => this.fail(new Error(`Endpoint exited (${code ?? signal})`)));
- }
- private fail(error: Error): void { this.failure = error; for (const item of this.pending.values()) item.reject(error); this.pending.clear(); }
- async call<T>(action: string, args: Record<string, unknown> = {}, timeout = 45_000): Promise<T> {
- const id = ++this.id;
- if (this.failure) throw this.failure;
- const observeAbort = !["close", "logs", "screenshot"].includes(action);
- if (observeAbort) this.signal.throwIfAborted();
- const abort = () => this.pending.get(id)?.reject(new Error("Laboratory run cancelled"));
- try {
- return await withDeadline(() => new Promise<T>((resolveCall, reject) => {
- this.pending.set(id, { resolve: value => resolveCall(value as T), reject });
- if (observeAbort) this.signal.addEventListener("abort", abort, { once: true });
- this.process.stdin.write(JSON.stringify({ id, action, ...args }) + "\n", error => {
- if (error) { this.pending.delete(id); reject(error); }
- });
- }), Date.now() + timeout);
- } finally { this.pending.delete(id); this.signal.removeEventListener("abort", abort); }
- }
-}
-
function labEnvironment(): NodeJS.ProcessEnv {
return { PATH: process.env.PATH, CHROME_PATH: process.env.CHROME_PATH, LANG: "C.UTF-8", TMPDIR: process.env.TMPDIR };
}
@@ -125,7 +85,7 @@ async function runCase(scenario: Scenario, seed: number, prediction: boolean, ou
await withDeadline(() => once(stun!.stdout, "data"), Date.now() + 5000);
}
for (let i = 0; i < (scenario.sibling ? 3 : 2); i++) endpoints.push(new Endpoint(network.endpoint(i),
- await mkdtemp(join(tmpdir(), "piik-client-media-")), signal));
+ await mkdtemp(join(tmpdir(), "piik-client-media-")), signal, labEnvironment()));
stage("calibration");
const baselines = await Promise.all(endpoints.map(endpoint => endpoint.call<ProbeResult>("probe")));
result.mappingCalibration = baselines;
diff --git a/scripts/network-lab/endpoint-client.ts b/scripts/network-lab/endpoint-client.ts
new file mode 100644
index 00000000..d0634c4c
--- /dev/null
+++ b/scripts/network-lab/endpoint-client.ts
@@ -0,0 +1,47 @@
+import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
+import { resolve } from "node:path";
+import { createInterface } from "node:readline";
+import { withDeadline } from "../browser-gate-harness";
+
+// Both laboratories use the same real UI worker. Only the network substrate differs.
+export class Endpoint {
+ private id = 0;
+ private failure: Error | null = null;
+ private readonly pending = new Map<number, { resolve: (value: unknown) => void; reject: (error: Error) => void }>();
+ readonly process: ChildProcessWithoutNullStreams;
+ constructor(namespace: string | null, readonly profile: string, private readonly signal: AbortSignal, env: NodeJS.ProcessEnv) {
+ const root = resolve(import.meta.dirname, "../..");
+ const args = [process.execPath, "--import", "tsx", resolve(root, "scripts/network-lab.ts"), "--worker", "endpoint"];
+ this.process = spawn(namespace ? "ip" : args.shift()!, namespace ? ["netns", "exec", namespace, ...args] : args,
+ { cwd: root, env: { ...env, PIIK_NETWORK_LAB_PROFILE: profile }, stdio: "pipe", windowsHide: true });
+ this.process.stderr.on("data", data => process.stderr.write(data));
+ this.process.stdin.on("error", error => this.fail(error));
+ createInterface({ input: this.process.stdout }).on("line", line => {
+ try {
+ const reply = JSON.parse(line), pending = this.pending.get(reply.id);
+ if (!pending) return;
+ this.pending.delete(reply.id);
+ if (reply.error) pending.reject(new Error(reply.error)); else pending.resolve(reply.result);
+ } catch { this.fail(new Error("Invalid endpoint response")); }
+ });
+ this.process.on("error", error => this.fail(error));
+ this.process.on("exit", (code, signal) => this.fail(new Error(`Endpoint exited (${code ?? signal})`)));
+ }
+ private fail(error: Error): void { this.failure = error; for (const item of this.pending.values()) item.reject(error); this.pending.clear(); }
+ async call<T>(action: string, args: Record<string, unknown> = {}, timeout = 45_000): Promise<T> {
+ const id = ++this.id;
+ if (this.failure) throw this.failure;
+ const observeAbort = !["close", "logs", "screenshot"].includes(action);
+ if (observeAbort) this.signal.throwIfAborted();
+ const abort = () => this.pending.get(id)?.reject(new Error("Laboratory run cancelled"));
+ try {
+ return await withDeadline(() => new Promise<T>((resolveCall, reject) => {
+ this.pending.set(id, { resolve: value => resolveCall(value as T), reject });
+ if (observeAbort) this.signal.addEventListener("abort", abort, { once: true });
+ this.process.stdin.write(JSON.stringify({ id, action, ...args }) + "\n", error => {
+ if (error) { this.pending.delete(id); reject(error); }
+ });
+ }), Date.now() + timeout);
+ } finally { this.pending.delete(id); this.signal.removeEventListener("abort", abort); }
+ }
+}
diff --git a/scripts/network-lab/endpoint.ts b/scripts/network-lab/endpoint.ts
index 462e640c..641efb7a 100644
--- a/scripts/network-lab/endpoint.ts
+++ b/scripts/network-lab/endpoint.ts
@@ -34,7 +34,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;
- url?: string; output?: string; load?: boolean; ipv6?: boolean; prediction?: boolean; sfuOnly?: boolean; peer?: { address: string; port: number } };
+ url?: string; output?: string; delayMs?: number; load?: boolean; ipv6?: boolean; prediction?: boolean; sfuOnly?: boolean; peer?: { address: string; port: number } };
try {
let result: unknown;
if (request.action === "probe") {
@@ -51,8 +51,8 @@ export async function endpointWorker(): Promise<void> {
await waitForSample(deadline => fetchJsonBefore<{ status: string }>(`${origin}/healthz`, deadline),
value => value.status === "ok", 10_000);
if (request.app) {
- app = spawn(resolve("build/network-lab/bin/piik-app"), ["--site", origin, "--debug", "--config", join(profile, "app.json")],
- { stdio: "pipe", env: { ...process.env, PIIK_CLIENT_GATE_NO_BROWSER: "true", PIIK_LOG_DIR: join(profile, "logs") } });
+ app = spawn(resolve(`build/network-lab/bin/piik-app${process.platform === "win32" ? ".exe" : ""}`), ["--site", origin, "--debug", "--config", join(profile, "app.json")],
+ { stdio: "pipe", windowsHide: true, env: { ...process.env, PIIK_CLIENT_GATE_NO_BROWSER: "true", PIIK_LOG_DIR: join(profile, "logs") } });
app.stderr.on("data", data => log("app", data));
await once(app, "spawn");
ports.push((await readAppEndpoint(app)).port); app.stdout.resume();
@@ -68,18 +68,33 @@ export async function endpointWorker(): Promise<void> {
const version = await waitForVersion(debugPort, chrome);
cdp = await CdpConnection.connect(version.webSocketDebuggerUrl, Date.now() + 10_000);
await cdp.call("Browser.setPermission", { origin, permission: { name: "loopback-network" }, setting: "granted" }, undefined, Date.now() + 5000);
- result = { browser: version.Browser, app: request.app === true };
+ result = { browser: version.Browser, app: request.app === true, appPort: request.app ? ports[0] : null };
} else if (request.action === "navigate") {
- page = await createPage(cdp!, request.url!, pageProbe);
+ if (page) {
+ const oldTimeOrigin = await evaluate<number>(cdp!, page!, "performance.timeOrigin", Date.now() + 5000);
+ const navigation = await cdp!.call<{ loaderId?: string; errorText?: string }>("Page.navigate", { url: request.url! }, page.sessionId, Date.now() + 5000);
+ if (navigation.errorText) throw new Error(navigation.errorText);
+ await waitForSample(deadline => evaluate<boolean>(cdp!, page!,
+ `${navigation.loaderId ? `performance.timeOrigin !== ${oldTimeOrigin} && ` : ""}document.readyState === 'complete' && Boolean(window.networkLab)`, deadline), Boolean, 10_000);
+ } else page = await createPage(cdp!, request.url!, pageProbe);
+ result = true;
+ } else if (request.action === "reload") {
+ const oldTimeOrigin = await evaluate<number>(cdp!, page!, "performance.timeOrigin", Date.now() + 5000);
+ await cdp!.call("Page.reload", {}, page!.sessionId, Date.now() + 5000);
+ await waitForSample(deadline => evaluate<boolean>(cdp!, page!,
+ `performance.timeOrigin !== ${oldTimeOrigin} && document.readyState === 'complete' && Boolean(window.networkLab)`, deadline), Boolean, 10_000);
result = true;
} else if (request.action === "share") {
- await click('button[aria-controls="host-advanced-door"]');
+ const advanced = 'button[aria-controls="host-advanced-door"]';
+ await waitForSample(deadline => evaluate<boolean>(cdp!, page!,
+ `Boolean(document.querySelector(${JSON.stringify(advanced)}))`, deadline), Boolean, 10_000);
+ if (await evaluate<boolean>(cdp!, page!, `document.querySelector(${JSON.stringify(advanced)}).getAttribute('aria-expanded') !== 'true'`, Date.now() + 5000)) await click(advanced);
const prediction = 'button[role="switch"][aria-label^="Enhanced NAT traversal"]';
if (request.sfuOnly) {
if (request.prediction) throw new Error("SFU-only cannot request NAT enhancement");
await waitForSample(deadline => evaluate<boolean>(cdp!, page!,
`Boolean(document.querySelector('button[role="switch"][aria-label^="Server media"][aria-disabled="true"][aria-checked="true"]')) && !document.querySelector(${JSON.stringify(prediction)})`, deadline), Boolean, 10_000);
- } else {
+ } else if (request.prediction !== undefined) {
await waitForSample(deadline => evaluate<boolean>(cdp!, page!,
`document.querySelector(${JSON.stringify(prediction)})?.getAttribute('aria-disabled') !== 'true' && Boolean(document.querySelector(${JSON.stringify(prediction)}))`, deadline), Boolean, 10_000);
const checked = await evaluate<boolean>(cdp!, page!,
@@ -98,12 +113,22 @@ export async function endpointWorker(): Promise<void> {
deadline), Boolean, 10_000);
await click('button[data-source-tab="browser"]');
await click(".lr-source-option.is-browser");
- await waitForSample(sample, value => value.sharing && value.width > 0 && value.natPrediction === request.prediction, 30_000);
+ await waitForSample(sample, value => value.sharing && value.width > 0 &&
+ (request.prediction === undefined || value.natPrediction === request.prediction), 30_000);
result = await evaluate<string>(cdp!, page!, "document.querySelector('.lr-invite-url').value", Date.now() + 5000);
} else if (request.action === "sample") {
result = await sample();
+ } else if (request.action === "delay-peer-signals") {
+ if (!Number.isInteger(request.delayMs) || request.delayMs! < 0 || request.delayMs! > 30_000) throw new Error("Invalid signal delay");
+ await evaluate(cdp!, page!, `networkLab.delayPeerSignals(${request.delayMs})`, Date.now() + 5000);
+ result = true;
+ } else if (request.action === "reset-playback-progress") {
+ await evaluate(cdp!, page!, "networkLab.resetPlaybackProgress()", Date.now() + 5000);
+ result = true;
} else if (request.action === "freeze-picture") {
result = await evaluate(cdp!, page!, "networkLab.freezeCapture()", Date.now() + 5000);
+ } else if (request.action === "resume-picture") {
+ result = await evaluate(cdp!, page!, "networkLab.resumeCapture()", Date.now() + 5000);
} else if (request.action === "signal-probe") {
result = await evaluate(cdp!, page!, "networkLab.signalProbe()", Date.now() + 5000);
} else if (request.action === "logs") {
diff --git a/scripts/network-lab/page.ts b/scripts/network-lab/page.ts
index 617bc01e..6bd640df 100644
--- a/scripts/network-lab/page.ts
+++ b/scripts/network-lab/page.ts
@@ -3,7 +3,18 @@
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,freezeCapture=()=>{};
+ let route=null,natPrediction=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;
+ observedVideo?.cancelVideoFrameCallback(frameCallback);observedVideo=video;
+ if(!video)return;
+ const frame=now=>{
+ if(video!==observedVideo)return;
+ if(lastFrameAt)maxFrameGapMs=Math.max(maxFrameGapMs,now-lastFrameAt);
+ lastFrameAt=now;renderedFrames++;frameCallback=video.requestVideoFrameCallback(frame);
+ };frameCallback=video.requestVideoFrameCallback(frame);
+ };
const PC=RTCPeerConnection, WS=WebSocket;
const picture=document.createElement('canvas');picture.width=64;picture.height=36;
const pictureContext=picture.getContext('2d',{willReadFrequently:true});
@@ -30,7 +41,21 @@ export const pageProbe = String.raw`(() => {
record(events,value);
});
}
- send(raw){if(this.url.includes('/control')){try{const m=JSON.parse(raw);record(controls,{type:m.type,connectionId:m.connectionId,punched:m.punched});}catch{}}super.send(raw);}
+ send(raw){
+ let m;try{m=JSON.parse(raw);}catch{}
+ if(this.url.includes('/control')){
+ if(m)record(controls,{type:m.type,connectionId:m.connectionId,punched:m.punched});
+ }else if(m?.type==='signal'&&peerSignalDelay>0){
+ // Delay real signaling once; never invent SDP, route state or readiness.
+ if(!peerSignalReleaseAt)peerSignalReleaseAt=performance.now()+peerSignalDelay;
+ const remaining=peerSignalReleaseAt-performance.now();
+ if(remaining>0){
+ record(events,{at:performance.now(),type:'test-signal-delayed',connectionId:m.connectionId});
+ setTimeout(()=>{if(this.readyState===WS.OPEN)super.send(raw);},remaining);return;
+ }
+ }
+ super.send(raw);
+ }
};
navigator.mediaDevices.getDisplayMedia=async()=>{
const canvas=document.createElement('canvas'); canvas.width=1280; canvas.height=720;
@@ -42,8 +67,9 @@ export const pageProbe = String.raw`(() => {
ctx.fillRect((x*64+frame*7)%1280,y*60,45,42);
}
ctx.fillStyle='#fff';ctx.font='70px sans-serif';ctx.fillText(String(frame++),80,540);};
- draw();const timer=setInterval(draw,1000/30),stream=canvas.captureStream(30);
- freezeCapture=()=>clearInterval(timer);
+ draw();let timer=setInterval(draw,1000/30);const stream=canvas.captureStream(30);
+ freezeCapture=()=>{clearInterval(timer);timer=null;};
+ resumeCapture=()=>{if(!timer)timer=setInterval(draw,1000/30);};
const video=stream.getVideoTracks()[0],stop=video.stop.bind(video);
video.stop=()=>{clearInterval(timer);stop();};
const ac=new AudioContext(),osc=ac.createOscillator(),dest=ac.createMediaStreamDestination();
@@ -52,7 +78,8 @@ export const pageProbe = String.raw`(() => {
sound.stop=()=>{if(stopped)return;stopped=true;osc.stop();void ac.close();stopSound();};
stream.addTrack(sound);return stream;
};
- window.networkLab={freezeCapture:()=>freezeCapture(),async signalProbe(){
+ window.networkLab={freezeCapture:()=>freezeCapture(),resumeCapture:()=>resumeCapture(),delayPeerSignals:ms=>{peerSignalDelay=ms;peerSignalReleaseAt=0;},
+ 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;
return new Promise(resolve=>{
@@ -89,6 +116,7 @@ export const pageProbe = String.raw`(() => {
}
}
const video=document.querySelector('video');
+ observeVideo(video);
const playbackTrack=video?.srcObject?.getVideoTracks?.()[0];
const playbackPeerId=playbackTrack?peers.findIndex(peer=>peer.getReceivers().some(receiver=>receiver.track===playbackTrack)):-1;
let pictureHash=null;
@@ -99,8 +127,10 @@ export const pageProbe = String.raw`(() => {
}
return{at:performance.now(),width:video?.videoWidth||0,height:video?.videoHeight||0,
frames:video?.getVideoPlaybackQuality().totalVideoFrames||0,pictureHash,playbackPeerId,route,natPrediction,stats,events:[...events],errors:[...errors],controls:[...controls],
+ playbackProgress:{renderedFrames,lastFrameAt,maxFrameGapMs:Math.max(maxFrameGapMs,lastFrameAt?performance.now()-lastFrameAt:0)},
sharing:!!document.querySelector('button[aria-label="Stop sharing"]'),
controlOpen:sockets.some(s=>s.url.includes('/control')&&s.readyState===WS.OPEN),
+ controlOrigins:sockets.filter(s=>s.url.includes('/control')&&s.readyState===WS.OPEN).map(s=>new URL(s.url).origin),
peerStates:peers.map(p=>p.connectionState),text:document.body.innerText.slice(-2000)};
}};
})();`;
@@ -121,6 +151,8 @@ export interface MediaSample {
controls: Array<{ type: string; connectionId?: string }>;
sharing: boolean;
controlOpen: boolean;
+ controlOrigins: string[];
+ playbackProgress: { renderedFrames: number; lastFrameAt: number; maxFrameGapMs: number };
peerStates: string[];
text: string;
}
diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json
index 977239a8..127a721d 100644
--- a/scripts/tsconfig.json
+++ b/scripts/tsconfig.json
@@ -26,6 +26,7 @@
"access-privacy-gate.ts",
"browser-local-pool-probe.ts",
"network-lab.ts",
+ "media-lifecycle-gate.ts",
"app-browser-fanout-gate.ts",
"app-media-gate.ts",
"app-native-host-gate.ts",
diff --git a/src/client/components/living/AppConnectionButton.tsx b/src/client/components/living/AppConnectionButton.tsx
index 845633ee..79453eab 100644
--- a/src/client/components/living/AppConnectionButton.tsx
+++ b/src/client/components/living/AppConnectionButton.tsx
@@ -115,11 +115,11 @@ export function AppConnectionButton({ sourcePicker = false, onConnected }: {
}
const label = t(state === "ready" ? "client.connect.standby" : state === "incompatible" ? "native.incompatible" :
- state === "unavailable" ? "client.connect.unavailable" : sourcePicker ? "client.connect.captureHint" : "client.connect.action");
+ state === "unavailable" ? "client.connect.unavailable" : "client.connect.action");
const failed = state === "unavailable" || state === "incompatible";
return <><Tooltip kind={state === "ready" ? "hint-app-ready" : state === "unavailable" ? "signal-failed" : state === "incompatible" ? "warning" : "hint-client-site"}
text={vis ? undefined : label} toggleOnClick={state === "ready"} disabled={helpOpen}
- place={sourcePicker ? "above" : "below"} align={sourcePicker ? "center" : "end"}>
+ place={sourcePicker ? "above" : "below"} align="end">
<button ref={trigger} type="button" className={`${sourcePicker ? "lr-source-picker-refresh lr-source-app-connect" : "lr-btn lr-app-connect"}${state === "ready" ? " is-ready" : ""}`}
aria-label={sourcePicker ? t("client.connect.action") : label} aria-busy={state === "busy" || undefined}
popoverTarget={failed ? helpId : undefined} aria-haspopup={failed ? "dialog" : undefined}
@@ -131,7 +131,7 @@ export function AppConnectionButton({ sourcePicker = false, onConnected }: {
}} disabled={state === "busy"}>
<Glyph name={state === "ready" ? "check" : state === "busy" ? "loader" : state === "unavailable" || state === "incompatible" ? "alert" : "window"}
size={16} className={state === "busy" ? "lr-spin" : undefined} />
- {vis ? null : <span className="lr-cap">{sourcePicker ? t("client.connect.action") : "App"}</span>}
+ {vis ? null : <span className="lr-cap">App</span>}
</button>
</Tooltip>
<div ref={help} id={helpId} popover="auto" role="dialog" aria-label={t("client.connect.action")} aria-describedby={`${helpId}-message`}
@@ -150,7 +150,13 @@ export function AppConnectionButton({ sourcePicker = false, onConnected }: {
panel.style.top = `${Math.max(8, openAbove ? rect.top - panel.offsetHeight - 8 : rect.bottom + 8)}px`;
panel.querySelector<HTMLButtonElement>("button")?.focus();
}}>
- <div className="lr-app-connect-scene" aria-hidden="true">{helpOpen && <Comic kind={state === "incompatible" ? "warning" : "signal-failed"} theme="paper" />}</div>
+ <div className="lr-app-connect-head">
+ <div className="lr-app-connect-scene" aria-hidden="true">{helpOpen && <Comic kind={state === "incompatible" ? "warning" : "signal-failed"} theme="paper" />}</div>
+ <button type="button" className="lr-btn lr-app-connect-close" aria-label={t("common.close")}
+ popoverTarget={helpId} popoverTargetAction="hide">
+ <Glyph name="x" size={16} />
+ </button>
+ </div>
<p id={`${helpId}-message`} className={vis ? "visually-hidden" : undefined}>{label}</p>
<div className="lr-app-connect-actions">
<button type="button" className="lr-btn" aria-label={t("common.retry")} onClick={() => void connect()}>
diff --git a/src/client/components/living/AppReleaseNotice.tsx b/src/client/components/living/AppReleaseNotice.tsx
index 5acba430..a7e25b9e 100644
--- a/src/client/components/living/AppReleaseNotice.tsx
+++ b/src/client/components/living/AppReleaseNotice.tsx
@@ -26,9 +26,14 @@ export function AppReleaseNotice({ update }: { update: ReleaseUpdateNotice }) {
{vis && <span aria-hidden="true">{update.version}</span>}
</a>
</Tooltip>
- <a href={update.releaseURL} target="_blank" rel="noopener noreferrer" aria-label={t("client.update.notes")}>
- {vis ? <Glyph name="window" size={17} /> : t("client.update.notes")}<span aria-hidden="true"> ↗</span>
- </a>
+ <div className="lr-client-release-links">
+ <a href={update.releaseURL} target="_blank" rel="noopener noreferrer" aria-label={t("client.update.notes")}>
+ {vis ? <Glyph name="window" size={17} /> : t("client.update.notes")}<span aria-hidden="true"> ↗</span>
+ </a>
+ <a href="https://piik.tv/#download" target="_blank" rel="noopener noreferrer" aria-label={t("client.update.downloads")}>
+ {vis ? <Glyph name="arrowDown" size={17} /> : t("client.update.downloads")}<span aria-hidden="true"> ↗</span>
+ </a>
+ </div>
</div>
{major && <p id={warningId} className={vis ? "visually-hidden" : "lr-client-release-warning"}>
{t("client.update.majorHint")}
diff --git a/src/client/components/living/CameraSources.tsx b/src/client/components/living/CameraSources.tsx
index 63591414..aa5a715e 100644
--- a/src/client/components/living/CameraSources.tsx
+++ b/src/client/components/living/CameraSources.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { loadCameraPreviews, type CameraPreview } from "../../media/camera-previews";
import { useCopy } from "../../ui/copy";
import { Glyph } from "../../ui/icons";
@@ -12,17 +12,18 @@ export function useCameraSources(active: boolean, activeVideo?: HTMLVideoElement
const [busy, setBusy] = useState(true);
const [error, setError] = useState<"host.camera.denied" | "host.camera.unavailable" | null>(null);
const [revision, refresh] = useState(0);
+ const pending = useRef<AbortController | null>(null);
useEffect(() => {
if (!active) return;
- const request = new AbortController();
+ const request = pending.current = new AbortController();
setBusy(true); setError(null); setSources([]);
void load(request.signal, result => { if (!request.signal.aborted) setSources(result); }, activeVideo).catch(error => {
if (!request.signal.aborted) setError(error instanceof DOMException && error.name === "NotAllowedError"
? "host.camera.denied" : "host.camera.unavailable");
}).finally(() => { if (!request.signal.aborted) setBusy(false); });
- return () => request.abort();
+ return () => { request.abort(); if (pending.current === request) pending.current = null; };
}, [active, revision, activeVideo, load]);
- return { sources, busy, error, refresh: () => refresh(value => value + 1) };
+ return { sources, busy, error, refresh: () => refresh(value => value + 1), cancel: () => pending.current?.abort() };
}
export function CameraSources({ sources, busy, error, selected, disabled, onSelect }:
@@ -36,7 +37,7 @@ export function CameraSources({ sources, busy, error, selected, disabled, onSele
title={source.label || `${t("host.device.camera")} ${index + 1}`}
action={t("host.sourcePicker.camera", { title: source.label || `${t("host.device.camera")} ${index + 1}` })}
icon="camera" hint="hint-capture-camera" preview={source.preview}
- selected={source.id === selected} disabled={disabled || busy} onSelect={() => onSelect(source.id)} />)}
+ selected={source.id === selected} disabled={disabled} onSelect={() => onSelect(source.id)} />)}
</div>
{busy && <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")}
diff --git a/src/client/components/living/CaptureSourcePicker.tsx b/src/client/components/living/CaptureSourcePicker.tsx
index 24cec432..4e87bdf2 100644
--- a/src/client/components/living/CaptureSourcePicker.tsx
+++ b/src/client/components/living/CaptureSourcePicker.tsx
@@ -101,8 +101,7 @@ export function CaptureSourcePicker({
const browserCapture = activeTab === "browser";
const cameraSources = useCameraSources(activeTab === "camera", activeCameraVideo, loadCameras);
const supportsCaptureBorder = nativeTab && nativeSources.kind === "ready" && nativeSources.captureBorderControl === true;
- const issueKey = nativeSources.kind === "incompatible" ? "native.incompatible"
- : nativeSources.kind === "unavailable" ? "host.sourcePicker.unavailable" : !nativeTab ? null
+ const issueKey = !nativeTab ? null : nativeSources.kind === "incompatible" ? "native.incompatible"
: (nativeSources.kind === "unsupported" || nativeSources.kind === "failed")
? `host.sourcePicker.${nativeSources.kind}` as const
: null;
@@ -338,7 +337,10 @@ export function CaptureSourcePicker({
) : null}
{activeTab === "camera" ? <CameraSources {...cameraSources} selected={initialCamera}
- disabled={selectionDisabled} onSelect={id => onCamera?.(id)} /> : <>
+ disabled={selectionDisabled} onSelect={id => {
+ cameraSources.cancel();
+ onCamera?.(id);
+ }} /> : <>
<div className="lr-source-picker-list">
{browserCapture ? (
<button
@@ -386,12 +388,9 @@ export function CaptureSourcePicker({
{onAppConnected && (nativeSources.kind === "browser" || nativeSources.kind === "unavailable") && (
<div className="lr-source-app-connection">
<AppConnectionButton sourcePicker onConnected={onAppConnected} />
- {vis ? null : <small>{t("client.connect.captureHint")}</small>}
</div>
)}
-
-
{issueKey ? (
<Pill icon="alert" label={t(issueKey)} comic="warning" />
) : browserCapture || activeTab === "camera" ? null : sources.length === 0 ? (
diff --git a/src/client/components/living/RoomInteractions.tsx b/src/client/components/living/RoomInteractions.tsx
index cefb692b..9f0c5a2d 100644
--- a/src/client/components/living/RoomInteractions.tsx
+++ b/src/client/components/living/RoomInteractions.tsx
@@ -186,7 +186,8 @@ function InteractionPanel({ session, state, ...couch }: ConnectedProps & {
: !state.ready ? <><Glyph name="network" size={14} /><span className={vis ? "visually-hidden" : undefined}>{t("interaction.reconnecting")}</span></>
: !vis && <span>{t("interaction.ephemeral")}</span>}
</div>
- {!settingsOpen && draft && <span className="lr-chat-count" aria-hidden="true">{Array.from(draft).length} / {MAX_CHAT_CODE_POINTS}</span>}
+ {!settingsOpen && <span className="lr-chat-count" aria-hidden="true" style={{ visibility: draft ? undefined : "hidden",
+ minWidth: `${String(MAX_CHAT_CODE_POINTS).length * 2 + 3}ch` }}>{Array.from(draft).length} / {MAX_CHAT_CODE_POINTS}</span>}
</footer>
</FloatingPanel>
<div className="lr-interaction-couch" ref={area}>
diff --git a/src/client/components/living/room-interactions.css b/src/client/components/living/room-interactions.css
index 36888236..758367c4 100644
--- a/src/client/components/living/room-interactions.css
+++ b/src/client/components/living/room-interactions.css
@@ -46,7 +46,7 @@
.lr-chat-export { display: grid; justify-items: start; gap: 8px; padding-top: 16px; border-top: 1px solid var(--wall-2); }
.lr-chat-export small { color: var(--muted); font-size: 12px; line-height: 1.6; }
.lr-chat-footer .lr-interaction-status { flex: 1 1 140px; min-width: 0; min-height: 0; margin: 0; overflow-wrap: anywhere; }
-.lr-chat-count { flex: none; margin-left: auto; font-variant-numeric: tabular-nums; }
+.lr-chat-count { flex: none; margin-left: auto; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.lr-interaction-status { display: flex; align-items: center; gap: 6px; min-height: 24px; margin-top: 6px; font-size: 11px; color: var(--muted); }
.lr-person-menu { transform-origin: center bottom; }
.lr-person-menu-body { flex: 1; min-height: 0; padding-top: 12px; overflow: auto; overscroll-behavior: contain; scrollbar-width: thin; }
diff --git a/src/client/locales/en.ts b/src/client/locales/en.ts
index 1e1f212f..fde61a7c 100644
--- a/src/client/locales/en.ts
+++ b/src/client/locales/en.ts
@@ -60,7 +60,6 @@ export const en: Record<CopyKey, string> = {
"client.launch.linkHint": "Create one temporary public invitation link",
"client.launch.room": "Create a room on this device",
"client.connect.action": "Connect Piik App",
- "client.connect.captureHint": "Choose windows or screens with App.",
"client.connect.standby": "App is ready",
"client.connect.standbyHint": "Open a Piik Site and click App at the top to connect.",
"client.connect.remove": "Revoke this Site's App access",
@@ -97,6 +96,7 @@ export const en: Record<CopyKey, string> = {
"client.update.major": "New major Piik release",
"client.update.majorHint": "This update includes breaking changes. Read the release notes before upgrading.",
"client.update.notes": "Release notes",
+ "client.update.downloads": "More downloads",
"client.update.differentBuild": "This build differs from the official release",
"client.update.official": "An official Piik release is available",
@@ -236,7 +236,6 @@ export const en: Record<CopyKey, string> = {
"host.sourcePicker.showCaptureBorder": "Show capture border",
"host.sourcePicker.showCaptureBorderHint": "Turn off to hide the border when Windows allows it. Other captures may keep it visible.",
"host.sourcePicker.empty": "No sources available; refresh the list",
- "host.sourcePicker.unavailable": "Cannot connect to Piik App. Keep the App running, then choose Connect Piik App.",
"host.sourcePicker.unsupported": "App capture is unavailable; check the App diagnostics.",
"host.sourcePicker.failed": "Could not load sources; refresh to retry.",
"native.incompatible": "App and page versions are incompatible; update the App and reload.",
diff --git a/src/client/locales/zh.ts b/src/client/locales/zh.ts
index bff6cb4a..4e6fbfa5 100644
--- a/src/client/locales/zh.ts
+++ b/src/client/locales/zh.ts
@@ -60,7 +60,6 @@ export const zh = {
"client.launch.linkHint": "生成一个临时公网邀请链接",
"client.launch.room": "在这台设备上创建房间",
"client.connect.action": "连接 Piik App",
- "client.connect.captureHint": "使用 App 选择窗口或屏幕。",
"client.connect.standby": "App 已就绪",
"client.connect.standbyHint": "打开 Piik 站点,点击顶部的 App 即可连接。",
"client.connect.remove": "取消此站点的 App 授权",
@@ -97,6 +96,7 @@ export const zh = {
"client.update.major": "发现新的 Piik 大版本",
"client.update.majorHint": "此更新含不兼容改动。升级前请查看版本说明。",
"client.update.notes": "版本说明",
+ "client.update.downloads": "备用下载",
"client.update.differentBuild": "当前构建与官方发行版不同",
"client.update.official": "可获取 Piik 正式发行版",
@@ -236,7 +236,6 @@ export const zh = {
"host.sourcePicker.showCaptureBorder": "显示采集边框",
"host.sourcePicker.showCaptureBorderHint": "关闭后尝试隐藏黄框;Windows 或其他采集程序可能仍要求显示。",
"host.sourcePicker.empty": "暂无可用来源,请刷新列表",
- "host.sourcePicker.unavailable": "未连接 Piik App。请保持 App 运行,点击「连接 Piik App」。",
"host.sourcePicker.unsupported": "App 采集暂不可用,请查看 App 诊断信息。",
"host.sourcePicker.failed": "无法读取来源列表,请刷新重试。",
"native.incompatible": "App 与页面不兼容,请更新 App 并刷新页面。",
diff --git a/src/client/media/viewer-audio.ts b/src/client/media/viewer-audio.ts
index b3140e5b..5e906a38 100644
--- a/src/client/media/viewer-audio.ts
+++ b/src/client/media/viewer-audio.ts
@@ -146,7 +146,8 @@ export class ViewerAudio {
private readonly onNativeVolume = (): void => {
if (this.disposed || this.context) return;
const { volume, muted } = this.video;
- if (this.level === volume && this.muted === muted) return;
+ // sync() also mutes zero volume. Its queued event is not a user mute.
+ if (this.level === volume && (this.muted || this.level === 0) === muted) return;
this.level = volume;
this.muted = muted;
this.onChange(this.snapshot);
diff --git a/src/client/native/client.ts b/src/client/native/client.ts
index dacf2384..8190b448 100644
--- a/src/client/native/client.ts
+++ b/src/client/native/client.ts
@@ -200,7 +200,7 @@ export async function notifyNativePresentation(
signal: AbortSignal,
): Promise<void> {
if (signal.aborted) return;
- const health = await discoverNativeHealth({ signal });
+ const health = await discoverNativeHealth({ signal, waitForPermission: false });
if (!health || signal.aborted) return;
await fetch(`http://127.0.0.1:${health.port}/presentation`, {
method: "POST",
diff --git a/src/client/native/native-viewer-peer.ts b/src/client/native/native-viewer-peer.ts
index a6b56411..ffd95233 100644
--- a/src/client/native/native-viewer-peer.ts
+++ b/src/client/native/native-viewer-peer.ts
@@ -104,19 +104,6 @@ export class NativeCapableViewerPeer implements ViewerMediaPeer {
return;
}
if (payload.kind === "description" && payload.description.type === "offer") {
- const preparation = this.punch;
- this.punch = null;
- const committed = payload.punched && preparation?.peerId === parentPeerId &&
- preparation.connectionId === payload.connectionId && preparation.commit();
- if (!committed) {
- preparation?.dispose();
- if (payload.punched) {
- // An expired optional preparation is a route failure, not evidence
- // that the App's media capability is unavailable for this session.
- this.events.onRecoveryExhausted?.(parentPeerId, payload.connectionId);
- return;
- }
- }
const nextIdentity = {
parentPeerId,
connectionId: payload.connectionId,
@@ -133,6 +120,20 @@ export class NativeCapableViewerPeer implements ViewerMediaPeer {
}
this.identity = nextIdentity;
}
+ const preparation = this.punch;
+ this.punch = null;
+ const committed = payload.punched && preparation?.peerId === parentPeerId &&
+ preparation.connectionId === payload.connectionId && preparation.commit();
+ if (!committed) {
+ preparation?.dispose();
+ if (payload.punched) {
+ // Adopt the offer identity before reporting failure: the route
+ // owner ignores reports from a peer that does not own this edge.
+ // Expiry does not disable the App's media capability.
+ this.events.onRecoveryExhausted?.(parentPeerId, payload.connectionId);
+ return;
+ }
+ }
if (!this.backend) {
const client = offerHasNativeVideoCodec(payload.description.sdp)
? await this.acquireNativeClient().catch(() => null)
diff --git a/src/client/native/quality-preference.ts b/src/client/native/quality-preference.ts
index 19b518e7..92600293 100644
--- a/src/client/native/quality-preference.ts
+++ b/src/client/native/quality-preference.ts
@@ -15,7 +15,7 @@ export class AppQualityPreference {
static async connect(signal: AbortSignal): Promise<AppQualityPreference | null> {
if (signal.aborted) return null;
- const health = await discoverNativeHealth({ signal });
+ const health = await discoverNativeHealth({ signal, waitForPermission: false });
return health?.qualityPreference && !signal.aborted
? new AppQualityPreference(health, signal) : null;
}
diff --git a/src/client/pages/ControlsPreview.tsx b/src/client/pages/ControlsPreview.tsx
index 132c0ffd..e52e2f29 100644
--- a/src/client/pages/ControlsPreview.tsx
+++ b/src/client/pages/ControlsPreview.tsx
@@ -233,6 +233,7 @@ export function ControlsPreview() {
onBrowser={() => { setSourceOpen(false); notify(); }}
onCamera={() => { setSourceOpen(false); notify(); }}
loadCameras={previewCameras}
+ onAppConnected={() => setSourceState("ready")}
onNative={(_target, _audio, showBorder) => { setShowCaptureBorder(showBorder); setSourceOpen(false); notify(); }}
onPreview={previewSource} onRefresh={() => setSourceState("ready")} 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>}
diff --git a/src/client/styles.css b/src/client/styles.css
index 8e891079..661e711e 100644
--- a/src/client/styles.css
+++ b/src/client/styles.css
@@ -547,8 +547,7 @@ body.lr-theater-open .lr-top { visibility: hidden; }
scrollbar-gutter: stable;
}
.lr-source-picker-body > [role="status"] { text-align: center; }
-.lr-source-app-connection { display: grid; justify-items: center; gap: 6px; margin-top: 12px; }
-.lr-source-app-connection small { color: #9aa8bb; font-size: 12px; text-align: center; }
+.lr-source-app-connection { display: flex; justify-content: flex-end; margin-top: 12px; }
.lr-source-app-connect { display: flex; gap: 8px; width: auto; padding: 0 12px; margin: 0; border-radius: 999px; }
.lr-source-picker-list {
display: grid;
@@ -1026,6 +1025,8 @@ html[data-theme="dark"] .lr-invite-url { background: #1a2634; border-color: #334
.lr-app-connect-help p { margin: 12px 0; font-size: 13px; line-height: 1.6; }
.lr-app-connect-help:popover-open { animation: lr-language-open 180ms var(--spring); }
.lr-app-connect-help.is-above { --language-offset: 5px; }
+.lr-app-connect-head { display: flex; align-items: flex-start; gap: 8px; }
+.lr-app-connect-close { flex: 0 0 auto; width: 32px; height: 32px; min-width: 32px; padding: 0; }
.lr-app-connect-scene { width: min(220px, 100%); aspect-ratio: 10 / 3; margin-inline: auto; --comic-repeat: infinite; }
.lr-app-connect-scene > svg { width: 100% !important; }
.lr-app-connect-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
@@ -1263,10 +1264,15 @@ html[data-theme="dark"] .lr-invite-url { background: #1a2634; border-color: #334
.lr-welcome-catalog li > span:first-child { display: block; }
.lr-welcome-catalog .lr-welcome-cipher { margin-top: 6px; }
.lr-input.lr-name-editor {
- width: clamp(140px, 18vw, 200px);
+ flex: 1;
+ min-width: 0;
+ width: 0;
height: 40px;
border-radius: 999px;
}
+.lr-group-name { width: 280px; max-width: 100%; flex-wrap: nowrap; }
+.lr-group-name > .lr-name-hint { flex: 1; width: 0; }
+.lr-group-name .lr-name-tag { width: 100%; }
.lr-name-tag {
display: inline-flex;
@@ -1625,6 +1631,7 @@ html[data-theme="dark"] .lr-door-glyph { color: #7d92a8; }
.lr-client-launch-detail { max-width: 100%; overflow-wrap: anywhere; white-space: pre-wrap; font: 13px/1.6 var(--mono); }
.lr-client-release { display: grid; justify-items: center; gap: 6px; max-width: 100%; }
.lr-client-release .lr-client-project-links { flex-direction: column; align-items: center; gap: 4px; font-size: 12.5px; }
+.lr-client-release-links { display: flex; justify-content: center; flex-wrap: wrap; gap: 4px 20px; }
.lr-client-release-warning { max-width: 42ch; color: var(--muted); font-size: 12px; line-height: 1.6; text-align: center; }
.lr-client-update {
gap: 7px;
@@ -1870,7 +1877,6 @@ html[data-theme="dark"] .lr-switch::after { background: var(--ink); }
}
.lr-viewer-action-cluster > .lr-comic-tip-wrap .lr-btn { width: 100%; }
.lr-viewer-action-separator { display: none; }
- .lr-input.lr-name-editor { width: min(136px, 40vw); }
.lr-group-actions { display: flex; width: 100%; gap: 8px; min-width: 0; }
.lr-host-diagnostics-slot,
.lr-viewer-actions-slot {
diff --git a/src/client/webrtc/nat-prediction.ts b/src/client/webrtc/nat-prediction.ts
index 4df872ef..8b972588 100644
--- a/src/client/webrtc/nat-prediction.ts
+++ b/src/client/webrtc/nat-prediction.ts
@@ -196,9 +196,19 @@ function parseSrflxCandidate(
) {
return null;
}
+ const related = (name: string): string => {
+ const index = fields.findIndex(field => field.toLowerCase() === name);
+ return index < 0 ? "" : fields[index + 1] ?? "";
+ };
+ // One public IP can cover multiple local sockets. Do not spend this
+ // generation's prediction budget on a sequence mixed across their bases.
+ // Redacted/missing related fields retain the bounded Browser hypothesis.
const groupKey = [
candidate.sdpMid ?? "",
candidate.sdpMLineIndex ?? "",
+ fields[1],
+ related("raddr"),
+ related("rport"),
address,
].join("/");
return { candidate, fields, address, port, groupKey };
diff --git a/tests/nat-prediction.test.ts b/tests/nat-prediction.test.ts
index ba8bab54..23e9044f 100644
--- a/tests/nat-prediction.test.ts
+++ b/tests/nat-prediction.test.ts
@@ -271,6 +271,39 @@ describe("NAT prediction ICE adapter", () => {
]);
});
+ it.each([
+ ["component", "base 1 udp", "base 2 udp"],
+ ["address", "raddr 192.0.2.7", "raddr 192.0.2.8"],
+ ["port", "rport 50000", "rport 50001"],
+ ] as const)("keeps the prediction budget when a different base %s interrupts the survey", (_, from, to) => {
+ const sent: SignalCandidate[] = [];
+ const batch = new NatPredictionCandidateBatch(value => sent.push(value));
+ const first = candidate(40_000)!;
+ const second = candidate(40_003)!;
+ const third = candidate(40_006)!;
+ const unrelated = { ...third, candidate: third.candidate.replace(from, to) };
+ batch.add(first);
+ batch.add(second);
+ batch.add(unrelated);
+ expect(sent).toEqual([first, second, unrelated]);
+
+ // A later observation from the original base can still use this
+ // generation's budget; the unrelated one must not consume it first.
+ batch.add(third);
+ expect(sent.filter(value => /^candidate:s[pm]/.test(value?.candidate ?? "")))
+ .toHaveLength(MAX_NAT_PREDICTION_CANDIDATES);
+ expect(sent.filter(value => !/^candidate:s[pm]/.test(value?.candidate ?? "")))
+ .toEqual([first, second, unrelated, third]);
+ });
+
+ it("retains bounded prediction when the Browser conceals related addresses", () => {
+ const concealed = [40_000, 40_003, 40_006].map(port => {
+ const value = candidate(port)!;
+ return { ...value, candidate: value.candidate.replace("raddr 192.0.2.7 rport 50000", "raddr 0.0.0.0 rport 0") };
+ });
+ expect(predictSrflxCandidates(concealed)).toHaveLength(MAX_NAT_PREDICTION_CANDIDATES);
+ });
+
it("does not predict an unstable or incomplete mapping shape", () => {
expect(predictSrflxCandidates([candidate(40_000), candidate(40_001)])).toEqual(
[],
diff --git a/tests/native-capture-selection.test.ts b/tests/native-capture-selection.test.ts
index a3fc714f..5e5c27ac 100644
--- a/tests/native-capture-selection.test.ts
+++ b/tests/native-capture-selection.test.ts
@@ -4,6 +4,7 @@ import { renderToStaticMarkup } from "react-dom/server";
import { CaptureSourcePicker } from "../src/client/components/living/CaptureSourcePicker";
import { AudioExclusionSelect } from "../src/client/components/living/AudioExclusionSelect";
+import { CameraSources } from "../src/client/components/living/CameraSources";
import { setCopy, t } from "../src/client/ui/copy";
import {
@@ -32,21 +33,35 @@ describe("native capture source selection", () => {
const game = target("My Game", "1");
afterEach(() => setCopy({ lang: "en", vis: true }));
+ it("keeps enumerated cameras selectable while optional thumbnails load", () => {
+ const props = {
+ sources: [{ id: "front", label: "Front camera", preview: null }],
+ busy: true, error: null, selected: "", onSelect: () => {},
+ };
+ const html = renderToStaticMarkup(createElement(CameraSources, { ...props, disabled: false }));
+ expect(html).toContain('data-camera-source="front"');
+ expect(html).not.toContain('disabled=""');
+ expect(renderToStaticMarkup(createElement(CameraSources, { ...props, disabled: true })))
+ .toContain('disabled=""');
+ });
+
it.each(["en", "zh", "vis"] as const)("keeps Browser capture reachable with an actionable App mismatch in %s", (mode) => {
const lang = mode === "zh" ? "zh" : "en";
setCopy({ lang, vis: mode === "vis" });
- const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
+ const props = {
nativeSources: { kind: "incompatible" },
- initialTab: "browser",
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
- }));
- expect(html).toContain(t(lang, "native.incompatible"));
+ } as const;
+ const html = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...props, initialTab: "browser" }));
+ expect(html).not.toContain(t(lang, "native.incompatible"));
expect(html).toContain(`aria-label="${t(lang, "host.sourcePicker.browser")}"`);
expect(html).not.toMatch(/data-source-tab="browser"[^>]*disabled=""/);
expect(html).not.toContain(t(lang, "host.sourcePicker.empty"));
- expect(html).toContain('role="status"');
- expect(html).toContain('tabindex="0"');
+ expect(html).not.toContain('role="status"');
+ const nativeTab = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...props, initialTab: "window" }));
+ expect(nativeTab).toContain(t(lang, "native.incompatible"));
+ expect(nativeTab).toContain('role="status"');
});
it("uses the complete window identity as the UI key", () => {
@@ -89,14 +104,14 @@ describe("native capture source selection", () => {
expect(nativeTab).not.toContain('data-source-tab="display"');
expect(nativeTab).toMatch(/data-source-tab="browser"[^>]*aria-selected="true"/);
}
- expect(nativeTab.replaceAll("&#x27;", "'")).toContain(t(lang, `host.sourcePicker.${kind}`));
+ if (kind === "unavailable") expect(nativeTab).not.toContain('role="status"');
+ else expect(nativeTab.replaceAll("&#x27;", "'")).toContain(t(lang, `host.sourcePicker.${kind}`));
expect(nativeTab).not.toContain(t(lang, "host.sourcePicker.empty"));
expect(nativeTab).not.toMatch(/class="lr-source-picker-refresh"[^>]*disabled=""/);
const browserTab = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...props, initialTab: "browser" }));
expect(browserTab).toContain(`aria-label="${t(lang, "host.sourcePicker.browser")}"`);
const browserCopy = browserTab.replaceAll("&#x27;", "'");
- if (kind === "unavailable") expect(browserCopy).toContain(t(lang, "host.sourcePicker.unavailable"));
- else expect(browserCopy).not.toContain(t(lang, `host.sourcePicker.${kind}`));
+ expect(browserCopy).not.toContain('role="status"');
expect(browserTab).not.toMatch(/class="lr-source-option is-browser"[^>]*disabled=""/);
}
});
@@ -150,7 +165,7 @@ describe("native capture source selection", () => {
.toEqual(["browser", "camera"]);
expect(html).toMatch(/data-source-tab="browser"[^>]*aria-selected="true"/);
expect(html).not.toMatch(/data-source-tab="camera"[^>]*disabled=""/);
- expect(html).not.toContain(t("en", "host.sourcePicker.unavailable"));
+ expect(html).not.toContain('role="status"');
});
it("selects a focusable Camera tab when Browser sharing is unavailable", () => {
diff --git a/tests/native-client.test.ts b/tests/native-client.test.ts
index 2555f848..3f234279 100644
--- a/tests/native-client.test.ts
+++ b/tests/native-client.test.ts
@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { discoverNativeConnect, discoverNativeHealth, NativeClient, NativeCompatibilityError, notifyNativePresentation } from "../src/client/native/client";
import { DEFAULT_QUALITY_SETTINGS } from "../src/shared/protocol";
+import { AppQualityPreference } from "../src/client/native/quality-preference";
import {
nativeEventSchema,
@@ -31,6 +32,17 @@ describe("native App private wire", () => {
nativeMedia: { video: true, hardwareH264: true },
};
+ it.each(["prompt", "denied"])("background App preferences do not request local access when permission is %s", async (state) => {
+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/") });
+ vi.stubGlobal("navigator", { permissions: { query: vi.fn().mockResolvedValue({ state }) } });
+ const fetcher = vi.fn(async () => new Response(JSON.stringify(health)));
+ vi.stubGlobal("fetch", fetcher);
+ const controller = new AbortController();
+ await notifyNativePresentation("en", controller.signal);
+ expect(await AppQualityPreference.connect(controller.signal)).toBeNull();
+ expect(fetcher).not.toHaveBeenCalled();
+ });
+
it("discovers only a local approval page without opening native control", async () => {
vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("https://piik.example/r/9527") });
const socket = vi.fn();
@@ -226,7 +238,7 @@ describe("native App private wire", () => {
it.each([NATIVE_CLIENT_PROTOCOL - 1, NATIVE_CLIENT_PROTOCOL + 1])(
"reports observed Piik App protocol %s without opening control or presentation",
async (protocol) => {
- vi.stubGlobal("window", { setTimeout, clearTimeout });
+ vi.stubGlobal("window", { setTimeout, clearTimeout, location: new URL("http://127.0.0.1/") });
const socket = vi.fn();
vi.stubGlobal("WebSocket", socket);
const fetcher = vi.fn(async (url: string) => url === `http://127.0.0.1:${health.port}/health`
@@ -349,6 +361,7 @@ describe("native App private wire", () => {
vi.stubGlobal("window", {
setTimeout: globalThis.setTimeout,
clearTimeout: globalThis.clearTimeout,
+ location: new URL("http://127.0.0.1/"),
});
const fetcher = vi.fn(async (url: string) => url.endsWith("/health")
? new Response(JSON.stringify({
@@ -362,10 +375,10 @@ describe("native App private wire", () => {
const staleNotification = notifyNativePresentation("zh", stale.signal);
stale.abort();
await staleNotification;
- expect(fetcher).toHaveBeenCalledTimes(NATIVE_CLIENT_PORT_END - NATIVE_CLIENT_PORT_START + 1);
- expect(fetcher).toHaveBeenCalledWith("http://127.0.0.1:39721/health", expect.anything());
+ expect(fetcher).not.toHaveBeenCalled();
const current = new AbortController();
await notifyNativePresentation("vis", current.signal);
+ expect(fetcher).toHaveBeenCalledWith("http://127.0.0.1:39721/health", expect.anything());
expect(fetcher).toHaveBeenLastCalledWith("http://127.0.0.1:39721/presentation", expect.objectContaining({
method: "POST", body: JSON.stringify({language: "vis"}), signal: current.signal,
}));
diff --git a/tests/native-quality-preference.test.ts b/tests/native-quality-preference.test.ts
index c716027c..a70a2082 100644
--- a/tests/native-quality-preference.test.ts
+++ b/tests/native-quality-preference.test.ts
@@ -22,7 +22,7 @@ describe("App quality preference transport", () => {
controller.abort();
expect(await AppQualityPreference.connect(controller.signal)).toBeNull();
expect(discoverNativeHealth).toHaveBeenCalledOnce();
- expect(discoverNativeHealth).toHaveBeenCalledWith({ signal: controller.signal });
+ expect(discoverNativeHealth).toHaveBeenCalledWith({ signal: controller.signal, waitForPermission: false });
expect(fetcher).not.toHaveBeenCalled();
});
diff --git a/tests/native-viewer-peer.test.ts b/tests/native-viewer-peer.test.ts
index a6f2cc59..f0d8171a 100644
--- a/tests/native-viewer-peer.test.ts
+++ b/tests/native-viewer-peer.test.ts
@@ -35,6 +35,8 @@ it.each(["commit", "expired", "rpc-expired"])("scopes optional connection prepar
natPredictionStunUrls: ["stun:site.example:3479", "stun:site.example:3480"],
}, { sendSignal: sent, sendRestartRequest: () => true, onStream: () => {}, onUpdate: () => {}, onRecoveryExhausted: exhausted },
{ natPredictionEnabled: true, recoveryOwner: "route" }, async () => client, unavailable, "session", 2);
+ // The page accepts a failure only while this peer owns that connection.
+ exhausted.mockImplementation(() => peer.hasConnectionId("current"));
const prepare: SignalPayload = { kind: "punch", connectionId: "current", punch: { phase: "prepare", secret: "42".repeat(32) } };
try {
await peer.acceptSignal("parent", prepare);
@@ -57,6 +59,7 @@ it.each(["commit", "expired", "rpc-expired"])("scopes optional connection prepar
expect(client.preparePunch).toHaveBeenCalledOnce();
} else {
expect(exhausted).toHaveBeenCalledExactlyOnceWith("parent", "current");
+ expect(exhausted.mock.results[0].value).toBe(true);
if (outcome === "expired") expect(client.receiveOffer).not.toHaveBeenCalled();
}
} finally { peer.dispose(); }
diff --git a/tests/publish-release.test.ts b/tests/publish-release.test.ts
index 5e5321c6..173f5984 100644
--- a/tests/publish-release.test.ts
+++ b/tests/publish-release.test.ts
@@ -46,7 +46,15 @@ describe("Gitee mirror publication", () => {
async function mirror(options: { existing?: "pending" | "published" | "unknown"; corrupt?: boolean;
wrongSource?: boolean; wrongDigest?: boolean; missing?: boolean;
stale?: boolean; supersededDuringUpload?: boolean;
- fault?: { method: string; remaining: number; status: number | "timeout" } } = {}) {
+ downloadFault?: { remaining: number; name: "TimeoutError" | "TypeError" };
+ fault?: { method: string; remaining: number; status: number | "timeout" | "body-timeout" } } = {}) {
+ const interrupted = (bytes: Uint8Array, error: Error) => {
+ let started = false;
+ return new Response(new ReadableStream({ pull(controller) {
+ if (started) controller.error(error);
+ else { started = true; controller.enqueue(bytes.slice(0, 2)); }
+ } }));
+ };
const mutations: string[] = [];
const attachments = sourceAssets.map(({ name, size }) => ({ name, size, browser_download_url: download + name }));
const present = options.existing === "pending" ? attachments.slice(0, 1) : options.missing ? [] : attachments;
@@ -71,6 +79,11 @@ describe("Gitee mirror publication", () => {
if (url.startsWith(download)) {
expect(init?.headers).toBeUndefined();
const file = url.slice(download.length);
+ if (options.downloadFault && options.downloadFault.remaining > 0) {
+ options.downloadFault.remaining--;
+ return interrupted(readFileSync(join(directory, file)), options.downloadFault.name === "TypeError"
+ ? new TypeError("terminated") : new DOMException("Timed out", "TimeoutError"));
+ }
return new Response(options.corrupt ? "corrupt" : readFileSync(join(directory, file)));
}
expect(init?.headers).toHaveProperty("Authorization", "Bearer fixture-mirror-token");
@@ -81,6 +94,9 @@ describe("Gitee mirror publication", () => {
if (options.fault?.method === method && options.fault.remaining > 0) {
options.fault.remaining--;
if (options.fault.status === "timeout") throw new DOMException("Timed out", "TimeoutError");
+ if (options.fault.status === "body-timeout") {
+ return interrupted(new TextEncoder().encode('{"id":42}'), new DOMException("Timed out", "TimeoutError"));
+ }
return new Response(null, { status: options.fault.status });
}
if (path.startsWith("/releases/tags/")) return options.existing
@@ -152,7 +168,7 @@ describe("Gitee mirror publication", () => {
expect(await mirror({ existing: "published", stale: true })).toEqual({ mutations: [], failure: undefined });
});
- it.each([503, "timeout"] as const)("retries transient read failures (%s) within a fixed bound", async (status) => {
+ it.each([503, "timeout", "body-timeout"] as const)("retries transient read failures (%s) within a fixed bound", async (status) => {
const fault = { method: "GET", remaining: 2, status };
expect((await mirror({ existing: "published", fault })).failure).toBeUndefined();
expect(fault.remaining).toBe(0);
@@ -161,15 +177,27 @@ describe("Gitee mirror publication", () => {
expect(fault.remaining).toBe(1);
});
+ it.each(["TimeoutError", "TypeError"] as const)("restarts interrupted downloads from byte zero (%s)", async (name) => {
+ const downloadFault = { remaining: 2, name };
+ expect((await mirror({ existing: "published", downloadFault })).failure).toBeUndefined();
+ downloadFault.remaining = 4;
+ const failed = await mirror({ downloadFault });
+ expect(failed.failure).toBeDefined();
+ expect(downloadFault.remaining).toBe(1);
+ expect(failed.mutations).not.toContain("PATCH /releases/42");
+ });
+
it("does not repeat forbidden reads or ambiguous writes", async () => {
const denied = { method: "GET", remaining: 2, status: 403 };
expect((await mirror({ fault: denied })).failure).toContain("HTTP 403");
expect(denied.remaining).toBe(1);
- const write = { method: "POST", remaining: 2, status: "timeout" as const };
- const result = await mirror({ fault: write });
- expect(result.failure).toBeDefined();
- expect(result.mutations).toEqual(["POST /releases"]);
- expect(write.remaining).toBe(1);
+ for (const status of ["timeout", "body-timeout"] as const) {
+ const write = { method: "POST", remaining: 2, status };
+ const result = await mirror({ fault: write });
+ expect(result.failure).toBeDefined();
+ expect(result.mutations).toEqual(["POST /releases"]);
+ expect(write.remaining).toBe(1);
+ }
});
it.each([{ wrongSource: true }, { wrongDigest: true }, { existing: "unknown" as const }])
diff --git a/tests/viewer-audio.test.ts b/tests/viewer-audio.test.ts
index f2c5f5eb..8245e953 100644
--- a/tests/viewer-audio.test.ts
+++ b/tests/viewer-audio.test.ts
@@ -183,4 +183,27 @@ describe("Viewer local audio", () => {
expect(video.muted).toBe(false);
expect(onChange).not.toHaveBeenCalled();
});
+
+ it.each(["absent", "failed"])("restores zero volume when the audio context is %s", (state) => {
+ vi.stubGlobal("AudioContext", state === "absent" ? undefined : class {
+ constructor() { throw new DOMException("Audio unavailable", "NotSupportedError"); }
+ });
+ const video = new PlaybackVideo();
+ const audio = new ViewerAudio(video as unknown as HTMLVideoElement, vi.fn());
+ audio.setLevel(0);
+ // HTMLMediaElement queues this for both volume and muted writes.
+ video.dispatchEvent(new Event("volumechange"));
+ audio.setLevel(0.5);
+ video.dispatchEvent(new Event("volumechange"));
+ expect(audio.snapshot).toEqual({ level: 0.5, muted: false, boostAvailable: false });
+ expect(video.muted).toBe(false);
+ audio.setMuted(true);
+ audio.setLevel(0);
+ video.dispatchEvent(new Event("volumechange"));
+ audio.setLevel(0.5);
+ video.dispatchEvent(new Event("volumechange"));
+ expect(audio.snapshot.muted).toBe(true);
+ expect(video.muted).toBe(true);
+ audio.dispose();
+ });
});