Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoSettingsContent.tsx 10KB

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