Factorized code

This commit is contained in:
Sylvain Berfini 2021-05-27 15:05:04 +02:00
parent 465ac2db74
commit 64f3808c9d

View file

@ -26,105 +26,55 @@ import org.linphone.core.tools.Log
class AudioRouteUtils {
companion object {
fun routeAudioToEarpiece(call: Call? = null) {
private fun routeAudioTo(types : List<AudioDevice.Type>, call: Call? = null) {
val listSize = types.size
val stringBuilder = StringBuilder()
var index = 0
while (index < listSize) {
stringBuilder.append(types[index].name)
if (index < listSize - 1) {
stringBuilder.append("/")
}
index++
}
val typesNames = stringBuilder.toString()
if (coreContext.core.callsNb == 0) {
Log.e("[Audio Route Helper] No call found, aborting earpiece audio route change")
Log.e("[Audio Route Helper] No call found, aborting [$typesNames] audio route change")
return
}
val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0]
val conference = coreContext.core.conference
for (audioDevice in coreContext.core.audioDevices) {
if (audioDevice.type == AudioDevice.Type.Earpiece) {
if (types.contains(audioDevice.type) && audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) {
if (conference != null && conference.isIn) {
Log.i("[Audio Route Helper] Found earpiece audio device [${audioDevice.deviceName}], routing audio of conference to it")
Log.i("[Audio Route Helper] Found [${audioDevice.type}] audio device [${audioDevice.deviceName}], routing conference audio to it")
conference.outputAudioDevice = audioDevice
return
} else {
Log.i("[Audio Route Helper] Found earpiece audio device [${audioDevice.deviceName}], routing audio of call to it")
Log.i("[Audio Route Helper] Found [${audioDevice.type}] audio device [${audioDevice.deviceName}], routing call audio to it")
currentCall.outputAudioDevice = audioDevice
return
}
}
return
}
}
Log.e("[Audio Route Helper] Couldn't find earpiece audio device")
Log.e("[Audio Route Helper] Couldn't find [$typesNames] audio device")
}
fun routeAudioToEarpiece(call: Call? = null) {
routeAudioTo(arrayListOf(AudioDevice.Type.Earpiece), call)
}
fun routeAudioToSpeaker(call: Call? = null) {
if (coreContext.core.callsNb == 0) {
Log.e("[Audio Route Helper] No call found, aborting speaker audio route change")
return
}
val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0]
val conference = coreContext.core.conference
for (audioDevice in coreContext.core.audioDevices) {
if (audioDevice.type == AudioDevice.Type.Speaker) {
if (conference != null && conference.isIn) {
Log.i("[Audio Route Helper] Found speaker audio device [${audioDevice.deviceName}], routing audio of conference to it")
conference.outputAudioDevice = audioDevice
return
} else {
Log.i("[Audio Route Helper] Found speaker audio device [${audioDevice.deviceName}], routing audio of call to it")
currentCall.outputAudioDevice = audioDevice
return
}
}
}
Log.e("[Audio Route Helper] Couldn't find speaker audio device")
routeAudioTo(arrayListOf(AudioDevice.Type.Speaker), call)
}
fun routeAudioToBluetooth(call: Call? = null) {
if (coreContext.core.callsNb == 0) {
Log.e("[Audio Route Helper] No call found, aborting bluetooth audio route change")
return
}
val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0]
val conference = coreContext.core.conference
for (audioDevice in coreContext.core.audioDevices) {
if (audioDevice.type == AudioDevice.Type.Bluetooth) {
if (audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) {
if (conference != null && conference.isIn) {
Log.i("[Audio Route Helper] Found bluetooth audio device [${audioDevice.deviceName}], routing audio of conference to it")
conference.outputAudioDevice = audioDevice
return
} else {
Log.i("[Audio Route Helper] Found bluetooth audio device [${audioDevice.deviceName}], routing audio of call to it")
currentCall.outputAudioDevice = audioDevice
return
}
}
}
}
Log.e("[Audio Route Helper] Couldn't find bluetooth audio device")
routeAudioTo(arrayListOf(AudioDevice.Type.Bluetooth), call)
}
fun routeAudioToHeadset(call: Call? = null) {
if (coreContext.core.callsNb == 0) {
Log.e("[Audio Route Helper] No call found, aborting headset audio route change")
return
}
val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0]
val conference = coreContext.core.conference
for (audioDevice in coreContext.core.audioDevices) {
if (audioDevice.type == AudioDevice.Type.Headphones || audioDevice.type == AudioDevice.Type.Headset) {
if (audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) {
if (conference != null && conference.isIn) {
Log.i("[Audio Route Helper] Found headset audio device [${audioDevice.deviceName}], routing audio of conference to it")
conference.outputAudioDevice = audioDevice
return
} else {
Log.i("[Audio Route Helper] Found headset audio device [${audioDevice.deviceName}], routing audio of call to it")
currentCall.outputAudioDevice = audioDevice
return
}
}
}
}
Log.e("[Audio Route Helper] Couldn't find headset audio device")
routeAudioTo(arrayListOf(AudioDevice.Type.Headphones, AudioDevice.Type.Headset), call)
}
fun isSpeakerAudioRouteCurrentlyUsed(call: Call? = null): Boolean {