diff mbox

[FFmpeg-devel] avformat/hlsenc: write fmp4 init header after first AV frame

Message ID 20171112132935.57691-1-lq@chinaffmpeg.org
State New
Headers show

Commit Message

Liu Steven Nov. 12, 2017, 1:29 p.m. UTC
fix ticket id: 6825

Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavformat/hlsenc.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

Comments

Aman Karmani Nov. 12, 2017, 10:35 p.m. UTC | #1
On Sun, Nov 12, 2017 at 5:29 AM, Steven Liu <lq@chinaffmpeg.org> wrote:

> fix ticket id: 6825
>
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>  libavformat/hlsenc.c | 28 +++++++++++++++++++++++++---
>  1 file changed, 25 insertions(+), 3 deletions(-)
>
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 5ea9d216a4..3a4c8d65be 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -108,6 +108,9 @@ typedef struct HLSContext {
>      uint32_t start_sequence_source_type;  // enum StartSequenceSourceType
>      AVOutputFormat *oformat;
>      AVOutputFormat *vtt_oformat;
> +    AVIOContext *out;
> +    int packets_written;
> +    int init_range_length;
>
>      AVFormatContext *avf;
>      AVFormatContext *vtt_avf;
> @@ -607,9 +610,14 @@ static int hls_mux_init(AVFormatContext *s)
>              av_log(s, AV_LOG_WARNING, "Multi-file byterange mode is
> currently unsupported in the HLS muxer.\n");
>              return AVERROR_PATCHWELCOME;
>          }
> +        hls->packets_written = 0;
> +        hls->init_range_length = 0;
>          hls->fmp4_init_mode = !byterange_mode;
>          set_http_options(s, &options, hls);
> -        if ((ret = s->io_open(s, &oc->pb, hls->base_output_dirname,
> AVIO_FLAG_WRITE, &options)) < 0) {
> +        if ((ret = avio_open_dyn_buf(&oc->pb)) < 0)
> +            return ret;
> +
> +        if ((ret = s->io_open(s, &hls->out, hls->base_output_dirname,
> AVIO_FLAG_WRITE, &options)) < 0) {
>              av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n",
> hls->fmp4_init_filename);
>              return ret;
>          }
> @@ -634,6 +642,7 @@ static int hls_mux_init(AVFormatContext *s)
>              av_dict_free(&options);
>              return AVERROR(EINVAL);
>          }
> +        avio_flush(oc->pb);
>          av_dict_free(&options);
>      }
>      return 0;
> @@ -1600,6 +1609,8 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>      int is_ref_pkt = 1;
>      int ret = 0, can_split = 1;
>      int stream_index = 0;
> +    int range_length = 0;
> +    uint8_t *buffer = NULL;
>
>      if (hls->sequence - hls->nb_entries > hls->start_sequence &&
> hls->init_time > 0) {
>          /* reset end_pts, hls->recording_time at end of the init hls list
> */
> @@ -1645,7 +1656,7 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>          }
>
>      }
> -    if (hls->fmp4_init_mode || can_split && av_compare_ts(pkt->pts -
> hls->start_pts, st->time_base,
> +    if (hls->packets_written && can_split && av_compare_ts(pkt->pts -
> hls->start_pts, st->time_base,
>                                     end_pts, AV_TIME_BASE_Q) >= 0) {
>          int64_t new_start_pos;
>          char *old_filename = av_strdup(hls->avf->filename);
> @@ -1661,7 +1672,17 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>          hls->size = new_start_pos - hls->start_pos;
>
>          if (!byterange_mode) {
> -            ff_format_io_close(s, &oc->pb);
> +            if (hls->segment_type == SEGMENT_TYPE_FMP4 &&
> !hls->init_range_length) {
> +                avio_flush(oc->pb);
> +                range_length = avio_close_dyn_buf(oc->pb, &buffer);
> +                avio_write(hls->out, buffer, range_length);
> +                hls->init_range_length = range_length;
> +                avio_open_dyn_buf(&oc->pb);
> +                hls->packets_written = 0;
> +                ff_format_io_close(s, &hls->out);
> +            } else {
> +                ff_format_io_close(s, &oc->pb);
> +            }
>              if (hls->vtt_avf) {
>                  ff_format_io_close(s, &hls->vtt_avf->pb);
>              }
> @@ -1719,6 +1740,7 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>              }
>      }
>
> +    hls->packets_written++;
>      ret = ff_write_chained(oc, stream_index, pkt, s, 0);
>
>      return ret;
>

Patch LGTM. I confirmed it fixes the issue in ticket 6825


> --
> 2.11.0 (Apple Git-81)
>
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Liu Steven Nov. 14, 2017, 2:40 a.m. UTC | #2
> 在 2017年11月13日,06:35,Aman Gupta <ffmpeg@tmm1.net> 写道:
> 
> On Sun, Nov 12, 2017 at 5:29 AM, Steven Liu <lq@chinaffmpeg.org> wrote:
> 
>> fix ticket id: 6825
>> 
>> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
>> ---
>> libavformat/hlsenc.c | 28 +++++++++++++++++++++++++---
>> 1 file changed, 25 insertions(+), 3 deletions(-)
>> 
>> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
>> index 5ea9d216a4..3a4c8d65be 100644
>> --- a/libavformat/hlsenc.c
>> +++ b/libavformat/hlsenc.c
>> @@ -108,6 +108,9 @@ typedef struct HLSContext {
>>     uint32_t start_sequence_source_type;  // enum StartSequenceSourceType
>>     AVOutputFormat *oformat;
>>     AVOutputFormat *vtt_oformat;
>> +    AVIOContext *out;
>> +    int packets_written;
>> +    int init_range_length;
>> 
>>     AVFormatContext *avf;
>>     AVFormatContext *vtt_avf;
>> @@ -607,9 +610,14 @@ static int hls_mux_init(AVFormatContext *s)
>>             av_log(s, AV_LOG_WARNING, "Multi-file byterange mode is
>> currently unsupported in the HLS muxer.\n");
>>             return AVERROR_PATCHWELCOME;
>>         }
>> +        hls->packets_written = 0;
>> +        hls->init_range_length = 0;
>>         hls->fmp4_init_mode = !byterange_mode;
>>         set_http_options(s, &options, hls);
>> -        if ((ret = s->io_open(s, &oc->pb, hls->base_output_dirname,
>> AVIO_FLAG_WRITE, &options)) < 0) {
>> +        if ((ret = avio_open_dyn_buf(&oc->pb)) < 0)
>> +            return ret;
>> +
>> +        if ((ret = s->io_open(s, &hls->out, hls->base_output_dirname,
>> AVIO_FLAG_WRITE, &options)) < 0) {
>>             av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n",
>> hls->fmp4_init_filename);
>>             return ret;
>>         }
>> @@ -634,6 +642,7 @@ static int hls_mux_init(AVFormatContext *s)
>>             av_dict_free(&options);
>>             return AVERROR(EINVAL);
>>         }
>> +        avio_flush(oc->pb);
>>         av_dict_free(&options);
>>     }
>>     return 0;
>> @@ -1600,6 +1609,8 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>     int is_ref_pkt = 1;
>>     int ret = 0, can_split = 1;
>>     int stream_index = 0;
>> +    int range_length = 0;
>> +    uint8_t *buffer = NULL;
>> 
>>     if (hls->sequence - hls->nb_entries > hls->start_sequence &&
>> hls->init_time > 0) {
>>         /* reset end_pts, hls->recording_time at end of the init hls list
>> */
>> @@ -1645,7 +1656,7 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>         }
>> 
>>     }
>> -    if (hls->fmp4_init_mode || can_split && av_compare_ts(pkt->pts -
>> hls->start_pts, st->time_base,
>> +    if (hls->packets_written && can_split && av_compare_ts(pkt->pts -
>> hls->start_pts, st->time_base,
>>                                    end_pts, AV_TIME_BASE_Q) >= 0) {
>>         int64_t new_start_pos;
>>         char *old_filename = av_strdup(hls->avf->filename);
>> @@ -1661,7 +1672,17 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>         hls->size = new_start_pos - hls->start_pos;
>> 
>>         if (!byterange_mode) {
>> -            ff_format_io_close(s, &oc->pb);
>> +            if (hls->segment_type == SEGMENT_TYPE_FMP4 &&
>> !hls->init_range_length) {
>> +                avio_flush(oc->pb);
>> +                range_length = avio_close_dyn_buf(oc->pb, &buffer);
>> +                avio_write(hls->out, buffer, range_length);
>> +                hls->init_range_length = range_length;
>> +                avio_open_dyn_buf(&oc->pb);
>> +                hls->packets_written = 0;
>> +                ff_format_io_close(s, &hls->out);
>> +            } else {
>> +                ff_format_io_close(s, &oc->pb);
>> +            }
>>             if (hls->vtt_avf) {
>>                 ff_format_io_close(s, &hls->vtt_avf->pb);
>>             }
>> @@ -1719,6 +1740,7 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>             }
>>     }
>> 
>> +    hls->packets_written++;
>>     ret = ff_write_chained(oc, stream_index, pkt, s, 0);
>> 
>>     return ret;
>> 
> 
> Patch LGTM. I confirmed it fixes the issue in ticket 6825
Pushed



Thanks
> 
> 
>> --
>> 2.11.0 (Apple Git-81)
>> 
>> 
>> 
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
diff mbox

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 5ea9d216a4..3a4c8d65be 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -108,6 +108,9 @@  typedef struct HLSContext {
     uint32_t start_sequence_source_type;  // enum StartSequenceSourceType
     AVOutputFormat *oformat;
     AVOutputFormat *vtt_oformat;
+    AVIOContext *out;
+    int packets_written;
+    int init_range_length;
 
     AVFormatContext *avf;
     AVFormatContext *vtt_avf;
@@ -607,9 +610,14 @@  static int hls_mux_init(AVFormatContext *s)
             av_log(s, AV_LOG_WARNING, "Multi-file byterange mode is currently unsupported in the HLS muxer.\n");
             return AVERROR_PATCHWELCOME;
         }
+        hls->packets_written = 0;
+        hls->init_range_length = 0;
         hls->fmp4_init_mode = !byterange_mode;
         set_http_options(s, &options, hls);
-        if ((ret = s->io_open(s, &oc->pb, hls->base_output_dirname, AVIO_FLAG_WRITE, &options)) < 0) {
+        if ((ret = avio_open_dyn_buf(&oc->pb)) < 0)
+            return ret;
+
+        if ((ret = s->io_open(s, &hls->out, hls->base_output_dirname, AVIO_FLAG_WRITE, &options)) < 0) {
             av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", hls->fmp4_init_filename);
             return ret;
         }
@@ -634,6 +642,7 @@  static int hls_mux_init(AVFormatContext *s)
             av_dict_free(&options);
             return AVERROR(EINVAL);
         }
+        avio_flush(oc->pb);
         av_dict_free(&options);
     }
     return 0;
@@ -1600,6 +1609,8 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
     int is_ref_pkt = 1;
     int ret = 0, can_split = 1;
     int stream_index = 0;
+    int range_length = 0;
+    uint8_t *buffer = NULL;
 
     if (hls->sequence - hls->nb_entries > hls->start_sequence && hls->init_time > 0) {
         /* reset end_pts, hls->recording_time at end of the init hls list */
@@ -1645,7 +1656,7 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         }
 
     }
-    if (hls->fmp4_init_mode || can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
+    if (hls->packets_written && can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
                                    end_pts, AV_TIME_BASE_Q) >= 0) {
         int64_t new_start_pos;
         char *old_filename = av_strdup(hls->avf->filename);
@@ -1661,7 +1672,17 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
         hls->size = new_start_pos - hls->start_pos;
 
         if (!byterange_mode) {
-            ff_format_io_close(s, &oc->pb);
+            if (hls->segment_type == SEGMENT_TYPE_FMP4 && !hls->init_range_length) {
+                avio_flush(oc->pb);
+                range_length = avio_close_dyn_buf(oc->pb, &buffer);
+                avio_write(hls->out, buffer, range_length);
+                hls->init_range_length = range_length;
+                avio_open_dyn_buf(&oc->pb);
+                hls->packets_written = 0;
+                ff_format_io_close(s, &hls->out);
+            } else {
+                ff_format_io_close(s, &oc->pb);
+            }
             if (hls->vtt_avf) {
                 ff_format_io_close(s, &hls->vtt_avf->pb);
             }
@@ -1719,6 +1740,7 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
             }
     }
 
+    hls->packets_written++;
     ret = ff_write_chained(oc, stream_index, pkt, s, 0);
 
     return ret;