我使用ffmpeg解码RTSP视频。它喜欢: 当它在文件末尾时,它会在av_read_frame()中长时间阻塞,为什么?
答案 0 :(得分:6)
各种原因可能导致长时间阻塞。但是您可以控制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.
答案 1 :(得分: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;
}