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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /**
  12. * The type of the React {@code Component} props of {@link DeviceSelection}.
  13. */
  14. export type Props = {
  15. ...$Exact<AbstractDialogTabProps>,
  16. /**
  17. * All known audio and video devices split by type. This prop comes from
  18. * the app state.
  19. */
  20. availableDevices: Object,
  21. /**
  22. * Whether or not the audio selector can be interacted with. If true,
  23. * the audio input selector will be rendered as disabled. This is
  24. * specifically used to prevent audio device changing in Firefox, which
  25. * currently does not work due to a browser-side regression.
  26. */
  27. disableAudioInputChange: boolean,
  28. /**
  29. * True if device changing is configured to be disallowed. Selectors
  30. * will display as disabled.
  31. */
  32. disableDeviceChange: boolean,
  33. /**
  34. * Function that checks whether or not a new audio input source can be
  35. * selected.
  36. */
  37. hasAudioPermission: Function,
  38. /**
  39. * Function that checks whether or not a new video input sources can be
  40. * selected.
  41. */
  42. hasVideoPermission: Function,
  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. This is specifically used for hiding audio output on
  53. * temasys browsers which do not support such change.
  54. */
  55. hideAudioOutputSelect: boolean,
  56. /**
  57. * The id of the audio input device to preview.
  58. */
  59. selectedAudioInputId: string,
  60. /**
  61. * The id of the audio output device to preview.
  62. */
  63. selectedAudioOutputId: string,
  64. /**
  65. * The id of the video input device to preview.
  66. */
  67. selectedVideoInputId: string,
  68. /**
  69. * Invoked to obtain translated strings.
  70. */
  71. t: Function
  72. };
  73. /**
  74. * The type of the React {@code Component} state of {@link DeviceSelection}.
  75. */
  76. type State = {
  77. /**
  78. * The JitsiTrack to use for previewing audio input.
  79. */
  80. previewAudioTrack: ?Object,
  81. /**
  82. * The JitsiTrack to use for previewing video input.
  83. */
  84. previewVideoTrack: ?Object,
  85. /**
  86. * The error message from trying to use a video input device.
  87. */
  88. previewVideoTrackError: ?string
  89. };
  90. /**
  91. * React {@code Component} for previewing audio and video input/output devices.
  92. *
  93. * @extends Component
  94. */
  95. class DeviceSelection extends AbstractDialogTab<Props, State> {
  96. /**
  97. * Initializes a new DeviceSelection instance.
  98. *
  99. * @param {Object} props - The read-only React Component props with which
  100. * the new instance is to be initialized.
  101. */
  102. constructor(props: Props) {
  103. super(props);
  104. this.state = {
  105. previewAudioTrack: null,
  106. previewVideoTrack: null,
  107. previewVideoTrackError: null
  108. };
  109. }
  110. /**
  111. * Generate the initial previews for audio input and video input.
  112. *
  113. * @inheritdoc
  114. */
  115. componentDidMount() {
  116. this._createAudioInputTrack(this.props.selectedAudioInputId);
  117. this._createVideoInputTrack(this.props.selectedVideoInputId);
  118. }
  119. /**
  120. * Updates audio input and video input previews.
  121. *
  122. * @inheritdoc
  123. * @param {Object} nextProps - The read-only props which this Component will
  124. * receive.
  125. * @returns {void}
  126. */
  127. componentWillReceiveProps(nextProps: Object) {
  128. const { selectedAudioInputId, selectedVideoInputId } = this.props;
  129. if (selectedAudioInputId !== nextProps.selectedAudioInputId) {
  130. this._createAudioInputTrack(nextProps.selectedAudioInputId);
  131. }
  132. if (selectedVideoInputId !== nextProps.selectedVideoInputId) {
  133. this._createVideoInputTrack(nextProps.selectedVideoInputId);
  134. }
  135. }
  136. /**
  137. * Ensure preview tracks are destroyed to prevent continued use.
  138. *
  139. * @inheritdoc
  140. */
  141. componentWillUnmount() {
  142. this._disposeAudioInputPreview();
  143. this._disposeVideoInputPreview();
  144. }
  145. /**
  146. * Implements React's {@link Component#render()}.
  147. *
  148. * @inheritdoc
  149. */
  150. render() {
  151. const {
  152. hideAudioInputPreview,
  153. hideAudioOutputSelect,
  154. selectedAudioOutputId
  155. } = this.props;
  156. return (
  157. <div className = 'device-selection'>
  158. <div className = 'device-selection-column column-video'>
  159. <div className = 'device-selection-video-container'>
  160. <VideoInputPreview
  161. error = { this.state.previewVideoTrackError }
  162. track = { this.state.previewVideoTrack } />
  163. </div>
  164. { !hideAudioInputPreview
  165. && <AudioInputPreview
  166. track = { this.state.previewAudioTrack } /> }
  167. </div>
  168. <div className = 'device-selection-column column-selectors'>
  169. <div className = 'device-selectors'>
  170. { this._renderSelectors() }
  171. </div>
  172. { !hideAudioOutputSelect
  173. && <AudioOutputPreview
  174. deviceId = { selectedAudioOutputId } /> }
  175. </div>
  176. </div>
  177. );
  178. }
  179. /**
  180. * Creates the JitiTrack for the audio input preview.
  181. *
  182. * @param {string} deviceId - The id of audio input device to preview.
  183. * @private
  184. * @returns {void}
  185. */
  186. _createAudioInputTrack(deviceId) {
  187. this._disposeAudioInputPreview()
  188. .then(() => createLocalTrack('audio', deviceId))
  189. .then(jitsiLocalTrack => {
  190. this.setState({
  191. previewAudioTrack: jitsiLocalTrack
  192. });
  193. })
  194. .catch(() => {
  195. this.setState({
  196. previewAudioTrack: null
  197. });
  198. });
  199. }
  200. /**
  201. * Creates the JitiTrack for the video input preview.
  202. *
  203. * @param {string} deviceId - The id of video device to preview.
  204. * @private
  205. * @returns {void}
  206. */
  207. _createVideoInputTrack(deviceId) {
  208. this._disposeVideoInputPreview()
  209. .then(() => createLocalTrack('video', deviceId))
  210. .then(jitsiLocalTrack => {
  211. if (!jitsiLocalTrack) {
  212. return Promise.reject();
  213. }
  214. this.setState({
  215. previewVideoTrack: jitsiLocalTrack,
  216. previewVideoTrackError: null
  217. });
  218. })
  219. .catch(() => {
  220. this.setState({
  221. previewVideoTrack: null,
  222. previewVideoTrackError:
  223. this.props.t('deviceSelection.previewUnavailable')
  224. });
  225. });
  226. }
  227. /**
  228. * Utility function for disposing the current audio input preview.
  229. *
  230. * @private
  231. * @returns {Promise}
  232. */
  233. _disposeAudioInputPreview(): Promise<*> {
  234. return this.state.previewAudioTrack
  235. ? this.state.previewAudioTrack.dispose() : Promise.resolve();
  236. }
  237. /**
  238. * Utility function for disposing the current video input preview.
  239. *
  240. * @private
  241. * @returns {Promise}
  242. */
  243. _disposeVideoInputPreview(): Promise<*> {
  244. return this.state.previewVideoTrack
  245. ? this.state.previewVideoTrack.dispose() : Promise.resolve();
  246. }
  247. /**
  248. * Creates a DeviceSelector instance based on the passed in configuration.
  249. *
  250. * @private
  251. * @param {Object} deviceSelectorProps - The props for the DeviceSelector.
  252. * @returns {ReactElement}
  253. */
  254. _renderSelector(deviceSelectorProps) {
  255. return (
  256. <div key = { deviceSelectorProps.label }>
  257. <div className = 'device-selector-label'>
  258. { this.props.t(deviceSelectorProps.label) }
  259. </div>
  260. <DeviceSelector { ...deviceSelectorProps } />
  261. </div>
  262. );
  263. }
  264. /**
  265. * Creates DeviceSelector instances for video output, audio input, and audio
  266. * output.
  267. *
  268. * @private
  269. * @returns {Array<ReactElement>} DeviceSelector instances.
  270. */
  271. _renderSelectors() {
  272. const { availableDevices } = this.props;
  273. const configurations = [
  274. {
  275. devices: availableDevices.videoInput,
  276. hasPermission: this.props.hasVideoPermission(),
  277. icon: 'icon-camera',
  278. isDisabled: this.props.disableDeviceChange,
  279. key: 'videoInput',
  280. label: 'settings.selectCamera',
  281. onSelect: selectedVideoInputId =>
  282. super._onChange({ selectedVideoInputId }),
  283. selectedDeviceId: this.props.selectedVideoInputId
  284. },
  285. {
  286. devices: availableDevices.audioInput,
  287. hasPermission: this.props.hasAudioPermission(),
  288. icon: 'icon-microphone',
  289. isDisabled: this.props.disableAudioInputChange
  290. || this.props.disableDeviceChange,
  291. key: 'audioInput',
  292. label: 'settings.selectMic',
  293. onSelect: selectedAudioInputId =>
  294. super._onChange({ selectedAudioInputId }),
  295. selectedDeviceId: this.props.selectedAudioInputId
  296. }
  297. ];
  298. if (!this.props.hideAudioOutputSelect) {
  299. configurations.push({
  300. devices: availableDevices.audioOutput,
  301. hasPermission: this.props.hasAudioPermission()
  302. || this.props.hasVideoPermission(),
  303. icon: 'icon-volume',
  304. isDisabled: this.props.disableDeviceChange,
  305. key: 'audioOutput',
  306. label: 'settings.selectAudioOutput',
  307. onSelect: selectedAudioOutputId =>
  308. super._onChange({ selectedAudioOutputId }),
  309. selectedDeviceId: this.props.selectedAudioOutputId
  310. });
  311. }
  312. return configurations.map(config => this._renderSelector(config));
  313. }
  314. }
  315. export default translate(DeviceSelection);