[FFmpeg-devel] [PATCH 2/3] avformat/segment: Ensure write_trailer is called

sebechlebskyjan at gmail.com sebechlebskyjan at gmail.com
Mon Jun 20 17:24:06 CEST 2016


From: Jan Sebechlebsky <sebechlebskyjan at gmail.com>

Ensure that write_trailer is always called after successful
write_header operation so underlying muxer is deinitialized.

Signed-off-by: Jan Sebechlebsky <sebechlebskyjan at gmail.com>
---
 This is a little tricky - we have to ensure that 
 write_trailer is called if the write_header succeeded, however
 if the io_open fails the AVIOContext is invalid and cause
 underlying muxer to crash (the muxer may assume that AVIOContext 
 is initialized). So temporarily AVIOContext is initialized to null
 context just to allow write_trailer to finalize underlying muxer
 successfully.

 libavformat/segment.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index 7e4e840..df37a56 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -763,6 +763,8 @@ static int seg_init(AVFormatContext *s)
         av_log(s, AV_LOG_ERROR,
                "Some of the provided format options in '%s' are not recognized\n", seg->format_options_str);
         ret = AVERROR(EINVAL);
+        av_write_trailer(oc);
+        ff_format_io_close(oc, &oc->pb);
         goto fail;
     }
 
@@ -785,8 +787,14 @@ static int seg_init(AVFormatContext *s)
         } else {
             close_null_ctxp(&oc->pb);
         }
-        if ((ret = oc->io_open(oc, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0)
+        if ((ret = oc->io_open(oc, &oc->pb, oc->filename, AVIO_FLAG_WRITE, NULL)) < 0) {
+            // Some muxers rely on valid AVIOContext so we need to create one
+            // before performing write trailer
+            open_null_ctx(&oc->pb);
+            av_write_trailer(oc);
+            close_null_ctxp(&oc->pb);
             goto fail;
+        }
         if (!seg->individual_header_trailer)
             oc->pb->seekable = 0;
     }
-- 
1.9.1



More information about the ffmpeg-devel mailing list