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

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