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

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