import { Image } from 'react-native'; import type { ImageRequireSource, ImageResolvedAssetSource, ImageURISource, } from 'react-native'; import { HMSVideoPlugin } from './HMSVideoPlugin'; export class HMSVirtualBackgroundPlugin extends HMSVideoPlugin { static NAME = 'HMSVirtualBackgroundPlugin'; constructor() { super(HMSVirtualBackgroundPlugin.NAME); } /** * Sets Blur as background * @returns {Promise} A promise that resolves to true when blur effect has been applied as background, otherwise, rejected promise is returned */ setBlur(blurRadius: number): Promise { const data = { id: '12345', background: { type: 'blur', blurRadius }, }; return this.nativeModule.changeVirtualBackground(data); } /** * Sets provided image as background * @param backgroundImage An image to apply as background on * @returns {Promise} A promise that resolves to true when provided background image has been applied as background, otherwise, rejected promise is returned * * Example Usage: * ``` * // Create instance of `HMSVirtualBackgroundPlugin` class * const hmsVirtualBackgroundPlugin = HMSVirtualBackgroundPlugin(); * ... * // In ON_PREVIEW or ON_JOIN event callback method * const image = require('path to image file'); * hmsVirtualBackgroundPlugin.setBackground(image); * * * ``` */ setBackground( backgroundImage: ImageURISource | ImageRequireSource ): Promise { const background = resolveBackground(backgroundImage); const data = { id: '12345', background, }; return this.nativeModule.changeVirtualBackground(data); } } function resolveBackground(background: ImageURISource | ImageRequireSource): { type: 'image'; source: ImageResolvedAssetSource | ImageURISource; } { // Check if it's a remote URL with dimensions already provided if ( typeof background === 'object' && 'uri' in background && background.uri && (background.uri.startsWith('http://') || background.uri.startsWith('https://')) && background.width && background.height ) { // For remote URLs with dimensions, pass them through directly // Don't use Image.resolveAssetSource() as it doesn't handle remote URLs properly return { type: 'image', source: background as ImageURISource, }; } // For bundled assets (require()) or local file:// URLs, use resolveAssetSource return { type: 'image', source: Image.resolveAssetSource(background), }; }