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

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