ffmpeg取rtsp流時av_read_frame阻塞的解決辦法


方法一
方法是設置超時參數

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長時間無響應的解決方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM