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