From patchwork Tue Oct 5 23:32:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 30916 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp5893292iob; Tue, 5 Oct 2021 16:33:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwA7WqBhnlJGYlQsJcTOxxfwKnnEORkV0EW9UgKecj8rXO8DfnbbHvLPA5kzS6vBdbtB36z X-Received: by 2002:a05:6402:34d6:: with SMTP id w22mr2661029edc.358.1633476809395; Tue, 05 Oct 2021 16:33:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633476809; cv=none; d=google.com; s=arc-20160816; b=KHkFtk33wh1LL35H6hvJV001WJlj+jTazDBiPyDEI/jsakMqpMIf3VHtBe2c7H0PLC XjsJOQIiwhkWEVTRYy2p6mWEpCBCnxTp3zwaRBpFpILZ1cP3lvZQSJw5nZv5815/Skkw PSKxQl/AGQzitvU8yOHMLNGrE+eINN79OMgNcJbdTOowvHVYqTGjyztDhTlwF56eoB78 uE8P2aWXF2YAxElAgCNlA3ot51JukrEIswuWfORWOc3rUI/RF5MyCdOYO2vvXKp97ZcH hsTlWkAs7y8uPX5cB9IJkmh3f06zlRaVLE15a3CEMMojD7/bhOZ9/mMcmK6Pf5uFKBvX Qlgg== 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 :delivered-to; bh=xwtQvxB58aW+NKR2cGGZq5rrVM3Ltvo3HjMOQGWVmKM=; b=YVFlv9GOMtOT5qHav8Zyqqe7KGy/R1cAabm2czOO1NrXEuCyhR6lwEGxZ31UUo5Iq+ 2Tq8JHKJzMyL+39jXEpQEeHikUoEVZpYU5D4WtR0Md2VkjKH6t83NCYin8taEDUqVTIY kGPZBs5nPRHv4BddIj1LkPyBKkWRkSs07W568KTmDyRmBKkS9gQUUp5ZeZ1ILN5Hqxou wbOCPDRCbj0A7f7AD/21gsYo7itj7yhSZ6rNvj9HmFUyhxyvGvIVyYX4jI1dJErIy+q4 dOZJPw01IVzujd0n1ySpExdf9rJalPqr77osM67nJLIPUg4Z0LXQ2E7VTn+ijegplZ/t 4XfQ== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hx14si21641975ejc.326.2021.10.05.16.33.06; Tue, 05 Oct 2021 16:33:29 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 58D6A68A42C; Wed, 6 Oct 2021 02:33:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 69C5D68A42C for ; Wed, 6 Oct 2021 02:32:56 +0300 (EEST) Received: by mail-pg1-f180.google.com with SMTP id r2so774673pgl.10 for ; Tue, 05 Oct 2021 16:32:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qw45S2U6PN8vyscxDVSVzNgXyGIBYNyqFSneQeiXOyI=; b=ndUcACJMRAkK+WOE980oxv3fm/SmM9FrXfXC6OCjos0OV/Y1jumoqikYnGtsOPT1cp 6JGYjX/HwdOqS1DMrPWo5inRmoeVgHT/+hwcyPTXQugme8Z5sGrMoEw82C0HFv5g5Swm ETY4uZKWs06hsDutP+yR8aJEXlw6/+4hzE1K8OwV8C6kg8s52foOUIJTpwnsHs+BxnKS XPqxBI6bh2DPEP+T0RgZ47oHdI9rh/uwVBJEici5n/8ZBrX7+kNSVHdWoAmXJZdVPGD9 xJZWqVqErbclBr8I343wlCAzwbLatF1oPNjtIglsXns1Pf+4PFIuph9xqlhiXHXzH/pN Sj1w== X-Gm-Message-State: AOAM531Q9iX4keupZR5vg5L9C/GlFKNnGwvBnVW5uOVi3vPJISRoC9Qi Pc/bJgzs4dtLosototJTMt0HaUvvC3cxTzKJ/Xc= X-Received: by 2002:a65:564e:: with SMTP id m14mr18075642pgs.113.1633476773878; Tue, 05 Oct 2021 16:32:53 -0700 (PDT) Received: from tmm1-imac.lan ([2600:8802:5d01:2170:752c:3095:1b32:fe3]) by smtp.gmail.com with ESMTPSA id k9sm19590224pff.22.2021.10.05.16.32.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Oct 2021 16:32:53 -0700 (PDT) From: Aman Karmani To: ffmpeg-devel@ffmpeg.org Date: Tue, 5 Oct 2021 16:32:44 -0700 Message-Id: <20211005233244.37582-1-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/http: parse Content-Range responses with unknown document size 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: Aman Karmani Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: FtE1fglk9fKg From: Aman Karmani The HTTP spec allows '*' for document size in Content-Range response headers, to specify that the file size is unknown: Content-Range: -/ Content-Range: -/* Content-Range: */ In the case where the filesize is unknown, this patch keeps track of a new fileend variable which is the approximate size based on the range and data returned by the server. Practically speaking, this patch can be used effectively "tail" a stream that is still being appended to. For example, a PVR system may record an mpegts stream to disk, and serve up the same file to players while the stream is actively being recorded. By adding a `Content-Range: bytes X-Y/*` header to the response, the server can inform the ffmpeg-based player that the stream is "active" and that the file size will continue to grow. For every incoming request, the server would: - open file handle to recording in progress - seek file handle to offset requested by the incoming `Range` header - query the current file size of the file - return a `Content-Range` header with the format `bytes -/*` - copy bytes from the file to the http socket - on eof, wait and try reading from the file again to "tail" it as long as the recording is "active" On the ffmpeg size, we simply use MAX(s->off, s->fileend) to decide how large the file is whenever requested via AVSEEK_SIZE. This effectively allows ffmpeg based players to watch and seek around a growing mpegts stream even while it is being appended to. Signed-off-by: Aman Karmani --- libavformat/http.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/libavformat/http.c b/libavformat/http.c index 476b9a8456..9dc0ff935d 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -68,7 +68,7 @@ typedef struct HTTPContext { /* Used if "Transfer-Encoding: chunked" otherwise -1. */ uint64_t chunksize; int chunkend; - uint64_t off, end_off, filesize; + uint64_t off, end_off, filesize, fileend; char *location; HTTPAuthState auth_state; HTTPAuthState proxy_auth_state; @@ -621,6 +621,7 @@ static int http_open(URLContext *h, const char *uri, int flags, h->is_streamed = 1; s->filesize = UINT64_MAX; + s->fileend = UINT64_MAX; s->location = av_strdup(uri); if (!s->location) return AVERROR(ENOMEM); @@ -750,13 +751,22 @@ static int parse_location(HTTPContext *s, const char *p) static void parse_content_range(URLContext *h, const char *p) { HTTPContext *s = h->priv_data; - const char *slash; + const char *slash, *dash; + uint64_t range_end = UINT64_MAX; if (!strncmp(p, "bytes ", 6)) { p += 6; s->off = strtoull(p, NULL, 10); - if ((slash = strchr(p, '/')) && strlen(slash) > 0) - s->filesize = strtoull(slash + 1, NULL, 10); + if ((dash = strchr(p, '-'))) + range_end = strtoll(dash + 1, NULL, 10); + if ((slash = strchr(p, '/')) && strlen(slash) > 0) { + if (slash[0] == '*') { + s->fileend = range_end; + } else { + s->filesize = strtoull(slash + 1, NULL, 10); + s->fileend = UINT64_MAX; + } + } } if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647)) h->is_streamed = 0; /* we _can_ in fact seek */ @@ -1404,6 +1414,7 @@ static int http_connect(URLContext *h, const char *path, const char *local_path, s->off = 0; s->icy_data_read = 0; s->filesize = UINT64_MAX; + s->fileend = UINT64_MAX; s->willclose = 0; s->end_chunked_post = 0; s->end_header = 0; @@ -1779,19 +1790,21 @@ static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int fo int old_buf_size, ret; AVDictionary *options = NULL; - if (whence == AVSEEK_SIZE) + if (whence == AVSEEK_SIZE && s->fileend != UINT64_MAX) + return s->off > s->fileend ? s->off : s->fileend; + else if (whence == AVSEEK_SIZE) return s->filesize; else if (!force_reconnect && ((whence == SEEK_CUR && off == 0) || (whence == SEEK_SET && off == s->off))) return s->off; - else if ((s->filesize == UINT64_MAX && whence == SEEK_END)) + else if ((s->filesize == UINT64_MAX && s->fileend == UINT64_MAX && whence == SEEK_END)) return AVERROR(ENOSYS); if (whence == SEEK_CUR) off += s->off; else if (whence == SEEK_END) - off += s->filesize; + off += s->fileend > 0 ? s->fileend : s->filesize; else if (whence != SEEK_SET) return AVERROR(EINVAL); if (off < 0)