-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathWebRTCStateAdapter.swift
More file actions
888 lines (783 loc) · 32.7 KB
/
WebRTCStateAdapter.swift
File metadata and controls
888 lines (783 loc) · 32.7 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//
// Copyright © 2026 Stream.io Inc. All rights reserved.
//
import Combine
import Foundation
import StreamWebRTC
/// An actor class that handles WebRTC state management and media tracks for a
/// video call. This class manages the connection setup, track handling, and
/// participants, including their media settings, capabilities, and track
/// updates.
actor WebRTCStateAdapter: ObservableObject, StreamAudioSessionAdapterDelegate, WebRTCPermissionsAdapterDelegate {
typealias ParticipantsStorage = [String: CallParticipant]
typealias ParticipantOperation = @Sendable (ParticipantsStorage) -> ParticipantsStorage
/// Enum representing different types of media tracks.
enum TrackEntry {
case audio(id: String, track: RTCAudioTrack)
case video(id: String, track: RTCVideoTrack)
case screenShare(id: String, track: RTCVideoTrack)
/// Returns the ID associated with the track entry.
var id: String {
switch self {
case let .audio(id, _):
return id
case let .video(id, _):
return id
case let .screenShare(id, _):
return id
}
}
}
@Injected(\.permissions) private var permissions
@Injected(\.audioStore) private var audioStore
// Properties for user, API key, call ID, video configuration, and factories.
let unifiedSessionId: String = UUID().uuidString
let user: User
let apiKey: String
let callCid: String
let videoConfig: VideoConfig
let peerConnectionFactory: PeerConnectionFactory
let videoCaptureSessionProvider: VideoCaptureSessionProvider
let screenShareSessionProvider: ScreenShareSessionProvider
let audioSession: CallAudioSession
let trackStorage: WebRTCTrackStorage = .init()
/// Published properties that represent different parts of the WebRTC state.
@Published private(set) var sessionID: String = UUID().uuidString
@Published private(set) var token: String = ""
@Published private(set) var callSettings: CallSettings
@Published private(set) var audioSettings: AudioSettings = .init()
/// Published property to track video options and update them.
@Published private(set) var videoOptions: VideoOptions = .init() {
didSet { didUpdate(videoOptions: videoOptions) }
}
/// Published property to track publish options and update them.
@Published private(set) var publishOptions: PublishOptions = .init() {
didSet { didUpdate(publishOptions: publishOptions) }
}
@Published private(set) var connectOptions: ConnectOptions = .init(iceServers: [])
@Published private(set) var ownCapabilities: Set<OwnCapability> = []
@Published private(set) var sfuAdapter: SFUAdapter?
@Published private(set) var publisher: RTCPeerConnectionCoordinator?
@Published private(set) var subscriber: RTCPeerConnectionCoordinator?
@Published private(set) var statsCollector: WebRTCStatsCollector?
@Published private(set) var statsAdapter: WebRTCStatsAdapting?
@Published private(set) var participants: ParticipantsStorage = [:]
@Published private(set) var participantsCount: UInt32 = 0
@Published private(set) var anonymousCount: UInt32 = 0
@Published private(set) var participantPins: [PinInfo] = []
@Published private(set) var incomingVideoQualitySettings: IncomingVideoQualitySettings = .none
@Published private(set) var isTracingEnabled: Bool = false
private(set) var clientCapabilities: Set<ClientCapability> = [
.subscriberVideoPause
]
// Various private and internal properties.
private(set) var initialCallSettings: CallSettings?
private var videoFilter: VideoFilter?
private let rtcPeerConnectionCoordinatorFactory: RTCPeerConnectionCoordinatorProviding
private let disposableBag = DisposableBag()
private let peerConnectionsDisposableBag = DisposableBag()
private let processingQueue = OperationQueue(maxConcurrentOperationCount: 1)
private let callSettingsProcessingQueue = OperationQueue(maxConcurrentOperationCount: 1)
private var queuedTraces: ConsumableBucket<WebRTCTrace> = .init()
private lazy var permissionsAdapter: WebRTCPermissionsAdapter = .init(self)
/// Initializes the WebRTC state adapter with user details and connection
/// configurations.
///
/// - Parameters:
/// - user: The user participating in the call.
/// - apiKey: The API key for authenticating WebRTC calls.
/// - callCid: The call identifier (callCid).
/// - videoConfig: Configuration for video settings.
/// - rtcPeerConnectionCoordinatorFactory: Factory for peer connection
/// creation.
/// - videoCaptureSessionProvider: Provides sessions for video capturing.
/// - screenShareSessionProvider: Provides sessions for screen sharing.
init(
user: User,
apiKey: String,
callCid: String,
videoConfig: VideoConfig,
callSettings: CallSettings,
rtcPeerConnectionCoordinatorFactory: RTCPeerConnectionCoordinatorProviding,
videoCaptureSessionProvider: VideoCaptureSessionProvider = .init(),
screenShareSessionProvider: ScreenShareSessionProvider = .init()
) {
self.init(
user: user,
apiKey: apiKey,
callCid: callCid,
videoConfig: videoConfig,
callSettings: callSettings,
peerConnectionFactory: PeerConnectionFactory.build(
audioProcessingModule: videoConfig.audioProcessingModule
),
rtcPeerConnectionCoordinatorFactory: rtcPeerConnectionCoordinatorFactory,
videoCaptureSessionProvider: videoCaptureSessionProvider,
screenShareSessionProvider: screenShareSessionProvider
)
}
/// Initializes the WebRTC state adapter with user details and connection
/// configurations.
///
/// - Parameters:
/// - user: The user participating in the call.
/// - apiKey: The API key for authenticating WebRTC calls.
/// - callCid: The call identifier (callCid).
/// - videoConfig: Configuration for video settings.
/// - peerConnectionFactory: The factory to use when constructing peerConnection and for the
/// audioSession..
/// - rtcPeerConnectionCoordinatorFactory: Factory for peer connection
/// creation.
/// - videoCaptureSessionProvider: Provides sessions for video capturing.
/// - screenShareSessionProvider: Provides sessions for screen sharing.
init(
user: User,
apiKey: String,
callCid: String,
videoConfig: VideoConfig,
callSettings: CallSettings,
peerConnectionFactory: PeerConnectionFactory,
rtcPeerConnectionCoordinatorFactory: RTCPeerConnectionCoordinatorProviding,
videoCaptureSessionProvider: VideoCaptureSessionProvider = .init(),
screenShareSessionProvider: ScreenShareSessionProvider = .init()
) {
self.user = user
self.apiKey = apiKey
self.callCid = callCid
self.videoConfig = videoConfig
self._callSettings = .init(initialValue: callSettings)
let peerConnectionFactory = peerConnectionFactory
self.peerConnectionFactory = peerConnectionFactory
self.rtcPeerConnectionCoordinatorFactory = rtcPeerConnectionCoordinatorFactory
self.videoCaptureSessionProvider = videoCaptureSessionProvider
self.screenShareSessionProvider = screenShareSessionProvider
self.audioSession = .init()
peerConnectionFactory
.setFrameBufferPolicy(
InjectedValues[\.videoRenderingOptions].bufferPolicy
)
log.debug(
"Active videoRenderingOptions: \(InjectedValues[\.videoRenderingOptions])",
subsystems: .webRTC
)
Task { [weak self] in
_ = await self?.permissionsAdapter
await self?.configureBatteryObservation()
}
}
/// Sets the session ID.
func set(sessionID value: String) {
self.sessionID = value
}
/// Sets the call settings.
private func set(
callSettings value: CallSettings,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line
) { self.callSettings = value }
/// Sets the initial call settings.
func set(initialCallSettings value: CallSettings?) { self.initialCallSettings = value }
/// Sets the audio settings.
func set(audioSettings value: AudioSettings) { self.audioSettings = value }
/// Sets the video options.
func set(videoOptions value: VideoOptions) { self.videoOptions = value }
/// Sets the publish options.
func set(publishOptions value: PublishOptions) { self.publishOptions = value }
/// Sets the connection options.
func set(connectOptions value: ConnectOptions) { self.connectOptions = value }
/// Sets the own capabilities of the current user.
private func set(ownCapabilities value: Set<OwnCapability>) { self.ownCapabilities = value }
/// Sets the WebRTC stats reporter.
func set(statsAdapter value: WebRTCStatsAdapting?) {
self.statsAdapter = value
value?.consume(queuedTraces)
}
/// Sets the SFU (Selective Forwarding Unit) adapter and updates the stats
/// reporter.
func set(sfuAdapter value: SFUAdapter?) {
self.sfuAdapter = value
statsAdapter?.sfuAdapter = value
}
/// Sets the number of participants in the call.
func set(participantsCount value: UInt32) { self.participantsCount = value }
/// Sets the anonymous participant count.
func set(anonymousCount value: UInt32) { self.anonymousCount = value }
/// Sets the participant pins.
func set(participantPins value: [PinInfo]) { self.participantPins = value }
/// Sets the token for the session.
func set(token value: String) { self.token = value }
/// Sets the video filter and applies it to the publisher.
func set(videoFilter value: VideoFilter?) {
videoFilter = value
publisher?.setVideoFilter(value)
}
/// Sets the manual trackSize that will be used when updating subscriptions with the SFU.
func set(incomingVideoQualitySettings value: IncomingVideoQualitySettings) {
self.incomingVideoQualitySettings = value
}
func set(isTracingEnabled value: Bool) {
self.isTracingEnabled = value
statsAdapter?.isTracingEnabled = value
}
func set(audioSessionPolicy: AudioSessionPolicy) {
audioSession.didUpdatePolicy(
audioSessionPolicy,
callSettings: callSettings,
ownCapabilities: ownCapabilities
)
}
// MARK: - Client Capabilities
func enableClientCapabilities(_ capabilities: Set<ClientCapability>) {
self.clientCapabilities = self.clientCapabilities.union(capabilities)
}
func disableClientCapabilities(_ capabilities: Set<ClientCapability>) {
self.clientCapabilities = self.clientCapabilities.subtracting(capabilities)
}
// MARK: - Session Management
/// Refreshes the session by setting a new session ID.
func refreshSession() {
set(sessionID: UUID().uuidString)
}
/// Configures the peer connections for the session.
///
/// - Throws: Throws an error if the SFU adapter is not set or other
/// connection setup fails.
func configurePeerConnections() async throws {
guard let sfuAdapter = sfuAdapter else {
throw ClientError("SFUAdapter hasn't been created.")
}
log.debug(
"""
Setting up PeerConnections with
sessionId: \(sessionID)
sfuAdapter: \(sfuAdapter.hostname)
callSettings
audioOn: \(callSettings.audioOn)
videoOn: \(callSettings.videoOn)
cameraPosition: \(callSettings.cameraPosition)
""",
subsystems: .webRTC
)
let publisher = rtcPeerConnectionCoordinatorFactory.buildCoordinator(
sessionId: sessionID,
peerType: .publisher,
peerConnection: try StreamRTCPeerConnection(
peerConnectionFactory,
configuration: connectOptions.rtcConfiguration
),
peerConnectionFactory: peerConnectionFactory,
videoOptions: videoOptions,
videoConfig: videoConfig,
callSettings: callSettings,
audioSettings: audioSettings,
publishOptions: publishOptions,
sfuAdapter: sfuAdapter,
videoCaptureSessionProvider: videoCaptureSessionProvider,
screenShareSessionProvider: screenShareSessionProvider,
clientCapabilities: clientCapabilities,
audioDeviceModule: peerConnectionFactory.audioDeviceModule
)
let subscriber = rtcPeerConnectionCoordinatorFactory.buildCoordinator(
sessionId: sessionID,
peerType: .subscriber,
peerConnection: try StreamRTCPeerConnection(
peerConnectionFactory,
configuration: connectOptions.rtcConfiguration
),
peerConnectionFactory: peerConnectionFactory,
videoOptions: videoOptions,
videoConfig: videoConfig,
callSettings: callSettings,
audioSettings: audioSettings,
publishOptions: publishOptions,
sfuAdapter: sfuAdapter,
videoCaptureSessionProvider: videoCaptureSessionProvider,
screenShareSessionProvider: screenShareSessionProvider,
clientCapabilities: clientCapabilities,
audioDeviceModule: peerConnectionFactory.audioDeviceModule
)
publisher
.trackPublisher
.log(.debug, subsystems: .peerConnectionPublisher)
.sinkTask(on: self, storeIn: disposableBag, handler: { executor, event in
await executor.peerConnectionReceivedTrackEvent(.publisher, event: event)
})
.store(in: peerConnectionsDisposableBag)
subscriber
.trackPublisher
.log(.debug, subsystems: .peerConnectionSubscriber)
.sinkTask(on: self, storeIn: disposableBag, handler: { executor, event in
await executor.peerConnectionReceivedTrackEvent(.subscriber, event: event)
})
.store(in: peerConnectionsDisposableBag)
/// We setUp and restoreScreenSharing on the publisher in order to prepare all required tracks
/// for publication. In that way, negotiation will wait until ``completeSetUp`` has been called.
/// Then, with all the tracks prepared, will continue the negotiation flow.
try await publisher.setUp(
with: callSettings,
ownCapabilities: Array(
ownCapabilities
)
)
self.publisher = publisher
try await restoreScreenSharing()
publisher.setVideoFilter(videoFilter)
publisher.completeSetUp()
try await subscriber.setUp(
with: callSettings,
ownCapabilities: Array(
ownCapabilities
)
)
self.subscriber = subscriber
subscriber.completeSetUp()
}
/// Cleans up the WebRTC session by closing connections and resetting
/// states.
func cleanUp() async {
screenShareSessionProvider.activeSession = nil
videoCaptureSessionProvider.activeSession = nil
peerConnectionsDisposableBag.removeAll()
disposableBag.removeAll()
await audioSession.deactivate()
await publisher?.close()
await subscriber?.close()
self.publisher = nil
self.subscriber = nil
self.statsAdapter = nil
await sfuAdapter?.disconnect()
enqueue { _ in [:] }
set(sfuAdapter: nil)
set(token: "")
set(sessionID: "")
set(ownCapabilities: [])
set(participantsCount: 0)
set(anonymousCount: 0)
set(participantPins: [])
trackStorage.removeAll()
permissionsAdapter.cleanUp()
}
/// Cleans up the session for reconnection, clearing adapters and tracks.
func cleanUpForReconnection() async {
set(
participants: participants
/// We remove the existing user in order to avoid showing a stale video tile
/// in the Call.
.filter { $0.key != sessionID }
.reduce(into: ParticipantsStorage()) { $0[$1.key] = $1.value.withUpdated(track: nil) }
)
peerConnectionsDisposableBag.removeAll()
disposableBag.removeAll()
await audioSession.deactivate()
await publisher?.prepareForClosing()
await subscriber?.prepareForClosing()
publisher = nil
subscriber = nil
set(sfuAdapter: nil)
set(statsAdapter: nil)
set(token: "")
trackStorage.removeAll()
/// We set the initialCallSettings to the last activated CallSettings, in order to maintain the state
/// during reconnects.
initialCallSettings = callSettings
}
/// Restores screen sharing if an active session exists.
///
/// Restores app audio capture when the active session requests it.
///
/// - Throws: Throws an error if the screen sharing session cannot be
/// restored.
func restoreScreenSharing() async throws {
guard let activeSession = screenShareSessionProvider.activeSession else {
return
}
try await publisher?.beginScreenSharing(
of: activeSession.screenSharingType,
ownCapabilities: Array(ownCapabilities),
includeAudio: activeSession.includeAudio
)
}
// MARK: - Track Management
/// Adds a track for the given participant ID and track type.
///
/// - Parameters:
/// - track: The media stream track to add.
/// - type: The type of track (audio, video, screenshare).
/// - id: The participant ID associated with the track.
func didAddTrack(
_ track: RTCMediaStreamTrack,
type: TrackType,
for id: String
) {
trackStorage.addTrack(track, type: type, for: id)
enqueue { $0 }
}
/// Removes a track for the given participant ID.
///
/// - Parameters:
/// - id: The participant ID whose track should be removed.
/// - type: The type of track (audio, video, screenshare) or `nil` to remove all.
func didRemoveTrack(for id: String, type: TrackType? = nil) {
trackStorage.removeTrack(for: id, type: type)
enqueue { $0 }
}
/// Retrieves a track by (trackLookUpPrefix or sessionId) and track type.
///
/// - Parameters:
/// - participant: The participant for which we want to fetch the track.
/// - trackType: The type of track (audio, video, screenshare).
/// - Returns: The associated media stream track, or `nil` if not found.
func track(
for participant: CallParticipant,
of trackType: TrackType
) -> RTCMediaStreamTrack? {
if let trackLookupPrefix = participant.trackLookupPrefix {
return trackStorage.track(
for: trackLookupPrefix,
of: trackType
) ?? trackStorage.track(for: participant.sessionId, of: trackType)
} else {
return trackStorage.track(for: participant.sessionId, of: trackType)
}
}
/// Retrieves a track by lookup and track type.
///
/// - Parameters:
/// - lookup: The participant trackLookUpPrefix or sessionId for which we want to fetch the track.
/// - trackType: The type of track (audio, video, screenshare).
/// - Returns: The associated media stream track, or `nil` if not found.
func track(
for lookup: String,
of trackType: TrackType
) -> RTCMediaStreamTrack? {
trackStorage.track(for: lookup, of: trackType)
}
// MARK: - Participant Operations
/// Enqueues a participant operation to be executed asynchronously but in serial order for the actor.
/// - Parameters:
/// - operation: The participant operation to perform.
/// - functionName: The name of the calling function. Defaults to the current function name.
/// - fileName: The name of the file where the function is called. Defaults to the current file name.
/// - lineNumber: The line number where the function is called. Defaults to the current line number.
func enqueue(
_ operation: @escaping ParticipantOperation,
functionName: StaticString = #function,
fileName: StaticString = #fileID,
lineNumber: UInt = #line
) {
processingQueue.addTaskOperation { [weak self] in
await self?.processEnqueuedOperation(
operation,
functionName: functionName,
fileName: fileName,
lineNumber: lineNumber
)
}
}
func enqueueCallSettings(
functionName: StaticString = #function,
fileName: StaticString = #fileID,
lineNumber: UInt = #line,
_ operation: @Sendable @escaping (CallSettings) -> CallSettings
) {
callSettingsProcessingQueue.addTaskOperation { [weak self] in
guard
let self
else {
return
}
let currentCallSettings = await callSettings
let updatedCallSettings = await permissionsAdapter
.willSet(callSettings: operation(currentCallSettings))
guard
updatedCallSettings != currentCallSettings
else {
return
}
await set(callSettings: updatedCallSettings)
log.debug(
"CallSettings updated \(currentCallSettings) -> \(updatedCallSettings)",
subsystems: .webRTC,
functionName: functionName,
fileName: fileName,
lineNumber: lineNumber
)
guard
let publisher = await self.publisher
else {
return
}
try await publisher.didUpdateCallSettings(updatedCallSettings)
if updatedCallSettings.cameraPosition != currentCallSettings.cameraPosition {
try await publisher.didUpdateCameraPosition(
updatedCallSettings.cameraPosition == .back ? .back : .front
)
}
log.debug(
"Publisher callSettings updated: \(updatedCallSettings).",
subsystems: .webRTC,
functionName: functionName,
fileName: fileName,
lineNumber: lineNumber
)
}
}
/// Enqueues an own capabilities update that is applied on the actor’s
/// serial queue.
///
/// The provided closure returns the new capability set. The updated set is
/// propagated to media adapters, updates call settings when needed, and
/// stops active screen sharing when the screenshare capability is removed.
func enqueueOwnCapabilities(
functionName: StaticString = #function,
fileName: StaticString = #fileID,
lineNumber: UInt = #line,
_ operation: @Sendable @escaping () -> Set<OwnCapability>
) async {
let newValue = operation()
set(ownCapabilities: newValue)
publisher?.didUpdateOwnCapabilities(newValue)
enqueueCallSettings(
functionName: functionName,
fileName: fileName,
lineNumber: lineNumber
) { [newValue] callSettings in
var updatedCallSettings = callSettings
if !newValue.contains(.sendAudio), callSettings.audioOn {
updatedCallSettings = updatedCallSettings
.withUpdatedAudioState(false)
}
if !newValue.contains(.sendVideo), callSettings.videoOn {
updatedCallSettings = updatedCallSettings
.withUpdatedVideoState(false)
}
return updatedCallSettings
}
if !newValue.contains(.screenshare), screenShareSessionProvider.activeSession != nil {
do {
try await publisher?.stopScreenSharing()
} catch {
log.error(error, subsystems: .webRTC)
}
}
}
func trace(_ trace: WebRTCTrace) {
if let statsAdapter {
statsAdapter.trace(trace)
} else {
queuedTraces.append(trace)
}
}
private func processEnqueuedOperation(
_ operation: @escaping ParticipantOperation,
functionName: StaticString = #function,
fileName: StaticString = #fileID,
lineNumber: UInt = #line
) {
/// Retrieves the current participants.
let current = participants
/// Applies the operation to get the next state of participants.
let next = operation(current)
/// Assigns media tracks to the participants.
let updated = assignTracks(on: next)
/// Sends the updated participants to observers while helping publishing streamlined updates.
set(participants: updated)
/// Logs the completion of the participant operation.
log.debug(
"Participant operation completed. Participants count:\(updated.count).",
functionName: functionName,
fileName: fileName,
lineNumber: lineNumber
)
}
/// Assigns media tracks to participants based on their media type.
/// - Parameter participants: The storage containing participant information.
/// - Returns: An updated participants storage with assigned tracks.
func assignTracks(
on participants: ParticipantsStorage
) -> ParticipantsStorage {
/// Reduces the participants to a new storage with updated tracks.
participants.reduce(into: ParticipantsStorage()) { partialResult, entry in
var newParticipant = entry
.value
/// Updates the participant with a video track if available.
.withUpdated(track: track(for: entry.value, of: .video) as? RTCVideoTrack)
/// Updates the participant with a screensharing track if available.
.withUpdated(screensharingTrack: track(for: entry.value, of: .screenshare) as? RTCVideoTrack)
/// For participants other than the local one, we check if the incomingVideoQualitySettings
/// provide additional limits.
if
newParticipant.sessionId != sessionID,
incomingVideoQualitySettings.isVideoDisabled(for: entry.value.sessionId) {
newParticipant = newParticipant.withUpdated(track: nil)
}
partialResult[entry.key] = newParticipant
}
}
func updateCallSettings(
from event: Stream_Video_Sfu_Event_TrackUnpublished
) {
guard
event.participant.sessionID == sessionID,
event.type == .audio || event.type == .video
else {
return
}
enqueueCallSettings { currentCallSettings in
let possibleNewCallSettings = {
switch event.type {
case .audio:
return currentCallSettings.withUpdatedAudioState(false)
case .video:
return currentCallSettings.withUpdatedVideoState(false)
default:
return currentCallSettings
}
}()
return possibleNewCallSettings
}
}
// MARK: - Private Helpers
/// Handles track events when they are added or removed from peer connections.
private func peerConnectionReceivedTrackEvent(
_ peerConnectionType: PeerConnectionType,
event: TrackEvent
) {
switch event {
case let .added(id, trackType, track):
didAddTrack(track, type: trackType, for: id)
case let .removed(id, trackType, _):
didRemoveTrack(for: id, type: trackType)
}
}
/// Updates the video options and notifies the publisher and subscriber.
private func didUpdate(videoOptions: VideoOptions) {
publisher?.videoOptions = videoOptions
subscriber?.videoOptions = videoOptions
}
/// Updates the publish options and notifies the publisher.
private func didUpdate(publishOptions: PublishOptions) {
publisher?.publishOptions = publishOptions
}
// MARK: Participant Operations
/// Updates the current participants and logs those with video tracks.
/// - Parameter participants: The storage containing participant information.
private func set(participants: ParticipantsStorage) {
/// Updates the local participants storage.
self.participants = participants
/// Filters participants who have video tracks.
let participantsWithVideoTracks = participants
.filter { $0.value.track != nil }
.map(\.value.name)
.sorted()
.joined(separator: ",")
/// Logs the count and names of participants with video tracks.
if participantsWithVideoTracks.isEmpty {
log.debug(
"\(participants.count) participants updated. None of the participants have video.",
subsystems: .webRTC
)
} else {
log.debug(
"\(participants.count) participants updated. \(participantsWithVideoTracks) have video tracks.",
subsystems: .webRTC
)
}
}
func configureAudioSession(source: JoinSource?) async throws {
try await audioStore.dispatch([
.setAudioDeviceModule(peerConnectionFactory.audioDeviceModule)
]).result()
if case let .callKit(completion) = source {
// Let CallKit release its audio session ownership once WebRTC has
// the audio device module it needs.
completion.complete()
}
audioSession.activate(
callSettingsPublisher: $callSettings.removeDuplicates().eraseToAnyPublisher(),
ownCapabilitiesPublisher: $ownCapabilities.removeDuplicates().eraseToAnyPublisher(),
delegate: self,
statsAdapter: statsAdapter,
/// If we are joining from CallKit the AudioSession will be activated from it and we
/// shouldn't attempt another activation.
shouldSetActive: source == .inApp
)
}
private func configureBatteryObservation() {
// Observe battery to attach traces when needed
let battery = BatteryStore.currentValue
Publishers
.CombineLatest(
battery.publisher(\.level).removeDuplicates().eraseToAnyPublisher(),
battery.publisher(\.state).removeDuplicates().eraseToAnyPublisher()
)
.compactMap { [weak battery] _ in battery }
.sinkTask(on: self, storeIn: disposableBag, handler: { actor, battery in
await actor.trace(.init(battery))
})
.store(in: disposableBag)
}
// MARK: - AudioSessionDelegate
nonisolated func audioSessionAdapterDidUpdateSpeakerOn(
_ speakerOn: Bool,
file: StaticString,
function: StaticString,
line: UInt
) {
Task(disposableBag: disposableBag) { [weak self] in
guard let self else {
return
}
await self.enqueueCallSettings(
functionName: function,
fileName: file,
lineNumber: line
) {
$0.withUpdatedSpeakerState(speakerOn)
}
}
log.debug(
"AudioSession delegated updated speakerOn:\(speakerOn).",
subsystems: .audioSession,
functionName: function,
fileName: file,
lineNumber: line
)
}
// MARK: - WebRTCPermissionsAdapterDelegate
nonisolated func permissionsAdapter(
_ permissionsAdapter: WebRTCPermissionsAdapter,
audioOn: Bool
) {
Task(disposableBag: disposableBag) { [weak self] in
guard let self else {
return
}
await self.enqueueCallSettings {
$0.withUpdatedAudioState(audioOn)
}
log.debug(
"PermissionsAdapter delegated updated audioOn:\(audioOn).",
subsystems: .audioSession
)
}
}
nonisolated func permissionsAdapter(
_ permissionsAdapter: WebRTCPermissionsAdapter,
videoOn: Bool
) {
Task(disposableBag: disposableBag) { [weak self] in
guard let self else {
return
}
await self.enqueueCallSettings {
$0.withUpdatedVideoState(videoOn)
}
log.debug(
"PermissionsAdapter delegated updated videoOn:\(videoOn).",
subsystems: .audioSession
)
}
}
}