Skip to content

Commit 8317d31

Browse files
authored
Add local participant identity to all outgoing data packets (#602)
Fixes #601
1 parent 0440e22 commit 8317d31

File tree

6 files changed

+57
-13
lines changed

6 files changed

+57
-13
lines changed

.nanpa/data-packet-identity.kdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
minor type="fixed" "Outgoing data packets missing local participant identity"

Sources/LiveKit/Core/Room+Engine.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ extension Room {
102102
log("publisher data channel is not .open", .error)
103103
}
104104

105+
var packet = packet
106+
if let identity = localParticipant.identity?.stringValue {
107+
packet.participantIdentity = identity
108+
}
109+
105110
// Should return true if successful
106111
try publisherDataChannel.send(dataPacket: packet)
107112
}

Tests/LiveKitTests/Broadcast/IPCChannelTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ final class IPCChannelTests: LKTestCase {
9393
await fulfillment(of: [cancelThrowsError], timeout: 5.0)
9494
}
9595

96+
// swiftformat:disable redundantSelf hoistAwait
9697
func testConnectorCancelDuringInit() async throws {
9798
try await assertInitCancellationThrows(
9899
await IPCChannel(connectingTo: self.socketPath)
@@ -105,6 +106,8 @@ final class IPCChannelTests: LKTestCase {
105106
)
106107
}
107108

109+
// swiftformat:enable all
110+
108111
private struct TestHeader: Codable, Equatable {
109112
let someField: Int
110113
}

Tests/LiveKitTests/RoomTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ class RoomTests: LKTestCase {
4040
// Nothing to do here
4141
}
4242
}
43+
44+
func testSendDataPacket() async throws {
45+
try await withRooms([RoomTestingOptions()]) { rooms in
46+
let room = rooms[0]
47+
48+
let expectDataPacket = self.expectation(description: "Should send data packet")
49+
50+
let mockDataChannel = MockDataChannelPair { packet in
51+
XCTAssertEqual(packet.participantIdentity, room.localParticipant.identity?.stringValue ?? "")
52+
expectDataPacket.fulfill()
53+
}
54+
room.publisherDataChannel = mockDataChannel
55+
56+
try await room.send(dataPacket: Livekit_DataPacket())
57+
58+
await self.fulfillment(of: [expectDataPacket], timeout: 5)
59+
}
60+
}
4361
}
4462

4563
extension RoomTests: RoomDelegate {

Tests/LiveKitTests/RpcTests.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@
1818
import XCTest
1919

2020
class RpcTests: LKTestCase {
21-
// Mock DataChannelPair to intercept outgoing packets
22-
class MockDataChannelPair: DataChannelPair {
23-
var packetHandler: (Livekit_DataPacket) -> Void
24-
25-
init(packetHandler: @escaping (Livekit_DataPacket) -> Void) {
26-
self.packetHandler = packetHandler
27-
}
28-
29-
override func send(dataPacket packet: Livekit_DataPacket) throws {
30-
packetHandler(packet)
31-
}
32-
}
33-
3421
// Test performing RPC calls and verifying outgoing packets
3522
func testPerformRpc() async throws {
3623
try await withRooms([RoomTestingOptions()]) { rooms in
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2025 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+
@testable import LiveKit
18+
19+
/// Mock ``DataChannelPair`` to intercept outgoing packets.
20+
class MockDataChannelPair: DataChannelPair {
21+
var packetHandler: (Livekit_DataPacket) -> Void
22+
23+
init(packetHandler: @escaping (Livekit_DataPacket) -> Void) {
24+
self.packetHandler = packetHandler
25+
}
26+
27+
override func send(dataPacket packet: Livekit_DataPacket) throws {
28+
packetHandler(packet)
29+
}
30+
}

0 commit comments

Comments
 (0)