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

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