C# Class Accord.Video.DirectShow.VideoCaptureDevice

Video source for local video capture device (for example USB webcam).

This video source class captures video data from local video capture device, like USB web camera (or internal), frame grabber, capture board - anything which supports DirectShow interface. For devices which has a shutter button or support external software triggering, the class also allows to do snapshots. Both video size and snapshot size can be configured.

Sample usage:

// enumerate video devices videoDevices = new FilterInfoCollection( FilterCategory.VideoInputDevice ); // create video source VideoCaptureDevice videoSource = new VideoCaptureDevice( videoDevices[0].MonikerString ); // set NewFrame event handler videoSource.NewFrame += new NewFrameEventHandler( video_NewFrame ); // start the video source videoSource.Start( ); // ... // signal to stop when you no longer need capturing videoSource.SignalToStop( ); // ... 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 Class Usage Examples

Private Properties

Property Type Description
ColletCrossbarVideoInputs Accord.Video.DirectShow.VideoInput[]
DisplayPropertyPage void
Free void
GetCurrentCrossbarInput VideoInput
GetPinCapabilitiesAndConfigureSizeAndRate void
OnNewFrame void
OnSnapshotFrame void
SetCurrentCrossbarInput void
SetResolution void
WorkerThread void
WorkerThread void

Public Methods

Method Description
CheckIfCrossbarAvailable ( ) : bool

Check if running video source provides crossbar for configuration.

The method reports if the video source provides crossbar configuration using DisplayCrossbarPropertyPage.

DisplayCrossbarPropertyPage ( IntPtr parentWindow ) : void

Display property page of video crossbar (Analog Video Crossbar filter).

The Analog Video Crossbar filter is modeled after a general switching matrix, with n inputs and m outputs. For example, a video card might have two external connectors: a coaxial connector for TV, and an S-video input. These would be represented as input pins on the filter. The displayed property page allows to configure the crossbar by selecting input of a video card to use.

This method can be invoked only when video source is running (IsRunning is ). Otherwise it generates exception.

Use CheckIfCrossbarAvailable method to check if running video source provides crossbar configuration.

DisplayPropertyPage ( IntPtr parentWindow ) : void

Display property window for the video capture device providing its configuration capabilities.

If you pass parent window's handle to this method, then the displayed property page will become modal window and none of the controls from the parent window will be accessible. In order to make it modeless it is required to pass IntPtr.Zero as parent window's handle.

GetCameraProperty ( CameraControlProperty property, int &value, CameraControlFlags &controlFlags ) : bool

Gets the current setting of a camera property.

GetCameraPropertyRange ( CameraControlProperty property, int &minValue, int &maxValue, int &stepSize, int &defaultValue, CameraControlFlags &controlFlags ) : bool

Gets the range and default value of a specified camera property.

SetCameraProperty ( CameraControlProperty property, int value, CameraControlFlags controlFlags ) : bool

Sets a specified property on the camera.

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.

SimulateTrigger ( ) : void

Simulates an external trigger.

The method simulates external trigger for video cameras, which support providing still image snapshots. The effect is equivalent as pressing camera's shutter button - a snapshot will be provided through SnapshotFrame event.

The ProvideSnapshots property must be set to to enable receiving snapshots.

Start ( ) : void

Start video source.

Starts video source and return execution to caller. Video source object creates background thread and notifies about new frames with the help of NewFrame event.

Stop ( ) : void

Stop video source.

Stops video source aborting its thread.

Since the method aborts background thread, its usage is highly not preferred and should be done only if there are no other options. The correct way of stopping camera is signaling it stop and then waiting for background thread's completion.

VideoCaptureDevice ( ) : System

Initializes a new instance of the VideoCaptureDevice class.

VideoCaptureDevice ( string deviceMoniker ) : System

Initializes a new instance of the VideoCaptureDevice class.

VideoCaptureDevice ( string deviceMoniker, PixelFormat pixelFormat ) : System

Initializes a new instance of the VideoCaptureDevice class.

WaitForStop ( ) : void

Wait for video source has stopped.

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

Private Methods

Method Description
ColletCrossbarVideoInputs ( IAMCrossbar crossbar ) : Accord.Video.DirectShow.VideoInput[]
DisplayPropertyPage ( IntPtr parentWindow, object sourceObject ) : void
Free ( ) : void

Free resource.

GetCurrentCrossbarInput ( IAMCrossbar crossbar ) : VideoInput
GetPinCapabilitiesAndConfigureSizeAndRate ( ICaptureGraphBuilder2 graphBuilder, IBaseFilter baseFilter, System.Guid pinCategory, VideoCapabilities resolutionToSet, VideoCapabilities &capabilities ) : void
OnNewFrame ( Bitmap image ) : void

Notifies clients about new frame.

OnSnapshotFrame ( Bitmap image ) : void

Notifies clients about new snapshot frame.

