@@ -15,216 +15,6 @@ import AccountContext
1515import DeviceProximity
1616import PhoneNumberFormat
1717
18- public final class SharedCallAudioContext {
19- let audioDevice : OngoingCallContext . AudioDevice ?
20- let callKitIntegration : CallKitIntegration ?
21-
22- private let defaultToSpeaker : Bool
23-
24- private var audioSessionDisposable : Disposable ?
25- private var audioSessionShouldBeActiveDisposable : Disposable ?
26- private var isAudioSessionActiveDisposable : Disposable ?
27- private var audioOutputStateDisposable : Disposable ?
28-
29- private( set) var audioSessionControl : ManagedAudioSessionControl ?
30-
31- private let isAudioSessionActivePromise = Promise < Bool > ( false )
32- private var isAudioSessionActive : Signal < Bool , NoError > {
33- return self . isAudioSessionActivePromise. get ( )
34- }
35-
36- private let audioOutputStatePromise = Promise < ( [ AudioSessionOutput ] , AudioSessionOutput ? ) > ( ( [ ] , nil ) )
37- private var audioOutputStateValue : ( [ AudioSessionOutput ] , AudioSessionOutput ? ) = ( [ ] , nil )
38- public private( set) var currentAudioOutputValue : AudioSessionOutput = . builtin
39- private var didSetCurrentAudioOutputValue : Bool = false
40- var audioOutputState : Signal < ( [ AudioSessionOutput ] , AudioSessionOutput ? ) , NoError > {
41- return self . audioOutputStatePromise. get ( )
42- }
43-
44- private let audioSessionShouldBeActive = Promise < Bool > ( true )
45- private var initialSetupTimer : Foundation . Timer ?
46-
47- init ( audioSession: ManagedAudioSession , callKitIntegration: CallKitIntegration ? , defaultToSpeaker: Bool = false ) {
48- self . callKitIntegration = callKitIntegration
49- self . audioDevice = OngoingCallContext . AudioDevice. create ( enableSystemMute: false )
50- self . defaultToSpeaker = defaultToSpeaker
51-
52- if defaultToSpeaker {
53- self . didSetCurrentAudioOutputValue = true
54- self . currentAudioOutputValue = . speaker
55- }
56-
57- var didReceiveAudioOutputs = false
58- self . audioSessionDisposable = audioSession. push ( audioSessionType: . voiceCall, manualActivate: { [ weak self] control in
59- Queue . mainQueue ( ) . async {
60- guard let self else {
61- return
62- }
63- let previousControl = self . audioSessionControl
64- self . audioSessionControl = control
65-
66- if previousControl == nil , let audioSessionControl = self . audioSessionControl {
67- if let callKitIntegration = self . callKitIntegration {
68- if self . didSetCurrentAudioOutputValue {
69- callKitIntegration. applyVoiceChatOutputMode ( outputMode: . custom( self . currentAudioOutputValue) )
70- }
71- } else {
72- audioSessionControl. setOutputMode ( . custom( self . currentAudioOutputValue) )
73- audioSessionControl. setup ( synchronous: true )
74- }
75-
76- let audioSessionActive : Signal < Bool , NoError >
77- if let callKitIntegration = self . callKitIntegration {
78- audioSessionActive = callKitIntegration. audioSessionActive
79- } else {
80- audioSessionControl. activate ( { _ in } )
81- audioSessionActive = . single( true )
82- }
83- self . isAudioSessionActivePromise. set ( audioSessionActive)
84-
85- self . initialSetupTimer? . invalidate ( )
86- self . initialSetupTimer = Foundation . Timer ( timeInterval: 0.5 , repeats: false , block: { [ weak self] _ in
87- guard let self else {
88- return
89- }
90-
91- if self . defaultToSpeaker, let audioSessionControl = self . audioSessionControl {
92- self . currentAudioOutputValue = . speaker
93- self . didSetCurrentAudioOutputValue = true
94-
95- if let callKitIntegration = self . callKitIntegration {
96- if self . didSetCurrentAudioOutputValue {
97- callKitIntegration. applyVoiceChatOutputMode ( outputMode: . custom( self . currentAudioOutputValue) )
98- }
99- } else {
100- audioSessionControl. setOutputMode ( . custom( self . currentAudioOutputValue) )
101- audioSessionControl. setup ( synchronous: true )
102- }
103- }
104- } )
105- }
106- }
107- } , deactivate: { [ weak self] _ in
108- return Signal { subscriber in
109- Queue . mainQueue ( ) . async {
110- if let self {
111- self . isAudioSessionActivePromise. set ( . single( false ) )
112- self . audioSessionControl = nil
113- }
114- subscriber. putCompletion ( )
115- }
116- return EmptyDisposable
117- }
118- } , availableOutputsChanged: { [ weak self] availableOutputs, currentOutput in
119- Queue . mainQueue ( ) . async {
120- guard let self else {
121- return
122- }
123- self . audioOutputStateValue = ( availableOutputs, currentOutput)
124- if let currentOutput = currentOutput {
125- self . currentAudioOutputValue = currentOutput
126- self . didSetCurrentAudioOutputValue = true
127- }
128-
129- var signal : Signal < ( [ AudioSessionOutput ] , AudioSessionOutput ? ) , NoError > = . single( ( availableOutputs, currentOutput) )
130- if !didReceiveAudioOutputs {
131- didReceiveAudioOutputs = true
132- if currentOutput == . speaker {
133- signal = . single( ( availableOutputs, . builtin) )
134- |> then (
135- signal
136- |> delay ( 1.0 , queue: Queue . mainQueue ( ) )
137- )
138- }
139- }
140- self . audioOutputStatePromise. set ( signal)
141- }
142- } )
143-
144- self . audioSessionShouldBeActive. set ( . single( true ) )
145- self . audioSessionShouldBeActiveDisposable = ( self . audioSessionShouldBeActive. get ( )
146- |> deliverOnMainQueue) . start ( next: { [ weak self] value in
147- guard let self else {
148- return
149- }
150- if value {
151- if let audioSessionControl = self . audioSessionControl {
152- let audioSessionActive : Signal < Bool , NoError >
153- if let callKitIntegration = self . callKitIntegration {
154- audioSessionActive = callKitIntegration. audioSessionActive
155- } else {
156- audioSessionControl. activate ( { _ in } )
157- audioSessionActive = . single( true )
158- }
159- self . isAudioSessionActivePromise. set ( audioSessionActive)
160- } else {
161- self . isAudioSessionActivePromise. set ( . single( false ) )
162- }
163- } else {
164- self . isAudioSessionActivePromise. set ( . single( false ) )
165- }
166- } )
167-
168- self . isAudioSessionActiveDisposable = ( self . isAudioSessionActive
169- |> deliverOnMainQueue) . start ( next: { [ weak self] value in
170- guard let self else {
171- return
172- }
173- self . audioDevice? . setIsAudioSessionActive ( value)
174- } )
175-
176- self . audioOutputStateDisposable = ( self . audioOutputStatePromise. get ( )
177- |> deliverOnMainQueue) . start ( next: { [ weak self] value in
178- guard let self else {
179- return
180- }
181- self . audioOutputStateValue = value
182- if let currentOutput = value. 1 {
183- self . currentAudioOutputValue = currentOutput
184- }
185- } )
186- }
187-
188- deinit {
189- self . audioSessionDisposable? . dispose ( )
190- self . audioSessionShouldBeActiveDisposable? . dispose ( )
191- self . isAudioSessionActiveDisposable? . dispose ( )
192- self . audioOutputStateDisposable? . dispose ( )
193- self . initialSetupTimer? . invalidate ( )
194- }
195-
196- func setCurrentAudioOutput( _ output: AudioSessionOutput ) {
197- self . initialSetupTimer? . invalidate ( )
198- self . initialSetupTimer = nil
199-
200- guard self . currentAudioOutputValue != output else {
201- return
202- }
203- self . currentAudioOutputValue = output
204- self . didSetCurrentAudioOutputValue = true
205-
206- self . audioOutputStatePromise. set ( . single( ( self . audioOutputStateValue. 0 , output) )
207- |> then (
208- . single( self . audioOutputStateValue)
209- |> delay ( 1.0 , queue: Queue . mainQueue ( ) )
210- ) )
211-
212- if let audioSessionControl = self . audioSessionControl {
213- if let callKitIntegration = self . callKitIntegration {
214- callKitIntegration. applyVoiceChatOutputMode ( outputMode: . custom( self . currentAudioOutputValue) )
215- } else {
216- audioSessionControl. setOutputMode ( . custom( output) )
217- }
218- }
219- }
220-
221- public func switchToSpeakerIfBuiltin( ) {
222- if case . builtin = self . currentAudioOutputValue {
223- self . setCurrentAudioOutput ( . speaker)
224- }
225- }
226- }
227-
22818public final class PresentationCallImpl : PresentationCall {
22919 public let context : AccountContext
23020 private let audioSession : ManagedAudioSession
@@ -556,7 +346,7 @@ public final class PresentationCallImpl: PresentationCall {
556346 if let data = context. currentAppConfiguration. with ( { $0 } ) . data, let _ = data [ " ios_killswitch_disable_call_device " ] {
557347 self . sharedAudioContext = nil
558348 } else {
559- self . sharedAudioContext = SharedCallAudioContext ( audioSession: audioSession, callKitIntegration: callKitIntegration, defaultToSpeaker: startWithVideo || initialState? . type == . video)
349+ self . sharedAudioContext = SharedCallAudioContext . get ( audioSession: audioSession, callKitIntegration: callKitIntegration, defaultToSpeaker: startWithVideo || initialState? . type == . video)
560350 }
561351
562352 if let _ = self . sharedAudioContext {
@@ -1118,7 +908,7 @@ public final class PresentationCallImpl: PresentationCall {
1118908 var found = false
1119909 if let members {
1120910 for participant in members. participants {
1121- if participant. peer . id == waitForRemotePeerId {
911+ if participant. id == . peer ( waitForRemotePeerId) {
1122912 found = true
1123913 break
1124914 }
@@ -1131,7 +921,7 @@ public final class PresentationCallImpl: PresentationCall {
1131921 if waitForLocalVideo {
1132922 if let members {
1133923 for participant in members. participants {
1134- if participant. peer . id == state. myPeerId {
924+ if participant. id == . peer ( state. myPeerId) {
1135925 if participant. videoDescription == nil {
1136926 return false
1137927 }
@@ -1142,7 +932,7 @@ public final class PresentationCallImpl: PresentationCall {
1142932 if let waitForRemoteVideo {
1143933 if let members {
1144934 for participant in members. participants {
1145- if participant. peer . id == waitForRemoteVideo {
935+ if participant. id == . peer ( waitForRemoteVideo) {
1146936 if participant. videoDescription == nil {
1147937 return false
1148938 }
0 commit comments