27 #define _DEFAULT_SOURCE
51 #define UDPLITE_SEND_CSCOV 10
52 #define UDPLITE_RECV_CSCOV 11
55 #ifndef IPPROTO_UDPLITE
56 #define IPPROTO_UDPLITE 136
59 #if HAVE_PTHREAD_CANCEL
63 #ifndef HAVE_PTHREAD_CANCEL
64 #define HAVE_PTHREAD_CANCEL 0
67 #ifndef IPV6_ADD_MEMBERSHIP
68 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
69 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
72 #define UDP_TX_BUF_SIZE 32768
73 #define UDP_MAX_PKT_SIZE 65536
74 #define UDP_HEADER_SIZE 8
99 #if HAVE_PTHREAD_CANCEL
114 #define OFFSET(x) offsetof(UDPContext, x)
115 #define D AV_OPT_FLAG_DECODING_PARAM
116 #define E AV_OPT_FLAG_ENCODING_PARAM
118 {
"buffer_size",
"System data size (in bytes)",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags =
D|
E },
119 {
"bitrate",
"Bits to send per second",
OFFSET(bitrate),
AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags =
E },
120 {
"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 },
124 {
"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 },
125 {
"pkt_size",
"Maximum UDP packet size",
OFFSET(pkt_size),
AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags =
D|
E },
126 {
"reuse",
"explicitly allow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
D|
E },
127 {
"reuse_socket",
"explicitly allow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags =
D|
E },
128 {
"broadcast",
"explicitly allow or disallow broadcast destination",
OFFSET(is_broadcast),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
E },
130 {
"connect",
"set if connect() should be called on socket",
OFFSET(is_connected),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags =
D|
E },
131 {
"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 },
132 {
"overrun_nonfatal",
"survive in case of UDP receiving circular buffer overrun",
OFFSET(overrun_nonfatal),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
D },
133 {
"timeout",
"set raise error timeout (only in read mode)",
OFFSET(timeout),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
D },
157 av_log(ctx, level,
"%s: %s\n", prefix, errbuf);
161 struct sockaddr *addr)
163 #ifdef IP_MULTICAST_TTL
164 if (addr->sa_family == AF_INET) {
165 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL,
sizeof(mcastTTL)) < 0) {
171 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
172 if (addr->sa_family == AF_INET6) {
173 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL,
sizeof(mcastTTL)) < 0) {
184 #ifdef IP_ADD_MEMBERSHIP
185 if (addr->sa_family == AF_INET) {
188 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
190 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
192 mreq.imr_interface.s_addr= INADDR_ANY;
193 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
199 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
200 if (addr->sa_family == AF_INET6) {
201 struct ipv6_mreq mreq6;
203 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
204 mreq6.ipv6mr_interface= 0;
216 #ifdef IP_DROP_MEMBERSHIP
217 if (addr->sa_family == AF_INET) {
220 mreq.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
222 mreq.imr_interface= ((
struct sockaddr_in *)local_addr)->sin_addr;
224 mreq.imr_interface.s_addr= INADDR_ANY;
225 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (
const void *)&mreq,
sizeof(mreq)) < 0) {
231 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
232 if (addr->sa_family == AF_INET6) {
233 struct ipv6_mreq mreq6;
235 memcpy(&mreq6.ipv6mr_multiaddr, &(((
struct sockaddr_in6 *)addr)->sin6_addr),
sizeof(
struct in6_addr));
236 mreq6.ipv6mr_interface= 0;
247 const char *hostname,
int port,
250 struct addrinfo hints = { 0 }, *res = 0;
253 const char *node = 0, *service =
"0";
256 snprintf(sport,
sizeof(sport),
"%d", port);
259 if ((hostname) && (hostname[0] !=
'\0') && (hostname[0] !=
'?')) {
265 if ((error =
getaddrinfo(node, service, &hints, &res))) {
268 node ? node :
"unknown",
277 int sockfd,
struct sockaddr *addr,
278 int addr_len,
char **sources,
279 int nb_sources,
int include)
281 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
285 for (i = 0; i < nb_sources; i++) {
286 struct group_source_req mreqs;
287 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
289 SOCK_DGRAM, AF_UNSPEC,
294 mreqs.gsr_interface = 0;
295 memcpy(&mreqs.gsr_group, addr, addr_len);
299 if (setsockopt(sockfd, level,
300 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
301 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
309 #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
311 if (addr->sa_family != AF_INET) {
313 "Setting multicast sources only supported for IPv4\n");
316 for (i = 0; i < nb_sources; i++) {
317 struct ip_mreq_source mreqs;
319 SOCK_DGRAM, AF_UNSPEC,
323 if (sourceaddr->
ai_addr->sa_family != AF_INET) {
330 mreqs.imr_multiaddr.s_addr = ((
struct sockaddr_in *)addr)->sin_addr.s_addr;
331 mreqs.imr_interface.s_addr = INADDR_ANY;
332 mreqs.imr_sourceaddr.s_addr = ((
struct sockaddr_in *)sourceaddr->
ai_addr)->sin_addr.s_addr;
335 if (setsockopt(sockfd, IPPROTO_IP,
336 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
337 (
const void *)&mreqs,
sizeof(mreqs)) < 0) {
352 const char *hostname,
int port)
358 if (!res0)
return AVERROR(EIO);
367 socklen_t *addr_len,
const char *localaddr)
372 int family = AF_UNSPEC;
374 if (((
struct sockaddr *) &s->
dest_addr)->sa_family)
375 family = ((
struct sockaddr *) &s->
dest_addr)->sa_family;
381 for (res = res0; res; res=res->
ai_next) {
386 if (udp_fd != -1)
break;
410 char sbuf[
sizeof(int)*3+1];
418 return strtol(sbuf,
NULL, 10);
441 char hostname[256],
buf[10];
453 p = strchr(uri,
'?');
494 #if HAVE_PTHREAD_CANCEL
495 static void *circular_buffer_task_rx(
void *_URLContext)
501 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
515 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
517 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
532 "Surviving due to overrun_nonfatal option\n");
536 "To avoid, increase fifo_size URL option. "
537 "To survive in such case, use overrun_nonfatal option\n");
552 static void *circular_buffer_task_tx(
void *_URLContext)
559 int64_t sent_bits = 0;
563 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
598 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
602 if (timestamp < target_timestamp) {
603 int64_t delay = target_timestamp - timestamp;
604 if (delay > max_delay) {
606 start_timestamp = timestamp + delay;
611 if (timestamp - burst_interval > target_timestamp) {
612 start_timestamp = timestamp - burst_interval;
616 sent_bits += len * 8;
617 target_timestamp = start_timestamp + sent_bits * 1000000 / s->
bitrate;
625 ret = sendto (s->
udp_fd, p, len, 0,
629 ret = send(s->
udp_fd, p, len, 0);
644 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
663 char *next = strchr(source_start,
',');
666 sources[*num_sources] =
av_strdup(source_start);
667 if (!sources[*num_sources])
669 source_start = next + 1;
671 if (*num_sources >= max_sources || !next)
681 char hostname[1024], localaddr[1024] =
"";
682 int port, udp_fd = -1,
tmp, bind_ret = -1, dscp = -1;
689 int i, num_include_sources = 0, num_exclude_sources = 0;
690 char *include_sources[32], *exclude_sources[32];
700 &num_include_sources,
714 p = strchr(uri,
'?');
731 "'overrun_nonfatal' option was set but it is not supported "
732 "on this build (pthread support is required)\n");
735 s->
ttl = strtol(buf,
NULL, 10);
753 dscp = strtol(buf,
NULL, 10);
759 "'circular_buffer_size' option was set but it is not supported "
760 "on this build (pthread support is required)\n");
766 "'bitrate' option was set but it is not supported "
767 "on this build (pthread support is required)\n");
773 av_strlcpy(localaddr, buf,
sizeof(localaddr));
803 if (hostname[0] ==
'\0' || hostname[0] ==
'?') {
854 if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp,
sizeof(dscp)) != 0)
863 bind_ret = bind(udp_fd,(
struct sockaddr *)&s->
dest_addr, len);
868 if (bind_ret < 0 && bind(udp_fd,(
struct sockaddr *)&my_addr, len) < 0) {
873 len =
sizeof(my_addr);
874 getsockname(udp_fd, (
struct sockaddr *)&my_addr, &len);
878 if (h->
flags & AVIO_FLAG_WRITE) {
885 if (num_include_sources && num_exclude_sources) {
886 av_log(h,
AV_LOG_ERROR,
"Simultaneously including and excluding multicast sources is not supported\n");
889 if (num_include_sources) {
894 num_include_sources, 1) < 0)
900 if (num_exclude_sources) {
905 num_exclude_sources, 0) < 0)
914 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp,
sizeof(tmp)) < 0) {
921 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp,
sizeof(tmp)) < 0) {
925 if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
929 if(tmp < s->buffer_size)
943 for (i = 0; i < num_include_sources; i++)
945 for (i = 0; i < num_exclude_sources; i++)
950 #if HAVE_PTHREAD_CANCEL
959 av_log(h,
AV_LOG_WARNING,
"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
977 ret =
pthread_create(&s->circular_buffer_thread,
NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
982 s->thread_started = 1;
987 #if HAVE_PTHREAD_CANCEL
997 for (i = 0; i < num_include_sources; i++)
999 for (i = 0; i < num_exclude_sources; i++)
1018 #if HAVE_PTHREAD_CANCEL
1043 }
else if(nonblock) {
1051 struct timespec tv = { .tv_sec = t / 1000000,
1052 .tv_nsec = (t % 1000000) * 1000 };
1053 if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
1055 return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
1068 ret = recv(s->
udp_fd, buf, size, 0);
1078 #if HAVE_PTHREAD_CANCEL
1114 ret = sendto (s->
udp_fd, buf, size, 0,
1118 ret = send(s->
udp_fd, buf, size, 0);
1127 #if HAVE_PTHREAD_CANCEL
1139 #if HAVE_PTHREAD_CANCEL
1140 if (s->thread_started) {
1144 pthread_cancel(s->circular_buffer_thread);
1165 .priv_data_class = &udp_class,
1177 .priv_data_class = &udplite_context_class,
static int udp_open(URLContext *h, const char *uri, int flags)
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
static int udp_set_multicast_sources(URLContext *h, int sockfd, struct sockaddr *addr, int addr_len, char **sources, int nb_sources, int include)
static const AVClass udplite_context_class
static void log_net_error(void *ctx, int level, const char *prefix)
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
#define URL_PROTOCOL_FLAG_NETWORK
#define HAVE_PTHREAD_CANCEL
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
const URLProtocol ff_udplite_protocol
int is_streamed
true if streamed (no seek possible), default = false
static int parse_source_list(char *buf, char **sources, int *num_sources, int max_sources)
#define AVIO_FLAG_READ
read-only
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in mcs
static int udp_set_url(URLContext *h, struct sockaddr_storage *addr, const char *hostname, int port)
#define AVIO_FLAG_WRITE
write-only
#define UDPLITE_SEND_CSCOV
int av_usleep(unsigned usec)
Sleep for a period of time.
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
int ff_socket(int af, int type, int proto)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define UDPLITE_RECV_CSCOV
miscellaneous OS support macros and functions.
int ff_udp_get_local_port(URLContext *h)
Return the local port used by the UDP connection.
static av_cold int end(AVCodecContext *avctx)
int av_fifo_space(const AVFifoBuffer *f)
Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write int...
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr)
static int udp_port(struct sockaddr_storage *addr, int addr_len)
int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
Attempt to find a specific tag in a URL.
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
static int udp_close(URLContext *h)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ff_udp_set_remote_url(URLContext *h, const char *uri)
If no filename is given to av_open_input_file because you want to get the local port first...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
simple assert() macros that are a bit more flexible than ISO C assert().
static struct addrinfo * udp_resolve_host(URLContext *h, const char *hostname, int port, int type, int family, int flags)
static int max_delay(struct NCSofa *sofa)
int ff_is_multicast_address(struct sockaddr *addr)
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
static const AVOption options[]
uint8_t tmp[UDP_MAX_PKT_SIZE+4]
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
static int udp_get_file_handle(URLContext *h)
Return the udp file handle for select() usage to wait for several RTP streams at the same time...
struct sockaddr_storage local_addr_storage
static pthread_mutex_t * mutex
int ff_socket_nonblock(int socket, int enable)
#define FF_ARRAY_ELEMS(a)
int64_t av_gettime(void)
Get the current time in microseconds.
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
#define IPV6_ADD_MEMBERSHIP
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
char * av_strdup(const char *s)
Duplicate the string s.
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
#define IPV6_DROP_MEMBERSHIP
a very simple circular buffer FIFO implementation
Describe the class of an AVClass context structure.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
struct sockaddr_storage dest_addr
static int udp_read(URLContext *h, uint8_t *buf, int size)
struct addrinfo * ai_next
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr)
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
static int udplite_open(URLContext *h, const char *uri, int flags)
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr)
int circular_buffer_error
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len, const char *localaddr)
int max_packet_size
if non zero, the stream is packetized with this max packet size
static const AVClass udp_class
int ff_network_wait_fd(int fd, int write)
unbuffered private I/O API
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
struct sockaddr * ai_addr
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
static int udp_write(URLContext *h, const uint8_t *buf, int size)
const URLProtocol ff_udp_protocol