Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ 63206a8f

History | View | Annotate | Download (14.4 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * UDP prototype streaming system
3 406792e7 Diego Biurrun
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 3adfb377 Luca Barbato
22
/**
23 ba87f080 Diego Biurrun
 * @file
24 3adfb377 Luca Barbato
 * UDP protocol
25
 */
26
27 a9e02947 Luca Abeni
#define _BSD_SOURCE     /* Needed for using struct ip_mreq with recent glibc */
28 8be1c656 Fabrice Bellard
#include "avformat.h"
29 de6d9b64 Fabrice Bellard
#include <unistd.h>
30 e4a9e3cc Aurelien Jacobs
#include "internal.h"
31 42572ef5 Ramiro Polla
#include "network.h"
32 087b3272 Luca Abeni
#include "os_support.h"
33 b250f9c6 Aurelien Jacobs
#if HAVE_SYS_SELECT_H
34 7d084299 Jindřich Makovička
#include <sys/select.h>
35
#endif
36 c0a8f8d4 Dave Yeo
#include <sys/time.h>
37 de6d9b64 Fabrice Bellard
38 9c633e9a Alex Beregszaszi
#ifndef IPV6_ADD_MEMBERSHIP
39
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
40
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
41
#endif
42 35b74c3d Luca Abeni
#ifndef IN_MULTICAST
43
#define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
44
#endif
45
#ifndef IN6_IS_ADDR_MULTICAST
46
#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
47
#endif
48 9c633e9a Alex Beregszaszi
49 de6d9b64 Fabrice Bellard
typedef struct {
50 2d225591 Fabrice Bellard
    int udp_fd;
51
    int ttl;
52 236bb1ab Jindřich Makovička
    int buffer_size;
53 2d225591 Fabrice Bellard
    int is_multicast;
54
    int local_port;
55 34ecc397 Thijs
    int reuse_socket;
56 7a91333f Hans Zandbelt
    struct sockaddr_storage dest_addr;
57 397db8ac Luca Abeni
    int dest_addr_len;
58 de6d9b64 Fabrice Bellard
} UDPContext;
59
60
#define UDP_TX_BUF_SIZE 32768
61 f18cae4d Ramiro Polla
#define UDP_MAX_PKT_SIZE 65536
62 de6d9b64 Fabrice Bellard
63 64810754 Ronald S. Bultje
static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
64
                                 struct sockaddr *addr)
65
{
66 a8bde059 Luca Abeni
#ifdef IP_MULTICAST_TTL
67 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
68
        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
69 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
70 7a91333f Hans Zandbelt
            return -1;
71
        }
72
    }
73 a8bde059 Luca Abeni
#endif
74 b4d68544 Martin Storsjö
#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
75 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
76
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
77 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
78 7a91333f Hans Zandbelt
            return -1;
79
        }
80
    }
81 a8bde059 Luca Abeni
#endif
82 7a91333f Hans Zandbelt
    return 0;
83
}
84
85 64810754 Ronald S. Bultje
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
86
{
87 a8bde059 Luca Abeni
#ifdef IP_ADD_MEMBERSHIP
88 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
89 a8bde059 Luca Abeni
        struct ip_mreq mreq;
90
91 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
92
        mreq.imr_interface.s_addr= INADDR_ANY;
93
        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
94 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
95 7a91333f Hans Zandbelt
            return -1;
96
        }
97
    }
98 a8bde059 Luca Abeni
#endif
99 63206a8f Martin Storsjö
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
100 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
101 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
102
103 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
104
        mreq6.ipv6mr_interface= 0;
105
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
106 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
107 7a91333f Hans Zandbelt
            return -1;
108
        }
109
    }
110 a8bde059 Luca Abeni
#endif
111 7a91333f Hans Zandbelt
    return 0;
112
}
113
114 64810754 Ronald S. Bultje
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
115
{
116 a8bde059 Luca Abeni
#ifdef IP_DROP_MEMBERSHIP
117 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
118 a8bde059 Luca Abeni
        struct ip_mreq mreq;
119
120 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
121
        mreq.imr_interface.s_addr= INADDR_ANY;
122
        if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
123 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
124 7a91333f Hans Zandbelt
            return -1;
125
        }
126
    }
127 a8bde059 Luca Abeni
#endif
128 63206a8f Martin Storsjö
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
129 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
130 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
131
132 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
133
        mreq6.ipv6mr_interface= 0;
134
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
135 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
136 7a91333f Hans Zandbelt
            return -1;
137
        }
138
    }
139 a8bde059 Luca Abeni
#endif
140 7a91333f Hans Zandbelt
    return 0;
141
}
142
143 64810754 Ronald S. Bultje
static struct addrinfo* udp_resolve_host(const char *hostname, int port,
144
                                         int type, int family, int flags)
145
{
146 7a91333f Hans Zandbelt
    struct addrinfo hints, *res = 0;
147
    int error;
148
    char sport[16];
149 d607861c Nicolas George
    const char *node = 0, *service = "0";
150 7a91333f Hans Zandbelt
151
    if (port > 0) {
152 2fc8ea24 Michael Niedermayer
        snprintf(sport, sizeof(sport), "%d", port);
153 7a91333f Hans Zandbelt
        service = sport;
154
    }
155
    if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
156
        node = hostname;
157
    }
158 7d8576c2 Luca Abeni
    memset(&hints, 0, sizeof(hints));
159
    hints.ai_socktype = type;
160
    hints.ai_family   = family;
161
    hints.ai_flags = flags;
162
    if ((error = getaddrinfo(node, service, &hints, &res))) {
163 11a74f36 Reimar Döffinger
        res = NULL;
164 7c823d8b Martin Storsjö
        av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
165 7d8576c2 Luca Abeni
    }
166
167 7a91333f Hans Zandbelt
    return res;
168
}
169
170 64810754 Ronald S. Bultje
static int udp_set_url(struct sockaddr_storage *addr,
171
                       const char *hostname, int port)
172
{
173 7a91333f Hans Zandbelt
    struct addrinfo *res0;
174 d05cb726 Luca Abeni
    int addr_len;
175
176 7c823d8b Martin Storsjö
    res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
177 6f3e0b21 Panagiotis Issaris
    if (res0 == 0) return AVERROR(EIO);
178 d05cb726 Luca Abeni
    memcpy(addr, res0->ai_addr, res0->ai_addrlen);
179
    addr_len = res0->ai_addrlen;
180 7a91333f Hans Zandbelt
    freeaddrinfo(res0);
181 d05cb726 Luca Abeni
182
    return addr_len;
183 7a91333f Hans Zandbelt
}
184
185 35b74c3d Luca Abeni
static int is_multicast_address(struct sockaddr_storage *addr)
186
{
187
    if (addr->ss_family == AF_INET) {
188
        return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
189
    }
190 b4d68544 Martin Storsjö
#if HAVE_STRUCT_SOCKADDR_IN6
191 35b74c3d Luca Abeni
    if (addr->ss_family == AF_INET6) {
192
        return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
193
    }
194 b4d68544 Martin Storsjö
#endif
195 35b74c3d Luca Abeni
196
    return 0;
197
}
198
199 64810754 Ronald S. Bultje
static int udp_socket_create(UDPContext *s,
200
                             struct sockaddr_storage *addr, int *addr_len)
201 aa519c47 Luca Abeni
{
202 7a91333f Hans Zandbelt
    int udp_fd = -1;
203 276358c1 Hans Zandbelt
    struct addrinfo *res0 = NULL, *res = NULL;
204 51844e6c Ronald S. Bultje
    int family = AF_UNSPEC;
205 115329f1 Diego Biurrun
206 51844e6c Ronald S. Bultje
    if (((struct sockaddr *) &s->dest_addr)->sa_family)
207
        family = ((struct sockaddr *) &s->dest_addr)->sa_family;
208 7c823d8b Martin Storsjö
    res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
209 7d8576c2 Luca Abeni
    if (res0 == 0)
210
        goto fail;
211
    for (res = res0; res; res=res->ai_next) {
212
        udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
213
        if (udp_fd > 0) break;
214 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
215 7d8576c2 Luca Abeni
    }
216 276358c1 Hans Zandbelt
217
    if (udp_fd < 0)
218 7a91333f Hans Zandbelt
        goto fail;
219 115329f1 Diego Biurrun
220 aa519c47 Luca Abeni
    memcpy(addr, res->ai_addr, res->ai_addrlen);
221
    *addr_len = res->ai_addrlen;
222 7a91333f Hans Zandbelt
223 aa519c47 Luca Abeni
    freeaddrinfo(res0);
224 115329f1 Diego Biurrun
225 7a91333f Hans Zandbelt
    return udp_fd;
226 115329f1 Diego Biurrun
227 7a91333f Hans Zandbelt
 fail:
228
    if (udp_fd >= 0)
229
        closesocket(udp_fd);
230 88730be6 Måns Rullgård
    if(res0)
231
        freeaddrinfo(res0);
232 7a91333f Hans Zandbelt
    return -1;
233
}
234
235 aa519c47 Luca Abeni
static int udp_port(struct sockaddr_storage *addr, int addr_len)
236
{
237 03c09e43 D Richard Felker III
    char sbuf[sizeof(int)*3+1];
238 aa519c47 Luca Abeni
239 03c09e43 D Richard Felker III
    if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0,  sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
240 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
241 aa519c47 Luca Abeni
        return -1;
242
    }
243
244
    return strtol(sbuf, NULL, 10);
245
}
246
247 7a91333f Hans Zandbelt
248 2d225591 Fabrice Bellard
/**
249
 * If no filename is given to av_open_input_file because you want to
250
 * get the local port first, then you must call this function to set
251
 * the remote server address.
252
 *
253
 * url syntax: udp://host:port[?option=val...]
254 35b74c3d Luca Abeni
 * option: 'ttl=n'       : set the ttl value (for multicast only)
255 2d225591 Fabrice Bellard
 *         'localport=n' : set the local port
256 9899efb4 Max Krasnyansky
 *         'pkt_size=n'  : set max packet size
257 34ecc397 Thijs
 *         'reuse=1'     : enable reusing the socket
258 2d225591 Fabrice Bellard
 *
259 9a58234f Diego Biurrun
 * @param h media file context
260 2d225591 Fabrice Bellard
 * @param uri of the remote server
261
 * @return zero if no error.
262
 */
263
int udp_set_remote_url(URLContext *h, const char *uri)
264
{
265
    UDPContext *s = h->priv_data;
266
    char hostname[256];
267
    int port;
268 115329f1 Diego Biurrun
269 f3bfe388 Måns Rullgård
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
270 de6d9b64 Fabrice Bellard
271 2d225591 Fabrice Bellard
    /* set the destination address */
272 d05cb726 Luca Abeni
    s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
273
    if (s->dest_addr_len < 0) {
274 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
275 d05cb726 Luca Abeni
    }
276 35b74c3d Luca Abeni
    s->is_multicast = is_multicast_address(&s->dest_addr);
277 d05cb726 Luca Abeni
278 2d225591 Fabrice Bellard
    return 0;
279
}
280
281
/**
282 48e77473 Martin Storsjö
 * Return the local port used by the UDP connection
283 9a58234f Diego Biurrun
 * @param h media file context
284 2d225591 Fabrice Bellard
 * @return the local port number
285
 */
286
int udp_get_local_port(URLContext *h)
287
{
288
    UDPContext *s = h->priv_data;
289
    return s->local_port;
290
}
291
292
/**
293
 * Return the udp file handle for select() usage to wait for several RTP
294
 * streams at the same time.
295
 * @param h media file context
296
 */
297 f0a80394 Ronald S. Bultje
#if (LIBAVFORMAT_VERSION_MAJOR >= 53)
298
static
299
#endif
300 2d225591 Fabrice Bellard
int udp_get_file_handle(URLContext *h)
301
{
302
    UDPContext *s = h->priv_data;
303
    return s->udp_fd;
304
}
305
306
/* put it in UDP context */
307 de6d9b64 Fabrice Bellard
/* return non zero if error */
308
static int udp_open(URLContext *h, const char *uri, int flags)
309
{
310
    char hostname[1024];
311 36b53281 Jindřich Makovička
    int port, udp_fd = -1, tmp, bind_ret = -1;
312 2d225591 Fabrice Bellard
    UDPContext *s = NULL;
313 88730be6 Måns Rullgård
    int is_output;
314 2d225591 Fabrice Bellard
    const char *p;
315
    char buf[256];
316 aa519c47 Luca Abeni
    struct sockaddr_storage my_addr;
317
    int len;
318 de6d9b64 Fabrice Bellard
319
    h->is_streamed = 1;
320 9899efb4 Max Krasnyansky
    h->max_packet_size = 1472;
321 de6d9b64 Fabrice Bellard
322 2d225591 Fabrice Bellard
    is_output = (flags & URL_WRONLY);
323 115329f1 Diego Biurrun
324 51844e6c Ronald S. Bultje
    s = av_mallocz(sizeof(UDPContext));
325 2d225591 Fabrice Bellard
    if (!s)
326 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
327 2d225591 Fabrice Bellard
328
    h->priv_data = s;
329
    s->ttl = 16;
330 236bb1ab Jindřich Makovička
    s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
331
332 2d225591 Fabrice Bellard
    p = strchr(uri, '?');
333
    if (p) {
334 34ecc397 Thijs
        s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
335 2d225591 Fabrice Bellard
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
336
            s->ttl = strtol(buf, NULL, 10);
337
        }
338
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
339
            s->local_port = strtol(buf, NULL, 10);
340
        }
341 9899efb4 Max Krasnyansky
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
342
            h->max_packet_size = strtol(buf, NULL, 10);
343
        }
344 236bb1ab Jindřich Makovička
        if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
345
            s->buffer_size = strtol(buf, NULL, 10);
346
        }
347 de6d9b64 Fabrice Bellard
    }
348 2d225591 Fabrice Bellard
349
    /* fill the dest addr */
350 f3bfe388 Måns Rullgård
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
351 115329f1 Diego Biurrun
352 f3bfe388 Måns Rullgård
    /* XXX: fix av_url_split */
353 2d225591 Fabrice Bellard
    if (hostname[0] == '\0' || hostname[0] == '?') {
354
        /* only accepts null hostname if input */
355 35b74c3d Luca Abeni
        if (flags & URL_WRONLY)
356 2d225591 Fabrice Bellard
            goto fail;
357
    } else {
358 82505611 Reimar Döffinger
        if (udp_set_remote_url(h, uri) < 0)
359
            goto fail;
360 2d225591 Fabrice Bellard
    }
361 de6d9b64 Fabrice Bellard
362 882d00f2 Luca Abeni
    if (s->is_multicast && !(h->flags & URL_WRONLY))
363
        s->local_port = port;
364 aa519c47 Luca Abeni
    udp_fd = udp_socket_create(s, &my_addr, &len);
365 2d225591 Fabrice Bellard
    if (udp_fd < 0)
366
        goto fail;
367
368 34ecc397 Thijs
    if (s->reuse_socket)
369
        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
370
            goto fail;
371
372 de6d9b64 Fabrice Bellard
    /* the bind is needed to give a port to the socket now */
373 36b53281 Jindřich Makovička
    /* if multicast, try the multicast address bind first */
374
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
375
        bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
376
    }
377
    /* bind to the local address if not multicast or if the multicast
378
     * bind failed */
379
    if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
380 de6d9b64 Fabrice Bellard
        goto fail;
381
382 d4936869 Luca Abeni
    len = sizeof(my_addr);
383
    getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
384 aa519c47 Luca Abeni
    s->local_port = udp_port(&my_addr, len);
385
386 7a91333f Hans Zandbelt
    if (s->is_multicast) {
387
        if (h->flags & URL_WRONLY) {
388 a8bde059 Luca Abeni
            /* output */
389 9cdcb04f Luca Abeni
            if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
390 7a91333f Hans Zandbelt
                goto fail;
391
        } else {
392 a8bde059 Luca Abeni
            /* input */
393 9cdcb04f Luca Abeni
            if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
394 7a91333f Hans Zandbelt
                goto fail;
395
        }
396
    }
397 de6d9b64 Fabrice Bellard
398 2d225591 Fabrice Bellard
    if (is_output) {
399
        /* limit the tx buf size to limit latency */
400 236bb1ab Jindřich Makovička
        tmp = s->buffer_size;
401 2d225591 Fabrice Bellard
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
402 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
403 2d225591 Fabrice Bellard
            goto fail;
404
        }
405 f18cae4d Ramiro Polla
    } else {
406
        /* set udp recv buffer size to the largest possible udp packet size to
407
         * avoid losing data on OSes that set this too low by default. */
408 236bb1ab Jindřich Makovička
        tmp = s->buffer_size;
409
        if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
410
            av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
411
        }
412 ce09d560 Jindřich Makovička
        /* make the socket non-blocking */
413
        ff_socket_nonblock(udp_fd, 1);
414 2d225591 Fabrice Bellard
    }
415
416
    s->udp_fd = udp_fd;
417 de6d9b64 Fabrice Bellard
    return 0;
418
 fail:
419 2d225591 Fabrice Bellard
    if (udp_fd >= 0)
420 9ddd71fc François Revol
        closesocket(udp_fd);
421 2d225591 Fabrice Bellard
    av_free(s);
422 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
423 de6d9b64 Fabrice Bellard
}
424
425 0c1a9eda Zdenek Kabelac
static int udp_read(URLContext *h, uint8_t *buf, int size)
426 de6d9b64 Fabrice Bellard
{
427
    UDPContext *s = h->priv_data;
428 191e8ca7 Måns Rullgård
    int len;
429 7d084299 Jindřich Makovička
    fd_set rfds;
430
    int ret;
431
    struct timeval tv;
432 2d225591 Fabrice Bellard
433
    for(;;) {
434 7d084299 Jindřich Makovička
        if (url_interrupt_cb())
435
            return AVERROR(EINTR);
436
        FD_ZERO(&rfds);
437
        FD_SET(s->udp_fd, &rfds);
438
        tv.tv_sec = 0;
439
        tv.tv_usec = 100 * 1000;
440
        ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
441 cae9a15c Martin Storsjö
        if (ret < 0) {
442
            if (ff_neterrno() == FF_NETERROR(EINTR))
443
                continue;
444 7d084299 Jindřich Makovička
            return AVERROR(EIO);
445 cae9a15c Martin Storsjö
        }
446 7d084299 Jindřich Makovička
        if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
447
            continue;
448 ce09d560 Jindřich Makovička
        len = recv(s->udp_fd, buf, size, 0);
449 2d225591 Fabrice Bellard
        if (len < 0) {
450 8da4034f Alex Beregszaszi
            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
451
                ff_neterrno() != FF_NETERROR(EINTR))
452 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
453 2d225591 Fabrice Bellard
        } else {
454
            break;
455
        }
456
    }
457
    return len;
458 de6d9b64 Fabrice Bellard
}
459
460 27241cbf Martin Storsjö
static int udp_write(URLContext *h, const uint8_t *buf, int size)
461 de6d9b64 Fabrice Bellard
{
462
    UDPContext *s = h->priv_data;
463 2d225591 Fabrice Bellard
    int ret;
464
465
    for(;;) {
466 115329f1 Diego Biurrun
        ret = sendto (s->udp_fd, buf, size, 0,
467 2d225591 Fabrice Bellard
                      (struct sockaddr *) &s->dest_addr,
468 7a91333f Hans Zandbelt
                      s->dest_addr_len);
469 2d225591 Fabrice Bellard
        if (ret < 0) {
470 8da4034f Alex Beregszaszi
            if (ff_neterrno() != FF_NETERROR(EINTR) &&
471
                ff_neterrno() != FF_NETERROR(EAGAIN))
472 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
473 2d225591 Fabrice Bellard
        } else {
474
            break;
475
        }
476 de6d9b64 Fabrice Bellard
    }
477 2d225591 Fabrice Bellard
    return size;
478
}
479
480
static int udp_close(URLContext *h)
481
{
482
    UDPContext *s = h->priv_data;
483
484 7a91333f Hans Zandbelt
    if (s->is_multicast && !(h->flags & URL_WRONLY))
485 9cdcb04f Luca Abeni
        udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
486 9ddd71fc François Revol
    closesocket(s->udp_fd);
487 2d225591 Fabrice Bellard
    av_free(s);
488
    return 0;
489 de6d9b64 Fabrice Bellard
}
490
491
URLProtocol udp_protocol = {
492
    "udp",
493
    udp_open,
494 2d225591 Fabrice Bellard
    udp_read,
495 de6d9b64 Fabrice Bellard
    udp_write,
496
    NULL, /* seek */
497
    udp_close,
498 f0a80394 Ronald S. Bultje
    .url_get_file_handle = udp_get_file_handle,
499 de6d9b64 Fabrice Bellard
};