You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VirtualBackgroundPreview.tsx 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import Spinner from '@atlaskit/spinner';
  2. import { Theme } from '@mui/material';
  3. import { withStyles } from '@mui/styles';
  4. import React, { PureComponent } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { connect } from 'react-redux';
  7. import { IReduxState } from '../../app/types';
  8. import { hideDialog } from '../../base/dialog/actions';
  9. import { translate } from '../../base/i18n/functions';
  10. import Video from '../../base/media/components/Video';
  11. import { equals } from '../../base/redux/functions';
  12. import { getCurrentCameraDeviceId } from '../../base/settings/functions.web';
  13. import { createLocalTracksF } from '../../base/tracks/functions';
  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
  184. // @ts-ignore
  185. invertColor = { true }
  186. isCompleting = { false }
  187. size = { 'large' } />
  188. </div>
  189. );
  190. }
  191. /**
  192. * Renders a preview entry.
  193. *
  194. * @param {Object} data - The track data.
  195. * @returns {React$Node}
  196. */
  197. _renderPreviewEntry(data: Object) {
  198. const { t } = this.props;
  199. if (this.state.loading) {
  200. return this._loadVideoPreview();
  201. }
  202. if (!data) {
  203. return (
  204. <div className = 'video-preview-error'>{t('deviceSelection.previewUnavailable')}</div>
  205. );
  206. }
  207. return (
  208. <Video
  209. className = { videoClassName }
  210. playsinline = { true }
  211. videoTrack = {{ jitsiTrack: data }} />
  212. );
  213. }
  214. /**
  215. * Implements React's {@link Component#componentDidMount}.
  216. *
  217. * @inheritdoc
  218. */
  219. componentDidMount() {
  220. this._setTracks();
  221. }
  222. /**
  223. * Implements React's {@link Component#componentWillUnmount}.
  224. *
  225. * @inheritdoc
  226. */
  227. componentWillUnmount() {
  228. this._componentWasUnmounted = true;
  229. this._stopStream(this.state.jitsiTrack);
  230. }
  231. /**
  232. * Implements React's {@link Component#componentDidUpdate}.
  233. *
  234. * @inheritdoc
  235. */
  236. async componentDidUpdate(prevProps: IProps) {
  237. if (!equals(this.props._currentCameraDeviceId, prevProps._currentCameraDeviceId)) {
  238. this._setTracks();
  239. }
  240. if (!equals(this.props.options, prevProps.options) && this.state.localTrackLoaded) {
  241. this._applyBackgroundEffect();
  242. }
  243. }
  244. /**
  245. * Implements React's {@link Component#render}.
  246. *
  247. * @inheritdoc
  248. */
  249. render() {
  250. const { jitsiTrack } = this.state;
  251. const { classes } = this.props;
  252. return (<div className = { classes.virtualBackgroundPreview }>
  253. {jitsiTrack
  254. ? this._renderPreviewEntry(jitsiTrack)
  255. : this._loadVideoPreview()
  256. }</div>);
  257. }
  258. }
  259. /**
  260. * Maps (parts of) the redux state to the associated props for the
  261. * {@code VirtualBackgroundPreview} component.
  262. *
  263. * @param {Object} state - The Redux state.
  264. * @private
  265. * @returns {{Props}}
  266. */
  267. function _mapStateToProps(state: IReduxState) {
  268. return {
  269. _currentCameraDeviceId: getCurrentCameraDeviceId(state)
  270. };
  271. }
  272. export default translate(connect(_mapStateToProps)(withStyles(styles)(VirtualBackgroundPreview)));