FFmpeg长时间无响应的解决方法

需要解决的问题

1、FFmpeg去连接的时候相机不在线导致avformat_open_input等函数一直死等,造成程序卡死

2、av_read_frame的过程中相机断开连接导致读取码流一直死等

解决方法

打开流媒体之前注册FFmpeg回调函数


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int CffmpegUIDlg::interrupt_cb(void *ctx)   
  2. {   
  3.     CffmpegUIDlg *pThis = (CffmpegUIDlg *)ctx;  
  4.     if((av_gettime() - pThis->dwLastFrameRealtime) > 100 * 1000 * 1000){//100s超时退出  
  5.         printf("主码流断开");  
  6.         return AVERROR_EOF;  
  7.     }  
  8.     return 0;  
  9. }   



[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. pFormatCtx = avformat_alloc_context();  
  2.   
  3.     pFormatCtx->interrupt_callback.opaque = pMainDlg; //C++  
  4.     pFormatCtx->interrupt_callback.callback = interrupt_cb;//设置回调函数,否则有可能ffmpeg一直被挂住。   
  5.     AVDictionary* options = nullptr;   
  6.     av_dict_set(&options, "rtsp_transport""tcp", 0);  //以udp方式打开,如果以tcp方式打开将udp替换为tcp  
  7.     av_dict_set(&options, "stimeout""3000000", 0);  //设置超时断开连接时间  
  8.   
  9.     //打开视频文件  
  10.     int nRet = avformat_open_input(&pFormatCtx, filename, NULL, &options);  
  11.     if (nRet != 0)  
  12.     {  
  13.         printf("av open input file failed!\n");  
  14.         exit(1);  
  15.     }  
  16.     if(options != nullptr){  
  17.         av_dict_free(&options);  
  18.     }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值