better UDP support - added preliminary multicast support (untested)
[libav.git] / libav / udp.c
1 /*
2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "avformat.h"
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26
27 typedef struct {
28 int udp_fd;
29 int ttl;
30 int is_multicast;
31 int local_port;
32 struct ip_mreq mreq;
33 struct sockaddr_in dest_addr;
34 } UDPContext;
35
36 #define UDP_TX_BUF_SIZE 32768
37
38 /**
39 * If no filename is given to av_open_input_file because you want to
40 * get the local port first, then you must call this function to set
41 * the remote server address.
42 *
43 * url syntax: udp://host:port[?option=val...]
44 * option: 'multicast=1' : enable multicast
45 * 'ttl=n' : set the ttl value (for multicast only)
46 * 'localport=n' : set the local port
47 *
48 * @param s1 media file context
49 * @param uri of the remote server
50 * @return zero if no error.
51 */
52 int udp_set_remote_url(URLContext *h, const char *uri)
53 {
54 UDPContext *s = h->priv_data;
55 char hostname[256];
56 int port;
57
58 url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
59
60 /* set the destination address */
61 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
62 return -EIO;
63 s->dest_addr.sin_family = AF_INET;
64 s->dest_addr.sin_port = htons(port);
65 return 0;
66 }
67
68 /**
69 * Return the local port used by the UDP connexion
70 * @param s1 media file context
71 * @return the local port number
72 */
73 int udp_get_local_port(URLContext *h)
74 {
75 UDPContext *s = h->priv_data;
76 return s->local_port;
77 }
78
79 /**
80 * Return the udp file handle for select() usage to wait for several RTP
81 * streams at the same time.
82 * @param h media file context
83 */
84 int udp_get_file_handle(URLContext *h)
85 {
86 UDPContext *s = h->priv_data;
87 return s->udp_fd;
88 }
89
90 /* put it in UDP context */
91 /* return non zero if error */
92 static int udp_open(URLContext *h, const char *uri, int flags)
93 {
94 struct sockaddr_in my_addr, my_addr1;
95 char hostname[1024];
96 int port, udp_fd = -1, tmp;
97 UDPContext *s = NULL;
98 int is_output, len;
99 const char *p;
100 char buf[256];
101
102 h->is_streamed = 1;
103
104 is_output = (flags & URL_WRONLY);
105
106 s = av_malloc(sizeof(UDPContext));
107 if (!s)
108 return -ENOMEM;
109
110 h->priv_data = s;
111 s->ttl = 16;
112 s->is_multicast = 0;
113 p = strchr(uri, '?');
114 if (p) {
115 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
116 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
117 s->ttl = strtol(buf, NULL, 10);
118 }
119 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
120 s->local_port = strtol(buf, NULL, 10);
121 }
122 }
123
124 /* fill the dest addr */
125 url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
126
127 /* XXX: fix url_split */
128 if (hostname[0] == '\0' || hostname[0] == '?') {
129 /* only accepts null hostname if input */
130 if (s->is_multicast || (flags & URL_WRONLY))
131 goto fail;
132 } else {
133 udp_set_remote_url(h, uri);
134 }
135
136 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
137 if (udp_fd < 0)
138 goto fail;
139
140 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
141 /* special case: the bind must be done on the multicast address */
142 my_addr = s->dest_addr;
143 } else {
144 my_addr.sin_family = AF_INET;
145 my_addr.sin_port = htons(s->local_port);
146 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
147 }
148
149 /* the bind is needed to give a port to the socket now */
150 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
151 goto fail;
152
153 len = sizeof(my_addr1);
154 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
155 s->local_port = ntohs(my_addr1.sin_port);
156
157 if (s->is_multicast) {
158 if (h->flags & URL_WRONLY) {
159 /* output */
160 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
161 &s->ttl, sizeof(s->ttl)) < 0) {
162 perror("IP_MULTICAST_TTL");
163 goto fail;
164 }
165 } else {
166 /* input */
167 memset(&s->mreq, 0, sizeof(s->mreq));
168 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
169 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
170 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
171 &s->mreq, sizeof(s->mreq)) < 0) {
172 perror("rtp: IP_ADD_MEMBERSHIP");
173 goto fail;
174 }
175 }
176 }
177
178 if (is_output) {
179 /* limit the tx buf size to limit latency */
180 tmp = UDP_TX_BUF_SIZE;
181 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
182 perror("setsockopt sndbuf");
183 goto fail;
184 }
185 }
186
187 s->udp_fd = udp_fd;
188 h->max_packet_size = 1472; /* XXX: probe it ? */
189 return 0;
190 fail:
191 if (udp_fd >= 0)
192 close(udp_fd);
193 av_free(s);
194 return -EIO;
195 }
196
197 static int udp_read(URLContext *h, UINT8 *buf, int size)
198 {
199 UDPContext *s = h->priv_data;
200 struct sockaddr_in from;
201 int from_len, len;
202
203 for(;;) {
204 from_len = sizeof(from);
205 len = recvfrom (s->udp_fd, buf, size, 0,
206 (struct sockaddr *)&from, &from_len);
207 if (len < 0) {
208 if (errno != EAGAIN && errno != EINTR)
209 return -EIO;
210 } else {
211 break;
212 }
213 }
214 return len;
215 }
216
217 static int udp_write(URLContext *h, UINT8 *buf, int size)
218 {
219 UDPContext *s = h->priv_data;
220 int ret;
221
222 for(;;) {
223 ret = sendto (s->udp_fd, buf, size, 0,
224 (struct sockaddr *) &s->dest_addr,
225 sizeof (s->dest_addr));
226 if (ret < 0) {
227 if (errno != EINTR && errno != EAGAIN)
228 return -EIO;
229 } else {
230 break;
231 }
232 }
233 return size;
234 }
235
236 static int udp_close(URLContext *h)
237 {
238 UDPContext *s = h->priv_data;
239
240 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
241 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
242 &s->mreq, sizeof(s->mreq)) < 0) {
243 perror("IP_DROP_MEMBERSHIP");
244 }
245 }
246 close(s->udp_fd);
247 av_free(s);
248 return 0;
249 }
250
251 URLProtocol udp_protocol = {
252 "udp",
253 udp_open,
254 udp_read,
255 udp_write,
256 NULL, /* seek */
257 udp_close,
258 };