ffmpeg fails with error "max delay reached. need to consume packet"

 


rtsp服务默认使用udp协议,容易丢包,报这个错误。改为tcp,则解决。

ffmpeg-设置rtsp推流/拉流使用的协议类型(TCP/UDP)(转)

拉流(设置TCP/UDP)

复制代码
//设置参数
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "stimeout", std::to_string( 2* 1000000).c_str(), 0); //设置链接超时时间(us)
av_dict_set(&format_opts, "rtsp_transport",  "tcp", 0); //设置推流的方式,默认udp。
//初始化输入上下文
AVFormatContext * m_InputContext = avformat_alloc_context();
//打开输入流。
avformat_open_input(&m_InputContext, "rtsp://127.0.0.1:554/", NULL, &format_opts);
// ......
复制代码

推流(设置TCP/UDP)

复制代码
//初始化输出流上下文。
AVFormatContext * output_format_context_ = NULL;
avformat_alloc_output_context2(&output_format_context_, NULL, "rtsp", "rtsp://127.0.0.1:554/");
/*
    添加流信息
    //TODO
*/
//设置参数,设置为TCP推流, 默认UDP
AVDictionary *format_opts = NULL;
av_dict_set(&format_opts, "stimeout", std::to_string(2 * 1000000).c_str(), 0);
av_dict_set(&format_opts, "rtsp_transport", "tcp", 0);
//写入输出头(建立rtsp连接)
avformat_write_header(output_format_context_, &format_opts);

while(1)
{
    AVPakcet media_pkt;
    /*
        初始化PKT,
        读取数据,
        填充数据,
    */
    //发送数据, 多个流时需要使用 av_interleaved_write_frame, 另附ret部分错误码,见其他文章。
    int ret = av_interleaved_write_frame(output_format_context_, &media_pkt);
    //释放数据
    av_packet_unref(&media_pkt);
    av_free_packet(&media_pkt);
}
复制代码

 

posted @   jiu~  阅读(3254)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2018-08-24 Ubuntu 16.04下添加新用户
2016-08-24 设计模式(一)
点击右上角即可分享
微信分享提示

目录导航