[FFmpeg-devel] [PATCH] avformat/hlsenc: fix hls_flags temp_file bug

Steven Liu lq at chinaffmpeg.org
Tue Feb 21 17:13:26 EET 2017


refer to ticket id: #6170

rename file from temp to origin name after complete current segment

Signed-off-by: Steven Liu <lq at chinaffmpeg.org>
---
 libavformat/hlsenc.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index e673f59..712a01b 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -691,6 +691,17 @@ static void write_m3u8_head_block(HLSContext *hls, AVIOContext *out, int version
     av_log(hls, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence);
 }
 
+static void hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
+{
+    size_t len = strlen(oc->filename);
+    char final_filename[sizeof(oc->filename)];
+
+    av_strlcpy(final_filename, oc->filename, len);
+    final_filename[len-4] = '\0';
+    ff_rename(oc->filename, final_filename, s);
+    oc->filename[len-4] = '\0';
+}
+
 static int hls_window(AVFormatContext *s, int last)
 {
     HLSContext *hls = s->priv_data;
@@ -833,15 +844,6 @@ static int hls_start(AVFormatContext *s)
     char *filename, iv_string[KEYSIZE*2 + 1];
     int err = 0;
 
-    if ((c->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) {
-        size_t len = strlen(oc->filename);
-        char final_filename[sizeof(oc->filename)];
-        av_strlcpy(final_filename, oc->filename, len);
-        final_filename[len-4] = '\0';
-        ff_rename(oc->filename, final_filename, s);
-        oc->filename[len-4] = '\0';
-    }
-
     if (c->flags & HLS_SINGLE_FILE) {
         av_strlcpy(oc->filename, c->basename,
                    sizeof(oc->filename));
@@ -962,6 +964,7 @@ static int hls_start(AVFormatContext *s)
         av_strlcat(oc->filename, ".tmp", sizeof(oc->filename));
     }
 
+
     if (c->key_info_file) {
         if ((err = hls_encryption_start(s)) < 0)
             goto fail;
@@ -1325,6 +1328,11 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
 
         new_start_pos = avio_tell(hls->avf->pb);
         hls->size = new_start_pos - hls->start_pos;
+
+        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) {
+            hls_rename_temp_file(s, oc);
+        }
+
         ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
         hls->start_pos = new_start_pos;
         if (ret < 0) {
@@ -1402,6 +1410,11 @@ static int hls_write_trailer(struct AVFormatContext *s)
     if (oc->pb) {
         hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
         ff_format_io_close(s, &oc->pb);
+
+        if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) {
+            hls_rename_temp_file(s, oc);
+        }
+
         /* after av_write_trailer, then duration + 1 duration per packet */
         hls_append_segment(s, hls, hls->duration + hls->dpp, hls->start_pos, hls->size);
     }
@@ -1411,15 +1424,6 @@ static int hls_write_trailer(struct AVFormatContext *s)
          ff_rename(old_filename, hls->avf->filename, hls);
     }
 
-    if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) {
-        size_t len = strlen(oc->filename);
-        char final_filename[sizeof(oc->filename)];
-        av_strlcpy(final_filename, oc->filename, len);
-        final_filename[len-4] = '\0';
-        ff_rename(oc->filename, final_filename, s);
-        oc->filename[len-4] = '\0';
-    }
-
     if (vtt_oc) {
         if (vtt_oc->pb)
             av_write_trailer(vtt_oc);
-- 
2.10.1.382.ga23ca1b.dirty





More information about the ffmpeg-devel mailing list