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

DeviceSelection.js 11KB

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