From 8096a09be6a22e513a67a83ef5a0d8d0beec5958 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Thu, 16 Feb 2023 02:09:08 +0200 Subject: [PATCH] Audio routing fixes --- .../com/tutpro/baresip/BaresipService.kt | 3 +- .../kotlin/com/tutpro/baresip/MainActivity.kt | 7 ++-- .../main/kotlin/com/tutpro/baresip/Utils.kt | 35 ++++++++++--------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 6a11b7a0..af75eb6b 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -1491,6 +1491,7 @@ class BaresipService: Service() { private var btAdapter: BluetoothAdapter? = null fun requestAudioFocus(am: AudioManager, type: Int): Boolean { + Log.d(TAG, "Requesting audio focus of type $type") if (audioFocusRequest != null) { if (audioFocusRequest!!.audioAttributesCompat.contentType == type) return true @@ -1515,7 +1516,7 @@ class BaresipService: Service() { if (type == AudioAttributes.CONTENT_TYPE_SPEECH) am.mode = AudioManager.MODE_IN_COMMUNICATION } else { - Log.d(TAG, "Audio focus denied") + Log.i(TAG, "Audio focus denied") audioFocusRequest = null isAudioFocused = false } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 537dacf5..94c87202 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -197,10 +197,10 @@ class MainActivity : AppCompatActivity() { if (device != null) { Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}") if (speakerIcon != null) { - if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) - speakerIcon!!.setIcon(R.drawable.speaker_off) - else + if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) speakerIcon!!.setIcon(R.drawable.speaker_on) + else + speakerIcon!!.setIcon(R.drawable.speaker_off) } } } @@ -1856,7 +1856,6 @@ class MainActivity : AppCompatActivity() { } } am.addOnModeChangedListener(mainExecutor, audioModeChangedListener!!) - Log.d(TAG, "Setting audio mode to MODE_IN_COMMUNICATION") if (!BaresipService.requestAudioFocus(am, AudioAttributes.CONTENT_TYPE_SPEECH)) Toast.makeText( applicationContext, diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index 1cbb8ec4..338e063e 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -892,7 +892,7 @@ object Utils { fun isSpeakerPhoneOn(am: AudioManager): Boolean { return if (Build.VERSION.SDK_INT >= 31) - am.communicationDevice!!.type != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE + am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER else am.isSpeakerphoneOn } @@ -904,16 +904,10 @@ object Utils { var speakerDevice: AudioDeviceInfo? = null if (enable) { for (device in am.availableCommunicationDevices) - if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) { + if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { speakerDevice = device break } - if (speakerDevice == null) - for (device in am.availableCommunicationDevices) - if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { - speakerDevice = device - break - } } else { for (device in am.availableCommunicationDevices) if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) { @@ -921,7 +915,11 @@ object Utils { break } } - if (speakerDevice != null && current != speakerDevice.type) { + if (speakerDevice == null) { + Log.w(TAG,"Could not find requested communication device") + return + } + if (current != speakerDevice.type) { if (speakerDevice.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) { am.clearCommunicationDevice() Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE") @@ -933,7 +931,7 @@ object Utils { } } else { // Currently at API levels 31+, speakerphone needs normal mode - if (am.mode == AudioManager.MODE_NORMAL) { + if (am.mode == AudioManager.MODE_NORMAL) { Log.d(TAG, "Setting com device to ${speakerDevice.type} in MODE_NORMAL") if (!am.setCommunicationDevice(speakerDevice)) Log.e(TAG, "Could not set com device") @@ -942,8 +940,10 @@ object Utils { override fun onModeChanged(mode: Int) { if (mode == AudioManager.MODE_NORMAL) { am.removeOnModeChangedListener(this) - Log.d(TAG, "Setting com device to ${speakerDevice.type}" + - " in mode ${am.mode}") + Log.d( + TAG, "Setting com device to ${speakerDevice.type}" + + " in mode ${am.mode}" + ) if (!am.setCommunicationDevice(speakerDevice)) Log.e(TAG, "Could not set com device") } @@ -963,11 +963,14 @@ object Utils { } fun toggleSpeakerPhone(executor: Executor, am: AudioManager) { - if (Build.VERSION.SDK_INT >= 31) - setSpeakerPhone(executor, am, - am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) - else + if (Build.VERSION.SDK_INT >= 31) { + if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) + setSpeakerPhone(executor, am, true) + else if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) + setSpeakerPhone(executor, am, false) + } else { setSpeakerPhone(executor, am, !am.isSpeakerphoneOn) + } } }