Merge pull request #20788 from JoVictorNunes/virtual-bg-enhancements-0726

enhancement(webcam): custom virtual backgrounds
This commit is contained in:
Ramón Souza 2024-07-29 10:41:35 -03:00 committed by GitHub
commit 1e2731ef63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 153 additions and 92 deletions

View File

@ -5,6 +5,7 @@ import {
} from 'react-intl'; } from 'react-intl';
import Button from '/imports/ui/components/common/button/component'; import Button from '/imports/ui/components/common/button/component';
import VirtualBgSelector from '/imports/ui/components/video-preview/virtual-background/component'; import VirtualBgSelector from '/imports/ui/components/video-preview/virtual-background/component';
import VirtualBgService from '/imports/ui/components/video-preview/virtual-background/service';
import logger from '/imports/startup/client/logger'; import logger from '/imports/startup/client/logger';
import browserInfo from '/imports/utils/browserInfo'; import browserInfo from '/imports/utils/browserInfo';
import PreviewService from './service'; import PreviewService from './service';
@ -19,10 +20,15 @@ import {
setSessionVirtualBackgroundInfo, setSessionVirtualBackgroundInfo,
getSessionVirtualBackgroundInfo, getSessionVirtualBackgroundInfo,
isVirtualBackgroundSupported, isVirtualBackgroundSupported,
clearSessionVirtualBackgroundInfo,
getSessionVirtualBackgroundInfoWithDefault,
} from '/imports/ui/services/virtual-background/service'; } from '/imports/ui/services/virtual-background/service';
import Settings from '/imports/ui/services/settings'; import Settings from '/imports/ui/services/settings';
import { isVirtualBackgroundsEnabled } from '/imports/ui/services/features'; import { isVirtualBackgroundsEnabled } from '/imports/ui/services/features';
import Checkbox from '/imports/ui/components/common/checkbox/component'; import Checkbox from '/imports/ui/components/common/checkbox/component';
import { CustomVirtualBackgroundsContext } from '/imports/ui/components/video-preview/virtual-background/context';
import Auth from '/imports/ui/services/auth';
import Users from '/imports/api/users';
const VIEW_STATES = { const VIEW_STATES = {
finding: 'finding', finding: 'finding',
@ -34,7 +40,9 @@ const ENABLE_CAMERA_BRIGHTNESS = Meteor.settings.public.app.enableCameraBrightne
const CAMERA_BRIGHTNESS_AVAILABLE = ENABLE_CAMERA_BRIGHTNESS && isVirtualBackgroundSupported(); const CAMERA_BRIGHTNESS_AVAILABLE = ENABLE_CAMERA_BRIGHTNESS && isVirtualBackgroundSupported();
const propTypes = { const propTypes = {
intl: PropTypes.object.isRequired, intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
closeModal: PropTypes.func.isRequired, closeModal: PropTypes.func.isRequired,
startSharing: PropTypes.func.isRequired, startSharing: PropTypes.func.isRequired,
stopSharing: PropTypes.func.isRequired, stopSharing: PropTypes.func.isRequired,
@ -43,6 +51,7 @@ const propTypes = {
hasVideoStream: PropTypes.bool.isRequired, hasVideoStream: PropTypes.bool.isRequired,
webcamDeviceId: PropTypes.string, webcamDeviceId: PropTypes.string,
sharedDevices: PropTypes.arrayOf(PropTypes.string), sharedDevices: PropTypes.arrayOf(PropTypes.string),
cameraAsContent: PropTypes.bool,
}; };
const defaultProps = { const defaultProps = {
@ -50,6 +59,7 @@ const defaultProps = {
camCapReached: true, camCapReached: true,
webcamDeviceId: null, webcamDeviceId: null,
sharedDevices: [], sharedDevices: [],
cameraAsContent: false,
}; };
const intlMessages = defineMessages({ const intlMessages = defineMessages({
@ -270,9 +280,44 @@ class VideoPreview extends Component {
webcamDeviceId, webcamDeviceId,
forceOpen, forceOpen,
} = this.props; } = this.props;
const { dispatch, backgrounds } = this.context;
this._isMounted = true; this._isMounted = true;
// Set the custom or default virtual background
const webcamBackground = Users.findOne({
meetingId: Auth.meetingID,
userId: Auth.userID,
}, {
fields: {
webcamBackground: 1,
},
});
const webcamBackgroundURL = webcamBackground?.webcamBackground;
if (webcamBackgroundURL !== '' && !backgrounds.webcamBackgroundURL) {
VirtualBgService.getFileFromUrl(webcamBackgroundURL).then((fetchedWebcamBackground) => {
if (fetchedWebcamBackground) {
const data = URL.createObjectURL(fetchedWebcamBackground);
const uniqueId = 'webcamBackgroundURL';
const filename = webcamBackgroundURL;
dispatch({
type: 'update',
background: {
filename,
uniqueId,
data,
lastActivityDate: Date.now(),
custom: true,
sessionOnly: true,
},
});
} else {
logger.error('Failed to fetch custom webcam background image. Using fallback image.');
}
});
}
if (deviceInfo.hasMediaDevices) { if (deviceInfo.hasMediaDevices) {
navigator.mediaDevices.enumerateDevices().then((devices) => { navigator.mediaDevices.enumerateDevices().then((devices) => {
VideoService.updateNumberOfDevices(devices); VideoService.updateNumberOfDevices(devices);
@ -620,8 +665,54 @@ class VideoPreview extends Component {
}); });
} }
async startEffects(deviceId) {
// Brightness and backgrounds are independent of each other,
// handle each one separately.
try {
await this.startCameraBrightness();
} catch (error) {
logger.warn({
logCode: 'brightness_effect_error',
extraInfo: {
errorName: error.name,
errorMessage: error.message,
},
}, 'Failed to start brightness effect');
}
let type;
let name;
let customParams;
const { backgrounds } = this.context;
const { webcamBackgroundURL } = backgrounds;
const storedBackgroundInfo = getSessionVirtualBackgroundInfo(deviceId);
if (storedBackgroundInfo) {
type = storedBackgroundInfo.type;
name = storedBackgroundInfo.name;
customParams = storedBackgroundInfo.customParams;
} else if (webcamBackgroundURL) {
const { data, filename } = webcamBackgroundURL;
type = EFFECT_TYPES.IMAGE_TYPE;
name = filename;
customParams = { file: data };
}
if (!type) return Promise.resolve(true);
try {
return this.handleVirtualBgSelected(type, name, customParams);
} catch (error) {
this.handleVirtualBgError(error, type, name);
clearSessionVirtualBackgroundInfo(deviceId);
throw error;
}
}
getCameraStream(deviceId, profile) { getCameraStream(deviceId, profile) {
const { webcamDeviceId } = this.state; const { webcamDeviceId } = this.state;
const { cameraAsContent } = this.props;
this.setState({ this.setState({
selectedProfile: profile.id, selectedProfile: profile.id,
@ -635,17 +726,33 @@ class VideoPreview extends Component {
// The return of doGUM is an instance of BBBVideoStream (a thin wrapper over a MediaStream) // The return of doGUM is an instance of BBBVideoStream (a thin wrapper over a MediaStream)
return PreviewService.doGUM(deviceId, profile).then((bbbVideoStream) => { return PreviewService.doGUM(deviceId, profile).then((bbbVideoStream) => {
// Late GUM resolve, clean up tracks, stop. // Late GUM resolve, clean up tracks, stop.
if (!this._isMounted) return this.terminateCameraStream(bbbVideoStream, deviceId); if (!this._isMounted) {
this.terminateCameraStream(bbbVideoStream, deviceId);
this.cleanupStreamAndVideo();
return Promise.resolve(false);
}
this.currentVideoStream = bbbVideoStream; this.currentVideoStream = bbbVideoStream;
this.startCameraBrightness().then(() => { this.updateDeviceId(deviceId);
const { type, name, customParams } = getSessionVirtualBackgroundInfo(deviceId);
this.handleVirtualBgSelected(type, name, customParams).then(() => { if (cameraAsContent) return Promise.resolve(true);
this.setState({
isStartSharingDisabled: false, return this.startEffects(deviceId)
}); .catch((error) => {
if (this.shouldSkipVideoPreview()) {
throw error;
}
})
.finally(() => {
if (this._isMounted) {
this.setState({
isStartSharingDisabled: false,
});
} else {
this.terminateCameraStream(bbbVideoStream, deviceId);
this.cleanupStreamAndVideo();
}
}); });
});
}).catch((error) => { }).catch((error) => {
// When video preview is set to skip, we need some way to bubble errors // When video preview is set to skip, we need some way to bubble errors
// up to users; so re-throw the error // up to users; so re-throw the error
@ -917,7 +1024,7 @@ class VideoPreview extends Component {
const initialVirtualBgState = this.currentVideoStream ? { const initialVirtualBgState = this.currentVideoStream ? {
type: this.currentVideoStream.virtualBgType, type: this.currentVideoStream.virtualBgType,
name: this.currentVideoStream.virtualBgName name: this.currentVideoStream.virtualBgName
} : getSessionVirtualBackgroundInfo(webcamDeviceId); } : getSessionVirtualBackgroundInfoWithDefault(webcamDeviceId);
return ( return (
<VirtualBgSelector <VirtualBgSelector
@ -1166,5 +1273,6 @@ class VideoPreview extends Component {
VideoPreview.propTypes = propTypes; VideoPreview.propTypes = propTypes;
VideoPreview.defaultProps = defaultProps; VideoPreview.defaultProps = defaultProps;
VideoPreview.contextType = CustomVirtualBackgroundsContext;
export default injectIntl(VideoPreview); export default injectIntl(VideoPreview);

View File

@ -18,8 +18,6 @@ import Skeleton, { SkeletonTheme } from 'react-loading-skeleton';
import 'react-loading-skeleton/dist/skeleton.css'; import 'react-loading-skeleton/dist/skeleton.css';
import Settings from '/imports/ui/services/settings'; import Settings from '/imports/ui/services/settings';
import { isCustomVirtualBackgroundsEnabled } from '/imports/ui/services/features'; import { isCustomVirtualBackgroundsEnabled } from '/imports/ui/services/features';
import Auth from '/imports/ui/services/auth';
import Users from '/imports/api/users';
const { MIME_TYPES_ALLOWED, MAX_FILE_SIZE } = VirtualBgService; const { MIME_TYPES_ALLOWED, MAX_FILE_SIZE } = VirtualBgService;
const ENABLE_CAMERA_BRIGHTNESS = Meteor.settings.public.app.enableCameraBrightness; const ENABLE_CAMERA_BRIGHTNESS = Meteor.settings.public.app.enableCameraBrightness;
@ -96,28 +94,6 @@ const VIRTUAL_BACKGROUNDS_CONFIG = Meteor.settings.public.virtualBackgrounds;
const ENABLE_UPLOAD = VIRTUAL_BACKGROUNDS_CONFIG.enableVirtualBackgroundUpload; const ENABLE_UPLOAD = VIRTUAL_BACKGROUNDS_CONFIG.enableVirtualBackgroundUpload;
const shouldEnableBackgroundUpload = () => ENABLE_UPLOAD && isCustomVirtualBackgroundsEnabled(); const shouldEnableBackgroundUpload = () => ENABLE_UPLOAD && isCustomVirtualBackgroundsEnabled();
// Function to convert image URL to a File object
async function getFileFromUrl(url) {
try {
const response = await fetch(url, {
credentials: 'omit',
mode: 'cors',
headers: {
Accept: 'image/*',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const file = new File([blob], 'fetchedWebcamBackground', { type: blob.type });
return file;
} catch (error) {
logger.error('Fetch error:', error);
return null;
}
}
const VirtualBgSelector = ({ const VirtualBgSelector = ({
intl, intl,
handleVirtualBgSelected, handleVirtualBgSelected,
@ -159,51 +135,6 @@ const VirtualBgSelector = ({
} }
if (!loaded) loadFromDB(); if (!loaded) loadFromDB();
} }
// Set the custom or default virtual background
const webcamBackground = Users.findOne({
meetingId: Auth.meetingID,
userId: Auth.userID,
}, {
fields: {
webcamBackground: 1,
},
});
const webcamBackgroundURL = webcamBackground?.webcamBackground;
if (webcamBackgroundURL !== '' && !backgrounds.webcamBackgroundURL) {
getFileFromUrl(webcamBackgroundURL).then((fetchedWebcamBackground) => {
if (fetchedWebcamBackground) {
const data = URL.createObjectURL(fetchedWebcamBackground);
const uniqueId = 'webcamBackgroundURL';
const filename = webcamBackgroundURL;
dispatch({
type: 'update',
background: {
filename,
uniqueId,
data,
lastActivityDate: Date.now(),
custom: true,
sessionOnly: true,
},
});
handleVirtualBgSelected(
EFFECT_TYPES.IMAGE_TYPE,
webcamBackgroundURL,
{ file: data, uniqueId },
).then((switched) => {
if (!switched) {
setCurrentVirtualBg({ type: EFFECT_TYPES.NONE_TYPE });
return;
}
setCurrentVirtualBg({ type: EFFECT_TYPES.IMAGE_TYPE, name: filename });
});
} else {
logger.error('Failed to fetch custom webcam background image. Using fallback image.');
}
});
}
}, []); }, []);
const _virtualBgSelected = (type, name, index, customParams) => const _virtualBgSelected = (type, name, index, customParams) =>

View File

@ -104,6 +104,28 @@ const update = (background) => {
}); });
}; };
// Function to convert image URL to a File object
async function getFileFromUrl(url) {
try {
const response = await fetch(url, {
credentials: 'omit',
mode: 'cors',
headers: {
Accept: 'image/*',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const file = new File([blob], 'fetchedWebcamBackground', { type: blob.type });
return file;
} catch (error) {
logger.error('Fetch error:', error);
return null;
}
}
export default { export default {
load, load,
save, save,
@ -111,4 +133,5 @@ export default {
update, update,
MIME_TYPES_ALLOWED, MIME_TYPES_ALLOWED,
MAX_FILE_SIZE, MAX_FILE_SIZE,
getFileFromUrl,
}; };

View File

@ -89,7 +89,8 @@ const ThumbnailButton = styled(Button)`
${({ background }) => background && ` ${({ background }) => background && `
background-image: url(${background}); background-image: url(${background});
background-size: 46px 46px; background-size: cover;
background-position: center;
background-origin: padding-box; background-origin: padding-box;
&:active { &:active {

View File

@ -17,7 +17,7 @@ import MediaStreamUtils from '/imports/utils/media-stream-utils';
import BBBVideoStream from '/imports/ui/services/webrtc-base/bbb-video-stream'; import BBBVideoStream from '/imports/ui/services/webrtc-base/bbb-video-stream';
import { import {
EFFECT_TYPES, EFFECT_TYPES,
getSessionVirtualBackgroundInfo, getSessionVirtualBackgroundInfoWithDefault,
} from '/imports/ui/services/virtual-background/service'; } from '/imports/ui/services/virtual-background/service';
import { notify } from '/imports/ui/services/notification'; import { notify } from '/imports/ui/services/notification';
import { shouldForceRelay } from '/imports/ui/services/bbb-webrtc-sfu/utils'; import { shouldForceRelay } from '/imports/ui/services/bbb-webrtc-sfu/utils';
@ -1049,7 +1049,7 @@ class VideoProvider extends Component {
peer.bbbVideoStream.mediaStream, peer.bbbVideoStream.mediaStream,
'video', 'video',
); );
const { type, name } = getSessionVirtualBackgroundInfo(deviceId); const { type, name } = getSessionVirtualBackgroundInfoWithDefault(deviceId);
this.restoreVirtualBackground(peer.bbbVideoStream, type, name).catch((error) => { this.restoreVirtualBackground(peer.bbbVideoStream, type, name).catch((error) => {
this.handleVirtualBgError(error, type, name); this.handleVirtualBgError(error, type, name);

View File

@ -73,18 +73,15 @@ const setSessionVirtualBackgroundInfo = (
deviceId, deviceId,
) => Session.set(`VirtualBackgroundInfo_${deviceId}`, { type, name, customParams }); ) => Session.set(`VirtualBackgroundInfo_${deviceId}`, { type, name, customParams });
const getSessionVirtualBackgroundInfo = (deviceId) => Session.get(`VirtualBackgroundInfo_${deviceId}`) || { const getSessionVirtualBackgroundInfo = (deviceId) => Session.get(`VirtualBackgroundInfo_${deviceId}`);
const clearSessionVirtualBackgroundInfo = (deviceId) => Session.set(`VirtualBackgroundInfo_${deviceId}`, null);
const getSessionVirtualBackgroundInfoWithDefault = (deviceId) => Session.get(`VirtualBackgroundInfo_${deviceId}`) || {
type: EFFECT_TYPES.NONE_TYPE, type: EFFECT_TYPES.NONE_TYPE,
name: '', name: '',
}; };
const getSessionVirtualBackgroundInfoWithDefault = (deviceId) => {
return Session.get(`VirtualBackgroundInfo_${deviceId}`) || {
type: EFFECT_TYPES.BLUR_TYPE,
name: BLUR_FILENAME,
};
}
const isVirtualBackgroundSupported = () => { const isVirtualBackgroundSupported = () => {
return !(deviceInfo.isIos || browserInfo.isSafari); return !(deviceInfo.isIos || browserInfo.isSafari);
} }
@ -110,4 +107,5 @@ export {
createVirtualBackgroundStream, createVirtualBackgroundStream,
getVirtualBackgroundThumbnail, getVirtualBackgroundThumbnail,
getVirtualBgImagePath, getVirtualBgImagePath,
} clearSessionVirtualBackgroundInfo,
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB