CVB++ 14.1
Cvb/CppStreamGenDCContainer
1// ----------------------------------------------------------------------------
4// ----------------------------------------------------------------------------
5
6#include <iostream>
7#include <string>
8#include <sstream>
9
10#include <cvb/device_factory.hpp>
11#include <cvb/global.hpp>
12#include <cvb/driver/composite_stream.hpp>
13
14static const constexpr int DELIVERABLE_COUNT = 10; // Number of deliverables to be acquired.
15static const constexpr int ITEM_COUNT =
16 2; // Number of items in a Composite object that are transmitted over a set of GenDC flows.
17static const constexpr int CVB_FLOW_SET_COUNT = 3; // Number of CVB Flow Set, i.e., a set of memory buffers as the
18 // acquired data destination that is transmitted over GenDC flows.
19
20int main()
21{
22 try
23 {
24 // Retrieve CVMockTL in the given system.
25 auto infoList = Cvb::DeviceFactory::Discover(Cvb::DiscoverFlags::IgnoreVins | // Ignores all of the CVB VINs.
26 Cvb::DiscoverFlags::IncludeMockTL // Include CVMockTL.
27 );
28
29 // Make sure if CVMockTL is on the candidate list.
30 Cvb::String accessToken;
31 for (auto n = 0; (n < infoList.size()) && accessToken.empty(); ++n)
32 if (infoList.at(n).AccessToken().find("MockTL") != std::string::npos)
33 accessToken = infoList.at(n).AccessToken();
34
35 if (accessToken.empty())
36 throw std::runtime_error("Could not find CVMockTL.");
37
38 // Instantiate a GenICam device from the CVMockTL.
39 auto device = Cvb::DeviceFactory::Open<Cvb::GenICamDevice>(accessToken, Cvb::AcquisitionStack::GenTL);
40
41 // Instantiate its device node map.
42 auto deviceNodeMap = device->NodeMap("Device");
43
44 // The following code block is for demonstration purpose. They are not public features.
45 {
46 auto configurationNode = deviceNodeMap->Node<Cvb::EnumerationNode>(CVB_LIT("GenDCFlowMappingConfiguration"));
47 configurationNode->SetValue(CVB_LIT("MultipleFlow"));
48 auto imageCountNode = deviceNodeMap->Node<Cvb::IntegerNode>(CVB_LIT("TestGenDCImageCount"));
49 imageCountNode->SetValue(ITEM_COUNT);
50 }
51
52 // Turn on the GenDC streaming mode.
53 auto streamingModeNode = deviceNodeMap->Node<Cvb::EnumerationNode>(CVB_LIT("GenDCStreamingMode"));
54 streamingModeNode->SetValue(CVB_LIT("On"));
55
56 // Get the first stream of the device.
57 auto stream = device->Stream<Cvb::CompositeStream>();
58
59 // Let it allocate the required amount of memory resource.
60 stream->RegisterManagedFlowSetPool(CVB_FLOW_SET_COUNT);
61
62 // Let it start data streaming.
63 stream->Start();
64
65 for (auto m = 0; m < DELIVERABLE_COUNT; ++m)
66 {
67 std::cout << "Acquisition #" << m << "\n";
68
69 // Wait for a single deliverable with a 10-second timeout value.
70 auto waitResultTuple = stream->WaitFor(std::chrono::seconds(10));
71 if (std::get<Cvb::WaitStatus>(waitResultTuple) == Cvb::WaitStatus::Timeout)
72 throw std::runtime_error("Acquisition timeout.");
73
74 // Retrieve VIN buffer node map and get the timestamp of the delivered image.
75 auto &enumerator = std::get<Cvb::NodeMapEnumerator>(waitResultTuple);
76 const auto names = enumerator.Names();
77 const auto keyName = CVB_LIT("VinBuffer");
78 if (std::all_of(names.begin(), names.end(), [keyName](const Cvb::String &name) { return name != keyName; }))
79 throw std::runtime_error("It must contain the node map of Vin buffer.");
80
81 auto vinBufferNodeMap = enumerator.NodeMap(keyName);
82 auto frameIDNode = vinBufferNodeMap->Node<Cvb::IntegerNode>(CVB_LIT("FrameID"));
83 auto timestampNode = vinBufferNodeMap->Node<Cvb::IntegerNode>(CVB_LIT("Timestamp"));
84 auto payloadTypeNode = vinBufferNodeMap->Node<Cvb::EnumerationNode>(CVB_LIT("BufferPayloadType"));
85
86 // Verify the acquired deliverable contents.
87 auto deliverable = std::get<Cvb::CompositePtr>(waitResultTuple);
88
89 std::cout << "Frame ID: " << frameIDNode->Value()
90 << ", Type: " << payloadTypeNode->Value() << ", Timestamp: 0x" << std::hex
91 << timestampNode->Value() << ", Item count: " << deliverable->ItemCount() << "\n";
92
93 // Identify the item in the deliverable.
94 for (auto n = 0; n < deliverable->ItemCount(); ++n)
95 {
96 auto item = deliverable->ItemAt(n);
97 std::stringstream strStream;
98 strStream << "Type: ";
99 if (Cvb::holds_alternative<Cvb::ImagePtr>(item))
100 {
101 auto image = Cvb::get<Cvb::ImagePtr>(item);
102 strStream << "Image, Width: " << image->Width() << ", Height: " << image->Height();
103 }
104 else if (Cvb::holds_alternative<Cvb::PlanePtr>(item))
105 strStream << "Plane";
106 else if (Cvb::holds_alternative<Cvb::PlaneEnumeratorPtr>(item))
107 strStream << "Plane enumerator";
108 else if (Cvb::holds_alternative<Cvb::BufferPtr>(item))
109 strStream << "Buffer";
110 else if (Cvb::holds_alternative<Cvb::PFNCBufferPtr>(item))
111 strStream << "PFNC buffer";
112 else
113 strStream << "Unknown";
114 std::cout << "Item index: " << n << ", " << strStream.str() << "\n";
115 }
116 }
117
118 // Try aborting data streaming.
119 stream->TryAbort();
120 }
121 catch (const std::exception &e)
122 {
123 std::cout << e.what() << std::endl;
124 }
125 return 0;
126}
static std::vector< DiscoveryInformation > Discover()
Discovers available devices (not vins) with a default time span of 300ms.
Definition: decl_device_factory.hpp:221
void RegisterManagedFlowSetPool(int flowSetCount)
Registers an internal flows set pool.
Definition: decl_composite_stream_base.hpp:291
Streams composites.
Definition: composite_stream.hpp:21
A node that presents a choice of values.
Definition: enumeration_node.hpp:18
void SetValue(const String &value)
Sets the symbolic value of this enumeration.
Definition: enumeration_node.hpp:51
Represents a integer number.
Definition: integer_node.hpp:20
void SetValue(std::int64_t value)
Sets the value of this integer node.
Definition: integer_node.hpp:46
std::int64_t Value() const
Gets the value of this integer node.
Definition: integer_node.hpp:33
@ Timeout
A timeout occurred, no image buffer has been returned.