DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

方法一
方法是设置超时参数

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长时间无响应的解决方法

posted on   DoubleLi  阅读(1609)  评论(0编辑  收藏  举报
编辑推荐:
· 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++)
点击右上角即可分享
微信分享提示