49 #define UDPLITE_SEND_CSCOV 10
50 #define UDPLITE_RECV_CSCOV 11
53 #ifndef IPPROTO_UDPLITE
54 #define IPPROTO_UDPLITE 136
57 #if HAVE_PTHREAD_CANCEL
61 #ifndef HAVE_PTHREAD_CANCEL
62 #define HAVE_PTHREAD_CANCEL 0
65 #ifndef IPV6_ADD_MEMBERSHIP
66 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
67 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
70 #define UDP_TX_BUF_SIZE 32768
71 #define UDP_MAX_PKT_SIZE 65536
72 #define UDP_HEADER_SIZE 8
93 #if HAVE_PTHREAD_CANCEL
107 #define OFFSET(x) offsetof(UDPContext, x)
108 #define D AV_OPT_FLAG_DECODING_PARAM
109 #define E AV_OPT_FLAG_ENCODING_PARAM
111 {
"buffer_size",
"set packet buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D|
E },
114 {
"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 },
115 {
"pkt_size",
"set size of UDP packets",
OFFSET(packet_size),
AV_OPT_TYPE_INT, {.i64 = 1472}, 0, INT_MAX,
D|
E },
116 {
"reuse",
"explicitly allow or disallow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
117 {
"broadcast",
"explicitly allow or disallow broadcast destination",
OFFSET(is_broadcast),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E },
118 {
"ttl",
"set the time to live value (for multicast only)",
OFFSET(ttl),
AV_OPT_TYPE_INT, {.i64 = 16}, 0, INT_MAX,
E },
119 {
"connect",
"set if connect() should be called on socket",
OFFSET(is_connected),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
121 {
"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 },
122 {
"overrun_nonfatal",
"survive in case of UDP receiving circular buffer overrun",
OFFSET(overrun_nonfatal),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D },
123 {
"timeout",
"set raise error timeout (only in read mode)",
OFFSET(timeout),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D },
145 av_log(ctx, level,
"%s: %s\n", prefix, errbuf);
149 struct sockaddr *addr)
151 #ifdef IP_MULTICAST_TTL
152 if (addr->sa_family == AF_INET) {
153 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL,
sizeof(mcastTTL)) < 0) {
159 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
160 if (addr->sa_family == AF_INET6) {
161 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL,
sizeof(mcastTTL)) < 0) {
172 #ifdef IP_ADD_MEMBERSHIP
173 if (addr->sa_family == AF_INET) {
176 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
178 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
180 mreq.imr_interface.s_addr= INADDR_ANY;
181 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
187 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
188 if (addr->sa_family == AF_INET6) {
189 struct ipv6_mreq mreq6;
191 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
192 mreq6.ipv6mr_interface= 0;
204 #ifdef IP_DROP_MEMBERSHIP
205 if (addr->sa_family == AF_INET) {
208 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
210 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
212 mreq.imr_interface.s_addr= INADDR_ANY;
213 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
219 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
220 if (addr->sa_family == AF_INET6) {
221 struct ipv6_mreq mreq6;
223 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
224 mreq6.ipv6mr_interface= 0;
237 struct addrinfo hints = { 0 }, *res = 0;
240 const char *node = 0, *service =
"0";
243 snprintf(sport,
sizeof(sport),
"%d", port);
246 if ((hostname) && (hostname[0] !=
'\0') && (hostname[0] !=
'?')) {
252 if ((error =
getaddrinfo(node, service, &hints, &res))) {
261 int addr_len,
char **sources,
262 int nb_sources,
int include)
264 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
268 for (i = 0; i < nb_sources; i++) {
269 struct group_source_req mreqs;
270 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
272 SOCK_DGRAM, AF_UNSPEC,
277 mreqs.gsr_interface = 0;
278 memcpy(&mreqs.gsr_group, addr, addr_len);
282 if (setsockopt(sockfd, level,
283 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
284 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
292 #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
294 if (addr->sa_family != AF_INET) {
296 "Setting multicast sources only supported for IPv4\n");
299 for (i = 0; i < nb_sources; i++) {
300 struct ip_mreq_source mreqs;
302 SOCK_DGRAM, AF_UNSPEC,
306 if (sourceaddr->
ai_addr->sa_family != AF_INET) {
313 mreqs.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
314 mreqs.imr_interface.s_addr = INADDR_ANY;
315 mreqs.imr_sourceaddr.s_addr = ((
struct sockaddr_in *)sourceaddr->
ai_addr)->sin_addr.s_addr;
318 if (setsockopt(sockfd, IPPROTO_IP,
319 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
320 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
334 const char *hostname,
int port)
340 if (!res0)
return AVERROR(EIO);
349 socklen_t *addr_len,
const char *localaddr)
353 int family = AF_UNSPEC;
355 if (((
struct sockaddr *) &s->
dest_addr)->sa_family)
356 family = ((
struct sockaddr *) &s->
dest_addr)->sa_family;
361 for (res = res0; res; res=res->
ai_next) {
366 if (udp_fd != -1)
break;
390 char sbuf[
sizeof(int)*3+1];
398 return strtol(sbuf, NULL, 10);
421 char hostname[256],
buf[10];
425 av_url_split(NULL, 0, NULL, 0, hostname,
sizeof(hostname), &port, NULL, 0, uri);
433 p = strchr(uri,
'?');
474 #if HAVE_PTHREAD_CANCEL
475 static void *circular_buffer_task(
void *_URLContext)
481 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
495 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
497 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
512 "Surviving due to overrun_nonfatal option\n");
516 "To avoid, increase fifo_size URL option. "
517 "To survive in such case, use overrun_nonfatal option\n");
540 char *next = strchr(source_start,
',');
543 sources[*num_sources] =
av_strdup(source_start);
544 if (!sources[*num_sources])
546 source_start = next + 1;
548 if (*num_sources >= max_sources || !next)
558 char hostname[1024], localaddr[1024] =
"";
559 int port, udp_fd = -1, tmp, bind_ret = -1, dscp = -1;
566 int reuse_specified = 0;
567 int i, num_include_sources = 0, num_exclude_sources = 0;
568 char *include_sources[32], *exclude_sources[32];
576 p = strchr(uri,
'?');
594 "'overrun_nonfatal' option was set but it is not supported "
595 "on this build (pthread support is required)\n");
598 s->
ttl = strtol(buf, NULL, 10);
616 dscp = strtol(buf, NULL, 10);
622 "'circular_buffer_size' option was set but it is not supported "
623 "on this build (pthread support is required)\n");
626 av_strlcpy(localaddr, buf,
sizeof(localaddr));
639 s->
timeout = strtol(buf, NULL, 10);
653 av_url_split(NULL, 0, NULL, 0, hostname,
sizeof(hostname), &port, NULL, 0, uri);
656 if (hostname[0] ==
'\0' || hostname[0] ==
'?') {
703 if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp,
sizeof(dscp)) != 0)
712 bind_ret = bind(udp_fd,(
struct sockaddr *)&s->
dest_addr, len);
717 if (bind_ret < 0 && bind(udp_fd,(
struct sockaddr *)&my_addr, len) < 0) {
722 len =
sizeof(my_addr);
723 getsockname(udp_fd, (
struct sockaddr *)&my_addr, &len);
727 if (h->
flags & AVIO_FLAG_WRITE) {
734 if (num_include_sources && num_exclude_sources) {
735 av_log(h,
AV_LOG_ERROR,
"Simultaneously including and excluding multicast sources is not supported\n");
738 if (num_include_sources) {
745 if (num_exclude_sources) {
755 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp,
sizeof(tmp)) < 0) {
762 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp,
sizeof(tmp)) < 0) {
766 if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
770 if(tmp < s->buffer_size)
784 for (i = 0; i < num_include_sources; i++)
786 for (i = 0; i < num_exclude_sources; i++)
791 #if HAVE_PTHREAD_CANCEL
807 ret =
pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h);
812 s->thread_started = 1;
817 #if HAVE_PTHREAD_CANCEL
827 for (i = 0; i < num_include_sources; i++)
829 for (i = 0; i < num_exclude_sources; i++)
848 #if HAVE_PTHREAD_CANCEL
873 }
else if(nonblock) {
881 struct timespec tv = { .tv_sec = t / 1000000,
882 .tv_nsec = (t % 1000000) * 1000 };
883 if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
885 return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
898 ret = recv(s->
udp_fd, buf, size, 0);
915 ret = sendto (s->
udp_fd, buf, size, 0,
919 ret = send(s->
udp_fd, buf, size, 0);
931 #if HAVE_PTHREAD_CANCEL
932 if (s->thread_started) {
934 pthread_cancel(s->circular_buffer_thread);
954 .priv_data_class = &udp_context_class,
966 .priv_data_class = &udplite_context_class,