|
| 1 | +/* |
| 2 | + * Copyright 2026 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Foundation |
| 18 | +import LiveKit |
| 19 | + |
| 20 | +/// A second SDK instance that acts as an echo participant for |
| 21 | +/// data channel and RPC benchmarks. |
| 22 | +/// |
| 23 | +/// Connects to the same room and echoes: |
| 24 | +/// - Data channel messages back to the sender |
| 25 | +/// - RPC calls back with the same payload |
| 26 | +/// |
| 27 | +/// Records its own processing timestamps for overhead decomposition. |
| 28 | +final class EchoParticipant: Sendable { |
| 29 | + let room: Room |
| 30 | + |
| 31 | + /// Processing timestamps: (receive time, echo sent time) in microseconds |
| 32 | + /// relative to an arbitrary monotonic epoch. |
| 33 | + struct ProcessingTimestamp { |
| 34 | + let receiveUs: Int64 |
| 35 | + let echoSentUs: Int64 |
| 36 | + var overheadUs: Int64 { echoSentUs - receiveUs } |
| 37 | + } |
| 38 | + |
| 39 | + private let _timestamps = StateSync<[ProcessingTimestamp]>([]) |
| 40 | + // Strong reference to keep the delegate alive (MulticastDelegate uses weak references) |
| 41 | + private nonisolated(unsafe) var _echoDelegate: AnyObject? |
| 42 | + |
| 43 | + var processingTimestamps: [ProcessingTimestamp] { |
| 44 | + _timestamps.copy() |
| 45 | + } |
| 46 | + |
| 47 | + init() { |
| 48 | + room = Room() |
| 49 | + } |
| 50 | + |
| 51 | + /// Connect to the specified room and set up echo handlers. |
| 52 | + func connect(url: String, token: String) async throws { |
| 53 | + try await room.connect(url: url, token: token) |
| 54 | + } |
| 55 | + |
| 56 | + /// Register the echo RPC handler. |
| 57 | + /// |
| 58 | + /// - Parameter delay: Optional delay in nanoseconds to simulate processing (for BM-RPC-003) |
| 59 | + func registerEchoRpc(delay: UInt64 = 0) async throws { |
| 60 | + try await room.registerRpcMethod("echo") { data in |
| 61 | + if delay > 0 { |
| 62 | + try await Task.sleep(nanoseconds: delay) |
| 63 | + } |
| 64 | + return data.payload |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// Set up data echo handler that echoes received data back to the sender. |
| 69 | + func setupDataEcho() { |
| 70 | + let delegate = DataEchoDelegate(room: room, timestamps: _timestamps) |
| 71 | + _echoDelegate = delegate |
| 72 | + room.delegates.add(delegate: delegate) |
| 73 | + } |
| 74 | + |
| 75 | + func disconnect() async { |
| 76 | + await room.disconnect() |
| 77 | + } |
| 78 | + |
| 79 | + func clearTimestamps() { |
| 80 | + _timestamps.mutate { $0.removeAll() } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Delegate that echoes data channel messages. |
| 85 | +private final class DataEchoDelegate: NSObject, RoomDelegate, @unchecked Sendable { |
| 86 | + private let room: Room |
| 87 | + private let timestamps: StateSync<[EchoParticipant.ProcessingTimestamp]> |
| 88 | + |
| 89 | + init(room: Room, timestamps: StateSync<[EchoParticipant.ProcessingTimestamp]>) { |
| 90 | + self.room = room |
| 91 | + self.timestamps = timestamps |
| 92 | + super.init() |
| 93 | + } |
| 94 | + |
| 95 | + func room(_: Room, participant _: RemoteParticipant?, didReceiveData data: Data, forTopic topic: String, encryptionType _: EncryptionType) { |
| 96 | + let recvTime = Int64(ProcessInfo.processInfo.systemUptime * 1_000_000) |
| 97 | + Task { |
| 98 | + try? await room.localParticipant.publish( |
| 99 | + data: data, |
| 100 | + options: .init(topic: topic) |
| 101 | + ) |
| 102 | + let sentTime = Int64(ProcessInfo.processInfo.systemUptime * 1_000_000) |
| 103 | + timestamps.mutate { |
| 104 | + $0.append(EchoParticipant.ProcessingTimestamp( |
| 105 | + receiveUs: recvTime, |
| 106 | + echoSentUs: sentTime |
| 107 | + )) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments