Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

add constructor to VideoCaptureDevice and Grabber #278

Merged
merged 1 commit into from Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 25 additions & 6 deletions Sources/Accord.Video.DirectShow/Internals/Uuids.cs
Expand Up @@ -8,12 +8,13 @@

namespace Accord.Video.DirectShow.Internals
{
using System;
using System.Runtime.InteropServices;

/// <summary>
/// DirectShow class IDs.
/// </summary>
using System;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

/// <summary>
/// DirectShow class IDs.
/// </summary>
[ComVisible( false )]
static internal class Clsid
{
Expand Down Expand Up @@ -256,6 +257,24 @@ static internal class MediaSubType
///
public static readonly Guid Asf =
new Guid( 0x3DB80F90, 0x9412, 0x11D1, 0xAD, 0xED, 0x00, 0x00, 0xF8, 0x75, 0x4B, 0x99 );

/// <summary>
/// Convert to MediaSubType from System.Drawing.Imaging.PixelFormat
/// </summary>
/// <param name="pixelFormat"></param>
/// <returns>MediaSubType value</returns>
public static Guid ConvertFrom(PixelFormat pixelFormat)
{
switch (pixelFormat)
{
case PixelFormat.Format24bppRgb:
return MediaSubType.RGB24;
case PixelFormat.Format32bppRgb:
return MediaSubType.RGB32;
default:
return MediaSubType.RGB24;
}
}
}

/// <summary>
Expand Down
44 changes: 31 additions & 13 deletions Sources/Accord.Video.DirectShow/VideoCaptureDevice.cs
Expand Up @@ -61,6 +61,9 @@ public class VideoCaptureDevice : IVideoSource
// recieved byte count
private long bytesReceived;

// pixel format
private PixelFormat pixelFormat = PixelFormat.Format24bppRgb;

// video and snapshot resolutions to set
private VideoCapabilities videoResolution = null;
private VideoCapabilities snapshotResolution = null;
Expand Down Expand Up @@ -547,6 +550,19 @@ public VideoCaptureDevice(string deviceMoniker)
this.deviceMoniker = deviceMoniker;
}

/// <summary>
/// Initializes a new instance of the <see cref="VideoCaptureDevice"/> class.
/// </summary>
///
/// <param name="deviceMoniker">Moniker string of video capture device.</param>
/// <param name="pixelFormat">Pixel format of video.</param>
///
public VideoCaptureDevice(string deviceMoniker, PixelFormat pixelFormat)
{
this.deviceMoniker = deviceMoniker;
this.pixelFormat = pixelFormat;
}

/// <summary>
/// Start video source.
/// </summary>
Expand Down Expand Up @@ -983,11 +999,11 @@ private void WorkerThread()
private void WorkerThread(bool runGraph)
{
ReasonToFinishPlaying reasonToStop = ReasonToFinishPlaying.StoppedByUser;
bool isSapshotSupported = false;
bool isSnapshotSupported = false;

// grabber
Grabber videoGrabber = new Grabber(this, false);
Grabber snapshotGrabber = new Grabber(this, true);
Grabber videoGrabber = new Grabber(this, false, this.pixelFormat);
Grabber snapshotGrabber = new Grabber(this, true, this.pixelFormat);

// objects
object captureGraphObject = null;
Expand Down Expand Up @@ -1073,7 +1089,7 @@ private void WorkerThread(bool runGraph)
// set media type
AMMediaType mediaType = new AMMediaType();
mediaType.MajorType = MediaType.Video;
mediaType.SubType = MediaSubType.RGB24;
mediaType.SubType = MediaSubType.ConvertFrom(pixelFormat);

videoSampleGrabber.SetMediaType(mediaType);
snapshotSampleGrabber.SetMediaType(mediaType);
Expand All @@ -1089,15 +1105,15 @@ private void WorkerThread(bool runGraph)

if (videoControl != null)
{
// find Still Image output pin of the vedio device
// find Still Image output pin of the video device
captureGraph.FindPin(sourceObject, PinDirection.Output,
PinCategory.StillImage, MediaType.Video, false, 0, out pinStillImage);
// check if it support trigger mode
if (pinStillImage != null)
{
VideoControlFlags caps;
videoControl.GetCaps(pinStillImage, out caps);
isSapshotSupported = ((caps & VideoControlFlags.ExternalTriggerEnable) != 0);
isSnapshotSupported = ((caps & VideoControlFlags.ExternalTriggerEnable) != 0);
}
}

Expand All @@ -1114,7 +1130,7 @@ private void WorkerThread(bool runGraph)
// configure pins
GetPinCapabilitiesAndConfigureSizeAndRate(captureGraph, sourceBase,
PinCategory.Capture, videoResolution, ref videoCapabilities);
if (isSapshotSupported)
if (isSnapshotSupported)
{
GetPinCapabilitiesAndConfigureSizeAndRate(captureGraph, sourceBase,
PinCategory.StillImage, snapshotResolution, ref snapshotCapabilities);
Expand Down Expand Up @@ -1155,7 +1171,7 @@ private void WorkerThread(bool runGraph)
mediaType.Dispose();
}

if ((isSapshotSupported) && (provideSnapshots))
if ((isSnapshotSupported) && (provideSnapshots))
{
// render snapshot pin
captureGraph.RenderStream(PinCategory.StillImage, MediaType.Video, sourceBase, null, snapshotGrabberBase);
Expand All @@ -1182,7 +1198,7 @@ private void WorkerThread(bool runGraph)
// run
mediaControl.Run();

if ((isSapshotSupported) && (provideSnapshots))
if ((isSnapshotSupported) && (provideSnapshots))
{
startTime = DateTime.Now;
videoControl.SetMode(pinStillImage, VideoControlFlags.ExternalTriggerEnable);
Expand Down Expand Up @@ -1219,7 +1235,7 @@ private void WorkerThread(bool runGraph)
{
needToSimulateTrigger = false;

if ((isSapshotSupported) && (provideSnapshots))
if ((isSnapshotSupported) && (provideSnapshots))
{
videoControl.SetMode(pinStillImage, VideoControlFlags.Trigger);
}
Expand Down Expand Up @@ -1618,6 +1634,7 @@ private class Grabber : ISampleGrabberCB
private VideoCaptureDevice parent;
private bool snapshotMode;
private int width, height;
private PixelFormat pixelFormat;

// Width property
public int Width
Expand All @@ -1633,10 +1650,11 @@ public int Height
}

// Constructor
public Grabber(VideoCaptureDevice parent, bool snapshotMode)
public Grabber(VideoCaptureDevice parent, bool snapshotMode, PixelFormat pixelFormat = PixelFormat.Format24bppRgb)
{
this.parent = parent;
this.snapshotMode = snapshotMode;
this.pixelFormat = pixelFormat;
}

// Callback to receive samples
Expand All @@ -1651,13 +1669,13 @@ public int BufferCB(double sampleTime, IntPtr buffer, int bufferLen)
if (parent.NewFrame != null)
{
// create new image
System.Drawing.Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);
System.Drawing.Bitmap image = new Bitmap(width, height, this.pixelFormat);

// lock bitmap data
BitmapData imageData = image.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
this.pixelFormat);

// copy image data
int srcStride = imageData.Stride;
Expand Down