Skip to content

Add FlexFEC negotiation and subscriber FEC protection - #1071

Open
cloudwebrtc wants to merge 7 commits into
mainfrom
duan/flexfec
Open

cloudwebrtc wants to merge 7 commits into
mainfrom
duan/flexfec

Conversation

@cloudwebrtc

@cloudwebrtc cloudwebrtc commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Note

Draft until livekit/protocol#1987 is merged. The protocol submodule currently points at duan/fec-subscribe-presets-android (3a115c89), which is #1987 cherry-picked onto the protocol commit main pins (30d320b7). Once #1987 lands, the submodule should be moved to the merged commit.

Adds FlexFEC negotiation and per-track subscriber FEC protection.

FlexFEC negotiation

LiveKit.enableFlexFec turns on the WebRTC-FlexFEC-03-Advertised and WebRTC-FlexFEC-03 field trials when libwebrtc is initialized, so flexfec-03 is negotiated. It is process-wide and must be set before the first Room is created. Disabled by default.

Subscriber FEC protection

RemoteTrackPublication.setFecProtection asks the server to send FlexFEC repair packets for a subscribed video track through UpdateTrackSettings.fec:

LiveKit.enableFlexFec = true
// ...
publication.setFecProtection(FecProtection.Minimum(FecProtectionLevel.MEDIUM))

FecProtection is a sealed class mirroring the protocol modes:

Kotlin Protocol
FecProtection.Disabled (default) FEC_MODE_DISABLED
FecProtection.Adaptive FEC_MODE_ADAPTIVE
FecProtection.Fixed(level) FEC_MODE_FIXED + level
FecProtection.Minimum(level) FEC_MODE_MINIMUM + level

FecProtectionLevel is LOW / MEDIUM / HIGH (about 15/25/35% repair overhead). Only the modes that use a level carry one, so invalid combinations cannot be expressed.

  • fec is only sent after setFecProtection has been called. That follows the optional semantics in #1987: omitting the field keeps the server's setting, so other settings updates do not reset it.
  • Unlike quality and dimensions, it also applies while adaptive stream manages the track.
  • The change takes effect without renegotiation.

Tests

  • Unit tests in RemoteTrackPublicationTest: fec is omitted until requested, it is kept on later updates, every mode and level maps to the right proto values, and it is sent under adaptive stream.
  • On-device tests moved to livekit/e2e-android#17 as FlexFecSubscribeTest and FlexFecPublishTest. They check the repairs received in each mode through the inbound-rtp FEC counters, and the publisher's FlexFEC negotiation with and without simulcast. They are skipped unless run with the flexFec=true instrumentation argument against a server with FlexFEC enabled. Results from the earlier on-device runs are in the comment below.

libwebrtc

Updates libwebrtc to 150.7871.08.

LiveKit.enableFlexFec turns on the FlexFEC field trials when libwebrtc is
initialized, so flexfec-03 is negotiated. Codec preferences already keep
unmatched codecs, so the repair codec is offered without further changes.

RemoteTrackPublication.setFecProtection requests FlexFEC repair packets from
the server for a subscribed video track through the new
UpdateTrackSettings.fec field, which requires livekit/protocol#1987.
FecProtection is a sealed class mirroring the protocol's modes: Disabled,
Adaptive, and Fixed and Minimum, which carry a FecProtectionLevel (low,
medium or high). The protection is only sent once requested, so other
settings updates leave the server's setting unchanged. Unlike quality and
dimensions it also applies while adaptive stream manages the track.

FlexFecOnDeviceTest subscribes to a video track on a real server and checks
the repairs received for each mode through the inbound-rtp FEC counters. It
is skipped unless the lkUrl and lkToken instrumentation arguments are given.

The protocol submodule points at livekit/protocol#1987 applied on top of the
current protocol commit.
@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fce3183

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
client-sdk-android Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cloudwebrtc

cloudwebrtc commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

End-to-end test results

Ran FlexFecOnDeviceTest on a real device against a local livekit-server built from source.

Setup

