DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

需要解决的问题

1、FFmpeg去连接的时候相机不在线导致avformat_open_input等函数一直死等,造成程序卡死

2、av_read_frame的过程中相机断开连接导致读取码流一直死等

解决方法

打开流媒体之前注册FFmpeg回调函数

 
 
int CffmpegUIDlg::interrupt_cb(void *ctx)
 
{
 
CffmpegUIDlg *pThis = (CffmpegUIDlg *)ctx;
 
if((av_gettime() - pThis->dwLastFrameRealtime) > 100 * 1000 * 1000){//100s超时退出
 
printf("主码流断开");
 
return AVERROR_EOF;
 
}
 
return 0;
 
}
 

 

 

 
 
pFormatCtx = avformat_alloc_context();
 
 
 
pFormatCtx->interrupt_callback.opaque = pMainDlg; //C++
 
pFormatCtx->interrupt_callback.callback = interrupt_cb;//设置回调函数,否则有可能ffmpeg一直被挂住。
 
AVDictionary* options = nullptr;
 
av_dict_set(&options, "rtsp_transport", "tcp", 0); //以udp方式打开,如果以tcp方式打开将udp替换为tcp
 
av_dict_set(&options, "stimeout", "3000000", 0); //设置超时断开连接时间
 
 
 
//打开视频文件
 
int nRet = avformat_open_input(&pFormatCtx, filename, NULL, &options);
 
if (nRet != 0)
 
{
 
printf("av open input file failed!\n");
 
exit(1);
 
}
 
if(options != nullptr){
 
av_dict_free(&options);
 
}
 
 


ffmpeg 4.0以上可以使用下面:

 
//请使用listen_timeout,不然会一直阻塞下去。
 
AVDictionary* options = nullptr;
 
//实时播放使用udp,减小带宽并防止断线
 
av_dict_set(&options, "rtsp_transport", "udp", 0);
 
//等待3秒超时
 
av_dict_set(&options, "listen_timeout", "3", 0);
 
posted on   DoubleLi  阅读(1058)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2022-02-28 C语言结构体初始化的四种方法
2022-02-28 ffmpeg filter过滤器 基础实例及全面解析
2022-02-28 FFmpeg中的时间戳表示方法
2022-02-28 FFmpeg-4.0 的filter机制的架构与实现.之三 Filter实现的源码分析
2022-02-28 FFmpeg-4.0 的filter机制的架构与实现.之一 Filter原理
2022-02-28 全局服务器调度简介
2022-02-28 FFmpeg-4.0 的filter机制的架构与实现.之二 结构体关系与定义
点击右上角即可分享
微信分享提示