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

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