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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. * Initializes a new DeviceSelection instance.
  101. *
  102. * @param {Object} props - The read-only React Component props with which
  103. * the new instance is to be initialized.
  104. */
  105. constructor(props: Props) {
  106. super(props);
  107. this.state = {
  108. hasAudioPermission: false,
  109. hasVideoPermission: false,
  110. previewAudioTrack: null,
  111. previewVideoTrack: null,
  112. previewVideoTrackError: null
  113. };
  114. }
  115. /**
  116. * Generate the initial previews for audio input and video input.
  117. *
  118. * @inheritdoc
  119. */
  120. componentDidMount() {
  121. Promise.all([
  122. this._createAudioInputTrack(this.props.selectedAudioInputId),
  123. this._createVideoInputTrack(this.props.selectedVideoInputId)
  124. ])
  125. .catch(err => logger.warn('Failed to initialize preview tracks', err))
  126. .then(() => this.props.mountCallback && this.props.mountCallback());
  127. }
  128. /**
  129. * Checks if audio / video permissions were granted.
  130. *
  131. * @param {Object} prevProps - Previous props this component received.
  132. * @param {Object} prevState - Previous state this component had.
  133. * @returns {void}
  134. */
  135. componentDidUpdate(prevProps, prevState) {
  136. const { previewAudioTrack, previewVideoTrack } = prevState;
  137. if ((!previewAudioTrack && this.state.previewAudioTrack)
  138. || (!previewVideoTrack && this.state.previewVideoTrack)) {
  139. Promise.all([
  140. JitsiMeetJS.mediaDevices.isDevicePermissionGranted('audio'),
  141. JitsiMeetJS.mediaDevices.isDevicePermissionGranted('video')
  142. ]).then(r => {
  143. const [ hasAudioPermission, hasVideoPermission ] = r;
  144. this.setState({
  145. hasAudioPermission,
  146. hasVideoPermission
  147. });
  148. });
  149. }
  150. }
  151. /**
  152. * Updates audio input and video input previews.
  153. *
  154. * @inheritdoc
  155. * @param {Object} nextProps - The read-only props which this Component will
  156. * receive.
  157. * @returns {void}
  158. */
  159. componentWillReceiveProps(nextProps: Object) {
  160. const { selectedAudioInputId, selectedVideoInputId } = this.props;
  161. if (selectedAudioInputId !== nextProps.selectedAudioInputId) {
  162. this._createAudioInputTrack(nextProps.selectedAudioInputId);
  163. }
  164. if (selectedVideoInputId !== nextProps.selectedVideoInputId) {
  165. this._createVideoInputTrack(nextProps.selectedVideoInputId);
  166. }
  167. }
  168. /**
  169. * Ensure preview tracks are destroyed to prevent continued use.
  170. *
  171. * @inheritdoc
  172. */
  173. componentWillUnmount() {
  174. this._disposeAudioInputPreview();
  175. this._disposeVideoInputPreview();
  176. }
  177. /**
  178. * Implements React's {@link Component#render()}.
  179. *
  180. * @inheritdoc
  181. */
  182. render() {
  183. const {
  184. hideAudioInputPreview,
  185. hideAudioOutputSelect,
  186. selectedAudioOutputId
  187. } = this.props;
  188. return (
  189. <div className = 'device-selection'>
  190. <div className = 'device-selection-column column-video'>
  191. <div className = 'device-selection-video-container'>
  192. <VideoInputPreview
  193. error = { this.state.previewVideoTrackError }
  194. track = { this.state.previewVideoTrack } />
  195. </div>
  196. { !hideAudioInputPreview
  197. && <AudioInputPreview
  198. track = { this.state.previewAudioTrack } /> }
  199. </div>
  200. <div className = 'device-selection-column column-selectors'>
  201. <div className = 'device-selectors'>
  202. { this._renderSelectors() }
  203. </div>
  204. { !hideAudioOutputSelect
  205. && <AudioOutputPreview
  206. deviceId = { selectedAudioOutputId } /> }
  207. </div>
  208. </div>
  209. );
  210. }
  211. /**
  212. * Creates the JitiTrack for the audio input preview.
  213. *
  214. * @param {string} deviceId - The id of audio input device to preview.
  215. * @private
  216. * @returns {void}
  217. */
  218. _createAudioInputTrack(deviceId) {
  219. return this._disposeAudioInputPreview()
  220. .then(() => createLocalTrack('audio', deviceId))
  221. .then(jitsiLocalTrack => {
  222. this.setState({
  223. previewAudioTrack: jitsiLocalTrack
  224. });
  225. })
  226. .catch(() => {
  227. this.setState({
  228. previewAudioTrack: null
  229. });
  230. });
  231. }
  232. /**
  233. * Creates the JitiTrack for the video input preview.
  234. *
  235. * @param {string} deviceId - The id of video device to preview.
  236. * @private
  237. * @returns {void}
  238. */
  239. _createVideoInputTrack(deviceId) {
  240. return this._disposeVideoInputPreview()
  241. .then(() => createLocalTrack('video', deviceId))
  242. .then(jitsiLocalTrack => {
  243. if (!jitsiLocalTrack) {
  244. return Promise.reject();
  245. }
  246. this.setState({
  247. previewVideoTrack: jitsiLocalTrack,
  248. previewVideoTrackError: null
  249. });
  250. })
  251. .catch(() => {
  252. this.setState({
  253. previewVideoTrack: null,
  254. previewVideoTrackError:
  255. this.props.t('deviceSelection.previewUnavailable')
  256. });
  257. });
  258. }
  259. /**
  260. * Utility function for disposing the current audio input preview.
  261. *
  262. * @private
  263. * @returns {Promise}
  264. */
  265. _disposeAudioInputPreview(): Promise<*> {
  266. return this.state.previewAudioTrack
  267. ? this.state.previewAudioTrack.dispose() : Promise.resolve();
  268. }
  269. /**
  270. * Utility function for disposing the current video input preview.
  271. *
  272. * @private
  273. * @returns {Promise}
  274. */
  275. _disposeVideoInputPreview(): Promise<*> {
  276. return this.state.previewVideoTrack
  277. ? this.state.previewVideoTrack.dispose() : Promise.resolve();
  278. }
  279. /**
  280. * Creates a DeviceSelector instance based on the passed in configuration.
  281. *
  282. * @private
  283. * @param {Object} deviceSelectorProps - The props for the DeviceSelector.
  284. * @returns {ReactElement}
  285. */
  286. _renderSelector(deviceSelectorProps) {
  287. return (
  288. <div key = { deviceSelectorProps.label }>
  289. <div className = 'device-selector-label'>
  290. { this.props.t(deviceSelectorProps.label) }
  291. </div>
  292. <DeviceSelector { ...deviceSelectorProps } />
  293. </div>
  294. );
  295. }
  296. /**
  297. * Creates DeviceSelector instances for video output, audio input, and audio
  298. * output.
  299. *
  300. * @private
  301. * @returns {Array<ReactElement>} DeviceSelector instances.
  302. */
  303. _renderSelectors() {
  304. const { availableDevices } = this.props;
  305. const { hasAudioPermission, hasVideoPermission } = this.state;
  306. const configurations = [
  307. {
  308. devices: availableDevices.videoInput,
  309. hasPermission: hasVideoPermission,
  310. icon: 'icon-camera',
  311. isDisabled: this.props.disableDeviceChange,
  312. key: 'videoInput',
  313. label: 'settings.selectCamera',
  314. onSelect: selectedVideoInputId =>
  315. super._onChange({ selectedVideoInputId }),
  316. selectedDeviceId: this.props.selectedVideoInputId
  317. },
  318. {
  319. devices: availableDevices.audioInput,
  320. hasPermission: hasAudioPermission,
  321. icon: 'icon-microphone',
  322. isDisabled: this.props.disableAudioInputChange
  323. || this.props.disableDeviceChange,
  324. key: 'audioInput',
  325. label: 'settings.selectMic',
  326. onSelect: selectedAudioInputId =>
  327. super._onChange({ selectedAudioInputId }),
  328. selectedDeviceId: this.props.selectedAudioInputId
  329. }
  330. ];
  331. if (!this.props.hideAudioOutputSelect) {
  332. configurations.push({
  333. devices: availableDevices.audioOutput,
  334. hasPermission: hasAudioPermission || hasVideoPermission,
  335. icon: 'icon-speaker',
  336. isDisabled: this.props.disableDeviceChange,
  337. key: 'audioOutput',
  338. label: 'settings.selectAudioOutput',
  339. onSelect: selectedAudioOutputId =>
  340. super._onChange({ selectedAudioOutputId }),
  341. selectedDeviceId: this.props.selectedAudioOutputId
  342. });
  343. }
  344. return configurations.map(config => this._renderSelector(config));
  345. }
  346. }
  347. export default translate(DeviceSelection);