Browse Source

obs-ffmpeg: Fix possible NVENC crash

If the codec hasn't even been fully initialized, calling these functions
in the shutdown code could cause a crash.
Richard Stanway 9 years ago
parent
commit
a8020b37be
1 changed files with 14 additions and 7 deletions
  1. 14 7
      plugins/obs-ffmpeg/obs-ffmpeg-nvenc.c

+ 14 - 7
plugins/obs-ffmpeg/obs-ffmpeg-nvenc.c

@@ -54,6 +54,7 @@ struct nvenc_encoder {
 
 	int                            height;
 	bool                           first_packet;
+	bool                           initialized;
 };
 
 static const char *nvenc_getname(void *unused)
@@ -113,6 +114,8 @@ static bool nvenc_init_codec(struct nvenc_encoder *enc)
 		return false;
 	}
 
+	enc->initialized = true;
+
 	*((AVPicture*)enc->vframe) = enc->dst_picture;
 	return true;
 }
@@ -233,15 +236,19 @@ static bool nvenc_update(void *data, obs_data_t *settings)
 static void nvenc_destroy(void *data)
 {
 	struct nvenc_encoder *enc = data;
-	AVPacket pkt = {0};
-	int r_pkt = 1;
 
-	while (r_pkt) {
-		if (avcodec_encode_video2(enc->context, &pkt, NULL, &r_pkt) < 0)
-			break;
+	if (enc->initialized) {
+		AVPacket pkt = {0};
+		int r_pkt = 1;
+
+		while (r_pkt) {
+			if (avcodec_encode_video2(enc->context, &pkt, NULL,
+						&r_pkt) < 0)
+				break;
 
-		if (r_pkt)
-			av_free_packet(&pkt);
+			if (r_pkt)
+				av_free_packet(&pkt);
+		}
 	}
 
 	avcodec_close(enc->context);