您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DeviceSelection.js 12KB

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