DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

需要解决的问题

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 2023-02-28 16:41  DoubleLi  阅读(1248)  评论(0)    收藏  举报