async function initializeBackgroundReplacement()

in src/providers/BackgroundReplacementProvider/index.tsx [107:149]


  async function initializeBackgroundReplacement(): Promise<
    BackgroundReplacementProcessor | undefined
  > {
    logger.info(
      `Initializing background replacement processor with, ${JSON.stringify(
        spec
      )}, options: { filterCPUUtilization: ${
        options?.filterCPUUtilization
      }, reportingPeriodMillis: ${
        options?.reportingPeriodMillis
      }}`
    );

    try {
      const createdProcessor =
        await BackgroundReplacementVideoFrameProcessor.create(
          { ...spec } as BackgroundFilterSpec,
          { ...options } as BackgroundReplacementOptions
        );
      // BackgroundReplacementVideoFrameProcessor.create will return a NoOpVideoFrameProcessor
      // in the case that BackgroundReplacementVideoFrameProcessor.isSupported() returns false.
      // BackgroundReplacementVideoFrameProcessor.create() can also throw an error in case loading
      // the assets are not fetched successfully.
      if (createdProcessor instanceof NoOpVideoFrameProcessor) {
        logger.warn('Initialized NoOpVideoFrameProcessor');
        setBackgroundReplacementProcessor(undefined);
        setIsBackgroundReplacementSupported(false);
        return undefined;
      } else {
        logger.info('Initialized background replacement processor');
        setBackgroundReplacementProcessor(createdProcessor);
        setIsBackgroundReplacementSupported(true);
        return createdProcessor;
      }
    } catch (error) {
      logger.error(
        `Error creating a background replacement video frame processor device. ${error}`
      );
      setBackgroundReplacementProcessor(undefined);
      setIsBackgroundReplacementSupported(false);
      return undefined;
    }
  }