forked from foxsi/SAAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.cpp
247 lines (198 loc) · 7.94 KB
/
stream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// *****************************************************************************
//
// Copyright (c) 2008, Pleora Technologies Inc., All rights reserved.
//
// *****************************************************************************
#include <PvSampleUtils.h>
#include <PvDevice.h>
#include <PvPipeline.h>
#include <PvBuffer.h>
#include <PvStream.h>
//#include <PvStreamRaw.h>
#include <PvSystem.h>
#include <PvInterface.h>
#include <PvDevice.h>
//PV_INIT_SIGNAL_HANDLER();
//
// Shows how to use a PvPipeline object to acquire images from a device
//
bool AcquireImages()
{
// Create a GEV Device finder dialog
//PvDeviceFinderWnd lDeviceFinderWnd;
// Prompt the user to select a GEV Device
//lDeviceFinderWnd.ShowModal();
// Get the connectivity information for the selected GEV Device
//PvDeviceInfo* lDeviceInfo = lDeviceFinderWnd.GetSelected();
PvSystem lSystem;
PvDeviceInfo* lDeviceInfo;
std::cout << "ImperxStream::Connect starting" << std::endl;
PvResult lResult;
// Find all GEV Devices on the network.
lSystem.SetDetectionTimeout( 2000 );
lResult = lSystem.Find();
if( !lResult.IsOK() )
{
printf( "PvSystem::Find Error: %s", lResult.GetCodeString().GetAscii() );
return -1;
}
// Get the number of GEV Interfaces that were found using GetInterfaceCount.
uint32_t lInterfaceCount = lSystem.GetInterfaceCount();
// Display information about all found interface
// For each interface, display information about all devices.
for( uint32_t x = 0; x < lInterfaceCount; x++ )
{
// get pointer to each of interface
PvInterface * lInterface = lSystem.GetInterface( x );
printf( "Interface %i\nMAC Address: %s\nIP Address: %s\nSubnet Mask: %s\n\n",
x,
lInterface->GetMACAddress().GetAscii(),
lInterface->GetIPAddress().GetAscii(),
lInterface->GetSubnetMask().GetAscii() );
// Get the number of GEV devices that were found using GetDeviceCount.
uint32_t lDeviceCount = lInterface->GetDeviceCount();
for( uint32_t y = 0; y < lDeviceCount ; y++ )
{
lDeviceInfo = lInterface->GetDeviceInfo( y );
printf( "Device %i\nMAC Address: %s\nIP Address: %s\nSerial number: %s\n\n",
y,
lDeviceInfo->GetMACAddress().GetAscii(),
lDeviceInfo->GetIPAddress().GetAscii(),
lDeviceInfo->GetSerialNumber().GetAscii() );
}
}
// If no device is selected, abort
if( lDeviceInfo == NULL )
{
printf( "No device selected.\n" );
return false;
}
// Connect to the GEV Device
PvDevice lDevice;
printf( "Connecting to %s\n", lDeviceInfo->GetMACAddress().GetAscii() );
if ( !lDevice.Connect( lDeviceInfo ).IsOK() )
{
printf( "Unable to connect to %s\n", lDeviceInfo->GetMACAddress().GetAscii() );
return false;
}
printf( "Successfully connected to %s\n", lDeviceInfo->GetMACAddress().GetAscii() );
printf( "\n" );
// Get device parameters need to control streaming
PvGenParameterArray *lDeviceParams = lDevice.GetGenParameters();
// Negotiate streaming packet size
lDevice.NegotiatePacketSize();
// Create the PvStream object
PvStream lStream;
// Open stream - have the PvDevice do it for us
printf( "Opening stream to device\n" );
lStream.Open( lDeviceInfo->GetIPAddress() );
// Create the PvPipeline object
PvPipeline lPipeline( &lStream );
// Reading payload size from device
int64_t lSize = 0;
lDeviceParams->GetIntegerValue( "PayloadSize", lSize );
// Set the Buffer size and the Buffer count
lPipeline.SetBufferSize( static_cast<uint32_t>( lSize ) );
lPipeline.SetBufferCount( 16 ); // Increase for high frame rate without missing block IDs
// Have to set the Device IP destination to the Stream
lDevice.SetStreamDestination( lStream.GetLocalIPAddress(), lStream.GetLocalPort() );
// IMPORTANT: the pipeline needs to be "armed", or started before
// we instruct the device to send us images
printf( "Starting pipeline\n" );
lPipeline.Start();
// Get stream parameters/stats
PvGenParameterArray *lStreamParams = lStream.GetParameters();
// TLParamsLocked is optional but when present, it MUST be set to 1
// before sending the AcquisitionStart command
lDeviceParams->SetIntegerValue( "TLParamsLocked", 1 );
printf( "Resetting timestamp counter...\n" );
lDeviceParams->ExecuteCommand( "GevTimestampControlReset" );
// The pipeline is already "armed", we just have to tell the device
// to start sending us images
printf( "Sending StartAcquisition command to device\n" );
lDeviceParams->ExecuteCommand( "AcquisitionStart" );
char lDoodle[] = "|\\-|-/";
int lDoodleIndex = 0;
int64_t lImageCountVal = 0;
double lFrameRateVal = 0.0;
double lBandwidthVal = 0.0;
// Acquire images until the user instructs us to stop
printf( "\n<press a key to stop streaming>\n" );
while ( 1 )
{
// Retrieve next buffer
PvBuffer *lBuffer = NULL;
PvResult lOperationResult;
PvResult lResult = lPipeline.RetrieveNextBuffer( &lBuffer, 1000, &lOperationResult );
if ( lResult.IsOK() )
{
if ( lOperationResult.IsOK() )
{
//
// We now have a valid buffer. This is where you would typically process the buffer.
// -----------------------------------------------------------------------------------------
// ...
lStreamParams->GetIntegerValue( "ImagesCount", lImageCountVal );
lStreamParams->GetFloatValue( "AcquisitionRateAverage", lFrameRateVal );
lStreamParams->GetFloatValue( "BandwidthAverage", lBandwidthVal );
// If the buffer contains an image, display width and height
uint32_t lWidth = 0, lHeight = 0;
if ( lBuffer->GetPayloadType() == PvPayloadTypeImage )
{
// Get image specific buffer interface
PvImage *lImage = lBuffer->GetImage();
// Read width, height
lWidth = lBuffer->GetImage()->GetWidth();
lHeight = lBuffer->GetImage()->GetHeight();
}
printf( "%c BlockID: %016llX W: %i H: %i %.01f FPS %.01f Mb/s\r",
lDoodle[ lDoodleIndex ],
lBuffer->GetBlockID(),
lWidth,
lHeight,
lFrameRateVal,
lBandwidthVal / 1000000.0 );
}
// We have an image - do some processing (...) and VERY IMPORTANT,
// release the buffer back to the pipeline
lPipeline.ReleaseBuffer( lBuffer );
}
else
{
// Timeout
printf( "%c Timeout\r", lDoodle[ lDoodleIndex ] );
}
++lDoodleIndex %= 6;
}
// PvGetChar(); // Flush key buffer for next stop
printf( "\n\n" );
// Tell the device to stop sending images
printf( "Sending AcquisitionStop command to the device\n" );
lDeviceParams->ExecuteCommand( "AcquisitionStop" );
// If present reset TLParamsLocked to 0. Must be done AFTER the
// streaming has been stopped
lDeviceParams->SetIntegerValue( "TLParamsLocked", 0 );
// We stop the pipeline - letting the object lapse out of
// scope would have had the destructor do the same, but we do it anyway
printf( "Stop pipeline\n" );
lPipeline.Stop();
// Now close the stream. Also optionnal but nice to have
printf( "Closing stream\n" );
lStream.Close();
// Finally disconnect the device. Optional, still nice to have
printf( "Disconnecting device\n" );
lDevice.Disconnect();
return true;
}
//
// Main function
//
int main()
{
// PvPipeline used to acquire images from a device
printf( "1. PvPipeline sample - image acquisition from a device\n\n" );
AcquireImages();
printf( "\n<press a key to exit>\n" );
// PvWaitForKeyPress();
return 0;
}