libvisiontransfer  6.5.0
asynctransfer.cpp
1 /*******************************************************************************
2  * Copyright (c) 2019 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #if __GNUC__ == 4 && __GNUC_MINOR__ < 9
16 // This is a very ugly workaround for GCC bug 54562. If omitted,
17 // passing timeouts to collectReceivedImage() is broken.
18 #include <bits/c++config.h>
19 #undef _GLIBCXX_USE_CLOCK_MONOTONIC
20 #endif
21 
22 #include <iostream>
23 #include <functional>
24 #include <stdexcept>
25 #include <thread>
26 #include <condition_variable>
27 #include <chrono>
28 #include <mutex>
29 #include <vector>
30 #include <cstring>
31 #include <algorithm>
32 #include "visiontransfer/asynctransfer.h"
33 #include "visiontransfer/alignedallocator.h"
34 
35 using namespace std;
36 using namespace visiontransfer;
37 using namespace visiontransfer::internal;
38 
39 namespace visiontransfer {
40 
41 /*************** Pimpl class containing all private members ***********/
42 
43 class AsyncTransfer::Pimpl {
44 public:
45  Pimpl(const char* address, const char* service,
46  ImageProtocol::ProtocolType protType, bool server,
47  int bufferSize, int maxUdpPacketSize);
48  ~Pimpl();
49 
50  // Redeclaration of public members
51  void sendImagePairAsync(const ImagePair& imagePair, bool deleteData);
52  bool collectReceivedImagePair(ImagePair& imagePair, double timeout);
53  int getNumDroppedFrames() const;
54  bool isConnected() const;
55  void disconnect();
56  std::string getRemoteAddress() const;
57  bool tryAccept();
58 
59 private:
60  static constexpr int NUM_BUFFERS = 6;
61  static constexpr int SEND_THREAD_SHORT_WAIT_MS = 1;
62  static constexpr int SEND_THREAD_LONG_WAIT_MS = 10;
63 
64  // The encapsulated image transfer object
65  ImageTransfer imgTrans;
66 
67  // Variable for controlling thread termination
68  volatile bool terminate;
69 
70  // There are two threads, one for sending and one for receiving.
71  // Each has a mutex and condition variable for synchronization.
72  std::thread sendThread;
73  std::mutex sendMutex;
74  std::condition_variable sendCond;
75  std::condition_variable sendWaitCond;
76 
77  std::thread receiveThread;
78  std::timed_mutex receiveMutex;
79  std::condition_variable_any receiveCond;
80  std::condition_variable_any receiveWaitCond;
81 
82  // Objects for exchanging images with the send and receive threads
83  ImagePair receivedPair;
84  std::vector<unsigned char, AlignedAllocator<unsigned char> > receivedData[NUM_BUFFERS];
85  bool newDataReceived;
86 
87  ImagePair sendImagePair;
88  bool sendPairValid;
89  bool deleteSendData;
90 
91  // Exception occurred in one of the threads
92  std::exception_ptr receiveException;
93  std::exception_ptr sendException;
94 
95  bool sendThreadCreated;
96  bool receiveThreadCreated;
97 
98  // Main loop for sending thread
99  void sendLoop();
100 
101  // Main loop for receiving;
102  void receiveLoop();
103 
104  void createSendThread();
105 };
106 
107 /******************** Stubs for all public members ********************/
108 
109 AsyncTransfer::AsyncTransfer(const char* address, const char* service,
110  ImageProtocol::ProtocolType protType, bool server,
111  int bufferSize, int maxUdpPacketSize)
112  : pimpl(new Pimpl(address, service, protType, server, bufferSize, maxUdpPacketSize)) {
113 }
114 
115 AsyncTransfer::AsyncTransfer(const DeviceInfo& device, int bufferSize, int maxUdpPacketSize)
116  : pimpl(new Pimpl(device.getIpAddress().c_str(), "7681", static_cast<ImageProtocol::ProtocolType>(device.getNetworkProtocol()),
117  false, bufferSize, maxUdpPacketSize)) {
118 }
119 
120 AsyncTransfer::~AsyncTransfer() {
121  delete pimpl;
122 }
123 
124 void AsyncTransfer::sendImagePairAsync(const ImagePair& imagePair, bool deleteData) {
125  pimpl->sendImagePairAsync(imagePair, deleteData);
126 }
127 
128 bool AsyncTransfer::collectReceivedImagePair(ImagePair& imagePair, double timeout) {
129  return pimpl->collectReceivedImagePair(imagePair, timeout);
130 }
131 
133  return pimpl->getNumDroppedFrames();
134 }
135 
137  return pimpl->isConnected();
138 }
139 
141  return pimpl->disconnect();
142 }
143 
144 std::string AsyncTransfer::getRemoteAddress() const {
145  return pimpl->getRemoteAddress();
146 }
147 
149  return pimpl->tryAccept();
150 }
151 
152 /******************** Implementation in pimpl class *******************/
153 
154 AsyncTransfer::Pimpl::Pimpl(const char* address, const char* service,
155  ImageProtocol::ProtocolType protType, bool server,
156  int bufferSize, int maxUdpPacketSize)
157  : imgTrans(address, service, protType, server, bufferSize, maxUdpPacketSize),
158  terminate(false), newDataReceived(false), sendPairValid(false),
159  deleteSendData(false), sendThreadCreated(false),
160  receiveThreadCreated(false) {
161 
162  if(server) {
163  createSendThread();
164  }
165 }
166 
167 AsyncTransfer::Pimpl::~Pimpl() {
168  terminate = true;
169 
170  sendCond.notify_all();
171  receiveCond.notify_all();
172  sendWaitCond.notify_all();
173  receiveWaitCond.notify_all();
174 
175  if(sendThreadCreated && sendThread.joinable()) {
176  sendThread.join();
177  }
178 
179  if(receiveThreadCreated && receiveThread.joinable()) {
180  receiveThread.join();
181  }
182 
183  if(sendPairValid && deleteSendData) {
184  delete[] sendImagePair.getPixelData(0);
185  delete[] sendImagePair.getPixelData(1);
186  }
187 }
188 
189 void AsyncTransfer::Pimpl::createSendThread() {
190  if(!sendThreadCreated) {
191  // Lazy initialization of the send thread as it is not always needed
192  unique_lock<mutex> lock(sendMutex);
193  sendThread = thread(bind(&AsyncTransfer::Pimpl::sendLoop, this));
194  sendThreadCreated = true;
195  }
196 }
197 
198 void AsyncTransfer::Pimpl::sendImagePairAsync(const ImagePair& imagePair, bool deleteData) {
199  createSendThread();
200 
201  while(true) {
202  unique_lock<mutex> lock(sendMutex);
203 
204  // Test for errors
205  if(sendException) {
206  std::rethrow_exception(sendException);
207  }
208 
209  if(!sendPairValid) {
210  sendImagePair = imagePair;
211  sendPairValid = true;
212  deleteSendData = deleteData;
213 
214  // Wake up the sender thread
215  sendCond.notify_one();
216 
217  return;
218  } else {
219  // Wait for old data to be processed first
220  sendWaitCond.wait(lock);
221  }
222  }
223 }
224 
225 bool AsyncTransfer::Pimpl::collectReceivedImagePair(ImagePair& imagePair, double timeout) {
226  if(!receiveThreadCreated) {
227  // Lazy initialization of receive thread
228  unique_lock<timed_mutex> lock(receiveMutex);
229  receiveThreadCreated = true;
230  receiveThread = thread(bind(&AsyncTransfer::Pimpl::receiveLoop, this));
231  }
232 
233  // Acquire mutex
234  unique_lock<timed_mutex> lock(receiveMutex, std::defer_lock);
235  if(timeout < 0) {
236  lock.lock();
237  } else {
238  std::chrono::steady_clock::time_point lockStart =
239  std::chrono::steady_clock::now();
240  if(!lock.try_lock_for(std::chrono::microseconds(static_cast<unsigned int>(timeout*1e6)))) {
241  // Timed out
242  return false;
243  }
244 
245  // Update timeout
246  unsigned int lockDuration = static_cast<unsigned int>(std::chrono::duration_cast<std::chrono::microseconds>(
247  std::chrono::steady_clock::now() - lockStart).count());
248  timeout = std::max(0.0, timeout - lockDuration*1e-6);
249  }
250 
251  // Test for errors
252  if(receiveException) {
253  std::rethrow_exception(receiveException);
254  }
255 
256  if(timeout == 0 && !newDataReceived) {
257  // No image has been received and we are not blocking
258  return false;
259  }
260 
261  // If there is no data yet then keep on waiting
262  if(!newDataReceived) {
263  if(timeout < 0) {
264  while(!terminate && !receiveException && !newDataReceived) {
265  receiveCond.wait(lock);
266  }
267  } else {
268  receiveCond.wait_for(lock, std::chrono::microseconds(static_cast<unsigned int>(timeout*1e6)));
269  }
270  }
271 
272  // Test for errors again
273  if(receiveException) {
274  std::rethrow_exception(receiveException);
275  }
276 
277  if(newDataReceived) {
278  // Get the received image
279  imagePair = receivedPair;
280 
281  newDataReceived = false;
282  receiveWaitCond.notify_one();
283 
284  return true;
285  } else {
286  return false;
287  }
288 }
289 
290 void AsyncTransfer::Pimpl::sendLoop() {
291  {
292  // Delay the thread start
293  unique_lock<mutex> lock(sendMutex);
294  }
295 
296  ImagePair pair;
297  bool deletePair = false;
298 
299  try {
300  while(!terminate) {
301  // Wait for next image
302  {
303  unique_lock<mutex> lock(sendMutex);
304  // Wait for next frame to be queued
305  bool firstWait = true;
306  while(!terminate && !sendPairValid) {
307  imgTrans.transferData();
308  sendCond.wait_for(lock, std::chrono::milliseconds(
309  firstWait ? SEND_THREAD_SHORT_WAIT_MS : SEND_THREAD_LONG_WAIT_MS));
310  firstWait = false;
311  }
312  if(!sendPairValid) {
313  continue;
314  }
315 
316  pair = sendImagePair;
317  deletePair = deleteSendData;
318  sendPairValid = false;
319 
320  sendWaitCond.notify_one();
321  }
322 
323  if(!terminate) {
324  imgTrans.setTransferImagePair(pair);
325  imgTrans.transferData();
326  }
327 
328  if(deletePair) {
329  delete[] pair.getPixelData(0);
330  delete[] pair.getPixelData(1);
331  deletePair = false;
332  }
333  }
334  } catch(...) {
335  // Store the exception for later
336  if(!sendException) {
337  sendException = std::current_exception();
338  }
339  sendWaitCond.notify_all();
340 
341  // Don't forget to free the memory
342  if(deletePair) {
343  delete[] pair.getPixelData(0);
344  delete[] pair.getPixelData(1);
345  deletePair = false;
346  }
347  }
348 }
349 
350 void AsyncTransfer::Pimpl::receiveLoop() {
351  {
352  // Delay the thread start
353  unique_lock<timed_mutex> lock(receiveMutex);
354  }
355 
356  try {
357  ImagePair currentPair;
358  int bufferIndex = 0;
359 
360  while(!terminate) {
361  // Receive new image
362  if(!imgTrans.receiveImagePair(currentPair)) {
363  // No image available
364  continue;
365  }
366 
367  // Copy the pixel data
368  for(int i=0;i<2;i++) {
369  int bytesPerPixel = currentPair.getBytesPerPixel(i);
370  int newStride = currentPair.getWidth() * bytesPerPixel;
371  int totalSize = currentPair.getHeight() * newStride;
372  if(static_cast<int>(receivedData[i + bufferIndex].size()) < totalSize) {
373  receivedData[i + bufferIndex].resize(totalSize);
374  }
375  if(newStride == currentPair.getRowStride(i)) {
376  memcpy(&receivedData[i + bufferIndex][0], currentPair.getPixelData(i),
377  newStride*currentPair.getHeight());
378  } else {
379  for(int y = 0; y<currentPair.getHeight(); y++) {
380  memcpy(&receivedData[i + bufferIndex][y*newStride],
381  &currentPair.getPixelData(i)[y*currentPair.getRowStride(i)],
382  newStride);
383  }
384  currentPair.setRowStride(i, newStride);
385  }
386  currentPair.setPixelData(i, &receivedData[i + bufferIndex][0]);
387  }
388 
389  {
390  unique_lock<timed_mutex> lock(receiveMutex);
391 
392  // Wait for previously received data to be processed
393  while(newDataReceived) {
394  receiveWaitCond.wait_for(lock, std::chrono::milliseconds(100));
395  if(terminate) {
396  return;
397  }
398  }
399 
400  // Notify that a new image pair has been received
401  newDataReceived = true;
402  receivedPair = currentPair;
403  receiveCond.notify_one();
404  }
405 
406  // Increment index for data buffers
407  bufferIndex = (bufferIndex + 2) % NUM_BUFFERS;
408  }
409  } catch(...) {
410  // Store the exception for later
411  if(!receiveException) {
412  receiveException = std::current_exception();
413  }
414  receiveCond.notify_all();
415  }
416 }
417 
418 bool AsyncTransfer::Pimpl::isConnected() const {
419  return imgTrans.isConnected();
420 }
421 
422 void AsyncTransfer::Pimpl::disconnect() {
423  imgTrans.disconnect();
424 }
425 
426 std::string AsyncTransfer::Pimpl::getRemoteAddress() const {
427  return imgTrans.getRemoteAddress();
428 }
429 
430 int AsyncTransfer::Pimpl::getNumDroppedFrames() const {
431  return imgTrans.getNumDroppedFrames();
432 }
433 
434 bool AsyncTransfer::Pimpl::tryAccept() {
435  return imgTrans.tryAccept();
436 }
437 
438 } // namespace
439 
int getHeight() const
Returns the height of each image.
Definition: imagepair.h:187
bool tryAccept()
Tries to accept a client connection.
Class for synchronous transfer of image pairs.
Definition: imagetransfer.h:36
bool collectReceivedImagePair(ImagePair &imagePair, double timeout=-1)
Collects the asynchronously received image.
AsyncTransfer(const char *address, const char *service="7681", ImageProtocol::ProtocolType protType=ImageProtocol::PROTOCOL_UDP, bool server=false, int bufferSize=1048576, int maxUdpPacketSize=1472)
Creates a new transfer object.
A lightweight protocol for transferring image pairs.
Definition: imageprotocol.h:38
void setPixelData(int imageNumber, unsigned char *pixelData)
Sets the pixel data for the given image.
Definition: imagepair.h:118
void setRowStride(int imageNumber, int stride)
Sets a new row stride for the pixel data of one image.
Definition: imagepair.h:88
bool isConnected() const
Returns true if a remote connection is established.
void disconnect()
Terminates the current connection.
int getRowStride(int imageNumber) const
Returns the row stride for the pixel data of one image.
Definition: imagepair.h:195
int getNumDroppedFrames() const
Returns the number of frames that have been dropped since connecting to the current remote host...
ProtocolType
Supported network protocols.
Definition: imageprotocol.h:41
std::string getRemoteAddress() const
Returns the address of the remote host.
int getWidth() const
Returns the width of each image.
Definition: imagepair.h:182
Aggregates information about a discovered device.
Definition: deviceinfo.h:25
unsigned char * getPixelData(int imageNumber) const
Returns the pixel data for the given image.
Definition: imagepair.h:217
void sendImagePairAsync(const ImagePair &imagePair, bool deleteData=false)
Starts an asynchronous transmission of the given image pair.
A set of two images, which are usually the left camera image and the disparity map.
Definition: imagepair.h:33
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
Definition: imagepair.h:293
Nerian Vision Technologies