From d209a3d50f9c00dc998de3a7f909f693f49bf47a Mon Sep 17 00:00:00 2001 From: "kjeyapal@akamai.com" Date: Wed, 12 Dec 2018 21:50:18 +0530 Subject: [PATCH] avformat/dashenc : Refactored HLS media playlist related code Made it as a separate function, so that it could be reused (in future) --- libavformat/dashenc.c | 135 +++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 60 deletions(-) diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index 585b34cb97..f797b7bd1c 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -407,6 +407,78 @@ static void get_hls_playlist_name(char *playlist_name, int string_size, snprintf(playlist_name, string_size, "media_%d.m3u8", id); } +static void get_start_index_number(OutputStream *os, DASHContext *c, + int *start_index, int *start_number) { + *start_index = 0; + *start_number = 1; + if (c->window_size) { + *start_index = FFMAX(os->nb_segments - c->window_size, 0); + *start_number = FFMAX(os->segment_index - c->window_size, 1); + } +} + +static void write_hls_media_playlist(OutputStream *os, AVFormatContext *s, + int representation_id, int final) { + DASHContext *c = s->priv_data; + int timescale = os->ctx->streams[0]->time_base.den; + char temp_filename_hls[1024]; + char filename_hls[1024]; + AVDictionary *http_opts = NULL; + int target_duration = 0; + int ret = 0; + const char *proto = avio_find_protocol_name(c->dirname); + int use_rename = proto && !strcmp(proto, "file"); + int i, start_index, start_number; + + get_start_index_number(os, c, &start_index, &start_number); + get_hls_playlist_name(filename_hls, sizeof(filename_hls), + c->dirname, representation_id); + + snprintf(temp_filename_hls, sizeof(temp_filename_hls), use_rename ? "%s.tmp" : "%s", filename_hls); + + set_http_options(&http_opts, c); + ret = dashenc_io_open(s, &c->m3u8_out, temp_filename_hls, &http_opts); + av_dict_free(&http_opts); + if (ret < 0) { + handle_io_open_error(s, ret, temp_filename_hls); + return; + } + for (i = start_index; i < os->nb_segments; i++) { + Segment *seg = os->segments[i]; + double duration = (double) seg->duration / timescale; + if (target_duration <= duration) + target_duration = lrint(duration); + } + + ff_hls_write_playlist_header(c->m3u8_out, 6, -1, target_duration, + start_number, PLAYLIST_TYPE_NONE); + + ff_hls_write_init_file(c->m3u8_out, os->initfile, c->single_file, + os->init_range_length, os->init_start_pos); + + for (i = start_index; i < os->nb_segments; i++) { + Segment *seg = os->segments[i]; + ret = ff_hls_write_file_entry(c->m3u8_out, 0, c->single_file, + (double) seg->duration / timescale, 0, + seg->range_length, seg->start_pos, NULL, + c->single_file ? os->initfile : seg->file, + NULL); + if (ret < 0) { + av_log(os->ctx, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n"); + } + } + + if (final) + ff_hls_write_end_list(c->m3u8_out); + + dashenc_io_close(s, &c->m3u8_out, temp_filename_hls); + + if (use_rename) + if (avpriv_io_move(temp_filename_hls, filename_hls) < 0) { + av_log(os->ctx, AV_LOG_WARNING, "renaming file %s to %s failed\n\n", temp_filename_hls, filename_hls); + } +} + static int flush_init_segment(AVFormatContext *s, OutputStream *os) { DASHContext *c = s->priv_data; @@ -463,11 +535,8 @@ static void output_segment_list(OutputStream *os, AVIOContext *out, AVFormatCont int representation_id, int final) { DASHContext *c = s->priv_data; - int i, start_index = 0, start_number = 1; - if (c->window_size) { - start_index = FFMAX(os->nb_segments - c->window_size, 0); - start_number = FFMAX(os->segment_index - c->window_size, 1); - } + int i, start_index, start_number; + get_start_index_number(os, c, &start_index, &start_number); if (c->use_template) { int timescale = c->use_timeline ? os->ctx->streams[0]->time_base.den : AV_TIME_BASE; @@ -527,61 +596,7 @@ static void output_segment_list(OutputStream *os, AVIOContext *out, AVFormatCont } if (c->hls_playlist && start_index < os->nb_segments && os->segment_type == SEGMENT_TYPE_MP4) { - int timescale = os->ctx->streams[0]->time_base.den; - char temp_filename_hls[1024]; - char filename_hls[1024]; - AVDictionary *http_opts = NULL; - int target_duration = 0; - int ret = 0; - const char *proto = avio_find_protocol_name(c->dirname); - int use_rename = proto && !strcmp(proto, "file"); - - get_hls_playlist_name(filename_hls, sizeof(filename_hls), - c->dirname, representation_id); - - snprintf(temp_filename_hls, sizeof(temp_filename_hls), use_rename ? "%s.tmp" : "%s", filename_hls); - - set_http_options(&http_opts, c); - ret = dashenc_io_open(s, &c->m3u8_out, temp_filename_hls, &http_opts); - av_dict_free(&http_opts); - if (ret < 0) { - handle_io_open_error(s, ret, temp_filename_hls); - return; - } - for (i = start_index; i < os->nb_segments; i++) { - Segment *seg = os->segments[i]; - double duration = (double) seg->duration / timescale; - if (target_duration <= duration) - target_duration = lrint(duration); - } - - ff_hls_write_playlist_header(c->m3u8_out, 6, -1, target_duration, - start_number, PLAYLIST_TYPE_NONE); - - ff_hls_write_init_file(c->m3u8_out, os->initfile, c->single_file, - os->init_range_length, os->init_start_pos); - - for (i = start_index; i < os->nb_segments; i++) { - Segment *seg = os->segments[i]; - ret = ff_hls_write_file_entry(c->m3u8_out, 0, c->single_file, - (double) seg->duration / timescale, 0, - seg->range_length, seg->start_pos, NULL, - c->single_file ? os->initfile : seg->file, - NULL); - if (ret < 0) { - av_log(os->ctx, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n"); - } - } - - if (final) - ff_hls_write_end_list(c->m3u8_out); - - dashenc_io_close(s, &c->m3u8_out, temp_filename_hls); - - if (use_rename) - if (avpriv_io_move(temp_filename_hls, filename_hls) < 0) { - av_log(os->ctx, AV_LOG_WARNING, "renaming file %s to %s failed\n\n", temp_filename_hls, filename_hls); - } + write_hls_media_playlist(os, s, representation_id, final); } }