diff --git a/.changeset/single-peer-connection.md b/.changeset/single-peer-connection.md new file mode 100644 index 000000000..0f030623a --- /dev/null +++ b/.changeset/single-peer-connection.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": minor +--- + +Add `ConnectOptions.singlePeerConnection`, which joins through the `/rtc/v1` signal path and publishes and receives on one peer connection instead of a publisher and a subscriber. The client makes every offer; the server's media sections requirement adds receive-only transceivers, and the answer's mid-to-track mapping resolves received tracks. A server without `/rtc/v1` answers that handshake with 404, in which case the join retries on the legacy path with dual peer connections and later reconnects stay there. The option is off by default. diff --git a/livekit-android-sdk/detekt-baseline-release.xml b/livekit-android-sdk/detekt-baseline-release.xml index c8efe8b1f..3b35e4f99 100644 --- a/livekit-android-sdk/detekt-baseline-release.xml +++ b/livekit-android-sdk/detekt-baseline-release.xml @@ -24,6 +24,7 @@ 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 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 CyclomaticComplexMethod:SignalClient.kt$SignalClient$override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/ConnectOptions.kt b/livekit-android-sdk/src/main/java/io/livekit/android/ConnectOptions.kt index 7a10b9694..1821bae09 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/ConnectOptions.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/ConnectOptions.kt @@ -62,6 +62,15 @@ data class ConnectOptions( * version supported by this SDK build. */ val clientProtocol: ClientProtocolVersion = ClientProtocolVersion.DATA_STREAM_V2, + + /** + * Use a single peer connection instead of one subscriber and one publisher. + * + * ``` + * room.connect(url, token, ConnectOptions(singlePeerConnection = true)) + * ``` + */ + val singlePeerConnection: Boolean = false, ) { internal var reconnect: Boolean = false internal var participantSid: String? = null 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 index 187a605fc..909a37c3b 100644 --- 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 @@ -111,6 +111,10 @@ internal class DualPeerConnectionTransport( return null } + override suspend fun attachBoundTracks() { + // Not needed for dual transport. + } + override suspend fun handleMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement) { // Not needed for dual transport. } diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/DuplexTransportObserver.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/DuplexTransportObserver.kt new file mode 100644 index 000000000..60ce457c8 --- /dev/null +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/DuplexTransportObserver.kt @@ -0,0 +1,47 @@ +/* + * 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.LKLog +import io.livekit.android.webrtc.peerconnection.RTCThreadToken +import io.livekit.android.webrtc.peerconnection.executeOnRTCThread +import livekit.org.webrtc.MediaStream +import livekit.org.webrtc.MediaStreamTrack +import livekit.org.webrtc.RtpReceiver +import livekit.org.webrtc.RtpTransceiver + +internal class DuplexTransportObserver(engine: RTCEngine, client: SignalClient, rtcThreadToken: RTCThreadToken) : + PublisherTransportObserver(engine, client, rtcThreadToken) { + + override fun onTrack(transceiver: RtpTransceiver?) { + when (transceiver?.mediaType) { + MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO -> LKLog.v { "peerconn started receiving audio" } + MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO -> LKLog.v { "peerconn started receiving video" } + else -> LKLog.d { "peerconn started receiving unknown media type: ${transceiver?.mediaType}" } + } + } + + override fun onAddTrack(receiver: RtpReceiver?, streams: Array?) { + executeOnRTCThread(rtcThreadToken) { + if (receiver != null && streams != null) { + val track = receiver.track() ?: return@executeOnRTCThread + LKLog.v { "onAddTrack: ${track.kind()}, ${track.id()}, ${streams.fold("") { sum, it -> "$sum, $it" }}" } + engine.listener?.onAddTrack(receiver, track, streams) + } + } + } +} 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 9a4c9c6f9..e7ff05ed9 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 @@ -135,11 +135,18 @@ constructor( } } + /** + * @return whether an answer for [offerId] answers an offer older than the latest one sent. + */ + fun isStaleAnswer(offerId: Int): Boolean { + val currentOfferId = latestOfferId.get() + return currentOfferId > 0 && offerId > 0 && currentOfferId > offerId + } + suspend fun setRemoteDescription(sd: SessionDescription, offerId: Int): Either { val result = launchRTCIfNotClosed { - val currentOfferId = latestOfferId.get() - if (sd.type == SessionDescription.Type.ANSWER && currentOfferId > 0 && offerId > 0 && currentOfferId > offerId) { - return@launchRTCIfNotClosed Either.Right("Old offer, ignoring. Expected: $currentOfferId, actual: $offerId") + if (sd.type == SessionDescription.Type.ANSWER && isStaleAnswer(offerId)) { + return@launchRTCIfNotClosed Either.Right("Old offer, ignoring. Expected: ${latestOfferId.get()}, actual: $offerId") } val result = peerConnection.setRemoteDescription(sd) if (result is Either.Left) { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/PublisherTransportObserver.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/PublisherTransportObserver.kt index 2dea63532..ed513703f 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/PublisherTransportObserver.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/PublisherTransportObserver.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. @@ -32,10 +32,10 @@ import livekit.org.webrtc.RtpReceiver import livekit.org.webrtc.RtpTransceiver import livekit.org.webrtc.SessionDescription -internal class PublisherTransportObserver( - private val engine: RTCEngine, - private val client: SignalClient, - private val rtcThreadToken: RTCThreadToken, +internal open class PublisherTransportObserver( + protected val engine: RTCEngine, + protected val client: SignalClient, + protected val rtcThreadToken: RTCThreadToken, ) : PeerConnection.Observer, PeerConnectionTransport.Listener, PeerConnectionStateObservable { var connectionChangeListener: PeerConnectionStateListener? = null @@ -43,7 +43,7 @@ internal class PublisherTransportObserver( @FlowObservable @get:FlowObservable override var connectionState by flowDelegate(PeerConnection.PeerConnectionState.NEW) - private set + protected set override fun onIceCandidate(iceCandidate: IceCandidate?) { executeOnRTCThread(rtcThreadToken) { @@ -107,6 +107,6 @@ internal class PublisherTransportObserver( override fun onTrack(transceiver: RtpTransceiver?) { } - override fun onAddTrack(p0: RtpReceiver?, p1: Array?) { + override fun onAddTrack(receiver: RtpReceiver?, streams: Array?) { } } 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 a2e9eb042..c8d95b402 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 @@ -205,7 +205,7 @@ internal constructor( internal val serverInfo: ServerInfo? get() = client.serverInfo - private val publisherObserver = PublisherTransportObserver(this, client, rtcThreadToken) + private var publisherObserver: PublisherTransportObserver? = null private val subscriberObserver = SubscriberTransportObserver(this, client, rtcThreadToken) internal var transport: Transport? = null @@ -298,7 +298,25 @@ internal constructor( if (connectionState == ConnectionState.DISCONNECTED) { connectionState = ConnectionState.CONNECTING } - val joinResponse = client.join(url, token, options, roomOptions) + var effectiveOptions = options + val joinResponse = try { + client.join(url, token, options, roomOptions) + } catch (e: RoomException.ConnectException) { + // A server that predates /rtc/v1 answers that handshake with 404. + if (!options.singlePeerConnection || e.statusCode != 404) { + onError(e) + throw e + } + LKLog.w { "server has no /rtc/v1 path, retrying on the legacy path with dual peer connections" } + effectiveOptions = options.copy(singlePeerConnection = false) + connectOptions = effectiveOptions + try { + client.join(url, token, effectiveOptions, roomOptions) + } catch (e: RoomException.ConnectException) { + onError(e) + throw e + } + } ensureActive() if (joinResponse.hasParticipant()) { @@ -318,9 +336,9 @@ internal constructor( isClosed = false listener?.onSignalConnected(false) - isSubscriberPrimary = joinResponse.subscriberPrimary + isSubscriberPrimary = !effectiveOptions.singlePeerConnection && joinResponse.subscriberPrimary - configure(joinResponse, options) + 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) @@ -347,6 +365,13 @@ internal constructor( } }, ) { + val publisherObserver = if (connectOptions.singlePeerConnection) { + DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) + } else { + PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) + } + this@RTCEngine.publisherObserver = publisherObserver + participantSid = if (joinResponse.hasParticipant()) { joinResponse.participant.sid } else { @@ -360,9 +385,6 @@ internal constructor( publisherObserver, publisherObserver, ) - val subscriber = runCatching { pctFactory.create(rtcConfig, subscriberObserver, null) } - .onFailure { publisher.close() } - .getOrThrow() val connectionStateListener: PeerConnectionStateListener = { newState -> LKLog.v { "onIceConnection new state: $newState" } @@ -373,7 +395,7 @@ internal constructor( } } - if (joinResponse.subscriberPrimary) { + if (this@RTCEngine.isSubscriberPrimary) { // in subscriber primary mode, server side opens sub data channels. subscriberObserver.dataChannelListener = onDataChannel@{ dataChannel: DataChannel -> when (dataChannel.label()) { @@ -396,14 +418,27 @@ internal constructor( publisherObserver.connectionChangeListener = connectionStateListener } - transport = DualPeerConnectionTransport( - publisher, - subscriber, - publisherObserver, - subscriberObserver, - joinResponse.subscriberPrimary, - joinResponse.fastPublish, - ) + if (connectOptions.singlePeerConnection) { + transport = SinglePeerConnectionTransport( + publisher, + publisherObserver, + ) { receiver, track, trackSid -> + listener?.onTrackBound(receiver, track, trackSid) == true + } + } else { + val subscriber = runCatching { pctFactory.create(rtcConfig, subscriberObserver, null) } + .onFailure { publisher.close() } + .getOrThrow() + + transport = DualPeerConnectionTransport( + publisher, + subscriber, + publisherObserver, + subscriberObserver, + joinResponse.subscriberPrimary, + joinResponse.fastPublish, + ) + } ensureActive() // data channels @@ -551,7 +586,7 @@ internal constructor( executeBlockingOnRTCThread(rtcThreadToken) { runBlocking { configurationLock.withLock { - publisherObserver.connectionChangeListener = null + publisherObserver?.connectionChangeListener = null subscriberObserver.connectionChangeListener = null transport?.close() transport = null @@ -969,7 +1004,7 @@ internal constructor( } throw RoomException.ConnectException( - "could not establish publisher connection: publisher state: ${publisherObserver.connectionState}, channel state: ${channelManager.state}", + "could not establish publisher connection: publisher state: ${publisherObserver?.connectionState}, channel state: ${channelManager.state}", ) } @@ -1149,6 +1184,14 @@ internal constructor( fun onJoinResponse(response: JoinResponse) fun reattachRemoteDataTracks() {} fun onAddTrack(receiver: RtpReceiver, track: MediaStreamTrack, streams: Array) + + /** + * The server bound [receiver] to the publication [trackSid] without the peer connection + * reporting a new track. + * + * @return whether [track] is attached to that publication. + */ + fun onTrackBound(receiver: RtpReceiver, track: MediaStreamTrack, trackSid: String): Boolean = false fun onUpdateParticipants(updates: List) fun onActiveSpeakersUpdate(speakers: List) fun onRemoteMuteChanged(trackSid: String, muted: Boolean) @@ -1401,6 +1444,8 @@ internal constructor( override fun onMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement) { coroutineScope.launch { transport?.handleMediaSectionsRequirement(requirement) + // The server asks for an offer this way, including when it needs no new section. + negotiatePublisher() } } @@ -1637,6 +1682,15 @@ internal constructor( return transport?.trackIdForReceiver(receiver) } + /** + * Attaches the tracks the server has bound to publications that have since arrived. + */ + internal fun attachBoundTracks() { + coroutineScope.launch { + transport?.attachBoundTracks() + } + } + fun createStatsGetter(receiver: RtpReceiver): RTCStatsGetter { val p = transport?.subscriber return { statsCallback: RTCStatsCollectorCallback -> 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 b4642f829..b0b449035 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 @@ -1305,12 +1305,31 @@ constructor( return } - val statsGetter = engine.createStatsGetter(receiver) + attachTrack(participant, receiver, track, trackSid!!) + } + + /** + * @suppress + */ + override fun onTrackBound(receiver: RtpReceiver, track: MediaStreamTrack, trackSid: String): Boolean { + val participant = remoteParticipants.values.firstOrNull { it.getTrackPublication(trackSid) != null } + if (participant == null) { + LKLog.d { "no publication yet for bound track $trackSid" } + return false + } + attachTrack(participant, receiver, track, trackSid) + return true + } + + private fun attachTrack(participant: RemoteParticipant, receiver: RtpReceiver, track: MediaStreamTrack, trackSid: String) { + if (participant.getTrackPublication(trackSid)?.track?.rtcTrack === track) { + return + } participant.addSubscribedMediaTrack( track, - trackSid!!, + trackSid, autoManageVideo = adaptiveStream, - statsGetter = statsGetter, + statsGetter = engine.createStatsGetter(receiver), receiver = receiver, ) } @@ -1353,6 +1372,8 @@ constructor( } } } + // A track can be bound before the update that announces its publication. + engine.attachBoundTracks() } /** @@ -1731,7 +1752,12 @@ constructor( sealed class RoomException(message: String? = null, cause: Throwable? = null) : Exception(message, cause) { - class ConnectException(message: String? = null, cause: Throwable? = null) : + /** + * @param statusCode HTTP status of the rejected signal connection: the validate endpoint's + * status when it reported an error, otherwise the websocket handshake's. Null when the + * failure did not come with an HTTP response, such as a network error. + */ + class ConnectException(message: String? = null, cause: Throwable? = null, val statusCode: Int? = null) : RoomException(message, cause) } 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 0cad99b45..875b52051 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 @@ -25,6 +25,7 @@ import io.livekit.android.dagger.InjectionNames import io.livekit.android.room.datatrack.DataTrackSchemaException import io.livekit.android.room.participant.ParticipantTrackPermission import io.livekit.android.room.track.Track +import io.livekit.android.room.util.SignalUrlBuilder import io.livekit.android.stats.NetworkInfo import io.livekit.android.stats.getClientInfo import io.livekit.android.util.CloseableCoroutineScope @@ -32,7 +33,6 @@ import io.livekit.android.util.Either import io.livekit.android.util.LKLog import io.livekit.android.util.TimeoutException import io.livekit.android.util.rethrowIfCancellationSignal -import io.livekit.android.util.toHttpUrl import io.livekit.android.util.toWebsocketUrl import io.livekit.android.util.withDeadline import io.livekit.android.webrtc.toProtoSessionDescription @@ -192,10 +192,16 @@ constructor( // Clean up any pre-existing connection. close(reason = "Starting new connection", shouldClearQueuedRequests = false) - val wsUrlString = "${url.toWebsocketUrl()}/rtc${createConnectionParams(getClientInfo(options.clientProtocol), options, roomOptions)}" + var wsUrlString = "${url.toWebsocketUrl()}/rtc${createConnectionParams(getClientInfo(options.clientProtocol), options, roomOptions)}" isReconnecting = options.reconnect - LKLog.i { "connecting to $wsUrlString" } + if (options.singlePeerConnection) { + wsUrlString = SignalUrlBuilder.buildJoinRequestUrl(wsUrlString, options, roomOptions, networkInfo.getNetworkType()) + // The join request carries client metadata, so only the path is logged. + LKLog.i { "connecting to ${wsUrlString.substringBefore('?')}?${SignalUrlBuilder.QUERY_JOIN_REQUEST}=" } + } else { + LKLog.i { "connecting to $wsUrlString" } + } coroutineScope = CloseableCoroutineScope(SupervisorJob() + ioDispatcher) lastUrl = wsUrlString @@ -359,12 +365,14 @@ constructor( return } var reason: String? = null + // The validate endpoint explains a rejected handshake; its status wins over the + // handshake's when it answers with an error. + var statusCode: Int? = response?.code try { val lastToken = webSocket.request().header("Authorization") - lastUrl?.let { - val validationUrl = it.toHttpUrl().replaceFirst("/rtc?", "/rtc/validate?") + lastUrl?.let { SignalUrlBuilder.toValidateUrl(it) }?.let { val request = Request.Builder() - .url(validationUrl) + .url(it) .apply { if (lastToken != null) { addHeader("Authorization", lastToken) @@ -376,6 +384,7 @@ constructor( val body = resp.body if (!resp.isSuccessful) { reason = body?.string() + statusCode = resp.code } body?.close() } @@ -393,22 +402,16 @@ constructor( if (reason != null) { LKLog.e(t) { "websocket failure: $reason" } - val error = if (joinCont != null) { - RoomException.ConnectException(reason, t) - } else { - Exception(reason) - } - listener?.onError(error) - joinCont?.resumeWithException(error) } else { LKLog.e(t) { "websocket failure: $response" } - val error = if (joinCont != null) { - RoomException.ConnectException(t.localizedMessage, t) - } else { - t - } - listener?.onError(error) - joinCont?.resumeWithException(error) + } + + if (joinCont != null) { + // The caller of join/reconnect receives the error through the continuation and decides + // whether to retry or report it, so the listener is not notified here. + joinCont.resumeWithException(RoomException.ConnectException(reason ?: t.localizedMessage, t, statusCode)) + } else { + listener?.onError(if (reason != null) Exception(reason) else t) } if (wasConnected || wasReconnectHandshake) { @@ -416,7 +419,7 @@ constructor( // Handle websocket closure here. // Also handle failure during a soft reconnect handshake (reconnect query): isConnected is // still false but the upper layer should be notified like a close. Initial join handshake - // failures do not call onClose so RTCEngine can surface them via onError / onFailToConnect. + // failures do not call onClose; the join caller surfaces them. handleWebSocketClose( reason = reason ?: response?.toString() ?: t.localizedMessage ?: "websocket failure", code = response?.code ?: CLOSE_REASON_WEBSOCKET_FAILURE, 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 new file mode 100644 index 000000000..03373b1ec --- /dev/null +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt @@ -0,0 +1,177 @@ +/* + * 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.PeerConnectionStateObservable +import io.livekit.android.room.util.waitUntilConnected +import io.livekit.android.util.Either +import io.livekit.android.util.LKLog +import io.livekit.android.util.isLeft +import io.livekit.android.webrtc.toProtoSessionDescription +import kotlinx.coroutines.runBlocking +import livekit.LivekitRtc +import livekit.org.webrtc.IceCandidate +import livekit.org.webrtc.MediaConstraints +import livekit.org.webrtc.MediaStreamTrack +import livekit.org.webrtc.PeerConnection +import livekit.org.webrtc.RtpReceiver +import livekit.org.webrtc.RtpTransceiver +import livekit.org.webrtc.SessionDescription +import java.util.concurrent.ConcurrentHashMap + +/** + * A [Transport] with one peer connection that both publishes and receives. The client makes + * every offer on [publisher], and the server opens receiving media sections on it through + * [handleMediaSectionsRequirement]. + * + * @param publisherState connection state of [publisher], as reported by its observer. + * @param onTrackBound called after an answer for each receiver the server has bound to a track + * ID that has not been attached yet. Returns whether the track was attached; a receiver that + * was not is offered again after the next answer. + */ +@Suppress("TooManyFunctions") +internal class SinglePeerConnectionTransport( + override val publisher: PeerConnectionTransport, + private val publisherState: PeerConnectionStateObservable, + private val onTrackBound: (receiver: RtpReceiver, track: MediaStreamTrack, trackId: String) -> Boolean, +) : Transport { + private val midToTrackId: MutableMap = ConcurrentHashMap() + + // Receiver ID to the track ID it was last attached as. + private val attachedTrackIds: MutableMap = ConcurrentHashMap() + override val subscriber: PeerConnectionTransport + get() = publisher + override val publisherConnectsOnDemand: Boolean + get() = false + override val publisherOfferConstraints: List + get() = emptyList() + + override fun shouldNegotiatePublisherOnJoin(hasPublished: Boolean): Boolean { + return true + } + + override fun addIceCandidate(candidate: IceCandidate, target: LivekitRtc.SignalTarget): Boolean { + if (target == LivekitRtc.SignalTarget.SUBSCRIBER || target == LivekitRtc.SignalTarget.PUBLISHER) { + publisher.addIceCandidate(candidate) + return true + } + LKLog.i { "unknown ice candidate target: $target" } + return false + } + + override suspend fun answerServerOffer( + offer: SessionDescription, + offerId: Int, + ): Either { + return Either.Right("Received offer but not in dual PC mode, ignoring") + } + + override suspend fun applyServerAnswer( + answer: SessionDescription, + offerId: Int, + midToTrackId: Map, + ): Either { + if (publisher.isStaleAnswer(offerId)) { + return Either.Right("Old offer, ignoring: $offerId") + } + // libwebrtc reports new tracks while it applies the description, and those lookups + // read this mapping. + this.midToTrackId.putAll(midToTrackId) + + val result = publisher.setRemoteDescription(answer, offerId) + if (result.isLeft()) { + attachBoundTracks() + } + return result + } + + /** + * Offers [onTrackBound] every receiver whose media section is mapped to a track ID it is + * not attached as. libwebrtc reports a track only when its section starts receiving, not + * when the server binds another track to a section that already is. + */ + override suspend fun attachBoundTracks() { + publisher.withPeerConnection { + for (transceiver in transceivers) { + val trackId = transceiver.mid?.let { midToTrackId[it] } ?: continue + val receiver = transceiver.receiver ?: continue + val track = receiver.track() ?: continue + if (attachedTrackIds[receiver.id()] == trackId) { + continue + } + if (onTrackBound(receiver, track, trackId)) { + attachedTrackIds[receiver.id()] = trackId + } + } + } + } + + override fun trackIdForReceiver(receiver: RtpReceiver): String? { + val receiverId = receiver.id() + val mid = runBlocking { + publisher.withPeerConnection { + transceivers.firstOrNull { it.receiver.id() == receiverId }?.mid + } + } ?: return null + return midToTrackId[mid] + } + + override suspend fun handleMediaSectionsRequirement(requirement: LivekitRtc.MediaSectionsRequirement) { + if (publisher.isClosed()) { + return + } + val transceiverInit = RtpTransceiver.RtpTransceiverInit(RtpTransceiver.RtpTransceiverDirection.RECV_ONLY) + publisher.withPeerConnection { + for (i in 0../rtc/v1?join_request=`. + * + * [url] may use an http(s) or ws(s) scheme and may include a path prefix. A trailing + * `rtc` or `validate` path segment is replaced rather than appended to. + * + * The join request is gzipped only when that makes it smaller, and is encoded with the + * URL-safe base64 alphabet, keeping padding. + * + * @param connectOptions also decides whether this is a reconnect, and with which participant. + * @param publisherOffer bundled into the join request so the server can answer it + * in the same exchange. + * @return the websocket URL. + * @throws IllegalArgumentException if [url] cannot be parsed. + */ + fun buildJoinRequestUrl( + url: String, + connectOptions: ConnectOptions, + roomOptions: RoomOptions, + networkType: NetworkType? = null, + publisherOffer: SessionDescription? = null, + ): String { + val baseUrl = url.toHttpUrl().parseHttpUrl() + + // An empty last segment means the path ends in '/', which marks it as a directory. + val isDirectory = baseUrl.pathSegments.last().isEmpty() + val segments = baseUrl.encodedPathSegments.filter { it.isNotEmpty() }.toMutableList() + val lastSegment = segments.lastOrNull() + if (!isDirectory && (lastSegment == "rtc" || lastSegment == "validate")) { + segments.removeLastOrNull() + } + segments.add("rtc") + segments.add("v1") + + val rtcUrl = baseUrl.newBuilder() + .encodedPath("/" + segments.joinToString("/")) + .query(null) + .fragment(null) + .build() + + val joinRequest = buildWrappedJoinRequest( + connectOptions = connectOptions, + roomOptions = roomOptions, + networkType = networkType, + publisherOffer = publisherOffer, + ) + + return "${rtcUrl.toString().toWebsocketUrl()}?$QUERY_JOIN_REQUEST=$joinRequest" + } + + /** + * Converts a signal websocket URL to its HTTP validation counterpart: + * - `wss://host/rtc?...` → `https://host/rtc/validate?...` + * - `wss://host/rtc/v1?...` → `https://host/rtc/v1/validate?...` + * + * @return the validation URL. + * @throws IllegalArgumentException if [wsUrl] cannot be parsed. + */ + fun toValidateUrl(wsUrl: String): String { + // addPathSegment replaces an empty last segment, so a trailing '/' doesn't double up. + return wsUrl.toHttpUrl().parseHttpUrl() + .newBuilder() + .addPathSegment("validate") + .build() + .toString() + } + + /** + * @return the serialized [WrappedJoinRequest], base64url-encoded with padding. + */ + private fun buildWrappedJoinRequest( + connectOptions: ConnectOptions, + roomOptions: RoomOptions, + networkType: NetworkType?, + publisherOffer: SessionDescription?, + ): String { + val joinRequest = with(JoinRequest.newBuilder()) { + clientInfo = with(getClientInfo(connectOptions.clientProtocol).toBuilder()) { + protocol = connectOptions.protocolVersion.value + networkType?.let { network = it.protoName } + build() + } + connectionSettings = with(ConnectionSettings.newBuilder()) { + autoSubscribe = connectOptions.autoSubscribe + adaptiveStream = roomOptions.adaptiveStream + build() + } + publisherOffer?.let { this.publisherOffer = it } + if (connectOptions.reconnect) { + reconnect = true + reconnectReason = ReconnectReason.RR_SIGNAL_DISCONNECTED + connectOptions.participantSid?.let { participantSid = it } + } + build() + }.toByteArray() + + // gzip framing outweighs the savings on a small request, so keep whichever is smaller. + val gzipped = joinRequest.gzip() + val (payload, compression) = if (gzipped.size < joinRequest.size) { + gzipped to Compression.GZIP + } else { + joinRequest to Compression.NONE + } + + return WrappedJoinRequest.newBuilder() + .setCompression(compression) + .setJoinRequest(ByteString.copyFrom(payload)) + .build() + .toByteArray() + .toByteString() + .base64Url() + } + + private fun ByteArray.gzip(): ByteArray { + val output = ByteArrayOutputStream() + GZIPOutputStream(output).use { it.write(this) } + return output.toByteArray() + } +} diff --git a/livekit-android-test/src/main/java/io/livekit/android/test/MockE2ETest.kt b/livekit-android-test/src/main/java/io/livekit/android/test/MockE2ETest.kt index 48fff3c17..d8a7234d4 100644 --- a/livekit-android-test/src/main/java/io/livekit/android/test/MockE2ETest.kt +++ b/livekit-android-test/src/main/java/io/livekit/android/test/MockE2ETest.kt @@ -137,6 +137,18 @@ abstract class MockE2ETest : BaseTest() { .build() } + /** + * The HTTP response of a websocket handshake the server rejected with [code]. + */ + fun createFailedHandshakeResponse(request: Request, code: Int): Response { + return Response.Builder() + .request(request) + .code(code) + .protocol(Protocol.HTTP_1_1) + .message("") + .build() + } + /** * Simulates receiving [message] from the server */ diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/DuplexTransportObserverTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/DuplexTransportObserverTest.kt new file mode 100644 index 000000000..2b554d2be --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/DuplexTransportObserverTest.kt @@ -0,0 +1,186 @@ +/* + * 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.test.mock.MockRTCThreadToken +import io.livekit.android.webrtc.peerconnection.RTCThreadToken +import io.livekit.android.webrtc.peerconnection.executeBlockingOnRTCThread +import livekit.LivekitRtc.SignalTarget +import livekit.org.webrtc.IceCandidate +import livekit.org.webrtc.MediaStream +import livekit.org.webrtc.MediaStreamTrack +import livekit.org.webrtc.PeerConnection.PeerConnectionState +import livekit.org.webrtc.RtpReceiver +import livekit.org.webrtc.RtpTransceiver +import livekit.org.webrtc.SessionDescription +import org.junit.Assert.assertEquals +import org.junit.Assert.assertSame +import org.junit.Before +import org.junit.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever + +class DuplexTransportObserverTest { + + private val disposedToken = object : RTCThreadToken { + override val isDisposed: Boolean = true + } + + private lateinit var engine: RTCEngine + private lateinit var engineListener: RTCEngine.Listener + private lateinit var client: SignalClient + private lateinit var observer: DuplexTransportObserver + + private val track = mock() + private val receiver = mock { on { track() } doReturn track } + private val streams = arrayOf(mock()) + + @Before + fun setup() { + engineListener = mock() + engine = mock() + whenever(engine.listener).thenReturn(engineListener) + client = mock() + observer = DuplexTransportObserver(engine, client, MockRTCThreadToken()) + } + + /** + * Callbacks run asynchronously on the single RTC thread; a blocking no-op queued + * behind them returns once they have all run. + */ + private fun flushRTCThread() { + executeBlockingOnRTCThread(MockRTCThreadToken()) {} + } + + // region onAddTrack + + @Test + fun addedTrackIsForwardedToEngine() { + observer.onAddTrack(receiver, streams) + flushRTCThread() + + verify(engineListener).onAddTrack(receiver, track, streams) + } + + @Test + fun addedTrackWithoutMediaTrackIsIgnored() { + val emptyReceiver = mock { on { track() } doReturn null } + + observer.onAddTrack(emptyReceiver, streams) + flushRTCThread() + + verify(engineListener, never()).onAddTrack(any(), any(), any()) + } + + @Test + fun addedTrackWithoutReceiverIsIgnored() { + observer.onAddTrack(null, streams) + flushRTCThread() + + verify(engineListener, never()).onAddTrack(any(), any(), any()) + } + + @Test + fun addedTrackWithoutStreamsIsIgnored() { + observer.onAddTrack(receiver, null) + flushRTCThread() + + verify(engineListener, never()).onAddTrack(any(), any(), any()) + } + + @Test + fun addedTrackWithoutEngineListenerIsDropped() { + whenever(engine.listener).thenReturn(null) + + observer.onAddTrack(receiver, streams) + flushRTCThread() + } + + @Test + fun addedTrackAfterDisposeIsDropped() { + val disposedObserver = DuplexTransportObserver(engine, client, disposedToken) + + disposedObserver.onAddTrack(receiver, streams) + flushRTCThread() + + verify(engineListener, never()).onAddTrack(any(), any(), any()) + } + + // endregion + + // region onTrack + + @Test + fun trackOfAnyMediaTypeIsAccepted() { + val audio = mock { on { mediaType } doReturn MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO } + val video = mock { on { mediaType } doReturn MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO } + + observer.onTrack(audio) + observer.onTrack(video) + observer.onTrack(null) + } + + // endregion + + // region publisher behavior + + @Test + fun localCandidateIsSentForPublisher() { + val candidate = IceCandidate("0", 0, "candidate:1 1 udp 2122260223 192.168.1.2 50000 typ host") + + observer.onIceCandidate(candidate) + flushRTCThread() + + verify(client).sendCandidate(candidate, SignalTarget.PUBLISHER) + } + + @Test + fun offerIsSentToServer() { + val offer = SessionDescription(SessionDescription.Type.OFFER, "local_offer") + + observer.onOffer(offer, offerId = 3) + flushRTCThread() + + verify(client).sendOffer(offer, 3) + } + + @Test + fun renegotiationNeededNegotiates() { + observer.onRenegotiationNeeded() + flushRTCThread() + + verify(engine).negotiatePublisher() + } + + @Test + fun connectionChangeUpdatesState() { + val states = mutableListOf() + observer.connectionChangeListener = { states.add(it) } + + observer.onConnectionChange(PeerConnectionState.CONNECTED) + flushRTCThread() + + assertSame(PeerConnectionState.CONNECTED, observer.connectionState) + assertEquals(listOf(PeerConnectionState.CONNECTED), states) + } + + // 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 b160a89d8..4a0bebb61 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 @@ -18,10 +18,12 @@ package io.livekit.android.room import com.google.protobuf.ByteString import io.livekit.android.ConnectOptions +import io.livekit.android.events.RoomEvent 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.EventCollector import io.livekit.android.test.events.FlowCollector import io.livekit.android.test.mock.MockDataChannel import io.livekit.android.test.mock.MockPeerConnection @@ -54,6 +56,7 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +import java.io.IOException @ExperimentalCoroutinesApi @RunWith(RobolectricTestRunner::class) @@ -170,6 +173,29 @@ class RTCEngineMockE2ETest : MockE2ETest() { ) } + @Test + fun joinFailureWithoutFallbackReportsFailedToConnectOnce() = runTest { + val collector = EventCollector(room.events, coroutineRule.scope) + val connectJob = async { + runCatching { room.connect(url = TestData.EXAMPLE_URL, token = "token") } + } + room::state.flow + .takeWhile { it != Room.State.CONNECTING } + .collect() + runCurrent() + + val firstRequest = wsFactory.request + wsFactory.listener.onFailure(wsFactory.ws, IOException("500"), createFailedHandshakeResponse(firstRequest, 500)) + advanceUntilIdle() + + val error = connectJob.await().exceptionOrNull() + assertTrue("expected ConnectException, got $error", error is RoomException.ConnectException) + assertEquals(500, (error as RoomException.ConnectException).statusCode) + assertTrue("no retry expected", wsFactory.request === firstRequest) + val events = collector.stopCollecting() + assertEquals(1, events.count { it is RoomEvent.FailedToConnect }) + } + @Test fun iceSubscriberConnect() = runTest { connect() diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SignalClientTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SignalClientTest.kt index 905a51c81..e2ad2a4df 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/SignalClientTest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SignalClientTest.kt @@ -235,8 +235,9 @@ class SignalClientTest : BaseTest() { /** * Cold [join] handshake failure: [isConnected] is still false and this is not a reconnect attempt, - * so the listener must not get [SignalClient.Listener.onClose] (RTCEngine uses [onError] while - * CONNECTING). OkHttp does not call [WebSocketListener.onClosed] after [WebSocketListener.onFailure]. + * so the listener gets neither [SignalClient.Listener.onClose] nor [SignalClient.Listener.onError]; + * the error reaches the [join] caller, which decides whether to retry or report it. OkHttp does + * not call [WebSocketListener.onClosed] after [WebSocketListener.onFailure]. */ @Test fun onCloseNotInvokedWhenJoinHandshakeFailsBeforeJoinMessage() = runTest { @@ -252,7 +253,7 @@ class SignalClientTest : BaseTest() { } Mockito.verify(listener, never()).onClose(any(), any()) - Mockito.verify(listener, times(1)).onError(any()) + Mockito.verify(listener, never()).onError(any()) } /** diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFallbackMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFallbackMockE2ETest.kt new file mode 100644 index 000000000..c139b0fa1 --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFallbackMockE2ETest.kt @@ -0,0 +1,138 @@ +/* + * 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.events.RoomEvent +import io.livekit.android.test.MockE2ETest +import io.livekit.android.test.events.EventCollector +import io.livekit.android.test.mock.TestData +import io.livekit.android.util.flow +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.takeWhile +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceTimeBy +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runCurrent +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +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] to a server that has no `/rtc/v1` path: + * the join retries on the legacy path with dual peer connections, and later reconnects stay there. + */ +@ExperimentalCoroutinesApi +@RunWith(RobolectricTestRunner::class) +class SinglePeerConnectionFallbackMockE2ETest : MockE2ETest() { + + /** + * Connects with single peer connection requested, answers the `/rtc/v1` handshake with 404, + * and completes the legacy join that follows. + */ + private suspend fun TestScope.connectWithFallback() { + val connectJob = coroutineRule.scope.launch { + room.connect( + url = TestData.EXAMPLE_URL, + token = "token", + options = ConnectOptions(singlePeerConnection = true), + ) + } + room::state.flow + .takeWhile { it != Room.State.CONNECTING } + .collect() + runCurrent() + + val v1Request = wsFactory.request + assertEquals("/rtc/v1", v1Request.url.encodedPath) + wsFactory.listener.onFailure(wsFactory.ws, IOException("404"), createFailedHandshakeResponse(v1Request, 404)) + // runCurrent rather than advanceUntilIdle: advancing virtual time would expire the + // legacy handshake's deadline before the join response below is delivered. + runCurrent() + + val legacyRequest = wsFactory.request + assertTrue("expected a second websocket on the legacy path", legacyRequest !== v1Request) + assertEquals("/rtc", legacyRequest.url.encodedPath) + wsFactory.listener.onOpen(wsFactory.ws, createOpenResponse(legacyRequest)) + simulateMessageFromServer(TestData.JOIN) + connectJob.join() + connectPeerConnection() + } + + @Test + fun fallsBackToLegacySignalPathWhenV1NotFound() = runTest { + val collector = EventCollector(room.events, coroutineRule.scope) + + connectWithFallback() + + val events = collector.stopCollecting() + assertTrue("fallback must not report a failure", events.none { it is RoomEvent.FailedToConnect }) + assertEquals(Room.State.CONNECTED, room.state) + } + + @Test + fun fullReconnectAfterFallbackStaysOnLegacyPath() = runTest { + room.setReconnectionType(ReconnectType.FORCE_FULL_RECONNECT) + connectWithFallback() + val fallbackRequest = wsFactory.request + + disconnectPeerConnection() + // Wait so that the reconnect job properly starts first. + testScheduler.advanceTimeBy(1000) + + val reconnectRequest = wsFactory.request + assertTrue("expected a new websocket for the reconnect", reconnectRequest !== fallbackRequest) + assertEquals("/rtc", reconnectRequest.url.encodedPath) + assertNull(reconnectRequest.url.queryParameter(SignalClient.CONNECT_QUERY_RECONNECT)) + + wsFactory.listener.onOpen(wsFactory.ws, createOpenResponse(reconnectRequest)) + simulateMessageFromServer(TestData.JOIN) + connectPeerConnection() + testScheduler.advanceUntilIdle() + + assertEquals(Room.State.CONNECTED, room.state) + } + + @Test + fun softReconnectAfterFallbackStaysOnLegacyPath() = runTest { + room.setReconnectionType(ReconnectType.FORCE_SOFT_RECONNECT) + connectWithFallback() + val fallbackRequest = wsFactory.request + + disconnectPeerConnection() + // Wait so that the reconnect job properly starts first. + testScheduler.advanceTimeBy(1000) + + val reconnectRequest = wsFactory.request + assertTrue("expected a new websocket for the reconnect", reconnectRequest !== fallbackRequest) + assertEquals("/rtc", reconnectRequest.url.encodedPath) + assertEquals("1", reconnectRequest.url.queryParameter(SignalClient.CONNECT_QUERY_RECONNECT)) + + wsFactory.listener.onOpen(wsFactory.ws, createOpenResponse(reconnectRequest)) + simulateMessageFromServer(TestData.RECONNECT) + connectPeerConnection() + testScheduler.advanceUntilIdle() + + assertEquals(Room.State.CONNECTED, room.state) + } +} 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 new file mode 100644 index 000000000..1debb049d --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt @@ -0,0 +1,258 @@ +/* + * 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.events.RoomEvent +import io.livekit.android.room.participant.RemoteParticipant +import io.livekit.android.room.track.Track +import io.livekit.android.room.util.SignalUrlBuilder +import io.livekit.android.test.MockE2ETest +import io.livekit.android.test.events.EventCollector +import io.livekit.android.test.mock.MockAudioStreamTrack +import io.livekit.android.test.mock.MockMediaStream +import io.livekit.android.test.mock.TestData +import io.livekit.android.test.mock.createMediaStreamId +import io.livekit.android.test.util.toPBByteString +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runCurrent +import livekit.LivekitRtc +import livekit.org.webrtc.MediaStreamTrack +import livekit.org.webrtc.PeerConnection +import livekit.org.webrtc.RtpTransceiver +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertSame +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.kotlin.whenever +import org.robolectric.RobolectricTestRunner + +/** + * Connecting with [ConnectOptions.singlePeerConnection] to a server that serves `/rtc/v1`: + * one peer connection carries the join-time offer, the server's media sections, and the + * received tracks. + */ +@ExperimentalCoroutinesApi +@RunWith(RobolectricTestRunner::class) +class SinglePeerConnectionMockE2ETest : MockE2ETest() { + + private fun publisherPeerConnection() = getPublisherPeerConnection() + + /** + * @return the offers the client has sent to the server, oldest first. + */ + private fun sentOffers(): List { + return wsFactory.ws.sentRequests + .map { LivekitRtc.SignalRequest.newBuilder().mergeFrom(it.toPBByteString()).build() } + .filter { it.hasOffer() } + .map { it.offer } + } + + /** + * Answers the client's most recent offer, 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() + val answer = with(LivekitRtc.SignalResponse.newBuilder()) { + setAnswer( + with(LivekitRtc.SessionDescription.newBuilder()) { + type = "answer" + sdp = "remote_answer" + id = offer.id + putAllMidToTrackId(midToTrackId) + build() + }, + ) + build() + } + simulateMessageFromServer(answer) + advanceUntilIdle() + } + + /** + * Joins through `/rtc/v1`, answers the offer the join triggers, and connects the peer + * connection. + */ + private suspend fun TestScope.connectSinglePeerConnection() { + val connectJob = coroutineRule.scope.launch { + room.connect( + url = TestData.EXAMPLE_URL, + token = "token", + options = ConnectOptions(singlePeerConnection = true), + ) + } + prepareSignal() + connectJob.join() + advanceUntilIdle() + + answerLastOffer() + publisherPeerConnection().moveToIceConnectionState(PeerConnection.IceConnectionState.CONNECTED) + runCurrent() + } + + /** + * Has the server require [audios] receiving audio sections and [videos] video ones, which + * leaves the renegotiation offer that follows unanswered. + */ + private fun TestScope.requireMediaSections(audios: Int, videos: Int) { + val requirement = with(LivekitRtc.SignalResponse.newBuilder()) { + setMediaSectionsRequirement( + with(LivekitRtc.MediaSectionsRequirement.newBuilder()) { + numAudios = audios + numVideos = videos + build() + }, + ) + build() + } + simulateMessageFromServer(requirement) + advanceUntilIdle() + } + + @Test + fun joinsThroughV1WithOnePeerConnection() = runTest { + val collector = EventCollector(room.events, coroutineRule.scope) + + connectSinglePeerConnection() + + val request = wsFactory.request + assertEquals("/rtc/v1", request.url.encodedPath) + assertNotNull(request.url.queryParameter(SignalUrlBuilder.QUERY_JOIN_REQUEST)) + assertTrue(component.rtcEngine().transport is SinglePeerConnectionTransport) + assertSame(getPublisherPeerConnection(), getSubscriberPeerConnection()) + assertEquals(Room.State.CONNECTED, room.state) + val events = collector.stopCollecting() + assertTrue(events.none { it is RoomEvent.FailedToConnect }) + } + + @Test + fun publisherOffersOnJoinWithoutOfferingToReceive() = runTest { + connectSinglePeerConnection() + + val constraints = publisherPeerConnection().lastOfferConstraints + assertNotNull(constraints) + assertTrue(constraints!!.mandatory.isEmpty()) + } + + @Test + fun mediaSectionsRequirementAddsReceivingSections() = runTest { + connectSinglePeerConnection() + + requireMediaSections(audios = 1, videos = 2) + answerLastOffer() + + val transceivers = publisherPeerConnection().transceivers + assertEquals(3, transceivers.size) + assertTrue(transceivers.all { it.direction == RtpTransceiver.RtpTransceiverDirection.RECV_ONLY }) + assertEquals(1, transceivers.count { it.mediaType == MediaStreamTrack.MediaType.MEDIA_TYPE_AUDIO }) + assertEquals(2, transceivers.count { it.mediaType == MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO }) + } + + /** + * Adds one receiving audio section with mid "0" and a track on its receiver, and leaves + * the renegotiation offer unanswered. + */ + private fun TestScope.addReceivingAudioSection(): RtpTransceiver { + requireMediaSections(audios = 1, videos = 0) + val transceiver = publisherPeerConnection().transceivers.single() + whenever(transceiver.mid).thenReturn("0") + whenever(transceiver.receiver.track()).thenReturn(MockAudioStreamTrack()) + return transceiver + } + + @Test + fun boundTrackAttachesWithoutTrackCallback() = runTest { + connectSinglePeerConnection() + simulateMessageFromServer(TestData.PARTICIPANT_JOIN) + val remoteParticipant = room.getParticipantBySid(TestData.REMOTE_PARTICIPANT.sid) as RemoteParticipant + addReceivingAudioSection() + val collector = EventCollector(room.events, coroutineRule.scope) + + answerLastOffer(midToTrackId = mapOf("0" to TestData.REMOTE_AUDIO_TRACK.sid)) + + val events = collector.stopCollecting() + assertEquals(1, events.count { it is RoomEvent.TrackSubscribed }) + val micPub = remoteParticipant.getTrackPublication(Track.Source.MICROPHONE) + assertNotNull(micPub?.track) + } + + @Test + fun boundTrackAttachesOncePublicationArrives() = runTest { + connectSinglePeerConnection() + addReceivingAudioSection() + val collector = EventCollector(room.events, coroutineRule.scope) + answerLastOffer(midToTrackId = mapOf("0" to TestData.REMOTE_AUDIO_TRACK.sid)) + + simulateMessageFromServer(TestData.PARTICIPANT_JOIN) + advanceUntilIdle() + + val events = collector.stopCollecting() + assertEquals(1, events.count { it is RoomEvent.TrackSubscribed }) + val remoteParticipant = room.getParticipantBySid(TestData.REMOTE_PARTICIPANT.sid) as RemoteParticipant + assertNotNull(remoteParticipant.getTrackPublication(Track.Source.MICROPHONE)?.track) + } + + @Test + fun trackCallbackAfterBoundTrackDoesNotAttachTwice() = runTest { + connectSinglePeerConnection() + simulateMessageFromServer(TestData.PARTICIPANT_JOIN) + val transceiver = addReceivingAudioSection() + val collector = EventCollector(room.events, coroutineRule.scope) + answerLastOffer(midToTrackId = mapOf("0" to TestData.REMOTE_AUDIO_TRACK.sid)) + + publisherPeerConnection().observer?.onAddTrack( + transceiver.receiver, + arrayOf(MockMediaStream(id = createMediaStreamId(TestData.REMOTE_PARTICIPANT.sid, "stream"))), + ) + advanceUntilIdle() + + val events = collector.stopCollecting() + assertEquals(1, events.count { it is RoomEvent.TrackSubscribed }) + } + + @Test + fun zeroCountRequirementSendsOffer() = runTest { + connectSinglePeerConnection() + val offersBefore = sentOffers().size + + requireMediaSections(audios = 0, videos = 0) + + assertEquals(offersBefore + 1, sentOffers().size) + assertTrue(publisherPeerConnection().transceivers.isEmpty()) + } + + @Test + fun serverOfferIsIgnored() = runTest { + connectSinglePeerConnection() + val sentBefore = wsFactory.ws.sentRequests.size + + simulateMessageFromServer(TestData.OFFER) + advanceUntilIdle() + + val answers = wsFactory.ws.sentRequests.drop(sentBefore) + .map { LivekitRtc.SignalRequest.newBuilder().mergeFrom(it.toPBByteString()).build() } + .count { it.hasAnswer() } + assertEquals(0, answers) + assertEquals(Room.State.CONNECTED, room.state) + } +} 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 new file mode 100644 index 000000000..e38be5953 --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt @@ -0,0 +1,591 @@ +/* + * 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.PeerConnectionStateObservable +import io.livekit.android.test.mock.MockPeerConnection +import io.livekit.android.test.mock.MockRTCThreadToken +import io.livekit.android.test.mock.MockRtpReceiver +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.MediaStreamTrack.MediaType +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.RtpReceiver +import livekit.org.webrtc.RtpTransceiver.RtpTransceiverDirection +import livekit.org.webrtc.RtpTransceiver.RtpTransceiverInit +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.doReturn +import org.mockito.kotlin.eq +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 SinglePeerConnectionTransportTest { + + private class FakeConnectionState : PeerConnectionStateObservable { + @FlowObservable + @get:FlowObservable + override var connectionState by flowDelegate(PeerConnectionState.NEW) + } + + private lateinit var publisher: PeerConnectionTransport + private val publisherState = FakeConnectionState() + + private val publisherPc: MockPeerConnection + get() = publisher.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 = spy( + PeerConnectionTransport( + config = RTCConfiguration(emptyList()), + pcObserver = mock(), + listener = null, + ioDispatcher = Dispatchers.Unconfined, + connectionFactory = MockPeerConnectionFactory(), + sdpFactory = SdpFactory.getInstance(), + rtcThreadToken = MockRTCThreadToken(), + ), + ) + } + + private val boundTracks = mutableListOf>() + private var attachBoundTracks = true + + private fun transport() = SinglePeerConnectionTransport( + publisher = publisher, + publisherState = publisherState, + onTrackBound = { receiver, _, trackId -> + boundTracks.add(receiver to trackId) + attachBoundTracks + }, + ) + + private fun answer(sdp: String = "remote_answer") = SessionDescription(SessionDescription.Type.ANSWER, sdp) + + // region roles + + @Test + fun publisherIsThePeerConnection() { + assertSame(publisher, transport().publisher) + } + + @Test + fun subscriberIsThePublisher() { + assertSame(publisher, transport().subscriber) + } + + @Test + fun publisherConnectsOnJoin() { + assertFalse(transport().publisherConnectsOnDemand) + } + + @Test + fun publisherOffersHaveNoConstraints() { + assertTrue(transport().publisherOfferConstraints.isEmpty()) + } + + @Test + fun publisherIsNegotiatedOnJoin() { + assertTrue(transport().shouldNegotiatePublisherOnJoin(hasPublished = false)) + assertTrue(transport().shouldNegotiatePublisherOnJoin(hasPublished = true)) + } + + // endregion + + // region addIceCandidate + + @Test + fun publisherCandidateGoesToPublisher() { + assertTrue(transport().addIceCandidate(candidate, SignalTarget.PUBLISHER)) + + verify(publisher).addIceCandidate(candidate) + } + + @Test + fun subscriberCandidateGoesToPublisher() { + assertTrue(transport().addIceCandidate(candidate, SignalTarget.SUBSCRIBER)) + + verify(publisher).addIceCandidate(candidate) + } + + @Test + fun unrecognizedTargetCandidateIsRejected() { + assertFalse(transport().addIceCandidate(candidate, SignalTarget.UNRECOGNIZED)) + + verify(publisher, never()).addIceCandidate(any()) + } + + // endregion + + // region answerServerOffer + + @Test + fun answerServerOfferIsRejected() = runTest { + val offer = SessionDescription(SessionDescription.Type.OFFER, "remote_offer") + + val result = transport().answerServerOffer(offer, offerId = 7) + + assertTrue("$result", result is Either.Right) + } + + @Test + fun answerServerOfferLeavesPublisherAlone() = runTest { + val offer = SessionDescription(SessionDescription.Type.OFFER, "remote_offer") + + transport().answerServerOffer(offer, offerId = 7) + + verifyBlocking(publisher, never()) { setRemoteDescription(any(), any()) } + assertNull(publisherPc.remoteDesc) + assertNull(publisherPc.localDesc) + } + + // endregion + + // region applyServerAnswer + + @Test + fun applyServerAnswerAppliesToPublisher() = runTest { + val answer = 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 = answer() + + transport().applyServerAnswer(answer, offerId = 7, midToTrackId = emptyMap()) + + verifyBlocking(publisher) { setRemoteDescription(answer, 7) } + } + + @Test + fun applyServerAnswerFailsWhenClosed() = runTest { + publisher.close() + + val result = transport().applyServerAnswer(answer(), offerId = 7, midToTrackId = emptyMap()) + + assertTrue("$result", result is Either.Right) + } + + // endregion + + // region trackIdForReceiver + + /** + * Adds a receiving media section with [mid] to the publisher. + * + * @return the receiver of the media section. + */ + private fun receiverForMid(mid: String?, mediaType: MediaType = MediaType.MEDIA_TYPE_AUDIO): RtpReceiver { + val transceiver = publisherPc.addTransceiver( + mediaType, + RtpTransceiverInit(RtpTransceiverDirection.RECV_ONLY), + ) + whenever(transceiver.mid).thenReturn(mid) + return transceiver.receiver + } + + /** + * Like [receiverForMid], with a track on the receiver as a negotiated section has. + */ + private fun boundReceiverForMid(mid: String): RtpReceiver { + val receiver = receiverForMid(mid) + whenever(receiver.track()).thenReturn(mock()) + return receiver + } + + @Test + fun trackIdForReceiverIsNullBeforeAnswer() { + val receiver = receiverForMid("0") + + assertNull(transport().trackIdForReceiver(receiver)) + } + + @Test + fun trackIdForReceiverComesFromAnswer() = runTest { + val transport = transport() + val audioReceiver = receiverForMid("0") + val videoReceiver = receiverForMid("1", MediaType.MEDIA_TYPE_VIDEO) + + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a", "1" to "TR_b")) + + assertEquals("TR_a", transport.trackIdForReceiver(audioReceiver)) + assertEquals("TR_b", transport.trackIdForReceiver(videoReceiver)) + } + + @Test + fun trackIdForReceiverIsNullForUnmappedMid() = runTest { + val transport = transport() + val receiver = receiverForMid("2") + + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertNull(transport.trackIdForReceiver(receiver)) + } + + @Test + fun trackIdForReceiverIsNullWithoutMid() = runTest { + val transport = transport() + val receiver = receiverForMid(null) + + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertNull(transport.trackIdForReceiver(receiver)) + } + + @Test + fun trackIdForReceiverIsNullForUnknownReceiver() = runTest { + val transport = transport() + receiverForMid("0") + + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertNull(transport.trackIdForReceiver(MockRtpReceiver.create(id = "unknown"))) + } + + @Test + fun trackIdForReceiverIsNullWhenClosed() = runTest { + val transport = transport() + val receiver = receiverForMid("0") + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + publisher.close() + + assertNull(transport.trackIdForReceiver(receiver)) + } + + @Test + fun trackIdForReceiverKeepsMappingAcrossAnswerWithoutOne() = runTest { + val transport = transport() + val receiver = receiverForMid("0") + + transport.applyServerAnswer(answer("first"), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + transport.applyServerAnswer(answer("second"), offerId = 8, midToTrackId = emptyMap()) + + assertEquals("TR_a", transport.trackIdForReceiver(receiver)) + } + + @Test + fun trackIdForReceiverTakesLatestAnswerMapping() = runTest { + val transport = transport() + val receiver = receiverForMid("0") + + transport.applyServerAnswer(answer("first"), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + transport.applyServerAnswer(answer("second"), offerId = 8, midToTrackId = mapOf("0" to "TR_b")) + + assertEquals("TR_b", transport.trackIdForReceiver(receiver)) + } + + @Test + fun staleAnswerLeavesMappingUntouched() = runTest { + val transport = transport() + val receiver = receiverForMid("0") + transport.applyServerAnswer(answer("current"), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + doReturn(true).whenever(publisher).isStaleAnswer(6) + + val result = transport.applyServerAnswer(answer("stale"), offerId = 6, midToTrackId = mapOf("0" to "TR_b")) + + assertTrue("$result", result is Either.Right) + assertEquals("TR_a", transport.trackIdForReceiver(receiver)) + verifyBlocking(publisher, never()) { setRemoteDescription(any(), eq(6)) } + } + + // endregion + + // region bound tracks + + @Test + fun boundReceiverIsOfferedAfterAnswer() = runTest { + val receiver = boundReceiverForMid("0") + + transport().applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertEquals(listOf(receiver to "TR_a"), boundTracks) + } + + @Test + fun attachedBindingIsNotOfferedAgain() = runTest { + val transport = transport() + boundReceiverForMid("0") + + transport.applyServerAnswer(answer("first"), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + transport.applyServerAnswer(answer("second"), offerId = 8, midToTrackId = mapOf("0" to "TR_a")) + + assertEquals(1, boundTracks.size) + } + + @Test + fun reboundReceiverIsOfferedAgain() = runTest { + val transport = transport() + val receiver = boundReceiverForMid("0") + + transport.applyServerAnswer(answer("first"), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + transport.applyServerAnswer(answer("second"), offerId = 8, midToTrackId = mapOf("0" to "TR_b")) + + assertEquals(listOf(receiver to "TR_a", receiver to "TR_b"), boundTracks) + } + + @Test + fun unattachedBindingIsOfferedAfterNextAnswer() = runTest { + val transport = transport() + val receiver = boundReceiverForMid("0") + attachBoundTracks = false + + transport.applyServerAnswer(answer("first"), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + transport.applyServerAnswer(answer("second"), offerId = 8, midToTrackId = emptyMap()) + + assertEquals(listOf(receiver to "TR_a", receiver to "TR_a"), boundTracks) + } + + @Test + fun unattachedBindingIsOfferedOnRequest() = runTest { + val transport = transport() + val receiver = boundReceiverForMid("0") + attachBoundTracks = false + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + transport.attachBoundTracks() + + assertEquals(listOf(receiver to "TR_a", receiver to "TR_a"), boundTracks) + } + + @Test + fun attachedBindingIsNotOfferedOnRequest() = runTest { + val transport = transport() + boundReceiverForMid("0") + transport.applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + transport.attachBoundTracks() + + assertEquals(1, boundTracks.size) + } + + @Test + fun receiverWithoutTrackIsNotOffered() = runTest { + receiverForMid("0") + + transport().applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertTrue(boundTracks.isEmpty()) + } + + @Test + fun unmappedReceiverIsNotOffered() = runTest { + boundReceiverForMid("1") + + transport().applyServerAnswer(answer(), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertTrue(boundTracks.isEmpty()) + } + + @Test + fun failedAnswerOffersNothing() = runTest { + boundReceiverForMid("0") + + val result = transport().applyServerAnswer(answer(sdp = ""), offerId = 7, midToTrackId = mapOf("0" to "TR_a")) + + assertTrue("$result", result is Either.Right) + assertTrue(boundTracks.isEmpty()) + } + + // endregion + + // region handleMediaSectionsRequirement + + private fun requirement(audios: Int, videos: Int) = MediaSectionsRequirement.newBuilder() + .setNumAudios(audios) + .setNumVideos(videos) + .build() + + @Test + fun handleMediaSectionsRequirementAddsRequiredSections() = runTest { + transport().handleMediaSectionsRequirement(requirement(audios = 2, videos = 3)) + + val mediaTypes = publisherPc.transceivers.map { it.mediaType } + assertEquals(2, mediaTypes.count { it == MediaType.MEDIA_TYPE_AUDIO }) + assertEquals(3, mediaTypes.count { it == MediaType.MEDIA_TYPE_VIDEO }) + } + + @Test + fun handleMediaSectionsRequirementAddsReceiveOnlySections() = runTest { + transport().handleMediaSectionsRequirement(requirement(audios = 2, videos = 3)) + + assertTrue(publisherPc.transceivers.isNotEmpty()) + publisherPc.transceivers.forEach { transceiver -> + assertEquals(RtpTransceiverDirection.RECV_ONLY, transceiver.direction) + } + } + + @Test + fun handleMediaSectionsRequirementWithNoSectionsAddsNothing() = runTest { + transport().handleMediaSectionsRequirement(requirement(audios = 0, videos = 0)) + + assertTrue(publisherPc.transceivers.isEmpty()) + } + + @Test + fun handleMediaSectionsRequirementWhenClosedAddsNothing() = runTest { + publisher.close() + + transport().handleMediaSectionsRequirement(requirement(audios = 2, videos = 3)) + + assertTrue(publisherPc.transceivers.isEmpty()) + } + + // endregion + + // region resume + + @Test + fun prepareForIceRestartLeavesPublisherAlone() { + transport().prepareForIceRestart() + + verify(publisher, never()).prepareForIceRestart() + } + + @Test + fun updateRTCConfigAppliesToPublisher() { + val config = RTCConfiguration(emptyList()) + + transport().updateRTCConfig(config) + + assertSame(config, publisherPc.rtcConfig) + } + + @Test + fun isConnectedRequiresPublisher() = runTest { + assertFalse(transport().isConnected(includePublisher = false)) + assertFalse(transport().isConnected(includePublisher = true)) + } + + @Test + fun isConnectedWhenPublisherConnected() = runTest { + publisherPc.moveToIceConnectionState(IceConnectionState.CONNECTED) + + assertTrue(transport().isConnected(includePublisher = false)) + assertTrue(transport().isConnected(includePublisher = true)) + } + + @Test + fun waitUntilConnectedWaitsForPublisher() = runTest { + val transport = transport() + val job = launch { transport.waitUntilConnected(includePublisher = true) } + + runCurrent() + assertTrue(job.isActive) + + publisherState.connectionState = PeerConnectionState.CONNECTED + runCurrent() + assertTrue(job.isCompleted) + } + + @Test + fun waitUntilConnectedWaitsForPublisherWhenNotIncluded() = runTest { + val transport = transport() + val job = launch { transport.waitUntilConnected(includePublisher = false) } + + runCurrent() + assertTrue(job.isActive) + + publisherState.connectionState = PeerConnectionState.CONNECTED + runCurrent() + assertTrue(job.isCompleted) + } + + @Test + fun syncStateDescriptionsComeFromPublisher() = runTest { + val offer = SessionDescription(SessionDescription.Type.OFFER, "publisher_local") + val answer = SessionDescription(SessionDescription.Type.ANSWER, "publisher_remote") + publisherPc.localDesc = offer + publisherPc.remoteDesc = answer + + val descriptions = transport().syncStateDescriptions() + + assertEquals(answer.toProtoSessionDescription(), descriptions.answer) + assertEquals(offer.toProtoSessionDescription(), descriptions.offer) + } + + @Test + fun syncStateDescriptionsWhenPublisherClosed() = runTest { + publisherPc.localDesc = SessionDescription(SessionDescription.Type.OFFER, "publisher_local") + publisherPc.remoteDesc = SessionDescription(SessionDescription.Type.ANSWER, "publisher_remote") + publisher.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 closeClosesPublisher() = runTest { + transport().close() + + assertTrue(publisher.isClosed()) + } + + // endregion +} diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/util/SignalUrlBuilderTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/util/SignalUrlBuilderTest.kt new file mode 100644 index 000000000..f01d764d9 --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/util/SignalUrlBuilderTest.kt @@ -0,0 +1,354 @@ +/* + * 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.util + +import io.livekit.android.ConnectOptions +import io.livekit.android.RoomOptions +import io.livekit.android.stats.NetworkType +import io.livekit.android.stats.getClientInfo +import livekit.LivekitModels +import livekit.LivekitRtc +import okhttp3.HttpUrl +import okhttp3.HttpUrl.Companion.toHttpUrl +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertThrows +import org.junit.Assert.assertTrue +import org.junit.Test +import java.util.Base64 +import java.util.zip.GZIPInputStream + +class SignalUrlBuilderTest { + + private fun buildUrl( + url: String = URL, + connectOptions: ConnectOptions = ConnectOptions(), + roomOptions: RoomOptions = RoomOptions(adaptiveStream = true), + networkType: NetworkType? = null, + publisherOffer: LivekitRtc.SessionDescription? = null, + ): String = SignalUrlBuilder.buildJoinRequestUrl( + url = url, + connectOptions = connectOptions, + roomOptions = roomOptions, + networkType = networkType, + publisherOffer = publisherOffer, + ) + + /** Options as `SignalClient.reconnect` sets them. */ + private fun reconnectOptions(participantSid: String?) = ConnectOptions().apply { + reconnect = true + this.participantSid = participantSid + } + + /** OkHttp's [HttpUrl] only parses http(s), so ws(s) URLs are parsed with the equivalent scheme. */ + private fun parseWs(wsUrl: String): HttpUrl { + assertTrue("expected ws(s) scheme: $wsUrl", wsUrl.startsWith("ws://") || wsUrl.startsWith("wss://")) + return wsUrl.replaceFirst("ws", "http").toHttpUrl() + } + + /** The percent-encoded query is what actually goes on the wire. */ + private fun joinRequestParam(url: String): String { + val query = parseWs(url).encodedQuery!! + return query.split("&") + .first { it.startsWith("${SignalUrlBuilder.QUERY_JOIN_REQUEST}=") } + .removePrefix("${SignalUrlBuilder.QUERY_JOIN_REQUEST}=") + } + + private fun decodeWrapped(url: String): LivekitRtc.WrappedJoinRequest { + val bytes = Base64.getUrlDecoder().decode(joinRequestParam(url)) + return LivekitRtc.WrappedJoinRequest.parseFrom(bytes) + } + + private fun decodeJoinRequest(url: String): LivekitRtc.JoinRequest { + val wrapped = decodeWrapped(url) + 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) + } + + // region join_request encoding + + /** + * Standard base64 '+' and '/' are not safe in a query value: a receiver parsing the + * query as form-urlencoded turns '+' into a space. + */ + @Test + fun joinRequestParamIsUrlSafe() { + val param = joinRequestParam(buildUrl(publisherOffer = largeOffer())) + + assertFalse("Standard base64 leaked into the query: $param", param.contains("+")) + assertFalse("Standard base64 leaked into the query: $param", param.contains("/")) + } + + /** Padding is kept, matching client-sdk-js, rust-sdks and client-sdk-swift. */ + @Test + fun joinRequestParamKeepsPadding() { + val param = joinRequestParam(buildUrl()) + + assertEquals("Unpadded base64: $param", 0, param.length % 4) + } + + @Test + fun joinRequestRoundTrips() { + val request = decodeJoinRequest(buildUrl()) + + assertEquals(LivekitModels.ClientInfo.SDK.ANDROID, request.clientInfo.sdk) + assertEquals(getClientInfo().version, request.clientInfo.version) + assertTrue(request.connectionSettings.adaptiveStream) + assertFalse(request.reconnect) + assertEquals("", request.participantSid) + } + + @Test + fun reconnectJoinRequestRoundTrips() { + val request = decodeJoinRequest(buildUrl(connectOptions = reconnectOptions(participantSid = "PA_123"))) + + assertTrue(request.reconnect) + assertEquals(LivekitModels.ReconnectReason.RR_SIGNAL_DISCONNECTED, request.reconnectReason) + assertEquals("PA_123", request.participantSid) + } + + @Test + fun participantSidIsOnlySentOnReconnect() { + val options = ConnectOptions().apply { participantSid = "PA_123" } + val request = decodeJoinRequest(buildUrl(connectOptions = options)) + + assertFalse(request.reconnect) + assertEquals("", request.participantSid) + } + + @Test + fun joinRequestCarriesClientInfo() { + val options = ConnectOptions() + val request = decodeJoinRequest(buildUrl()) + + assertEquals(options.protocolVersion.value, request.clientInfo.protocol) + assertEquals(options.clientProtocol.value, request.clientInfo.clientProtocol) + assertEquals(getClientInfo().os, request.clientInfo.os) + } + + @Test + fun joinRequestCarriesConnectionSettings() { + val request = decodeJoinRequest( + buildUrl( + connectOptions = ConnectOptions(autoSubscribe = false), + roomOptions = RoomOptions(adaptiveStream = false), + ), + ) + + assertFalse(request.connectionSettings.autoSubscribe) + assertFalse(request.connectionSettings.adaptiveStream) + } + + @Test + fun joinRequestCarriesNetworkType() { + val request = decodeJoinRequest(buildUrl(networkType = NetworkType.CELLULAR)) + + assertEquals("cellular", request.clientInfo.network) + } + + @Test + fun networkTypeIsOmittedWhenUnknown() { + val request = decodeJoinRequest(buildUrl(networkType = null)) + + assertEquals("", request.clientInfo.network) + } + + @Test + fun joinRequestBundlesPublisherOffer() { + val offer = LivekitRtc.SessionDescription.newBuilder() + .setType("offer") + .setSdp("v=0\r\n") + .setId(7) + .build() + val request = decodeJoinRequest(buildUrl(publisherOffer = offer)) + + assertEquals(offer, request.publisherOffer) + } + + @Test + fun joinRequestHasNoPublisherOfferByDefault() { + val request = decodeJoinRequest(buildUrl()) + + assertFalse(request.hasPublisherOffer()) + } + + /** + * A minimal join request is small enough that gzip framing outweighs the + * savings, so it must stay uncompressed rather than growing the URL. + */ + @Test + fun smallRequestIsNotCompressed() { + val wrapped = decodeWrapped(buildUrl()) + + assertEquals(LivekitRtc.WrappedJoinRequest.Compression.NONE, wrapped.compression) + } + + @Test + fun largeRequestIsCompressed() { + val offer = largeOffer() + val wrapped = decodeWrapped(buildUrl(publisherOffer = offer)) + + assertEquals(LivekitRtc.WrappedJoinRequest.Compression.GZIP, wrapped.compression) + assertTrue(wrapped.joinRequest.size() < offer.serializedSize) + assertEquals(offer, decodeJoinRequest(buildUrl(publisherOffer = offer)).publisherOffer) + } + + // endregion + + // region URL + + @Test + fun onlyJoinRequestParam() { + val url = buildUrl(connectOptions = reconnectOptions(participantSid = "PA_123"), networkType = NetworkType.WIFI) + + assertEquals(setOf(SignalUrlBuilder.QUERY_JOIN_REQUEST), parseWs(url).queryParameterNames) + } + + @Test + fun httpsBecomesWss() { + val url = buildUrl(url = "https://example.livekit.cloud") + + assertTrue(url, url.startsWith("wss://example.livekit.cloud/rtc/v1?")) + } + + @Test + fun httpBecomesWs() { + val url = buildUrl(url = "http://localhost:7880") + + assertTrue(url, url.startsWith("ws://localhost:7880/rtc/v1?")) + } + + @Test + fun wssIsKept() { + val url = buildUrl(url = "wss://example.livekit.cloud") + + assertTrue(url, url.startsWith("wss://example.livekit.cloud/rtc/v1?")) + } + + @Test + fun trailingSlashIsHandled() { + assertEquals("/rtc/v1", parseWs(buildUrl(url = "wss://example.livekit.cloud/")).encodedPath) + } + + @Test + fun pathPrefixIsKept() { + assertEquals("/livekit/rtc/v1", parseWs(buildUrl(url = "wss://example.com/livekit")).encodedPath) + assertEquals("/livekit/rtc/v1", parseWs(buildUrl(url = "wss://example.com/livekit/")).encodedPath) + } + + @Test + fun trailingRtcSegmentIsReplaced() { + assertEquals("/rtc/v1", parseWs(buildUrl(url = "wss://example.livekit.cloud/rtc")).encodedPath) + assertEquals("/livekit/rtc/v1", parseWs(buildUrl(url = "wss://example.com/livekit/rtc")).encodedPath) + } + + @Test + fun trailingValidateSegmentIsReplaced() { + assertEquals("/rtc/v1", parseWs(buildUrl(url = "wss://example.livekit.cloud/validate")).encodedPath) + } + + @Test + fun hostNamedLikeSignalSegmentIsKept() { + val url = buildUrl(url = "ws://rtc:7880") + + assertTrue(url, url.startsWith("ws://rtc:7880/rtc/v1?")) + } + + @Test + fun baseUrlQueryIsDropped() { + val url = buildUrl(url = "wss://example.livekit.cloud/livekit?foo=bar") + + assertEquals("/livekit/rtc/v1", parseWs(url).encodedPath) + assertEquals(setOf(SignalUrlBuilder.QUERY_JOIN_REQUEST), parseWs(url).queryParameterNames) + } + + @Test + fun invalidUrlThrows() { + assertThrows(IllegalArgumentException::class.java) { + SignalUrlBuilder.buildJoinRequestUrl( + url = "not a url", + connectOptions = ConnectOptions(), + roomOptions = RoomOptions(), + ) + } + } + + // endregion + + // region toValidateUrl + + @Test + fun validateUrlForLegacyPath() { + val validate = SignalUrlBuilder.toValidateUrl("wss://example.livekit.cloud/rtc?protocol=13&sdk=android") + + assertEquals("https://example.livekit.cloud/rtc/validate?protocol=13&sdk=android", validate) + } + + @Test + fun validateUrlForV1Path() { + val url = buildUrl() + val validate = SignalUrlBuilder.toValidateUrl(url).toHttpUrl() + + assertEquals("https", validate.scheme) + assertEquals("/rtc/v1/validate", validate.encodedPath) + assertEquals(joinRequestParam(url), validate.encodedQuery!!.removePrefix("${SignalUrlBuilder.QUERY_JOIN_REQUEST}=")) + } + + @Test + fun validateUrlWsBecomesHttp() { + val validate = SignalUrlBuilder.toValidateUrl("ws://localhost:7880/rtc/v1?join_request=abc=") + + assertEquals("http://localhost:7880/rtc/v1/validate?join_request=abc=", validate) + } + + @Test + fun validateUrlHandlesTrailingSlash() { + val validate = SignalUrlBuilder.toValidateUrl("wss://example.livekit.cloud/rtc/") + + assertEquals("https://example.livekit.cloud/rtc/validate", validate) + } + + @Test + fun validateUrlInvalidUrlThrows() { + assertThrows(IllegalArgumentException::class.java) { + SignalUrlBuilder.toValidateUrl("not a url") + } + } + + // endregion + + companion object { + private const val URL = "wss://example.livekit.cloud" + + /** An SDP-sized offer: large enough to compress well and to need the full base64 alphabet. */ + private fun largeOffer(): LivekitRtc.SessionDescription { + val sdp = (0 until 40).joinToString("\r\n") { i -> + "a=candidate:$i 1 udp ${2122260223 - i} 192.168.1.${i % 255} ${50000 + i} typ host generation 0 ufrag ~?>+/" + } + return LivekitRtc.SessionDescription.newBuilder() + .setType("offer") + .setSdp(sdp) + .setId(1) + .build() + } + } +}