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.9KB

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