From patchwork Wed Apr 11 07:02:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dixit, Vishwanath" X-Patchwork-Id: 8398 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp370195jad; Wed, 11 Apr 2018 00:02:52 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/H4+7f2UmkwgpnNid1m8FCiDqsCdv4Yi2no8I2cR+9Foq4NkIQpJt6XFjU08+FJjU+cyQs X-Received: by 10.28.116.7 with SMTP id p7mr1622842wmc.65.1523430172589; Wed, 11 Apr 2018 00:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523430172; cv=none; d=google.com; s=arc-20160816; b=HGhen0FUeK5lw6oqyByVbKPu00p3Xir14Q8Q5TPzbbi0l3dALNG/YN2OYkKeJIBk7v eNozGLrfFgiFzZ8BdE17Z6K346w7e5lumeUmVTfNHTe6OfTH4NT2K2P/Age0SJd/dZ4V dQf91hLF3NBnqKnlsx6bIsD4+TEMB22TcJpt9xLpVJhIiEWBFk4O8Jc/B7qH8gFFGxqT 1maMP4judUainX0TqpG/WoEvpYLyg0gwwl0icOSmOdLAJAqAfWJ6qKaYYp2zrHhP8Kbk KfUB+ob1hrphLfRU7htpQZq7B/aSvAloejrWUg4MG9KUWmtcteJ+dHGo4X0HL1/eOoYA HUcA== 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=xJjdrqTUFJbfQFS8W40DmLdwgi/aDjsM1yPUqY4CDC4=; b=N15sCoF8wXp0iO9gGUGw9gvLf3lXU2bsIlWhbVeI0gR4jvOY9wIvw+Cwk1FP0ICozx 6un+8S/7/KGSfznUcWMRmOV+7tlCFgP/utNmk5vov0NSwDRBcnnnBDxfXhT9TPHClRhH 49oRvc55PIAMCvlwa1qOvispKc6A5j+9h+DhLMnS+Pp4kWIWjN3zxDgNZZm+09wfkvVE BznetN5l/QxeM+9U9qqAHEOd+XbhkYVDWD8sjrfbtjeQYN14B75ooznTJCt+rZmza6zD zgYxDMQQk8LlecLJPNwEZgOx3TEqticvRlAUMXFXtiuJQMnpZoIZXX14uyZAWGT/bTEs Y2Kg== 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=uIkEbYIk; 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 b69si537345wmb.184.2018.04.11.00.02.52; Wed, 11 Apr 2018 00:02:52 -0700 (PDT) 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=uIkEbYIk; 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 D98A668A273; Wed, 11 Apr 2018 10:02:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from a1i318.smtp2go.com (a1i318.smtp2go.com [43.228.185.62]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A05A5689983 for ; Wed, 11 Apr 2018 10:02:07 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpservice.net; s=m78bu0.a1-4.dyn; x=1523431052; h=Feedback-ID: X-Smtpcorp-Track:Message-Id:Date:Subject:To:From:Reply-To:Sender: List-Unsubscribe; bh=2slo/unk0GrXKyMYQyb8u4G+0MfNpxB2J/3G29qONm0=; b=uIkEbYIk SUKGM8Tf5OsCEr8aC0G/PgG2IIRdo2+tmUi0MaO7sLnvC+uThJjHzTCyf1YYwH5EVib1D+hB+SjnJ hAcsiTIHVrX0LR5jU6XoC9igNiKKjEc6MBhuFA3kpkNWrN5PcMhLsj+5+76gQJnvKq0lEMHL8TtLC 9LIXGK9LHQNBiDr67cDR9qtdY2SZ8oFI0oDdqvBVfDnCUXCHBUmceHif7AAL2sXw9NDdda1CA+2yJ OdCBpYPp0n92Dy6jSEbbHUq9SuDW8AMZJGsu4YIpsMDSOfiXs7sLQlbLmzrsJDlM3MLUvM1aTAtKs Ppw5ovSIIizquBT26EGZibiRew==; From: vdixit@akamai.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 11 Apr 2018 12:32:25 +0530 Message-Id: <1523430145-17663-1-git-send-email-vdixit@akamai.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1522831101-7665-1-git-send-email-vdixit@akamai.com> References: <1522831101-7665-1-git-send-email-vdixit@akamai.com> X-Smtpcorp-Track: 1f69X04bajUPDI.zfuFZn5AD Feedback-ID: 337386m:337386asVRLGB:337386sjP2J3sOFI:SMTPCORP X-Report-Abuse: Please forward a copy of this message, including all headers, to Subject: [FFmpeg-devel] [PATCH v3 11/11] avformat/dashenc: addition of segment index correction logic 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: Vishwanath Dixit MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Vishwanath Dixit The logic is applicable only when use_template is enabled and use_timeline is disabled. The logic monitors the flow of segment indexes. If a streams's segment index value is not at the expected real time position, then the logic corrects that index value. Typically this logic is needed in live streaming use cases. The network bandwidth fluctuations are common during long run streaming. Each fluctuation can cause the segment indexes fall behind the expected real time position. Without this logic, players will not be able to consume the content, even after encoder's network condition comes back to normal state. --- doc/muxers.texi | 11 +++++++++++ libavformat/dashenc.c | 31 ++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index e1b6666..fa528b6 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -271,6 +271,17 @@ To map all video (or audio) streams to an AdaptationSet, "v" (or "a") can be use When no assignment is defined, this defaults to an AdaptationSet for each stream. @item -timeout @var{timeout} Set timeout for socket I/O operations. Applicable only for HTTP output. +@item -index_correction @var{index_correction} +Enable (1) or Disable (0) segment index correction logic. Applicable only when +@var{use_template} is enabled and @var{use_timeline} is disabled. + +When enabled, the logic monitors the flow of segment indexes. If a streams's +segment index value is not at the expected real time position, then the logic +corrects that index value. + +Typically this logic is needed in live streaming use cases. The network bandwidth +fluctuations are common during long run streaming. Each fluctuation can cause +the segment indexes fall behind the expected real time position. @end table @anchor{framecrc} diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index bf8698f..9e72636 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -76,7 +76,7 @@ typedef struct OutputStream { int nb_segments, segments_size, segment_index; Segment **segments; int64_t first_pts, start_pts, max_pts; - int64_t last_dts; + int64_t last_dts, last_pts; int bit_rate; char codec_str[100]; @@ -123,6 +123,7 @@ typedef struct DASHContext { AVIOContext *m3u8_out; int streaming; int64_t timeout; + int index_correction; } DASHContext; static struct codec_string { @@ -1060,7 +1061,7 @@ static int dash_write_header(AVFormatContext *s) static int add_segment(OutputStream *os, const char *file, int64_t time, int duration, int64_t start_pos, int64_t range_length, - int64_t index_length) + int64_t index_length, int next_exp_index) { int err; Segment *seg; @@ -1088,6 +1089,12 @@ static int add_segment(OutputStream *os, const char *file, seg->index_length = index_length; os->segments[os->nb_segments++] = seg; os->segment_index++; + //correcting the segment index if it has fallen behind the expected value + if (os->segment_index < next_exp_index) { + av_log(NULL, AV_LOG_WARNING, "Correcting the segment index after file %s: current=%d corrected=%d\n", + file, os->segment_index, next_exp_index); + os->segment_index = next_exp_index; + } return 0; } @@ -1177,10 +1184,22 @@ static int dash_flush(AVFormatContext *s, int final, int stream) const char *proto = avio_find_protocol_name(s->url); int use_rename = proto && !strcmp(proto, "file"); - int cur_flush_segment_index = 0; - if (stream >= 0) + int cur_flush_segment_index = 0, next_exp_index = -1; + if (stream >= 0) { cur_flush_segment_index = c->streams[stream].segment_index; + //finding the next segment's expected index, based on the current pts value + if (c->use_template && !c->use_timeline && c->index_correction && + c->streams[stream].last_pts != AV_NOPTS_VALUE && + c->streams[stream].first_pts != AV_NOPTS_VALUE) { + int64_t pts_diff = av_rescale_q(c->streams[stream].last_pts - + c->streams[stream].first_pts, + s->streams[stream]->time_base, + AV_TIME_BASE_Q); + next_exp_index = (pts_diff / c->seg_duration) + 1; + } + } + for (i = 0; i < s->nb_streams; i++) { OutputStream *os = &c->streams[i]; AVStream *st = s->streams[i]; @@ -1240,7 +1259,7 @@ static int dash_flush(AVFormatContext *s, int final, int stream) if (bitrate >= 0) os->bit_rate = bitrate; } - add_segment(os, os->filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length); + add_segment(os, os->filename, os->start_pts, os->max_pts - os->start_pts, os->pos, range_length, index_length, next_exp_index); av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, os->full_path); os->pos += range_length; @@ -1303,6 +1322,7 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) if (os->first_pts == AV_NOPTS_VALUE) os->first_pts = pkt->pts; + os->last_pts = pkt->pts; if (!c->availability_start_time[0]) format_date_now(c->availability_start_time, @@ -1485,6 +1505,7 @@ static const AVOption options[] = { { "hls_playlist", "Generate HLS playlist files(master.m3u8, media_%d.m3u8)", OFFSET(hls_playlist), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { "streaming", "Enable/Disable streaming mode of output. Each frame will be moof fragment", OFFSET(streaming), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { "timeout", "set timeout for socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT_MAX, .flags = E }, + { "index_correction", "Enable/Disable segment index correction logic", OFFSET(index_correction), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { NULL }, };