diff --git a/README.md b/README.md index 1647ac7..1008a89 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,4 @@ ros2 launch image2rtsp rtsp.launch.py ## To Do - Add the camera directly as a source - Add web camera ros2 topic formats -- Add compressed images formats (jpeg, png) +- Add compressed images formats (jpeg, png) \ No newline at end of file diff --git a/config/parameters.yaml b/config/parameters.yaml index 0460e51..5110ac6 100644 --- a/config/parameters.yaml +++ b/config/parameters.yaml @@ -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" diff --git a/include/image2rtsp.hpp b/include/image2rtsp.hpp index 122d085..28dda0c 100644 --- a/include/image2rtsp.hpp +++ b/include/image2rtsp.hpp @@ -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(); diff --git a/src/image2rtsp.cpp b/src/image2rtsp.cpp index 8983950..d78c3a7 100644 --- a/src/image2rtsp.cpp +++ b/src/image2rtsp.cpp @@ -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(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 )"; - 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)); + 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()); }