需要解决的问题
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);
}