async function initializeBackgroundReplacement()

in src/providers/BackgroundReplacementProvider/index.tsx [85:114]


  async function initializeBackgroundReplacement(): Promise<BackgroundReplacementProcessor | undefined> {
    console.log('Initializing background replacement processor with ', spec, options);

    try {
      const createdProcessor = await BackgroundReplacementVideoFrameProcessor.create(
        spec,
        options
      );
      // 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) {
        console.warn(`Initialized NoOpVideoFrameProcessor.`);
        setProcessor(undefined);
        setIsBackgroundReplacementSupported(false);
        return undefined;
      } else {
        console.log(`Initialized background replacement processor: ${JSON.stringify(createdProcessor)}`);
        setProcessor(createdProcessor);
        setIsBackgroundReplacementSupported(true);
        return createdProcessor;
      }
    } catch (error) {
      console.error(`Error creating a background replacement video frame processor device.`, error);
      setProcessor(undefined);
      setIsBackgroundReplacementSupported(false);
      return undefined;
    }
  }