Message ID | 20190829231428.27877-2-lq@chinaffmpeg.org |
---|---|
State | New |
Headers | show |
On Thu, Aug 29, 2019 at 6:15 PM Steven Liu <lq@chinaffmpeg.org> wrote: > fix ticket: 7975 > > Signed-off-by: Steven Liu <lq@chinaffmpeg.org> > --- > libavformat/hlsenc.c | 66 > +++++++++++++++++++++++++++++++++++++++++++--------- > 1 file changed, 55 insertions(+), 11 deletions(-) > > diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c > index c6bd664b92..d9b48f0e7c 100644 > --- a/libavformat/hlsenc.c > +++ b/libavformat/hlsenc.c > @@ -118,6 +118,7 @@ typedef struct VariantStream { > AVIOContext *out; > int packets_written; > int init_range_length; > + uint8_t *temp_buffer; > > AVFormatContext *avf; > AVFormatContext *vtt_avf; > @@ -262,11 +263,12 @@ static int hlsenc_io_open(AVFormatContext *s, > AVIOContext **pb, char *filename, > return err; > } > > -static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char > *filename) { > +static int hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char > *filename) { > HLSContext *hls = s->priv_data; > int http_base_proto = filename ? ff_is_http_proto(filename) : 0; > + int ret = 0; > if (!*pb) > - return; > + return ret; > if (!http_base_proto || !hls->http_persistent || hls->key_info_file > || hls->encrypt) { > ff_format_io_close(s, pb); > #if CONFIG_HTTP_PROTOCOL > @@ -275,8 +277,10 @@ static void hlsenc_io_close(AVFormatContext *s, > AVIOContext **pb, char *filename > av_assert0(http_url_context); > avio_flush(*pb); > ffurl_shutdown(http_url_context, AVIO_FLAG_WRITE); > + ret = ff_http_get_shutdown_status(http_url_context); > #endif > } > + return ret; > } > > static void set_http_options(AVFormatContext *s, AVDictionary **options, > HLSContext *c) > @@ -447,7 +451,6 @@ static void write_styp(AVIOContext *pb) > static int flush_dynbuf(VariantStream *vs, int *range_length) > { > AVFormatContext *ctx = vs->avf; > - uint8_t *buffer; > > if (!ctx->pb) { > return AVERROR(EINVAL); > @@ -458,15 +461,20 @@ static int flush_dynbuf(VariantStream *vs, int > *range_length) > avio_flush(ctx->pb); > > // write out to file > - *range_length = avio_close_dyn_buf(ctx->pb, &buffer); > + *range_length = avio_close_dyn_buf(ctx->pb, &vs->temp_buffer); > ctx->pb = NULL; > - avio_write(vs->out, buffer, *range_length); > - av_free(buffer); > + avio_write(vs->out, vs->temp_buffer, *range_length); > > // re-open buffer > return avio_open_dyn_buf(&ctx->pb); > } > > +static void reflush_dynbuf(VariantStream *vs, int *range_length) > +{ > + // re-open buffer > + avio_write(vs->out, vs->temp_buffer, *range_length);; > +} > + > static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls, > VariantStream *vs) { > > @@ -1544,7 +1552,10 @@ static int hls_window(AVFormatContext *s, int last, > VariantStream *vs) > > fail: > av_dict_free(&options); > - hlsenc_io_close(s, (byterange_mode || hls->segment_type == > SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename); > + ret = hlsenc_io_close(s, (byterange_mode || hls->segment_type == > SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename); > + if (ret < 0) { > + return ret; > + } > hlsenc_io_close(s, &hls->sub_m3u8_out, vs->vtt_m3u8_name); > if (use_temp_file) { > ff_rename(temp_filename, vs->m3u8_name, s); > @@ -2399,7 +2410,16 @@ static int hls_write_packet(AVFormatContext *s, > AVPacket *pkt) > if (ret < 0) { > return ret; > } > - hlsenc_io_close(s, &vs->out, filename); > + ret = hlsenc_io_close(s, &vs->out, filename); > + if (ret < 0) { > + av_log(s, AV_LOG_WARNING, "upload segment failed," > + "and will retry upload by a new http > session.\n"); > + ff_format_io_close(s, &vs->out); > + ret = hlsenc_io_open(s, &vs->out, filename, &options); > + reflush_dynbuf(vs, &range_length); > + ret = hlsenc_io_close(s, &vs->out, filename); > + } > + av_free(vs->temp_buffer); > av_free(filename); > } > } > @@ -2426,8 +2446,13 @@ static int hls_write_packet(AVFormatContext *s, > AVPacket *pkt) > // if we're building a VOD playlist, skip writing the manifest > multiple times, and just wait until the end > if (hls->pl_type != PLAYLIST_TYPE_VOD) { > if ((ret = hls_window(s, 0, vs)) < 0) { > - av_free(old_filename); > - return ret; > + av_log(s, AV_LOG_WARNING, "update playlist failed, will > retry once time\n"); > + ff_format_io_close(s, &vs->out); > + vs->out = NULL; > + if ((ret = hls_window(s, 0, vs)) < 0) { > + av_free(old_filename); > + return ret; > + } > } > } > > @@ -2579,6 +2604,20 @@ static int hls_write_trailer(struct AVFormatContext > *s) > > vs->size = range_length; > hlsenc_io_close(s, &vs->out, filename); > + ret = hlsenc_io_close(s, &vs->out, filename); > + if (ret < 0) { > + av_log(s, AV_LOG_WARNING, "write segment failed, will close > old handle and retry once time\n"); > + ff_format_io_close(s, &vs->out); > + ret = hlsenc_io_open(s, &vs->out, filename, &options); > + if (ret < 0) { > + av_log(s, AV_LOG_ERROR, "Failed to open file '%s'\n", > vs->avf->url); > + goto failed; > + } > + reflush_dynbuf(vs, &range_length); > + ret = hlsenc_io_close(s, &vs->out, filename); > + } > + av_free(vs->temp_buffer); > + > failed: > av_free(filename); > av_write_trailer(oc); > @@ -2610,7 +2649,12 @@ failed: > ff_format_io_close(s, &vtt_oc->pb); > avformat_free_context(vtt_oc); > } > - hls_window(s, 1, vs); > + ret = hls_window(s, 1, vs); > + if (ret < 0) { > + av_log(s, AV_LOG_WARNING, "update playlist failed, will retry > once time\n"); > + ff_format_io_close(s, &vs->out); > + hls_window(s, 1, vs); > + } > avformat_free_context(oc); > > vs->avf = NULL; > -- > 2.15.1 > > > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". Hi, I have confirmed that these patches fix the bug. Thank you! Just a couple of comments: 1) Currently two different error messages are shown for the same condition: "upload segment failed,and will retry upload by a new http session" and "update playlist failed, will retry once time" I would suggest having one message, perhaps "upload failed, will retry with a new http session". 2) Is AV_LOG_WARNING the correct log level for this type of message? It clutters the output very quickly for what could be a normal condition (for servers that don't support persistent connections or have limited the number of consecutive requests on a connection). Thanks again! Ian
> 在 2019年9月3日,22:30,Ian Klassen <ian@virtualfunc.com> 写道: > > On Thu, Aug 29, 2019 at 6:15 PM Steven Liu <lq@chinaffmpeg.org> wrote: > >> fix ticket: 7975 >> >> Signed-off-by: Steven Liu <lq@chinaffmpeg.org> >> --- >> libavformat/hlsenc.c | 66 >> +++++++++++++++++++++++++++++++++++++++++++--------- >> 1 file changed, 55 insertions(+), 11 deletions(-) >> >> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c >> index c6bd664b92..d9b48f0e7c 100644 >> --- a/libavformat/hlsenc.c >> +++ b/libavformat/hlsenc.c >> @@ -118,6 +118,7 @@ typedef struct VariantStream { >> AVIOContext *out; >> int packets_written; >> int init_range_length; >> + uint8_t *temp_buffer; >> >> AVFormatContext *avf; >> AVFormatContext *vtt_avf; >> @@ -262,11 +263,12 @@ static int hlsenc_io_open(AVFormatContext *s, >> AVIOContext **pb, char *filename, >> return err; >> } >> >> -static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char >> *filename) { >> +static int hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char >> *filename) { >> HLSContext *hls = s->priv_data; >> int http_base_proto = filename ? ff_is_http_proto(filename) : 0; >> + int ret = 0; >> if (!*pb) >> - return; >> + return ret; >> if (!http_base_proto || !hls->http_persistent || hls->key_info_file >> || hls->encrypt) { >> ff_format_io_close(s, pb); >> #if CONFIG_HTTP_PROTOCOL >> @@ -275,8 +277,10 @@ static void hlsenc_io_close(AVFormatContext *s, >> AVIOContext **pb, char *filename >> av_assert0(http_url_context); >> avio_flush(*pb); >> ffurl_shutdown(http_url_context, AVIO_FLAG_WRITE); >> + ret = ff_http_get_shutdown_status(http_url_context); >> #endif >> } >> + return ret; >> } >> >> static void set_http_options(AVFormatContext *s, AVDictionary **options, >> HLSContext *c) >> @@ -447,7 +451,6 @@ static void write_styp(AVIOContext *pb) >> static int flush_dynbuf(VariantStream *vs, int *range_length) >> { >> AVFormatContext *ctx = vs->avf; >> - uint8_t *buffer; >> >> if (!ctx->pb) { >> return AVERROR(EINVAL); >> @@ -458,15 +461,20 @@ static int flush_dynbuf(VariantStream *vs, int >> *range_length) >> avio_flush(ctx->pb); >> >> // write out to file >> - *range_length = avio_close_dyn_buf(ctx->pb, &buffer); >> + *range_length = avio_close_dyn_buf(ctx->pb, &vs->temp_buffer); >> ctx->pb = NULL; >> - avio_write(vs->out, buffer, *range_length); >> - av_free(buffer); >> + avio_write(vs->out, vs->temp_buffer, *range_length); >> >> // re-open buffer >> return avio_open_dyn_buf(&ctx->pb); >> } >> >> +static void reflush_dynbuf(VariantStream *vs, int *range_length) >> +{ >> + // re-open buffer >> + avio_write(vs->out, vs->temp_buffer, *range_length);; >> +} >> + >> static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls, >> VariantStream *vs) { >> >> @@ -1544,7 +1552,10 @@ static int hls_window(AVFormatContext *s, int last, >> VariantStream *vs) >> >> fail: >> av_dict_free(&options); >> - hlsenc_io_close(s, (byterange_mode || hls->segment_type == >> SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename); >> + ret = hlsenc_io_close(s, (byterange_mode || hls->segment_type == >> SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename); >> + if (ret < 0) { >> + return ret; >> + } >> hlsenc_io_close(s, &hls->sub_m3u8_out, vs->vtt_m3u8_name); >> if (use_temp_file) { >> ff_rename(temp_filename, vs->m3u8_name, s); >> @@ -2399,7 +2410,16 @@ static int hls_write_packet(AVFormatContext *s, >> AVPacket *pkt) >> if (ret < 0) { >> return ret; >> } >> - hlsenc_io_close(s, &vs->out, filename); >> + ret = hlsenc_io_close(s, &vs->out, filename); >> + if (ret < 0) { >> + av_log(s, AV_LOG_WARNING, "upload segment failed," >> + "and will retry upload by a new http >> session.\n"); >> + ff_format_io_close(s, &vs->out); >> + ret = hlsenc_io_open(s, &vs->out, filename, &options); >> + reflush_dynbuf(vs, &range_length); >> + ret = hlsenc_io_close(s, &vs->out, filename); >> + } >> + av_free(vs->temp_buffer); >> av_free(filename); >> } >> } >> @@ -2426,8 +2446,13 @@ static int hls_write_packet(AVFormatContext *s, >> AVPacket *pkt) >> // if we're building a VOD playlist, skip writing the manifest >> multiple times, and just wait until the end >> if (hls->pl_type != PLAYLIST_TYPE_VOD) { >> if ((ret = hls_window(s, 0, vs)) < 0) { >> - av_free(old_filename); >> - return ret; >> + av_log(s, AV_LOG_WARNING, "update playlist failed, will >> retry once time\n"); >> + ff_format_io_close(s, &vs->out); >> + vs->out = NULL; >> + if ((ret = hls_window(s, 0, vs)) < 0) { >> + av_free(old_filename); >> + return ret; >> + } >> } >> } >> >> @@ -2579,6 +2604,20 @@ static int hls_write_trailer(struct AVFormatContext >> *s) >> >> vs->size = range_length; >> hlsenc_io_close(s, &vs->out, filename); >> + ret = hlsenc_io_close(s, &vs->out, filename); >> + if (ret < 0) { >> + av_log(s, AV_LOG_WARNING, "write segment failed, will close >> old handle and retry once time\n"); >> + ff_format_io_close(s, &vs->out); >> + ret = hlsenc_io_open(s, &vs->out, filename, &options); >> + if (ret < 0) { >> + av_log(s, AV_LOG_ERROR, "Failed to open file '%s'\n", >> vs->avf->url); >> + goto failed; >> + } >> + reflush_dynbuf(vs, &range_length); >> + ret = hlsenc_io_close(s, &vs->out, filename); >> + } >> + av_free(vs->temp_buffer); >> + >> failed: >> av_free(filename); >> av_write_trailer(oc); >> @@ -2610,7 +2649,12 @@ failed: >> ff_format_io_close(s, &vtt_oc->pb); >> avformat_free_context(vtt_oc); >> } >> - hls_window(s, 1, vs); >> + ret = hls_window(s, 1, vs); >> + if (ret < 0) { >> + av_log(s, AV_LOG_WARNING, "update playlist failed, will retry >> once time\n"); >> + ff_format_io_close(s, &vs->out); >> + hls_window(s, 1, vs); >> + } >> avformat_free_context(oc); >> >> vs->avf = NULL; >> -- >> 2.15.1 >> >> >> >> _______________________________________________ >> ffmpeg-devel mailing list >> ffmpeg-devel@ffmpeg.org >> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel >> >> To unsubscribe, visit link above, or email >> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". > > > Hi, > > I have confirmed that these patches fix the bug. Thank you! > > Just a couple of comments: > > 1) Currently two different error messages are shown for the same condition: > > "upload segment failed,and will retry upload by a new http session" and > "update playlist failed, will retry once time" > > I would suggest having one message, perhaps "upload failed, will retry with > a new http session". > > 2) Is AV_LOG_WARNING the correct log level for this type of message? It > clutters the output very quickly for what could be a normal condition (for > servers that don't support persistent connections or have limited the > number of consecutive requests on a connection). Hi Ian, Thanks for your test and the comments, will push with the comments by you suggestions. > > Thanks again! > > Ian > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". Thanks Steven
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index c6bd664b92..d9b48f0e7c 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -118,6 +118,7 @@ typedef struct VariantStream { AVIOContext *out; int packets_written; int init_range_length; + uint8_t *temp_buffer; AVFormatContext *avf; AVFormatContext *vtt_avf; @@ -262,11 +263,12 @@ static int hlsenc_io_open(AVFormatContext *s, AVIOContext **pb, char *filename, return err; } -static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename) { +static int hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename) { HLSContext *hls = s->priv_data; int http_base_proto = filename ? ff_is_http_proto(filename) : 0; + int ret = 0; if (!*pb) - return; + return ret; if (!http_base_proto || !hls->http_persistent || hls->key_info_file || hls->encrypt) { ff_format_io_close(s, pb); #if CONFIG_HTTP_PROTOCOL @@ -275,8 +277,10 @@ static void hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename av_assert0(http_url_context); avio_flush(*pb); ffurl_shutdown(http_url_context, AVIO_FLAG_WRITE); + ret = ff_http_get_shutdown_status(http_url_context); #endif } + return ret; } static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c) @@ -447,7 +451,6 @@ static void write_styp(AVIOContext *pb) static int flush_dynbuf(VariantStream *vs, int *range_length) { AVFormatContext *ctx = vs->avf; - uint8_t *buffer; if (!ctx->pb) { return AVERROR(EINVAL); @@ -458,15 +461,20 @@ static int flush_dynbuf(VariantStream *vs, int *range_length) avio_flush(ctx->pb); // write out to file - *range_length = avio_close_dyn_buf(ctx->pb, &buffer); + *range_length = avio_close_dyn_buf(ctx->pb, &vs->temp_buffer); ctx->pb = NULL; - avio_write(vs->out, buffer, *range_length); - av_free(buffer); + avio_write(vs->out, vs->temp_buffer, *range_length); // re-open buffer return avio_open_dyn_buf(&ctx->pb); } +static void reflush_dynbuf(VariantStream *vs, int *range_length) +{ + // re-open buffer + avio_write(vs->out, vs->temp_buffer, *range_length);; +} + static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls, VariantStream *vs) { @@ -1544,7 +1552,10 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs) fail: av_dict_free(&options); - hlsenc_io_close(s, (byterange_mode || hls->segment_type == SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename); + ret = hlsenc_io_close(s, (byterange_mode || hls->segment_type == SEGMENT_TYPE_FMP4) ? &hls->m3u8_out : &vs->out, temp_filename); + if (ret < 0) { + return ret; + } hlsenc_io_close(s, &hls->sub_m3u8_out, vs->vtt_m3u8_name); if (use_temp_file) { ff_rename(temp_filename, vs->m3u8_name, s); @@ -2399,7 +2410,16 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) if (ret < 0) { return ret; } - hlsenc_io_close(s, &vs->out, filename); + ret = hlsenc_io_close(s, &vs->out, filename); + if (ret < 0) { + av_log(s, AV_LOG_WARNING, "upload segment failed," + "and will retry upload by a new http session.\n"); + ff_format_io_close(s, &vs->out); + ret = hlsenc_io_open(s, &vs->out, filename, &options); + reflush_dynbuf(vs, &range_length); + ret = hlsenc_io_close(s, &vs->out, filename); + } + av_free(vs->temp_buffer); av_free(filename); } } @@ -2426,8 +2446,13 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) // if we're building a VOD playlist, skip writing the manifest multiple times, and just wait until the end if (hls->pl_type != PLAYLIST_TYPE_VOD) { if ((ret = hls_window(s, 0, vs)) < 0) { - av_free(old_filename); - return ret; + av_log(s, AV_LOG_WARNING, "update playlist failed, will retry once time\n"); + ff_format_io_close(s, &vs->out); + vs->out = NULL; + if ((ret = hls_window(s, 0, vs)) < 0) { + av_free(old_filename); + return ret; + } } } @@ -2579,6 +2604,20 @@ static int hls_write_trailer(struct AVFormatContext *s) vs->size = range_length; hlsenc_io_close(s, &vs->out, filename); + ret = hlsenc_io_close(s, &vs->out, filename); + if (ret < 0) { + av_log(s, AV_LOG_WARNING, "write segment failed, will close old handle and retry once time\n"); + ff_format_io_close(s, &vs->out); + ret = hlsenc_io_open(s, &vs->out, filename, &options); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Failed to open file '%s'\n", vs->avf->url); + goto failed; + } + reflush_dynbuf(vs, &range_length); + ret = hlsenc_io_close(s, &vs->out, filename); + } + av_free(vs->temp_buffer); + failed: av_free(filename); av_write_trailer(oc); @@ -2610,7 +2649,12 @@ failed: ff_format_io_close(s, &vtt_oc->pb); avformat_free_context(vtt_oc); } - hls_window(s, 1, vs); + ret = hls_window(s, 1, vs); + if (ret < 0) { + av_log(s, AV_LOG_WARNING, "update playlist failed, will retry once time\n"); + ff_format_io_close(s, &vs->out); + hls_window(s, 1, vs); + } avformat_free_context(oc); vs->avf = NULL;
fix ticket: 7975 Signed-off-by: Steven Liu <lq@chinaffmpeg.org> --- libavformat/hlsenc.c | 66 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-)