import { loadWorker } from '../../libs/voicefocus/loader' ; import { supportsWASM , supportsWorker } from '../../libs/voicefocus/support' ; import { AssetSpec } from '../../libs/voicefocus/voicefocus' ; import ModelSpecBuilder from '../backgroundblurprocessor/ModelSpecBuilder' ; import BackgroundFilterPaths from '../backgroundfilter/BackgroundFilterPaths' ; import DefaultBrowserBehavior from '../browserbehavior/DefaultBrowserBehavior' ; import Logger from '../logger/Logger' ; import ModelSpec from '../modelspec/ModelSpec' ; import Versioning from '../versioning/Versioning' ; import BackgroundFilterOptions from './BackgroundFilterOptions' ; import BackgroundFilterSpec from './BackgroundFilterSpec' ; internal const CREATE_DEFAULT_MODEL_SPEC = ( ) : ModelSpec => ModelSpecBuilder . builder ( ) . withSelfieSegmentationDefaults ( ) . build ( ) ; internal const DEFAULT_CDN = 'https://static.sdkassets.chime.aws' ; internal const DEFAULT_PATHS = { worker : `${ DEFAULT_CDN }/bgblur/workers/worker.js` , wasm : `${ DEFAULT_CDN }/bgblur/wasm/_cwt-wasm.wasm` , simd : `${ DEFAULT_CDN }/bgblur/wasm/_cwt-wasm-simd.wasm` , } ; export default class BackgroundFilterVideoFrameProcessor { returns private static defaultAssetSpec ( ) { const version = Versioning . sdkVersionSemVer ; return { assetGroup : `sdk-${ version . major }.${ version . minor }` , } ; } param url param queryParams returns private static createUrlWithParams ( url , queryParams ) { const u = new URL ( url ) ; const keys = Object . keys ( queryParams ) ; for ( const key of keys ) { if ( queryParams [ key ] !== undefined ) { u . searchParams . set ( key , queryParams [ key ] ) ; } } return u . toString ( ) ; } param spec returns protected static resolveSpec ( spec ? ) { const { paths = DEFAULT_PATHS , model = CREATE_DEFAULT_MODEL_SPEC ( ) , assetGroup = this . defaultAssetSpec ( ) . assetGroup , revisionID = this . defaultAssetSpec ( ) . revisionID , } = spec || { } ; const params = { assetGroup , revisionID , sdk : encodeURIComponent ( Versioning . sdkVersion ) , ua : encodeURIComponent ( Versioning . sdkUserAgentLowResolution ) , } ; paths . worker = this . createUrlWithParams ( paths . worker , params ) ; paths . wasm = this . createUrlWithParams ( paths . wasm , params ) ; paths . simd = this . createUrlWithParams ( paths . simd , params ) ; model . path = this . createUrlWithParams ( model . path , params ) ; return { paths , model , assetGroup , revisionID , } ; } param options returns protected static resolveOptions ( options ? ) { if ( ! options . reportingPeriodMillis ) { options . reportingPeriodMillis = 1000 ; } const DEFAULT_FILTER_CPU_UTILIZATION = 30 ; if ( ! options . filterCPUUtilization ) { options . filterCPUUtilization = DEFAULT_FILTER_CPU_UTILIZATION ; } else if ( options . filterCPUUtilization < 0 || options . filterCPUUtilization > 100 ) { options . logger . warn ( `filterCPUUtilization must be set to a range between 0 and 100 percent. Falling back to default of ${ DEFAULT_FILTER_CPU_UTILIZATION } percent` ) ; options . filterCPUUtilization = DEFAULT_FILTER_CPU_UTILIZATION ; } return options ; } param spec The  BackgroundBlurSpec spec that will be used to initialize assets
     *  param options returns static isSupported ( spec ? , options ? ) { const { logger } = options ; if ( typeof globalThis === 'undefined' ) { logger . info ( 'Browser does not have globalThis.' ) ; return Promise . resolve ( false ) ; } const browser = new DefaultBrowserBehavior ( ) ; if ( ! browser . supportsBackgroundFilter ( ) ) { logger . info ( 'Browser is not supported.' ) ; return Promise . resolve ( false ) ; } if ( ! supportsWASM ( globalThis , logger ) ) { logger . info ( 'Browser does not support WASM.' ) ; return Promise . resolve ( false ) ; } return this . supportsBackgroundFilter ( globalThis , spec , logger ) ; } private static async supportsBackgroundFilter ( scope = globalThis , spec ? , logger ? ) { if ( ! supportsWorker ( scope , logger ) ) { logger . info ( 'Browser does not support web workers.' ) ; return false ; } const workerURL = spec . paths . worker ; try { const worker = await loadWorker ( workerURL , 'BackgroundFilterWorker' , { } , null ) ; try { worker . terminate ( ) ; } catch ( e ) { logger . info ( `Failed to terminate worker. ${ e . message }` ) ; } return true ; } catch ( e ) { logger . info ( `Failed to fetch and instantiate test worker ${ e . message }` ) ; return false ; } } }