From 636e4cd67b2f2b01b4dfc66fad26c72b46bd0cba Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Fri, 7 May 2021 15:33:00 +0200 Subject: [PATCH] Use the linphone functions for audio route management rather than IOS specific ones. Also removed redundant triggers on Speaker and Bluetooth buttons. --- Classes/CallManager.swift | 46 ++++++++++++++++++++++---- Classes/CallOutgoingView.m | 9 ++--- Classes/CallView.m | 9 ++--- Classes/LinphoneManager.h | 2 +- Classes/LinphoneManager.m | 6 ++-- Classes/LinphoneUI/UIBluetoothButton.m | 8 ----- Classes/LinphoneUI/UISpeakerButton.m | 9 ----- Classes/LinphoneUI/UIVideoButton.m | 3 +- 8 files changed, 51 insertions(+), 41 deletions(-) diff --git a/Classes/CallManager.swift b/Classes/CallManager.swift index 366c31c0d..85739737b 100644 --- a/Classes/CallManager.swift +++ b/Classes/CallManager.swift @@ -126,13 +126,13 @@ import AVFoundation #endif return false } - + /* @objc func allowSpeaker() -> Bool { if (UIDevice.current.userInterfaceIdiom == .pad) { // For now, ipad support only speaker. return true } - + return true var allow = true let newRoute = AVAudioSession.sharedInstance().currentRoute if (newRoute.outputs.count > 0) { @@ -142,7 +142,39 @@ import AVFoundation return allow } - + */ + + @objc func changeRouteToSpeaker() { + speakerEnabled = true + for device in lc!.audioDevices { + if (device.type == AudioDeviceType.Speaker) { + lc!.outputAudioDevice = device + break + } + } + UIDevice.current.isProximityMonitoringEnabled = false + bluetoothEnabled = false + } + + @objc func changeRouteToBluetooth() { + bluetoothEnabled = true + for device in lc!.audioDevices { + if (device.type == AudioDeviceType.Bluetooth || device.type == AudioDeviceType.BluetoothA2DP) { + lc!.outputAudioDevice = device + break + } + } + speakerEnabled = false + UIDevice.current.isProximityMonitoringEnabled = (lc!.callsNb > 0) + } + + @objc func changeRouteToDefault() { + bluetoothEnabled = false + speakerEnabled = false + lc!.outputAudioDevice = lc!.defaultOutputAudioDevice + } + /* + @objc func enableSpeaker(enable: Bool) { speakerEnabled = enable do { @@ -160,7 +192,7 @@ import AVFoundation Log.directLog(BCTBX_LOG_ERROR, text: "Failed to change audio route: err \(error)") } } - +*/ func requestTransaction(_ transaction: CXTransaction, action: String) { callController.request(transaction) { error in if let error = error { @@ -510,7 +542,7 @@ import AVFoundation if (CallManager.instance().speakerBeforePause) { CallManager.instance().speakerBeforePause = false - CallManager.instance().enableSpeaker(enable: true) + CallManager.instance().changeRouteToSpeaker() CallManager.speaker_already_enabled = true } break @@ -539,7 +571,7 @@ import AVFoundation UIDevice.current.isProximityMonitoringEnabled = false CallManager.speaker_already_enabled = false if (CallManager.instance().lc!.callsNb == 0) { - CallManager.instance().enableSpeaker(enable: false) + CallManager.instance().changeRouteToDefault() // disable this because I don't find anygood reason for it: _bluetoothAvailable = FALSE; // furthermore it introduces a bug when calling multiple times since route may not be // reconfigured between cause leading to bluetooth being disabled while it should not @@ -605,7 +637,7 @@ import AVFoundation if (cstate == .IncomingReceived || cstate == .OutgoingInit || cstate == .Connected || cstate == .StreamsRunning) { if ((call.currentParams?.videoEnabled ?? false) && !CallManager.speaker_already_enabled && !CallManager.instance().bluetoothEnabled) { - CallManager.instance().enableSpeaker(enable: true) + CallManager.instance().changeRouteToSpeaker() CallManager.speaker_already_enabled = true } } diff --git a/Classes/CallOutgoingView.m b/Classes/CallOutgoingView.m index 39391e894..10555ad92 100644 --- a/Classes/CallOutgoingView.m +++ b/Classes/CallOutgoingView.m @@ -127,20 +127,17 @@ static UICompositeViewDescription *compositeDescription = nil; - (IBAction)onRoutesBluetoothClick:(id)sender { [self hideRoutes:TRUE animated:TRUE]; - [CallManager.instance enableSpeakerWithEnable:FALSE]; - [LinphoneManager.instance setBluetoothEnabled:TRUE]; + [CallManager.instance changeRouteToBluetooth]; } - (IBAction)onRoutesEarpieceClick:(id)sender { [self hideRoutes:TRUE animated:TRUE]; - [CallManager.instance enableSpeakerWithEnable:FALSE]; - [LinphoneManager.instance setBluetoothEnabled:FALSE]; + [CallManager.instance changeRouteToDefault]; } - (IBAction)onRoutesSpeakerClick:(id)sender { [self hideRoutes:TRUE animated:TRUE]; - [LinphoneManager.instance setBluetoothEnabled:FALSE]; - [CallManager.instance enableSpeakerWithEnable:TRUE]; + [CallManager.instance changeRouteToSpeaker]; } - (IBAction)onRoutesClick:(id)sender { diff --git a/Classes/CallView.m b/Classes/CallView.m index 705f4ef47..739e0abe1 100644 --- a/Classes/CallView.m +++ b/Classes/CallView.m @@ -803,20 +803,17 @@ static void hideSpinner(LinphoneCall *call, void *user_data) { - (IBAction)onRoutesBluetoothClick:(id)sender { [self hideRoutes:TRUE animated:TRUE]; - [CallManager.instance enableSpeakerWithEnable:FALSE]; - [LinphoneManager.instance setBluetoothEnabled:TRUE]; + [CallManager.instance changeRouteToBluetooth]; } - (IBAction)onRoutesEarpieceClick:(id)sender { [self hideRoutes:TRUE animated:TRUE]; - [CallManager.instance enableSpeakerWithEnable:FALSE]; - [LinphoneManager.instance setBluetoothEnabled:FALSE]; + [CallManager.instance changeRouteToDefault]; } - (IBAction)onRoutesSpeakerClick:(id)sender { [self hideRoutes:TRUE animated:TRUE]; - [LinphoneManager.instance setBluetoothEnabled:FALSE]; - [CallManager.instance enableSpeakerWithEnable:TRUE]; + [CallManager.instance changeRouteToSpeaker]; } - (IBAction)onRoutesClick:(id)sender { diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index 7d06cc6bc..98e4856c4 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -190,7 +190,7 @@ typedef struct _LinphoneManagerSounds { - (void)migrationPerAccount; - (void)setupGSMInteraction; -- (void)setBluetoothEnabled:(BOOL)enable; +//- (void)setBluetoothEnabled:(BOOL)enable; - (BOOL)isCTCallCenterExist; @property (readonly) BOOL isTesting; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 1e1b90a99..1eeccefbc 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -1731,7 +1731,7 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { - (void)audioRouteChangeListenerCallback:(NSNotification *)notif { if (IPAD) return; - + // there is at least one bug when you disconnect an audio bluetooth headset // since we only get notification of route having changed, we cannot tell if that is due to: // -bluetooth headset disconnected or @@ -1760,7 +1760,7 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { userInfo:dict]; } } - +/* - (void)setBluetoothEnabled:(BOOL)enable { if (_bluetoothAvailable) { // The change of route will be done in enableSpeaker @@ -1783,7 +1783,7 @@ static int comp_call_state_paused(const LinphoneCall *call, const void *param) { } [CallManager.instance enableSpeakerWithEnable:CallManager.instance.speakerEnabled]; } - +*/ #pragma mark - Call Functions - (void)send:(NSString *)replyText toChatRoom:(LinphoneChatRoom *)room { LinphoneChatMessage *msg = linphone_chat_room_create_message(room, replyText.UTF8String); diff --git a/Classes/LinphoneUI/UIBluetoothButton.m b/Classes/LinphoneUI/UIBluetoothButton.m index 547222d53..53abc1818 100644 --- a/Classes/LinphoneUI/UIBluetoothButton.m +++ b/Classes/LinphoneUI/UIBluetoothButton.m @@ -29,14 +29,6 @@ if (au != 0) \ LOGE(@"UIBluetoothButton error for %s: ret=%ld", method, au) -- (void)onOn { - [LinphoneManager.instance setBluetoothEnabled:TRUE]; -} - -- (void)onOff { - [LinphoneManager.instance setBluetoothEnabled:FALSE]; -} - - (bool)onUpdate { return false; } diff --git a/Classes/LinphoneUI/UISpeakerButton.m b/Classes/LinphoneUI/UISpeakerButton.m index c3408903d..8ba12c8de 100644 --- a/Classes/LinphoneUI/UISpeakerButton.m +++ b/Classes/LinphoneUI/UISpeakerButton.m @@ -45,16 +45,7 @@ INIT_WITH_COMMON_CF { [self update];}); } -- (void)onOn { - [CallManager.instance enableSpeakerWithEnable:TRUE]; -} - -- (void)onOff { - [CallManager.instance enableSpeakerWithEnable:FALSE]; -} - - (bool)onUpdate { - self.enabled = [CallManager.instance allowSpeaker]; return CallManager.instance.speakerEnabled; } diff --git a/Classes/LinphoneUI/UIVideoButton.m b/Classes/LinphoneUI/UIVideoButton.m index 76083379a..a39195ba0 100644 --- a/Classes/LinphoneUI/UIVideoButton.m +++ b/Classes/LinphoneUI/UIVideoButton.m @@ -58,7 +58,8 @@ INIT_WITH_COMMON_CF { if (!linphone_core_video_display_enabled(LC)) return; - [CallManager.instance enableSpeakerWithEnable:FALSE]; + [CallManager.instance changeRouteToDefault]; + //[CallManager.instance enableSpeakerWithEnable:FALSE]; [self setEnabled:FALSE]; [waitView startAnimating];