2021-05-06 06:54:23 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import Spinner from '@atlaskit/spinner';
|
2022-03-29 09:07:55 +00:00
|
|
|
import { withStyles } from '@material-ui/core/styles';
|
2021-05-06 06:54:23 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
import { hideDialog } from '../../base/dialog';
|
2021-05-06 06:54:23 +00:00
|
|
|
import { translate } from '../../base/i18n';
|
2021-05-26 11:53:14 +00:00
|
|
|
import { VIDEO_TYPE } from '../../base/media';
|
2021-05-06 06:54:23 +00:00
|
|
|
import Video from '../../base/media/components/Video';
|
|
|
|
import { connect, equals } from '../../base/redux';
|
|
|
|
import { getCurrentCameraDeviceId } from '../../base/settings';
|
|
|
|
import { createLocalTracksF } from '../../base/tracks/functions';
|
2021-11-24 11:05:27 +00:00
|
|
|
import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications';
|
2021-08-26 13:33:43 +00:00
|
|
|
import { showWarningNotification } from '../../notifications/actions';
|
2021-05-06 06:54:23 +00:00
|
|
|
import { toggleBackgroundEffect } from '../actions';
|
2021-05-26 11:53:14 +00:00
|
|
|
import { VIRTUAL_BACKGROUND_TYPE } from '../constants';
|
2021-05-19 09:57:11 +00:00
|
|
|
import { localTrackStopped } from '../functions';
|
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}.
|
|
|
|
*/
|
|
|
|
export type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The deviceId of the camera device currently being used.
|
|
|
|
*/
|
|
|
|
_currentCameraDeviceId: string,
|
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
/**
|
|
|
|
* An object containing the CSS classes.
|
|
|
|
*/
|
|
|
|
classes: Object,
|
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
|
|
|
* The redux {@code dispatch} function.
|
|
|
|
*/
|
|
|
|
dispatch: Function,
|
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
/**
|
|
|
|
* Dialog callback that indicates if the background preview was loaded.
|
|
|
|
*/
|
|
|
|
loadedPreview: Function,
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
options: Object,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
|
|
|
t: Function
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} state of {@link VirtualBackgroundPreview}.
|
|
|
|
*/
|
|
|
|
type State = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loader activated on setting virtual background.
|
|
|
|
*/
|
|
|
|
loading: boolean,
|
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
/**
|
|
|
|
* Flag that indicates if the local track was loaded.
|
|
|
|
*/
|
|
|
|
localTrackLoaded: boolean,
|
|
|
|
|
2021-05-06 06:54:23 +00:00
|
|
|
/**
|
|
|
|
* Activate the selected device camera only.
|
|
|
|
*/
|
|
|
|
jitsiTrack: Object
|
|
|
|
};
|
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
/**
|
|
|
|
* Creates the styles for the component.
|
|
|
|
*
|
|
|
|
* @param {Object} theme - The current UI theme.
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
const styles = theme => {
|
|
|
|
return {
|
|
|
|
virtualBackgroundPreview: {
|
|
|
|
'& .video-preview': {
|
|
|
|
height: '250px'
|
|
|
|
},
|
|
|
|
|
|
|
|
'& .video-background-preview-entry': {
|
|
|
|
marginLeft: '-10px',
|
|
|
|
height: '250px',
|
|
|
|
width: '570px',
|
|
|
|
marginBottom: `${theme.spacing(2)}px`,
|
|
|
|
zIndex: 2,
|
|
|
|
|
|
|
|
'@media (max-width: 632px)': {
|
|
|
|
maxWidth: '336px'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'& .video-preview-loader': {
|
|
|
|
borderRadius: '6px',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
height: '250px',
|
|
|
|
marginBottom: `${theme.spacing(2)}px`,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
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>}
|
|
|
|
*/
|
|
|
|
_stopStream(jitsiTrack) {
|
|
|
|
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;
|
|
|
|
}
|
2021-06-20 02:17:52 +00:00
|
|
|
|
2021-08-26 13:33:43 +00:00
|
|
|
if (this.props.options.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE
|
|
|
|
&& this.state.localTrackLoaded) {
|
2021-06-20 02:17:52 +00:00
|
|
|
this._applyBackgroundEffect();
|
|
|
|
}
|
2021-05-06 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
invertColor = { true }
|
|
|
|
isCompleting = { false }
|
|
|
|
size = { 'large' } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a preview entry.
|
|
|
|
*
|
|
|
|
* @param {Object} data - The track data.
|
|
|
|
* @returns {React$Node}
|
|
|
|
*/
|
|
|
|
_renderPreviewEntry(data) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
async componentDidUpdate(prevProps) {
|
|
|
|
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-26 11:53:14 +00:00
|
|
|
if (prevProps.options.backgroundType === VIRTUAL_BACKGROUND_TYPE.DESKTOP_SHARE) {
|
2021-05-19 09:57:11 +00:00
|
|
|
prevProps.options.url.dispose();
|
|
|
|
}
|
2021-05-06 06:54:23 +00:00
|
|
|
this._applyBackgroundEffect();
|
|
|
|
}
|
2021-05-26 11:53:14 +00:00
|
|
|
if (this.props.options.url?.videoType === VIDEO_TYPE.DESKTOP) {
|
2021-05-19 09:57:11 +00:00
|
|
|
localTrackStopped(this.props.dispatch, this.props.options.url, this.state.jitsiTrack);
|
|
|
|
}
|
2021-05-06 06:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}}
|
|
|
|
*/
|
|
|
|
function _mapStateToProps(state): Object {
|
|
|
|
return {
|
|
|
|
_currentCameraDeviceId: getCurrentCameraDeviceId(state)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:07:55 +00:00
|
|
|
export default translate(connect(_mapStateToProps)(withStyles(styles)(VirtualBackgroundPreview)));
|