Repository navigation
Request downstream FlexFEC for subscribed video tracks - #1668
cloudwebrtc wants to merge 5 commits into
Conversation
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.
|
Depends on protocol PR livekit/protocol#1794 ( |
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.
| let TrackDimension(width, height) = publication.dimension(); | ||
| let update_track_settings = proto::UpdateTrackSettings { | ||
| track_sids: vec![publication.sid().into()], | ||
| disabled: !publication.is_enabled(), |
There was a problem hiding this comment.
🔴 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
| 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() |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
| 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; |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
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
livekit-protocolsubmodule atlivekit/protocol@de2bab40, the head of feat: fec subscribe presets protocol#1987, and fills the newlivekit-apirequest fields.UpdateTrackSettings.fec(field 9,optional) and theFECProtection { mode, level }message from #1987.livekitRemoteTrackPublication::set_fec_protection(FecProtection). It takes the sameFecProtection { mode, level }asTrackPublishOptions::fec, so the publish and subscribe sides share one type.FecProtectionModeandFecProtectionLevelmap one to one toFECProtectionModeandFECProtectionLevel. The level is always sent explicitly, soFEC_LEVEL_UNSPECIFIEDis never used.UpdateTrackSettingsmessage. 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.fec_testexample--fec-schedule(for example0:none,20:fixed-high,40:adaptive,60:minimum-low) to change the requested protection during a run. Protections arenone,adaptive,fixed-<level>andminimum-<level>, matching the publisher's--fec-mode; levels arelow,mediumandhigh.Not included
livekit-ffibindings forset_fec_protection.Known issues
Testing
Unit tests and lints
cargo test -p livekit fec_testspasses, including a test for the mapping of everyFecProtectionmode and level to the protocol message.cargo check --all-targetspasses forlivekit,livekit-api,livekit-ffiandfec_test.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 toFecProtection::DISABLEDandFecProtection::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.
FECProtectionandUpdateTrackSettingsare identical between this submodule and the protocol version the server was built with: field 9,optional.fecPacketsDiscarded= 0, at a steady 24 fps10% loss on both legs (RTX also lost), FEC on both legs vs. off:
Each scenario ran once for 15–60 s, so treat the freeze counts as indicative.