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"
30 #include "avio_internal.h"
38 #include "os_support.h"
44 typedef struct RTPContext {
45 URLContext *rtp_hd, *rtcp_hd;
46 int rtp_fd, rtcp_fd, nb_ssm_include_addrs, nb_ssm_exclude_addrs;
47 struct sockaddr_storage **ssm_include_addrs, **ssm_exclude_addrs;
51 * If no filename is given to av_open_input_file because you want to
52 * get the local port first, then you must call this function to set
53 * the remote server address.
55 * @param h media file context
56 * @param uri of the remote server
57 * @return zero if no error.
60 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
62 RTPContext *s = h->priv_data;
70 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
71 path, sizeof(path), uri);
76 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
77 rtcp_port = strtol(buf, NULL, 10);
81 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
82 ff_udp_set_remote_url(s->rtp_hd, buf);
84 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
85 ff_udp_set_remote_url(s->rtcp_hd, buf);
89 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
90 int type, int family, int flags)
92 struct addrinfo hints = { 0 }, *res = 0;
96 snprintf(service, sizeof(service), "%d", port);
97 hints.ai_socktype = type;
98 hints.ai_family = family;
99 hints.ai_flags = flags;
100 if ((error = getaddrinfo(hostname, service, &hints, &res))) {
102 av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
108 static int compare_addr(const struct sockaddr_storage *a,
109 const struct sockaddr_storage *b)
111 if (a->ss_family != b->ss_family)
113 if (a->ss_family == AF_INET) {
114 return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
115 ((const struct sockaddr_in *)b)->sin_addr.s_addr);
118 #if defined(IPPROTO_IPV6)
119 if (a->ss_family == AF_INET6) {
120 const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
121 const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
122 return memcmp(s6_addr_a, s6_addr_b, 16);
128 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
131 if (s->nb_ssm_exclude_addrs) {
132 for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
133 if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
137 if (s->nb_ssm_include_addrs) {
138 for (i = 0; i < s->nb_ssm_include_addrs; i++) {
139 if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
148 * add option to url of the form:
149 * "http://host:port/path?option1=val1&option2=val2...
152 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
158 if (strchr(buf, '?'))
159 av_strlcat(buf, "&", buf_size);
161 av_strlcat(buf, "?", buf_size);
162 vsnprintf(buf1, sizeof(buf1), fmt, ap);
163 av_strlcat(buf, buf1, buf_size);
167 static void build_udp_url(char *buf, int buf_size,
168 const char *hostname, int port,
169 int local_port, int ttl,
170 int max_packet_size, int connect,
171 const char *include_sources,
172 const char *exclude_sources)
174 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
176 url_add_option(buf, buf_size, "localport=%d", local_port);
178 url_add_option(buf, buf_size, "ttl=%d", ttl);
179 if (max_packet_size >=0)
180 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
182 url_add_option(buf, buf_size, "connect=1");
183 url_add_option(buf, buf_size, "fifo_size=0");
184 if (include_sources && include_sources[0])
185 url_add_option(buf, buf_size, "sources=%s", include_sources);
186 if (exclude_sources && exclude_sources[0])
187 url_add_option(buf, buf_size, "block=%s", exclude_sources);
190 static void rtp_parse_addr_list(URLContext *h, char *buf,
191 struct sockaddr_storage ***address_list_ptr,
192 int *address_list_size_ptr)
194 struct addrinfo *ai = NULL;
195 struct sockaddr_storage *source_addr;
196 char tmp = '\0', *p = buf, *next;
198 /* Resolve all of the IPs */
201 next = strchr(p, ',');
208 ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
210 source_addr = av_mallocz(sizeof(struct sockaddr_storage));
214 memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
216 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
218 av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
231 * url syntax: rtp://host:port[?option=val...]
232 * option: 'ttl=n' : set the ttl value (for multicast only)
233 * 'rtcpport=n' : set the remote rtcp port to n
234 * 'localrtpport=n' : set the local rtp port to n
235 * 'localrtcpport=n' : set the local rtcp port to n
236 * 'pkt_size=n' : set max packet size
237 * 'connect=0/1' : do a connect() on the UDP socket
239 * 'localport=n' : set the local port to n
240 * 'sources=ip[,ip]' : list allowed source IP addresses
242 * if rtcpport isn't set the rtcp port will be the rtp port + 1
243 * if local rtp port isn't set any available port will be used for the local
245 * if the local rtcp port is not set it will be the local rtp port + 1
248 static int rtp_open(URLContext *h, const char *uri, int flags)
250 RTPContext *s = h->priv_data;
251 int rtp_port, rtcp_port,
253 local_rtp_port, local_rtcp_port, max_packet_size;
254 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
259 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
260 path, sizeof(path), uri);
261 /* extract parameters */
263 rtcp_port = rtp_port+1;
265 local_rtcp_port = -1;
266 max_packet_size = -1;
269 p = strchr(uri, '?');
271 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
272 ttl = strtol(buf, NULL, 10);
274 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
275 rtcp_port = strtol(buf, NULL, 10);
277 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
278 local_rtp_port = strtol(buf, NULL, 10);
280 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
281 local_rtp_port = strtol(buf, NULL, 10);
283 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
284 local_rtcp_port = strtol(buf, NULL, 10);
286 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
287 max_packet_size = strtol(buf, NULL, 10);
289 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
290 connect = strtol(buf, NULL, 10);
292 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
293 av_strlcpy(include_sources, buf, sizeof(include_sources));
294 rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
296 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
297 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
298 rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
302 build_udp_url(buf, sizeof(buf),
303 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
304 connect, include_sources, exclude_sources);
305 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
307 if (local_rtp_port>=0 && local_rtcp_port<0)
308 local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
310 build_udp_url(buf, sizeof(buf),
311 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
312 connect, include_sources, exclude_sources);
313 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
316 /* just to ease handle access. XXX: need to suppress direct handle
318 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
319 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
321 h->max_packet_size = s->rtp_hd->max_packet_size;
327 ffurl_close(s->rtp_hd);
329 ffurl_close(s->rtcp_hd);
333 static int rtp_read(URLContext *h, uint8_t *buf, int size)
335 RTPContext *s = h->priv_data;
336 struct sockaddr_storage from;
339 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
340 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
343 if (ff_check_interrupt(&h->interrupt_callback))
345 n = poll(p, 2, poll_delay);
347 /* first try RTCP, then RTP */
348 for (i = 1; i >= 0; i--) {
349 if (!(p[i].revents & POLLIN))
351 from_len = sizeof(from);
352 len = recvfrom(p[i].fd, buf, size, 0,
353 (struct sockaddr *)&from, &from_len);
355 if (ff_neterrno() == AVERROR(EAGAIN) ||
356 ff_neterrno() == AVERROR(EINTR))
360 if (rtp_check_source_lists(s, &from))
365 if (ff_neterrno() == AVERROR(EINTR))
369 if (h->flags & AVIO_FLAG_NONBLOCK)
370 return AVERROR(EAGAIN);
375 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
377 RTPContext *s = h->priv_data;
382 return AVERROR(EINVAL);
384 if (RTP_PT_IS_RTCP(buf[1])) {
385 /* RTCP payload type */
388 /* RTP payload type */
392 ret = ffurl_write(hd, buf, size);
396 static int rtp_close(URLContext *h)
398 RTPContext *s = h->priv_data;
401 for (i = 0; i < s->nb_ssm_include_addrs; i++)
402 av_free(s->ssm_include_addrs[i]);
403 av_freep(&s->ssm_include_addrs);
404 for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
405 av_free(s->ssm_exclude_addrs[i]);
406 av_freep(&s->ssm_exclude_addrs);
408 ffurl_close(s->rtp_hd);
409 ffurl_close(s->rtcp_hd);
414 * Return the local rtp port used by the RTP connection
415 * @param h media file context
416 * @return the local port number
419 int ff_rtp_get_local_rtp_port(URLContext *h)
421 RTPContext *s = h->priv_data;
422 return ff_udp_get_local_port(s->rtp_hd);
426 * Return the local rtcp port used by the RTP connection
427 * @param h media file context
428 * @return the local port number
431 int ff_rtp_get_local_rtcp_port(URLContext *h)
433 RTPContext *s = h->priv_data;
434 return ff_udp_get_local_port(s->rtcp_hd);
437 static int rtp_get_file_handle(URLContext *h)
439 RTPContext *s = h->priv_data;
443 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
446 RTPContext *s = h->priv_data;
447 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
449 return AVERROR(ENOMEM);
456 URLProtocol ff_rtp_protocol = {
458 .url_open = rtp_open,
459 .url_read = rtp_read,
460 .url_write = rtp_write,
461 .url_close = rtp_close,
462 .url_get_file_handle = rtp_get_file_handle,
463 .url_get_multi_file_handle = rtp_get_multi_file_handle,
464 .priv_data_size = sizeof(RTPContext),
465 .flags = URL_PROTOCOL_FLAG_NETWORK,