]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpproto.c
Merge commit 'c7e921a54ffe7feb9f695c82f0a0764ab8d0f62b'
[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;
65
66     char buf[1024];
67     char path[1024];
68
69     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
70                  path, sizeof(path), uri);
71
72     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
73     ff_udp_set_remote_url(s->rtp_hd, buf);
74
75     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
76     ff_udp_set_remote_url(s->rtcp_hd, buf);
77     return 0;
78 }
79
80 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
81                                          int type, int family, int flags)
82 {
83     struct addrinfo hints = { 0 }, *res = 0;
84     int error;
85     char service[16];
86
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))) {
92         res = NULL;
93         av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
94     }
95
96     return res;
97 }
98
99 static int compare_addr(const struct sockaddr_storage *a,
100                         const struct sockaddr_storage *b)
101 {
102     if (a->ss_family != b->ss_family)
103         return 1;
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);
107     }
108
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);
114     }
115 #endif
116     return 1;
117 }
118
119 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
120 {
121     int i;
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]))
125                 return 1;
126         }
127     }
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]))
131                 return 0;
132         }
133         return 1;
134     }
135     return 0;
136 }
137
138 /**
139  * add option to url of the form:
140  * "http://host:port/path?option1=val1&option2=val2...
141  */
142
143 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
144 {
145     char buf1[1024];
146     va_list ap;
147
148     va_start(ap, fmt);
149     if (strchr(buf, '?'))
150         av_strlcat(buf, "&", buf_size);
151     else
152         av_strlcat(buf, "?", buf_size);
153     vsnprintf(buf1, sizeof(buf1), fmt, ap);
154     av_strlcat(buf, buf1, buf_size);
155     va_end(ap);
156 }
157
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)
164 {
165     ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
166     if (local_port >= 0)
167         url_add_option(buf, buf_size, "localport=%d", local_port);
168     if (ttl >= 0)
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);
172     if (connect)
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);
179 }
180
181 static void rtp_parse_addr_list(URLContext *h, char *buf,
182                                 struct sockaddr_storage ***address_list_ptr,
183                                 int *address_list_size_ptr)
184 {
185     struct addrinfo *ai = NULL;
186     struct sockaddr_storage *source_addr;
187     char tmp = '\0', *p = buf, *next;
188
189     /* Resolve all of the IPs */
190
191     while (p && p[0]) {
192         next = strchr(p, ',');
193
194         if (next) {
195             tmp = *next;
196             *next = '\0';
197         }
198
199         ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
200         if (ai) {
201             source_addr = av_mallocz(sizeof(struct sockaddr_storage));
202             if (!source_addr)
203                 break;
204
205             memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
206             freeaddrinfo(ai);
207             dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
208         } else {
209             av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
210         }
211
212         if (next) {
213             *next = tmp;
214             p = next + 1;
215         } else {
216             p = NULL;
217         }
218     }
219 }
220
221 /**
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
229  * deprecated option:
230  *         'localport=n'      : set the local port to n
231  *         'sources=ip[,ip]'  : list allowed source IP addresses
232  *
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
235  * rtp and rtcp ports
236  * if the local rtcp port is not set it will be the local rtp port + 1
237  */
238
239 static int rtp_open(URLContext *h, const char *uri, int flags)
240 {
241     RTPContext *s = h->priv_data;
242     int rtp_port, rtcp_port,
243         ttl, connect,
244         local_rtp_port, local_rtcp_port, max_packet_size;
245     char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
246     char buf[1024];
247     char path[1024];
248     const char *p;
249
250     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
251                  path, sizeof(path), uri);
252     /* extract parameters */
253     ttl = -1;
254     rtcp_port = rtp_port+1;
255     local_rtp_port = -1;
256     local_rtcp_port = -1;
257     max_packet_size = -1;
258     connect = 0;
259
260     p = strchr(uri, '?');
261     if (p) {
262         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
263             ttl = strtol(buf, NULL, 10);
264         }
265         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
266             rtcp_port = strtol(buf, NULL, 10);
267         }
268         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
269             local_rtp_port = strtol(buf, NULL, 10);
270         }
271         if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
272             local_rtp_port = strtol(buf, NULL, 10);
273         }
274         if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
275             local_rtcp_port = strtol(buf, NULL, 10);
276         }
277         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
278             max_packet_size = strtol(buf, NULL, 10);
279         }
280         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
281             connect = strtol(buf, NULL, 10);
282         }
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);
286         }
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);
290         }
291     }
292
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)
297         goto fail;
298     if (local_rtp_port>=0 && local_rtcp_port<0)
299         local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
300
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)
305         goto fail;
306
307     /* just to ease handle access. XXX: need to suppress direct handle
308        access */
309     s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
310     s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
311
312     h->max_packet_size = s->rtp_hd->max_packet_size;
313     h->is_streamed = 1;
314     return 0;
315
316  fail:
317     if (s->rtp_hd)
318         ffurl_close(s->rtp_hd);
319     if (s->rtcp_hd)
320         ffurl_close(s->rtcp_hd);
321     return AVERROR(EIO);
322 }
323
324 static int rtp_read(URLContext *h, uint8_t *buf, int size)
325 {
326     RTPContext *s = h->priv_data;
327     struct sockaddr_storage from;
328     socklen_t from_len;
329     int len, n, i;
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;
332
333     for(;;) {
334         if (ff_check_interrupt(&h->interrupt_callback))
335             return AVERROR_EXIT;
336         n = poll(p, 2, poll_delay);
337         if (n > 0) {
338             /* first try RTCP, then RTP */
339             for (i = 1; i >= 0; i--) {
340                 if (!(p[i].revents & POLLIN))
341                     continue;
342                 from_len = sizeof(from);
343                 len = recvfrom(p[i].fd, buf, size, 0,
344                                 (struct sockaddr *)&from, &from_len);
345                 if (len < 0) {
346                     if (ff_neterrno() == AVERROR(EAGAIN) ||
347                         ff_neterrno() == AVERROR(EINTR))
348                         continue;
349                     return AVERROR(EIO);
350                 }
351                 if (rtp_check_source_lists(s, &from))
352                     continue;
353                 return len;
354             }
355         } else if (n < 0) {
356             if (ff_neterrno() == AVERROR(EINTR))
357                 continue;
358             return AVERROR(EIO);
359         }
360         if (h->flags & AVIO_FLAG_NONBLOCK)
361             return AVERROR(EAGAIN);
362     }
363     return len;
364 }
365
366 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
367 {
368     RTPContext *s = h->priv_data;
369     int ret;
370     URLContext *hd;
371
372     if (RTP_PT_IS_RTCP(buf[1])) {
373         /* RTCP payload type */
374         hd = s->rtcp_hd;
375     } else {
376         /* RTP payload type */
377         hd = s->rtp_hd;
378     }
379
380     ret = ffurl_write(hd, buf, size);
381     return ret;
382 }
383
384 static int rtp_close(URLContext *h)
385 {
386     RTPContext *s = h->priv_data;
387     int i;
388
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);
395
396     ffurl_close(s->rtp_hd);
397     ffurl_close(s->rtcp_hd);
398     return 0;
399 }
400
401 /**
402  * Return the local rtp port used by the RTP connection
403  * @param h media file context
404  * @return the local port number
405  */
406
407 int ff_rtp_get_local_rtp_port(URLContext *h)
408 {
409     RTPContext *s = h->priv_data;
410     return ff_udp_get_local_port(s->rtp_hd);
411 }
412
413 /**
414  * Return the local rtcp 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_rtcp_port(URLContext *h)
420 {
421     RTPContext *s = h->priv_data;
422     return ff_udp_get_local_port(s->rtcp_hd);
423 }
424
425 static int rtp_get_file_handle(URLContext *h)
426 {
427     RTPContext *s = h->priv_data;
428     return s->rtp_fd;
429 }
430
431 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
432                                      int *numhandles)
433 {
434     RTPContext *s = h->priv_data;
435     int *hs       = *handles = av_malloc(sizeof(**handles) * 2);
436     if (!hs)
437         return AVERROR(ENOMEM);
438     hs[0] = s->rtp_fd;
439     hs[1] = s->rtcp_fd;
440     *numhandles = 2;
441     return 0;
442 }
443
444 URLProtocol ff_rtp_protocol = {
445     .name                      = "rtp",
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,
454 };