diff --git a/submodules/TelegramAudio/Sources/ManagedAudioSession.swift b/submodules/TelegramAudio/Sources/ManagedAudioSession.swift index de023e5d59..6269ce926a 100644 --- a/submodules/TelegramAudio/Sources/ManagedAudioSession.swift +++ b/submodules/TelegramAudio/Sources/ManagedAudioSession.swift @@ -961,7 +961,7 @@ public final class ManagedAudioSession { public func callKitActivatedAudioSession() { self.queue.async { - managedAudioSessionLog("ManagedAudioSession callKitDeactivatedAudioSession") + managedAudioSessionLog("ManagedAudioSession callKitActivatedAudioSession") self.callKitAudioSessionIsActive = true self.updateHolders() } diff --git a/submodules/TelegramCallsUI/Sources/CallKitIntegration.swift b/submodules/TelegramCallsUI/Sources/CallKitIntegration.swift index 6bff917ab6..dc78dd3ef6 100644 --- a/submodules/TelegramCallsUI/Sources/CallKitIntegration.swift +++ b/submodules/TelegramCallsUI/Sources/CallKitIntegration.swift @@ -9,6 +9,7 @@ import SwiftSignalKit import AppBundle import AccountContext import TelegramAudio +import TelegramVoip private let sharedProviderDelegate: AnyObject? = { if #available(iOSApplicationExtension 10.0, iOS 10.0, *) { @@ -169,25 +170,32 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } private func requestTransaction(_ transaction: CXTransaction, completion: ((Bool) -> Void)? = nil) { + Logger.shared.log("CallKitIntegration", "requestTransaction \(transaction)") self.callController.request(transaction) { error in if let error = error { - print("Error requesting transaction \(transaction): \(error)") + Logger.shared.log("CallKitIntegration", "error in requestTransaction \(transaction): \(error)") } completion?(error == nil) } } func endCall(uuid: UUID) { + Logger.shared.log("CallKitIntegration", "endCall \(uuid)") + let endCallAction = CXEndCallAction(call: uuid) let transaction = CXTransaction(action: endCallAction) self.requestTransaction(transaction) } func dropCall(uuid: UUID) { + Logger.shared.log("CallKitIntegration", "report call ended \(uuid)") + self.provider.reportCall(with: uuid, endedAt: nil, reason: CXCallEndedReason.remoteEnded) } func answerCall(uuid: UUID) { + Logger.shared.log("CallKitIntegration", "answer call \(uuid)") + let answerCallAction = CXAnswerCallAction(call: uuid) let transaction = CXTransaction(action: answerCallAction) self.requestTransaction(transaction) @@ -211,6 +219,8 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { startCallAction.isVideo = isVideo let transaction = CXTransaction(action: startCallAction) + Logger.shared.log("CallKitIntegration", "initiate call \(uuid)") + self.requestTransaction(transaction, completion: { _ in let update = CXCallUpdate() update.remoteHandle = handle @@ -246,11 +256,15 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { update.supportsDTMF = false update.hasVideo = isVideo - do { + Logger.shared.log("CallKitIntegration", "report incoming call \(uuid)") + + OngoingCallContext.setupAudioSession() + + /*do { try AVAudioSession.sharedInstance().setMode(.voiceChat) } catch let e { print("AVAudioSession.sharedInstance().setMode(.voiceChat) error \(e)") - } + }*/ self.provider.reportNewIncomingCall(with: uuid, update: update, completion: { error in completion?(error as NSError?) @@ -258,17 +272,24 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } func reportOutgoingCallConnecting(uuid: UUID, at date: Date) { + Logger.shared.log("CallKitIntegration", "report outgoing call connecting \(uuid)") + self.provider.reportOutgoingCall(with: uuid, startedConnectingAt: date) } func reportOutgoingCallConnected(uuid: UUID, at date: Date) { + Logger.shared.log("CallKitIntegration", "report call connected \(uuid)") + self.provider.reportOutgoingCall(with: uuid, connectedAt: date) } func providerDidReset(_ provider: CXProvider) { + Logger.shared.log("CallKitIntegration", "providerDidReset") } func provider(_ provider: CXProvider, perform action: CXStartCallAction) { + Logger.shared.log("CallKitIntegration", "provider perform start call action \(action)") + guard let startCall = self.startCall, let (uuid, context) = self.currentStartCallAccount, uuid == action.callUUID else { action.fail() return @@ -295,6 +316,8 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) { + Logger.shared.log("CallKitIntegration", "provider perform answer call action \(action)") + guard let answerCall = self.answerCall else { action.fail() return @@ -304,6 +327,8 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } func provider(_ provider: CXProvider, perform action: CXEndCallAction) { + Logger.shared.log("CallKitIntegration", "provider perform end call action \(action)") + guard let endCall = self.endCall else { action.fail() return @@ -326,6 +351,8 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) { + Logger.shared.log("CallKitIntegration", "provider perform mute call action \(action)") + guard let setCallMuted = self.setCallMuted else { action.fail() return @@ -335,7 +362,7 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) { - print("provider didActivate default? \(audioSession === AVAudioSession.sharedInstance())") + Logger.shared.log("CallKitIntegration", "provider didActivate audio session") self.isAudioSessionActive = true self.audioSessionActivationChanged?(true) self.audioSessionActivePromise?.set(true) @@ -347,6 +374,7 @@ class CallKitProviderDelegate: NSObject, CXProviderDelegate { } func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) { + Logger.shared.log("CallKitIntegration", "provider didDeactivate audio session") self.isAudioSessionActive = false self.audioSessionActivationChanged?(false) self.audioSessionActivePromise?.set(false) diff --git a/submodules/TelegramCallsUI/Sources/PresentationCall.swift b/submodules/TelegramCallsUI/Sources/PresentationCall.swift index 6cf0a084d5..e4084d3cf1 100644 --- a/submodules/TelegramCallsUI/Sources/PresentationCall.swift +++ b/submodules/TelegramCallsUI/Sources/PresentationCall.swift @@ -883,6 +883,10 @@ public final class PresentationCallImpl: PresentationCall { } public func answer() { + self.answer(fromCallKitAction: false) + } + + func answer(fromCallKitAction: Bool) { let (presentationData, present, openSettings) = self.getDeviceAccessData() DeviceAccess.authorizeAccess(to: .microphone(.voiceCall), presentationData: presentationData, present: { c, a in @@ -905,14 +909,18 @@ public final class PresentationCallImpl: PresentationCall { } if value { strongSelf.callSessionManager.accept(internalId: strongSelf.internalId) - strongSelf.callKitIntegration?.answerCall(uuid: strongSelf.internalId) + if !fromCallKitAction { + strongSelf.callKitIntegration?.answerCall(uuid: strongSelf.internalId) + } } else { let _ = strongSelf.hangUp().start() } }) } else { strongSelf.callSessionManager.accept(internalId: strongSelf.internalId) - strongSelf.callKitIntegration?.answerCall(uuid: strongSelf.internalId) + if !fromCallKitAction { + strongSelf.callKitIntegration?.answerCall(uuid: strongSelf.internalId) + } } } else { let _ = strongSelf.hangUp().start() diff --git a/submodules/TelegramCallsUI/Sources/PresentationCallManager.swift b/submodules/TelegramCallsUI/Sources/PresentationCallManager.swift index 99f57fb5fe..0337e2688e 100644 --- a/submodules/TelegramCallsUI/Sources/PresentationCallManager.swift +++ b/submodules/TelegramCallsUI/Sources/PresentationCallManager.swift @@ -240,7 +240,7 @@ public final class PresentationCallManagerImpl: PresentationCallManager { answerCallImpl = { [weak self] uuid in if let strongSelf = self { - strongSelf.currentCall?.answer() + strongSelf.currentCall?.answer(fromCallKitAction: true) } } diff --git a/submodules/TelegramVoip/Sources/OngoingCallContext.swift b/submodules/TelegramVoip/Sources/OngoingCallContext.swift index 3fedf43db4..a4bdf14d58 100644 --- a/submodules/TelegramVoip/Sources/OngoingCallContext.swift +++ b/submodules/TelegramVoip/Sources/OngoingCallContext.swift @@ -706,6 +706,10 @@ public final class OngoingCallContext { } } + public static func setupAudioSession() { + OngoingCallThreadLocalContextWebrtc.setupAudioSession() + } + public let callId: CallId public let internalId: CallSessionInternalId diff --git a/submodules/TgVoipWebrtc/PublicHeaders/TgVoipWebrtc/OngoingCallThreadLocalContext.h b/submodules/TgVoipWebrtc/PublicHeaders/TgVoipWebrtc/OngoingCallThreadLocalContext.h index ea6855191d..6325fd73a7 100644 --- a/submodules/TgVoipWebrtc/PublicHeaders/TgVoipWebrtc/OngoingCallThreadLocalContext.h +++ b/submodules/TgVoipWebrtc/PublicHeaders/TgVoipWebrtc/OngoingCallThreadLocalContext.h @@ -202,6 +202,8 @@ typedef NS_ENUM(int32_t, OngoingCallDataSavingWebrtc) { + (int32_t)maxLayer; + (NSArray * _Nonnull)versionsWithIncludeReference:(bool)includeReference; ++ (void)setupAudioSession; + @property (nonatomic, copy) void (^ _Nullable stateChanged)(OngoingCallStateWebrtc, OngoingCallVideoStateWebrtc, OngoingCallRemoteVideoStateWebrtc, OngoingCallRemoteAudioStateWebrtc, OngoingCallRemoteBatteryLevelWebrtc, float); @property (nonatomic, copy) void (^ _Nullable signalBarsChanged)(int32_t); @property (nonatomic, copy) void (^ _Nullable audioLevelUpdated)(float); diff --git a/submodules/TgVoipWebrtc/Sources/OngoingCallThreadLocalContext.mm b/submodules/TgVoipWebrtc/Sources/OngoingCallThreadLocalContext.mm index e25e451a74..0ca3837f05 100644 --- a/submodules/TgVoipWebrtc/Sources/OngoingCallThreadLocalContext.mm +++ b/submodules/TgVoipWebrtc/Sources/OngoingCallThreadLocalContext.mm @@ -808,6 +808,18 @@ static void (*InternalVoipLoggingFunction)(NSString *) = NULL; } } ++ (void)setupAudioSession { + RTCAudioSessionConfiguration *sharedConfiguration = [RTCAudioSessionConfiguration webRTCConfiguration]; + sharedConfiguration.mode = AVAudioSessionModeVoiceChat; + sharedConfiguration.categoryOptions |= AVAudioSessionCategoryOptionMixWithOthers; + sharedConfiguration.outputNumberOfChannels = 1; + [RTCAudioSessionConfiguration setWebRTCConfiguration:sharedConfiguration]; + + [[RTCAudioSession sharedInstance] lockForConfiguration]; + [[RTCAudioSession sharedInstance] setConfiguration:sharedConfiguration active:false error:nil disableRecording:false]; + [[RTCAudioSession sharedInstance] unlockForConfiguration]; +} + + (int32_t)maxLayer { return 92; }