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

VideoSettingsContent.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import React, { useCallback, useEffect, useRef, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { IReduxState, IStore } from '../../../../app/types';
  6. import { IconImage } from '../../../../base/icons/svg';
  7. import { Video } from '../../../../base/media/components/index';
  8. import { equals } from '../../../../base/redux/functions';
  9. import { updateSettings } from '../../../../base/settings/actions';
  10. import { withPixelLineHeight } from '../../../../base/styles/functions.web';
  11. import Checkbox from '../../../../base/ui/components/web/Checkbox';
  12. import ContextMenu from '../../../../base/ui/components/web/ContextMenu';
  13. import ContextMenuItem from '../../../../base/ui/components/web/ContextMenuItem';
  14. import ContextMenuItemGroup from '../../../../base/ui/components/web/ContextMenuItemGroup';
  15. import { checkBlurSupport, checkVirtualBackgroundEnabled } from '../../../../virtual-background/functions';
  16. import { openSettingsDialog } from '../../../actions';
  17. import { SETTINGS_TABS } from '../../../constants';
  18. import { createLocalVideoTracks } from '../../../functions.web';
  19. /**
  20. * The type of the React {@code Component} props of {@link VideoSettingsContent}.
  21. */
  22. export interface IProps {
  23. /**
  24. * Callback to change the flip state.
  25. */
  26. changeFlip: (flip: boolean) => void;
  27. /**
  28. * The deviceId of the camera device currently being used.
  29. */
  30. currentCameraDeviceId: string;
  31. /**
  32. * Whether the local video flip is disabled.
  33. */
  34. disableLocalVideoFlip: boolean | undefined;
  35. /**
  36. * Whether or not the local video is flipped.
  37. */
  38. localFlipX: boolean;
  39. /**
  40. * Open virtual background dialog.
  41. */
  42. selectBackground: () => void;
  43. /**
  44. * Callback invoked to change current camera.
  45. */
  46. setVideoInputDevice: Function;
  47. /**
  48. * Callback invoked to toggle the settings popup visibility.
  49. */
  50. toggleVideoSettings: Function;
  51. /**
  52. * All the camera device ids currently connected.
  53. */
  54. videoDeviceIds: string[];
  55. /**
  56. * Whether or not the virtual background is visible.
  57. */
  58. visibleVirtualBackground: boolean;
  59. }
  60. const useStyles = makeStyles()(theme => {
  61. return {
  62. container: {
  63. maxHeight: 'calc(100dvh - 100px)',
  64. overflow: 'auto',
  65. margin: 0,
  66. marginBottom: theme.spacing(1),
  67. position: 'relative',
  68. right: 'auto'
  69. },
  70. previewEntry: {
  71. cursor: 'pointer',
  72. height: '138px',
  73. width: '244px',
  74. position: 'relative',
  75. margin: '0 7px',
  76. marginBottom: theme.spacing(1),
  77. borderRadius: theme.shape.borderRadius,
  78. boxSizing: 'border-box',
  79. overflow: 'hidden',
  80. '&:last-child': {
  81. marginBottom: 0
  82. }
  83. },
  84. selectedEntry: {
  85. border: `2px solid ${theme.palette.action01Hover}`
  86. },
  87. previewVideo: {
  88. height: '100%',
  89. width: '100%',
  90. objectFit: 'cover'
  91. },
  92. error: {
  93. display: 'flex',
  94. alignItems: 'center',
  95. justifyContent: 'center',
  96. height: '100%',
  97. width: '100%',
  98. position: 'absolute'
  99. },
  100. labelContainer: {
  101. position: 'absolute',
  102. bottom: 0,
  103. left: 0,
  104. right: 0,
  105. maxWidth: '100%',
  106. zIndex: 2,
  107. padding: theme.spacing(2)
  108. },
  109. label: {
  110. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  111. borderRadius: '4px',
  112. padding: `${theme.spacing(1)} ${theme.spacing(2)}`,
  113. color: theme.palette.text01,
  114. ...withPixelLineHeight(theme.typography.labelBold),
  115. width: 'fit-content',
  116. maxwidth: `calc(100% - ${theme.spacing(2)} - ${theme.spacing(2)})`,
  117. overflow: 'hidden',
  118. textOverflow: 'ellipsis',
  119. whiteSpace: 'nowrap'
  120. },
  121. checkboxContainer: {
  122. padding: '10px 14px'
  123. }
  124. };
  125. });
  126. const stopPropagation = (e: React.MouseEvent) => {
  127. e.stopPropagation();
  128. };
  129. const VideoSettingsContent = ({
  130. changeFlip,
  131. currentCameraDeviceId,
  132. disableLocalVideoFlip,
  133. localFlipX,
  134. selectBackground,
  135. setVideoInputDevice,
  136. toggleVideoSettings,
  137. videoDeviceIds,
  138. visibleVirtualBackground
  139. }: IProps) => {
  140. const _componentWasUnmounted = useRef(false);
  141. const [ trackData, setTrackData ] = useState(new Array(videoDeviceIds.length).fill({
  142. jitsiTrack: null
  143. }));
  144. const { t } = useTranslation();
  145. const videoDevicesRef = useRef(videoDeviceIds);
  146. const trackDataRef = useRef(trackData);
  147. const { classes, cx } = useStyles();
  148. /**
  149. * Toggles local video flip state.
  150. *
  151. * @returns {void}
  152. */
  153. const _onToggleFlip = useCallback(() => {
  154. changeFlip(!localFlipX);
  155. }, [ localFlipX, changeFlip ]);
  156. /**
  157. * Destroys all the tracks from trackData object.
  158. *
  159. * @param {Object[]} tracks - An array of tracks that are to be disposed.
  160. * @returns {Promise<void>}
  161. */
  162. const _disposeTracks = (tracks: { jitsiTrack: any; }[]) => {
  163. tracks.forEach(({ jitsiTrack }) => {
  164. jitsiTrack?.dispose();
  165. });
  166. };
  167. /**
  168. * Creates and updates the track data.
  169. *
  170. * @returns {void}
  171. */
  172. const _setTracks = async () => {
  173. _disposeTracks(trackData);
  174. const newTrackData = await createLocalVideoTracks(videoDeviceIds, 5000);
  175. // In case the component gets unmounted before the tracks are created
  176. // avoid a leak by not setting the state
  177. if (_componentWasUnmounted.current) {
  178. _disposeTracks(newTrackData);
  179. } else {
  180. setTrackData(newTrackData);
  181. trackDataRef.current = newTrackData;
  182. }
  183. };
  184. /**
  185. * Returns the click handler used when selecting the video preview.
  186. *
  187. * @param {string} deviceId - The id of the camera device.
  188. * @returns {Function}
  189. */
  190. const _onEntryClick = (deviceId: string) => () => {
  191. setVideoInputDevice(deviceId);
  192. toggleVideoSettings();
  193. };
  194. /**
  195. * Renders a preview entry.
  196. *
  197. * @param {Object} data - The track data.
  198. * @param {number} index - The index of the entry.
  199. * @returns {React$Node}
  200. */
  201. // eslint-disable-next-line react/no-multi-comp
  202. const _renderPreviewEntry = (data: { deviceId: string; error?: string; jitsiTrack: any | null; },
  203. index: number) => {
  204. const { error, jitsiTrack, deviceId } = data;
  205. const isSelected = deviceId === currentCameraDeviceId;
  206. const key = `vp-${index}`;
  207. const tabIndex = '0';
  208. if (error) {
  209. return (
  210. <div
  211. className = { classes.previewEntry }
  212. key = { key }
  213. tabIndex = { -1 } >
  214. <div className = { classes.error }>{t(error)}</div>
  215. </div>
  216. );
  217. }
  218. const previewProps: any = {
  219. className: classes.previewEntry,
  220. key,
  221. tabIndex
  222. };
  223. const label = jitsiTrack?.getTrackLabel();
  224. if (isSelected) {
  225. previewProps['aria-checked'] = true;
  226. previewProps.className = cx(classes.previewEntry, classes.selectedEntry);
  227. } else {
  228. previewProps.onClick = _onEntryClick(deviceId);
  229. previewProps.onKeyPress = (e: React.KeyboardEvent) => {
  230. if (e.key === ' ' || e.key === 'Enter') {
  231. e.preventDefault();
  232. previewProps.onClick();
  233. }
  234. };
  235. }
  236. return (
  237. <div
  238. { ...previewProps }
  239. role = 'radio'>
  240. <div className = { classes.labelContainer }>
  241. {label && <div className = { classes.label }>
  242. <span>{label}</span>
  243. </div>}
  244. </div>
  245. <Video
  246. className = { cx(classes.previewVideo, 'flipVideoX') }
  247. playsinline = { true }
  248. videoTrack = {{ jitsiTrack }} />
  249. </div>
  250. );
  251. };
  252. useEffect(() => {
  253. _setTracks();
  254. return () => {
  255. _componentWasUnmounted.current = true;
  256. _disposeTracks(trackDataRef.current);
  257. };
  258. }, []);
  259. useEffect(() => {
  260. if (!equals(videoDeviceIds, videoDevicesRef.current)) {
  261. _setTracks();
  262. videoDevicesRef.current = videoDeviceIds;
  263. }
  264. }, [ videoDeviceIds ]);
  265. return (
  266. <ContextMenu
  267. activateFocusTrap = { true }
  268. aria-labelledby = 'video-settings-button'
  269. className = { classes.container }
  270. hidden = { false }
  271. id = 'video-settings-dialog'
  272. role = 'radiogroup'
  273. tabIndex = { -1 }>
  274. <ContextMenuItemGroup>
  275. {trackData.map((data, i) => _renderPreviewEntry(data, i))}
  276. </ContextMenuItemGroup>
  277. <ContextMenuItemGroup>
  278. { visibleVirtualBackground && <ContextMenuItem
  279. accessibilityLabel = { t('virtualBackground.title') }
  280. icon = { IconImage }
  281. onClick = { selectBackground }
  282. text = { t('virtualBackground.title') } /> }
  283. {!disableLocalVideoFlip && (
  284. <div
  285. className = { classes.checkboxContainer }
  286. onClick = { stopPropagation }>
  287. <Checkbox
  288. checked = { localFlipX }
  289. label = { t('videothumbnail.mirrorVideo') }
  290. onChange = { _onToggleFlip } />
  291. </div>
  292. )}
  293. </ContextMenuItemGroup>
  294. </ContextMenu>
  295. );
  296. };
  297. const mapStateToProps = (state: IReduxState) => {
  298. const { disableLocalVideoFlip } = state['features/base/config'];
  299. const { localFlipX } = state['features/base/settings'];
  300. return {
  301. disableLocalVideoFlip,
  302. localFlipX: Boolean(localFlipX),
  303. visibleVirtualBackground: checkBlurSupport()
  304. && checkVirtualBackgroundEnabled(state)
  305. };
  306. };
  307. const mapDispatchToProps = (dispatch: IStore['dispatch']) => {
  308. return {
  309. selectBackground: () => dispatch(openSettingsDialog(SETTINGS_TABS.VIRTUAL_BACKGROUND)),
  310. changeFlip: (flip: boolean) => {
  311. dispatch(updateSettings({
  312. localFlipX: flip
  313. }));
  314. }
  315. };
  316. };
  317. export default connect(mapStateToProps, mapDispatchToProps)(VideoSettingsContent);