Skip to content

Commit 2530485

Browse files
authored
Extract agent components (#693)
1 parent 4b582a1 commit 2530485

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
import Foundation
18+
19+
public extension Participant {
20+
@objc
21+
var isAgent: Bool {
22+
switch kind {
23+
case .agent: return true
24+
default: return false
25+
}
26+
}
27+
28+
@objc
29+
var agentState: AgentState {
30+
guard isAgent else { return .unknown }
31+
guard let attrString = attributes[agentStateAttributeKey] else { return .unknown }
32+
guard let state = AgentState.fromString(attrString) else { return .unknown }
33+
return state
34+
}
35+
}
36+
37+
public extension Participant {
38+
private var publishingOnBehalf: [Participant.Identity: Participant] {
39+
guard let _room else { return [:] }
40+
return _room.allParticipants.filter { $0.value.attributes[publishOnBehalfAttributeKey] == identity?.stringValue }
41+
}
42+
43+
/// The avatar worker participant associated with the agent.
44+
@objc
45+
var avatarWorker: Participant? {
46+
publishingOnBehalf.values.first
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
import Foundation
18+
19+
let publishOnBehalfAttributeKey = "lk.publish_on_behalf"
20+
21+
public extension Room {
22+
/// A dictionary containing all agent participants.
23+
///
24+
/// - Note: This will not include participants that are publishing on behalf of another participant
25+
/// e.g. avatar workers. To access them directly use ``Participant/avatarWorker`` property of `agentParticipant`.
26+
@objc
27+
var agentParticipants: [Participant.Identity: Participant] {
28+
allParticipants.filter { $0.value.isAgent && $0.value.attributes[publishOnBehalfAttributeKey] == nil }
29+
}
30+
31+
/// The first agent participant.
32+
@objc
33+
var agentParticipant: Participant? {
34+
agentParticipants.values.first
35+
}
36+
}

Sources/LiveKit/Convenience/AudioProcessing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public extension Sequence where Iterator.Element == AudioLevel {
134134
}
135135
}
136136

137-
public class AudioVisualizeProcessor {
137+
public actor AudioVisualizeProcessor {
138138
static let bufferSize = 1024
139139

140140
// MARK: - Public

Sources/LiveKit/Participant/Participant.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ public class Participant: NSObject, @unchecked Sendable, ObservableObject, Logga
7575
_state.trackPublications.values.filter { $0.kind == .video }
7676
}
7777

78-
@objc
79-
public var agentState: AgentState {
80-
guard case .agent = kind else { return .unknown }
81-
guard let attrString = _state.attributes[agentStateAttributeKey] else { return .connecting }
82-
guard let state = AgentState.fromString(attrString) else { return .connecting }
83-
return state
84-
}
85-
8678
var info: Livekit_ParticipantInfo?
8779

8880
// Reference to the Room this Participant belongs to

0 commit comments

Comments
 (0)