FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtpproto.c
Go to the documentation of this file.
1 /*
2  * RTP network protocol
3  * Copyright (c) 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  * RTP protocol
25  */
26 
27 #include "libavutil/parseutils.h"
28 #include "libavutil/avstring.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "rtp.h"
32 #include "rtpproto.h"
33 #include "url.h"
34 
35 #include <stdarg.h>
36 #include "internal.h"
37 #include "network.h"
38 #include "os_support.h"
39 #include <fcntl.h>
40 #if HAVE_POLL_H
41 #include <sys/poll.h>
42 #endif
43 
44 typedef struct RTPContext {
49  struct sockaddr_storage last_rtp_source, last_rtcp_source;
51 } RTPContext;
52 
53 /**
54  * If no filename is given to av_open_input_file because you want to
55  * get the local port first, then you must call this function to set
56  * the remote server address.
57  *
58  * @param h media file context
59  * @param uri of the remote server
60  * @return zero if no error.
61  */
62 
63 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
64 {
65  RTPContext *s = h->priv_data;
66  char hostname[256];
67  int port, rtcp_port;
68  const char *p;
69 
70  char buf[1024];
71  char path[1024];
72 
73  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
74  path, sizeof(path), uri);
75  rtcp_port = port + 1;
76 
77  p = strchr(uri, '?');
78  if (p) {
79  if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
80  rtcp_port = strtol(buf, NULL, 10);
81  }
82  }
83 
84  ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
86 
87  ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
89  return 0;
90 }
91 
92 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
93  int type, int family, int flags)
94 {
95  struct addrinfo hints = { 0 }, *res = 0;
96  int error;
97  char service[16];
98 
99  snprintf(service, sizeof(service), "%d", port);
100  hints.ai_socktype = type;
101  hints.ai_family = family;
102  hints.ai_flags = flags;
103  if ((error = getaddrinfo(hostname, service, &hints, &res))) {
104  res = NULL;
105  av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
106  }
107 
108  return res;
109 }
110 
111 static int compare_addr(const struct sockaddr_storage *a,
112  const struct sockaddr_storage *b)
113 {
114  if (a->ss_family != b->ss_family)
115  return 1;
116  if (a->ss_family == AF_INET) {
117  return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
118  ((const struct sockaddr_in *)b)->sin_addr.s_addr);
119  }
120 
121 #if HAVE_STRUCT_SOCKADDR_IN6
122  if (a->ss_family == AF_INET6) {
123  const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
124  const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
125  return memcmp(s6_addr_a, s6_addr_b, 16);
126  }
127 #endif
128  return 1;
129 }
130 
131 static int get_port(const struct sockaddr_storage *ss)
132 {
133  sockaddr_union ssu = (sockaddr_union){.storage = *ss};
134  if (ss->ss_family == AF_INET)
135  return ntohs(ssu.in.sin_port);
136 #if HAVE_STRUCT_SOCKADDR_IN6
137  if (ss->ss_family == AF_INET6)
138  return ntohs(ssu.in6.sin6_port);
139 #endif
140  return 0;
141 }
142 
143 static void set_port(struct sockaddr_storage *ss, int port)
144 {
145  sockaddr_union ssu = (sockaddr_union){.storage = *ss};
146  if (ss->ss_family == AF_INET)
147  ssu.in.sin_port = htons(port);
148 #if HAVE_STRUCT_SOCKADDR_IN6
149  else if (ss->ss_family == AF_INET6)
150  ssu.in6.sin6_port = htons(port);
151 #endif
152  *ss = ssu.storage;
153 }
154 
155 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
156 {
157  int i;
158  if (s->nb_ssm_exclude_addrs) {
159  for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
160  if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
161  return 1;
162  }
163  }
164  if (s->nb_ssm_include_addrs) {
165  for (i = 0; i < s->nb_ssm_include_addrs; i++) {
166  if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
167  return 0;
168  }
169  return 1;
170  }
171  return 0;
172 }
173 
174 /**
175  * add option to url of the form:
176  * "http://host:port/path?option1=val1&option2=val2...
177  */
178 
179 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
180 {
181  char buf1[1024];
182  va_list ap;
183 
184  va_start(ap, fmt);
185  if (strchr(buf, '?'))
186  av_strlcat(buf, "&", buf_size);
187  else
188  av_strlcat(buf, "?", buf_size);
189  vsnprintf(buf1, sizeof(buf1), fmt, ap);
190  av_strlcat(buf, buf1, buf_size);
191  va_end(ap);
192 }
193 
194 static void build_udp_url(char *buf, int buf_size,
195  const char *hostname, int port,
196  int local_port, int ttl,
197  int max_packet_size, int connect,
198  const char *include_sources,
199  const char *exclude_sources)
200 {
201  ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
202  if (local_port >= 0)
203  url_add_option(buf, buf_size, "localport=%d", local_port);
204  if (ttl >= 0)
205  url_add_option(buf, buf_size, "ttl=%d", ttl);
206  if (max_packet_size >=0)
207  url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
208  if (connect)
209  url_add_option(buf, buf_size, "connect=1");
210  url_add_option(buf, buf_size, "fifo_size=0");
211  if (include_sources && include_sources[0])
212  url_add_option(buf, buf_size, "sources=%s", include_sources);
213  if (exclude_sources && exclude_sources[0])
214  url_add_option(buf, buf_size, "block=%s", exclude_sources);
215 }
216 
217 static void rtp_parse_addr_list(URLContext *h, char *buf,
218  struct sockaddr_storage ***address_list_ptr,
219  int *address_list_size_ptr)
220 {
221  struct addrinfo *ai = NULL;
222  struct sockaddr_storage *source_addr;
223  char tmp = '\0', *p = buf, *next;
224 
225  /* Resolve all of the IPs */
226 
227  while (p && p[0]) {
228  next = strchr(p, ',');
229 
230  if (next) {
231  tmp = *next;
232  *next = '\0';
233  }
234 
235  ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
236  if (ai) {
237  source_addr = av_mallocz(sizeof(struct sockaddr_storage));
238  if (!source_addr)
239  break;
240 
241  memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
242  freeaddrinfo(ai);
243  dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
244  } else {
245  av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
246  }
247 
248  if (next) {
249  *next = tmp;
250  p = next + 1;
251  } else {
252  p = NULL;
253  }
254  }
255 }
256 
257 /**
258  * url syntax: rtp://host:port[?option=val...]
259  * option: 'ttl=n' : set the ttl value (for multicast only)
260  * 'rtcpport=n' : set the remote rtcp port to n
261  * 'localrtpport=n' : set the local rtp port to n
262  * 'localrtcpport=n' : set the local rtcp port to n
263  * 'pkt_size=n' : set max packet size
264  * 'connect=0/1' : do a connect() on the UDP socket
265  * 'sources=ip[,ip]' : list allowed source IP addresses
266  * 'block=ip[,ip]' : list disallowed source IP addresses
267  * 'write_to_source=0/1' : send packets to the source address of the latest received packet
268  * deprecated option:
269  * 'localport=n' : set the local port to n
270  *
271  * if rtcpport isn't set the rtcp port will be the rtp port + 1
272  * if local rtp port isn't set any available port will be used for the local
273  * rtp and rtcp ports
274  * if the local rtcp port is not set it will be the local rtp port + 1
275  */
276 
277 static int rtp_open(URLContext *h, const char *uri, int flags)
278 {
279  RTPContext *s = h->priv_data;
280  int rtp_port, rtcp_port,
281  ttl, connect,
282  local_rtp_port, local_rtcp_port, max_packet_size;
283  char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
284  char buf[1024];
285  char path[1024];
286  const char *p;
287  int i, max_retry_count = 3;
288 
289  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
290  path, sizeof(path), uri);
291  /* extract parameters */
292  ttl = -1;
293  rtcp_port = rtp_port+1;
294  local_rtp_port = -1;
295  local_rtcp_port = -1;
296  max_packet_size = -1;
297  connect = 0;
298 
299  p = strchr(uri, '?');
300  if (p) {
301  if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
302  ttl = strtol(buf, NULL, 10);
303  }
304  if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
305  rtcp_port = strtol(buf, NULL, 10);
306  }
307  if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
308  local_rtp_port = strtol(buf, NULL, 10);
309  }
310  if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
311  local_rtp_port = strtol(buf, NULL, 10);
312  }
313  if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
314  local_rtcp_port = strtol(buf, NULL, 10);
315  }
316  if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
317  max_packet_size = strtol(buf, NULL, 10);
318  }
319  if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
320  connect = strtol(buf, NULL, 10);
321  }
322  if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
323  s->write_to_source = strtol(buf, NULL, 10);
324  }
325  if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
326  av_strlcpy(include_sources, buf, sizeof(include_sources));
328  }
329  if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
330  av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
332  }
333  }
334 
335  for (i = 0;i < max_retry_count;i++) {
336  build_udp_url(buf, sizeof(buf),
337  hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
338  connect, include_sources, exclude_sources);
339  if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
340  goto fail;
341  local_rtp_port = ff_udp_get_local_port(s->rtp_hd);
342  if(local_rtp_port == 65535) {
343  local_rtp_port = -1;
344  continue;
345  }
346  if (local_rtcp_port<0) {
347  local_rtcp_port = local_rtp_port + 1;
348  build_udp_url(buf, sizeof(buf),
349  hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
350  connect, include_sources, exclude_sources);
351  if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0) {
352  local_rtp_port = local_rtcp_port = -1;
353  continue;
354  }
355  break;
356  }
357  build_udp_url(buf, sizeof(buf),
358  hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
359  connect, include_sources, exclude_sources);
360  if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
361  goto fail;
362  break;
363  }
364 
365  /* just to ease handle access. XXX: need to suppress direct handle
366  access */
369 
371  h->is_streamed = 1;
372  return 0;
373 
374  fail:
375  if (s->rtp_hd)
376  ffurl_close(s->rtp_hd);
377  if (s->rtcp_hd)
378  ffurl_close(s->rtcp_hd);
379  return AVERROR(EIO);
380 }
381 
382 static int rtp_read(URLContext *h, uint8_t *buf, int size)
383 {
384  RTPContext *s = h->priv_data;
385  int len, n, i;
386  struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
387  int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
388  struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
389  socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
390 
391  for(;;) {
393  return AVERROR_EXIT;
394  n = poll(p, 2, poll_delay);
395  if (n > 0) {
396  /* first try RTCP, then RTP */
397  for (i = 1; i >= 0; i--) {
398  if (!(p[i].revents & POLLIN))
399  continue;
400  *addr_lens[i] = sizeof(*addrs[i]);
401  len = recvfrom(p[i].fd, buf, size, 0,
402  (struct sockaddr *)addrs[i], addr_lens[i]);
403  if (len < 0) {
404  if (ff_neterrno() == AVERROR(EAGAIN) ||
405  ff_neterrno() == AVERROR(EINTR))
406  continue;
407  return AVERROR(EIO);
408  }
409  if (rtp_check_source_lists(s, addrs[i]))
410  continue;
411  return len;
412  }
413  } else if (n < 0) {
414  if (ff_neterrno() == AVERROR(EINTR))
415  continue;
416  return AVERROR(EIO);
417  }
418  if (h->flags & AVIO_FLAG_NONBLOCK)
419  return AVERROR(EAGAIN);
420  }
421  return len;
422 }
423 
424 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
425 {
426  RTPContext *s = h->priv_data;
427  int ret;
428  URLContext *hd;
429 
430  if (size < 2)
431  return AVERROR(EINVAL);
432 
433  if (s->write_to_source) {
434  int fd;
435  struct sockaddr_storage *source, temp_source;
436  socklen_t *source_len, temp_len;
437  if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
438  av_log(h, AV_LOG_ERROR,
439  "Unable to send packet to source, no packets received yet\n");
440  // Intentionally not returning an error here
441  return size;
442  }
443 
444  if (RTP_PT_IS_RTCP(buf[1])) {
445  fd = s->rtcp_fd;
446  source = &s->last_rtcp_source;
447  source_len = &s->last_rtcp_source_len;
448  } else {
449  fd = s->rtp_fd;
450  source = &s->last_rtp_source;
451  source_len = &s->last_rtp_source_len;
452  }
453  if (!source->ss_family) {
454  source = &temp_source;
455  source_len = &temp_len;
456  if (RTP_PT_IS_RTCP(buf[1])) {
457  temp_source = s->last_rtp_source;
458  temp_len = s->last_rtp_source_len;
459  set_port(source, get_port(source) + 1);
460  av_log(h, AV_LOG_INFO,
461  "Not received any RTCP packets yet, inferring peer port "
462  "from the RTP port\n");
463  } else {
464  temp_source = s->last_rtcp_source;
465  temp_len = s->last_rtcp_source_len;
466  set_port(source, get_port(source) - 1);
467  av_log(h, AV_LOG_INFO,
468  "Not received any RTP packets yet, inferring peer port "
469  "from the RTCP port\n");
470  }
471  }
472 
473  if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
474  ret = ff_network_wait_fd(fd, 1);
475  if (ret < 0)
476  return ret;
477  }
478  ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
479  *source_len);
480 
481  return ret < 0 ? ff_neterrno() : ret;
482  }
483 
484  if (RTP_PT_IS_RTCP(buf[1])) {
485  /* RTCP payload type */
486  hd = s->rtcp_hd;
487  } else {
488  /* RTP payload type */
489  hd = s->rtp_hd;
490  }
491 
492  ret = ffurl_write(hd, buf, size);
493  return ret;
494 }
495 
496 static int rtp_close(URLContext *h)
497 {
498  RTPContext *s = h->priv_data;
499  int i;
500 
501  for (i = 0; i < s->nb_ssm_include_addrs; i++)
502  av_free(s->ssm_include_addrs[i]);
504  for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
505  av_free(s->ssm_exclude_addrs[i]);
507 
508  ffurl_close(s->rtp_hd);
509  ffurl_close(s->rtcp_hd);
510  return 0;
511 }
512 
513 /**
514  * Return the local rtp port used by the RTP connection
515  * @param h media file context
516  * @return the local port number
517  */
518 
520 {
521  RTPContext *s = h->priv_data;
522  return ff_udp_get_local_port(s->rtp_hd);
523 }
524 
525 /**
526  * Return the local rtcp port used by the RTP connection
527  * @param h media file context
528  * @return the local port number
529  */
530 
532 {
533  RTPContext *s = h->priv_data;
534  return ff_udp_get_local_port(s->rtcp_hd);
535 }
536 
538 {
539  RTPContext *s = h->priv_data;
540  return s->rtp_fd;
541 }
542 
543 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
544  int *numhandles)
545 {
546  RTPContext *s = h->priv_data;
547  int *hs = *handles = av_malloc(sizeof(**handles) * 2);
548  if (!hs)
549  return AVERROR(ENOMEM);
550  hs[0] = s->rtp_fd;
551  hs[1] = s->rtcp_fd;
552  *numhandles = 2;
553  return 0;
554 }
555 
557  .name = "rtp",
558  .url_open = rtp_open,
559  .url_read = rtp_read,
560  .url_write = rtp_write,
561  .url_close = rtp_close,
562  .url_get_file_handle = rtp_get_file_handle,
563  .url_get_multi_file_handle = rtp_get_multi_file_handle,
564  .priv_data_size = sizeof(RTPContext),
566 };