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
64 #if HAVE_PTHREAD_CANCEL
68 #ifndef IPV6_ADD_MEMBERSHIP
69 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
70 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
73 #define UDP_TX_BUF_SIZE 32768
74 #define UDP_MAX_PKT_SIZE 65536
75 #define UDP_HEADER_SIZE 8
100 #if HAVE_PTHREAD_CANCEL
116 #define OFFSET(x) offsetof(UDPContext, x)
117 #define D AV_OPT_FLAG_DECODING_PARAM
118 #define E AV_OPT_FLAG_ENCODING_PARAM
120 {
"buffer_size",
"System data size (in bytes)",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
122 {
"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 },
126 {
"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 },
127 {
"pkt_size",
"Maximum UDP packet size",
OFFSET(pkt_size),
AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags =
D|
E },
128 {
"reuse",
"explicitly allow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
D|
E },
129 {
"reuse_socket",
"explicitly allow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags =
D|
E },
130 {
"broadcast",
"explicitly allow or disallow broadcast destination",
OFFSET(is_broadcast),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
E },
132 {
"connect",
"set if connect() should be called on socket",
OFFSET(is_connected),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags =
D|
E },
133 {
"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 },
134 {
"overrun_nonfatal",
"survive in case of UDP receiving circular buffer overrun",
OFFSET(overrun_nonfatal),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
D },
135 {
"timeout",
"set raise error timeout (only in read mode)",
OFFSET(timeout),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
D },
156 struct sockaddr *addr)
158 #ifdef IP_MULTICAST_TTL
159 if (addr->sa_family == AF_INET) {
160 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL,
sizeof(mcastTTL)) < 0) {
166 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
167 if (addr->sa_family == AF_INET6) {
168 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL,
sizeof(mcastTTL)) < 0) {
179 #ifdef IP_ADD_MEMBERSHIP
180 if (addr->sa_family == AF_INET) {
183 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
185 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
187 mreq.imr_interface.s_addr= INADDR_ANY;
188 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
194 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
195 if (addr->sa_family == AF_INET6) {
196 struct ipv6_mreq mreq6;
198 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
200 mreq6.ipv6mr_interface= 0;
212 #ifdef IP_DROP_MEMBERSHIP
213 if (addr->sa_family == AF_INET) {
216 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
218 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
220 mreq.imr_interface.s_addr= INADDR_ANY;
221 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
227 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
228 if (addr->sa_family == AF_INET6) {
229 struct ipv6_mreq mreq6;
231 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
233 mreq6.ipv6mr_interface= 0;
244 int sockfd,
struct sockaddr *addr,
247 int nb_sources,
int include)
250 if (addr->sa_family != AF_INET) {
251 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE)
256 for (
i = 0;
i < nb_sources;
i++) {
257 struct group_source_req mreqs;
258 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
261 mreqs.gsr_interface = 0;
262 memcpy(&mreqs.gsr_group, addr, addr_len);
265 if (setsockopt(sockfd,
level,
266 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
267 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
278 "Setting multicast sources only supported for IPv4\n");
282 #if HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
283 for (
i = 0;
i < nb_sources;
i++) {
284 struct ip_mreq_source mreqs;
285 if (
sources[
i].ss_family != AF_INET) {
290 mreqs.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
292 mreqs.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
294 mreqs.imr_interface.s_addr= INADDR_ANY;
295 mreqs.imr_sourceaddr.s_addr = ((
struct sockaddr_in *)&
sources[
i])->sin_addr.s_addr;
297 if (setsockopt(sockfd, IPPROTO_IP,
298 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
299 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
314 const char *hostname,
int port)
320 if (!res0)
return AVERROR(EIO);
329 socklen_t *addr_len,
const char *localaddr)
334 int family = AF_UNSPEC;
336 if (((
struct sockaddr *) &
s->dest_addr)->sa_family)
337 family = ((
struct sockaddr *) &
s->dest_addr)->sa_family;
343 for (res = res0; res; res=res->
ai_next) {
344 if (
s->udplite_coverage)
348 if (udp_fd != -1)
break;
372 char sbuf[
sizeof(
int)*3+1];
380 return strtol(sbuf,
NULL, 10);
403 char hostname[256],
buf[10];
411 if (
s->dest_addr_len < 0) {
415 p = strchr(uri,
'?');
418 int was_connected =
s->is_connected;
419 s->is_connected = strtol(
buf,
NULL, 10);
420 if (
s->is_connected && !was_connected) {
421 if (connect(
s->udp_fd, (
struct sockaddr *) &
s->dest_addr,
442 return s->local_port;
456 #if HAVE_PTHREAD_CANCEL
457 static void *circular_buffer_task_rx(
void *_URLContext)
463 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
467 s->circular_buffer_error =
AVERROR(EIO);
473 socklen_t addr_len =
sizeof(addr);
479 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
480 len = recvfrom(
s->udp_fd,
s->tmp+4,
sizeof(
s->tmp)-4, 0, (
struct sockaddr *)&addr, &addr_len);
481 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
496 if (
s->overrun_nonfatal) {
498 "Surviving due to overrun_nonfatal option\n");
502 "To avoid, increase fifo_size URL option. "
503 "To survive in such case, use overrun_nonfatal option\n");
504 s->circular_buffer_error =
AVERROR(EIO);
518 static void *circular_buffer_task_tx(
void *_URLContext)
525 int64_t sent_bits = 0;
526 int64_t burst_interval =
s->bitrate ? (
s->burst_bits * 1000000 /
s->bitrate) : 0;
527 int64_t max_delay =
s->bitrate ? ((int64_t)
h->max_packet_size * 8 * 1000000 /
s->bitrate + 1) : 0;
529 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
534 s->circular_buffer_error =
AVERROR(EIO);
564 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
568 if (timestamp < target_timestamp) {
569 int64_t delay = target_timestamp - timestamp;
570 if (delay > max_delay) {
572 start_timestamp = timestamp + delay;
577 if (timestamp - burst_interval > target_timestamp) {
578 start_timestamp = timestamp - burst_interval;
582 sent_bits +=
len * 8;
583 target_timestamp = start_timestamp + sent_bits * 1000000 /
s->bitrate;
590 if (!
s->is_connected) {
591 ret = sendto (
s->udp_fd, p,
len, 0,
592 (
struct sockaddr *) &
s->dest_addr,
595 ret = send(
s->udp_fd, p,
len, 0);
603 s->circular_buffer_error =
ret;
610 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
626 char hostname[1024], localaddr[1024] =
"";
627 int port, udp_fd = -1,
tmp, bind_ret = -1, dscp = -1;
638 if (
s->buffer_size < 0)
652 h->max_packet_size =
s->pkt_size;
654 p = strchr(uri,
'?');
658 s->reuse_socket = strtol(
buf, &endptr, 10);
665 s->overrun_nonfatal = strtol(
buf, &endptr, 10);
668 s->overrun_nonfatal = 1;
669 if (!HAVE_PTHREAD_CANCEL)
671 "'overrun_nonfatal' option was set but it is not supported "
672 "on this build (pthread support is required)\n");
678 s->udplite_coverage = strtol(
buf,
NULL, 10);
681 s->local_port = strtol(
buf,
NULL, 10);
684 s->pkt_size = strtol(
buf,
NULL, 10);
687 s->buffer_size = strtol(
buf,
NULL, 10);
690 s->is_connected = strtol(
buf,
NULL, 10);
696 s->circular_buffer_size = strtol(
buf,
NULL, 10);
697 if (!HAVE_PTHREAD_CANCEL)
699 "'circular_buffer_size' option was set but it is not supported "
700 "on this build (pthread support is required)\n");
703 s->bitrate = strtoll(
buf,
NULL, 10);
704 if (!HAVE_PTHREAD_CANCEL)
706 "'bitrate' option was set but it is not supported "
707 "on this build (pthread support is required)\n");
710 s->burst_bits = strtoll(
buf,
NULL, 10);
726 s->is_broadcast = strtol(
buf,
NULL, 10);
729 s->circular_buffer_size *= 188;
731 h->max_packet_size =
s->pkt_size;
735 h->rw_timeout =
s->timeout;
741 if (hostname[0] ==
'\0' || hostname[0] ==
'?') {
751 s->local_port = port;
760 s->local_addr_storage=my_addr;
765 if (
s->reuse_socket > 0 || (
s->is_multicast &&
s->reuse_socket < 0)) {
767 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(
s->reuse_socket),
sizeof(
s->reuse_socket)) != 0)
771 if (
s->is_broadcast) {
773 if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(
s->is_broadcast),
sizeof(
s->is_broadcast)) != 0)
782 if (
s->udplite_coverage) {
792 if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp,
sizeof(dscp)) != 0)
801 bind_ret = bind(udp_fd,(
struct sockaddr *)&
s->dest_addr,
len);
806 if (bind_ret < 0 && bind(udp_fd,(
struct sockaddr *)&my_addr,
len) < 0) {
811 len =
sizeof(my_addr);
812 getsockname(udp_fd, (
struct sockaddr *)&my_addr, &
len);
815 if (
s->is_multicast) {
823 if (
s->filters.nb_include_addrs) {
825 (
struct sockaddr *)&
s->dest_addr,
826 s->dest_addr_len, &
s->local_addr_storage,
827 s->filters.include_addrs,
828 s->filters.nb_include_addrs, 1) < 0)
834 if (
s->filters.nb_exclude_addrs) {
836 (
struct sockaddr *)&
s->dest_addr,
837 s->dest_addr_len, &
s->local_addr_storage,
838 s->filters.exclude_addrs,
839 s->filters.nb_exclude_addrs, 0) < 0)
847 tmp =
s->buffer_size;
848 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &
tmp,
sizeof(
tmp)) < 0) {
854 tmp =
s->buffer_size;
855 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &
tmp,
sizeof(
tmp)) < 0) {
859 if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &
tmp, &
len) < 0) {
863 if(tmp < s->buffer_size)
864 av_log(
h,
AV_LOG_WARNING,
"attempted to set receive buffer to size %d but it only ended up set as %d",
s->buffer_size,
tmp);
870 if (
s->is_connected) {
871 if (connect(udp_fd, (
struct sockaddr *) &
s->dest_addr,
s->dest_addr_len)) {
879 #if HAVE_PTHREAD_CANCEL
886 if (is_output &&
s->bitrate && !
s->circular_buffer_size) {
888 av_log(
h,
AV_LOG_WARNING,
"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
891 if ((!is_output &&
s->circular_buffer_size) || (is_output &&
s->bitrate &&
s->circular_buffer_size)) {
906 ret =
pthread_create(&
s->circular_buffer_thread,
NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx,
h);
911 s->thread_started = 1;
916 #if HAVE_PTHREAD_CANCEL
945 socklen_t addr_len =
sizeof(addr);
946 #if HAVE_PTHREAD_CANCEL
967 }
else if(
s->circular_buffer_error){
968 int err =
s->circular_buffer_error;
971 }
else if(nonblock) {
979 struct timespec tv = { .tv_sec = t / 1000000,
980 .tv_nsec = (t % 1000000) * 1000 };
981 if (pthread_cond_timedwait(&
s->cond, &
s->mutex, &tv) < 0) {
983 return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
996 ret = recvfrom(
s->udp_fd,
buf,
size, 0, (
struct sockaddr *)&addr, &addr_len);
1009 #if HAVE_PTHREAD_CANCEL
1019 if (
s->circular_buffer_error<0) {
1020 int err=
s->circular_buffer_error;
1044 if (!
s->is_connected) {
1046 (
struct sockaddr *) &
s->dest_addr,
1058 #if HAVE_PTHREAD_CANCEL
1070 #if HAVE_PTHREAD_CANCEL
1071 if (
s->thread_started) {
1075 pthread_cancel(
s->circular_buffer_thread);
1083 closesocket(
s->udp_fd);