Mercurial > libavformat.hg
annotate rtpproto.c @ 3712:30d4d95e068f libavformat
Add needed include, make it compile without -D_BSD_SOURCE.
author | michael |
---|---|
date | Tue, 12 Aug 2008 21:02:37 +0000 |
parents | 6f61c3b36632 |
children | 1968303c7566 |
rev | line source |
---|---|
0 | 1 /* |
2 * RTP network protocol | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1332
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
3229 | 21 |
22 /** | |
23 * @file rtpproto.c | |
24 * RTP protocol | |
25 */ | |
26 | |
3286 | 27 #include "libavutil/avstring.h" |
0 | 28 #include "avformat.h" |
29 | |
30 #include <unistd.h> | |
31 #include <stdarg.h> | |
1754 | 32 #include "network.h" |
2782
419e121ce80a
Add #include "os_support.h" to restore OS/2 support.
diego
parents:
2274
diff
changeset
|
33 #include "os_support.h" |
0 | 34 #include <fcntl.h> |
3712
30d4d95e068f
Add needed include, make it compile without -D_BSD_SOURCE.
michael
parents:
3286
diff
changeset
|
35 #include <sys/select.h> |
0 | 36 |
37 #define RTP_TX_BUF_SIZE (64 * 1024) | |
38 #define RTP_RX_BUF_SIZE (128 * 1024) | |
39 | |
40 typedef struct RTPContext { | |
41 URLContext *rtp_hd, *rtcp_hd; | |
42 int rtp_fd, rtcp_fd; | |
43 } RTPContext; | |
44 | |
45 /** | |
46 * If no filename is given to av_open_input_file because you want to | |
47 * get the local port first, then you must call this function to set | |
48 * the remote server address. | |
49 * | |
50 * @param s1 media file context | |
51 * @param uri of the remote server | |
52 * @return zero if no error. | |
53 */ | |
3229 | 54 |
0 | 55 int rtp_set_remote_url(URLContext *h, const char *uri) |
56 { | |
57 RTPContext *s = h->priv_data; | |
58 char hostname[256]; | |
59 int port; | |
60 | |
61 char buf[1024]; | |
62 char path[1024]; | |
885 | 63 |
64 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, | |
0 | 65 path, sizeof(path), uri); |
66 | |
67 snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port, path); | |
68 udp_set_remote_url(s->rtp_hd, buf); | |
69 | |
70 snprintf(buf, sizeof(buf), "udp://%s:%d%s", hostname, port + 1, path); | |
71 udp_set_remote_url(s->rtcp_hd, buf); | |
72 return 0; | |
73 } | |
74 | |
75 | |
3229 | 76 /** |
77 * add option to url of the form: | |
78 * "http://host:port/path?option1=val1&option2=val2... | |
79 */ | |
80 | |
64 | 81 static void url_add_option(char *buf, int buf_size, const char *fmt, ...) |
0 | 82 { |
83 char buf1[1024]; | |
84 va_list ap; | |
85 | |
86 va_start(ap, fmt); | |
87 if (strchr(buf, '?')) | |
2193
5ce5fad0dfac
replace the uses of old string functions that Reimar missed
mru
parents:
2079
diff
changeset
|
88 av_strlcat(buf, "&", buf_size); |
0 | 89 else |
2193
5ce5fad0dfac
replace the uses of old string functions that Reimar missed
mru
parents:
2079
diff
changeset
|
90 av_strlcat(buf, "?", buf_size); |
0 | 91 vsnprintf(buf1, sizeof(buf1), fmt, ap); |
2193
5ce5fad0dfac
replace the uses of old string functions that Reimar missed
mru
parents:
2079
diff
changeset
|
92 av_strlcat(buf, buf1, buf_size); |
0 | 93 va_end(ap); |
94 } | |
95 | |
64 | 96 static void build_udp_url(char *buf, int buf_size, |
887 | 97 const char *hostname, int port, |
3228 | 98 int local_port, int ttl, |
99 int max_packet_size) | |
0 | 100 { |
101 snprintf(buf, buf_size, "udp://%s:%d", hostname, port); | |
102 if (local_port >= 0) | |
103 url_add_option(buf, buf_size, "localport=%d", local_port); | |
104 if (ttl >= 0) | |
105 url_add_option(buf, buf_size, "ttl=%d", ttl); | |
3228 | 106 if (max_packet_size >=0) |
107 url_add_option(buf, buf_size, "pkt_size=%d", max_packet_size); | |
0 | 108 } |
109 | |
3229 | 110 /** |
0 | 111 * url syntax: rtp://host:port[?option=val...] |
3221 | 112 * option: 'ttl=n' : set the ttl value (for multicast only) |
0 | 113 * 'localport=n' : set the local port to n |
3228 | 114 * 'pkt_size=n' : set max packet size |
0 | 115 * |
116 */ | |
3229 | 117 |
0 | 118 static int rtp_open(URLContext *h, const char *uri, int flags) |
119 { | |
120 RTPContext *s; | |
3228 | 121 int port, is_output, ttl, local_port, max_packet_size; |
0 | 122 char hostname[256]; |
123 char buf[1024]; | |
124 char path[1024]; | |
125 const char *p; | |
885 | 126 |
0 | 127 is_output = (flags & URL_WRONLY); |
128 | |
129 s = av_mallocz(sizeof(RTPContext)); | |
130 if (!s) | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1754
diff
changeset
|
131 return AVERROR(ENOMEM); |
0 | 132 h->priv_data = s; |
885 | 133 |
134 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, | |
0 | 135 path, sizeof(path), uri); |
136 /* extract parameters */ | |
137 ttl = -1; | |
138 local_port = -1; | |
3228 | 139 max_packet_size = -1; |
140 | |
0 | 141 p = strchr(uri, '?'); |
142 if (p) { | |
143 if (find_info_tag(buf, sizeof(buf), "ttl", p)) { | |
144 ttl = strtol(buf, NULL, 10); | |
145 } | |
146 if (find_info_tag(buf, sizeof(buf), "localport", p)) { | |
147 local_port = strtol(buf, NULL, 10); | |
148 } | |
3228 | 149 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) { |
150 max_packet_size = strtol(buf, NULL, 10); | |
151 } | |
0 | 152 } |
153 | |
154 build_udp_url(buf, sizeof(buf), | |
3228 | 155 hostname, port, local_port, ttl, max_packet_size); |
0 | 156 if (url_open(&s->rtp_hd, buf, flags) < 0) |
157 goto fail; | |
158 local_port = udp_get_local_port(s->rtp_hd); | |
2079 | 159 /* XXX: need to open another connection if the port is not even */ |
0 | 160 |
161 /* well, should suppress localport in path */ | |
885 | 162 |
0 | 163 build_udp_url(buf, sizeof(buf), |
3228 | 164 hostname, port + 1, local_port + 1, ttl, max_packet_size); |
0 | 165 if (url_open(&s->rtcp_hd, buf, flags) < 0) |
166 goto fail; | |
885 | 167 |
0 | 168 /* just to ease handle access. XXX: need to suppress direct handle |
169 access */ | |
170 s->rtp_fd = udp_get_file_handle(s->rtp_hd); | |
171 s->rtcp_fd = udp_get_file_handle(s->rtcp_hd); | |
172 | |
885 | 173 h->max_packet_size = url_get_max_packet_size(s->rtp_hd); |
0 | 174 h->is_streamed = 1; |
175 return 0; | |
176 | |
177 fail: | |
178 if (s->rtp_hd) | |
179 url_close(s->rtp_hd); | |
180 if (s->rtcp_hd) | |
181 url_close(s->rtcp_hd); | |
182 av_free(s); | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2200
diff
changeset
|
183 return AVERROR(EIO); |
0 | 184 } |
185 | |
65 | 186 static int rtp_read(URLContext *h, uint8_t *buf, int size) |
0 | 187 { |
188 RTPContext *s = h->priv_data; | |
189 struct sockaddr_in from; | |
1332 | 190 socklen_t from_len; |
191 int len, fd_max, n; | |
0 | 192 fd_set rfds; |
193 #if 0 | |
194 for(;;) { | |
195 from_len = sizeof(from); | |
196 len = recvfrom (s->rtp_fd, buf, size, 0, | |
197 (struct sockaddr *)&from, &from_len); | |
198 if (len < 0) { | |
2056
eeea52739ff3
use ff_neterrno() and FF_NETERROR() for networking error handling
alex
parents:
1930
diff
changeset
|
199 if (ff_neterrno() == FF_NETERROR(EAGAIN) || |
eeea52739ff3
use ff_neterrno() and FF_NETERROR() for networking error handling
alex
parents:
1930
diff
changeset
|
200 ff_neterrno() == FF_NETERROR(EINTR)) |
0 | 201 continue; |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2200
diff
changeset
|
202 return AVERROR(EIO); |
0 | 203 } |
204 break; | |
205 } | |
206 #else | |
207 for(;;) { | |
208 /* build fdset to listen to RTP and RTCP packets */ | |
209 FD_ZERO(&rfds); | |
210 fd_max = s->rtp_fd; | |
211 FD_SET(s->rtp_fd, &rfds); | |
212 if (s->rtcp_fd > fd_max) | |
213 fd_max = s->rtcp_fd; | |
214 FD_SET(s->rtcp_fd, &rfds); | |
215 n = select(fd_max + 1, &rfds, NULL, NULL, NULL); | |
216 if (n > 0) { | |
217 /* first try RTCP */ | |
218 if (FD_ISSET(s->rtcp_fd, &rfds)) { | |
219 from_len = sizeof(from); | |
220 len = recvfrom (s->rtcp_fd, buf, size, 0, | |
221 (struct sockaddr *)&from, &from_len); | |
222 if (len < 0) { | |
2056
eeea52739ff3
use ff_neterrno() and FF_NETERROR() for networking error handling
alex
parents:
1930
diff
changeset
|
223 if (ff_neterrno() == FF_NETERROR(EAGAIN) || |
eeea52739ff3
use ff_neterrno() and FF_NETERROR() for networking error handling
alex
parents:
1930
diff
changeset
|
224 ff_neterrno() == FF_NETERROR(EINTR)) |
0 | 225 continue; |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2200
diff
changeset
|
226 return AVERROR(EIO); |
0 | 227 } |
228 break; | |
229 } | |
230 /* then RTP */ | |
231 if (FD_ISSET(s->rtp_fd, &rfds)) { | |
232 from_len = sizeof(from); | |
233 len = recvfrom (s->rtp_fd, buf, size, 0, | |
234 (struct sockaddr *)&from, &from_len); | |
235 if (len < 0) { | |
2056
eeea52739ff3
use ff_neterrno() and FF_NETERROR() for networking error handling
alex
parents:
1930
diff
changeset
|
236 if (ff_neterrno() == FF_NETERROR(EAGAIN) || |
eeea52739ff3
use ff_neterrno() and FF_NETERROR() for networking error handling
alex
parents:
1930
diff
changeset
|
237 ff_neterrno() == FF_NETERROR(EINTR)) |
0 | 238 continue; |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2200
diff
changeset
|
239 return AVERROR(EIO); |
0 | 240 } |
241 break; | |
242 } | |
243 } | |
244 } | |
245 #endif | |
246 return len; | |
247 } | |
248 | |
65 | 249 static int rtp_write(URLContext *h, uint8_t *buf, int size) |
0 | 250 { |
251 RTPContext *s = h->priv_data; | |
252 int ret; | |
253 URLContext *hd; | |
885 | 254 |
0 | 255 if (buf[1] >= 200 && buf[1] <= 204) { |
256 /* RTCP payload type */ | |
257 hd = s->rtcp_hd; | |
258 } else { | |
259 /* RTP payload type */ | |
260 hd = s->rtp_hd; | |
261 } | |
262 | |
263 ret = url_write(hd, buf, size); | |
264 #if 0 | |
265 { | |
266 struct timespec ts; | |
267 ts.tv_sec = 0; | |
268 ts.tv_nsec = 10 * 1000000; | |
269 nanosleep(&ts, NULL); | |
270 } | |
271 #endif | |
272 return ret; | |
273 } | |
274 | |
275 static int rtp_close(URLContext *h) | |
276 { | |
277 RTPContext *s = h->priv_data; | |
278 | |
279 url_close(s->rtp_hd); | |
280 url_close(s->rtcp_hd); | |
281 av_free(s); | |
282 return 0; | |
283 } | |
284 | |
285 /** | |
2079 | 286 * Return the local port used by the RTP connection |
0 | 287 * @param s1 media file context |
288 * @return the local port number | |
289 */ | |
3229 | 290 |
0 | 291 int rtp_get_local_port(URLContext *h) |
292 { | |
293 RTPContext *s = h->priv_data; | |
294 return udp_get_local_port(s->rtp_hd); | |
295 } | |
296 | |
297 /** | |
3229 | 298 * Return the rtp and rtcp file handles for select() usage to wait for |
299 * several RTP streams at the same time. | |
0 | 300 * @param h media file context |
301 */ | |
3229 | 302 |
0 | 303 void rtp_get_file_handles(URLContext *h, int *prtp_fd, int *prtcp_fd) |
304 { | |
305 RTPContext *s = h->priv_data; | |
306 | |
307 *prtp_fd = s->rtp_fd; | |
308 *prtcp_fd = s->rtcp_fd; | |
309 } | |
310 | |
311 URLProtocol rtp_protocol = { | |
312 "rtp", | |
313 rtp_open, | |
314 rtp_read, | |
315 rtp_write, | |
316 NULL, /* seek */ | |
317 rtp_close, | |
318 }; |