Skip to content

Commit 0e04d4c

Browse files
committed
support new image transport node interface
1 parent 8c03e83 commit 0e04d4c

File tree

8 files changed

+109
-61
lines changed

8 files changed

+109
-61
lines changed

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,27 @@ find_package(sensor_msgs REQUIRED)
3737
find_package(std_msgs REQUIRED)
3838

3939

40+
if(${image_transport_VERSION} VERSION_GREATER_EQUAL "3.1.0")
41+
# Humble and later (formerly API_V2)
42+
# add_definitions(-DIMAGE_TRANSPORT_API_V2)
43+
endif()
44+
45+
if(${image_transport_VERSION} VERSION_GREATER_EQUAL "5.1.7")
46+
# Jazzy and later (formerly API_V3)
47+
add_definitions(-DIMAGE_TRANSPORT_USE_PUBLISHER_T)
48+
add_definitions(-DIMAGE_TRANSPORT_NEEDS_PUBLISHEROPTIONS)
49+
# add_definitions(-DIMAGE_TRANSPORT_API_V3)
50+
endif()
51+
4052
if(${image_transport_VERSION} VERSION_GREATER_EQUAL "6.0.0")
4153
add_definitions(-DIMAGE_TRANSPORT_RESOLVES_BASE_TOPIC)
4254
endif()
4355
if(${image_transport_VERSION} VERSION_GREATER_EQUAL "6.3.0")
4456
add_definitions(-DIMAGE_TRANSPORT_USE_QOS)
4557
endif()
58+
if(${image_transport_VERSION} VERSION_GREATER_EQUAL "6.4.0")
59+
add_definitions(-DIMAGE_TRANSPORT_USE_NODEINTERFACE)
60+
endif()
4661

4762
set(LIBRARY_NAME ${PROJECT_NAME}_component)
4863

include/ffmpeg_image_transport/ffmpeg_publisher.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,33 @@ using FFMPEGPacketConstPtr = FFMPEGPacket::ConstSharedPtr;
3232
class FFMPEGPublisher : public FFMPEGPublisherPlugin
3333
{
3434
public:
35-
#if defined(IMAGE_TRANSPORT_API_V1) || defined(IMAGE_TRANSPORT_API_V2)
36-
using PublisherTFn = PublishFn;
37-
#else
35+
#ifdef IMAGE_TRANSPORT_USE_PUBLISHER_T
3836
using PublisherTFn = PublisherT;
37+
#else
38+
using PublisherTFn = PublishFn;
3939
#endif
4040
#ifdef IMAGE_TRANSPORT_USE_QOS
4141
using QoSType = rclcpp::QoS;
4242
#else
4343
using QoSType = rmw_qos_profile_t;
4444
#endif
45+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
46+
using NodeType = image_transport::RequiredInterfaces;
47+
#else
48+
using NodeType = rclcpp::Node *;
49+
#endif
50+
4551
FFMPEGPublisher();
4652
~FFMPEGPublisher() override;
4753
std::string getTransportName() const override { return "ffmpeg"; }
4854

4955
protected:
50-
#if defined(IMAGE_TRANSPORT_API_V1) || defined(IMAGE_TRANSPORT_API_V2)
56+
#ifdef IMAGE_TRANSPORT_NEEDS_PUBLISHEROPTIONS
5157
void advertiseImpl(
52-
rclcpp::Node * node, const std::string & base_topic, QoSType custom_qos) override;
53-
#else
54-
void advertiseImpl(
55-
rclcpp::Node * node, const std::string & base_topic, QoSType custom_qos,
58+
NodeType node, const std::string & base_topic, QoSType custom_qos,
5659
rclcpp::PublisherOptions opt) override;
60+
#else
61+
void advertiseImpl(NodeType node, const std::string & base_topic, QoSType custom_qos) override;
5762
#endif
5863
void publish(const Image & message, const PublisherTFn & publisher) const override;
5964
void shutdown() override;
@@ -63,8 +68,8 @@ class FFMPEGPublisher : public FFMPEGPublisherPlugin
6368
const std::string & frame_id, const rclcpp::Time & stamp, const std::string & codec,
6469
uint32_t width, uint32_t height, uint64_t pts, uint8_t flags, uint8_t * data, size_t sz);
6570

66-
QoSType initialize(rclcpp::Node * node, const std::string & base_name, QoSType custom_qos);
67-
void declareParameter(rclcpp::Node * node, const ParameterDefinition & definition);
71+
QoSType initialize(NodeType node, const std::string & base_name, QoSType custom_qos);
72+
void declareParameter(NodeType node, const ParameterDefinition & definition);
6873
void handleAVOptions(const std::string & opt);
6974
// variables ---------
7075
rclcpp::Logger logger_;

include/ffmpeg_image_transport/ffmpeg_subscriber.hpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,25 @@
2424

