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

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