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"
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 if (include_sources && include_sources[0])
184 url_add_option(buf, buf_size, "sources=%s", include_sources);
185 if (exclude_sources && exclude_sources[0])
186 url_add_option(buf, buf_size, "block=%s", exclude_sources);
189 static void rtp_parse_addr_list(URLContext *h, char *buf,
190 struct sockaddr_storage ***address_list_ptr,
191 int *address_list_size_ptr)
193 struct addrinfo *ai = NULL;
194 struct sockaddr_storage *source_addr;
195 char tmp = '\0', *p = buf, *next;
197 /* Resolve all of the IPs */
200 next = strchr(p, ',');
207 ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
209 source_addr = av_mallocz(sizeof(struct sockaddr_storage));
213 memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
215 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
217 av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
230 * url syntax: rtp://host:port[?option=val...]
231 * option: 'ttl=n' : set the ttl value (for multicast only)
232 * 'rtcpport=n' : set the remote rtcp port to n
233 * 'localrtpport=n' : set the local rtp port to n
234 * 'localrtcpport=n' : set the local rtcp port to n
235 * 'pkt_size=n' : set max packet size
236 * 'connect=0/1' : do a connect() on the UDP socket
238 * 'localport=n' : set the local port to n
239 * 'sources=ip[,ip]' : list allowed source IP addresses
241 * if rtcpport isn't set the rtcp port will be the rtp port + 1
242 * if local rtp port isn't set any available port will be used for the local
244 * if the local rtcp port is not set it will be the local rtp port + 1
247 static int rtp_open(URLContext *h, const char *uri, int flags)
249 RTPContext *s = h->priv_data;
250 int rtp_port, rtcp_port,
252 local_rtp_port, local_rtcp_port, max_packet_size;
253 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
258 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
259 path, sizeof(path), uri);
260 /* extract parameters */
262 rtcp_port = rtp_port+1;
264 local_rtcp_port = -1;
265 max_packet_size = -1;
268 p = strchr(uri, '?');
270 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
271 ttl = strtol(buf, NULL, 10);
273 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
274 rtcp_port = strtol(buf, NULL, 10);
276 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
277 local_rtp_port = strtol(buf, NULL, 10);
279 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
280 local_rtp_port = strtol(buf, NULL, 10);
282 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
283 local_rtcp_port = strtol(buf, NULL, 10);
285 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
286 max_packet_size = strtol(buf, NULL, 10);
288 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
289 connect = strtol(buf, NULL, 10);
291 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
292 av_strlcpy(include_sources, buf, sizeof(include_sources));
293 rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
295 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
296 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
297 rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
301 build_udp_url(buf, sizeof(buf),
302 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
303 connect, include_sources, exclude_sources);
304 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
306 if (local_rtp_port>=0 && local_rtcp_port<0)
307 local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
309 build_udp_url(buf, sizeof(buf),
310 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
311 connect, include_sources, exclude_sources);
312 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
315 /* just to ease handle access. XXX: need to suppress direct handle
317 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
318 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
320 h->max_packet_size = s->rtp_hd->max_packet_size;
326 ffurl_close(s->rtp_hd);
328 ffurl_close(s->rtcp_hd);
332 static int rtp_read(URLContext *h, uint8_t *buf, int size)
334 RTPContext *s = h->priv_data;
335 struct sockaddr_storage from;
338 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
339 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
342 if (ff_check_interrupt(&h->interrupt_callback))
344 n = poll(p, 2, poll_delay);
346 /* first try RTCP, then RTP */
347 for (i = 1; i >= 0; i--) {
348 if (!(p[i].revents & POLLIN))
350 from_len = sizeof(from);
351 len = recvfrom(p[i].fd, buf, size, 0,
352 (struct sockaddr *)&from, &from_len);
354 if (ff_neterrno() == AVERROR(EAGAIN) ||
355 ff_neterrno() == AVERROR(EINTR))
359 if (rtp_check_source_lists(s, &from))
364 if (ff_neterrno() == AVERROR(EINTR))
368 if (h->flags & AVIO_FLAG_NONBLOCK)
369 return AVERROR(EAGAIN);
374 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
376 RTPContext *s = h->priv_data;
381 return AVERROR(EINVAL);
383 if (RTP_PT_IS_RTCP(buf[1])) {
384 /* RTCP payload type */
387 /* RTP payload type */
391 ret = ffurl_write(hd, buf, size);
395 static int rtp_close(URLContext *h)
397 RTPContext *s = h->priv_data;
400 for (i = 0; i < s->nb_ssm_include_addrs; i++)
401 av_free(s->ssm_include_addrs[i]);
402 av_freep(&s->ssm_include_addrs);
403 for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
404 av_free(s->ssm_exclude_addrs[i]);
405 av_freep(&s->ssm_exclude_addrs);
407 ffurl_close(s->rtp_hd);
408 ffurl_close(s->rtcp_hd);
413 * Return the local rtp port used by the RTP connection
414 * @param h media file context
415 * @return the local port number
418 int ff_rtp_get_local_rtp_port(URLContext *h)
420 RTPContext *s = h->priv_data;
421 return ff_udp_get_local_port(s->rtp_hd);
425 * Return the local rtcp port used by the RTP connection
426 * @param h media file context
427 * @return the local port number
430 int ff_rtp_get_local_rtcp_port(URLContext *h)
432 RTPContext *s = h->priv_data;
433 return ff_udp_get_local_port(s->rtcp_hd);
436 static int rtp_get_file_handle(URLContext *h)
438 RTPContext *s = h->priv_data;
442 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
445 RTPContext *s = h->priv_data;
446 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
448 return AVERROR(ENOMEM);
455 URLProtocol ff_rtp_protocol = {
457 .url_open = rtp_open,
458 .url_read = rtp_read,
459 .url_write = rtp_write,
460 .url_close = rtp_close,
461 .url_get_file_handle = rtp_get_file_handle,
462 .url_get_multi_file_handle = rtp_get_multi_file_handle,
463 .priv_data_size = sizeof(RTPContext),
464 .flags = URL_PROTOCOL_FLAG_NETWORK,