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