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 7.7KB

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