Files
piik/docs/adr/0007-demand-driven-dual-representation-quality.md
T

20 KiB

ADR-0007: Path-Isolated Dual-Representation Quality

  • Status: Accepted quality/representation decision; topology capacity follows ADR-0005
  • Date: 2026-08-19

Context

Screener needs to protect a healthy viewer from another viewer's weak network or decoder without growing host encode work with viewer count. Draft PR #28 proved that one VideoEncoder object can be reconfigured from feedback shared by two Pion/WebRTC legs. Its minimum-of-two target was deliberately conservative for a bounded experiment. It is not an acceptable product policy: a weak leg would lower the shared stream for every healthy leg, and the experiment did not test heterogeneous estimates.

A single ordinary, non-scalable encoded representation has one resolution, frame cadence, and rate-control result. Forwarding those packets cannot create a second quality. Per-viewer quality therefore requires another encoded representation, a negotiated scalable representation, or transcoding at a relay/SFU. Per-viewer encoders would violate the game's performance budget.

Decision

Use path-isolated dual representations:

  1. Direct/peer paths keep their requested HIGH target and independent stock WebRTC congestion control. No viewer feedback changes another path's target.
  2. Each SFU path starts with a HIGH ceiling. The preferred implementation publishes one shared HIGH+LOW pair and lets LiveKit BWE independently choose and recover the forwarded layer for each subscriber. If that gate passes, Screener does not add a media-layer selector.
  3. Sender/viewer evidence diagnoses capture, encode, transport, receive, and decode behavior. It does not change topology or command ordinary built-in layer changes. ADR-0005 reacts only to an exact child edge's hard connection failure or non-paused decoded-frame stall.
  4. Explicit subscriber quality or sender layer activation is considered only if the built-in candidate fails its bounded gate. Deactivating an unused LOW is a resource optimization, not an acceptance requirement.
  5. The active representation/layer count is a hard HIGH + at most one LOW <= 2; it never grows with viewer count.

Making LOW available is conditional on positive hardware-encoder evidence and measured spare game-performance and upload budget. A permanently active two-representation path is acceptable when that gate passes; stopping unused LOW remains a later optimization. If the hardware path is absent or the additional representation causes unacceptable CPU/GPU/game/upload cost, fail closed for that attempt: preserve HIGH and show the weak path an explicit degraded/unavailable state. This is exceptional damage containment, not permission to ignore a weak path indefinitely. A supported sender cohort that cannot reliably provide the one shared LOW, either always-on or on demand, fails quality acceptance; optimize its encode path or mark that cohort unsupported. Never protect a weak path by reducing HIGH.

There is no weighted score, device ranking, machine-learning controller, custom media selector, or continuous room-wide optimizer. Representation evidence does not create route states.

The topology and representation budgets are independent invariants. Each non-server endpoint follows ADR-0005 steady outbound media-copy capacity C (1, 2, or 3, default 2); the two-edge host result below is a historical experiment configuration, not a fixed policy. A native relay forwards selected encoded packets without decoding or re-encoding. Future dual-tree or striped delivery may reduce host upload from about two full copies toward one copy plus necessary redundancy, but it does not block this decision.

LiveKit may downshift an SFU subscription before the application observes it. That is normal per-subscriber adaptation and does not alter relay eligibility. If the exact ingress later hard-fails or stops decoding while unpaused, ADR-0005 reparents that Viewer as a child and retains its subtree. A viewer's advisory quality request cannot trigger route mutation by itself.

Evidence Contract

For diagnosis and representation acceptance, correlate the same time interval and stream generation across three stages:

Stage Required evidence
A. Capture MediaStreamTrack.getSettings() width, height, and frame rate
B. Host outbound actual width/FPS/bitrate, target or available bitrate when present, interval encode time, qualityLimitationReason, RTT, loss/retransmission, selected direct/TURN route, and derived negotiated video codec/profile/parameters plus scalabilityMode when applicable
C. Viewer inbound actual width/FPS/bitrate, loss, jitter, interval decode/drop/freeze evidence, the corresponding derived codec/profile/parameters and applicable scalabilityMode, and actual decode behavior

Interpretation is deliberately ordered:

Observation Classification
Capture is already low capture or constraint problem
Capture high, outbound low, reason cpu sender encode/resource pressure
Capture high, outbound low, reason bandwidth congestion, uplink, GCC, or TURN/path pressure
Outbound healthy, inbound degraded transport or receiver-path problem
Inbound metrics healthy, image still blurry insufficient bitrate/quantization, negotiated codec/profile/layer, or display scaling problem

WebRTC stats are sampled dictionaries and many useful counters are cumulative. The evidence sampler must use adjacent, non-overlapping deltas and rebase on a missing field, stream/stat ID change, or counter reset. Missing values remain unknown; they are not zeros.

