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