需要解决的问题
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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源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机制的架构与实现.之二 结构体关系与定义