It does not expose the advanced sctp features yet.
---
 Changelog                |    1 +
 configure                |    3 +
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    1 +
 libavformat/sctp.c       |  181 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 187 insertions(+), 0 deletions(-)
 create mode 100644 libavformat/sctp.c

diff --git a/Changelog b/Changelog
index e06bbd7..cbc9398 100644
--- a/Changelog
+++ b/Changelog
@@ -104,6 +104,7 @@ easier to use. The changes are:
 - OS X Video Decoder Acceleration (VDA) support
 - CRI ADX audio format demuxer
 - Unix socket protocol
+- SCTP protocol
 
 version 0.7:
 
diff --git a/configure b/configure
index a0c7f59..916f5bd 100755
--- a/configure
+++ b/configure
@@ -1121,6 +1121,7 @@ HAVE_LIST="
     strptime
     strtok_r
     struct_addrinfo
+    struct_sctp_event_subscribe
     struct_ipv6_mreq
     struct_sockaddr_in6
     struct_sockaddr_sa_len
@@ -1491,6 +1492,7 @@ mmsh_protocol_select="http_protocol"
 mmst_protocol_deps="network"
 rtmp_protocol_select="tcp_protocol"
 rtp_protocol_select="udp_protocol"
+sctp_protocol_deps="network struct_sctp_event_subscribe"
 tcp_protocol_deps="network"
 tls_protocol_deps_any="openssl gnutls"
 tls_protocol_select="tcp_protocol"
@@ -2796,6 +2798,7 @@ if enabled network; then
     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_type "netinet/sctp.h" "struct sctp_event_subscribe"
     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 d1081ed..cc537f6 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -337,6 +337,7 @@ RTMP-OBJS-$(!CONFIG_LIBRTMP)              = rtmpproto.o 
rtmppkt.o
 OBJS-$(CONFIG_RTMP_PROTOCOL)             += $(RTMP-OBJS-yes)
 
 OBJS-$(CONFIG_RTP_PROTOCOL)              += rtpproto.o
+OBJS-$(CONFIG_SCTP_PROTOCOL)             += sctp.o
 OBJS-$(CONFIG_TCP_PROTOCOL)              += tcp.o
 OBJS-$(CONFIG_TLS_PROTOCOL)              += tls.o
 OBJS-$(CONFIG_UDP_PROTOCOL)              += udp.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 4723d20..21843f0 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -257,6 +257,7 @@ 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);
diff --git a/libavformat/sctp.c b/libavformat/sctp.c
new file mode 100644
index 0000000..e4073b9
--- /dev/null
+++ b/libavformat/sctp.c
@@ -0,0 +1,181 @@
+/*
+ * SCTP 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 "internal.h"
+#include "network.h"
+#include "os_support.h"
+
+#include <netinet/sctp.h>
+
+typedef struct SCTPContext {
+    int fd;
+} SCTPContext;
+
+/* return non zero if error */
+static int sctp_open(URLContext *h, const char *uri, int flags)
+{
+    struct addrinfo hints, *ai, *cur_ai;
+    struct sctp_event_subscribe event;
+    int port, fd = -1;
+    SCTPContext *s = NULL;
+    int ret;
+    socklen_t optlen;
+    char hostname[1024],proto[1024],path[1024];
+    char portstr[10];
+
+    av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
+        &port, path, sizeof(path), uri);
+    if (strcmp(proto,"sctp") || port <= 0 || port >= 65536)
+        return AVERROR(EINVAL);
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_UNSPEC;
+    hints.ai_socktype = SOCK_STREAM;
+    snprintf(portstr, sizeof(portstr), "%d", port);
+    ret = getaddrinfo(hostname, portstr, &hints, &ai);
+    if (ret) {
+        av_log(NULL, AV_LOG_ERROR,
+               "Failed to resolve hostname %s: %s\n",
+               hostname, gai_strerror(ret));
+        return AVERROR(EIO);
+    }
+
+    cur_ai = ai;
+
+    fd = socket(cur_ai->ai_family, SOCK_STREAM, IPPROTO_SCTP);
+    if (fd < 0)
+        goto fail;
+    ff_socket_nonblock(fd, 1);
+
+    if(flags & URL_WRONLY) {
+        ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
+    } else {
+        ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
+        listen(fd, 100);
+        fd = accept(fd, NULL, NULL);
+    }
+
+    event.sctp_data_io_event = 1;
+    event.sctp_association_event = 1;
+    event.sctp_address_event = 1;
+    event.sctp_send_failure_event = 1;
+    event.sctp_peer_error_event = 1;
+    event.sctp_shutdown_event = 1;
+    event.sctp_partial_delivery_event = 1;
+    event.sctp_adaptation_layer_event = 1;
+
+    if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
+                   sizeof(event)) != 0) {
+        //XXX
+    }
+
+    s = av_malloc(sizeof(SCTPContext));
+
+    if (!s) {
+        freeaddrinfo(ai);
+        return AVERROR(ENOMEM);
+    }
+
+    h->priv_data = s;
+    h->is_streamed = 1;
+    s->fd = fd;
+    freeaddrinfo(ai);
+    return 0;
+
+fail:
+    ret = AVERROR(EIO);
+    freeaddrinfo(ai);
+    return ret;
+}
+
+static int sctp_wait_fd(int fd, int write)
+{
+    int ev = write ? POLLOUT : POLLIN;
+    struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
+    int ret;
+
+    av_log(NULL, AV_LOG_INFO, "POLL\n");
+    ret = poll(&p, 1, 100);
+    av_log(NULL, AV_LOG_INFO, "POLL res %d\n", ret);
+    return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
+}
+
+static int sctp_read(URLContext *h, uint8_t *buf, int size)
+{
+    SCTPContext *s = h->priv_data;
+    int ret;
+
+    if (!(h->flags & URL_FLAG_NONBLOCK)) {
+        ret = sctp_wait_fd(s->fd, 0);
+        if (ret < 0)
+            return ret;
+    }
+
+    av_log(NULL, AV_LOG_INFO, "READ\n");
+    ret = recv(s->fd, buf, size, 0);
+    av_log(NULL, AV_LOG_INFO, "READ res %d\n", ret);
+    return ret < 0 ? ff_neterrno() : ret;
+}
+
+static int sctp_write(URLContext *h, const uint8_t *buf, int size)
+{
+    SCTPContext *s = h->priv_data;
+    int ret;
+
+    if (!(h->flags & URL_FLAG_NONBLOCK)) {
+        ret = sctp_wait_fd(s->fd, 1);
+        if (ret < 0)
+            return ret;
+    }
+    av_log(NULL, AV_LOG_INFO, "WRITE\n");
+    ret = send(s->fd, buf, size, 0);
+    av_log(NULL, AV_LOG_INFO, "WRITE res %d\n", ret);
+    return ret < 0 ? ff_neterrno() : ret;
+}
+
+static int sctp_close(URLContext *h)
+{
+    SCTPContext *s = h->priv_data;
+    closesocket(s->fd);
+    av_free(s);
+    return 0;
+}
+
+static int sctp_get_file_handle(URLContext *h)
+{
+    SCTPContext *s = h->priv_data;
+    return s->fd;
+}
+
+URLProtocol ff_sctp_protocol = {
+    .name                = "sctp",
+    .url_open            = sctp_open,
+    .url_read            = sctp_read,
+    .url_write           = sctp_write,
+    .url_close           = sctp_close,
+    .url_get_file_handle = sctp_get_file_handle,
+};
-- 
1.7.8.rc1

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

Reply via email to