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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { translate } from '../../../../base/i18n';
  4. import { IconMicrophoneHollow, IconVolumeEmpty } 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 set a new microphone as the current one.
  36. */
  37. setAudioInputDevice: Function,
  38. /**
  39. * Used to set a new output device as the current one.
  40. */
  41. setAudioOutputDevice: Function,
  42. /**
  43. * A list of objects containing the labels and deviceIds
  44. * of all the output devices.
  45. */
  46. outputDevices: Object[],
  47. /**
  48. * A list with objects containing the labels and deviceIds
  49. * of all the input devices.
  50. */
  51. microphoneDevices: Object[],
  52. /**
  53. * Invoked to obtain translated strings.
  54. */
  55. t: Function
  56. };
  57. type State = {
  58. /**
  59. * An list of objects, each containing the microphone label, audio track, device id
  60. * and track error if the case.
  61. */
  62. audioTracks: Object[]
  63. }
  64. /**
  65. * Implements a React {@link Component} which displays a list of all
  66. * the audio input & output devices to choose from.
  67. *
  68. * @extends Component
  69. */
  70. class AudioSettingsContent extends Component<Props, State> {
  71. _componentWasUnmounted: boolean;
  72. /**
  73. * Initializes a new {@code AudioSettingsContent} instance.
  74. *
  75. * @param {Object} props - The read-only properties with which the new
  76. * instance is to be initialized.
  77. */
  78. constructor(props) {
  79. super(props);
  80. this._onMicrophoneEntryClick = this._onMicrophoneEntryClick.bind(this);
  81. this._onSpeakerEntryClick = this._onSpeakerEntryClick.bind(this);
  82. this.state = {
  83. audioTracks: props.microphoneDevices.map(({ deviceId, label }) => {
  84. return {
  85. deviceId,
  86. hasError: false,
  87. jitsiTrack: null,
  88. label
  89. };
  90. })
  91. };
  92. }
  93. _onMicrophoneEntryClick: (string) => void;
  94. /**
  95. * Click handler for the microphone entries.
  96. *
  97. * @param {string} deviceId - The deviceId for the clicked microphone.
  98. * @returns {void}
  99. */
  100. _onMicrophoneEntryClick(deviceId) {
  101. this.props.setAudioInputDevice(deviceId);
  102. }
  103. _onSpeakerEntryClick: (string) => void;
  104. /**
  105. * Click handler for the speaker entries.
  106. *
  107. * @param {string} deviceId - The deviceId for the clicked speaker.
  108. * @returns {void}
  109. */
  110. _onSpeakerEntryClick(deviceId) {
  111. this.props.setAudioOutputDevice(deviceId);
  112. }
  113. /**
  114. * Renders a single microphone entry.
  115. *
  116. * @param {Object} data - An object with the deviceId, jitsiTrack & label of the microphone.
  117. * @param {number} index - The index of the element, used for creating a key.
  118. * @param {Function} t - The translation function.
  119. * @returns {React$Node}
  120. */
  121. _renderMicrophoneEntry(data, index, t) {
  122. const { deviceId, jitsiTrack, hasError } = data;
  123. const label = transformDefaultDeviceLabel(deviceId, data.label, t);
  124. const isSelected = deviceId === this.props.currentMicDeviceId;
  125. return (
  126. <MicrophoneEntry
  127. deviceId = { deviceId }
  128. hasError = { hasError }
  129. isSelected = { isSelected }
  130. jitsiTrack = { jitsiTrack }
  131. key = { `me-${index}` }
  132. onClick = { this._onMicrophoneEntryClick }>
  133. {label}
  134. </MicrophoneEntry>
  135. );
  136. }
  137. /**
  138. * Renders a single speaker entry.
  139. *
  140. * @param {Object} data - An object with the deviceId and label of the speaker.
  141. * @param {number} index - The index of the element, used for creating a key.
  142. * @param {Function} t - The translation function.
  143. * @returns {React$Node}
  144. */
  145. _renderSpeakerEntry(data, index, t) {
  146. const { deviceId } = data;
  147. const label = transformDefaultDeviceLabel(deviceId, data.label, t);
  148. const key = `se-${index}`;
  149. return (
  150. <SpeakerEntry
  151. deviceId = { deviceId }
  152. isSelected = { deviceId === this.props.currentOutputDeviceId }
  153. key = { key }
  154. onClick = { this._onSpeakerEntryClick }>
  155. {label}
  156. </SpeakerEntry>
  157. );
  158. }
  159. /**
  160. * Creates and updates the audio tracks.
  161. *
  162. * @returns {void}
  163. */
  164. async _setTracks() {
  165. if (browser.isWebKitBased()) {
  166. // It appears that at the time of this writing, creating audio tracks blocks the browser's main thread for
  167. // long time on safari. Wasn't able to confirm which part of track creation does the blocking exactly, but
  168. // not creating the tracks seems to help and makes the UI much more responsive.
  169. return;
  170. }
  171. this._disposeTracks(this.state.audioTracks);
  172. const audioTracks = await createLocalAudioTracks(this.props.microphoneDevices, 5000);
  173. if (this._componentWasUnmounted) {
  174. this._disposeTracks(audioTracks);
  175. } else {
  176. this.setState({
  177. audioTracks
  178. });
  179. }
  180. }
  181. /**
  182. * Disposes the audio tracks.
  183. *
  184. * @param {Object} audioTracks - The object holding the audio tracks.
  185. * @returns {void}
  186. */
  187. _disposeTracks(audioTracks) {
  188. audioTracks.forEach(({ jitsiTrack }) => {
  189. jitsiTrack && jitsiTrack.dispose();
  190. });
  191. }
  192. /**
  193. * Implements React's {@link Component#componentDidMount}.
  194. *
  195. * @inheritdoc
  196. */
  197. componentDidMount() {
  198. this._setTracks();
  199. }
  200. /**
  201. * Implements React's {@link Component#componentWillUnmount}.
  202. *
  203. * @inheritdoc
  204. */
  205. componentWillUnmount() {
  206. this._componentWasUnmounted = true;
  207. this._disposeTracks(this.state.audioTracks);
  208. }
  209. /**
  210. * Implements React's {@link Component#componentDidUpdate}.
  211. *
  212. * @inheritdoc
  213. */
  214. componentDidUpdate(prevProps) {
  215. if (!equals(this.props.microphoneDevices, prevProps.microphoneDevices)) {
  216. this._setTracks();
  217. }
  218. }
  219. /**
  220. * Implements React's {@link Component#render}.
  221. *
  222. * @inheritdoc
  223. */
  224. render() {
  225. const { outputDevices, t } = this.props;
  226. return (
  227. <div>
  228. <div className = 'audio-preview-content'>
  229. <AudioSettingsHeader
  230. IconComponent = { IconMicrophoneHollow }
  231. text = { t('settings.microphones') } />
  232. {this.state.audioTracks.map((data, i) =>
  233. this._renderMicrophoneEntry(data, i, t),
  234. )}
  235. { outputDevices.length > 0 && (
  236. <>
  237. <hr className = 'audio-preview-hr' />
  238. <AudioSettingsHeader
  239. IconComponent = { IconVolumeEmpty }
  240. text = { t('settings.speakers') } />
  241. </>
  242. )
  243. }
  244. {outputDevices.map((data, i) =>
  245. this._renderSpeakerEntry(data, i, t),
  246. )}
  247. </div>
  248. </div>
  249. );
  250. }
  251. }
  252. export default translate(AudioSettingsContent);