2525
namespace ffmpeg_image_transport
2626
{
27-
using Image = sensor_msgs::msg::Image;
28-
using ImageConstPtr = Image::ConstSharedPtr;
29-
using ffmpeg_image_transport_msgs::msg::FFMPEGPacket;
30-
using FFMPEGPacketConstPtr = FFMPEGPacket::ConstSharedPtr;
3127
using FFMPEGSubscriberPlugin = image_transport::SimpleSubscriberPlugin<FFMPEGPacket>;
28+
class FFMPEGSubscriber : public FFMPEGSubscriberPlugin
29+
{
30+
public:
31+
using Image = sensor_msgs::msg::Image;
32+
using ImageConstPtr = Image::ConstSharedPtr;
33+
using FFMPEGPacket = ffmpeg_image_transport_msgs::msg::FFMPEGPacket;
34+
using FFMPEGPacketConstPtr = FFMPEGPacket::ConstSharedPtr;
3235
#ifdef IMAGE_TRANSPORT_USE_QOS
33-
using QoSType = rclcpp::QoS;
36+
using QoSType = rclcpp::QoS;
3437
#else
35-
using QoSType = rmw_qos_profile_t;
38+
using QoSType = rmw_qos_profile_t;
39+
#endif
40+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
41+
using NodeType = image_transport::RequiredInterfaces;
42+
#else
43+
using NodeType = rclcpp::Node *;
3644
#endif
3745

38-
class FFMPEGSubscriber : public FFMPEGSubscriberPlugin
39-
{
40-
public:
4146
FFMPEGSubscriber();
4247
~FFMPEGSubscriber() override;
4348

@@ -46,26 +51,22 @@ class FFMPEGSubscriber : public FFMPEGSubscriberPlugin
4651
protected:
4752
void internalCallback(const FFMPEGPacketConstPtr & msg, const Callback & user_cb) override;
4853

49-
#ifdef IMAGE_TRANSPORT_API_V1
50-
void subscribeImpl(
51-
rclcpp::Node * node, const std::string & base_topic, const Callback & callback,
52-
QoSType custom_qos) override;
53-
#else
5454
void subscribeImpl(
55-
rclcpp::Node * node, const std::string & base_topic, const Callback & callback,
56-
QoSType custom_qos, rclcpp::SubscriptionOptions) override;
57-
#endif
55+
NodeType node, const std::string & base_topic, const Callback & callback, QoSType custom_qos,
56+
rclcpp::SubscriptionOptions) override;
57+
5858
void shutdown() override;
59+
rclcpp::Logger logger_;
60+
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_param_interface_;
5961

6062
private:
6163
void frameReady(const ImageConstPtr & img, bool /*isKeyFrame*/) const;
62-
void initialize(rclcpp::Node * node, const std::string & base_topic);
64+
void initialize(NodeType node, const std::string & base_topic);
6365
std::string getDecodersFromMap(const std::string & encoding);
64-
void declareParameter(rclcpp::Node * node, const ParameterDefinition & definition);
66+
void declareParameter(NodeType node, const ParameterDefinition & definition);
6567
void handleAVOptions(const std::string & opt);
6668
// -------------- variables
67-
rclcpp::Logger logger_;
68-
rclcpp::Node * node_{nullptr};
69+
NodeType node_;
6970
ffmpeg_encoder_decoder::Decoder decoder_;
7071
std::string decoderType_;
7172
const Callback * userCallback_{nullptr};

include/ffmpeg_image_transport/parameter_definition.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef FFMPEG_IMAGE_TRANSPORT__PARAMETER_DEFINITION_HPP_
1717
#define FFMPEG_IMAGE_TRANSPORT__PARAMETER_DEFINITION_HPP_
1818

19+
#include <image_transport/image_transport.hpp>
1920
#include <rclcpp/rclcpp.hpp>
2021

2122
namespace ffmpeg_image_transport
@@ -24,8 +25,13 @@ struct ParameterDefinition
2425
{
2526
using ParameterDescriptor = rcl_interfaces::msg::ParameterDescriptor;
2627
using ParameterValue = rclcpp::ParameterValue;
28+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
29+
using NodeType = image_transport::RequiredInterfaces;
30+
#else
31+
using NodeType = rclcpp::Node *;
32+
#endif
2733

28-
rclcpp::ParameterValue declare(rclcpp::Node * node, const std::string & paramBase) const;
34+
rclcpp::ParameterValue declare(NodeType node, const std::string & paramBase) const;
2935
// ---- variables
3036
ParameterValue defaultValue;
3137
ParameterDescriptor descriptor;

src/ffmpeg_publisher.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void FFMPEGPublisher::shutdown()
101101

102102
// This code was lifted from compressed_image_transport
103103

104-
void FFMPEGPublisher::declareParameter(rclcpp::Node * node, const ParameterDefinition & definition)
104+
void FFMPEGPublisher::declareParameter(NodeType node, const ParameterDefinition & definition)
105105
{
106106
// transport scoped parameter (e.g. image_raw.compressed.format)
107107
const auto v = definition.declare(node, paramNamespace_);
@@ -163,31 +163,34 @@ void FFMPEGPublisher::packetReady(
163163
#endif
164164
}
165165

166-
#if defined(IMAGE_TRANSPORT_API_V1) || defined(IMAGE_TRANSPORT_API_V2)
166+
#ifdef IMAGE_TRANSPORT_NEEDS_PUBLISHEROPTIONS
167167
void FFMPEGPublisher::advertiseImpl(
168-
rclcpp::Node * node, const std::string & base_topic, rmw_qos_profile_t custom_qos)
168+
NodeType node, const std::string & base_topic, QoSType custom_qos, rclcpp::PublisherOptions opt)
169169
{
170170
auto qos = initialize(node, base_topic, custom_qos);
171-
FFMPEGPublisherPlugin::advertiseImpl(node, base_topic, qos);
171+
FFMPEGPublisherPlugin::advertiseImpl(node, base_topic, qos, opt);
172172
}
173173
#else
174174
void FFMPEGPublisher::advertiseImpl(
175-
rclcpp::Node * node, const std::string & base_topic, QoSType custom_qos,
176-
rclcpp::PublisherOptions opt)
175+
NodeType node, const std::string & base_topic, rmw_qos_profile_t custom_qos)
177176
{
178177
auto qos = initialize(node, base_topic, custom_qos);
179-
FFMPEGPublisherPlugin::advertiseImpl(node, base_topic, qos, opt);
178+
FFMPEGPublisherPlugin::advertiseImpl(node, base_topic, qos);
180179
}
181180
#endif
182181

183182
FFMPEGPublisher::QoSType FFMPEGPublisher::initialize(
184-
rclcpp::Node * node, const std::string & base_topic, QoSType custom_qos)
183+
NodeType node, const std::string & base_topic, QoSType custom_qos)
185184
{
186185
// namespace handling code lifted from compressed_image_transport
186+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
187+
uint ns_prefix_len = std::string(node.get_node_base_interface()->get_namespace()).length();
188+
#else
187189
uint ns_len = node->get_effective_namespace().length();
188190
// if a namespace is given (ns_len > 1), then strip one more
189191
// character to avoid a leading "/" that will then become a "."
190192
uint ns_prefix_len = ns_len > 1 ? ns_len + 1 : ns_len;
193+
#endif
191194
std::string param_base_name = base_topic.substr(ns_prefix_len);
192195
std::replace(param_base_name.begin(), param_base_name.end(), '/', '.');
193196
paramNamespace_ = param_base_name + "." + getTransportName() + ".";

src/ffmpeg_subscriber.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ static const ParameterDefinition params[] = {
4040
.set__description("enable performance timing")
4141
.set__read_only(false)}};
4242

43-
FFMPEGSubscriber::FFMPEGSubscriber() : logger_(rclcpp::get_logger("FFMPEGSubscriber")) {}
43+
FFMPEGSubscriber::FFMPEGSubscriber() : logger_(rclcpp::get_logger("FFMPEGSubscriber"))
44+
{
45+
#ifndef IMAGE_TRANSPORT_USE_NODEINTERFACE
46+
node_ = nullptr;
47+
#endif
48+
}
4449

4550
FFMPEGSubscriber::~FFMPEGSubscriber() { decoder_.reset(); }
4651

@@ -56,29 +61,20 @@ void FFMPEGSubscriber::shutdown()
5661

5762
void FFMPEGSubscriber::frameReady(const ImageConstPtr & img, bool) const { (*userCallback_)(img); }
5863

59-
#ifdef IMAGE_TRANSPORT_API_V1
6064
void FFMPEGSubscriber::subscribeImpl(
61-
rclcpp::Node * node, const std::string & base_topic, const Callback & callback,
62-
rmw_qos_profile_t custom_qos)
65+
NodeType node, const std::string & base_topic, const Callback & callback, QoSType custom_qos,
66+
rclcpp::SubscriptionOptions opt)
6367
{
6468
initialize(node, base_topic);
65-
FFMPEGSubscriberPlugin::subscribeImpl(node, base_topic, callback, custom_qos);
66-
}
69+
#ifdef IMAGE_TRANSPORT_NEEDS_PUBLISHEROPTIONS
70+
FFMPEGSubscriberPlugin::subscribeImpl(node, base_topic, callback, custom_qos, opt);
6771
#else
68-
void FFMPEGSubscriber::subscribeImpl(
69-
rclcpp::Node * node, const std::string & base_topic, const Callback & callback,
70-
QoSType custom_qos, rclcpp::SubscriptionOptions opt)
71-
{
72-
initialize(node, base_topic);
73-
#ifdef IMAGE_TRANSPORT_API_V2
7472
(void)opt; // to suppress compiler warning
7573
FFMPEGSubscriberPlugin::subscribeImpl(node, base_topic, callback, custom_qos);
76-
#else
77-
FFMPEGSubscriberPlugin::subscribeImpl(node, base_topic, callback, custom_qos, opt);
7874
#endif
7975
}
80-
#endif
81-
void FFMPEGSubscriber::initialize(rclcpp::Node * node, const std::string & base_topic_o)
76+
77+
void FFMPEGSubscriber::initialize(NodeType node, const std::string & base_topic_o)
8278
{
8379
node_ = node;
8480
#ifdef IMAGE_TRANSPORT_RESOLVES_BASE_TOPIC
@@ -87,10 +83,15 @@ void FFMPEGSubscriber::initialize(rclcpp::Node * node, const std::string & base_
8783
const std::string base_topic =
8884
node_->get_node_topics_interface()->resolve_topic_name(base_topic_o);
8985
#endif
90-
uint ns_len = node_->get_effective_namespace().length();
86+
87+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
88+
uint ns_prefix_len = std::string(node.get_node_base_interface()->get_namespace()).length();
89+
#else
90+
uint ns_len = node->get_effective_namespace().length();
9191
// if a namespace is given (ns_len > 1), then strip one more
9292
// character to avoid a leading "/" that will then become a "."
9393
uint ns_prefix_len = ns_len > 1 ? ns_len + 1 : ns_len;
94+
#endif
9495
std::string param_base_name = base_topic.substr(ns_prefix_len);
9596
std::replace(param_base_name.begin(), param_base_name.end(), '/', '.');
9697
paramNamespace_ = param_base_name + "." + getTransportName() + ".";
@@ -100,7 +101,7 @@ void FFMPEGSubscriber::initialize(rclcpp::Node * node, const std::string & base_
100101
}
101102
}
102103

103-
void FFMPEGSubscriber::declareParameter(rclcpp::Node * node, const ParameterDefinition & definition)
104+
void FFMPEGSubscriber::declareParameter(NodeType node, const ParameterDefinition & definition)
104105
{
105106
const auto v = definition.declare(node, paramNamespace_);
106107
const auto & n = definition.descriptor.name;

src/parameter_definition.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,29 @@
1818
namespace ffmpeg_image_transport
1919
{
2020
rclcpp::ParameterValue ParameterDefinition::declare(
21-
rclcpp::Node * node, const std::string & paramBase) const
21+
NodeType node, const std::string & paramBase) const
2222
{
2323
// transport scoped parameter (e.g. image_raw.compressed.format)
2424
const std::string paramName = paramBase + descriptor.name;
2525
rclcpp::ParameterValue v;
26+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
27+
try {
28+
v =
29+
node.get_node_parameters_interface()->declare_parameter(paramName, defaultValue, descriptor);
30+
} catch (const rclcpp::exceptions::ParameterAlreadyDeclaredException &) {
31+
RCLCPP_DEBUG(
32+
node.get_node_logging_interface()->get_logger(), "%s was previously declared",
33+
descriptor.name.c_str());
34+
v = node.get_node_parameters_interface()->get_parameter(paramName).get_parameter_value();
35+
}
36+
#else
2637
try {
2738
v = node->declare_parameter(paramName, defaultValue, descriptor);
2839
} catch (const rclcpp::exceptions::ParameterAlreadyDeclaredException &) {
2940
RCLCPP_DEBUG(node->get_logger(), "%s was previously declared", descriptor.name.c_str());
3041
v = node->get_parameter(paramName).get_parameter_value();
3142
}
43+
#endif
3244
return (v);
3345
}
3446
} // namespace ffmpeg_image_transport

test/test_1.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ class TestSubscriber : public rclcpp::Node
128128
}
129129
void initialize()
130130
{
131-
image_transport::TransportHints hints(this);
131+
// image_transport::TransportHints hints(this->get_node_interface());
132132
sub_ = std::make_shared<image_transport::Subscriber>(image_transport::create_subscription(
133-
this, "camera/image", std::bind(&TestSubscriber::imageCallback, this, std::placeholders::_1),
133+
#ifdef IMAGE_TRANSPORT_USE_NODEINTERFACE
134+
*this,
135+
#else
136+
this,
137+
#endif
138+
"camera/image", std::bind(&TestSubscriber::imageCallback, this, std::placeholders::_1),
134139
"ffmpeg", convert_profile(rmw_qos_profile_default)));
135140
}
136141
void shutDown()

0 commit comments

Comments
 (0)