YARP
Yet Another Robot Platform
H264Carrier.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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 }
H264Stream.h
yarp::os::impl::FakeFace
A dummy Face for testing purposes.
Definition: FakeFace.h:24
H264Carrier::setParameters
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: H264Carrier.cpp:94
yarp::os::Route::getCarrierName
const std::string & getCarrierName() const
Get the carrier type of the route.
Definition: Route.cpp:126
yarp::sig
Signal processing.
Definition: Image.h:25
H264Carrier::supportReply
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition: H264Carrier.cpp:64
H264Carrier::createFace
yarp::os::Face * createFace() const override
Create new Face object that the carrier needs.
Definition: H264Carrier.cpp:220
H264Carrier::expectReplyToHeader
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: H264Carrier.cpp:155
FakeFace.h
H264Carrier::isLocal
bool isLocal() const override
Check if carrier operates within a single process.
Definition: H264Carrier.cpp:69
Name.h
H264Carrier::expectAck
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition: H264Carrier.cpp:210
H264Carrier::handleEnvelope
void handleEnvelope(const std::string &envelope) override
Carriers that do not distinguish data from administrative headers (i.e.
Definition: H264Carrier.cpp:54
yarp::os::ConnectionState::takeStreams
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
yarp::os::ConnectionState::getRoute
virtual const Route & getRoute() const =0
Get the route associated with this connection.
H264Stream
Definition: H264Stream.h:22
ConnectionState.h
yarp::os::Contact::getPort
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:242
H264Carrier::isActive
bool isActive() const override
Check if carrier is alive and error free.
Definition: H264Carrier.cpp:179
H264Carrier::getBootstrapCarrierName
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...
Definition: H264Carrier.cpp:215
H264Carrier::expectSenderSpecifier
bool expectSenderSpecifier(yarp::os::ConnectionState &proto) override
Expect the name of the sending port.
Definition: H264Carrier.cpp:137
yarp::os::Name::getCarrierModifier
std::string getCarrierModifier(const char *mod, bool *hasModifier=nullptr)
Definition: Name.cpp:47
yarp::os::TwoWayStream::getRemoteAddress
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
yarp::os::Name
Simple abstraction for a YARP port name.
Definition: Name.h:22
H264Carrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: H264Carrier.cpp:59
H264LogComponent.h
H264Carrier::sendAck
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition: H264Carrier.cpp:205
yarp::os::ConnectionState::getStreams
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
H264Carrier.h
H264Carrier::canEscape
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: H264Carrier.cpp:49
getIntParam
static int getIntParam(Name &n, const char *param)
Definition: H264Carrier.cpp:101
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
yarp::os::Value::makeValue
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
Definition: Value.cpp:465
yarp::os::Value::asInt32
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
H264Carrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: H264Carrier.cpp:29
H264Carrier::write
bool write(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Write a message.
Definition: H264Carrier.cpp:184
H264Carrier::prepareSend
bool prepareSend(yarp::os::ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
Definition: H264Carrier.cpp:117
H264Carrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: H264Carrier.cpp:24
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
H264Carrier::getHeader
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
H264Carrier::isTextMode
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: H264Carrier.cpp:44
yarp::os::Route::getToContact
const Contact & getToContact() const
Get the destination contact of the route, if available.
Definition: Route.cpp:116
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
H264Carrier::canOffer
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition: H264Carrier.cpp:39
H264Carrier::reply
bool reply(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Definition: H264Carrier.cpp:190
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
H264Carrier::toString
std::string toString() const override
Get name of carrier.
Definition: H264Carrier.cpp:80
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
Contact.h
H264Carrier::isPush
bool isPush() const override
Check if carrier is "push" or "pull" style.
Definition: H264Carrier.cpp:75
H264Carrier::sendIndex
virtual bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer)
Definition: H264Carrier.cpp:195
yarp::os::Face
The initial point-of-contact with a port.
Definition: Face.h:24
H264Carrier::respondToHeader
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
Definition: H264Carrier.cpp:149
H264Carrier::checkHeader
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
H264Carrier::canAccept
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition: H264Carrier.cpp:34
H264Carrier::sendHeader
bool sendHeader(yarp::os::ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition: H264Carrier.cpp:131
H264CARRIER
const yarp::os::LogComponent & H264CARRIER()
Definition: H264LogComponent.cpp:16
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
H264Carrier::expectExtraHeader
bool expectExtraHeader(yarp::os::ConnectionState &proto) override
Receive any carrier-specific header.
Definition: H264Carrier.cpp:143
H264Carrier::expectIndex
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: H264Carrier.cpp:200
yarp::os::Value::isInt32
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Definition: Value.cpp:135