SetCurrentCrossbarInput ( IAMCrossbar crossbar, VideoInput videoInput ) : void
SetResolution ( IAMStreamConfig streamConfig, VideoCapabilities resolution ) : void
WorkerThread ( ) : void

Worker thread.

WorkerThread ( bool runGraph ) : void

Method Details

CheckIfCrossbarAvailable() public method

Check if running video source provides crossbar for configuration.

The method reports if the video source provides crossbar configuration using DisplayCrossbarPropertyPage.

public CheckIfCrossbarAvailable ( ) : bool
return bool

DisplayCrossbarPropertyPage() public method

Display property page of video crossbar (Analog Video Crossbar filter).

The Analog Video Crossbar filter is modeled after a general switching matrix, with n inputs and m outputs. For example, a video card might have two external connectors: a coaxial connector for TV, and an S-video input. These would be represented as input pins on the filter. The displayed property page allows to configure the crossbar by selecting input of a video card to use.

This method can be invoked only when video source is running (IsRunning is ). Otherwise it generates exception.

Use CheckIfCrossbarAvailable method to check if running video source provides crossbar configuration.

The video source must be running in order to display crossbar property page. Crossbar configuration is not supported by currently running video source.
public DisplayCrossbarPropertyPage ( IntPtr parentWindow ) : void
parentWindow System.IntPtr Handle of parent window.
return void

DisplayPropertyPage() public method

Display property window for the video capture device providing its configuration capabilities.

If you pass parent window's handle to this method, then the displayed property page will become modal window and none of the controls from the parent window will be accessible. In order to make it modeless it is required to pass IntPtr.Zero as parent window's handle.

The video source does not support configuration property page.
public DisplayPropertyPage ( IntPtr parentWindow ) : void
parentWindow System.IntPtr Handle of parent window.
return void

GetCameraProperty() public method

Gets the current setting of a camera property.
Video source is not specified - device moniker is not set. Failed creating device object for moniker. The video source does not support camera control.
public GetCameraProperty ( CameraControlProperty property, int &value, CameraControlFlags &controlFlags ) : bool
property CameraControlProperty Specifies the property to retrieve.
value int Receives the value of the property.
controlFlags CameraControlFlags Receives the value indicating whether the setting is controlled manually or automatically
return bool

GetCameraPropertyRange() public method

Gets the range and default value of a specified camera property.
Video source is not specified - device moniker is not set. Failed creating device object for moniker. The video source does not support camera control.
public GetCameraPropertyRange ( CameraControlProperty property, int &minValue, int &maxValue, int &stepSize, int &defaultValue, CameraControlFlags &controlFlags ) : bool
property CameraControlProperty Specifies the property to query.
minValue int Receives the minimum value of the property.
maxValue int Receives the maximum value of the property.
stepSize int Receives the step size for the property.
defaultValue int Receives the default value of the property.
controlFlags CameraControlFlags Receives a member of the enumeration, indicating whether the property is controlled automatically or manually.
return bool

SetCameraProperty() public method

Sets a specified property on the camera.
Video source is not specified - device moniker is not set. Failed creating device object for moniker. The video source does not support camera control.
public SetCameraProperty ( CameraControlProperty property, int value, CameraControlFlags controlFlags ) : bool
property CameraControlProperty Specifies the property to set.
value int Specifies the new value of the property.
controlFlags CameraControlFlags Specifies the desired control setting.
return bool

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

SimulateTrigger() public method

Simulates an external trigger.

The method simulates external trigger for video cameras, which support providing still image snapshots. The effect is equivalent as pressing camera's shutter button - a snapshot will be provided through SnapshotFrame event.

The ProvideSnapshots property must be set to to enable receiving snapshots.

public SimulateTrigger ( ) : void
return void

Start() public method

Start video source.
Starts video source and return execution to caller. Video source object creates background thread and notifies about new frames with the help of NewFrame event.
public Start ( ) : void
return void

Stop() public method

Stop video source.

Stops video source aborting its thread.

Since the method aborts background thread, its usage is highly not preferred and should be done only if there are no other options. The correct way of stopping camera is signaling it stop and then waiting for background thread's completion.

public Stop ( ) : void
return void

VideoCaptureDevice() public method

Initializes a new instance of the VideoCaptureDevice class.
public VideoCaptureDevice ( ) : System
return System

VideoCaptureDevice() public method

Initializes a new instance of the VideoCaptureDevice class.
public VideoCaptureDevice ( string deviceMoniker ) : System
deviceMoniker string Moniker string of video capture device.
return System

VideoCaptureDevice() public method

Initializes a new instance of the VideoCaptureDevice class.
public VideoCaptureDevice ( string deviceMoniker, PixelFormat pixelFormat ) : System
deviceMoniker string Moniker string of video capture device.
pixelFormat PixelFormat Pixel format of video.
return System

WaitForStop() public method

Wait for video source has stopped.
Waits for source stopping after it was signalled to stop using SignalToStop method.
public WaitForStop ( ) : void
return void