From e7812c7d84af0117456a70671b20948cd24e828f Mon Sep 17 00:00:00 2001 From: virtuacoplenny Date: Mon, 8 Apr 2019 10:03:45 -0700 Subject: [PATCH] fix(device-selection): search for device by label and kind (#4064) Searching for a device (id) by label alone can result in false results when devices share labels, such as a mic and speaker having the same label. To prevent such, specify the device kind to be found instead of iterating over all device kinds. --- react/features/base/devices/actions.js | 2 +- react/features/base/devices/functions.js | 25 +++++++++++++------- react/features/device-selection/functions.js | 4 +++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/react/features/base/devices/actions.js b/react/features/base/devices/actions.js index 76b4fdc5c..3e1743b0e 100644 --- a/react/features/base/devices/actions.js +++ b/react/features/base/devices/actions.js @@ -75,7 +75,7 @@ export function configureInitialDevices() { Object.keys(deviceLabels).forEach(key => { const label = deviceLabels[key]; - const deviceId = getDeviceIdByLabel(state, label); + const deviceId = getDeviceIdByLabel(state, label, key); if (deviceId) { newSettings[devicesKeysToSettingsKeys[key]] = deviceId; diff --git a/react/features/base/devices/functions.js b/react/features/base/devices/functions.js index 247cfaee0..d2c78229b 100644 --- a/react/features/base/devices/functions.js +++ b/react/features/base/devices/functions.js @@ -44,19 +44,26 @@ export function getAudioOutputDeviceId() { * * @param {Object} state - The redux state. * @param {string} label - The label. + * @param {string} kind - The type of the device. One of "audioInput", + * "audioOutput", and "videoInput". Also supported is all lowercase versions + * of the preceding types. * @returns {string|undefined} */ -export function getDeviceIdByLabel(state: Object, label: string) { - const types = [ 'audioInput', 'audioOutput', 'videoInput' ]; +export function getDeviceIdByLabel(state: Object, label: string, kind: string) { + const webrtcKindToJitsiKindTranslator = { + audioinput: 'audioInput', + audiooutput: 'audioOutput', + videoinput: 'videoInput' + }; - for (const type of types) { - const device - = (state['features/base/devices'].availableDevices[type] || []) - .find(d => d.label === label); + const kindToSearch = webrtcKindToJitsiKindTranslator[kind] || kind; - if (device) { - return device.deviceId; - } + const device + = (state['features/base/devices'].availableDevices[kindToSearch] || []) + .find(d => d.label === label); + + if (device) { + return device.deviceId; } } diff --git a/react/features/device-selection/functions.js b/react/features/device-selection/functions.js index 02571a968..5fe6f0d2d 100644 --- a/react/features/device-selection/functions.js +++ b/react/features/device-selection/functions.js @@ -155,7 +155,9 @@ export function processExternalDeviceRequest( // eslint-disable-line max-params } const { label, id } = device; - const deviceId = label ? getDeviceIdByLabel(state, device.label) : id; + const deviceId = label + ? getDeviceIdByLabel(state, device.label, device.kind) + : id; if (deviceId) { switch (device.kind) {