From 8cb643c79f4f6178edfe7cc98f6f69ac6fb5dad3 Mon Sep 17 00:00:00 2001 From: Sen Chang Date: Fri, 6 Nov 2026 09:52:57 -0800 Subject: [PATCH 1/6] Bundle the publisher offer with the /rtc/v1 join request In single peer connection mode the client is the only offerer, so the publisher, its three data channels and the first offer are built before the signal socket opens and the offer travels in JoinRequest.publisher_offer. The server answers it over the signal channel right after the JoinResponse, which removes one offer/answer round trip from the connect path. PeerConnectionTransport.createInitialOffer creates and munges the offer but does not apply it: setLocalDescription would start ICE gathering before the server's ICE servers are known. The offer stays pending and setRemoteDescription applies it just before the answer. While it is pending, negotiate queues a renegotiation the same way as for an offer already in flight. Offer creation and munging are shared with createAndSendOffer through createMungedOffer. RTCEngine builds the early publisher with a client-side configuration (makeRTCConfig with no server response) and configure adopts it, installing the server's ICE servers and relay policy through updateRTCConfig before the pending offer can be applied. The eager negotiate on join is skipped while the initial offer is pending. Not yet handled: closing the early publisher when the join fails, and the /rtc/v1 404 fallback, which still adopts it. The two fallback reconnect tests in SinglePeerConnectionFallbackMockE2ETest fail until then. --- .../android/room/PeerConnectionTransport.kt | 197 ++++++++++---- .../java/io/livekit/android/room/RTCEngine.kt | 145 +++++++---- .../io/livekit/android/room/SignalClient.kt | 8 +- .../room/SinglePeerConnectionTransport.kt | 2 +- .../android/test/mock/MockPeerConnection.kt | 7 +- .../room/PeerConnectionTransportTest.kt | 241 ++++++++++++++++++ .../room/SinglePeerConnectionTransportTest.kt | 8 + 7 files changed, 494 insertions(+), 114 deletions(-) create mode 100644 livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt index e7ff05ed9..a3ef66d85 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt @@ -114,6 +114,7 @@ constructor( private var isClosed = AtomicBoolean(false) private val latestOfferId = AtomicInteger(0) + private var initialOffer: InitialOffer? = null interface Listener { fun onOffer(sd: SessionDescription, offerId: Int) @@ -148,7 +149,11 @@ constructor( if (sd.type == SessionDescription.Type.ANSWER && isStaleAnswer(offerId)) { return@launchRTCIfNotClosed Either.Right("Old offer, ignoring. Expected: ${latestOfferId.get()}, actual: $offerId") } + initialOffer?.let { + peerConnection.setLocalDescription(it.sessionDescription) + } val result = peerConnection.setRemoteDescription(sd) + initialOffer = null if (result is Either.Left) { pendingCandidates.forEach { pending -> peerConnection.addIceCandidate(pending) @@ -167,6 +172,60 @@ constructor( return result } + /** + * An offer from [createInitialOffer]: the description to bundle with the join request, and + * the offer id the server's answer carries. + */ + data class InitialOffer( + val sessionDescription: SessionDescription, + val offerId: Int, + ) + + /** + * Creates the offer that is bundled with the join request instead of being sent through + * [Listener.onOffer]. + * + * The offer is created with [constraints] and the usual munging, and takes the next offer id, + * but it is not set as the local description: that would start ICE gathering before the + * server's ICE servers are known. It is kept pending instead, and [setRemoteDescription] + * applies it as the local description just before the answer, whatever the answer's offer id. + * While it is pending, a [negotiate] request is queued the same way as when an offer is already + * in flight, and runs once the answer is applied. [clearPendingInitialOffer] and [close] drop it. + * + * @return the offer, or an error message if the signaling state is not stable, the offer + * could not be created, or the peer connection is closed. + */ + suspend fun createInitialOffer(constraints: MediaConstraints = MediaConstraints()): Either { + offerLock.withLock { + return launchRTCIfNotClosed> { + val signalingState = peerConnection.signalingState() + if (signalingState != SignalingState.STABLE) { + return@launchRTCIfNotClosed Either.Right("Signaling state is $signalingState, cannot create the initial offer") + } + val offerId = latestOfferId.incrementAndGet() + val munged = when (val outcome = createMungedOffer(constraints)) { + is Either.Left -> outcome.value + is Either.Right -> return@launchRTCIfNotClosed Either.Right(outcome.value) + } + val offer = InitialOffer(SessionDescription(munged.original.type, munged.description), offerId) + initialOffer = offer + Either.Left(offer) + } ?: Either.Right("PCT is closed.") + } + } + + /** + * Drops the offer kept by [createInitialOffer], when the join it was bundled with did not + * succeed, so [negotiate] offers normally again. + */ + fun clearPendingInitialOffer() { + initialOffer = null + } + + fun hasPendingInitialOffer(): Boolean { + return initialOffer != null + } + val negotiate = debounce(20, coroutineScope) { if (it != null) { createAndSendOffer(it) @@ -194,7 +253,7 @@ constructor( restartingIce = true } - if (peerConnection.signalingState() == SignalingState.HAVE_LOCAL_OFFER) { + if (peerConnection.signalingState() == SignalingState.HAVE_LOCAL_OFFER || initialOffer != null) { // we're waiting for the peer to accept our offer, so we'll just wait // the only exception to this is when ICE restart is needed val curSd = peerConnection.remoteDescription @@ -215,7 +274,7 @@ constructor( // this may skip some ids, but is not an issue. offerId = latestOfferId.incrementAndGet() - val sdpOffer = when (val outcome = peerConnection.createOffer(constraints)) { + val munged = when (val outcome = createMungedOffer(constraints)) { is Either.Left -> outcome.value is Either.Right -> { LKLog.d { "error creating offer: ${outcome.value}" } @@ -223,60 +282,10 @@ constructor( } } - if (isClosed()) { - return@launchRTCIfNotClosed - } - - // WARP (draft-uberti-tsvwg-warp) shows up as a=goog-sped-v1 (DTLS handshake carried - // in the ICE exchange) and a=sctp-init (SCTP INIT params in the SDP, skipping the - // cookie exchange). libwebrtc only emits them when the acceleration is actually in - // use, so this reports what went on the wire. - LKLog.i { - "WARP: SPED ${if (sdpOffer.description.contains("goog-sped-v1")) "enabled" else "disabled"}, " + - "SNAP ${if (sdpOffer.description.contains("a=sctp-init")) "enabled" else "disabled"}" - } - - // munge sdp - val sdpDescription = sdpFactory.createSessionDescription(sdpOffer.description) - - val mediaDescs = sdpDescription.getMediaDescriptions(true) - .filterIsInstance() - // The publisher PeerConnection may negotiate before any video is published - // (for example, data channel only or audio first). Those offers should not - // consume the video start hint. When the first video offer is created, use - // one connection-level value across all video m-sections so libwebrtc's - // last-writer-wins handling cannot depend on SDP m-section order. - val setupCompleted = connectionSetupTime != null - val connectionSetupTime = connectionSetupTimeForOffer() - val connectionStartBitrate = if (!hasAppliedVideoStartBitrate) { - computeConnectionStartBitrate(mediaDescs, trackBitrates, connectionSetupTime) - } else { - null - } - if (connectionStartBitrate != null) { - LKLog.i { - "Applying x-google-start-bitrate=$connectionStartBitrate kbps " + - "(connection setup ${connectionSetupTime?.inWholeMilliseconds} ms" + - "${if (setupCompleted) "" else ", still connecting"})" - } - } - for (mediaDesc in mediaDescs) { - if (mediaDesc.media.mediaType == "audio") { - // TODO - } else if (mediaDesc.media.mediaType == "video") { - ensureVideoDDExtensionForSVC(mediaDesc) - ensureCodecBitrates( - mediaDesc, - trackBitrates = trackBitrates, - connectionStartBitrate = connectionStartBitrate, - ) - } - } - val mungedDescription = sdpDescription.toString() - finalSdp = setMungedSdp(sdpOffer, mungedDescription) + finalSdp = setMungedSdp(munged.original, munged.description) // setMungedSdp may fall back to the original SDP. Only mark the one-shot // hint as used after the SDP with the hint is accepted locally. - if (connectionStartBitrate != null && finalSdp?.description == mungedDescription) { + if (munged.connectionStartBitrate != null && finalSdp?.description == munged.description) { hasAppliedVideoStartBitrate = true } } @@ -296,6 +305,85 @@ constructor( } } + /** + * An offer from [createMungedOffer]. + * + * @param original the offer as libwebrtc created it, to fall back to if [description] is rejected. + * @param description the munged offer. + * @param connectionStartBitrate the start bitrate hint written into [description], if any. + */ + private class MungedOffer( + val original: SessionDescription, + val description: String, + val connectionStartBitrate: Long?, + ) + + /** + * Creates an offer with [constraints] and munges it. Runs on the RTC thread. + * + * The munge adds the dependency descriptor extension to SVC video sections and the + * connection-level start bitrate hint to video sections with a registered track bitrate. + * Writing the hint is one-shot per peer connection, so the caller marks + * [hasAppliedVideoStartBitrate] once libwebrtc has accepted [MungedOffer.description]. + * + * @return the offer, or an error message if it could not be created or the peer connection + * is closed. + */ + private suspend fun createMungedOffer(constraints: MediaConstraints): Either { + val sdpOffer = when (val outcome = peerConnection.createOffer(constraints)) { + is Either.Left -> outcome.value + is Either.Right -> return Either.Right(outcome.value) + } + + if (isClosed()) { + return Either.Right("PCT is closed.") + } + + // WARP (draft-uberti-tsvwg-warp) shows up as a=goog-sped-v1 (DTLS handshake carried + // in the ICE exchange) and a=sctp-init (SCTP INIT params in the SDP, skipping the + // cookie exchange). libwebrtc only emits them when the acceleration is actually in + // use, so this reports what went on the wire. + LKLog.i { + "WARP: SPED ${if (sdpOffer.description.contains("goog-sped-v1")) "enabled" else "disabled"}, " + + "SNAP ${if (sdpOffer.description.contains("a=sctp-init")) "enabled" else "disabled"}" + } + + val sdpDescription = sdpFactory.createSessionDescription(sdpOffer.description) + + val mediaDescs = sdpDescription.getMediaDescriptions(true) + .filterIsInstance() + // The publisher PeerConnection may negotiate before any video is published + // (for example, data channel only or audio first). Those offers should not + // consume the video start hint. When the first video offer is created, use + // one connection-level value across all video m-sections so libwebrtc's + // last-writer-wins handling cannot depend on SDP m-section order. + val setupCompleted = connectionSetupTime != null + val connectionSetupTime = connectionSetupTimeForOffer() + val connectionStartBitrate = if (!hasAppliedVideoStartBitrate) { + computeConnectionStartBitrate(mediaDescs, trackBitrates, connectionSetupTime) + } else { + null + } + if (connectionStartBitrate != null) { + LKLog.i { + "Applying x-google-start-bitrate=$connectionStartBitrate kbps " + + "(connection setup ${connectionSetupTime?.inWholeMilliseconds} ms" + + "${if (setupCompleted) "" else ", still connecting"})" + } + } + for (mediaDesc in mediaDescs) { + if (mediaDesc.media.mediaType == "video") { + ensureVideoDDExtensionForSVC(mediaDesc) + ensureCodecBitrates( + mediaDesc, + trackBitrates = trackBitrates, + connectionStartBitrate = connectionStartBitrate, + ) + } + } + return Either.Left(MungedOffer(sdpOffer, sdpDescription.toString(), connectionStartBitrate)) + } + private suspend fun setMungedSdp(sdp: SessionDescription, mungedDescription: String, remote: Boolean = false): SessionDescription { val mungedSdp = SessionDescription(sdp.type, mungedDescription) @@ -375,6 +463,7 @@ constructor( launchRTCIfNotClosed { isClosed.set(true) peerConnection.dispose() + initialOffer = null } coroutineScope.cancel() } diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt index c8d95b402..2ca581b48 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt @@ -64,6 +64,7 @@ import io.livekit.android.webrtc.isDisconnected import io.livekit.android.webrtc.peerconnection.RTCThreadToken import io.livekit.android.webrtc.peerconnection.executeBlockingOnRTCThread import io.livekit.android.webrtc.peerconnection.launchBlockingOnRTCThread +import io.livekit.android.webrtc.toProtoSessionDescription import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob @@ -103,6 +104,7 @@ import javax.inject.Inject import javax.inject.Named import javax.inject.Singleton import kotlin.coroutines.Continuation +import kotlin.coroutines.coroutineContext import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.time.Duration.Companion.milliseconds @@ -299,8 +301,25 @@ internal constructor( connectionState = ConnectionState.CONNECTING } var effectiveOptions = options + var earlyPublisher: PeerConnectionTransport? = null + var earlyPublisherObserver: DuplexTransportObserver? = null + if (effectiveOptions.singlePeerConnection) { + earlyPublisherObserver = DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) + val rtcConfig = makeRTCConfig(null, effectiveOptions) + earlyPublisher = pctFactory.create( + rtcConfig, + earlyPublisherObserver, + earlyPublisherObserver, + ) + createPublisherDataChannels(earlyPublisher) + } + + val offerValue = when (val earlyOffer = earlyPublisher?.createInitialOffer(getPublisherOfferConstraints())) { + is Either.Left -> earlyOffer.value.sessionDescription.toProtoSessionDescription(earlyOffer.value.offerId) + else -> null + } val joinResponse = try { - client.join(url, token, options, roomOptions) + client.join(url, token, options, roomOptions, offerValue) } catch (e: RoomException.ConnectException) { // A server that predates /rtc/v1 answers that handshake with 404. if (!options.singlePeerConnection || e.statusCode != 404) { @@ -338,7 +357,7 @@ internal constructor( isSubscriberPrimary = !effectiveOptions.singlePeerConnection && joinResponse.subscriberPrimary - configure(joinResponse, effectiveOptions) + configure(joinResponse, effectiveOptions, earlyPublisher, earlyPublisherObserver) // The publisher created above needs the attempt's start time before its first offer, in // case video is published before the primary transport connects. transport?.publisher?.setConnectStartedAt(startedAtMs) @@ -349,12 +368,18 @@ internal constructor( if (transport?.shouldNegotiatePublisherOnJoin(hasPublished) == true) { negotiatePublisher() } + client.onReadyForResponses() return@coroutineScope joinResponse } - private suspend fun configure(joinResponse: JoinResponse, connectOptions: ConnectOptions) { + private suspend fun configure( + joinResponse: JoinResponse, + connectOptions: ConnectOptions, + earlyPublisher: PeerConnectionTransport?, + earlyObserver: PublisherTransportObserver? + ) { launchBlockingOnRTCThread(rtcThreadToken) { configurationLock.withCheckLock( { @@ -365,11 +390,7 @@ internal constructor( } }, ) { - val publisherObserver = if (connectOptions.singlePeerConnection) { - DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) - } else { - PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) - } + val publisherObserver = earlyObserver ?: PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) this@RTCEngine.publisherObserver = publisherObserver participantSid = if (joinResponse.hasParticipant()) { @@ -380,11 +401,18 @@ internal constructor( // Setup peer connections val rtcConfig = makeRTCConfig(Either.Left(joinResponse), connectOptions) - val publisher = pctFactory.create( - rtcConfig, - publisherObserver, - publisherObserver, - ) + + val publisher = if (earlyPublisher != null) { + earlyPublisher.updateRTCConfig(rtcConfig) + hasPublished = true + earlyPublisher + } else { + pctFactory.create( + rtcConfig, + publisherObserver, + publisherObserver, + ) + } val connectionStateListener: PeerConnectionStateListener = { newState -> LKLog.v { "onIceConnection new state: $newState" } @@ -441,45 +469,9 @@ internal constructor( } ensureActive() - // data channels - val reliableInit = DataChannel.Init() - reliableInit.ordered = true - reliableDataChannel = publisher.withPeerConnection { - createDataChannel( - RELIABLE_DATA_CHANNEL_LABEL, - reliableInit, - ).also { dataChannel -> - - val dataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) - reliableDataChannelManager = dataChannelManager - dataChannel.registerObserver(dataChannelManager) - reliableBufferedAmountJob?.cancel() - reliableBufferedAmountJob = coroutineScope.launch { - dataChannelManager::bufferedAmount.flow.collect { bufferedAmount -> - synchronized(reliableStateLock) { - reliableMessageBuffer.trim(bufferedAmount) - } - } - } - } + if (earlyPublisher == null) { + createPublisherDataChannels(publisher) } - - ensureActive() - val lossyInit = DataChannel.Init() - lossyInit.ordered = false - lossyInit.maxRetransmits = 0 - lossyDataChannel = publisher.withPeerConnection { - createDataChannel( - LOSSY_DATA_CHANNEL_LABEL, - lossyInit, - ).also { dataChannel -> - lossyDataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) - dataChannel.registerObserver(lossyDataChannelManager) - } - } - - ensureActive() - createPublisherDataTrackChannel() } } } @@ -1044,13 +1036,51 @@ internal constructor( } /** - * Creates the publisher `_data_track` channel and hands it to [dataTrackPublisherChannel]. + * Creates the publisher's three data channels on [publisher]: reliable, lossy, and the + * `_data_track` channel handed to [dataTrackPublisherChannel]. They are created before the + * publisher's first offer, so that offer carries their `m=application` section. */ - private suspend fun createPublisherDataTrackChannel() { + private suspend fun createPublisherDataChannels(publisher: PeerConnectionTransport) { + val reliableInit = DataChannel.Init() + reliableInit.ordered = true + reliableDataChannel = publisher.withPeerConnection { + createDataChannel( + RELIABLE_DATA_CHANNEL_LABEL, + reliableInit, + ).also { dataChannel -> + val dataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) + reliableDataChannelManager = dataChannelManager + dataChannel.registerObserver(dataChannelManager) + reliableBufferedAmountJob?.cancel() + reliableBufferedAmountJob = coroutineScope.launch { + dataChannelManager::bufferedAmount.flow.collect { bufferedAmount -> + synchronized(reliableStateLock) { + reliableMessageBuffer.trim(bufferedAmount) + } + } + } + } + } + + coroutineContext.ensureActive() + val lossyInit = DataChannel.Init() + lossyInit.ordered = false + lossyInit.maxRetransmits = 0 + lossyDataChannel = publisher.withPeerConnection { + createDataChannel( + LOSSY_DATA_CHANNEL_LABEL, + lossyInit, + ).also { dataChannel -> + lossyDataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) + dataChannel.registerObserver(lossyDataChannelManager) + } + } + + coroutineContext.ensureActive() val dataTrackInit = DataChannel.Init() dataTrackInit.ordered = false dataTrackInit.maxRetransmits = 0 - transport?.publisher?.withPeerConnection { + publisher.withPeerConnection { createDataChannel( DATA_TRACK_DATA_CHANNEL_LABEL, dataTrackInit, @@ -1097,7 +1127,7 @@ internal constructor( } private fun makeRTCConfig( - serverResponse: Either, + serverResponse: Either?, connectOptions: ConnectOptions, ): RTCConfiguration { // Convert protobuf ice servers @@ -1106,12 +1136,13 @@ internal constructor( val responseServers = when (serverResponse) { is Either.Left -> serverResponse.value.iceServersList is Either.Right -> serverResponse.value.iceServersList + null -> emptyList() } for (serverInfo in responseServers) { servers.add(serverInfo.toWebrtc()) } - if (servers.isEmpty()) { + if (servers.isEmpty() && serverResponse != null) { servers.addAll(SignalClient.DEFAULT_ICE_SERVERS) } servers @@ -1163,6 +1194,8 @@ internal constructor( null } } + + null -> null } if (clientConfig != null) { if (clientConfig.forceRelay == LivekitModels.ClientConfigSetting.ENABLED) { 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..06d9062da 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 @@ -152,8 +152,9 @@ constructor( token: String, options: ConnectOptions = ConnectOptions(), roomOptions: RoomOptions = RoomOptions(), + publisherOffer: LivekitRtc.SessionDescription? = null ): JoinResponse { - return when (val result = connect(url, token, options, roomOptions)) { + return when (val result = connect(url, token, options, roomOptions, publisherOffer)) { is ConnectResult.Join -> result.response else -> throw IllegalStateException("Unexpected response during join: $result") } @@ -175,6 +176,7 @@ constructor( this.participantSid = participantSid }, lastRoomOptions ?: RoomOptions(), + null ) ) { is ConnectResult.Reconnect -> Either.Left(result.response) @@ -188,6 +190,8 @@ constructor( token: String, options: ConnectOptions, roomOptions: RoomOptions, + publisherOffer: LivekitRtc.SessionDescription?, + ): ConnectResult { // Clean up any pre-existing connection. close(reason = "Starting new connection", shouldClearQueuedRequests = false) @@ -196,7 +200,7 @@ constructor( isReconnecting = options.reconnect if (options.singlePeerConnection) { - wsUrlString = SignalUrlBuilder.buildJoinRequestUrl(wsUrlString, options, roomOptions, networkInfo.getNetworkType()) + wsUrlString = SignalUrlBuilder.buildJoinRequestUrl(wsUrlString, options, roomOptions, networkInfo.getNetworkType(), publisherOffer) // The join request carries client metadata, so only the path is logged. LKLog.i { "connecting to ${wsUrlString.substringBefore('?')}?${SignalUrlBuilder.QUERY_JOIN_REQUEST}=" } } else { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt index 03373b1ec..67040526b 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt @@ -61,7 +61,7 @@ internal class SinglePeerConnectionTransport( get() = emptyList() override fun shouldNegotiatePublisherOnJoin(hasPublished: Boolean): Boolean { - return true + return !publisher.hasPendingInitialOffer() } override fun addIceCandidate(candidate: IceCandidate, target: LivekitRtc.SignalTarget): Boolean { diff --git a/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt b/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt index b7375db1e..c3e3035b0 100644 --- a/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt +++ b/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt @@ -113,9 +113,14 @@ class MockPeerConnection( */ var lastOfferConstraints: MediaConstraints? = null + /** + * The description every [createOffer] returns. + */ + var offerDescription: String = "local_offer" + override fun createOffer(observer: SdpObserver?, constraints: MediaConstraints?) { lastOfferConstraints = constraints - val sdp = SessionDescription(SessionDescription.Type.OFFER, "local_offer") + val sdp = SessionDescription(SessionDescription.Type.OFFER, offerDescription) observer?.onCreateSuccess(sdp) } diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt new file mode 100644 index 000000000..f933ba0a7 --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt @@ -0,0 +1,241 @@ +/* + * 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 + +import android.javax.sdp.SdpFactory +import io.livekit.android.room.util.MediaConstraintKeys +import io.livekit.android.room.util.findConstraint +import io.livekit.android.test.mock.MockPeerConnection +import io.livekit.android.test.mock.MockRTCThreadToken +import io.livekit.android.util.Either +import io.livekit.android.webrtc.JainSdpUtilsTest +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withTimeout +import livekit.org.webrtc.MediaConstraints +import livekit.org.webrtc.MockPeerConnectionFactory +import livekit.org.webrtc.PeerConnection +import livekit.org.webrtc.PeerConnection.RTCConfiguration +import livekit.org.webrtc.PeerConnection.SignalingState +import livekit.org.webrtc.SdpObserver +import livekit.org.webrtc.SessionDescription +import livekit.org.webrtc.WebRTCInitializer +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.mockito.kotlin.mock + +class PeerConnectionTransportTest { + + private class RecordingListener : PeerConnectionTransport.Listener { + val offers = mutableListOf>() + + override fun onOffer(sd: SessionDescription, offerId: Int) { + offers.add(sd to offerId) + } + } + + private val listener = RecordingListener() + private lateinit var transport: PeerConnectionTransport + + private val pc: MockPeerConnection + get() = transport.peerConnection as MockPeerConnection + + @Before + fun setup() { + WebRTCInitializer.initialize() + transport = PeerConnectionTransport( + config = RTCConfiguration(emptyList()), + pcObserver = mock(), + listener = listener, + ioDispatcher = Dispatchers.Unconfined, + connectionFactory = MockPeerConnectionFactory(), + sdpFactory = SdpFactory.getInstance(), + rtcThreadToken = MockRTCThreadToken(), + ) + // The initial offer is stored munged, which needs an offer that parses as SDP. + pc.offerDescription = JainSdpUtilsTest.DESCRIPTION + } + + private fun answer() = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer") + + private suspend fun initialOffer(constraints: MediaConstraints = MediaConstraints()): PeerConnectionTransport.InitialOffer { + return when (val result = transport.createInitialOffer(constraints)) { + is Either.Left -> result.value + is Either.Right -> throw AssertionError("createInitialOffer failed: ${result.value}") + } + } + + /** + * [PeerConnectionTransport.negotiate] is debounced on the real clock, so offers are awaited + * rather than scheduled. + */ + private suspend fun awaitOffers(count: Int) { + withTimeout(OFFER_TIMEOUT_MS) { + while (listener.offers.size < count) { + delay(10) + } + } + } + + // region createInitialOffer + + @Test + fun initialOfferTakesTheFirstOfferId() = runTest { + val offer = initialOffer() + + assertEquals(1, offer.offerId) + assertEquals(SessionDescription.Type.OFFER, offer.sessionDescription.type) + assertTrue(offer.sessionDescription.description.isNotEmpty()) + } + + @Test + fun initialOfferUsesTheGivenConstraints() = runTest { + val constraints = MediaConstraints().apply { + mandatory.add(MediaConstraints.KeyValuePair(MediaConstraintKeys.OFFER_TO_RECV_AUDIO, MediaConstraintKeys.FALSE)) + } + + initialOffer(constraints) + + assertEquals( + MediaConstraintKeys.FALSE, + pc.lastOfferConstraints?.findConstraint(MediaConstraintKeys.OFFER_TO_RECV_AUDIO), + ) + } + + @Test + fun initialOfferIsMunged() = runTest { + pc.offerDescription = SdpMungingTest.NO_DD_DESCRIPTION + + val offer = initialOffer() + + assertTrue( + "AV1 section is missing the dependency descriptor extension", + offer.sessionDescription.description.contains(DD_EXTENSION_URI), + ) + } + + @Test + fun initialOfferIsNotSetAsLocalDescription() = runTest { + initialOffer() + + assertNull(pc.localDesc) + assertEquals(SignalingState.STABLE, pc.signalingState()) + } + + @Test + fun initialOfferIsNotSentToTheListener() = runBlocking { + initialOffer() + delay(OFFER_SETTLE_MS) + + assertTrue(listener.offers.isEmpty()) + } + + @Test + fun initialOfferFailsOutsideStableState() = runTest { + pc.setLocalDescription(NoopSdpObserver, SessionDescription(SessionDescription.Type.OFFER, "in_flight_offer")) + assertEquals(SignalingState.HAVE_LOCAL_OFFER, pc.signalingState()) + + assertTrue(transport.createInitialOffer() is Either.Right) + } + + @Test + fun initialOfferFailsWhenClosed() = runTest { + transport.close() + + assertTrue(transport.createInitialOffer() is Either.Right) + } + + // endregion + + // region answer + + @Test + fun answerAppliesThePendingOfferFirst() = runTest { + val offer = initialOffer() + + val result = transport.setRemoteDescription(answer(), offer.offerId) + + assertTrue(result is Either.Left) + assertEquals(offer.sessionDescription.description, pc.localDesc?.description) + assertEquals("remote_answer", pc.remoteDesc?.description) + assertEquals(SignalingState.STABLE, pc.signalingState()) + } + + @Test + fun answerWithoutAnOfferIdAppliesThePendingOffer() = runTest { + val offer = initialOffer() + + val result = transport.setRemoteDescription(answer(), 0) + + assertTrue(result is Either.Left) + assertEquals(offer.sessionDescription.description, pc.localDesc?.description) + } + + @Test + fun negotiationWaitsForThePendingOfferToBeAnswered() = runBlocking { + val offer = initialOffer() + + transport.negotiate(null) + delay(OFFER_SETTLE_MS) + assertTrue("offered over the pending initial offer", listener.offers.isEmpty()) + assertEquals(SignalingState.STABLE, pc.signalingState()) + + assertTrue(transport.setRemoteDescription(answer(), offer.offerId) is Either.Left) + + awaitOffers(1) + assertEquals(2, listener.offers.single().second) + assertEquals(SignalingState.HAVE_LOCAL_OFFER, pc.signalingState()) + } + + // endregion + + // region clearPendingInitialOffer + + @Test + fun clearingThePendingOfferResumesNegotiation() = runBlocking { + initialOffer() + transport.clearPendingInitialOffer() + + transport.negotiate(null) + + awaitOffers(1) + assertEquals(2, listener.offers.single().second) + assertEquals(SignalingState.HAVE_LOCAL_OFFER, pc.signalingState()) + } + + // endregion + + private object NoopSdpObserver : SdpObserver { + override fun onCreateSuccess(sdp: SessionDescription?) {} + override fun onSetSuccess() {} + override fun onCreateFailure(error: String?) {} + override fun onSetFailure(error: String?) {} + } + + companion object { + private const val OFFER_TIMEOUT_MS = 2_000L + private const val DD_EXTENSION_URI = "https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension" + + /** Comfortably longer than the negotiate debounce. */ + private const val OFFER_SETTLE_MS = 200L + } +} diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt index e38be5953..ce1501ba8 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt @@ -137,6 +137,14 @@ class SinglePeerConnectionTransportTest { assertTrue(transport().shouldNegotiatePublisherOnJoin(hasPublished = true)) } + @Test + fun publisherIsNotNegotiatedOnJoinWhileItsInitialOfferIsPending() { + whenever(publisher.hasPendingInitialOffer()).thenReturn(true) + + assertFalse(transport().shouldNegotiatePublisherOnJoin(hasPublished = false)) + assertFalse(transport().shouldNegotiatePublisherOnJoin(hasPublished = true)) + } + // endregion // region addIceCandidate From 2039f8bad3762548876d88dd2b9a66ab245938f8 Mon Sep 17 00:00:00 2001 From: Sen Chang Date: Mon, 9 Nov 2026 13:08:41 -0800 Subject: [PATCH 2/6] Own the early publisher in RTCEngine until configure adopts it The publisher created ahead of a single peer connection join lived in locals of joinImpl, so a join that failed before configure ran left it with no owner, and the /rtc/v1 404 fallback handed the already-closed publisher to configure, which adopted it into the dual peer connection transport. RTCEngine now holds the early publisher and its observer in fields. configure adopts them into the transport and clears the fields once the data channels are in place; closeResources closes a publisher that was never adopted, which covers a failed or cancelled join through the engine's normal teardown. The 404 fallback calls closeEarlyPublisher before the legacy join, so that join starts from nothing. The helper also disposes the publisher's data channels, and closeResources reuses it for that side of the teardown. The two SinglePeerConnectionFallbackMockE2ETest reconnect tests pass again. --- .../java/io/livekit/android/room/RTCEngine.kt | 86 +++++++++++-------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt index 2ca581b48..0c3b1cf92 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt @@ -210,6 +210,12 @@ internal constructor( private var publisherObserver: PublisherTransportObserver? = null private val subscriberObserver = SubscriberTransportObserver(this, client, rtcThreadToken) + // Publisher created ahead of a single peer connection join so its offer rides in the /rtc/v1 + // request. configure adopts it into the transport and clears it; until then closeResources + // disposes it. + private var earlyPublisher: PeerConnectionTransport? = null + private var earlyPublisherObserver: DuplexTransportObserver? = null + internal var transport: Transport? = null private var reliableDataChannel: DataChannel? = null private var reliableDataChannelSub: DataChannel? = null @@ -301,23 +307,23 @@ internal constructor( connectionState = ConnectionState.CONNECTING } var effectiveOptions = options - var earlyPublisher: PeerConnectionTransport? = null - var earlyPublisherObserver: DuplexTransportObserver? = null if (effectiveOptions.singlePeerConnection) { - earlyPublisherObserver = DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) - val rtcConfig = makeRTCConfig(null, effectiveOptions) - earlyPublisher = pctFactory.create( - rtcConfig, - earlyPublisherObserver, - earlyPublisherObserver, + val observer = DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) + val publisher = pctFactory.create( + makeRTCConfig(null, effectiveOptions), + observer, + observer, ) - createPublisherDataChannels(earlyPublisher) + earlyPublisherObserver = observer + earlyPublisher = publisher + createPublisherDataChannels(publisher) } val offerValue = when (val earlyOffer = earlyPublisher?.createInitialOffer(getPublisherOfferConstraints())) { is Either.Left -> earlyOffer.value.sessionDescription.toProtoSessionDescription(earlyOffer.value.offerId) else -> null } + val joinResponse = try { client.join(url, token, options, roomOptions, offerValue) } catch (e: RoomException.ConnectException) { @@ -329,6 +335,7 @@ internal constructor( LKLog.w { "server has no /rtc/v1 path, retrying on the legacy path with dual peer connections" } effectiveOptions = options.copy(singlePeerConnection = false) connectOptions = effectiveOptions + closeEarlyPublisher() try { client.join(url, token, effectiveOptions, roomOptions) } catch (e: RoomException.ConnectException) { @@ -357,7 +364,7 @@ internal constructor( isSubscriberPrimary = !effectiveOptions.singlePeerConnection && joinResponse.subscriberPrimary - configure(joinResponse, effectiveOptions, earlyPublisher, earlyPublisherObserver) + configure(joinResponse, effectiveOptions) // The publisher created above needs the attempt's start time before its first offer, in // case video is published before the primary transport connects. transport?.publisher?.setConnectStartedAt(startedAtMs) @@ -374,12 +381,7 @@ internal constructor( return@coroutineScope joinResponse } - private suspend fun configure( - joinResponse: JoinResponse, - connectOptions: ConnectOptions, - earlyPublisher: PeerConnectionTransport?, - earlyObserver: PublisherTransportObserver? - ) { + private suspend fun configure(joinResponse: JoinResponse, connectOptions: ConnectOptions) { launchBlockingOnRTCThread(rtcThreadToken) { configurationLock.withCheckLock( { @@ -390,7 +392,7 @@ internal constructor( } }, ) { - val publisherObserver = earlyObserver ?: PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) + val publisherObserver = earlyPublisherObserver ?: PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) this@RTCEngine.publisherObserver = publisherObserver participantSid = if (joinResponse.hasParticipant()) { @@ -402,17 +404,14 @@ internal constructor( // Setup peer connections val rtcConfig = makeRTCConfig(Either.Left(joinResponse), connectOptions) - val publisher = if (earlyPublisher != null) { - earlyPublisher.updateRTCConfig(rtcConfig) + val publisher = earlyPublisher?.also { + it.updateRTCConfig(rtcConfig) hasPublished = true - earlyPublisher - } else { - pctFactory.create( - rtcConfig, - publisherObserver, - publisherObserver, - ) - } + } ?: pctFactory.create( + rtcConfig, + publisherObserver, + publisherObserver, + ) val connectionStateListener: PeerConnectionStateListener = { newState -> LKLog.v { "onIceConnection new state: $newState" } @@ -472,6 +471,9 @@ internal constructor( if (earlyPublisher == null) { createPublisherDataChannels(publisher) } + // The transport owns the publisher from here on. + earlyPublisher = null + earlyPublisherObserver = null } } } @@ -582,21 +584,13 @@ internal constructor( subscriberObserver.connectionChangeListener = null transport?.close() transport = null - reliableBufferedAmountJob?.cancel() - reliableBufferedAmountJob = null - reliableDataChannelManager?.dispose() - reliableDataChannelManager = null - reliableDataChannel = null + closeEarlyPublisher() reliableDataChannelSubManager?.dispose() reliableDataChannelSubManager = null reliableDataChannelSub = null - lossyDataChannelManager?.dispose() - lossyDataChannelManager = null - lossyDataChannel = null lossyDataChannelSubManager?.dispose() lossyDataChannelSubManager = null lossyDataChannelSub = null - dataTrackPublisherChannel.detach() dataTrackDataChannelSub = null isSubscriberPrimary = false } @@ -1035,6 +1029,26 @@ internal constructor( } } + /** + * Closes the early publisher when one is still unadopted, and the publisher's data channels + * with it. The 404 fallback calls this so the legacy join starts from nothing; closeResources + * calls it when tearing the connection down. + */ + private suspend fun closeEarlyPublisher() { + earlyPublisher?.close() + earlyPublisher = null + earlyPublisherObserver = null + reliableBufferedAmountJob?.cancel() + reliableBufferedAmountJob = null + reliableDataChannelManager?.dispose() + reliableDataChannelManager = null + reliableDataChannel = null + lossyDataChannelManager?.dispose() + lossyDataChannelManager = null + lossyDataChannel = null + dataTrackPublisherChannel.detach() + } + /** * Creates the publisher's three data channels on [publisher]: reliable, lossy, and the * `_data_track` channel handed to [dataTrackPublisherChannel]. They are created before the From 72aee711ddd388ea7d09cfc1116c49827290af05 Mon Sep 17 00:00:00 2001 From: Sen Chang Date: Fri, 13 Nov 2026 12:55:02 -0800 Subject: [PATCH 3/6] Answer the join offer in the single peer connection E2E tests SinglePeerConnectionMockE2ETest answered the last offer sent over the signal connection after joining. With the publisher offer bundled in the /rtc/v1 join request there is no such offer, so every test that connects failed on an empty list. The connect helper now decodes the offer from the join request URL and answers that one by id. publisherOfferRidesInTheJoinRequest checks that the offer travels with the join request and that nothing is offered over the signal connection before or after its answer. The detekt baseline entry for makeRTCConfig takes the nullable server response the function has had since the early publisher was added. --- .../detekt-baseline-release.xml | 2 +- .../room/SinglePeerConnectionMockE2ETest.kt | 60 ++++++++++++++++--- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/livekit-android-sdk/detekt-baseline-release.xml b/livekit-android-sdk/detekt-baseline-release.xml index 3b35e4f99..2c0c7c89f 100644 --- a/livekit-android-sdk/detekt-baseline-release.xml +++ b/livekit-android-sdk/detekt-baseline-release.xml @@ -23,7 +23,7 @@ CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$@CheckResult internal suspend fun sendData(dataPacket: LivekitModels.DataPacket): Result<Unit> CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$@Synchronized @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) fun reconnect() CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$fun onMessage(dataChannel: DataChannel, buffer: DataChannel.Buffer?) - CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$private fun makeRTCConfig( serverResponse: Either<JoinResponse, ReconnectResponse>, connectOptions: ConnectOptions, ): RTCConfiguration + CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$private fun makeRTCConfig( serverResponse: Either<JoinResponse, ReconnectResponse>?, connectOptions: ConnectOptions, ): RTCConfiguration CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$private suspend fun configure(joinResponse: JoinResponse, connectOptions: ConnectOptions) CyclomaticComplexMethod:Room.kt$Room$@Throws(Exception::class) suspend fun connect(url: String, token: String, options: ConnectOptions = ConnectOptions()) CyclomaticComplexMethod:RoomEvent.kt$fun LivekitModels.DisconnectReason?.convert(): DisconnectReason diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt index 1debb049d..1621135f5 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt @@ -45,6 +45,8 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner +import java.util.Base64 +import java.util.zip.GZIPInputStream /** * Connecting with [ConnectOptions.singlePeerConnection] to a server that serves `/rtc/v1`: @@ -58,7 +60,23 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { private fun publisherPeerConnection() = getPublisherPeerConnection() /** - * @return the offers the client has sent to the server, oldest first. + * @return the publisher offer bundled with the `/rtc/v1` join request. + */ + private fun joinOffer(): LivekitRtc.SessionDescription { + val param = wsFactory.request.url.queryParameter(SignalUrlBuilder.QUERY_JOIN_REQUEST) + val wrapped = LivekitRtc.WrappedJoinRequest.parseFrom(Base64.getUrlDecoder().decode(param)) + val joinRequestBytes = when (wrapped.compression) { + LivekitRtc.WrappedJoinRequest.Compression.GZIP -> + GZIPInputStream(wrapped.joinRequest.newInput()).use { it.readBytes() } + + else -> wrapped.joinRequest.toByteArray() + } + return LivekitRtc.JoinRequest.parseFrom(joinRequestBytes).publisherOffer + } + + /** + * @return the offers the client has sent over the signal connection, oldest first. The + * offer bundled with the join request is not one of them. */ private fun sentOffers(): List { return wsFactory.ws.sentRequests @@ -68,17 +86,20 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { } /** - * Answers the client's most recent offer, with [midToTrackId] as the answer's mapping of - * receiving media sections to track IDs. + * Answers the client's most recent offer sent over the signal connection, with + * [midToTrackId] as the answer's mapping of receiving media sections to track IDs. */ private fun TestScope.answerLastOffer(midToTrackId: Map = emptyMap()) { - val offer = sentOffers().last() + answerOffer(sentOffers().last().id, midToTrackId) + } + + private fun TestScope.answerOffer(offerId: Int, midToTrackId: Map = emptyMap()) { val answer = with(LivekitRtc.SignalResponse.newBuilder()) { setAnswer( with(LivekitRtc.SessionDescription.newBuilder()) { type = "answer" sdp = "remote_answer" - id = offer.id + id = offerId putAllMidToTrackId(midToTrackId) build() }, @@ -90,10 +111,9 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { } /** - * Joins through `/rtc/v1`, answers the offer the join triggers, and connects the peer - * connection. + * Joins through `/rtc/v1` and leaves the offer bundled with the join request unanswered. */ - private suspend fun TestScope.connectSinglePeerConnection() { + private suspend fun TestScope.joinSinglePeerConnection() { val connectJob = coroutineRule.scope.launch { room.connect( url = TestData.EXAMPLE_URL, @@ -104,8 +124,16 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { prepareSignal() connectJob.join() advanceUntilIdle() + } - answerLastOffer() + /** + * Joins through `/rtc/v1`, answers the offer bundled with the join request, and connects + * the peer connection. + */ + private suspend fun TestScope.connectSinglePeerConnection() { + joinSinglePeerConnection() + + answerOffer(joinOffer().id) publisherPeerConnection().moveToIceConnectionState(PeerConnection.IceConnectionState.CONNECTED) runCurrent() } @@ -145,6 +173,20 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { assertTrue(events.none { it is RoomEvent.FailedToConnect }) } + @Test + fun publisherOfferRidesInTheJoinRequest() = runTest { + joinSinglePeerConnection() + + val offer = joinOffer() + assertEquals("offer", offer.type) + assertEquals(1, offer.id) + assertTrue("no offer is expected over the signal connection", sentOffers().isEmpty()) + + answerOffer(offer.id) + + assertTrue("the answered join offer must not be offered again", sentOffers().isEmpty()) + } + @Test fun publisherOffersOnJoinWithoutOfferingToReceive() = runTest { connectSinglePeerConnection() From fc1c31052e07117a72c0022fa7c6c98d01bc7e7c Mon Sep 17 00:00:00 2001 From: Sen Chang Date: Fri, 13 Nov 2026 15:36:48 -0800 Subject: [PATCH 4/6] Bundle the unmunged offer when the munge leaves nothing createInitialOffer bundled the munged description as is. The ordinary offer path sends whatever setMungedSdp managed to apply, so an empty munge falls back to the offer libwebrtc created, but the initial offer is sent before anything is applied and had no such step: an empty munge went to the server as an empty publisher offer. createInitialOffer now bundles the unmunged description in that case. The pending offer is the bundled one, so what the server answers is what the answer path sets locally. In the mock E2E suite createOffer returns a placeholder that munges to nothing, so publisherOfferRidesInTheJoinRequest can now also check that the bundled SDP is not empty and becomes the local description once it is answered. --- .../livekit/android/room/PeerConnectionTransport.kt | 10 ++++++---- .../android/room/PeerConnectionTransportTest.kt | 12 ++++++++++++ .../android/room/SinglePeerConnectionMockE2ETest.kt | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt index a3ef66d85..1ae68446a 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt @@ -185,9 +185,10 @@ constructor( * Creates the offer that is bundled with the join request instead of being sent through * [Listener.onOffer]. * - * The offer is created with [constraints] and the usual munging, and takes the next offer id, - * but it is not set as the local description: that would start ICE gathering before the - * server's ICE servers are known. It is kept pending instead, and [setRemoteDescription] + * The offer is created with [constraints] and the usual munging, or left unmunged if the + * munge leaves nothing. It takes the next offer id, but it is not set as the local + * description: that would start ICE gathering before the server's ICE servers are known. + * It is kept pending instead, and [setRemoteDescription] * applies it as the local description just before the answer, whatever the answer's offer id. * While it is pending, a [negotiate] request is queued the same way as when an offer is already * in flight, and runs once the answer is applied. [clearPendingInitialOffer] and [close] drop it. @@ -207,7 +208,8 @@ constructor( is Either.Left -> outcome.value is Either.Right -> return@launchRTCIfNotClosed Either.Right(outcome.value) } - val offer = InitialOffer(SessionDescription(munged.original.type, munged.description), offerId) + val description = munged.description.ifBlank { munged.original.description } + val offer = InitialOffer(SessionDescription(munged.original.type, description), offerId) initialOffer = offer Either.Left(offer) } ?: Either.Right("PCT is closed.") diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt index f933ba0a7..8dc52cf3d 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt @@ -133,6 +133,15 @@ class PeerConnectionTransportTest { ) } + @Test + fun initialOfferIsUnmungedWhenTheMungeLeavesNothing() = runTest { + pc.offerDescription = UNPARSEABLE_OFFER + + val offer = initialOffer() + + assertEquals(UNPARSEABLE_OFFER, offer.sessionDescription.description) + } + @Test fun initialOfferIsNotSetAsLocalDescription() = runTest { initialOffer() @@ -237,5 +246,8 @@ class PeerConnectionTransportTest { /** Comfortably longer than the negotiate debounce. */ private const val OFFER_SETTLE_MS = 200L + + /** Not SDP, so munging it leaves an empty description. */ + private const val UNPARSEABLE_OFFER = "local_offer" } } diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt index 1621135f5..a9005c2dc 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt @@ -180,10 +180,12 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { val offer = joinOffer() assertEquals("offer", offer.type) assertEquals(1, offer.id) + assertTrue(offer.sdp.isNotEmpty()) assertTrue("no offer is expected over the signal connection", sentOffers().isEmpty()) answerOffer(offer.id) + assertEquals(offer.sdp, publisherPeerConnection().localDescription?.description) assertTrue("the answered join offer must not be offered again", sentOffers().isEmpty()) } From 20e2663ebddb580cd70d83522aaac6389733bc6b Mon Sep 17 00:00:00 2001 From: Sen Chang Date: Fri, 13 Nov 2026 15:37:13 -0800 Subject: [PATCH 5/6] Add changeset for offer on join --- .changeset/single-pc-offer-on-join.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/single-pc-offer-on-join.md diff --git a/.changeset/single-pc-offer-on-join.md b/.changeset/single-pc-offer-on-join.md new file mode 100644 index 000000000..f0626bbe0 --- /dev/null +++ b/.changeset/single-pc-offer-on-join.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": patch +--- + +Bundle the publisher offer with the `/rtc/v1` join request when connecting with `ConnectOptions.singlePeerConnection`. The publisher peer connection, its data channels and the first offer are created before the signal connection opens, and the server answers that offer right after the join response, which saves one offer/answer round trip on connect. From a730a7863dd56e5b2f24be0ce915d630ff057a10 Mon Sep 17 00:00:00 2001 From: Sen Chang Date: Fri, 13 Nov 2026 15:57:52 -0800 Subject: [PATCH 6/6] Close the early publisher of a join that ends before its response RTCEngine counts as closed until a join response arrives, and close returns early in that state. The early publisher is created before the response, so a rejected or cancelled first join left it in its field with nothing to dispose it: Room's close after the failure did nothing, and a retry against another region replaced the field and dropped the only reference to the first peer connection. close now disposes an unadopted early publisher even when the engine is already closed, and joinImpl disposes one that an earlier join left behind before creating the next. SinglePeerConnectionFailedJoinMockE2ETest covers both paths. MockPeerConnectionFactory records the peer connections it creates so the tests can reach the early publisher. --- .../java/io/livekit/android/room/RTCEngine.kt | 23 +++- .../org/webrtc/MockPeerConnectionFactory.kt | 9 +- ...nglePeerConnectionFailedJoinMockE2ETest.kt | 100 ++++++++++++++++++ 3 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt index 0c3b1cf92..caf78fcf9 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt @@ -211,8 +211,8 @@ internal constructor( private val subscriberObserver = SubscriberTransportObserver(this, client, rtcThreadToken) // Publisher created ahead of a single peer connection join so its offer rides in the /rtc/v1 - // request. configure adopts it into the transport and clears it; until then closeResources - // disposes it. + // request. configure adopts it into the transport and clears it; until then close disposes + // it, as does the next join. private var earlyPublisher: PeerConnectionTransport? = null private var earlyPublisherObserver: DuplexTransportObserver? = null @@ -308,6 +308,9 @@ internal constructor( } var effectiveOptions = options if (effectiveOptions.singlePeerConnection) { + if (earlyPublisher != null) { + closeEarlyPublisher() + } val observer = DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) val publisher = pctFactory.create( makeRTCConfig(null, effectiveOptions), @@ -547,6 +550,17 @@ internal constructor( fun close(reason: String = "Normal Closure") { if (isClosed) { + // A join that ends before its response leaves the engine closed with the early + // publisher still held. + if (earlyPublisher != null) { + executeBlockingOnRTCThread(rtcThreadToken) { + runBlocking { + configurationLock.withLock { + closeEarlyPublisher() + } + } + } + } return } LKLog.v { "Close - $reason" } @@ -1031,8 +1045,9 @@ internal constructor( /** * Closes the early publisher when one is still unadopted, and the publisher's data channels - * with it. The 404 fallback calls this so the legacy join starts from nothing; closeResources - * calls it when tearing the connection down. + * with it. The 404 fallback calls this so the legacy join starts from nothing, a join calls + * it for the publisher of an earlier join that ended before its response, and close and + * closeResources call it when tearing the connection down. */ private suspend fun closeEarlyPublisher() { earlyPublisher?.close() diff --git a/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt b/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt index 529b13dd2..70497e83c 100644 --- a/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt +++ b/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.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. @@ -23,13 +23,18 @@ import io.livekit.android.test.mock.MockVideoSource import io.livekit.android.test.mock.MockVideoStreamTrack class MockPeerConnectionFactory : PeerConnectionFactory(1L) { + /** + * Every peer connection this factory has created, oldest first. + */ + val peerConnections = mutableListOf() + override fun createPeerConnectionInternal( rtcConfig: PeerConnection.RTCConfiguration, constraints: MediaConstraints?, observer: PeerConnection.Observer?, sslCertificateVerifier: SSLCertificateVerifier?, ): PeerConnection { - return MockPeerConnection(rtcConfig, observer) + return MockPeerConnection(rtcConfig, observer).also { peerConnections.add(it) } } override fun createAudioSource(constraints: MediaConstraints?): AudioSource { diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt new file mode 100644 index 000000000..24224b99d --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt @@ -0,0 +1,100 @@ +/* + * 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 + +import io.livekit.android.ConnectOptions +import io.livekit.android.RoomOptions +import io.livekit.android.test.MockE2ETest +import io.livekit.android.test.mock.MockPeerConnection +import io.livekit.android.test.mock.TestData +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runCurrent +import livekit.org.webrtc.MockPeerConnectionFactory +import livekit.org.webrtc.PeerConnection.IceConnectionState +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import java.io.IOException + +/** + * Connecting with [ConnectOptions.singlePeerConnection] when the `/rtc/v1` join is rejected: the + * publisher created ahead of the join does not outlive it. + */ +@ExperimentalCoroutinesApi +@RunWith(RobolectricTestRunner::class) +class SinglePeerConnectionFailedJoinMockE2ETest : MockE2ETest() { + + private val options = ConnectOptions(singlePeerConnection = true) + + private fun peerConnections(): List { + return (component.peerConnectionFactory() as MockPeerConnectionFactory).peerConnections + } + + /** + * Answers the pending `/rtc/v1` handshake with 403 and waits for [join] to end. + */ + private suspend fun TestScope.rejectJoin(join: Job) { + runCurrent() + val request = wsFactory.request + assertEquals("/rtc/v1", request.url.encodedPath) + wsFactory.listener.onFailure(wsFactory.ws, IOException("403"), createFailedHandshakeResponse(request, 403)) + join.join() + } + + @Test + fun failedJoinClosesTheEarlyPublisher() = runTest { + var failure: Throwable? = null + val connectJob = coroutineRule.scope.launch { + failure = runCatching { room.connect(TestData.EXAMPLE_URL, "token", options) }.exceptionOrNull() + } + + rejectJoin(connectJob) + + assertTrue("expected the join to fail, got $failure", failure is RoomException.ConnectException) + assertEquals(Room.State.DISCONNECTED, room.state) + assertEquals(IceConnectionState.CLOSED, peerConnections().single().iceConnectionState()) + } + + @Test + fun joinClosesTheEarlyPublisherOfAFailedJoin() = runTest { + val engine = component.rtcEngine() + val firstJoin = coroutineRule.scope.launch { + runCatching { engine.join(TestData.EXAMPLE_URL, "token", options, RoomOptions()) } + } + rejectJoin(firstJoin) + val firstPublisher = peerConnections().single() + assertNotEquals(IceConnectionState.CLOSED, firstPublisher.iceConnectionState()) + + val secondJoin = coroutineRule.scope.launch { + runCatching { engine.join(TestData.EXAMPLE_URL, "token", options, RoomOptions()) } + } + runCurrent() + + assertEquals(2, peerConnections().size) + assertEquals(IceConnectionState.CLOSED, firstPublisher.iceConnectionState()) + + rejectJoin(secondJoin) + engine.close() + assertEquals(IceConnectionState.CLOSED, peerConnections().last().iceConnectionState()) + } +}