2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-04-05 05:09:35 +08:00
|
|
|
import {
|
2022-04-12 03:40:16 +08:00
|
|
|
defineMessages,
|
2022-04-05 05:09:35 +08:00
|
|
|
} from 'react-intl';
|
2022-05-03 23:07:16 +08:00
|
|
|
import Styled from './styles';
|
2023-02-23 22:23:51 +08:00
|
|
|
import { uniqueId } from '/imports/utils/string-utils';
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2022-04-05 05:09:35 +08:00
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
kind: PropTypes.oneOf(['audioinput', 'audiooutput']),
|
2016-12-09 02:17:17 +08:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2022-04-13 05:32:06 +08:00
|
|
|
blocked: PropTypes.bool,
|
2022-04-28 22:38:35 +08:00
|
|
|
deviceId: PropTypes.string,
|
2024-06-05 19:26:27 +08:00
|
|
|
devices: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
deviceId: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
|
|
|
})),
|
|
|
|
supportsTransparentListenOnly: PropTypes.bool.isRequired,
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
kind: 'audioinput',
|
2022-04-13 05:32:06 +08:00
|
|
|
blocked: false,
|
2022-04-28 22:38:35 +08:00
|
|
|
deviceId: '',
|
2024-06-05 19:26:27 +08:00
|
|
|
devices: [],
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
2022-04-05 05:09:35 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
fallbackInputLabel: {
|
|
|
|
id: 'app.audio.audioSettings.fallbackInputLabel',
|
|
|
|
description: 'Audio input device label',
|
|
|
|
},
|
|
|
|
fallbackOutputLabel: {
|
|
|
|
id: 'app.audio.audioSettings.fallbackOutputLabel',
|
|
|
|
description: 'Audio output device label',
|
|
|
|
},
|
|
|
|
defaultOutputDeviceLabel: {
|
|
|
|
id: 'app.audio.audioSettings.defaultOutputDeviceLabel',
|
|
|
|
description: 'Default output device label',
|
|
|
|
},
|
2022-04-13 05:32:06 +08:00
|
|
|
findingDevicesLabel: {
|
|
|
|
id: 'app.audio.audioSettings.findingDevicesLabel',
|
|
|
|
description: 'Finding devices label',
|
|
|
|
},
|
2022-04-05 05:09:35 +08:00
|
|
|
noDeviceFoundLabel: {
|
|
|
|
id: 'app.audio.noDeviceFound',
|
|
|
|
description: 'No audio device found',
|
|
|
|
},
|
2024-06-05 19:26:27 +08:00
|
|
|
noMicListenOnlyLabel: {
|
|
|
|
id: 'app.audio.audioSettings.noMicListenOnly',
|
|
|
|
description: 'No microphone, listen only mode label',
|
|
|
|
},
|
2022-04-05 05:09:35 +08:00
|
|
|
});
|
|
|
|
|
2016-12-09 02:17:17 +08:00
|
|
|
class DeviceSelector extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.handleSelectChange = this.handleSelectChange.bind(this);
|
2022-04-13 05:32:06 +08:00
|
|
|
}
|
2016-12-09 02:17:17 +08:00
|
|
|
|
2022-04-19 04:05:26 +08:00
|
|
|
handleSelectChange(event) {
|
|
|
|
const { value } = event.target;
|
2024-06-05 19:26:27 +08:00
|
|
|
const { devices, onChange } = this.props;
|
|
|
|
const selectedDeviceId = (value === 'listen-only')
|
|
|
|
? value
|
|
|
|
: devices.find((d) => d.deviceId === value)?.deviceId;
|
|
|
|
|
|
|
|
onChange(selectedDeviceId);
|
2022-04-19 04:05:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getFallbackLabel(index) {
|
|
|
|
const { intl, kind } = this.props;
|
|
|
|
const label = kind === 'audioinput' ? intlMessages.fallbackInputLabel : intlMessages.fallbackOutputLabel;
|
|
|
|
|
|
|
|
return intl.formatMessage(label, { 0: index });
|
|
|
|
}
|
|
|
|
|
2016-12-09 02:17:17 +08:00
|
|
|
render() {
|
2018-12-04 05:24:18 +08:00
|
|
|
const {
|
2024-06-05 19:26:27 +08:00
|
|
|
intl,
|
|
|
|
kind,
|
|
|
|
blocked,
|
|
|
|
deviceId,
|
|
|
|
devices,
|
|
|
|
supportsTransparentListenOnly,
|
2018-12-04 05:24:18 +08:00
|
|
|
} = this.props;
|
2019-02-06 19:48:01 +08:00
|
|
|
|
2024-06-05 19:26:27 +08:00
|
|
|
const options = devices.map((d, i) => ({
|
|
|
|
label: d.label || this.getFallbackLabel(i),
|
|
|
|
value: d.deviceId,
|
|
|
|
key: uniqueId('device-option-'),
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (kind === 'audioinput' && supportsTransparentListenOnly && !blocked) {
|
|
|
|
options.push({
|
|
|
|
label: intl.formatMessage(intlMessages.noMicListenOnlyLabel),
|
|
|
|
value: 'listen-only',
|
|
|
|
key: uniqueId('device-option-'),
|
|
|
|
});
|
|
|
|
}
|
2016-12-09 02:17:17 +08:00
|
|
|
|
2021-08-09 22:24:02 +08:00
|
|
|
let notFoundOption;
|
|
|
|
|
2022-04-13 05:32:06 +08:00
|
|
|
if (blocked) {
|
|
|
|
notFoundOption = <option value="finding">{intl.formatMessage(intlMessages.findingDevicesLabel)}</option>;
|
2023-04-02 17:28:40 +08:00
|
|
|
} else if (kind === 'audiooutput' && !('setSinkId' in HTMLMediaElement.prototype)) {
|
2022-04-05 05:09:35 +08:00
|
|
|
const defaultOutputDeviceLabel = intl.formatMessage(intlMessages.defaultOutputDeviceLabel);
|
|
|
|
notFoundOption = <option value="not-found">{defaultOutputDeviceLabel}</option>;
|
2021-08-09 22:24:02 +08:00
|
|
|
} else {
|
2022-04-05 05:09:35 +08:00
|
|
|
const noDeviceFoundLabel = intl.formatMessage(intlMessages.noDeviceFoundLabel);
|
|
|
|
notFoundOption = <option value="not-found">{noDeviceFoundLabel}</option>;
|
2021-08-09 22:24:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-09 02:17:17 +08:00
|
|
|
return (
|
2022-05-03 23:07:16 +08:00
|
|
|
<Styled.Select
|
2022-04-28 22:38:35 +08:00
|
|
|
value={deviceId}
|
2016-12-20 01:11:43 +08:00
|
|
|
onChange={this.handleSelectChange}
|
2017-06-03 03:25:02 +08:00
|
|
|
disabled={!options.length}
|
|
|
|
>
|
2016-12-20 01:11:43 +08:00
|
|
|
{
|
2019-06-29 05:45:50 +08:00
|
|
|
options.length
|
2021-08-09 22:24:02 +08:00
|
|
|
? options.map((option) => (
|
2016-12-20 01:11:43 +08:00
|
|
|
<option
|
2017-11-01 01:34:06 +08:00
|
|
|
key={option.key}
|
2017-06-03 03:25:02 +08:00
|
|
|
value={option.value}
|
|
|
|
>
|
2016-12-20 01:11:43 +08:00
|
|
|
{option.label}
|
|
|
|
</option>
|
2019-06-29 05:45:50 +08:00
|
|
|
))
|
2021-08-09 22:24:02 +08:00
|
|
|
: notFoundOption
|
2016-12-20 01:11:43 +08:00
|
|
|
}
|
2022-05-03 23:07:16 +08:00
|
|
|
</Styled.Select>
|
2016-12-09 02:17:17 +08:00
|
|
|
);
|
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
DeviceSelector.propTypes = propTypes;
|
|
|
|
DeviceSelector.defaultProps = defaultProps;
|
|
|
|
|
|
|
|
export default DeviceSelector;
|