diff mbox series

[FFmpeg-devel,v2,1/3] avformat/file: add fd protocol

Message ID tencent_123E43576B9DEAE0FA5AC43475804A35E909@qq.com
State New
Headers show
Series [FFmpeg-devel,v2,1/3] avformat/file: add fd protocol | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished

Commit Message

Zhao Zhili Dec. 11, 2022, 3:17 p.m. UTC
From: Zhao Zhili <zhilizhao@tencent.com>

Unlike the pipe protocol, fd protocol has seek support if it
corresponding to a regular file.
---
v2: dup the file descriptor for safety

 doc/protocols.texi      | 24 ++++++++++++++++++
 libavformat/Makefile    |  1 +
 libavformat/file.c      | 55 +++++++++++++++++++++++++++++++++++++++++
 libavformat/protocols.c |  1 +
 libavformat/version.h   |  4 +--
 5 files changed, 83 insertions(+), 2 deletions(-)

Comments

Rémi Denis-Courmont Dec. 12, 2022, 4:35 p.m. UTC | #1
Le sunnuntaina 11. joulukuuta 2022, 17.17.27 EET Zhao Zhili a écrit :
> From: Zhao Zhili <zhilizhao@tencent.com>
> 
> Unlike the pipe protocol, fd protocol has seek support if it
> corresponding to a regular file.
> ---
> v2: dup the file descriptor for safety
> 
>  doc/protocols.texi      | 24 ++++++++++++++++++
>  libavformat/Makefile    |  1 +
>  libavformat/file.c      | 55 +++++++++++++++++++++++++++++++++++++++++
>  libavformat/protocols.c |  1 +
>  libavformat/version.h   |  4 +--
>  5 files changed, 83 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///////////////ywA
> AAAACAAIAAADF0gEDLojDgdGiJdJqUX02iB4E8Q9jUMkADs=" smiley.png @end example
> 
> +@section fd
> +
> +File descriptor access protocol.
> +
> +The accepted syntax is:
> +@example
> +fd:[@var{number}]
> +@end example

C.f. what I wrote in the previous version thread.
Rémi Denis-Courmont Dec. 13, 2022, 4:28 p.m. UTC | #2
Le sunnuntaina 11. joulukuuta 2022, 17.17.27 EET Zhao Zhili a écrit :
> From: Zhao Zhili <zhilizhao@tencent.com>
> 
> Unlike the pipe protocol, fd protocol has seek support if it
> corresponding to a regular file.
> ---
> v2: dup the file descriptor for safety
> 
>  doc/protocols.texi      | 24 ++++++++++++++++++
>  libavformat/Makefile    |  1 +
>  libavformat/file.c      | 55 +++++++++++++++++++++++++++++++++++++++++
>  libavformat/protocols.c |  1 +
>  libavformat/version.h   |  4 +--
>  5 files changed, 83 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///////////////ywA
> AAAACAAIAAADF0gEDLojDgdGiJdJqUX02iB4E8Q9jUMkADs=" 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..b8725c1f48 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,51 @@ 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 ) {
> +        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 = dup(fd);

This leaks c->fd into child processes. You need atomic close-on-exec.
diff mbox series

Patch

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..b8725c1f48 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,51 @@  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 ) {
+        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 = dup(fd);
+    if (c->fd < 0)
+        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 a7e5a9ac66..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 102
+#define LIBAVFORMAT_VERSION_MINOR  35
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \