2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
2017-10-23 20:41:09 +08:00
|
|
|
import _ from 'lodash';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-04 05:24:18 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2021-04-01 19:14:24 +08:00
|
|
|
import browserInfo from '/imports/utils/browserInfo';
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
kind: PropTypes.oneOf(['audioinput', 'audiooutput', 'videoinput']),
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-02-16 02:49:40 +08:00
|
|
|
value: PropTypes.string,
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
kind: 'audioinput',
|
2017-02-16 02:49:40 +08:00
|
|
|
value: undefined,
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DeviceSelector extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.handleSelectChange = this.handleSelectChange.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
2017-02-16 02:49:40 +08:00
|
|
|
value: props.value,
|
2016-12-09 02:17:17 +08:00
|
|
|
devices: [],
|
|
|
|
options: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-06-29 05:45:50 +08:00
|
|
|
const { kind } = this.props;
|
2017-10-23 20:41:09 +08:00
|
|
|
const handleEnumerateDevicesSuccess = (deviceInfos) => {
|
2021-08-09 22:24:02 +08:00
|
|
|
const devices = deviceInfos.filter((d) => d.kind === kind);
|
2019-06-29 05:45:50 +08:00
|
|
|
logger.info({
|
|
|
|
logCode: 'audiodeviceselector_component_enumeratedevices_success',
|
|
|
|
extraInfo: {
|
|
|
|
deviceKind: kind,
|
|
|
|
devices,
|
|
|
|
},
|
|
|
|
}, 'Success on enumerateDevices() for audio');
|
2017-10-23 20:41:09 +08:00
|
|
|
this.setState({
|
|
|
|
devices,
|
|
|
|
options: devices.map((d, i) => ({
|
2019-06-29 05:45:50 +08:00
|
|
|
label: d.label || `${kind} - ${i}`,
|
2017-10-23 20:41:09 +08:00
|
|
|
value: d.deviceId,
|
2017-11-01 01:34:06 +08:00
|
|
|
key: _.uniqueId('device-option-'),
|
2017-10-23 20:41:09 +08:00
|
|
|
})),
|
|
|
|
});
|
|
|
|
};
|
2016-12-09 02:17:17 +08:00
|
|
|
|
2017-10-23 20:41:09 +08:00
|
|
|
navigator.mediaDevices
|
|
|
|
.enumerateDevices()
|
2018-12-04 05:24:18 +08:00
|
|
|
.then(handleEnumerateDevicesSuccess)
|
2021-08-09 22:24:02 +08:00
|
|
|
.catch(() => {
|
2019-06-29 05:45:50 +08:00
|
|
|
logger.error({
|
|
|
|
logCode: 'audiodeviceselector_component_enumeratedevices_error',
|
|
|
|
extraInfo: {
|
|
|
|
deviceKind: kind,
|
|
|
|
},
|
|
|
|
}, 'Error on enumerateDevices(): ');
|
2018-12-04 05:24:18 +08:00
|
|
|
});
|
2016-12-09 02:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSelectChange(event) {
|
2018-12-04 05:24:18 +08:00
|
|
|
const { value } = event.target;
|
2019-07-07 06:04:05 +08:00
|
|
|
const { onChange } = this.props;
|
|
|
|
const { devices } = this.state;
|
2016-12-09 02:17:17 +08:00
|
|
|
this.setState({ value }, () => {
|
2021-08-09 22:24:02 +08:00
|
|
|
const selectedDevice = devices.find((d) => d.deviceId === value);
|
2016-12-09 02:17:17 +08:00
|
|
|
onChange(selectedDevice.deviceId, selectedDevice, event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-12-04 05:24:18 +08:00
|
|
|
const {
|
2021-11-10 00:08:01 +08:00
|
|
|
kind, ...props
|
2018-12-04 05:24:18 +08:00
|
|
|
} = this.props;
|
2019-02-06 19:48:01 +08:00
|
|
|
|
2016-12-09 02:17:17 +08:00
|
|
|
const { options, value } = this.state;
|
2021-04-01 19:14:24 +08:00
|
|
|
const { isSafari } = browserInfo;
|
2016-12-09 02:17:17 +08:00
|
|
|
|
2021-08-09 22:24:02 +08:00
|
|
|
let notFoundOption;
|
|
|
|
|
|
|
|
if (kind === 'audiooutput' && isSafari) {
|
|
|
|
notFoundOption = <option value="not-found">Default</option>;
|
|
|
|
} else {
|
|
|
|
notFoundOption = <option value="not-found">{`no ${kind} found`}</option>;
|
|
|
|
}
|
|
|
|
|
2016-12-09 02:17:17 +08:00
|
|
|
return (
|
|
|
|
<select
|
|
|
|
{...props}
|
|
|
|
value={value}
|
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
|
|
|
}
|
2016-12-09 02:17:17 +08:00
|
|
|
</select>
|
|
|
|
);
|
|
|
|
}
|
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;
|