FFmpeg
udp.c
Go to the documentation of this file.
1 /*
2  * UDP prototype streaming system
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * UDP protocol
25  */
26 
27 #define _DEFAULT_SOURCE
28 #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
29 
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/fifo.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/log.h"
39 #include "libavutil/time.h"
40 #include "internal.h"
41 #include "network.h"
42 #include "os_support.h"
43 #include "url.h"
44 #include "ip.h"
45 
46 #ifdef __APPLE__
47 #include "TargetConditionals.h"
48 #endif
49 
50 #if HAVE_UDPLITE_H
51 #include "udplite.h"
52 #else
53 /* On many Linux systems, udplite.h is missing but the kernel supports UDP-Lite.
54  * So, we provide a fallback here.
55  */
56 #define UDPLITE_SEND_CSCOV 10
57 #define UDPLITE_RECV_CSCOV 11
58 #endif
59 
60 #ifndef IPPROTO_UDPLITE
61 #define IPPROTO_UDPLITE 136
62 #endif
63 
64 #if HAVE_PTHREAD_CANCEL
65 #include <pthread.h>
66 #endif
67 
68 #ifndef IPV6_ADD_MEMBERSHIP
69 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
70 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
71 #endif
72 
73 #define UDP_TX_BUF_SIZE 32768
74 #define UDP_RX_BUF_SIZE 393216
75 #define UDP_MAX_PKT_SIZE 65536
76 #define UDP_HEADER_SIZE 8
77 
78 typedef struct UDPContext {
79  const AVClass *class;
80  int udp_fd;
81  int ttl;
84  int pkt_size;
93 
94  /* Circular Buffer variables for use in UDP receive code */
98  int64_t bitrate; /* number of bits to send per second */
99  int64_t burst_bits;
101 #if HAVE_PTHREAD_CANCEL
102  pthread_t circular_buffer_thread;
105  int thread_started;
106 #endif
109  char *localaddr;
110  int timeout;
112  char *sources;
113  char *block;
115 } UDPContext;
116 
117 #define OFFSET(x) offsetof(UDPContext, x)
118 #define D AV_OPT_FLAG_DECODING_PARAM
119 #define E AV_OPT_FLAG_ENCODING_PARAM
120 static const AVOption options[] = {
121  { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
122  { "bitrate", "Bits to send per second", OFFSET(bitrate), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
123  { "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  { "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E },
125  { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
126  { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
127  { "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 },
128  { "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E },
129  { "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D|E },
130  { "reuse_socket", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags = D|E },
131  { "broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
132  { "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, E },
133  { "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
134  { "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 },
135  { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D },
136  { "timeout", "set raise error timeout (only in read mode)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
137  { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
138  { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
139  { NULL }
140 };
141 
142 static const AVClass udp_class = {
143  .class_name = "udp",
144  .item_name = av_default_item_name,
145  .option = options,
146  .version = LIBAVUTIL_VERSION_INT,
147 };
148 
150  .class_name = "udplite",
151  .item_name = av_default_item_name,
152  .option = options,
153  .version = LIBAVUTIL_VERSION_INT,
154 };
155 
156 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
157  struct sockaddr *addr)
158 {
159 #ifdef IP_MULTICAST_TTL
160  if (addr->sa_family == AF_INET) {
161  if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
162  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
163  return -1;
164  }
165  }
166 #endif
167 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
168  if (addr->sa_family == AF_INET6) {
169  if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
170  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
171  return -1;
172  }
173  }
174 #endif
175  return 0;
176 }
177 
178 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
179 {
180 #ifdef IP_ADD_MEMBERSHIP
181  if (addr->sa_family == AF_INET) {
182  struct ip_mreq mreq;
183 
184  mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
185  if (local_addr)
186  mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
187  else
188  mreq.imr_interface.s_addr= INADDR_ANY;
189  if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
190  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
191  return -1;
192  }
193  }
194 #endif
195 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
196  if (addr->sa_family == AF_INET6) {
197  struct ipv6_mreq mreq6;
198 
199  memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
200  //TODO: Interface index should be looked up from local_addr
201  mreq6.ipv6mr_interface= 0;
202  if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
203  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
204  return -1;
205  }
206  }
207 #endif
208  return 0;
209 }
210 
211 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
212 {
213 #ifdef IP_DROP_MEMBERSHIP
214  if (addr->sa_family == AF_INET) {
215  struct ip_mreq mreq;
216 
217  mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
218  if (local_addr)
219  mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
220  else
221  mreq.imr_interface.s_addr= INADDR_ANY;
222  if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
223  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
224  return -1;
225  }
226  }
227 #endif
228 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
229  if (addr->sa_family == AF_INET6) {
230  struct ipv6_mreq mreq6;
231 
232  memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
233  //TODO: Interface index should be looked up from local_addr
234  mreq6.ipv6mr_interface= 0;
235  if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
236  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
237  return -1;
238  }
239  }
240 #endif
241  return 0;
242 }
243 
245  int sockfd, struct sockaddr *addr,
246  int addr_len, struct sockaddr_storage *local_addr,
247  struct sockaddr_storage *sources,
248  int nb_sources, int include)
249 {
250  int i;
251  if (addr->sa_family != AF_INET) {
252 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE)
253  /* For IPv4 prefer the old approach, as that alone works reliably on
254  * Windows and it also supports supplying the interface based on its
255  * address. */
256  int i;
257  for (i = 0; i < nb_sources; i++) {
258  struct group_source_req mreqs;
259  int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
260 
261  //TODO: Interface index should be looked up from local_addr
262  mreqs.gsr_interface = 0;
263  memcpy(&mreqs.gsr_group, addr, addr_len);
264  memcpy(&mreqs.gsr_source, &sources[i], sizeof(*sources));
265 
266  if (setsockopt(sockfd, level,
267  include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
268  (const void *)&mreqs, sizeof(mreqs)) < 0) {
269  if (include)
270  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
271  else
272  ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
273  return ff_neterrno();
274  }
275  }
276  return 0;
277 #else
278  av_log(h, AV_LOG_ERROR,
279  "Setting multicast sources only supported for IPv4\n");
280  return AVERROR(EINVAL);
281 #endif
282  }
283 #if HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
284  for (i = 0; i < nb_sources; i++) {
285  struct ip_mreq_source mreqs;
286  if (sources[i].ss_family != AF_INET) {
287  av_log(h, AV_LOG_ERROR, "Source/block address %d is of incorrect protocol family\n", i + 1);
288  return AVERROR(EINVAL);
289  }
290 
291  mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
292  if (local_addr)
293  mreqs.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
294  else
295  mreqs.imr_interface.s_addr= INADDR_ANY;
296  mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)&sources[i])->sin_addr.s_addr;
297 
298  if (setsockopt(sockfd, IPPROTO_IP,
299  include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
300  (const void *)&mreqs, sizeof(mreqs)) < 0) {
301  if (include)
302  ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
303  else
304  ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
305  return ff_neterrno();
306  }
307  }
308 #else
309  return AVERROR(ENOSYS);
310 #endif
311  return 0;
312 }
314  struct sockaddr_storage *addr,
315  const char *hostname, int port)
316 {
317  struct addrinfo *res0;
318  int addr_len;
319 
320  res0 = ff_ip_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
321  if (!res0) return AVERROR(EIO);
322  memcpy(addr, res0->ai_addr, res0->ai_addrlen);
323  addr_len = res0->ai_addrlen;
324  freeaddrinfo(res0);
325 
326  return addr_len;
327 }
328 
329 static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
330  socklen_t *addr_len, const char *localaddr)
331 {
332  UDPContext *s = h->priv_data;
333  int udp_fd = -1;
334  struct addrinfo *res0, *res;
335  int family = AF_UNSPEC;
336 
337  if (((struct sockaddr *) &s->dest_addr)->sa_family)
338  family = ((struct sockaddr *) &s->dest_addr)->sa_family;
339  res0 = ff_ip_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
340  s->local_port,
341  SOCK_DGRAM, family, AI_PASSIVE);
342  if (!res0)
343  goto fail;
344  for (res = res0; res; res=res->ai_next) {
345  if (s->udplite_coverage)
346  udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, IPPROTO_UDPLITE);
347  else
348  udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
349  if (udp_fd != -1) break;
350  ff_log_net_error(NULL, AV_LOG_ERROR, "socket");
351  }
352 
353  if (udp_fd < 0)
354  goto fail;
355 
356  memcpy(addr, res->ai_addr, res->ai_addrlen);
357  *addr_len = res->ai_addrlen;
358 
359  freeaddrinfo(res0);
360 
361  return udp_fd;
362 
363  fail:
364  if (udp_fd >= 0)
365  closesocket(udp_fd);
366  if(res0)
367  freeaddrinfo(res0);
368  return -1;
369 }
370 
371 static int udp_port(struct sockaddr_storage *addr, int addr_len)
372 {
373  char sbuf[sizeof(int)*3+1];
374  int error;
375 
376  if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
377  av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
378  return -1;
379  }
380 
381  return strtol(sbuf, NULL, 10);
382 }
383 
384 
385 /**
386  * If no filename is given to av_open_input_file because you want to
387  * get the local port first, then you must call this function to set
388  * the remote server address.
389  *
390  * url syntax: udp://host:port[?option=val...]
391  * option: 'ttl=n' : set the ttl value (for multicast only)
392  * 'localport=n' : set the local port
393  * 'pkt_size=n' : set max packet size
394  * 'reuse=1' : enable reusing the socket
395  * 'overrun_nonfatal=1': survive in case of circular buffer overrun
396  *
397  * @param h media file context
398  * @param uri of the remote server
399  * @return zero if no error.
400  */
401 int ff_udp_set_remote_url(URLContext *h, const char *uri)
402 {
403  UDPContext *s = h->priv_data;
404  char hostname[256], buf[10];
405  int port;
406  const char *p;
407 
408  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
409 
410  /* set the destination address */
411  s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
412  if (s->dest_addr_len < 0) {
413  return AVERROR(EIO);
414  }
415  s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
416  p = strchr(uri, '?');
417  if (p) {
418  if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
419  int was_connected = s->is_connected;
420  s->is_connected = strtol(buf, NULL, 10);
421  if (s->is_connected && !was_connected) {
422  if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
423  s->dest_addr_len)) {
424  s->is_connected = 0;
425  ff_log_net_error(h, AV_LOG_ERROR, "connect");
426  return AVERROR(EIO);
427  }
428  }
429  }
430  }
431 
432  return 0;
433 }
434 
435 /**
436  * Return the local port used by the UDP connection
437  * @param h media file context
438  * @return the local port number
439  */
441 {
442  UDPContext *s = h->priv_data;
443  return s->local_port;
444 }
445 
446 /**
447  * Return the udp file handle for select() usage to wait for several RTP
448  * streams at the same time.
449  * @param h media file context
450  */
452 {
453  UDPContext *s = h->priv_data;
454  return s->udp_fd;
455 }
456 
457 #if HAVE_PTHREAD_CANCEL
458 static void *circular_buffer_task_rx( void *_URLContext)
459 {
460  URLContext *h = _URLContext;
461  UDPContext *s = h->priv_data;
462  int old_cancelstate;
463 
464  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
465  pthread_mutex_lock(&s->mutex);
466  if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
467  av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
468  s->circular_buffer_error = AVERROR(EIO);
469  goto end;
470  }
471  while(1) {
472  int len;
473  struct sockaddr_storage addr;
474  socklen_t addr_len = sizeof(addr);
475 
476  pthread_mutex_unlock(&s->mutex);
477  /* Blocking operations are always cancellation points;
478  see "General Information" / "Thread Cancelation Overview"
479  in Single Unix. */
480  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
481  len = recvfrom(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0, (struct sockaddr *)&addr, &addr_len);
482  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
483  pthread_mutex_lock(&s->mutex);
484  if (len < 0) {
485  if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
487  goto end;
488  }
489  continue;
490  }
491  if (ff_ip_check_source_lists(&addr, &s->filters))
492  continue;
493  AV_WL32(s->tmp, len);
494 
495  if(av_fifo_space(s->fifo) < len + 4) {
496  /* No Space left */
497  if (s->overrun_nonfatal) {
498  av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
499  "Surviving due to overrun_nonfatal option\n");
500  continue;
501  } else {
502  av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "
503  "To avoid, increase fifo_size URL option. "
504  "To survive in such case, use overrun_nonfatal option\n");
505  s->circular_buffer_error = AVERROR(EIO);
506  goto end;
507  }
508  }
509  av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
510  pthread_cond_signal(&s->cond);
511  }
512 
513 end:
514  pthread_cond_signal(&s->cond);
515  pthread_mutex_unlock(&s->mutex);
516  return NULL;
517 }
518 
519 static void *circular_buffer_task_tx( void *_URLContext)
520 {
521  URLContext *h = _URLContext;
522  UDPContext *s = h->priv_data;
523  int old_cancelstate;
524  int64_t target_timestamp = av_gettime_relative();
525  int64_t start_timestamp = av_gettime_relative();
526  int64_t sent_bits = 0;
527  int64_t burst_interval = s->bitrate ? (s->burst_bits * 1000000 / s->bitrate) : 0;
528  int64_t max_delay = s->bitrate ? ((int64_t)h->max_packet_size * 8 * 1000000 / s->bitrate + 1) : 0;
529 
530  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
531  pthread_mutex_lock(&s->mutex);
532 
533  if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
534  av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
535  s->circular_buffer_error = AVERROR(EIO);
536  goto end;
537  }
538 
539  for(;;) {
540  int len;
541  const uint8_t *p;
542  uint8_t tmp[4];
543  int64_t timestamp;
544 
545  len=av_fifo_size(s->fifo);
546 
547  while (len<4) {
548  if (s->close_req)
549  goto end;
550  if (pthread_cond_wait(&s->cond, &s->mutex) < 0) {
551  goto end;
552  }
553  len=av_fifo_size(s->fifo);
554  }
555 
556  av_fifo_generic_read(s->fifo, tmp, 4, NULL);
557  len=AV_RL32(tmp);
558 
559  av_assert0(len >= 0);
560  av_assert0(len <= sizeof(s->tmp));
561 
562  av_fifo_generic_read(s->fifo, s->tmp, len, NULL);
563 
564  pthread_mutex_unlock(&s->mutex);
565  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
566 
567  if (s->bitrate) {
568  timestamp = av_gettime_relative();
569  if (timestamp < target_timestamp) {
570  int64_t delay = target_timestamp - timestamp;
571  if (delay > max_delay) {
572  delay = max_delay;
573  start_timestamp = timestamp + delay;
574  sent_bits = 0;
575  }
576  av_usleep(delay);
577  } else {
578  if (timestamp - burst_interval > target_timestamp) {
579  start_timestamp = timestamp - burst_interval;
580  sent_bits = 0;
581  }
582  }
583  sent_bits += len * 8;
584  target_timestamp = start_timestamp + sent_bits * 1000000 / s->bitrate;
585  }
586 
587  p = s->tmp;
588  while (len) {
589  int ret;
590  av_assert0(len > 0);
591  if (!s->is_connected) {
592  ret = sendto (s->udp_fd, p, len, 0,
593  (struct sockaddr *) &s->dest_addr,
594  s->dest_addr_len);
595  } else
596  ret = send(s->udp_fd, p, len, 0);
597  if (ret >= 0) {
598  len -= ret;
599  p += ret;
600  } else {
601  ret = ff_neterrno();
602  if (ret != AVERROR(EAGAIN) && ret != AVERROR(EINTR)) {
603  pthread_mutex_lock(&s->mutex);
605  pthread_mutex_unlock(&s->mutex);
606  return NULL;
607  }
608  }
609  }
610 
611  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
612  pthread_mutex_lock(&s->mutex);
613  }
614 
615 end:
616  pthread_mutex_unlock(&s->mutex);
617  return NULL;
618 }
619 
620 
621 #endif
622 
623 /* put it in UDP context */
624 /* return non zero if error */
625 static int udp_open(URLContext *h, const char *uri, int flags)
626 {
627  char hostname[1024], localaddr[1024] = "";
628  int port, udp_fd = -1, tmp, bind_ret = -1, dscp = -1;
629  UDPContext *s = h->priv_data;
630  int is_output;
631  const char *p;
632  char buf[256];
633  struct sockaddr_storage my_addr;
634  socklen_t len;
635 
636  h->is_streamed = 1;
637 
638  is_output = !(flags & AVIO_FLAG_READ);
639  if (s->buffer_size < 0)
640  s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_RX_BUF_SIZE;
641 
642  if (s->sources) {
643  if (ff_ip_parse_sources(h, s->sources, &s->filters) < 0)
644  goto fail;
645  }
646 
647  if (s->block) {
648  if (ff_ip_parse_blocks(h, s->block, &s->filters) < 0)
649  goto fail;
650  }
651 
652  if (s->pkt_size > 0)
653  h->max_packet_size = s->pkt_size;
654 
655  p = strchr(uri, '?');
656  if (p) {
657  if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
658  char *endptr = NULL;
659  s->reuse_socket = strtol(buf, &endptr, 10);
660  /* assume if no digits were found it is a request to enable it */
661  if (buf == endptr)
662  s->reuse_socket = 1;
663  }
664  if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
665  char *endptr = NULL;
666  s->overrun_nonfatal = strtol(buf, &endptr, 10);
667  /* assume if no digits were found it is a request to enable it */
668  if (buf == endptr)
669  s->overrun_nonfatal = 1;
670  if (!HAVE_PTHREAD_CANCEL)
672  "'overrun_nonfatal' option was set but it is not supported "
673  "on this build (pthread support is required)\n");
674  }
675  if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
676  s->ttl = strtol(buf, NULL, 10);
677  }
678  if (av_find_info_tag(buf, sizeof(buf), "udplite_coverage", p)) {
679  s->udplite_coverage = strtol(buf, NULL, 10);
680  }
681  if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
682  s->local_port = strtol(buf, NULL, 10);
683  }
684  if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
685  s->pkt_size = strtol(buf, NULL, 10);
686  }
687  if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
688  s->buffer_size = strtol(buf, NULL, 10);
689  }
690  if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
691  s->is_connected = strtol(buf, NULL, 10);
692  }
693  if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
694  dscp = strtol(buf, NULL, 10);
695  }
696  if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
697  s->circular_buffer_size = strtol(buf, NULL, 10);
698  if (!HAVE_PTHREAD_CANCEL)
700  "'circular_buffer_size' option was set but it is not supported "
701  "on this build (pthread support is required)\n");
702  }
703  if (av_find_info_tag(buf, sizeof(buf), "bitrate", p)) {
704  s->bitrate = strtoll(buf, NULL, 10);
705  if (!HAVE_PTHREAD_CANCEL)
707  "'bitrate' option was set but it is not supported "
708  "on this build (pthread support is required)\n");
709  }
710  if (av_find_info_tag(buf, sizeof(buf), "burst_bits", p)) {
711  s->burst_bits = strtoll(buf, NULL, 10);
712  }
713  if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
714  av_strlcpy(localaddr, buf, sizeof(localaddr));
715  }
716  if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
717  if (ff_ip_parse_sources(h, buf, &s->filters) < 0)
718  goto fail;
719  }
720  if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
721  if (ff_ip_parse_blocks(h, buf, &s->filters) < 0)
722  goto fail;
723  }
724  if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p))
725  s->timeout = strtol(buf, NULL, 10);
726  if (is_output && av_find_info_tag(buf, sizeof(buf), "broadcast", p))
727  s->is_broadcast = strtol(buf, NULL, 10);
728  }
729  /* handling needed to support options picking from both AVOption and URL */
730  s->circular_buffer_size *= 188;
731  if (flags & AVIO_FLAG_WRITE) {
732  h->max_packet_size = s->pkt_size;
733  } else {
735  }
736  h->rw_timeout = s->timeout;
737 
738  /* fill the dest addr */
739  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
740 
741  /* XXX: fix av_url_split */
742  if (hostname[0] == '\0' || hostname[0] == '?') {
743  /* only accepts null hostname if input */
744  if (!(flags & AVIO_FLAG_READ))
745  goto fail;
746  } else {
747  if (ff_udp_set_remote_url(h, uri) < 0)
748  goto fail;
749  }
750 
751  if ((s->is_multicast || s->local_port <= 0) && (h->flags & AVIO_FLAG_READ))
752  s->local_port = port;
753 
754  if (localaddr[0])
755  udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
756  else
757  udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
758  if (udp_fd < 0)
759  goto fail;
760 
761  s->local_addr_storage=my_addr; //store for future multicast join
762 
763  /* Follow the requested reuse option, unless it's multicast in which
764  * case enable reuse unless explicitly disabled.
765  */
766  if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
767  s->reuse_socket = 1;
768  if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
769  goto fail;
770  }
771 
772  if (s->is_broadcast) {
773 #ifdef SO_BROADCAST
774  if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0)
775 #endif
776  goto fail;
777  }
778 
779  /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
780  * The receiver coverage has to be less than or equal to the sender coverage.
781  * Otherwise, the receiver will drop all packets.
782  */
783  if (s->udplite_coverage) {
784  if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
785  av_log(h, AV_LOG_WARNING, "socket option UDPLITE_SEND_CSCOV not available");
786 
787  if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
788  av_log(h, AV_LOG_WARNING, "socket option UDPLITE_RECV_CSCOV not available");
789  }
790 
791  if (dscp >= 0) {
792  dscp <<= 2;
793  if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
794  goto fail;
795  }
796 
797  /* If multicast, try binding the multicast address first, to avoid
798  * receiving UDP packets from other sources aimed at the same UDP
799  * port. This fails on windows. This makes sending to the same address
800  * using sendto() fail, so only do it if we're opened in read-only mode. */
801  if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
802  bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
803  }
804  /* bind to the local address if not multicast or if the multicast
805  * bind failed */
806  /* the bind is needed to give a port to the socket now */
807  if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
808  ff_log_net_error(h, AV_LOG_ERROR, "bind failed");
809  goto fail;
810  }
811 
812  len = sizeof(my_addr);
813  getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
814  s->local_port = udp_port(&my_addr, len);
815 
816  if (s->is_multicast) {
817  if (h->flags & AVIO_FLAG_WRITE) {
818  /* output */
819  if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
820  goto fail;
821  }
822  if (h->flags & AVIO_FLAG_READ) {
823  /* input */
824  if (s->filters.nb_include_addrs) {
825  if (udp_set_multicast_sources(h, udp_fd,
826  (struct sockaddr *)&s->dest_addr,
829  s->filters.nb_include_addrs, 1) < 0)
830  goto fail;
831  } else {
832  if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage) < 0)
833  goto fail;
834  }
835  if (s->filters.nb_exclude_addrs) {
836  if (udp_set_multicast_sources(h, udp_fd,
837  (struct sockaddr *)&s->dest_addr,
840  s->filters.nb_exclude_addrs, 0) < 0)
841  goto fail;
842  }
843  }
844  }
845 
846  if (is_output) {
847  /* limit the tx buf size to limit latency */
848  tmp = s->buffer_size;
849  if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
850  ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
851  goto fail;
852  }
853  } else {
854  /* set udp recv buffer size to the requested value (default 64K) */
855  tmp = s->buffer_size;
856  if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
857  ff_log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
858  }
859  len = sizeof(tmp);
860  if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
861  ff_log_net_error(h, AV_LOG_WARNING, "getsockopt(SO_RCVBUF)");
862  } else {
863  av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp);
864  if(tmp < s->buffer_size)
865  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);
866  }
867 
868  /* make the socket non-blocking */
869  ff_socket_nonblock(udp_fd, 1);
870  }
871  if (s->is_connected) {
872  if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
873  ff_log_net_error(h, AV_LOG_ERROR, "connect");
874  goto fail;
875  }
876  }
877 
878  s->udp_fd = udp_fd;
879 
880 #if HAVE_PTHREAD_CANCEL
881  /*
882  Create thread in case of:
883  1. Input and circular_buffer_size is set
884  2. Output and bitrate and circular_buffer_size is set
885  */
886 
887  if (is_output && s->bitrate && !s->circular_buffer_size) {
888  /* Warn user in case of 'circular_buffer_size' is not set */
889  av_log(h, AV_LOG_WARNING,"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
890  }
891 
892  if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
893  int ret;
894 
895  /* start the task going */
897  ret = pthread_mutex_init(&s->mutex, NULL);
898  if (ret != 0) {
899  av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
900  goto fail;
901  }
902  ret = pthread_cond_init(&s->cond, NULL);
903  if (ret != 0) {
904  av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
905  goto cond_fail;
906  }
907  ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
908  if (ret != 0) {
909  av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
910  goto thread_fail;
911  }
912  s->thread_started = 1;
913  }
914 #endif
915 
916  return 0;
917 #if HAVE_PTHREAD_CANCEL
918  thread_fail:
919  pthread_cond_destroy(&s->cond);
920  cond_fail:
921  pthread_mutex_destroy(&s->mutex);
922 #endif
923  fail:
924  if (udp_fd >= 0)
925  closesocket(udp_fd);
926  av_fifo_freep(&s->fifo);
928  return AVERROR(EIO);
929 }
930 
931 static int udplite_open(URLContext *h, const char *uri, int flags)
932 {
933  UDPContext *s = h->priv_data;
934 
935  // set default checksum coverage
937 
938  return udp_open(h, uri, flags);
939 }
940 
941 static int udp_read(URLContext *h, uint8_t *buf, int size)
942 {
943  UDPContext *s = h->priv_data;
944  int ret;
945  struct sockaddr_storage addr;
946  socklen_t addr_len = sizeof(addr);
947 #if HAVE_PTHREAD_CANCEL
948  int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
949 
950  if (s->fifo) {
951  pthread_mutex_lock(&s->mutex);
952  do {
953  avail = av_fifo_size(s->fifo);
954  if (avail) { // >=size) {
955  uint8_t tmp[4];
956 
957  av_fifo_generic_read(s->fifo, tmp, 4, NULL);
958  avail= AV_RL32(tmp);
959  if(avail > size){
960  av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
961  avail= size;
962  }
963 
964  av_fifo_generic_read(s->fifo, buf, avail, NULL);
965  av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
966  pthread_mutex_unlock(&s->mutex);
967  return avail;
968  } else if(s->circular_buffer_error){
969  int err = s->circular_buffer_error;
970  pthread_mutex_unlock(&s->mutex);
971  return err;
972  } else if(nonblock) {
973  pthread_mutex_unlock(&s->mutex);
974  return AVERROR(EAGAIN);
975  }
976  else {
977  /* FIXME: using the monotonic clock would be better,
978  but it does not exist on all supported platforms. */
979  int64_t t = av_gettime() + 100000;
980  struct timespec tv = { .tv_sec = t / 1000000,
981  .tv_nsec = (t % 1000000) * 1000 };
982  int err = pthread_cond_timedwait(&s->cond, &s->mutex, &tv);
983  if (err) {
984  pthread_mutex_unlock(&s->mutex);
985  return AVERROR(err == ETIMEDOUT ? EAGAIN : err);
986  }
987  nonblock = 1;
988  }
989  } while( 1);
990  }
991 #endif
992 
993  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
994  ret = ff_network_wait_fd(s->udp_fd, 0);
995  if (ret < 0)
996  return ret;
997  }
998  ret = recvfrom(s->udp_fd, buf, size, 0, (struct sockaddr *)&addr, &addr_len);
999  if (ret < 0)
1000  return ff_neterrno();
1001  if (ff_ip_check_source_lists(&addr, &s->filters))
1002  return AVERROR(EINTR);
1003  return ret;
1004 }
1005 
1006 static int udp_write(URLContext *h, const uint8_t *buf, int size)
1007 {
1008  UDPContext *s = h->priv_data;
1009  int ret;
1010 
1011 #if HAVE_PTHREAD_CANCEL
1012  if (s->fifo) {
1013  uint8_t tmp[4];
1014 
1015  pthread_mutex_lock(&s->mutex);
1016 
1017  /*
1018  Return error if last tx failed.
1019  Here we can't know on which packet error was, but it needs to know that error exists.
1020  */
1021  if (s->circular_buffer_error<0) {
1022  int err=s->circular_buffer_error;
1023  pthread_mutex_unlock(&s->mutex);
1024  return err;
1025  }
1026 
1027  if(av_fifo_space(s->fifo) < size + 4) {
1028  /* What about a partial packet tx ? */
1029  pthread_mutex_unlock(&s->mutex);
1030  return AVERROR(ENOMEM);
1031  }
1032  AV_WL32(tmp, size);
1033  av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
1034  av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
1035  pthread_cond_signal(&s->cond);
1036  pthread_mutex_unlock(&s->mutex);
1037  return size;
1038  }
1039 #endif
1040  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
1041  ret = ff_network_wait_fd(s->udp_fd, 1);
1042  if (ret < 0)
1043  return ret;
1044  }
1045 
1046  if (!s->is_connected) {
1047  ret = sendto (s->udp_fd, buf, size, 0,
1048  (struct sockaddr *) &s->dest_addr,
1049  s->dest_addr_len);
1050  } else
1051  ret = send(s->udp_fd, buf, size, 0);
1052 
1053  return ret < 0 ? ff_neterrno() : ret;
1054 }
1055 
1056 static int udp_close(URLContext *h)
1057 {
1058  UDPContext *s = h->priv_data;
1059 
1060 #if HAVE_PTHREAD_CANCEL
1061  // Request close once writing is finished
1062  if (s->thread_started && !(h->flags & AVIO_FLAG_READ)) {
1063  pthread_mutex_lock(&s->mutex);
1064  s->close_req = 1;
1065  pthread_cond_signal(&s->cond);
1066  pthread_mutex_unlock(&s->mutex);
1067  }
1068 #endif
1069 
1070  if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
1071  udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage);
1072 #if HAVE_PTHREAD_CANCEL
1073  if (s->thread_started) {
1074  int ret;
1075  // Cancel only read, as write has been signaled as success to the user
1076  if (h->flags & AVIO_FLAG_READ)
1077  pthread_cancel(s->circular_buffer_thread);
1078  ret = pthread_join(s->circular_buffer_thread, NULL);
1079  if (ret != 0)
1080  av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
1081  pthread_mutex_destroy(&s->mutex);
1082  pthread_cond_destroy(&s->cond);
1083  }
1084 #endif
1085  closesocket(s->udp_fd);
1086  av_fifo_freep(&s->fifo);
1088  return 0;
1089 }
1090 
1092  .name = "udp",
1093  .url_open = udp_open,
1094  .url_read = udp_read,
1095  .url_write = udp_write,
1096  .url_close = udp_close,
1097  .url_get_file_handle = udp_get_file_handle,
1098  .priv_data_size = sizeof(UDPContext),
1099  .priv_data_class = &udp_class,
1101 };
1102 
1104  .name = "udplite",
1105  .url_open = udplite_open,
1106  .url_read = udp_read,
1107  .url_write = udp_write,
1108  .url_close = udp_close,
1109  .url_get_file_handle = udp_get_file_handle,
1110  .priv_data_size = sizeof(UDPContext),
1111  .priv_data_class = &udplite_context_class,
1113 };
static int udp_open(URLContext *h, const char *uri, int flags)
Definition: udp.c:625
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.
Definition: utils.c:4777
#define NULL
Definition: coverity.c:32
static const AVClass udplite_context_class
Definition: udp.c:149
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:111
static AVMutex mutex
Definition: log.c:44
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
#define pthread_mutex_lock(a)
Definition: ffprobe.c:61
#define NI_NUMERICSERV
Definition: network.h:203
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:191
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const URLProtocol ff_udplite_protocol
Definition: udp.c:1103
char * block
Definition: udp.c:113
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:45
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int64_t rw_timeout
maximum time to wait for (network) read/write operation completion, in mcs
Definition: url.h:48
static int udp_set_url(URLContext *h, struct sockaddr_storage *addr, const char *hostname, int port)
Definition: udp.c:313
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:675
int ff_ip_parse_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters)
Parses the address[,address] source block list in buf and adds it to the filters in the IPSourceFilte...
Definition: ip.c:148
#define AI_PASSIVE
Definition: network.h:179
#define UDPLITE_SEND_CSCOV
Definition: udp.c:56
struct sockaddr_storage * exclude_addrs
Definition: ip.h:33
#define IPPROTO_UDPLITE
Definition: udp.c:61
int flags
Definition: url.h:43
int udplite_coverage
Definition: udp.c:82
#define freeaddrinfo
Definition: network.h:218
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
static void error(const char *err)
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:143
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.
Definition: fifo.c:122
int ff_socket(int af, int type, int proto)
Definition: network.c:183
int64_t bitrate
Definition: udp.c:98
int ff_ip_parse_sources(void *log_ctx, const char *buf, IPSourceFilters *filters)
Parses the address[,address] source list in buf and adds it to the filters in the IPSourceFilters str...
Definition: ip.c:143
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define UDPLITE_RECV_CSCOV
Definition: udp.c:57
uint8_t
AVOptions.
miscellaneous OS support macros and functions.
int ff_udp_get_local_port(URLContext *h)
Return the local port used by the UDP connection.
Definition: udp.c:440
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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...
Definition: fifo.c:82
int is_broadcast
Definition: udp.c:86
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr)
Definition: udp.c:211
#define UDP_HEADER_SIZE
Definition: udp.c:76
static int udp_port(struct sockaddr_storage *addr, int addr_len)
Definition: udp.c:371
int nb_include_addrs
Definition: ip.h:30
struct sockaddr_storage * include_addrs
Definition: ip.h:32
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.
Definition: parseutils.c:749
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:151
ptrdiff_t size
Definition: opengl_enc.c:100
static int udp_close(URLContext *h)
Definition: udp.c:1056
#define av_log(a,...)
#define UDP_RX_BUF_SIZE
Definition: udp.c:74
IPSourceFilters filters
Definition: udp.c:114
int circular_buffer_size
Definition: udp.c:95
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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...
Definition: udp.c:401
#define E
Definition: udp.c:119
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
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.
Definition: fifo.c:213
int reuse_socket
Definition: udp.c:88
#define UDP_TX_BUF_SIZE
Definition: udp.c:73
simple assert() macros that are a bit more flexible than ISO C assert().
#define D
Definition: udp.c:118
int dest_addr_len
Definition: udp.c:91
void ff_ip_reset_filters(IPSourceFilters *filters)
Resets the IP filter list and frees the internal fields of an IPSourceFilters structure.
Definition: ip.c:153
int ff_is_multicast_address(struct sockaddr *addr)
Definition: network.c:145
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.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:122
int timeout
Definition: udp.c:110
#define OFFSET(x)
Definition: udp.c:117
char * localaddr
Definition: udp.c:109
int ai_addrlen
Definition: network.h:142
static const AVOption options[]
Definition: udp.c:120
uint8_t tmp[UDP_MAX_PKT_SIZE+4]
Definition: udp.c:107
#define ff_neterrno()
Definition: network.h:68
int overrun_nonfatal
Definition: udp.c:89
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:93
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:103
static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
Definition: os2threads.h:169
#define s(width, name)
Definition: cbs_vp9.c:257
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...
Definition: udp.c:451
int is_connected
Definition: udp.c:92
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:65
struct sockaddr_storage local_addr_storage
Definition: udp.c:111
Definition: udp.c:78
int ff_socket_nonblock(int socket, int enable)
int64_t burst_bits
Definition: udp.c:99
int ttl
Definition: udp.c:81
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:79
#define IPV6_ADD_MEMBERSHIP
Definition: udp.c:69
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 ...
Definition: fifo.c:77
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:693
int local_port
Definition: udp.c:87
int pkt_size
Definition: udp.c:84
#define IPV6_DROP_MEMBERSHIP
Definition: udp.c:70
a very simple circular buffer FIFO implementation
void * buf
Definition: avisynth_c.h:766
Definition: url.h:38
int ff_ip_check_source_lists(struct sockaddr_storage *source_addr_ptr, IPSourceFilters *s)
Checks the source address against a given IP source filter.
Definition: ip.c:44
Describe the class of an AVClass context structure.
Definition: log.h:67
char * sources
Definition: udp.c:112
static int udp_set_multicast_sources(URLContext *h, int sockfd, struct sockaddr *addr, int addr_len, struct sockaddr_storage *local_addr, struct sockaddr_storage *sources, int nb_sources, int include)
Definition: udp.c:244
void * priv_data
Definition: url.h:41
#define gai_strerror
Definition: network.h:225
AVFifoBuffer * fifo
Definition: udp.c:96
misc parsing utilities
const char * name
Definition: url.h:55
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
#define flags(name, subs,...)
Definition: cbs_av1.c:564
uint8_t level
Definition: svq3.c:207
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
Main libavformat public API header.
int
struct sockaddr_storage dest_addr
Definition: udp.c:90
_fmutex pthread_mutex_t
Definition: os2threads.h:52
static int udp_read(URLContext *h, uint8_t *buf, int size)
Definition: udp.c:941
int remaining_in_dg
Definition: udp.c:108
struct addrinfo * ai_next
Definition: network.h:145
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:132
static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr)
Definition: udp.c:156
int udp_fd
Definition: udp.c:80
int nb_exclude_addrs
Definition: ip.h:31
#define getnameinfo
Definition: network.h:219
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
static int udplite_open(URLContext *h, const char *uri, int flags)
Definition: udp.c:931
int len
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr)
Definition: udp.c:178
int circular_buffer_error
Definition: udp.c:97
int close_req
Definition: udp.c:100
int buffer_size
Definition: udp.c:83
static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len, const char *localaddr)
Definition: udp.c:329
int is_multicast
Definition: udp.c:85
struct addrinfo * ff_ip_resolve_host(void *log_ctx, const char *hostname, int port, int type, int family, int flags)
Resolves hostname into an addrinfo structure.
Definition: ip.c:63
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:44
static const AVClass udp_class
Definition: udp.c:142
#define UDP_MAX_PKT_SIZE
Definition: udp.c:75
int ff_network_wait_fd(int fd, int write)
Definition: network.c:69
unbuffered private I/O API
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
Definition: fifo.c:63
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
void av_fifo_drain(AVFifoBuffer *f, int size)
Discard data from the FIFO.
Definition: fifo.c:233
struct sockaddr * ai_addr
Definition: network.h:143
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
Structure for storing IP (UDP) source filters or block lists.
Definition: ip.h:29
void ff_log_net_error(void *ctx, int level, const char *prefix)
Definition: network.c:581
static int udp_write(URLContext *h, const uint8_t *buf, int size)
Definition: udp.c:1006
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
const URLProtocol ff_udp_protocol
Definition: udp.c:1091
int ai_family
Definition: network.h:139