Repository navigation
Add FlexFEC negotiation and subscriber FEC protection #1071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1acac75
c91fdee
8a1a900
c8cc41e
5441c1c
55a41b2
fce3183
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "client-sdk-android": minor | ||
| --- | ||
|
|
||
| Add `LiveKit.enableFlexFec` to negotiate FlexFEC, and `RemoteTrackPublication.setFecProtection` to request FlexFEC repair packets from the server for a subscribed video track, with disabled, adaptive, fixed and minimum modes and low, medium and high levels. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "client-sdk-android": patch | ||
| --- | ||
|
|
||
| Update libwebrtc to 150.7871.08. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright 2026 LiveKit, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.livekit.android.room.track | ||
|
|
||
| import livekit.LivekitRtc | ||
|
|
||
| /** | ||
| * Proactive FlexFEC protection the server adds to a subscribed video track. | ||
| * | ||
| * The server sends repair packets alongside the track's media, so the receiver can recover lost | ||
| * packets without waiting for a retransmission. | ||
| * | ||
| * @see RemoteTrackPublication.setFecProtection | ||
| */ | ||
| sealed class FecProtection { | ||
| /** No repair packets. This is the default. */ | ||
| data object Disabled : FecProtection() | ||
|
|
||
| /** | ||
| * Protection follows the observed packet loss, using the same algorithm as libwebrtc. | ||
| */ | ||
| data object Adaptive : FecProtection() | ||
|
|
||
| /** | ||
| * Constant protection at [level], regardless of packet loss. | ||
| */ | ||
| data class Fixed(val level: FecProtectionLevel) : FecProtection() | ||
|
|
||
| /** | ||
| * Protection never drops below [level]. It rises adaptively under higher packet loss, and | ||
| * returns to [level] once the loss subsides. | ||
| */ | ||
| data class Minimum(val level: FecProtectionLevel) : FecProtection() | ||
|
|
||
| internal fun toProto(): LivekitRtc.FECProtection { | ||
| val builder = LivekitRtc.FECProtection.newBuilder() | ||
| when (this) { | ||
| Disabled -> builder.mode = LivekitRtc.FECProtectionMode.FEC_MODE_DISABLED | ||
| Adaptive -> builder.mode = LivekitRtc.FECProtectionMode.FEC_MODE_ADAPTIVE | ||
| is Fixed -> { | ||
| builder.mode = LivekitRtc.FECProtectionMode.FEC_MODE_FIXED | ||
| builder.level = level.toProto() | ||
| } | ||
|
|
||
| is Minimum -> { | ||
| builder.mode = LivekitRtc.FECProtectionMode.FEC_MODE_MINIMUM | ||
| builder.level = level.toProto() | ||
| } | ||
| } | ||
| return builder.build() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The share of packets spent on FlexFEC repairs, used by [FecProtection.Fixed] and | ||
| * [FecProtection.Minimum]. | ||
| * | ||
| * Frames that span only a few packets get proportionally more repairs. | ||
| */ | ||
| enum class FecProtectionLevel { | ||
| /** Approximately 15% repair packet overhead. */ | ||
| LOW, | ||
|
|
||
| /** Approximately 25% repair packet overhead. */ | ||
| MEDIUM, | ||
|
|
||
| /** Approximately 35% repair packet overhead. */ | ||
| HIGH, | ||
| ; | ||
|
|
||
| internal fun toProto(): LivekitRtc.FECProtectionLevel { | ||
| return when (this) { | ||
| LOW -> LivekitRtc.FECProtectionLevel.FEC_LEVEL_LOW | ||
| MEDIUM -> LivekitRtc.FECProtectionLevel.FEC_LEVEL_MEDIUM | ||
| HIGH -> LivekitRtc.FECProtectionLevel.FEC_LEVEL_HIGH | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright 2023-2025 LiveKit, Inc. | ||
| * Copyright 2023-2026 LiveKit, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -75,6 +75,15 @@ class RemoteTrackPublication( | |
| private var videoDimensions: Track.Dimensions? = null | ||
| private var fps: Int? = null | ||
|
|
||
| /** | ||
| * The FlexFEC protection requested with [setFecProtection]. | ||
| */ | ||
| val fecProtection: FecProtection | ||
| get() = requestedFecProtection ?: FecProtection.Disabled | ||
|
|
||
| // null until requested, so updates leave the server's setting unchanged | ||
| private var requestedFecProtection: FecProtection? = null | ||
|
|
||
| var subscriptionAllowed: Boolean = true | ||
| internal set | ||
|
|
||
|
|
@@ -204,6 +213,29 @@ class RemoteTrackPublication( | |
| sendUpdateTrackSettings.invoke() | ||
| } | ||
|
|
||
| /** | ||
| * Requests proactive FlexFEC repair packets from the server for this video track. | ||
| * | ||
| * Repairs let the receiver recover lost packets without waiting for a retransmission, at the | ||
| * cost of extra bandwidth. The protection changes without renegotiation, and is applied even | ||
| * when the track's quality is managed by adaptive stream. | ||
| * | ||
| * Repairs are only sent when the server has downstream FlexFEC enabled and FlexFEC was | ||
| * negotiated, which requires [io.livekit.android.LiveKit.enableFlexFec]. Otherwise the request | ||
| * has no effect. | ||
| * | ||
| * ``` | ||
| * publication.setFecProtection(FecProtection.Minimum(FecProtectionLevel.MEDIUM)) | ||
| * ``` | ||
| */ | ||
| fun setFecProtection(protection: FecProtection) { | ||
| if (!subscribed || track !is VideoTrack || protection == requestedFecProtection) { | ||
| return | ||
| } | ||
| requestedFecProtection = protection | ||
| sendUpdateTrackSettings.invoke() | ||
|
Comment on lines
+231
to
+236
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Lost FlexFEC protection after resubscribe After a completed unsubscribe, Learn moreThe 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 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. Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
|
||
| private fun handleVisibilityChanged(isVisible: Boolean) { | ||
| disabled = !isVisible | ||
| sendUpdateTrackSettings.invoke() | ||
|
|
@@ -237,6 +269,7 @@ class RemoteTrackPublication( | |
| videoDimensions, | ||
| videoQuality?.toProto(), | ||
| fps, | ||
| requestedFecProtection?.toProto(), | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| +6 −0 | .changeset/subscription-fec-presets.md | |
| +586 −383 | livekit/livekit_rtc.pb.go | |
| +37 −0 | protobufs/livekit_rtc.proto |
There was a problem hiding this comment.
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
enableFlexFecoff, libwebrtc still acceptsflexfec-03in 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-03for 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-03from 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.Was this helpful? React with 👍 or 👎 to provide feedback.