On Tue, 29 Nov 2011, Luca Barbato wrote:

It has the same behaviour of the tcp protocol.
---
Changelog                |    2 +-
configure                |    3 +
libavformat/Makefile     |    1 +
libavformat/allformats.c |    1 +
libavformat/unix.c       |  144 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 150 insertions(+), 1 deletions(-)
create mode 100644 libavformat/unix.c

diff --git a/Changelog b/Changelog
index e5c4993..e06bbd7 100644
--- a/Changelog
+++ b/Changelog
@@ -103,7 +103,7 @@ easier to use. The changes are:
- VBLE Decoder
- OS X Video Decoder Acceleration (VDA) support
- CRI ADX audio format demuxer
-
+- Unix socket protocol

This removes one of the empty lines


version 0.7:

diff --git a/configure b/configure
index a0dc6e0..a0c7f59 100755
--- a/configure
+++ b/configure
@@ -1125,6 +1125,7 @@ HAVE_LIST="
    struct_sockaddr_in6
    struct_sockaddr_sa_len
    struct_sockaddr_storage
+    struct_sockaddr_un
    symver
    symver_gnu_asm
    symver_asm_label
@@ -1494,6 +1495,7 @@ tcp_protocol_deps="network"
tls_protocol_deps_any="openssl gnutls"
tls_protocol_select="tcp_protocol"
udp_protocol_deps="network"
+unix_protocol_deps="network struct_sockaddr_un"

# filters
blackframe_filter_deps="gpl"
@@ -2793,6 +2795,7 @@ if enabled network; then
    check_type netinet/in.h "struct ipv6_mreq" -D_DARWIN_C_SOURCE
    check_type netinet/in.h "struct sockaddr_in6"
    check_type "sys/types.h sys/socket.h" "struct sockaddr_storage"
+    check_type "sys/un.h" "struct sockaddr_un"
    check_struct "sys/types.h sys/socket.h" "struct sockaddr" sa_len
    # Prefer arpa/inet.h over winsock2
    if check_header arpa/inet.h ; then
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 64b2d82..d1081ed 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -340,6 +340,7 @@ OBJS-$(CONFIG_RTP_PROTOCOL)              += rtpproto.o
OBJS-$(CONFIG_TCP_PROTOCOL)              += tcp.o
OBJS-$(CONFIG_TLS_PROTOCOL)              += tls.o
OBJS-$(CONFIG_UDP_PROTOCOL)              += udp.o
+OBJS-$(CONFIG_UNIX_PROTOCOL)             += unix.o

EXAMPLES  = metadata output
TESTPROGS = seek
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 573b714..4723d20 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -260,4 +260,5 @@ void av_register_all(void)
    REGISTER_PROTOCOL (TCP, tcp);
    REGISTER_PROTOCOL (TLS, tls);
    REGISTER_PROTOCOL (UDP, udp);
+    REGISTER_PROTOCOL (UNIX, unix);
}
diff --git a/libavformat/unix.c b/libavformat/unix.c
new file mode 100644
index 0000000..0f5c757
--- /dev/null
+++ b/libavformat/unix.c
@@ -0,0 +1,144 @@
+/*
+ * 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 "avformat.h"
+#if HAVE_POLL_H
+#include <poll.h>
+#endif
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/un.h>
+
+#include "libavutil/avstring.h"
+#include "internal.h"
+#include "network.h"
+#include "os_support.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);

Same comment as in the previous iteration:

Hmm, is this common practice for unix socket addresses? Shouldn't a plain sizeof(s->addr) work too, since the string is contained within that anyway?

I'm not saying it's wrong, I just want a comment on it.


+
+    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);
+    }

These error returns could perhaps still be combined into a goto fail path for simplification. And if the priv_data is allocated by the caller (by setting priv_data_size), one wouldn't have to take care of freeing/allocating that here either. But this is just extra, the code is correct and ok this way of course.

+
+    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);

Same as for the other unix socket thing, I'd like to know why this is done (please explain the normal usage pattern).

+    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 = {
+    .name                = "unix",
+    .url_open            = unix_open,
+    .url_read            = unix_read,
+    .url_write           = unix_write,
+    .url_close           = unix_close,
+    .url_get_file_handle = unix_get_file_handle,
+};
--
1.7.8.rc1

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

Reply via email to