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

VirtualBackgroundPreview.tsx 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import React, { PureComponent } from 'react';
  4. import { WithTranslation } from 'react-i18next';
  5. import { connect } from 'react-redux';
  6. import { IStore } from '../../app/types';
  7. import { hideDialog } from '../../base/dialog/actions';
  8. import { translate } from '../../base/i18n/functions';
  9. import { Video } from '../../base/media/components/index';
  10. import { equals } from '../../base/redux/functions';
  11. import { createLocalTracksF } from '../../base/tracks/functions';
  12. import Spinner from '../../base/ui/components/web/Spinner';
  13. import { showWarningNotification } from '../../notifications/actions';
  14. import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
  15. import { toggleBackgroundEffect } from '../actions';
  16. import logger from '../logger';
  17. import { IVirtualBackground } from '../reducer';
  18. /**
  19. * The type of the React {@code PureComponent} props of {@link VirtualBackgroundPreview}.
  20. */
  21. export interface IProps extends WithTranslation {
  22. /**
  23. * An object containing the CSS classes.
  24. */
  25. classes: any;
  26. /**
  27. * The redux {@code dispatch} function.
  28. */
  29. dispatch: IStore['dispatch'];
  30. /**
  31. * Dialog callback that indicates if the background preview was loaded.
  32. */
  33. loadedPreview: Function;
  34. /**
  35. * Represents the virtual background set options.
  36. */
  37. options: IVirtualBackground;
  38. /**
  39. * The id of the selected video device.
  40. */
  41. selectedVideoInputId: string;
  42. }
  43. /**
  44. * The type of the React {@code Component} state of {@link VirtualBackgroundPreview}.
  45. */
  46. interface IState {
  47. /**
  48. * Activate the selected device camera only.
  49. */
  50. jitsiTrack: Object | null;
  51. /**
  52. * Loader activated on setting virtual background.
  53. */
  54. loading: boolean;
  55. /**
  56. * Flag that indicates if the local track was loaded.
  57. */
  58. localTrackLoaded: boolean;
  59. }
  60. /**
  61. * Creates the styles for the component.
  62. *
  63. * @param {Object} theme - The current UI theme.
  64. *
  65. * @returns {Object}
  66. */
  67. const styles = (theme: Theme) => {
  68. return {
  69. virtualBackgroundPreview: {
  70. height: 'auto',
  71. width: '100%',
  72. overflow: 'hidden',
  73. marginBottom: theme.spacing(3),
  74. zIndex: 2,
  75. borderRadius: '3px',
  76. backgroundColor: theme.palette.uiBackground,
  77. position: 'relative' as const
  78. },
  79. previewLoader: {
  80. height: '220px',
  81. '& svg': {
  82. position: 'absolute' as const,
  83. top: '40%',
  84. left: '45%'
  85. }
  86. },
  87. previewVideo: {
  88. height: '100%',
  89. width: '100%',
  90. objectFit: 'cover' as const
  91. },
  92. error: {
  93. display: 'flex',
  94. alignItems: 'center',
  95. justifyContent: 'center',
  96. width: '100%',
  97. height: '220px',
  98. position: 'relative' as const
  99. }
  100. };
  101. };
  102. /**
  103. * Implements a React {@link PureComponent} which displays the virtual
  104. * background preview.
  105. *
  106. * @augments PureComponent
  107. */
  108. class VirtualBackgroundPreview extends PureComponent<IProps, IState> {
  109. _componentWasUnmounted: boolean;
  110. /**
  111. * Initializes a new {@code VirtualBackgroundPreview} instance.
  112. *
  113. * @param {Object} props - The read-only properties with which the new
  114. * instance is to be initialized.
  115. */
  116. constructor(props: IProps) {
  117. super(props);
  118. this.state = {
  119. loading: false,
  120. localTrackLoaded: false,
  121. jitsiTrack: null
  122. };
  123. }
  124. /**
  125. * Destroys the jitsiTrack object.
  126. *
  127. * @param {Object} jitsiTrack - The track that needs to be disposed.
  128. * @returns {Promise<void>}
  129. */
  130. _stopStream(jitsiTrack: any) {
  131. if (jitsiTrack) {
  132. jitsiTrack.dispose();
  133. }
  134. }
  135. /**
  136. * Creates and updates the track data.
  137. *
  138. * @returns {void}
  139. */
  140. async _setTracks() {
  141. try {
  142. this.setState({ loading: true });
  143. const [ jitsiTrack ] = await createLocalTracksF({
  144. cameraDeviceId: this.props.selectedVideoInputId,
  145. devices: [ 'video' ]
  146. });
  147. this.setState({ localTrackLoaded: true });
  148. // In case the component gets unmounted before the tracks are created
  149. // avoid a leak by not setting the state
  150. if (this._componentWasUnmounted) {
  151. this._stopStream(jitsiTrack);
  152. return;
  153. }
  154. this.setState({
  155. jitsiTrack,
  156. loading: false
  157. });
  158. this.props.loadedPreview(true);
  159. } catch (error) {
  160. this.props.dispatch(hideDialog());
  161. this.props.dispatch(
  162. showWarningNotification({
  163. titleKey: 'virtualBackground.backgroundEffectError',
  164. description: 'Failed to access camera device.'
  165. }, NOTIFICATION_TIMEOUT_TYPE.LONG)
  166. );
  167. logger.error('Failed to access camera device. Error on apply background effect.');
  168. return;
  169. }
  170. }
  171. /**
  172. * Apply background effect on video preview.
  173. *
  174. * @returns {Promise}
  175. */
  176. async _applyBackgroundEffect() {
  177. this.setState({ loading: true });
  178. this.props.loadedPreview(false);
  179. await this.props.dispatch(toggleBackgroundEffect(this.props.options, this.state.jitsiTrack));
  180. this.props.loadedPreview(true);
  181. this.setState({ loading: false });
  182. }
  183. /**
  184. * Apply video preview loader.
  185. *
  186. * @returns {Promise}
  187. */
  188. _loadVideoPreview() {
  189. return (
  190. <div className = { this.props.classes.previewLoader }>
  191. <Spinner size = 'large' />
  192. </div>
  193. );
  194. }
  195. /**
  196. * Renders a preview entry.
  197. *
  198. * @param {Object} data - The track data.
  199. * @returns {React$Node}
  200. */
  201. _renderPreviewEntry(data: Object) {
  202. const { classes, t } = this.props;
  203. if (this.state.loading) {
  204. return this._loadVideoPreview();
  205. }
  206. if (!data) {
  207. return (
  208. <div className = { classes.error }>{t('deviceSelection.previewUnavailable')}</div>
  209. );
  210. }
  211. return (
  212. <Video
  213. className = { classes.previewVideo }
  214. playsinline = { true }
  215. videoTrack = {{ jitsiTrack: data }} />
  216. );
  217. }
  218. /**
  219. * Implements React's {@link Component#componentDidMount}.
  220. *
  221. * @inheritdoc
  222. */
  223. componentDidMount() {
  224. this._setTracks();
  225. }
  226. /**
  227. * Implements React's {@link Component#componentWillUnmount}.
  228. *
  229. * @inheritdoc
  230. */
  231. componentWillUnmount() {
  232. this._componentWasUnmounted = true;
  233. this._stopStream(this.state.jitsiTrack);
  234. }
  235. /**
  236. * Implements React's {@link Component#componentDidUpdate}.
  237. *
  238. * @inheritdoc
  239. */
  240. async componentDidUpdate(prevProps: IProps) {
  241. if (!equals(this.props.selectedVideoInputId, prevProps.selectedVideoInputId)) {
  242. this._setTracks();
  243. }
  244. if (!equals(this.props.options, prevProps.options) && this.state.localTrackLoaded) {
  245. this._applyBackgroundEffect();
  246. }
  247. }
  248. /**
  249. * Implements React's {@link Component#render}.
  250. *
  251. * @inheritdoc
  252. */
  253. render() {
  254. const { jitsiTrack } = this.state;
  255. const { classes } = this.props;
  256. return (
  257. <div className = { classes.virtualBackgroundPreview }>
  258. {jitsiTrack
  259. ? this._renderPreviewEntry(jitsiTrack)
  260. : this._loadVideoPreview()
  261. }</div>
  262. );
  263. }
  264. }
  265. export default translate(connect()(withStyles(styles)(VirtualBackgroundPreview)));