Implementation is staged. First correlate A and B locally in one sampling tick, with an explicit interval, media/stat identity, and valid deltas. A minimal authenticated C report may add receive/decode diagnosis after that probe is trustworthy. The B/C correlation uses normalized fields derived from negotiated parameters/stats and actual decode behavior; it must never upload raw SDP, raw stats, candidate addresses, or raw device/network identifiers. Opaque server-issued path and connection IDs authorize diagnostic correlation only. Do not build a general telemetry schema.

A viewer may request LOW, but the request is advisory. It must be carried on an authenticated, current room/path session and be rate-limited and deduplicated. It may be shown with matching sender/viewer diagnosis, but it cannot lower quality, start a representation, or change topology.

UA, platform, and device-model detection does not participate in quality or relay-capacity selection. Browser capability queries can guide a bounded probe, but runtime encode/decode behavior is authoritative.

Standard Capability Short-Circuits

Inspect standard capabilities in order and stop at the first accepted path before custom dual-representation media. Static API or pinned-protocol incompatibilities can close a candidate without a browser run; runtime media gates start only after the A+B/C evidence contract is trustworthy. These results do not create an application media-layer selector or route authority:

  1. Web P2P simulcast: rejected (no-go-web-p2p-simulcast-layer-selection). WebRTC exposes getParameters() and setParameters() on RTCRtpSender, including the sender-side active flag, but RTCRtpReceiver exposes only getParameters() and no standard per-RID layer-selection setter. A sender toggle is scoped to that sender and PeerConnection; separate viewer PeerConnections have no portable shared-encoder contract. The standard API therefore cannot provide one shared HIGH/LOW encode whose direct P2P receivers independently select a layer. Chrome was not run because the required product semantic is absent. In particular, the normative rule that active=false stops sending an encoding is not evidence that its physical encoder, CPU work, or GPU allocation is released.

  2. Pinned LiveKit two-layer simulcast: implemented; acceptance remains open. Client 2.22.0 can publish a screen-share original plus one lower simulcast encoding. RemoteTrackPublication.setVideoQuality(HIGH) sets a per-subscriber spatial-quality ceiling. Server 1.13.5 derives requested spatial/temporal maxima from quality, dimensions and FPS, but actual selection is codec-specific: VP8 has temporal selection, while H.264/H.265 simulcast is spatial-only. The current H.264 candidate can adapt each SFU downtrack between q,h spatial representations and recover it independently. Server Dynacast takes the maximum requested quality and enables every quality at or below it, so any HIGH root keeps LOW active. That cumulative behavior prevents dynamic LOW stop while HIGH is subscribed, but dynamic stop is now an optimization rather than a hard requirement.

    The candidate therefore publishes exactly HIGH+LOW with standard simulcast/send encodings, leaves each LiveKit root's ceiling at HIGH, and first tests built-in per-subscriber SFU bandwidth adaptation on zero-descendant leaf viewers. Start with deterministic two-layer publication and Dynacast off. Dynacast controls publisher-layer pausing from aggregated subscriber demand; disabling it does not disable server-side per-subscriber BWE or turn a HIGH ceiling into a guaranteed received layer. Its pinned cumulative behavior can later be measured as the equivalent always-on case. Screener's subscriber does not attach a RemoteTrack, so LiveKit adaptiveStream is not directly usable without changing that ownership; it is not required for the SFU bandwidth-adaptation candidate. A root with children uses the same built-in BWE; an automatic downshift does not mutate topology. If built-in selection fails the product gates, test explicit standard subscriber quality selection before sender activation/deactivation. The gate must prove that a healthy P2P/HIGH path is unchanged, no third layer appears, the expected layer is actually received, and hardware encoder, game FPS/p1 low, CPU/GPU, interval encode cost, host upload, and HIGH+LOW bytes fit budget. Every active SFU publication now configures exactly two ordered q,h encodings, constructs Room({ dynacast: false }), disables backup-codec publication, and sets each subscriber's ceiling to HIGH; the two-layer publication is not behind a separate quality flag. Client 2.22.0 also defaults Dynacast to false, but the explicit option prevents silent option drift and backupCodec: false prevents the pinned multi-codec path from enabling it automatically. The 2026-08-22 executable preflight verifies this configuration, retains a zero-child subscriber, and rejects excess central roots. Per-subscriber BWE, actual layer forwarding for leaves and relay roots, and hardware cost remain unverified, so no runtime performance claim follows. If the always-on cost fails, test manual standard sender activation/deactivation next; custom/native dual encoding follows only if built-in and manual standard primitives fail.

  3. Current Web/LiveKit SVC: rejected (no-go-web-svc-cross-path-hardware-contract). WebRTC-SVC adds scalabilityMode to sender encoding parameters, but it adds no receiver setter for choosing a base or enhancement layer. The setting is scoped to one RTCRtpSender; separate viewer PeerConnections still have no portable shared-encoder or cross-connection layer-selection contract. An applied scalabilityMode proves only the browser's configured mode. The SVC specification explicitly says successful mode probing cannot distinguish a hardware encoder from a software encoder, while Media Capabilities powerEfficient and RTCStats powerEfficientEncoder are user-agent-defined signals rather than hardware guarantees.

    Pinned LiveKit narrows the result further. Client 2.22.0 overwrites SVC screen-share publication to L1T3: one spatial resolution and three temporal layers, so it provides no low-resolution base and exceeds this product's two-active-layer ceiling. Its RemoteTrackPublication.setVideoQuality() controls per-subscriber spatial quality, while server 1.13.5 maps quality/dimensions/FPS to requested spatial/temporal maxima. Actual selection remains codec-dependent: VP8 has a temporal selector, whereas H.264/H.265 simulcast is spatial-only. That selection exists only on an SFU downtrack; it does not extend to Screener's direct/peer receivers. The current subscriber only requests the publication, and LiveKit documents that Dynacast can pause an entire SVC stream but not individual SVC layers. Chrome ships the outgoing-track API; current Firefox and WebKit WebIDL do not expose the standard scalabilityMode member, and Firefox's implementation remains behind a preference. A pinned-SDK Safari compatibility branch is not a portable standard contract. These static blockers cannot be changed by a Chrome media run, so no harness was written and no browser was run.

