From patchwork Wed Dec 14 17:10:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?emhpbGl6aGFvKOi1teW/l+eriyk=?= X-Patchwork-Id: 39705 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp496725pzb; Wed, 14 Dec 2022 09:11:31 -0800 (PST) X-Google-Smtp-Source: AA0mqf777GLuYtJBhJoPrcGKMLgK5TMpbS7m9zvKmXJZy4O/yedselrlgTCxjTVzl+gO1NcYnzDS X-Received: by 2002:a17:906:a845:b0:7c0:faca:52c with SMTP id dx5-20020a170906a84500b007c0faca052cmr20644026ejb.37.1671037891525; Wed, 14 Dec 2022 09:11:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671037891; cv=none; d=google.com; s=arc-20160816; b=cjyDXzi9RYAXgYP8qZXtduLWN+Hz0Al+9euy4nPpxLFUxy5F047HbcamhXRQgLUyIq 3qo6J6n5e7SIx8m6TEZzIq3YvRerGUpyUMznzqtixx7o7FsH5TfM4xnMoJs4V3sI6f60 r5LQWI2u4YCjbFi/PHx3vOoQvPMC0sY2yt/y4hwJyq6noigN9ATINdKtP/f+FuV1ujQ6 A39QiJz4NtQoQqskM6P7kuN95rpu12H8VtLN/9eMwB+D0IRsWfJVhr/xNP5KcOdaI2RQ yg9O0M19QthR15oh8zzBLCtjaX30uWvPe6hcB3h8TsMx4yQg0j/JZVmEetlhzUx/ekSn MB6A== 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:references:in-reply-to:date :to:from:message-id:dkim-signature:delivered-to; bh=4AMsgCGyw2iZBUIK+xi/wkZhgnuuGqvdDeK9ZA86ZHk=; b=RVDyTtRHPrKWfOzXOo/Mzz6MfJavz05da87tP6q3NeKIoiMrw9gyEhzsOBzDAFLAcv UjMs46PtJw+yOoyJ03oPGgs70BwTvijR0vGZTrjodgV1X+ENe185TH78H1XUbewZrdNj YIeDN49clzauQtDjIotCsi0MtSdywDR8p/P1uHV6Yqcq7cVt7cTvrts9A4attNRBNai+ gMrRPUP/hYDlgOvJaJCCWxOVLA8jnbIdevA3tyeKhSAttGu+jy86Us7OEbk5508JCzCF VO6sbo8EVBO1emoTotVer24XA9YnriDX4FcRzFkRtPY7c/dSykDgg5MDzSsJfupX+rXv ViNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@foxmail.com header.s=s201512 header.b=Ezm1iT9Z; 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 nb6-20020a1709071c8600b0073306ff26besi13956403ejc.969.2022.12.14.09.11.31; Wed, 14 Dec 2022 09:11:31 -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=Ezm1iT9Z; 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 A8FA868BCA6; Wed, 14 Dec 2022 19:10:53 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out162-62-58-216.mail.qq.com (out162-62-58-216.mail.qq.com [162.62.58.216]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5AB8268BA97 for ; Wed, 14 Dec 2022 19:10:43 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1671037840; bh=qZJJZuDHcyyEtJotoUc/lag/+FCBwIZ0L8bEl0EPIR8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Ezm1iT9ZOzSc1KSnWUjX9SFFLM4EhRE/u1MfEC+Hzy9xO3CXvumigpIkmPdubUKOp VoPUbis37xmdFKwW0SgpXFvUpMc8BVS1cSpplAPqdSrLuma4wPfr75EQRXK7PWPdVF lf6a+TeqNGp1LUqk7mjbPGwe/AmUjOm0TdIvHPUg= Received: from localhost.localdomain ([113.116.29.217]) by newxmesmtplogicsvrsza12-0.qq.com (NewEsmtp) with SMTP id 2A515E8A; Thu, 15 Dec 2022 01:10:37 +0800 X-QQ-mid: xmsmtpt1671037840tp55hdprh Message-ID: X-QQ-XMAILINFO: NsLYjltVP0el9JlY0ukGG8HYbKVgYeTrQMZIkVvgTsDrYHqQywWbcksrJUOXll uz5vJXy+aA8NGOkVOOJQCr1b4taEtYC4BXPitDyjFqcbyOHme7CG0RB9PmKgMGiozOOZZp5aUei9 GMgGPWBC10SDk9KOQ2zETDjXaqAhzXPdI/mODPu4V5n6oyDKZF0AfADEYCslOvkwkp996HdHlpJm ZgL/kVNgia6ItjLp4R82uQnWlSA4miNPlY5whxRZjK2rWWoOHi+dZVE1c/9ZvDx1fwDQxGa82bHE 9zWw+EcFjtd5ab8mF7XRGnZIG0qIlVm8RSbc6YlKPCuevN4duaHdRWU1zIa5GitBrNHbrZdHerOH InGbljbEaN7XE4g4nMZnlKKwXyRtWHqS6hoiHK+b2kbyOtfQsc8H08l8KA+DXHUhg2mrRyDeAz9L fmD6tM/KaPTKmUhli5fP7riHIhWGTiLwBbmtnIdaCblK+x9lS8X/6/ZUAkO9EqCgR/KUneauntoG l7E8L9v0VrMVu+5IaUwHx9matcQNH0othZAfvf9bNgOqkFuDxZOKIAGxsWlCfrRiI9Dk1mnAydOr wdeVWvctIsZV46gbzcBlThJQaJRvNB3eE+9AFJxSj1+w0+2u22TXSFPJClhopgpNNhYAKToVqX+q Nbv1P9xeAHqmCSEbEsONA6ZFXJE2Hkn1whTH1+fj0s4f1ZXLUgB7kddkP7urYbf55kxvwLD3KsRF xn3s23P04Id3ST7lpav4qPoTRbbIkV7INbRDLirXchtGVE1dzrsP+L7oRop8/hM+4SvY6VfZGHIx RrM+gc3EK2Xyd2Yu5EKTbEaGGn9XrGq1GAT6Z4xSehJBMFpFcCu2mQ8Sxf5ug2dEAnSrsZ4lcPF0 G8Eduit1xTAKwbXujYDGkkwGehTxML3OOCfRR3enISrVu4i40tz0ciot6fJu/ivrdzUXqrnb0+dC c0li72/DQTi+hAbYs9osKkhMfSFLJbqtvM6QJixq9Wc2MOJ+WaGQolXmv2o7++ From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Thu, 15 Dec 2022 01:10:10 +0800 X-OQ-MSGID: <20221214171014.1058709-5-quinkblack@foxmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214171014.1058709-1-quinkblack@foxmail.com> References: <20221214171014.1058709-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 4/8] 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: KTUEuSN/IUPK From: Zhao Zhili Unlike the pipe protocol, fd protocol has seek support if it corresponding to a regular file. --- doc/protocols.texi | 27 +++++++++++++ libavformat/Makefile | 1 + libavformat/file.c | 87 +++++++++++++++++++++++++++++++++-------- libavformat/protocols.c | 1 + libavformat/version.h | 4 +- 5 files changed, 101 insertions(+), 19 deletions(-) diff --git a/doc/protocols.texi b/doc/protocols.texi index a686a7f0ac..21ae6181a0 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -275,6 +275,33 @@ 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: -fd @var{file_descriptor} +@end example + +If @option{fd} is not specified, by default the stdout file descriptor will be +used for writing, stdin for reading. Unlike the pipe protocol, fd protocol has +seek support if it corresponding to a regular file. fd protocol doesn't support +pass file descriptor via URL for security. + +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. + +@item fd +Set file descriptor. +@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 878983a336..d20208e31f 100644 --- a/libavformat/file.c +++ b/libavformat/file.c @@ -110,6 +110,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; @@ -197,6 +204,23 @@ static int file_close(URLContext *h) return (ret == -1) ? AVERROR(errno) : 0; } +/* XXX: use llseek */ +static int64_t file_seek(URLContext *h, int64_t pos, int whence) +{ + FileContext *c = h->priv_data; + int64_t ret; + + if (whence == AVSEEK_SIZE) { + struct stat st; + ret = fstat(c->fd, &st); + return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size); + } + + ret = lseek(c->fd, pos, whence); + + return ret < 0 ? AVERROR(errno) : ret; +} + #if CONFIG_FILE_PROTOCOL static int file_delete(URLContext *h) @@ -276,23 +300,6 @@ static int file_open(URLContext *h, const char *filename, int flags) return 0; } -/* XXX: use llseek */ -static int64_t file_seek(URLContext *h, int64_t pos, int whence) -{ - FileContext *c = h->priv_data; - int64_t ret; - - if (whence == AVSEEK_SIZE) { - struct stat st; - ret = fstat(c->fd, &st); - return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size); - } - - ret = lseek(c->fd, pos, whence); - - return ret < 0 ? AVERROR(errno) : ret; -} - static int file_open_dir(URLContext *h) { #if HAVE_LSTAT @@ -441,3 +448,49 @@ 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; + struct stat st; + + if (strcmp(filename, "fd:") != 0) { + av_log(h, AV_LOG_ERROR, "Doesn't support pass file descriptor via URL," + " please set it via -fd {num}\n"); + return AVERROR(EINVAL); + } + + if (c->fd < 0) { + if (flags & AVIO_FLAG_WRITE) { + c->fd = 1; + } else { + c->fd = 0; + } + } + if (fstat(c->fd, &st) < 0) + return AVERROR(errno); + h->is_streamed = !(S_ISREG(st.st_mode) || S_ISBLK(st.st_mode)); + c->fd = fd_dup(h, c->fd); + if (c->fd == -1) + return AVERROR(errno); + + 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_close = file_close, + .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 63e753a3aa..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 103 +#define LIBAVFORMAT_VERSION_MINOR 35 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \