]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpproto.c
Voxware MetaSound decoder
[ffmpeg] / libavformat / rtpproto.c
1 /*
2  * RTP network protocol
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
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     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);
187 }
188
189 static void rtp_parse_addr_list(URLContext *h, char *buf,
190                                 struct sockaddr_storage ***address_list_ptr,
191                                 int *address_list_size_ptr)
192 {
193     struct addrinfo *ai = NULL;
194     struct sockaddr_storage *source_addr;
195     char tmp = '\0', *p = buf, *next;
196
197     /* Resolve all of the IPs */
198
199     while (p && p[0]) {
200         next = strchr(p, ',');
201
202         if (next) {
203             tmp = *next;
204             *next = '\0';
205         }
206
207         ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
208         if (ai) {
209             source_addr = av_mallocz(sizeof(struct sockaddr_storage));
210             if (!source_addr)
211                 break;
212
213             memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
214             freeaddrinfo(ai);
215             dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
216         } else {
217             av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
218         }
219
220         if (next) {
221             *next = tmp;
222             p = next + 1;
223         } else {
224             p = NULL;
225         }
226     }
227 }
228
229 /**
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
237  * deprecated option:
238  *         'localport=n'      : set the local port to n
239  *         'sources=ip[,ip]'  : list allowed source IP addresses
240  *
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
243  * rtp and rtcp ports
244  * if the local rtcp port is not set it will be the local rtp port + 1
245  */
246
247 static int rtp_open(URLContext *h, const char *uri, int flags)
248 {
249     RTPContext *s = h->priv_data;
250     int rtp_port, rtcp_port,
251         ttl, connect,
252         local_rtp_port, local_rtcp_port, max_packet_size;
253     char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
254     char buf[1024];
255     char path[1024];
256     const char *p;
257
258     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
259                  path, sizeof(path), uri);
260     /* extract parameters */
261     ttl = -1;
262     rtcp_port = rtp_port+1;
263     local_rtp_port = -1;
264     local_rtcp_port = -1;
265     max_packet_size = -1;
266     connect = 0;
267
268     p = strchr(uri, '?');
269     if (p) {
270         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
271             ttl = strtol(buf, NULL, 10);
272         }
273         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
274             rtcp_port = strtol(buf, NULL, 10);
275         }
276         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
277             local_rtp_port = strtol(buf, NULL, 10);
278         }
279         if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
280             local_rtp_port = strtol(buf, NULL, 10);
281         }
282         if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
283             local_rtcp_port = strtol(buf, NULL, 10);
284         }
285         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
286             max_packet_size = strtol(buf, NULL, 10);
287         }
288         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
289             connect = strtol(buf, NULL, 10);
290         }
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);
294         }
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);
298         }
299     }
300
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)
305         goto fail;
306     if (local_rtp_port>=0 && local_rtcp_port<0)
307         local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
308
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)
313         goto fail;
314
315     /* just to ease handle access. XXX: need to suppress direct handle
316        access */
317     s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
318     s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
319
320     h->max_packet_size = s->rtp_hd->max_packet_size;
321     h->is_streamed = 1;
322     return 0;
323
324  fail:
325     if (s->rtp_hd)
326         ffurl_close(s->rtp_hd);
327     if (s->rtcp_hd)
328         ffurl_close(s->rtcp_hd);
329     return AVERROR(EIO);
330 }
331
332 static int rtp_read(URLContext *h, uint8_t *buf, int size)
333 {
334     RTPContext *s = h->priv_data;
335     struct sockaddr_storage from;
336     socklen_t from_len;
337     int len, n, i;
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;
340
341     for(;;) {
342         if (ff_check_interrupt(&h->interrupt_callback))
343             return AVERROR_EXIT;
344         n = poll(p, 2, poll_delay);
345         if (n > 0) {
346             /* first try RTCP, then RTP */
347             for (i = 1; i >= 0; i--) {
348                 if (!(p[i].revents & POLLIN))
349                     continue;
350                 from_len = sizeof(from);
351                 len = recvfrom(p[i].fd, buf, size, 0,
352                                 (struct sockaddr *)&from, &from_len);
353                 if (len < 0) {
354                     if (ff_neterrno() == AVERROR(EAGAIN) ||
355                         ff_neterrno() == AVERROR(EINTR))
356                         continue;
357                     return AVERROR(EIO);
358                 }
359                 if (rtp_check_source_lists(s, &from))
360                     continue;
361                 return len;
362             }
363         } else if (n < 0) {
364             if (ff_neterrno() == AVERROR(EINTR))
365                 continue;
366             return AVERROR(EIO);
367         }
368         if (h->flags & AVIO_FLAG_NONBLOCK)
369             return AVERROR(EAGAIN);
370     }
371     return len;
372 }
373
374 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
375 {
376     RTPContext *s = h->priv_data;
377     int ret;
378     URLContext *hd;
379
380     if (size < 2)
381         return AVERROR(EINVAL);
382
383     if (RTP_PT_IS_RTCP(buf[1])) {
384         /* RTCP payload type */
385         hd = s->rtcp_hd;
386     } else {
387         /* RTP payload type */
388         hd = s->rtp_hd;
389     }
390
391     ret = ffurl_write(hd, buf, size);
392     return ret;
393 }
394
395 static int rtp_close(URLContext *h)
396 {
397     RTPContext *s = h->priv_data;
398     int i;
399
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);
406
407     ffurl_close(s->rtp_hd);
408     ffurl_close(s->rtcp_hd);
409     return 0;
410 }
411
412 /**
413  * Return the local rtp port used by the RTP connection
414  * @param h media file context
415  * @return the local port number
416  */
417
418 int ff_rtp_get_local_rtp_port(URLContext *h)
419 {
420     RTPContext *s = h->priv_data;
421     return ff_udp_get_local_port(s->rtp_hd);
422 }
423
424 /**
425  * Return the local rtcp port used by the RTP connection
426  * @param h media file context
427  * @return the local port number
428  */
429
430 int ff_rtp_get_local_rtcp_port(URLContext *h)
431 {
432     RTPContext *s = h->priv_data;
433     return ff_udp_get_local_port(s->rtcp_hd);
434 }
435
436 static int rtp_get_file_handle(URLContext *h)
437 {
438     RTPContext *s = h->priv_data;
439     return s->rtp_fd;
440 }
441
442 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
443                                      int *numhandles)
444 {
445     RTPContext *s = h->priv_data;
446     int *hs       = *handles = av_malloc(sizeof(**handles) * 2);
447     if (!hs)
448         return AVERROR(ENOMEM);
449     hs[0] = s->rtp_fd;
450     hs[1] = s->rtcp_fd;
451     *numhandles = 2;
452     return 0;
453 }
454
455 URLProtocol ff_rtp_protocol = {
456     .name                      = "rtp",
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,
465 };