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