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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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';
  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. }
  100. /**
  101. * Apply background effect on video preview.
  102. *
  103. * @returns {Promise}
  104. */
  105. async _applyBackgroundEffect() {
  106. this.setState({ loading: true });
  107. await this.props.dispatch(toggleBackgroundEffect(this.props.options, this.state.jitsiTrack));
  108. this.setState({ loading: false });
  109. }
  110. /**
  111. * Apply video preview loader.
  112. *
  113. * @returns {Promise}
  114. */
  115. _loadVideoPreview() {
  116. return (
  117. <div className = 'video-preview-loader'>
  118. <Spinner
  119. invertColor = { true }
  120. isCompleting = { false }
  121. size = { 'large' } />
  122. </div>
  123. );
  124. }
  125. /**
  126. * Renders a preview entry.
  127. *
  128. * @param {Object} data - The track data.
  129. * @returns {React$Node}
  130. */
  131. _renderPreviewEntry(data) {
  132. const { t } = this.props;
  133. const className = 'video-background-preview-entry';
  134. if (this.state.loading) {
  135. return this._loadVideoPreview();
  136. }
  137. if (!data) {
  138. return (
  139. <div
  140. className = { className }
  141. video-preview-container = { true }>
  142. <div className = 'video-preview-error'>{t('deviceSelection.previewUnavailable')}</div>
  143. </div>
  144. );
  145. }
  146. const props: Object = {
  147. className
  148. };
  149. return (
  150. <div { ...props }>
  151. <Video
  152. className = { videoClassName }
  153. playsinline = { true }
  154. videoTrack = {{ jitsiTrack: data }} />
  155. </div>
  156. );
  157. }
  158. /**
  159. * Implements React's {@link Component#componentDidMount}.
  160. *
  161. * @inheritdoc
  162. */
  163. componentDidMount() {
  164. this._setTracks();
  165. }
  166. /**
  167. * Implements React's {@link Component#componentWillUnmount}.
  168. *
  169. * @inheritdoc
  170. */
  171. componentWillUnmount() {
  172. this._componentWasUnmounted = true;
  173. this._stopStream(this.state.jitsiTrack);
  174. }
  175. /**
  176. * Implements React's {@link Component#componentDidUpdate}.
  177. *
  178. * @inheritdoc
  179. */
  180. async componentDidUpdate(prevProps) {
  181. if (!equals(this.props._currentCameraDeviceId, prevProps._currentCameraDeviceId)) {
  182. this._setTracks();
  183. }
  184. if (!equals(this.props.options, prevProps.options)) {
  185. if (prevProps.options.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
  186. prevProps.options.url.dispose();
  187. }
  188. this._applyBackgroundEffect();
  189. }
  190. if (this.props.options.url?.videoType === VIDEO_TYPE.DESKTOP) {
  191. localTrackStopped(this.props.dispatch, this.props.options.url, this.state.jitsiTrack);
  192. }
  193. }
  194. /**
  195. * Implements React's {@link Component#render}.
  196. *
  197. * @inheritdoc
  198. */
  199. render() {
  200. const { jitsiTrack } = this.state;
  201. return jitsiTrack
  202. ? <div className = 'video-preview'>{this._renderPreviewEntry(jitsiTrack)}</div>
  203. : <div className = 'video-preview-loader'>{this._loadVideoPreview()}</div>
  204. ;
  205. }
  206. }
  207. /**
  208. * Maps (parts of) the redux state to the associated props for the
  209. * {@code VirtualBackgroundPreview} component.
  210. *
  211. * @param {Object} state - The Redux state.
  212. * @private
  213. * @returns {{Props}}
  214. */
  215. function _mapStateToProps(state): Object {
  216. return {
  217. _currentCameraDeviceId: getCurrentCameraDeviceId(state)
  218. };
  219. }
  220. export default translate(connect(_mapStateToProps)(VirtualBackgroundPreview));