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