davidliu
Committed by GitHub

Add version to rpc requests (#605)

* Add version to rpc requests

* spotless
---
"client-sdk-android": patch
---
Add version number to rpc requests
... ...
... ... @@ -35,6 +35,7 @@ import io.livekit.android.room.RTCEngine
import io.livekit.android.room.Room
import io.livekit.android.room.TrackBitrateInfo
import io.livekit.android.room.isSVCCodec
import io.livekit.android.room.rpc.RpcManager
import io.livekit.android.room.track.DataPublishReliability
import io.livekit.android.room.track.LocalAudioTrack
import io.livekit.android.room.track.LocalAudioTrackOptions
... ... @@ -986,6 +987,7 @@ internal constructor(
this.method = method
this.payload = payload
this.responseTimeoutMs = responseTimeout.inWholeMilliseconds.toUInt().toInt()
this.version = RpcManager.RPC_VERSION
build()
}
build()
... ... @@ -1071,7 +1073,7 @@ internal constructor(
) {
publishRpcAck(callerIdentity, requestId)
if (version != 1) {
if (version != RpcManager.RPC_VERSION) {
publishRpcResponse(
destinationIdentity = callerIdentity,
requestId = requestId,
... ...
/*
* Copyright 2025 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.rpc
class RpcManager {
companion object {
const val RPC_VERSION = 1
}
}
... ...
... ... @@ -18,6 +18,7 @@ package io.livekit.android.room.participant
import com.google.protobuf.ByteString
import io.livekit.android.room.RTCEngine
import io.livekit.android.room.rpc.RpcManager
import io.livekit.android.rpc.RpcError
import io.livekit.android.test.MockE2ETest
import io.livekit.android.test.mock.MockDataChannel
... ... @@ -31,6 +32,7 @@ import kotlinx.coroutines.launch
import livekit.LivekitModels
import livekit.LivekitRtc
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
... ... @@ -147,6 +149,43 @@ class RpcMockE2ETest : MockE2ETest() {
}
@Test
fun handleRpcRequestWithNoVersion() = runTest {
connect()
var methodCalled = false
room.localParticipant.registerRpcMethod("hello") {
methodCalled = true
return@registerRpcMethod "hello back"
}
val noVersionRequest = with(TestData.DATA_PACKET_RPC_REQUEST.toBuilder()) {
rpcRequest = with(rpcRequest.toBuilder()) {
clearVersion()
build()
}
build()
}
subDataChannel.simulateBufferReceived(noVersionRequest.toDataChannelBuffer())
coroutineRule.dispatcher.scheduler.advanceUntilIdle()
assertFalse(methodCalled)
// Check that ack and response were sent
val buffers = pubDataChannel.sentBuffers
assertEquals(2, buffers.size)
val ackBuffer = LivekitModels.DataPacket.parseFrom(ByteString.copyFrom(buffers[0].data))
val responseBuffer = LivekitModels.DataPacket.parseFrom(ByteString.copyFrom(buffers[1].data))
assertTrue(ackBuffer.hasRpcAck())
assertEquals(TestData.DATA_PACKET_RPC_REQUEST.rpcRequest.id, ackBuffer.rpcAck.requestId)
assertTrue(responseBuffer.hasRpcResponse())
assertEquals(TestData.DATA_PACKET_RPC_REQUEST.rpcRequest.id, responseBuffer.rpcResponse.requestId)
assertEquals(RpcError.BuiltinRpcError.UNSUPPORTED_VERSION.create(), RpcError.fromProto(responseBuffer.rpcResponse.error))
}
@Test
fun handleRpcRequestWithNoHandler() = runTest {
connect()
... ... @@ -190,6 +229,7 @@ class RpcMockE2ETest : MockE2ETest() {
assertTrue(requestBuffer.hasRpcRequest())
assertEquals("hello", requestBuffer.rpcRequest.method)
assertEquals("hello world", requestBuffer.rpcRequest.payload)
assertEquals(RpcManager.RPC_VERSION, requestBuffer.rpcRequest.version)
val requestId = requestBuffer.rpcRequest.id
... ...