Files
piik/docs/research/node-local-adaptation.md
T
AAA 066e218d84 fix(media): improve connection recovery and capture compatibility (#498)
## Problem and outcome

Stale ICE and transport callbacks, interrupted media ownership, and optional encoder capabilities could break otherwise recoverable sharing sessions. This maintenance phase corrects those owners across Web, Native and embedded SFU, improves current-state diagnostics, and finishes the affected UI and documentation animation paths.

Android, QR invitations and App/Server headline presentation stay on their feature branch. This PR contains the accepted maintenance scope only.

## Release notes

**Play together. Your turn!**

Approved friends can play together with shared keyboard and mouse controls or optional gamepads.

**远程同乐,轮到你啦!**

获准的朋友可以共用键鼠,或使用可选手柄,一起玩本地多人游戏。

### English

This maintenance update improves connection recovery, capture compatibility and troubleshooting for existing Piik rooms.

- Keep ICE candidates and gathering completion tied to their current connection. Preserve Viewer route preferences through reconnection and retry newly available Peer opportunities after SFU fallback.
- Correct picture and voice ownership during restart, source replacement and connection retirement. Report failed picture dependencies while keeping room voice available.
- Separate capture and forwarding statistics and isolate pooled sender attachment failures.
- Improve Windows H.264 compatibility with optional encoder properties, retain safe capture failure codes, and keep shared sound volume usable without microphone support.
- Improve remote input continuity and mouse responsiveness after idle.
- Fix fullscreen handling in some mobile browsers, source-picker refresh, delayed chat confirmations, starred-site startup and local App requests. Refine control motion and reduced-motion behavior.
- Restore the mascot's face and shared animation lifecycle in the documentation header.
- Include current connection and playback state in diagnostic exports, update Zod to 4.6.6 and simplify shared lifecycle code.

Existing 2.x protocols, configuration and default quality settings are preserved.

### 中文

本次维护更新改善现有房间的连接恢复、采集兼容性和故障诊断。

- ICE 候选和收集结束事件跟随当前连接;重连保留观众线路偏好,SFU 兜底后出现新的可用 P2P 机会仍可尝试。
- 修正重启、换源和连接结束时的画面与语音生命周期;语音继续时,画面失败会明确反馈。
- 区分采集与转发统计,隔离编码池中单个输出连接的失败。
- 改善 Windows H.264 可选属性兼容性,保留有效的采集失败原因;没有麦克风能力时也能调整共享声音音量。
- 修复部分移动浏览器全屏、选源刷新、迟到聊天确认、收藏站点启动和本机 App 请求问题,打磨控件动效及减少动态效果行为。
- 修复文档导航 mascot 面部一直隐藏的问题,接回共用动画生命周期。
- 诊断导出补取当前连接与播放状态,升级 Zod 至 4.6.6,精简共用生命周期代码。

沿用现有 2.x 协议、配置和画质默认值。Android、二维码邀请及 App/Server 新增的 HEADLINE 展示留待后续版本。

## Verification

- `npm run check`: type-check, 98 test files / 1,765 tests, production Web build passed.
- `npm run check:go-core`: Go formatting, tests, vet, Windows/Linux builds passed; Darwin builds require the native CI runners.
- `npm run check:native`: current Windows capture compilation, fixtures and capability probe passed.
- `npm run build:website`, TypeScript and repository hygiene checks passed after the documentation-brand fix.
- Existing controlled media lifecycle and P2P/SFU voice acceptance covered the retained route/transport changes. Final local browser acceptance covered actual sharing and pause/resume, fullscreen, source refresh, voice/keyboard/settings, 320px to desktop layouts, both themes and all presentation modes. Documentation brand was reproduced broken, then verified on load, replay and reduced motion.
- Independent scoped reviews found no integration blocker.

## Impact

Patch release v2.4.1. Existing 2.x wire, configuration, authority, capacity and default quality contracts remain intact; no data migration. Source volume no longer depends on microphone availability. Zod 4.6.6 fixes GHSA-mrhx-c7xc-m3w5.

The device-specific Windows reports #495 and #452 remain under investigation; this PR improves compatibility and diagnostics without claiming those reports fully resolved. Physical macOS acceptance remains with its native runner/device checks.
2026-10-24 12:30:39 -04:00

15 KiB

Node-Local Adaptive Encoded Reuse

Initial review: 2026-09-07. ADR-0013 owns the accepted model and highest-demand envelope. Codec backends and timing values below describe that initial candidate. Node-local media design records the subsequent published implementation and recovery evidence; media quality owns current behavior.

Agreed Model

ADR-0013 is the sole owner of the accepted direct-child model and policy. This file retains evidence and alternative comparisons, not a second design contract. The implementation design is owned by node-local media design. base/native names the selected source quality; it is not a second output alongside an identical high encoding.

Two Workstreams

  • Self-contained Piik Server: library-level STUN/media components inside the executable, not bundled external service processes or an automatic installer.
  • Adaptive reuse: local derivation, shared lower outputs, independent child delivery and quality recovery, usable by Native App and the server.

These share media-source and lifetime concepts but need separate acceptance. A forwarding-only SFU library does not provide the second workstream by itself; server-side derivation needs real decoding/encoding capacity. Pure Browser peers can consume normal WebRTC outputs and benefit from capable upstreams, but have no generic cross-PeerConnection encoded-forwarding API.

Initial Building Blocks And Gaps

  • mediaedge.Source already forwards encoded RTP to multiple Pion edges.
  • Native GCC observations currently drive route evidence, not the shared encoder. A sustained weak edge may use a measured Browser-sender exception. That exception is not yet a reusable derived-encoding pool.
  • Existing capture encoders, generation fences, keyframe requests and bounded route operations are reusable. A new standalone quality controller, global node score or periodic rebalancer is not assumed.
  • Current routing reacts primarily to persistently degraded senders. A healthy edge forwarding already-low input may not trigger a move. Transport health and available source quality must be distinguished before claiming recovery.
  • Simulcast, encoder allocation, active encoding, layer forwarding and bandwidth probing are different operations. Pausing a representation must not be mistaken for destroying its codec object or reopening capture.

Open Choices

  • Always encode all configured lower outputs, enable all after the first weak child, or activate only the representations currently demanded.
  • Keep unused encoder resources warm or release them; decide from cold-start, first-keyframe, GPU-session, memory and idle-CPU measurements.
  • Use a bounded representation set and one parameter owner per shared output. Determine which mature component owns demand grouping and adaptation; do not assume that independent child GCC loops can all mutate one shared encoder.
  • Coordinate a usable-frame handoff before switching, preserve unaffected branches, and avoid repeated downgrade/recovery churn.
  • Check server software-transcode cost and platform-native hardware availability independently. A network-capable parent is not necessarily a capable encoder.

Simulcast And Dynacast Findings

These observations were checked against LiveKit Server 1.13.6 and JS SDK 2.22.1, not inferred from the words "automatic" or "simulcast":

  • Simulcast advertises several encoded representations of one source. The SDK constructs a finite encoding set before publishing; it does not create an arbitrary new representation for every bandwidth sample or subscriber.
  • The pinned screen-share defaults create the original plus one lower spatial representation for ordinary screen sizes, not invariably three encoders.
  • The server's Dynacast video manager aggregates the highest subscribed quality across subscribers/downstream SFU nodes. Its update enables existing qualities at or below that maximum. Thus a high requirement can retain lower fallback representations; this is not exact per-current-layer consumer reference counting. Do not claim every unused individual lower layer is always stopped.
  • A higher quality request is not subject to downgrade-only debounce. Reduced maximum demand is debounced; the pinned server default is five seconds. This is an upstream implementation fact, not a proposed Piik timing constant. Publisher deactivation debounce is not a five-second delay before reducing traffic to a congested subscriber.
  • The SDK applies the selected active flags through RTCRtpSender.setParameters on the existing sender. WebRTC's active=false preserves SSRC identity. It is not a guarantee that every Browser releases, or retains, hardware encoder resources in the same way.
  • Subscriber forwarding choice is separate from publishing activity and from destroying an encoder. A decoder-safe layer switch needs an appropriate random-access point and consistent RTP/timestamps, not just another byte array.
  • LiveKit's prober documents padding-based capacity probes independently of video-layer production. Higher-video-layer probing is another possible strategy, but cannot be substituted for a calibrated congestion controller.

The useful lessons are finite representations, distinct demand/transport ownership, asymmetric activation/deactivation and independent network probing. They do not imply importing the whole LiveKit service or extending its subscriber-tree demand aggregation to override our direct-child derivation rule.

Strategy Comparison

Shared encoding and per-child selective delivery are common to both compared policies. The owner explicitly excludes Native-plus-Browser and repeated full encoders as the next experiment's reference baseline. Compare exact demanded outputs against the maximum-demand envelope, not shared versus unshared media.

Strategy Main benefit Cost to verify
Base plus all lower encodings always active Lower outputs are already being produced when needed Permanent encode/GPU cost even for an entirely healthy tree; still needs a switch point
Base only, then all lower outputs after any weak child Small policy, some idle savings First downgrade remains a cold start; one weak child starts outputs nobody requested
Finite outputs activated only as needed Avoids unused representations and shares compatible demands Cold start and churn if codecs are constantly destroyed/recreated
Demand activation with bounded warm retention or a minimal fallback output Potential compromise between steady cost and transition delay Requires measurements to justify retention or pre-encoding; not free memory/session capacity

The owner's 2026-09-07 preference makes the LiveKit-style maximum-demand envelope a first-class candidate: keep the highest requested representation and its lower fallbacks active; stop unused higher outputs when all direct children need less. Compare it with exact demand activation, not with an assumption that exact demand is inherently better. Prefer the simpler envelope if its measured cost is small and switching is better. Good network capacity alone does not prove spare encoding capacity or cheap lower outputs.

Both policies retain the original source authority and the ability to recover; stopping an unneeded high output does not lower capture or discard the best received input. Exact demand can retain inactive codec resources for a bounded comparison instead of repeatedly destroying them. No production timing value, extra polling loop or prediction score has been selected. Do not blindly create a fallback encoder set at every forwarding relay: that would add decode/encode work to the entirely healthy tree. Reusing received encodings and deciding how an already-needed encoder set stays active are separate decisions; publisher measurements cannot stand in for relay costs. Where the media framework supports it, negotiate the finite representation capability up front and activate existing identities; a quality change should not reopen capture or recreate a PeerConnection merely to add an encoder.

Preparing/retaining an encoder, actively encoding frames and forwarding packets are separate costs. Share stop, source replacement and failed ownership still retire their resources immediately; warm retention cannot revive a retired source.

Reusable Components

Component Useful capability Boundary
Existing mediaedge.Source and Pion RTP/WebRTC/interceptors Encoded fanout, per-edge transport and feedback No decoder/transcoder or shared-variant policy is supplied automatically
Existing Native encoders and generation/keyframe ownership Platform encode paths already integrated with Piik Received-source decode/scale and shared derived outputs still need design
LiveKit Dynacast, layer selector and prober sources Reference for demand aggregation, safe switching and recovery probes Apache-2.0 code; inspect imports/ownership before any extraction, not whole-service embedding
libvpx multi-resolution VP8 API/example Multiple spatial encodes through one coordinated API; libvpx is already used VP8-specific; no promise that H264 hardware sessions have the same cost curve
OpenH264 AVC simulcast API Library H264 encode/decode and per-spatial-layer configuration Software codec; inspect realtime cost and actual source/binary distribution terms before selection
FFmpeg libavcodec and frame/filter APIs Embedded decoding, scaling and hardware/software encoding A library option, not launching ffmpeg; build/license/size cost remains to compare
Pion mediadevices Go codec-controller and raw-frame broadcast interface reference Cgo/native codecs still exist; not a reason to replace the working capture adapters
GStreamer tee/queues and codec elements Shared decode/frame flow and isolated output branches webrtcsink currently encodes per consumer, so it does not directly solve shared derived output

At the initial review, no candidate had demonstrated the complete local derivation, reuse and recovery contract. The inLive probe proved forwarding only. A small SFU library does not by itself replace a transcoder. Do not add FFmpeg/GStreamer or change the current Native codec stack on documentation claims alone.

Focused Comparison Plan

Use the same source, codec, frame rate and direct-child count for each arm:

  1. Healthy children: verify no derived encode is added just for fanout.
  2. One weak child, then two compatible weak children: verify one derived output is sufficient for both and the original branch stays unchanged.
  3. Bandwidth drop/recovery and short oscillation: compare cold versus retained encoder startup, first usable frame, freezes, CPU/GPU, memory and actual RTP traffic. Count extra decoders and CPU/GPU frame copies as well as encoders; Browser-decoded preview frames are not automatically zero-copy Native input. Measure extra IDR/probe traffic, not only the selected bitrate.
  4. Low inherited input on an otherwise healthy edge: distinguish lack of better source from lack of link capacity; exercise higher-input recovery or the existing bounded route operation without a new global optimization loop.
  5. Source replacement, child departure and stop: verify generations and release of decoder/encoder surfaces, tracks and queues.

The first CPU-only encoder comparison is complete. It compares existing Native VP8 encodes and scripted direct-child demand, not network adaptation or playback. It supports keeping the maximum-demand envelope as a candidate, but also exposes timestamp-sensitive resume behavior in a naive warm-encoder composition. Validate mature multi-resolution pause/resume semantics before implementing retention. H264 hardware, relay decode/scale and actual receiver switching remain open; no production policy changed. The subsequent software-preference decode comparison now includes VP8/H264, scaling and two real VideoDecoders, plus received-source relay derivation. It measures local component readiness only, not WebRTC delivery or congestion. The design treats representation bookkeeping as small and puts the remaining overhead work in codec lifecycle, shared decode/surfaces and the transport library's switching/probing path.

Primary References

中文确认

核心是“只负责直属下游”:有合适编码就转发,缺少低档才由直属上级按需派生, 相同需求复用派生输出。不是 Host 为全树预编码所有档位,也不是每条弱边独立 编码。Host 同样遵守此规则。基准档、常用档的名字不能造成重复编码。

拓扑优化负责修复上游输入质量受限,局部媒体模块负责可用表示的派生、复用和 选择。稳定传输低画质不等于路径已经最优;现有优化不能直接视为已覆盖这种 恢复。当前先研究按需/预编码策略及成熟实现,尚未改动生产媒体或路由。

优先把 LiveKit 式“最高需求档及以下保持编码”与精确按需启用作为平等候选, 不预设后者更优;若常备低档成本小、切换更好,就选择更简单的策略。所有直属 下游都不需要高档时可以停掉高档输出,但不修改捕获或丢弃最好的输入来源。 先用已有 VP8 编码器短时测量成本和关键帧启停,不把结果外推成 H264 硬件或 真实网络验收。具体库、档位与启停时间仍未作为生产决策。