| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- import React, { Component } from 'react';
- import { WithTranslation } from 'react-i18next';
- import { connect } from 'react-redux';
-
- import { IReduxState, IStore } from '../../../../app/types';
- import { translate } from '../../../../base/i18n/functions';
- import { IconMic, IconVolumeUp } from '../../../../base/icons/svg';
- import JitsiMeetJS from '../../../../base/lib-jitsi-meet';
- import { equals } from '../../../../base/redux/functions';
- import Checkbox from '../../../../base/ui/components/web/Checkbox';
- import ContextMenu from '../../../../base/ui/components/web/ContextMenu';
- import ContextMenuItem from '../../../../base/ui/components/web/ContextMenuItem';
- import ContextMenuItemGroup from '../../../../base/ui/components/web/ContextMenuItemGroup';
- import { toggleNoiseSuppression } from '../../../../noise-suppression/actions';
- import { isNoiseSuppressionEnabled } from '../../../../noise-suppression/functions';
- import { isPrejoinPageVisible } from '../../../../prejoin/functions';
- import { createLocalAudioTracks } from '../../../functions.web';
-
- import MicrophoneEntry from './MicrophoneEntry';
- import SpeakerEntry from './SpeakerEntry';
-
- const browser = JitsiMeetJS.util.browser;
-
- /**
- * Translates the default device label into a more user friendly one.
- *
- * @param {string} deviceId - The device Id.
- * @param {string} label - The device label.
- * @param {Function} t - The translation function.
- * @returns {string}
- */
- function transformDefaultDeviceLabel(deviceId: string, label: string, t: Function) {
- return deviceId === 'default'
- ? t('settings.sameAsSystem', { label: label.replace('Default - ', '') })
- : label;
- }
-
- export interface IProps extends WithTranslation {
-
- /**
- * The deviceId of the microphone in use.
- */
- currentMicDeviceId: string;
-
- /**
- * The deviceId of the output device in use.
- */
- currentOutputDeviceId?: string;
-
- /**
- * Used to decide whether to measure audio levels for microphone devices.
- */
- measureAudioLevels: boolean;
-
- /**
- * A list with objects containing the labels and deviceIds
- * of all the input devices.
- */
- microphoneDevices: Array<{ deviceId: string; label: string; }>;
-
- /**
- * Whether noise suppression is enabled or not.
- */
- noiseSuppressionEnabled: boolean;
-
- /**
- * A list of objects containing the labels and deviceIds
- * of all the output devices.
- */
- outputDevices: Array<{ deviceId: string; label: string; }>;
-
- /**
- * Whether the prejoin page is visible or not.
- */
- prejoinVisible: boolean;
-
- /**
- * Used to set a new microphone as the current one.
- */
- setAudioInputDevice: Function;
-
- /**
- * Used to set a new output device as the current one.
- */
- setAudioOutputDevice: Function;
-
- /**
- * Function to toggle noise suppression.
- */
- toggleSuppression: () => void;
- }
-
- type State = {
-
- /**
- * An list of objects, each containing the microphone label, audio track, device id
- * and track error if the case.
- */
- audioTracks: Array<{ deviceId: string; hasError: boolean; jitsiTrack: any; label: string; }>;
- };
-
- /**
- * Implements a React {@link Component} which displays a list of all
- * the audio input & output devices to choose from.
- *
- * @augments Component
- */
- class AudioSettingsContent extends Component<IProps, State> {
- _componentWasUnmounted: boolean;
- microphoneHeaderId = 'microphone_settings_header';
- speakerHeaderId = 'speaker_settings_header';
-
-
- /**
- * Initializes a new {@code AudioSettingsContent} instance.
- *
- * @param {Object} props - The read-only properties with which the new
- * instance is to be initialized.
- */
- constructor(props: IProps) {
- super(props);
-
- this._onMicrophoneEntryClick = this._onMicrophoneEntryClick.bind(this);
- this._onSpeakerEntryClick = this._onSpeakerEntryClick.bind(this);
-
- this.state = {
- audioTracks: props.microphoneDevices.map(({ deviceId, label }) => {
- return {
- deviceId,
- hasError: false,
- jitsiTrack: null,
- label
- };
- })
- };
- }
-
- /**
- * Click handler for the microphone entries.
- *
- * @param {string} deviceId - The deviceId for the clicked microphone.
- * @returns {void}
- */
- _onMicrophoneEntryClick(deviceId: string) {
- this.props.setAudioInputDevice(deviceId);
- }
-
- /**
- * Click handler for the speaker entries.
- *
- * @param {string} deviceId - The deviceId for the clicked speaker.
- * @returns {void}
- */
- _onSpeakerEntryClick(deviceId: string) {
- this.props.setAudioOutputDevice(deviceId);
- }
-
- /**
- * Renders a single microphone entry.
- *
- * @param {Object} data - An object with the deviceId, jitsiTrack & label of the microphone.
- * @param {number} index - The index of the element, used for creating a key.
- * @param {length} length - The length of the microphone list.
- * @param {Function} t - The translation function.
- * @returns {React$Node}
- */
- _renderMicrophoneEntry(data: { deviceId: string; hasError: boolean; jitsiTrack: any; label: string; },
- index: number, length: number, t: Function) {
- const { deviceId, jitsiTrack, hasError } = data;
- const label = transformDefaultDeviceLabel(deviceId, data.label, t);
- const isSelected = deviceId === this.props.currentMicDeviceId;
-
- return (
- <MicrophoneEntry
- deviceId = { deviceId }
- hasError = { hasError }
- index = { index }
- isSelected = { isSelected }
- jitsiTrack = { jitsiTrack }
- key = { `me-${index}` }
- length = { length }
- listHeaderId = { this.microphoneHeaderId }
- measureAudioLevels = { this.props.measureAudioLevels }
- onClick = { this._onMicrophoneEntryClick }>
- {label}
- </MicrophoneEntry>
- );
- }
-
- /**
- * Renders a single speaker entry.
- *
- * @param {Object} data - An object with the deviceId and label of the speaker.
- * @param {number} index - The index of the element, used for creating a key.
- * @param {length} length - The length of the speaker list.
- * @param {Function} t - The translation function.
- * @returns {React$Node}
- */
- _renderSpeakerEntry(data: { deviceId: string; label: string; }, index: number, length: number, t: Function) {
- const { deviceId } = data;
- const label = transformDefaultDeviceLabel(deviceId, data.label, t);
- const key = `se-${index}`;
- const isSelected = deviceId === this.props.currentOutputDeviceId;
-
- return (
- <SpeakerEntry
- deviceId = { deviceId }
- index = { index }
- isSelected = { isSelected }
- key = { key }
- length = { length }
- listHeaderId = { this.speakerHeaderId }
- onClick = { this._onSpeakerEntryClick }>
- {label}
- </SpeakerEntry>
- );
- }
-
- /**
- * Creates and updates the audio tracks.
- *
- * @returns {void}
- */
- async _setTracks() {
- if (browser.isWebKitBased()) {
-
- // It appears that at the time of this writing, creating audio tracks blocks the browser's main thread for
- // long time on safari. Wasn't able to confirm which part of track creation does the blocking exactly, but
- // not creating the tracks seems to help and makes the UI much more responsive.
- return;
- }
-
- this._disposeTracks(this.state.audioTracks);
-
- const audioTracks = await createLocalAudioTracks(this.props.microphoneDevices, 5000);
-
- if (this._componentWasUnmounted) {
- this._disposeTracks(audioTracks);
- } else {
- this.setState({
- audioTracks
- });
- }
- }
-
- /**
- * Disposes the audio tracks.
- *
- * @param {Object} audioTracks - The object holding the audio tracks.
- * @returns {void}
- */
- _disposeTracks(audioTracks: Array<{ jitsiTrack: any; }>) {
- audioTracks.forEach(({ jitsiTrack }) => {
- jitsiTrack?.dispose();
- });
- }
-
- /**
- * Implements React's {@link Component#componentDidMount}.
- *
- * @inheritdoc
- */
- componentDidMount() {
- this._setTracks();
- }
-
- /**
- * Implements React's {@link Component#componentWillUnmount}.
- *
- * @inheritdoc
- */
- componentWillUnmount() {
- this._componentWasUnmounted = true;
- this._disposeTracks(this.state.audioTracks);
- }
-
- /**
- * Implements React's {@link Component#componentDidUpdate}.
- *
- * @inheritdoc
- */
- componentDidUpdate(prevProps: IProps) {
- if (!equals(this.props.microphoneDevices, prevProps.microphoneDevices)) {
- this._setTracks();
- }
- }
-
-
- /**
- * Implements React's {@link Component#render}.
- *
- * @inheritdoc
- */
- render() {
- const { outputDevices, t, noiseSuppressionEnabled, toggleSuppression, prejoinVisible } = this.props;
-
- return (
- <ContextMenu
- aria-labelledby = 'audio-settings-button'
- className = 'audio-preview-content'
- hidden = { false }
- id = 'audio-settings-dialog'
- tabIndex = { -1 }>
- <ContextMenuItemGroup>
- <ContextMenuItem
- accessibilityLabel = { t('settings.microphones') }
- className = 'audio-preview-header'
- icon = { IconMic }
- id = { this.microphoneHeaderId }
- text = { t('settings.microphones') } />
- <ul
- aria-labelledby = { this.microphoneHeaderId }
- className = 'audio-preview-content-ul'
- role = 'radiogroup'
- tabIndex = { -1 }>
- {this.state.audioTracks.map((data, i) =>
- this._renderMicrophoneEntry(data, i, this.state.audioTracks.length, t)
- )}
- </ul>
- </ContextMenuItemGroup>
- { outputDevices.length > 0 && (
- <ContextMenuItemGroup>
- <ContextMenuItem
- accessibilityLabel = { t('settings.speakers') }
- className = 'audio-preview-header'
- icon = { IconVolumeUp }
- id = { this.speakerHeaderId }
- text = { t('settings.speakers') } />
- <ul
- aria-labelledby = { this.speakerHeaderId }
- className = 'audio-preview-content-ul'
- role = 'radiogroup'
- tabIndex = { -1 }>
- { outputDevices.map((data, i) =>
- this._renderSpeakerEntry(data, i, outputDevices.length, t)
- )}
- </ul>
- </ContextMenuItemGroup>)
- }
- {!prejoinVisible && (
- <ContextMenuItemGroup>
- <div
- className = 'audio-preview-checkbox-container'
- // eslint-disable-next-line react/jsx-no-bind
- onClick = { e => e.stopPropagation() }>
- <Checkbox
- checked = { noiseSuppressionEnabled }
- label = { t('toolbar.noiseSuppression') }
- onChange = { toggleSuppression } />
- </div>
- </ContextMenuItemGroup>
- )}
- </ContextMenu>
- );
- }
- }
-
- const mapStateToProps = (state: IReduxState) => {
- return {
- noiseSuppressionEnabled: isNoiseSuppressionEnabled(state),
- prejoinVisible: isPrejoinPageVisible(state)
- };
- };
-
- const mapDispatchToProps = (dispatch: IStore['dispatch']) => {
- return {
- toggleSuppression() {
- dispatch(toggleNoiseSuppression());
- }
- };
- };
-
- export default translate(connect(mapStateToProps, mapDispatchToProps)(AudioSettingsContent));
|