YARP
Yet Another Robot Platform
H264Carrier.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "H264Carrier.h"
10 #include "H264Stream.h"
11 #include "H264LogComponent.h"
12 #include <yarp/os/Contact.h>
13 #include <yarp/os/impl/FakeFace.h>
14 #include <yarp/os/Name.h>
16 #include <cstdio>
17 
18 
19 using namespace yarp::os;
20 using namespace yarp::sig;
21 
22 
23 
24 std::string H264Carrier::getName() const
25 {
26  return "h264";
27 }
28 
30 {
31  return true;
32 }
33 
35 {
36  return true;
37 }
38 
40 {
41  return true;
42 }
43 
45 {
46  return false;
47 }
48 
50 {
51  return false;
52 }
53 
54 void H264Carrier::handleEnvelope(const std::string& envelope)
55 {
56  this->envelope = envelope;
57 }
58 
60 {
61  return false;
62 }
63 
65 {
66  return false;
67 }
68 
70 {
71  return false;
72 }
73 
74 // this is important - flips expected flow of messages
75 bool H264Carrier::isPush() const
76 {
77  return false;
78 }
79 
80 std::string H264Carrier::toString() const
81 {
82  return "h264_carrier";
83 }
84 
85 void H264Carrier::getHeader(Bytes& header) const
86 {
87 }
88 
89 bool H264Carrier::checkHeader(const Bytes& header)
90 {
91  return true;
92 }
93 
94 void H264Carrier::setParameters(const Bytes& header)
95 {
96  // no parameters - no carrier variants
97  yCTrace(H264CARRIER, "setParameters");
98 }
99 
100 
101 static int getIntParam(Name &n, const char *param)
102 {
103  bool hasField;
104  std::string strValue = n.getCarrierModifier(param, &hasField);
105  Value *v = Value::makeValue(strValue);
106  int intvalue = 0;
107  if((hasField) && v->isInt32())
108  {
109  intvalue = v->asInt32();
110  }
111 
112  delete v;
113  return intvalue;
114 }
115 
116 // Now, the initial hand-shaking
118 {
119  //get all parameters of this carrier
120  Name n(proto.getRoute().getCarrierName() + "://test");
121 
122  cfgParams.crop.left = getIntParam(n, "cropLeft");
123  cfgParams.crop.right = getIntParam(n, "cropRight");
124  cfgParams.crop.top = getIntParam(n, "cropTop");
125  cfgParams.crop.bottom = getIntParam(n, "cropBottom");
126  cfgParams.fps_max = getIntParam(n, "max_fps");
127  cfgParams.removeJitter = (getIntParam(n, "removeJitter") > 0) ? true : false;
128  return true;
129 }
130 
132 {
133  yCTrace(H264CARRIER, "sendHeader");
134  return true;
135 }
136 
138 {
139  yCTrace(H264CARRIER, "expectSenderSpecifier");
140  return true;
141 }
142 
144 {
145  yCTrace(H264CARRIER, "expectExtraHeader");
146  return true;
147 }
148 
150 {
151  yCTrace(H264CARRIER, "respondToHeader");
152  return true;
153 }
154 
156 {
157  // I'm the receiver...
158 
159  cfgParams.remotePort = proto.getRoute().getToContact().getPort();
160 
161  auto* stream = new H264Stream(cfgParams);
162  if (stream==nullptr) { return false; }
163 
164  yarp::os::Contact remote = proto.getStreams().getRemoteAddress();
165  bool ok = stream->open(remote);
166 
167  //std::cout << "Remote contact info: host=" << proto.getRoute().getToContact().getHost() << " port= " << proto.getRoute().getToContact().getPort() <<std::endl;
168  if (!ok)
169  {
170  delete stream;
171  return false;
172  }
173  stream->start();
174 
175  proto.takeStreams(stream);
176  return true;
177 }
178 
180 {
181  return true;
182 }
183 
185 {
186  //I should not be here: the carried doesn't perform writing
187  return false;
188 }
189 
191 {
192  return false;
193 }
194 
196 {
197  return true;
198 }
199 
201 {
202  return true;
203 }
204 
206 {
207  return true;
208 }
209 
211 {
212  return true;
213 }
214 
216 {
217  return {};
218 }
219 
221 {
222  return new yarp::os::impl::FakeFace();
223 }
static int getIntParam(Name &n, const char *param)
const yarp::os::LogComponent & H264CARRIER()
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: H264Carrier.cpp:49
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: H264Carrier.cpp:24
bool write(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Write a message.
bool expectSenderSpecifier(yarp::os::ConnectionState &proto) override
Expect the name of the sending port.
void handleEnvelope(const std::string &envelope) override
Carriers that do not distinguish data from administrative headers (i.e.
Definition: H264Carrier.cpp:54
bool isPush() const override
Check if carrier is "push" or "pull" style.
Definition: H264Carrier.cpp:75
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
virtual bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer)
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: H264Carrier.cpp:29
bool expectExtraHeader(yarp::os::ConnectionState &proto) override
Receive any carrier-specific header.
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: H264Carrier.cpp:44
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition: H264Carrier.cpp:64
std::string toString() const override
Get name of carrier.
Definition: H264Carrier.cpp:80
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: H264Carrier.cpp:94
bool prepareSend(yarp::os::ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
bool isLocal() const override
Check if carrier operates within a single process.
Definition: H264Carrier.cpp:69
bool isActive() const override
Check if carrier is alive and error free.
void getHeader(yarp::os::Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: H264Carrier.cpp:85
bool reply(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition: H264Carrier.cpp:39
std::string getBootstrapCarrierName() const override
Get the name of the carrier that should be used prior to handshaking, if a port is registered with th...
bool sendHeader(yarp::os::ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: H264Carrier.cpp:59
bool checkHeader(const yarp::os::Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
Definition: H264Carrier.cpp:89
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition: H264Carrier.cpp:34
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
yarp::os::Face * createFace() const override
Create new Face object that the carrier needs.
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
The basic state of a connection - route, streams in use, etc.
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
virtual const Route & getRoute() const =0
Get the route associated with this connection.
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:242
The initial point-of-contact with a port.
Definition: Face.h:24
Simple abstraction for a YARP port name.
Definition: Name.h:22
std::string getCarrierModifier(const char *mod, bool *hasModifier=nullptr)
Definition: Name.cpp:47
const std::string & getCarrierName() const
Get the carrier type of the route.
Definition: Route.cpp:126
const Contact & getToContact() const
Get the destination contact of the route, if available.
Definition: Route.cpp:116
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
A single value (typically within a Bottle).
Definition: Value.h:47
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
Definition: Value.cpp:465
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Definition: Value.cpp:135
A dummy Face for testing purposes.
Definition: FakeFace.h:24
#define yCTrace(component,...)
Definition: LogComponent.h:88
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25