diff --git a/.changeset/single-pc-offer-on-join.md b/.changeset/single-pc-offer-on-join.md new file mode 100644 index 000000000..f0626bbe0 --- /dev/null +++ b/.changeset/single-pc-offer-on-join.md @@ -0,0 +1,5 @@ +--- +"client-sdk-android": patch +--- + +Bundle the publisher offer with the `/rtc/v1` join request when connecting with `ConnectOptions.singlePeerConnection`. The publisher peer connection, its data channels and the first offer are created before the signal connection opens, and the server answers that offer right after the join response, which saves one offer/answer round trip on connect. diff --git a/livekit-android-sdk/detekt-baseline-release.xml b/livekit-android-sdk/detekt-baseline-release.xml index 3b35e4f99..2c0c7c89f 100644 --- a/livekit-android-sdk/detekt-baseline-release.xml +++ b/livekit-android-sdk/detekt-baseline-release.xml @@ -23,7 +23,7 @@ CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$@CheckResult internal suspend fun sendData(dataPacket: LivekitModels.DataPacket): Result<Unit> CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$@Synchronized @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) fun reconnect() CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$fun onMessage(dataChannel: DataChannel, buffer: DataChannel.Buffer?) - CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$private fun makeRTCConfig( serverResponse: Either<JoinResponse, ReconnectResponse>, connectOptions: ConnectOptions, ): RTCConfiguration + CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$private fun makeRTCConfig( serverResponse: Either<JoinResponse, ReconnectResponse>?, connectOptions: ConnectOptions, ): RTCConfiguration CyclomaticComplexMethod:RTCEngine.kt$RTCEngine$private suspend fun configure(joinResponse: JoinResponse, connectOptions: ConnectOptions) CyclomaticComplexMethod:Room.kt$Room$@Throws(Exception::class) suspend fun connect(url: String, token: String, options: ConnectOptions = ConnectOptions()) CyclomaticComplexMethod:RoomEvent.kt$fun LivekitModels.DisconnectReason?.convert(): DisconnectReason diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt index e7ff05ed9..1ae68446a 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/PeerConnectionTransport.kt @@ -114,6 +114,7 @@ constructor( private var isClosed = AtomicBoolean(false) private val latestOfferId = AtomicInteger(0) + private var initialOffer: InitialOffer? = null interface Listener { fun onOffer(sd: SessionDescription, offerId: Int) @@ -148,7 +149,11 @@ constructor( if (sd.type == SessionDescription.Type.ANSWER && isStaleAnswer(offerId)) { return@launchRTCIfNotClosed Either.Right("Old offer, ignoring. Expected: ${latestOfferId.get()}, actual: $offerId") } + initialOffer?.let { + peerConnection.setLocalDescription(it.sessionDescription) + } val result = peerConnection.setRemoteDescription(sd) + initialOffer = null if (result is Either.Left) { pendingCandidates.forEach { pending -> peerConnection.addIceCandidate(pending) @@ -167,6 +172,62 @@ constructor( return result } + /** + * An offer from [createInitialOffer]: the description to bundle with the join request, and + * the offer id the server's answer carries. + */ + data class InitialOffer( + val sessionDescription: SessionDescription, + val offerId: Int, + ) + + /** + * Creates the offer that is bundled with the join request instead of being sent through + * [Listener.onOffer]. + * + * The offer is created with [constraints] and the usual munging, or left unmunged if the + * munge leaves nothing. It takes the next offer id, but it is not set as the local + * description: that would start ICE gathering before the server's ICE servers are known. + * It is kept pending instead, and [setRemoteDescription] + * applies it as the local description just before the answer, whatever the answer's offer id. + * While it is pending, a [negotiate] request is queued the same way as when an offer is already + * in flight, and runs once the answer is applied. [clearPendingInitialOffer] and [close] drop it. + * + * @return the offer, or an error message if the signaling state is not stable, the offer + * could not be created, or the peer connection is closed. + */ + suspend fun createInitialOffer(constraints: MediaConstraints = MediaConstraints()): Either { + offerLock.withLock { + return launchRTCIfNotClosed> { + val signalingState = peerConnection.signalingState() + if (signalingState != SignalingState.STABLE) { + return@launchRTCIfNotClosed Either.Right("Signaling state is $signalingState, cannot create the initial offer") + } + val offerId = latestOfferId.incrementAndGet() + val munged = when (val outcome = createMungedOffer(constraints)) { + is Either.Left -> outcome.value + is Either.Right -> return@launchRTCIfNotClosed Either.Right(outcome.value) + } + val description = munged.description.ifBlank { munged.original.description } + val offer = InitialOffer(SessionDescription(munged.original.type, description), offerId) + initialOffer = offer + Either.Left(offer) + } ?: Either.Right("PCT is closed.") + } + } + + /** + * Drops the offer kept by [createInitialOffer], when the join it was bundled with did not + * succeed, so [negotiate] offers normally again. + */ + fun clearPendingInitialOffer() { + initialOffer = null + } + + fun hasPendingInitialOffer(): Boolean { + return initialOffer != null + } + val negotiate = debounce(20, coroutineScope) { if (it != null) { createAndSendOffer(it) @@ -194,7 +255,7 @@ constructor( restartingIce = true } - if (peerConnection.signalingState() == SignalingState.HAVE_LOCAL_OFFER) { + if (peerConnection.signalingState() == SignalingState.HAVE_LOCAL_OFFER || initialOffer != null) { // we're waiting for the peer to accept our offer, so we'll just wait // the only exception to this is when ICE restart is needed val curSd = peerConnection.remoteDescription @@ -215,7 +276,7 @@ constructor( // this may skip some ids, but is not an issue. offerId = latestOfferId.incrementAndGet() - val sdpOffer = when (val outcome = peerConnection.createOffer(constraints)) { + val munged = when (val outcome = createMungedOffer(constraints)) { is Either.Left -> outcome.value is Either.Right -> { LKLog.d { "error creating offer: ${outcome.value}" } @@ -223,60 +284,10 @@ constructor( } } - if (isClosed()) { - return@launchRTCIfNotClosed - } - - // WARP (draft-uberti-tsvwg-warp) shows up as a=goog-sped-v1 (DTLS handshake carried - // in the ICE exchange) and a=sctp-init (SCTP INIT params in the SDP, skipping the - // cookie exchange). libwebrtc only emits them when the acceleration is actually in - // use, so this reports what went on the wire. - LKLog.i { - "WARP: SPED ${if (sdpOffer.description.contains("goog-sped-v1")) "enabled" else "disabled"}, " + - "SNAP ${if (sdpOffer.description.contains("a=sctp-init")) "enabled" else "disabled"}" - } - - // munge sdp - val sdpDescription = sdpFactory.createSessionDescription(sdpOffer.description) - - val mediaDescs = sdpDescription.getMediaDescriptions(true) - .filterIsInstance() - // The publisher PeerConnection may negotiate before any video is published - // (for example, data channel only or audio first). Those offers should not - // consume the video start hint. When the first video offer is created, use - // one connection-level value across all video m-sections so libwebrtc's - // last-writer-wins handling cannot depend on SDP m-section order. - val setupCompleted = connectionSetupTime != null - val connectionSetupTime = connectionSetupTimeForOffer() - val connectionStartBitrate = if (!hasAppliedVideoStartBitrate) { - computeConnectionStartBitrate(mediaDescs, trackBitrates, connectionSetupTime) - } else { - null - } - if (connectionStartBitrate != null) { - LKLog.i { - "Applying x-google-start-bitrate=$connectionStartBitrate kbps " + - "(connection setup ${connectionSetupTime?.inWholeMilliseconds} ms" + - "${if (setupCompleted) "" else ", still connecting"})" - } - } - for (mediaDesc in mediaDescs) { - if (mediaDesc.media.mediaType == "audio") { - // TODO - } else if (mediaDesc.media.mediaType == "video") { - ensureVideoDDExtensionForSVC(mediaDesc) - ensureCodecBitrates( - mediaDesc, - trackBitrates = trackBitrates, - connectionStartBitrate = connectionStartBitrate, - ) - } - } - val mungedDescription = sdpDescription.toString() - finalSdp = setMungedSdp(sdpOffer, mungedDescription) + finalSdp = setMungedSdp(munged.original, munged.description) // setMungedSdp may fall back to the original SDP. Only mark the one-shot // hint as used after the SDP with the hint is accepted locally. - if (connectionStartBitrate != null && finalSdp?.description == mungedDescription) { + if (munged.connectionStartBitrate != null && finalSdp?.description == munged.description) { hasAppliedVideoStartBitrate = true } } @@ -296,6 +307,85 @@ constructor( } } + /** + * An offer from [createMungedOffer]. + * + * @param original the offer as libwebrtc created it, to fall back to if [description] is rejected. + * @param description the munged offer. + * @param connectionStartBitrate the start bitrate hint written into [description], if any. + */ + private class MungedOffer( + val original: SessionDescription, + val description: String, + val connectionStartBitrate: Long?, + ) + + /** + * Creates an offer with [constraints] and munges it. Runs on the RTC thread. + * + * The munge adds the dependency descriptor extension to SVC video sections and the + * connection-level start bitrate hint to video sections with a registered track bitrate. + * Writing the hint is one-shot per peer connection, so the caller marks + * [hasAppliedVideoStartBitrate] once libwebrtc has accepted [MungedOffer.description]. + * + * @return the offer, or an error message if it could not be created or the peer connection + * is closed. + */ + private suspend fun createMungedOffer(constraints: MediaConstraints): Either { + val sdpOffer = when (val outcome = peerConnection.createOffer(constraints)) { + is Either.Left -> outcome.value + is Either.Right -> return Either.Right(outcome.value) + } + + if (isClosed()) { + return Either.Right("PCT is closed.") + } + + // WARP (draft-uberti-tsvwg-warp) shows up as a=goog-sped-v1 (DTLS handshake carried + // in the ICE exchange) and a=sctp-init (SCTP INIT params in the SDP, skipping the + // cookie exchange). libwebrtc only emits them when the acceleration is actually in + // use, so this reports what went on the wire. + LKLog.i { + "WARP: SPED ${if (sdpOffer.description.contains("goog-sped-v1")) "enabled" else "disabled"}, " + + "SNAP ${if (sdpOffer.description.contains("a=sctp-init")) "enabled" else "disabled"}" + } + + val sdpDescription = sdpFactory.createSessionDescription(sdpOffer.description) + + val mediaDescs = sdpDescription.getMediaDescriptions(true) + .filterIsInstance() + // The publisher PeerConnection may negotiate before any video is published + // (for example, data channel only or audio first). Those offers should not + // consume the video start hint. When the first video offer is created, use + // one connection-level value across all video m-sections so libwebrtc's + // last-writer-wins handling cannot depend on SDP m-section order. + val setupCompleted = connectionSetupTime != null + val connectionSetupTime = connectionSetupTimeForOffer() + val connectionStartBitrate = if (!hasAppliedVideoStartBitrate) { + computeConnectionStartBitrate(mediaDescs, trackBitrates, connectionSetupTime) + } else { + null + } + if (connectionStartBitrate != null) { + LKLog.i { + "Applying x-google-start-bitrate=$connectionStartBitrate kbps " + + "(connection setup ${connectionSetupTime?.inWholeMilliseconds} ms" + + "${if (setupCompleted) "" else ", still connecting"})" + } + } + for (mediaDesc in mediaDescs) { + if (mediaDesc.media.mediaType == "video") { + ensureVideoDDExtensionForSVC(mediaDesc) + ensureCodecBitrates( + mediaDesc, + trackBitrates = trackBitrates, + connectionStartBitrate = connectionStartBitrate, + ) + } + } + return Either.Left(MungedOffer(sdpOffer, sdpDescription.toString(), connectionStartBitrate)) + } + private suspend fun setMungedSdp(sdp: SessionDescription, mungedDescription: String, remote: Boolean = false): SessionDescription { val mungedSdp = SessionDescription(sdp.type, mungedDescription) @@ -375,6 +465,7 @@ constructor( launchRTCIfNotClosed { isClosed.set(true) peerConnection.dispose() + initialOffer = null } coroutineScope.cancel() } diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt index c8d95b402..caf78fcf9 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt @@ -64,6 +64,7 @@ import io.livekit.android.webrtc.isDisconnected import io.livekit.android.webrtc.peerconnection.RTCThreadToken import io.livekit.android.webrtc.peerconnection.executeBlockingOnRTCThread import io.livekit.android.webrtc.peerconnection.launchBlockingOnRTCThread +import io.livekit.android.webrtc.toProtoSessionDescription import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob @@ -103,6 +104,7 @@ import javax.inject.Inject import javax.inject.Named import javax.inject.Singleton import kotlin.coroutines.Continuation +import kotlin.coroutines.coroutineContext import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.time.Duration.Companion.milliseconds @@ -208,6 +210,12 @@ internal constructor( private var publisherObserver: PublisherTransportObserver? = null private val subscriberObserver = SubscriberTransportObserver(this, client, rtcThreadToken) + // Publisher created ahead of a single peer connection join so its offer rides in the /rtc/v1 + // request. configure adopts it into the transport and clears it; until then close disposes + // it, as does the next join. + private var earlyPublisher: PeerConnectionTransport? = null + private var earlyPublisherObserver: DuplexTransportObserver? = null + internal var transport: Transport? = null private var reliableDataChannel: DataChannel? = null private var reliableDataChannelSub: DataChannel? = null @@ -299,8 +307,28 @@ internal constructor( connectionState = ConnectionState.CONNECTING } var effectiveOptions = options + if (effectiveOptions.singlePeerConnection) { + if (earlyPublisher != null) { + closeEarlyPublisher() + } + val observer = DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) + val publisher = pctFactory.create( + makeRTCConfig(null, effectiveOptions), + observer, + observer, + ) + earlyPublisherObserver = observer + earlyPublisher = publisher + createPublisherDataChannels(publisher) + } + + val offerValue = when (val earlyOffer = earlyPublisher?.createInitialOffer(getPublisherOfferConstraints())) { + is Either.Left -> earlyOffer.value.sessionDescription.toProtoSessionDescription(earlyOffer.value.offerId) + else -> null + } + val joinResponse = try { - client.join(url, token, options, roomOptions) + client.join(url, token, options, roomOptions, offerValue) } catch (e: RoomException.ConnectException) { // A server that predates /rtc/v1 answers that handshake with 404. if (!options.singlePeerConnection || e.statusCode != 404) { @@ -310,6 +338,7 @@ internal constructor( LKLog.w { "server has no /rtc/v1 path, retrying on the legacy path with dual peer connections" } effectiveOptions = options.copy(singlePeerConnection = false) connectOptions = effectiveOptions + closeEarlyPublisher() try { client.join(url, token, effectiveOptions, roomOptions) } catch (e: RoomException.ConnectException) { @@ -349,6 +378,7 @@ internal constructor( if (transport?.shouldNegotiatePublisherOnJoin(hasPublished) == true) { negotiatePublisher() } + client.onReadyForResponses() return@coroutineScope joinResponse @@ -365,11 +395,7 @@ internal constructor( } }, ) { - val publisherObserver = if (connectOptions.singlePeerConnection) { - DuplexTransportObserver(this@RTCEngine, client, rtcThreadToken) - } else { - PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) - } + val publisherObserver = earlyPublisherObserver ?: PublisherTransportObserver(this@RTCEngine, client, rtcThreadToken) this@RTCEngine.publisherObserver = publisherObserver participantSid = if (joinResponse.hasParticipant()) { @@ -380,7 +406,11 @@ internal constructor( // Setup peer connections val rtcConfig = makeRTCConfig(Either.Left(joinResponse), connectOptions) - val publisher = pctFactory.create( + + val publisher = earlyPublisher?.also { + it.updateRTCConfig(rtcConfig) + hasPublished = true + } ?: pctFactory.create( rtcConfig, publisherObserver, publisherObserver, @@ -441,45 +471,12 @@ internal constructor( } ensureActive() - // data channels - val reliableInit = DataChannel.Init() - reliableInit.ordered = true - reliableDataChannel = publisher.withPeerConnection { - createDataChannel( - RELIABLE_DATA_CHANNEL_LABEL, - reliableInit, - ).also { dataChannel -> - - val dataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) - reliableDataChannelManager = dataChannelManager - dataChannel.registerObserver(dataChannelManager) - reliableBufferedAmountJob?.cancel() - reliableBufferedAmountJob = coroutineScope.launch { - dataChannelManager::bufferedAmount.flow.collect { bufferedAmount -> - synchronized(reliableStateLock) { - reliableMessageBuffer.trim(bufferedAmount) - } - } - } - } + if (earlyPublisher == null) { + createPublisherDataChannels(publisher) } - - ensureActive() - val lossyInit = DataChannel.Init() - lossyInit.ordered = false - lossyInit.maxRetransmits = 0 - lossyDataChannel = publisher.withPeerConnection { - createDataChannel( - LOSSY_DATA_CHANNEL_LABEL, - lossyInit, - ).also { dataChannel -> - lossyDataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) - dataChannel.registerObserver(lossyDataChannelManager) - } - } - - ensureActive() - createPublisherDataTrackChannel() + // The transport owns the publisher from here on. + earlyPublisher = null + earlyPublisherObserver = null } } } @@ -553,6 +550,17 @@ internal constructor( fun close(reason: String = "Normal Closure") { if (isClosed) { + // A join that ends before its response leaves the engine closed with the early + // publisher still held. + if (earlyPublisher != null) { + executeBlockingOnRTCThread(rtcThreadToken) { + runBlocking { + configurationLock.withLock { + closeEarlyPublisher() + } + } + } + } return } LKLog.v { "Close - $reason" } @@ -590,21 +598,13 @@ internal constructor( subscriberObserver.connectionChangeListener = null transport?.close() transport = null - reliableBufferedAmountJob?.cancel() - reliableBufferedAmountJob = null - reliableDataChannelManager?.dispose() - reliableDataChannelManager = null - reliableDataChannel = null + closeEarlyPublisher() reliableDataChannelSubManager?.dispose() reliableDataChannelSubManager = null reliableDataChannelSub = null - lossyDataChannelManager?.dispose() - lossyDataChannelManager = null - lossyDataChannel = null lossyDataChannelSubManager?.dispose() lossyDataChannelSubManager = null lossyDataChannelSub = null - dataTrackPublisherChannel.detach() dataTrackDataChannelSub = null isSubscriberPrimary = false } @@ -1044,13 +1044,72 @@ internal constructor( } /** - * Creates the publisher `_data_track` channel and hands it to [dataTrackPublisherChannel]. + * Closes the early publisher when one is still unadopted, and the publisher's data channels + * with it. The 404 fallback calls this so the legacy join starts from nothing, a join calls + * it for the publisher of an earlier join that ended before its response, and close and + * closeResources call it when tearing the connection down. */ - private suspend fun createPublisherDataTrackChannel() { + private suspend fun closeEarlyPublisher() { + earlyPublisher?.close() + earlyPublisher = null + earlyPublisherObserver = null + reliableBufferedAmountJob?.cancel() + reliableBufferedAmountJob = null + reliableDataChannelManager?.dispose() + reliableDataChannelManager = null + reliableDataChannel = null + lossyDataChannelManager?.dispose() + lossyDataChannelManager = null + lossyDataChannel = null + dataTrackPublisherChannel.detach() + } + + /** + * Creates the publisher's three data channels on [publisher]: reliable, lossy, and the + * `_data_track` channel handed to [dataTrackPublisherChannel]. They are created before the + * publisher's first offer, so that offer carries their `m=application` section. + */ + private suspend fun createPublisherDataChannels(publisher: PeerConnectionTransport) { + val reliableInit = DataChannel.Init() + reliableInit.ordered = true + reliableDataChannel = publisher.withPeerConnection { + createDataChannel( + RELIABLE_DATA_CHANNEL_LABEL, + reliableInit, + ).also { dataChannel -> + val dataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) + reliableDataChannelManager = dataChannelManager + dataChannel.registerObserver(dataChannelManager) + reliableBufferedAmountJob?.cancel() + reliableBufferedAmountJob = coroutineScope.launch { + dataChannelManager::bufferedAmount.flow.collect { bufferedAmount -> + synchronized(reliableStateLock) { + reliableMessageBuffer.trim(bufferedAmount) + } + } + } + } + } + + coroutineContext.ensureActive() + val lossyInit = DataChannel.Init() + lossyInit.ordered = false + lossyInit.maxRetransmits = 0 + lossyDataChannel = publisher.withPeerConnection { + createDataChannel( + LOSSY_DATA_CHANNEL_LABEL, + lossyInit, + ).also { dataChannel -> + lossyDataChannelManager = DataChannelManager(dataChannel, DataChannelObserver(dataChannel), rtcThreadToken) + dataChannel.registerObserver(lossyDataChannelManager) + } + } + + coroutineContext.ensureActive() val dataTrackInit = DataChannel.Init() dataTrackInit.ordered = false dataTrackInit.maxRetransmits = 0 - transport?.publisher?.withPeerConnection { + publisher.withPeerConnection { createDataChannel( DATA_TRACK_DATA_CHANNEL_LABEL, dataTrackInit, @@ -1097,7 +1156,7 @@ internal constructor( } private fun makeRTCConfig( - serverResponse: Either, + serverResponse: Either?, connectOptions: ConnectOptions, ): RTCConfiguration { // Convert protobuf ice servers @@ -1106,12 +1165,13 @@ internal constructor( val responseServers = when (serverResponse) { is Either.Left -> serverResponse.value.iceServersList is Either.Right -> serverResponse.value.iceServersList + null -> emptyList() } for (serverInfo in responseServers) { servers.add(serverInfo.toWebrtc()) } - if (servers.isEmpty()) { + if (servers.isEmpty() && serverResponse != null) { servers.addAll(SignalClient.DEFAULT_ICE_SERVERS) } servers @@ -1163,6 +1223,8 @@ internal constructor( null } } + + null -> null } if (clientConfig != null) { if (clientConfig.forceRelay == LivekitModels.ClientConfigSetting.ENABLED) { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt index 875b52051..06d9062da 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/SignalClient.kt @@ -152,8 +152,9 @@ constructor( token: String, options: ConnectOptions = ConnectOptions(), roomOptions: RoomOptions = RoomOptions(), + publisherOffer: LivekitRtc.SessionDescription? = null ): JoinResponse { - return when (val result = connect(url, token, options, roomOptions)) { + return when (val result = connect(url, token, options, roomOptions, publisherOffer)) { is ConnectResult.Join -> result.response else -> throw IllegalStateException("Unexpected response during join: $result") } @@ -175,6 +176,7 @@ constructor( this.participantSid = participantSid }, lastRoomOptions ?: RoomOptions(), + null ) ) { is ConnectResult.Reconnect -> Either.Left(result.response) @@ -188,6 +190,8 @@ constructor( token: String, options: ConnectOptions, roomOptions: RoomOptions, + publisherOffer: LivekitRtc.SessionDescription?, + ): ConnectResult { // Clean up any pre-existing connection. close(reason = "Starting new connection", shouldClearQueuedRequests = false) @@ -196,7 +200,7 @@ constructor( isReconnecting = options.reconnect if (options.singlePeerConnection) { - wsUrlString = SignalUrlBuilder.buildJoinRequestUrl(wsUrlString, options, roomOptions, networkInfo.getNetworkType()) + wsUrlString = SignalUrlBuilder.buildJoinRequestUrl(wsUrlString, options, roomOptions, networkInfo.getNetworkType(), publisherOffer) // The join request carries client metadata, so only the path is logged. LKLog.i { "connecting to ${wsUrlString.substringBefore('?')}?${SignalUrlBuilder.QUERY_JOIN_REQUEST}=" } } else { diff --git a/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt b/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt index 03373b1ec..67040526b 100644 --- a/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt +++ b/livekit-android-sdk/src/main/java/io/livekit/android/room/SinglePeerConnectionTransport.kt @@ -61,7 +61,7 @@ internal class SinglePeerConnectionTransport( get() = emptyList() override fun shouldNegotiatePublisherOnJoin(hasPublished: Boolean): Boolean { - return true + return !publisher.hasPendingInitialOffer() } override fun addIceCandidate(candidate: IceCandidate, target: LivekitRtc.SignalTarget): Boolean { diff --git a/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt b/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt index b7375db1e..c3e3035b0 100644 --- a/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt +++ b/livekit-android-test/src/main/java/io/livekit/android/test/mock/MockPeerConnection.kt @@ -113,9 +113,14 @@ class MockPeerConnection( */ var lastOfferConstraints: MediaConstraints? = null + /** + * The description every [createOffer] returns. + */ + var offerDescription: String = "local_offer" + override fun createOffer(observer: SdpObserver?, constraints: MediaConstraints?) { lastOfferConstraints = constraints - val sdp = SessionDescription(SessionDescription.Type.OFFER, "local_offer") + val sdp = SessionDescription(SessionDescription.Type.OFFER, offerDescription) observer?.onCreateSuccess(sdp) } diff --git a/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt b/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt index 529b13dd2..70497e83c 100644 --- a/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt +++ b/livekit-android-test/src/main/java/livekit/org/webrtc/MockPeerConnectionFactory.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 LiveKit, Inc. + * Copyright 2023-2026 LiveKit, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,13 +23,18 @@ import io.livekit.android.test.mock.MockVideoSource import io.livekit.android.test.mock.MockVideoStreamTrack class MockPeerConnectionFactory : PeerConnectionFactory(1L) { + /** + * Every peer connection this factory has created, oldest first. + */ + val peerConnections = mutableListOf() + override fun createPeerConnectionInternal( rtcConfig: PeerConnection.RTCConfiguration, constraints: MediaConstraints?, observer: PeerConnection.Observer?, sslCertificateVerifier: SSLCertificateVerifier?, ): PeerConnection { - return MockPeerConnection(rtcConfig, observer) + return MockPeerConnection(rtcConfig, observer).also { peerConnections.add(it) } } override fun createAudioSource(constraints: MediaConstraints?): AudioSource { diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt new file mode 100644 index 000000000..8dc52cf3d --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/PeerConnectionTransportTest.kt @@ -0,0 +1,253 @@ +/* + * Copyright 2026 LiveKit, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.livekit.android.room + +import android.javax.sdp.SdpFactory +import io.livekit.android.room.util.MediaConstraintKeys +import io.livekit.android.room.util.findConstraint +import io.livekit.android.test.mock.MockPeerConnection +import io.livekit.android.test.mock.MockRTCThreadToken +import io.livekit.android.util.Either +import io.livekit.android.webrtc.JainSdpUtilsTest +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withTimeout +import livekit.org.webrtc.MediaConstraints +import livekit.org.webrtc.MockPeerConnectionFactory +import livekit.org.webrtc.PeerConnection +import livekit.org.webrtc.PeerConnection.RTCConfiguration +import livekit.org.webrtc.PeerConnection.SignalingState +import livekit.org.webrtc.SdpObserver +import livekit.org.webrtc.SessionDescription +import livekit.org.webrtc.WebRTCInitializer +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.mockito.kotlin.mock + +class PeerConnectionTransportTest { + + private class RecordingListener : PeerConnectionTransport.Listener { + val offers = mutableListOf>() + + override fun onOffer(sd: SessionDescription, offerId: Int) { + offers.add(sd to offerId) + } + } + + private val listener = RecordingListener() + private lateinit var transport: PeerConnectionTransport + + private val pc: MockPeerConnection + get() = transport.peerConnection as MockPeerConnection + + @Before + fun setup() { + WebRTCInitializer.initialize() + transport = PeerConnectionTransport( + config = RTCConfiguration(emptyList()), + pcObserver = mock(), + listener = listener, + ioDispatcher = Dispatchers.Unconfined, + connectionFactory = MockPeerConnectionFactory(), + sdpFactory = SdpFactory.getInstance(), + rtcThreadToken = MockRTCThreadToken(), + ) + // The initial offer is stored munged, which needs an offer that parses as SDP. + pc.offerDescription = JainSdpUtilsTest.DESCRIPTION + } + + private fun answer() = SessionDescription(SessionDescription.Type.ANSWER, "remote_answer") + + private suspend fun initialOffer(constraints: MediaConstraints = MediaConstraints()): PeerConnectionTransport.InitialOffer { + return when (val result = transport.createInitialOffer(constraints)) { + is Either.Left -> result.value + is Either.Right -> throw AssertionError("createInitialOffer failed: ${result.value}") + } + } + + /** + * [PeerConnectionTransport.negotiate] is debounced on the real clock, so offers are awaited + * rather than scheduled. + */ + private suspend fun awaitOffers(count: Int) { + withTimeout(OFFER_TIMEOUT_MS) { + while (listener.offers.size < count) { + delay(10) + } + } + } + + // region createInitialOffer + + @Test + fun initialOfferTakesTheFirstOfferId() = runTest { + val offer = initialOffer() + + assertEquals(1, offer.offerId) + assertEquals(SessionDescription.Type.OFFER, offer.sessionDescription.type) + assertTrue(offer.sessionDescription.description.isNotEmpty()) + } + + @Test + fun initialOfferUsesTheGivenConstraints() = runTest { + val constraints = MediaConstraints().apply { + mandatory.add(MediaConstraints.KeyValuePair(MediaConstraintKeys.OFFER_TO_RECV_AUDIO, MediaConstraintKeys.FALSE)) + } + + initialOffer(constraints) + + assertEquals( + MediaConstraintKeys.FALSE, + pc.lastOfferConstraints?.findConstraint(MediaConstraintKeys.OFFER_TO_RECV_AUDIO), + ) + } + + @Test + fun initialOfferIsMunged() = runTest { + pc.offerDescription = SdpMungingTest.NO_DD_DESCRIPTION + + val offer = initialOffer() + + assertTrue( + "AV1 section is missing the dependency descriptor extension", + offer.sessionDescription.description.contains(DD_EXTENSION_URI), + ) + } + + @Test + fun initialOfferIsUnmungedWhenTheMungeLeavesNothing() = runTest { + pc.offerDescription = UNPARSEABLE_OFFER + + val offer = initialOffer() + + assertEquals(UNPARSEABLE_OFFER, offer.sessionDescription.description) + } + + @Test + fun initialOfferIsNotSetAsLocalDescription() = runTest { + initialOffer() + + assertNull(pc.localDesc) + assertEquals(SignalingState.STABLE, pc.signalingState()) + } + + @Test + fun initialOfferIsNotSentToTheListener() = runBlocking { + initialOffer() + delay(OFFER_SETTLE_MS) + + assertTrue(listener.offers.isEmpty()) + } + + @Test + fun initialOfferFailsOutsideStableState() = runTest { + pc.setLocalDescription(NoopSdpObserver, SessionDescription(SessionDescription.Type.OFFER, "in_flight_offer")) + assertEquals(SignalingState.HAVE_LOCAL_OFFER, pc.signalingState()) + + assertTrue(transport.createInitialOffer() is Either.Right) + } + + @Test + fun initialOfferFailsWhenClosed() = runTest { + transport.close() + + assertTrue(transport.createInitialOffer() is Either.Right) + } + + // endregion + + // region answer + + @Test + fun answerAppliesThePendingOfferFirst() = runTest { + val offer = initialOffer() + + val result = transport.setRemoteDescription(answer(), offer.offerId) + + assertTrue(result is Either.Left) + assertEquals(offer.sessionDescription.description, pc.localDesc?.description) + assertEquals("remote_answer", pc.remoteDesc?.description) + assertEquals(SignalingState.STABLE, pc.signalingState()) + } + + @Test + fun answerWithoutAnOfferIdAppliesThePendingOffer() = runTest { + val offer = initialOffer() + + val result = transport.setRemoteDescription(answer(), 0) + + assertTrue(result is Either.Left) + assertEquals(offer.sessionDescription.description, pc.localDesc?.description) + } + + @Test + fun negotiationWaitsForThePendingOfferToBeAnswered() = runBlocking { + val offer = initialOffer() + + transport.negotiate(null) + delay(OFFER_SETTLE_MS) + assertTrue("offered over the pending initial offer", listener.offers.isEmpty()) + assertEquals(SignalingState.STABLE, pc.signalingState()) + + assertTrue(transport.setRemoteDescription(answer(), offer.offerId) is Either.Left) + + awaitOffers(1) + assertEquals(2, listener.offers.single().second) + assertEquals(SignalingState.HAVE_LOCAL_OFFER, pc.signalingState()) + } + + // endregion + + // region clearPendingInitialOffer + + @Test + fun clearingThePendingOfferResumesNegotiation() = runBlocking { + initialOffer() + transport.clearPendingInitialOffer() + + transport.negotiate(null) + + awaitOffers(1) + assertEquals(2, listener.offers.single().second) + assertEquals(SignalingState.HAVE_LOCAL_OFFER, pc.signalingState()) + } + + // endregion + + private object NoopSdpObserver : SdpObserver { + override fun onCreateSuccess(sdp: SessionDescription?) {} + override fun onSetSuccess() {} + override fun onCreateFailure(error: String?) {} + override fun onSetFailure(error: String?) {} + } + + companion object { + private const val OFFER_TIMEOUT_MS = 2_000L + private const val DD_EXTENSION_URI = "https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension" + + /** Comfortably longer than the negotiate debounce. */ + private const val OFFER_SETTLE_MS = 200L + + /** Not SDP, so munging it leaves an empty description. */ + private const val UNPARSEABLE_OFFER = "local_offer" + } +} diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt new file mode 100644 index 000000000..24224b99d --- /dev/null +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionFailedJoinMockE2ETest.kt @@ -0,0 +1,100 @@ +/* + * Copyright 2026 LiveKit, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.livekit.android.room + +import io.livekit.android.ConnectOptions +import io.livekit.android.RoomOptions +import io.livekit.android.test.MockE2ETest +import io.livekit.android.test.mock.MockPeerConnection +import io.livekit.android.test.mock.TestData +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runCurrent +import livekit.org.webrtc.MockPeerConnectionFactory +import livekit.org.webrtc.PeerConnection.IceConnectionState +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import java.io.IOException + +/** + * Connecting with [ConnectOptions.singlePeerConnection] when the `/rtc/v1` join is rejected: the + * publisher created ahead of the join does not outlive it. + */ +@ExperimentalCoroutinesApi +@RunWith(RobolectricTestRunner::class) +class SinglePeerConnectionFailedJoinMockE2ETest : MockE2ETest() { + + private val options = ConnectOptions(singlePeerConnection = true) + + private fun peerConnections(): List { + return (component.peerConnectionFactory() as MockPeerConnectionFactory).peerConnections + } + + /** + * Answers the pending `/rtc/v1` handshake with 403 and waits for [join] to end. + */ + private suspend fun TestScope.rejectJoin(join: Job) { + runCurrent() + val request = wsFactory.request + assertEquals("/rtc/v1", request.url.encodedPath) + wsFactory.listener.onFailure(wsFactory.ws, IOException("403"), createFailedHandshakeResponse(request, 403)) + join.join() + } + + @Test + fun failedJoinClosesTheEarlyPublisher() = runTest { + var failure: Throwable? = null + val connectJob = coroutineRule.scope.launch { + failure = runCatching { room.connect(TestData.EXAMPLE_URL, "token", options) }.exceptionOrNull() + } + + rejectJoin(connectJob) + + assertTrue("expected the join to fail, got $failure", failure is RoomException.ConnectException) + assertEquals(Room.State.DISCONNECTED, room.state) + assertEquals(IceConnectionState.CLOSED, peerConnections().single().iceConnectionState()) + } + + @Test + fun joinClosesTheEarlyPublisherOfAFailedJoin() = runTest { + val engine = component.rtcEngine() + val firstJoin = coroutineRule.scope.launch { + runCatching { engine.join(TestData.EXAMPLE_URL, "token", options, RoomOptions()) } + } + rejectJoin(firstJoin) + val firstPublisher = peerConnections().single() + assertNotEquals(IceConnectionState.CLOSED, firstPublisher.iceConnectionState()) + + val secondJoin = coroutineRule.scope.launch { + runCatching { engine.join(TestData.EXAMPLE_URL, "token", options, RoomOptions()) } + } + runCurrent() + + assertEquals(2, peerConnections().size) + assertEquals(IceConnectionState.CLOSED, firstPublisher.iceConnectionState()) + + rejectJoin(secondJoin) + engine.close() + assertEquals(IceConnectionState.CLOSED, peerConnections().last().iceConnectionState()) + } +} diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt index 1debb049d..a9005c2dc 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionMockE2ETest.kt @@ -45,6 +45,8 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.whenever import org.robolectric.RobolectricTestRunner +import java.util.Base64 +import java.util.zip.GZIPInputStream /** * Connecting with [ConnectOptions.singlePeerConnection] to a server that serves `/rtc/v1`: @@ -58,7 +60,23 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { private fun publisherPeerConnection() = getPublisherPeerConnection() /** - * @return the offers the client has sent to the server, oldest first. + * @return the publisher offer bundled with the `/rtc/v1` join request. + */ + private fun joinOffer(): LivekitRtc.SessionDescription { + val param = wsFactory.request.url.queryParameter(SignalUrlBuilder.QUERY_JOIN_REQUEST) + val wrapped = LivekitRtc.WrappedJoinRequest.parseFrom(Base64.getUrlDecoder().decode(param)) + val joinRequestBytes = when (wrapped.compression) { + LivekitRtc.WrappedJoinRequest.Compression.GZIP -> + GZIPInputStream(wrapped.joinRequest.newInput()).use { it.readBytes() } + + else -> wrapped.joinRequest.toByteArray() + } + return LivekitRtc.JoinRequest.parseFrom(joinRequestBytes).publisherOffer + } + + /** + * @return the offers the client has sent over the signal connection, oldest first. The + * offer bundled with the join request is not one of them. */ private fun sentOffers(): List { return wsFactory.ws.sentRequests @@ -68,17 +86,20 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { } /** - * Answers the client's most recent offer, with [midToTrackId] as the answer's mapping of - * receiving media sections to track IDs. + * Answers the client's most recent offer sent over the signal connection, with + * [midToTrackId] as the answer's mapping of receiving media sections to track IDs. */ private fun TestScope.answerLastOffer(midToTrackId: Map = emptyMap()) { - val offer = sentOffers().last() + answerOffer(sentOffers().last().id, midToTrackId) + } + + private fun TestScope.answerOffer(offerId: Int, midToTrackId: Map = emptyMap()) { val answer = with(LivekitRtc.SignalResponse.newBuilder()) { setAnswer( with(LivekitRtc.SessionDescription.newBuilder()) { type = "answer" sdp = "remote_answer" - id = offer.id + id = offerId putAllMidToTrackId(midToTrackId) build() }, @@ -90,10 +111,9 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { } /** - * Joins through `/rtc/v1`, answers the offer the join triggers, and connects the peer - * connection. + * Joins through `/rtc/v1` and leaves the offer bundled with the join request unanswered. */ - private suspend fun TestScope.connectSinglePeerConnection() { + private suspend fun TestScope.joinSinglePeerConnection() { val connectJob = coroutineRule.scope.launch { room.connect( url = TestData.EXAMPLE_URL, @@ -104,8 +124,16 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { prepareSignal() connectJob.join() advanceUntilIdle() + } - answerLastOffer() + /** + * Joins through `/rtc/v1`, answers the offer bundled with the join request, and connects + * the peer connection. + */ + private suspend fun TestScope.connectSinglePeerConnection() { + joinSinglePeerConnection() + + answerOffer(joinOffer().id) publisherPeerConnection().moveToIceConnectionState(PeerConnection.IceConnectionState.CONNECTED) runCurrent() } @@ -145,6 +173,22 @@ class SinglePeerConnectionMockE2ETest : MockE2ETest() { assertTrue(events.none { it is RoomEvent.FailedToConnect }) } + @Test + fun publisherOfferRidesInTheJoinRequest() = runTest { + joinSinglePeerConnection() + + val offer = joinOffer() + assertEquals("offer", offer.type) + assertEquals(1, offer.id) + assertTrue(offer.sdp.isNotEmpty()) + assertTrue("no offer is expected over the signal connection", sentOffers().isEmpty()) + + answerOffer(offer.id) + + assertEquals(offer.sdp, publisherPeerConnection().localDescription?.description) + assertTrue("the answered join offer must not be offered again", sentOffers().isEmpty()) + } + @Test fun publisherOffersOnJoinWithoutOfferingToReceive() = runTest { connectSinglePeerConnection() diff --git a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt index e38be5953..ce1501ba8 100644 --- a/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt +++ b/livekit-android-test/src/test/java/io/livekit/android/room/SinglePeerConnectionTransportTest.kt @@ -137,6 +137,14 @@ class SinglePeerConnectionTransportTest { assertTrue(transport().shouldNegotiatePublisherOnJoin(hasPublished = true)) } + @Test + fun publisherIsNotNegotiatedOnJoinWhileItsInitialOfferIsPending() { + whenever(publisher.hasPendingInitialOffer()).thenReturn(true) + + assertFalse(transport().shouldNegotiatePublisherOnJoin(hasPublished = false)) + assertFalse(transport().shouldNegotiatePublisherOnJoin(hasPublished = true)) + } + // endregion // region addIceCandidate