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

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