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.js 11KB

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