最近在做流媒体解析,使用ffmpeg开源库解析流媒体。在做解析rtp流时发现在解析rtp流时,有一次写错rtp地址,发现程序阻塞在avformat_open_input()函数,通过查阅网上办法通过av_dict_set()函数设置超时检测,本人亲测,非常不好使,通过查看源代码,添加打印发现程序不是阻塞在协议连接上,是阻塞在获取数据上,也就是av_read_frame(),通过查阅资料,找到通过回调函数完成超时检测。测试很好使。下面附代码。
pFormatCtx = avformat_alloc_context();
pFormatCtx->interrupt_callback.callback = interrupt_cb;--------注册回调函数
pFormatCtx->interrupt_callback.opaque = pFormatCtx;
avformat_open_input(pFormatCtx,url,0,0);
struct timeval tv;
start=gettimeofday(&tv, NULL);
//核心是超时返回1,正常等待返回0 5s超时检测
static int interrupt_cb(void *ctx)
{
if (gettimeofday(&tv, NULL)-start>5000)
{
return 1;//这个就是超时的返回
}
}
return 0;
}
return 0;
}