From patchwork Fri Nov 18 18:48:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhao Zhili X-Patchwork-Id: 39329 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp534521pzb; Fri, 18 Nov 2022 10:48:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf7IOp78v+6wWCIWz/bCQ6WeXxbE0TYV82l5OkwmhahXFfA3dkCDItzNzvi6YlktACPN8fLT X-Received: by 2002:a17:906:2ac3:b0:7ad:f2f9:2b49 with SMTP id m3-20020a1709062ac300b007adf2f92b49mr6665328eje.94.1668797323753; Fri, 18 Nov 2022 10:48:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668797323; cv=none; d=google.com; s=arc-20160816; b=OR0dqY921na6HMOMF3Gpglfuz5vZ9C5x1biaTeqc/2rtuJWMX65+V7h2j1EL+DEkLj NYTne+SPryLKoZzCF26MDF6WuoQ+v5UgFGaHJqkBMfuCctRLvOs0w4Zi9JJqfYjmEhWI aBsCjkONbaOADyXWf8+pvoMFsLL+hvNjdA1X29iHAzlkaCWiYR63UffHpxgRJiJAIAJZ NfyHifTDjbjhrmW+PQ6zYJyH2AV6Y2O/KGqdc4P/Q3y4SXBJdvtSB1N7b6Q4bHOhT97L uOJy4o+ZFOGW+Cow7DCAfx/foPknLBAvEmwms2RTZNTlTA1ROjaPxYZ/q843HX/s1BDz IyLg== 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:date:to:from:message-id :dkim-signature:delivered-to; bh=6FCnVrW4UTGYoy1B8UZxJrv60bTqEIzePr5W7ebkfjA=; b=xeAwPAdHsh/IHfGggFSxhOxaBs/aYenONpveM1vzD/x/frpdBbXaqsXtaFt/8CAIwg EQWUvkQaOyVMm1kQ6wNj4Cpyb+zuTZiRA8RP0VRpMRbeifaOaSJO0tPNL2tDK0MF5hoe pEoXzr3+ARcFryVxeaDcpdlBLKvKUaXOtZtWTgIlx2R92nexvG7ZwPKdM4rMqt6dkrO1 sS6CnrdtyQxtKfPnUZaCwGjm3UV6bB8pFRCOOQ5+ZjLLLoxn9vOQosbaOyyE2LKfTZsV g/JDrxo0VRgweoSeJw/KHuWPzUDG61ze9rawr70zGoL7PdmoKQYv0uyw1ppRawprosbi b+8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=BflUg+63; 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=foxmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y25-20020a170906071900b0073306ff26besi2919790ejb.969.2022.11.18.10.48.42; Fri, 18 Nov 2022 10:48:43 -0800 (PST) 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=@foxmail.com header.s=s201512 header.b=BflUg+63; 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=foxmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AF54368BCA9; Fri, 18 Nov 2022 20:48:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out162-62-58-211.mail.qq.com (out162-62-58-211.mail.qq.com [162.62.58.211]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 96D6968B8BF for ; Fri, 18 Nov 2022 20:48:31 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1668797306; bh=m+d3KvdEwBHPRZkh/3lYVRmj13OdPOlW2uPcVzLe2u0=; h=From:To:Cc:Subject:Date; b=BflUg+63K9dZnDPYhx8KNkAsegubV8xl6KSMcVJ+VvfhYFMnvsL8A75G4Vto1W1Jb ukiPkcmkT9AvrBhCVfdAekuVibCDLgmoASb8jJ5VGa1hADsQod1fhzDICVIVBPAgyR jazdrRBINAoQ1O7MW/nRHI2jOcmPwch3bzY2Qzao= Received: from localhost.localdomain ([14.153.77.200]) by newxmesmtplogicsvrszb6-0.qq.com (NewEsmtp) with SMTP id C1994248; Sat, 19 Nov 2022 02:48:25 +0800 X-QQ-mid: xmsmtpt1668797305tcjyj7ceu Message-ID: X-QQ-XMAILINFO: MK5U7QanZrCwlOX7aVi0WIvO8erhefmuYV4ZSAN7Iq/u1j3E3GGtIx0apj1IP9 mFxk/6TdW9Dfw4VCPecOjxZxhsv/3JxfvkHcqjqMeB/sTCOnxIsWfRXrOmyc+wuFC6C3R+T0T0j7 gpATv+qRXqGDNsJ6Imssa0zkHqmaTYrsNzQLM5edc3p6MOmd6gVl4m5B0tr3I3p0R2OeTiKzlriJ vqy3Sj83B2PNVxQJQlEtPl2Pg3K0TTzY4KSXOP0ZTwRPBVDrbhhENqV8768h+77P5vdZ/mPr8r/O yjESTXJp8JhIfimbKGZr62cSVqj5ue7qKp+GBZ5wBgZDa4FzyHxwyoVjWe8LZ5ytiN90TCLzbS57 og4ZlbmJYk/I36B4aSPCYp/rbuEOPTIwrBdFpTa0f2a6y4RdfesBna4qVdogkHqUFMOJMOPlNG// zgsTpw9gJ24Mf5XonM/f4kLxBlLXYCX0mwKEtyotnXNZldmAHWPJM2SL9nWSw4zG2nbbwPSibVjG 0KuJt540Sc5SJl6Qyw9Jq6Qq7HMH/0QgDGNdxLydWFuS+2x8sbhlESSUdNcmLlK9fHG7HGMbguH9 f4z+cm5QuoXFqzSZHK9itotePb7tZwnpAF1yfIXgMItH64r+fZnjuFpioAVzotuo1XGuinwu8F5i 9a48G5gLaD/l9q9hFqVrE+wz4f0PzheT6KU5yCVPHiJlAlUaX3mbG3+01V01ZmhH5fZdFr9xjUfB RyHW6jyLAGNSSXJMwLyOgBabYYnDAOVxe4j8EL9BfEgL6+z83rXd1I9EG0eGbQMfYYcvJGvW2HUS NXrbGefwb6h/xlnDDF4FhcFZaD8IT/WW2ApurPa3w4oWWRnoLS8OtoNXUlE5tVXQZGrks0OJDc79 lUwJGmvBGz2pfs9TIxVsLVwnXkF0bjseA8rwLSbrJtLzazGw7A0za66s6mDDXTozv4ypiAcVbfeS 19Juf0GOY= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Sat, 19 Nov 2022 02:48:02 +0800 X-OQ-MSGID: <20221118184802.100249-1-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/file: add fd protocol 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: Zhao Zhili Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 4Tp3GFnjZcaf From: Zhao Zhili Unlike the pipe protocol, fd protocol has seek support if it corresponding to a regular file. --- Sometimes it's the only way to access files via file descriptor, e.g., requesting a shared file on Android: https://developer.android.com/training/secure-file-sharing/request-file doc/protocols.texi | 24 +++++++++++++++++++ libavformat/Makefile | 1 + libavformat/file.c | 51 +++++++++++++++++++++++++++++++++++++++++ libavformat/protocols.c | 1 + libavformat/version.h | 4 ++-- 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/doc/protocols.texi b/doc/protocols.texi index 5e9198e67c..d9f2d2dec3 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -275,6 +275,30 @@ For example, to convert a GIF file given inline with @command{ffmpeg}: ffmpeg -i "data:image/gif;base64,R0lGODdhCAAIAMIEAAAAAAAA//8AAP//AP///////////////ywAAAAACAAIAAADF0gEDLojDgdGiJdJqUX02iB4E8Q9jUMkADs=" smiley.png @end example +@section fd + +File descriptor access protocol. + +The accepted syntax is: +@example +fd:[@var{number}] +@end example + +@var{number} is the number corresponding to a file descriptor. Unlike the pipe +protocol, fd protocol has seek support if it corresponding to a regular file. +If @var{number} is not specified, by default the stdout file descriptor will +be used for writing, stdin for reading. + +This protocol accepts the following options: + +@table @option +@item blocksize +Set I/O operation maximum block size, in bytes. Default value is +@code{INT_MAX}, which results in not limiting the requested block size. +Setting this value reasonably low improves user termination request reaction +time, which is valuable if data transmission is slow. +@end table + @section file File access protocol. diff --git a/libavformat/Makefile b/libavformat/Makefile index d7f198bf39..1452216e29 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -647,6 +647,7 @@ OBJS-$(CONFIG_DATA_PROTOCOL) += data_uri.o OBJS-$(CONFIG_FFRTMPCRYPT_PROTOCOL) += rtmpcrypt.o rtmpdigest.o rtmpdh.o OBJS-$(CONFIG_FFRTMPHTTP_PROTOCOL) += rtmphttp.o OBJS-$(CONFIG_FILE_PROTOCOL) += file.o +OBJS-$(CONFIG_FD_PROTOCOL) += file.o OBJS-$(CONFIG_FTP_PROTOCOL) += ftp.o urldecode.o OBJS-$(CONFIG_GOPHER_PROTOCOL) += gopher.o OBJS-$(CONFIG_GOPHERS_PROTOCOL) += gopher.o diff --git a/libavformat/file.c b/libavformat/file.c index 6103c37b34..765ad21d67 100644 --- a/libavformat/file.c +++ b/libavformat/file.c @@ -109,6 +109,13 @@ static const AVClass pipe_class = { .version = LIBAVUTIL_VERSION_INT, }; +static const AVClass fd_class = { + .class_name = "fd", + .item_name = av_default_item_name, + .option = pipe_options, + .version = LIBAVUTIL_VERSION_INT, +}; + static int file_read(URLContext *h, unsigned char *buf, int size) { FileContext *c = h->priv_data; @@ -412,3 +419,47 @@ const URLProtocol ff_pipe_protocol = { }; #endif /* CONFIG_PIPE_PROTOCOL */ + +#if CONFIG_FD_PROTOCOL + +static int fd_open(URLContext *h, const char *filename, int flags) +{ + FileContext *c = h->priv_data; + int fd; + char *final; + struct stat st; + + av_strstart(filename, "fd:", &filename); + + fd = strtol(filename, &final, 10); + if((filename == final) || *final ) {/* No digits found, or something like 10ab */ + if (flags & AVIO_FLAG_WRITE) { + fd = 1; + } else { + fd = 0; + } + } +#if HAVE_SETMODE + setmode(fd, O_BINARY); +#endif + if (fstat(fd, &st) < 0) + return AVERROR(errno); + h->is_streamed = !(S_ISREG(st.st_mode) || S_ISBLK(st.st_mode)); + c->fd = fd; + return 0; +} + +const URLProtocol ff_fd_protocol = { + .name = "fd", + .url_open = fd_open, + .url_read = file_read, + .url_write = file_write, + .url_seek = file_seek, + .url_get_file_handle = file_get_handle, + .url_check = file_check, + .priv_data_size = sizeof(FileContext), + .priv_data_class = &fd_class, + .default_whitelist = "crypto,data" +}; + +#endif /* CONFIG_FD_PROTOCOL */ diff --git a/libavformat/protocols.c b/libavformat/protocols.c index 8b7d1b940f..82cb1033e4 100644 --- a/libavformat/protocols.c +++ b/libavformat/protocols.c @@ -29,6 +29,7 @@ extern const URLProtocol ff_concat_protocol; extern const URLProtocol ff_concatf_protocol; extern const URLProtocol ff_crypto_protocol; extern const URLProtocol ff_data_protocol; +extern const URLProtocol ff_fd_protocol; extern const URLProtocol ff_ffrtmpcrypt_protocol; extern const URLProtocol ff_ffrtmphttp_protocol; extern const URLProtocol ff_file_protocol; diff --git a/libavformat/version.h b/libavformat/version.h index 7c9d50b7b3..f5a7f579b3 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,8 +31,8 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 34 -#define LIBAVFORMAT_VERSION_MICRO 101 +#define LIBAVFORMAT_VERSION_MINOR 35 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \