Skip to content

Commit

Permalink
Also change input audio device when routing audio to Bluetooth device…
Browse files Browse the repository at this point in the history
… or headphones/headset
  • Loading branch information
Viish committed Nov 2, 2021
1 parent 19b15e2 commit 806d8f4
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions app/src/main/java/org/linphone/utils/AudioRouteUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import org.linphone.core.tools.Log

class AudioRouteUtils {
companion object {
private fun routeAudioTo(types: List<AudioDevice.Type>, call: Call? = null) {
private fun routeAudioTo(
types: List<AudioDevice.Type>,
call: Call? = null,
output: Boolean = true
) {
val listSize = types.size
val stringBuilder = StringBuilder()
var index = 0
Expand All @@ -45,15 +49,21 @@ class AudioRouteUtils {
}
val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0]
val conference = coreContext.core.conference
val capability = if (output)
AudioDevice.Capabilities.CapabilityPlay
else
AudioDevice.Capabilities.CapabilityRecord

for (audioDevice in coreContext.core.audioDevices) {
if (types.contains(audioDevice.type) && audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) {
if (types.contains(audioDevice.type) && audioDevice.hasCapability(capability)) {
if (conference != null && conference.isIn) {
Log.i("[Audio Route Helper] Found [${audioDevice.type}] audio device [${audioDevice.deviceName}], routing conference audio to it")
conference.outputAudioDevice = audioDevice
Log.i("[Audio Route Helper] Found [${audioDevice.type}] ${if (output) "playback" else "recorder"} audio device [${audioDevice.deviceName}], routing conference audio to it")
if (output) conference.outputAudioDevice = audioDevice
else conference.inputAudioDevice = audioDevice
} else {
Log.i("[Audio Route Helper] Found [${audioDevice.type}] audio device [${audioDevice.deviceName}], routing call audio to it")
currentCall.outputAudioDevice = audioDevice
Log.i("[Audio Route Helper] Found [${audioDevice.type}] ${if (output) "playback" else "recorder"} audio device [${audioDevice.deviceName}], routing call audio to it")
if (output) currentCall.outputAudioDevice = audioDevice
else currentCall.inputAudioDevice = audioDevice
}
return
}
Expand All @@ -71,10 +81,18 @@ class AudioRouteUtils {

fun routeAudioToBluetooth(call: Call? = null) {
routeAudioTo(arrayListOf(AudioDevice.Type.Bluetooth), call)
if (isBluetoothAudioRecorderAvailable()) {
Log.i("[Audio Route Helper] Bluetooth device is able to record audio, also change input audio device")
routeAudioTo(arrayListOf(AudioDevice.Type.Bluetooth), call, false)
}
}

fun routeAudioToHeadset(call: Call? = null) {
routeAudioTo(arrayListOf(AudioDevice.Type.Headphones, AudioDevice.Type.Headset), call)
if (isHeadsetAudioRecorderAvailable()) {
Log.i("[Audio Route Helper] Headphones/headset device is able to record audio, also change input audio device")
routeAudioTo((arrayListOf(AudioDevice.Type.Headphones, AudioDevice.Type.Headset)), call, false)
}
}

fun isSpeakerAudioRouteCurrentlyUsed(call: Call? = null): Boolean {
Expand Down Expand Up @@ -115,6 +133,18 @@ class AudioRouteUtils {
return false
}

private fun isBluetoothAudioRecorderAvailable(): Boolean {
for (audioDevice in coreContext.core.audioDevices) {
if (audioDevice.type == AudioDevice.Type.Bluetooth &&
audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityRecord)
) {
Log.i("[Audio Route Helper] Found bluetooth audio recorder [${audioDevice.deviceName}]")
return true
}
}
return false
}

fun isHeadsetAudioRouteAvailable(): Boolean {
for (audioDevice in coreContext.core.audioDevices) {
if ((audioDevice.type == AudioDevice.Type.Headset || audioDevice.type == AudioDevice.Type.Headphones) &&
Expand All @@ -126,5 +156,17 @@ class AudioRouteUtils {
}
return false
}

private fun isHeadsetAudioRecorderAvailable(): Boolean {
for (audioDevice in coreContext.core.audioDevices) {
if ((audioDevice.type == AudioDevice.Type.Headset || audioDevice.type == AudioDevice.Type.Headphones) &&
audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityRecord)
) {
Log.i("[Audio Route Helper] Found headset/headphones audio recorder [${audioDevice.deviceName}]")
return true
}
}
return false
}
}
}

0 comments on commit 806d8f4

Please sign in to comment.