From patchwork Thu Oct 20 12:11:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gnattu X-Patchwork-Id: 38857 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp128295pzb; Thu, 20 Oct 2022 05:12:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4jA80SJICBfogtlczyw4evGZ+6ct49YkdH2/7HvzQILykXaVlIHU9J+4YDd9LSCYdlLBzz X-Received: by 2002:a17:907:847:b0:77f:f489:cc25 with SMTP id ww7-20020a170907084700b0077ff489cc25mr10669253ejb.80.1666267923800; Thu, 20 Oct 2022 05:12:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666267923; cv=none; d=google.com; s=arc-20160816; b=Pq6GGp2/b+raon0dvYpmLVTqKo77SG0C12PWZPqBmFQjdUNNve0fb+myXOMPwSGeF/ o+uFQRLHkf9B8PRWiy+nxeLPr5R45LxUJ+RJ4tC2rsq/imHJhXL6kE6OBQiwai5airVr UxptIM6nASidOzOX9BBFRJYPkHbSUsG6SGAoE37cerdnaCOr03Dh88teCsiwT7PghC4v yl2rnua38WIWn4Zx3dq3ypRfB8n6nj0kNTqnZLnRPh+wp+YKE5BSDPF2hrLZQLV6D1Mp ZPqV5neI4aJh1SyqJJfOm6joZF8tIGKaRtrAx2hL85Rs+qeVWa1QpI38S5/8idQ+r3k9 Wy+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=pimzPcOtgw4XU1iXceFiVmj8tR5F7Zz5G8VKPOfq7bA=; b=Z3XwrFoHGbAAQ0knFxpbkob8GUMBIsUKFL9b8FoJyhFJYxmDVw/LjR1OlsFb3EGctD oxTzdGgCXxhz/pcHWj/LkD7bk0+hAhuUcQjQlyzWMNXrE4xPYqMSTxvqH9DQ+Cqf99B3 ULVw9oqf9G4XKmPbhx/5tHU//wqY0zdUG4t815cCxzD/AILpUxG68mriV6IMhcJwCCFe FOARaBmcnNt9AjDYrw+AY698ZTvjwfhHQG9PsxBu1itwlebG7X0CCEwLvcHcfU5cKpnn bXuvs+DofAMmmll5GEpW46Vf4dtigP4NvJPmQRW6jVxHuF+pk+FwfSGEHNDaP9q8D92D U37Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@me.com header.s=1a1hai header.b=H8uMEmNB; 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=QUARANTINE dis=NONE) header.from=me.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ji4-20020a170907980400b0078e2f4c8851si14266155ejc.595.2022.10.20.05.12.00; Thu, 20 Oct 2022 05:12:03 -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=@me.com header.s=1a1hai header.b=H8uMEmNB; 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=QUARANTINE dis=NONE) header.from=me.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 877FA68BE45; Thu, 20 Oct 2022 15:11:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from pv50p00im-ztdg10021101.me.com (pv50p00im-ztdg10021101.me.com [17.58.6.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A9BE668BE44 for ; Thu, 20 Oct 2022 15:11:48 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=me.com; s=1a1hai; t=1666267906; bh=0H+08qp9P+jPSakbujTXQdoZdukF8ku5J+/YKDNDzOU=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=H8uMEmNBc9m6I5Yw+QCuB4BN9vPeZ36rfVVt5X2SPMtmUUZW/COLg7vm/FLg/2Cnt HiotGocRptAwEam+e/tx8zn76YMxGXiWVjPRHCxJF+8IC7NxSb2T/oy8aA+c2a/Za8 ozA9zwoLQwZp4QNSCEOOKj4296Omj40luOyL44PU0kXqe9JsK1qur/rJOg02E/Ipx+ 5t6nTMEthPrnM6f82WCPrEUwa8y/zv5JfJImwP8PASiZkTUWFCrFJHxvlgR4XNR1vR Pgrh5SoWZbd4lip7y9ClA0ZhFQY8VImoL8/tmbzpuAk8lucCOf1pqryZlkavxiUGam BDABv5DvPpcVw== Received: from localhost.localdomain (pv50p00im-dlb-asmtp-mailmevip.me.com [17.56.9.10]) by pv50p00im-ztdg10021101.me.com (Postfix) with ESMTPSA id 928DDD008EF; Thu, 20 Oct 2022 12:11:44 +0000 (UTC) From: gnattu To: ffmpeg-devel@ffmpeg.org Date: Thu, 20 Oct 2022 20:11:38 +0800 Message-Id: <20221020121138.98198-1-gnattuoc@me.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) MIME-Version: 1.0 X-Proofpoint-GUID: T7P7PJpkNu74MCGfzcc6m00q6cDkVFZ2 X-Proofpoint-ORIG-GUID: T7P7PJpkNu74MCGfzcc6m00q6cDkVFZ2 X-Proofpoint-Virus-Version: =?utf-8?q?vendor=3Dfsecure_engine=3D1=2E1=2E170-?= =?utf-8?q?22c6f66c430a71ce266a39bfe25bc2903e8d5c8f=3A6=2E0=2E138=2C18=2E0?= =?utf-8?q?=2E883=2C17=2E0=2E605=2E474=2E0000000_definitions=3D2022-06-21=5F?= =?utf-8?q?08=3A2020-02-14=5F02=2C2022-06-21=5F08=2C2020-01-23=5F02_signatur?= =?utf-8?q?es=3D0?= X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 suspectscore=0 spamscore=0 malwarescore=0 clxscore=1015 bulkscore=0 mlxscore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210200072 Subject: [FFmpeg-devel] [PATCH v5] avformat/hls: Add option to retry failed segments for hls X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: gnattu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jvBStC7nxc8f Current HLS implementation simply skip a failed segment to catch up the stream, but this is not optimal for some use cases like livestream recording. Add an option to retry a failed segment to ensure the output file is a complete stream. Signed-off-by: gnattu --- v5 changed coding style as requested v4 added documentation for the new seg_max_try option doc/demuxers.texi | 4 ++++ libavformat/hls.c | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/demuxers.texi b/doc/demuxers.texi index 2b6dd86c2a..3e09a0f14e 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -401,6 +401,10 @@ Use HTTP partial requests for downloading HTTP segments. @item seg_format_options Set options for the demuxer of media segments using a list of key=value pairs separated by @code{:}. + +@item seg_max_retry +Maximum number of times to reload a segment on error, useful when segment skip on network error is not desired. +Default value is 0. @end table @section image2 diff --git a/libavformat/hls.c b/libavformat/hls.c index e622425e80..2a5ffb927f 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -225,6 +225,7 @@ typedef struct HLSContext { int http_persistent; int http_multiple; int http_seekable; + int seg_max_retry; AVIOContext *playlist_pb; HLSCryptoContext crypto_ctx; } HLSContext; @@ -1472,6 +1473,7 @@ static int read_data(void *opaque, uint8_t *buf, int buf_size) int ret; int just_opened = 0; int reload_count = 0; + int segment_retries = 0; struct segment *seg; restart: @@ -1563,9 +1565,18 @@ reload: av_log(v->parent, AV_LOG_WARNING, "Failed to open segment %"PRId64" of playlist %d\n", v->cur_seq_no, v->index); - v->cur_seq_no += 1; + if (segment_retries >= c->seg_max_retry) { + av_log(v->parent, AV_LOG_WARNING, "Segment %"PRId64" of playlist %d failed too many times, skipping\n", + v->cur_seq_no, + v->index); + v->cur_seq_no++; + segment_retries = 0; + } else { + segment_retries++; + } goto reload; } + segment_retries = 0; just_opened = 1; } @@ -2549,6 +2560,8 @@ static const AVOption hls_options[] = { OFFSET(http_seekable), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, FLAGS}, {"seg_format_options", "Set options for segment demuxer", OFFSET(seg_format_opts), AV_OPT_TYPE_DICT, {.str = NULL}, 0, 0, FLAGS}, + {"seg_max_retry", "Maximum number of times to reload a segment on error.", + OFFSET(seg_max_retry), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS}, {NULL} };