3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
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,
229 const char *include_sources,
230 const char *exclude_sources)
232 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
234 url_add_option(buf, buf_size, "localport=%d", local_port);
236 url_add_option(buf, buf_size, "ttl=%d", s->ttl);
237 if (s->buffer_size >= 0)
238 url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
239 if (s->pkt_size >= 0)
240 url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
242 url_add_option(buf, buf_size, "connect=1");
244 url_add_option(buf, buf_size, "dscp=%d", dscp);
245 url_add_option(buf, buf_size, "fifo_size=0");
246 if (include_sources && include_sources[0])
247 url_add_option(buf, buf_size, "sources=%s", include_sources);
248 if (exclude_sources && exclude_sources[0])
249 url_add_option(buf, buf_size, "block=%s", exclude_sources);
252 static void rtp_parse_addr_list(URLContext *h, char *buf,
253 struct sockaddr_storage ***address_list_ptr,
254 int *address_list_size_ptr)
256 struct addrinfo *ai = NULL;
257 struct sockaddr_storage *source_addr;
258 char tmp = '\0', *p = buf, *next;
260 /* Resolve all of the IPs */
263 next = strchr(p, ',');
270 ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
272 source_addr = av_mallocz(sizeof(struct sockaddr_storage));
278 memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
280 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
282 av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
295 * url syntax: rtp://host:port[?option=val...]
296 * option: 'ttl=n' : set the ttl value (for multicast only)
297 * 'rtcpport=n' : set the remote rtcp port to n
298 * 'localrtpport=n' : set the local rtp port to n
299 * 'localrtcpport=n' : set the local rtcp port to n
300 * 'pkt_size=n' : set max packet size
301 * 'connect=0/1' : do a connect() on the UDP socket
302 * 'sources=ip[,ip]' : list allowed source IP addresses
303 * 'block=ip[,ip]' : list disallowed source IP addresses
304 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
305 * 'dscp=n' : set DSCP value to n (QoS)
307 * 'localport=n' : set the local port to n
309 * if rtcpport isn't set the rtcp port will be the rtp port + 1
310 * if local rtp port isn't set any available port will be used for the local
312 * if the local rtcp port is not set it will be the local rtp port + 1
315 static int rtp_open(URLContext *h, const char *uri, int flags)
317 RTPContext *s = h->priv_data;
320 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
321 char *sources = include_sources, *block = exclude_sources;
325 int i, max_retry_count = 3;
327 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
328 path, sizeof(path), uri);
329 /* extract parameters */
331 if (s->rtcp_port < 0)
332 s->rtcp_port = rtp_port + 1;
334 p = strchr(uri, '?');
336 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
337 s->ttl = strtol(buf, NULL, 10);
339 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
340 s->rtcp_port = strtol(buf, NULL, 10);
342 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
343 s->local_rtpport = strtol(buf, NULL, 10);
345 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
346 s->local_rtpport = strtol(buf, NULL, 10);
348 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
349 s->local_rtcpport = strtol(buf, NULL, 10);
351 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
352 s->pkt_size = strtol(buf, NULL, 10);
354 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
355 s->connect = strtol(buf, NULL, 10);
357 if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
358 s->write_to_source = strtol(buf, NULL, 10);
360 if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
361 dscp = strtol(buf, NULL, 10);
363 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
364 av_strlcpy(include_sources, buf, sizeof(include_sources));
366 rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
368 rtp_parse_addr_list(h, s->sources, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
369 sources = s->sources;
371 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
372 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
373 rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
375 rtp_parse_addr_list(h, s->block, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
380 for (i = 0; i < max_retry_count; i++) {
381 build_udp_url(s, buf, sizeof(buf),
382 hostname, rtp_port, s->local_rtpport,
383 dscp, sources, block);
384 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
386 s->local_rtpport = ff_udp_get_local_port(s->rtp_hd);
387 if(s->local_rtpport == 65535) {
388 s->local_rtpport = -1;
391 if (s->local_rtcpport < 0) {
392 s->local_rtcpport = s->local_rtpport + 1;
393 build_udp_url(s, buf, sizeof(buf),
394 hostname, s->rtcp_port, s->local_rtcpport,
395 dscp, sources, block);
396 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0) {
397 s->local_rtpport = s->local_rtcpport = -1;
402 build_udp_url(s, buf, sizeof(buf),
403 hostname, s->rtcp_port, s->local_rtcpport,
404 dscp, sources, block);
405 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
410 /* just to ease handle access. XXX: need to suppress direct handle
412 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
413 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
415 h->max_packet_size = s->rtp_hd->max_packet_size;
421 ffurl_close(s->rtp_hd);
423 ffurl_close(s->rtcp_hd);
427 static int rtp_read(URLContext *h, uint8_t *buf, int size)
429 RTPContext *s = h->priv_data;
431 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
432 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
433 struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
434 socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
437 if (ff_check_interrupt(&h->interrupt_callback))
439 n = poll(p, 2, poll_delay);
441 /* first try RTCP, then RTP */
442 for (i = 1; i >= 0; i--) {
443 if (!(p[i].revents & POLLIN))
445 *addr_lens[i] = sizeof(*addrs[i]);
446 len = recvfrom(p[i].fd, buf, size, 0,
447 (struct sockaddr *)addrs[i], addr_lens[i]);
449 if (ff_neterrno() == AVERROR(EAGAIN) ||
450 ff_neterrno() == AVERROR(EINTR))
454 if (rtp_check_source_lists(s, addrs[i]))
459 if (ff_neterrno() == AVERROR(EINTR))
463 if (h->flags & AVIO_FLAG_NONBLOCK)
464 return AVERROR(EAGAIN);
469 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
471 RTPContext *s = h->priv_data;
476 return AVERROR(EINVAL);
478 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
479 av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
480 "make sure the RTP muxer is used\n");
482 if (s->write_to_source) {
484 struct sockaddr_storage *source, temp_source;
485 socklen_t *source_len, temp_len;
486 if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
487 av_log(h, AV_LOG_ERROR,
488 "Unable to send packet to source, no packets received yet\n");
489 // Intentionally not returning an error here
493 if (RTP_PT_IS_RTCP(buf[1])) {
495 source = &s->last_rtcp_source;
496 source_len = &s->last_rtcp_source_len;
499 source = &s->last_rtp_source;
500 source_len = &s->last_rtp_source_len;
502 if (!source->ss_family) {
503 source = &temp_source;
504 source_len = &temp_len;
505 if (RTP_PT_IS_RTCP(buf[1])) {
506 temp_source = s->last_rtp_source;
507 temp_len = s->last_rtp_source_len;
508 set_port(source, get_port(source) + 1);
509 av_log(h, AV_LOG_INFO,
510 "Not received any RTCP packets yet, inferring peer port "
511 "from the RTP port\n");
513 temp_source = s->last_rtcp_source;
514 temp_len = s->last_rtcp_source_len;
515 set_port(source, get_port(source) - 1);
516 av_log(h, AV_LOG_INFO,
517 "Not received any RTP packets yet, inferring peer port "
518 "from the RTCP port\n");
522 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
523 ret = ff_network_wait_fd(fd, 1);
527 ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
530 return ret < 0 ? ff_neterrno() : ret;
533 if (RTP_PT_IS_RTCP(buf[1])) {
534 /* RTCP payload type */
537 /* RTP payload type */
541 ret = ffurl_write(hd, buf, size);
545 static int rtp_close(URLContext *h)
547 RTPContext *s = h->priv_data;
550 for (i = 0; i < s->nb_ssm_include_addrs; i++)
551 av_freep(&s->ssm_include_addrs[i]);
552 av_freep(&s->ssm_include_addrs);
553 for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
554 av_freep(&s->ssm_exclude_addrs[i]);
555 av_freep(&s->ssm_exclude_addrs);
557 ffurl_close(s->rtp_hd);
558 ffurl_close(s->rtcp_hd);
563 * Return the local rtp port used by the RTP connection
564 * @param h media file context
565 * @return the local port number
568 int ff_rtp_get_local_rtp_port(URLContext *h)
570 RTPContext *s = h->priv_data;
571 return ff_udp_get_local_port(s->rtp_hd);
575 * Return the local rtcp port used by the RTP connection
576 * @param h media file context
577 * @return the local port number
580 int ff_rtp_get_local_rtcp_port(URLContext *h)
582 RTPContext *s = h->priv_data;
583 return ff_udp_get_local_port(s->rtcp_hd);
586 static int rtp_get_file_handle(URLContext *h)
588 RTPContext *s = h->priv_data;
592 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
595 RTPContext *s = h->priv_data;
596 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
598 return AVERROR(ENOMEM);
605 URLProtocol ff_rtp_protocol = {
607 .url_open = rtp_open,
608 .url_read = rtp_read,
609 .url_write = rtp_write,
610 .url_close = rtp_close,
611 .url_get_file_handle = rtp_get_file_handle,
612 .url_get_multi_file_handle = rtp_get_multi_file_handle,
613 .priv_data_size = sizeof(RTPContext),
614 .flags = URL_PROTOCOL_FLAG_NETWORK,
615 .priv_data_class = &rtp_class,