From 7328a813f99043266e799b6497c312a67fb0c4d6 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Tue, 31 Mar 2020 12:09:43 +1100 Subject: [PATCH] ffmpegencoder: fixed bug that failed to flush the audio encoder For some reason, when the encoder closed it only flushed the video encoder and not the audio encoder. Now it does both. --- app/codec/ffmpeg/ffmpegencoder.cpp | 45 ++++++++++++++++++------------ app/codec/ffmpeg/ffmpegencoder.h | 1 + 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/codec/ffmpeg/ffmpegencoder.cpp b/app/codec/ffmpeg/ffmpegencoder.cpp index 597ceb19a..b837d0f7f 100644 --- a/app/codec/ffmpeg/ffmpegencoder.cpp +++ b/app/codec/ffmpeg/ffmpegencoder.cpp @@ -422,25 +422,34 @@ bool FFmpegEncoder::SetupCodecContext(AVStream* stream, AVCodecContext* codec_ct void FFmpegEncoder::FlushEncoders() { if (video_codec_ctx_) { - avcodec_send_frame(video_codec_ctx_, nullptr); - AVPacket* pkt = av_packet_alloc(); - - int error_code; - do { - error_code = avcodec_receive_packet(video_codec_ctx_, pkt); - - if (error_code < 0) { - break; - } - - pkt->stream_index = video_stream_->index; - av_packet_rescale_ts(pkt, video_codec_ctx_->time_base, video_stream_->time_base); - av_interleaved_write_frame(fmt_ctx_, pkt); - av_packet_unref(pkt); - } while (error_code >= 0); - - av_packet_free(&pkt); + FlushCodecCtx(video_codec_ctx_, video_stream_); } + + if (audio_codec_ctx_) { + FlushCodecCtx(audio_codec_ctx_, audio_stream_); + } +} + +void FFmpegEncoder::FlushCodecCtx(AVCodecContext *codec_ctx, AVStream* stream) +{ + avcodec_send_frame(codec_ctx, nullptr); + AVPacket* pkt = av_packet_alloc(); + + int error_code; + do { + error_code = avcodec_receive_packet(codec_ctx, pkt); + + if (error_code < 0) { + break; + } + + pkt->stream_index = stream->index; + av_packet_rescale_ts(pkt, codec_ctx->time_base, stream->time_base); + av_interleaved_write_frame(fmt_ctx_, pkt); + av_packet_unref(pkt); + } while (error_code >= 0); + + av_packet_free(&pkt); } void FFmpegEncoder::Error(const QString &s) diff --git a/app/codec/ffmpeg/ffmpegencoder.h b/app/codec/ffmpeg/ffmpegencoder.h index 8da572954..6bd59e734 100644 --- a/app/codec/ffmpeg/ffmpegencoder.h +++ b/app/codec/ffmpeg/ffmpegencoder.h @@ -51,6 +51,7 @@ private: bool SetupCodecContext(AVStream *stream, AVCodecContext *codec_ctx, AVCodec *codec); void FlushEncoders(); + void FlushCodecCtx(AVCodecContext* codec_ctx, AVStream *stream); AVFormatContext* fmt_ctx_;