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

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