3#include "../_cexports/c_driver.h"
4#include "../_cexports/c_img.h"
6#include "../global.hpp"
8#include "../_decl/decl_device_factory.hpp"
10#include "../driver/_decl/decl_vin_device.hpp"
11#include "../driver/_decl/decl_emu_device.hpp"
12#include "../driver/_decl/decl_video_device.hpp"
13#include "../driver/_decl/decl_genicam_device.hpp"
15#include "../driver/non_streaming_device.hpp"
22 inline HandleGuard<Device> DeviceFactory::LoadAsGenTL(
const String &provider,
AcquisitionStack acquisitionStack)
24 CExports::DEVOBJ obj =
nullptr;
25 if (AllowFallback(acquisitionStack))
27 CVB_CALL_CAPI(CVDCreateOrGetDeviceObjectTyped(provider.c_str(), obj));
31 CVB_CALL_CAPI_CHECKED(CVDCreateOrGetDeviceObjectTyped(provider.c_str(), obj));
33 return HandleGuard<Device>(obj);
36 inline HandleGuard<Device> DeviceFactory::LoadAsVin(
const String &provider,
AcquisitionStack acquisitionStack)
38 CExports::IMG img =
nullptr;
39 if (AllowFallback(acquisitionStack))
41 CVB_CALL_CAPI(LoadImageFileTyped(provider.c_str(), img));
45 CVB_CALL_CAPI_CHECKED(LoadImageFileTyped(provider.c_str(), img));
47 return HandleGuard<Device>(img);
50 inline HandleGuard<Device> DeviceFactory::LoadAsNonStreaming(
const String &provider)
52 CExports::DEVOBJ obj =
nullptr;
53 CVB_CALL_CAPI_CHECKED(LoadDeviceObjectTyped(provider.c_str(), obj));
54 return HandleGuard<Device>(obj);
58 Cvb::HandleGuard<Cvb::Device> &&guard)
noexcept
61 CExports::cvbguid_t deviceGuid = {0};
62 CVB_CALL_CAPI_CHECKED(GetDriverID(guard.Handle(), deviceGuid));
64 if (Cvb::CExports::operator==(deviceGuid, DeviceFactory::AviMpgGuid()))
65 return Device::FromHandle<VideoDevice>(std::move(guard), provider);
66 else if (Cvb::CExports::operator==(deviceGuid, DeviceFactory::EmuGuid()))
67 return Device::FromHandle<EmuDevice>(std::move(guard), provider);
69 return Device::FromHandle<VinDevice>(std::move(guard), provider);
76 switch (acquisitionStack)
78 case AcquisitionStack::Vin:
79 return CreateDeviceByDriverGUID(provider, LoadAsVin(provider, acquisitionStack));
81 case AcquisitionStack::PreferVin:
83 auto vinGuard = LoadAsVin(provider, acquisitionStack);
84 if (vinGuard.Handle())
85 return CreateDeviceByDriverGUID(provider,
std::move(vinGuard));
86 return Device::FromHandle<NonStreamingDevice>(LoadAsNonStreaming(provider), provider);
89 case AcquisitionStack::GenTL:
90 return Device::FromHandle<GenICamDevice>(LoadAsGenTL(provider, acquisitionStack), provider);
92 case AcquisitionStack::PreferGenTL:
95 auto gentlGuard = LoadAsGenTL(provider, acquisitionStack);
96 if (gentlGuard.Handle())
97 return Device::FromHandle<GenICamDevice>(
std::move(gentlGuard), provider);
98 auto vinGuard = LoadAsVin(provider, acquisitionStack);
99 if (vinGuard.Handle())
100 return CreateDeviceByDriverGUID(provider,
std::move(vinGuard));
101 return Device::FromHandle<NonStreamingDevice>(LoadAsNonStreaming(provider), provider);
111 template <
class Rep,
class Period>
124 CExports::ATLIST handle =
nullptr;
125 auto discoverResult = CExports::DODiscoverTyped(accessToken.c_str(),
static_cast<CExports::cvbuint64_t
>(flags), 0,
126 handle,
static_cast<size_t>(msTimeSpan));
127 if (discoverResult < 0)
130 Driver::DiscoveryInformation::List listHandle(handle, [](
void *handle) { CVB_CALL_CAPI(
ReleaseObject(handle)); });
133 auto numEntriesResult = CExports::DOGetNumEntries(listHandle.get(), numEntries);
134 if (numEntriesResult < 0)
136 CvbException::FromCvbResult(numEntriesResult,
"failed to get number of entries for device object"));
139 discoveryInformationVector.reserve(numEntries);
141 for (
size_t i = 0; i < numEntries; ++i)
144 return discoveryInformationVector;
147 inline void DeviceFactory::ChangeBoard(
DevicePtr &device,
int board)
149 if (CExports::CanBoardSelect2(device->Handle()))
151 std::unique_lock<std::mutex>
lock(LockAnchor());
152 ChangeBoard2(device, board);
154 else if (CExports::CanBoardSelect(device->Handle()))
156 std::unique_lock<std::mutex>
lock(LockAnchor());
157 ChangeBoard1(device, board);
164 std::rethrow_exception(CvbException::FromCvbResult(
ErrorCodes::CVB_ERROR,
"no IBoardSelect or IBoardSelect2"));
168 inline void DeviceFactory::ChangeBoard1(
DevicePtr &device,
int board)
170 CExports::cvbval_t currentBoard = 0;
171 auto getBoardResult = CExports::GetBoard(device->Handle(), ¤tBoard);
172 if (getBoardResult < 0)
173 std::rethrow_exception(CvbException::FromCvbResult(getBoardResult,
"no current board"));
174 if (
static_cast<int>(currentBoard) == board)
177 auto setBoardResult = CExports::SetBoard(device->Handle(),
static_cast<CExports::cvbval_t
>(board));
178 if (setBoardResult < 0)
179 std::rethrow_exception(CvbException::FromCvbResult(setBoardResult,
"failed to set board"));
182 inline void DeviceFactory::ChangeBoard2(
DevicePtr &device,
int board)
184 CExports::cvbval_t currentBoard = 0;
185 auto getBoardResult = CExports::BS2GetBoard(device->Handle(), currentBoard);
186 if (getBoardResult < 0)
187 std::rethrow_exception(CvbException::FromCvbResult(getBoardResult,
"no current board"));
188 if (
static_cast<int>(currentBoard) == board)
190 CExports::cvbval_t numBoards = 0;
191 auto numBoardsResult = CExports::BS2GetNumBoards(device->Handle(), numBoards);
192 if (numBoardsResult < 0)
193 std::rethrow_exception(CvbException::FromCvbResult(numBoardsResult,
"no number of boards"));
194 if (board >=
static_cast<int>(numBoards))
195 throw std::out_of_range(
"board out of range");
197 CExports::IMG newImg =
nullptr;
198 auto setBoardResult = CExports::BS2SetBoard(device->Handle(), board, newImg);
199 if (setBoardResult < 0)
200 std::rethrow_exception(CvbException::FromCvbResult(setBoardResult,
"failed to set board"));
202 HandleGuard<Device> guard(newImg);
203 device = Device::FromHandle<Driver::VinDevice>(std::move(guard), device->ResourceLocator());
206 inline void DeviceFactory::ChangePort(
DevicePtr &device,
int port)
208 if (CExports::CanCameraSelect2(device->Handle()))
210 std::unique_lock<std::mutex>
lock(LockAnchor());
211 ChangePort2(device, port);
213 else if (CExports::CanCameraSelect(device->Handle()))
215 std::unique_lock<std::mutex>
lock(LockAnchor());
216 ChangePort1(device, port);
223 std::rethrow_exception(
228 inline void DeviceFactory::ChangePort1(
DevicePtr &device,
int port)
230 CExports::cvbval_t currentPort = 0;
231 auto getPortResult = CExports::GetCamPort(device->Handle(), currentPort);
232 if (getPortResult < 0)
233 std::rethrow_exception(CvbException::FromCvbResult(getPortResult,
"no current port"));
234 if (
static_cast<int>(currentPort) == port)
236 CExports::cvbval_t numPorts = 0;
237 auto numPortsResult = CExports::GetNumPorts(device->Handle(), numPorts);
238 if (numPortsResult < 0)
239 std::rethrow_exception(CvbException::FromCvbResult(numPortsResult,
"no number of ports"));
240 if (port >=
static_cast<int>(numPorts))
241 throw std::out_of_range(
"port out of range");
243 auto setPortResult = CExports::SetCamPort(device->Handle(),
static_cast<CExports::cvbval_t
>(port), 0);
244 if (setPortResult < 0)
245 std::rethrow_exception(CvbException::FromCvbResult(setPortResult,
"failed to set port"));
248 inline void DeviceFactory::ChangePort2(
DevicePtr &device,
int port)
250 CExports::cvbval_t currentPort = 0;
251 auto getPortResult = CExports::CS2GetCamPort(device->Handle(), currentPort);
252 if (getPortResult < 0)
253 std::rethrow_exception(CvbException::FromCvbResult(getPortResult,
"no current port"));
254 if (
static_cast<int>(currentPort) == port)
256 CExports::cvbval_t numPorts = 0;
257 auto numPortsResult = CExports::CS2GetNumPorts(device->Handle(), numPorts);
258 if (numPortsResult < 0)
259 std::rethrow_exception(CvbException::FromCvbResult(numPortsResult,
"no number of ports"));
260 if (port >=
static_cast<int>(numPorts))
261 throw std::out_of_range(
"port out of range");
263 CExports::IMG newImg =
nullptr;
264 auto setPortResult = CExports::CS2SetCamPort(device->Handle(),
static_cast<CExports::cvbval_t
>(port), 0, newImg);
265 if (setPortResult < 0)
266 std::rethrow_exception(CvbException::FromCvbResult(setPortResult,
"failed to set port"));
268 HandleGuard<Device> guard(newImg);
269 device = Device::FromHandle<Driver::VinDevice>(std::move(guard), device->ResourceLocator());
static std::vector< DiscoveryInformation > Discover()
Discovers available devices (not vins) with a default time span of 300ms.
Definition decl_device_factory.hpp:263
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition decl_device_factory.hpp:56
T duration_cast(T... args)
cvbbool_t ReleaseObject(OBJ &Object)
AcquisitionStack
Defines the acquisition stack when opening the device.
Definition driver.hpp:282
DiscoverFlags
Flags controlling the discovery process.
Definition driver.hpp:302
const int CVB_ERROR
Generic unspecified error.
Definition exception.hpp:23
Root namespace for the Image Manager interface.
Definition version.hpp:11
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::shared_ptr< Device > DevicePtr
Convenience shared pointer for Device.
Definition global.hpp:98
T rethrow_exception(T... args)