-
Notifications
You must be signed in to change notification settings - Fork 227
/
image_transport.cpp
228 lines (200 loc) · 7.28 KB
/
image_transport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2009, Willow Garage, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include "image_transport/image_transport.hpp"
#include <memory>
#include <string>
#include <vector>
#include "pluginlib/class_loader.hpp"
#include "image_transport/camera_common.hpp"
#include "image_transport/loader_fwds.hpp"
#include "image_transport/publisher_plugin.hpp"
#include "image_transport/subscriber_plugin.hpp"
namespace image_transport
{
struct Impl
{
PubLoaderPtr pub_loader_;
SubLoaderPtr sub_loader_;
Impl()
: pub_loader_(std::make_shared<PubLoader>("image_transport", "image_transport::PublisherPlugin")),
sub_loader_(std::make_shared<SubLoader>("image_transport", "image_transport::SubscriberPlugin"))
{
}
};
static Impl * kImpl = new Impl();
Publisher create_publisher(
rclcpp::Node * node,
const std::string & base_topic,
rmw_qos_profile_t custom_qos)
{
return Publisher(node, base_topic, kImpl->pub_loader_, custom_qos);
}
Subscriber create_subscription(
rclcpp::Node * node,
const std::string & base_topic,
const Subscriber::Callback & callback,
const std::string & transport,
rmw_qos_profile_t custom_qos,
rclcpp::SubscriptionOptions options)
{
return Subscriber(node, base_topic, callback, kImpl->sub_loader_, transport, custom_qos, options);
}
CameraPublisher create_camera_publisher(
rclcpp::Node * node,
const std::string & base_topic,
rmw_qos_profile_t custom_qos)
{
return CameraPublisher(node, base_topic, custom_qos);
}
CameraSubscriber create_camera_subscription(
rclcpp::Node * node,
const std::string & base_topic,
const CameraSubscriber::Callback & callback,
const std::string & transport,
rmw_qos_profile_t custom_qos)
{
return CameraSubscriber(node, base_topic, callback, transport, custom_qos);
}
std::vector<std::string> getDeclaredTransports()
{
std::vector<std::string> transports = kImpl->sub_loader_->getDeclaredClasses();
// Remove the "_sub" at the end of each class name.
for (std::string & transport : transports) {
transport = erase_last_copy(transport, "_sub");
}
return transports;
}
std::vector<std::string> getLoadableTransports()
{
std::vector<std::string> loadableTransports;
for (const std::string & transportPlugin : kImpl->sub_loader_->getDeclaredClasses() ) {
// If the plugin loads without throwing an exception, add its
// transport name to the list of valid plugins, otherwise ignore
// it.
try {
std::shared_ptr<image_transport::SubscriberPlugin> sub =
kImpl->sub_loader_->createUniqueInstance(transportPlugin);
// Remove the "_sub" at the end of each class name.
loadableTransports.push_back(erase_last_copy(transportPlugin, "_sub"));
} catch (const pluginlib::LibraryLoadException & e) {
(void) e;
} catch (const pluginlib::CreateClassException & e) {
(void) e;
}
}
return loadableTransports;
}
struct ImageTransport::Impl
{
rclcpp::Node::SharedPtr node_;
};
ImageTransport::ImageTransport(rclcpp::Node::SharedPtr node)
: impl_(std::make_unique<ImageTransport::Impl>())
{
impl_->node_ = node;
}
ImageTransport::~ImageTransport() = default;
Publisher ImageTransport::advertise(const std::string & base_topic, uint32_t queue_size, bool latch)
{
// TODO(ros2) implement when resolved: https://github.com/ros2/ros2/issues/464
(void) latch;
rmw_qos_profile_t custom_qos = rmw_qos_profile_default;
custom_qos.depth = queue_size;
return create_publisher(impl_->node_.get(), base_topic, custom_qos);
}
Subscriber ImageTransport::subscribe(
const std::string & base_topic, uint32_t queue_size,
const Subscriber::Callback & callback,
const VoidPtr & tracked_object,
const TransportHints * transport_hints)
{
(void) tracked_object;
rmw_qos_profile_t custom_qos = rmw_qos_profile_default;
custom_qos.depth = queue_size;
return create_subscription(
impl_->node_.get(), base_topic, callback,
getTransportOrDefault(transport_hints), custom_qos);
}
Subscriber ImageTransport::subscribe(
const std::string & base_topic, uint32_t queue_size,
const Subscriber::Callback & callback,
const VoidPtr & tracked_object,
const TransportHints * transport_hints,
const rclcpp::SubscriptionOptions options)
{
(void) tracked_object;
rmw_qos_profile_t custom_qos = rmw_qos_profile_default;
custom_qos.depth = queue_size;
return create_subscription(
impl_->node_.get(), base_topic, callback,
getTransportOrDefault(transport_hints), custom_qos,
options);
}
CameraPublisher ImageTransport::advertiseCamera(
const std::string & base_topic, uint32_t queue_size,
bool latch)
{
// TODO(ros2) implement when resolved: https://github.com/ros2/ros2/issues/464
(void) latch;
rmw_qos_profile_t custom_qos = rmw_qos_profile_default;
custom_qos.depth = queue_size;
return create_camera_publisher(impl_->node_.get(), base_topic, custom_qos);
}
CameraSubscriber ImageTransport::subscribeCamera(
const std::string & base_topic, uint32_t queue_size,
const CameraSubscriber::Callback & callback,
const VoidPtr & tracked_object,
const TransportHints * transport_hints)
{
(void) tracked_object;
rmw_qos_profile_t custom_qos = rmw_qos_profile_default;
custom_qos.depth = queue_size;
return create_camera_subscription(
impl_->node_.get(), base_topic, callback,
getTransportOrDefault(transport_hints), custom_qos);
}
std::vector<std::string> ImageTransport::getDeclaredTransports() const
{
return image_transport::getDeclaredTransports();
}
std::vector<std::string> ImageTransport::getLoadableTransports() const
{
return image_transport::getLoadableTransports();
}
std::string ImageTransport::getTransportOrDefault(const TransportHints * transport_hints)
{
std::string ret;
if (nullptr == transport_hints) {
TransportHints th(impl_->node_.get());
ret = th.getTransport();
} else {
ret = transport_hints->getTransport();
}
return ret;
}
} // namespace image_transport