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