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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // @flow
  2. import Spinner from '@atlaskit/spinner';
  3. import React, { PureComponent } from 'react';
  4. import { hideDialog } from '../../base/dialog';
  5. import { translate } from '../../base/i18n';
  6. import { VIDEO_TYPE } from '../../base/media';
  7. import Video from '../../base/media/components/Video';
  8. import { connect, equals } from '../../base/redux';
  9. import { getCurrentCameraDeviceId } from '../../base/settings';
  10. import { createLocalTracksF } from '../../base/tracks/functions';
  11. import { showWarningNotification } from '../../notifications/actions';
  12. import { toggleBackgroundEffect } from '../actions';
  13. import { VIRTUAL_BACKGROUND_TYPE } from '../constants';
  14. import { localTrackStopped } from '../functions';
  15. import logger from '../logger';
  16. const videoClassName = 'video-preview-video';
  17. /**
  18. * The type of the React {@code PureComponent} props of {@link VirtualBackgroundPreview}.
  19. */
  20. export type Props = {
  21. /**
  22. * The deviceId of the camera device currently being used.
  23. */
  24. _currentCameraDeviceId: string,
  25. /**
  26. * The redux {@code dispatch} function.
  27. */
  28. dispatch: Function,
  29. /**
  30. * Dialog callback that indicates if the background preview was loaded.
  31. */
  32. loadedPreview: Function,
  33. /**
  34. * Represents the virtual background setted options.
  35. */
  36. options: Object,
  37. /**
  38. * Invoked to obtain translated strings.
  39. */
  40. t: Function
  41. };
  42. /**
  43. * The type of the React {@code Component} state of {@link VirtualBackgroundPreview}.
  44. */
  45. type State = {
  46. /**
  47. * Loader activated on setting virtual background.
  48. */
  49. loading: boolean,
  50. /**
  51. * Flag that indicates if the local track was loaded.
  52. */
  53. localTrackLoaded: boolean,
  54. /**
  55. * Activate the selected device camera only.
  56. */
  57. jitsiTrack: Object
  58. };
  59. /**
  60. * Implements a React {@link PureComponent} which displays the virtual
  61. * background preview.
  62. *
  63. * @extends PureComponent
  64. */
  65. class VirtualBackgroundPreview extends PureComponent<Props, State> {
  66. _componentWasUnmounted: boolean;
  67. /**
  68. * Initializes a new {@code VirtualBackgroundPreview} instance.
  69. *
  70. * @param {Object} props - The read-only properties with which the new
  71. * instance is to be initialized.
  72. */
  73. constructor(props) {
  74. super(props);
  75. this.state = {
  76. loading: false,
  77. localTrackLoaded: false,
  78. jitsiTrack: null
  79. };
  80. }
  81. /**
  82. * Destroys the jitsiTrack object.
  83. *
  84. * @param {Object} jitsiTrack - The track that needs to be disposed.
  85. * @returns {Promise<void>}
  86. */
  87. _stopStream(jitsiTrack) {
  88. if (jitsiTrack) {
  89. jitsiTrack.dispose();
  90. }
  91. }
  92. /**
  93. * Creates and updates the track data.
  94. *
  95. * @returns {void}
  96. */
  97. async _setTracks() {
  98. try {
  99. this.setState({ loading: true });
  100. const [ jitsiTrack ] = await createLocalTracksF({
  101. cameraDeviceId: this.props._currentCameraDeviceId,
  102. devices: [ 'video' ]
  103. });
  104. this.setState({ localTrackLoaded: true });
  105. // In case the component gets unmounted before the tracks are created
  106. // avoid a leak by not setting the state
  107. if (this._componentWasUnmounted) {
  108. this._stopStream(jitsiTrack);
  109. return;
  110. }
  111. this.setState({
  112. jitsiTrack,
  113. loading: false
  114. });
  115. this.props.loadedPreview(true);
  116. } catch (error) {
  117. this.props.dispatch(hideDialog());
  118. this.props.dispatch(
  119. showWarningNotification({
  120. titleKey: 'virtualBackground.backgroundEffectError',
  121. description: 'Failed to access camera device.'
  122. })
  123. );
  124. logger.error('Failed to access camera device. Error on apply background effect.');
  125. return;
  126. }
  127. if (this.props.options.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE
  128. && this.state.localTrackLoaded) {
  129. this._applyBackgroundEffect();
  130. }
  131. }
  132. /**
  133. * Apply background effect on video preview.
  134. *
  135. * @returns {Promise}
  136. */
  137. async _applyBackgroundEffect() {
  138. this.setState({ loading: true });
  139. this.props.loadedPreview(false);
  140. await this.props.dispatch(toggleBackgroundEffect(this.props.options, this.state.jitsiTrack));
  141. this.props.loadedPreview(true);
  142. this.setState({ loading: false });
  143. }
  144. /**
  145. * Apply video preview loader.
  146. *
  147. * @returns {Promise}
  148. */
  149. _loadVideoPreview() {
  150. return (
  151. <div className = 'video-preview-loader'>
  152. <Spinner
  153. invertColor = { true }
  154. isCompleting = { false }
  155. size = { 'large' } />
  156. </div>
  157. );
  158. }
  159. /**
  160. * Renders a preview entry.
  161. *
  162. * @param {Object} data - The track data.
  163. * @returns {React$Node}
  164. */
  165. _renderPreviewEntry(data) {
  166. const { t } = this.props;
  167. const className = 'video-background-preview-entry';
  168. if (this.state.loading) {
  169. return this._loadVideoPreview();
  170. }
  171. if (!data) {
  172. return (
  173. <div
  174. className = { className }
  175. video-preview-container = { true }>
  176. <div className = 'video-preview-error'>{t('deviceSelection.previewUnavailable')}</div>
  177. </div>
  178. );
  179. }
  180. const props: Object = {
  181. className
  182. };
  183. return (
  184. <div { ...props }>
  185. <Video
  186. className = { videoClassName }
  187. playsinline = { true }
  188. videoTrack = {{ jitsiTrack: data }} />
  189. </div>
  190. );
  191. }
  192. /**
  193. * Implements React's {@link Component#componentDidMount}.
  194. *
  195. * @inheritdoc
  196. */
  197. componentDidMount() {
  198. this._setTracks();
  199. }
  200. /**
  201. * Implements React's {@link Component#componentWillUnmount}.
  202. *
  203. * @inheritdoc
  204. */
  205. componentWillUnmount() {
  206. this._componentWasUnmounted = true;
  207. this._stopStream(this.state.jitsiTrack);
  208. }
  209. /**
  210. * Implements React's {@link Component#componentDidUpdate}.
  211. *
  212. * @inheritdoc
  213. */
  214. async componentDidUpdate(prevProps) {
  215. if (!equals(this.props._currentCameraDeviceId, prevProps._currentCameraDeviceId)) {
  216. this._setTracks();
  217. }
  218. if (!equals(this.props.options, prevProps.options) && this.state.localTrackLoaded) {
  219. if (prevProps.options.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
  220. prevProps.options.url.dispose();
  221. }
  222. this._applyBackgroundEffect();
  223. }
  224. if (this.props.options.url?.videoType === VIDEO_TYPE.DESKTOP) {
  225. localTrackStopped(this.props.dispatch, this.props.options.url, this.state.jitsiTrack);
  226. }
  227. }
  228. /**
  229. * Implements React's {@link Component#render}.
  230. *
  231. * @inheritdoc
  232. */
  233. render() {
  234. const { jitsiTrack } = this.state;
  235. return jitsiTrack
  236. ? <div className = 'video-preview'>{this._renderPreviewEntry(jitsiTrack)}</div>
  237. : <div className = 'video-preview-loader'>{this._loadVideoPreview()}</div>
  238. ;
  239. }
  240. }
  241. /**
  242. * Maps (parts of) the redux state to the associated props for the
  243. * {@code VirtualBackgroundPreview} component.
  244. *
  245. * @param {Object} state - The Redux state.
  246. * @private
  247. * @returns {{Props}}
  248. */
  249. function _mapStateToProps(state): Object {
  250. return {
  251. _currentCameraDeviceId: getCurrentCameraDeviceId(state)
  252. };
  253. }
  254. export default translate(connect(_mapStateToProps)(VirtualBackgroundPreview));