YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
StreamReceiver.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025-2025 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "StreamReceiver.h"
7
9#include <yarp/os/LogStream.h>
10
11
12namespace {
13YARP_LOG_COMPONENT(FRAMEGRABBER_NWC_YARP, "yarp.devices.frameGrabber_nwc_yarp")
14} // namespace
15
16using namespace yarp::dev;
17using namespace yarp::sig;
18
20{
21 return m_lastHeight;
22}
23
25{
26 return m_lastWidth;
27}
28
30{
31 return m_lastStamp;
32}
33
34template <typename ImageType>
36{
37 std::lock_guard<std::mutex> lock(m_mutex);
38
39 bool ret {false};
40 if (!port.isOpen()) {
41 image.zero();
42 ret = false;
43 } else if (reader.check()) {
44 ret = image.copy(*(reader.read(true)));
45 reader.getEnvelope(m_lastStamp);
46 m_lastHeight = image.height();
47 m_lastWidth = image.width();
48 } else {
49 ret = image.copy(*(reader.lastRead()));
50 }
51
52 return ret;
53}
54
55bool StreamReceiver::open(const std::string& local,
56 const std::string& remote,
57 const std::string& carrier)
58{
59 std::lock_guard<std::mutex> lock(m_mutex);
60
61 if (!port.open(local)) {
62 yCError(FRAMEGRABBER_NWC_YARP) << "Failed to open " << local << "port.";
63 }
64
65 if (!remote.empty()) {
66 yCInfo(FRAMEGRABBER_NWC_YARP) << "Connecting" << port.getName() << "to" << remote;
67 if (!yarp::os::NetworkBase::connect(remote, port.getName(), carrier)) {
68 yCError(FRAMEGRABBER_NWC_YARP) << "Failed to connect" << local << "to" << remote;
69 return false;
70 }
71 } else {
72 yCInfo(FRAMEGRABBER_NWC_YARP) << "No remote specified. Waiting for connection";
73 }
74
75 reader.attach(port);
76
77 return true;
78}
79
81{
82 std::lock_guard<std::mutex> lock(m_mutex);
83
84 m_lastStamp = {0, 0.0};
85 m_lastHeight = 0;
86 m_lastWidth = 0;
87
88 if (!port.isOpen()) {
89 return true;
90 }
91 port.interrupt();
92 port.close();
93
94 return true;
95}
96
97// BEGIN Template instances
98template bool StreamReceiver::lastImage<ImageOf<PixelRgb>>(ImageOf<PixelRgb>& image);
99template bool StreamReceiver::lastImage<ImageOf<PixelMono>>(ImageOf<PixelMono>& image);
100template bool StreamReceiver::lastImage<ImageOf<PixelFloat>>(ImageOf<PixelFloat>& image);
101template bool StreamReceiver::lastImage<FlexImage>(FlexImage& image);
102// END Template instances
bool ret
int lastHeight() const
int lastWidth() const
yarp::os::Stamp lastStamp() const
bool open(const std::string &local, const std::string &remote, const std::string &carrier)
bool lastImage(ImageType &image)
virtual std::string getName() const
Get name of port.
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()
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:347
void close() override
Stop port activity.
Definition Port.cpp:330
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:631
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
Image class with user control of representation details.
Definition Image.h:361
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.