]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/rtpproto.c
frsh: Export information about the last RTP contract and VRES
[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     int budget=frsh_rtp_budget;
150     int period_ms=frsh_rtp_period_ms;
151     int deadline_ms=frsh_rtp_deadline_ms;
152
153     is_output = (flags & URL_WRONLY);
154
155     s = av_mallocz(sizeof(RTPContext));
156     if (!s)
157         return AVERROR(ENOMEM);
158     h->priv_data = s;
159
160     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
161               path, sizeof(path), uri);
162     /* extract parameters */
163     ttl = -1;
164     local_port = -1;
165     max_packet_size = -1;
166
167     p = strchr(uri, '?');
168     if (p) {
169         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
170             ttl = strtol(buf, NULL, 10);
171         }
172         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
173             local_port = strtol(buf, NULL, 10);
174         }
175         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
176             max_packet_size = strtol(buf, NULL, 10);
177         }
178         if (find_info_tag(buf, sizeof(buf), "budget", p)) {
179             budget = strtol(buf, NULL, 10);
180         }
181         if (find_info_tag(buf, sizeof(buf), "period", p)) {
182             period_ms = strtol(buf, NULL, 10);
183         }
184         if (find_info_tag(buf, sizeof(buf), "deadline", p)) {
185             deadline_ms = strtol(buf, NULL, 10);
186         }
187     }
188
189     build_udp_url(buf, sizeof(buf),
190                   hostname, port, local_port, ttl, max_packet_size, "RTP",
191                   budget, period_ms, deadline_ms);
192     if (url_open(&s->rtp_hd, buf, flags) < 0)
193         goto fail;
194     local_port = udp_get_local_port(s->rtp_hd);
195     /* XXX: need to open another connection if the port is not even */
196
197     /* well, should suppress localport in path */
198
199     build_udp_url(buf, sizeof(buf),
200                   hostname, port + 1, local_port + 1, ttl, max_packet_size, "RTCP",
201                   1000, 100*1000, 0);
202     if (url_open(&s->rtcp_hd, buf, flags) < 0)
203         goto fail;
204
205     /* just to ease handle access. XXX: need to suppress direct handle
206        access */
207     s->rtp_fd = url_get_file_handle(s->rtp_hd);
208     s->rtcp_fd = url_get_file_handle(s->rtcp_hd);
209
210     h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
211     h->is_streamed = 1;
212     return 0;
213
214  fail:
215     if (s->rtp_hd)
216         url_close(s->rtp_hd);
217     if (s->rtcp_hd)
218         url_close(s->rtcp_hd);
219     av_free(s);
220     return AVERROR(EIO);
221 }
222
223 static int rtp_read(URLContext *h, uint8_t *buf, int size)
224 {
225     RTPContext *s = h->priv_data;
226     struct sockaddr_in from;
227     socklen_t from_len;
228     int len, fd_max, n;
229     fd_set rfds;
230 #if 0
231     for(;;) {
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 #else
244     for(;;) {
245         /* build fdset to listen to RTP and RTCP packets */
246         FD_ZERO(&rfds);
247         fd_max = s->rtp_fd;
248         FD_SET(s->rtp_fd, &rfds);
249         if (s->rtcp_fd > fd_max)
250             fd_max = s->rtcp_fd;
251         FD_SET(s->rtcp_fd, &rfds);
252         n = select(fd_max + 1, &rfds, NULL, NULL, NULL);
253         if (n > 0) {
254             /* first try RTCP */
255             if (FD_ISSET(s->rtcp_fd, &rfds)) {
256                 from_len = sizeof(from);
257                 len = recvfrom (s->rtcp_fd, buf, size, 0,
258                                 (struct sockaddr *)&from, &from_len);
259                 if (len < 0) {
260                     if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
261                         ff_neterrno() == FF_NETERROR(EINTR))
262                         continue;
263                     return AVERROR(EIO);
264                 }
265                 break;
266             }
267             /* then RTP */
268             if (FD_ISSET(s->rtp_fd, &rfds)) {
269                 from_len = sizeof(from);
270                 len = recvfrom (s->rtp_fd, buf, size, 0,
271                                 (struct sockaddr *)&from, &from_len);
272                 if (len < 0) {
273                     if (ff_neterrno() == FF_NETERROR(EAGAIN) ||
274                         ff_neterrno() == FF_NETERROR(EINTR))
275                         continue;
276                     return AVERROR(EIO);
277                 }
278                 break;
279             }
280         }
281     }
282 #endif
283     return len;
284 }
285
286 static int rtp_write(URLContext *h, uint8_t *buf, int size)
287 {
288     RTPContext *s = h->priv_data;
289     int ret;
290     URLContext *hd;
291
292     if (buf[1] >= 200 && buf[1] <= 204) {
293         /* RTCP payload type */
294         hd = s->rtcp_hd;
295     } else {
296         /* RTP payload type */
297         hd = s->rtp_hd;
298     }
299
300     ret = url_write(hd, buf, size);
301 #if 0
302     {
303         struct timespec ts;
304         ts.tv_sec = 0;
305         ts.tv_nsec = 10 * 1000000;
306         nanosleep(&ts, NULL);
307     }
308 #endif
309     return ret;
310 }
311
312 static int rtp_close(URLContext *h)
313 {
314     RTPContext *s = h->priv_data;
315
316     url_close(s->rtp_hd);
317     url_close(s->rtcp_hd);
318     av_free(s);
319     return 0;
320 }
321
322 /**
323  * Return the local port used by the RTP connection
324  * @param s1 media file context
325  * @return the local port number
326  */
327
328 int rtp_get_local_port(URLContext *h)
329 {
330     RTPContext *s = h->priv_data;
331     return udp_get_local_port(s->rtp_hd);
332 }
333
334 #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
335 /**
336  * Return the rtp and rtcp file handles for select() usage to wait for
337  * several RTP streams at the same time.
338  * @param h media file context
339  */
340
341 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd)
342 {
343     RTPContext *s = h->priv_data;
344
345     *prtp_fd = s->rtp_fd;
346     *prtcp_fd = s->rtcp_fd;
347 }
348 #endif
349
350 static int rtp_get_file_handle(URLContext *h)
351 {
352     RTPContext *s = h->priv_data;
353     return s->rtp_fd;
354 }
355
356 URLProtocol rtp_protocol = {
357     "rtp",
358     rtp_open,
359     rtp_read,
360     rtp_write,
361     NULL, /* seek */
362     rtp_close,
363     .url_get_file_handle = rtp_get_file_handle,
364 };