rtsp_topic

1.打开推流服务器

2.接收ros图像

3.ffmpeg命令推流

rtsp_topic_第1张图片

pushflow.cpp

 

// 1.包含头文件;
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include 
#include 
#include 
#include 
#include 
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("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("detectResultMatTopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
      //subscription_ = this->create_subscription("pubImageTopic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
      //pclose(fp);
       // }

    }

  private:
    
    rclcpp::Subscription::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("rtsp");
  rclcpp::spin(node);

  //rclcpp::init(argc, argv);
  // 4.调用spin函数,并传入节点对象指针。
  //auto node = std::make_shared("detect");
  //rclcpp::spin(node);
  //rclcpp::spin(std::make_shared());
  // 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




  rtsp_topic
  0.0.0
  TODO: Package description
  jetson
  TODO: License declaration

  ament_cmake

  rclcpp
  std_msgs

  ament_lint_auto
  ament_lint_common

  
    ament_cmake
  

你可能感兴趣的:(c++,YOLO)