Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoDeviceSelection.web.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { Theme } from '@mui/material';
  2. import React from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { connect } from 'react-redux';
  5. import { withStyles } from 'tss-react/mui';
  6. import { IReduxState, IStore } from '../../app/types';
  7. import { getAvailableDevices } from '../../base/devices/actions.web';
  8. import AbstractDialogTab, {
  9. type IProps as AbstractDialogTabProps
  10. } from '../../base/dialog/components/web/AbstractDialogTab';
  11. import { translate } from '../../base/i18n/functions';
  12. import { createLocalTrack } from '../../base/lib-jitsi-meet/functions.web';
  13. import Checkbox from '../../base/ui/components/web/Checkbox';
  14. import Select from '../../base/ui/components/web/Select';
  15. import { SS_DEFAULT_FRAME_RATE } from '../../settings/constants';
  16. import logger from '../logger';
  17. import DeviceSelector from './DeviceSelector.web';
  18. import VideoInputPreview from './VideoInputPreview';
  19. /**
  20. * The type of the React {@code Component} props of {@link VideoDeviceSelection}.
  21. */
  22. export interface IProps extends AbstractDialogTabProps, WithTranslation {
  23. /**
  24. * All known audio and video devices split by type. This prop comes from
  25. * the app state.
  26. */
  27. availableDevices: { videoInput?: MediaDeviceInfo[]; };
  28. /**
  29. * CSS classes object.
  30. */
  31. classes?: Partial<Record<keyof ReturnType<typeof styles>, string>>;
  32. /**
  33. * The currently selected desktop share frame rate in the frame rate select dropdown.
  34. */
  35. currentFramerate: string;
  36. /**
  37. * All available desktop capture frame rates.
  38. */
  39. desktopShareFramerates: Array<number>;
  40. /**
  41. * True if device changing is configured to be disallowed. Selectors
  42. * will display as disabled.
  43. */
  44. disableDeviceChange: boolean;
  45. /**
  46. * Whether the local video can be flipped or not.
  47. */
  48. disableLocalVideoFlip: boolean | undefined;
  49. /**
  50. * Whether video input dropdown should be enabled or not.
  51. */
  52. disableVideoInputSelect: boolean;
  53. /**
  54. * Redux dispatch.
  55. */
  56. dispatch: IStore['dispatch'];
  57. /**
  58. * Whether or not the audio permission was granted.
  59. */
  60. hasVideoPermission: boolean;
  61. /**
  62. * Whether to hide the additional settings or not.
  63. */
  64. hideAdditionalSettings: boolean;
  65. /**
  66. * Whether video input preview should be displayed or not.
  67. * (In the case of iOS Safari).
  68. */
  69. hideVideoInputPreview: boolean;
  70. /**
  71. * Whether or not the local video is flipped.
  72. */
  73. localFlipX: boolean;
  74. /**
  75. * The id of the video input device to preview.
  76. */
  77. selectedVideoInputId: string;
  78. }
  79. /**
  80. * The type of the React {@code Component} state of {@link VideoDeviceSelection}.
  81. */
  82. interface IState {
  83. /**
  84. * The JitsiTrack to use for previewing video input.
  85. */
  86. previewVideoTrack: any | null;
  87. /**
  88. * The error message from trying to use a video input device.
  89. */
  90. previewVideoTrackError: string | null;
  91. }
  92. const styles = (theme: Theme) => {
  93. return {
  94. container: {
  95. display: 'flex',
  96. flexDirection: 'column' as const,
  97. padding: '0 2px',
  98. width: '100%'
  99. },
  100. checkboxContainer: {
  101. margin: `${theme.spacing(4)} 0`
  102. }
  103. };
  104. };
  105. /**
  106. * React {@code Component} for previewing audio and video input/output devices.
  107. *
  108. * @augments Component
  109. */
  110. class VideoDeviceSelection extends AbstractDialogTab<IProps, IState> {
  111. /**
  112. * Whether current component is mounted or not.
  113. *
  114. * In component did mount we start a Promise to create tracks and
  115. * set the tracks in the state, if we unmount the component in the meanwhile
  116. * tracks will be created and will never been disposed (dispose tracks is
  117. * in componentWillUnmount). When tracks are created and component is
  118. * unmounted we dispose the tracks.
  119. */
  120. _unMounted: boolean;
  121. /**
  122. * Initializes a new DeviceSelection instance.
  123. *
  124. * @param {Object} props - The read-only React Component props with which
  125. * the new instance is to be initialized.
  126. */
  127. constructor(props: IProps) {
  128. super(props);
  129. this.state = {
  130. previewVideoTrack: null,
  131. previewVideoTrackError: null
  132. };
  133. this._unMounted = true;
  134. this._onFramerateItemSelect = this._onFramerateItemSelect.bind(this);
  135. }
  136. /**
  137. * Generate the initial previews for audio input and video input.
  138. *
  139. * @inheritdoc
  140. */
  141. componentDidMount() {
  142. this._unMounted = false;
  143. Promise.all([
  144. this._createVideoInputTrack(this.props.selectedVideoInputId)
  145. ])
  146. .catch(err => logger.warn('Failed to initialize preview tracks', err))
  147. .then(() => {
  148. this.props.dispatch(getAvailableDevices());
  149. });
  150. }
  151. /**
  152. * Checks if audio / video permissions were granted. Updates audio input and
  153. * video input previews.
  154. *
  155. * @param {Object} prevProps - Previous props this component received.
  156. * @returns {void}
  157. */
  158. componentDidUpdate(prevProps: IProps) {
  159. if (prevProps.selectedVideoInputId
  160. !== this.props.selectedVideoInputId) {
  161. this._createVideoInputTrack(this.props.selectedVideoInputId);
  162. }
  163. }
  164. /**
  165. * Ensure preview tracks are destroyed to prevent continued use.
  166. *
  167. * @inheritdoc
  168. */
  169. componentWillUnmount() {
  170. this._unMounted = true;
  171. this._disposeVideoInputPreview();
  172. }
  173. /**
  174. * Implements React's {@link Component#render()}.
  175. *
  176. * @inheritdoc
  177. */
  178. render() {
  179. const {
  180. disableLocalVideoFlip,
  181. hideAdditionalSettings,
  182. hideVideoInputPreview,
  183. localFlipX,
  184. t
  185. } = this.props;
  186. const classes = withStyles.getClasses(this.props);
  187. return (
  188. <div className = { classes.container }>
  189. { !hideVideoInputPreview
  190. && <VideoInputPreview
  191. error = { this.state.previewVideoTrackError }
  192. localFlipX = { localFlipX }
  193. track = { this.state.previewVideoTrack } />
  194. }
  195. <div
  196. aria-live = 'polite'>
  197. {this._renderVideoSelector()}
  198. </div>
  199. {!hideAdditionalSettings && (
  200. <>
  201. {!disableLocalVideoFlip && (
  202. <div className = { classes.checkboxContainer }>
  203. <Checkbox
  204. checked = { localFlipX }
  205. label = { t('videothumbnail.mirrorVideo') }
  206. // eslint-disable-next-line react/jsx-no-bind
  207. onChange = { () => super._onChange({ localFlipX: !localFlipX }) } />
  208. </div>
  209. )}
  210. {this._renderFramerateSelect()}
  211. </>
  212. )}
  213. </div>
  214. );
  215. }
  216. /**
  217. * Creates the JitsiTrack for the video input preview.
  218. *
  219. * @param {string} deviceId - The id of video device to preview.
  220. * @private
  221. * @returns {void}
  222. */
  223. _createVideoInputTrack(deviceId: string) {
  224. const { hideVideoInputPreview } = this.props;
  225. if (hideVideoInputPreview) {
  226. return;
  227. }
  228. return this._disposeVideoInputPreview()
  229. .then(() => createLocalTrack('video', deviceId, 5000))
  230. .then(jitsiLocalTrack => {
  231. if (!jitsiLocalTrack) {
  232. return Promise.reject();
  233. }
  234. if (this._unMounted) {
  235. jitsiLocalTrack.dispose();
  236. return;
  237. }
  238. this.setState({
  239. previewVideoTrack: jitsiLocalTrack,
  240. previewVideoTrackError: null
  241. });
  242. })
  243. .catch(() => {
  244. this.setState({
  245. previewVideoTrack: null,
  246. previewVideoTrackError:
  247. this.props.t('deviceSelection.previewUnavailable')
  248. });
  249. });
  250. }
  251. /**
  252. * Utility function for disposing the current video input preview.
  253. *
  254. * @private
  255. * @returns {Promise}
  256. */
  257. _disposeVideoInputPreview(): Promise<any> {
  258. return this.state.previewVideoTrack
  259. ? this.state.previewVideoTrack.dispose() : Promise.resolve();
  260. }
  261. /**
  262. * Creates a DeviceSelector instance based on the passed in configuration.
  263. *
  264. * @private
  265. * @returns {ReactElement}
  266. */
  267. _renderVideoSelector() {
  268. const { availableDevices, hasVideoPermission } = this.props;
  269. const videoConfig = {
  270. devices: availableDevices.videoInput,
  271. hasPermission: hasVideoPermission,
  272. icon: 'icon-camera',
  273. isDisabled: this.props.disableVideoInputSelect || this.props.disableDeviceChange,
  274. key: 'videoInput',
  275. id: 'videoInput',
  276. label: 'settings.selectCamera',
  277. onSelect: (selectedVideoInputId: string) => super._onChange({ selectedVideoInputId }),
  278. selectedDeviceId: this.state.previewVideoTrack
  279. ? this.state.previewVideoTrack.getDeviceId() : this.props.selectedVideoInputId
  280. };
  281. return (
  282. <DeviceSelector
  283. { ...videoConfig }
  284. key = { videoConfig.id } />
  285. );
  286. }
  287. /**
  288. * Callback invoked to select a frame rate from the select dropdown.
  289. *
  290. * @param {Object} e - The key event to handle.
  291. * @private
  292. * @returns {void}
  293. */
  294. _onFramerateItemSelect(e: React.ChangeEvent<HTMLSelectElement>) {
  295. const frameRate = e.target.value;
  296. super._onChange({ currentFramerate: frameRate });
  297. }
  298. /**
  299. * Returns the React Element for the desktop share frame rate dropdown.
  300. *
  301. * @returns {JSX}
  302. */
  303. _renderFramerateSelect() {
  304. const { currentFramerate, desktopShareFramerates, t } = this.props;
  305. const frameRateItems = desktopShareFramerates.map((frameRate: number) => {
  306. return {
  307. value: frameRate,
  308. label: `${frameRate} ${t('settings.framesPerSecond')}`
  309. };
  310. });
  311. return (
  312. <Select
  313. bottomLabel = { parseInt(currentFramerate, 10) > SS_DEFAULT_FRAME_RATE
  314. ? t('settings.desktopShareHighFpsWarning')
  315. : t('settings.desktopShareWarning') }
  316. id = 'more-framerate-select'
  317. label = { t('settings.desktopShareFramerate') }
  318. onChange = { this._onFramerateItemSelect }
  319. options = { frameRateItems }
  320. value = { currentFramerate } />
  321. );
  322. }
  323. }
  324. const mapStateToProps = (state: IReduxState) => {
  325. return {
  326. availableDevices: state['features/base/devices'].availableDevices ?? {}
  327. };
  328. };
  329. export default connect(mapStateToProps)(withStyles(translate(VideoDeviceSelection), styles));