Commit 4f3f5ee1 authored by Michael Niedermayer's avatar Michael Niedermayer

Merge commit '66028b7b'

* commit '66028b7b':
  udp: Use AVOptions

Conflicts:
	libavformat/udp.c

See: aefed6caMerged-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parents 54db7df3 66028b7b
...@@ -77,6 +77,7 @@ typedef struct UDPContext { ...@@ -77,6 +77,7 @@ typedef struct UDPContext {
int ttl; int ttl;
int udplite_coverage; int udplite_coverage;
int buffer_size; int buffer_size;
int pkt_size;
int is_multicast; int is_multicast;
int is_broadcast; int is_broadcast;
int local_port; int local_port;
...@@ -98,33 +99,38 @@ typedef struct UDPContext { ...@@ -98,33 +99,38 @@ typedef struct UDPContext {
#endif #endif
uint8_t tmp[UDP_MAX_PKT_SIZE+4]; uint8_t tmp[UDP_MAX_PKT_SIZE+4];
int remaining_in_dg; int remaining_in_dg;
char *local_addr; char *localaddr;
int packet_size;
int timeout; int timeout;
struct sockaddr_storage local_addr_storage; struct sockaddr_storage local_addr_storage;
char *sources;
char *block;
} UDPContext; } UDPContext;
#define OFFSET(x) offsetof(UDPContext, x) #define OFFSET(x) offsetof(UDPContext, x)
#define D AV_OPT_FLAG_DECODING_PARAM #define D AV_OPT_FLAG_DECODING_PARAM
#define E AV_OPT_FLAG_ENCODING_PARAM #define E AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = { static const AVOption options[] = {
{"buffer_size", "set packet buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E }, { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
{"localport", "set local port to bind to", OFFSET(local_port), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E }, { "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E },
{"localaddr", "choose local IP address", OFFSET(local_addr), AV_OPT_TYPE_STRING, {.str = ""}, 0, 0, D|E }, { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
{"udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E }, { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = "" }, .flags = D|E },
{"pkt_size", "set size of UDP packets", OFFSET(packet_size), AV_OPT_TYPE_INT, {.i64 = 1472}, 0, INT_MAX, D|E }, { "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
{"reuse", "explicitly allow or disallow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E }, { "pkt_size", "set size of UDP packets", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, D|E },
{"broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E }, { "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E },
{"ttl", "set the time to live value (for multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, {.i64 = 16}, 0, INT_MAX, E }, { "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, D|E },
{"connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E }, { "reuse_socket", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
/* TODO 'sources', 'block' option */ { "broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
{"fifo_size", "set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = 7*4096}, 0, INT_MAX, D }, { "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, E },
{"overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D }, { "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
{"timeout", "set raise error timeout (only in read mode)", OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D }, { "fifo_size", "set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = 7*4096}, 0, INT_MAX, D },
{NULL} { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D },
{ "timeout", "set raise error timeout (only in read mode)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
{ "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
{ "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
{ NULL }
}; };
static const AVClass udp_context_class = { static const AVClass udp_class = {
.class_name = "udp", .class_name = "udp",
.item_name = av_default_item_name, .item_name = av_default_item_name,
.option = options, .option = options,
...@@ -563,16 +569,31 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -563,16 +569,31 @@ static int udp_open(URLContext *h, const char *uri, int flags)
char buf[256]; char buf[256];
struct sockaddr_storage my_addr; struct sockaddr_storage my_addr;
socklen_t len; socklen_t len;
int reuse_specified = 0;
int i, num_include_sources = 0, num_exclude_sources = 0; int i, num_include_sources = 0, num_exclude_sources = 0;
char *include_sources[32], *exclude_sources[32]; char *include_sources[32], *exclude_sources[32];
h->is_streamed = 1; h->is_streamed = 1;
is_output = !(flags & AVIO_FLAG_READ); is_output = !(flags & AVIO_FLAG_READ);
if (!s->buffer_size) /* if not set explicitly */ if (s->buffer_size < 0)
s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE; s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
if (s->sources) {
if (parse_source_list(s->sources, include_sources,
&num_include_sources,
FF_ARRAY_ELEMS(include_sources)))
goto fail;
}
if (s->block) {
if (parse_source_list(s->block, exclude_sources, &num_exclude_sources,
FF_ARRAY_ELEMS(exclude_sources)))
goto fail;
}
if (s->pkt_size)
h->max_packet_size = s->pkt_size;
p = strchr(uri, '?'); p = strchr(uri, '?');
if (p) { if (p) {
if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) { if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
...@@ -581,7 +602,6 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -581,7 +602,6 @@ static int udp_open(URLContext *h, const char *uri, int flags)
/* assume if no digits were found it is a request to enable it */ /* assume if no digits were found it is a request to enable it */
if (buf == endptr) if (buf == endptr)
s->reuse_socket = 1; s->reuse_socket = 1;
reuse_specified = 1;
} }
if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) { if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
char *endptr = NULL; char *endptr = NULL;
...@@ -604,7 +624,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -604,7 +624,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
s->local_port = strtol(buf, NULL, 10); s->local_port = strtol(buf, NULL, 10);
} }
if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) { if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
s->packet_size = strtol(buf, NULL, 10); s->pkt_size = strtol(buf, NULL, 10);
} }
if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) { if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
s->buffer_size = strtol(buf, NULL, 10); s->buffer_size = strtol(buf, NULL, 10);
...@@ -643,7 +663,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -643,7 +663,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
/* handling needed to support options picking from both AVOption and URL */ /* handling needed to support options picking from both AVOption and URL */
s->circular_buffer_size *= 188; s->circular_buffer_size *= 188;
if (flags & AVIO_FLAG_WRITE) { if (flags & AVIO_FLAG_WRITE) {
h->max_packet_size = s->packet_size; h->max_packet_size = s->pkt_size;
} else { } else {
h->max_packet_size = UDP_MAX_PKT_SIZE; h->max_packet_size = UDP_MAX_PKT_SIZE;
} }
...@@ -662,9 +682,13 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -662,9 +682,13 @@ static int udp_open(URLContext *h, const char *uri, int flags)
goto fail; goto fail;
} }
if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ)) if ((s->is_multicast || s->local_port <= 0) && (h->flags & AVIO_FLAG_READ))
s->local_port = port; s->local_port = port;
udp_fd = udp_socket_create(s, &my_addr, &len, localaddr[0] ? localaddr : s->local_addr);
if (localaddr[0])
udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
else
udp_fd = udp_socket_create(s, &my_addr, &len, s->localaddr);
if (udp_fd < 0) if (udp_fd < 0)
goto fail; goto fail;
...@@ -673,7 +697,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -673,7 +697,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
/* Follow the requested reuse option, unless it's multicast in which /* Follow the requested reuse option, unless it's multicast in which
* case enable reuse unless explicitly disabled. * case enable reuse unless explicitly disabled.
*/ */
if (s->reuse_socket || (s->is_multicast && !reuse_specified)) { if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
s->reuse_socket = 1; s->reuse_socket = 1;
if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0) if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
goto fail; goto fail;
...@@ -951,7 +975,7 @@ URLProtocol ff_udp_protocol = { ...@@ -951,7 +975,7 @@ URLProtocol ff_udp_protocol = {
.url_close = udp_close, .url_close = udp_close,
.url_get_file_handle = udp_get_file_handle, .url_get_file_handle = udp_get_file_handle,
.priv_data_size = sizeof(UDPContext), .priv_data_size = sizeof(UDPContext),
.priv_data_class = &udp_context_class, .priv_data_class = &udp_class,
.flags = URL_PROTOCOL_FLAG_NETWORK, .flags = URL_PROTOCOL_FLAG_NETWORK,
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment