Skip to content

Commit

Permalink
Improve support for device switching
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar committed Aug 7, 2024
1 parent 3892ea9 commit 6acdd61
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,12 @@ class MicrophoneManager(
}

if (canHandleDeviceSwitch()) {
audioHandler = TelecomCompat.listenForDevices(
audioHandler = TelecomCompat.getAudioHandler(
context = mediaManager.context,
listener = { devices, selected ->
logger.i { "[setup] listenForDevices. Selected: $selected, available: $devices" }
logger.i {
"[setup] listenForDevices. Selected: ${selected?.name}, available: ${devices.map { it.name }}"
}

_devices.value = devices
_selectedDevice.value = selected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import io.getstream.video.android.model.StreamCallId
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -161,14 +161,15 @@ internal class TelecomHandler private constructor(
callControlScope = this
streamToTelecomEventBridge.onEvent(this)

val availableEndpoints = callControlScope?.availableEndpoints
val selectedEndpoint = callControlScope?.currentCallEndpoint

availableEndpoints
?.map { endpointList -> endpointList.map { it.toStreamAudioDevice() } }
?.onEach { deviceListener?.invoke(it, null) }
?.distinctUntilChanged()
?.launchIn(this) // TODO-Telecom: manage 2 scopes?
combine(availableEndpoints, currentCallEndpoint) { list, device ->
Pair(
list.map { it.toStreamAudioDevice() },
device.toStreamAudioDevice(),
)
}
.distinctUntilChanged()
.onEach { deviceListener?.invoke(it.first, it.second) }
.launchIn(this)
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal object TelecomCompat {
}

@TargetApi(Build.VERSION_CODES.O)
fun listenForDevices(context: Context, listener: AvailableDevicesListener): AudioHandler {
fun getAudioHandler(context: Context, listener: AvailableDevicesListener): AudioHandler {
val applicationContext = context.applicationContext // TODO-Telecom: Abstract out in one place
val isTelecomSupported = TelecomHandler.isSupported(applicationContext)
val telecomHandler = TelecomHandler.getInstance(applicationContext)
Expand Down

0 comments on commit 6acdd61

Please sign in to comment.