diff --git a/app/src/main/java/org/linphone/activities/main/chat/data/ChatMessageContentData.kt b/app/src/main/java/org/linphone/activities/main/chat/data/ChatMessageContentData.kt index a1d243b75..412b99fce 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/data/ChatMessageContentData.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/data/ChatMessageContentData.kt @@ -42,6 +42,7 @@ import org.linphone.R import org.linphone.core.* import org.linphone.core.tools.Log import org.linphone.utils.AppUtils +import org.linphone.utils.AudioRouteUtils import org.linphone.utils.FileUtils import org.linphone.utils.TimestampUtils @@ -438,33 +439,7 @@ class ChatMessageContentData( private fun initVoiceRecordPlayer() { Log.i("[Voice Recording] Creating player for voice record") - // In case no headphones/headset/hearing aid/bluetooth is connected, use speaker sound card to play recordings, otherwise use earpiece - // If none are available, default one will be used - var headphonesCard: String? = null - var bluetoothCard: String? = null - var speakerCard: String? = null - var earpieceCard: String? = null - for (device in coreContext.core.audioDevices) { - if (device.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { - when (device.type) { - AudioDevice.Type.Headphones, AudioDevice.Type.Headset, AudioDevice.Type.HearingAid -> { - headphonesCard = device.id - } - AudioDevice.Type.Bluetooth -> { - bluetoothCard = device.id - } - AudioDevice.Type.Speaker -> { - speakerCard = device.id - } - AudioDevice.Type.Earpiece -> { - earpieceCard = device.id - } - else -> {} - } - } - } - Log.i("[Voice Recording] Found headset/headphones/hearingAid sound card [$headphonesCard], bluetooth sound card [$bluetoothCard], speaker sound card [$speakerCard] and earpiece sound card [$earpieceCard]") - val playbackSoundCard = headphonesCard ?: bluetoothCard ?: speakerCard ?: earpieceCard + val playbackSoundCard = AudioRouteUtils.getAudioPlaybackDeviceIdForCallRecordingOrVoiceMessage() Log.i("[Voice Recording] Using device $playbackSoundCard to make the voice message playback") val localPlayer = coreContext.core.createLocalPlayer(playbackSoundCard, null, null) diff --git a/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageSendingViewModel.kt b/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageSendingViewModel.kt index 042bbb4f8..46333493b 100644 --- a/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageSendingViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/chat/viewmodels/ChatMessageSendingViewModel.kt @@ -41,10 +41,8 @@ import org.linphone.activities.main.chat.data.ChatMessageData import org.linphone.compatibility.Compatibility import org.linphone.core.* import org.linphone.core.tools.Log -import org.linphone.utils.AppUtils +import org.linphone.utils.* import org.linphone.utils.Event -import org.linphone.utils.FileUtils -import org.linphone.utils.PermissionHelper class ChatMessageSendingViewModelFactory(private val chatRoom: ChatRoom) : ViewModelProvider.NewInstanceFactory() { @@ -464,29 +462,8 @@ class ChatMessageSendingViewModel(private val chatRoom: ChatRoom) : ViewModel() recorderParams.fileFormat = RecorderFileFormat.Wav } - // In case no headphones/headset/hearing aid/bluetooth is connected, use microphone - // If none are available, default one will be used - var bluetoothAudioDevice: AudioDevice? = null - var headsetAudioDevice: AudioDevice? = null - var builtinMicrophone: AudioDevice? = null - for (device in coreContext.core.audioDevices) { - if (device.hasCapability(AudioDevice.Capabilities.CapabilityRecord)) { - when (device.type) { - AudioDevice.Type.Bluetooth -> { - bluetoothAudioDevice = device - } - AudioDevice.Type.Headset, AudioDevice.Type.HearingAid, AudioDevice.Type.Headphones -> { - headsetAudioDevice = device - } - AudioDevice.Type.Microphone -> { - builtinMicrophone = device - } - else -> {} - } - } - } - Log.i("[Chat Message Sending] Found headset/headphones/hearingAid [${headsetAudioDevice?.id}], bluetooth [${bluetoothAudioDevice?.id}] and builtin microphone [${builtinMicrophone?.id}]") - recorderParams.audioDevice = headsetAudioDevice ?: bluetoothAudioDevice ?: builtinMicrophone + val recordingAudioDevice = AudioRouteUtils.getAudioRecordingDeviceForVoiceMessage() + recorderParams.audioDevice = recordingAudioDevice Log.i("[Chat Message Sending] Using device ${recorderParams.audioDevice?.id} to make the voice message recording") recorder = coreContext.core.createRecorder(recorderParams) @@ -494,33 +471,8 @@ class ChatMessageSendingViewModel(private val chatRoom: ChatRoom) : ViewModel() private fun initVoiceRecordPlayer() { Log.i("[Chat Message Sending] Creating player for voice record") - // In case no headphones/headset/hearing aid/bluetooth is connected, use speaker sound card to play recordings, otherwise use earpiece - // If none are available, default one will be used - var headphonesCard: String? = null - var bluetoothCard: String? = null - var speakerCard: String? = null - var earpieceCard: String? = null - for (device in coreContext.core.audioDevices) { - if (device.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { - when (device.type) { - AudioDevice.Type.Headphones, AudioDevice.Type.Headset, AudioDevice.Type.HearingAid -> { - headphonesCard = device.id - } - AudioDevice.Type.Bluetooth -> { - bluetoothCard = device.id - } - AudioDevice.Type.Speaker -> { - speakerCard = device.id - } - AudioDevice.Type.Earpiece -> { - earpieceCard = device.id - } - else -> {} - } - } - } - Log.i("[Chat Message Sending] Found headset/headphones/hearingAid sound card [$headphonesCard], bluetooth sound card [$bluetoothCard], speaker sound card [$speakerCard] and earpiece sound card [$earpieceCard]") - val playbackSoundCard = headphonesCard ?: bluetoothCard ?: speakerCard ?: earpieceCard + + val playbackSoundCard = AudioRouteUtils.getAudioPlaybackDeviceIdForCallRecordingOrVoiceMessage() Log.i("[Chat Message Sending] Using device $playbackSoundCard to make the voice message playback") val localPlayer = coreContext.core.createLocalPlayer(playbackSoundCard, null, null) diff --git a/app/src/main/java/org/linphone/activities/main/recordings/data/RecordingData.kt b/app/src/main/java/org/linphone/activities/main/recordings/data/RecordingData.kt index a4b155173..6512c77dd 100644 --- a/app/src/main/java/org/linphone/activities/main/recordings/data/RecordingData.kt +++ b/app/src/main/java/org/linphone/activities/main/recordings/data/RecordingData.kt @@ -29,10 +29,10 @@ import java.util.regex.Pattern import kotlinx.coroutines.* import kotlinx.coroutines.channels.ticker import org.linphone.LinphoneApplication.Companion.coreContext -import org.linphone.core.AudioDevice import org.linphone.core.Player import org.linphone.core.PlayerListener import org.linphone.core.tools.Log +import org.linphone.utils.AudioRouteUtils import org.linphone.utils.LinphoneUtils class RecordingData(val path: String, private val recordingListener: RecordingListener) : Comparable { @@ -79,7 +79,7 @@ class RecordingData(val path: String, private val recordingListener: RecordingLi width: Int, height: Int ) { - Log.i("[Recording VM] Surface texture should be available now") + Log.i("[Recording] Surface texture should be available now") player.setWindowId(surface) } } @@ -157,7 +157,7 @@ class RecordingData(val path: String, private val recordingListener: RecordingLi } fun setTextureView(textureView: TextureView) { - Log.i("[Recording VM] Is TextureView available? ${textureView.isAvailable}") + Log.i("[Recording] Is TextureView available? ${textureView.isAvailable}") if (textureView.isAvailable) { player.setWindowId(textureView.surfaceTexture) } else { @@ -170,32 +170,12 @@ class RecordingData(val path: String, private val recordingListener: RecordingLi } private fun initPlayer() { - // In case no headphones/headset is connected, use speaker sound card to play recordings, otherwise use earpiece - // If none are available, default one will be used - var headphonesCard: String? = null - var speakerCard: String? = null - var earpieceCard: String? = null - for (device in coreContext.core.audioDevices) { - if (device.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { - when (device.type) { - AudioDevice.Type.Speaker -> { - speakerCard = device.id - } - AudioDevice.Type.Earpiece -> { - earpieceCard = device.id - } - AudioDevice.Type.Headphones, AudioDevice.Type.Headset -> { - headphonesCard = device.id - } - else -> {} - } - } - } - Log.i("[Recording VM] Found headset/headphones sound card [$headphonesCard], speaker sound card [$speakerCard] and earpiece sound card [$earpieceCard]") + val playbackSoundCard = AudioRouteUtils.getAudioPlaybackDeviceIdForCallRecordingOrVoiceMessage() + Log.i("[Recording] Using device $playbackSoundCard to make the call recording playback") - val localPlayer = coreContext.core.createLocalPlayer(headphonesCard ?: speakerCard ?: earpieceCard, null, null) + val localPlayer = coreContext.core.createLocalPlayer(playbackSoundCard, null, null) if (localPlayer != null) player = localPlayer - else Log.e("[Recording VM] Couldn't create local player!") + else Log.e("[Recording] Couldn't create local player!") player.addListener(listener) player.open(path) diff --git a/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt b/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt index 3a0a60b59..68d18dc34 100644 --- a/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt +++ b/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt @@ -250,5 +250,61 @@ class AudioRouteUtils { } return false } + + fun getAudioPlaybackDeviceIdForCallRecordingOrVoiceMessage(): String? { + // In case no headphones/headset/hearing aid/bluetooth is connected, use speaker sound card to play recordings, otherwise use earpiece + // If none are available, default one will be used + var headphonesCard: String? = null + var bluetoothCard: String? = null + var speakerCard: String? = null + var earpieceCard: String? = null + for (device in coreContext.core.audioDevices) { + if (device.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { + when (device.type) { + AudioDevice.Type.Headphones, AudioDevice.Type.Headset, AudioDevice.Type.HearingAid -> { + headphonesCard = device.id + } + AudioDevice.Type.Bluetooth -> { + bluetoothCard = device.id + } + AudioDevice.Type.Speaker -> { + speakerCard = device.id + } + AudioDevice.Type.Earpiece -> { + earpieceCard = device.id + } + else -> {} + } + } + } + Log.i("[Audio Route Helper] Found headset/headphones/hearingAid sound card [$headphonesCard], bluetooth sound card [$bluetoothCard], speaker sound card [$speakerCard] and earpiece sound card [$earpieceCard]") + return headphonesCard ?: bluetoothCard ?: speakerCard ?: earpieceCard + } + + fun getAudioRecordingDeviceForVoiceMessage(): AudioDevice? { + // In case no headphones/headset/hearing aid/bluetooth is connected, use microphone + // If none are available, default one will be used + var bluetoothAudioDevice: AudioDevice? = null + var headsetAudioDevice: AudioDevice? = null + var builtinMicrophone: AudioDevice? = null + for (device in coreContext.core.audioDevices) { + if (device.hasCapability(AudioDevice.Capabilities.CapabilityRecord)) { + when (device.type) { + AudioDevice.Type.Bluetooth -> { + bluetoothAudioDevice = device + } + AudioDevice.Type.Headset, AudioDevice.Type.HearingAid, AudioDevice.Type.Headphones -> { + headsetAudioDevice = device + } + AudioDevice.Type.Microphone -> { + builtinMicrophone = device + } + else -> {} + } + } + } + Log.i("[Audio Route Helper] Found headset/headphones/hearingAid [${headsetAudioDevice?.id}], bluetooth [${bluetoothAudioDevice?.id}] and builtin microphone [${builtinMicrophone?.id}]") + return headsetAudioDevice ?: bluetoothAudioDevice ?: builtinMicrophone + } } }