• rtsp_topic


    1.打开推流服务器

    2.接收ros图像

    3.ffmpeg命令推流

    pushflow.cpp

     

    1. // 1.包含头文件;
    2. #include "rclcpp/rclcpp.hpp"
    3. #include "std_msgs/msg/string.hpp"
    4. #include <iostream>
    5. #include <csignal>
    6. #include <opencv4/opencv2/opencv.hpp>
    7. #include <cv_bridge/cv_bridge.h>
    8. #include <sensor_msgs/msg/image.hpp>
    9. using namespace std::chrono_literals;
    10. using std::placeholders::_1;
    11. FILE *fp = nullptr;
    12. // 3.定义节点类;
    13. class MinimalSubscriber : public rclcpp::Node
    14. {
    15. public:
    16. MinimalSubscriber(std::string name)
    17. : Node(name)
    18. {
    19. // 3-1.创建订阅方;
    20. //subscription_ = this->create_subscription<std_msgs::msg::String>("rtsptopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    21. std::string rtspUrl = "rtsp://127.0.0.1:8554/test";
    22. std::stringstream command;
    23. command << "ffmpeg "
    24. << "-re "
    25. << "-f rawvideo "
    26. << "-vcodec rawvideo "
    27. << "-pix_fmt bgr24 "
    28. << "-s 1080x720 "
    29. << "-r 19 "
    30. << "-i - "
    31. << "-an "
    32. << "-q:v 1 "
    33. //<<"-c:v libx264 "
    34. << "-f rtsp "
    35. << rtspUrl;//<<" "
    36. //<< "-c copy";
    37. fp = popen(command.str().c_str(),"w");
    38. //if(fp!= nullptr){
    39. subscription_ = this->create_subscription<sensor_msgs::msg::CompressedImage>("detectResultMatTopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    40. //subscription_ = this->create_subscription<sensor_msgs::msg::CompressedImage>("pubImageTopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
    41. //pclose(fp);
    42. // }
    43. }
    44. private:
    45. rclcpp::Subscription<sensor_msgs::msg::CompressedImage>::SharedPtr subscription_;
    46. // 3-2.处理订阅到的消息;
    47. void topic_callback(const sensor_msgs::msg::CompressedImage::ConstPtr msg) const
    48. {
    49. cv_bridge::CvImagePtr cv_ptr_compressed = cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);
    50. cv::Mat imgCallback = cv_ptr_compressed->image;
    51. cv::Mat des1080;
    52. cv::resize(imgCallback, des1080, cv::Size(1080, 720), 0, 0, cv::INTER_NEAREST);
    53. //cv::imshow("imgCallbackrtsp",des1080);
    54. //cv::waitKey(1);
    55. fwrite(des1080.data,sizeof(char),des1080.total()*des1080.elemSize(),fp);
    56. }
    57. };
    58. int main(int argc, char * argv[])
    59. {
    60. // 2.初始化 ROS2 客户端;
    61. rclcpp::init(argc, argv);
    62. // 4.调用spin函数,并传入节点对象指针。
    63. auto node = std::make_shared<MinimalSubscriber>("rtsp");
    64. rclcpp::spin(node);
    65. //rclcpp::init(argc, argv);
    66. // 4.调用spin函数,并传入节点对象指针。
    67. //auto node = std::make_shared<MinimalSubscriber>("detect");
    68. //rclcpp::spin(node);
    69. //rclcpp::spin(std::make_shared<MinimalSubscriber>());
    70. // 5.释放资源;
    71. //rclcpp::shutdown();
    72. // 5.释放资源;
    73. rclcpp::shutdown();
    74. return 0;
    75. }

    CMakeLists.txt

    1. cmake_minimum_required(VERSION 3.5)
    2. project(rtsp_topic)
    3. # Default to C99
    4. if(NOT CMAKE_C_STANDARD)
    5. set(CMAKE_C_STANDARD 99)
    6. endif()
    7. # Default to C++14
    8. if(NOT CMAKE_CXX_STANDARD)
    9. set(CMAKE_CXX_STANDARD 14)
    10. endif()
    11. if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    12. add_compile_options(-Wall -Wextra -Wpedantic)
    13. endif()
    14. # find dependencies
    15. find_package(ament_cmake REQUIRED)
    16. find_package(rclcpp REQUIRED)
    17. find_package(std_msgs REQUIRED)
    18. find_package(cv_bridge REQUIRED)
    19. find_package(image_transport)
    20. find_package(sensor_msgs REQUIRED)
    21. find_package(OpenCV REQUIRED)
    22. include_directories( ${OpenCV_INCLUDE_DIRS})
    23. add_executable(rtsp
    24. src/pushflow.cpp
    25. )
    26. target_link_libraries(rtsp ${OpenCV_LIBS})
    27. # ros
    28. target_link_libraries(rtsp
    29. ${rclcpp_LIBRARIES}
    30. )
    31. ament_target_dependencies(rtsp
    32. rclcpp
    33. std_msgs
    34. sensor_msgs
    35. cv_bridge
    36. OpenCV
    37. image_transport
    38. )
    39. # 安装可执行文件
    40. install(TARGETS rtsp
    41. DESTINATION lib/${PROJECT_NAME}
    42. )
    43. ament_package()

    package.xml

    1. <?xml version="1.0"?>
    2. <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
    3. <package format="3">
    4. <name>rtsp_topic</name>
    5. <version>0.0.0</version>
    6. <description>TODO: Package description</description>
    7. <maintainer email="jetson@todo.todo">jetson</maintainer>
    8. <license>TODO: License declaration</license>
    9. <buildtool_depend>ament_cmake</buildtool_depend>
    10. <depend>rclcpp</depend>
    11. <depend>std_msgs</depend>
    12. <test_depend>ament_lint_auto</test_depend>
    13. <test_depend>ament_lint_common</test_depend>
    14. <export>
    15. <build_type>ament_cmake</build_type>
    16. </export>
    17. </package>

  • 相关阅读:
    面试-synchronized(java5以前唯一)和ReentrantLock的区别
    SpringBoot集成SpringSecurity从0到1搭建权限管理详细过程(认证+授权)
    web 面试高频考点 —— HTML & CSS 篇
    美团高性能终端实时日志系统建设实践
    File类中的方法、获取功能的方法合集、判断功能的方法合集、创建、删除功能的方法合集、目录遍历方法合集
    响应格式规范
    快速搭建Vue前端框架
    leetcode 2525 根据规则将箱子分类 c++ pyhton java c题解
    电脑出现错误代码0x80004005的解决方法
    Web数据流的一生——从前端界面到后端数据,以及万级QPS的后台服务搭建
  • 原文地址:https://blog.csdn.net/qq_16377055/article/details/133804972