YARP
Yet Another Robot Platform
FrameGrabber_nwc_yarp.cpp
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 
8 
9 #include <yarp/os/Network.h>
10 #include <yarp/os/LogComponent.h>
11 #include <yarp/os/LogStream.h>
12 
14 
15 
16 namespace {
17 YARP_LOG_COMPONENT(FRAMEGRABBER_NWC_YARP, "yarp.devices.frameGrabber_nwc_yarp")
18 } // namespace
19 
20 
21 
22 // BEGIN StreamReceiver
24 {
25  return m_lastHeight;
26 }
27 
29 {
30  return m_lastWidth;
31 }
32 
34 {
35  return m_lastStamp;
36 }
37 
38 template <typename ImageType>
39 bool StreamReceiver::lastImage(ImageType& image)
40 {
41  std::lock_guard<std::mutex> lock(m_mutex);
42 
43  bool ret {false};
44  if (!port.isOpen()) {
45  image.zero();
46  ret = false;
47  } else if (reader.check()) {
48  ret = image.copy(*(reader.read(true)));
49  reader.getEnvelope(m_lastStamp);
50  m_lastHeight = image.height();
51  m_lastWidth = image.width();
52  } else {
53  ret = image.copy(*(reader.lastRead()));
54  }
55 
56  return ret;
57 }
58 
59 bool StreamReceiver::open(const std::string& local,
60  const std::string& remote,
61  const std::string& carrier)
62 {
63  std::lock_guard<std::mutex> lock(m_mutex);
64 
65  if (!port.open(local)) {
66  yCError(FRAMEGRABBER_NWC_YARP) << "Failed to open " << local << "port.";
67  }
68 
69  if (!remote.empty()) {
70  yCInfo(FRAMEGRABBER_NWC_YARP) << "Connecting" << port.getName() << "to" << remote;
71  if (!yarp::os::NetworkBase::connect(remote, port.getName(), carrier)) {
72  yCError(FRAMEGRABBER_NWC_YARP) << "Failed to connect" << local << "to" << remote;
73  return false;
74  }
75  } else {
76  yCInfo(FRAMEGRABBER_NWC_YARP) << "No remote specified. Waiting for connection";
77  }
78 
79  reader.attach(port);
80 
81  return true;
82 }
83 
85 {
86  std::lock_guard<std::mutex> lock(m_mutex);
87 
88  m_lastStamp = {0, 0.0};
89  m_lastHeight = 0;
90  m_lastWidth = 0;
91 
92  if (!port.isOpen()) {
93  return true;
94  }
95  port.interrupt();
96  port.close();
97 
98  return true;
99 }
100 // END StreamReceiver
101 
102 
103 // BEGIN FrameGrabberOf_ForwarderWithStream
104 template <typename ImageType,
105  yarp::conf::vocab32_t IfVocab,
106  yarp::conf::vocab32_t ImgVocab>
108  yarp::proto::framegrabber::FrameGrabberOf_Forwarder<ImageType, IfVocab, ImgVocab>(rpcPort)
109 {
110 }
111 
112 template <typename ImageType,
113  yarp::conf::vocab32_t IfVocab,
114  yarp::conf::vocab32_t ImgVocab>
116 {
117  std::lock_guard<std::mutex> lock(m_mutex);
118  if (!m_streamReceiver) {
120  }
121 
122  return m_streamReceiver->lastHeight();
123 }
124 
125 template <typename ImageType,
126  yarp::conf::vocab32_t IfVocab,
127  yarp::conf::vocab32_t ImgVocab>
129 {
130  std::lock_guard<std::mutex> lock(m_mutex);
131  if (!m_streamReceiver) {
133  }
134 
135  return m_streamReceiver->lastWidth();
136 }
137 
138 template <typename ImageType,
139  yarp::conf::vocab32_t IfVocab,
140  yarp::conf::vocab32_t ImgVocab>
142 {
143  std::lock_guard<std::mutex> lock(m_mutex);
144  if (!m_streamReceiver) {
146  }
147 
148  return m_streamReceiver->lastImage(image);
149 }
150 
151 template <typename ImageType,
152  yarp::conf::vocab32_t IfVocab,
153  yarp::conf::vocab32_t ImgVocab>
155  yarp::sig::VectorOf<std::pair<int, int>> vertices,
156  ImageType& image)
157 {
158  std::lock_guard<std::mutex> lock(m_mutex);
159  if(!m_streamReceiver) {
161  }
162 
163  // yarp::dev::IFrameGrabberOf<ImageType>::getImageCrop() calls getImage()
164  // internally, and crops the returned image
165  return yarp::dev::IFrameGrabberOf<ImageType>::getImageCrop(cropType, vertices, image);
166 }
167 
168 
169 template <typename ImageType,
170  yarp::conf::vocab32_t IfVocab,
171  yarp::conf::vocab32_t ImgVocab>
173 {
174  std::lock_guard<std::mutex> lock(m_mutex);
175  m_streamReceiver = streamReceiver;
176 }
177 // END FrameGrabberOf_ForwarderWithStream
178 
179 
180 
181 // BEGIN FrameGrabber_nwc_yarp
183  FrameGrabberOf_ForwarderWithStream<yarp::sig::ImageOf<yarp::sig::PixelRgb>>(rpcPort),
185  FrameGrabberOf_ForwarderWithStream<yarp::sig::ImageOf<yarp::sig::PixelFloat>>(rpcPort),
186  FrameGrabberOf_ForwarderWithStream<yarp::sig::FlexImage>(rpcPort),
187  yarp::proto::framegrabber::FrameGrabberControls_Forwarder(rpcPort),
188  yarp::proto::framegrabber::FrameGrabberControlsDC1394_Forwarder(rpcPort),
189  yarp::proto::framegrabber::RgbVisualParams_Forwarder(rpcPort)
190 {
191 }
192 
193 
195 {
196  std::string remote = config.check("remote", yarp::os::Value(""), "port name of real grabber").asString();
197  std::string local = config.check("local", yarp::os::Value("..."), "port name to use locally").asString();
198  std::string carrier = config.check("stream", yarp::os::Value("tcp"), "carrier to use for streaming").asString();
199  bool no_stream = config.check("no_stream");
200 
201  if (!no_stream) {
202  if (!streamReceiver.open(local, remote, carrier)) {
203  return false;
204  }
205  }
206 
207  std::string rpc_local = local + "/rpc_client";
208  std::string rpc_remote = remote + "/rpc";
209  if (!rpcPort.open(rpc_local)) {
210  yCError(FRAMEGRABBER_NWC_YARP) << "Failed to open " << rpc_local << "port.";
211  }
212 
213  if (!remote.empty()) {
214  yCInfo(FRAMEGRABBER_NWC_YARP) << "Connecting" << rpcPort.getName() << "to" << rpc_remote;
215  if (!yarp::os::NetworkBase::connect(rpcPort.getName(), rpc_remote)) {
216  yCError(FRAMEGRABBER_NWC_YARP) << "Failed to connect" << rpcPort.getName() << "to" << rpc_remote;
217  return false;
218  }
219  } else {
220  yCInfo(FRAMEGRABBER_NWC_YARP) << "No remote specified. Waiting for connection";
221  }
222 
223  return true;
224 }
225 
227 {
228  rpcPort.interrupt();
229  rpcPort.close();
230 
231  streamReceiver.close();
232 
233  return true;
234 }
235 
236 
238 {
239  return streamReceiver.lastStamp();
240 }
241 
242 // END FrameGrabber_nwc_yarp
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_IMAGERAW
Definition: CameraVocabs.h:17
cropType_id_t
bool ret
int width() const override
Return the width of each frame.
FrameGrabberOf_ForwarderWithStream(yarp::os::Port &rpcPort)
int height() const override
Return the height of each frame.
void setStreamReceiver(StreamReceiver *m_streamReceiver)
bool getImage(ImageType &image) override
Get an image from the frame grabber.
bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int >> vertices, ImageType &image) override
Get a crop of the image from the frame grabber.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
yarp::os::Stamp getLastInputStamp() override
Return the time stamp relative to the last acquisition.
bool lastImage(ImageType &image)
yarp::os::Stamp lastStamp() const
bool open(const std::string &local, const std::string &remote, const std::string &carrier)
virtual bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int >> vertices, ImageType &image)
Get a crop of the image from the frame grabber.
virtual std::string getName() const
Get name of port.
Definition: Contactable.cpp:14
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
T * read(bool shouldWait=true) override
Read an available object from the port.
virtual bool getEnvelope(PortReader &envelope)
bool check()
Check if data is available.
void attach(Port &port)
Attach this buffer to a particular port.
T * lastRead() override
Get the last data returned by read()
A mini-server for network communication.
Definition: Port.h:47
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: Port.cpp:374
void close() override
Stop port activity.
Definition: Port.cpp:354
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
bool isOpen() const
Check if the port has been opened.
Definition: Port.cpp:668
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
A single value (typically within a Bottle).
Definition: Value.h:45
int height() const override
Return the height of each frame.
int width() const override
Return the width of each frame.
bool getImage(ImageType &image) override
Get an image from the frame grabber.
bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int >> vertices, ImageType &image) override
Get a crop of the image from the frame grabber.
Provides:
Definition: Vector.h:119
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
std::int32_t vocab32_t
Definition: numeric.h:78
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:447
float PixelFloat
Floating point pixel type.
Definition: Image.h:583
The main, catch-all namespace for YARP.
Definition: dirs.h:16