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
8#include <yarp/os/Network.h>
10#include <yarp/os/LogStream.h>
11
12
13namespace {
14YARP_LOG_COMPONENT(FRAMEGRABBER_NWC_YARP, "yarp.devices.frameGrabber_nwc_yarp")
15} // namespace
16
17using namespace yarp::dev;
18using namespace yarp::sig;
19
21{
22 return m_lastHeight;
23}
24
26{
27 return m_lastWidth;
28}
29
31{
32 return m_lastStamp;
33}
34
35template <typename ImageType>
37{
38 std::lock_guard<std::mutex> lock(m_mutex);
39
40 bool ret {false};
41 if (port.isClosed()) {
42 image.zero();
43 ret = false;
44 } else if (port.getPendingReads() > 0) {
45 yarp::sig::FlexImage* received = port.read(true);
46 if (received != nullptr) {
47 ret = image.copy(*received);
48 port.getEnvelope(m_lastStamp);
49 m_lastHeight = image.height();
50 m_lastWidth = image.width();
51 }
52 } else if (yarp::sig::FlexImage* last = port.lastRead(); last != nullptr) {
53 ret = image.copy(*last);
54 }
55
56 return ret;
57}
58
59bool 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 return true;
80}
81
83{
84 std::lock_guard<std::mutex> lock(m_mutex);
85
86 m_lastStamp = {0, 0.0};
87 m_lastHeight = 0;
88 m_lastWidth = 0;
89
90 if (port.isClosed()) {
91 return true;
92 }
93 port.interrupt();
94 port.close();
95
96 return true;
97}
98
99// BEGIN Template instances
100template bool StreamReceiver::lastImage<ImageOf<PixelRgb>>(ImageOf<PixelRgb>& image);
101template bool StreamReceiver::lastImage<ImageOf<PixelMono>>(ImageOf<PixelMono>& image);
102template bool StreamReceiver::lastImage<ImageOf<PixelFloat>>(ImageOf<PixelFloat>& image);
103template bool StreamReceiver::lastImage<FlexImage>(FlexImage& image);
104// END Template instances
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)
std::string getName() const override
Get name of port.
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
int getPendingReads() override
Get the number of objects ready to be read.
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool isClosed() override
Returns whether the port associated with this reader has been closed.
T * read(bool shouldWait=true) override
Read an available object from the port.
T * lastRead() override
Get the last data returned by read()
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:784
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:18
Image class with user control of representation details.
Definition Image.h:361
bool copy(const Image &alt)
Copy operator.
Definition Image.cpp:611
#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.