[FFmpeg-devel] [PATCH] lavf/dashenc: Write media trailers when DASH trailer is written.

Andrey Semashev andrey.semashev at gmail.com
Wed Nov 28 13:43:42 EET 2018


This commit ensures that all (potentially, long) filesystem activity is
performed when the user calls av_write_trailer on the DASH libavformat
context, not when freeing the context. Also, this defers media segment
deletion until after the media trailers are written.
---
 libavformat/dashenc.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 6ce70e0076..e1c959dc89 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -424,8 +424,6 @@ static void dash_free(AVFormatContext *s)
         return;
     for (i = 0; i < s->nb_streams; i++) {
         OutputStream *os = &c->streams[i];
-        if (os->ctx && os->ctx_inited)
-            av_write_trailer(os->ctx);
         if (os->ctx && os->ctx->pb)
             ffio_free_dyn_buf(&os->ctx->pb);
         ff_format_io_close(s, &os->out);
@@ -1420,13 +1418,11 @@ static int dash_flush(AVFormatContext *s, int final, int stream)
         os->pos += range_length;
     }
 
-    if (c->window_size || (final && c->remove_at_exit)) {
+    if (c->window_size) {
         for (i = 0; i < s->nb_streams; i++) {
             OutputStream *os = &c->streams[i];
             int j;
             int remove = os->nb_segments - c->window_size - c->extra_window_size;
-            if (final && c->remove_at_exit)
-                remove = os->nb_segments;
             if (remove > 0) {
                 for (j = 0; j < remove; j++) {
                     char filename[1024];
@@ -1599,11 +1595,24 @@ static int dash_write_trailer(AVFormatContext *s)
     }
     dash_flush(s, 1, -1);
 
+    for (int i = 0; i < s->nb_streams; ++i) {
+        OutputStream *os = &c->streams[i];
+        if (os->ctx && os->ctx_inited) {
+            av_write_trailer(os->ctx);
+        }
+    }
+
     if (c->remove_at_exit) {
         char filename[1024];
         int i;
         for (i = 0; i < s->nb_streams; i++) {
             OutputStream *os = &c->streams[i];
+            for (int j = 0; j < os->nb_segments; ++j) {
+                snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->segments[j]->file);
+                dashenc_delete_file(s, filename);
+                av_free(os->segments[j]);
+            }
+            os->nb_segments = 0;
             snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile);
             dashenc_delete_file(s, filename);
         }
-- 
2.19.1



More information about the ffmpeg-devel mailing list