duvc-ctl 2.0.0
USB Video Class Camera Control Library
Loading...
Searching...
No Matches
camera.cpp
Go to the documentation of this file.
1
9
10namespace duvc {
11
12Camera::Camera(const Device &device) : device_(device), connection_(nullptr) {}
13
14Camera::Camera(int device_index) : connection_(nullptr) {
15 auto devices = list_devices();
16 if (device_index >= 0 && device_index < static_cast<int>(devices.size())) {
17 device_ = devices[device_index];
18 }
19 // Invalid index results in invalid camera (device_ will be empty)
20}
21
22Camera::Camera(const std::wstring &device_path) : connection_(nullptr) {
24
25 // Validate device was found and has valid identifiers
26 if (!device_.is_valid()) {
27 throw std::runtime_error(
28 "Device found by path but failed validation");
29 }
30}
31
32Camera::~Camera() = default;
33
36
37bool Camera::is_valid() const {
38 return device_.is_valid() && is_device_connected(device_);
39}
40
41DeviceConnection *Camera::get_connection() const {
42 if (!connection_) {
43 connection_ = std::make_unique<DeviceConnection>(device_);
44 }
45 return connection_.get();
46}
47
49 auto *conn = get_connection();
50 if (!conn || !conn->is_valid()) {
51 return Err<PropSetting>(ErrorCode::DeviceNotFound, "Device not connected");
52 }
53
55 if (conn->get(prop, setting)) {
56 return Ok(setting);
57 }
59 "Failed to get camera property");
60}
61
63 auto *conn = get_connection();
64 if (!conn || !conn->is_valid()) {
65 return Err<void>(ErrorCode::DeviceNotFound, "Device not connected");
66 }
67
68 if (conn->set(prop, setting)) {
69 return Ok();
70 }
72 "Failed to set camera property");
73}
74
76 auto *conn = get_connection();
77 if (!conn || !conn->is_valid()) {
78 return Err<PropRange>(ErrorCode::DeviceNotFound, "Device not connected");
79 }
80
81 PropRange range;
82 if (conn->get_range(prop, range)) {
83 return Ok(range);
84 }
86 "Failed to get camera property range");
87}
88
90 auto *conn = get_connection();
91 if (!conn || !conn->is_valid()) {
92 return Err<PropSetting>(ErrorCode::DeviceNotFound, "Device not connected");
93 }
94
96 if (conn->get(prop, setting)) {
97 return Ok(setting);
98 }
100 "Failed to get video property");
101}
102
104 auto *conn = get_connection();
105 if (!conn || !conn->is_valid()) {
106 return Err<void>(ErrorCode::DeviceNotFound, "Device not connected");
107 }
108
109 if (conn->set(prop, setting)) {
110 return Ok();
111 }
113 "Failed to set video property");
114}
115
117 auto *conn = get_connection();
118 if (!conn || !conn->is_valid()) {
119 return Err<PropRange>(ErrorCode::DeviceNotFound, "Device not connected");
120 }
121
122 PropRange range;
123 if (conn->get_range(prop, range)) {
124 return Ok(range);
125 }
127 "Failed to get video property range");
128}
129
131 auto devices = list_devices();
132 if (device_index < 0 || device_index >= static_cast<int>(devices.size())) {
133 return Err<Camera>(ErrorCode::DeviceNotFound, "Invalid device index");
134 }
135
137}
138
140 if (!device.is_valid()) {
141 return Err<Camera>(ErrorCode::InvalidArgument, "Invalid device");
142 }
143
144 if (!is_device_connected(device)) {
145 return Err<Camera>(ErrorCode::DeviceNotFound, "Device not connected");
146 }
147
148 return Ok(Camera(device));
149}
150
152 try {
154
155 if (!device.is_valid()) {
157 "Found device has invalid identifiers");
158 }
159
160 if (!is_device_connected(device)) {
162 "Device not connected");
163 }
164
165 return Ok(Camera(device));
166
167 } catch (const std::exception &e) {
169 std::string("Failed to open camera by path: ") + e.what());
170 }
171}
172
173} // namespace duvc
RAII camera handle for simplified device management.
RAII camera handle for simplified device management.
Definition camera.h:23
~Camera()
Destructor - automatically releases device connection.
Camera(const Device &device)
Create camera handle for device.
Definition camera.cpp:12
Result< PropRange > get_range(CamProp prop)
Get camera property range.
Definition camera.cpp:75
bool is_valid() const
Check if camera is valid and connected.
Definition camera.cpp:37
Result< void > set(CamProp prop, const PropSetting &setting)
Set camera property value.
Definition camera.cpp:62
Result< PropSetting > get(CamProp prop)
Get camera property value.
Definition camera.cpp:48
RAII wrapper for DirectShow device connections.
Definition core.h:55
Result type that can contain either a value or an error.
Definition result.h:75
Windows-specific device connection pooling.
Device enumeration and management functions.
Definition core.h:13
VidProp
Video processing properties (IAMVideoProcAmp interface)
Definition types.h:50
@ InvalidArgument
Invalid function argument.
@ PropertyNotSupported
Property not supported by device.
@ DeviceNotFound
Device not found or disconnected.
std::vector< Device > list_devices()
Enumerate all available video input devices.
Definition core.cpp:626
CamProp
Camera control properties (IAMCameraControl interface)
Definition types.h:18
bool is_device_connected(const Device &dev)
Check if a device is currently connected and accessible.
Definition core.cpp:549
Result< Camera > open_camera(int device_index)
Create camera from device index.
Definition camera.cpp:130
Device find_device_by_path(const std::wstring &device_path)
Find device by unique Windows device path.
Definition device.cpp:211
Result< void > Ok()
Helper to create successful void Result.
Definition result.h:222
Represents a camera device.
Definition types.h:131
bool is_valid() const
Check if device has valid identifying information.
Definition types.h:186
Property range and default information.
Definition types.h:92
Property setting with value and control mode.
Definition types.h:74