bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/audio/device-selector/component.jsx

173 lines
4.5 KiB
React
Raw Normal View History

import React, { Component } from 'react';
2017-10-23 20:41:09 +08:00
import _ from 'lodash';
import PropTypes from 'prop-types';
import logger from '/imports/startup/client/logger';
import browserInfo from '/imports/utils/browserInfo';
import {
defineMessages,
} from 'react-intl';
import Styled from './styles';
const propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
kind: PropTypes.oneOf(['audioinput', 'audiooutput']),
onChange: PropTypes.func.isRequired,
blocked: PropTypes.bool,
deviceId: PropTypes.string,
};
const defaultProps = {
kind: 'audioinput',
blocked: false,
deviceId: '',
};
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',
},
findingDevicesLabel: {
id: 'app.audio.audioSettings.findingDevicesLabel',
description: 'Finding devices label',
},
noDeviceFoundLabel: {
id: 'app.audio.noDeviceFound',
description: 'No audio device found',
},
});
class DeviceSelector extends Component {
constructor(props) {
super(props);
this.handleSelectChange = this.handleSelectChange.bind(this);
this.state = {
devices: [],
options: [],
};
}
componentDidMount() {
const { blocked } = this.props;
if (!blocked) this.enumerate();
}
componentDidUpdate(prevProps) {
const { blocked } = this.props;
if (prevProps.blocked === true && blocked === false) this.enumerate();
}
handleEnumerateDevicesSuccess(deviceInfos) {
const { kind } = this.props;
const devices = deviceInfos.filter((d) => d.kind === kind);
logger.info({
logCode: 'audiodeviceselector_component_enumeratedevices_success',
extraInfo: {
deviceKind: kind,
2017-10-23 20:41:09 +08:00
devices,
},
}, 'Success on enumerateDevices() for audio');
this.setState({
devices,
options: devices.map((d, i) => ({
label: d.label || this.getFallbackLabel(i),
value: d.deviceId,
key: _.uniqueId('device-option-'),
})),
});
}
handleSelectChange(event) {
const { value } = event.target;
const { onChange } = this.props;
const { devices } = this.state;
const selectedDevice = devices.find((d) => d.deviceId === value);
onChange(selectedDevice.deviceId, selectedDevice, event);
}
getFallbackLabel(index) {
const { intl, kind } = this.props;
const label = kind === 'audioinput' ? intlMessages.fallbackInputLabel : intlMessages.fallbackOutputLabel;
return intl.formatMessage(label, { 0: index });
}
enumerate() {
const { kind } = this.props;
2017-10-23 20:41:09 +08:00
navigator.mediaDevices
.enumerateDevices()
.then(this.handleEnumerateDevicesSuccess.bind(this))
2021-08-09 22:24:02 +08:00
.catch(() => {
logger.error({
logCode: 'audiodeviceselector_component_enumeratedevices_error',
extraInfo: {
deviceKind: kind,
},
}, 'Error on enumerateDevices(): ');
});
}
render() {
const {
intl, kind, blocked, deviceId,
} = this.props;
const { options } = this.state;
const { isSafari } = browserInfo;
2021-08-09 22:24:02 +08:00
let notFoundOption;
if (blocked) {
notFoundOption = <option value="finding">{intl.formatMessage(intlMessages.findingDevicesLabel)}</option>;
} else if (kind === 'audiooutput' && isSafari) {
const defaultOutputDeviceLabel = intl.formatMessage(intlMessages.defaultOutputDeviceLabel);
notFoundOption = <option value="not-found">{defaultOutputDeviceLabel}</option>;
2021-08-09 22:24:02 +08:00
} else {
const noDeviceFoundLabel = intl.formatMessage(intlMessages.noDeviceFoundLabel);
notFoundOption = <option value="not-found">{noDeviceFoundLabel}</option>;
2021-08-09 22:24:02 +08:00
}
return (
<Styled.Select
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
{
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>
))
2021-08-09 22:24:02 +08:00
: notFoundOption
2016-12-20 01:11:43 +08:00
}
</Styled.Select>
);
}
2017-06-03 03:25:02 +08:00
}
DeviceSelector.propTypes = propTypes;
DeviceSelector.defaultProps = defaultProps;
export default DeviceSelector;