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

DeviceSelection.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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
  204. aria-live = 'polite all'
  205. className = 'device-selectors'>
  206. { this._renderSelectors() }
  207. </div>
  208. { !hideAudioOutputSelect
  209. && <AudioOutputPreview
  210. deviceId = { selectedAudioOutputId } /> }
  211. </div>
  212. </div>
  213. );
  214. }
  215. /**
  216. * Creates the JitiTrack for the audio input preview.
  217. *
  218. * @param {string} deviceId - The id of audio input device to preview.
  219. * @private
  220. * @returns {void}
  221. */
  222. _createAudioInputTrack(deviceId) {
  223. return this._disposeAudioInputPreview()
  224. .then(() => createLocalTrack('audio', deviceId, 5000))
  225. .then(jitsiLocalTrack => {
  226. if (this._unMounted) {
  227. jitsiLocalTrack.dispose();
  228. return;
  229. }
  230. this.setState({
  231. previewAudioTrack: jitsiLocalTrack
  232. });
  233. })
  234. .catch(() => {
  235. this.setState({
  236. previewAudioTrack: null
  237. });
  238. });
  239. }
  240. /**
  241. * Creates the JitiTrack for the video input preview.
  242. *
  243. * @param {string} deviceId - The id of video device to preview.
  244. * @private
  245. * @returns {void}
  246. */
  247. _createVideoInputTrack(deviceId) {
  248. const { hideVideoInputPreview } = this.props;
  249. if (hideVideoInputPreview) {
  250. return;
  251. }
  252. return this._disposeVideoInputPreview()
  253. .then(() => createLocalTrack('video', deviceId, 5000))
  254. .then(jitsiLocalTrack => {
  255. if (!jitsiLocalTrack) {
  256. return Promise.reject();
  257. }
  258. if (this._unMounted) {
  259. jitsiLocalTrack.dispose();
  260. return;
  261. }
  262. this.setState({
  263. previewVideoTrack: jitsiLocalTrack,
  264. previewVideoTrackError: null
  265. });
  266. })
  267. .catch(() => {
  268. this.setState({
  269. previewVideoTrack: null,
  270. previewVideoTrackError:
  271. this.props.t('deviceSelection.previewUnavailable')
  272. });
  273. });
  274. }
  275. /**
  276. * Utility function for disposing the current audio input preview.
  277. *
  278. * @private
  279. * @returns {Promise}
  280. */
  281. _disposeAudioInputPreview(): Promise<*> {
  282. return this.state.previewAudioTrack
  283. ? this.state.previewAudioTrack.dispose() : Promise.resolve();
  284. }
  285. /**
  286. * Utility function for disposing the current video input preview.
  287. *
  288. * @private
  289. * @returns {Promise}
  290. */
  291. _disposeVideoInputPreview(): Promise<*> {
  292. return this.state.previewVideoTrack
  293. ? this.state.previewVideoTrack.dispose() : Promise.resolve();
  294. }
  295. /**
  296. * Creates a DeviceSelector instance based on the passed in configuration.
  297. *
  298. * @private
  299. * @param {Object} deviceSelectorProps - The props for the DeviceSelector.
  300. * @returns {ReactElement}
  301. */
  302. _renderSelector(deviceSelectorProps) {
  303. return (
  304. <div key = { deviceSelectorProps.label }>
  305. <label
  306. className = 'device-selector-label'
  307. htmlFor = { deviceSelectorProps.id }>
  308. { this.props.t(deviceSelectorProps.label) }
  309. </label>
  310. <DeviceSelector { ...deviceSelectorProps } />
  311. </div>
  312. );
  313. }
  314. /**
  315. * Creates DeviceSelector instances for video output, audio input, and audio
  316. * output.
  317. *
  318. * @private
  319. * @returns {Array<ReactElement>} DeviceSelector instances.
  320. */
  321. _renderSelectors() {
  322. const { availableDevices, hasAudioPermission, hasVideoPermission } = this.props;
  323. const configurations = [
  324. {
  325. devices: availableDevices.audioInput,
  326. hasPermission: hasAudioPermission,
  327. icon: 'icon-microphone',
  328. isDisabled: this.props.disableAudioInputChange
  329. || this.props.disableDeviceChange,
  330. key: 'audioInput',
  331. id: 'audioInput',
  332. label: 'settings.selectMic',
  333. onSelect: selectedAudioInputId =>
  334. super._onChange({ selectedAudioInputId }),
  335. selectedDeviceId: this.state.previewAudioTrack
  336. ? this.state.previewAudioTrack.getDeviceId() : null
  337. }
  338. ];
  339. if (!this.props.hideVideoOutputSelect) {
  340. configurations.unshift({
  341. devices: availableDevices.videoInput,
  342. hasPermission: hasVideoPermission,
  343. icon: 'icon-camera',
  344. isDisabled: this.props.disableDeviceChange,
  345. key: 'videoInput',
  346. id: 'videoInput',
  347. label: 'settings.selectCamera',
  348. onSelect: selectedVideoInputId =>
  349. super._onChange({ selectedVideoInputId }),
  350. selectedDeviceId: this.state.previewVideoTrack
  351. ? this.state.previewVideoTrack.getDeviceId() : null
  352. });
  353. }
  354. if (!this.props.hideAudioOutputSelect) {
  355. configurations.push({
  356. devices: availableDevices.audioOutput,
  357. hasPermission: hasAudioPermission || hasVideoPermission,
  358. icon: 'icon-speaker',
  359. isDisabled: this.props.disableDeviceChange,
  360. key: 'audioOutput',
  361. id: 'audioOutput',
  362. label: 'settings.selectAudioOutput',
  363. onSelect: selectedAudioOutputId =>
  364. super._onChange({ selectedAudioOutputId }),
  365. selectedDeviceId: this.props.selectedAudioOutputId
  366. });
  367. }
  368. return configurations.map(config => this._renderSelector(config));
  369. }
  370. }
  371. export default translate(DeviceSelection);