Server livekit/livekit#5195 (branch duan/flexfec-downstream) @ cc316561 (Support downstream FEC protection modes). The PR's later commit 2f17766b only points go.mod at livekit/protocol#1987, which the test build already used through go.work.
Protocol livekit/protocol#1987 head de2bab40, used by the server through go.work; this PR's submodule is the same change on 30d320b7
Publisher lk room join --publish-demo (simulcast video)
Subscriber Xiaomi M367FC, Android 17, over Wi-Fi; LiveKit.enableFlexFec = true, adaptiveStream = false, highest layer subscribed
libwebrtc io.github.webrtc-sdk:android-prefixed:150.7871.04, the version this PR built with at the time; it now uses 150.7871.08, see below

Server config:

rtc:
  flexfec:
    upstream_enabled: true
    downstream_enabled: true

Run with:

./gradlew :livekit-android-test:connectedDebugAndroidTest \
  -Pandroid.testInstrumentationRunnerArguments.class=io.livekit.android.room.track.FlexFecOnDeviceTest \
  -Pandroid.testInstrumentationRunnerArguments.lkUrl=ws://<server-ip>:7880 \
  -Pandroid.testInstrumentationRunnerArguments.lkToken=<subscriber token>

The test has since moved to livekit/e2e-android#17 as FlexFecSubscribeTest, which publishes its own video track instead of needing lk. From e2e-android, with livekitUrl, livekitApiKey and livekitApiSecret set in gradle.properties:

./gradlew :app:connectedDebugAndroidTest \
  -Pandroid.testInstrumentationRunnerArguments.class=io.livekit.android.e2e.test.room.flexfec.FlexFecSubscribeTest \
  -Pandroid.testInstrumentationRunnerArguments.flexFec=true

The server's own FlexFEC tests (go test ./pkg/sfu/ ./pkg/rtc/ -run 'FEC|Fec|FlexFEC' and go test ./test/ -run FlexFEC) also pass at that commit.

Results

Each row is a 6 s window, after a 2 s settle following setFecProtection. Counts come from the subscriber's video inbound-rtp stats (packetsReceived, fecPacketsReceived, fecPacketsDiscarded).

Protection Media packets FEC packets FEC / media FEC discarded
not requested 1083 0 0 0
Fixed(LOW) 1156 191 16.5% 0
Fixed(MEDIUM) 1176 219 18.6% 0
Fixed(HIGH) 1311 330 25.2% 0
Minimum(MEDIUM) 1230 225 18.3% 0
Adaptive 1072 0 0 0
Disabled 971 0 0 0

An earlier run gave the same picture (LOW 17.1%, HIGH 25.1%, Minimum MEDIUM 18.6%, Adaptive and Disabled 0).

What this shows:

  • flexfec-03 is negotiated, and the repairs are received and accepted by libwebrtc (none discarded).
  • No repairs are sent until FEC is requested.
  • Mode changes apply right away without renegotiation, and Disabled stops the repairs.
  • Without loss, Minimum(MEDIUM) matches Fixed(MEDIUM) and Adaptive sends no repairs.

Note on the ratios

Fixed(HIGH) measures about 25%, not 35%. This is expected from the server's per-frame NumFecPackets rounding, max(1, round(n * factor / 256)) with factor 89 for HIGH: a 4-packet frame gets 1 repair (25%) and a 7-packet frame gets 2 (29%). The nominal 15/25/35% is only reached for larger frames. The test therefore checks ordering and loose lower bounds rather than the nominal percentages.

libwebrtc version

The results above were measured with 150.7871.04. This PR now uses 150.7871.08, which includes livekit/libwebrtc#329 (RtpParameters::fec_protection). Negotiating flexfec-03 through the field trials and receiving the server's repairs do not depend on #329, but the on-device tests have not been rerun on .08 yet.

Not covered

No loss was simulated, so recovery of lost packets and adaptive protection rising with loss were not exercised on device. The server's test/flexfec_downstream_test.go covers both.

Upstream (publish) results

Ran FlexFecPublishOnDeviceTest (added in 8a1a900) on the same device and server, publishing a synthetic 640x480 30 fps video track with LiveKit.enableFlexFec = true.

Negotiation

Without simulcast With simulcast (SDK default)
Publisher offer flexfec-03 and a=ssrc-group:FEC-FR no FlexFEC SSRC
Server answer accepts flexfec-03 n/a

libwebrtc only protects a single media stream with FlexFEC (media_session.cc: "Our FlexFEC implementation only supports protecting a single media stream"), so tracks published with simulcast do not send FlexFEC. The LiveKit.enableFlexFec KDoc now says so (c91fdee).

