当前位置: 首页 > news >正文

ffmpeg RTP PS推流

要实现 CRtpSendPs 类,使其能够将 H264 数据通过 RTP PS 流推送到指定的 URL,并支持 TCP 和 UDP 传输方式,您需要使用 FFmpeg 库。以下是该类的实现示例,包括必要的初始化、推流和退出函数。

步骤

  1. 初始化 FFmpeg 库:需要初始化 FFmpeg 网络、格式和编码器相关的库。
  2. 配置 RTP 传输:使用 RTP 封装 PS (Program Stream) 格式。
  3. 推送数据:接收 H264 数据并将其封装为 RTP 包,发送到指定的目标 URL(支持 UDP 和 TCP)。
  4. 退出处理:释放相关资源。

完整代码实现

 
#include <iostream>
#include <string>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavdevice/avdevice.h>
#include <libavfilter/avfilter.h>
#include <libswscale/swscale.h>class CRtpSendPs
{
public:CRtpSendPs();~CRtpSendPs();int Init(const char *url, bool bUdpOrTcp);  // 初始化,设置目标 URL 和传输协议int PushStream(const void *pH264Data, uint32_t u32Len);  // 推送视频数据int Exit();  // 清理资源private:AVFormatContext *outputFmtCtx;AVStream *outputStream;AVCodecContext *codecCtx;AVPacket pkt;bool isUdp;std::string outputUrl;bool isInitialized;
};CRtpSendPs::CRtpSendPs() : outputFmtCtx(nullptr), outputStream(nullptr), codecCtx(nullptr), isInitialized(false)
{av_register_all();avformat_network_init();avcodec_register_all();avdevice_register_all();isUdp = true;isInitialized = false;
}CRtpSendPs::~CRtpSendPs()
{Exit();
}int CRtpSendPs::Init(const char *url, bool bUdpOrTcp)
{if (isInitialized) {std::cerr << "Already initialized!" << std::endl;return -1;}isUdp = bUdpOrTcp;outputUrl = url;// Create output contextAVOutputFormat *fmt = av_guess_format(NULL, outputUrl.c_str(), NULL);if (!fmt) {std::cerr << "Could not guess output format!" << std::endl;return -1;}int ret = avformat_alloc_output_context2(&outputFmtCtx, fmt, NULL, outputUrl.c_str());if (ret < 0) {std::cerr << "Failed to create output context!" << std::endl;return ret;}// Create a new stream for the outputoutputStream = avformat_new_stream(outputFmtCtx, NULL);if (!outputStream) {std::cerr << "Failed to create new stream!" << std::endl;return AVERROR(ENOMEM);}// Setup codec parameters (for H264)AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);if (!encoder) {std::cerr << "H264 encoder not found!" << std::endl;return AVERROR_ENCODER_NOT_FOUND;}codecCtx = avcodec_alloc_context3(encoder);if (!codecCtx) {std::cerr << "Failed to allocate codec context!" << std::endl;return AVERROR(ENOMEM);}codecCtx->codec_id = AV_CODEC_ID_H264;codecCtx->bit_rate = 1000000;codecCtx->width = 1920;codecCtx->height = 1080;codecCtx->time_base = {1, 30};  // 30 fpscodecCtx->gop_size = 12;codecCtx->max_b_frames = 3;codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;ret = avcodec_open2(codecCtx, encoder, NULL);if (ret < 0) {std::cerr << "Failed to open codec!" << std::endl;return ret;}// Copy codec parameters to the streamret = avcodec_parameters_from_context(outputStream->codecpar, codecCtx);if (ret < 0) {std::cerr << "Failed to copy codec parameters!" << std::endl;return ret;}// Open the output file or network streamif (!(outputFmtCtx->oformat->flags & AVFMT_NOFILE)) {ret = avio_open(&outputFmtCtx->pb, outputUrl.c_str(), AVIO_FLAG_WRITE);if (ret < 0) {std::cerr << "Failed to open output URL!" << std::endl;return ret;}}// Write the header of the file (or stream)ret = avformat_write_header(outputFmtCtx, NULL);if (ret < 0) {std::cerr << "Failed to write header!" << std::endl;return ret;}isInitialized = true;return 0;
}int CRtpSendPs::PushStream(const void *pH264Data, uint32_t u32Len)
{if (!isInitialized) {std::cerr << "Not initialized!" << std::endl;return -1;}// Create a packet and fill it with H264 dataav_init_packet(&pkt);pkt.data = (uint8_t *)pH264Data;pkt.size = u32Len;pkt.stream_index = outputStream->index;pkt.flags |= AV_PKT_FLAG_KEY;  // Assuming it's a keyframe, adjust if needed// Write the packet to the RTP streamint ret = av_write_frame(outputFmtCtx, &pkt);if (ret < 0) {std::cerr << "Error writing frame!" << std::endl;return ret;}return 0;
}int CRtpSendPs::Exit()
{if (!isInitialized) {return 0;  // Already exited}av_write_trailer(outputFmtCtx);// Close the output stream and free resourcesif (outputFmtCtx && !(outputFmtCtx->oformat->flags & AVFMT_NOFILE)) {avio_closep(&outputFmtCtx->pb);}avcodec_free_context(&codecCtx);avformat_free_context(outputFmtCtx);isInitialized = false;return 0;
}

代码说明

  1. CRtpSendPs 类的构造函数和析构函数

    • Init():初始化 FFmpeg 输出格式和编码器配置,设置输出 URL 并建立连接。
    • PushStream():接受一帧 H264 数据并将其封装为 RTP 包,然后通过网络发送。
    • Exit():释放资源并关闭输出流。
  2. FFmpeg 配置

    • 使用 avformat_alloc_output_context2() 创建输出流上下文。
    • 使用 avcodec_find_encoder() 查找 H264 编码器,初始化编码器上下文。
    • 使用 av_write_frame() 将数据包发送到指定的输出 URL(支持 RTP 协议)。
  3. RTP 传输:通过 RTP 推送数据流,支持 UDP 和 TCP(通过 URL 中的协议部分决定)。

编译命令

在编译前,确保已经安装了 FFmpeg 库。如果没有安装,您可以使用以下命令:

sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev libavdevice-dev libswscale-dev

然后使用以下命令来编译代码:

 
g++ -o rtp_send_ps rtp_send_ps.cpp -lavformat -lavcodec -lavutil -lavdevice -lswscale -lavfilter -lm -lz

运行示例

 
./rtp_send_ps rtp://192.168.0.49:5000

此命令将会把 H264 数据通过 RTP 协议推送到目标 IP 地址 192.168.0.49,端口 5000

注意事项

  • 您需要确保目标服务器或设备能够接收 RTP 流。
  • 在真实应用中,PushStream() 中的 H264 数据可以从文件或网络中获取,并通过该函数传输。
http://www.lryc.cn/news/492915.html

相关文章:

  • Rust语言俄罗斯方块(漂亮的界面案例+详细的代码解说+完美运行)
  • NUMA架构及在极速网络IO场景下的优化实践
  • Brain.js 用于浏览器的 GPU 加速神经网络
  • Linux——用户级缓存区及模拟实现fopen、fweite、fclose
  • 视觉感知与处理:解密计算机视觉的未来
  • 【大数据学习 | Spark-Core】广播变量和累加器
  • postgresql按照年月日统计历史数据
  • pywin32库 -- 读取word文档中的图形
  • GitLab使用示例
  • uniapp echarts tooltip formation 不识别html
  • 3D扫描对文博行业有哪些影响?
  • 面试(十一)
  • React-useState的使用
  • 设计模式之破环单例模式和阻止破坏
  • 11.19c++面向对象+单例模式
  • 一文了解TensorFlow是什么
  • 如何做好一份技术文档?
  • Linux和Ubuntu的关系
  • 软件工程之静态建模
  • PICO VR串流调试Unity程序
  • 自媒体图文视频自动生成软件|03| 页面和结构介绍
  • 深入浅出摸透AIGC文生图产品SD(Stable Diffusion)
  • 解析生成对抗网络(GAN):原理与应用
  • CodeIgniter URL结构
  • 从 App Search 到 Elasticsearch — 挖掘搜索的未来
  • 鸿蒙本地模拟器 模拟TCP服务端的过程
  • Qt/C++基于重力模拟的像素点水平堆叠效果
  • Zookeeper学习心得
  • 嵌入式开发工程师面试题 - 2024/11/24
  • Python中打印当前目录文件树的脚本