方法一
方法是设置超时参数
AVFormatContext *pAVFormatContext = avformat_alloc_context();//申请一个AVFormatContext结构的内存,并进行简单初始化
AVDictionary* options = NULL;
av_dict_set(&options, "buffer_size", "102400", 0); //设置缓存大小,1080p可将值调大
av_dict_set(&options, "rtsp_transport", "tcp", 0); //默认以udp方式打开,改为tcp
av_dict_set(&options, "stimeout", "2000000", 0); //设置超时断开连接时间,单位微秒
av_dict_set(&options, "max_delay", "500000", 0); //设置最大时延
参考:ffmpeg播放RTSP的一点优化
方法二
注册回调函数,在回调函数中设置超时判断(这种方法我未成功)
pFormatCtx = avformat_alloc_context();
pFormatCtx->interrupt_callback.callback = interrupt_cb;--------注册回调函数
pFormatCtx->interrupt_callback.opaque = pFormatCtx;
AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "tcp", 0);
//核心是超时返回1,正常等待返回0
static int interrupt_cb(void *ctx)//这个回调函数应该是用了博主内部的一些函数和变量
{
// do something
NSLog(@"%d",time_out);
time_out++;
if (time_out > 40) {
NSLog(@"------%d", firsttimeplay);
time_out=0;
if (firsttimeplay) {
firsttimeplay=0;
NSLog(@"++++++++");
return 1;//这个就是超时的返回
}
}
return 0;
}
//回调函数伪代码如下
int interruptCallBack(void *ctx){
//once your preferred time is out you can return 1 and exit from the loop
if(timeout){
//exit
return 1;
}
//continue
return 0;
}
麻烦点的实现,另建立了一个监视线程,参考文献3
参考:
使用ffmpeg的av_read_frame,如何控制连接超时
timeout on av_read_frame()
FFmpeg长时间无响应的解决方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2012-12-13 C++常用库
2012-12-13 VC++学习方法及书籍推荐 .
2012-12-13 演练:创建和使用动态链接库 (C++)
2012-12-13 演练:创建和使用静态库 (C++)