ffmpeg取rtsp流时av_read_frame阻塞的解决办法

搜索关键词:ffmpeg 超时/timeout

方法一

方法是设置超时参数

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

参考:

  1. 使用ffmpeg的av_read_frame,如何控制连接超时
  2. timeout on av_read_frame()
  3. FFmpeg长时间无响应的解决方法
  • 8
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值