27 #define _DEFAULT_SOURCE
47 #include "TargetConditionals.h"
56 #define UDPLITE_SEND_CSCOV 10
57 #define UDPLITE_RECV_CSCOV 11
60 #ifndef IPPROTO_UDPLITE
61 #define IPPROTO_UDPLITE 136
65 #undef HAVE_PTHREAD_CANCEL
66 #define HAVE_PTHREAD_CANCEL 1
69 #if HAVE_PTHREAD_CANCEL
73 #ifndef IPV6_ADD_MEMBERSHIP
74 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
75 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
78 #define UDP_TX_BUF_SIZE 32768
79 #define UDP_RX_BUF_SIZE 393216
80 #define UDP_MAX_PKT_SIZE 65536
81 #define UDP_HEADER_SIZE 8
106 #if HAVE_PTHREAD_CANCEL
122 #define OFFSET(x) offsetof(UDPContext, x)
123 #define D AV_OPT_FLAG_DECODING_PARAM
124 #define E AV_OPT_FLAG_ENCODING_PARAM
126 {
"buffer_size",
"System data size (in bytes)",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
128 {
"burst_bits",
"Max length of bursts in bits (when using bitrate)",
OFFSET(burst_bits),
AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags =
E },
132 {
"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 },
133 {
"pkt_size",
"Maximum UDP packet size",
OFFSET(pkt_size),
AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags =
D|
E },
134 {
"reuse",
"explicitly allow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
D|
E },
135 {
"reuse_socket",
"explicitly allow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags =
D|
E },
136 {
"broadcast",
"explicitly allow or disallow broadcast destination",
OFFSET(is_broadcast),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
E },
138 {
"connect",
"set if connect() should be called on socket",
OFFSET(is_connected),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags =
D|
E },
139 {
"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 },
140 {
"overrun_nonfatal",
"survive in case of UDP receiving circular buffer overrun",
OFFSET(overrun_nonfatal),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
D },
141 {
"timeout",
"set raise error timeout, in microseconds (only in read mode)",
OFFSET(timeout),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D },
162 struct sockaddr *addr,
171 switch (addr->sa_family) {
172 #ifdef IP_MULTICAST_TTL
174 protocol = IPPROTO_IP;
175 cmd = IP_MULTICAST_TTL;
178 #ifdef IPV6_MULTICAST_HOPS
180 protocol = IPPROTO_IPV6;
181 cmd = IPV6_MULTICAST_HOPS;
188 if (setsockopt(sockfd, protocol, cmd, &mcastTTL,
sizeof(mcastTTL)) < 0) {
190 unsigned char ttl = (
unsigned char) mcastTTL;
193 if (setsockopt(sockfd, protocol, cmd, &ttl,
sizeof(ttl)) < 0) {
203 struct sockaddr *local_addr,
void *logctx)
205 #ifdef IP_ADD_MEMBERSHIP
206 if (addr->sa_family == AF_INET) {
209 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
211 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
213 mreq.imr_interface.s_addr = INADDR_ANY;
214 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
220 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
221 if (addr->sa_family == AF_INET6) {
222 struct ipv6_mreq mreq6;
224 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
226 mreq6.ipv6mr_interface = 0;
237 struct sockaddr *local_addr,
void *logctx)
239 #ifdef IP_DROP_MEMBERSHIP
240 if (addr->sa_family == AF_INET) {
243 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
245 mreq.imr_interface = ((
struct sockaddr_in *)local_addr)->sin_addr;
247 mreq.imr_interface.s_addr = INADDR_ANY;
248 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
254 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
255 if (addr->sa_family == AF_INET6) {
256 struct ipv6_mreq mreq6;
258 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
260 mreq6.ipv6mr_interface = 0;
271 int sockfd,
struct sockaddr *addr,
274 int nb_sources,
int include)
277 if (addr->sa_family != AF_INET) {
278 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE)
283 for (
i = 0;
i < nb_sources;
i++) {
284 struct group_source_req mreqs;
285 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
288 mreqs.gsr_interface = 0;
289 memcpy(&mreqs.gsr_group, addr, addr_len);
292 if (setsockopt(sockfd,
level,
293 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
294 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
305 "Setting multicast sources only supported for IPv4\n");
309 #if HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
310 for (
i = 0;
i < nb_sources;
i++) {
311 struct ip_mreq_source mreqs;
312 if (
sources[
i].ss_family != AF_INET) {
317 mreqs.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
319 mreqs.imr_interface = ((
struct sockaddr_in *)local_addr)->sin_addr;
321 mreqs.imr_interface.s_addr = INADDR_ANY;
322 mreqs.imr_sourceaddr.s_addr = ((
struct sockaddr_in *)&
sources[
i])->sin_addr.s_addr;
324 if (setsockopt(sockfd, IPPROTO_IP,
325 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
326 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
341 const char *hostname,
int port)
347 if (!res0)
return AVERROR(EIO);
356 socklen_t *addr_len,
const char *localaddr)
361 int family = AF_UNSPEC;
363 if (((
struct sockaddr *) &
s->dest_addr)->sa_family)
364 family = ((
struct sockaddr *) &
s->dest_addr)->sa_family;
370 for (res = res0; res; res=res->
ai_next) {
371 if (
s->udplite_coverage)
375 if (udp_fd != -1)
break;
399 char sbuf[
sizeof(
int)*3+1];
407 return strtol(sbuf,
NULL, 10);
430 char hostname[256], buf[10];
438 if (
s->dest_addr_len < 0) {
442 p = strchr(uri,
'?');
445 int was_connected =
s->is_connected;
446 s->is_connected = strtol(buf,
NULL, 10);
447 if (
s->is_connected && !was_connected) {
448 if (connect(
s->udp_fd, (
struct sockaddr *) &
s->dest_addr,
469 return s->local_port;
483 #if HAVE_PTHREAD_CANCEL
484 static void *circular_buffer_task_rx(
void *_URLContext)
494 s->circular_buffer_error =
AVERROR(EIO);
500 socklen_t addr_len =
sizeof(addr);
507 len = recvfrom(
s->udp_fd,
s->tmp+4,
sizeof(
s->tmp)-4, 0, (
struct sockaddr *)&addr, &addr_len);
523 if (
s->overrun_nonfatal) {
525 "Surviving due to overrun_nonfatal option\n");
529 "To avoid, increase fifo_size URL option. "
530 "To survive in such case, use overrun_nonfatal option\n");
531 s->circular_buffer_error =
AVERROR(EIO);
545 static void *circular_buffer_task_tx(
void *_URLContext)
551 int64_t sent_bits = 0;
552 int64_t burst_interval =
s->bitrate ? (
s->burst_bits * 1000000 /
s->bitrate) : 0;
553 int64_t max_delay =
s->bitrate ? ((int64_t)
h->max_packet_size * 8 * 1000000 /
s->bitrate + 1) : 0;
559 s->circular_buffer_error =
AVERROR(EIO);
590 if (timestamp < target_timestamp) {
591 int64_t delay = target_timestamp - timestamp;
592 if (delay > max_delay) {
594 start_timestamp = timestamp + delay;
599 if (timestamp - burst_interval > target_timestamp) {
600 start_timestamp = timestamp - burst_interval;
604 sent_bits +=
len * 8;
605 target_timestamp = start_timestamp + sent_bits * 1000000 /
s->bitrate;
612 if (!
s->is_connected) {
613 ret = sendto (
s->udp_fd, p,
len, 0,
614 (
struct sockaddr *) &
s->dest_addr,
617 ret = send(
s->udp_fd, p,
len, 0);
625 s->circular_buffer_error =
ret;
648 int port, udp_fd = -1,
tmp, bind_ret = -1, dscp = -1;
660 if (
s->buffer_size < 0)
673 p = strchr(uri,
'?');
677 s->reuse_socket = strtol(buf, &endptr, 10);
684 s->overrun_nonfatal = strtol(buf, &endptr, 10);
687 s->overrun_nonfatal = 1;
688 if (!HAVE_PTHREAD_CANCEL)
690 "'overrun_nonfatal' option was set but it is not supported "
691 "on this build (pthread support is required)\n");
694 s->ttl = strtol(buf,
NULL, 10);
695 if (
s->ttl < 0 ||
s->ttl > 255) {
702 s->udplite_coverage = strtol(buf,
NULL, 10);
705 s->local_port = strtol(buf,
NULL, 10);
708 s->pkt_size = strtol(buf,
NULL, 10);
711 s->buffer_size = strtol(buf,
NULL, 10);
714 s->is_connected = strtol(buf,
NULL, 10);
717 dscp = strtol(buf,
NULL, 10);
720 s->circular_buffer_size = strtol(buf,
NULL, 10);
721 if (!HAVE_PTHREAD_CANCEL)
723 "'circular_buffer_size' option was set but it is not supported "
724 "on this build (pthread support is required)\n");
727 s->bitrate = strtoll(buf,
NULL, 10);
728 if (!HAVE_PTHREAD_CANCEL)
730 "'bitrate' option was set but it is not supported "
731 "on this build (pthread support is required)\n");
734 s->burst_bits = strtoll(buf,
NULL, 10);
749 s->timeout = strtol(buf,
NULL, 10);
751 s->is_broadcast = strtol(buf,
NULL, 10);
754 s->circular_buffer_size *= 188;
756 h->max_packet_size =
s->pkt_size;
760 h->rw_timeout =
s->timeout;
766 if (hostname[0] ==
'\0' || hostname[0] ==
'?') {
778 s->local_port = port;
786 s->local_addr_storage=my_addr;
791 if (
s->reuse_socket > 0 || (
s->is_multicast &&
s->reuse_socket < 0)) {
793 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(
s->reuse_socket),
sizeof(
s->reuse_socket)) != 0) {
799 if (
s->is_broadcast) {
801 if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(
s->is_broadcast),
sizeof(
s->is_broadcast)) != 0) {
815 if (
s->udplite_coverage) {
825 if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp,
sizeof(dscp)) != 0) {
836 bind_ret = bind(udp_fd,(
struct sockaddr *)&
s->dest_addr,
len);
841 if (bind_ret < 0 && bind(udp_fd,(
struct sockaddr *)&my_addr,
len) < 0) {
847 len =
sizeof(my_addr);
848 getsockname(udp_fd, (
struct sockaddr *)&my_addr, &
len);
851 if (
s->is_multicast) {
859 if (
s->filters.nb_include_addrs) {
861 (
struct sockaddr *)&
s->dest_addr,
862 s->dest_addr_len, &
s->local_addr_storage,
863 s->filters.include_addrs,
864 s->filters.nb_include_addrs, 1)) < 0)
868 (
struct sockaddr *)&
s->local_addr_storage,
h)) < 0)
871 if (
s->filters.nb_exclude_addrs) {
873 (
struct sockaddr *)&
s->dest_addr,
874 s->dest_addr_len, &
s->local_addr_storage,
875 s->filters.exclude_addrs,
876 s->filters.nb_exclude_addrs, 0)) < 0)
884 tmp =
s->buffer_size;
885 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &
tmp,
sizeof(
tmp)) < 0) {
892 tmp =
s->buffer_size;
893 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &
tmp,
sizeof(
tmp)) < 0) {
897 if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &
tmp, &
len) < 0) {
901 if(tmp < s->buffer_size)
902 av_log(
h,
AV_LOG_WARNING,
"attempted to set receive buffer to size %d but it only ended up set as %d\n",
s->buffer_size,
tmp);
908 if (
s->is_connected) {
909 if (connect(udp_fd, (
struct sockaddr *) &
s->dest_addr,
s->dest_addr_len)) {
918 #if HAVE_PTHREAD_CANCEL
925 if (is_output &&
s->bitrate && !
s->circular_buffer_size) {
927 av_log(
h,
AV_LOG_WARNING,
"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
930 if ((!is_output &&
s->circular_buffer_size) || (is_output &&
s->bitrate &&
s->circular_buffer_size)) {
949 ret =
pthread_create(&
s->circular_buffer_thread,
NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx,
h);
955 s->thread_started = 1;
960 #if HAVE_PTHREAD_CANCEL
989 socklen_t addr_len =
sizeof(addr);
990 #if HAVE_PTHREAD_CANCEL
1011 }
else if(
s->circular_buffer_error){
1012 int err =
s->circular_buffer_error;
1015 }
else if(nonblock) {
1022 struct timespec tv = { .tv_sec = t / 1000000,
1023 .tv_nsec = (t % 1000000) * 1000 };
1027 return AVERROR(err == ETIMEDOUT ? EAGAIN : err);
1040 ret = recvfrom(
s->udp_fd, buf,
size, 0, (
struct sockaddr *)&addr, &addr_len);
1053 #if HAVE_PTHREAD_CANCEL
1063 if (
s->circular_buffer_error<0) {
1064 int err =
s->circular_buffer_error;
1088 if (!
s->is_connected) {
1089 ret = sendto (
s->udp_fd, buf,
size, 0,
1090 (
struct sockaddr *) &
s->dest_addr,
1093 ret = send(
s->udp_fd, buf,
size, 0);
1102 #if HAVE_PTHREAD_CANCEL
1114 (
struct sockaddr *)&
s->local_addr_storage,
h);
1115 #if HAVE_PTHREAD_CANCEL
1116 if (
s->thread_started) {
1124 shutdown(
s->udp_fd, SD_RECEIVE);
1125 CancelIoEx((HANDLE)(SOCKET)
s->udp_fd,
NULL);
1127 pthread_cancel(
s->circular_buffer_thread);
1137 closesocket(
s->udp_fd);