Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AudioSettingsContent.tsx 11KB

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