您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AudioSettingsContent.js 7.4KB

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