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.

DeviceSelectionDialogBase.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import React, { Component } from 'react';
  2. import { StatelessDialog } from '../../base/dialog';
  3. import { translate } from '../../base/i18n';
  4. import { createLocalTrack } from '../../base/lib-jitsi-meet';
  5. import AudioInputPreview from './AudioInputPreview';
  6. import AudioOutputPreview from './AudioOutputPreview';
  7. import DeviceSelector from './DeviceSelector';
  8. import VideoInputPreview from './VideoInputPreview';
  9. /**
  10. * React component for previewing and selecting new audio and video sources.
  11. *
  12. * @extends Component
  13. */
  14. class DeviceSelectionDialogBase extends Component {
  15. /**
  16. * DeviceSelectionDialogBase component's property types.
  17. *
  18. * @static
  19. */
  20. static propTypes = {
  21. /**
  22. * All known audio and video devices split by type. This prop comes from
  23. * the app state.
  24. */
  25. availableDevices: React.PropTypes.object,
  26. /**
  27. * Closes the dialog.
  28. */
  29. closeModal: React.PropTypes.func,
  30. /**
  31. * Device id for the current audio input device. This device will be set
  32. * as the default audio input device to preview.
  33. */
  34. currentAudioInputId: React.PropTypes.string,
  35. /**
  36. * Device id for the current audio output device. This device will be
  37. * set as the default audio output device to preview.
  38. */
  39. currentAudioOutputId: React.PropTypes.string,
  40. /**
  41. * Device id for the current video input device. This device will be set
  42. * as the default video input device to preview.
  43. */
  44. currentVideoInputId: React.PropTypes.string,
  45. /**
  46. * Whether or not the audio selector can be interacted with. If true,
  47. * the audio input selector will be rendered as disabled. This is
  48. * specifically used to prevent audio device changing in Firefox, which
  49. * currently does not work due to a browser-side regression.
  50. */
  51. disableAudioInputChange: React.PropTypes.bool,
  52. /**
  53. * Disables dismissing the dialog when the blanket is clicked. Enabled
  54. * by default.
  55. */
  56. disableBlanketClickDismiss: React.PropTypes.bool,
  57. /**
  58. * True if device changing is configured to be disallowed. Selectors
  59. * will display as disabled.
  60. */
  61. disableDeviceChange: React.PropTypes.bool,
  62. /**
  63. * Whether or not a new audio input source can be selected.
  64. */
  65. hasAudioPermission: React.PropTypes.bool,
  66. /**
  67. * Whether or not a new video input sources can be selected.
  68. */
  69. hasVideoPermission: React.PropTypes.bool,
  70. /**
  71. * If true, the audio meter will not display. Necessary for browsers or
  72. * configurations that do not support local stats to prevent a
  73. * non-responsive mic preview from displaying.
  74. */
  75. hideAudioInputPreview: React.PropTypes.bool,
  76. /**
  77. * Whether or not the audio output source selector should display. If
  78. * true, the audio output selector and test audio link will not be
  79. * rendered. This is specifically used for hiding audio output on
  80. * temasys browsers which do not support such change.
  81. */
  82. hideAudioOutputSelect: React.PropTypes.bool,
  83. /**
  84. * Function that sets the audio input device.
  85. */
  86. setAudioInputDevice: React.PropTypes.func,
  87. /**
  88. * Function that sets the audio output device.
  89. */
  90. setAudioOutputDevice: React.PropTypes.func,
  91. /**
  92. * Function that sets the video input device.
  93. */
  94. setVideoInputDevice: React.PropTypes.func,
  95. /**
  96. * Invoked to obtain translated strings.
  97. */
  98. t: React.PropTypes.func
  99. };
  100. /**
  101. * Initializes a new DeviceSelectionDialogBase instance.
  102. *
  103. * @param {Object} props - The read-only React Component props with which
  104. * the new instance is to be initialized.
  105. */
  106. constructor(props) {
  107. super(props);
  108. const { availableDevices } = this.props;
  109. this.state = {
  110. // JitsiLocalTrack to use for live previewing of audio input.
  111. previewAudioTrack: null,
  112. // JitsiLocalTrack to use for live previewing of video input.
  113. previewVideoTrack: null,
  114. // An message describing a problem with obtaining a video preview.
  115. previewVideoTrackError: null,
  116. // The audio input device id to show as selected by default.
  117. selectedAudioInputId: this.props.currentAudioInputId || '',
  118. // The audio output device id to show as selected by default.
  119. selectedAudioOutputId: this.props.currentAudioOutputId || '',
  120. // The video input device id to show as selected by default.
  121. // FIXME: On temasys, without a device selected and put into local
  122. // storage as the default device to use, the current video device id
  123. // is a blank string. This is because the library gets a local video
  124. // track and then maps the track's device id by matching the track's
  125. // label to the MediaDeviceInfos returned from enumerateDevices. In
  126. // WebRTC, the track label is expected to return the camera device
  127. // label. However, temasys video track labels refer to track id, not
  128. // device label, so the library cannot match the track to a device.
  129. // The workaround of defaulting to the first videoInput available
  130. // is re-used from the previous device settings implementation.
  131. selectedVideoInputId: this.props.currentVideoInputId
  132. || (availableDevices.videoInput
  133. && availableDevices.videoInput[0]
  134. && availableDevices.videoInput[0].deviceId)
  135. || ''
  136. };
  137. // Preventing closing while cleaning up previews is important for
  138. // supporting temasys video cleanup. Temasys requires its video object
  139. // to be in the dom and visible for proper detaching of tracks. Delaying
  140. // closure until cleanup is complete ensures no errors in the process.
  141. this._isClosing = false;
  142. this._setDevicesAndClose = this._setDevicesAndClose.bind(this);
  143. this._onCancel = this._onCancel.bind(this);
  144. this._onSubmit = this._onSubmit.bind(this);
  145. this._updateAudioOutput = this._updateAudioOutput.bind(this);
  146. this._updateAudioInput = this._updateAudioInput.bind(this);
  147. this._updateVideoInput = this._updateVideoInput.bind(this);
  148. }
  149. /**
  150. * Sets default device choices so a choice is pre-selected in the dropdowns
  151. * and live previews are created.
  152. *
  153. * @inheritdoc
  154. */
  155. componentDidMount() {
  156. this._updateAudioOutput(this.state.selectedAudioOutputId);
  157. this._updateAudioInput(this.state.selectedAudioInputId);
  158. this._updateVideoInput(this.state.selectedVideoInputId);
  159. }
  160. /**
  161. * Disposes preview tracks that might not already be disposed.
  162. *
  163. * @inheritdoc
  164. */
  165. componentWillUnmount() {
  166. // This handles the case where neither submit nor cancel were triggered,
  167. // such as on modal switch. In that case, make a dying attempt to clean
  168. // up previews.
  169. if (!this._isClosing) {
  170. this._attemptPreviewTrackCleanup();
  171. }
  172. }
  173. /**
  174. * Implements React's {@link Component#render()}.
  175. *
  176. * @inheritdoc
  177. */
  178. render() {
  179. return (
  180. <StatelessDialog
  181. cancelTitleKey = { 'dialog.Cancel' }
  182. disableBlanketClickDismiss
  183. = { this.props.disableBlanketClickDismiss }
  184. okTitleKey = { 'dialog.Save' }
  185. onCancel = { this._onCancel }
  186. onSubmit = { this._onSubmit }
  187. titleKey = 'deviceSelection.deviceSettings'>
  188. <div className = 'device-selection'>
  189. <div className = 'device-selection-column column-video'>
  190. <div className = 'device-selection-video-container'>
  191. <VideoInputPreview
  192. error = { this.state.previewVideoTrackError }
  193. track = { this.state.previewVideoTrack } />
  194. </div>
  195. { this._renderAudioInputPreview() }
  196. </div>
  197. <div className = 'device-selection-column column-selectors'>
  198. <div className = 'device-selectors'>
  199. { this._renderSelectors() }
  200. </div>
  201. { this._renderAudioOutputPreview() }
  202. </div>
  203. </div>
  204. </StatelessDialog>
  205. );
  206. }
  207. /**
  208. * Cleans up preview tracks if they are not active tracks.
  209. *
  210. * @private
  211. * @returns {Array<Promise>} Zero to two promises will be returned. One
  212. * promise can be for video cleanup and another for audio cleanup.
  213. */
  214. _attemptPreviewTrackCleanup() {
  215. return Promise.all([
  216. this._disposeVideoPreview(),
  217. this._disposeAudioPreview()
  218. ]);
  219. }
  220. /**
  221. * Utility function for disposing the current audio preview.
  222. *
  223. * @private
  224. * @returns {Promise}
  225. */
  226. _disposeAudioPreview() {
  227. return this.state.previewAudioTrack
  228. ? this.state.previewAudioTrack.dispose() : Promise.resolve();
  229. }
  230. /**
  231. * Utility function for disposing the current video preview.
  232. *
  233. * @private
  234. * @returns {Promise}
  235. */
  236. _disposeVideoPreview() {
  237. return this.state.previewVideoTrack
  238. ? this.state.previewVideoTrack.dispose() : Promise.resolve();
  239. }
  240. /**
  241. * Disposes preview tracks and signals to
  242. * close DeviceSelectionDialogBase.
  243. *
  244. * @private
  245. * @returns {boolean} Returns false to prevent closure until cleanup is
  246. * complete.
  247. */
  248. _onCancel() {
  249. if (this._isClosing) {
  250. return false;
  251. }
  252. this._isClosing = true;
  253. const cleanupPromises = this._attemptPreviewTrackCleanup();
  254. Promise.all(cleanupPromises)
  255. .then(this.props.closeModal)
  256. .catch(this.props.closeModal);
  257. return false;
  258. }
  259. /**
  260. * Identifies changes to the preferred input/output devices and perform
  261. * necessary cleanup and requests to use those devices. Closes the modal
  262. * after cleanup and device change requests complete.
  263. *
  264. * @private
  265. * @returns {boolean} Returns false to prevent closure until cleanup is
  266. * complete.
  267. */
  268. _onSubmit() {
  269. if (this._isClosing) {
  270. return false;
  271. }
  272. this._isClosing = true;
  273. this._attemptPreviewTrackCleanup()
  274. .then(this._setDevicesAndClose, this._setDevicesAndClose);
  275. return false;
  276. }
  277. /**
  278. * Creates an AudioInputPreview for previewing if audio is being received.
  279. * Null will be returned if local stats for tracking audio input levels
  280. * cannot be obtained.
  281. *
  282. * @private
  283. * @returns {ReactComponent|null}
  284. */
  285. _renderAudioInputPreview() {
  286. if (this.props.hideAudioInputPreview) {
  287. return null;
  288. }
  289. return (
  290. <AudioInputPreview
  291. track = { this.state.previewAudioTrack } />
  292. );
  293. }
  294. /**
  295. * Creates an AudioOutputPreview instance for playing a test sound with the
  296. * passed in device id. Null will be returned if hideAudioOutput is truthy.
  297. *
  298. * @private
  299. * @returns {ReactComponent|null}
  300. */
  301. _renderAudioOutputPreview() {
  302. if (this.props.hideAudioOutputSelect) {
  303. return null;
  304. }
  305. return (
  306. <AudioOutputPreview
  307. deviceId = { this.state.selectedAudioOutputId } />
  308. );
  309. }
  310. /**
  311. * Creates a DeviceSelector instance based on the passed in configuration.
  312. *
  313. * @private
  314. * @param {Object} props - The props for the DeviceSelector.
  315. * @returns {ReactElement}
  316. */
  317. _renderSelector(props) {
  318. return (
  319. <DeviceSelector { ...props } />
  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 } = this.props;
  331. const configurations = [
  332. {
  333. devices: availableDevices.videoInput,
  334. hasPermission: this.props.hasVideoPermission,
  335. icon: 'icon-camera',
  336. isDisabled: this.props.disableDeviceChange,
  337. key: 'videoInput',
  338. label: 'settings.selectCamera',
  339. onSelect: this._updateVideoInput,
  340. selectedDeviceId: this.state.selectedVideoInputId
  341. },
  342. {
  343. devices: availableDevices.audioInput,
  344. hasPermission: this.props.hasAudioPermission,
  345. icon: 'icon-microphone',
  346. isDisabled: this.props.disableAudioInputChange
  347. || this.props.disableDeviceChange,
  348. key: 'audioInput',
  349. label: 'settings.selectMic',
  350. onSelect: this._updateAudioInput,
  351. selectedDeviceId: this.state.selectedAudioInputId
  352. }
  353. ];
  354. if (!this.props.hideAudioOutputSelect) {
  355. configurations.push({
  356. devices: availableDevices.audioOutput,
  357. hasPermission: this.props.hasAudioPermission
  358. || this.props.hasVideoPermission,
  359. icon: 'icon-volume',
  360. isDisabled: this.props.disableDeviceChange,
  361. key: 'audioOutput',
  362. label: 'settings.selectAudioOutput',
  363. onSelect: this._updateAudioOutput,
  364. selectedDeviceId: this.state.selectedAudioOutputId
  365. });
  366. }
  367. return configurations.map(this._renderSelector);
  368. }
  369. /**
  370. * Sets the selected devices and closes the dialog.
  371. *
  372. * @returns {void}
  373. */
  374. _setDevicesAndClose() {
  375. const {
  376. setVideoInputDevice,
  377. setAudioInputDevice,
  378. setAudioOutputDevice,
  379. closeModal
  380. } = this.props;
  381. const promises = [];
  382. if (this.state.selectedVideoInputId
  383. !== this.props.currentVideoInputId) {
  384. promises.push(setVideoInputDevice(this.state.selectedVideoInputId));
  385. }
  386. if (this.state.selectedAudioInputId
  387. !== this.props.currentAudioInputId) {
  388. promises.push(setAudioInputDevice(this.state.selectedAudioInputId));
  389. }
  390. if (this.state.selectedAudioOutputId
  391. !== this.props.currentAudioOutputId) {
  392. promises.push(
  393. setAudioOutputDevice(this.state.selectedAudioOutputId));
  394. }
  395. Promise.all(promises).then(closeModal, closeModal);
  396. }
  397. /**
  398. * Callback invoked when a new audio input device has been selected. Updates
  399. * the internal state of the user's selection as well as the audio track
  400. * that should display in the preview.
  401. *
  402. * @param {string} deviceId - The id of the chosen audio input device.
  403. * @private
  404. * @returns {void}
  405. */
  406. _updateAudioInput(deviceId) {
  407. this.setState({
  408. selectedAudioInputId: deviceId
  409. }, () => {
  410. this._disposeAudioPreview()
  411. .then(() => createLocalTrack('audio', deviceId))
  412. .then(jitsiLocalTrack => {
  413. this.setState({
  414. previewAudioTrack: jitsiLocalTrack
  415. });
  416. })
  417. .catch(() => {
  418. this.setState({
  419. previewAudioTrack: null
  420. });
  421. });
  422. });
  423. }
  424. /**
  425. * Callback invoked when a new audio output device has been selected.
  426. * Updates the internal state of the user's selection.
  427. *
  428. * @param {string} deviceId - The id of the chosen audio output device.
  429. * @private
  430. * @returns {void}
  431. */
  432. _updateAudioOutput(deviceId) {
  433. this.setState({
  434. selectedAudioOutputId: deviceId
  435. });
  436. }
  437. /**
  438. * Callback invoked when a new video input device has been selected. Updates
  439. * the internal state of the user's selection as well as the video track
  440. * that should display in the preview.
  441. *
  442. * @param {string} deviceId - The id of the chosen video input device.
  443. * @private
  444. * @returns {void}
  445. */
  446. _updateVideoInput(deviceId) {
  447. this.setState({
  448. selectedVideoInputId: deviceId
  449. }, () => {
  450. this._disposeVideoPreview()
  451. .then(() => createLocalTrack('video', deviceId))
  452. .then(jitsiLocalTrack => {
  453. this.setState({
  454. previewVideoTrack: jitsiLocalTrack,
  455. previewVideoTrackError: null
  456. });
  457. })
  458. .catch(() => {
  459. this.setState({
  460. previewVideoTrack: null,
  461. previewVideoTrackError:
  462. this.props.t('deviceSelection.previewUnavailable')
  463. });
  464. });
  465. });
  466. }
  467. }
  468. export default translate(DeviceSelectionDialogBase);