【问题标题】:ffmpeg av_read_frame() need very long time to stopffmpeg av_read_frame() 需要很长时间才能停止
【发布时间】:2013-01-28 08:30:18
【问题描述】:

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

【问题讨论】:

  • while(av_read_frame()>=0)

标签: ffmpeg


【解决方案1】:

各种原因都可能导致长时间阻塞。但您可以控制 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.   

【讨论】:

  • 这个问题已经解决了。服务器发送“BYE”包,然后av_read_frame()就可以过去了。但是如何发送“TeerDown”包给服务器呢?
  • 如果服务器发送BYE数据包,那么av_read_frame应该返回AVERROR_EOF
【解决方案2】:

这个问题是因为 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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2013-10-11
    • 2012-12-04
    • 2012-12-03
    • 2014-07-21
    • 2019-10-26
    • 2012-06-24
    相关资源
    最近更新 更多