From patchwork Tue Mar 14 10:50:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?5Y2i54Kv5YGl?= X-Patchwork-Id: 2925 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.50.79 with SMTP id y76csp1703453vsy; Tue, 14 Mar 2017 10:39:43 -0700 (PDT) X-Received: by 10.28.135.21 with SMTP id j21mr798158wmd.70.1489513183773; Tue, 14 Mar 2017 10:39:43 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k71si5926231wrc.80.2017.03.14.10.39.43; Tue, 14 Mar 2017 10:39:43 -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=@gmail.com; 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=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E652D6804BA; Tue, 14 Mar 2017 19:39:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f67.google.com (mail-pg0-f67.google.com [74.125.83.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A504E688284 for ; Tue, 14 Mar 2017 12:50:43 +0200 (EET) Received: by mail-pg0-f67.google.com with SMTP id g2so19241038pge.2 for ; Tue, 14 Mar 2017 03:50:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Y5OaSxfwj11sax0omY4uKtGJ1XDW19fsV4BYExJ/Z9c=; b=MylFwihKP/+ANKIFHU0mhJNvYPWyMrwiTQabuH+OOZeDW0xWGeBQVOd15v52RfbBFE CyZ7Tsge/ruOyKJPRd+D5RokOdVpouEmVcylSS1z6pfOQiwf+O1AzWW/PYfv/ryTcKZ8 bcTCxxXm6NfujuxvZAcRSTsU5U26VtIRpSKLAOVmnkW/MgLJg0vZQWAzhLMVDOJpqCES /3D1XpEte//wv7TqM4XMxt1IB6+UqdFsSq9zyG6GQlmmRVjfvhJwQffD4C4oqyNaicW2 rf1Xqt1Qoa44552cKFdKVFA3lTujuyT2UCZgoZgMQMF/Vhq9NxJgr/+coLaROU9Fwiee CPPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Y5OaSxfwj11sax0omY4uKtGJ1XDW19fsV4BYExJ/Z9c=; b=ALofwPBd1UKRixT4fZogv3nFLWvNDlSTTbkLjJ+cumNeSZ0So/sDopTaAC/cPJPYwk TOfxgtegdBMoQzCOrqeleekcWOmrrZWT97RKTrJqSfLFWAJec+WWU4dbFXZjy5ujlZiq lDxfUiVZlGC5JwEVyV75NUk/I7sqPdZ7/L4ATk1zKQTvrEqP2qQ06UPysUkSRJsV5fZi JjPnBZiZbJ/o8YWH/Ye8mwwW7Hup7XawWNnRYfAOTivwlPm6OnwFYMddaJGtjeswdmHa YS/DAdRVV8itJA8P/R51WSqhPdWUkNLJf0U5PvA/hyBNVIDxUq8KQrx27du7pl1jN1DE 3ZGw== X-Gm-Message-State: AMke39mQviV/RH43eq0hNjYwfbgltyXUE3ZGkaKHdVq4PNWABP3AG/LyVph3OsVC1gxtlA== X-Received: by 10.84.168.4 with SMTP id e4mr55483183plb.138.1489488657621; Tue, 14 Mar 2017 03:50:57 -0700 (PDT) Received: from localhost.localdomain ([216.189.52.178]) by smtp.googlemail.com with ESMTPSA id u75sm38221670pgc.31.2017.03.14.03.50.56 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 14 Mar 2017 03:50:57 -0700 (PDT) From: Wing Lo To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Mar 2017 18:50:24 +0800 Message-Id: <20170314105024.20051-1-lujiongjian1005@gmail.com> X-Mailer: git-send-email 2.10.1 (Apple Git-78) X-Mailman-Approved-At: Tue, 14 Mar 2017 19:39:23 +0200 Subject: [FFmpeg-devel] [PATCH] avformat/http: fix http reconnect 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: Wing Lo MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" I use the commad line below: ffplay -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 4000 http://movie_url.mp4 I find that if network source down, ffplay keep reconnect only a few seconds even if I set -reconnect_delay_max 4000. So I reopen http connection cyclically until timeout, reconnect successfully or user quit. My patch's commad line is as below: ffplay -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_timeout 30 http://movie_url.mp4 Signed-off-by: Wing Lo --- libavformat/http.c | 56 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/libavformat/http.c b/libavformat/http.c index 293a8a7..d38c569 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -112,8 +112,8 @@ typedef struct HTTPContext { int reconnect; int reconnect_at_eof; int reconnect_streamed; - int reconnect_delay; - int reconnect_delay_max; + int reconnect_timeout; + uint8_t can_reconnect; int listen; char *resource; int reply_code; @@ -156,7 +156,7 @@ static const AVOption options[] = { { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D }, - { "reconnect_delay_max", "max reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_max), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D }, + { "reconnect_timeout", "set timeout (in seconds) for http reconnection", OFFSET(reconnect_timeout), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D }, { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E }, { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E}, @@ -489,6 +489,7 @@ static int http_open(URLContext *h, const char *uri, int flags, else h->is_streamed = 1; + s->can_reconnect = 1; s->filesize = UINT64_MAX; s->location = av_strdup(uri); if (!s->location) @@ -1270,11 +1271,42 @@ static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size) static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect); +static int http_reconnect(URLContext *h) +{ + int64_t wait_start = 0; + int64_t seek_ret; + HTTPContext *s = h->priv_data; + uint64_t target = h->is_streamed ? 0 : s->off; + int timeout = s->reconnect_timeout * 1000*1000; + + if (!s->can_reconnect) + return -1; + + av_log(h, AV_LOG_INFO, "Will reconnect at %"PRIu64".\n", s->off); + do { + if (ff_check_interrupt(&h->interrupt_callback)) + return AVERROR_EXIT; + seek_ret = http_seek_internal(h, target, SEEK_SET, 1); + usleep(1000*1000); + if (timeout > 0) { + if (!wait_start) + wait_start = av_gettime_relative(); + else if (av_gettime_relative() - wait_start > timeout) { + av_log(h, AV_LOG_ERROR, "Reconnect time out. Failed to reconnect at %"PRIu64".\n", target); + s->can_reconnect = 0; + return AVERROR(ETIMEDOUT); + } + } + }while (seek_ret != target); + + av_log(h, AV_LOG_INFO, "Successful to reconnect at %"PRIu64".\n", target); + return 0; +} + static int http_read_stream(URLContext *h, uint8_t *buf, int size) { HTTPContext *s = h->priv_data; int err, new_location, read_ret; - int64_t seek_ret; if (!s->hd) return AVERROR_EOF; @@ -1292,23 +1324,13 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size) read_ret = http_buf_read(h, buf, size); if ( (read_ret < 0 && s->reconnect && (!h->is_streamed || s->reconnect_streamed) && s->filesize > 0 && s->off < s->filesize) || (read_ret == 0 && s->reconnect_at_eof && (!h->is_streamed || s->reconnect_streamed))) { - uint64_t target = h->is_streamed ? 0 : s->off; + av_log(h, AV_LOG_INFO, "http buffer read error=%s.\n", av_err2str(read_ret)); - if (s->reconnect_delay > s->reconnect_delay_max) - return AVERROR(EIO); - - av_log(h, AV_LOG_INFO, "Will reconnect at %"PRIu64" error=%s.\n", s->off, av_err2str(read_ret)); - av_usleep(1000U*1000*s->reconnect_delay); - s->reconnect_delay = 1 + 2*s->reconnect_delay; - seek_ret = http_seek_internal(h, target, SEEK_SET, 1); - if (seek_ret != target) { - av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRIu64".\n", target); + if (http_reconnect(h) < 0) return read_ret; - } read_ret = http_buf_read(h, buf, size); - } else - s->reconnect_delay = 0; + } return read_ret; }