Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AudioSettingsContent.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. listHeaderId = { microphoneHeaderId }
  172. measureAudioLevels = { measureAudioLevels }
  173. onClick = { _onMicrophoneEntryClick }>
  174. {label}
  175. </MicrophoneEntry>
  176. );
  177. };
  178. /**
  179. * Renders a single speaker entry.
  180. *
  181. * @param {Object} data - An object with the deviceId and label of the speaker.
  182. * @param {number} index - The index of the element, used for creating a key.
  183. * @param {length} length - The length of the speaker list.
  184. * @returns {React$Node}
  185. */
  186. const _renderSpeakerEntry = (data: { deviceId: string; label: string; }, index: number, length: number) => {
  187. const { deviceId } = data;
  188. const label = transformDefaultDeviceLabel(deviceId, data.label, t);
  189. const key = `se-${index}`;
  190. const isSelected = deviceId === currentOutputDeviceId;
  191. return (
  192. <SpeakerEntry
  193. deviceId = { deviceId }
  194. index = { index }
  195. isSelected = { isSelected }
  196. key = { key }
  197. length = { length }
  198. listHeaderId = { speakerHeaderId }
  199. onClick = { _onSpeakerEntryClick }>
  200. {label}
  201. </SpeakerEntry>
  202. );
  203. };
  204. /**
  205. * Disposes the audio tracks.
  206. *
  207. * @param {Object} tracks - The object holding the audio tracks.
  208. * @returns {void}
  209. */
  210. const _disposeTracks = (tracks: Array<{ jitsiTrack: any; }>) => {
  211. tracks.forEach(({ jitsiTrack }) => {
  212. jitsiTrack?.dispose();
  213. });
  214. };
  215. /**
  216. * Creates and updates the audio tracks.
  217. *
  218. * @returns {void}
  219. */
  220. const _setTracks = async () => {
  221. if (browser.isWebKitBased()) {
  222. // It appears that at the time of this writing, creating audio tracks blocks the browser's main thread for
  223. // long time on safari. Wasn't able to confirm which part of track creation does the blocking exactly, but
  224. // not creating the tracks seems to help and makes the UI much more responsive.
  225. return;
  226. }
  227. _disposeTracks(audioTracks);
  228. const newAudioTracks = await createLocalAudioTracks(microphoneDevices, 5000);
  229. if (_componentWasUnmounted.current) {
  230. _disposeTracks(newAudioTracks);
  231. } else {
  232. setAudioTracks(newAudioTracks);
  233. }
  234. };
  235. useEffect(() => {
  236. _setTracks();
  237. return () => {
  238. _componentWasUnmounted.current = true;
  239. _disposeTracks(audioTracks);
  240. };
  241. }, []);
  242. useEffect(() => {
  243. if (!equals(microphoneDevices, microphoneDevicesRef.current)) {
  244. _setTracks();
  245. microphoneDevicesRef.current = microphoneDevices;
  246. }
  247. }, [ microphoneDevices ]);
  248. return (
  249. <ContextMenu
  250. aria-labelledby = 'audio-settings-button'
  251. className = { classes.contextMenu }
  252. hidden = { false }
  253. id = 'audio-settings-dialog'
  254. tabIndex = { -1 }>
  255. <ContextMenuItemGroup>
  256. <ContextMenuItem
  257. accessibilityLabel = { t('settings.microphones') }
  258. className = { classes.header }
  259. icon = { IconMic }
  260. id = { microphoneHeaderId }
  261. text = { t('settings.microphones') } />
  262. <ul
  263. aria-labelledby = { microphoneHeaderId }
  264. className = { classes.list }
  265. role = 'radiogroup'
  266. tabIndex = { -1 }>
  267. {audioTracks.map((data, i) =>
  268. _renderMicrophoneEntry(data, i, audioTracks.length)
  269. )}
  270. </ul>
  271. </ContextMenuItemGroup>
  272. {outputDevices.length > 0 && (
  273. <ContextMenuItemGroup>
  274. <ContextMenuItem
  275. accessibilityLabel = { t('settings.speakers') }
  276. className = { classes.header }
  277. icon = { IconVolumeUp }
  278. id = { speakerHeaderId }
  279. text = { t('settings.speakers') } />
  280. <ul
  281. aria-labelledby = { speakerHeaderId }
  282. className = { classes.list }
  283. role = 'radiogroup'
  284. tabIndex = { -1 }>
  285. {outputDevices.map((data: any, i: number) =>
  286. _renderSpeakerEntry(data, i, outputDevices.length)
  287. )}
  288. </ul>
  289. </ContextMenuItemGroup>)
  290. }
  291. {!prejoinVisible && (
  292. <ContextMenuItemGroup>
  293. <div
  294. className = { classes.checkboxContainer }
  295. // eslint-disable-next-line react/jsx-no-bind
  296. onClick = { e => e.stopPropagation() }>
  297. <Checkbox
  298. checked = { noiseSuppressionEnabled }
  299. label = { t('toolbar.noiseSuppression') }
  300. onChange = { toggleSuppression } />
  301. </div>
  302. </ContextMenuItemGroup>
  303. )}
  304. </ContextMenu>
  305. );
  306. };
  307. const mapStateToProps = (state: IReduxState) => {
  308. return {
  309. noiseSuppressionEnabled: isNoiseSuppressionEnabled(state),
  310. prejoinVisible: isPrejoinPageVisible(state)
  311. };
  312. };
  313. const mapDispatchToProps = (dispatch: IStore['dispatch']) => {
  314. return {
  315. toggleSuppression() {
  316. dispatch(toggleNoiseSuppression());
  317. }
  318. };
  319. };
  320. export default connect(mapStateToProps, mapDispatchToProps)(AudioSettingsContent);