ffmpeg av_read_frame() 需要很长时间才能停止 [英] ffmpeg av_read_frame() need very long time to stop

查看:26
本文介绍了ffmpeg av_read_frame() 需要很长时间才能停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use ffmpeg to decode RTSP video.It likes that: When it's on the end of file,it block in the av_read_frame() for a long time,why?

推荐答案

各种原因都会导致长时间阻塞.但是你可以控制一个 I/O 层的处理时间.

Various reasons can cause long blocking. But you can control the processing time for a I/O layer.

使用结构AVFormatContext::interrupt_callback来设置中断处理程序.

Use the structure AVFormatContext::interrupt_callback to set the interrupt handler.

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.   

这篇关于ffmpeg av_read_frame() 需要很长时间才能停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