From 6d2d5641a51256478f3283d683e7b74a3b8df6f9 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 6 Oct 2023 11:55:56 +0100 Subject: [PATCH] Fix logic a bit as per comments (and... add comments) --- src/settings/SettingsModal.tsx | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/settings/SettingsModal.tsx b/src/settings/SettingsModal.tsx index c9129218..e688b504 100644 --- a/src/settings/SettingsModal.tsx +++ b/src/settings/SettingsModal.tsx @@ -101,27 +101,38 @@ export const SettingsModal = (props: Props) => { // We may present a different device as the currently selected one if we have an active track // from the default device, because the default device may have changed since we acquired the // track, in which case we want to display the one we're actually using rather than what the - // default is now. + // default is now. We only do this if we've selected, and are using, the default device if ( trackUsedByRoom && (devices.selectedId === "" || !devices.selectedId || - devices.selectedId === "default") + devices.selectedId === "default") && + trackUsedByRoom.mediaStreamTrack.getSettings().deviceId === "default" ) { // we work out what the actual device is based on groupId, but this only works if // there is only one such device with the same group ID, which there won't be if // we're using hardware with multiple sub-devices (eg. a multitrack soundcard) const usedGroupId = trackUsedByRoom?.mediaStreamTrack.getSettings().groupId; - const devicesWithMatchingGroupId = devices.available.filter( - (d) => d.groupId === usedGroupId && d.groupId !== "default" - ); + const defaultGroupId = devices.available.find( + (d) => d.deviceId === "default" + )?.groupId; - if (devicesWithMatchingGroupId.length === 1) { - logger.info( - `Current default device doesn't appear to match device in use: selecting ${devicesWithMatchingGroupId[0].label}` + // If the device we're actually using doesn't match tne group ID of what the default is + // now, then display a different one. + if (usedGroupId !== defaultGroupId) { + const devicesWithMatchingGroupId = devices.available.filter( + (d) => d.groupId === usedGroupId && d.deviceId !== "default" ); - selectedKey = devicesWithMatchingGroupId[0].deviceId; + + // One final check: check that there is only one such device matching the group ID. + // If not, we simply can't match up the device correctly: we don't have enough info. + if (devicesWithMatchingGroupId.length === 1) { + logger.info( + `Current default device doesn't appear to match device in use: selecting ${devicesWithMatchingGroupId[0].label}` + ); + selectedKey = devicesWithMatchingGroupId[0].deviceId; + } } }