() 10l
[libav.git] / libavformat / rtpproto.c
CommitLineData
3b50d2ad
FB
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
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3b50d2ad
FB
18 */
19#include "avformat.h"
20
21#include <unistd.h>
51885599 22#include <stdarg.h>
3b50d2ad 23#include <sys/types.h>
b8a78f41 24#include <sys/socket.h>
3b50d2ad 25#include <netinet/in.h>
9ddd71fc
FR
26#ifndef __BEOS__
27# include <arpa/inet.h>
28#else
29# include "barpainet.h"
30#endif
3b50d2ad
FB
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
37typedef 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 *
3b50d2ad
FB
47 * @param s1 media file context
48 * @param uri of the remote server
49 * @return zero if no error.
50 */
51int rtp_set_remote_url(URLContext *h, const char *uri)
52{
53 RTPContext *s = h->priv_data;
54 char hostname[256];
55 int port;
51885599 56
3b50d2ad
FB
57 char buf[1024];
58 char path[1024];
115329f1
DB
59
60 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
3b50d2ad
FB
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
51885599
FB
72/* add option to url of the form:
73 "http://host:port/path?option1=val1&option2=val2... */
5c91a675 74static void url_add_option(char *buf, int buf_size, const char *fmt, ...)
51885599
FB
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
5c91a675 89static void build_udp_url(char *buf, int buf_size,
bb270c08
DB
90 const char *hostname, int port,
91 int local_port, int multicast, int ttl)
51885599
FB
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...]
115329f1 104 * option: 'multicast=1' : enable multicast
51885599
FB
105 * 'ttl=n' : set the ttl value (for multicast only)
106 * 'localport=n' : set the local port to n
107 *
108 */
3b50d2ad
FB
109static int rtp_open(URLContext *h, const char *uri, int flags)
110{
111 RTPContext *s;
51885599 112 int port, is_output, is_multicast, ttl, local_port;
3b50d2ad
FB
113 char hostname[256];
114 char buf[1024];
115 char path[1024];
51885599 116 const char *p;
115329f1 117
3b50d2ad
FB
118 is_output = (flags & URL_WRONLY);
119
120 s = av_mallocz(sizeof(RTPContext));
121 if (!s)
122 return -ENOMEM;
123 h->priv_data = s;
115329f1
DB
124
125 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
3b50d2ad 126 path, sizeof(path), uri);
51885599
FB
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 }
3b50d2ad 141
51885599
FB
142 build_udp_url(buf, sizeof(buf),
143 hostname, port, local_port, is_multicast, ttl);
3b50d2ad
FB
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 */
115329f1 150
51885599
FB
151 build_udp_url(buf, sizeof(buf),
152 hostname, port + 1, local_port + 1, is_multicast, ttl);
3b50d2ad
FB
153 if (url_open(&s->rtcp_hd, buf, flags) < 0)
154 goto fail;
115329f1 155
3b50d2ad
FB
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
115329f1 161 h->max_packet_size = url_get_max_packet_size(s->rtp_hd);
3b50d2ad
FB
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);
0bd586c5 171 return AVERROR_IO;
3b50d2ad
FB
172}
173
0c1a9eda 174static int rtp_read(URLContext *h, uint8_t *buf, int size)
3b50d2ad
FB
175{
176 RTPContext *s = h->priv_data;
177 struct sockaddr_in from;
191e8ca7
MR
178 socklen_t from_len;
179 int len, fd_max, n;
3b50d2ad
FB
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;
0bd586c5 189 return AVERROR_IO;
3b50d2ad
FB
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;
0bd586c5 212 return AVERROR_IO;
3b50d2ad
FB
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;
0bd586c5 224 return AVERROR_IO;
3b50d2ad
FB
225 }
226 break;
227 }
228 }
229 }
230#endif
231 return len;
232}
233
0c1a9eda 234static int rtp_write(URLContext *h, uint8_t *buf, int size)
3b50d2ad
FB
235{
236 RTPContext *s = h->priv_data;
51885599 237 int ret;
3b50d2ad 238 URLContext *hd;
115329f1 239
3b50d2ad
FB
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
260static 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 */
275int 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 */
286void 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
294URLProtocol rtp_protocol = {
295 "rtp",
296 rtp_open,
297 rtp_read,
298 rtp_write,
299 NULL, /* seek */
300 rtp_close,
301};