From patchwork Wed Nov 29 15:16:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jeyapal, Karthick" X-Patchwork-Id: 6448 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp4622104jah; Wed, 29 Nov 2017 07:17:13 -0800 (PST) X-Google-Smtp-Source: AGs4zMbOwHU/uBv4wiScVLFM4YV8UQK3sbE1pgBf46Qu/AKeDLNL8Ykm/4Z6YeOWRcJuYTNF3xjV X-Received: by 10.28.220.67 with SMTP id t64mr3006673wmg.150.1511968632999; Wed, 29 Nov 2017 07:17:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511968632; cv=none; d=google.com; s=arc-20160816; b=npcjB72kkfOCobmKtrWU9l+Qiz7OaZeLtPY/CXsTyKNE1CtUfOhhzqCbG+vnVNpSsC aOfGjL2NQE1dwWAacwn3P8XBonsE/KuK1jnysT6dn74P5/s6bAm0xEieLPJLtok/iEeL rpycargmjv7S9orSBF/OgKCMhYXPDzFXylO9Z0V/n142ErwGKavxK3Lgp33CscK8oAwD 3fnd9wsEwuyupooDvExMNTaFjsw6FolNqEiL069iKh/sMuMW2X3TpZom6ZNj88AtDUoN I/29ivjPkbjOJZ6OUzS7xvcG6BzWCYudwKw4/QTW/81MqRF98WXI1jjQrtKHuaOD2kKj IvcA== 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=F2ys8aIG2q34UWnDKusZNyaNmi4asuhpIRMqRPIOUQ4=; b=KjFht2nWUG/OqMoUFKDLIj0toEDCV8R4Duv4BHm391Ht3K4qGuCFzBfGAijLr9tp39 /Wcz5jpw28tLghN6dv4USOd+R0d9colwfl31gpGf9qHyVEOPiBuSbQZQM1XDdV29iDsN //M36rWTeF+2a0zZahnZf1BqK8lNbIuMEphHtr/znO1K7YuexvYnCyRlxRm0IzpdvRrR LmGWibuv0uMuP2NNk6FQ8fwiji6uo3cc0Dn9q3zxLHxjdQFWGXjOCXsiz/EwRAL5gKza f9efzVkUkdjKWVzEYS6+0GjeAM2BU9/fCzKgR2hqmqERbxsu6AgK6JIWFUvflcwrvDmz TVeA== 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=Clfbh+h0; 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 w206si402956wmd.25.2017.11.29.07.17.12; Wed, 29 Nov 2017 07:17:12 -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=Clfbh+h0; 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 1F22E68A488; Wed, 29 Nov 2017 17:17:03 +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 D02FF68A452 for ; Wed, 29 Nov 2017 17:16:54 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpservice.net; s=m78bu0.a1-4.dyn; x=1511969523; h=Feedback-ID: X-Smtpcorp-Track:Message-Id:Date:Subject:To:From:Reply-To:Sender: List-Unsubscribe; bh=fAFjmneSn8wjO57RyxyDYNp3jGUfnWblj+0UgVVUSAw=; b=Clfbh+h0 WesVcosEHohZCKTwDiAvH0G68E4PNl+88jvlbsASHh1Edk5Ur6lF6JYDsOm5Xq6MZhH5ACa8JLmdg kMwc9vH2MFxCTcSRjSld8Yn0fB1HQp1RKxlNSQZMNGzKVwBV8fKo5Ov54I4qam+S9wZa0gRymmw1+ K5i0OmZNXZBjiPzxMR/mgYIzbmigvMUVE+mVKqUP5nnYe+5Darf8Vnoo5rB4BgXofuC1tBwuOpO1k l0foJ9+Z62KQyWb3Z4Ceq4893oMexSwkcKHuQc4pcFXygIpap4E/k+VIMa1/RfGsVJmWgPvFIaPRz svcFqQ9LcVMFTozTZDhscWYa3Q==; From: Karthick J To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 Nov 2017 20:46:40 +0530 Message-Id: <1511968600-8168-2-git-send-email-kjeyapal@akamai.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1511968600-8168-1-git-send-email-kjeyapal@akamai.com> References: <1511968600-8168-1-git-send-email-kjeyapal@akamai.com> X-Smtpcorp-Track: 1-K46Y4baUnLQm.Ho9nd390i Feedback-ID: 337386m:337386asVRLGB:337386sH2tyybamo:SMTPCORP X-Report-Abuse: Please forward a copy of this message, including all headers, to Subject: [FFmpeg-devel] [PATCH 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/Makefile | 2 +- libavformat/dashenc.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 8ec48c2..3d0c7bf 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/Makefile b/libavformat/Makefile index fd8b9f9..4bffdf2 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -135,7 +135,7 @@ OBJS-$(CONFIG_CONCAT_DEMUXER) += concatdec.o OBJS-$(CONFIG_CRC_MUXER) += crcenc.o OBJS-$(CONFIG_DATA_DEMUXER) += rawdec.o OBJS-$(CONFIG_DATA_MUXER) += rawenc.o -OBJS-$(CONFIG_DASH_MUXER) += dash.o dashenc.o +OBJS-$(CONFIG_DASH_MUXER) += dash.o dashenc.o hlsplaylist.o OBJS-$(CONFIG_DASH_DEMUXER) += dash.o dashdec.o OBJS-$(CONFIG_DAUD_DEMUXER) += dauddec.o OBJS-$(CONFIG_DAUD_MUXER) += daudenc.o diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index 0fee3cd..efc2012 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -36,6 +36,7 @@ #include "avc.h" #include "avformat.h" #include "avio_internal.h" +#include "hlsplaylist.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]; + double duration = (double) seg->duration / timescale; + if (target_duration <= duration) + target_duration = hls_get_int_from_double(duration); + } + + 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, + (double) seg->duration / timescale, 0, + 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 }, };