]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpproto.c
dsputil: Move LOCAL_ALIGNED macros to libavutil
[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 "rtpdec.h"
32 #include "url.h"
33
34 #include <stdarg.h>
35 #include "internal.h"
36 #include "network.h"
37 #include "os_support.h"
38 #include <fcntl.h>
39 #if HAVE_POLL_H
40 #include <sys/poll.h>
41 #endif
42
43 typedef struct RTPContext {
44     URLContext *rtp_hd, *rtcp_hd;
45     int rtp_fd, rtcp_fd;
46 } RTPContext;
47
48 /**
49  * If no filename is given to av_open_input_file because you want to
50  * get the local port first, then you must call this function to set
51  * the remote server address.
52  *
53  * @param h media file context
54  * @param uri of the remote server
55  * @return zero if no error.
56  */
57
58 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
59 {
60     RTPContext *s = h->priv_data;
61     char hostname[256];
62     int port;
63
64     char buf[1024];
65     char path[1024];
66
67     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
68                  path, sizeof(path), uri);
69
70     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
71     ff_udp_set_remote_url(s->rtp_hd, buf);
72
73     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port + 1, "%s", path);
74     ff_udp_set_remote_url(s->rtcp_hd, buf);
75     return 0;
76 }
77
78
79 /**
80  * add option to url of the form:
81  * "http://host:port/path?option1=val1&option2=val2...
82  */
83
84 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
85 {
86     char buf1[1024];
87     va_list ap;
88
89     va_start(ap, fmt);
90     if (strchr(buf, '?'))
91         av_strlcat(buf, "&", buf_size);
92     else
93         av_strlcat(buf, "?", buf_size);
94     vsnprintf(buf1, sizeof(buf1), fmt, ap);
95     av_strlcat(buf, buf1, buf_size);
96     va_end(ap);
97 }
98
99 static void build_udp_url(char *buf, int buf_size,
100                           const char *hostname, int port,
101                           int local_port, int ttl,
102                           int max_packet_size, int connect)
103 {
104     ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
105     if (local_port >= 0)
106         url_add_option(buf, buf_size, "localport=%d", local_port);
107     if (ttl >= 0)
108         url_add_option(buf, buf_size, "ttl=%d", ttl);
109     if (max_packet_size >=0)
110         url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
111     if (connect)
112         url_add_option(buf, buf_size, "connect=1");
113 }
114
115 /**
116  * url syntax: rtp://host:port[?option=val...]
117  * option: 'ttl=n'            : set the ttl value (for multicast only)
118  *         'rtcpport=n'       : set the remote rtcp port to n
119  *         'localrtpport=n'   : set the local rtp port to n
120  *         'localrtcpport=n'  : set the local rtcp port to n
121  *         'pkt_size=n'       : set max packet size
122  *         'connect=0/1'      : do a connect() on the UDP socket
123  * deprecated option:
124  *         'localport=n'      : set the local port to n
125  *
126  * if rtcpport isn't set the rtcp port will be the rtp port + 1
127  * if local rtp port isn't set any available port will be used for the local
128  * rtp and rtcp ports
129  * if the local rtcp port is not set it will be the local rtp port + 1
130  */
131
132 static int rtp_open(URLContext *h, const char *uri, int flags)
133 {
134     RTPContext *s = h->priv_data;
135     int rtp_port, rtcp_port,
136         ttl, connect,
137         local_rtp_port, local_rtcp_port, max_packet_size;
138     char hostname[256];
139     char buf[1024];
140     char path[1024];
141     const char *p;
142
143     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
144                  path, sizeof(path), uri);
145     /* extract parameters */
146     ttl = -1;
147     rtcp_port = rtp_port+1;
148     local_rtp_port = -1;
149     local_rtcp_port = -1;
150     max_packet_size = -1;
151     connect = 0;
152
153     p = strchr(uri, '?');
154     if (p) {
155         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
156             ttl = strtol(buf, NULL, 10);
157         }
158         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
159             rtcp_port = strtol(buf, NULL, 10);
160         }
161         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
162             local_rtp_port = strtol(buf, NULL, 10);
163         }
164         if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
165             local_rtp_port = strtol(buf, NULL, 10);
166         }
167         if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
168             local_rtcp_port = strtol(buf, NULL, 10);
169         }
170         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
171             max_packet_size = strtol(buf, NULL, 10);
172         }
173         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
174             connect = strtol(buf, NULL, 10);
175         }
176     }
177
178     build_udp_url(buf, sizeof(buf),
179                   hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
180                   connect);
181     if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
182         goto fail;
183     if (local_rtp_port>=0 && local_rtcp_port<0)
184         local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
185
186     build_udp_url(buf, sizeof(buf),
187                   hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
188                   connect);
189     if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
190         goto fail;
191
192     /* just to ease handle access. XXX: need to suppress direct handle
193        access */
194     s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
195     s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
196
197     h->max_packet_size = s->rtp_hd->max_packet_size;
198     h->is_streamed = 1;
199     return 0;
200
201  fail:
202     if (s->rtp_hd)
203         ffurl_close(s->rtp_hd);
204     if (s->rtcp_hd)
205         ffurl_close(s->rtcp_hd);
206     return AVERROR(EIO);
207 }
208
209 static int rtp_read(URLContext *h, uint8_t *buf, int size)
210 {
211     RTPContext *s = h->priv_data;
212     struct sockaddr_storage from;
213     socklen_t from_len;
214     int len, n;
215     struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
216
217     for(;;) {
218         if (ff_check_interrupt(&h->interrupt_callback))
219             return AVERROR_EXIT;
220         /* build fdset to listen to RTP and RTCP packets */
221         n = poll(p, 2, 100);
222         if (n > 0) {
223             /* first try RTCP */
224             if (p[1].revents & POLLIN) {
225                 from_len = sizeof(from);
226                 len = recvfrom (s->rtcp_fd, buf, size, 0,
227                                 (struct sockaddr *)&from, &from_len);
228                 if (len < 0) {
229                     if (ff_neterrno() == AVERROR(EAGAIN) ||
230                         ff_neterrno() == AVERROR(EINTR))
231                         continue;
232                     return AVERROR(EIO);
233                 }
234                 break;
235             }
236             /* then RTP */
237             if (p[0].revents & POLLIN) {
238                 from_len = sizeof(from);
239                 len = recvfrom (s->rtp_fd, buf, size, 0,
240                                 (struct sockaddr *)&from, &from_len);
241                 if (len < 0) {
242                     if (ff_neterrno() == AVERROR(EAGAIN) ||
243                         ff_neterrno() == AVERROR(EINTR))
244                         continue;
245                     return AVERROR(EIO);
246                 }
247                 break;
248             }
249         } else if (n < 0) {
250             if (ff_neterrno() == AVERROR(EINTR))
251                 continue;
252             return AVERROR(EIO);
253         }
254     }
255     return len;
256 }
257
258 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
259 {
260     RTPContext *s = h->priv_data;
261     int ret;
262     URLContext *hd;
263
264     if (RTP_PT_IS_RTCP(buf[1])) {
265         /* RTCP payload type */
266         hd = s->rtcp_hd;
267     } else {
268         /* RTP payload type */
269         hd = s->rtp_hd;
270     }
271
272     ret = ffurl_write(hd, buf, size);
273     return ret;
274 }
275
276 static int rtp_close(URLContext *h)
277 {
278     RTPContext *s = h->priv_data;
279
280     ffurl_close(s->rtp_hd);
281     ffurl_close(s->rtcp_hd);
282     return 0;
283 }
284
285 /**
286  * Return the local rtp port used by the RTP connection
287  * @param h media file context
288  * @return the local port number
289  */
290
291 int ff_rtp_get_local_rtp_port(URLContext *h)
292 {
293     RTPContext *s = h->priv_data;
294     return ff_udp_get_local_port(s->rtp_hd);
295 }
296
297 /**
298  * Return the local rtcp port used by the RTP connection
299  * @param h media file context
300  * @return the local port number
301  */
302
303 int ff_rtp_get_local_rtcp_port(URLContext *h)
304 {
305     RTPContext *s = h->priv_data;
306     return ff_udp_get_local_port(s->rtcp_hd);
307 }
308
309 static int rtp_get_file_handle(URLContext *h)
310 {
311     RTPContext *s = h->priv_data;
312     return s->rtp_fd;
313 }
314
315 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
316                                      int *numhandles)
317 {
318     RTPContext *s = h->priv_data;
319     int *hs       = *handles = av_malloc(sizeof(**handles) * 2);
320     if (!hs)
321         return AVERROR(ENOMEM);
322     hs[0] = s->rtp_fd;
323     hs[1] = s->rtcp_fd;
324     *numhandles = 2;
325     return 0;
326 }
327
328 URLProtocol ff_rtp_protocol = {
329     .name                      = "rtp",
330     .url_open                  = rtp_open,
331     .url_read                  = rtp_read,
332     .url_write                 = rtp_write,
333     .url_close                 = rtp_close,
334     .url_get_file_handle       = rtp_get_file_handle,
335     .url_get_multi_file_handle = rtp_get_multi_file_handle,
336     .priv_data_size            = sizeof(RTPContext),
337     .flags                     = URL_PROTOCOL_FLAG_NETWORK,
338 };