Skip to content

Commit 3437fee

Browse files
committed
List snapshot streamers
1 parent 90f3ed1 commit 3437fee

7 files changed

Lines changed: 96 additions & 17 deletions

File tree

include/web_video_server/base_image_transport_streamer.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,10 @@ class BaseImageTransportStreamerFactory : public BaseImageStreamerFactory
8888
virtual std::vector<std::string> get_available_topics(rclcpp::Node::SharedPtr node);
8989
};
9090

91+
class BaseImageTransportSnapshotStreamerFactory : public BaseSnapshotStreamerFactory
92+
{
93+
public:
94+
virtual std::vector<std::string> get_available_topics(rclcpp::Node::SharedPtr node);
95+
};
96+
9197
} // namespace web_video_server

include/web_video_server/streamers/jpeg_streamers.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class JpegSnapshotStreamer : public web_video_server::BaseImageTransportStreamer
8989
int quality_;
9090
};
9191

92-
class JpegSnapshotStreamerFactory : public web_video_server::BaseSnapshotStreamerFactory
92+
class JpegSnapshotStreamerFactory : public web_video_server::
93+
BaseImageTransportSnapshotStreamerFactory
9394
{
9495
public:
9596
std::string get_type() {return "jpeg";}

include/web_video_server/streamers/png_streamers.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class PngSnapshotStreamer : public web_video_server::BaseImageTransportStreamer
9191
int quality_;
9292
};
9393

94-
class PngSnapshotStreamerFactory : public web_video_server::BaseSnapshotStreamerFactory
94+
class PngSnapshotStreamerFactory : public web_video_server::
95+
BaseImageTransportSnapshotStreamerFactory
9596
{
9697
public:
9798
std::string get_type() {return "png";}

include/web_video_server/streamers/ros_compressed_streamer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class RosCompressedSnapshotStreamerFactory : public web_video_server::BaseSnapsh
115115
const async_web_server_cpp::HttpRequest & request,
116116
async_web_server_cpp::HttpConnectionPtr connection,
117117
rclcpp::Node::SharedPtr node);
118+
std::vector<std::string> get_available_topics(rclcpp::Node::SharedPtr node);
118119
};
119120

120121
} // namespace web_video_server_streamers

src/base_image_transport_streamer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,20 @@ std::vector<std::string> BaseImageTransportStreamerFactory::get_available_topics
254254
return result;
255255
}
256256

257+
std::vector<std::string> BaseImageTransportSnapshotStreamerFactory::get_available_topics(
258+
rclcpp::Node::SharedPtr node)
259+
{
260+
std::vector<std::string> result;
261+
auto tnat = node->get_topic_names_and_types();
262+
for (auto topic_and_types : tnat) {
263+
for (auto & type : topic_and_types.second) {
264+
if (type == "sensor_msgs/msg/Image") {
265+
result.push_back(topic_and_types.first);
266+
break;
267+
}
268+
}
269+
}
270+
return result;
271+
}
272+
257273
} // namespace web_video_server

src/streamers/ros_compressed_streamer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,25 @@ RosCompressedSnapshotStreamerFactory::create_streamer(
333333
return std::make_shared<RosCompressedSnapshotStreamer>(request, connection, node);
334334
}
335335

336+
std::vector<std::string> RosCompressedSnapshotStreamerFactory::get_available_topics(
337+
rclcpp::Node::SharedPtr node)
338+
{
339+
std::vector<std::string> result;
340+
auto tnat = node->get_topic_names_and_types();
341+
for (auto topic_and_types : tnat) {
342+
for (auto & type : topic_and_types.second) {
343+
if (type == "sensor_msgs/msg/CompressedImage") {
344+
std::string topic_name = topic_and_types.first;
345+
if (topic_name.size() > 11 && topic_name.substr(topic_name.size() - 11) == "/compressed") {
346+
topic_name = topic_name.substr(0, topic_name.size() - 11);
347+
}
348+
result.push_back(topic_name);
349+
}
350+
}
351+
}
352+
return result;
353+
}
354+
336355
} // namespace web_video_server_streamers
337356

338357
#include "pluginlib/class_list_macros.hpp"

