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