Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ordered-fast-publication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"client-sdk-android": patch
---

Send track registration before creating the sender during fast publication, preserving overlap with server acceptance in both single and dual peer connection modes.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import io.livekit.android.util.flow
import io.livekit.android.util.rethrowIfCancellationSignal
import io.livekit.android.webrtc.sortVideoCodecPreferences
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
Expand Down Expand Up @@ -815,10 +816,9 @@ internal constructor(
// Can simultaneous publish and negotiate.
// codec is pre-verified in publishVideoTrack
trackInfo = coroutineScope {
val negotiateJob = launch { negotiate() }
val publishJob = async { requestAddTrack() }

negotiateJob.join()
// addTrack installs its waiter and sends the request before its first suspension.
val publishJob = async(start = CoroutineStart.UNDISPATCHED) { requestAddTrack() }
negotiate()
return@coroutineScope publishJob.await()
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.participant

import io.livekit.android.ConnectOptions
import io.livekit.android.room.util.SignalUrlBuilder
import io.livekit.android.test.MockE2ETest
import io.livekit.android.test.mock.TestData
import io.livekit.android.test.mock.room.track.createMockLocalAudioTrack
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runCurrent
import livekit.LivekitModels
import livekit.LivekitRtc
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.Base64
import java.util.zip.GZIPInputStream

@OptIn(ExperimentalCoroutinesApi::class)
class FastPublishMockE2ETest : MockE2ETest() {
@Test fun dualFast() = checkPublication(singlePC = false, fast = true)

@Test fun dualSequential() = checkPublication(singlePC = false, fast = false)

@Test fun singleFast() = checkPublication(singlePC = true, fast = true)

@Test fun singleSequential() = checkPublication(singlePC = true, fast = false)

private fun checkPublication(singlePC: Boolean, fast: Boolean) = runTest {
val connecting = launch {
room.connect(TestData.EXAMPLE_URL, "token", ConnectOptions(singlePeerConnection = singlePC))
}
prepareSignal()
connecting.join()
if (singlePC) {
val encoded = wsFactory.request.url.queryParameter(SignalUrlBuilder.QUERY_JOIN_REQUEST)
assertNotNull(encoded)
val wrapped = LivekitRtc.WrappedJoinRequest.parseFrom(Base64.getUrlDecoder().decode(encoded))
val bytes = if (wrapped.compression == LivekitRtc.WrappedJoinRequest.Compression.GZIP) {
GZIPInputStream(wrapped.joinRequest.newInput()).use { it.readBytes() }
} else {
wrapped.joinRequest.toByteArray()
}
val join = LivekitRtc.JoinRequest.parseFrom(bytes)
simulateMessageFromServer(
LivekitRtc.SignalResponse.newBuilder().setAnswer(
LivekitRtc.SessionDescription.newBuilder().setType("answer").setSdp("answer")
.setId(join.publisherOffer.id),
).build(),
)
runCurrent()
} else {
connectPeerConnection()
runCurrent()
}
room.localParticipant.setEnabledPublishCodecs(
if (fast) listOf(LivekitModels.Codec.newBuilder().setMime("video/VP8").build()) else emptyList(),
)
var add: LivekitRtc.AddTrackRequest? = null
wsFactory.registerSignalRequestHandler { request ->
if (request.hasAddTrack()) {
add = request.addTrack
// No sender may be created until registration has been sent.
assertNull(trackUnderTest?.transceiver)
true
} else {
false
}
}
val track = createMockLocalAudioTrack()
trackUnderTest = track
val publishing = async { room.localParticipant.publishAudioTrack(track) }
runCurrent()
assertNotNull(add)
assertFalse(publishing.isCompleted)
assertEquals(fast, track.transceiver != null)
assertTrue(room.localParticipant.trackPublications.isEmpty())
simulateMessageFromServer(
LivekitRtc.SignalResponse.newBuilder().setTrackPublished(
LivekitRtc.TrackPublishedResponse.newBuilder().setCid(requireNotNull(add).cid)
.setTrack(TestData.LOCAL_AUDIO_TRACK),
).build(),
)
runCurrent()
assertTrue(publishing.await())
assertNotNull(track.transceiver)
assertEquals(1, room.localParticipant.trackPublications.size)
}

private var trackUnderTest: io.livekit.android.room.track.LocalAudioTrack? = null
}
Loading