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

VideoSettingsContent.tsx 10KB

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