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

Aman Gupta ffmpeg at tmm1.net
Mon Nov 13 00:35:31 EET 2017


On Sun, Nov 12, 2017 at 5:29 AM, Steven Liu <lq at chinaffmpeg.org> wrote:

> fix ticket id: 6825
>
> Signed-off-by: Steven Liu <lq at 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 at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


More information about the ffmpeg-devel mailing list