Add a fd protocol

Message ID 20190425092710.30367-1-lu_zero@gentoo.org
State New
Headers show
Series
  • Add a fd protocol
Related show

Commit Message

Luca Barbato April 25, 2019, 9:27 a.m.
---

Sometimes you receive a seekable fd from the outside.

 libavformat/file.c      | 32 ++++++++++++++++++++++++++++++++
 libavformat/protocols.c |  1 +
 2 files changed, 33 insertions(+)

--
2.12.2

Comments

Nicolas George April 25, 2019, 9:31 a.m. | #1
Luca Barbato (12019-04-25):
> ---
> 
> Sometimes you receive a seekable fd from the outside.
> 
>  libavformat/file.c      | 32 ++++++++++++++++++++++++++++++++
>  libavformat/protocols.c |  1 +
>  2 files changed, 33 insertions(+)

It looks completely redundant with pipe:. If it is just a matter of the
name "pipe" suggesting it is not seekable, I do not think it warrants a
completely new protocol.

Also, the doc is missing.

Regards,
Martin Storsjö April 25, 2019, 9:41 a.m. | #2
On Thu, 25 Apr 2019, Luca Barbato wrote:

> ---
>
> Sometimes you receive a seekable fd from the outside.
>
> libavformat/file.c      | 32 ++++++++++++++++++++++++++++++++
> libavformat/protocols.c |  1 +
> 2 files changed, 33 insertions(+)
>
> diff --git a/libavformat/file.c b/libavformat/file.c
> index 27ce4de6eb..6a74ebbf48 100644
> --- a/libavformat/file.c
> +++ b/libavformat/file.c
> @@ -204,3 +204,35 @@ 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;
> +    av_strstart(filename, "fd:", &filename);
> +
> +    fd = strtol(filename, &final, 10);
> +    if ((filename == final) || *final ) {
> +        return AVERROR(EINVAL);
> +    }
> +#if HAVE_SETMODE
> +    setmode(fd, O_BINARY);
> +#endif
> +    c->fd = fd;
> +    return 0;
> +}
> +
> +const URLProtocol ff_pipe_protocol = {

Did you test compilation of this? It doesn't look like it would work given 
this ^

Isn't this essentially exactly the same as the pipe protocol, except for 
not setting the is_streamed flag? Even though the name pipe doesn't feel 
quite right for that case, wouldn't it be possible to just add an option 
to the pipe protocol for controlling this?

// Martin
Luca Barbato April 26, 2019, 7:30 a.m. | #3
On 25/04/2019 11:41, Martin Storsjö wrote:
> Isn't this essentially exactly the same as the pipe protocol, except for 
> not setting the is_streamed flag? Even though the name pipe doesn't feel 
> quite right for that case, wouldn't it be possible to just add an option 
> to the pipe protocol for controlling this?

I can add an option but having the name is more straightforward.


(yes, I missed a -a while amending again :p)

lu

Patch

diff --git a/libavformat/file.c b/libavformat/file.c
index 27ce4de6eb..6a74ebbf48 100644
--- a/libavformat/file.c
+++ b/libavformat/file.c
@@ -204,3 +204,35 @@  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;
+    av_strstart(filename, "fd:", &filename);
+
+    fd = strtol(filename, &final, 10);
+    if ((filename == final) || *final ) {
+        return AVERROR(EINVAL);
+    }
+#if HAVE_SETMODE
+    setmode(fd, O_BINARY);
+#endif
+    c->fd = fd;
+    return 0;
+}
+
+const URLProtocol ff_pipe_protocol = {
+    .name                = "fd",
+    .url_open            = fd_open,
+    .url_read            = file_read,
+    .url_write           = file_write,
+    .url_get_file_handle = file_get_handle,
+    .url_check           = file_check,
+    .priv_data_size      = sizeof(FileContext),
+};
+
+#endif /* CONFIG_FD_PROTOCOL */
diff --git a/libavformat/protocols.c b/libavformat/protocols.c
index 15b9ed736d..b3f8b4af56 100644
--- a/libavformat/protocols.c
+++ b/libavformat/protocols.c
@@ -27,6 +27,7 @@  extern const URLProtocol ff_concat_protocol;
 extern const URLProtocol ff_crypto_protocol;
 extern const URLProtocol ff_ffrtmpcrypt_protocol;
 extern const URLProtocol ff_ffrtmphttp_protocol;
+extern const URLProtocol ff_fd_protocol;
 extern const URLProtocol ff_file_protocol;
 extern const URLProtocol ff_gopher_protocol;
 extern const URLProtocol ff_hls_protocol;