Click or drag to resize
Accord.NET (logo)

AsyncVideoSource Class

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

Namespace:  Accord.Video
Assembly:  Accord.Video (in Accord.Video.dll) Version: 3.8.0
public class AsyncVideoSource : IVideoSource
Request Example View Source

The AsyncVideoSource type exposes the following members.

Public methodAsyncVideoSource(IVideoSource)
Initializes a new instance of the AsyncVideoSource class.
Public methodAsyncVideoSource(IVideoSource, Boolean)
Initializes a new instance of the AsyncVideoSource class.
Public propertyBytesReceived
Received bytes count.
Public propertyFramesProcessed
Processed frames count.
Public propertyFramesReceived
Received frames count.
Public propertyIsRunning
State of the video source.
Public propertyNestedVideoSource
Nested video source which is the target for asynchronous processing.
Public propertySkipFramesIfBusy
Specifies if the object should skip frames from the nested video source when it is busy.
Public propertySource
Video source string.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodSignalToStop
Signal video source to stop its work.
Public methodStart
Start video source.
Public methodStop
Stop video source.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodWaitForStop
Wait for video source has stopped.
Public eventNewFrame
New frame event.
Public eventPlayingFinished
Video playing finished event.
Public eventVideoSourceError
Video source error event.
Extension Methods
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)

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
See Also