[FFmpeg-devel] [PATCH 4/6] tcp: accept params through avio_open2() options

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Sep 13 02:43:31 CEST 2012


---
 libavformat/tcp.c |   38 ++++++++++++++++++++++++++++++--------
 1 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/libavformat/tcp.c b/libavformat/tcp.c
index d3487b7..0f5d6fe 100644
--- a/libavformat/tcp.c
+++ b/libavformat/tcp.c
@@ -20,6 +20,7 @@
  */
 #include "avformat.h"
 #include "libavutil/parseutils.h"
+#include "libavutil/opt.h"
 #include "internal.h"
 #include "network.h"
 #include "os_support.h"
@@ -29,21 +30,40 @@
 #endif
 
 typedef struct TCPContext {
+    const AVClass *class;
     int fd;
+    int listen;
+    int rw_timeout;
+    int listen_timeout;
 } TCPContext;
 
+#define OFFSET(x) offsetof(TCPContext, x)
+#define D AV_OPT_FLAG_DECODING_PARAM
+#define E AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+{"listen", "listen on port instead of connecting", OFFSET(listen), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
+{"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
+{"listen_timeout", "connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
+{NULL}
+};
+
+static const AVClass tcp_context_class = {
+    .class_name = "tcp",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 /* return non zero if error */
 static int tcp_open(URLContext *h, const char *uri, int flags)
 {
     struct addrinfo hints = { 0 }, *ai, *cur_ai;
     int port, fd = -1;
     TCPContext *s = h->priv_data;
-    int listen_socket = 0;
     const char *p;
     char buf[256];
     int ret;
     socklen_t optlen;
-    int listen_timeout = -1;
     char hostname[1024],proto[1024],path[1024];
     char portstr[10];
     h->rw_timeout = 5000000;
@@ -59,18 +79,19 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
     p = strchr(uri, '?');
     if (p) {
         if (av_find_info_tag(buf, sizeof(buf), "listen", p))
-            listen_socket = 1;
+            s->listen = 1;
         if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
-            h->rw_timeout = strtol(buf, NULL, 10);
+            s->rw_timeout = strtol(buf, NULL, 10);
         }
         if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
-            listen_timeout = strtol(buf, NULL, 10);
+            s->listen_timeout = strtol(buf, NULL, 10);
         }
     }
+    h->rw_timeout = s->rw_timeout;
     hints.ai_family = AF_UNSPEC;
     hints.ai_socktype = SOCK_STREAM;
     snprintf(portstr, sizeof(portstr), "%d", port);
-    if (listen_socket)
+    if (s->listen)
         hints.ai_flags |= AI_PASSIVE;
     if (!hostname[0])
         ret = getaddrinfo(NULL, portstr, &hints, &ai);
@@ -91,7 +112,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
     if (fd < 0)
         goto fail;
 
-    if (listen_socket) {
+    if (s->listen) {
         int fd1;
         int reuse = 1;
         struct pollfd lp = { fd, POLLIN, 0 };
@@ -106,7 +127,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
             ret = ff_neterrno();
             goto fail1;
         }
-        ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
+        ret = poll(&lp, 1, s->listen_timeout >= 0 ? s->listen_timeout : -1);
         if (ret <= 0) {
             ret = AVERROR(ETIMEDOUT);
             goto fail1;
@@ -273,5 +294,6 @@ URLProtocol ff_tcp_protocol = {
     .url_get_file_handle = tcp_get_file_handle,
     .url_shutdown        = tcp_shutdown,
     .priv_data_size      = sizeof(TCPContext),
+    .priv_data_class     = &tcp_context_class,
     .flags               = URL_PROTOCOL_FLAG_NETWORK,
 };
-- 
1.7.8.6



More information about the ffmpeg-devel mailing list