import BackgroundFilterSpec from '../backgroundfilter/BackgroundFilterSpec' ; import BackgroundFilterVideoFrameProcessor from '../backgroundfilter/BackgroundFilterVideoFrameProcessor' ; import BackgroundReplacementProcessor from '../backgroundreplacementprocessor/BackgroundReplacementProcessor' ; import ConsoleLogger from '../logger/ConsoleLogger' ; import LogLevel from '../logger/LogLevel' ; import NoOpVideoFrameProcessor from '../videoframeprocessor/NoOpVideoFrameProcessor' ; import BackgroundReplacementFilter from './BackgroundReplacementFilter' ; import BackgroundReplacementOptions from './BackgroundReplacementOptions' ; /**
 * No-op implementation of the background replacement processor. An instance of this class will be returned when a user attempts
 * to create a background replacement processor when it is not supported.
 */ internal class NoOpBackgroundReplacementProcessor extends NoOpVideoFrameProcessor implements BackgroundReplacementProcessor { returns async loadAssets ( ) { return ; } /**
     * no-op
     */ addObserver ( ) { } /**
     * no-op
     */ removeObserver ( ) { } /**
     * no-op
     */ async setImageBlob ( ) { return ; } } /**
 * [[BackgroundReplacementVideoFrameProcessor]]
 * Creates a background replacement processor which identifies the foreground person and replaces the background.
 */ export default class BackgroundReplacementVideoFrameProcessor extends BackgroundFilterVideoFrameProcessor { param spec param imagePath static async create ( spec ? , options ? ) { spec = this . resolveSpec ( spec ) ; options = this . resolveOptions ( options ) ; await this . resolveOptionsAsync ( options ) ; const { logger } = options ; const supported = await BackgroundReplacementVideoFrameProcessor . isSupported ( spec , options ) ; if ( ! supported ) { logger . warn ( 'Using no-op processor because background replacement is not supported' ) ; return new NoOpBackgroundReplacementProcessor ( ) ; } logger . info ( 'Using background replacement filter' ) ; const processor = new BackgroundReplacementFilter ( spec , options ) ; await processor . loadAssets ( ) ; return processor ; } param options returns protected static resolveOptions ( options = { } ) { if ( ! options . logger ) { options . logger = new ConsoleLogger ( 'BackgroundReplacementProcessor' , LogLevel . INFO ) ; } return super . resolveOptions ( options ) ; } private static async resolveOptionsAsync ( options ) { if ( ! options . imageBlob ) { const canvas = document . createElement ( 'canvas' ) ; canvas . width = 100 ; canvas . height = 100 ; const ctx = canvas . getContext ( '2d' ) ; ctx . fillStyle = 'blue' ; ctx . fillRect ( 0 , 0 , 100 , 100 ) ; const blob = await new Promise < Blob > ( resolve => { canvas . toBlob ( resolve ) ; } ) ; options . imageBlob = blob ; } return ; } param spec The  BackgroundFilterSpec spec that will be used to initialize assets
     *  param options returns static async isSupported ( spec ? , options ? ) { spec = this . resolveSpec ( spec ) ; options = this . resolveOptions ( options ) ; await this . resolveOptionsAsync ( options ) ; const imageBlob = options . imageBlob ; const imageUrl = URL . createObjectURL ( imageBlob ) ; try { await BackgroundReplacementFilter . loadImage ( imageUrl ) ; } catch ( e ) { options . logger . info ( `Failed to fetch load replacement image ${ e . message }` ) ; return false ; } finally { URL . revokeObjectURL ( imageUrl ) ; } return super . isSupported ( spec , options ) ; } }