async function toggleBackgroundReplacement()

in apps/meeting/src/components/MeetingControls/VideoInputTransformControl.tsx [112:146]


  async function toggleBackgroundReplacement() {
    let current = activeVideoDevice;
    if (isLoading) {
      return;
    }
    try {
      setIsLoading(true);
      if (!isVideoTransformDevice(current)) {
        // Enable video transform on the non-transformed device.
        current = await createBackgroundReplacementDevice(current) as VideoTransformDevice;
        meetingManager.logger?.info('Video filter turned on - selecting video transform device: ' + JSON.stringify(current));
      } else {
        // Switch back to intrinsicDevic.
        const intrinsicDevice = await current.intrinsicDevice();
        // Stop existing VideoTransformDevice.
        await current.stop();
        current = intrinsicDevice;
        // Switch to background replacement device if old selection was background blur otherwise switch to default intrinsic device.
        if (activeVideoTransformOption === VideoTransformOptions.Blur) {
          current = await createBackgroundReplacementDevice(current) as VideoTransformDevice;
          meetingManager.logger?.info('Video filter turned on - selecting video transform device: ' + JSON.stringify(current));
        } else {
          meetingManager.logger?.info('Video filter was turned off - selecting inner device: ' + JSON.stringify(current));
        }
      }
      // Use the new created video device as input.
      await meetingManager.selectVideoInputDevice(current);
      // Update the current selected transform.
      setActiveVideoTransformOption(activeVideoTransformOption => activeVideoTransformOption === VideoTransformOptions.Replacement ? VideoTransformOptions.None : VideoTransformOptions.Replacement);
    } catch (e) {
      console.error('Error trying to toggle background replacement', e);
    } finally {
      setIsLoading(false);
    }
  }