av_read_frame 阻塞,基于回调的解决办法

设置回调,每次 av_read_frame 前刷新时间

static int InterruptFouction(void *theTimeSpec)
{
    if (!theTimeSpec) {
        return 0;
    }

    std::cout << "InterruptFouction called~!" << std::endl;
    time_t mtime;
    time(&mtime);
    time_t *t = (time_t*)theTimeSpec;
    if( ( mtime - *t ) > 5) {
        return 1;
    }
    return 0;
}

time_t TimeNow;
m_pFmtCtx->interrupt_callback.callback = InterruptFouction;
m_pFmtCtx->interrupt_callback.opaque = &TimeNow;

while(isRunning){
	time(&TimeNow);
	ret = av_read_frame(m_pFmtCtx, m_pAVPacket);
	if ( ret == 0 ) {
		// to do something
	} else {
		break;
	}
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
av_read_frame函数是FFmpeg库中用于读取音视频帧的函数。默认情况下,该函数会在没有可用的音视频帧时阻塞,直到有新的音视频帧可用为止。如果你想让av_read_frame函数立即返回而不阻塞,可以通过设置回调函数来实现。 方法一:设置回调函数 你可以通过设置interrupt_callback回调函数来打断av_read_frame函数的阻塞。具体步骤如下: 1. 定义一个回调函数,用于打断av_read_frame函数的阻塞回调函数的原型如下: ```c int interrupt_cb(void *opaque); ``` 2. 在设置interrupt_callback之前,先设置AVFormatContext的opaque字段,将回调函数所需的上下文信息传递给回调函数。例如: ```c m_avformatCtx->interrupt_callback.opaque = this; ``` 3. 设置interrupt_callback回调函数,将回调函数的指针赋值给interrupt_callback.callback字段。例如: ```c m_avformatCtx->interrupt_callback.callback = interrupt_cb; ``` 4. 在回调函数中实现打断逻辑。当回调函数返回非零值时,av_read_frame函数将立即返回。 方法二:设置超时 除了设置回调函数外,你还可以通过设置超时时间来让av_read_frame函数在超过指定时间后立即返回。具体步骤如下: 1. 使用av_dict_set函数设置超时时间。例如: ```c av_dict_set(&inputDic, "stimeout", "2000000", 0); // 设置超时时间为2秒 ``` 2. 在avformat_open_input函数调用之前,将设置好的字典传递给该函数。 这两种方法的区别在于,设置回调函数可以在任何情况下打断av_read_frame函数的阻塞,而设置超时只能在超过指定时间后打断阻塞
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值