rtsp_topic

1.打开推流服务器

2.接收ros图像

3.ffmpeg命令推流

pushflow.cpp

// 1.包含头文件;
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include <iostream>
#include <csignal>
#include <opencv4/opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/msg/image.hpp>
using namespace std::chrono_literals;
using std::placeholders::_1;

FILE *fp = nullptr;
 
// 3.定义节点类;
class MinimalSubscriber : public rclcpp::Node
{
  public:
    MinimalSubscriber(std::string name)
    : Node(name)
    {
      // 3-1.创建订阅方;
      //subscription_ = this->create_subscription<std_msgs::msg::String>("rtsptopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
      
      std::string rtspUrl = "rtsp://127.0.0.1:8554/test";
      std::stringstream command;
      command << "ffmpeg "
              << "-re "
              << "-f rawvideo "
              << "-vcodec rawvideo "
              << "-pix_fmt bgr24 "
              << "-s 1080x720 "
              << "-r 19 "
              << "-i - "
              << "-an "
              << "-q:v 1 "
              //<<"-c:v libx264 "
              << "-f rtsp "
              << rtspUrl;//<<" "
              //<< "-c copy";

      fp = popen(command.str().c_str(),"w");
     
      

      //if(fp!= nullptr){
      subscription_ = this->create_subscription<sensor_msgs::msg::CompressedImage>("detectResultMatTopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
      //subscription_ = this->create_subscription<sensor_msgs::msg::CompressedImage>("pubImageTopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
      //pclose(fp);
       // }

    }

  private:
    
    rclcpp::Subscription<sensor_msgs::msg::CompressedImage>::SharedPtr subscription_;
    // 3-2.处理订阅到的消息;

    void topic_callback(const sensor_msgs::msg::CompressedImage::ConstPtr  msg) const
    {
      cv_bridge::CvImagePtr cv_ptr_compressed = cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);
      cv::Mat imgCallback = cv_ptr_compressed->image;

      cv::Mat des1080;

      cv::resize(imgCallback, des1080, cv::Size(1080, 720), 0, 0, cv::INTER_NEAREST);
      //cv::imshow("imgCallbackrtsp",des1080);
      //cv::waitKey(1);

      fwrite(des1080.data,sizeof(char),des1080.total()*des1080.elemSize(),fp);
    }
    
};


 
int main(int argc, char * argv[])
{
  // 2.初始化 ROS2 客户端;
  rclcpp::init(argc, argv);
  // 4.调用spin函数,并传入节点对象指针。
  auto node = std::make_shared<MinimalSubscriber>("rtsp");
  rclcpp::spin(node);

  //rclcpp::init(argc, argv);
  // 4.调用spin函数,并传入节点对象指针。
  //auto node = std::make_shared<MinimalSubscriber>("detect");
  //rclcpp::spin(node);
  //rclcpp::spin(std::make_shared<MinimalSubscriber>());
  // 5.释放资源;
  //rclcpp::shutdown();
  // 5.释放资源;
  rclcpp::shutdown();
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(rtsp_topic)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

find_package(cv_bridge REQUIRED)
find_package(image_transport)
find_package(sensor_msgs REQUIRED)

find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS})

add_executable(rtsp
  src/pushflow.cpp
)

target_link_libraries(rtsp ${OpenCV_LIBS})




# ros
target_link_libraries(rtsp
  ${rclcpp_LIBRARIES} 
)

ament_target_dependencies(rtsp
  rclcpp
  std_msgs
  sensor_msgs 
  cv_bridge 
  OpenCV
  image_transport
)
 


# 安装可执行文件
install(TARGETS rtsp
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()

package.xml

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>rtsp_topic</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="jetson@todo.todo">jetson</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>std_msgs</depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>
相关推荐
白鹭float.2 小时前
【OpenGL/C++】面向对象扩展——测试环境
c++·图形学·opengl
小wanga2 小时前
【C++】类型转换
jvm·c++
我不是程序猿儿2 小时前
【C++】xml烧录 调用twinCat流程自动化
xml·c++·自动化
find_starshine2 小时前
xml-dota-yolo数据集格式转换
xml·python·yolo
小酒丸子3 小时前
基于QT和C++的实时日期和时间显示
c++·qt
弓.长.4 小时前
【leetcode刷题】:双指针篇(有效三角形的个数、和为s的两个数)
c++·算法·leetcode
thisiszdy4 小时前
<C++> XlsxWriter写EXCEL
c++·excel
14_115 小时前
Cherno C++学习笔记 P51 创建并使用库
c++·笔记·学习
就叫飞六吧6 小时前
51 单片机和 STM32 引脚命名对照表与解析
c++·stm32·单片机·嵌入式硬件·51单片机
霜雪殇璃6 小时前
c++对结构体的扩充以及类的介绍
开发语言·c++·笔记·学习