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

AudioSettingsContent.js 7.0KB

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