8

我使用ffmpeg解码RTSP视频。它喜欢这样:当它在文件末尾时,它会在av_read_frame()中阻塞很长时间,为什么?

4

2 回答 2

8

各种原因都可能导致长时间阻塞。但是您可以控制 I/O 层的处理时间。

使用该结构AVFormatContext::interrupt_callback设置中断处理程序。

class timeout_handler {
  public:
    timeout_handler(unsigned int t) : timeout_ms_(TimeoutMs){}

    void reset(unsigned int 0) {
      timeout_ms_ = TimeoutMs;
      lastTime_ = my_get_local_time();
    }

    bool is_timeout(){
      const my_time_duration actualDelay = my_get_local_time() - lastTime_;
      return actualDelay > timeout_ms_;
    }

    static int check_interrupt(void * t) {
       return t && static_cast<timeout_handler *>(t)->is_timeout();
    }

 public:
   unsigned int timeout_ms_;
   my_time_t lastTime_;      
 };


 /// .................
 AVFormatContext * ic;
 timeout_handler * th = new timeout_handler(kDefaultTimeout);
 /// .................
 ic->interrupt_callback.opaque = (void*)th ;
 ic->interrupt_callback.callback = &timeout_handler::check_interrupt;
 /// open input
 // avformat_open_input(ic, ... );
 // etc

 /// .................
 /// before any I/O operations, for example:
 th->reset(kDefaultTimeout);
 int e = AVERROR(EAGAIN);
 while (AVERROR(EAGAIN) == e) 
  e = av_read_frame(ic, &packet);
 // If the time exceeds the limit, then the process interruped at the next IO operation.   
于 2013-01-28T11:03:54.590 回答
0

出现这个问题是因为 av_read_frame() 卡在网络无限循环中我遇到了同样的问题然后我使用了中断回调请参考示例代码

首先初始化你的上下文并设置中断回调

AVFormatContext *_formatCtx;

//Initialize format context
_formatCtx=avformat_alloc_context();

//Initialize intrrupt callback
AVIOInterruptCB icb={interruptCallBack,(__bridge void *)(self)};
_formatCtx->interrupt_callback=icb;

现在在你的回调中处理中断

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;

}
于 2014-06-02T13:59:29.927 回答