C# Class Accord.Video.AsyncVideoSource

Proxy video source for asynchronous processing of another nested video source.

The class represents a simple proxy, which wraps the specified NestedVideoSource with the aim of asynchronous processing of received video frames. The class intercepts NewFrame event from the nested video source and fires it to clients from its own thread, which is different from the thread used by nested video source for video acquisition. This allows clients to perform processing of video frames without blocking video acquisition thread, which continue to run and acquire next video frame while current is still processed.

For example, let’s suppose that it takes 100 ms for the nested video source to acquire single frame, so the original frame rate is 10 frames per second. Also let’s assume that we have an image processing routine, which also takes 100 ms to process a single frame. If the acquisition and processing are done sequentially, then resulting frame rate will drop to 5 frames per second. However, if doing both in parallel, then there is a good chance to keep resulting frame rate equal (or close) to the original frame rate.

The class provides a bonus side effect - easer debugging of image processing routines, which are put into NewFrame event handler. In many cases video source classes fire their IVideoSource.NewFrame event from a try/catch block, which makes it very hard to spot error made in user's code - the catch block simply hides exception raised in user’s code. The AsyncVideoSource does not have any try/catch blocks around firing of NewFrame event, so always user gets exception in the case it comes from his code. At the same time nested video source is not affected by the user's exception, since it runs in different thread.

Sample usage:

// usage of AsyncVideoSource is the same as usage of any // other video source class, so code change is very little // create nested video source, for example JPEGStream JPEGStream stream = new JPEGStream( "some url" ); // create async video source AsyncVideoSource asyncSource = new AsyncVideoSource( stream ); // set NewFrame event handler asyncSource.NewFrame += new NewFrameEventHandler( video_NewFrame ); // start the video source asyncSource.Start( ); // ... private void video_NewFrame( object sender, NewFrameEventArgs eventArgs ) { // get new frame Bitmap bitmap = eventArgs.Frame; // process the frame }
Inheritance: IVideoSource
Show file Open project: accord-net/framework

Public Methods

Method Description
AsyncVideoSource ( IVideoSource nestedVideoSource ) : System

Initializes a new instance of the AsyncVideoSource class.

AsyncVideoSource ( IVideoSource nestedVideoSource, bool skipFramesIfBusy ) : System

Initializes a new instance of the AsyncVideoSource class.

SignalToStop ( ) : void

Signal video source to stop its work.

Signals video source to stop its background thread, stop to provide new frames and free resources.

Start ( ) : void

Start video source.

Starts the nested video source and returns execution to caller. This object creates an extra thread which is used to fire NewFrame events, so the image processing could be done on another thread without blocking video acquisition thread.

Stop ( ) : void

Stop video source.

Stops nested video source by calling its IVideoSource.Stop method. See documentation of the particular video source for additional details.

WaitForStop ( ) : void

Wait for video source has stopped.

Waits for video source stopping after it was signalled to stop using SignalToStop method.

Private Methods

Method Description
CloneImage ( Bitmap source ) : Bitmap
CloneImage ( BitmapData sourceData ) : Bitmap
Free ( ) : void
imageProcessingThread_Worker ( ) : void
nestedVideoSource_NewFrame ( object sender, NewFrameEventArgs eventArgs ) : void

Method Details

AsyncVideoSource() public method

Initializes a new instance of the AsyncVideoSource class.
public AsyncVideoSource ( IVideoSource nestedVideoSource ) : System
nestedVideoSource IVideoSource Nested video source which is the target for asynchronous processing.
return System

AsyncVideoSource() public method

Initializes a new instance of the AsyncVideoSource class.
public AsyncVideoSource ( IVideoSource nestedVideoSource, bool skipFramesIfBusy ) : System
nestedVideoSource IVideoSource Nested video source which is the target for asynchronous processing.
skipFramesIfBusy bool Specifies if the object should skip frames from the nested video source /// in the case if it is still busy processing the previous video frame.
return System

SignalToStop() public method

Signal video source to stop its work.

Signals video source to stop its background thread, stop to provide new frames and free resources.

public SignalToStop ( ) : void
return void

Start() public method

Start video source.

Starts the nested video source and returns execution to caller. This object creates an extra thread which is used to fire NewFrame events, so the image processing could be done on another thread without blocking video acquisition thread.

public Start ( ) : void
return void

Stop() public method

Stop video source.

Stops nested video source by calling its IVideoSource.Stop method. See documentation of the particular video source for additional details.

public Stop ( ) : void
return void

WaitForStop() public method

Wait for video source has stopped.

Waits for video source stopping after it was signalled to stop using SignalToStop method.

public WaitForStop ( ) : void
return void