annotate udp.c @ 422:2f30932cda1f libavformat

support for wider range of codecs
author alex
date Wed, 07 Apr 2004 12:47:33 +0000
parents e01949598794
children 0fdc96c2f2fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * UDP prototype streaming system
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
18 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
19 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20 #include <unistd.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 #include <sys/types.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 #include <sys/socket.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #include <netinet/in.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 #ifndef __BEOS__
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 # include <arpa/inet.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 # include "barpainet.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 #include <netdb.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 int udp_fd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33 int ttl;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 int is_multicast;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 int local_port;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 struct ip_mreq mreq;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 struct sockaddr_in dest_addr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 } UDPContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 #define UDP_TX_BUF_SIZE 32768
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 * If no filename is given to av_open_input_file because you want to
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 * get the local port first, then you must call this function to set
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 * the remote server address.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 * url syntax: udp://host:port[?option=val...]
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 * option: 'multicast=1' : enable multicast
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 * 'ttl=n' : set the ttl value (for multicast only)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 * 'localport=n' : set the local port
62
56a6676125b4 variable UDP packet size patch by Max Krasnyansky
bellard
parents: 0
diff changeset
51 * 'pkt_size=n' : set max packet size
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53 * @param s1 media file context
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54 * @param uri of the remote server
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 * @return zero if no error.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 int udp_set_remote_url(URLContext *h, const char *uri)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59 UDPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60 char hostname[256];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 int port;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 /* set the destination address */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 s->dest_addr.sin_family = AF_INET;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 s->dest_addr.sin_port = htons(port);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74 * Return the local port used by the UDP connexion
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 * @param s1 media file context
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 * @return the local port number
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 int udp_get_local_port(URLContext *h)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 UDPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 return s->local_port;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 * Return the udp file handle for select() usage to wait for several RTP
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 * streams at the same time.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 * @param h media file context
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 int udp_get_file_handle(URLContext *h)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 UDPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92 return s->udp_fd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 /* put it in UDP context */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 /* return non zero if error */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 static int udp_open(URLContext *h, const char *uri, int flags)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 struct sockaddr_in my_addr, my_addr1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 char hostname[1024];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 int port, udp_fd = -1, tmp;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 UDPContext *s = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 int is_output, len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 const char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 char buf[256];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 h->is_streamed = 1;
62
56a6676125b4 variable UDP packet size patch by Max Krasnyansky
bellard
parents: 0
diff changeset
108 h->max_packet_size = 1472;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 is_output = (flags & URL_WRONLY);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 s = av_malloc(sizeof(UDPContext));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 if (!s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 return -ENOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 h->priv_data = s;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 s->ttl = 16;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 s->is_multicast = 0;
161
e01949598794 undefined local_port fix by (Giancarlo Formicuccia <ilsensine at inwind dot it>)
michaelni
parents: 65
diff changeset
119 s->local_port = 0;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 p = strchr(uri, '?');
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121 if (p) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 s->ttl = strtol(buf, NULL, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 s->local_port = strtol(buf, NULL, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128 }
62
56a6676125b4 variable UDP packet size patch by Max Krasnyansky
bellard
parents: 0
diff changeset
129 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
56a6676125b4 variable UDP packet size patch by Max Krasnyansky
bellard
parents: 0
diff changeset
130 h->max_packet_size = strtol(buf, NULL, 10);
56a6676125b4 variable UDP packet size patch by Max Krasnyansky
bellard
parents: 0
diff changeset
131 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 /* fill the dest addr */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137 /* XXX: fix url_split */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 if (hostname[0] == '\0' || hostname[0] == '?') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 /* only accepts null hostname if input */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 if (s->is_multicast || (flags & URL_WRONLY))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 udp_set_remote_url(h, uri);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 if (udp_fd < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 my_addr.sin_family = AF_INET;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 /* special case: the bind must be done on the multicast address port */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 my_addr.sin_port = s->dest_addr.sin_port;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 my_addr.sin_port = htons(s->local_port);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 /* the bind is needed to give a port to the socket now */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 len = sizeof(my_addr1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 s->local_port = ntohs(my_addr1.sin_port);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167 #ifndef CONFIG_BEOS_NETSERVER
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 if (s->is_multicast) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 if (h->flags & URL_WRONLY) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 /* output */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 &s->ttl, sizeof(s->ttl)) < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 perror("IP_MULTICAST_TTL");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 /* input */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 memset(&s->mreq, 0, sizeof(s->mreq));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 &s->mreq, sizeof(s->mreq)) < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 perror("rtp: IP_ADD_MEMBERSHIP");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 if (is_output) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 /* limit the tx buf size to limit latency */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 tmp = UDP_TX_BUF_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 perror("setsockopt sndbuf");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
196 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 s->udp_fd = udp_fd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 if (udp_fd >= 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 #ifdef CONFIG_BEOS_NETSERVER
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 closesocket(udp_fd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 close(udp_fd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 av_free(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 62
diff changeset
212 static int udp_read(URLContext *h, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 UDPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 struct sockaddr_in from;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 int from_len, len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 from_len = sizeof(from);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 len = recvfrom (s->udp_fd, buf, size, 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 (struct sockaddr *)&from, &from_len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 if (len < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 if (errno != EAGAIN && errno != EINTR)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 return len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 62
diff changeset
232 static int udp_write(URLContext *h, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 UDPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 int ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 ret = sendto (s->udp_fd, buf, size, 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 (struct sockaddr *) &s->dest_addr,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 sizeof (s->dest_addr));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 if (ret < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 if (errno != EINTR && errno != EAGAIN)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 return size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 static int udp_close(URLContext *h)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 UDPContext *s = h->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 #ifndef CONFIG_BEOS_NETSERVER
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 &s->mreq, sizeof(s->mreq)) < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259 perror("IP_DROP_MEMBERSHIP");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 close(s->udp_fd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 closesocket(s->udp_fd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 av_free(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 URLProtocol udp_protocol = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 "udp",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 udp_open,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 udp_read,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 udp_write,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 NULL, /* seek */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 udp_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 };