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, *fec_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;
61 char *fec_options_str;
64 #define OFFSET(x) offsetof(RTPContext, x)
65 #define D AV_OPT_FLAG_DECODING_PARAM
66 #define E AV_OPT_FLAG_ENCODING_PARAM
67 static const AVOption options[] = {
68 { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
69 { "buffer_size", "Send/Receive buffer size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
70 { "rtcp_port", "Custom rtcp port", OFFSET(rtcp_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
71 { "local_rtpport", "Local rtp port", OFFSET(local_rtpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
72 { "local_rtcpport", "Local rtcp port", OFFSET(local_rtcpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
73 { "connect", "Connect socket", OFFSET(connect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
74 { "write_to_source", "Send packets to the source address of the latest received packet", OFFSET(write_to_source), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
75 { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
76 { "dscp", "DSCP class", OFFSET(dscp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
77 { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
78 { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
79 { "fec", "FEC", OFFSET(fec_options_str), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = E },
83 static const AVClass rtp_class = {
85 .item_name = av_default_item_name,
87 .version = LIBAVUTIL_VERSION_INT,
91 * If no filename is given to av_open_input_file because you want to
92 * get the local port first, then you must call this function to set
93 * the remote server address.
95 * @param h media file context
96 * @param uri of the remote server
97 * @return zero if no error.
100 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
102 RTPContext *s = h->priv_data;
110 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
111 path, sizeof(path), uri);
112 rtcp_port = port + 1;
114 p = strchr(uri, '?');
116 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
117 rtcp_port = strtol(buf, NULL, 10);
121 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
122 ff_udp_set_remote_url(s->rtp_hd, buf);
124 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
125 ff_udp_set_remote_url(s->rtcp_hd, buf);
129 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
130 int type, int family, int flags)
132 struct addrinfo hints = { 0 }, *res = 0;
136 snprintf(service, sizeof(service), "%d", port);
137 hints.ai_socktype = type;
138 hints.ai_family = family;
139 hints.ai_flags = flags;
140 if ((error = getaddrinfo(hostname, service, &hints, &res))) {
142 av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
148 static int compare_addr(const struct sockaddr_storage *a,
149 const struct sockaddr_storage *b)
151 if (a->ss_family != b->ss_family)
153 if (a->ss_family == AF_INET) {
154 return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
155 ((const struct sockaddr_in *)b)->sin_addr.s_addr);
158 #if HAVE_STRUCT_SOCKADDR_IN6
159 if (a->ss_family == AF_INET6) {
160 const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
161 const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
162 return memcmp(s6_addr_a, s6_addr_b, 16);
168 static int get_port(const struct sockaddr_storage *ss)
170 if (ss->ss_family == AF_INET)
171 return ntohs(((const struct sockaddr_in *)ss)->sin_port);
172 #if HAVE_STRUCT_SOCKADDR_IN6
173 if (ss->ss_family == AF_INET6)
174 return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
179 static void set_port(struct sockaddr_storage *ss, int port)
181 if (ss->ss_family == AF_INET)
182 ((struct sockaddr_in *)ss)->sin_port = htons(port);
183 #if HAVE_STRUCT_SOCKADDR_IN6
184 else if (ss->ss_family == AF_INET6)
185 ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
189 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
192 if (s->nb_ssm_exclude_addrs) {
193 for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
194 if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
198 if (s->nb_ssm_include_addrs) {
199 for (i = 0; i < s->nb_ssm_include_addrs; i++) {
200 if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
209 * add option to url of the form:
210 * "http://host:port/path?option1=val1&option2=val2...
213 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
219 if (strchr(buf, '?'))
220 av_strlcat(buf, "&", buf_size);
222 av_strlcat(buf, "?", buf_size);
223 vsnprintf(buf1, sizeof(buf1), fmt, ap);
224 av_strlcat(buf, buf1, buf_size);
228 static void build_udp_url(RTPContext *s,
229 char *buf, int buf_size,
230 const char *hostname,
231 int port, int local_port,
232 const char *include_sources,
233 const char *exclude_sources)
235 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
237 url_add_option(buf, buf_size, "localport=%d", local_port);
239 url_add_option(buf, buf_size, "ttl=%d", s->ttl);
240 if (s->buffer_size >= 0)
241 url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
242 if (s->pkt_size >= 0)
243 url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
245 url_add_option(buf, buf_size, "connect=1");
247 url_add_option(buf, buf_size, "dscp=%d", s->dscp);
248 url_add_option(buf, buf_size, "fifo_size=0");
249 if (include_sources && include_sources[0])
250 url_add_option(buf, buf_size, "sources=%s", include_sources);
251 if (exclude_sources && exclude_sources[0])
252 url_add_option(buf, buf_size, "block=%s", exclude_sources);
255 static void rtp_parse_addr_list(URLContext *h, char *buf,
256 struct sockaddr_storage ***address_list_ptr,
257 int *address_list_size_ptr)
259 struct addrinfo *ai = NULL;
260 struct sockaddr_storage *source_addr;
261 char tmp = '\0', *p = buf, *next;
263 /* Resolve all of the IPs */
266 next = strchr(p, ',');
273 ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
275 source_addr = av_mallocz(sizeof(struct sockaddr_storage));
281 memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
283 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
285 av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
298 * url syntax: rtp://host:port[?option=val...]
299 * option: 'ttl=n' : set the ttl value (for multicast only)
300 * 'rtcpport=n' : set the remote rtcp port to n
301 * 'localrtpport=n' : set the local rtp port to n
302 * 'localrtcpport=n' : set the local rtcp port to n
303 * 'pkt_size=n' : set max packet size
304 * 'connect=0/1' : do a connect() on the UDP socket
305 * 'sources=ip[,ip]' : list allowed source IP addresses
306 * 'block=ip[,ip]' : list disallowed source IP addresses
307 * 'write_to_source=0/1' : send packets to the source address of the latest received packet
308 * 'dscp=n' : set DSCP value to n (QoS)
310 * 'localport=n' : set the local port to n
312 * if rtcpport isn't set the rtcp port will be the rtp port + 1
313 * if local rtp port isn't set any available port will be used for the local
315 * if the local rtcp port is not set it will be the local rtp port + 1
318 static int rtp_open(URLContext *h, const char *uri, int flags)
320 RTPContext *s = h->priv_data;
321 AVDictionary *fec_opts = NULL;
323 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
324 char *sources = include_sources, *block = exclude_sources;
325 char *fec_protocol = NULL;
329 int i, max_retry_count = 3;
332 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
333 path, sizeof(path), uri);
334 /* extract parameters */
335 if (s->rtcp_port < 0)
336 s->rtcp_port = rtp_port + 1;
338 p = strchr(uri, '?');
340 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
341 s->ttl = strtol(buf, NULL, 10);
343 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
344 s->rtcp_port = strtol(buf, NULL, 10);
346 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
347 s->local_rtpport = strtol(buf, NULL, 10);
349 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
350 s->local_rtpport = strtol(buf, NULL, 10);
352 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
353 s->local_rtcpport = strtol(buf, NULL, 10);
355 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
356 s->pkt_size = strtol(buf, NULL, 10);
358 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
359 s->connect = strtol(buf, NULL, 10);
361 if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
362 s->write_to_source = strtol(buf, NULL, 10);
364 if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
365 s->dscp = strtol(buf, NULL, 10);
367 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
368 av_strlcpy(include_sources, buf, sizeof(include_sources));
370 rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
372 rtp_parse_addr_list(h, s->sources, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
373 sources = s->sources;
375 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
376 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
377 rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
379 rtp_parse_addr_list(h, s->block, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
384 if (s->fec_options_str) {
385 p = s->fec_options_str;
387 if (!(fec_protocol = av_get_token(&p, "="))) {
388 av_log(h, AV_LOG_ERROR, "Failed to parse the FEC protocol value\n");
391 if (strcmp(fec_protocol, "prompeg")) {
392 av_log(h, AV_LOG_ERROR, "Unsupported FEC protocol %s\n", fec_protocol);
396 p = s->fec_options_str + strlen(fec_protocol);
397 while (*p && *p == '=') p++;
399 if (av_dict_parse_string(&fec_opts, p, "=", ":", 0) < 0) {
400 av_log(h, AV_LOG_ERROR, "Failed to parse the FEC options\n");
404 snprintf(buf, sizeof (buf), "%d", s->ttl);
405 av_dict_set(&fec_opts, "ttl", buf, 0);
409 for (i = 0; i < max_retry_count; i++) {
410 build_udp_url(s, buf, sizeof(buf),
411 hostname, rtp_port, s->local_rtpport,
413 if (ffurl_open_whitelist(&s->rtp_hd, buf, flags, &h->interrupt_callback,
414 NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
416 s->local_rtpport = ff_udp_get_local_port(s->rtp_hd);
417 if(s->local_rtpport == 65535) {
418 s->local_rtpport = -1;
421 rtcpflags = flags | AVIO_FLAG_WRITE;
422 if (s->local_rtcpport < 0) {
423 s->local_rtcpport = s->local_rtpport + 1;
424 build_udp_url(s, buf, sizeof(buf),
425 hostname, s->rtcp_port, s->local_rtcpport,
427 if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags,
428 &h->interrupt_callback, NULL,
429 h->protocol_whitelist, h->protocol_blacklist, h) < 0) {
430 s->local_rtpport = s->local_rtcpport = -1;
435 build_udp_url(s, buf, sizeof(buf),
436 hostname, s->rtcp_port, s->local_rtcpport,
438 if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags, &h->interrupt_callback,
439 NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
446 ff_url_join(buf, sizeof(buf), fec_protocol, NULL, hostname, rtp_port, NULL);
447 if (ffurl_open_whitelist(&s->fec_hd, buf, flags, &h->interrupt_callback,
448 &fec_opts, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
452 /* just to ease handle access. XXX: need to suppress direct handle
454 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
455 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
457 h->max_packet_size = s->rtp_hd->max_packet_size;
460 av_free(fec_protocol);
461 av_dict_free(&fec_opts);
467 ffurl_close(s->rtp_hd);
469 ffurl_close(s->rtcp_hd);
470 ffurl_closep(&s->fec_hd);
471 av_free(fec_protocol);
472 av_dict_free(&fec_opts);
476 static int rtp_read(URLContext *h, uint8_t *buf, int size)
478 RTPContext *s = h->priv_data;
480 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
481 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
482 struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
483 socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
486 if (ff_check_interrupt(&h->interrupt_callback))
488 n = poll(p, 2, poll_delay);
490 /* first try RTCP, then RTP */
491 for (i = 1; i >= 0; i--) {
492 if (!(p[i].revents & POLLIN))
494 *addr_lens[i] = sizeof(*addrs[i]);
495 len = recvfrom(p[i].fd, buf, size, 0,
496 (struct sockaddr *)addrs[i], addr_lens[i]);
498 if (ff_neterrno() == AVERROR(EAGAIN) ||
499 ff_neterrno() == AVERROR(EINTR))
503 if (rtp_check_source_lists(s, addrs[i]))
508 if (ff_neterrno() == AVERROR(EINTR))
512 if (h->flags & AVIO_FLAG_NONBLOCK)
513 return AVERROR(EAGAIN);
517 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
519 RTPContext *s = h->priv_data;
524 return AVERROR(EINVAL);
526 if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
527 av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
528 "make sure the RTP muxer is used\n");
530 if (s->write_to_source) {
532 struct sockaddr_storage *source, temp_source;
533 socklen_t *source_len, temp_len;
534 if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
535 av_log(h, AV_LOG_ERROR,
536 "Unable to send packet to source, no packets received yet\n");
537 // Intentionally not returning an error here
541 if (RTP_PT_IS_RTCP(buf[1])) {
543 source = &s->last_rtcp_source;
544 source_len = &s->last_rtcp_source_len;
547 source = &s->last_rtp_source;
548 source_len = &s->last_rtp_source_len;
550 if (!source->ss_family) {
551 source = &temp_source;
552 source_len = &temp_len;
553 if (RTP_PT_IS_RTCP(buf[1])) {
554 temp_source = s->last_rtp_source;
555 temp_len = s->last_rtp_source_len;
556 set_port(source, get_port(source) + 1);
557 av_log(h, AV_LOG_INFO,
558 "Not received any RTCP packets yet, inferring peer port "
559 "from the RTP port\n");
561 temp_source = s->last_rtcp_source;
562 temp_len = s->last_rtcp_source_len;
563 set_port(source, get_port(source) - 1);
564 av_log(h, AV_LOG_INFO,
565 "Not received any RTP packets yet, inferring peer port "
566 "from the RTCP port\n");
570 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
571 ret = ff_network_wait_fd(fd, 1);
575 ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
578 return ret < 0 ? ff_neterrno() : ret;
581 if (RTP_PT_IS_RTCP(buf[1])) {
582 /* RTCP payload type */
585 /* RTP payload type */
589 if ((ret = ffurl_write(hd, buf, size)) < 0) {
593 if (s->fec_hd && !RTP_PT_IS_RTCP(buf[1])) {
594 if ((ret_fec = ffurl_write(s->fec_hd, buf, size)) < 0) {
595 av_log(h, AV_LOG_ERROR, "Failed to send FEC\n");
603 static int rtp_close(URLContext *h)
605 RTPContext *s = h->priv_data;
608 for (i = 0; i < s->nb_ssm_include_addrs; i++)
609 av_freep(&s->ssm_include_addrs[i]);
610 av_freep(&s->ssm_include_addrs);
611 for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
612 av_freep(&s->ssm_exclude_addrs[i]);
613 av_freep(&s->ssm_exclude_addrs);
615 ffurl_close(s->rtp_hd);
616 ffurl_close(s->rtcp_hd);
617 ffurl_closep(&s->fec_hd);
622 * Return the local rtp port used by the RTP connection
623 * @param h media file context
624 * @return the local port number
627 int ff_rtp_get_local_rtp_port(URLContext *h)
629 RTPContext *s = h->priv_data;
630 return ff_udp_get_local_port(s->rtp_hd);
634 * Return the local rtcp port used by the RTP connection
635 * @param h media file context
636 * @return the local port number
639 int ff_rtp_get_local_rtcp_port(URLContext *h)
641 RTPContext *s = h->priv_data;
642 return ff_udp_get_local_port(s->rtcp_hd);
645 static int rtp_get_file_handle(URLContext *h)
647 RTPContext *s = h->priv_data;
651 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
654 RTPContext *s = h->priv_data;
655 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
657 return AVERROR(ENOMEM);
664 const URLProtocol ff_rtp_protocol = {
666 .url_open = rtp_open,
667 .url_read = rtp_read,
668 .url_write = rtp_write,
669 .url_close = rtp_close,
670 .url_get_file_handle = rtp_get_file_handle,
671 .url_get_multi_file_handle = rtp_get_multi_file_handle,
672 .priv_data_size = sizeof(RTPContext),
673 .flags = URL_PROTOCOL_FLAG_NETWORK,
674 .priv_data_class = &rtp_class,