runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
ZmqPubExtractor.cpp
Go to the documentation of this file.
1 #include <ZmqPubExtractor.h>
2 
3 #include <iostream>
4 
5 #include <stringtricks.h>
6 
7 using namespace std;
8 
9 ZmqPubExtractor::ZmqPubExtractor(zmq::context_t & _ctx,
10  const PubSubEndpoint & _source,
11  const PubSubEndpoint & _destination,
12  const Roi<unsigned int> & _roi):
13  Extractor(_ctx, _source), destination(_destination), roi(_roi) {
14 }
15 
17 
18 }
19 
20 std::string ZmqPubExtractor::type() {
21  return "zmqPub";
22 }
23 
25  return destination.topic;
26 }
27 
28 std::list<std::string> ZmqPubExtractor::parameters() {
29  return {SSTR(roi.x), SSTR(roi.y), SSTR(roi.width), SSTR(roi.height)};
30 }
31 
32 void ZmqPubExtractor::extractSetup() {
33  destinationSocket = new zmq::socket_t(ctx, ZMQ_PUB);
34  destinationSocket->connect(destination.endpoint.c_str());
35 }
36 
37 void ZmqPubExtractor::extractImpl(unsigned int width,
38  unsigned int height,
39  unsigned int bytesPerPixel,
40  uint64_t highResTime,
41  uint64_t wallTime,
42  zmq::message_t * sourceImage) {
43  (void) highResTime;
44  (void) wallTime;
45 
46  zmq::message_t header(destination.topic.length());
47  memcpy(header.data(), destination.topic.c_str(), destination.topic.length());
48  destinationSocket->send(header, ZMQ_SNDMORE);
49 
50  /* TODO: maybe stop preview transmission if too many packets are on the way.
51  This might be helpful:
52 
53  zmq::message_t data(((char*)sourceImage->data()) + (((roi.y * width) + roi.x) * bytesPerPixel),
54  roi.width * roi.height * bytesPerPixel,
55  cbWrapper,
56  new std::function<void()>([=]{delete sourceImage;}));
57  destinationSocket->send(data);
58  */
59 
60  zmq::message_t dimensions(5 * 4); /* width, height, bytesPerPixel, extract_width, extract_height [uint32] */
61  insertNetworkUInt32(((char*)dimensions.data()) + 0, width);
62  insertNetworkUInt32(((char*)dimensions.data()) + 4, height);
63  insertNetworkUInt32(((char*)dimensions.data()) + 8, bytesPerPixel);
64  insertNetworkUInt32(((char*)dimensions.data()) + 12, roi.width);
65  insertNetworkUInt32(((char*)dimensions.data()) + 16, roi.height);
66  destinationSocket->send(dimensions, ZMQ_SNDMORE);
67 
68  if(roi.width > width) {
69  roi.width = width;
70  }
71  if(roi.height > height) {
72  roi.height = height;
73  }
74 
75  /* we want to free the image buffer as fast as possible, so we copy our roi
76  even in cases where the data is in the right shape already */
77  zmq::message_t destinationImage(roi.width * roi.height * bytesPerPixel);
78  for(unsigned int i = 0; i < roi.height; ++i) {
79  memcpy(((char*)destinationImage.data()) + i * roi.width * bytesPerPixel,
80  ((char*)sourceImage->data()) + ((((roi.y + i) * width) + roi.x) * bytesPerPixel),
81  roi.width * bytesPerPixel);
82  }
83  delete sourceImage;
84  destinationSocket->send(destinationImage);
85 }
86 
87 void ZmqPubExtractor::extractTeardown() {
88  delete destinationSocket;
89 }
void insertNetworkUInt32(void *buffer, uint32_t value)
Definition: utils.cpp:40
T height
Definition: utils.h:24
Base class for an image extractor.
Definition: Extractor.h:17
STL namespace.
std::string SSTR(Args &&...components)
Creates a temporary string stream for string concatenation.
Definition: stringtricks.h:21
std::string identifier()
std::string type()
zmq::context_t & ctx
Definition: Extractor.h:27
ZmqPubExtractor(zmq::context_t &_ctx, const PubSubEndpoint &_source, const PubSubEndpoint &_destination, const Roi< unsigned int > &_roi)
std::string endpoint
Definition: utils.h:15
T width
Definition: utils.h:23
std::list< std::string > parameters()
T y
Definition: utils.h:22
T x
Definition: utils.h:21
std::string topic
Definition: utils.h:16