Browse Source

obs-ffmpeg: Fix memory leak with mpegts

Fixes #7305.
Some copied data was not freed and also not all paths freed AVPackets.

Signed-off-by: pkv <[email protected]>
pkv 3 years ago
parent
commit
d0366671e4
1 changed files with 13 additions and 11 deletions
  1. 13 11
      plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c

+ 13 - 11
plugins/obs-ffmpeg/obs-ffmpeg-mpegts.c

@@ -729,18 +729,16 @@ static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
 static int mpegts_process_packet(struct ffmpeg_output *output)
 {
 	AVPacket *packet = NULL;
-	bool new_packet = false;
-	int ret;
+	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,
@@ -751,13 +749,17 @@ static int mpegts_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;
+	uint8_t *buf = packet->data;
 	ret = av_interleaved_write_frame(output->ff_data.output, packet);
+	av_freep(&buf);
+
 	if (ret < 0) {
-		av_packet_free(&packet);
 		ffmpeg_mpegts_log_error(
 			LOG_WARNING, &output->ff_data,
 			"process_packet: Error writing packet: %s",
@@ -766,12 +768,12 @@ static int mpegts_process_packet(struct ffmpeg_output *output)
 		/* Treat "Invalid data found when processing input" and
 		 * "Invalid argument" as non-fatal */
 		if (ret == AVERROR_INVALIDDATA || ret == -EINVAL) {
-			return 0;
+			ret = 0;
 		}
-		return ret;
 	}
-
-	return 0;
+end:
+	av_packet_free(&packet);
+	return ret;
 }
 
 static void *write_thread(void *data)