2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
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.
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.
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
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
25 # include <arpa/inet.h>
27 # include "barpainet.h"
37 struct sockaddr_in dest_addr;
40 #define UDP_TX_BUF_SIZE 32768
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.
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
52 * @param s1 media file context
53 * @param uri of the remote server
54 * @return zero if no error.
56 int udp_set_remote_url(URLContext *h, const char *uri)
58 UDPContext *s = h->priv_data;
62 url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
64 /* set the destination address */
65 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
67 s->dest_addr.sin_family = AF_INET;
68 s->dest_addr.sin_port = htons(port);
73 * Return the local port used by the UDP connexion
74 * @param s1 media file context
75 * @return the local port number
77 int udp_get_local_port(URLContext *h)
79 UDPContext *s = h->priv_data;
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
88 int udp_get_file_handle(URLContext *h)
90 UDPContext *s = h->priv_data;
94 /* put it in UDP context */
95 /* return non zero if error */
96 static int udp_open(URLContext *h, const char *uri, int flags)
98 struct sockaddr_in my_addr, my_addr1;
100 int port, udp_fd = -1, tmp;
101 UDPContext *s = NULL;
108 is_output = (flags & URL_WRONLY);
110 s = av_malloc(sizeof(UDPContext));
117 p = strchr(uri, '?');
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);
123 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
124 s->local_port = strtol(buf, NULL, 10);
128 /* fill the dest addr */
129 url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
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))
137 udp_set_remote_url(h, uri);
140 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
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;
150 my_addr.sin_port = htons(s->local_port);
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)
157 len = sizeof(my_addr1);
158 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
159 s->local_port = ntohs(my_addr1.sin_port);
161 #ifndef CONFIG_BEOS_NETSERVER
162 if (s->is_multicast) {
163 if (h->flags & URL_WRONLY) {
165 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
166 &s->ttl, sizeof(s->ttl)) < 0) {
167 perror("IP_MULTICAST_TTL");
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");
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");
194 h->max_packet_size = 1472; /* XXX: probe it ? */
198 #ifdef CONFIG_BEOS_NETSERVER
207 static int udp_read(URLContext *h, UINT8 *buf, int size)
209 UDPContext *s = h->priv_data;
210 struct sockaddr_in from;
214 from_len = sizeof(from);
215 len = recvfrom (s->udp_fd, buf, size, 0,
216 (struct sockaddr *)&from, &from_len);
218 if (errno != EAGAIN && errno != EINTR)
227 static int udp_write(URLContext *h, UINT8 *buf, int size)
229 UDPContext *s = h->priv_data;
233 ret = sendto (s->udp_fd, buf, size, 0,
234 (struct sockaddr *) &s->dest_addr,
235 sizeof (s->dest_addr));
237 if (errno != EINTR && errno != EAGAIN)
246 static int udp_close(URLContext *h)
248 UDPContext *s = h->priv_data;
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");
259 closesocket(s->udp_fd);
265 URLProtocol udp_protocol = {