Assembly: AForge.Video (in AForge.Video.dll) Version: 2.2.5.0 (2.2.5.0)
Syntax
| C# |
|---|
public class AsyncVideoSource : IVideoSource |
Remarks
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 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 }