-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathConnectionManaging.swift
More file actions
56 lines (47 loc) · 1.85 KB
/
Copy pathConnectionManaging.swift
File metadata and controls
56 lines (47 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
import LiveKit
enum ConnectionManagerError: Error {
case notConnected
}
@MainActor
protocol ConnectionManaging: AnyObject {
var onEventReceived: (@Sendable (IncomingEvent) -> Void)? { get set }
var onDisconnected: (() async -> Void)? { get set }
var onStartupPhaseChange: ((StartupPhase) -> Void)? { get set }
func connect(auth: ConversationAuth, config: ConversationConfig) async throws
func disconnect() async
func send(data: Data) async throws
}
@MainActor
protocol WebSocketConnectionManaging: ConnectionManaging {}
@MainActor
protocol WebRTCConnectionManaging: ConnectionManaging {
var onRemoteSpeakingChanged: (@Sendable (Bool) -> Void)? { get set }
var inputTrack: LocalAudioTrack? { get }
var agentAudioTrack: RemoteAudioTrack? { get }
var isMicrophoneMuted: Bool { get }
func setMicrophoneMuted(_ muted: Bool) async throws
}
extension ConnectionManaging {
func handleIncomingData(_ data: Data, logger: any Logging) {
do {
if let event = try EventParser.parseIncomingEvent(from: data) {
onEventReceived?(event)
}
} catch let EventParseError.unknownEventType(type) {
// Unrecognized event types are expected (newer server) — not errors.
logger.debug("Ignoring unknown incoming event type", context: ["type": type])
} catch {
logger.error("Failed to parse incoming event", context: ["error": "\(error)"])
logger.debug("Incoming raw data bytes", context: ["bytes": "\(data.count)"])
}
}
func send(event: OutgoingEvent) async throws {
let data = try EventSerializer.serializeOutgoingEvent(event)
do {
try await send(data: data)
} catch ConnectionManagerError.notConnected {
throw ConversationError.notConnected
}
}
}