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

283 lines
7.8 KiB
React
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
2022-02-15 04:20:50 +08:00
import Button from '/imports/ui/components/common/button/component';
2022-02-15 23:54:55 +08:00
import { withModalMounter } from '/imports/ui/components/common/modal/service';
import AudioTestContainer from '/imports/ui/components/audio/audio-test/container';
2021-11-09 22:33:40 +08:00
import Styled from './styles';
import logger from '/imports/startup/client/logger';
import AudioStreamVolume from '/imports/ui/components/audio/audio-stream-volume/component';
import LocalEcho from '/imports/ui/components/audio/local-echo/component';
import {
getAudioConstraints,
} from '/imports/api/audio/client/bridge/service';
import MediaStreamUtils from '/imports/utils/media-stream-utils';
const propTypes = {
2021-08-09 22:24:02 +08:00
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
changeInputDevice: PropTypes.func.isRequired,
changeOutputDevice: PropTypes.func.isRequired,
handleBack: PropTypes.func.isRequired,
handleConfirmation: PropTypes.func.isRequired,
handleGUMFailure: PropTypes.func.isRequired,
isConnecting: PropTypes.bool.isRequired,
inputDeviceId: PropTypes.string.isRequired,
outputDeviceId: PropTypes.string.isRequired,
withEcho: PropTypes.bool.isRequired,
withVolumeMeter: PropTypes.bool.isRequired,
};
const intlMessages = defineMessages({
backLabel: {
id: 'app.audio.backLabel',
description: 'audio settings back button label',
},
descriptionLabel: {
id: 'app.audio.audioSettings.descriptionLabel',
description: 'audio settings description label',
},
micSourceLabel: {
id: 'app.audio.audioSettings.microphoneSourceLabel',
description: 'Label for mic source',
},
speakerSourceLabel: {
id: 'app.audio.audioSettings.speakerSourceLabel',
description: 'Label for speaker source',
},
testSpeakerLabel: {
id: 'app.audio.audioSettings.testSpeakerLabel',
description: 'Label for the speaker test button',
},
streamVolumeLabel: {
id: 'app.audio.audioSettings.microphoneStreamLabel',
description: 'Label for stream volume',
},
2017-10-05 04:49:11 +08:00
retryLabel: {
id: 'app.audio.joinAudio',
description: 'Confirmation button label',
2017-09-29 21:38:10 +08:00
},
});
class AudioSettings extends React.Component {
constructor(props) {
super(props);
const {
inputDeviceId,
outputDeviceId,
} = props;
this.handleInputChange = this.handleInputChange.bind(this);
this.handleOutputChange = this.handleOutputChange.bind(this);
2017-10-04 04:42:10 +08:00
this.state = {
inputDeviceId,
outputDeviceId,
stream: null,
};
this._isMounted = false;
}
componentDidMount() {
const { inputDeviceId } = this.state;
this._isMounted = true;
this.handleInputChange(inputDeviceId);
}
componentWillUnmount() {
const { stream } = this.state;
this._mounted = false;
if (stream) {
MediaStreamUtils.stopMediaStreamTracks(stream);
}
}
generateInputStream(inputDeviceId) {
const { stream } = this.state;
if (inputDeviceId && stream) {
const currentDeviceId = MediaStreamUtils.extractDeviceIdFromStream(stream, 'audio');
if (currentDeviceId === inputDeviceId) return Promise.resolve(stream);
MediaStreamUtils.stopMediaStreamTracks(stream);
}
const constraints = {
audio: getAudioConstraints({ deviceId: inputDeviceId }),
};
return navigator.mediaDevices.getUserMedia(constraints)
}
2017-10-23 20:41:09 +08:00
handleInputChange(deviceId) {
const {
handleGUMFailure,
changeInputDevice,
withEcho,
withVolumeMeter,
} = this.props;
2017-11-01 01:34:06 +08:00
changeInputDevice(deviceId);
// Only generate input streams if they're going to be used with something
// In this case, the volume meter or local echo test.
if (withEcho || withVolumeMeter) {
this.generateInputStream(deviceId).then((stream) => {
if (!this._isMounted) return;
this.setState({
inputDeviceId: deviceId,
stream,
});
}).catch((error) => {
logger.warn({
logCode: 'audiosettings_gum_failed',
extraInfo: {
deviceId,
errorMessage: error.message,
errorName: error.name,
},
}, `Audio settings gUM failed: ${error.name}`);
handleGUMFailure(error);
});
} else {
this.setState({
inputDeviceId: deviceId,
});
}
}
2017-10-23 20:41:09 +08:00
handleOutputChange(deviceId) {
const {
changeOutputDevice,
withEcho,
} = this.props;
changeOutputDevice(deviceId, withEcho);
this.setState({
outputDeviceId: deviceId,
});
}
renderOutputTest() {
const { withEcho, intl } = this.props;
const { stream } = this.state;
return (
<Styled.Row>
<Styled.SpacedLeftCol>
<Styled.LabelSmall htmlFor="audioTest">
{intl.formatMessage(intlMessages.testSpeakerLabel)}
{!withEcho
? <AudioTestContainer id="audioTest" />
: <LocalEcho
intl={this.props.intl}
stream={stream}
/>
}
</Styled.LabelSmall>
</Styled.SpacedLeftCol>
</Styled.Row>
);
}
renderVolumeMeter() {
const { withVolumeMeter, intl } = this.props;
const { stream } = this.state;
return withVolumeMeter ? (
<Styled.Row>
<Styled.LabelSmallFullWidth htmlFor="audioStreamVolume">
{intl.formatMessage(intlMessages.streamVolumeLabel)}
<AudioStreamVolume
stream={stream}
/>
</Styled.LabelSmallFullWidth>
</Styled.Row>
) : null
}
render() {
const {
2017-09-29 21:38:10 +08:00
isConnecting,
intl,
handleBack,
handleConfirmation,
} = this.props;
2021-08-09 22:24:02 +08:00
const { inputDeviceId, outputDeviceId } = this.state;
return (
2021-11-09 22:33:40 +08:00
<Styled.FormWrapper>
<Styled.Form>
<Styled.Row>
<Styled.AudioNote>
{intl.formatMessage(intlMessages.descriptionLabel)}
2021-11-09 22:33:40 +08:00
</Styled.AudioNote>
</Styled.Row>
<Styled.Row>
<Styled.Col>
<Styled.FormElement>
<Styled.LabelSmall htmlFor="inputDeviceSelector">
{intl.formatMessage(intlMessages.micSourceLabel)}
2021-11-09 22:33:40 +08:00
<Styled.DeviceSelectorSelect
id="inputDeviceSelector"
2021-08-09 22:24:02 +08:00
value={inputDeviceId}
2017-07-06 23:54:18 +08:00
kind="audioinput"
onChange={this.handleInputChange}
intl={intl}
2017-07-06 23:54:18 +08:00
/>
2021-11-09 22:33:40 +08:00
</Styled.LabelSmall>
</Styled.FormElement>
</Styled.Col>
<Styled.Col>
<Styled.FormElement>
<Styled.LabelSmall htmlFor="outputDeviceSelector">
{intl.formatMessage(intlMessages.speakerSourceLabel)}
2021-11-09 22:33:40 +08:00
<Styled.DeviceSelectorSelect
id="outputDeviceSelector"
2021-08-09 22:24:02 +08:00
value={outputDeviceId}
2017-07-06 23:54:18 +08:00
kind="audiooutput"
onChange={this.handleOutputChange}
intl={intl}
2017-07-06 23:54:18 +08:00
/>
2021-11-09 22:33:40 +08:00
</Styled.LabelSmall>
</Styled.FormElement>
</Styled.Col>
</Styled.Row>
{this.renderOutputTest()}
{this.renderVolumeMeter()}
2021-11-09 22:33:40 +08:00
</Styled.Form>
2021-11-09 22:33:40 +08:00
<Styled.EnterAudio>
<Styled.BackButton
label={intl.formatMessage(intlMessages.backLabel)}
size="md"
color="primary"
onClick={handleBack}
2017-09-29 21:38:10 +08:00
disabled={isConnecting}
ghost
/>
2017-09-29 21:38:10 +08:00
<Button
size="md"
color="primary"
2017-10-05 04:49:11 +08:00
label={intl.formatMessage(intlMessages.retryLabel)}
onClick={handleConfirmation}
2017-10-05 04:49:11 +08:00
/>
2021-11-09 22:33:40 +08:00
</Styled.EnterAudio>
</Styled.FormWrapper>
2017-09-29 21:42:08 +08:00
);
}
2017-06-03 03:25:02 +08:00
}
AudioSettings.propTypes = propTypes;
export default withModalMounter(injectIntl(AudioSettings));