-
Notifications
You must be signed in to change notification settings - Fork 100
/
Room.swift
300 lines (259 loc) · 10 KB
/
Room.swift
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// File.swift
//
//
// Created by Russell D'Sa on 11/7/20.
//
import Foundation
import Network
import Promises
import WebRTC
enum RoomError: Error {
case missingRoomId(String)
case invalidURL(String)
case protocolError(String)
}
// network path discovery updates multiple times, causing us to disconnect again
// using a timer interval to ignore changes that are happening too close to each other
let networkChangeIgnoreInterval = 3.0
public class Room {
public typealias Sid = String
public var delegate: RoomDelegate?
public private(set) var sid: Room.Sid?
public private(set) var name: String?
public private(set) var state: RoomState = .disconnected
public private(set) var localParticipant: LocalParticipant?
public private(set) var remoteParticipants = [Participant.Sid: RemoteParticipant]()
public private(set) var activeSpeakers: [Participant] = []
private var connectOptions: ConnectOptions
private let monitor: NWPathMonitor
private let monitorQueue: DispatchQueue
private var prevPath: NWPath?
private var lastPathUpdate: TimeInterval = 0
internal var engine: RTCEngine
init(options: ConnectOptions) {
connectOptions = options
monitor = NWPathMonitor()
monitorQueue = DispatchQueue(label: "networkMonitor", qos: .background)
engine = RTCEngine(client: SignalClient())
monitor.pathUpdateHandler = { path in
logger.debug("network path update: \(path.availableInterfaces), \(path.status)")
if self.prevPath == nil || path.status != .satisfied {
self.prevPath = path
return
}
// In iOS 14.4, this update is sent multiple times during a connection change
// ICE restarts are expensive and error prone (due to renegotiation)
// We'll ignore frequent updates
let currTime = Date().timeIntervalSince1970
if currTime - self.lastPathUpdate < networkChangeIgnoreInterval {
logger.debug("skipping duplicate network update")
return
}
// trigger reconnect
if self.state != .disconnected {
logger.info("network path changed, starting engine reconnect")
self.reconnect()
}
self.prevPath = path
self.lastPathUpdate = currTime
}
engine.delegate = self
}
func connect() {
logger.info("connecting to room")
guard localParticipant == nil else {
return
}
state = .connecting
monitor.start(queue: monitorQueue)
engine.join(options: connectOptions)
}
public func disconnect() {
engine.client.sendLeave()
engine.close()
handleDisconnect()
}
func reconnect() {
if state != .connected && state != .reconnecting {
return
}
state = .reconnecting
engine.reconnect()
delegate?.isReconnecting(room: self)
}
private func handleParticipantDisconnect(sid: Participant.Sid, participant: RemoteParticipant) {
guard let participant = remoteParticipants.removeValue(forKey: sid) else {
return
}
participant.tracks.values.forEach { publication in
participant.unpublishTrack(sid: publication.sid)
}
delegate?.participantDidDisconnect(room: self, participant: participant)
}
private func getOrCreateRemoteParticipant(sid: Participant.Sid, info: Livekit_ParticipantInfo? = nil) -> RemoteParticipant {
if let participant = remoteParticipants[sid] {
return participant
}
let participant = RemoteParticipant(sid: sid, info: info)
participant.room = self // wire up to room delegate calls
remoteParticipants[sid] = participant
return participant
}
private func handleSpeakerUpdate(speakers: [Livekit_SpeakerInfo]) {
var activeSpeakers: [Participant] = []
var seenSids = [String: Bool]()
for speaker in speakers {
seenSids[speaker.sid] = true
if speaker.sid == localParticipant?.sid {
localParticipant?.audioLevel = speaker.level
localParticipant?.isSpeaking = true
activeSpeakers.append(localParticipant!)
} else {
if let participant = remoteParticipants[speaker.sid] {
participant.audioLevel = speaker.level
participant.isSpeaking = true
activeSpeakers.append(participant)
}
}
}
if let localParticipant = localParticipant, seenSids[localParticipant.sid] == nil {
localParticipant.audioLevel = 0.0
localParticipant.isSpeaking = false
}
for participant in remoteParticipants.values {
if seenSids[participant.sid] == nil {
participant.audioLevel = 0.0
participant.isSpeaking = false
}
}
self.activeSpeakers = activeSpeakers
delegate?.activeSpeakersDidChange(speakers: activeSpeakers, room: self)
}
private func handleDisconnect() {
if state == .disconnected {
// only allow cleanup to be completed once
return
}
logger.info("disconnected from room: \(self.name ?? "")")
state = .disconnected
// stop any tracks && release audio session
for participant in remoteParticipants.values {
for publication in participant.tracks.values {
guard let track = publication.track else {
continue
}
track.stop()
}
}
if let localParticipant = localParticipant {
for publication in localParticipant.tracks.values {
guard let track = publication.track else {
continue
}
track.stop()
}
}
remoteParticipants.removeAll()
activeSpeakers.removeAll()
monitor.cancel()
delegate?.didDisconnect(room: self, error: nil)
// should be the only call from delegate, room is done
delegate = nil
}
}
extension Room: RTCEngineDelegate {
func didUpdateSpeakers(speakers: [Livekit_SpeakerInfo]) {
handleSpeakerUpdate(speakers: speakers)
}
func didDisconnect() {
handleDisconnect()
}
func didJoin(response: Livekit_JoinResponse) {
logger.info("connected to room, server version: \(response.serverVersion)")
sid = response.room.sid
name = response.room.name
if response.hasParticipant {
localParticipant = LocalParticipant(fromInfo: response.participant, engine: engine, room: self)
}
if !response.otherParticipants.isEmpty {
for otherParticipant in response.otherParticipants {
_ = getOrCreateRemoteParticipant(sid: otherParticipant.sid, info: otherParticipant)
}
}
}
func ICEDidConnect() {
state = .connected
delegate?.didConnect(room: self)
}
func ICEDidReconnect() {
state = .connected
delegate?.didReconnect(room: self)
}
func didAddTrack(track: RTCMediaStreamTrack, streams: [RTCMediaStream]) {
guard streams.count > 0 else {
logger.error("received onTrack with no streams!")
return
}
let unpacked = unPackStreamId(streams[0].streamId)
let participantSid = unpacked.participantId
var trackSid = unpacked.trackId
if trackSid == "" {
trackSid = track.trackId
}
let participant = getOrCreateRemoteParticipant(sid: participantSid)
logger.debug("added media track from: \(participantSid), sid: \(trackSid)")
DispatchQueue.global(qos: .background).async {
// ensure audio session is configured
if track.kind == "audio" {
if !LiveKit.audioConfigured {
LiveKit.configureAudioSession()
}
}
participant.addSubscribedMediaTrack(rtcTrack: track, sid: trackSid)
}
}
func didUpdateParticipants(updates: [Livekit_ParticipantInfo]) {
for info in updates {
if info.sid == localParticipant?.sid {
localParticipant?.updateFromInfo(info: info)
continue
}
let isNewParticipant = remoteParticipants[info.sid] == nil
let participant = getOrCreateRemoteParticipant(sid: info.sid, info: info)
if info.state == .disconnected {
handleParticipantDisconnect(sid: info.sid, participant: participant)
} else if isNewParticipant {
delegate?.participantDidConnect(room: self, participant: participant)
} else {
participant.updateFromInfo(info: info)
}
}
}
func didReceive(packet: Livekit_UserPacket, kind _: Livekit_DataPacket.Kind) {
guard let participant = remoteParticipants[packet.participantSid] else {
logger.warning("could not find participant for data packet: \(packet.participantSid)")
return
}
delegate?.didReceive(data: packet.payload, participant: participant)
participant.delegate?.didReceive(data: packet.payload, participant: participant)
}
func remoteMuteDidChange(trackSid: String, muted: Bool) {
if let track = localParticipant?.tracks[trackSid] as? LocalTrackPublication {
track.setMuted(muted)
}
}
func didDisconnect(reason: String, code: UInt16) {
delegate?.didDisconnect(room: self, error: nil)
}
func didFailToConnect(error: Error) {
delegate?.didFailToConnect(room: self, error: error)
}
}
func unPackStreamId(_ streamId: String) -> (participantId: String, trackId: String) {
let parts = streamId.split(separator: "|")
if parts.count == 2 {
return (String(parts[0]), String(parts[1]))
}
return (streamId, "")
}