Virtual Background with Effects SDK

Virtual background plugin helps in customising one’s background. The customising options are blurring the background or replacing it with a static image. This guide provides an overview of usage of the virtual background plugin of 100ms.
Pre-requisites
Get the 100ms VirtualBackground Package** (Supported since version 1.11.28)
npm install --save @100mslive/hms-virtual-background@latest
Features
The following features are currently supported under the HMSEffectsPlugin class:
setBlur(blur) {}
setBackground(url) {}
getName() {}
getBackground() {}
setPreset(preset) {}
getPreset() {}
removeEffects() {}
stop() {}
Callbacks supported by the plugin:
On initialization, after the required resources are downloaded by the plugin:
const effectsPlugin = new HMSEffectsPlugin(<key>, () => console.log("Plugin initialised"));
On resolution change (on device rotation or when the video aspect ratio changes):
effectsPlugin.onResolutionChange = (width: number, height: number) => {
console.log(`Resolution changed to ${width}x${height}`)
}
Instantiate Virtual Background
The SDK key for effects is needed to instantiate the HMSEffectsPlugin class:
const effectsKey = useHMSStore(selectEffectsKey);
It is recommended to initialise the object in a separate file to prevent multiple initialisations on re-renders and keep the UI level code independent of internal calls by the SDK.
import { HMSEffectsPlugin, HMSVirtualBackgroundTypes } from '@100mslive/hms-virtual-background';
export class VBPlugin {
private effectsPlugin?: HMSEffectsPlugin | undefined;
initialisePlugin = (effectsSDKKey?: string) => {
if (this.getVBObject()) {
return;
}
if (effectsSDKKey) {
this.effectsPlugin = new HMSEffectsPlugin(effectsSDKKey);
}
};
getBackground = () => {
return this.effectsPlugin?.getBackground();
};
getBlurAmount = () => {
return this.effectsPlugin?.getBlurAmount();
};
getVBObject = () => {
return this.effectsPlugin;
};
getName = () => {
return this.effectsPlugin?.getName();
};
setBlur = async (blurPower: number) => {
this.effectsPlugin?.setBlur(blurPower);
};
setBackground = async (mediaURL: string) => {
this.effectsPlugin?.setBackground(mediaURL);
};
setPreset = (preset: string) => {
this.effectsPlugin.setPreset(preset);
};
getPreset = () => {
return this.effectsPlugin?.getPreset() || '';
};
removeEffects = async () => {
this.effectsPlugin?.removeEffects();
};
reset = () => {
this.effectsPlugin = undefined;
};
}
export const VBHandler = new VBPlugin();
Building the UI
The following snippet illustrates how to add the plugin to the video and manage the UI state to preserve configuration:
import {
selectEffectsKey,
selectIsLocalVideoPluginPresent
selectLocalVideoTrackID,
useHMSStore,
} from '@100mslive/react-sdk';
import {
HMSEffectsPlugin,
HMSVirtualBackgroundTypes
} from '@100mslive/hms-virtual-background';
import { VBHandler } from './VBHandler';
export const VirtualBackgroundPicker = () => {
const hmsActions = useHMSActions();
const effectsKey = useHMSStore(selectEffectsKey);
const trackId = useHMSStore(selectLocalVideoTrackID);
const isPluginAdded = useHMSStore(selectIsLocalVideoPluginPresent(VBHandler?.getName() || ''));
const [background, setBackground] = useState<string | HMSVirtualBackgroundTypes>(
VBHandler.getBackground() as string | HMSVirtualBackgroundTypes,
);
useEffect(() => {
if (!track?.id) {
return;
}
if (!isPluginAdded) {
let vbObject = VBHandler.getVBObject();
if (!vbObject) {
VBHandler.initialisePlugin(effectsKey);
vbObject = VBHandler.getVBObject();
if (effectsKey) {
hmsActions.addPluginsToVideoStream([vbObject as HMSEffectsPlugin]);
}
}
}
}, [hmsActions, isPluginAdded, effectsKey, track?.id]);
}
This handles initialisation and adding the plugin to the video stream. The plugin takes a few seconds on first load during initialisation. Subsequent filter and effect selections should take less than a second to reflect.
The methods can be called via the VBHandler object:
const setBackground = async(mediaURL : string) => {
await VBHandler?.setBackground(mediaURL);
setActiveBackground(mediaURL);
}
const setBlur = async(blurAmount: number) => {
await VBHandler?.setBlur(blurAmount);
setActiveBackground(HMSVirtualBackgroundTypes.BLUR);
}
const removeEffects = async() => {
await VBHandler.removeEffects();
setActiveBackground(HMSVirtualBackgroundTypes.NONE);
}
The full implementation can be viewed in the roomkit-react package.
Supported Browsers
Effects virtual background is supported on Safari, Firefox and Chromium based browsers.