CVB++ 15.0
detail_stream.hpp
1#pragma once
2
3#include "../../global.hpp"
4
5#include "../../_decl/decl_device.hpp"
6
7#include "../_decl/decl_stream.hpp"
8
9#include "../_decl/decl_buffer_image.hpp"
10#include "../_decl/decl_ring_buffer.hpp"
11#include "../_decl/decl_ring_buffer_image.hpp"
12
13namespace Cvb
14{
15
16 CVB_BEGIN_INLINE_NS
17
18 namespace Driver
19 {
20
21 inline Stream::Stream(const DevicePtr &device)
22 : StreamBase(device)
23 {
24 SetAcquisitionInterface(AcquisitionEngine::GetBestAcquisitionInterface(*device));
25
26 if (CExports::CanGrab2(Parent()->Handle()))
27 StreamStats::CreateGrab2(*Parent()).swap(statistics_);
28 else
29 StreamStats::CreateNo(*Parent()).swap(statistics_);
30 }
31
32 template <class Rep, class Period>
34 {
35 acquisitionEngine_->WaitFor(timeSpan, waitStatus);
36 if (waitStatus != WaitStatus::Ok)
38
39 auto image = MakeStreamImage();
40 Parent()->OnImageAcquired();
41 return image;
42 }
43
44 template <class Rep, class Period>
46 WaitStatus &waitStatus)
47 {
48 acquisitionEngine_->GetTimedSnapshot(timeSpan, waitStatus);
49 if (waitStatus != WaitStatus::Ok)
51
52 auto image = MakeSnapshotImage();
53 Parent()->OnImageAcquired();
54 return image;
55 }
56
58 Stream::AcquisitionEngine::GetBestAcquisitionInterface(const Device &device)
59 {
60 if (CExports::CanGrab2(device.Handle()))
62 else if (CExports::CanGrabber(device.Handle()))
64 else
65 throw std::runtime_error("no suitable acquisition interface available");
66 }
67
68 inline void Stream::AcquisitionEngine::SetTimeout(uint32_t msTimeSpan)
69 {
70 if (lastTimeout_ == msTimeSpan)
71 return;
72
73 auto result = CExports::SetTimeout(device_.Handle(), msTimeSpan);
74 if (result < 0)
75 CvbException::FromCvbResult(result, "failed to set timeout");
76 lastTimeout_ = msTimeSpan;
77 }
78
79 inline void *Stream::AcquisitionEngine::Handle() const noexcept
80 {
81 return device_.Handle();
82 }
83
84 inline Stream::Grab::Grab(const Device &device)
85 : AcquisitionEngine(device)
86 , isRunning_(false)
87 {
88 if (!CExports::CanGrabber(device.Handle()))
89 throw std::runtime_error("no IGrabber");
90 }
91
92 inline int Stream::Grab::TryWaitFor(uint32_t msTineSpan)
93 {
94 SetTimeout(msTineSpan);
95 return CExports::Snap(Handle());
96 }
97
98 inline int Stream::Grab::TryGetTimedSnapshot(uint32_t msTineSpan)
99 {
100 SetTimeout(msTineSpan);
101 return CExports::Snap(Handle());
102 }
103
104 inline Stream::Grab2::Grab2(const Device &device)
105 : AcquisitionEngine(device)
106 {
107 if (!CExports::CanGrab2(device.Handle()))
108 throw std::runtime_error("no IGrab2");
109 }
110
111 inline void Stream::Grab2::Start()
112 {
113 auto result = CExports::G2Grab(Handle());
114 if (result < 0)
115 std::rethrow_exception(CvbException::FromCvbResult(result, "failed to start acquisition with G2Grab"));
116 }
117
118 inline bool Stream::Grab2::TryStop() noexcept
119 {
120 auto result = CExports::G2Freeze(Handle(), false);
121 return (result >= 0);
122 }
123
124 inline bool Stream::Grab2::TryAbort() noexcept
125 {
126 auto result = CExports::G2Freeze(Handle(), true);
127 return (result >= 0);
128 }
129
130 inline bool Stream::Grab2::IsRunning() const noexcept
131 {
132 double active = 0.0;
133 CExports::G2GetGrabStatus(Handle(), CExports::GRAB_INFO_GRAB_ACTIVE, active);
134 return active != 0.0;
135 }
136
137 inline int Stream::Grab2::TryWaitFor(uint32_t msTineSpan)
138 {
139 SetTimeout(msTineSpan);
140 return CExports::G2Wait(Handle());
141 }
142
143 inline int Stream::Grab2::TryGetTimedSnapshot(uint32_t msTineSpan)
144 {
145 SetTimeout(msTineSpan);
146 return CExports::Snap(Handle());
147 }
148
149 inline bool Stream::Grab2Stats::TryGetValue(StreamInfo query, double &value) const noexcept
150 {
151 auto result = CExports::G2GetGrabStatus(Handle(), static_cast<CExports::GRAB_INFO_CMD>(query), value);
152 return result >= 0;
153 }
154
155 inline void *Stream::StreamStats::Handle() const noexcept
156 {
157 return device_.Handle();
158 }
159
160 } // namespace Driver
161
162 CVB_END_INLINE_NS
163
164} // namespace Cvb
Generic CVB physical device.
Definition decl_device.hpp:78
void * Handle() const noexcept
Classic API device handle.
Definition decl_device.hpp:122
Base class of all streams.
Definition stream_base.hpp:19
DevicePtr Parent() const noexcept
Gets the parent device of this stream.
Definition stream_base.hpp:42
std::shared_ptr< T > WaitFor(const std::chrono::duration< Rep, Period > &timeSpan, WaitStatus &waitStatus)
Waits for the given time span for the next acquired image.
Definition decl_stream.hpp:153
std::shared_ptr< T > GetTimedSnapshot(const std::chrono::duration< Rep, Period > &timeSpan, WaitStatus &waitStatus)
Acquires a single image for a given time span and returns it.
Definition decl_stream.hpp:300
Namespace for driver or device related operations.
Definition decl_composite.hpp:28
std::shared_ptr< StreamImage > StreamImagePtr
Convenience shared pointer for StreamImage.
Definition driver.hpp:93
StreamInfo
Queryable stream information.
Definition driver.hpp:451
AcquisitionInterface
Known acquisition CVB interfaces.
Definition driver.hpp:435
@ Grab2
Ring buffer / queue based acquisition.
Definition driver.hpp:439
@ Grabber
Basic grabber interface for single image acquisition.
Definition driver.hpp:437
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
WaitStatus
Status after waiting for an image to be returned.
Definition global.hpp:396
@ Ok
Everything is fine, a new image arrived.
Definition global.hpp:398
std::shared_ptr< Device > DevicePtr
Convenience shared pointer for Device.
Definition global.hpp:98