mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Adjust CallKit integration
This commit is contained in:
parent
d391651586
commit
2a9358f130
@ -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()
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -706,6 +706,10 @@ public final class OngoingCallContext {
|
||||
}
|
||||
}
|
||||
|
||||
public static func setupAudioSession() {
|
||||
OngoingCallThreadLocalContextWebrtc.setupAudioSession()
|
||||
}
|
||||
|
||||
public let callId: CallId
|
||||
public let internalId: CallSessionInternalId
|
||||
|
||||
|
@ -202,6 +202,8 @@ typedef NS_ENUM(int32_t, OngoingCallDataSavingWebrtc) {
|
||||
+ (int32_t)maxLayer;
|
||||
+ (NSArray<NSString *> * _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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user