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;
69 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
70 path, sizeof(path), uri);
72 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
73 ff_udp_set_remote_url(s->rtp_hd, buf);
75 ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
76 ff_udp_set_remote_url(s->rtcp_hd, buf);
80 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
81 int type, int family, int flags)
83 struct addrinfo hints = { 0 }, *res = 0;
87 snprintf(service, sizeof(service), "%d", port);
88 hints.ai_socktype = type;
89 hints.ai_family = family;
90 hints.ai_flags = flags;
91 if ((error = getaddrinfo(hostname, service, &hints, &res))) {
93 av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
99 static int compare_addr(const struct sockaddr_storage *a,
100 const struct sockaddr_storage *b)
102 if (a->ss_family != b->ss_family)
104 if (a->ss_family == AF_INET) {
105 return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
106 ((const struct sockaddr_in *)b)->sin_addr.s_addr);
109 #if defined(IPPROTO_IPV6)
110 if (a->ss_family == AF_INET6) {
111 const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
112 const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
113 return memcmp(s6_addr_a, s6_addr_b, 16);
119 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
122 if (s->nb_ssm_exclude_addrs) {
123 for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
124 if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
128 if (s->nb_ssm_include_addrs) {
129 for (i = 0; i < s->nb_ssm_include_addrs; i++) {
130 if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
139 * add option to url of the form:
140 * "http://host:port/path?option1=val1&option2=val2...
143 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
149 if (strchr(buf, '?'))
150 av_strlcat(buf, "&", buf_size);
152 av_strlcat(buf, "?", buf_size);
153 vsnprintf(buf1, sizeof(buf1), fmt, ap);
154 av_strlcat(buf, buf1, buf_size);
158 static void build_udp_url(char *buf, int buf_size,
159 const char *hostname, int port,
160 int local_port, int ttl,
161 int max_packet_size, int connect,
162 const char *include_sources,
163 const char *exclude_sources)
165 ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
167 url_add_option(buf, buf_size, "localport=%d", local_port);
169 url_add_option(buf, buf_size, "ttl=%d", ttl);
170 if (max_packet_size >=0)
171 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
173 url_add_option(buf, buf_size, "connect=1");
174 url_add_option(buf, buf_size, "fifo_size=0");
175 if (include_sources && include_sources[0])
176 url_add_option(buf, buf_size, "sources=%s", include_sources);
177 if (exclude_sources && exclude_sources[0])
178 url_add_option(buf, buf_size, "block=%s", exclude_sources);
181 static void rtp_parse_addr_list(URLContext *h, char *buf,
182 struct sockaddr_storage ***address_list_ptr,
183 int *address_list_size_ptr)
185 struct addrinfo *ai = NULL;
186 struct sockaddr_storage *source_addr;
187 char tmp = '\0', *p = buf, *next;
189 /* Resolve all of the IPs */
192 next = strchr(p, ',');
199 ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
201 source_addr = av_mallocz(sizeof(struct sockaddr_storage));
205 memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
207 dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
209 av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
222 * url syntax: rtp://host:port[?option=val...]
223 * option: 'ttl=n' : set the ttl value (for multicast only)
224 * 'rtcpport=n' : set the remote rtcp port to n
225 * 'localrtpport=n' : set the local rtp port to n
226 * 'localrtcpport=n' : set the local rtcp port to n
227 * 'pkt_size=n' : set max packet size
228 * 'connect=0/1' : do a connect() on the UDP socket
230 * 'localport=n' : set the local port to n
231 * 'sources=ip[,ip]' : list allowed source IP addresses
233 * if rtcpport isn't set the rtcp port will be the rtp port + 1
234 * if local rtp port isn't set any available port will be used for the local
236 * if the local rtcp port is not set it will be the local rtp port + 1
239 static int rtp_open(URLContext *h, const char *uri, int flags)
241 RTPContext *s = h->priv_data;
242 int rtp_port, rtcp_port,
244 local_rtp_port, local_rtcp_port, max_packet_size;
245 char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
250 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
251 path, sizeof(path), uri);
252 /* extract parameters */
254 rtcp_port = rtp_port+1;
256 local_rtcp_port = -1;
257 max_packet_size = -1;
260 p = strchr(uri, '?');
262 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
263 ttl = strtol(buf, NULL, 10);
265 if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
266 rtcp_port = strtol(buf, NULL, 10);
268 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
269 local_rtp_port = strtol(buf, NULL, 10);
271 if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
272 local_rtp_port = strtol(buf, NULL, 10);
274 if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
275 local_rtcp_port = strtol(buf, NULL, 10);
277 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
278 max_packet_size = strtol(buf, NULL, 10);
280 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
281 connect = strtol(buf, NULL, 10);
283 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
284 av_strlcpy(include_sources, buf, sizeof(include_sources));
285 rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
287 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
288 av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
289 rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
293 build_udp_url(buf, sizeof(buf),
294 hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
295 connect, include_sources, exclude_sources);
296 if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
298 if (local_rtp_port>=0 && local_rtcp_port<0)
299 local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
301 build_udp_url(buf, sizeof(buf),
302 hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
303 connect, include_sources, exclude_sources);
304 if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
307 /* just to ease handle access. XXX: need to suppress direct handle
309 s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
310 s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
312 h->max_packet_size = s->rtp_hd->max_packet_size;
318 ffurl_close(s->rtp_hd);
320 ffurl_close(s->rtcp_hd);
324 static int rtp_read(URLContext *h, uint8_t *buf, int size)
326 RTPContext *s = h->priv_data;
327 struct sockaddr_storage from;
330 struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
331 int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
334 if (ff_check_interrupt(&h->interrupt_callback))
336 n = poll(p, 2, poll_delay);
338 /* first try RTCP, then RTP */
339 for (i = 1; i >= 0; i--) {
340 if (!(p[i].revents & POLLIN))
342 from_len = sizeof(from);
343 len = recvfrom(p[i].fd, buf, size, 0,
344 (struct sockaddr *)&from, &from_len);
346 if (ff_neterrno() == AVERROR(EAGAIN) ||
347 ff_neterrno() == AVERROR(EINTR))
351 if (rtp_check_source_lists(s, &from))
356 if (ff_neterrno() == AVERROR(EINTR))
360 if (h->flags & AVIO_FLAG_NONBLOCK)
361 return AVERROR(EAGAIN);
366 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
368 RTPContext *s = h->priv_data;
372 if (RTP_PT_IS_RTCP(buf[1])) {
373 /* RTCP payload type */
376 /* RTP payload type */
380 ret = ffurl_write(hd, buf, size);
384 static int rtp_close(URLContext *h)
386 RTPContext *s = h->priv_data;
389 for (i = 0; i < s->nb_ssm_include_addrs; i++)
390 av_free(s->ssm_include_addrs[i]);
391 av_freep(&s->ssm_include_addrs);
392 for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
393 av_free(s->ssm_exclude_addrs[i]);
394 av_freep(&s->ssm_exclude_addrs);
396 ffurl_close(s->rtp_hd);
397 ffurl_close(s->rtcp_hd);
402 * Return the local rtp port used by the RTP connection
403 * @param h media file context
404 * @return the local port number
407 int ff_rtp_get_local_rtp_port(URLContext *h)
409 RTPContext *s = h->priv_data;
410 return ff_udp_get_local_port(s->rtp_hd);
414 * Return the local rtcp port used by the RTP connection
415 * @param h media file context
416 * @return the local port number
419 int ff_rtp_get_local_rtcp_port(URLContext *h)
421 RTPContext *s = h->priv_data;
422 return ff_udp_get_local_port(s->rtcp_hd);
425 static int rtp_get_file_handle(URLContext *h)
427 RTPContext *s = h->priv_data;
431 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
434 RTPContext *s = h->priv_data;
435 int *hs = *handles = av_malloc(sizeof(**handles) * 2);
437 return AVERROR(ENOMEM);
444 URLProtocol ff_rtp_protocol = {
446 .url_open = rtp_open,
447 .url_read = rtp_read,
448 .url_write = rtp_write,
449 .url_close = rtp_close,
450 .url_get_file_handle = rtp_get_file_handle,
451 .url_get_multi_file_handle = rtp_get_multi_file_handle,
452 .priv_data_size = sizeof(RTPContext),
453 .flags = URL_PROTOCOL_FLAG_NETWORK,