54#define OFFSET(x) offsetof(TCPContext, x)
55#define D AV_OPT_FLAG_DECODING_PARAM
56#define E AV_OPT_FLAG_ENCODING_PARAM
58 {
"listen",
"Listen for incoming connections",
OFFSET(listen),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags =
D|
E },
61 {
"timeout",
"set timeout (in microseconds) of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
62 {
"listen_timeout",
"Connection awaiting timeout (in milliseconds)",
OFFSET(listen_timeout),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
63 {
"send_buffer_size",
"Socket send buffer size (in bytes)",
OFFSET(send_buffer_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
64 {
"recv_buffer_size",
"Socket receive buffer size (in bytes)",
OFFSET(recv_buffer_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
65 {
"tcp_nodelay",
"Use TCP_NODELAY to disable nagle's algorithm",
OFFSET(tcp_nodelay),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags =
D|
E },
66 {
"tcp_keepalive",
"Use TCP keepalive to detect dead connections and keep long-lived connections active.",
OFFSET(tcp_keepalive),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags =
D|
E },
68 {
"tcp_mss",
"Maximum segment size for outgoing TCP packets",
OFFSET(tcp_mss),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
84 if (
s->local_addr ||
s->local_port) {
85 struct addrinfo hints = { 0 }, *ai, *cur_ai;
94 "Failed to getaddrinfo local addr: %s port: %s err: %s\n",
101 ret = bind(fd, (
struct sockaddr *)cur_ai->ai_addr, (
int)cur_ai->ai_addrlen);
103 cur_ai = cur_ai->ai_next;
116 if (
s->recv_buffer_size > 0) {
117 if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &
s->recv_buffer_size, sizeof (
s->recv_buffer_size))) {
121 if (
s->send_buffer_size > 0) {
122 if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &
s->send_buffer_size, sizeof (
s->send_buffer_size))) {
126 if (
s->tcp_nodelay > 0) {
127 if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &
s->tcp_nodelay, sizeof (
s->tcp_nodelay))) {
131 if (
s->tcp_keepalive > 0) {
132 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &
s->tcp_keepalive,
sizeof(
s->tcp_keepalive))) {
138 if (
s->tcp_mss > 0) {
139 if (setsockopt (fd, IPPROTO_TCP, TCP_MAXSEG, &
s->tcp_mss, sizeof (
s->tcp_mss))) {
151 struct addrinfo hints = { 0 }, *ai, *cur_ai;
156 char hostname[1024],proto[1024],path[1024];
158 s->open_timeout = 5000000;
161 &port, path,
sizeof(path), uri);
162 if (strcmp(proto,
"tcp"))
164 if (port <= 0 || port >= 65536) {
168 p = strchr(uri,
'?');
174 if (
s->rw_timeout >= 0) {
176 h->rw_timeout =
s->rw_timeout;
180 snprintf(portstr,
sizeof(portstr),
"%d", port);
189 "Failed to resolve hostname %s: %s\n",
196#if HAVE_STRUCT_SOCKADDR_IN6
198 if (cur_ai->ai_family == AF_INET6){
199 struct sockaddr_in6 * sockaddr_v6 = (
struct sockaddr_in6 *)cur_ai->ai_addr;
200 if (!sockaddr_v6->sin6_port){
201 sockaddr_v6->sin6_port = htons(port);
207 while (cur_ai && fd < 0) {
210 cur_ai->ai_protocol,
h);
213 cur_ai = cur_ai->ai_next;
221 if (
s->listen == 2) {
223 if ((ret =
ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
h)) < 0)
225 }
else if (
s->listen == 1) {
227 if ((ret =
ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
228 s->listen_timeout,
h)) < 0)
257 if ((ret =
ffurl_alloc(
c,
s->filename,
s->flags, &
s->interrupt_callback)) < 0)
259 cc = (*c)->priv_data;
279 ret = recv(
s->fd, buf,
size, 0);
312 return shutdown(
s->fd, how);
332 socklen_t avail_len =
sizeof(avail);
337 if (
s->recv_buffer_size < 0) {
342 if (getsockopt(
s->fd, SOL_SOCKET, SO_RCVBUF, &avail, &avail_len)) {
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
#define AVIO_FLAG_READ
read-only
#define AVIO_FLAG_WRITE
write-only
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
#define flags(name, subs,...)
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
#define AVERROR_EOF
End of file.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
Memory handling functions.
int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address, int parallel, URLContext *h, int *fd, int(*customize_fd)(void *, int, int), void *customize_ctx)
Connect to any of the given addrinfo addresses, with multiple attempts running in parallel.
int ff_socket(int af, int type, int proto, void *logctx)
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen, void *logctx)
Bind to a file descriptor to an address without accepting connections.
void ff_log_net_error(void *ctx, int level, const char *prefix)
int ff_listen_bind(int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
Bind to a file descriptor and poll for a connection.
int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds Uses ff_network_wa...
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
miscellaneous OS support macros and functions.
const URLProtocol ff_tcp_protocol
Describe the class of an AVClass context structure.
static int tcp_close(URLContext *h)
static const AVClass tcp_class
static int tcp_accept(URLContext *s, URLContext **c)
static int tcp_write(URLContext *h, const uint8_t *buf, int size)
static int tcp_get_window_size(URLContext *h)
static int customize_fd(void *ctx, int fd, int family)
static int tcp_open(URLContext *h, const char *uri, int flags)
static int tcp_read(URLContext *h, uint8_t *buf, int size)
static int tcp_shutdown(URLContext *h, int flags)
static int tcp_get_file_handle(URLContext *h)
static AVFormatContext * ctx
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NETWORK