9#include <duvc-ctl/detail/com_helpers.h>
13#include <unordered_map>
19#ifndef __AMCAMERACONTROL__
20#define CameraControl_Pan 0L
21#define CameraControl_Tilt 1L
22#define CameraControl_Roll 2L
23#define CameraControl_Zoom 3L
24#define CameraControl_Exposure 4L
25#define CameraControl_Iris 5L
26#define CameraControl_Focus 6L
27#define CameraControl_ScanMode 7L
28#define CameraControl_Privacy 8L
29#define CameraControl_PanRelative 9L
30#define CameraControl_TiltRelative 10L
31#define CameraControl_RollRelative 11L
32#define CameraControl_ZoomRelative 12L
33#define CameraControl_ExposureRelative 13L
34#define CameraControl_IrisRelative 14L
35#define CameraControl_FocusRelative 15L
36#define CameraControl_PanTilt 16L
37#define CameraControl_PanTiltRelative 17L
38#define CameraControl_FocusSimple 18L
39#define CameraControl_DigitalZoom 19L
40#define CameraControl_DigitalZoomRelative 20L
41#define CameraControl_BacklightCompensation 21L
42#define CameraControl_Lamp 22L
43#define CameraControl_Flags_Auto 0x0001
44#define CameraControl_Flags_Manual 0x0002
47#ifndef __AMVIDEOPROCAMP__
48#define VideoProcAmp_Brightness 0
49#define VideoProcAmp_Contrast 1
50#define VideoProcAmp_Hue 2
51#define VideoProcAmp_Saturation 3
52#define VideoProcAmp_Sharpness 4
53#define VideoProcAmp_Gamma 5
54#define VideoProcAmp_ColorEnable 6
55#define VideoProcAmp_WhiteBalance 7
56#define VideoProcAmp_BacklightCompensation 8
57#define VideoProcAmp_Gain 9
58#define VideoProcAmp_Flags_Auto 0x0001
59#define VideoProcAmp_Flags_Manual 0x0002
63#pragma comment(lib, "ole32.lib")
64#pragma comment(lib, "oleaut32.lib")
65#pragma comment(lib, "strmiids.lib")
70using namespace detail;
77extern bool is_same_device(
const Device &d,
const std::wstring &name,
78 const std::wstring &path);
84 reinterpret_cast<void **
>(
f.put()));
93 reinterpret_cast<void **
>(
cam.put())))) {
102 reinterpret_cast<void **
>(
vp.put())))) {
115 throw std::runtime_error(
"No video devices available");
118 com_ptr<IMoniker> mon;
119 while (en->Next(1, mon.put(), &fetched) == S_OK && fetched) {
128 throw std::runtime_error(
"Device not found");
232 : com_(std::make_unique<com_apartment>()), filter_(nullptr),
233 cam_ctrl_(nullptr), vid_proc_(nullptr) {
242 filter_ =
new com_ptr<IBaseFilter>(std::move(filter));
243 cam_ctrl_ =
new com_ptr<IAMCameraControl>(std::move(cam_ctrl));
244 vid_proc_ =
new com_ptr<IAMVideoProcAmp>(std::move(vid_proc));
251DeviceConnection::~DeviceConnection() {
252 delete static_cast<com_ptr<IBaseFilter> *
>(filter_);
253 delete static_cast<com_ptr<IAMCameraControl> *
>(cam_ctrl_);
254 delete static_cast<com_ptr<IAMVideoProcAmp> *
>(vid_proc_);
257bool DeviceConnection::get(CamProp prop, PropSetting &val) {
258 auto *cam_ctrl =
static_cast<com_ptr<IAMCameraControl> *
>(cam_ctrl_);
259 if (!cam_ctrl || !*cam_ctrl)
266 long value = 0, flags = 0;
267 HRESULT hr = (*cam_ctrl)->Get(pid, &value, &flags);
271 val.value =
static_cast<int>(value);
276bool DeviceConnection::set(CamProp prop,
const PropSetting &val) {
277 auto *cam_ctrl =
static_cast<com_ptr<IAMCameraControl> *
>(cam_ctrl_);
278 if (!cam_ctrl || !*cam_ctrl)
285 long flags =
to_flag(val.mode,
true);
286 HRESULT hr = (*cam_ctrl)->Set(pid,
static_cast<long>(val.value), flags);
287 return SUCCEEDED(hr);
290bool DeviceConnection::get(VidProp prop, PropSetting &val) {
291 auto *vid_proc =
static_cast<com_ptr<IAMVideoProcAmp> *
>(vid_proc_);
292 if (!vid_proc || !*vid_proc)
299 long value = 0, flags = 0;
300 HRESULT hr = (*vid_proc)->Get(pid, &value, &flags);
304 val.value =
static_cast<int>(value);
309bool DeviceConnection::set(VidProp prop,
const PropSetting &val) {
310 auto *vid_proc =
static_cast<com_ptr<IAMVideoProcAmp> *
>(vid_proc_);
311 if (!vid_proc || !*vid_proc)
318 long flags =
to_flag(val.mode,
false);
319 HRESULT hr = (*vid_proc)->Set(pid,
static_cast<long>(val.value), flags);
320 return SUCCEEDED(hr);
323bool DeviceConnection::get_range(CamProp prop, PropRange &range) {
324 auto *cam_ctrl =
static_cast<com_ptr<IAMCameraControl> *
>(cam_ctrl_);
325 if (!cam_ctrl || !*cam_ctrl)
332 long min = 0, max = 0, step = 0, def = 0, flags = 0;
333 HRESULT hr = (*cam_ctrl)->GetRange(pid, &min, &max, &step, &def, &flags);
337 range.min =
static_cast<int>(min);
338 range.max =
static_cast<int>(max);
339 range.step =
static_cast<int>(step);
340 range.default_val =
static_cast<int>(def);
341 range.default_mode =
from_flag(flags,
true);
345bool DeviceConnection::get_range(VidProp prop, PropRange &range) {
346 auto *vid_proc =
static_cast<com_ptr<IAMVideoProcAmp> *
>(vid_proc_);
347 if (!vid_proc || !*vid_proc)
354 long min = 0, max = 0, step = 0, def = 0, flags = 0;
355 HRESULT hr = (*vid_proc)->GetRange(pid, &min, &max, &step, &def, &flags);
359 range.min =
static_cast<int>(min);
360 range.max =
static_cast<int>(max);
361 range.step =
static_cast<int>(step);
362 range.default_val =
static_cast<int>(def);
363 range.default_mode =
from_flag(flags,
false);
DeviceConnection(const Device &dev)
Result type that can contain either a value or an error.
Windows-specific device connection pooling.
#define VideoProcAmp_Contrast
#define CameraControl_PanRelative
#define CameraControl_Roll
#define VideoProcAmp_ColorEnable
#define CameraControl_DigitalZoom
#define CameraControl_Tilt
#define CameraControl_Zoom
#define VideoProcAmp_Sharpness
#define CameraControl_Focus
#define CameraControl_IrisRelative
#define CameraControl_Lamp
#define CameraControl_FocusSimple
#define CameraControl_TiltRelative
#define CameraControl_ExposureRelative
#define CameraControl_PanTiltRelative
#define VideoProcAmp_WhiteBalance
#define CameraControl_ZoomRelative
#define VideoProcAmp_Gamma
#define CameraControl_ScanMode
#define CameraControl_Pan
#define CameraControl_Privacy
#define CameraControl_Exposure
#define CameraControl_Flags_Manual
#define CameraControl_DigitalZoomRelative
#define CameraControl_Iris
#define VideoProcAmp_BacklightCompensation
#define CameraControl_PanTilt
#define CameraControl_RollRelative
#define CameraControl_BacklightCompensation
#define CameraControl_Flags_Auto
#define CameraControl_FocusRelative
#define VideoProcAmp_Flags_Manual
#define VideoProcAmp_Brightness
#define VideoProcAmp_Flags_Auto
#define VideoProcAmp_Saturation
#define VideoProcAmp_Gain
Device enumeration and management functions.
std::wstring read_friendly_name(IMoniker *mon)
Read friendly name from device moniker.
static com_ptr< IAMVideoProcAmp > get_vproc(IBaseFilter *f)
VidProp
Video processing properties (IAMVideoProcAmp interface)
@ Saturation
Color saturation level.
@ WhiteBalance
White balance adjustment.
@ Brightness
Image brightness level.
@ ColorEnable
Color vs. monochrome mode.
@ Sharpness
Image sharpness level.
@ Contrast
Image contrast level.
@ Gamma
Gamma correction value.
@ BacklightCompensation
Backlight compensation level.
@ Hue
Color hue adjustment.
com_ptr< IBaseFilter > open_device_filter(const Device &dev)
Create DirectShow filter from device.
bool is_same_device(const Device &d, const std::wstring &name, const std::wstring &path)
Check if two device identifiers refer to same device.
com_ptr< IEnumMoniker > enum_video_devices(ICreateDevEnum *dev)
Enumerate video input devices.
static void throw_hr(HRESULT hr, const char *where)
std::wstring read_device_path(IMoniker *mon)
Read device path from moniker.
CamMode
Property control mode.
@ Auto
Automatic control by camera.
@ Manual
Manual control by application.
static com_ptr< IAMCameraControl > get_cam_ctrl(IBaseFilter *f)
static com_ptr< IBaseFilter > bind_to_filter(IMoniker *mon)
CamProp
Camera control properties (IAMCameraControl interface)
@ ExposureRelative
Relative exposure adjustment.
@ Roll
Camera roll rotation.
@ FocusSimple
Simple focus control.
@ PanRelative
Relative pan movement.
@ RollRelative
Relative roll movement.
@ Zoom
Optical zoom level.
@ Lamp
Camera lamp/flash control.
@ PanTiltRelative
Relative pan/tilt movement.
@ Tilt
Vertical camera rotation.
@ TiltRelative
Relative tilt movement.
@ ScanMode
Scan mode (progressive/interlaced)
@ ZoomRelative
Relative zoom movement.
@ Privacy
Privacy mode on/off.
@ IrisRelative
Relative iris adjustment.
@ Iris
Aperture/iris setting.
@ FocusRelative
Relative focus adjustment.
@ PanTilt
Combined pan/tilt control.
@ DigitalZoomRelative
Relative digital zoom.
@ Pan
Horizontal camera rotation.
@ BacklightCompensation
Backlight compensation.
@ DigitalZoom
Digital zoom level.
static long to_flag(CamMode m, bool is_camera_control)
com_ptr< ICreateDevEnum > create_dev_enum()
Create DirectShow device enumerator.
static long camprop_to_dshow(CamProp p)
static CamMode from_flag(long flag, bool is_camera_control)
static long vidprop_to_dshow(VidProp p)