From 7e50bcbcce12248de6626d0b451c992b4d2ddf34 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Mon, 18 Sep 2017 16:30:29 +1000 Subject: [PATCH] motion: write trailer even if codec drain fails --- package/motion/0013-drain-codec.patch | 55 +++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/package/motion/0013-drain-codec.patch b/package/motion/0013-drain-codec.patch index e9c17357cb..480403a1ea 100644 --- a/package/motion/0013-drain-codec.patch +++ b/package/motion/0013-drain-codec.patch @@ -1,11 +1,11 @@ -commit 9c604dc427804b9620044a2bdddca1b8c1831627 +commit ff55aecd6fb88a030790b494dcd55bd99cace52b Author: Joo Aun Saw -Date: Mon Sep 18 14:30:22 2017 +1000 +Date: Mon Sep 18 16:25:44 2017 +1000 drain codec at end of recording diff --git a/ffmpeg.c b/ffmpeg.c -index aa06bef..c690896 100644 +index aa06bef..ecdc3e5 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -350,7 +350,7 @@ static int ffmpeg_write_packet(struct ffmpeg *ffmpeg){ @@ -46,7 +46,20 @@ index aa06bef..c690896 100644 } if (retcd < 0 ){ av_strerror(retcd, errstr, sizeof(errstr)); -@@ -404,7 +409,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ +@@ -385,7 +390,11 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ + if (retcd < 0) { + MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Error while writing video frame"); + my_packet_unref(ffmpeg->pkt); +- ffmpeg_free_context(ffmpeg); ++ /* Do not free ffmpeg context if we are draining codec. We still ++ * need to write trailer even if draining fails. ++ */ ++ if (picture != NULL) ++ ffmpeg_free_context(ffmpeg); + return -1; + } + } +@@ -404,7 +413,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ ffmpeg->pkt.data = NULL; ffmpeg->pkt.size = 0; @@ -55,7 +68,20 @@ index aa06bef..c690896 100644 if (retcd < 0 ){ av_strerror(retcd, errstr, sizeof(errstr)); MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Error encoding video:%s",errstr); -@@ -441,7 +446,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ +@@ -421,7 +430,11 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ + if (retcd < 0) { + MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Error while writing video frame"); + my_packet_unref(ffmpeg->pkt); +- ffmpeg_free_context(ffmpeg); ++ /* Do not free ffmpeg context if we are draining codec. We still ++ * need to write trailer even if draining fails. ++ */ ++ if (picture != NULL) ++ ffmpeg_free_context(ffmpeg); + return -1; + } + +@@ -441,7 +454,7 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ video_outbuf_size = (ffmpeg->ctx_codec->width +16) * (ffmpeg->ctx_codec->height +16) * 1; video_outbuf = mymalloc(video_outbuf_size); @@ -64,7 +90,7 @@ index aa06bef..c690896 100644 if (retcd < 0 ){ MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Error encoding video"); my_packet_unref(ffmpeg->pkt); -@@ -457,11 +462,12 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ +@@ -457,11 +470,12 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ ffmpeg->pkt.size = retcd; ffmpeg->pkt.data = video_outbuf; @@ -82,7 +108,20 @@ index aa06bef..c690896 100644 retcd = ffmpeg_write_packet(ffmpeg); if (retcd < 0) { -@@ -817,7 +823,7 @@ static int ffmpeg_put_frame(struct ffmpeg *ffmpeg, const struct timeval *tv1){ +@@ -469,7 +483,11 @@ static int ffmpeg_encode_video(struct ffmpeg *ffmpeg){ + MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Error while writing video frame"); + my_packet_unref(ffmpeg->pkt); + free(video_outbuf); +- ffmpeg_free_context(ffmpeg); ++ /* Do not free ffmpeg context if we are draining codec. We still ++ * need to write trailer even if draining fails. ++ */ ++ if (picture != NULL) ++ ffmpeg_free_context(ffmpeg); + return -1; + } + +@@ -817,7 +835,7 @@ static int ffmpeg_put_frame(struct ffmpeg *ffmpeg, const struct timeval *tv1){ return 0; } @@ -91,7 +130,7 @@ index aa06bef..c690896 100644 if (retcd < 0){ if (retcd != -2) MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Error while encoding picture"); -@@ -957,6 +963,7 @@ void ffmpeg_close(struct ffmpeg *ffmpeg){ +@@ -957,6 +975,7 @@ void ffmpeg_close(struct ffmpeg *ffmpeg){ #ifdef HAVE_FFMPEG if (ffmpeg != NULL) {