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