3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/parseutils.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/opt.h"
31 #include "avio_internal.h"
39 #include "os_support.h"
45 typedef struct RTPContext {
47 URLContext *rtp_hd, *rtcp_hd;
48 int rtp_fd, rtcp_fd, nb_ssm_include_addrs, nb_ssm_exclude_addrs;
49 struct sockaddr_storage **ssm_include_addrs, **ssm_exclude_addrs;
51 struct sockaddr_storage last_rtp_source, last_rtcp_source;
52 socklen_t last_rtp_source_len, last_rtcp_source_len;
55 int rtcp_port, local_rtpport, local_rtcpport;
62 #define OFFSET(x) offsetof(RTPContext, x)
63 #define D AV_OPT_FLAG_DECODING_PARAM
64 #define E AV_OPT_FLAG_ENCODING_PARAM
65 static const AVOption options[] = {
66 { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
67 { "buffer_size", "Send/Receive buffer size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
68 { "rtcp_port", "Custom rtcp port", OFFSET(rtcp_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
69 { "local_rtpport", "Local rtp port", OFFSET(local_rtpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
70 { "local_rtcpport", "Local rtcp port", OFFSET(local_rtcpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
71 { "connect", "Connect socket", OFFSET(connect), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
72 { "write_to_source", "Send packets to the source address of the latest received packet", OFFSET(write_to_source), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
73 { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
74 { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
75 { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
79 static const AVClass rtp_class = {
81 .item_name = av_default_item_name,
83 .version = LIBAVUTIL_VERSION_INT,
87 * If no filename is given to av_open_input_file because you want to
88 * get the local port first, then you must call this function to set
89 * the remote server address.
91 * @param h media file context
92 * @param uri of the remote server
93 * @return zero if no error.
96 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
98 RTPContext *s = h->priv_data;
106 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
107 path, sizeof(path), uri);
108 rtcp_port = port + 1;
110 p = strchr(uri, '?');
112 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
113 rtcp_port = strtol(buf, NULL, 10);
117 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
118 ff_udp_set_remote_url(s->rtp_hd, buf);
120 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
121 ff_udp_set_remote_url(s->rtcp_hd, buf);
125 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
126 int type, int family, int flags)
128 struct addrinfo hints = { 0 }, *res = 0;
132 snprintf(service, sizeof(service), "%d", port);
133 hints.ai_socktype = type;
134 hints.ai_family = family;
135 hints.ai_flags = flags;
136 if ((error = getaddrinfo(hostname, service, &hints, &res))) {
138 av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
144 static int compare_addr(const struct sockaddr_storage *a,
145 const struct sockaddr_storage *b)
147 if (a->ss_family != b->ss_family)
149 if (a->ss_family == AF_INET) {
150 return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
151 ((const struct sockaddr_in *)b)->sin_addr.s_addr);
154 #if HAVE_STRUCT_SOCKADDR_IN6
155 if (a->ss_family == AF_INET6) {
156 const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
157 const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
158 return memcmp(s6_addr_a, s6_addr_b, 16);
164 static int get_port(const struct sockaddr_storage *ss)
166 if (ss->ss_family == AF_INET)
167 return ntohs(((const struct sockaddr_in *)ss)->sin_port);
168 #if HAVE_STRUCT_SOCKADDR_IN6
169 if (ss->ss_family == AF_INET6)
170 return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
175 static void set_port(struct sockaddr_storage *ss, int port)
177 if (ss->ss_family == AF_INET)
178 ((struct sockaddr_in *)ss)->sin_port = htons(port);
179 #if HAVE_STRUCT_SOCKADDR_IN6
180 else if (ss->ss_family == AF_INET6)
181 ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
185 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
188 if (s->nb_ssm_exclude_addrs) {
189 for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
190 if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
194 if (s->nb_ssm_include_addrs) {
195 for (i = 0; i < s->nb_ssm_include_addrs; i++) {
196 if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
205 * add option to url of the form:
206 * "http://host:port/path?option1=val1&option2=val2...
209 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
215 if (strchr(buf, '?'))
216 av_strlcat(buf, "&", buf_size);
218 av_strlcat(buf, "?", buf_size);
219 vsnprintf(buf1, sizeof(buf1), fmt, ap);
220 av_strlcat(buf, buf1, buf_size);
224 static void build_udp_url(RTPContext *s,
225 char *buf, int buf_size,
226 const char *hostname,
227 int port, int local_port,
228 const char *include_sources,
229 const char *exclude_sources)
231 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
233 url_add_option(buf, buf_size, "localport=%d", local_port);
235 url_add_option(buf, buf_size, "ttl=%d", s->ttl);
236 if (s->buffer_size >= 0)
237 url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
238 if (s->pkt_size >= 0)
239 url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
241 url_add_option(buf, buf_size, "connect=1");
242 if (include_sources && include_sources[0])
243 url_add_option(buf, buf_size, "sources=%s", include_sources);
244 if (exclude_sources && exclude_sources[0])
245 url_add_option(buf, buf_size, "block=%s", exclude_sources);
248 static void rtp_parse_addr_list(URLContext *h, char *buf,
249 struct sockaddr_storage ***address_list_ptr,
250 int *address_list_size_ptr)
252 struct addrinfo *ai = NULL;
253 struct sockaddr_storage *source_addr;
254 char tmp = '\0', *p = buf, *next;
256 /* Resolve all of the IPs */
259 next = strchr(p, ',');
266 ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
268 source_addr = av_mallocz(sizeof(struct sockaddr_storage));
274 memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
276 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
278 av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
291 * url syntax: rtp://host:port[?option=val...]
292 * option: 'ttl=n' : set the ttl value (for multicast only)
293 * 'rtcpport=n' : set the remote rtcp port to n
294 * 'localrtpport=n' : set the local rtp port to n
295 * 'localrtcpport=n' : set the local rtcp port to n
296 * 'pkt_size=n' : set max packet size
297 * 'connect=0/1' : do a connect() on the UDP socket
298 * 'sources=ip[,ip]' : list allowed source IP addresses
299 * 'block=ip[,ip]' : list disallowed source IP addresses
300 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
302 * 'localport=n' : set the local port to n
304 * if rtcpport isn't set the rtcp port will be the rtp port + 1
305 * if local rtp port isn't set any available port will be used for the local
307 * if the local rtcp port is not set it will be the local rtp port + 1
310 static int rtp_open(URLContext *h, const char *uri, int flags)
312 RTPContext *s = h->priv_data;
314 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
315 char *sources = include_sources, *block = exclude_sources;
320 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
321 path, sizeof(path), uri);
322 /* extract parameters */
323 if (s->rtcp_port < 0)
324 s->rtcp_port = rtp_port + 1;
326 p = strchr(uri, '?');
328 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
329 s->ttl = strtol(buf, NULL, 10);
331 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
332 s->rtcp_port = strtol(buf, NULL, 10);
334 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
335 s->local_rtpport = strtol(buf, NULL, 10);
337 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
338 s->local_rtpport = strtol(buf, NULL, 10);
340 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
341 s->local_rtcpport = strtol(buf, NULL, 10);
343 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
344 s->pkt_size = strtol(buf, NULL, 10);
346 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
347 s->connect = strtol(buf, NULL, 10);
349 if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
350 s->write_to_source = strtol(buf, NULL, 10);
352 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
353 av_strlcpy(include_sources, buf, sizeof(include_sources));
355 rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
357 rtp_parse_addr_list(h, s->sources, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
358 sources = s->sources;
360 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
361 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
362 rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
364 rtp_parse_addr_list(h, s->block, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
369 build_udp_url(s, buf, sizeof(buf),
370 hostname, rtp_port, s->local_rtpport, sources, block);
371 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL,
372 h->protocols, h) < 0)
374 if (s->local_rtpport >= 0 && s->local_rtcpport < 0)
375 s->local_rtcpport = ff_udp_get_local_port(s->rtp_hd) + 1;
377 build_udp_url(s, buf, sizeof(buf),
378 hostname, s->rtcp_port, s->local_rtcpport, sources, block);
379 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL,
380 h->protocols, h) < 0)
383 /* just to ease handle access. XXX: need to suppress direct handle
385 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
386 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
388 h->max_packet_size = s->rtp_hd->max_packet_size;
394 ffurl_close(s->rtp_hd);
396 ffurl_close(s->rtcp_hd);
400 static int rtp_read(URLContext *h, uint8_t *buf, int size)
402 RTPContext *s = h->priv_data;
404 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
405 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
406 struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
407 socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
410 if (ff_check_interrupt(&h->interrupt_callback))
412 n = poll(p, 2, poll_delay);
414 /* first try RTCP, then RTP */
415 for (i = 1; i >= 0; i--) {
416 if (!(p[i].revents & POLLIN))
418 *addr_lens[i] = sizeof(*addrs[i]);
419 len = recvfrom(p[i].fd, buf, size, 0,
420 (struct sockaddr *)addrs[i], addr_lens[i]);
422 if (ff_neterrno() == AVERROR(EAGAIN) ||
423 ff_neterrno() == AVERROR(EINTR))
427 if (rtp_check_source_lists(s, addrs[i]))
432 if (ff_neterrno() == AVERROR(EINTR))
436 if (h->flags & AVIO_FLAG_NONBLOCK)
437 return AVERROR(EAGAIN);
441 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
443 RTPContext *s = h->priv_data;
448 return AVERROR(EINVAL);
450 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
451 av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
452 "make sure the RTP muxer is used\n");
454 if (s->write_to_source) {
456 struct sockaddr_storage *source, temp_source;
457 socklen_t *source_len, temp_len;
458 if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
459 av_log(h, AV_LOG_ERROR,
460 "Unable to send packet to source, no packets received yet\n");
461 // Intentionally not returning an error here
465 if (RTP_PT_IS_RTCP(buf[1])) {
467 source = &s->last_rtcp_source;
468 source_len = &s->last_rtcp_source_len;
471 source = &s->last_rtp_source;
472 source_len = &s->last_rtp_source_len;
474 if (!source->ss_family) {
475 source = &temp_source;
476 source_len = &temp_len;
477 if (RTP_PT_IS_RTCP(buf[1])) {
478 temp_source = s->last_rtp_source;
479 temp_len = s->last_rtp_source_len;
480 set_port(source, get_port(source) + 1);
481 av_log(h, AV_LOG_INFO,
482 "Not received any RTCP packets yet, inferring peer port "
483 "from the RTP port\n");
485 temp_source = s->last_rtcp_source;
486 temp_len = s->last_rtcp_source_len;
487 set_port(source, get_port(source) - 1);
488 av_log(h, AV_LOG_INFO,
489 "Not received any RTP packets yet, inferring peer port "
490 "from the RTCP port\n");
494 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
495 ret = ff_network_wait_fd(fd, 1);
499 ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
502 return ret < 0 ? ff_neterrno() : ret;
505 if (RTP_PT_IS_RTCP(buf[1])) {
506 /* RTCP payload type */
509 /* RTP payload type */
513 ret = ffurl_write(hd, buf, size);
517 static int rtp_close(URLContext *h)
519 RTPContext *s = h->priv_data;
522 for (i = 0; i < s->nb_ssm_include_addrs; i++)
523 av_free(s->ssm_include_addrs[i]);
524 av_freep(&s->ssm_include_addrs);
525 for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
526 av_free(s->ssm_exclude_addrs[i]);
527 av_freep(&s->ssm_exclude_addrs);
529 ffurl_close(s->rtp_hd);
530 ffurl_close(s->rtcp_hd);
535 * Return the local rtp port used by the RTP connection
536 * @param h media file context
537 * @return the local port number
540 int ff_rtp_get_local_rtp_port(URLContext *h)
542 RTPContext *s = h->priv_data;
543 return ff_udp_get_local_port(s->rtp_hd);
547 * Return the local rtcp port used by the RTP connection
548 * @param h media file context
549 * @return the local port number
552 int ff_rtp_get_local_rtcp_port(URLContext *h)
554 RTPContext *s = h->priv_data;
555 return ff_udp_get_local_port(s->rtcp_hd);
558 static int rtp_get_file_handle(URLContext *h)
560 RTPContext *s = h->priv_data;
564 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
567 RTPContext *s = h->priv_data;
568 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
570 return AVERROR(ENOMEM);
577 const URLProtocol ff_rtp_protocol = {
579 .url_open = rtp_open,
580 .url_read = rtp_read,
581 .url_write = rtp_write,
582 .url_close = rtp_close,
583 .url_get_file_handle = rtp_get_file_handle,
584 .url_get_multi_file_handle = rtp_get_multi_file_handle,
585 .priv_data_size = sizeof(RTPContext),
586 .flags = URL_PROTOCOL_FLAG_NETWORK,
587 .priv_data_class = &rtp_class,