]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpproto.c
Merge commit 'abe5268c3328bf0e8fcfb7dc6e231b8920177c3a'
[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  * deprecated option:
239  *         'localport=n'      : set the local port to n
240  *         'sources=ip[,ip]'  : list allowed source IP addresses
241  *
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
244  * rtp and rtcp ports
245  * if the local rtcp port is not set it will be the local rtp port + 1
246  */
247
248 static int rtp_open(URLContext *h, const char *uri, int flags)
249 {
250     RTPContext *s = h->priv_data;
251     int rtp_port, rtcp_port,
252         ttl, connect,
253         local_rtp_port, local_rtcp_port, max_packet_size;
254     char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
255     char buf[1024];
256     char path[1024];
257     const char *p;
258
259     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
260                  path, sizeof(path), uri);
261     /* extract parameters */
262     ttl = -1;
263     rtcp_port = rtp_port+1;
264     local_rtp_port = -1;
265     local_rtcp_port = -1;
266     max_packet_size = -1;
267     connect = 0;
268
269     p = strchr(uri, '?');
270     if (p) {
271         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
272             ttl = strtol(buf, NULL, 10);
273         }
274         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
275             rtcp_port = strtol(buf, NULL, 10);
276         }
277         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
278             local_rtp_port = strtol(buf, NULL, 10);
279         }
280         if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
281             local_rtp_port = strtol(buf, NULL, 10);
282         }
283         if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
284             local_rtcp_port = strtol(buf, NULL, 10);
285         }
286         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
287             max_packet_size = strtol(buf, NULL, 10);
288         }
289         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
290             connect = strtol(buf, NULL, 10);
291         }
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);
295         }
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);
299         }
300     }
301
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)
306         goto fail;
307     if (local_rtp_port>=0 && local_rtcp_port<0)
308         local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
309
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)
314         goto fail;
315
316     /* just to ease handle access. XXX: need to suppress direct handle
317        access */
318     s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
319     s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
320
321     h->max_packet_size = s->rtp_hd->max_packet_size;
322     h->is_streamed = 1;
323     return 0;
324
325  fail:
326     if (s->rtp_hd)
327         ffurl_close(s->rtp_hd);
328     if (s->rtcp_hd)
329         ffurl_close(s->rtcp_hd);
330     return AVERROR(EIO);
331 }
332
333 static int rtp_read(URLContext *h, uint8_t *buf, int size)
334 {
335     RTPContext *s = h->priv_data;
336     struct sockaddr_storage from;
337     socklen_t from_len;
338     int len, n, i;
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;
341
342     for(;;) {
343         if (ff_check_interrupt(&h->interrupt_callback))
344             return AVERROR_EXIT;
345         n = poll(p, 2, poll_delay);
346         if (n > 0) {
347             /* first try RTCP, then RTP */
348             for (i = 1; i >= 0; i--) {
349                 if (!(p[i].revents & POLLIN))
350                     continue;
351                 from_len = sizeof(from);
352                 len = recvfrom(p[i].fd, buf, size, 0,
353                                 (struct sockaddr *)&from, &from_len);
354                 if (len < 0) {
355                     if (ff_neterrno() == AVERROR(EAGAIN) ||
356                         ff_neterrno() == AVERROR(EINTR))
357                         continue;
358                     return AVERROR(EIO);
359                 }
360                 if (rtp_check_source_lists(s, &from))
361                     continue;
362                 return len;
363             }
364         } else if (n < 0) {
365             if (ff_neterrno() == AVERROR(EINTR))
366                 continue;
367             return AVERROR(EIO);
368         }
369         if (h->flags & AVIO_FLAG_NONBLOCK)
370             return AVERROR(EAGAIN);
371     }
372     return len;
373 }
374
375 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
376 {
377     RTPContext *s = h->priv_data;
378     int ret;
379     URLContext *hd;
380
381     if (size < 2)
382         return AVERROR(EINVAL);
383
384     if (RTP_PT_IS_RTCP(buf[1])) {
385         /* RTCP payload type */
386         hd = s->rtcp_hd;
387     } else {
388         /* RTP payload type */
389         hd = s->rtp_hd;
390     }
391
392     ret = ffurl_write(hd, buf, size);
393     return ret;
394 }
395
396 static int rtp_close(URLContext *h)
397 {
398     RTPContext *s = h->priv_data;
399     int i;
400
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);
407
408     ffurl_close(s->rtp_hd);
409     ffurl_close(s->rtcp_hd);
410     return 0;
411 }
412
413 /**
414  * Return the local rtp port used by the RTP connection
415  * @param h media file context
416  * @return the local port number
417  */
418
419 int ff_rtp_get_local_rtp_port(URLContext *h)
420 {
421     RTPContext *s = h->priv_data;
422     return ff_udp_get_local_port(s->rtp_hd);
423 }
424
425 /**
426  * Return the local rtcp port used by the RTP connection
427  * @param h media file context
428  * @return the local port number
429  */
430
431 int ff_rtp_get_local_rtcp_port(URLContext *h)
432 {
433     RTPContext *s = h->priv_data;
434     return ff_udp_get_local_port(s->rtcp_hd);
435 }
436
437 static int rtp_get_file_handle(URLContext *h)
438 {
439     RTPContext *s = h->priv_data;
440     return s->rtp_fd;
441 }
442
443 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
444                                      int *numhandles)
445 {
446     RTPContext *s = h->priv_data;
447     int *hs       = *handles = av_malloc(sizeof(**handles) * 2);
448     if (!hs)
449         return AVERROR(ENOMEM);
450     hs[0] = s->rtp_fd;
451     hs[1] = s->rtcp_fd;
452     *numhandles = 2;
453     return 0;
454 }
455
456 URLProtocol ff_rtp_protocol = {
457     .name                      = "rtp",
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,
466 };