[PATCHv2] Add a fd protocol

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

Commit Message

Luca Barbato April 25, 2019, 12:28 p.m.
---

I failed at amending again :P

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

--
2.12.2

Patch

diff --git a/libavformat/file.c b/libavformat/file.c
index 27ce4de6eb..c5b41b125c 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_fd_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;