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.

DeviceSelection.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // @flow
  2. import React from 'react';
  3. import { AbstractDialogTab } from '../../base/dialog';
  4. import type { Props as AbstractDialogTabProps } from '../../base/dialog';
  5. import { translate } from '../../base/i18n';
  6. import { createLocalTrack } from '../../base/lib-jitsi-meet';
  7. import AudioInputPreview from './AudioInputPreview';
  8. import AudioOutputPreview from './AudioOutputPreview';
  9. import DeviceSelector from './DeviceSelector';
  10. import VideoInputPreview from './VideoInputPreview';
  11. /**
  12. * The type of the React {@code Component} props of {@link DeviceSelection}.
  13. */
  14. export type Props = {
  15. ...$Exact<AbstractDialogTabProps>,
  16. /**
  17. * All known audio and video devices split by type. This prop comes from
  18. * the app state.
  19. */
  20. availableDevices: Object,
  21. /**
  22. * Whether or not the audio selector can be interacted with. If true,
  23. * the audio input selector will be rendered as disabled. This is
  24. * specifically used to prevent audio device changing in Firefox, which
  25. * currently does not work due to a browser-side regression.
  26. */
  27. disableAudioInputChange: boolean,
  28. /**
  29. * True if device changing is configured to be disallowed. Selectors
  30. * will display as disabled.
  31. */
  32. disableDeviceChange: boolean,
  33. /**
  34. * Function that checks whether or not a new audio input source can be
  35. * selected.
  36. */
  37. hasAudioPermission: Function,
  38. /**
  39. * Function that checks whether or not a new video input sources can be
  40. * selected.
  41. */
  42. hasVideoPermission: Function,
  43. /**
  44. * If true, the audio meter will not display. Necessary for browsers or
  45. * configurations that do not support local stats to prevent a
  46. * non-responsive mic preview from displaying.
  47. */
  48. hideAudioInputPreview: boolean,
  49. /**
  50. * Whether or not the audio output source selector should display. If
  51. * true, the audio output selector and test audio link will not be
  52. * rendered.
  53. */
  54. hideAudioOutputSelect: boolean,
  55. /**
  56. * The id of the audio input device to preview.
  57. */
  58. selectedAudioInputId: string,
  59. /**
  60. * The id of the audio output device to preview.
  61. */
  62. selectedAudioOutputId: string,
  63. /**
  64. * The id of the video input device to preview.
  65. */
  66. selectedVideoInputId: string,
  67. /**
  68. * Invoked to obtain translated strings.
  69. */
  70. t: Function
  71. };
  72. /**
  73. * The type of the React {@code Component} state of {@link DeviceSelection}.
  74. */
  75. type State = {
  76. /**
  77. * The JitsiTrack to use for previewing audio input.
  78. */
  79. previewAudioTrack: ?Object,
  80. /**
  81. * The JitsiTrack to use for previewing video input.
  82. */
  83. previewVideoTrack: ?Object,
  84. /**
  85. * The error message from trying to use a video input device.
  86. */
  87. previewVideoTrackError: ?string
  88. };
  89. /**
  90. * React {@code Component} for previewing audio and video input/output devices.
  91. *
  92. * @extends Component
  93. */
  94. class DeviceSelection extends AbstractDialogTab<Props, State> {
  95. /**
  96. * Initializes a new DeviceSelection instance.
  97. *
  98. * @param {Object} props - The read-only React Component props with which
  99. * the new instance is to be initialized.
  100. */
  101. constructor(props: Props) {
  102. super(props);
  103. this.state = {
  104. previewAudioTrack: null,
  105. previewVideoTrack: null,
  106. previewVideoTrackError: null
  107. };
  108. }
  109. /**
  110. * Generate the initial previews for audio input and video input.
  111. *
  112. * @inheritdoc
  113. */
  114. componentDidMount() {
  115. this._createAudioInputTrack(this.props.selectedAudioInputId);
  116. this._createVideoInputTrack(this.props.selectedVideoInputId);
  117. }
  118. /**
  119. * Updates audio input and video input previews.
  120. *
  121. * @inheritdoc
  122. * @param {Object} nextProps - The read-only props which this Component will
  123. * receive.
  124. * @returns {void}
  125. */
  126. componentWillReceiveProps(nextProps: Object) {
  127. const { selectedAudioInputId, selectedVideoInputId } = this.props;
  128. if (selectedAudioInputId !== nextProps.selectedAudioInputId) {
  129. this._createAudioInputTrack(nextProps.selectedAudioInputId);
  130. }
  131. if (selectedVideoInputId !== nextProps.selectedVideoInputId) {
  132. this._createVideoInputTrack(nextProps.selectedVideoInputId);
  133. }
  134. }
  135. /**
  136. * Ensure preview tracks are destroyed to prevent continued use.
  137. *
  138. * @inheritdoc
  139. */
  140. componentWillUnmount() {
  141. this._disposeAudioInputPreview();
  142. this._disposeVideoInputPreview();
  143. }
  144. /**
  145. * Implements React's {@link Component#render()}.
  146. *
  147. * @inheritdoc
  148. */
  149. render() {
  150. const {
  151. hideAudioInputPreview,
  152. hideAudioOutputSelect,
  153. selectedAudioOutputId
  154. } = this.props;
  155. return (
  156. <div className = 'device-selection'>
  157. <div className = 'device-selection-column column-video'>
  158. <div className = 'device-selection-video-container'>
  159. <VideoInputPreview
  160. error = { this.state.previewVideoTrackError }
  161. track = { this.state.previewVideoTrack } />
  162. </div>
  163. { !hideAudioInputPreview
  164. && <AudioInputPreview
  165. track = { this.state.previewAudioTrack } /> }
  166. </div>
  167. <div className = 'device-selection-column column-selectors'>
  168. <div className = 'device-selectors'>
  169. { this._renderSelectors() }
  170. </div>
  171. { !hideAudioOutputSelect
  172. && <AudioOutputPreview
  173. deviceId = { selectedAudioOutputId } /> }
  174. </div>
  175. </div>
  176. );
  177. }
  178. /**
  179. * Creates the JitiTrack for the audio input preview.
  180. *
  181. * @param {string} deviceId - The id of audio input device to preview.
  182. * @private
  183. * @returns {void}
  184. */
  185. _createAudioInputTrack(deviceId) {
  186. this._disposeAudioInputPreview()
  187. .then(() => createLocalTrack('audio', deviceId))
  188. .then(jitsiLocalTrack => {
  189. this.setState({
  190. previewAudioTrack: jitsiLocalTrack
  191. });
  192. })
  193. .catch(() => {
  194. this.setState({
  195. previewAudioTrack: null
  196. });
  197. });
  198. }
  199. /**
  200. * Creates the JitiTrack for the video input preview.
  201. *
  202. * @param {string} deviceId - The id of video device to preview.
  203. * @private
  204. * @returns {void}
  205. */
  206. _createVideoInputTrack(deviceId) {
  207. this._disposeVideoInputPreview()
  208. .then(() => createLocalTrack('video', deviceId))
  209. .then(jitsiLocalTrack => {
  210. if (!jitsiLocalTrack) {
  211. return Promise.reject();
  212. }
  213. this.setState({
  214. previewVideoTrack: jitsiLocalTrack,
  215. previewVideoTrackError: null
  216. });
  217. })
  218. .catch(() => {
  219. this.setState({
  220. previewVideoTrack: null,
  221. previewVideoTrackError:
  222. this.props.t('deviceSelection.previewUnavailable')
  223. });
  224. });
  225. }
  226. /**
  227. * Utility function for disposing the current audio input preview.
  228. *
  229. * @private
  230. * @returns {Promise}
  231. */
  232. _disposeAudioInputPreview(): Promise<*> {
  233. return this.state.previewAudioTrack
  234. ? this.state.previewAudioTrack.dispose() : Promise.resolve();
  235. }
  236. /**
  237. * Utility function for disposing the current video input preview.
  238. *
  239. * @private
  240. * @returns {Promise}
  241. */
  242. _disposeVideoInputPreview(): Promise<*> {
  243. return this.state.previewVideoTrack
  244. ? this.state.previewVideoTrack.dispose() : Promise.resolve();
  245. }
  246. /**
  247. * Creates a DeviceSelector instance based on the passed in configuration.
  248. *
  249. * @private
  250. * @param {Object} deviceSelectorProps - The props for the DeviceSelector.
  251. * @returns {ReactElement}
  252. */
  253. _renderSelector(deviceSelectorProps) {
  254. return (
  255. <div key = { deviceSelectorProps.label }>
  256. <div className = 'device-selector-label'>
  257. { this.props.t(deviceSelectorProps.label) }
  258. </div>
  259. <DeviceSelector { ...deviceSelectorProps } />
  260. </div>
  261. );
  262. }
  263. /**
  264. * Creates DeviceSelector instances for video output, audio input, and audio
  265. * output.
  266. *
  267. * @private
  268. * @returns {Array<ReactElement>} DeviceSelector instances.
  269. */
  270. _renderSelectors() {
  271. const { availableDevices } = this.props;
  272. const configurations = [
  273. {
  274. devices: availableDevices.videoInput,
  275. hasPermission: this.props.hasVideoPermission(),
  276. icon: 'icon-camera',
  277. isDisabled: this.props.disableDeviceChange,
  278. key: 'videoInput',
  279. label: 'settings.selectCamera',
  280. onSelect: selectedVideoInputId =>
  281. super._onChange({ selectedVideoInputId }),
  282. selectedDeviceId: this.props.selectedVideoInputId
  283. },
  284. {
  285. devices: availableDevices.audioInput,
  286. hasPermission: this.props.hasAudioPermission(),
  287. icon: 'icon-microphone',
  288. isDisabled: this.props.disableAudioInputChange
  289. || this.props.disableDeviceChange,
  290. key: 'audioInput',
  291. label: 'settings.selectMic',
  292. onSelect: selectedAudioInputId =>
  293. super._onChange({ selectedAudioInputId }),
  294. selectedDeviceId: this.props.selectedAudioInputId
  295. }
  296. ];
  297. if (!this.props.hideAudioOutputSelect) {
  298. configurations.push({
  299. devices: availableDevices.audioOutput,
  300. hasPermission: this.props.hasAudioPermission()
  301. || this.props.hasVideoPermission(),
  302. icon: 'icon-speaker',
  303. isDisabled: this.props.disableDeviceChange,
  304. key: 'audioOutput',
  305. label: 'settings.selectAudioOutput',
  306. onSelect: selectedAudioOutputId =>
  307. super._onChange({ selectedAudioOutputId }),
  308. selectedDeviceId: this.props.selectedAudioOutputId
  309. });
  310. }
  311. return configurations.map(config => this._renderSelector(config));
  312. }
  313. }
  314. export default translate(DeviceSelection);