From f5be6f5fdd2fe34b18518ecd38030f0768845688 Mon Sep 17 00:00:00 2001 From: fuyingqi Date: Thu, 18 Aug 2022 15:01:03 +0800 Subject: [PATCH] obs-ffmpeg: Fix ffmpeg_output memory leak The memory leak was introduced by a commit ba68eda59 to use av_packet_alloc because av_init_packet got deprecated. Also removes a boolean flag `new_packet` and use the pointer `packet`, which is introduced by ba68eda59, to indicate there is a new packet. Co-authored-by: Norihiro Kamae --- plugins/obs-ffmpeg/obs-ffmpeg-output.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/obs-ffmpeg/obs-ffmpeg-output.c b/plugins/obs-ffmpeg/obs-ffmpeg-output.c index 7d28ad669..e0add46b8 100644 --- a/plugins/obs-ffmpeg/obs-ffmpeg-output.c +++ b/plugins/obs-ffmpeg/obs-ffmpeg-output.c @@ -1011,19 +1011,17 @@ static uint64_t get_packet_sys_dts(struct ffmpeg_output *output, static int process_packet(struct ffmpeg_output *output) { - AVPacket *packet; - bool new_packet = false; - int ret; + AVPacket *packet = NULL; + int ret = 0; pthread_mutex_lock(&output->write_mutex); if (output->packets.num) { packet = output->packets.array[0]; da_erase(output->packets, 0); - new_packet = true; } pthread_mutex_unlock(&output->write_mutex); - if (!new_packet) + if (!packet) return 0; /*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, " @@ -1033,22 +1031,24 @@ static int process_packet(struct ffmpeg_output *output) if (stopping(output)) { uint64_t sys_ts = get_packet_sys_dts(output, packet); - if (sys_ts >= output->stop_ts) - return 0; + if (sys_ts >= output->stop_ts) { + ret = 0; + goto end; + } } output->total_bytes += packet->size; ret = av_interleaved_write_frame(output->ff_data.output, packet); if (ret < 0) { - av_packet_free(&packet); ffmpeg_log_error(LOG_WARNING, &output->ff_data, "process_packet: Error writing packet: %s", av_err2str(ret)); - return ret; } - return 0; +end: + av_packet_free(&packet); + return ret; } static void *write_thread(void *data)