From patchwork Sat Dec 21 18:02:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 16911 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 9B13244958D for ; Sat, 21 Dec 2019 20:03:47 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 85CB168AA7B; Sat, 21 Dec 2019 20:03:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EF0F968AA17 for ; Sat, 21 Dec 2019 20:03:38 +0200 (EET) Received: by mail-pj1-f67.google.com with SMTP id kx11so1465358pjb.4 for ; Sat, 21 Dec 2019 10:03:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ExN9vnodhdOaXk5UUYA4GYh68VOANnFh7euKH3ZDu54=; b=eh6uCjFKmMgwISeS/aASCCw4yMT94PzBinN33lq4dbcv4lsT5Jl6TfyLHLGpLE/XwG B+V9Q0jtebV2U/cO2Pl+k9NCFA1V5VYdTDSrN4jebbWtYZ1ZPuLb/tGXdyjUdr8O6b0W c6yBaqsnSmOBPfCiNPiT5sA4qPGLfjZEQOpmHiSXf3c0YYYOLd8pMC+wOpvqGFDjrnnV 21BsBXYMGzEQWDtqL/fRqzlG3NDBHgXKwheKDvEMiQ54VgzV3Bkytm6JaiNDSfu6Hftm PL4pfM/1szpSgDJT9vSvp13Y3+rWe5mKJqRyu8RINdTgAYlHWPboFad56S3r7mEhabWs lyAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ExN9vnodhdOaXk5UUYA4GYh68VOANnFh7euKH3ZDu54=; b=iNYFwVris7cshqlkI58pfErb9uIL8e6D85luT6aYypJ3f4NG5+y6C7j/ki6fYRhGDK My9jAlVrdlAXM8LswtOGymgW9U8eyGrgNmDQTyofMLUTbtF5WYSooBKJ07+eTj/44ehF PRxkxpXgUOzTsbHPeFctlxXsaNIIc31svQALD1stUSqNsRu8ZKzBTd9bJ6GzsZUy/IeJ Lm3z6bNijgiQMOaszf470J6G0k/cRyheqcyyu55eeUS9JU4xEG6MiLHiridtdUn1Pjrd a0+Qba9bTGkmebPozDsI1mCGeYvH2zzlRcpDespbaNEEnNo+5XLKe3VvAJG5VVGa+JTE GNtw== X-Gm-Message-State: APjAAAW5UapynPmYKN8JZ38ZnO3s4JVtz3NgNaBbfWngCHNb3VeJrHrL 140vTeReZ4h65FksBHreKkcq0xLn X-Google-Smtp-Source: APXvYqxqya8AwG0Y84LY+5xrKF1af/FsK00wJBPybe4mX/ruwzYpWbxWFDbUrJ+9OHz54Ei5T/VPAA== X-Received: by 2002:a17:902:9a89:: with SMTP id w9mr22075294plp.19.1576951417168; Sat, 21 Dec 2019 10:03:37 -0800 (PST) Received: from localhost.localdomain ([191.83.216.116]) by smtp.gmail.com with ESMTPSA id s196sm18223564pfs.136.2019.12.21.10.03.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Dec 2019 10:03:36 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 21 Dec 2019 15:02:45 -0300 Message-Id: <20191221180252.6091-8-jamrial@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191221180252.6091-1-jamrial@gmail.com> References: <20191221180252.6091-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/15] avformat/dashenc: Write a Producer Reference Time element 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: James Almer --- libavformat/dashenc.c | 53 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index 82941eb459..baed933d6a 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -116,6 +116,8 @@ typedef struct OutputStream { char full_path[1024]; char temp_path[1024]; double availability_time_offset; + int64_t producer_reference_time; + char producer_reference_time_str[100]; int total_pkt_size; int total_pkt_duration; int muxer_overhead; @@ -144,6 +146,7 @@ typedef struct DASHContext { int64_t total_duration; char availability_start_time[100]; time_t start_time_s; + int64_t presentation_time_offset; char dirname[1024]; const char *single_file_name; /* file names as specified in options */ const char *init_seg_name; @@ -168,6 +171,7 @@ typedef struct DASHContext { int nr_of_streams_to_flush; int nr_of_streams_flushed; int frag_type; + int write_prft; } DASHContext; static struct codec_string { @@ -636,7 +640,10 @@ static void output_segment_list(OutputStream *os, AVIOContext *out, AVFormatCont avio_printf(out, "availabilityTimeOffset=\"%.3f\" ", os->availability_time_offset); } - avio_printf(out, "initialization=\"%s\" media=\"%s\" startNumber=\"%d\">\n", os->init_seg_name, os->media_seg_name, c->use_timeline ? start_number : 1); + avio_printf(out, "initialization=\"%s\" media=\"%s\" startNumber=\"%d\"", os->init_seg_name, os->media_seg_name, c->use_timeline ? start_number : 1); + if (c->presentation_time_offset) + avio_printf(out, " presentationTimeOffset=\"%"PRId64"\"", c->presentation_time_offset); + avio_printf(out, ">\n"); if (c->use_timeline) { int64_t cur_time = 0; avio_printf(out, "\t\t\t\t\t\n"); @@ -745,10 +752,9 @@ static void write_time(AVIOContext *out, int64_t time) avio_printf(out, "%d.%dS", seconds, fractions / (AV_TIME_BASE / 10)); } -static void format_date_now(char *buf, int size) +static void format_date(char *buf, int size, int64_t time_us) { struct tm *ptm, tmbuf; - int64_t time_us = av_gettime(); int64_t time_ms = time_us / 1000; const time_t time_s = time_ms / 1000; int millisec = time_ms - (time_s * 1000); @@ -787,6 +793,7 @@ static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_ind if (as->descriptor) avio_printf(out, "\t\t\t%s\n", as->descriptor); for (i = 0; i < s->nb_streams; i++) { + AVStream *st = s->streams[i]; OutputStream *os = &c->streams[i]; char bandwidth_str[64] = {'\0'}; @@ -798,7 +805,6 @@ static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_ind os->bit_rate); if (as->media_type == AVMEDIA_TYPE_VIDEO) { - AVStream *st = s->streams[i]; avio_printf(out, "\t\t\tformat_name, os->codec_str, bandwidth_str, s->streams[i]->codecpar->width, s->streams[i]->codecpar->height); if (st->avg_frame_rate.num) @@ -810,6 +816,12 @@ 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); } + if (!final && c->write_prft && os->producer_reference_time_str[0]) { + avio_printf(out, "\t\t\t\t\n", + i, os->producer_reference_time_str, c->presentation_time_offset); + avio_printf(out, "\t\t\t\t\t\n", c->utc_timing_url); + avio_printf(out, "\t\t\t\t\n"); + } output_segment_list(os, out, s, i, final); avio_printf(out, "\t\t\t\n"); } @@ -1038,7 +1050,7 @@ static int write_manifest(AVFormatContext *s, int final) avio_printf(out, "\tsuggestedPresentationDelay=\"PT%"PRId64"S\"\n", c->last_duration / AV_TIME_BASE); if (c->availability_start_time[0]) avio_printf(out, "\tavailabilityStartTime=\"%s\"\n", c->availability_start_time); - format_date_now(now_str, sizeof(now_str)); + format_date(now_str, sizeof(now_str), av_gettime()); if (now_str[0]) avio_printf(out, "\tpublishTime=\"%s\"\n", now_str); if (c->window_size && c->use_template) { @@ -1230,6 +1242,16 @@ static int dash_init(AVFormatContext *s) c->frag_type = FRAG_TYPE_EVERY_FRAME; } + if (c->write_prft && !c->utc_timing_url) { + av_log(s, AV_LOG_WARNING, "Producer Reference Time element option will be ignored as utc_timing_url is not set\n"); + c->write_prft = 0; + } + + if (c->write_prft && !c->streaming) { + av_log(s, AV_LOG_WARNING, "Producer Reference Time element option will be ignored as streaming is not enabled\n"); + c->write_prft = 0; + } + av_strlcpy(c->dirname, s->url, sizeof(c->dirname)); ptr = strrchr(c->dirname, '/'); if (ptr) { @@ -1385,6 +1407,8 @@ static int dash_init(AVFormatContext *s) av_dict_set(&opts, "movflags", "+frag_custom", AV_DICT_APPEND); if (os->frag_type == FRAG_TYPE_DURATION) av_dict_set_int(&opts, "frag_duration", os->frag_duration, 0); + if (c->write_prft) + av_dict_set(&opts, "write_prft", "wallclock", 0); } else { av_dict_set_int(&opts, "cluster_time_limit", c->seg_duration / 1000, 0); av_dict_set_int(&opts, "cluster_size_limit", 5 * 1024 * 1024, 0); // set a large cluster size limit @@ -1796,15 +1820,21 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) pkt->dts = 0; } - if (os->first_pts == AV_NOPTS_VALUE) + if (os->first_pts == AV_NOPTS_VALUE) { + int side_data_size; + AVProducerReferenceTime *prft = (AVProducerReferenceTime *)av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, + &side_data_size); + if (prft && side_data_size == sizeof(AVProducerReferenceTime) && !prft->flags) + os->producer_reference_time = prft->wallclock; os->first_pts = pkt->pts; + } os->last_pts = pkt->pts; if (!c->availability_start_time[0]) { int64_t start_time_us = av_gettime(); c->start_time_s = start_time_us / 1000000; - format_date_now(c->availability_start_time, - sizeof(c->availability_start_time)); + format_date(c->availability_start_time, + sizeof(c->availability_start_time), start_time_us); } if (!os->packets_written) @@ -1860,6 +1890,12 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) } } + if (c->write_prft && os->producer_reference_time && !os->producer_reference_time_str[0]) + format_date(os->producer_reference_time_str, + sizeof(os->producer_reference_time_str), + os->producer_reference_time + (av_rescale_q(os->first_pts, st->time_base, AV_TIME_BASE_Q) - + c->presentation_time_offset)); + if ((ret = dash_flush(s, 0, pkt->stream_index)) < 0) return ret; } @@ -2062,6 +2098,7 @@ static const AVOption options[] = { { "ignore_io_errors", "Ignore IO errors during open and write. Useful for long-duration runs with network output", OFFSET(ignore_io_errors), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { "lhls", "Enable Low-latency HLS(Experimental). Adds #EXT-X-PREFETCH tag with current segment's URI", OFFSET(lhls), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { "master_m3u8_publish_rate", "Publish master playlist every after this many segment intervals", OFFSET(master_publish_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT_MAX, E}, + { "write_prft", "Write producer reference time element", OFFSET(write_prft), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E}, { NULL }, };