It has the same behaviour of the tcp protocol.
---
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    3 +-
 libavformat/unix.c       |  146 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+), 1 deletions(-)
 create mode 100644 libavformat/unix.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 65167cf..9969eed 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -339,6 +339,7 @@ OBJS-$(CONFIG_TCP_PROTOCOL)              += tcp.o
 OBJS-$(CONFIG_TLS_PROTOCOL)              += tls.o
 OBJS-$(CONFIG_UDP_PROTOCOL)              += sctp.o
 OBJS-$(CONFIG_UDP_PROTOCOL)              += udp.o
+OBJS-$(CONFIG_UDP_PROTOCOL)              += unix.o
 
 EXAMPLES  = metadata output
 TESTPROGS = seek
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 1ae0ae4..a078c7a 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -255,8 +255,9 @@ void av_register_all(void)
     REGISTER_PROTOCOL (RTMP, rtmps);
 #endif
     REGISTER_PROTOCOL (RTP, rtp);
+    REGISTER_PROTOCOL (SCTP, sctp);
     REGISTER_PROTOCOL (TCP, tcp);
     REGISTER_PROTOCOL (TLS, tls);
     REGISTER_PROTOCOL (UDP, udp);
-    REGISTER_PROTOCOL (SCTP, sctp);
+    REGISTER_PROTOCOL (UNIX, unix);
 }
diff --git a/libavformat/unix.c b/libavformat/unix.c
new file mode 100644
index 0000000..2f4ab02
--- /dev/null
+++ b/libavformat/unix.c
@@ -0,0 +1,146 @@
+/*
+ * Unix protocol
+ * Copyright (c) 2011 Luca Barbato
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avstring.h"
+#include "avformat.h"
+#include <unistd.h>
+#include "internal.h"
+#include "network.h"
+#include "os_support.h"
+#if HAVE_POLL_H
+#include <poll.h>
+#endif
+#include <sys/time.h>
+
+#include <sys/un.h>
+
+typedef struct UnixContext {
+    int fd;
+    struct sockaddr_un addr;
+    int addr_len;
+} UnixContext;
+
+static int unix_open(URLContext *h, const char *filename, int flags)
+{
+    int fd = -1;
+    UnixContext *s = NULL;
+    int ret = 1;
+
+    s = av_malloc(sizeof(UnixContext));
+    if (!s) {
+        return AVERROR(ENOMEM);
+    }
+
+    av_strstart(filename, "unix:", &filename);
+    s->addr.sun_family = AF_UNIX;
+    av_strlcpy(s->addr.sun_path, filename, sizeof(s->addr.sun_path));
+    s->addr_len = sizeof(s->addr.sun_family) + strlen(s->addr.sun_path);
+
+    fd = socket(AF_UNIX, SOCK_DGRAM, 0);
+
+    if (fd < 0) {
+        av_free(s);
+        return AVERROR(ENOMEM);
+    }
+
+    if (flags & URL_WRONLY)
+        ret = connect(fd, (struct sockaddr *)&s->addr, s->addr_len);
+    else
+        ret = bind(fd, (struct sockaddr *)&s->addr, s->addr_len);
+
+    if (ret < 0) {
+        av_free(s);
+        closesocket(fd);
+        return AVERROR(EIO);
+    }
+
+    ff_socket_nonblock(fd, 1);
+
+    h->priv_data = s;
+    h->is_streamed = 1;
+    s->fd = fd;
+
+    return 0;
+}
+
+static int unix_wait_fd(int fd, int write)
+{
+    int ev = write ? POLLOUT : POLLIN;
+    struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
+    int ret;
+
+    ret = poll(&p, 1, 100);
+    return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
+}
+
+static int unix_read(URLContext *h, uint8_t *buf, int size)
+{
+    UnixContext *s = h->priv_data;
+    int ret;
+
+    if (!(h->flags & URL_FLAG_NONBLOCK)) {
+        ret = unix_wait_fd(s->fd, 0);
+        if (ret < 0)
+            return ret;
+    }
+    ret = recv(s->fd, buf, size, 0);
+    return ret < 0 ? ff_neterrno() : ret;
+}
+
+static int unix_write(URLContext *h, const uint8_t *buf, int size)
+{
+    UnixContext *s = h->priv_data;
+    int ret;
+
+    if (!(h->flags & URL_FLAG_NONBLOCK)) {
+        ret = unix_wait_fd(s->fd, 1);
+        if (ret < 0)
+            return ret;
+    }
+    ret = send(s->fd, buf, size, 0);
+    return ret < 0 ? ff_neterrno() : ret;
+}
+
+static int unix_close(URLContext *h)
+{
+    UnixContext *s = h->priv_data;
+    closesocket(s->fd);
+    if (h->flags & URL_RDONLY)
+        unlink(s->addr.sun_path);
+    av_free(s);
+    return 0;
+}
+
+static int unix_get_file_handle(URLContext *h)
+{
+    UnixContext *s = h->priv_data;
+    return s->fd;
+}
+
+URLProtocol ff_unix_protocol = {
+    "unix",
+    unix_open,
+    unix_read,
+    unix_write,
+    NULL, /* seek */
+    unix_close,
+    .url_get_file_handle = unix_get_file_handle,
+};
-- 
1.7.6

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to