src/web_video_server.cpp

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ bool WebVideoServer::handle_list_streams(
265265
async_web_server_cpp::HttpConnectionPtr connection, const char * /* begin */,
266266
const char * /* end */)
267267
{
268-
std::map<std::string, std::vector<std::string>> topics_by_type;
268+
std::map<std::string, std::vector<std::string>> topics_by_streamer_type;
269+
std::map<std::string, std::vector<std::string>> topics_by_snapshot_type;
269270
std::set<std::string> all_topics;
270271

271272
for (const auto & factory_pair : image_streamer_factories_) {
@@ -276,7 +277,20 @@ bool WebVideoServer::handle_list_streams(
276277
factory_pair.first.c_str(), factory_topics.size());
277278
for (const auto & topic : factory_topics) {
278279
RCLCPP_DEBUG(get_logger(), " Topic: %s", topic.c_str());
279-
topics_by_type[factory_pair.first].push_back(topic);
280+
topics_by_streamer_type[factory_pair.first].push_back(topic);
281+
all_topics.insert(topic);
282+
}
283+
}
284+
285+
for (const auto & factory_pair : snapshot_streamer_factories_) {
286+
RCLCPP_DEBUG(get_logger(), "Getting topics from factory: %s", factory_pair.first.c_str());
287+
std::vector<std::string> factory_topics =
288+
factory_pair.second->get_available_topics(shared_from_this());
289+
RCLCPP_DEBUG(get_logger(), "Factory %s returned %zu topics",
290+
factory_pair.first.c_str(), factory_topics.size());
291+
for (const auto & topic : factory_topics) {
292+
RCLCPP_DEBUG(get_logger(), " Topic: %s", topic.c_str());
293+
topics_by_snapshot_type[factory_pair.first].push_back(topic);
280294
all_topics.insert(topic);
281295
}
282296
}
@@ -297,8 +311,9 @@ bool WebVideoServer::handle_list_streams(
297311
for (const std::string & topic : all_topics) {
298312
std::vector<std::string> available_stream_viewers;
299313
std::vector<std::string> available_streams;
314+
std::vector<std::string> available_snapshots;
300315

301-
for (const auto & factory_pair : topics_by_type) {
316+
for (const auto & factory_pair : topics_by_streamer_type) {
302317
const auto & type = factory_pair.first;
303318
const auto & topics = factory_pair.second;
304319
if (std::find(topics.begin(), topics.end(), topic) != topics.end()) {
@@ -311,21 +326,41 @@ bool WebVideoServer::handle_list_streams(
311326
}
312327
}
313328

329+
for (const auto & factory_pair : topics_by_snapshot_type) {
330+
const auto & type = factory_pair.first;
331+
const auto & topics = factory_pair.second;
332+
if (std::find(topics.begin(), topics.end(), topic) != topics.end()) {
333+
available_snapshots.push_back(
334+
"<a href=\"/snapshot?topic=" + topic +
335+
"&type=" + type + "\">" + type + "</a>");
336+
}
337+
}
338+
314339
connection->write("<li>");
315340
connection->write(topic);
316341
connection->write("<ul>");
317-
connection->write("<li>");
318-
connection->write("<a href=\"/stream_viewer?topic=" + topic + "\">");
319-
connection->write("Stream Viewer</a> (");
320-
connection->write(boost::algorithm::join(available_stream_viewers, ", "));
321-
connection->write(")");
322-
connection->write("</li>");
323-
connection->write("<li>");
324-
connection->write("<a href=\"/stream?topic=" + topic + "\">");
325-
connection->write("Stream</a> (");
326-
connection->write(boost::algorithm::join(available_streams, ", "));
327-
connection->write(")");
328-
connection->write("</li>");
342+
if (!available_streams.empty()) {
343+
connection->write("<li>");
344+
connection->write("<a href=\"/stream_viewer?topic=" + topic + "\">");
345+
connection->write("Stream Viewer</a> (");
346+
connection->write(boost::algorithm::join(available_stream_viewers, ", "));
347+
connection->write(")");
348+
connection->write("</li>");
349+
connection->write("<li>");
350+
connection->write("<a href=\"/stream?topic=" + topic + "\">");
351+
connection->write("Stream</a> (");
352+
connection->write(boost::algorithm::join(available_streams, ", "));
353+
connection->write(")");
354+
connection->write("</li>");
355+
}
356+
if (!available_snapshots.empty()) {
357+
connection->write("<li>");
358+
connection->write("<a href=\"/snapshot?topic=" + topic + "\">");
359+
connection->write("Snapshot</a> (");
360+
connection->write(boost::algorithm::join(available_snapshots, ", "));
361+
connection->write(")");
362+
connection->write("</li>");
363+
}
329364
connection->write("</ul>");
330365
connection->write("</li>");
331366

0 commit comments

Comments
 (0)