2021-05-06 06:54:23 +00:00
|
|
|
import Spinner from '@atlaskit/spinner';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
|
|
|
import { withStyles } from '@mui/styles';
|
2021-05-06 06:54:23 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2022-08-25 11:35:19 +00:00
|
|
|
import { WithTranslation } from 'react-i18next';
|
2022-10-11 10:47:54 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
import { IState } from '../../app/types';
|
2022-10-11 10:47:54 +00:00
|
|
|
import { hideDialog } from '../../base/dialog/actions';
|
|
|
|
import { translate } from '../../base/i18n/functions';
|
|
|
|
// eslint-disable-next-line lines-around-comment
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2021-05-06 06:54:23 +00:00
|
|
|
import Video from '../../base/media/components/Video';
|
2022-10-11 10:47:54 +00:00
|
|
|
import { equals } from '../../base/redux/functions';
|
|
|
|
import { getCurrentCameraDeviceId } from '../../base/settings/functions.web';
|
2021-05-06 06:54:23 +00:00
|
|
|
import { createLocalTracksF } from '../../base/tracks/functions';
|
2021-08-26 13:33:43 +00:00
|
|
|
import { showWarningNotification } from '../../notifications/actions';
|
2022-09-06 17:32:20 +00:00
|
|
|
import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
|
2021-05-06 06:54:23 +00:00
|
|
|
import { toggleBackgroundEffect } from '../actions';
|
2021-08-26 13:33:43 +00:00
|
|
|
import logger from '../logger';
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2021-07-08 17:20:12 +00:00
|
|
|
const videoClassName = 'video-preview-video';
|
2021-05-06 06:54:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the React {@code PureComponent} props of {@link VirtualBackgroundPreview}.
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
export type Props = WithTranslation & {
|
2021-05-06 06:54:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The deviceId of the camera device currently being used.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
_currentCameraDeviceId: string;
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
/**
|
|
|
|
* An object containing the CSS classes.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
classes: any;
|
2022-03-29 09:07:55 +00:00
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
|
|
|
* The redux {@code dispatch} function.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
dispatch: Function;
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
/**
|
|
|
|
* Dialog callback that indicates if the background preview was loaded.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
loadedPreview: Function;
|
2021-08-26 13:33:43 +00:00
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
2022-07-14 07:10:08 +00:00
|
|
|
* Represents the virtual background set options.
|
2021-05-06 06:54:23 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
options: any;
|
2021-05-06 06:54:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} state of {@link VirtualBackgroundPreview}.
|
|
|
|
*/
|
|
|
|
type State = {
|
|
|
|
|
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* Activate the selected device camera only.
|
2021-05-06 06:54:23 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
jitsiTrack: Object | null;
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* Loader activated on setting virtual background.
|
2021-08-26 13:33:43 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
loading: boolean;
|
2021-08-26 13:33:43 +00:00
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* Flag that indicates if the local track was loaded.
|
2021-05-06 06:54:23 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
localTrackLoaded: boolean;
|
2021-05-06 06:54:23 +00:00
|
|
|
};
|
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
/**
|
|
|
|
* Creates the styles for the component.
|
|
|
|
*
|
|
|
|
* @param {Object} theme - The current UI theme.
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-09-13 07:36:00 +00:00
|
|
|
const styles = (theme: Theme) => {
|
2022-03-29 09:07:55 +00:00
|
|
|
return {
|
|
|
|
virtualBackgroundPreview: {
|
|
|
|
'& .video-preview': {
|
|
|
|
height: '250px'
|
|
|
|
},
|
|
|
|
|
|
|
|
'& .video-background-preview-entry': {
|
|
|
|
height: '250px',
|
|
|
|
width: '570px',
|
2022-09-13 07:36:00 +00:00
|
|
|
marginBottom: theme.spacing(2),
|
2022-03-29 09:07:55 +00:00
|
|
|
zIndex: 2,
|
|
|
|
|
|
|
|
'@media (max-width: 632px)': {
|
|
|
|
maxWidth: '336px'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'& .video-preview-loader': {
|
|
|
|
borderRadius: '6px',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
height: '250px',
|
2022-09-13 07:36:00 +00:00
|
|
|
marginBottom: theme.spacing(2),
|
2022-03-29 09:07:55 +00:00
|
|
|
width: '572px',
|
|
|
|
position: 'fixed',
|
|
|
|
zIndex: 2,
|
|
|
|
|
|
|
|
'& svg': {
|
|
|
|
position: 'absolute',
|
|
|
|
top: '40%',
|
|
|
|
left: '45%'
|
|
|
|
},
|
|
|
|
|
|
|
|
'@media (min-width: 432px) and (max-width: 632px)': {
|
|
|
|
width: '340px'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
|
|
|
* Implements a React {@link PureComponent} which displays the virtual
|
|
|
|
* background preview.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments PureComponent
|
2021-05-06 06:54:23 +00:00
|
|
|
*/
|
|
|
|
class VirtualBackgroundPreview extends PureComponent<Props, State> {
|
|
|
|
_componentWasUnmounted: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code VirtualBackgroundPreview} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
constructor(props: Props) {
|
2021-05-06 06:54:23 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
loading: false,
|
2021-08-26 13:33:43 +00:00
|
|
|
localTrackLoaded: false,
|
2021-05-06 06:54:23 +00:00
|
|
|
jitsiTrack: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:36:49 +00:00
|
|
|
/**
|
|
|
|
* Destroys the jitsiTrack object.
|
|
|
|
*
|
|
|
|
* @param {Object} jitsiTrack - The track that needs to be disposed.
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
_stopStream(jitsiTrack: any) {
|
2021-05-12 09:36:49 +00:00
|
|
|
if (jitsiTrack) {
|
|
|
|
jitsiTrack.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
|
|
|
* Creates and updates the track data.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
async _setTracks() {
|
2021-08-26 13:33:43 +00:00
|
|
|
try {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
const [ jitsiTrack ] = await createLocalTracksF({
|
|
|
|
cameraDeviceId: this.props._currentCameraDeviceId,
|
|
|
|
devices: [ 'video' ]
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({ localTrackLoaded: true });
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
// In case the component gets unmounted before the tracks are created
|
|
|
|
// avoid a leak by not setting the state
|
|
|
|
if (this._componentWasUnmounted) {
|
|
|
|
this._stopStream(jitsiTrack);
|
2021-05-12 09:36:49 +00:00
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
jitsiTrack,
|
|
|
|
loading: false
|
|
|
|
});
|
|
|
|
this.props.loadedPreview(true);
|
|
|
|
} catch (error) {
|
|
|
|
this.props.dispatch(hideDialog());
|
|
|
|
this.props.dispatch(
|
|
|
|
showWarningNotification({
|
|
|
|
titleKey: 'virtualBackground.backgroundEffectError',
|
|
|
|
description: 'Failed to access camera device.'
|
2021-11-24 11:05:27 +00:00
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.LONG)
|
2021-08-26 13:33:43 +00:00
|
|
|
);
|
|
|
|
logger.error('Failed to access camera device. Error on apply background effect.');
|
2021-05-12 09:36:49 +00:00
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply background effect on video preview.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async _applyBackgroundEffect() {
|
|
|
|
this.setState({ loading: true });
|
2021-08-26 13:33:43 +00:00
|
|
|
this.props.loadedPreview(false);
|
2021-05-06 06:54:23 +00:00
|
|
|
await this.props.dispatch(toggleBackgroundEffect(this.props.options, this.state.jitsiTrack));
|
2021-08-26 13:33:43 +00:00
|
|
|
this.props.loadedPreview(true);
|
2021-05-06 06:54:23 +00:00
|
|
|
this.setState({ loading: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply video preview loader.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
_loadVideoPreview() {
|
|
|
|
return (
|
|
|
|
<div className = 'video-preview-loader'>
|
|
|
|
<Spinner
|
2022-10-11 10:47:54 +00:00
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2021-05-06 06:54:23 +00:00
|
|
|
invertColor = { true }
|
|
|
|
isCompleting = { false }
|
|
|
|
size = { 'large' } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a preview entry.
|
|
|
|
*
|
|
|
|
* @param {Object} data - The track data.
|
|
|
|
* @returns {React$Node}
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
_renderPreviewEntry(data: Object) {
|
2021-05-06 06:54:23 +00:00
|
|
|
const { t } = this.props;
|
|
|
|
const className = 'video-background-preview-entry';
|
|
|
|
|
|
|
|
if (this.state.loading) {
|
|
|
|
return this._loadVideoPreview();
|
|
|
|
}
|
|
|
|
if (!data) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className = { className }
|
|
|
|
video-preview-container = { true }>
|
|
|
|
<div className = 'video-preview-error'>{t('deviceSelection.previewUnavailable')}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const props: Object = {
|
|
|
|
className
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div { ...props }>
|
|
|
|
<Video
|
|
|
|
className = { videoClassName }
|
|
|
|
playsinline = { true }
|
|
|
|
videoTrack = {{ jitsiTrack: data }} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#componentDidMount}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
this._setTracks();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#componentWillUnmount}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._componentWasUnmounted = true;
|
2021-05-12 09:36:49 +00:00
|
|
|
this._stopStream(this.state.jitsiTrack);
|
2021-05-06 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#componentDidUpdate}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
async componentDidUpdate(prevProps: Props) {
|
2021-05-06 06:54:23 +00:00
|
|
|
if (!equals(this.props._currentCameraDeviceId, prevProps._currentCameraDeviceId)) {
|
|
|
|
this._setTracks();
|
|
|
|
}
|
2021-08-26 13:33:43 +00:00
|
|
|
if (!equals(this.props.options, prevProps.options) && this.state.localTrackLoaded) {
|
2021-05-06 06:54:23 +00:00
|
|
|
this._applyBackgroundEffect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
2022-03-29 11:14:23 +00:00
|
|
|
const { jitsiTrack } = this.state;
|
|
|
|
const { classes } = this.props;
|
2021-05-06 06:54:23 +00:00
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
return (<div className = { classes.virtualBackgroundPreview }>
|
|
|
|
{jitsiTrack
|
|
|
|
? <div className = 'video-preview'>{this._renderPreviewEntry(jitsiTrack)}</div>
|
|
|
|
: <div className = 'video-preview-loader'>{this._loadVideoPreview()}</div>
|
|
|
|
}</div>);
|
2021-05-06 06:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps (parts of) the redux state to the associated props for the
|
|
|
|
* {@code VirtualBackgroundPreview} component.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @private
|
|
|
|
* @returns {{Props}}
|
|
|
|
*/
|
2022-10-11 10:47:54 +00:00
|
|
|
function _mapStateToProps(state: IState) {
|
2021-05-06 06:54:23 +00:00
|
|
|
return {
|
|
|
|
_currentCameraDeviceId: getCurrentCameraDeviceId(state)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
export default translate(connect(_mapStateToProps)(withStyles(styles)(VirtualBackgroundPreview)));
|