Fix audio output selection for Element Call (#4602)

* Fix audio output selection.

* Ensure that Element Call audio output uses a new connected device, even during a call.
Also add a few logs.

* Extract functions.

* Add more log and protect from crash.

* Revert formatting change

* Update screenshots

---------

Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
Benoit Marty 2025-04-17 10:36:42 +02:00 committed by GitHub
parent 7ed362b9db
commit be1c9b793b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 17 deletions

View file

@ -8,6 +8,9 @@
package io.element.android.features.call.impl.ui
import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioDeviceCallback
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.util.Log
import android.view.ViewGroup
@ -22,6 +25,10 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
@ -151,15 +158,11 @@ private fun CallWebView(
Text("WebView - can't be previewed")
}
} else {
var audioDeviceCallback: AudioDeviceCallback? by remember { mutableStateOf(null) }
AndroidView(
modifier = modifier,
factory = { context ->
// Set 'voice call' mode so volume keys actually control the call volume
val audioManager = context.getSystemService<AudioManager>()
audioManager?.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager?.enableExternalAudioDevice()
audioDeviceCallback = context.setupAudioConfiguration()
WebView(context).apply {
onWebViewCreate(this)
setup(userAgent, onPermissionsRequest)
@ -172,16 +175,40 @@ private fun CallWebView(
},
onRelease = { webView ->
// Reset audio mode
val audioManager = webView.context.getSystemService<AudioManager>()
audioManager?.disableExternalAudioDevice()
audioManager?.mode = AudioManager.MODE_NORMAL
webView.context.releaseAudioConfiguration(audioDeviceCallback)
webView.destroy()
}
)
}
}
private fun Context.setupAudioConfiguration(): AudioDeviceCallback? {
val audioManager = getSystemService<AudioManager>() ?: return null
// Set 'voice call' mode so volume keys actually control the call volume
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager.enableExternalAudioDevice()
return object : AudioDeviceCallback() {
override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) {
Timber.d("Audio devices added")
audioManager.enableExternalAudioDevice()
}
override fun onAudioDevicesRemoved(removedDevices: Array<out AudioDeviceInfo>?) {
Timber.d("Audio devices removed")
audioManager.enableExternalAudioDevice()
}
}.also {
audioManager.registerAudioDeviceCallback(it, null)
}
}
private fun Context.releaseAudioConfiguration(audioDeviceCallback: AudioDeviceCallback?) {
val audioManager = getSystemService<AudioManager>() ?: return
audioManager.unregisterAudioDeviceCallback(audioDeviceCallback)
audioManager.disableExternalAudioDevice()
audioManager.mode = AudioManager.MODE_NORMAL
}
@SuppressLint("SetJavaScriptEnabled")
private fun WebView.setup(
userAgent: String,

View file

@ -10,6 +10,8 @@ package io.element.android.libraries.androidutils.compat
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.os.Build
import io.element.android.libraries.core.data.tryOrNull
import timber.log.Timber
fun AudioManager.enableExternalAudioDevice() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@ -30,10 +32,26 @@ fun AudioManager.enableExternalAudioDevice() {
AudioDeviceInfo.TYPE_BUILTIN_EARPIECE,
)
val devices = availableCommunicationDevices
val selectedDevice = devices.find {
wantedDeviceTypes.contains(it.type)
val selectedDevice = devices.minByOrNull {
wantedDeviceTypes.indexOf(it.type).let { index ->
// If the device type is not in the wantedDeviceTypes list, we give it a low priority
if (index == -1) Int.MAX_VALUE else index
}
}
selectedDevice?.let { device ->
Timber.d("Audio device selected, type: ${device.type}")
tryOrNull(
onError = { failure ->
Timber.e(failure, "Audio: exception when setting communication device")
}
) {
setCommunicationDevice(device).also {
if (!it) {
Timber.w("Audio: unable to set the communication device")
}
}
}
}
selectedDevice?.let { setCommunicationDevice(it) }
} else {
// If we don't have access to the new APIs, use the deprecated ones
@Suppress("DEPRECATION")

View file

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ddbe76235df4343660750e42512a431d10911442db54a2441fcb204ee4a6eb38
size 230808
oid sha256:03afa6a6d6c2b3ce7fd8b665e3bdf2b1cfdfe8414449817ec5d8c8f4ec5c6d7e
size 227835

View file

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bcc7cc69fdcae48da59f2bec88950aad996b869813d7f569bb205235efbd2731
size 233656
oid sha256:d6ac3b50611b79a15d5d80fdbc3790fda66f7877d58e65c64362577e377bd4db
size 230660