None may reuse PR #28's minimum-of-two target, bypass a per-edge stock WebRTC congestion controller, or expand the representation limit beyond two.

SVC Static Verdict

This static verdict rejects current Web/LiveKit SVC, not the separate LiveKit two-layer simulcast candidate above. Requested and post-negotiation applied scalabilityMode remain useful diagnostics, because the browser may apply a different mode or omit the field when none was requested. They are not proof of the actual hardware implementation, and neither a support query nor a browser resource run can add SVC's missing direct/peer receiver-selection contract.

This rejection is scoped to the current standard Web/LiveKit shortcut, not to scalable codecs forever. A future native sender may reopen SVC only if its media API positively selects a hardware encoder without silent software fallback, publishes at most two decodable layers, reuses one encoded output across direct, peer, and SFU transports, preserves independent path selection, and passes the same game-performance and latency gates. That would be a new native capability decision, not completion of this browser spike.

Consequences

Positive:

  • Healthy viewers are not reduced to the worst path.
  • One shared LOW is available to all SFU paths and LiveKit selects it per subscriber; its idle deactivation is allowed but not required. Physical encoder instances and resource cost remain measured outcomes.
  • Encode cost is bounded independently of viewer count.
  • The diagnostic evidence remains inspectable.

Negative:

  • An always-on LOW adds representation bandwidth and may require another physical encoder even without a weak path; the exact cost is an acceptance measurement, not an assumed negligible overhead.
  • Any later explicit quality/layer fallback needs a keyframe-safe transition.
  • Browser capability reporting cannot by itself prove hardware or sustained decode performance. The current SVC path is rejected before that matrix; custom/native candidates still require it.

Product Stop Lines

  • Do not productize PR #28's minimum-of-two bitrate or frame-rate aggregation.
  • Do not use simulcast, Dynacast, SVC, or an SFU as a semantic bypass around PR #28's stock-GCC/RTX stop line or per-edge congestion control.
  • Do not lower HIGH because one viewer reports trouble.
  • Do not create one representation or encoder per viewer.
  • Do not use UA/device identity as a quality signal.
  • Do not enable software SVC as an invisible fallback.
  • Do not start or retain LOW when its measured hardware, game, or upload cost violates budget.
  • Do not add a composite health score or media selector when built-in adaptation and ADR-0005 edge recovery suffice.

Verification Gates

  • Direct/peer paths retain independent stock WebRTC congestion control. Each SFU path starts with a HIGH ceiling; under shaping, built-in LiveKit BWE sends a weak leaf the shared LOW while a healthy leaf stays HIGH, then restores the weak leaf without an application media selector.
  • An idle LOW may remain active only after hardware, game-performance, and upload cost passes. If idle stopping exists, verify bytes/frames stop; otherwise record and accept the bounded always-on cost.
  • Representation count never exceeds two. Host and relay endpoint media copies follow ADR-0005 steady capacity C; the two-edge result is only the bounded experiment configuration.
  • Built-in BWE downshift and recovery remain local to each SFU subscription and do not change topology. Exact ingress failure is handled only by ADR-0005's generic child reparent operation.
  • An unavailable/over-budget LOW fails visibly for weak paths while healthy paths and HIGH remain unchanged.
  • Every supported sender cohort can reliably provide the one shared LOW, always-on or on demand; otherwise the quality gate does not pass.
  • Native relays show packet forwarding without an added decode/encode stage.
  • Spoofed, stale, duplicated, or rate-excessive viewer requests have no effect.
  • Controlled capture/CPU/bandwidth/TURN/receiver/display cases produce the classifications in the evidence table without a room-wide downgrade.

References

Primary sources checked through 2026-08-22: