camera is added

This commit is contained in:
dmalad 2024-04-21 20:17:55 +02:00
parent 56b996397a
commit a8d8bb5572
3 changed files with 23 additions and 3 deletions

View File

@ -1,6 +1,14 @@
/image2rtsp:
ros__parameters:
# If camera serves as a source
camera: True
source: "v4l2src device=/dev/video0"
# If the source is a ros2 topic
topic: "/color/image_raw"
# General setup
mountpoint: "/back"
bitrate: "500"
framerate: "30"

View File

@ -15,6 +15,7 @@ public:
GstRTSPServer *rtsp_server;
private:
string source;
string topic;
string mountpoint;
string bitrate;
@ -26,6 +27,7 @@ private:
string pipeline_head;
string pipeline_tail;
bool local_only;
bool camera;
GstAppSrc *appsrc;
void video_mainloop_start();

View File

@ -9,6 +9,7 @@ using std::placeholders::_1;
Image2rtsp::Image2rtsp() : Node("image2rtsp"){
// Declare and get the parameters
this->declare_parameter("source", "v4l2src device=/dev/video0");
this->declare_parameter("topic", "/color/image_raw");
this->declare_parameter("mountpoint", "/back");
this->declare_parameter("bitrate", "500");
@ -17,7 +18,9 @@ Image2rtsp::Image2rtsp() : Node("image2rtsp"){
this->declare_parameter("caps_2", "/1,width=640,height=480");
this->declare_parameter("port", "8554");
this->declare_parameter("local_only", true);
this->declare_parameter("camera", false);
source = this->get_parameter("source").as_string();
topic = this->get_parameter("topic").as_string();
mountpoint = this->get_parameter("mountpoint").as_string();
bitrate = this->get_parameter("bitrate").as_string();
@ -26,6 +29,7 @@ Image2rtsp::Image2rtsp() : Node("image2rtsp"){
caps_2 = this->get_parameter("caps_2").as_string();
port = this->get_parameter("port").as_string();
local_only = this->get_parameter("local_only").as_bool();
camera = this->get_parameter("camera").as_bool();
// Start the subscription
subscription_ = this->create_subscription<sensor_msgs::msg::Image>(topic, 10, std::bind(&Image2rtsp::topic_callback, this, _1));
@ -35,10 +39,16 @@ 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_tail = "key-int-max=30 ! video/x-h264, profile=baseline ! rtph264pay name=pay0 pt=96 )";
if (camera == false){
pipeline_head = "( appsrc name=imagesrc do-timestamp=true min-latency=0 max-latency=0 max-bytes=1000 is-live=true ! videoconvert ! videoscale ! ";
pipeline = pipeline_head + caps_1 + framerate + caps_2 + " ! x264enc tune=zerolatency bitrate=" + bitrate + pipeline_tail;
rtsp_server_add_url(mountpoint.c_str(), pipeline.c_str(), (GstElement **)&(appsrc));
}
else {
pipeline = "( " + source + " ! videoconvert ! videoscale ! " + caps_1 + framerate + caps_2 + " ! x264enc tune=zerolatency bitrate=" + bitrate + pipeline_tail;
rtsp_server_add_url(mountpoint.c_str(), pipeline.c_str(), NULL);
}
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());
}