YARP
Yet Another Robot Platform
RemoteFrameGrabber.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_DEV_REMOTEFRAMEGRABBER_H
8 #define YARP_DEV_REMOTEFRAMEGRABBER_H
9 
10 #include <cstring> // for memcpy
11 
12 #include <yarp/os/Network.h>
13 #include <yarp/os/LogComponent.h>
14 #include <yarp/os/LogStream.h>
15 #include <yarp/dev/DeviceDriver.h>
17 
22 
23 #include <mutex>
24 
26 
27 
36  public yarp::dev::DeviceDriver,
37  public yarp::dev::IFrameGrabberImage,
38  public yarp::proto::framegrabber::FrameGrabberControls_Forwarder,
39  public yarp::proto::framegrabber::FrameGrabberControlsDC1394_Forwarder,
40  public yarp::proto::framegrabber::RgbVisualParams_Forwarder
41 {
42 public:
48  ~RemoteFrameGrabber() override = default;
49 
50 
52  {
53  mutex.lock();
54  if(no_stream == true)
55  {
56  image.zero();
57  mutex.unlock();
58  return false;
59  }
60 
61  if (reader.read(true)!=nullptr) {
62  image = *(reader.lastRead());
63  lastHeight = image.height();
64  lastWidth = image.width();
65  mutex.unlock();
66  return true;
67  }
68  mutex.unlock();
69  return false;
70  }
71 
72  bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf<std::pair<int, int> > vertices, yarp::sig::ImageOf<yarp::sig::PixelRgb>& image) override
73  {
74  yarp::os::Bottle cmd;
75  yarp::os::Bottle response;
77  cmd.addVocab32(VOCAB_GET);
79  cmd.addInt32(cropType);
80  yarp::os::Bottle & list = cmd.addList();
81  for(size_t i=0; i<vertices.size(); i++)
82  {
83  list.addInt32(vertices[i].first);
84  list.addInt32(vertices[i].second);
85  }
86  port.write(cmd,response);
87 
88  // Parse the response
89  image.zero();
90  if( (response.get(0).asVocab32() != VOCAB_CROP) || (response.size() != 5) || (!response.get(4).isBlob()))
91  {
92  yCError(REMOTEFRAMEGRABBER) << "getImageCrop: malformed response message. Size is " << response.size();
93  return false;
94  }
95 
96  image.resize(response.get(2).asInt32(), response.get(3).asInt32());
97  unsigned char *pixelOut = image.getRawImage();
98 
99  if (response.get(4).asBlob()) {
100  memcpy(pixelOut, response.get(4).asBlob(), (size_t)image.getRawImageSize());
101  }
102 
103  return true;
104  }
105 
106  // this is bad!
107  int height() const override
108  {
109  return lastHeight;
110  }
111 
112  int width() const override
113  {
114  return lastWidth;
115  }
116 
127  bool open(yarp::os::Searchable& config) override
128  {
130  yCDebug(REMOTEFRAMEGRABBER) << "config is " << config.toString();
131 
132  remote = config.check("remote",yarp::os::Value(""),
133  "port name of real grabber").asString();
134  local = config.check("local",yarp::os::Value("..."),
135  "port name to use locally").asString();
136  std::string carrier =
137  config.check("stream",yarp::os::Value("tcp"),
138  "carrier to use for streaming").asString();
139  port.open(local);
140  if (remote!="") {
141  yCInfo(REMOTEFRAMEGRABBER) << "connecting " << local << " to " << remote;
142 
143  if(!config.check("no_stream") )
144  {
145  no_stream = false;
146  if (!yarp::os::Network::connect(remote, local, carrier)) {
147  yCError(REMOTEFRAMEGRABBER) << "cannot connect " << local << " to " << remote;
148  }
149  } else {
150  no_stream = true;
151  }
152 
153  // reverse connection for RPC
154  // could choose to do this only on need
155 
156  yarp::os::Network::connect(local,remote);
157  }
158  reader.attach(port);
159  return true;
160  }
161 
162  bool close() override
163  {
164  port.close();
165 // mutex.lock(); // why does it need this?
166  return true;
167  }
168 
169 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
170  bool setBrightness(double v) override
171  {
172  return setCommand(VOCAB_BRIGHTNESS, v);
173  }
174  double getBrightness() override
175  {
176  return getCommand(VOCAB_BRIGHTNESS);
177  }
178  bool setExposure(double v) override
179  {
180  return setCommand(VOCAB_EXPOSURE, v);
181  }
182  double getExposure() override
183  {
184  return getCommand(VOCAB_EXPOSURE);
185  }
186 
187  bool setSharpness(double v) override
188  {
189  return setCommand(VOCAB_SHARPNESS, v);
190  }
191  double getSharpness() override
192  {
193  return getCommand(VOCAB_SHARPNESS);
194  }
195 
196  bool setWhiteBalance(double blue, double red) override
197  {
198  return setCommand(VOCAB_WHITE, blue, red);
199  }
200  bool getWhiteBalance(double &blue, double &red) override
201  {
202  return getCommand(VOCAB_WHITE, blue, red);
203  }
204 
205  bool setHue(double v) override
206  {
207  return setCommand(VOCAB_HUE,v);
208  }
209  double getHue() override
210  {
211  return getCommand(VOCAB_HUE);
212  }
213 
214  bool setSaturation(double v) override
215  {
216  return setCommand(VOCAB_SATURATION,v);
217  }
218  double getSaturation() override
219  {
220  return getCommand(VOCAB_SATURATION);
221  }
222 
223  bool setGamma(double v) override
224  {
225  return setCommand(VOCAB_GAMMA,v);
226  }
227  double getGamma() override
228  {
229  return getCommand(VOCAB_GAMMA);
230  }
231 
232  bool setShutter(double v) override
233  {
234  return setCommand(VOCAB_SHUTTER,v);
235  }
236  double getShutter() override
237  {
238  return getCommand(VOCAB_SHUTTER);
239  }
240 
241  bool setGain(double v) override
242  {
243  return setCommand(VOCAB_GAIN,v);
244  }
245  double getGain() override
246  {
247  return getCommand(VOCAB_GAIN);
248  }
249 
250  bool setIris(double v) override
251  {
252  return setCommand(VOCAB_IRIS,v);
253  }
254  double getIris() override
255  {
256  return getCommand(VOCAB_IRIS);
257  }
258 #endif
259 
260 private:
262  yarp::os::Port port;
263  std::string remote;
264  std::string local;
265  std::mutex mutex;
266  int lastHeight{0};
267  int lastWidth{0};
268  bool no_stream{false};
269 
270 protected:
271 
272  IFrameGrabberControlsDC1394 *Ifirewire{nullptr};
273 
274  bool setCommand(int code, double v)
275  {
276  yarp::os::Bottle cmd, response;
278  cmd.addVocab32(VOCAB_SET);
279  cmd.addVocab32(code);
280  cmd.addFloat64(v);
281  port.write(cmd,response);
282  return true;
283  }
284 
285  bool setCommand(int code, double b, double r)
286  {
287  yarp::os::Bottle cmd, response;
289  cmd.addVocab32(VOCAB_SET);
290  cmd.addVocab32(code);
291  cmd.addFloat64(b);
292  cmd.addFloat64(r);
293  port.write(cmd,response);
294  return true;
295  }
296 
297  double getCommand(int code) const
298  {
299  yarp::os::Bottle cmd, response;
301  cmd.addVocab32(VOCAB_GET);
302  cmd.addVocab32(code);
303  port.write(cmd,response);
304  // response should be [cmd] [name] value
305  return response.get(2).asFloat64();
306  }
307 
308  bool getCommand(int code, double &b, double &r) const
309  {
310  yarp::os::Bottle cmd, response;
312  cmd.addVocab32(VOCAB_GET);
313  cmd.addVocab32(code);
314  port.write(cmd,response);
315  // response should be [cmd] [name] value
316  b=response.get(2).asFloat64();
317  r=response.get(3).asFloat64();
318  return true;
319  }
320 };
321 
322 #endif // YARP_DEV_REMOTEFRAMEGRABBER_H
constexpr yarp::conf::vocab32_t VOCAB_GAMMA
Definition: CameraVocabs.h:32
constexpr yarp::conf::vocab32_t VOCAB_GAIN
Definition: CameraVocabs.h:34
constexpr yarp::conf::vocab32_t VOCAB_SHUTTER
Definition: CameraVocabs.h:33
constexpr yarp::conf::vocab32_t VOCAB_WHITE
Definition: CameraVocabs.h:29
constexpr yarp::conf::vocab32_t VOCAB_IRIS
Definition: CameraVocabs.h:35
constexpr yarp::conf::vocab32_t VOCAB_HUE
Definition: CameraVocabs.h:30
constexpr yarp::conf::vocab32_t VOCAB_SHARPNESS
Definition: CameraVocabs.h:28
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_IMAGE
Definition: CameraVocabs.h:16
constexpr yarp::conf::vocab32_t VOCAB_BRIGHTNESS
Definition: CameraVocabs.h:26
constexpr yarp::conf::vocab32_t VOCAB_SATURATION
Definition: CameraVocabs.h:31
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_CONTROL
Definition: CameraVocabs.h:39
constexpr yarp::conf::vocab32_t VOCAB_CROP
Definition: CameraVocabs.h:38
constexpr yarp::conf::vocab32_t VOCAB_EXPOSURE
Definition: CameraVocabs.h:27
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:13
constexpr yarp::conf::vocab32_t VOCAB_SET
Definition: GenericVocabs.h:12
cropType_id_t
const yarp::os::LogComponent & REMOTEFRAMEGRABBER()
remote_grabber: Connect to a ServerFrameGrabber. See ServerFrameGrabber for the network protocol used...
bool setExposure(double v) override
Set the exposure.
double getCommand(int code) const
double getIris() override
Read the iris parameter.
bool open(yarp::os::Searchable &config) override
Configure with a set of options.
bool setShutter(double v) override
Set the shutter parameter.
bool setWhiteBalance(double blue, double red) override
Set the white balance for the frame grabber.
bool setSaturation(double v) override
Set the saturation.
RemoteFrameGrabber & operator=(RemoteFrameGrabber &&)=delete
bool setCommand(int code, double v)
bool setGamma(double v) override
Set the gamma.
bool setIris(double v) override
Set the iris.
int height() const override
Return the height of each frame.
double getExposure() override
Read the exposure parameter.
bool setCommand(int code, double b, double r)
double getSaturation() override
Read the saturation parameter.
RemoteFrameGrabber(RemoteFrameGrabber &&)=delete
double getShutter() override
Read the shutter parameter.
RemoteFrameGrabber & operator=(const RemoteFrameGrabber &)=delete
bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int > > vertices, yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
bool setHue(double v) override
Set the hue.
bool getWhiteBalance(double &blue, double &red) override
Read the white balance parameters.
double getBrightness() override
Read the brightness parameter.
double getSharpness() override
Read the sharpness parameter.
double getHue() override
Read the hue parameter.
int width() const override
Return the width of each frame.
double getGain() override
Read the gain parameter.
bool close() override
Close the DeviceDriver.
bool setBrightness(double v) override
Set the brightness.
bool setSharpness(double v) override
Set the sharpness.
bool getCommand(int code, double &b, double &r) const
double getGamma() override
Read the gamma parameter.
RemoteFrameGrabber(const RemoteFrameGrabber &)=delete
~RemoteFrameGrabber() override=default
bool getImage(yarp::sig::ImageOf< yarp::sig::PixelRgb > &image) override
bool setGain(double v) override
Set the gain.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:182
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition: Bottle.cpp:158
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:140
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:682
Buffer incoming data to a port.
A mini-server for network communication.
Definition: Port.h:46
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:436
A base class for nested structures that can be searched.
Definition: Searchable.h:63
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
A single value (typically within a Bottle).
Definition: Value.h:43
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual bool isBlob() const
Checks if value is a binary object.
Definition: Value.cpp:180
virtual const char * asBlob() const
Get binary data value.
Definition: Value.cpp:261
size_t width() const
Gets width of image in pixels.
Definition: Image.h:163
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:541
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
Definition: Image.cpp:550
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition: Image.cpp:452
void zero()
Set all pixels to 0.
Definition: Image.cpp:445
size_t height() const
Gets height of image in pixels.
Definition: Image.h:169
Provides:
Definition: Vector.h:117
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
#define YARP_DECLARE_LOG_COMPONENT(name)
Definition: LogComponent.h:73
#define yCDebug(component,...)
Definition: LogComponent.h:128
IFrameGrabberOf< yarp::sig::ImageOf< yarp::sig::PixelRgb > > IFrameGrabberImage
The main, catch-all namespace for YARP.
Definition: dirs.h:16