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.js 9.3KB

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