選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VirtualBackgroundPreview.tsx 7.7KB

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