import React from 'react'; import { MediaDeviceMenu, TrackToggle, useLocalParticipant, VideoTrack, } from '@livekit/components-react'; import { BackgroundBlur, VirtualBackground } from '@livekit/track-processors'; import { isLocalTrack, LocalTrackPublication, Track } from 'livekit-client'; import Desk from '../public/background-images/samantha-gades-BlIhVfXbi9s-unsplash.jpg'; import Nature from '../public/background-images/ali-kazal-tbw_KQE3Cbg-unsplash.jpg'; // Background image paths const BACKGROUND_IMAGES = [ { name: 'Desk', path: Desk }, { name: 'Nature', path: Nature }, ]; // Background options type BackgroundType = 'none' | 'blur' | 'image'; export function CameraSettings() { const { cameraTrack, localParticipant } = useLocalParticipant(); const [backgroundType, setBackgroundType] = React.useState( (cameraTrack as LocalTrackPublication)?.track?.getProcessor()?.name === 'background-blur' ? 'blur' : (cameraTrack as LocalTrackPublication)?.track?.getProcessor()?.name === 'virtual-background' ? 'image' : 'none', ); const [virtualBackgroundImagePath, setVirtualBackgroundImagePath] = React.useState( null, ); const camTrackRef = React.useMemo(() => { return { participant: localParticipant, publication: cameraTrack, source: Track.Source.Camera }; }, [localParticipant, cameraTrack]); const selectBackground = (type: BackgroundType, imagePath?: string) => { setBackgroundType(type); if (type === 'image' && imagePath) { setVirtualBackgroundImagePath(imagePath); } else if (type !== 'image') { setVirtualBackgroundImagePath(null); } }; React.useEffect(() => { if (isLocalTrack(cameraTrack?.track)) { if (backgroundType === 'blur') { cameraTrack.track?.setProcessor(BackgroundBlur()); } else if (backgroundType === 'image' && virtualBackgroundImagePath) { cameraTrack.track?.setProcessor(VirtualBackground(virtualBackgroundImagePath)); } else { cameraTrack.track?.stopProcessor(); } } }, [cameraTrack, backgroundType, virtualBackgroundImagePath]); return (
Camera
Background Effects
{BACKGROUND_IMAGES.map((image) => ( ))}
); }