Virtual Background

Updated

Blur the background or replace it with a solid color or an image.

Virtual Background enables users to blur their background, or replace it with a solid color or an image. This feature is applicable to use-cases such as online conferences, online classes, and live streaming. It helps protect personal privacy and reduces audience distraction.

Understand the tech

Virtual Background offers the following options:

FeatureExample
Blurred background and image background
Video/Animated background
Portrait-in-picture Allows the presenter to use slides as the virtual background while superimposing their video. The effect is similar to a weather news cast on television, preventing interruptions during a layout toggle.

Want to test Virtual Background? Try the online demo.

Prerequisites

Ensure that you have implemented the SDK quickstart in your project.

Implement virtual background

This section shows you how to add a virtual background to the local video.

Check device compatibility

To avoid performance degradation or unavailable features when enabling Virtual Background on low-end devices, check whether the device supports the feature.

private bool IsFeatureAvailable() {
  return agoraEngine.IsFeatureAvailableOnDevice(FeatureType.VIDEO_VIRTUAL_BACKGROUND);
}

Set a blurred background

To blur the video background, use the following code:

private void SetBackgroundBlur()
{
  if(IsFeatureAvailable() == false)
  {
    Debug.Log("Device does not support the virtual background feature");
    return;
  }

  VirtualBackgroundSource virtualBackgroundSource = new VirtualBackgroundSource();

  // Set background blur
  virtualBackgroundSource.background_source_type = BACKGROUND_SOURCE_TYPE.BACKGROUND_BLUR;
  virtualBackgroundSource.blur_degree = BACKGROUND_BLUR_DEGREE.BLUR_DEGREE_HIGH;

  // Set processing properties for background
  SegmentationProperty segmentationProperty = new SegmentationProperty();
  segmentationProperty.modelType = SEG_MODEL_TYPE.SEG_MODEL_AI; // Use SEG_MODEL_GREEN if you have a green background
  segmentationProperty.greenCapacity = 0.5F; // Accuracy for identifying green colors (range 0-1)

  // Enable or disable virtual background
  agoraEngine.EnableVirtualBackground(true, virtualBackgroundSource, segmentationProperty, MEDIA_SOURCE_TYPE.PRIMARY_CAMERA_SOURCE);
}

Set a color background

To apply a solid color as the virtual background, use a hexadecimal color code. For example, 0x0000FF for blue:

private void SetBackgroundColor()
{
  if(IsFeatureAvailable() == false)
  {
    Debug.Log("Device does not support the virtual background feature");
    return;
  }

  VirtualBackgroundSource virtualBackgroundSource = new VirtualBackgroundSource();

  // Set a solid background color
  virtualBackgroundSource.background_source_type = BACKGROUND_SOURCE_TYPE.BACKGROUND_COLOR;
  virtualBackgroundSource.color = 0x0000FF;

  // Set processing properties for background
  SegmentationProperty segmentationProperty = new SegmentationProperty();
  segmentationProperty.modelType = SEG_MODEL_TYPE.SEG_MODEL_AI; // Use SEG_MODEL_GREEN if you have a green background
  segmentationProperty.greenCapacity = 0.5F; // Accuracy for identifying green colors (range 0-1)

  // Enable or disable virtual background
  agoraEngine.EnableVirtualBackground(true, virtualBackgroundSource, segmentationProperty, MEDIA_SOURCE_TYPE.PRIMARY_CAMERA_SOURCE);
}

Set an image background

To set a custom image as the virtual background, specify the absolute path to the image file.

private void SetBackgroundImage()
{
  if(IsFeatureAvailable() == false)
  {
    Debug.Log("Device does not support the virtual background feature");
    return;
  }

  VirtualBackgroundSource virtualBackgroundSource = new VirtualBackgroundSource();

  virtualBackgroundSource.background_source_type = BACKGROUND_SOURCE_TYPE.BACKGROUND_IMG;
  string filePath = Path.Combine(Application.persistentDataPath, "<path-of-the-file>.png");
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
    filePath = filePath.Replace('/', '\\');
#endif
  virtualBackgroundSource.source = filePath;

  var segproperty = new SegmentationProperty();
  RtcEngine.EnableVirtualBackground(true, virtualBackgroundSource, segproperty, MEDIA_SOURCE_TYPE.PRIMARY_CAMERA_SOURCE);
}

Reset the background

Resets the virtual background, disabling any applied effects and reverting to the original video state.

private void DisableVirtualBackground()
{
  VirtualBackgroundSource virtualBackgroundSource = new VirtualBackgroundSource();
  var segproperty = new SegmentationProperty();
  RtcEngine.EnableVirtualBackground(false, virtualBackgroundSource, segproperty, MEDIA_SOURCE_TYPE.PRIMARY_CAMERA_SOURCE);
}

Reference

This section contains content that completes the information in this page, or points you to documentation that explains other aspects to this product.

API reference