Skip to content

Instantly share code, notes, and snippets.

@MZachmann
Last active October 27, 2023 09:54
Show Gist options
  • Save MZachmann/557bf6663ce4806dfa85f1cd348027b4 to your computer and use it in GitHub Desktop.
Save MZachmann/557bf6663ce4806dfa85f1cd348027b4 to your computer and use it in GitHub Desktop.
/// <summary>
/// DirectShow definitions pulled from the headers
/// </summary>
using System;
namespace MFModels
{
// See CppSDK\SDK\include\shared\ksmedia.h
// typedef enum {
// KSPROPERTY_CAMERACONTROL_PAN, // RW O
// KSPROPERTY_CAMERACONTROL_TILT, // RW O
// KSPROPERTY_CAMERACONTROL_ROLL, // RW O
// KSPROPERTY_CAMERACONTROL_ZOOM, // RW O
// KSPROPERTY_CAMERACONTROL_EXPOSURE, // RW O
// KSPROPERTY_CAMERACONTROL_IRIS, // RW O
// KSPROPERTY_CAMERACONTROL_FOCUS // RW O
// KSPROPERTY_VIDCAP_CAMERACONTROL;
/// <summary>
/// The list of camera property settings
/// </summary>
public enum CameraControlProperty
{
Pan = 0,
Tilt,
Roll,
Zoom,
Exposure,
Iris,
Focus
}
/// <summary>
/// Is the setting automatic?
/// </summary>
[Flags]
public enum CameraControlFlags
{
None = 0x0,
Auto = 0x0001,
Manual = 0x0002
}
}
using MediaFoundation;
using System;
using System.Runtime.InteropServices;
// see: https://msdn.microsoft.com/en-us/library/windows/desktop/dd389145(v=vs.85).aspx
namespace MFModels
{
/// <summary>
/// The IAMCameraControl interface controls web camera settings such as zoom, pan, aperture adjustment,
/// or shutter speed. To obtain this interface, cast a MediaSource.
/// </summary>
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
Guid( "C6E13370-30AC-11d0-A18C-00A0C9118956" ),
InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
public interface IAMCameraControl
{
/// <summary>
/// Get the range and default value of a specified camera property.
/// </summary>
///
/// <param name="Property">The property to query.</param>
/// <param name="pMin">The minimum value of the property.</param>
/// <param name="pMax">The maximum value of the property.</param>
/// <param name="pSteppingDelta">The step size for the property.</param>
/// <param name="pDefault">The default value of the property. </param>
/// <param name="pCapsFlags">Can it be controlled automatically or manually?</param>
///
/// <returns>Error code.</returns>
///
[PreserveSig]
HResult GetRange(
[In] CameraControlProperty Property,
[Out] out int pMin,
[Out] out int pMax,
[Out] out int pSteppingDelta,
[Out] out int pDefault,
[Out] out CameraControlFlags pCapsFlags
);
/// <summary>
/// Set a specified property on the camera.
/// </summary>
///
/// <param name="Property">The property to set.</param>
/// <param name="lValue">The new value of the property.</param>
/// <param name="Flags">Control it manually or automatically.</param>
///
/// <returns>Error code.</returns>
///
[PreserveSig]
HResult Set(
[In] CameraControlProperty Property,
[In] int lValue,
[In] CameraControlFlags Flags
);
/// <summary>
/// Get the current setting of a camera property.
/// </summary>
///
/// <param name="Property">The property to retrieve.</param>
/// <param name="lValue">The current value of the property.</param>
/// <param name="Flags">Is it currently manual or automatic?.</param>
///
/// <returns>Error code.</returns>
///
[PreserveSig]
HResult Get(
[In] CameraControlProperty Property,
[Out] out int lValue,
[Out] out CameraControlFlags Flags
);
}
}
using MediaFoundation;
using System;
using System.Runtime.InteropServices;
// see https://msdn.microsoft.com/en-us/library/windows/desktop/dd376033(v=vs.85).aspx
namespace MFModels
{
/// <summary>
/// The IAMVideoProcAmp interface controls video camera settings such as brightness, contrast, hue,
/// or saturation. To obtain this interface, cast the MediaSource.
/// </summary>
[ComImport, System.Security.SuppressUnmanagedCodeSecurity,
Guid("C6E13360-30AC-11D0-A18C-00A0C9118956"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAMVideoProcAmp
{
/// <summary>
/// Get the range and default value of a camera property.
/// </summary>
///
/// <param name="Property">The property.</param>
/// <param name="pMin">The min value.</param>
/// <param name="pMax">The max value.</param>
/// <param name="pSteppingDelta">The step size.</param>
/// <param name="pDefault">The deafult value. </param>
/// <param name="pCapsFlags">Shows if it can be controlled automatically and/or manually.</param>
///
/// <returns>Error code.</returns>
///
[PreserveSig]
HResult GetRange(
[In] VideoProcAmpProperty Property,
[Out] out int pMin,
[Out] out int pMax,
[Out] out int pSteppingDelta,
[Out] out int pDefault,
[Out] out VideoProcAmpFlags pCapsFlags
);
/// <summary>
/// Set a specified property on the camera.
/// </summary>
///
/// <param name="Property">The property to set.</param>
/// <param name="lValue">The new value of the property.</param>
/// <param name="Flags">The auto or manual setting.</param>
///
/// <returns>Error code.</returns>
///
[PreserveSig]
HResult Set(
[In] VideoProcAmpProperty Property,
[In] int lValue,
[In] VideoProcAmpFlags Flags
);
/// <summary>
/// Get the current setting of a camera property.
/// </summary>
///
/// <param name="Property">The property to retrieve.</param>
/// <param name="lValue">The current value of the property.</param>
/// <param name="Flags">Is it manual or automatic?</param>
///
/// <returns>Error code.</returns>
///
[PreserveSig]
HResult Get(
[In] VideoProcAmpProperty Property,
[Out] out int lValue,
[Out] out VideoProcAmpFlags Flags
);
}
}
IMFMediaSource imfSource = ppv as IMFMediaSource;
if (imfSource != null)
{
try
{
// control the 'camera'
var imc = imfSource as IAMCameraControl;
int rMin, rMax, rDelta, rDeflt;
CameraControlFlags cflag;
var rslt = imc.GetRange(CameraControlProperty.Exposure, out rMin, out rMax, out rDelta, out rDeflt, out cflag);
// mdDevice.CameraController = imc;
// control the 'video'
var imu = imfSource as IAMVideoProcAmp;
VideoProcAmpFlags vflag;
rslt = imu.GetRange(VideoProcAmpProperty.Contrast, out rMin, out rMax, out rDelta, out rDeflt, out vflag);
// mdDevice.VideoController = imu;
}
catch (Exception ex)
{
Utility.Logger.Error("Failed to create: " + ex.Message);
}
}
using System;
// See CppSDK\SDK\include\shared\ksmedia.h
// typedef enum {
// KSPROPERTY_VIDEOPROCAMP_BRIGHTNESS, // RW O
// KSPROPERTY_VIDEOPROCAMP_CONTRAST, // RW O
// KSPROPERTY_VIDEOPROCAMP_HUE, // RW O
// KSPROPERTY_VIDEOPROCAMP_SATURATION, // RW O
// KSPROPERTY_VIDEOPROCAMP_SHARPNESS, // RW O
// KSPROPERTY_VIDEOPROCAMP_GAMMA, // RW O
// KSPROPERTY_VIDEOPROCAMP_COLORENABLE, // RW O
// KSPROPERTY_VIDEOPROCAMP_WHITEBALANCE, // RW O
// KSPROPERTY_VIDEOPROCAMP_BACKLIGHT_COMPENSATION // RW O
// KSPROPERTY_VIDEOPROCAMP_GAIN // RW O
// }
// KSPROPERTY_VIDCAP_VIDEOPROCAMP;
namespace MFModels
{
/// <summary>
/// The list of video camera settings
/// </summary>
public enum VideoProcAmpProperty
{
Brightness = 0,
Contrast,
Hue,
Saturation,
Sharpness,
Gamma,
ColorEnable,
WhiteBalance,
BacklightCompensation,
Gain
}
/// <summary>
/// The auto and manual flag
/// </summary>
[Flags]
public enum VideoProcAmpFlags
{
None = 0x0,
Auto = 0x0001,
Manual = 0x0002
}
}
@MZachmann
Copy link
Author

These four files allow for using MediaFoundation with C# and getting camera control of exposure, brightness, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment