Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DeviceSelection.js 12KB

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