@@ -74,10 +74,7 @@ final class DevicesContext : NSObject {
74
74
75
75
private let updaterContext : UpdaterContext = UpdaterContext ( )
76
76
77
- private( set) var currentCameraId : String ? = nil
78
- private( set) var currentMicroId : String ? = nil
79
- private( set) var currentOutputId : String ? = nil
80
-
77
+
81
78
82
79
func updater( ) -> Signal < ( camera: String ? , input: String ? , output: String ? ) , NoError > {
83
80
return Signal { subscriber in
@@ -103,6 +100,21 @@ final class DevicesContext : NSObject {
103
100
104
101
105
102
private let disposable = MetaDisposable ( )
103
+ private let devicesQueue = Queue ( name: " devicesQueue " )
104
+
105
+ private let _currentCameraId : Atomic < String ? > = Atomic ( value: nil )
106
+ private let _currentMicroId : Atomic < String ? > = Atomic ( value: nil )
107
+ private let _currentOutputId : Atomic < String ? > = Atomic ( value: nil )
108
+
109
+ var currentCameraId : String ? {
110
+ return _currentCameraId. with { $0 }
111
+ }
112
+ var currentMicroId : String ? {
113
+ return _currentMicroId. with { $0 }
114
+ }
115
+ var currentOutputId : String ? {
116
+ return _currentOutputId. with { $0 }
117
+ }
106
118
107
119
init ( _ accountManager: AccountManager ) {
108
120
super. init ( )
@@ -119,24 +131,32 @@ final class DevicesContext : NSObject {
119
131
self ? . update ( )
120
132
} )
121
133
122
- disposable. set ( combineLatest ( queue: . mainQueue( ) , voiceCallSettings ( accountManager) , signal) . start ( next: { [ weak self] settings, devices in
123
- guard let `self` = self else {
124
- return
125
- }
126
- let inputUpdated = self . updateMicroId ( settings, devices: devices)
127
- let cameraUpdated = self . updateCameraId ( settings, devices: devices)
128
- let outputUpdated = self . updateOutputId ( settings, devices: devices)
134
+ let currentCameraId = self . _currentCameraId
135
+ let currentMicroId = self . _currentMicroId
136
+ let currentOutputId = self . _currentOutputId
137
+
138
+ let updated = combineLatest ( queue: devicesQueue, voiceCallSettings ( accountManager) , signal) |> map { settings, devices -> ( camera: String ? , input: String ? , output: String ? ) in
139
+ let inputUpdated = DevicesContext . updateMicroId ( settings, devices: devices)
140
+ let cameraUpdated = DevicesContext . updateCameraId ( settings, devices: devices)
141
+ let outputUpdated = DevicesContext . updateOutputId ( settings, devices: devices)
129
142
130
143
var result : ( camera: String ? , input: String ? , output: String ? ) = ( camera: nil , input: nil , output: nil )
131
144
132
- if inputUpdated {
133
- result. input = self . currentMicroId ?? " "
145
+ if currentMicroId . swap ( inputUpdated ) != inputUpdated {
146
+ result. input = inputUpdated
134
147
}
135
- if cameraUpdated {
136
- result. camera = self . currentCameraId ?? " "
148
+ if currentCameraId . swap ( cameraUpdated ) != cameraUpdated {
149
+ result. camera = cameraUpdated
137
150
}
138
- if outputUpdated {
139
- result. output = self . currentOutputId ?? " "
151
+ if currentOutputId. swap ( outputUpdated) != outputUpdated {
152
+ result. output = outputUpdated
153
+ }
154
+ return result
155
+ } |> deliverOnMainQueue
156
+
157
+ disposable. set ( updated. start ( next: { [ weak self] result in
158
+ guard let `self` = self else {
159
+ return
140
160
}
141
161
self . updaterContext. status = result
142
162
@@ -156,7 +176,7 @@ final class DevicesContext : NSObject {
156
176
self . update ( )
157
177
}
158
178
159
- @ discardableResult func updateCameraId( _ settings: VoiceCallSettings , devices: IODevices ) -> Bool {
179
+ static func updateCameraId( _ settings: VoiceCallSettings , devices: IODevices ) -> String ? {
160
180
let cameraDevice = devices. camera. first ( where: { $0. uniqueID == settings. cameraInputDeviceId } )
161
181
162
182
let activeDevice : AVCaptureDevice ?
@@ -172,13 +192,9 @@ final class DevicesContext : NSObject {
172
192
activeDevice = devices. camera. first ( where: { $0. isConnected && !$0. isSuspended } )
173
193
}
174
194
175
- defer {
176
- self . currentCameraId = activeDevice? . uniqueID
177
- }
178
-
179
- return self . currentCameraId != activeDevice? . uniqueID
195
+ return activeDevice? . uniqueID
180
196
}
181
- @ discardableResult func updateMicroId( _ settings: VoiceCallSettings , devices: IODevices ) -> Bool {
197
+ static func updateMicroId( _ settings: VoiceCallSettings , devices: IODevices ) -> String ? {
182
198
let audiodevice = devices. audioInput. first ( where: { $0. uniqueID == settings. audioInputDeviceId } )
183
199
184
200
let activeDevice : AVCaptureDevice ?
@@ -194,15 +210,11 @@ final class DevicesContext : NSObject {
194
210
activeDevice = devices. audioInput. first ( where: { $0. isConnected && !$0. isSuspended } )
195
211
}
196
212
197
- defer {
198
- self . currentMicroId = activeDevice? . uniqueID
199
- }
200
-
201
- return self . currentMicroId != activeDevice? . uniqueID
213
+ return activeDevice? . uniqueID
202
214
}
203
215
204
- @ discardableResult func updateOutputId( _ settings: VoiceCallSettings , devices: IODevices ) -> Bool {
205
- var deviceUid : String = " "
216
+ static func updateOutputId( _ settings: VoiceCallSettings , devices: IODevices ) -> String ? {
217
+ var deviceUid : String ? = nil
206
218
var found = false
207
219
for id in devices. audioOutput {
208
220
let current = Audio . getDeviceUid ( deviceId: id)
@@ -215,11 +227,7 @@ final class DevicesContext : NSObject {
215
227
deviceUid = Audio . getDeviceUid ( deviceId: Audio . getDefaultOutputDevice ( ) )
216
228
}
217
229
218
- defer {
219
- self . currentOutputId = deviceUid as String
220
- }
221
-
222
- return self . currentOutputId != deviceUid as String
230
+ return deviceUid
223
231
}
224
232
225
233
deinit {
0 commit comments