]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/rtpproto.c
Add needed include, make it compile without -D_BSD_SOURCE.
[frescor/ffmpeg.git] / 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 rtpproto.c
24  * RTP protocol
25  */
26
27 #include "libavutil/avstring.h"
28 #include "avformat.h"
29
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include "network.h"
33 #include "os_support.h"
34 #include <fcntl.h>
35 #include <sys/select.h>
36
37 #define RTP_TX_BUF_SIZE  (64 * 1024)
38 #define RTP_RX_BUF_SIZE  (128 * 1024)
39
40 typedef struct RTPContext {
41     URLContext *rtp_hd, *rtcp_hd;
42     int rtp_fd, rtcp_fd;
43 } RTPContext;
44
45 /**
46  * If no filename is given to av_open_input_file because you want to
47  * get the local port first, then you must call this function to set
48  * the remote server address.
49  *
50  * @param s1 media file context
51  * @param uri of the remote server
52  * @return zero if no error.
53  */
54
55 int rtp_set_remote_url(URLContext *h, const char *uri)
56 {
57     RTPContext *s = h->priv_data;
58     char hostname[256];
59     int port;
60
61     char buf[1024];
62     char path[1024];
63
64     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
65               path, sizeof(path), uri);
66
67     snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path);
68     udp_set_remote_url(s->rtp_hd, buf);
69
70     snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path);
71     udp_set_remote_url(s->rtcp_hd, buf);
72     return 0;
73 }
74
75
76 /**
77  * add option to url of the form:
78  * "http://host:port/path?option1=val1&option2=val2...
79  */
80
81 static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
82 {
83     char buf1[1024];
84     va_list ap;
85
86     va_start(ap, fmt);
87     if (strchr(buf, '?'))
88         av_strlcat(buf, "&", buf_size);
89     else
90         av_strlcat(buf, "?", buf_size);
91     vsnprintf(buf1, sizeof(buf1), fmt, ap);
92     av_strlcat(buf, buf1, buf_size);
93     va_end(ap);
94 }
95
96 static void build_udp_url(char *buf, int buf_size,
97                           const char *hostname, int port,
98                           int local_port, int ttl,
99                           int max_packet_size)
100 {
101     snprintf(buf, buf_size, "udp://%s:%d", hostname, port);
102     if (local_port >= 0)
103         url_add_option(buf, buf_size, "localport=%d", local_port);
104     if (ttl >= 0)
105         url_add_option(buf, buf_size, "ttl=%d", ttl);
106     if (max_packet_size >=0)
107         url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size);
108 }
109
110 /**
111  * url syntax: rtp://host:port[?option=val...]
112  * option: 'ttl=n'       : set the ttl value (for multicast only)
113  *         'localport=n' : set the local port to n
114  *         'pkt_size=n'  : set max packet size
115  *
116  */
117
118 static int rtp_open(URLContext *h, const char *uri, int flags)
119 {
120     RTPContext *s;
121     int port, is_output, ttl, local_port, max_packet_size;
122     char hostname[256];
123     char buf[1024];
124     char path[1024];
125     const char *p;
126
127     is_output = (flags & URL_WRONLY);
128
129     s = av_mallocz(sizeof(RTPContext));
130     if (!s)
131         return AVERROR(ENOMEM);
132     h->priv_data = s;
133
134     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
135               path, sizeof(path), uri);
136     /* extract parameters */
137     ttl = -1;
138     local_port = -1;
139     max_packet_size = -1;
140
141     p = strchr(uri, '?');
142     if (p) {
143         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
144             ttl = strtol(buf, NULL, 10);
145         }
146         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
147             local_port = strtol(buf, NULL, 10);
148         }
149         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
150             max_packet_size = strtol(buf, NULL, 10);
151         }
152     }
153
154     build_udp_url(buf, sizeof(buf),
155                   hostname, port, local_port, ttl, max_packet_size);
156     if (url_open(&s->rtp_hd, buf, flags) < 0)
157         goto fail;
158     local_port = udp_get_local_port(s->rtp_hd);
159     /* XXX: need to open another connection if the port is not even */
160
161     /* well, should suppress localport in path */
162
163     build_udp_url(buf, sizeof(buf),
164                   hostname, port + 1, local_port + 1, ttl, max_packet_size);
165     if (url_open(&s->rtcp_hd, buf, flags) < 0)
166         goto fail;
167
168     /* just to ease handle access. XXX: need to suppress direct handle
169        access */
170     s->rtp_fd = udp_get_file_handle(s->rtp_hd);
171     s->rtcp_fd = udp_get_file_handle(s->rtcp_hd);
172
173     h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
174     h->is_streamed = 1;
175     return 0;
176
177  fail:
178     if (s->rtp_hd)
179         url_close(s->rtp_hd);
180     if (s->rtcp_hd)
181         url_close(s->rtcp_hd);
182     av_free(s);
183     return AVERROR(EIO);
184 }
185
186 static int rtp_read(URLContext *h, uint8_t *buf, int size)
187 {
188     RTPContext *s = h->priv_data;
189     struct sockaddr_in from;
190     socklen_t from_len;
191     int len, fd_max, n;
192     fd_set rfds;
193 #if 0
194     for(;;) {
195         from_len = sizeof(from);
196         len = recvfrom (s->rtp_fd, buf, size, 0,
197                         (struct sockaddr *)&from, &from_len);
198         if (len < 0) {
199             if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
200                 ff_neterrno() == FF_NETERROR(EINTR))
201                 continue;
202             return AVERROR(EIO);
203         }
204         break;
205     }
206 #else
207     for(;;) {
208         /* build fdset to listen to RTP and RTCP packets */
209         FD_ZERO(&rfds);
210         fd_max = s->rtp_fd;
211         FD_SET(s->rtp_fd, &rfds);
212         if (s->rtcp_fd > fd_max)
213             fd_max = s->rtcp_fd;
214         FD_SET(s->rtcp_fd, &rfds);
215         n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
216         if (n > 0) {
217             /* first try RTCP */
218             if (FD_ISSET(s->rtcp_fd, &rfds)) {
219                 from_len = sizeof(from);
220                 len = recvfrom (s->rtcp_fd, buf, size, 0,
221                                 (struct sockaddr *)&from, &from_len);
222                 if (len < 0) {
223                     if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
224                         ff_neterrno() == FF_NETERROR(EINTR))
225                         continue;
226                     return AVERROR(EIO);
227                 }
228                 break;
229             }
230             /* then RTP */
231             if (FD_ISSET(s->rtp_fd, &rfds)) {
232                 from_len = sizeof(from);
233                 len = recvfrom (s->rtp_fd, buf, size, 0,
234                                 (struct sockaddr *)&from, &from_len);
235                 if (len < 0) {
236                     if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
237                         ff_neterrno() == FF_NETERROR(EINTR))
238                         continue;
239                     return AVERROR(EIO);
240                 }
241                 break;
242             }
243         }
244     }
245 #endif
246     return len;
247 }
248
249 static int rtp_write(URLContext *h, uint8_t *buf, int size)
250 {
251     RTPContext *s = h->priv_data;
252     int ret;
253     URLContext *hd;
254
255     if (buf[1] >= 200 && buf[1] <= 204) {
256         /* RTCP payload type */
257         hd = s->rtcp_hd;
258     } else {
259         /* RTP payload type */
260         hd = s->rtp_hd;
261     }
262
263     ret = url_write(hd, buf, size);
264 #if 0
265     {
266         struct timespec ts;
267         ts.tv_sec = 0;
268         ts.tv_nsec = 10 * 1000000;
269         nanosleep(&ts, NULL);
270     }
271 #endif
272     return ret;
273 }
274
275 static int rtp_close(URLContext *h)
276 {
277     RTPContext *s = h->priv_data;
278
279     url_close(s->rtp_hd);
280     url_close(s->rtcp_hd);
281     av_free(s);
282     return 0;
283 }
284
285 /**
286  * Return the local port used by the RTP connection
287  * @param s1 media file context
288  * @return the local port number
289  */
290
291 int rtp_get_local_port(URLContext *h)
292 {
293     RTPContext *s = h->priv_data;
294     return udp_get_local_port(s->rtp_hd);
295 }
296
297 /**
298  * Return the rtp and rtcp file handles for select() usage to wait for
299  * several RTP streams at the same time.
300  * @param h media file context
301  */
302
303 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
304 {
305     RTPContext *s = h->priv_data;
306
307     *prtp_fd = s->rtp_fd;
308     *prtcp_fd = s->rtcp_fd;
309 }
310
311 URLProtocol rtp_protocol = {
312     "rtp",
313     rtp_open,
314     rtp_read,
315     rtp_write,
316     NULL, /* seek */
317     rtp_close,
318 };