ffmpeg av_read_frame() need very long time to stop

后端 未结 2 1863
执笔经年
执笔经年 2021-01-02 12:55

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?

相关标签:
2条回答
  • 2021-01-02 13:13

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

    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.   
    
    0 讨论(0)
  • 2021-01-02 13:14

    This problem come because av_read_frame() stuck in network infinite loop I got the same problem then I have used interrupt call back please refer the sample code

    First initialize your context and set interrupt call back

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

    now handle the interrupt in your callback

    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;
    
    }
    
    0 讨论(0)
提交回复
热议问题