1. 程式人生 > >ffmpeg取rtsp流時av_read_frame阻塞的解決辦法

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方式開啟將udp替換為tcp
av_dict_set(&options, "stimeout", "2000000", 0); //設定超時斷開連線時間,單位微秒 av_dict_set(&options, "max_delay", "500000", 0); //設定最大時延

方法二

註冊回撥函式,在回撥函式中設定超時判斷(這種方法我未成功)

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

參考: