summaryrefslogtreecommitdiff
path: root/ffmpeg/libavformat/tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ffmpeg/libavformat/tcp.c')
-rw-r--r--ffmpeg/libavformat/tcp.c111
1 files changed, 31 insertions, 80 deletions
diff --git a/ffmpeg/libavformat/tcp.c b/ffmpeg/libavformat/tcp.c
index 0d792e7..e457fba 100644
--- a/ffmpeg/libavformat/tcp.c
+++ b/ffmpeg/libavformat/tcp.c
@@ -34,6 +34,7 @@ typedef struct TCPContext {
const AVClass *class;
int fd;
int listen;
+ int open_timeout;
int rw_timeout;
int listen_timeout;
} TCPContext;
@@ -43,8 +44,8 @@ typedef struct TCPContext {
#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 },
+{"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
+{"listen_timeout", "set connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
{NULL}
};
@@ -64,10 +65,9 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
const char *p;
char buf[256];
int ret;
- socklen_t optlen;
char hostname[1024],proto[1024],path[1024];
char portstr[10];
- h->rw_timeout = 5000000;
+ s->open_timeout = 5000000;
av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
&port, path, sizeof(path), uri);
@@ -79,8 +79,13 @@ 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))
- s->listen = 1;
+ if (av_find_info_tag(buf, sizeof(buf), "listen", p)) {
+ char *endptr = NULL;
+ s->listen = strtol(buf, &endptr, 10);
+ /* assume if no digits were found it is a request to enable it */
+ if (buf == endptr)
+ s->listen = 1;
+ }
if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
s->rw_timeout = strtol(buf, NULL, 10);
}
@@ -88,7 +93,10 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
s->listen_timeout = strtol(buf, NULL, 10);
}
}
- h->rw_timeout = s->rw_timeout;
+ if (s->rw_timeout >= 0) {
+ s->open_timeout =
+ h->rw_timeout = s->rw_timeout;
+ }
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, sizeof(portstr), "%d", port);
@@ -108,89 +116,31 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
cur_ai = ai;
restart:
- ret = AVERROR(EIO);
- fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
- if (fd < 0)
+ fd = ff_socket(cur_ai->ai_family,
+ cur_ai->ai_socktype,
+ cur_ai->ai_protocol);
+ if (fd < 0) {
+ ret = ff_neterrno();
goto fail;
+ }
if (s->listen) {
- int fd1;
- int reuse = 1;
- struct pollfd lp = { fd, POLLIN, 0 };
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
- ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
- if (ret) {
- ret = ff_neterrno();
- goto fail1;
- }
- ret = listen(fd, 1);
- if (ret) {
- ret = ff_neterrno();
- goto fail1;
- }
- ret = poll(&lp, 1, s->listen_timeout >= 0 ? s->listen_timeout : -1);
- if (ret <= 0) {
- ret = AVERROR(ETIMEDOUT);
- goto fail1;
- }
- fd1 = accept(fd, NULL, NULL);
- if (fd1 < 0) {
- ret = ff_neterrno();
+ if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
+ s->listen_timeout, h)) < 0) {
+ ret = fd;
goto fail1;
}
- closesocket(fd);
- fd = fd1;
- ff_socket_nonblock(fd, 1);
} else {
- redo:
- ff_socket_nonblock(fd, 1);
- ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
- }
+ if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
+ s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
- if (ret < 0) {
- struct pollfd p = {fd, POLLOUT, 0};
- int64_t wait_started;
- ret = ff_neterrno();
- if (ret == AVERROR(EINTR)) {
- if (ff_check_interrupt(&h->interrupt_callback)) {
- ret = AVERROR_EXIT;
+ if (ret == AVERROR_EXIT)
goto fail1;
- }
- goto redo;
- }
- if (ret != AVERROR(EINPROGRESS) &&
- ret != AVERROR(EAGAIN))
- goto fail;
-
- /* wait until we are connected or until abort */
- wait_started = av_gettime();
- do {
- if (ff_check_interrupt(&h->interrupt_callback)) {
- ret = AVERROR_EXIT;
- goto fail1;
- }
- ret = poll(&p, 1, 100);
- if (ret > 0)
- break;
- } while (!h->rw_timeout || (av_gettime() - wait_started < h->rw_timeout));
- if (ret <= 0) {
- ret = AVERROR(ETIMEDOUT);
- goto fail;
- }
- /* test error */
- optlen = sizeof(ret);
- if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
- ret = AVUNERROR(ff_neterrno());
- if (ret != 0) {
- char errbuf[100];
- ret = AVERROR(ret);
- av_strerror(ret, errbuf, sizeof(errbuf));
- av_log(h, AV_LOG_ERROR,
- "TCP connection to %s:%d failed: %s\n",
- hostname, port, errbuf);
- goto fail;
+ else
+ goto fail;
}
}
+
h->is_streamed = 1;
s->fd = fd;
freeaddrinfo(ai);
@@ -202,6 +152,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
cur_ai = cur_ai->ai_next;
if (fd >= 0)
closesocket(fd);
+ ret = 0;
goto restart;
}
fail1: