You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioDevicesSelection.web.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import React from 'react';
  4. import { WithTranslation } from 'react-i18next';
  5. import { connect } from 'react-redux';
  6. import { IReduxState, IStore } from '../../app/types';
  7. import { getAvailableDevices } from '../../base/devices/actions.web';
  8. import AbstractDialogTab, {
  9. type IProps as AbstractDialogTabProps
  10. } from '../../base/dialog/components/web/AbstractDialogTab';
  11. import { translate } from '../../base/i18n/functions';
  12. import { createLocalTrack } from '../../base/lib-jitsi-meet/functions.web';
  13. import Checkbox from '../../base/ui/components/web/Checkbox';
  14. import { iAmVisitor as iAmVisitorCheck } from '../../visitors/functions';
  15. import logger from '../logger';
  16. import AudioInputPreview from './AudioInputPreview';
  17. import AudioOutputPreview from './AudioOutputPreview';
  18. import DeviceHidContainer from './DeviceHidContainer.web';
  19. import DeviceSelector from './DeviceSelector.web';
  20. /**
  21. * The type of the React {@code Component} props of {@link AudioDevicesSelection}.
  22. */
  23. interface IProps extends AbstractDialogTabProps, WithTranslation {
  24. /**
  25. * All known audio and video devices split by type. This prop comes from
  26. * the app state.
  27. */
  28. availableDevices: {
  29. audioInput?: MediaDeviceInfo[];
  30. audioOutput?: MediaDeviceInfo[];
  31. };
  32. /**
  33. * CSS classes object.
  34. */
  35. classes: any;
  36. /**
  37. * Whether or not the audio selector can be interacted with. If true,
  38. * the audio input selector will be rendered as disabled. This is
  39. * specifically used to prevent audio device changing in Firefox, which
  40. * currently does not work due to a browser-side regression.
  41. */
  42. disableAudioInputChange: boolean;
  43. /**
  44. * True if device changing is configured to be disallowed. Selectors
  45. * will display as disabled.
  46. */
  47. disableDeviceChange: boolean;
  48. /**
  49. * Redux dispatch function.
  50. */
  51. dispatch: IStore['dispatch'];
  52. /**
  53. * Whether or not the audio permission was granted.
  54. */
  55. hasAudioPermission: boolean;
  56. /**
  57. * If true, the audio meter will not display. Necessary for browsers or
  58. * configurations that do not support local stats to prevent a
  59. * non-responsive mic preview from displaying.
  60. */
  61. hideAudioInputPreview: boolean;
  62. /**
  63. * If true, the button to play a test sound on the selected speaker will not be displayed.
  64. * This needs to be hidden on browsers that do not support selecting an audio output device.
  65. */
  66. hideAudioOutputPreview: boolean;
  67. /**
  68. * Whether or not the audio output source selector should display. If
  69. * true, the audio output selector and test audio link will not be
  70. * rendered.
  71. */
  72. hideAudioOutputSelect: boolean;
  73. /**
  74. * Whether or not the hid device container should display.
  75. */
  76. hideDeviceHIDContainer: boolean;
  77. /**
  78. * Whether to hide noise suppression checkbox or not.
  79. */
  80. hideNoiseSuppression: boolean;
  81. /**
  82. * Whether we are in visitors mode.
  83. */
  84. iAmVisitor: boolean;
  85. /**
  86. * Wether noise suppression is on or not.
  87. */
  88. noiseSuppressionEnabled: boolean;
  89. /**
  90. * The id of the audio input device to preview.
  91. */
  92. selectedAudioInputId: string;
  93. /**
  94. * The id of the audio output device to preview.
  95. */
  96. selectedAudioOutputId: string;
  97. }
  98. /**
  99. * The type of the React {@code Component} state of {@link AudioDevicesSelection}.
  100. */
  101. interface IState {
  102. /**
  103. * The JitsiTrack to use for previewing audio input.
  104. */
  105. previewAudioTrack?: any | null;
  106. }
  107. const styles = (theme: Theme) => {
  108. return {
  109. container: {
  110. display: 'flex',
  111. flexDirection: 'column' as const,
  112. padding: '0 2px',
  113. width: '100%'
  114. },
  115. inputContainer: {
  116. marginBottom: theme.spacing(3)
  117. },
  118. outputContainer: {
  119. margin: `${theme.spacing(5)} 0`,
  120. display: 'flex',
  121. alignItems: 'flex-end'
  122. },
  123. outputButton: {
  124. marginLeft: theme.spacing(3)
  125. },
  126. noiseSuppressionContainer: {
  127. marginBottom: theme.spacing(5)
  128. }
  129. };
  130. };
  131. /**
  132. * React {@code Component} for previewing audio and video input/output devices.
  133. *
  134. * @augments Component
  135. */
  136. class AudioDevicesSelection extends AbstractDialogTab<IProps, IState> {
  137. /**
  138. * Whether current component is mounted or not.
  139. *
  140. * In component did mount we start a Promise to create tracks and
  141. * set the tracks in the state, if we unmount the component in the meanwhile
  142. * tracks will be created and will never been disposed (dispose tracks is
  143. * in componentWillUnmount). When tracks are created and component is
  144. * unmounted we dispose the tracks.
  145. */
  146. _unMounted: boolean;
  147. /**
  148. * Initializes a new DeviceSelection instance.
  149. *
  150. * @param {Object} props - The read-only React Component props with which
  151. * the new instance is to be initialized.
  152. */
  153. constructor(props: IProps) {
  154. super(props);
  155. this.state = {
  156. previewAudioTrack: null
  157. };
  158. this._unMounted = true;
  159. }
  160. /**
  161. * Generate the initial previews for audio input and video input.
  162. *
  163. * @inheritdoc
  164. */
  165. componentDidMount() {
  166. this._unMounted = false;
  167. Promise.all([
  168. this._createAudioInputTrack(this.props.selectedAudioInputId)
  169. ])
  170. .catch(err => logger.warn('Failed to initialize preview tracks', err))
  171. .then(() => {
  172. this.props.dispatch(getAvailableDevices());
  173. });
  174. }
  175. /**
  176. * Checks if audio / video permissions were granted. Updates audio input and
  177. * video input previews.
  178. *
  179. * @param {Object} prevProps - Previous props this component received.
  180. * @returns {void}
  181. */
  182. componentDidUpdate(prevProps: IProps) {
  183. if (prevProps.selectedAudioInputId
  184. !== this.props.selectedAudioInputId) {
  185. this._createAudioInputTrack(this.props.selectedAudioInputId);
  186. }
  187. }
  188. /**
  189. * Ensure preview tracks are destroyed to prevent continued use.
  190. *
  191. * @inheritdoc
  192. */
  193. componentWillUnmount() {
  194. this._unMounted = true;
  195. this._disposeAudioInputPreview();
  196. }
  197. /**
  198. * Implements React's {@link Component#render()}.
  199. *
  200. * @inheritdoc
  201. */
  202. render() {
  203. const {
  204. classes,
  205. hasAudioPermission,
  206. hideAudioInputPreview,
  207. hideAudioOutputPreview,
  208. hideDeviceHIDContainer,
  209. hideNoiseSuppression,
  210. iAmVisitor,
  211. noiseSuppressionEnabled,
  212. selectedAudioOutputId,
  213. t
  214. } = this.props;
  215. const { audioInput, audioOutput } = this._getSelectors();
  216. return (
  217. <div className = { classes.container }>
  218. {!iAmVisitor && <div
  219. aria-live = 'polite'
  220. className = { classes.inputContainer }>
  221. {this._renderSelector(audioInput)}
  222. </div>}
  223. {!hideAudioInputPreview && hasAudioPermission && !iAmVisitor
  224. && <AudioInputPreview
  225. track = { this.state.previewAudioTrack } />}
  226. <div
  227. aria-live = 'polite'
  228. className = { classes.outputContainer }>
  229. {this._renderSelector(audioOutput)}
  230. {!hideAudioOutputPreview && hasAudioPermission
  231. && <AudioOutputPreview
  232. className = { classes.outputButton }
  233. deviceId = { selectedAudioOutputId } />}
  234. </div>
  235. {!hideNoiseSuppression && !iAmVisitor && (
  236. <div className = { classes.noiseSuppressionContainer }>
  237. <Checkbox
  238. checked = { noiseSuppressionEnabled }
  239. label = { t('toolbar.enableNoiseSuppression') }
  240. // eslint-disable-next-line react/jsx-no-bind
  241. onChange = { () => super._onChange({
  242. noiseSuppressionEnabled: !noiseSuppressionEnabled
  243. }) } />
  244. </div>
  245. )}
  246. {!hideDeviceHIDContainer && !iAmVisitor
  247. && <DeviceHidContainer />}
  248. </div>
  249. );
  250. }
  251. /**
  252. * Creates the JitsiTrack for the audio input preview.
  253. *
  254. * @param {string} deviceId - The id of audio input device to preview.
  255. * @private
  256. * @returns {void}
  257. */
  258. _createAudioInputTrack(deviceId: string) {
  259. const { hideAudioInputPreview } = this.props;
  260. if (hideAudioInputPreview) {
  261. return;
  262. }
  263. return this._disposeAudioInputPreview()
  264. .then(() => createLocalTrack('audio', deviceId, 5000))
  265. .then(jitsiLocalTrack => {
  266. if (this._unMounted) {
  267. jitsiLocalTrack.dispose();
  268. return;
  269. }
  270. this.setState({
  271. previewAudioTrack: jitsiLocalTrack
  272. });
  273. })
  274. .catch(() => {
  275. this.setState({
  276. previewAudioTrack: null
  277. });
  278. });
  279. }
  280. /**
  281. * Utility function for disposing the current audio input preview.
  282. *
  283. * @private
  284. * @returns {Promise}
  285. */
  286. _disposeAudioInputPreview(): Promise<any> {
  287. return this.state.previewAudioTrack
  288. ? this.state.previewAudioTrack.dispose() : Promise.resolve();
  289. }
  290. /**
  291. * Creates a DeviceSelector instance based on the passed in configuration.
  292. *
  293. * @private
  294. * @param {Object} deviceSelectorProps - The props for the DeviceSelector.
  295. * @returns {ReactElement}
  296. */
  297. _renderSelector(deviceSelectorProps: any) {
  298. return deviceSelectorProps ? (
  299. <DeviceSelector
  300. { ...deviceSelectorProps }
  301. key = { deviceSelectorProps.id } />
  302. ) : null;
  303. }
  304. /**
  305. * Returns object configurations for audio input and output.
  306. *
  307. * @private
  308. * @returns {Object} Configurations.
  309. */
  310. _getSelectors() {
  311. const { availableDevices, hasAudioPermission } = this.props;
  312. const audioInput = {
  313. devices: availableDevices.audioInput,
  314. hasPermission: hasAudioPermission,
  315. icon: 'icon-microphone',
  316. isDisabled: this.props.disableAudioInputChange || this.props.disableDeviceChange,
  317. key: 'audioInput',
  318. id: 'audioInput',
  319. label: 'settings.selectMic',
  320. onSelect: (selectedAudioInputId: string) => super._onChange({ selectedAudioInputId }),
  321. selectedDeviceId: this.state.previewAudioTrack
  322. ? this.state.previewAudioTrack.getDeviceId() : this.props.selectedAudioInputId
  323. };
  324. let audioOutput;
  325. if (!this.props.hideAudioOutputSelect) {
  326. audioOutput = {
  327. devices: availableDevices.audioOutput,
  328. hasPermission: hasAudioPermission,
  329. icon: 'icon-speaker',
  330. isDisabled: this.props.disableDeviceChange,
  331. key: 'audioOutput',
  332. id: 'audioOutput',
  333. label: 'settings.selectAudioOutput',
  334. onSelect: (selectedAudioOutputId: string) => super._onChange({ selectedAudioOutputId }),
  335. selectedDeviceId: this.props.selectedAudioOutputId
  336. };
  337. }
  338. return { audioInput,
  339. audioOutput };
  340. }
  341. }
  342. const mapStateToProps = (state: IReduxState) => {
  343. return {
  344. availableDevices: state['features/base/devices'].availableDevices ?? {},
  345. iAmVisitor: iAmVisitorCheck(state)
  346. };
  347. };
  348. export default connect(mapStateToProps)(withStyles(styles)(translate(AudioDevicesSelection)));