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