With .04 the publisher uses libwebrtc's loss-based FEC rate, which corresponds to FEC_MODE_ADAPTIVE; no repairs are sent without loss. Publish-side fixed levels need livekit/libwebrtc#329, which is in .08, but this PR does not use it yet.

Under loss

Upstream loss was added with a userspace UDP proxy between the device and the server that drops only RTP. With 5% and 20% upstream loss the server's livekit_fec_total{direction="incoming"} shows FlexFEC packets received and lost packets recovered from them.

While doing this I found that the server's receiver reports to the publisher always carried fractionLost 0: packets restored by RTX or FEC cancelled the loss, so libwebrtc's loss-based FEC backed off. libwebrtc receivers report loss before repair. I have a local server fix for this on top of livekit/livekit#5195 (not pushed yet) that counts repaired packets as lost in the RR. 20% upstream loss, 30 s publish, without simulcast:

Before fix After fix
fractionLost in RR seen by publisher 0 ~0.15
FEC packets received by server 64 204
Packets recovered from FEC 11 61
Retransmissions 261 221

Run with:

./gradlew :livekit-android-test:connectedDebugAndroidTest \
  -Pandroid.testInstrumentationRunnerArguments.class=io.livekit.android.room.track.FlexFecPublishOnDeviceTest \
  -Pandroid.testInstrumentationRunnerArguments.lkUrl=ws://<server-ip>:7880 \
  -Pandroid.testInstrumentationRunnerArguments.lkPublisherToken=<publisher token> \
  -Pandroid.testInstrumentationRunnerArguments.lkPublishSeconds=30

This test has also moved to livekit/e2e-android#17, as FlexFecPublishTest:

./gradlew :app:connectedDebugAndroidTest \
  -Pandroid.testInstrumentationRunnerArguments.class=io.livekit.android.e2e.test.room.flexfec.FlexFecPublishTest \
  -Pandroid.testInstrumentationRunnerArguments.flexFec=true \
  -Pandroid.testInstrumentationRunnerArguments.publishSeconds=30

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Diffuse output:

OLD: diffuse-source-file
NEW: livekit-android-sdk-release.aar

 AAR      │ old      │ new      │ diff      
──────────┼──────────┼──────────┼───────────
      jar │  3.1 MiB │  3.2 MiB │ +47.7 KiB 
 manifest │  1.5 KiB │  1.5 KiB │       0 B 
 lint-jar │ 12.7 KiB │ 12.7 KiB │       0 B 
    other │  2.4 KiB │  2.4 KiB │       0 B 
──────────┼──────────┼──────────┼───────────
    total │  3.1 MiB │  3.2 MiB │ +47.7 KiB 

 JAR     │ old   │ new   │ diff             
─────────┼───────┼───────┼──────────────────
 classes │  1794 │  1823 │  +29 (+36 -7)    
 methods │ 22661 │ 22877 │ +216 (+671 -455) 
  fields │  5984 │  6083 │  +99 (+120 -21)
AAR
 size    │ diff      │ path          
─────────┼───────────┼───────────────
 3.2 MiB │ +47.7 KiB │ ∆ classes.jar 
─────────┼───────────┼───────────────
 3.2 MiB │ +47.7 KiB │ (total)
