tcp: accept params through avio_open2() options
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
parent
949acefc11
commit
2e009c6042
@ -20,6 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
#include "avformat.h"
|
#include "avformat.h"
|
||||||
#include "libavutil/parseutils.h"
|
#include "libavutil/parseutils.h"
|
||||||
|
#include "libavutil/opt.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "os_support.h"
|
#include "os_support.h"
|
||||||
@ -29,21 +30,40 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct TCPContext {
|
typedef struct TCPContext {
|
||||||
|
const AVClass *class;
|
||||||
int fd;
|
int fd;
|
||||||
|
int listen;
|
||||||
|
int rw_timeout;
|
||||||
|
int listen_timeout;
|
||||||
} TCPContext;
|
} 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 */
|
/* return non zero if error */
|
||||||
static int tcp_open(URLContext *h, const char *uri, int flags)
|
static int tcp_open(URLContext *h, const char *uri, int flags)
|
||||||
{
|
{
|
||||||
struct addrinfo hints = { 0 }, *ai, *cur_ai;
|
struct addrinfo hints = { 0 }, *ai, *cur_ai;
|
||||||
int port, fd = -1;
|
int port, fd = -1;
|
||||||
TCPContext *s = h->priv_data;
|
TCPContext *s = h->priv_data;
|
||||||
int listen_socket = 0;
|
|
||||||
const char *p;
|
const char *p;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
int ret;
|
int ret;
|
||||||
socklen_t optlen;
|
socklen_t optlen;
|
||||||
int listen_timeout = -1;
|
|
||||||
char hostname[1024],proto[1024],path[1024];
|
char hostname[1024],proto[1024],path[1024];
|
||||||
char portstr[10];
|
char portstr[10];
|
||||||
h->rw_timeout = 5000000;
|
h->rw_timeout = 5000000;
|
||||||
@ -59,18 +79,19 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
|
|||||||
p = strchr(uri, '?');
|
p = strchr(uri, '?');
|
||||||
if (p) {
|
if (p) {
|
||||||
if (av_find_info_tag(buf, sizeof(buf), "listen", 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)) {
|
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)) {
|
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_family = AF_UNSPEC;
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
snprintf(portstr, sizeof(portstr), "%d", port);
|
snprintf(portstr, sizeof(portstr), "%d", port);
|
||||||
if (listen_socket)
|
if (s->listen)
|
||||||
hints.ai_flags |= AI_PASSIVE;
|
hints.ai_flags |= AI_PASSIVE;
|
||||||
if (!hostname[0])
|
if (!hostname[0])
|
||||||
ret = getaddrinfo(NULL, portstr, &hints, &ai);
|
ret = getaddrinfo(NULL, portstr, &hints, &ai);
|
||||||
@ -91,7 +112,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
|
|||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (listen_socket) {
|
if (s->listen) {
|
||||||
int fd1;
|
int fd1;
|
||||||
int reuse = 1;
|
int reuse = 1;
|
||||||
struct pollfd lp = { fd, POLLIN, 0 };
|
struct pollfd lp = { fd, POLLIN, 0 };
|
||||||
@ -106,7 +127,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
|
|||||||
ret = ff_neterrno();
|
ret = ff_neterrno();
|
||||||
goto fail1;
|
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) {
|
if (ret <= 0) {
|
||||||
ret = AVERROR(ETIMEDOUT);
|
ret = AVERROR(ETIMEDOUT);
|
||||||
goto fail1;
|
goto fail1;
|
||||||
@ -255,5 +276,6 @@ URLProtocol ff_tcp_protocol = {
|
|||||||
.url_get_file_handle = tcp_get_file_handle,
|
.url_get_file_handle = tcp_get_file_handle,
|
||||||
.url_shutdown = tcp_shutdown,
|
.url_shutdown = tcp_shutdown,
|
||||||
.priv_data_size = sizeof(TCPContext),
|
.priv_data_size = sizeof(TCPContext),
|
||||||
|
.priv_data_class = &tcp_context_class,
|
||||||
.flags = URL_PROTOCOL_FLAG_NETWORK,
|
.flags = URL_PROTOCOL_FLAG_NETWORK,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user