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