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