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