From patchwork Thu Nov 23 02:42:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jeyapal, Karthick" X-Patchwork-Id: 6288 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp234064jah; Wed, 22 Nov 2017 18:42:58 -0800 (PST) X-Google-Smtp-Source: AGs4zMZ9VQ8LEMz6otYQtroN8gWikRUATxw5E5kDSIzLqgIlMU5RqQ4P4k/piJYYykxNvxWErEOV X-Received: by 10.223.141.180 with SMTP id o49mr1125424wrb.35.1511404978096; Wed, 22 Nov 2017 18:42:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511404978; cv=none; d=google.com; s=arc-20160816; b=GFLZC/h3kPBabLOB/I6JymPWcCt0nb3tsv9uNkR+3kpyioADuRHuweA2dR5YBW8AR+ M9u3ebmDuoVubChpToVFD2xmojbS7HFKbLcTGfc7oth2QnQUdPy5UvOTw+VAunCWQA54 svNFkcOWJAuDaYOJwK53ano3iftRfr1w7PQJsIJrIv0HIPQl/6/Tj3PO1zmP/JnNg7VE xIN5I2eYl4fSAb82h13Qsz6I2Ifnx2LWZW3MhdhjxoIoK9f35HmxonqrQz4CrDQ/C4Z/ LfhR5RplN5oq0snNLsm4jXqlKFPk/DLsKsCNWdlxGuLckTAKH2hjF2P2aHdB65vvs+lC Mjpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:feedback-id:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to :arc-authentication-results; bh=bjZg0/R0UF1OLTkVyvXb4xRrJf2KvEEaj3PRqHtdRDY=; b=U7Re82F4g3LjDOoKdX+DXIpsYwxyldD3DtufszCH+CGr0Iv3pcwBfio8jrp0EVhuV1 ucmPIwmpvCCp3fW/BMK3LqlBazKJnEma0/UGPf7XIpSecJZVs0rTmpOXfIeiXtQPjSGk MnnZyxRVhqdzKGo2V2IkiCu6U4MD9kuo9iWJayEuhD/1J9Pjt995bXhlLu2zTfYEPBdV eCwm11Vn1kzTM8LeRY83Jn+Z4pmNAd9D2peUpqGIBrdfBVdR3zoCPeZyZYWbXFLFE6r2 DgCfVXejLiHAmMNkr1SACaoCeq5oJAojnxdd0Ltgoysw1IjkijddeNXXaOFspF+d/tRy xXzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@smtpservice.net header.s=m78bu0.a1-4.dyn header.b=RqSzy+NN; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=akamai.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r79si14239536wrb.379.2017.11.22.18.42.57; Wed, 22 Nov 2017 18:42:58 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@smtpservice.net header.s=m78bu0.a1-4.dyn header.b=RqSzy+NN; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=akamai.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 29546689FF3; Thu, 23 Nov 2017 04:42:56 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from a2i831.smtp2go.com (a2i831.smtp2go.com [103.47.207.63]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B2A96689F9F for ; Thu, 23 Nov 2017 04:42:48 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpservice.net; s=m78bu0.a1-4.dyn; x=1511405874; h=Feedback-ID: X-Smtpcorp-Track:Message-Id:Date:Subject:To:From:Reply-To:Sender: List-Unsubscribe; bh=U4g8mfXEtgg3ii4PPwvcgNCQ17EPSlAQILcOh3OBG00=; b=RqSzy+NN cKQZ5Y5QHACIaNDY67lTHkSyqDRE+Ut483xYIGY4zrVTOKLDzWsuujQFZHkCxm8atwp6s/RNYvcen ENtYk/WjuDg1Zrw5/G2NAlv5U9iR8XZyEcK8dn+bRvtHPJQIVSZPBpGoaQxg4hH8O+Xo+xubx/eUT duZGn5qAPhJ3PKGfTZ8PKkyJ8f3xtb4VeWrNgnAzcXq5OXtb196UwfBmZxZSFMueL79IPmrD3EClw hlwL+jb5sAzhKD8P9CDkC136zdo6ExDAenAqDThEY8x6iUWI7tzgpJNJG/hlvtu+SOCcSHYK6bRxa ppsaMm+2q0zMln+m6NdTOXqH0Q==; From: Karthick J To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Nov 2017 08:12:32 +0530 Message-Id: <1511404952-9435-1-git-send-email-kjeyapal@akamai.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <20171122153614.GO4645@nb4> References: <20171122153614.GO4645@nb4> X-Smtpcorp-Track: 1-HhTRDIIalUE2.FnWkZvmQe Feedback-ID: 337386m:337386asVRLGB:337386suiXvxWnXP:SMTPCORP X-Report-Abuse: Please forward a copy of this message, including all headers, to Subject: [FFmpeg-devel] [PATCH v3 2/2] avformat/dashenc: Option to generate hls playlist as well 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" This is to take full advantage of Common Media Application Format. Now server can generate one content and serve both HLS and DASH players. --- doc/muxers.texi | 3 ++ libavformat/dashenc.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 0bb8ad2..1cf2481 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -249,6 +249,9 @@ DASH-templated name to used for the media segments. Default is "chunk-stream$Rep URL of the page that will return the UTC timestamp in ISO format. Example: "https://time.akamai.com/?iso" @item -http_user_agent @var{user_agent} Override User-Agent field in HTTP header. Applicable only for HTTP output. +@item -hls_playlist @var{hls_playlist} +Generate HLS playlist files as well. The master playlist is generated with the filename master.m3u8. +One media playlist file is generated for each stream with filenames media_0.m3u8, media_1.m3u8, etc. @item -adaptation_sets @var{adaptation_sets} Assign streams to AdaptationSets. Syntax is "id=x,streams=a,b,c id=y,streams=d,e" with x and y being the IDs of the adaptation sets and a,b,c,d and e are the indices of the mapped streams. diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index 201668a..3fd1ef3 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -36,6 +36,7 @@ #include "avc.h" #include "avformat.h" #include "avio_internal.h" +#include "hlsenc.h" #include "internal.h" #include "isom.h" #include "os_support.h" @@ -101,6 +102,8 @@ typedef struct DASHContext { const char *media_seg_name; const char *utc_timing_url; const char *user_agent; + int hls_playlist; + int master_playlist_created; } DASHContext; static struct codec_string { @@ -217,6 +220,14 @@ static void set_http_options(AVDictionary **options, DASHContext *c) av_dict_set(options, "user_agent", c->user_agent, 0); } +static void get_hls_playlist_name(char *playlist_name, int string_size, + const char *base_url, int id) { + if (base_url) + snprintf(playlist_name, string_size, "%smedia_%d.m3u8", base_url, id); + else + snprintf(playlist_name, string_size, "media_%d.m3u8", id); +} + static int flush_init_segment(AVFormatContext *s, OutputStream *os) { DASHContext *c = s->priv_data; @@ -262,7 +273,8 @@ static void dash_free(AVFormatContext *s) av_freep(&c->streams); } -static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c) +static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c, + int representation_id, int final) { int i, start_index = 0, start_number = 1; if (c->window_size) { @@ -322,6 +334,55 @@ static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext } avio_printf(out, "\t\t\t\t\n"); } + if (c->hls_playlist && start_index < os->nb_segments) + { + int timescale = os->ctx->streams[0]->time_base.den; + char temp_filename_hls[1024]; + char filename_hls[1024]; + AVIOContext *out_hls = NULL; + AVDictionary *http_opts = NULL; + int target_duration = 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); + avio_open2(&out_hls, temp_filename_hls, AVIO_FLAG_WRITE, NULL, &http_opts); + av_dict_free(&http_opts); + for (i = start_index; i < os->nb_segments; i++) { + Segment *seg = os->segments[i]; + if (target_duration < seg->duration) + target_duration = seg->duration; + } + target_duration = lrint((double) target_duration / timescale); + + ff_hls_write_playlist_header(out_hls, 6, -1, target_duration, + start_number, PLAYLIST_TYPE_NONE); + + ff_hls_write_init_file(out_hls, 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]; + ff_hls_write_file_entry(out_hls, 0, c->single_file, 0, + (double) seg->duration / timescale, + seg->range_length, seg->start_pos, NULL, + c->single_file ? os->initfile : seg->file, + NULL); + } + + if (final) + ff_hls_write_end_list(out_hls); + + avio_close(out_hls); + if (use_rename) + avpriv_io_move(temp_filename_hls, filename_hls); + } + } static char *xmlescape(const char *str) { @@ -391,7 +452,8 @@ static void format_date_now(char *buf, int size) } } -static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index) +static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index, + int final) { DASHContext *c = s->priv_data; AdaptationSet *as = &c->as[as_index]; @@ -430,7 +492,7 @@ static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_ind avio_printf(out, "\t\t\t\t\n", s->streams[i]->codecpar->channels); } - output_segment_list(os, out, c); + output_segment_list(os, out, c, i, final); avio_printf(out, "\t\t\t\n"); } avio_printf(out, "\t\t\n"); @@ -650,7 +712,7 @@ static int write_manifest(AVFormatContext *s, int final) } for (i = 0; i < c->nb_as; i++) { - if ((ret = write_adaptation_set(s, out, i)) < 0) + if ((ret = write_adaptation_set(s, out, i, final)) < 0) return ret; } avio_printf(out, "\t\n"); @@ -665,6 +727,38 @@ static int write_manifest(AVFormatContext *s, int final) if (use_rename) return avpriv_io_move(temp_filename, s->filename); + if (c->hls_playlist && !c->master_playlist_created) { + char filename_hls[1024]; + + if (c->dirname) + snprintf(filename_hls, sizeof(filename_hls), "%s/master.m3u8", c->dirname); + else + snprintf(filename_hls, sizeof(filename_hls), "master.m3u8"); + + snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", filename_hls); + + set_http_options(&opts, c); + ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, NULL, &opts); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename); + return ret; + } + av_dict_free(&opts); + + ff_hls_write_playlist_version(out, 6); + + for (i = 0; i < s->nb_streams; i++) { + char playlist_file[64]; + AVStream *st = s->streams[i]; + get_hls_playlist_name(playlist_file, sizeof(playlist_file), NULL, i); + ff_hls_write_stream_info(st, out, st->codecpar->bit_rate, playlist_file); + } + avio_close(out); + if (use_rename) + avpriv_io_move(temp_filename, filename_hls); + c->master_playlist_created = 1; + } + return 0; } @@ -1206,6 +1300,7 @@ static const AVOption options[] = { { "media_seg_name", "DASH-templated name to used for the media segments", OFFSET(media_seg_name), AV_OPT_TYPE_STRING, {.str = "chunk-stream$RepresentationID$-$Number%05d$.m4s"}, 0, 0, E }, { "utc_timing_url", "URL of the page that will return the UTC timestamp in ISO format", OFFSET(utc_timing_url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E }, { "http_user_agent", "override User-Agent field in HTTP header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E}, + { "hls_playlist", "Generate HLS playlist files(master.m3u8, media_%d.m3u8)", OFFSET(hls_playlist), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { NULL }, };