]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpproto.c
Merge commit '2427ac6ccd868811d1fe9df7c64c50ca58abe6f6'
[ffmpeg] / libavformat / rtpproto.c
1 /*
2  * RTP network protocol
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * RTP protocol
25  */
26
27 #include "libavutil/parseutils.h"
28 #include "libavutil/avstring.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "rtp.h"
32 #include "rtpproto.h"
33 #include "url.h"
34
35 #include <stdarg.h>
36 #include "internal.h"
37 #include "network.h"
38 #include "os_support.h"
39 #include <fcntl.h>
40 #if HAVE_POLL_H
41 #include <sys/poll.h>
42 #endif
43
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;
48 } RTPContext;
49
50 /**
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.
54  *
55  * @param h media file context
56  * @param uri of the remote server
57  * @return zero if no error.
58  */
59
60 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
61 {
62     RTPContext *s = h->priv_data;
63     char hostname[256];
64     int port, rtcp_port;
65     const char *p;
66
67     char buf[1024];
68     char path[1024];
69
70     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
71                  path, sizeof(path), uri);
72     rtcp_port = port + 1;
73
74     p = strchr(uri, '?');
75     if (p) {
76         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
77             rtcp_port = strtol(buf, NULL, 10);
78         }
79     }
80
81     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
82     ff_udp_set_remote_url(s->rtp_hd, buf);
83
84     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
85     ff_udp_set_remote_url(s->rtcp_hd, buf);
86     return 0;
87 }
88
89 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
90                                          int type, int family, int flags)
91 {
92     struct addrinfo hints = { 0 }, *res = 0;
93     int error;
94     char service[16];
95
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))) {
101         res = NULL;
102         av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
103     }
104
105     return res;
106 }
107
108 static int compare_addr(const struct sockaddr_storage *a,
109                         const struct sockaddr_storage *b)
110 {
111     if (a->ss_family != b->ss_family)
112         return 1;
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);
116     }
117
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);
123     }
124 #endif
125     return 1;
126 }
127
128 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
129 {
130     int i;
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]))
134                 return 1;
135         }
136     }
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]))
140                 return 0;
141         }
142         return 1;
143     }
144     return 0;
145 }
146
147 /**
148  * add option to url of the form:
149  * "http://host:port/path?option1=val1&option2=val2...
150  */
151
152 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
153 {
154     char buf1[1024];
155     va_list ap;
156
157     va_start(ap, fmt);
158     if (strchr(buf, '?'))
159         av_strlcat(buf, "&", buf_size);
160     else
161         av_strlcat(buf, "?", buf_size);
162     vsnprintf(buf1, sizeof(buf1), fmt, ap);
163     av_strlcat(buf, buf1, buf_size);
164     va_end(ap);
165 }
166
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)
173 {
174     ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
175     if (local_port >= 0)
176         url_add_option(buf, buf_size, "localport=%d", local_port);
177     if (ttl >= 0)
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);
181     if (connect)
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);
188 }
189
190 static void rtp_parse_addr_list(URLContext *h, char *buf,
191                                 struct sockaddr_storage ***address_list_ptr,
192                                 int *address_list_size_ptr)
193 {
194     struct addrinfo *ai = NULL;
195     struct sockaddr_storage *source_addr;
196     char tmp = '\0', *p = buf, *next;
197
198     /* Resolve all of the IPs */
199
200     while (p && p[0]) {
201         next = strchr(p, ',');
202
203         if (next) {
204             tmp = *next;
205             *next = '\0';
206         }
207
208         ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
209         if (ai) {
210             source_addr = av_mallocz(sizeof(struct sockaddr_storage));
211             if (!source_addr)
212                 break;
213
214             memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
215             freeaddrinfo(ai);
216             dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
217         } else {
218             av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
219         }
220
221         if (next) {
222             *next = tmp;
223             p = next + 1;
224         } else {
225             p = NULL;
226         }
227     }
228 }
229
230 /**
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
238  *         'sources=ip[,ip]'  : list allowed source IP addresses
239  *         'block=ip[,ip]'    : list disallowed source IP addresses
240  * deprecated option:
241  *         'localport=n'      : set the local port to n
242  *
243  * if rtcpport isn't set the rtcp port will be the rtp port + 1
244  * if local rtp port isn't set any available port will be used for the local
245  * rtp and rtcp ports
246  * if the local rtcp port is not set it will be the local rtp port + 1
247  */
248
249 static int rtp_open(URLContext *h, const char *uri, int flags)
250 {
251     RTPContext *s = h->priv_data;
252     int rtp_port, rtcp_port,
253         ttl, connect,
254         local_rtp_port, local_rtcp_port, max_packet_size;
255     char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
256     char buf[1024];
257     char path[1024];
258     const char *p;
259
260     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
261                  path, sizeof(path), uri);
262     /* extract parameters */
263     ttl = -1;
264     rtcp_port = rtp_port+1;
265     local_rtp_port = -1;
266     local_rtcp_port = -1;
267     max_packet_size = -1;
268     connect = 0;
269
270     p = strchr(uri, '?');
271     if (p) {
272         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
273             ttl = strtol(buf, NULL, 10);
274         }
275         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
276             rtcp_port = strtol(buf, NULL, 10);
277         }
278         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
279             local_rtp_port = strtol(buf, NULL, 10);
280         }
281         if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
282             local_rtp_port = strtol(buf, NULL, 10);
283         }
284         if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
285             local_rtcp_port = strtol(buf, NULL, 10);
286         }
287         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
288             max_packet_size = strtol(buf, NULL, 10);
289         }
290         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
291             connect = strtol(buf, NULL, 10);
292         }
293         if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
294             av_strlcpy(include_sources, buf, sizeof(include_sources));
295             rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
296         }
297         if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
298             av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
299             rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
300         }
301     }
302
303     build_udp_url(buf, sizeof(buf),
304                   hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
305                   connect, include_sources, exclude_sources);
306     if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
307         goto fail;
308     if (local_rtp_port>=0 && local_rtcp_port<0)
309         local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
310
311     build_udp_url(buf, sizeof(buf),
312                   hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
313                   connect, include_sources, exclude_sources);
314     if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
315         goto fail;
316
317     /* just to ease handle access. XXX: need to suppress direct handle
318        access */
319     s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
320     s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
321
322     h->max_packet_size = s->rtp_hd->max_packet_size;
323     h->is_streamed = 1;
324     return 0;
325
326  fail:
327     if (s->rtp_hd)
328         ffurl_close(s->rtp_hd);
329     if (s->rtcp_hd)
330         ffurl_close(s->rtcp_hd);
331     return AVERROR(EIO);
332 }
333
334 static int rtp_read(URLContext *h, uint8_t *buf, int size)
335 {
336     RTPContext *s = h->priv_data;
337     struct sockaddr_storage from;
338     socklen_t from_len;
339     int len, n, i;
340     struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
341     int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
342
343     for(;;) {
344         if (ff_check_interrupt(&h->interrupt_callback))
345             return AVERROR_EXIT;
346         n = poll(p, 2, poll_delay);
347         if (n > 0) {
348             /* first try RTCP, then RTP */
349             for (i = 1; i >= 0; i--) {
350                 if (!(p[i].revents & POLLIN))
351                     continue;
352                 from_len = sizeof(from);
353                 len = recvfrom(p[i].fd, buf, size, 0,
354                                 (struct sockaddr *)&from, &from_len);
355                 if (len < 0) {
356                     if (ff_neterrno() == AVERROR(EAGAIN) ||
357                         ff_neterrno() == AVERROR(EINTR))
358                         continue;
359                     return AVERROR(EIO);
360                 }
361                 if (rtp_check_source_lists(s, &from))
362                     continue;
363                 return len;
364             }
365         } else if (n < 0) {
366             if (ff_neterrno() == AVERROR(EINTR))
367                 continue;
368             return AVERROR(EIO);
369         }
370         if (h->flags & AVIO_FLAG_NONBLOCK)
371             return AVERROR(EAGAIN);
372     }
373     return len;
374 }
375
376 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
377 {
378     RTPContext *s = h->priv_data;
379     int ret;
380     URLContext *hd;
381
382     if (size < 2)
383         return AVERROR(EINVAL);
384
385     if (RTP_PT_IS_RTCP(buf[1])) {
386         /* RTCP payload type */
387         hd = s->rtcp_hd;
388     } else {
389         /* RTP payload type */
390         hd = s->rtp_hd;
391     }
392
393     ret = ffurl_write(hd, buf, size);
394     return ret;
395 }
396
397 static int rtp_close(URLContext *h)
398 {
399     RTPContext *s = h->priv_data;
400     int i;
401
402     for (i = 0; i < s->nb_ssm_include_addrs; i++)
403         av_free(s->ssm_include_addrs[i]);
404     av_freep(&s->ssm_include_addrs);
405     for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
406         av_free(s->ssm_exclude_addrs[i]);
407     av_freep(&s->ssm_exclude_addrs);
408
409     ffurl_close(s->rtp_hd);
410     ffurl_close(s->rtcp_hd);
411     return 0;
412 }
413
414 /**
415  * Return the local rtp port used by the RTP connection
416  * @param h media file context
417  * @return the local port number
418  */
419
420 int ff_rtp_get_local_rtp_port(URLContext *h)
421 {
422     RTPContext *s = h->priv_data;
423     return ff_udp_get_local_port(s->rtp_hd);
424 }
425
426 /**
427  * Return the local rtcp port used by the RTP connection
428  * @param h media file context
429  * @return the local port number
430  */
431
432 int ff_rtp_get_local_rtcp_port(URLContext *h)
433 {
434     RTPContext *s = h->priv_data;
435     return ff_udp_get_local_port(s->rtcp_hd);
436 }
437
438 static int rtp_get_file_handle(URLContext *h)
439 {
440     RTPContext *s = h->priv_data;
441     return s->rtp_fd;
442 }
443
444 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
445                                      int *numhandles)
446 {
447     RTPContext *s = h->priv_data;
448     int *hs       = *handles = av_malloc(sizeof(**handles) * 2);
449     if (!hs)
450         return AVERROR(ENOMEM);
451     hs[0] = s->rtp_fd;
452     hs[1] = s->rtcp_fd;
453     *numhandles = 2;
454     return 0;
455 }
456
457 URLProtocol ff_rtp_protocol = {
458     .name                      = "rtp",
459     .url_open                  = rtp_open,
460     .url_read                  = rtp_read,
461     .url_write                 = rtp_write,
462     .url_close                 = rtp_close,
463     .url_get_file_handle       = rtp_get_file_handle,
464     .url_get_multi_file_handle = rtp_get_multi_file_handle,
465     .priv_data_size            = sizeof(RTPContext),
466     .flags                     = URL_PROTOCOL_FLAG_NETWORK,
467 };