Skip to content

Engine

Core generic engine

Engine is a core of any app and organizer of a pipeline. It’s responsible to interact with lower-level instances and at the same time provide simple and user-friendly interface. Engine combines together data (video) capturing, processing and rendering. It’s created for particular Processor. Processor’s constructor or instance is provided to the engine and former initializes and controls state of processor during life-circle of the application. Results of processing and captured image are passed to a Renderer attached to the Engine. Notable feature is fast rescaling of images for processing down to the requested resolution in cases when available camera output is bigger. Engine parameters have an option to limit processed image size. Original video stream can be preserved on request, this is useful when processing cannot handle high resolution images but you still want to render high quality video in your app. All core components: Engine, Processor, and Renderer are generics parametrized by type of processing results and optionally tuning parameters.

ResultT extends object

Type of processing results

ParamsT extends ProcParams

Type of processor parameters

ProcessorT extends Processor<ResultT, ParamsT>

Type of processor

new Engine<ResultT, ParamsT, ProcessorT>(Processor, engineParams?, Source?): Engine<ResultT, ParamsT, ProcessorT>

Constructor

Processor class or instance

ProcessorT | () => ProcessorT

EngineParams

Parameters of the engine

Video source class or instance

VideoSource | () => VideoSource

Engine<ResultT, ParamsT, ProcessorT>

EventEmitterT.constructor

protected optional engineParams: EngineParams

Parameters of the engine


protected optional processCanvas: HTMLCanvasElement

Shallow copy of canvas with video for processors


protected processor: ProcessorT

Processor utilized by the engine


protected processSize: Size

Size of video for processors


protected renderers: Renderer<ResultT>[] = []

Renderer attached to the engine


protected optional resizeBuffer: ImageBuffer

Buffer to resize frames


protected resizeEnabled: boolean = false

Original stream is resized


protected optional streamCanvas: HTMLCanvasElement

Shallow copy of canvas with video for renderers


protected streamSize: Size

Size of video for renderers


protected video: VideoSource

Video source instance


protected videoRatio: number

Ratio of video stream

addListener<E>(event, listener): this

Adds the listener function to the end of the listeners array

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.addListener


addRenderer(renderer): Promise<void>

Attach Renderer to the engine

Renderer<ResultT>

Object to be attached

Promise<void>


emit<E>(event, …args): boolean

Synchronously calls each of the listeners registered for the event

E extends keyof EngineEvents

E

The name of the event

Args<EngineEvents[E]>

Arguments passed to the listeners

boolean

True if the event had listeners, False otherwise

EventEmitterT.emit


protected enqueue(): void

Enqueue the next iteration

void


eventNames(): (string | symbol)[]

List of emitter’s events

(string | symbol)[]

List of emitter’s events

EventEmitterT.eventNames


getMaxListeners(): number

Maximum number of listeners per event

number

Maximum number of listeners per event

EventEmitterT.getMaxListeners


init(procParams): Promise<boolean>

Initialize engine. Sets up processor.

The SDK access token is required parameter that authenticates the user and enables the SDK on the current url. By default, path to required wasm modules provided with SDK packages is the current url. You can change the root path to wasms passing root parameter.

ParamsT

Parameters of the processor

Promise<boolean>

Status of initialization


protected iterate(): Promise<void>

Iterate

Promise<void>


listenerCount<E>(event): number

The number of listeners listening to the event

E extends keyof EngineEvents

E

The name of the event

number

Number of listeners

EventEmitterT.listenerCount


listeners<E>(event): Function[]

Copy of the array of listeners for the event

E extends keyof EngineEvents

E

The name of the event

Function[]

Copy of the listeners array

EventEmitterT.listeners


off<E>(event, listener): this

Removes the listener from the listener array for the event

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.off


on<E>(event, listener): this

Adds the listener function to the event

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.on


once<E>(event, listener): this

Adds a one-time listener function for the event

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.once


pause(): void

Pause pipeline.

Nothing happens if pipeline is not started yet.

void


prependListener<E>(event, listener): this

Adds the listener function to the beginning of the listeners array

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.prependListener


prependOnceListener<E>(event, listener): this

Adds a one-time listener function to the beginning of the listeners array

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

EventEmitterT.prependOnceListener


rawListeners<E>(event): Function[]

Copy of the array of listeners for the event including wrappers

E extends keyof EngineEvents

E

The name of the event

Function[]

Copy of the listeners array

EventEmitterT.rawListeners


removeAllListeners<E>(event?): this

Removes all listeners, or those of the specified event

E extends keyof EngineEvents

E

The name of the event

this

This EventEmitter

EventEmitterT.removeAllListeners


removeListener<E>(event, listener): this

Removes the specified listener from the listener array

E extends keyof EngineEvents

E

The name of the event

EngineEvents[E]

The callback function

this

This EventEmitter

EventEmitterT.removeListener


removeRenderer(renderer): void

Remove attached Renderer

Renderer<ResultT>

Renderer to be removed

void


reset(): void

Reset pipeline

Stops pipeline, resets video capture and processor. After reset one needs to reinitialize video capture calling setup() before pipeline can be started again.

void


protected resizeVideo(size): void

Callback called when video resolution is changed

Size

Size of the video

void


setMaxListeners(n): this

Sets maximum number of listeners per event

number

Maximum number of listeners

this

This EventEmitter

EventEmitterT.setMaxListeners


setup(videoParams?): Promise<boolean>

Setup engine. Initializes video capture.

Video capture can be setup by simplified VideoParams opening default front/rear camera with provided resolution. Additional fine-grained MediaStreamConstraints can be passed via opts field providing the most flexible way to setup video stream (for example, by requesting specific deviceId). Stream constraints have higher priority than rest or params. Another options are an external MediaStream allowing custom video sources (e.g. remote stream) or a media file defined by url or FileParams that allows to limit maximum capture size in cases resolution is too big for a device.

VideoSourceParams

Parameters of video capture

Promise<boolean>

Status of initialization


protected setupProcessor(procParams): Promise<boolean>

Setup processor

ParamsT

Promise<boolean>


protected setupSize(size): Promise<void>

Setup video size

Size

Promise<void>


protected setupVideo(videoParams?): Promise<boolean>

Setup video capture

VideoSourceParams

Promise<boolean>


start(): Promise<void>

Start pipeline.

Pipeline can be started only after successful init and setup.

Promise<void>