ros2 component stable version

This commit is contained in:
dmalad committed 2023-12-04 16:37:58 +01:00
1 parent 083693732c
commit 59baa89f5d
12 files changed
+170 -145

No files matched your search

Regular → Executable
+5 -39
View File
@@ -1,34 +1,13 @@
#include "../include/image2rtsp.hpp"
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#include <gst/app/gstappsrc.h>
#include "../include/image2rtsp.hpp"
using std::placeholders::_1;
Image2rtsp::Image2rtsp() : Node("image2rtsp"){
// Declare and get the parameters
this->declare_parameter("topic");
this->declare_parameter("mountpoint");
this->declare_parameter("bitrate");
this->declare_parameter("caps");
this->declare_parameter("port");
this->declare_parameter("local_only");
topic = this->get_parameter("topic").as_string();
mountpoint = this->get_parameter("mountpoint").as_string();
bitrate = this->get_parameter("bitrate").as_string();
caps = this->get_parameter("caps").as_string();
port = this->get_parameter("port").as_string();
local_only = this->get_parameter("local_only").as_bool();
// Check if the parameter is set, since no default value is provided
if (!this->has_parameter("topic") || !this->has_parameter("mountpoint") || !this->has_parameter("bitrate") || !this->has_parameter("caps") || !this->has_parameter("port")){
rclcpp::shutdown(); // Shutdown the node if there are some issues with launch file
return;
}
Image2rtsp::Image2rtsp(const rclcpp::NodeOptions & options) : Node("image2rtsp"){
// Start the subscription
subscription_ = this->create_subscription<sensor_msgs::msg::Image>(topic, 10, std::bind(&Image2rtsp::topic_callback, this, _1));
@@ -37,25 +16,12 @@ Image2rtsp::Image2rtsp() : Node("image2rtsp"){
rtsp_server = rtsp_server_create(port, local_only);
appsrc = NULL;
// Setup the pipeline
pipeline_head = "( appsrc name=imagesrc do-timestamp=true min-latency=0 max-latency=0 max-bytes=1000 is-live=true ! videoconvert ! videoscale ! ";
pipeline_head = "( appsrc name=imagesrc do-timestamp=true min-latency=0 max-latency=0 max-bytes=1000 is-live=true ! videoconvert ! ";
pipeline_tail = "key-int-max=30 ! video/x-h264, profile=baseline ! rtph264pay name=pay0 pt=96 )";
pipeline = pipeline_head + caps + " ! x264enc tune=zerolatency bitrate=" + bitrate + pipeline_tail;
rtsp_server_add_url(mountpoint.c_str(), pipeline.c_str(), (GstElement **)&(appsrc));
RCLCPP_INFO(this->get_logger(), "Stream available at rtsp://%s:%s%s", gst_rtsp_server_get_address(rtsp_server), port.c_str(), mountpoint.c_str());
}
int main(int argc, char *argv[]){
rclcpp::init(argc, argv);
try{
auto node = std::make_shared<Image2rtsp>();
if (rclcpp::ok()){
rclcpp::spin(node);
}
}catch (const std::exception &e){
RCLCPP_INFO(rclcpp::get_logger("image2rtsp"), "One or more required parameters are not set. Shutting down the node.");
RCLCPP_INFO(rclcpp::get_logger("image2rtsp"), "Tip: Start the node only using launch file. Also rebuild after making changes in the YAML file");
RCLCPP_ERROR(rclcpp::get_logger("image2rtsp"), "Parameter is not set: %s", e.what());
}
rclcpp::shutdown();
return 0;
}
#include "rclcpp_components/register_node_macro.hpp"
RCLCPP_COMPONENTS_REGISTER_NODE(Image2rtsp)
+6 -2
View File
@@ -4,10 +4,14 @@
#include <rclcpp/rclcpp.hpp>
#include "../include/image2rtsp.hpp"
#include "../include/image_encodings.h"
extern "C" {
#include "../include/image_encodings.h"
}
using namespace std;
static void *mainloop(void *arg){
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
@@ -131,7 +135,7 @@ GstCaps *Image2rtsp::gst_caps_new_from_image(const sensor_msgs::msg::Image::Shar
"format", G_TYPE_STRING, format->second.c_str(),
"width", G_TYPE_INT, msg->width,
"height", G_TYPE_INT, msg->height,
"framerate", GST_TYPE_FRACTION, 10, 1,
"framerate", GST_TYPE_FRACTION, stoi(framerate), 1,
nullptr);
}