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

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