Skip to content

Request downstream FlexFEC for subscribed video tracks - #1668

Open
cloudwebrtc wants to merge 5 commits into
dc/feature/fecfrom
duan/fec-subscribe-presets
Open

cloudwebrtc wants to merge 5 commits into
dc/feature/fecfrom
duan/fec-subscribe-presets

Conversation

@cloudwebrtc

@cloudwebrtc cloudwebrtc commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Requires protocol PR livekit/protocol#1987 (UpdateTrackSettings.fec, not merged yet), and a server with downstream FlexFEC (livekit/livekit#5195).

Summary

Lets a subscriber ask the SFU for FlexFEC repair packets on a subscribed video track, either at a fixed level, adaptively from observed loss, or adaptively above a minimum level.

Stacked on #1424 (dc/feature/fec), which handles the publish side.

Changes

Protocol

  • No protocol changes in this PR. The base branch (Enable publishing FlexFEC #1424) already points the livekit-protocol submodule at livekit/protocol@de2bab40, the head of feat: fec subscribe presets protocol#1987, and fills the new livekit-api request fields.
  • This PR uses UpdateTrackSettings.fec (field 9, optional) and the FECProtection { mode, level } message from #1987.

livekit

  • New RemoteTrackPublication::set_fec_protection(FecProtection). It takes the same FecProtection { mode, level } as TrackPublishOptions::fec, so the publish and subscribe sides share one type.
  • FecProtectionMode and FecProtectionLevel map one to one to FECProtectionMode and FECProtectionLevel. The level is always sent explicitly, so FEC_LEVEL_UNSPECIFIED is never used.
  • The request goes out as an UpdateTrackSettings message. The SFU replaces every setting except FEC on each update, so the message also repeats the current enabled state and dimensions; otherwise the subscription's resolution would be reset.
  • The setting is kept across resubscriptions and changes without renegotiation.

fec_test example

  • The subscriber gains --fec-schedule (for example 0:none,20:fixed-high,40:adaptive,60:minimum-low) to change the requested protection during a run. Protections are none, adaptive, fixed-<level> and minimum-<level>, matching the publisher's --fec-mode; levels are low, medium and high.
  • It also records the current protection in its CSV.

Not included

  • livekit-ffi bindings for set_fec_protection.

Known issues

Testing

Unit tests and lints

  • cargo test -p livekit fec_tests passes, including a test for the mapping of every FecProtection mode and level to the protocol message.
  • cargo check --all-targets passes for livekit, livekit-api, livekit-ffi and fec_test.
  • clippy reports nothing on the changed files.

End to end against a local SFU built from livekit/livekit#5195 (on #5194)

These runs predate the move to protocol#1987 and used the earlier preset-only API (none/low/medium/high, equivalent to FecProtection::DISABLED and FecProtection::fixed(level) now). The adaptive and minimum modes have not been tested end to end yet.

Setup: two rust-sdks clients, publisher → SFU → subscriber, with libwebrtc on both ends (webrtc-83af826). 640x360, 24 fps, 1 Mbps VP8.

  • Protocol consistency: FECProtection and UpdateTrackSettings are identical between this submodule and the protocol version the server was built with: field 9, optional.
  • Smoke run, FEC high on both legs:
    • the SFU received 1,044 repair packets from the publisher and sent 1,028 to the subscriber
    • the subscriber received 1,022, with fecPacketsDiscarded = 0, at a steady 24 fps
  • Switching: the requested level was switched off → high → off at runtime. Repairs start and stop with each switch, and the SDP does not change.
  • 10% downstream loss injected at the SFU after FEC encoding (a local test patch, not in any PR):
scenario FEC repairs received NACKs freezes e2e latency
RTX delivered off 0 274 0 64.8 ms
high 757 164 0 24.5 ms
RTX also lost off 0 334 5 90.1 ms
high 752 210 2 29.3 ms
  • 10% loss on both legs (RTX also lost), FEC on both legs vs. off:

    • freezes: 26 → 11
    • dropped frames: 71 → 0
    • e2e latency: 100.8 → 43.4 ms

    Each scenario ran once for 15–60 s, so treat the freeze counts as indicative.

Bump livekit-protocol to a protocol revision with UpdateTrackSettings.fec
(livekit/protocol#1794 applied on top of the current submodule commit) and
regenerate the Rust protobufs; the only generated change is the new field and
the FECProtection enum.

Add RemoteTrackPublication::set_fec_protection, which reuses FecProtection to
select the repairs the SFU sends for a subscribed video track. The SFU
replaces every subscription setting but the FEC level on each update, so the
request repeats the current enabled state and dimensions.

The fec_test subscriber gains --fec-schedule to change the requested level
over time, and records the level in its CSV.
@cloudwebrtc

Copy link
Copy Markdown
Contributor Author

Depends on protocol PR livekit/protocol#1794 (UpdateTrackSettings.fec). Until it merges, the livekit-protocol submodule points to livekit/protocol@9026a9c8 (branch duan/fec-subscribe-presets-rust), which is #1794 applied on top of the protocol commit this repo already pins. Server side: livekit/livekit#5195

Move the livekit-protocol submodule to de2bab40, the head of
livekit/protocol#1987, and regenerate the Rust protobufs. The request
now carries a FECProtection message with a mode (disabled, adaptive,
fixed, minimum) and a level, exposed as SubscriptionFecProtection.

The newer protocol adds UpdateStreamRequest.add_destinations and
TransferSipParticipantRequest.dialtone; livekit-api leaves both at
their defaults.
The level of SubscriptionFecProtection::Fixed and ::Minimum reused the
publish-side FecProtection, so FecProtection::Disabled stood in for
FEC_LEVEL_UNSPECIFIED and Fixed(Disabled) meant fixed protection at the
server default. Use a dedicated SubscriptionFecLevel that mirrors
FECProtectionLevel one to one, with Default for the server default.
dc/feature/fec now models publish-side FEC as FecProtection { mode, level },
mirroring FECProtection in livekit/protocol#1987. Use the same type for
RemoteTrackPublication::set_fec_protection instead of the separate
SubscriptionFecProtection and SubscriptionFecLevel, and map it to the
protocol message through FecProtectionMode and FecProtectionLevel.

The fec_test subscriber schedule now spells the minimum mode as
minimum-<level>, matching the publisher's --fec-mode values.
The protocol update and livekit-api fixes now come from dc/feature/fec,
whose changeset already bumps livekit-protocol and livekit-api.
@cloudwebrtc
cloudwebrtc marked this pull request as ready for review November 17, 2026 08:47
@cloudwebrtc
cloudwebrtc requested a review from ladvoc as a code owner November 17, 2026 08:47

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 3 potential issues.

Devin Review

let TrackDimension(width, height) = publication.dimension();
let update_track_settings = proto::UpdateTrackSettings {
track_sids: vec![publication.sid().into()],
disabled: !publication.is_enabled(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Pre-subscription FEC requests pause video

Before subscription, set_fec_protection sends disabled: true because is_enabled returns false without a track. set_subscribed(true) never clears that setting, so the SFU starts the subscription paused.

Learn more

The publication exists before its media track arrives. is_enabled reports false in that state, although the application has not requested a pause. The FEC request therefore includes a server-side pause. The SFU stores track settings before subscription and reapplies them when the subscription is created. set_subscribed changes subscription intent but does not send an enabling settings update.

Example: Connect with auto_subscribe = false, configure fixed-high FEC on a published video track, then subscribe to that track. The FEC request stores disabled = true; the subscription starts paused instead of delivering protected video.

Recommended fix: Preserve desired enabled state independently of media-track attachment, or retain the FEC request locally and apply it after attachment. Add a regression test that configures FEC before subscribing and verifies video delivery without a manual disable/enable cycle.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +487 to +494
let TrackDimension(width, height) = publication.dimension();
let update_track_settings = proto::UpdateTrackSettings {
track_sids: vec![publication.sid().into()],
disabled: !publication.is_enabled(),
width,
height,
fec: Some(protection.into()),
..Default::default()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 FEC changes discard video quality limits

After set_video_quality(Low), set_fec_protection sends the publication's full dimensions and overrides the low-quality request. set_video_quality never updates those dimensions, so changing FEC can restore a higher simulcast layer.

Learn more

The publication's dimensions initially come from the publisher's TrackInfo. set_video_quality only emits an event, and its handler sends the chosen quality without retaining it. The SFU replaces non-FEC settings on every update and prioritizes nonzero dimensions over the quality field. Repeating publication dimensions therefore does not preserve a quality-based subscription limit.

Example: A publisher offers 1280×720 simulcast video. A subscriber requests VideoQuality::Low, then requests fixed-high FEC. The FEC message includes 1280×720, so the SFU replaces the low-quality limit with the full-resolution request.

Recommended fix: Retain the requested subscription settings, including whether the caller selected quality or dimensions, separately from publisher metadata. Build FEC updates from that state. Add a regression test that requests Low quality, changes FEC, and verifies that the selected simulcast layer remains Low.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +143 to +147
tokio::spawn(async move {
let subscribed_at = tokio::time::Instant::now();
for (secs, protection) in schedule {
tokio::time::sleep_until(subscribed_at + Duration::from_secs(secs))
.await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 FEC schedules outlive their subscriptions

When a track is unpublished before its final step, tokio::spawn retains the publication and sends later FEC requests. The discarded handle provides no cancellation or join path, violating the repository's lifecycle rules.

Learn more

The schedule task captures publication strongly while waiting between steps. Removing a track does not cancel that task because its handle is discarded and the event loop only processes subscription events. The publication also retains the engine through its FEC callback. The repository requires spawned tasks to have named owners and deterministic cancellation and joining.

Example: Run with --fec-schedule 0:none,60:fixed-high, then unpublish the track after ten seconds. Its schedule remains alive for another fifty seconds and sends a settings request for the removed track.

Recommended fix: Track schedule handles by track SID, cancel and await them on track unsubscription or unpublication, and drain them before closing the room. Add a lifecycle test proving that removal completes the task and releases its captured publication.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant