42 #if HAVE_PTHREAD_CANCEL
46 #ifndef HAVE_PTHREAD_CANCEL
47 #define HAVE_PTHREAD_CANCEL 0
50 #ifndef IPV6_ADD_MEMBERSHIP
51 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
52 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
55 #define UDP_TX_BUF_SIZE 32768
56 #define UDP_MAX_PKT_SIZE 65536
75 #if HAVE_PTHREAD_CANCEL
88 #define OFFSET(x) offsetof(UDPContext, x)
89 #define D AV_OPT_FLAG_DECODING_PARAM
90 #define E AV_OPT_FLAG_ENCODING_PARAM
92 {
"buffer_size",
"Socket buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D|
E },
96 {
"reuse",
"Explicitly allow or disallow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
97 {
"ttl",
"Set the time to live value (for multicast only)",
OFFSET(ttl),
AV_OPT_TYPE_INT, {.i64 = 16}, 0, INT_MAX,
E },
98 {
"connect",
"Should connect() be called on socket",
OFFSET(is_connected),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
100 {
"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 },
101 {
"overrun_nonfatal",
"Survive in case of UDP receiving circular buffer overrun",
OFFSET(overrun_nonfatal),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D },
102 {
"timeout",
"In read mode: if no data arrived in more than this time interval, raise error",
OFFSET(timeout),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D },
117 av_log(ctx, level,
"%s: %s\n", prefix, errbuf);
121 struct sockaddr *addr)
123 #ifdef IP_MULTICAST_TTL
124 if (addr->sa_family == AF_INET) {
125 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL,
sizeof(mcastTTL)) < 0) {
131 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
132 if (addr->sa_family == AF_INET6) {
133 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL,
sizeof(mcastTTL)) < 0) {
144 #ifdef IP_ADD_MEMBERSHIP
145 if (addr->sa_family == AF_INET) {
148 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
149 mreq.imr_interface.s_addr= INADDR_ANY;
150 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
156 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
157 if (addr->sa_family == AF_INET6) {
158 struct ipv6_mreq mreq6;
160 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
161 mreq6.ipv6mr_interface= 0;
173 #ifdef IP_DROP_MEMBERSHIP
174 if (addr->sa_family == AF_INET) {
177 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
178 mreq.imr_interface.s_addr= INADDR_ANY;
179 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
185 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
186 if (addr->sa_family == AF_INET6) {
187 struct ipv6_mreq mreq6;
189 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
190 mreq6.ipv6mr_interface= 0;
201 int type,
int family,
int flags)
203 struct addrinfo hints = { 0 }, *res = 0;
206 const char *node = 0, *service =
"0";
209 snprintf(sport,
sizeof(sport),
"%d", port);
212 if ((hostname) && (hostname[0] !=
'\0') && (hostname[0] !=
'?')) {
218 if ((error =
getaddrinfo(node, service, &hints, &res))) {
227 int addr_len,
char **sources,
228 int nb_sources,
int include)
230 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
234 for (i = 0; i < nb_sources; i++) {
235 struct group_source_req mreqs;
236 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
238 SOCK_DGRAM, AF_UNSPEC,
243 mreqs.gsr_interface = 0;
244 memcpy(&mreqs.gsr_group, addr, addr_len);
248 if (setsockopt(sockfd, level,
249 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
250 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
258 #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
260 if (addr->sa_family != AF_INET) {
262 "Setting multicast sources only supported for IPv4\n");
265 for (i = 0; i < nb_sources; i++) {
266 struct ip_mreq_source mreqs;
268 SOCK_DGRAM, AF_UNSPEC,
272 if (sourceaddr->
ai_addr->sa_family != AF_INET) {
279 mreqs.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
280 mreqs.imr_interface.s_addr = INADDR_ANY;
281 mreqs.imr_sourceaddr.s_addr = ((
struct sockaddr_in *)sourceaddr->
ai_addr)->sin_addr.s_addr;
284 if (setsockopt(sockfd, IPPROTO_IP,
285 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
286 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
300 const char *hostname,
int port)
306 if (res0 == 0)
return AVERROR(EIO);
315 socklen_t *addr_len,
const char *localaddr)
319 int family = AF_UNSPEC;
321 if (((
struct sockaddr *) &s->
dest_addr)->sa_family)
322 family = ((
struct sockaddr *) &s->
dest_addr)->sa_family;
327 for (res = res0; res; res=res->
ai_next) {
328 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
329 if (udp_fd != -1)
break;
336 memcpy(addr, res->ai_addr, res->ai_addrlen);
337 *addr_len = res->ai_addrlen;
353 char sbuf[
sizeof(int)*3+1];
361 return strtol(sbuf,
NULL, 10);
384 char hostname[256], buf[10];
396 p = strchr(uri,
'?');
437 #if HAVE_PTHREAD_CANCEL
438 static void *circular_buffer_task(
void *_URLContext)
444 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
458 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
460 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
475 "Surviving due to overrun_nonfatal option\n");
479 "To avoid, increase fifo_size URL option. "
480 "To survive in such case, use overrun_nonfatal option\n");
500 char hostname[1024], localaddr[1024] =
"";
501 int port, udp_fd = -1, tmp, bind_ret = -1;
508 int reuse_specified = 0;
509 int i, include = 0, num_sources = 0;
518 p = strchr(uri,
'?');
536 "'overrun_nonfatal' option was set but it is not supported "
537 "on this build (pthread support is required)\n");
540 s->
ttl = strtol(buf,
NULL, 10);
558 "'circular_buffer_size' option was set but it is not supported "
559 "on this build (pthread support is required)\n");
562 av_strlcpy(localaddr, buf,
sizeof(localaddr));
571 char *next = strchr(source_start,
',');
574 sources[num_sources] =
av_strdup(source_start);
575 if (!sources[num_sources])
577 source_start = next + 1;
595 if (hostname[0] ==
'\0' || hostname[0] ==
'?') {
624 bind_ret = bind(udp_fd,(
struct sockaddr *)&s->
dest_addr, len);
629 if (bind_ret < 0 && bind(udp_fd,(
struct sockaddr *)&my_addr, len) < 0) {
634 len =
sizeof(my_addr);
635 getsockname(udp_fd, (
struct sockaddr *)&my_addr, &len);
646 if (num_sources == 0 || !include) {
654 }
else if (include && num_sources) {
667 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp,
sizeof(tmp)) < 0) {
675 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp,
sizeof(tmp)) < 0) {
688 for (i = 0; i < num_sources; i++)
693 #if HAVE_PTHREAD_CANCEL
714 s->thread_started = 1;
719 #if HAVE_PTHREAD_CANCEL
729 for (i = 0; i < num_sources; i++)
740 #if HAVE_PTHREAD_CANCEL
763 }
else if(nonblock) {
771 struct timespec tv = { .tv_sec = t / 1000000,
772 .tv_nsec = (t % 1000000) * 1000 };
773 if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
775 return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
788 ret = recv(s->
udp_fd, buf, size, 0);
805 ret = sendto (s->
udp_fd, buf, size, 0,
809 ret = send(s->
udp_fd, buf, size, 0);
822 #if HAVE_PTHREAD_CANCEL
823 if (s->thread_started) {
824 pthread_cancel(s->circular_buffer_thread);
844 .priv_data_class = &udp_context_class,