diff --git a/.changeset/transport-interface.md b/.changeset/transport-interface.md
new file mode 100644
index 000000000..81753e923
--- /dev/null
+++ b/.changeset/transport-interface.md
@@ -0,0 +1,5 @@
+---
+"client-sdk-android": patch
+---
+
+Move RTCEngine's peer connections behind an internal `Transport` interface. `DualPeerConnectionTransport` implements it for the existing publisher and subscriber peer connection pair and owns the decisions that depend on that arrangement: ICE candidate routing, answering server offers, which peer connections a resume waits on, and the sync state descriptions. No behavior changes.
diff --git a/livekit-android-sdk/detekt-baseline-release.xml b/livekit-android-sdk/detekt-baseline-release.xml
index 026aca043..c8efe8b1f 100644
--- a/livekit-android-sdk/detekt-baseline-release.xml
+++ b/livekit-android-sdk/detekt-baseline-release.xml
@@ -109,8 +109,7 @@
UnsafeCallOnNullableType:LocalScreencastVideoTrack.kt$LocalScreencastVideoTrack.MediaProjectionCallback$track!!
UnsafeCallOnNullableType:LocalVideoTrack.kt$LocalVideoTrack$simulcastInfo.sender!!
UnsafeCallOnNullableType:PeerConnectionTransport.kt$PeerConnectionTransport$executeBlockingOnRTCThread(rtcThreadToken) { connectionFactory.createPeerConnection( config, pcObserver, ) ?: throw IllegalStateException("peer connection creation failed?") }!!
- UnsafeCallOnNullableType:RTCEngine.kt$RTCEngine$publisher!!
- UnsafeCallOnNullableType:RTCEngine.kt$RTCEngine$subscriber!!
+ UnsafeCallOnNullableType:RTCEngine.kt$RTCEngine$transport!!
UnsafeCallOnNullableType:RTCEngine.kt$RTCEngine$url!!
UnsafeCallOnNullableType:RTCModule.kt$RTCModule$executeBlockingOnRTCThread(LibWebrtcInitializationThreadToken) { val peerConnectionFactory = PeerConnectionFactory.builder() .setAudioDeviceModule(audioDeviceModule) .setAudioProcessingFactory(audioProcessingFactory) .setVideoEncoderFactory(videoEncoderFactory) .setVideoDecoderFactory(videoDecoderFactory) .apply { if (peerConnectionFactoryOptions != null) { setOptions(peerConnectionFactoryOptions) } } .createPeerConnectionFactory() return@executeBlockingOnRTCThread PeerConnectionFactoryManager(peerConnectionFactory) .apply { memoryManager.registerClosable { executeOnRTCThread(RTCThreadTokenImpl(this)) { dispose() } } } }!!
UnsafeCallOnNullableType:Room.kt$Room$e2eeManager!!
diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/DualPeerConnectionTransport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/DualPeerConnectionTransport.kt
new file mode 100644
index 000000000..187a605fc
--- /dev/null
+++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/DualPeerConnectionTransport.kt
@@ -0,0 +1,156 @@
+/*
+ * 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.room.util.MediaConstraintKeys
+import io.livekit.android.room.util.PeerConnectionStateObservable
+import io.livekit.android.room.util.createAnswer
+import io.livekit.android.room.util.setLocalDescription
+import io.livekit.android.room.util.waitUntilConnected
+import io.livekit.android.util.Either
+import io.livekit.android.util.LKLog
+import io.livekit.android.util.isRight
+import io.livekit.android.util.nullSafe
+import io.livekit.android.webrtc.toProtoSessionDescription
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.launch
+import livekit.LivekitRtc
+import livekit.org.webrtc.IceCandidate
+import livekit.org.webrtc.MediaConstraints
+import livekit.org.webrtc.PeerConnection.RTCConfiguration
+import livekit.org.webrtc.RtpReceiver
+import livekit.org.webrtc.SessionDescription
+
+/**
+ * A [Transport] with separate publishing and receiving peer connections. The server offers
+ * on [subscriber] and the client offers on [publisher].
+ *
+ * @param publisherState connection state of [publisher], as reported by its observer.
+ * @param subscriberState connection state of [subscriber], as reported by its observer.
+ * @param subscriberPrimary the server's `subscriber_primary` flag from the join response.
+ * @param fastPublish the server's `fast_publish` flag from the join response.
+ */
+@Suppress("TooManyFunctions")
+internal class DualPeerConnectionTransport(
+ override val publisher: PeerConnectionTransport,
+ override val subscriber: PeerConnectionTransport,
+ private val publisherState: PeerConnectionStateObservable,
+ private val subscriberState: PeerConnectionStateObservable,
+ private val subscriberPrimary: Boolean,
+ private val fastPublish: Boolean,
+) : Transport {
+ override val publisherConnectsOnDemand: Boolean
+ get() = subscriberPrimary
+ override val publisherOfferConstraints: List = listOf(
+ MediaConstraints.KeyValuePair(MediaConstraintKeys.OFFER_TO_RECV_AUDIO, MediaConstraintKeys.FALSE),
+ MediaConstraints.KeyValuePair(MediaConstraintKeys.OFFER_TO_RECV_VIDEO, MediaConstraintKeys.FALSE),
+ )
+
+ override fun shouldNegotiatePublisherOnJoin(hasPublished: Boolean): Boolean {
+ return !subscriberPrimary || hasPublished || fastPublish
+ }
+
+ override fun addIceCandidate(candidate: IceCandidate, target: LivekitRtc.SignalTarget): Boolean {
+ when (target) {
+ LivekitRtc.SignalTarget.PUBLISHER -> {
+ publisher.addIceCandidate(candidate)
+ return true
+ }
+
+ LivekitRtc.SignalTarget.SUBSCRIBER -> {
+ subscriber.addIceCandidate(candidate)
+ return true
+ }
+ else -> {
+ LKLog.i { "unknown ice candidate target: $target" }
+ return false
+ }
+ }
+ }
+
+ override suspend fun answerServerOffer(offer: SessionDescription, offerId: Int): Either {
+ val remote = subscriber.setRemoteDescription(offer, offerId)
+ if (remote.isRight()) { return remote }
+
+ val answer = subscriber.withPeerConnection { createAnswer(MediaConstraints()) }.nullSafe()
+ if (answer.isRight()) { return answer }
+
+ val outcome = subscriber.withPeerConnection { setLocalDescription(answer.value) }.nullSafe()
+ if (outcome.isRight()) {
+ // This is a failure
+ return outcome
+ } else {
+ // The caller wants the answer on success
+ return answer
+ }
+ }
+
+ override suspend fun applyServerAnswer(
+ answer: SessionDescription,
+ offerId: Int,
+ midToTrackId: Map,
+ ): Either {
+ return publisher.setRemoteDescription(answer, offerId)
+ }
+
+ override fun trackIdForReceiver(receiver: RtpReceiver): String? {
+ return null
+ }
+
+ override suspend fun handleMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement) {
+ // Not needed for dual transport.
+ }
+
+ override fun prepareForIceRestart() {
+ subscriber.prepareForIceRestart()
+ }
+
+ override fun updateRTCConfig(config: RTCConfiguration) {
+ subscriber.updateRTCConfig(config)
+ publisher.updateRTCConfig(config)
+ }
+
+ override suspend fun waitUntilConnected(includePublisher: Boolean) = coroutineScope {
+ if (includePublisher) {
+ launch { publisherState.waitUntilConnected() }
+ }
+ launch { subscriberState.waitUntilConnected() }
+ Unit
+ }
+
+ override suspend fun isConnected(includePublisher: Boolean): Boolean {
+ if (includePublisher) {
+ return publisher.isConnected() && subscriber.isConnected()
+ }
+ return subscriber.isConnected()
+ }
+
+ override suspend fun syncStateDescriptions(): Transport.SyncStateDescriptions {
+ var answer: LivekitRtc.SessionDescription? = null
+ var offer: LivekitRtc.SessionDescription? = null
+ subscriber.withPeerConnection {
+ answer = localDescription?.toProtoSessionDescription()
+ offer = remoteDescription?.toProtoSessionDescription()
+ }
+ return Transport.SyncStateDescriptions(answer, offer)
+ }
+
+ override suspend fun close() {
+ publisher.close()
+ subscriber.close()
+ }
+}
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 888187cbc..a2e9eb042 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
@@ -41,8 +41,6 @@ import io.livekit.android.room.participant.Participant
import io.livekit.android.room.participant.ParticipantTrackPermission
import io.livekit.android.room.track.TrackException
import io.livekit.android.room.util.MediaConstraintKeys
-import io.livekit.android.room.util.createAnswer
-import io.livekit.android.room.util.setLocalDescription
import io.livekit.android.room.util.waitUntilConnected
import io.livekit.android.util.CloseableCoroutineScope
import io.livekit.android.util.Either
@@ -51,6 +49,7 @@ import io.livekit.android.util.LKLog
import io.livekit.android.util.TTLMap
import io.livekit.android.util.flow
import io.livekit.android.util.flowDelegate
+import io.livekit.android.util.isRight
import io.livekit.android.util.nullSafe
import io.livekit.android.util.rethrowIfCancellationSignal
import io.livekit.android.util.withCheckLock
@@ -65,14 +64,12 @@ 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
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
-import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
@@ -211,9 +208,7 @@ internal constructor(
private val publisherObserver = PublisherTransportObserver(this, client, rtcThreadToken)
private val subscriberObserver = SubscriberTransportObserver(this, client, rtcThreadToken)
- internal var publisher: PeerConnectionTransport? = null
- private var subscriber: PeerConnectionTransport? = null
-
+ internal var transport: Transport? = null
private var reliableDataChannel: DataChannel? = null
private var reliableDataChannelSub: DataChannel? = null
private var lossyDataChannel: DataChannel? = null
@@ -289,7 +284,7 @@ internal constructor(
connectStartedAtMs = null
val setupTime = (SystemClock.elapsedRealtime() - startedAtMs).milliseconds
LKLog.i { "connection setup took ${setupTime.inWholeMilliseconds} ms" }
- publisher?.setConnectionSetupTime(setupTime)
+ transport?.publisher?.setConnectionSetupTime(setupTime)
}
suspend fun joinImpl(
@@ -328,12 +323,12 @@ internal constructor(
configure(joinResponse, options)
// The publisher created above needs the attempt's start time before its first offer, in
// case video is published before the primary transport connects.
- publisher?.setConnectStartedAt(startedAtMs)
+ transport?.publisher?.setConnectStartedAt(startedAtMs)
// Subscriber-primary defers the publisher PC until something is published. After a full
// reconnect `hasPublished` is still set, so re-negotiate here — otherwise the ICE wait
// stalls and data-track republish never runs.
- if (!isSubscriberPrimary || joinResponse.fastPublish || hasPublished) {
+ if (transport?.shouldNegotiatePublisherOnJoin(hasPublished) == true) {
negotiatePublisher()
}
client.onReadyForResponses()
@@ -346,7 +341,7 @@ internal constructor(
configurationLock.withCheckLock(
{
ensureActive()
- if (publisher != null && subscriber != null) {
+ if (transport != null) {
// already configured
return@launchBlockingOnRTCThread
}
@@ -360,19 +355,14 @@ internal constructor(
// Setup peer connections
val rtcConfig = makeRTCConfig(Either.Left(joinResponse), connectOptions)
-
- publisher?.close()
- publisher = pctFactory.create(
+ val publisher = pctFactory.create(
rtcConfig,
publisherObserver,
publisherObserver,
)
- subscriber?.close()
- subscriber = pctFactory.create(
- rtcConfig,
- subscriberObserver,
- null,
- )
+ val subscriber = runCatching { pctFactory.create(rtcConfig, subscriberObserver, null) }
+ .onFailure { publisher.close() }
+ .getOrThrow()
val connectionStateListener: PeerConnectionStateListener = { newState ->
LKLog.v { "onIceConnection new state: $newState" }
@@ -406,11 +396,20 @@ internal constructor(
publisherObserver.connectionChangeListener = connectionStateListener
}
+ transport = DualPeerConnectionTransport(
+ publisher,
+ subscriber,
+ publisherObserver,
+ subscriberObserver,
+ joinResponse.subscriberPrimary,
+ joinResponse.fastPublish,
+ )
+
ensureActive()
// data channels
val reliableInit = DataChannel.Init()
reliableInit.ordered = true
- reliableDataChannel = publisher?.withPeerConnection {
+ reliableDataChannel = publisher.withPeerConnection {
createDataChannel(
RELIABLE_DATA_CHANNEL_LABEL,
reliableInit,
@@ -434,7 +433,7 @@ internal constructor(
val lossyInit = DataChannel.Init()
lossyInit.ordered = false
lossyInit.maxRetransmits = 0
- lossyDataChannel = publisher?.withPeerConnection {
+ lossyDataChannel = publisher.withPeerConnection {
createDataChannel(
LOSSY_DATA_CHANNEL_LABEL,
lossyInit,
@@ -495,7 +494,7 @@ internal constructor(
transInit: RtpTransceiverInit,
): SenderTransceiverHandle? {
val sessionState = signalSessionState
- val publisher = publisher ?: return null
+ val publisher = transport?.publisher ?: return null
val transceiver = publisher.withPeerConnection {
addTransceiver(rtcTrack, transInit)
} ?: return null
@@ -554,11 +553,8 @@ internal constructor(
configurationLock.withLock {
publisherObserver.connectionChangeListener = null
subscriberObserver.connectionChangeListener = null
- publisher?.closeBlocking()
- publisher = null
- subscriber?.closeBlocking()
- subscriber = null
-
+ transport?.close()
+ transport = null
reliableBufferedAmountJob?.cancel()
reliableBufferedAmountJob = null
reliableDataChannelManager?.dispose()
@@ -701,14 +697,13 @@ internal constructor(
}
connectionState = ConnectionState.RESUMING
LKLog.v { "Attempting soft reconnect." }
- subscriber?.prepareForIceRestart()
+ transport?.prepareForIceRestart()
try {
val response = client.reconnect(url!!, token, participantSid)
if (response is Either.Left) {
val reconnectResponse = response.value
val rtcConfig = makeRTCConfig(Either.Right(reconnectResponse), connectOptions)
- subscriber?.updateRTCConfig(rtcConfig)
- publisher?.updateRTCConfig(rtcConfig)
+ transport?.updateRTCConfig(rtcConfig)
lastMessageSeq = reconnectResponse.lastMessageSeq
} else {
LKLog.w { "Did not receive reconnect response" }
@@ -739,20 +734,7 @@ internal constructor(
withTimeoutOrNull(MAX_ICE_CONNECT_TIMEOUT_MS.toLong()) {
// wait until publisher ICE connected
- var publisherWaitJob: Job? = null
- if (hasPublished) {
- publisherWaitJob = launch {
- publisherObserver.waitUntilConnected()
- }
- }
-
- // wait until subscriber ICE connected
- val subscriberWaitJob = launch {
- subscriberObserver.waitUntilConnected()
- }
-
- listOfNotNull(publisherWaitJob, subscriberWaitJob)
- .joinAll()
+ transport?.waitUntilConnected(hasPublished)
}
ensureActive()
@@ -761,11 +743,8 @@ internal constructor(
break
}
- val subscriberConnected = subscriber?.isConnected() == true
- val publisherConnected = !hasPublished || publisher?.isConnected() == true
- if ((connectionState == ConnectionState.CONNECTED || connectionState == ConnectionState.RESUMING) &&
- subscriberConnected &&
- publisherConnected
+ val connected = transport?.isConnected(hasPublished) == true
+ if ((connectionState == ConnectionState.CONNECTED || connectionState == ConnectionState.RESUMING) && connected
) {
if (connectionState == ConnectionState.RESUMING) {
connectionState = ConnectionState.CONNECTED
@@ -819,7 +798,7 @@ internal constructor(
coroutineScope.launch {
negotiatePublisherMutex.withLock {
- publisher?.negotiate?.invoke(getPublisherOfferConstraints())
+ transport?.publisher?.negotiate?.invoke(getPublisherOfferConstraints())
}
}
}
@@ -958,12 +937,12 @@ internal constructor(
return
}
- if (publisher == null) {
+ if (transport?.publisher == null) {
throw RoomException.ConnectException("Publisher isn't setup yet! Is the room connected?")
}
- if (publisher?.isConnected() != true &&
- publisher?.iceConnectionState() != PeerConnection.IceConnectionState.CHECKING
+ if (transport?.publisher?.isConnected() != true &&
+ transport?.publisher?.iceConnectionState() != PeerConnection.IceConnectionState.CHECKING
) {
// start negotiation
this.negotiatePublisher()
@@ -981,7 +960,7 @@ internal constructor(
// wait until publisher ICE connected
val endTime = SystemClock.elapsedRealtime() + MAX_ICE_CONNECT_TIMEOUT_MS
while (SystemClock.elapsedRealtime() < endTime) {
- if (publisher?.isConnected() == true &&
+ if (transport?.publisher?.isConnected() == true &&
channelManager.state == DataChannel.State.OPEN
) {
return
@@ -1009,7 +988,7 @@ internal constructor(
// Always mark publish intent so a full reconnect's joinImpl renegotiates even if this
// wait started against a torn-down publisher transport.
if (isSubscriberPrimary) {
- val publisherTransport = publisher
+ val publisherTransport = transport?.publisher
val iceChecking = publisherTransport?.iceConnectionState() ==
PeerConnection.IceConnectionState.CHECKING
if (publisherTransport?.isConnected() != true && !iceChecking) {
@@ -1036,7 +1015,7 @@ internal constructor(
val dataTrackInit = DataChannel.Init()
dataTrackInit.ordered = false
dataTrackInit.maxRetransmits = 0
- publisher?.withPeerConnection {
+ transport?.publisher?.withPeerConnection {
createDataChannel(
DATA_TRACK_DATA_CHANNEL_LABEL,
dataTrackInit,
@@ -1069,18 +1048,7 @@ internal constructor(
private fun getPublisherOfferConstraints(): MediaConstraints {
return MediaConstraints().apply {
with(mandatory) {
- add(
- MediaConstraints.KeyValuePair(
- MediaConstraintKeys.OFFER_TO_RECV_AUDIO,
- MediaConstraintKeys.FALSE,
- ),
- )
- add(
- MediaConstraints.KeyValuePair(
- MediaConstraintKeys.OFFER_TO_RECV_VIDEO,
- MediaConstraintKeys.FALSE,
- ),
- )
+ transport?.publisherOfferConstraints?.let { addAll(it) }
if (connectionState == ConnectionState.RECONNECTING || connectionState == ConnectionState.RESUMING) {
add(
MediaConstraints.KeyValuePair(
@@ -1254,10 +1222,10 @@ internal constructor(
// ---------------------------------- SignalClient.Listener --------------------------------------//
- override fun onServerAnswer(sessionDescription: SessionDescription, offerId: Int) {
- LKLog.v { "received server answer: ${sessionDescription.type}, ${runBlocking { publisher?.signalingState() }}" }
+ override fun onServerAnswer(sessionDescription: SessionDescription, offerId: Int, midToTrackId: Map) {
+ LKLog.v { "received server answer: ${sessionDescription.type}, ${runBlocking { transport?.publisher?.signalingState() }}" }
coroutineScope.launch {
- when (val outcome = publisher?.setRemoteDescription(sessionDescription, offerId).nullSafe()) {
+ when (val outcome = transport?.applyServerAnswer(sessionDescription, offerId, midToTrackId).nullSafe()) {
is Either.Left -> {
listener?.onRequestSubscribedCodecRefresh()
}
@@ -1270,68 +1238,24 @@ internal constructor(
}
override fun onServerOffer(sessionDescription: SessionDescription, offerId: Int) {
- LKLog.v { "received server offer: ${sessionDescription.type}, ${runBlocking { publisher?.signalingState() }}" }
+ LKLog.v { "received server offer: ${sessionDescription.type}, ${runBlocking { transport?.publisher?.signalingState() }}" }
coroutineScope.launch {
- run {
- when (val outcome = subscriber?.setRemoteDescription(sessionDescription, offerId).nullSafe()) {
- is Either.Right -> {
- LKLog.e { "error setting remote description for offer: ${outcome.value} " }
- return@launch
- }
-
- else -> {}
- }
- }
-
- if (isClosed) {
- return@launch
- }
-
- val answer = run {
- when (val outcome = subscriber?.withPeerConnection { createAnswer(MediaConstraints()) }.nullSafe()) {
- is Either.Left -> outcome.value
- is Either.Right -> {
- LKLog.e { "error creating answer: ${outcome.value}" }
- return@launch
- }
- }
- }
-
- if (isClosed) {
+ val answer = transport?.answerServerOffer(sessionDescription, offerId).nullSafe()
+ if (answer.isRight()) {
+ LKLog.e { "Failed to process server offer, ${answer.value}" }
return@launch
}
-
- run {
- when (val outcome = subscriber?.withPeerConnection { setLocalDescription(answer) }.nullSafe()) {
- is Either.Left -> Unit
- is Either.Right -> {
- LKLog.e { "error setting local description for answer: ${outcome.value}" }
- return@launch
- }
- }
- }
-
if (isClosed) {
return@launch
}
- client.sendAnswer(answer, offerId)
+ client.sendAnswer(answer.value, offerId)
}
}
override fun onTrickle(candidate: IceCandidate, target: LivekitRtc.SignalTarget) {
LKLog.v { "received ice candidate from peer: $candidate, $target" }
- when (target) {
- LivekitRtc.SignalTarget.PUBLISHER -> {
- publisher?.addIceCandidate(candidate)
- ?: LKLog.w { "received candidate for publisher when we don't have one. ignoring." }
- }
-
- LivekitRtc.SignalTarget.SUBSCRIBER -> {
- subscriber?.addIceCandidate(candidate)
- ?: LKLog.w { "received candidate for subscriber when we don't have one. ignoring." }
- }
-
- else -> LKLog.i { "unknown ice candidate target?" }
+ if (transport?.addIceCandidate(candidate, target) != true) {
+ LKLog.w { "received candidate for $target when we don't have one. ignoring." }
}
}
@@ -1474,6 +1398,12 @@ internal constructor(
incomingDataTrackManager.handleSubscriberHandles(encoded)
}
+ override fun onMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement) {
+ coroutineScope.launch {
+ transport?.handleMediaSectionsRequirement(requirement)
+ }
+ }
+
/**
* Forwards an encoded [LivekitRtc.SignalRequest] produced by a UniFFI data track manager.
*/
@@ -1624,15 +1554,6 @@ internal constructor(
subscription: LivekitRtc.UpdateSubscription,
publishedTracks: List,
) {
- var answer: LivekitRtc.SessionDescription? = null
- var offer: LivekitRtc.SessionDescription? = null
- runBlocking {
- subscriber?.withPeerConnection {
- answer = localDescription?.toProtoSessionDescription()
- offer = remoteDescription?.toProtoSessionDescription()
- }
- }
-
val dataChannelInfos = LivekitModels.DataPacket.Kind.entries
.filterNot { it == LivekitModels.DataPacket.Kind.UNRECOGNIZED }
.mapNotNull { kind -> dataChannelForKind(kind) }
@@ -1663,12 +1584,14 @@ internal constructor(
}
}
+ val syncStateDescription = transport?.syncStateDescriptions()
+
val syncState = with(LivekitRtc.SyncState.newBuilder()) {
- if (answer != null) {
- setAnswer(answer)
+ if (syncStateDescription?.answer != null) {
+ setAnswer(syncStateDescription.answer)
}
- if (offer != null) {
- setOffer(offer)
+ if (syncStateDescription?.offer != null) {
+ setOffer(syncStateDescription.offer)
}
setSubscription(subscription)
addAllPublishTracks(publishedTracks)
@@ -1683,20 +1606,20 @@ internal constructor(
fun getPublisherRTCStats(callback: RTCStatsCollectorCallback) {
runBlocking {
- publisher?.withPeerConnection { getStats(callback) }
+ transport?.publisher?.withPeerConnection { getStats(callback) }
?: callback.onStatsDelivered(RTCStatsReport(0, emptyMap()))
}
}
fun getSubscriberRTCStats(callback: RTCStatsCollectorCallback) {
runBlocking {
- subscriber?.withPeerConnection { getStats(callback) }
+ transport?.subscriber?.withPeerConnection { getStats(callback) }
?: callback.onStatsDelivered(RTCStatsReport(0, emptyMap()))
}
}
fun createStatsGetter(sender: RtpSender): RTCStatsGetter {
- val p = publisher
+ val p = transport?.publisher
return { statsCallback: RTCStatsCollectorCallback ->
runBlocking {
p?.withPeerConnection {
@@ -1706,8 +1629,16 @@ internal constructor(
}
}
+ /**
+ * @return the track SID the server mapped to the media section that [receiver] receives on,
+ * or null if the media section has no mapping.
+ */
+ internal fun getTrackSidForReceiver(receiver: RtpReceiver): String? {
+ return transport?.trackIdForReceiver(receiver)
+ }
+
fun createStatsGetter(receiver: RtpReceiver): RTCStatsGetter {
- val p = subscriber
+ val p = transport?.subscriber
return { statsCallback: RTCStatsCollectorCallback ->
runBlocking {
p?.withPeerConnection {
@@ -1718,12 +1649,12 @@ internal constructor(
}
internal fun registerTrackBitrateInfo(cid: String, trackBitrateInfo: TrackBitrateInfo) {
- publisher?.registerTrackBitrateInfo(cid, trackBitrateInfo)
+ transport?.publisher?.registerTrackBitrateInfo(cid, trackBitrateInfo)
}
internal fun removeTrack(rtcTrack: MediaStreamTrack) {
runBlocking {
- publisher?.withPeerConnection {
+ transport?.publisher?.withPeerConnection {
val senders = this.senders
for (sender in senders) {
val t = sender.track() ?: continue
@@ -1740,7 +1671,7 @@ internal constructor(
return
}
runBlocking {
- publisher?.withPeerConnection {
+ transport?.publisher?.withPeerConnection {
for (transceiver in transceivers) {
if (!transceiver.isStopped) {
transceiver.stopInternal()
@@ -1771,7 +1702,7 @@ internal constructor(
return runBlocking {
publisher.withPeerConnection {
val sessionState = signalSessionState
- val publisherIsIdle = this@RTCEngine.publisher === publisher &&
+ val publisherIsIdle = this@RTCEngine.transport?.publisher === publisher &&
connectionState == ConnectionState.CONNECTED &&
reconnectingJob?.isActive != true &&
senderTransceiver.signalSessionState === sessionState &&
@@ -1791,11 +1722,11 @@ internal constructor(
@VisibleForTesting
fun getPublisherPeerConnection() =
- publisher!!.peerConnection
+ transport!!.publisher.peerConnection
@VisibleForTesting
fun getSubscriberPeerConnection() =
- subscriber!!.peerConnection
+ transport!!.subscriber.peerConnection
}
/**
diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt
index ebb9de898..b4642f829 100644
--- a/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt
+++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt
@@ -1297,6 +1297,7 @@ constructor(
if (streamId != null && streamId.startsWith("TR")) {
trackSid = streamId
}
+ engine.getTrackSidForReceiver(receiver)?.let { trackSid = it }
val participant = getParticipantBySid(participantSid) as? RemoteParticipant
if (participant == null) {
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 ddf99c3a1..0cad99b45 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
@@ -865,7 +865,8 @@ constructor(
LivekitRtc.SignalResponse.MessageCase.ANSWER -> {
val sd = fromProtoSessionDescription(response.answer)
val offerId = response.answer.id
- listener?.onServerAnswer(sd, offerId)
+ val midToTrackId = response.answer.midToTrackIdMap
+ listener?.onServerAnswer(sd, offerId, midToTrackId)
}
LivekitRtc.SignalResponse.MessageCase.OFFER -> {
@@ -986,7 +987,7 @@ constructor(
}
LivekitRtc.SignalResponse.MessageCase.MEDIA_SECTIONS_REQUIREMENT -> {
- // TODO
+ listener?.onMediaSectionsRequirement(response.mediaSectionsRequirement)
}
LivekitRtc.SignalResponse.MessageCase.SUBSCRIBED_AUDIO_CODEC_UPDATE -> {
@@ -1101,7 +1102,7 @@ constructor(
}
internal interface Listener {
- fun onServerAnswer(sessionDescription: SessionDescription, offerId: Int)
+ fun onServerAnswer(sessionDescription: SessionDescription, offerId: Int, midToTrackId: Map)
fun onServerOffer(sessionDescription: SessionDescription, offerId: Int)
fun onTrickle(candidate: IceCandidate, target: LivekitRtc.SignalTarget)
fun onLocalTrackPublished(response: LivekitRtc.TrackPublishedResponse)
@@ -1124,6 +1125,7 @@ constructor(
fun onUnpublishDataTrackResponse(encoded: ByteArray) {}
fun onRequestResponse(encoded: ByteArray) {}
fun onDataTrackSubscriberHandles(encoded: ByteArray) {}
+ fun onMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement) {}
}
/**
diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/Transport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/Transport.kt
new file mode 100644
index 000000000..4c280038d
--- /dev/null
+++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/Transport.kt
@@ -0,0 +1,147 @@
+/*
+ * 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.util.Either
+import livekit.LivekitRtc
+import livekit.org.webrtc.IceCandidate
+import livekit.org.webrtc.MediaConstraints
+import livekit.org.webrtc.PeerConnection.RTCConfiguration
+import livekit.org.webrtc.RtpReceiver
+import livekit.org.webrtc.SessionDescription
+
+/**
+ * The peer connections [RTCEngine] uses for one join, and the decisions that depend on how
+ * they are arranged.
+ *
+ * Operations that only ever touch the publishing peer connection, such as adding transceivers
+ * or creating client data channels, go through [publisher] directly.
+ */
+@Suppress("TooManyFunctions")
+internal interface Transport {
+
+ /**
+ * Publishes local tracks and owns the client-created data channels.
+ */
+ val publisher: PeerConnectionTransport
+
+ /**
+ * Receives remote tracks and server-opened data channels.
+ */
+ val subscriber: PeerConnectionTransport
+
+ /**
+ * Whether the publisher connects only once something is published or sent, rather than
+ * as part of joining.
+ */
+ val publisherConnectsOnDemand: Boolean
+
+ /**
+ * Mandatory constraints for every offer from [publisher], such as declining to receive when
+ * remote media arrives on another peer connection.
+ */
+ val publisherOfferConstraints: List
+
+ /**
+ * Whether the publisher has to be negotiated as part of joining, rather than on demand.
+ *
+ * @param hasPublished whether the publisher was in use before this join, such as before a
+ * full reconnect.
+ */
+ fun shouldNegotiatePublisherOnJoin(hasPublished: Boolean): Boolean
+
+ /**
+ * Adds a trickled remote ICE candidate to the peer connection for [target].
+ *
+ * @return false if [target] does not resolve to a peer connection.
+ */
+ fun addIceCandidate(candidate: IceCandidate, target: LivekitRtc.SignalTarget): Boolean
+
+ /**
+ * Applies a server offer to the receiving peer connection and creates the answer to send back.
+ *
+ * @return the answer, which has been set as the local description, or an error message if
+ * any step failed or the peer connection is closed.
+ */
+ suspend fun answerServerOffer(offer: SessionDescription, offerId: Int): Either
+
+ /**
+ * Applies the server's answer to an offer from [publisher].
+ *
+ * @param midToTrackId the answer's `mid_to_track_id`, which maps the mids of media sections
+ * that receive remote tracks to their track IDs. Read it back with [trackIdForReceiver].
+ * @return an error message if the answer could not be applied or the peer connection is closed.
+ */
+ suspend fun applyServerAnswer(
+ answer: SessionDescription,
+ offerId: Int,
+ midToTrackId: Map,
+ ): Either
+
+ /**
+ * @return the track ID the server mapped to the media section [receiver] receives on, or null
+ * if there is no mapping and the track ID has to come from the track's stream instead.
+ */
+ fun trackIdForReceiver(receiver: RtpReceiver): String?
+
+ /**
+ * Ensures there are at least as many receive-only media sections as the server requires
+ * to deliver remote tracks.
+ */
+ suspend fun handleMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement)
+
+ /**
+ * Prepares for a resume, which restarts ICE.
+ */
+ fun prepareForIceRestart()
+
+ /**
+ * Applies [config] to all peer connections.
+ */
+ fun updateRTCConfig(config: RTCConfiguration)
+
+ /**
+ * Suspends until the peer connections a resume depends on are connected.
+ *
+ * @param includePublisher whether to also wait for the publisher.
+ */
+ suspend fun waitUntilConnected(includePublisher: Boolean)
+
+ /**
+ * @param includePublisher whether the publisher must also be connected.
+ * @return whether the peer connections a resume depends on are connected.
+ */
+ suspend fun isConnected(includePublisher: Boolean): Boolean
+
+ /**
+ * @return the previous answer and offer to report in a [LivekitRtc.SyncState] on resume.
+ */
+ suspend fun syncStateDescriptions(): SyncStateDescriptions
+
+ /**
+ * Closes all peer connections.
+ */
+ suspend fun close()
+
+ /**
+ * The session descriptions reported in a [LivekitRtc.SyncState].
+ */
+ data class SyncStateDescriptions(
+ val answer: LivekitRtc.SessionDescription?,
+ val offer: LivekitRtc.SessionDescription?,
+ )
+}
diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt
index 158309819..714f28d23 100644
--- a/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt
+++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/participant/LocalParticipant.kt
@@ -716,7 +716,7 @@ internal constructor(
var senderTransceiver: SenderTransceiverHandle? = null
var previousTrackTransceiver: RtpTransceiver? = null
suspend fun negotiate() {
- if (this.engine.publisher == null) {
+ if (this.engine.transport?.publisher == null) {
throw IllegalStateException("publisher is not configured yet!")
}
diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/util/Either.kt b/livekit-android-sdk/src/main/java/io/livekit/android/util/Either.kt
index 5884eb9f3..95b72381d 100644
--- a/livekit-android-sdk/src/main/java/io/livekit/android/util/Either.kt
+++ b/livekit-android-sdk/src/main/java/io/livekit/android/util/Either.kt
@@ -1,5 +1,5 @@
/*
- * Copyright 2023-2024 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.
@@ -16,6 +16,9 @@
package io.livekit.android.util
+import kotlin.contracts.ExperimentalContracts
+import kotlin.contracts.contract
+
/**
* @suppress
*/
@@ -27,3 +30,31 @@ sealed class Either {
internal fun Either?.nullSafe(): Either {
return this ?: Either.Right("null")
}
+
+/**
+ * Whether this is an [Either.Left]. A true result smart-casts this to [Either.Left],
+ * and a false result to [Either.Right].
+ */
+@OptIn(ExperimentalContracts::class)
+internal fun Either.isLeft(): Boolean {
+ contract {
+ returns(true) implies (this@isLeft is Either.Left)
+ returns(false) implies (this@isLeft is Either.Right)
+ }
+ return this is Either.Left
+}
+
+/**
+ * Whether this is an [Either.Right]. A true result smart-casts this to [Either.Right],
+ * and a false result to [Either.Left].
+ */
+@OptIn(ExperimentalContracts::class)
+internal fun Either.isRight(): Boolean {
+ // Kotlin 1.9 inverts this contract when reading it from compiled code unless the
+ // Either.Left clause comes first. EitherTest checks the narrowing from another module.
+ contract {
+ returns(false) implies (this@isRight is Either.Left)
+ returns(true) implies (this@isRight is Either.Right)
+ }
+ return this is Either.Right
+}
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 6a51cfebe..b7375db1e 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
@@ -108,7 +108,13 @@ class MockPeerConnection(
return dataChannel
}
+ /**
+ * The constraints passed to the most recent [createOffer].
+ */
+ var lastOfferConstraints: MediaConstraints? = null
+
override fun createOffer(observer: SdpObserver?, constraints: MediaConstraints?) {
+ lastOfferConstraints = constraints
val sdp = SessionDescription(SessionDescription.Type.OFFER, "local_offer")
observer?.onCreateSuccess(sdp)
}
@@ -188,15 +194,19 @@ class MockPeerConnection(
return transceiver
}
- override fun addTransceiver(mediaType: MediaStreamTrack.MediaType?): RtpTransceiver {
- return super.addTransceiver(mediaType)
+ override fun addTransceiver(mediaType: MediaStreamTrack.MediaType): RtpTransceiver {
+ val transceiver = MockRtpTransceiver.create(mediaType, RtpTransceiver.RtpTransceiverInit())
+ transceivers.add(transceiver)
+ return transceiver
}
override fun addTransceiver(
- mediaType: MediaStreamTrack.MediaType?,
+ mediaType: MediaStreamTrack.MediaType,
init: RtpTransceiver.RtpTransceiverInit?,
): RtpTransceiver {
- return super.addTransceiver(mediaType, init)
+ val transceiver = MockRtpTransceiver.create(mediaType, init ?: RtpTransceiver.RtpTransceiverInit())
+ transceivers.add(transceiver)
+ return transceiver
}
@Deprecated("Deprecated in Java")
diff --git a/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpTransceiver.kt b/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpTransceiver.kt
index d48991142..bafa82407 100644
--- a/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpTransceiver.kt
+++ b/livekit-android-test/src/main/java/livekit/org/webrtc/MockRtpTransceiver.kt
@@ -26,18 +26,25 @@ object MockRtpTransceiver {
fun create(
track: MediaStreamTrack,
init: RtpTransceiver.RtpTransceiverInit = RtpTransceiver.RtpTransceiverInit(),
+ ): RtpTransceiver {
+ val mediaType = when (track.kind()) {
+ MediaStreamTrack.AUDIO_TRACK_KIND -> MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO
+ MediaStreamTrack.VIDEO_TRACK_KIND -> MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO
+ else -> throw IllegalStateException("illegal kind: ${track.kind()}")
+ }
+ return create(mediaType, init)
+ }
+
+ fun create(
+ mediaType: MediaStreamTrack.MediaType,
+ init: RtpTransceiver.RtpTransceiverInit = RtpTransceiver.RtpTransceiverInit(),
): RtpTransceiver {
val mock = Mockito.mock(RtpTransceiver::class.java)
val id = UUID.randomUUID().toString()
- Mockito.`when`(mock.mediaType).then {
- return@then when (track.kind()) {
- MediaStreamTrack.AUDIO_TRACK_KIND -> MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO
- MediaStreamTrack.VIDEO_TRACK_KIND -> MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO
- else -> throw IllegalStateException("illegal kind: ${track.kind()}")
- }
- }
+ Mockito.`when`(mock.mediaType).then { mediaType }
val direction = RtpTransceiverDirection.fromNativeIndex(init.directionNativeIndex)
+ Mockito.`when`(mock.direction).then { direction }
when (direction) {
RtpTransceiverDirection.SEND_RECV, RtpTransceiverDirection.SEND_ONLY -> {
diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/DualPeerConnectionTransportTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/DualPeerConnectionTransportTest.kt
new file mode 100644
index 000000000..515379f92
--- /dev/null
+++ b/livekit-android-test/src/test/java/io/livekit/android/room/DualPeerConnectionTransportTest.kt
@@ -0,0 +1,503 @@
+/*
+ * 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.PeerConnectionStateObservable
+import io.livekit.android.test.mock.MockPeerConnection
+import io.livekit.android.test.mock.MockRTCThreadToken
+import io.livekit.android.util.Either
+import io.livekit.android.util.FlowObservable
+import io.livekit.android.util.flowDelegate
+import io.livekit.android.webrtc.toProtoSessionDescription
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.test.runCurrent
+import kotlinx.coroutines.test.runTest
+import livekit.LivekitRtc.MediaSectionsRequirement
+import livekit.LivekitRtc.SignalTarget
+import livekit.org.webrtc.IceCandidate
+import livekit.org.webrtc.MediaStreamTrack
+import livekit.org.webrtc.MockPeerConnectionFactory
+import livekit.org.webrtc.PeerConnection
+import livekit.org.webrtc.PeerConnection.IceConnectionState
+import livekit.org.webrtc.PeerConnection.PeerConnectionState
+import livekit.org.webrtc.PeerConnection.RTCConfiguration
+import livekit.org.webrtc.RtpTransceiver
+import livekit.org.webrtc.SessionDescription
+import livekit.org.webrtc.WebRTCInitializer
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertSame
+import org.junit.Assert.assertTrue
+import org.junit.Before
+import org.junit.Test
+import org.mockito.kotlin.any
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.never
+import org.mockito.kotlin.spy
+import org.mockito.kotlin.verify
+import org.mockito.kotlin.verifyBlocking
+import org.mockito.kotlin.whenever
+
+@ExperimentalCoroutinesApi
+class DualPeerConnectionTransportTest {
+
+ private class FakeConnectionState : PeerConnectionStateObservable {
+ @FlowObservable
+ @get:FlowObservable
+ override var connectionState by flowDelegate(PeerConnectionState.NEW)
+ }
+
+ private lateinit var publisher: PeerConnectionTransport
+ private lateinit var subscriber: PeerConnectionTransport
+ private val publisherState = FakeConnectionState()
+ private val subscriberState = FakeConnectionState()
+
+ private val publisherPc: MockPeerConnection
+ get() = publisher.peerConnection as MockPeerConnection
+ private val subscriberPc: MockPeerConnection
+ get() = subscriber.peerConnection as MockPeerConnection
+
+ private val candidate = IceCandidate("0", 0, "candidate:1 1 udp 2122260223 192.168.1.2 50000 typ host")
+
+ @Before
+ fun setup() {
+ WebRTCInitializer.initialize()
+ publisher = createPeerConnectionTransport()
+ subscriber = createPeerConnectionTransport()
+ }
+
+ private fun createPeerConnectionTransport(): PeerConnectionTransport = spy(
+ PeerConnectionTransport(
+ config = RTCConfiguration(emptyList()),
+ pcObserver = mock(),
+ listener = null,
+ ioDispatcher = Dispatchers.Unconfined,
+ connectionFactory = MockPeerConnectionFactory(),
+ sdpFactory = SdpFactory.getInstance(),
+ rtcThreadToken = MockRTCThreadToken(),
+ ),
+ )
+
+ private fun transport(
+ subscriberPrimary: Boolean = true,
+ fastPublish: Boolean = false,
+ ) = DualPeerConnectionTransport(
+ publisher = publisher,
+ subscriber = subscriber,
+ publisherState = publisherState,
+ subscriberState = subscriberState,
+ subscriberPrimary = subscriberPrimary,
+ fastPublish = fastPublish,
+ )
+
+ // region roles
+
+ @Test
+ fun publisherIsPublishingPeerConnection() {
+ assertSame(publisher, transport().publisher)
+ }
+
+ @Test
+ fun subscriberIsReceivingPeerConnection() {
+ assertSame(subscriber, transport().subscriber)
+ }
+
+ @Test
+ fun publisherConnectsOnDemandWhenSubscriberPrimary() {
+ assertTrue(transport(subscriberPrimary = true).publisherConnectsOnDemand)
+ }
+
+ @Test
+ fun publisherConnectsOnJoinWhenPublisherPrimary() {
+ assertFalse(transport(subscriberPrimary = false).publisherConnectsOnDemand)
+ }
+
+ @Test
+ fun publisherOffersDeclineToReceive() {
+ val declineToReceive = mapOf(
+ MediaConstraintKeys.OFFER_TO_RECV_AUDIO to MediaConstraintKeys.FALSE,
+ MediaConstraintKeys.OFFER_TO_RECV_VIDEO to MediaConstraintKeys.FALSE,
+ )
+
+ for (subscriberPrimary in listOf(true, false)) {
+ val constraints = transport(subscriberPrimary = subscriberPrimary).publisherOfferConstraints
+ assertEquals(declineToReceive, constraints.associate { it.key to it.value })
+ }
+ }
+
+ // endregion
+
+ // region shouldNegotiatePublisherOnJoin
+
+ @Test
+ fun subscriberPrimaryDefersPublisher() {
+ assertFalse(transport(subscriberPrimary = true).shouldNegotiatePublisherOnJoin(hasPublished = false))
+ }
+
+ @Test
+ fun publisherPrimaryNegotiatesPublisherOnJoin() {
+ assertTrue(transport(subscriberPrimary = false).shouldNegotiatePublisherOnJoin(hasPublished = false))
+ }
+
+ @Test
+ fun fastPublishNegotiatesPublisherOnJoin() {
+ assertTrue(
+ transport(subscriberPrimary = true, fastPublish = true)
+ .shouldNegotiatePublisherOnJoin(hasPublished = false),
+ )
+ }
+
+ @Test
+ fun hasPublishedNegotiatesPublisherOnJoin() {
+ assertTrue(transport(subscriberPrimary = true).shouldNegotiatePublisherOnJoin(hasPublished = true))
+ }
+
+ @Test
+ fun shouldNegotiatePublisherOnJoinDoesNotNegotiate() {
+ val transport = transport(subscriberPrimary = false)
+
+ transport.shouldNegotiatePublisherOnJoin(hasPublished = true)
+
+ verify(publisher, never()).negotiate
+ verify(subscriber, never()).negotiate
+ }
+
+ // endregion
+
+ // region addIceCandidate
+
+ @Test
+ fun publisherCandidateGoesToPublisher() {
+ assertTrue(transport().addIceCandidate(candidate, SignalTarget.PUBLISHER))
+
+ verify(publisher).addIceCandidate(candidate)
+ verify(subscriber, never()).addIceCandidate(any())
+ }
+
+ @Test
+ fun subscriberCandidateGoesToSubscriber() {
+ assertTrue(transport().addIceCandidate(candidate, SignalTarget.SUBSCRIBER))
+
+ verify(subscriber).addIceCandidate(candidate)
+ verify(publisher, never()).addIceCandidate(any())
+ }
+
+ @Test
+ fun unrecognizedTargetCandidateIsRejected() {
+ assertFalse(transport().addIceCandidate(candidate, SignalTarget.UNRECOGNIZED))
+
+ verify(publisher, never()).addIceCandidate(any())
+ verify(subscriber, never()).addIceCandidate(any())
+ }
+
+ // endregion
+
+ // region answerServerOffer
+
+ @Test
+ fun answerServerOfferAnswersOnSubscriber() = runTest {
+ val offer = SessionDescription(SessionDescription.Type.OFFER, "remote_offer")
+
+ val result = transport().answerServerOffer(offer, offerId = 7)
+
+ assertTrue("$result", result is Either.Left)
+ val answer = (result as Either.Left).value
+ assertEquals(SessionDescription.Type.ANSWER, answer.type)
+ assertSame(offer, subscriberPc.remoteDesc)
+ assertSame(answer, subscriberPc.localDesc)
+ }
+
+ @Test
+ fun answerServerOfferPassesOfferId() = runTest {
+ val offer = SessionDescription(SessionDescription.Type.OFFER, "remote_offer")
+
+ transport().answerServerOffer(offer, offerId = 7)
+
+ verifyBlocking(subscriber) { setRemoteDescription(offer, 7) }
+ }
+
+ @Test
+ fun answerServerOfferLeavesPublisherAlone() = runTest {
+ val offer = SessionDescription(SessionDescription.Type.OFFER, "remote_offer")
+
+ transport().answerServerOffer(offer, offerId = 7)
+
+ assertNull(publisherPc.remoteDesc)
+ assertNull(publisherPc.localDesc)
+ }
+
+ @Test
+ fun answerServerOfferFailsOnInvalidOffer() = runTest {
+ val offer = SessionDescription(SessionDescription.Type.OFFER, "")
+
+ val result = transport().answerServerOffer(offer, offerId = 7)
+
+ assertTrue("$result", result is Either.Right)
+ assertNull(subscriberPc.localDesc)
+ }
+
+ @Test
+ fun answerServerOfferFailsWhenClosed() = runTest {
+ subscriber.close()
+ val offer = SessionDescription(SessionDescription.Type.OFFER, "remote_offer")
+
+ val result = transport().answerServerOffer(offer, offerId = 7)
+
+ assertTrue("$result", result is Either.Right)
+ }
+
+ // endregion
+
+ // region applyServerAnswer
+
+ @Test
+ fun applyServerAnswerAppliesToPublisher() = runTest {
+ val answer = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer")
+
+ val result = transport().applyServerAnswer(answer, offerId = 7, midToTrackId = emptyMap())
+
+ assertTrue("$result", result is Either.Left)
+ assertSame(answer, publisherPc.remoteDesc)
+ }
+
+ @Test
+ fun applyServerAnswerPassesOfferId() = runTest {
+ val answer = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer")
+
+ transport().applyServerAnswer(answer, offerId = 7, midToTrackId = emptyMap())
+
+ verifyBlocking(publisher) { setRemoteDescription(answer, 7) }
+ }
+
+ @Test
+ fun applyServerAnswerLeavesSubscriberAlone() = runTest {
+ val answer = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer")
+
+ transport().applyServerAnswer(answer, offerId = 7, midToTrackId = emptyMap())
+
+ assertNull(subscriberPc.remoteDesc)
+ assertNull(subscriberPc.localDesc)
+ }
+
+ @Test
+ fun applyServerAnswerFailsWhenClosed() = runTest {
+ publisher.close()
+ val answer = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer")
+
+ val result = transport().applyServerAnswer(answer, offerId = 7, midToTrackId = emptyMap())
+
+ assertTrue("$result", result is Either.Right)
+ }
+
+ // endregion
+
+ // region trackIdForReceiver
+
+ @Test
+ fun trackIdForReceiverIgnoresAnswerMapping() = runTest {
+ val transport = transport()
+ val transceiver = subscriberPc.addTransceiver(
+ MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO,
+ RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.RECV_ONLY),
+ )
+ whenever(transceiver.mid).thenReturn("0")
+ val answer = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer")
+
+ transport.applyServerAnswer(answer, offerId = 7, midToTrackId = mapOf("0" to "TR_remote"))
+
+ assertNull(transport.trackIdForReceiver(transceiver.receiver))
+ }
+
+ // endregion
+
+ // region handleMediaSectionsRequirement
+
+ private val mediaSectionsRequirement = MediaSectionsRequirement.newBuilder()
+ .setNumAudios(2)
+ .setNumVideos(3)
+ .build()
+
+ @Test
+ fun handleMediaSectionsRequirementAddsNoTransceivers() = runTest {
+ transport().handleMediaSectionsRequirement(mediaSectionsRequirement)
+
+ assertTrue(publisherPc.transceivers.isEmpty())
+ assertTrue(subscriberPc.transceivers.isEmpty())
+ }
+
+ @Test
+ fun handleMediaSectionsRequirementDoesNotNegotiate() = runTest {
+ transport().handleMediaSectionsRequirement(mediaSectionsRequirement)
+
+ verify(publisher, never()).negotiate
+ assertNull(publisherPc.localDesc)
+ }
+
+ // endregion
+
+ // region resume
+
+ @Test
+ fun prepareForIceRestartPreparesSubscriber() {
+ transport().prepareForIceRestart()
+
+ verify(subscriber).prepareForIceRestart()
+ verify(publisher, never()).prepareForIceRestart()
+ }
+
+ @Test
+ fun updateRTCConfigAppliesToBoth() {
+ val config = RTCConfiguration(emptyList())
+
+ transport().updateRTCConfig(config)
+
+ assertSame(config, publisherPc.rtcConfig)
+ assertSame(config, subscriberPc.rtcConfig)
+ }
+
+ @Test
+ fun isConnectedRequiresSubscriber() = runTest {
+ publisherPc.moveToIceConnectionState(IceConnectionState.CONNECTED)
+
+ assertFalse(transport().isConnected(includePublisher = false))
+ assertFalse(transport().isConnected(includePublisher = true))
+ }
+
+ @Test
+ fun isConnectedIgnoresPublisherWhenNotIncluded() = runTest {
+ subscriberPc.moveToIceConnectionState(IceConnectionState.CONNECTED)
+
+ assertTrue(transport().isConnected(includePublisher = false))
+ }
+
+ @Test
+ fun isConnectedRequiresPublisherWhenIncluded() = runTest {
+ subscriberPc.moveToIceConnectionState(IceConnectionState.CONNECTED)
+
+ assertFalse(transport().isConnected(includePublisher = true))
+
+ publisherPc.moveToIceConnectionState(IceConnectionState.CONNECTED)
+
+ assertTrue(transport().isConnected(includePublisher = true))
+ }
+
+ @Test
+ fun waitUntilConnectedWaitsForSubscriber() = runTest {
+ val transport = transport()
+ val job = launch { transport.waitUntilConnected(includePublisher = false) }
+
+ runCurrent()
+ assertTrue(job.isActive)
+
+ subscriberState.connectionState = PeerConnectionState.CONNECTED
+ runCurrent()
+ assertTrue(job.isCompleted)
+ }
+
+ @Test
+ fun waitUntilConnectedIgnoresPublisherWhenNotIncluded() = runTest {
+ subscriberState.connectionState = PeerConnectionState.CONNECTED
+ val transport = transport()
+ val job = launch { transport.waitUntilConnected(includePublisher = false) }
+
+ runCurrent()
+ assertTrue(job.isCompleted)
+ }
+
+ @Test
+ fun waitUntilConnectedWaitsForPublisherWhenIncluded() = runTest {
+ subscriberState.connectionState = PeerConnectionState.CONNECTED
+ val transport = transport()
+ val job = launch { transport.waitUntilConnected(includePublisher = true) }
+
+ runCurrent()
+ assertTrue(job.isActive)
+
+ publisherState.connectionState = PeerConnectionState.CONNECTED
+ runCurrent()
+ assertTrue(job.isCompleted)
+ }
+
+ @Test
+ fun waitUntilConnectedWaitsForBothAtOnce() = runTest {
+ val transport = transport()
+ val job = launch { transport.waitUntilConnected(includePublisher = true) }
+ runCurrent()
+
+ subscriberState.connectionState = PeerConnectionState.CONNECTED
+ runCurrent()
+ subscriberState.connectionState = PeerConnectionState.DISCONNECTED
+ publisherState.connectionState = PeerConnectionState.CONNECTED
+ runCurrent()
+
+ assertTrue(job.isCompleted)
+ }
+
+ @Test
+ fun syncStateDescriptionsComeFromSubscriber() = runTest {
+ val offer = SessionDescription(SessionDescription.Type.OFFER, "subscriber_remote")
+ val answer = SessionDescription(SessionDescription.Type.ANSWER, "subscriber_local")
+ subscriberPc.remoteDesc = offer
+ subscriberPc.localDesc = answer
+ publisherPc.localDesc = SessionDescription(SessionDescription.Type.OFFER, "publisher_local")
+ publisherPc.remoteDesc = SessionDescription(SessionDescription.Type.ANSWER, "publisher_remote")
+
+ val descriptions = transport().syncStateDescriptions()
+
+ assertEquals(answer.toProtoSessionDescription(), descriptions.answer)
+ assertEquals(offer.toProtoSessionDescription(), descriptions.offer)
+ }
+
+ @Test
+ fun syncStateDescriptionsWhenSubscriberClosed() = runTest {
+ subscriberPc.remoteDesc = SessionDescription(SessionDescription.Type.OFFER, "subscriber_remote")
+ subscriberPc.localDesc = SessionDescription(SessionDescription.Type.ANSWER, "subscriber_local")
+ subscriber.close()
+
+ val descriptions = transport().syncStateDescriptions()
+
+ assertNull(descriptions.answer)
+ assertNull(descriptions.offer)
+ }
+
+ @Test
+ fun syncStateDescriptionsBeforeNegotiation() = runTest {
+ val descriptions = transport().syncStateDescriptions()
+
+ assertNull(descriptions.answer)
+ assertNull(descriptions.offer)
+ }
+
+ // endregion
+
+ // region close
+
+ @Test
+ fun closeClosesBoth() = runTest {
+ transport().close()
+
+ assertTrue(publisher.isClosed())
+ assertTrue(subscriber.isClosed())
+ }
+
+ // endregion
+}
diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt
index 6225fb1ec..b160a89d8 100644
--- a/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt
+++ b/livekit-android-test/src/test/java/io/livekit/android/room/RTCEngineMockE2ETest.kt
@@ -19,6 +19,8 @@ package io.livekit.android.room
import com.google.protobuf.ByteString
import io.livekit.android.ConnectOptions
import io.livekit.android.room.track.TrackException
+import io.livekit.android.room.util.MediaConstraintKeys
+import io.livekit.android.room.util.findConstraint
import io.livekit.android.test.MockE2ETest
import io.livekit.android.test.events.FlowCollector
import io.livekit.android.test.mock.MockDataChannel
@@ -41,6 +43,7 @@ import kotlinx.coroutines.test.runCurrent
import livekit.LivekitModels
import livekit.LivekitModels.DataPacket
import livekit.LivekitRtc
+import livekit.org.webrtc.MediaConstraints
import livekit.org.webrtc.PeerConnection
import org.junit.Assert
import org.junit.Assert.assertArrayEquals
@@ -63,6 +66,24 @@ class RTCEngineMockE2ETest : MockE2ETest() {
rtcEngine = component.rtcEngine()
}
+ private fun negotiatePublisherOffer(): MediaConstraints {
+ val publisher = rtcEngine.getPublisherPeerConnection() as MockPeerConnection
+ publisher.lastOfferConstraints = null
+ publisher.observer?.onRenegotiationNeeded()
+ coroutineRule.dispatcher.scheduler.advanceUntilIdle()
+ return requireNotNull(publisher.lastOfferConstraints) { "publisher did not create an offer" }
+ }
+
+ @Test
+ fun dualPeerConnectionOfferDoesNotOfferToReceive() = runTest {
+ connect()
+
+ val constraints = negotiatePublisherOffer()
+
+ assertEquals(MediaConstraintKeys.FALSE, constraints.findConstraint(MediaConstraintKeys.OFFER_TO_RECV_AUDIO))
+ assertEquals(MediaConstraintKeys.FALSE, constraints.findConstraint(MediaConstraintKeys.OFFER_TO_RECV_VIDEO))
+ }
+
@Test
fun connectionState() = runTest {
val collector = FlowCollector(rtcEngine::connectionState.flow, coroutineRule.scope)
diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/RoomMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/RoomMockE2ETest.kt
index 3eb74af16..e4022c0f8 100644
--- a/livekit-android-test/src/test/java/io/livekit/android/room/RoomMockE2ETest.kt
+++ b/livekit-android-test/src/test/java/io/livekit/android/room/RoomMockE2ETest.kt
@@ -41,6 +41,8 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import livekit.LivekitRtc
+import livekit.org.webrtc.RtpReceiver
+import livekit.org.webrtc.RtpTransceiver
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
@@ -237,6 +239,74 @@ class RoomMockE2ETest : MockE2ETest() {
assertNotNull(micPub?.track)
}
+ /**
+ * Adds a receiving media section to the subscriber and has the transport map it to
+ * [trackSid].
+ *
+ * @return the receiver of the media section.
+ */
+ private fun mapReceiverToTrackSid(trackSid: String): RtpReceiver {
+ val engine = component.rtcEngine()
+ val transceiver = engine.getSubscriberPeerConnection().addTransceiver(
+ MockAudioStreamTrack(),
+ RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.RECV_ONLY),
+ )
+ val receiver = transceiver.receiver
+
+ val transport = Mockito.spy(engine.transport!!)
+ Mockito.doReturn(trackSid).`when`(transport).trackIdForReceiver(receiver)
+ engine.transport = transport
+ return receiver
+ }
+
+ @Test
+ fun participantSubscribesRemoteTrackByMid() = runTest {
+ connect()
+
+ simulateMessageFromServer(TestData.PARTICIPANT_JOIN)
+ val remoteParticipant = room.getParticipantBySid(TestData.REMOTE_PARTICIPANT.sid)!!
+ val receiver = mapReceiverToTrackSid(trackSid = TestData.REMOTE_AUDIO_TRACK.sid)
+ val eventCollector = EventCollector(room.events, coroutineRule.scope)
+ room.onAddTrack(
+ receiver,
+ MockAudioStreamTrack(id = "local_track_id"),
+ arrayOf(
+ MockMediaStream(
+ id = createMediaStreamId(TestData.REMOTE_PARTICIPANT.sid, "local_stream_id"),
+ ),
+ ),
+ )
+ val events = eventCollector.stopCollecting()
+
+ assertIsClassList(
+ listOf(RoomEvent.TrackSubscribed::class.java),
+ events,
+ )
+ val micPub = remoteParticipant.getTrackPublication(Track.Source.MICROPHONE)
+ assertNotNull(micPub?.track)
+ }
+
+ @Test
+ fun midTakesPrecedenceOverStreamTrackSid() = runTest {
+ connect()
+
+ simulateMessageFromServer(TestData.PARTICIPANT_JOIN)
+ val remoteParticipant = room.getParticipantBySid(TestData.REMOTE_PARTICIPANT.sid)!!
+ val receiver = mapReceiverToTrackSid(trackSid = TestData.REMOTE_AUDIO_TRACK.sid)
+ room.onAddTrack(
+ receiver,
+ MockAudioStreamTrack(),
+ arrayOf(
+ MockMediaStream(
+ id = createMediaStreamId(TestData.REMOTE_PARTICIPANT.sid, "TR_unknown"),
+ ),
+ ),
+ )
+
+ val micPub = remoteParticipant.getTrackPublication(Track.Source.MICROPHONE)
+ assertNotNull(micPub?.track)
+ }
+
@Test
fun participantDisconnected() = runTest {
connect()
diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/RoomReconnectionMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/RoomReconnectionMockE2ETest.kt
index 1b1742bd3..66415eb9e 100644
--- a/livekit-android-test/src/test/java/io/livekit/android/room/RoomReconnectionMockE2ETest.kt
+++ b/livekit-android-test/src/test/java/io/livekit/android/room/RoomReconnectionMockE2ETest.kt
@@ -144,6 +144,35 @@ class RoomReconnectionMockE2ETest : MockE2ETest() {
assertTrue(sentSyncState)
}
+ @Test
+ fun softReconnectSyncStateCarriesSubscriberDescriptions() = runTest {
+ room.setReconnectionType(ReconnectType.FORCE_SOFT_RECONNECT)
+
+ connect()
+
+ disconnectPeerConnection()
+ // Wait so that the reconnect job properly starts first.
+ testScheduler.advanceTimeBy(1000)
+ reconnectWebsocket()
+ connectPeerConnection()
+
+ testScheduler.advanceUntilIdle()
+ val syncState = wsFactory.ws.sentRequests
+ .map { requestString ->
+ LivekitRtc.SignalRequest.newBuilder()
+ .mergeFrom(requestString.toPBByteString())
+ .build()
+ }
+ .last { it.hasSyncState() }
+ .syncState
+
+ val subscriberAnswer = getSubscriberPeerConnection().localDescription!!
+ assertEquals("answer", syncState.answer.type)
+ assertEquals(subscriberAnswer.description, syncState.answer.sdp)
+ assertEquals("offer", syncState.offer.type)
+ assertEquals(TestData.OFFER.offer.sdp, syncState.offer.sdp)
+ }
+
@Test
fun softReconnectSendsSyncStateUnsub() = runTest {
room.setReconnectionType(ReconnectType.FORCE_SOFT_RECONNECT)
diff --git a/livekit-android-test/src/test/java/io/livekit/android/util/EitherTest.kt b/livekit-android-test/src/test/java/io/livekit/android/util/EitherTest.kt
new file mode 100644
index 000000000..6a2bd2091
--- /dev/null
+++ b/livekit-android-test/src/test/java/io/livekit/android/util/EitherTest.kt
@@ -0,0 +1,91 @@
+/*
+ * 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.util
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class EitherTest {
+
+ private val left: Either = Either.Left(1)
+ private val right: Either = Either.Right("error")
+
+ @Test
+ fun isLeft() {
+ assertTrue(left.isLeft())
+ assertFalse(right.isLeft())
+ }
+
+ @Test
+ fun isRight() {
+ assertTrue(right.isRight())
+ assertFalse(left.isRight())
+ }
+
+ // The tests below only compile if the contracts smart-cast `value` into reach.
+
+ @Test
+ fun isLeftSmartCastsToLeft() {
+ if (left.isLeft()) {
+ val value: Int = left.value
+ assertEquals(1, value)
+ }
+ }
+
+ @Test
+ fun isLeftFalseSmartCastsToRight() {
+ if (!right.isLeft()) {
+ val value: String = right.value
+ assertEquals("error", value)
+ }
+ }
+
+ @Test
+ fun isRightSmartCastsToRight() {
+ if (right.isRight()) {
+ val value: String = right.value
+ assertEquals("error", value)
+ }
+ }
+
+ @Test
+ fun isRightFalseSmartCastsToLeft() {
+ if (!left.isRight()) {
+ val value: Int = left.value
+ assertEquals(1, value)
+ }
+ }
+
+ @Test
+ fun earlyReturnOfRightKeepsLeftType() {
+ fun firstStep(): Either = right
+
+ fun pipeline(): Either {
+ val step = firstStep()
+ if (step.isRight()) {
+ return step
+ }
+ return Either.Left(step.value.toLong())
+ }
+
+ val result = pipeline()
+ assertTrue(result.isRight())
+ assertEquals("error", (result as Either.Right).value)
+ }
+}