JAR
CLASSES:

   old  │ new  │ diff         
  ──────┼──────┼──────────────
   1794 │ 1823 │ +29 (+36 -7) 
  
  + io.livekit.android.room.OpusNackKt
  + io.livekit.android.room.PeerConnectionTransport_Factory_DefaultImpls
  + io.livekit.android.room.PeerConnectionTransport_InitialOffer
  + io.livekit.android.room.PeerConnectionTransport_MungedOffer
  + io.livekit.android.room.PeerConnectionTransport_createInitialOffer_1
  + io.livekit.android.room.PeerConnectionTransport_createInitialOffer_2_1
  + io.livekit.android.room.PeerConnectionTransport_createMungedOffer_1
  + io.livekit.android.room.PeerConnectionTransport_setAnswerWithOpusNack_1
  + io.livekit.android.room.PeerConnectionTransport_setAnswerWithOpusNack_result_1
  + io.livekit.android.room.PeerConnectionTransport_setAnswerWithOpusNack_result_2
  + io.livekit.android.room.RTCEngine_close__inlined_executeBlockingOnRTCThread_1
  + io.livekit.android.room.RTCEngine_close_1_1
  + io.livekit.android.room.RTCEngine_closeEarlyPublisher_1
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_1
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_2_1_1_1
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_2_1_1_2
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_2_1_1
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_2
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_3
  + io.livekit.android.room.RTCEngine_createPublisherDataChannels_4
  + io.livekit.android.room.track.FecProtection_Adaptive
  + io.livekit.android.room.track.FecProtection_Disabled
  + io.livekit.android.room.track.FecProtection_Fixed
  + io.livekit.android.room.track.FecProtection_Minimum
  + io.livekit.android.room.track.FecProtection
  + io.livekit.android.room.track.FecProtectionLevel_WhenMappings
  + io.livekit.android.room.track.FecProtectionLevel
  + livekit.LivekitRtc_FECProtection_Builder
  + livekit.LivekitRtc_FECProtection
  + livekit.LivekitRtc_FECProtectionLevel_1
  + livekit.LivekitRtc_FECProtectionLevel_FECProtectionLevelVerifier
  + livekit.LivekitRtc_FECProtectionLevel
  + livekit.LivekitRtc_FECProtectionMode_1
  + livekit.LivekitRtc_FECProtectionMode_FECProtectionModeVerifier
  + livekit.LivekitRtc_FECProtectionMode
  + livekit.LivekitRtc_FECProtectionOrBuilder
  
  - io.livekit.android.room.DualPeerConnectionTransport_answerServerOffer_outcome_1
  - io.livekit.android.room.RTCEngine_configure_2_2_4_1_1_1
  - io.livekit.android.room.RTCEngine_configure_2_2_4_1_1_2
  - io.livekit.android.room.RTCEngine_configure_2_2_4_1_1
  - io.livekit.android.room.RTCEngine_configure_2_2_4
  - io.livekit.android.room.RTCEngine_configure_2_2_5
  - io.livekit.android.room.RTCEngine_createPublisherDataTrackChannel_2
  

