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.

AudioSettingsContent.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../../app/types';
  5. import { translate } from '../../../../base/i18n/functions';
  6. import { IconMic, IconVolumeUp } from '../../../../base/icons/svg';
  7. import JitsiMeetJS from '../../../../base/lib-jitsi-meet';
  8. import { equals } from '../../../../base/redux/functions';
  9. import Checkbox from '../../../../base/ui/components/web/Checkbox';
  10. import ContextMenu from '../../../../base/ui/components/web/ContextMenu';
  11. import ContextMenuItem from '../../../../base/ui/components/web/ContextMenuItem';
  12. import ContextMenuItemGroup from '../../../../base/ui/components/web/ContextMenuItemGroup';
  13. import { toggleNoiseSuppression } from '../../../../noise-suppression/actions';
  14. import { isNoiseSuppressionEnabled } from '../../../../noise-suppression/functions';
  15. import { isPrejoinPageVisible } from '../../../../prejoin/functions';
  16. import { createLocalAudioTracks } from '../../../functions.web';
  17. import MicrophoneEntry from './MicrophoneEntry';
  18. import SpeakerEntry from './SpeakerEntry';
  19. const browser = JitsiMeetJS.util.browser;
  20. /**
  21. * Translates the default device label into a more user friendly one.
  22. *
  23. * @param {string} deviceId - The device Id.
  24. * @param {string} label - The device label.
  25. * @param {Function} t - The translation function.
  26. * @returns {string}
  27. */
  28. function transformDefaultDeviceLabel(deviceId: string, label: string, t: Function) {
  29. return deviceId === 'default'
  30. ? t('settings.sameAsSystem', { label: label.replace('Default - ', '') })
  31. : label;
  32. }
  33. export interface IProps extends WithTranslation {
  34. /**
  35. * The deviceId of the microphone in use.
  36. */
  37. currentMicDeviceId: string;
  38. /**
  39. * The deviceId of the output device in use.
  40. */
  41. currentOutputDeviceId?: string;
  42. /**
  43. * Used to decide whether to measure audio levels for microphone devices.
  44. */
  45. measureAudioLevels: boolean;
  46. /**
  47. * A list with objects containing the labels and deviceIds
  48. * of all the input devices.
  49. */
  50. microphoneDevices: Array<{ deviceId: string; label: string; }>;
  51. /**
  52. * Whether noise suppression is enabled or not.
  53. */
  54. noiseSuppressionEnabled: boolean;
  55. /**
  56. * A list of objects containing the labels and deviceIds
  57. * of all the output devices.
  58. */
  59. outputDevices: Array<{ deviceId: string; label: string; }>;
  60. /**
  61. * Whether the prejoin page is visible or not.
  62. */
  63. prejoinVisible: boolean;
  64. /**
  65. * Used to set a new microphone as the current one.
  66. */
  67. setAudioInputDevice: Function;
  68. /**
  69. * Used to set a new output device as the current one.
  70. */
  71. setAudioOutputDevice: Function;
  72. /**
  73. * Function to toggle noise suppression.
  74. */
  75. toggleSuppression: () => void;
  76. }
  77. type State = {
  78. /**
  79. * An list of objects, each containing the microphone label, audio track, device id
  80. * and track error if the case.
  81. */
  82. audioTracks: Array<{ deviceId: string; hasError: boolean; jitsiTrack: any; label: string; }>;
  83. };
  84. /**
  85. * Implements a React {@link Component} which displays a list of all
  86. * the audio input & output devices to choose from.
  87. *
  88. * @augments Component
  89. */
  90. class AudioSettingsContent extends Component<IProps, State> {
  91. _componentWasUnmounted: boolean;
  92. microphoneHeaderId = 'microphone_settings_header';
  93. speakerHeaderId = 'speaker_settings_header';
  94. /**
  95. * Initializes a new {@code AudioSettingsContent} instance.
  96. *
  97. * @param {Object} props - The read-only properties with which the new
  98. * instance is to be initialized.
  99. */
  100. constructor(props: IProps) {
  101. super(props);
  102. this._onMicrophoneEntryClick = this._onMicrophoneEntryClick.bind(this);
  103. this._onSpeakerEntryClick = this._onSpeakerEntryClick.bind(this);
  104. this.state = {
  105. audioTracks: props.microphoneDevices.map(({ deviceId, label }) => {
  106. return {
  107. deviceId,
  108. hasError: false,
  109. jitsiTrack: null,
  110. label
  111. };
  112. })
  113. };
  114. }
  115. /**
  116. * Click handler for the microphone entries.
  117. *
  118. * @param {string} deviceId - The deviceId for the clicked microphone.
  119. * @returns {void}
  120. */
  121. _onMicrophoneEntryClick(deviceId: string) {
  122. this.props.setAudioInputDevice(deviceId);
  123. }
  124. /**
  125. * Click handler for the speaker entries.
  126. *
  127. * @param {string} deviceId - The deviceId for the clicked speaker.
  128. * @returns {void}
  129. */
  130. _onSpeakerEntryClick(deviceId: string) {
  131. this.props.setAudioOutputDevice(deviceId);
  132. }
  133. /**
  134. * Renders a single microphone entry.
  135. *
  136. * @param {Object} data - An object with the deviceId, jitsiTrack & label of the microphone.
  137. * @param {number} index - The index of the element, used for creating a key.
  138. * @param {length} length - The length of the microphone list.
  139. * @param {Function} t - The translation function.
  140. * @returns {React$Node}
  141. */
  142. _renderMicrophoneEntry(data: { deviceId: string; hasError: boolean; jitsiTrack: any; label: string; },
  143. index: number, length: number, t: Function) {
  144. const { deviceId, jitsiTrack, hasError } = data;
  145. const label = transformDefaultDeviceLabel(deviceId, data.label, t);
  146. const isSelected = deviceId === this.props.currentMicDeviceId;
  147. return (
  148. <MicrophoneEntry
  149. deviceId = { deviceId }
  150. hasError = { hasError }
  151. index = { index }
  152. isSelected = { isSelected }
  153. jitsiTrack = { jitsiTrack }
  154. key = { `me-${index}` }
  155. length = { length }
  156. listHeaderId = { this.microphoneHeaderId }
  157. measureAudioLevels = { this.props.measureAudioLevels }
  158. onClick = { this._onMicrophoneEntryClick }>
  159. {label}
  160. </MicrophoneEntry>
  161. );
  162. }
  163. /**
  164. * Renders a single speaker entry.
  165. *
  166. * @param {Object} data - An object with the deviceId and label of the speaker.
  167. * @param {number} index - The index of the element, used for creating a key.
  168. * @param {length} length - The length of the speaker list.
  169. * @param {Function} t - The translation function.
  170. * @returns {React$Node}
  171. */
  172. _renderSpeakerEntry(data: { deviceId: string; label: string; }, index: number, length: number, t: Function) {
  173. const { deviceId } = data;
  174. const label = transformDefaultDeviceLabel(deviceId, data.label, t);
  175. const key = `se-${index}`;
  176. const isSelected = deviceId === this.props.currentOutputDeviceId;
  177. return (
  178. <SpeakerEntry
  179. deviceId = { deviceId }
  180. index = { index }
  181. isSelected = { isSelected }
  182. key = { key }
  183. length = { length }
  184. listHeaderId = { this.speakerHeaderId }
  185. onClick = { this._onSpeakerEntryClick }>
  186. {label}
  187. </SpeakerEntry>
  188. );
  189. }
  190. /**
  191. * Creates and updates the audio tracks.
  192. *
  193. * @returns {void}
  194. */
  195. async _setTracks() {
  196. if (browser.isWebKitBased()) {
  197. // It appears that at the time of this writing, creating audio tracks blocks the browser's main thread for
  198. // long time on safari. Wasn't able to confirm which part of track creation does the blocking exactly, but
  199. // not creating the tracks seems to help and makes the UI much more responsive.
  200. return;
  201. }
  202. this._disposeTracks(this.state.audioTracks);
  203. const audioTracks = await createLocalAudioTracks(this.props.microphoneDevices, 5000);
  204. if (this._componentWasUnmounted) {
  205. this._disposeTracks(audioTracks);
  206. } else {
  207. this.setState({
  208. audioTracks
  209. });
  210. }
  211. }
  212. /**
  213. * Disposes the audio tracks.
  214. *
  215. * @param {Object} audioTracks - The object holding the audio tracks.
  216. * @returns {void}
  217. */
  218. _disposeTracks(audioTracks: Array<{ jitsiTrack: any; }>) {
  219. audioTracks.forEach(({ jitsiTrack }) => {
  220. jitsiTrack?.dispose();
  221. });
  222. }
  223. /**
  224. * Implements React's {@link Component#componentDidMount}.
  225. *
  226. * @inheritdoc
  227. */
  228. componentDidMount() {
  229. this._setTracks();
  230. }
  231. /**
  232. * Implements React's {@link Component#componentWillUnmount}.
  233. *
  234. * @inheritdoc
  235. */
  236. componentWillUnmount() {
  237. this._componentWasUnmounted = true;
  238. this._disposeTracks(this.state.audioTracks);
  239. }
  240. /**
  241. * Implements React's {@link Component#componentDidUpdate}.
  242. *
  243. * @inheritdoc
  244. */
  245. componentDidUpdate(prevProps: IProps) {
  246. if (!equals(this.props.microphoneDevices, prevProps.microphoneDevices)) {
  247. this._setTracks();
  248. }
  249. }
  250. /**
  251. * Implements React's {@link Component#render}.
  252. *
  253. * @inheritdoc
  254. */
  255. render() {
  256. const { outputDevices, t, noiseSuppressionEnabled, toggleSuppression, prejoinVisible } = this.props;
  257. return (
  258. <ContextMenu
  259. aria-labelledby = 'audio-settings-button'
  260. className = 'audio-preview-content'
  261. hidden = { false }
  262. id = 'audio-settings-dialog'
  263. tabIndex = { -1 }>
  264. <ContextMenuItemGroup>
  265. <ContextMenuItem
  266. accessibilityLabel = { t('settings.microphones') }
  267. className = 'audio-preview-header'
  268. icon = { IconMic }
  269. id = { this.microphoneHeaderId }
  270. text = { t('settings.microphones') } />
  271. <ul
  272. aria-labelledby = { this.microphoneHeaderId }
  273. className = 'audio-preview-content-ul'
  274. role = 'radiogroup'
  275. tabIndex = { -1 }>
  276. {this.state.audioTracks.map((data, i) =>
  277. this._renderMicrophoneEntry(data, i, this.state.audioTracks.length, t)
  278. )}
  279. </ul>
  280. </ContextMenuItemGroup>
  281. { outputDevices.length > 0 && (
  282. <ContextMenuItemGroup>
  283. <ContextMenuItem
  284. accessibilityLabel = { t('settings.speakers') }
  285. className = 'audio-preview-header'
  286. icon = { IconVolumeUp }
  287. id = { this.speakerHeaderId }
  288. text = { t('settings.speakers') } />
  289. <ul
  290. aria-labelledby = { this.speakerHeaderId }
  291. className = 'audio-preview-content-ul'
  292. role = 'radiogroup'
  293. tabIndex = { -1 }>
  294. { outputDevices.map((data, i) =>
  295. this._renderSpeakerEntry(data, i, outputDevices.length, t)
  296. )}
  297. </ul>
  298. </ContextMenuItemGroup>)
  299. }
  300. {!prejoinVisible && (
  301. <ContextMenuItemGroup>
  302. <div
  303. className = 'audio-preview-checkbox-container'
  304. // eslint-disable-next-line react/jsx-no-bind
  305. onClick = { e => e.stopPropagation() }>
  306. <Checkbox
  307. checked = { noiseSuppressionEnabled }
  308. label = { t('toolbar.noiseSuppression') }
  309. onChange = { toggleSuppression } />
  310. </div>
  311. </ContextMenuItemGroup>
  312. )}
  313. </ContextMenu>
  314. );
  315. }
  316. }
  317. const mapStateToProps = (state: IReduxState) => {
  318. return {
  319. noiseSuppressionEnabled: isNoiseSuppressionEnabled(state),
  320. prejoinVisible: isPrejoinPageVisible(state)
  321. };
  322. };
  323. const mapDispatchToProps = (dispatch: IStore['dispatch']) => {
  324. return {
  325. toggleSuppression() {
  326. dispatch(toggleNoiseSuppression());
  327. }
  328. };
  329. };
  330. export default translate(connect(mapStateToProps, mapDispatchToProps)(AudioSettingsContent));