ros2 c++ FFmpeg opencv rtsp拉流

ros2 c++ rtsp拉流 播放 延时200-600ms

一、前提条件

1.ubuntu ros2 humble
2.sudo apt-get install ffmpeg libavformat-dev libavcodec-dev libavutil-dev libswscale-dev

二、简单代码

rtmp_player_node.cpp

#include <chrono>
#include <cstdio>
#include <rclcpp/rclcpp.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/msg/image.hpp>
#include <std_msgs/msg/string.hpp>

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

extern "C" {
   
   
    #include "libavcodec/avcodec.h"
    #include "libavformat/avformat.h"
    #include "libavutil/avutil.h"
    #include "libswscale/swscale.h"
}
 
using namespace std::chrono_literals;
 
class VideoGrab : public rclcpp::Node
{
   
   
public:
  VideoGrab() : Node("video_grab")
  {
   
   
    image_publisher_ = this->create_publisher<sensor_msgs::msg::Image>("/video_stream", 5);
   
    avformat_network_init();
    if (avformat_open_input(&formatContext_, "rtsp://user:pass@ip:port", NULL, NULL) != 0)
    {
   
   
      std::cout << "Unable to open RTSP stream" << std::endl;
      return;
    }

    if (avformat_find_stream_info(formatContext_, NULL) < 0)
    {
   
   
      std::cout << "Unable to retrieve stream information" << std::endl;
      return;
    }

    cv::Mat image_;

    rtsp_th_ = std::make_shared<std::thread>(std::bind(&VideoGrab::run, this));
    rtsp_th_->detach();
    pub_th_ = std::make_shared<std::thread>(std::bind(&VideoGrab::publish, this));
### ROSRTSP的集成与使用 在机器人操作系统(ROS)环境中,实时传输协议(Real-Time Streaming Protocol, RTSP)是一种常见的用于视频传输的技术。为了在ROS中集成并使用RTSP,可以通过以下几种方式实现。 #### 使用GSCam包 GSCam 是一个由布朗大学机器人实验室开发的 ROS 包,它能够通过 GStreamer 框架无缝集成各种视频ROS 生态系统中[^3]。由于 GStreamer 支持多种媒体源,包括 RTSP ,因此可以借助 GSCam 来处理 RTSP 数据。以下是具体操作: 1. 安装 `gscam` 包: ```bash sudo apt-get install ros-$ROS_DISTRO-gscam ``` 2. 配置 GStreamer 的管道字符串来支持 RTSP 。例如,假设有一个 RTSP 地址为 `rtsp://example.com/stream`,则可以在启动节点时指定如下参数: ```bash export GSCAM_CONFIG="rtspsrc location=rtsp://example.com/stream ! decodebin ! videoconvert" roslaunch gscam gscam.launch ``` 上述命令会将 RTSP 转换成 ROS 中的标准图像消息 `/image_raw` 并发布出来供其他节点订阅和处理。 #### 自定义解决方案 除了依赖现成工具外,还可以编写自定义代码来解析 RTSP 并将数据封装进 ROS 消息类型里。这通常涉及以下几个步骤: 1. 利用 OpenCVFFmpeg 库读取 RTSP 视频帧; 2. 将每一帧转化为传感器消息形式 (sensor_msgs/Image); 3. 创建一个新的 Publisher 节点定期广播这些转化后的图片信息给感兴趣的主题名称下。 下面是一个简单的 Python 实现例子展示如何完成这一过程: ```python import cv2 from cv_bridge import CvBridge import rospy from sensor_msgs.msg import Image def publish_rtsp_stream(rtsp_url): bridge = CvBridge() cap = cv2.VideoCapture(rtsp_url) if not cap.isOpened(): raise IOError("Cannot open RTSP stream") pub = rospy.Publisher('/camera/image', Image, queue_size=10) rospy.init_node('rtsp_publisher', anonymous=True) rate = rospy.Rate(30) # Hz while not rospy.is_shutdown() and cap.isOpened(): ret, frame = cap.read() if ret: img_msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8") pub.publish(img_msg) rate.sleep() if __name__ == '__main__': try: rtsp_url = 'rtsp://your.rtsp.stream' publish_rtsp_stream(rtsp_url) except Exception as e: print(e) ``` 此脚本初始化了一个名为 '/camera/image' 的主题,并持续从指定 URL 获取最新画面更新至该频道上直到程序终止为止。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值