我正在使用 ffmpeg 实现一个(非常)低延迟的视频流 C++ 应用程序。客户端接收到一个使用 x264 的 zerolatency 预设编码的视频,因此不需要缓冲。如上所述这里,如果你使用av_read_frame()来读取编码视频流的数据包,你将永远有,因为在做的ffmpeg内部缓冲的至少一个帧的延迟。因此,当我在帧 n+1 发送到客户端后调用av_read_frame () 时,该函数将返回帧 n。
通过设置 AVFormatContext 标志 AVFMT_FLAG_NOPARSE 来摆脱这种缓冲 | AVFMT_FLAG_NOFILLIN如在建议源禁用分组解析,并因此中断解码,如在所提到的源。
因此,我正在编写自己的数据包接收器和解析器。首先,这里是使用av_read_frame ()的工作解决方案(包括一帧延迟)的相关步骤:
AVFormatContext *fctx;
AVCodecContext *cctx;
AVPacket *pkt;
AVFrame *frm;
//Initialization of AV structures
//…
//Main Loop
while(true){
//Receive packet
av_read_frame(fctx, pkt);
//Decode:
avcodec_send_packet(cctx, pkt);
avcodec_receive_frame(cctx, frm);
//Display frame
//…
}
Run Code Online (Sandbox Code Playgroud)
下面是我的解决方案,它模仿了av_read_frame ()的行为,尽我所能重现它。我能够跟踪av_read_frame ()的源代码到ff_read_packet (),但是我找不到AVInputformat.read_packet ()的源代码。
int tcpsocket;
AVCodecContext *cctx;
AVPacket *pkt;
AVFrame *frm;
uint8_t …Run Code Online (Sandbox Code Playgroud)