On Thu, 24 Nov 2011, Luca Barbato wrote:
Please do not use it yet for anything serious, error handling and socket
tracking is missing
---
libavformat/Makefile | 1 +
libavformat/allformats.c | 1 +
libavformat/sctp.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 182 insertions(+), 0 deletions(-)
create mode 100644 libavformat/sctp.c
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 0c7d258..65167cf 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -337,6 +337,7 @@ OBJS-$(CONFIG_RTMP_PROTOCOL) += $(RTMP-OBJS-yes)
OBJS-$(CONFIG_RTP_PROTOCOL) += rtpproto.o
OBJS-$(CONFIG_TCP_PROTOCOL) += tcp.o
OBJS-$(CONFIG_TLS_PROTOCOL) += tls.o
+OBJS-$(CONFIG_UDP_PROTOCOL) += sctp.o
OBJS-$(CONFIG_UDP_PROTOCOL) += udp.o
CONFIG_SCTP_PROTOCOL
EXAMPLES = metadata output
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 00924c8..1ae0ae4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -258,4 +258,5 @@ void av_register_all(void)
REGISTER_PROTOCOL (TCP, tcp);
REGISTER_PROTOCOL (TLS, tls);
REGISTER_PROTOCOL (UDP, udp);
+ REGISTER_PROTOCOL (SCTP, sctp);
}
diff --git a/libavformat/sctp.c b/libavformat/sctp.c
new file mode 100644
index 0000000..57d7eaf
--- /dev/null
+++ b/libavformat/sctp.c
@@ -0,0 +1,180 @@
+/*
+ * 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"
+#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 <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];
Some spaces between these would be nice
+ char portstr[10];
+
+ av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
+ &port, path, sizeof(path), uri);
Perhaps align this second line with proto on the previous line
+ 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;
Missing setting ret to some error code
+ 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
log error message, ret = AVERROR(EIO), goto fail?
+ }
+
+ s = av_malloc(sizeof(SCTPContext));
+
+ if (!s) {
+ freeaddrinfo(ai);
+ return AVERROR(ENOMEM);
+ }
This manual malloc could be avoided by setting priv_data_size I think
(same for the unix proto). I guess the other old protos could be updated
to use this, too.
+ 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);
+}
These log messages should be verbose/debug or removed before the code is
to be committed for real
+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 = {
+ "sctp",
+ sctp_open,
+ sctp_read,
+ sctp_write,
+ NULL, /* seek */
+ sctp_close,
+ .url_get_file_handle = sctp_get_file_handle,
+};
--
1.7.6
Please use designated initializers.
Also, use AVIO_FLAG_NONBLOCK instead of URL_FLAG_NONBLOCK and use the
URLContext as logging context (this is the same symptoms as my tls code -
old patchset from before those changes)
Also missing dependencies in configure, like for the unix proto.
// Martin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel