fix image stream copy
[libav.git] / libavformat / udp.c
CommitLineData
de6d9b64
FB
1/*
2 * UDP prototype streaming system
2d225591 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
de6d9b64 18 */
8be1c656 19#include "avformat.h"
de6d9b64 20#include <unistd.h>
de6d9b64 21#include <sys/types.h>
b8a78f41 22#include <sys/socket.h>
de6d9b64 23#include <netinet/in.h>
9ddd71fc
FR
24#ifndef __BEOS__
25# include <arpa/inet.h>
26#else
27# include "barpainet.h"
28#endif
de6d9b64
FB
29#include <netdb.h>
30
de6d9b64 31typedef struct {
2d225591
FB
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;
de6d9b64
FB
38} UDPContext;
39
40#define UDP_TX_BUF_SIZE 32768
41
2d225591
FB
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
9899efb4 51 * 'pkt_size=n' : set max packet size
2d225591
FB
52 *
53 * @param s1 media file context
54 * @param uri of the remote server
55 * @return zero if no error.
56 */
57int udp_set_remote_url(URLContext *h, const char *uri)
58{
59 UDPContext *s = h->priv_data;
60 char hostname[256];
61 int port;
62
6ba5cbc6 63 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
de6d9b64 64
2d225591
FB
65 /* set the destination address */
66 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
0bd586c5 67 return AVERROR_IO;
2d225591
FB
68 s->dest_addr.sin_family = AF_INET;
69 s->dest_addr.sin_port = htons(port);
70 return 0;
71}
72
73/**
74 * Return the local port used by the UDP connexion
75 * @param s1 media file context
76 * @return the local port number
77 */
78int udp_get_local_port(URLContext *h)
79{
80 UDPContext *s = h->priv_data;
81 return s->local_port;
82}
83
84/**
85 * Return the udp file handle for select() usage to wait for several RTP
86 * streams at the same time.
87 * @param h media file context
88 */
89int udp_get_file_handle(URLContext *h)
90{
91 UDPContext *s = h->priv_data;
92 return s->udp_fd;
93}
94
95/* put it in UDP context */
de6d9b64
FB
96/* return non zero if error */
97static int udp_open(URLContext *h, const char *uri, int flags)
98{
2d225591 99 struct sockaddr_in my_addr, my_addr1;
de6d9b64 100 char hostname[1024];
2d225591
FB
101 int port, udp_fd = -1, tmp;
102 UDPContext *s = NULL;
103 int is_output, len;
104 const char *p;
105 char buf[256];
de6d9b64
FB
106
107 h->is_streamed = 1;
9899efb4 108 h->max_packet_size = 1472;
de6d9b64 109
2d225591 110 is_output = (flags & URL_WRONLY);
de6d9b64 111
2d225591
FB
112 s = av_malloc(sizeof(UDPContext));
113 if (!s)
114 return -ENOMEM;
115
116 h->priv_data = s;
117 s->ttl = 16;
118 s->is_multicast = 0;
cc74aafb 119 s->local_port = 0;
2d225591
FB
120 p = strchr(uri, '?');
121 if (p) {
122 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
123 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
124 s->ttl = strtol(buf, NULL, 10);
125 }
126 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
127 s->local_port = strtol(buf, NULL, 10);
128 }
9899efb4
MK
129 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
130 h->max_packet_size = strtol(buf, NULL, 10);
131 }
de6d9b64 132 }
2d225591
FB
133
134 /* fill the dest addr */
6ba5cbc6 135 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
de6d9b64 136
2d225591
FB
137 /* XXX: fix url_split */
138 if (hostname[0] == '\0' || hostname[0] == '?') {
139 /* only accepts null hostname if input */
140 if (s->is_multicast || (flags & URL_WRONLY))
141 goto fail;
142 } else {
143 udp_set_remote_url(h, uri);
144 }
de6d9b64 145
2d225591
FB
146 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
147 if (udp_fd < 0)
148 goto fail;
149
772956c5
FB
150 my_addr.sin_family = AF_INET;
151 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
2d225591 152 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
772956c5
FB
153 /* special case: the bind must be done on the multicast address port */
154 my_addr.sin_port = s->dest_addr.sin_port;
2d225591 155 } else {
2d225591 156 my_addr.sin_port = htons(s->local_port);
2d225591 157 }
de6d9b64
FB
158
159 /* the bind is needed to give a port to the socket now */
2d225591 160 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
de6d9b64
FB
161 goto fail;
162
2d225591
FB
163 len = sizeof(my_addr1);
164 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
165 s->local_port = ntohs(my_addr1.sin_port);
de6d9b64 166
9ddd71fc 167#ifndef CONFIG_BEOS_NETSERVER
2d225591
FB
168 if (s->is_multicast) {
169 if (h->flags & URL_WRONLY) {
170 /* output */
171 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
172 &s->ttl, sizeof(s->ttl)) < 0) {
173 perror("IP_MULTICAST_TTL");
174 goto fail;
175 }
176 } else {
177 /* input */
178 memset(&s->mreq, 0, sizeof(s->mreq));
179 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
180 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
181 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
182 &s->mreq, sizeof(s->mreq)) < 0) {
183 perror("rtp: IP_ADD_MEMBERSHIP");
184 goto fail;
185 }
186 }
de6d9b64 187 }
9ddd71fc 188#endif
de6d9b64 189
2d225591
FB
190 if (is_output) {
191 /* limit the tx buf size to limit latency */
192 tmp = UDP_TX_BUF_SIZE;
193 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
194 perror("setsockopt sndbuf");
195 goto fail;
196 }
197 }
198
199 s->udp_fd = udp_fd;
de6d9b64
FB
200 return 0;
201 fail:
2d225591 202 if (udp_fd >= 0)
9ddd71fc
FR
203#ifdef CONFIG_BEOS_NETSERVER
204 closesocket(udp_fd);
205#else
2d225591 206 close(udp_fd);
9ddd71fc 207#endif
2d225591 208 av_free(s);
0bd586c5 209 return AVERROR_IO;
de6d9b64
FB
210}
211
0c1a9eda 212static int udp_read(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
213{
214 UDPContext *s = h->priv_data;
2d225591
FB
215 struct sockaddr_in from;
216 int from_len, len;
217
218 for(;;) {
219 from_len = sizeof(from);
220 len = recvfrom (s->udp_fd, buf, size, 0,
221 (struct sockaddr *)&from, &from_len);
222 if (len < 0) {
223 if (errno != EAGAIN && errno != EINTR)
0bd586c5 224 return AVERROR_IO;
2d225591
FB
225 } else {
226 break;
227 }
228 }
229 return len;
de6d9b64
FB
230}
231
0c1a9eda 232static int udp_write(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
233{
234 UDPContext *s = h->priv_data;
2d225591
FB
235 int ret;
236
237 for(;;) {
238 ret = sendto (s->udp_fd, buf, size, 0,
239 (struct sockaddr *) &s->dest_addr,
240 sizeof (s->dest_addr));
241 if (ret < 0) {
242 if (errno != EINTR && errno != EAGAIN)
0bd586c5 243 return AVERROR_IO;
2d225591
FB
244 } else {
245 break;
246 }
de6d9b64 247 }
2d225591
FB
248 return size;
249}
250
251static int udp_close(URLContext *h)
252{
253 UDPContext *s = h->priv_data;
254
9ddd71fc 255#ifndef CONFIG_BEOS_NETSERVER
2d225591
FB
256 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
257 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
258 &s->mreq, sizeof(s->mreq)) < 0) {
259 perror("IP_DROP_MEMBERSHIP");
260 }
261 }
262 close(s->udp_fd);
9ddd71fc
FR
263#else
264 closesocket(s->udp_fd);
265#endif
2d225591
FB
266 av_free(s);
267 return 0;
de6d9b64
FB
268}
269
270URLProtocol udp_protocol = {
271 "udp",
272 udp_open,
2d225591 273 udp_read,
de6d9b64
FB
274 udp_write,
275 NULL, /* seek */
276 udp_close,
277};