VirtualBackgroundProcessor class

Platform-aware virtual background processor.

This class provides a unified interface for virtual background processing. It automatically selects the appropriate platform-specific segmenter.

Architecture

Uses conditional imports to select the right implementation:

  • Android/iOS: ML Kit Selfie Segmentation
  • Web/Desktop: Stub (graceful fallback)

Integration with React MediaSFU

This implementation mirrors the React version which uses:

  • @mediapipe/selfie_segmentation for web
  • Canvas-based compositing for background replacement

Usage

final processor = VirtualBackgroundProcessor();
await processor.initialize();

// Set background
await processor.setBackground(VirtualBackground.blur(intensity: 0.8));

// Process frames from camera
final result = await processor.processFrame(cameraFrame, width: 640, height: 480);

// When done
await processor.dispose();

Custom Segmenter

You can provide a custom segmenter by setting legacySegmenter:

processor.legacySegmenter = MyCustomSegmenter();

Or use the built-in platform segmenter which is auto-created.

Available extensions

Properties

currentBackground VirtualBackground?
Current virtual background configuration
no setter
hashCode int
The hash code for this object.
no setterinherited
isProcessing bool
Whether the processor is currently processing a frame
no setter
isReady bool
Whether the processor is initialized and ready
no setter
legacySegmenter BackgroundSegmenter?
Optional legacy segmenter for backward compatibility. @deprecated Use the built-in platform segmenter instead.
getter/setter pair
onFrameProcessed ↔ void Function(SegmentationResult)?
Frame processing callback for real-time processing. This is called for each processed frame.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
segmenter BackgroundSegmenter?
Alias for legacySegmenter for backward compatibility.
getter/setter pair
segmenterInfo String
The platform-specific segmenter info
no setter

Methods

applyBlur(Uint8List frameData, {required double intensity, required int width, required int height, Uint8List? mask}) Future<Uint8List?>
Apply blur effect to the background (person stays sharp).
applyColorBackground(Uint8List frameData, {required Color color, required int width, required int height, required Uint8List mask}) Future<Uint8List?>
Apply solid color background replacement.
applyImageBackground(Uint8List frameData, {required Image backgroundImage, required int width, required int height, required Uint8List mask}) Future<Uint8List?>
Apply image background replacement.
createVideoTransformerConfig() Map<String, dynamic>

Available on VirtualBackgroundProcessor, provided by the VirtualBackgroundVideoIntegration extension

Create a video frame transformer for the WebRTC pipeline.
dispose() Future<void>
Dispose of all resources.
initialize([SegmenterConfig? config]) Future<void>
Initialize the processor.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
processFrame(Uint8List frameData, {required int width, required int height, int rotationDegrees = 0, SegmenterInputFormat format = SegmenterInputFormat.rgba8888}) Future<SegmentationResult>
Process a camera frame with the current virtual background.
setBackground(VirtualBackground background) Future<void>
Set the current virtual background.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

isSupported bool
Whether virtual backgrounds are supported on this platform
no setter