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

VirtualBackgroundPreview.tsx 8.0KB

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