[FFmpeg-devel] [PATCH] avformat/hlsenc: move init file write code block to hls_write_header

Steven Liu lq at chinaffmpeg.org
Thu May 7 13:47:49 EEST 2020


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

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 5695c6cc95..a1eddade7b 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -2266,6 +2266,26 @@ static int hls_write_header(AVFormatContext *s)
                 }
             }
         }
+        if (hls->segment_type == SEGMENT_TYPE_FMP4 && !vs->init_range_length) {
+            int range_length = 0;
+            int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
+            av_write_frame(vs->avf, NULL); /* Flush any buffered data */
+            avio_tell(vs->avf->pb);
+            avio_flush(vs->avf->pb);
+            range_length = avio_close_dyn_buf(vs->avf->pb, &vs->init_buffer);
+            if (range_length <= 0)
+                return AVERROR(EINVAL);
+            avio_write(vs->out, vs->init_buffer, range_length);
+            if (!hls->resend_init_file)
+                av_freep(&vs->init_buffer);
+            vs->init_range_length = range_length;
+            avio_open_dyn_buf(&vs->avf->pb);
+            vs->packets_written = 0;
+            vs->start_pos = range_length;
+            if (!byterange_mode) {
+                hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
+            }
+        }
     }
 
     return ret;
@@ -2383,23 +2403,6 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         new_start_pos = avio_tell(oc->pb);
         vs->size = new_start_pos - vs->start_pos;
         avio_flush(oc->pb);
-        if (hls->segment_type == SEGMENT_TYPE_FMP4) {
-            if (!vs->init_range_length) {
-                range_length = avio_close_dyn_buf(oc->pb, &vs->init_buffer);
-                if (range_length <= 0)
-                    return AVERROR(EINVAL);
-                avio_write(vs->out, vs->init_buffer, range_length);
-                if (!hls->resend_init_file)
-                    av_freep(&vs->init_buffer);
-                vs->init_range_length = range_length;
-                avio_open_dyn_buf(&oc->pb);
-                vs->packets_written = 0;
-                vs->start_pos = range_length;
-                if (!byterange_mode) {
-                    hlsenc_io_close(s, &vs->out, vs->base_output_dirname);
-                }
-            }
-        }
         if (!byterange_mode) {
             if (vs->vtt_avf) {
                 hlsenc_io_close(s, &vs->vtt_avf->pb, vs->vtt_avf->url);
-- 
2.25.0





More information about the ffmpeg-devel mailing list