METHODS:

   old   │ new   │ diff             
  ───────┼───────┼──────────────────
   22661 │ 22877 │ +216 (+671 -455) 
  
  + io.livekit.android.LiveKit getEnableFlexFec() → boolean
  + io.livekit.android.LiveKit getEnableFlexFec_annotations()
  + io.livekit.android.LiveKit setEnableFlexFec(boolean)
  + io.livekit.android.dagger.RTCModule access_getFieldTrials(RTCModule) → String
  + io.livekit.android.dagger.RTCModule getFieldTrials() → String
  + io.livekit.android.room.OpusNackKt <clinit>()
  + io.livekit.android.room.OpusNackKt ensureOpusNack(MediaDescription)
  + io.livekit.android.room.OpusNackKt mungeOpusNackForAnswer(String, String, SdpFactory) → String
  + io.livekit.android.room.OpusNackKt nackPayloads(MediaDescription) → Set
  + io.livekit.android.room.OpusNackKt opusPayloads(MediaDescription) → Set
  + io.livekit.android.room.PeerConnectionTransport <init>(PeerConnection_RTCConfiguration, PeerConnection_Observer, PeerConnectionTransport_Listener, CoroutineDispatcher, PeerConnectionFactory, SdpFactory, RTCThreadToken, boolean)
  + io.livekit.android.room.PeerConnectionTransport <init>(PeerConnection_RTCConfiguration, PeerConnection_Observer, PeerConnectionTransport_Listener, CoroutineDispatcher, PeerConnectionFactory, SdpFactory, RTCThreadToken, boolean, int, DefaultConstructorMarker)
  + io.livekit.android.room.PeerConnectionTransport access_createMungedOffer(PeerConnectionTransport, MediaConstraints, Continuation) → Object
  + io.livekit.android.room.PeerConnectionTransport access_getInitialOffer_p(PeerConnectionTransport) → PeerConnectionTransport_InitialOffer
  + io.livekit.android.room.PeerConnectionTransport access_setInitialOffer_p(PeerConnectionTransport, PeerConnectionTransport_Initia
...✂

libwebrtc only protects a single media stream with FlexFEC, so it signals no
FlexFEC SSRC for a sender with simulcast layers.
FlexFecPublishOnDeviceTest publishes a synthetic video track to a real server
and checks the publisher negotiation: without simulcast the offer includes
flexfec-03 and a FEC-FR SSRC and the server accepts flexfec-03, and with
simulcast no FlexFEC SSRC is signaled. The track can be held for a while to
watch the repairs arrive at the server under upstream loss. It is skipped
unless the lkUrl and lkPublisherToken instrumentation arguments are given.

@davidliu davidliu 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.

LGTM, just needs moving the integrated tests over to e2e-android.

FlexFecOnDeviceTest and FlexFecPublishOnDeviceTest now live in
livekit/e2e-android as FlexFecSubscribeTest and FlexFecPublishTest, next to
the other instrumented tests. The androidTest manifest only existed to allow
cleartext connections for them.
@github-actions

Copy link
Copy Markdown
Contributor

Dependency diff:

-\--- io.github.webrtc-sdk:android-prefixed:150.7871.04
+\--- io.github.webrtc-sdk:android-prefixed:150.7871.08

@cloudwebrtc
cloudwebrtc marked this pull request as ready for review November 23, 2026 00:53

@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 2 potential issues.

3 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +231 to +236
fun setFecProtection(protection: FecProtection) {
if (!subscribed || track !is VideoTrack || protection == requestedFecProtection) {
return
}
requestedFecProtection = protection
sendUpdateTrackSettings.invoke()

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.

🟡 Lost FlexFEC protection after resubscribe

After a completed unsubscribe, setFecProtection drops an identical request when the app resubscribes without adaptive stream. The server starts the new subscription with repairs off, but fecProtection still reports the previous request.

Learn more

The server deletes the subscription and its settings when an explicit unsubscribe finishes. In the companion server implementation, SubscriptionManager.reconcileMediaTrackSubscriptions removes subscriptions once needsCleanup reports that the closed track is no longer desired. However, setSubscribed leaves the client's publication and its cached request intact. On resubscribe, addSubscribedMediaTrack looks up the same publication by SID and attaches the track. With adaptive stream off, the track setter does not send settings, and repeating the request hits the equality check.

Example: Set Fixed(HIGH) on a subscribed camera. Unsubscribe long enough for the server to finish removing the subscription, then resubscribe and apply Fixed(HIGH) again. The getter still reports Fixed(HIGH), but the new subscription gets no repair packets until another setting sends the cached request.

Recommended fix: Replay a non-null FEC request when resubscribing, including with adaptive stream disabled, or scope request deduplication to the current subscription. Add a test that unsubscribes and resubscribes to the same SID, then checks that the prior mode and level are sent again.

Devin Review


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

Comment on lines +96 to +97
private val fieldTrials: String
get() = if (LiveKit.enableFlexFec) DEFAULT_FIELD_TRIALS + FLEXFEC_FIELD_TRIALS else DEFAULT_FIELD_TRIALS

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.

🟡 Downstream FlexFEC bypasses opt-in

With enableFlexFec off, libwebrtc still accepts flexfec-03 in receive-only video offers. On a downstream-enabled server, setFecProtection can start repair traffic despite the app leaving the flag off.

Learn more

In the pinned libwebrtc build, GetDefaultSupportedFormats adds flexfec-03 for decoders even when the advertisement trial is unset. CodecVendor::GetVideoCodecsForAnswer uses that receive codec list for receive-only answers on the default SDP path. WebRtcVideoReceiveChannel::ConfigureReceiverRtp also accepts a negotiated repair stream when the trial is unset. The SDK's answerServerOffer applies the native answer unchanged, and the track settings request sends FEC without checking the flag latched at initialization.

Example: Leave the flag at its default, join a two-peer-connection room on a server that offers downstream FlexFEC, and request Fixed(HIGH) on a subscribed video. The downstream codec can negotiate and the request can activate repairs instead of being a no-op.

Recommended fix: If the flag is meant to gate negotiation, exclude flexfec-03 from the subscriber's negotiated codecs when disabled. If receive negotiation is intentionally native-default, document that distinction and gate outgoing FEC requests on the latched opt-in if they must remain a no-op. Cover the default-off receive case with an integration test.

Devin Review


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

libwebrtc 150.7871.08 adds a fecProtection parameter to the RtpParameters
constructor.
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.

2 participants