diff --git a/.changeset/flexfec-subscription.md b/.changeset/flexfec-subscription.md new file mode 100644 index 000000000..27599e3ab --- /dev/null +++ b/.changeset/flexfec-subscription.md @@ -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. diff --git a/.changeset/webrtc-150-7871-08.md b/.changeset/webrtc-150-7871-08.md new file mode 100644 index 000000000..8c72f6440 --- /dev/null +++ b/.changeset/webrtc-150-7871-08.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": patch +--- + +Update libwebrtc to 150.7871.08. diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4dd8d6b30..3e9cdebe5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -webrtc = "150.7871.04" +webrtc = "150.7871.08" androidJainSipRi = "1.3.0-91" androidx-activity = "1.9.0" diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt b/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt index c281b5aa8..967532ea6 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/LiveKit.kt @@ -64,6 +64,27 @@ object LiveKit { @JvmStatic var enableWebRTCLogging: Boolean = false + /** + * Enables FlexFEC forward error correction for video. + * + * When enabled, connections negotiate `flexfec-03`, which lets subscribed video tracks + * receive repair packets from the server, see + * [io.livekit.android.room.track.RemoteTrackPublication.setFecProtection]. + * Video tracks published without simulcast also send FlexFEC, at a rate WebRTC adapts to the + * reported loss; WebRTC does not protect simulcast tracks with FlexFEC. + * Disabled by default. + * + * This is a process-wide WebRTC setting, so it must be set before the first [Room] is + * created or [init] is called; later changes have no effect. + * + * ``` + * LiveKit.enableFlexFec = true + * val room = LiveKit.create(appContext) + * ``` + */ + @JvmStatic + var enableFlexFec: Boolean = false + /** * Certain WebRTC classes need to be initialized prior to use. * diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/dagger/RTCModule.kt b/livekit-android-sdk/src/main/java/io/livekit/android/dagger/RTCModule.kt index 91e3ad0ff..6f1aafbf6 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/dagger/RTCModule.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/dagger/RTCModule.kt @@ -91,6 +91,10 @@ internal object RTCModule { * it. */ private const val DEFAULT_FIELD_TRIALS = "WebRTC-IceHandshakeDtls/Enabled/" + private const val FLEXFEC_FIELD_TRIALS = "WebRTC-FlexFEC-03-Advertised/Enabled/WebRTC-FlexFEC-03/Enabled/" + + private val fieldTrials: String + get() = if (LiveKit.enableFlexFec) DEFAULT_FIELD_TRIALS + FLEXFEC_FIELD_TRIALS else DEFAULT_FIELD_TRIALS /** * Certain classes require libwebrtc to be initialized prior to use. @@ -122,7 +126,7 @@ internal object RTCModule { PeerConnectionFactory.InitializationOptions .builder(appContext) .setNativeLibraryName("lkjingle_peerconnection_so") - .setFieldTrials(DEFAULT_FIELD_TRIALS) + .setFieldTrials(fieldTrials) .setInjectableLogger( { s, severity, s2 -> if (!LiveKit.enableWebRTCLogging) { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt index 875b52051..2903c71df 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt @@ -529,12 +529,14 @@ constructor( sendRequest(request) } + @Suppress("LongParameterList") fun sendUpdateTrackSettings( sid: String, disabled: Boolean, videoDimensions: Track.Dimensions?, videoQuality: LivekitModels.VideoQuality?, fps: Int?, + fecProtection: LivekitRtc.FECProtection? = null, ) { val trackSettings = LivekitRtc.UpdateTrackSettings.newBuilder() .addTrackSids(sid) @@ -553,6 +555,10 @@ constructor( if (fps != null) { setFps(fps) } + + if (fecProtection != null) { + fec = fecProtection + } } val request = LivekitRtc.SignalRequest.newBuilder() diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/track/FecProtection.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/track/FecProtection.kt new file mode 100644 index 000000000..d9807d076 --- /dev/null +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/track/FecProtection.kt @@ -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 + } + } +} diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/track/RemoteTrackPublication.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/track/RemoteTrackPublication.kt index 0f04a588d..6537e2481 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/track/RemoteTrackPublication.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/track/RemoteTrackPublication.kt @@ -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() + } + private fun handleVisibilityChanged(isVisible: Boolean) { disabled = !isVisible sendUpdateTrackSettings.invoke() @@ -237,6 +269,7 @@ class RemoteTrackPublication( videoDimensions, videoQuality?.toProto(), fps, + requestedFecProtection?.toProto(), ) } diff --git a/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpParameters.kt b/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpParameters.kt index 6bce8e5df..c0b589aea 100644 --- a/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpParameters.kt +++ b/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpParameters.kt @@ -1,5 +1,5 @@ /* - * Copyright 2024 LiveKit, Inc. + * Copyright 2024-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. @@ -23,7 +23,8 @@ class MockRtpParameters( headerExtensions: MutableList?, encodings: MutableList?, codecs: MutableList?, -) : RtpParameters(transactionId, degradationPreference, rtcp, headerExtensions, encodings, codecs) { + fecProtection: FecProtection? = null, +) : RtpParameters(transactionId, degradationPreference, fecProtection, rtcp, headerExtensions, encodings, codecs) { class MockRtcp(cname: String?, reducedSize: Boolean) : Rtcp(cname, reducedSize) class MockHeaderExtension(uri: String?, id: Int, encrypted: Boolean) : HeaderExtension(uri, id, encrypted) class MockCodec(payloadType: Int, name: String?, kind: MediaStreamTrack.MediaType?, clockRate: Int?, numChannels: Int?, parameters: MutableMap?) : diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/track/RemoteTrackPublicationTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/track/RemoteTrackPublicationTest.kt index 77a433452..f28996f73 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/track/RemoteTrackPublicationTest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/track/RemoteTrackPublicationTest.kt @@ -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. @@ -84,6 +84,104 @@ class RemoteTrackPublicationTest : MockE2ETest() { assertEquals(VideoQuality.LOW, lastRequest.trackSetting.quality) } + private suspend fun subscribeRemoteVideo(): RemoteTrackPublication { + connect() + + wsFactory.listener.onMessage( + wsFactory.ws, + TestData.PARTICIPANT_JOIN.toOkioByteString(), + ) + + room.onAddTrack( + MockRtpReceiver.create(), + MockVideoStreamTrack(), + arrayOf( + MockMediaStream( + id = createMediaStreamId( + TestData.REMOTE_PARTICIPANT.sid, + TestData.REMOTE_VIDEO_TRACK.sid, + ), + ), + ), + ) + + return room.remoteParticipants.values.first() + .videoTrackPublications.first() + .first as RemoteTrackPublication + } + + private fun lastTrackSetting(): LivekitRtc.UpdateTrackSettings { + val requests = wsFactory.ws.sentRequests.map { + LivekitRtc.SignalRequest.newBuilder().mergeFrom(it.toPBByteString()).build() + } + val lastTrackSetting = requests.lastOrNull { it.hasTrackSetting() } + assertTrue("no track setting sent", lastTrackSetting != null) + return lastTrackSetting!!.trackSetting + } + + @Test + fun fecProtectionOmittedUntilRequested() = runTest { + room.adaptiveStream = false + val remoteVideoPub = subscribeRemoteVideo() + advanceUntilIdle() + wsFactory.ws.clearRequests() + + remoteVideoPub.setVideoFps(30) + advanceUntilIdle() + + assertFalse(lastTrackSetting().hasFec()) + assertEquals(FecProtection.Disabled, remoteVideoPub.fecProtection) + } + + @Test + fun fecProtectionSentAndKept() = runTest { + room.adaptiveStream = false + val remoteVideoPub = subscribeRemoteVideo() + advanceUntilIdle() + wsFactory.ws.clearRequests() + + remoteVideoPub.setFecProtection(FecProtection.Fixed(FecProtectionLevel.HIGH)) + advanceUntilIdle() + + assertEquals(FecProtection.Fixed(FecProtectionLevel.HIGH), remoteVideoPub.fecProtection) + assertTrue(lastTrackSetting().hasFec()) + assertEquals(LivekitRtc.FECProtectionMode.FEC_MODE_FIXED, lastTrackSetting().fec.mode) + assertEquals(LivekitRtc.FECProtectionLevel.FEC_LEVEL_HIGH, lastTrackSetting().fec.level) + + // later settings updates keep the requested level + remoteVideoPub.setVideoFps(30) + advanceUntilIdle() + assertEquals(30, lastTrackSetting().fps) + assertEquals(LivekitRtc.FECProtectionMode.FEC_MODE_FIXED, lastTrackSetting().fec.mode) + assertEquals(LivekitRtc.FECProtectionLevel.FEC_LEVEL_HIGH, lastTrackSetting().fec.level) + + remoteVideoPub.setFecProtection(FecProtection.Adaptive) + advanceUntilIdle() + assertEquals(LivekitRtc.FECProtectionMode.FEC_MODE_ADAPTIVE, lastTrackSetting().fec.mode) + assertEquals(LivekitRtc.FECProtectionLevel.FEC_LEVEL_UNSPECIFIED, lastTrackSetting().fec.level) + + // disabling is sent explicitly rather than omitted + remoteVideoPub.setFecProtection(FecProtection.Disabled) + advanceUntilIdle() + assertTrue(lastTrackSetting().hasFec()) + assertEquals(LivekitRtc.FECProtectionMode.FEC_MODE_DISABLED, lastTrackSetting().fec.mode) + } + + @Test + fun fecProtectionWithAdaptiveStream() = runTest { + room.adaptiveStream = true + val remoteVideoPub = subscribeRemoteVideo() + advanceUntilIdle() + wsFactory.ws.clearRequests() + + // applies even when adaptive stream manages the track's quality + remoteVideoPub.setFecProtection(FecProtection.Minimum(FecProtectionLevel.MEDIUM)) + advanceUntilIdle() + + assertEquals(LivekitRtc.FECProtectionMode.FEC_MODE_MINIMUM, lastTrackSetting().fec.mode) + assertEquals(LivekitRtc.FECProtectionLevel.FEC_LEVEL_MEDIUM, lastTrackSetting().fec.level) + } + @Test fun subscriptionStatus() = runTest { connect() diff --git a/protocol b/protocol index 30d320b79..3a115c89c 160000 --- a/protocol +++ b/protocol @@ -1 +1 @@ -Subproject commit 30d320b796396930fbfa1d82932bfbfdea314353 +Subproject commit 3a115c89c9285e738e6db8e681af0acfd10570db