From patchwork Wed Dec 12 16:20:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jeyapal, Karthick" X-Patchwork-Id: 11382 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9639F44DD93 for ; Wed, 12 Dec 2018 18:20:37 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D93F468A78E; Wed, 12 Dec 2018 18:20:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from a2i252.smtp2go.com (a2i252.smtp2go.com [103.47.204.252]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2B5D768A662 for ; Wed, 12 Dec 2018 18:20:22 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpservice.net; s=m78bu0.a1-4.dyn; x=1544632539; h=Feedback-ID: X-Smtpcorp-Track:Message-Id:Date:Subject:To:From:Reply-To:Sender: List-Unsubscribe; bh=uVRirRAqxKkIVKVjfbIRfAYYuLryLd60HT8oIyGiquw=; b=JcnNbY7r x/Fhb4wKnvJQfQl+Rduv6obqbV/fR4e/Bxn5XHhDjcpVKa7db9zcjafsWPnnPW11jL4hunwgdSrGk urolZinx2cZMuYmeqCNTed9pMJhXmtHMjfC5/0i+NlCiChi5bkMjRaOTLqRoWN+rh+6gZQX8yLdCR OO9xnpTAFjrWJ3oRWvyGg35K6kbozGJHDfEW0LPX3HQImj5p7IX9OSZB85LRxVGGnftUf3Vl6eVuX f9b13ixDR3tyDLzsYx0INRBQ/wOzPwFDbfGdrVdLvD05FbiIiXId06D6KEnAXZ3aV/7aakP67TqY5 zksI0evKi8LnhyP4X6Mc8Lkb5w==; Received: from [10.66.228.43] (helo=SmtpCorp) by smtpcorp.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.91) (envelope-from ) id 1gX7FO-095CbZ-TL; Wed, 12 Dec 2018 16:20:30 +0000 Received: from [10.49.207.48] (helo=blr-mp4tf.bangalore.corp.akamai.com) by smtpcorp.com with esmtpa (Exim 4.91) (envelope-from ) id 1gX7FN-wSEWMO-Ud; Wed, 12 Dec 2018 16:20:30 +0000 From: Karthick J To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Dec 2018 21:50:18 +0530 Message-Id: <20181212162019.38072-1-kjeyapal@akamai.com> X-Mailer: git-send-email 2.17.1 (Apple Git-112) X-Smtpcorp-Track: 1gb7FNwSEWuOld.EbVuqezsd Feedback-ID: 337386m:337386asVRLGB:337386sAr5MKgw_m X-Report-Abuse: Please forward a copy of this message, including all headers, to Subject: [FFmpeg-devel] [PATCH v2 1/2] avformat/dashenc : Refactored HLS media playlist related code X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Karthick J MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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); } }