Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ c76374c6

History | View | Annotate | Download (15.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 59612534 Martin Storsjö
#define _DARWIN_C_SOURCE /* Needed for using IP_MULTICAST_TTL on OS X */
29 8be1c656 Fabrice Bellard
#include "avformat.h"
30 ab0287fc Anton Khirnov
#include "libavutil/parseutils.h"
31 de6d9b64 Fabrice Bellard
#include <unistd.h>
32 e4a9e3cc Aurelien Jacobs
#include "internal.h"
33 42572ef5 Ramiro Polla
#include "network.h"
34 087b3272 Luca Abeni
#include "os_support.h"
35 a8475bbd Luca Barbato
#if HAVE_POLL_H
36
#include <poll.h>
37 7d084299 Jindřich Makovička
#endif
38 c0a8f8d4 Dave Yeo
#include <sys/time.h>
39 de6d9b64 Fabrice Bellard
40 9c633e9a Alex Beregszaszi
#ifndef IPV6_ADD_MEMBERSHIP
41
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
42
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
43
#endif
44
45 de6d9b64 Fabrice Bellard
typedef struct {
46 2d225591 Fabrice Bellard
    int udp_fd;
47
    int ttl;
48 236bb1ab Jindřich Makovička
    int buffer_size;
49 2d225591 Fabrice Bellard
    int is_multicast;
50
    int local_port;
51 34ecc397 Thijs
    int reuse_socket;
52 7a91333f Hans Zandbelt
    struct sockaddr_storage dest_addr;
53 397db8ac Luca Abeni
    int dest_addr_len;
54 6766a354 Martin Storsjö
    int is_connected;
55 de6d9b64 Fabrice Bellard
} UDPContext;
56
57
#define UDP_TX_BUF_SIZE 32768
58 f18cae4d Ramiro Polla
#define UDP_MAX_PKT_SIZE 65536
59 de6d9b64 Fabrice Bellard
60 64810754 Ronald S. Bultje
static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
61
                                 struct sockaddr *addr)
62
{
63 a8bde059 Luca Abeni
#ifdef IP_MULTICAST_TTL
64 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
65
        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
66 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
67 7a91333f Hans Zandbelt
            return -1;
68
        }
69
    }
70 a8bde059 Luca Abeni
#endif
71 b4d68544 Martin Storsjö
#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
72 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
73
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
74 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
75 7a91333f Hans Zandbelt
            return -1;
76
        }
77
    }
78 a8bde059 Luca Abeni
#endif
79 7a91333f Hans Zandbelt
    return 0;
80
}
81
82 64810754 Ronald S. Bultje
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
83
{
84 a8bde059 Luca Abeni
#ifdef IP_ADD_MEMBERSHIP
85 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
86 a8bde059 Luca Abeni
        struct ip_mreq mreq;
87
88 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
89
        mreq.imr_interface.s_addr= INADDR_ANY;
90
        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
91 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
92 7a91333f Hans Zandbelt
            return -1;
93
        }
94
    }
95 a8bde059 Luca Abeni
#endif
96 63206a8f Martin Storsjö
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
97 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
98 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
99
100 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
101
        mreq6.ipv6mr_interface= 0;
102
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
103 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
104 7a91333f Hans Zandbelt
            return -1;
105
        }
106
    }
107 a8bde059 Luca Abeni
#endif
108 7a91333f Hans Zandbelt
    return 0;
109
}
110
111 64810754 Ronald S. Bultje
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
112
{
113 a8bde059 Luca Abeni
#ifdef IP_DROP_MEMBERSHIP
114 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET) {
115 a8bde059 Luca Abeni
        struct ip_mreq mreq;
116
117 7a91333f Hans Zandbelt
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
118
        mreq.imr_interface.s_addr= INADDR_ANY;
119
        if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
120 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
121 7a91333f Hans Zandbelt
            return -1;
122
        }
123
    }
124 a8bde059 Luca Abeni
#endif
125 63206a8f Martin Storsjö
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
126 7a91333f Hans Zandbelt
    if (addr->sa_family == AF_INET6) {
127 a8bde059 Luca Abeni
        struct ipv6_mreq mreq6;
128
129 7a91333f Hans Zandbelt
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
130
        mreq6.ipv6mr_interface= 0;
131
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
132 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
133 7a91333f Hans Zandbelt
            return -1;
134
        }
135
    }
136 a8bde059 Luca Abeni
#endif
137 7a91333f Hans Zandbelt
    return 0;
138
}
139
140 64810754 Ronald S. Bultje
static struct addrinfo* udp_resolve_host(const char *hostname, int port,
141
                                         int type, int family, int flags)
142
{
143 7a91333f Hans Zandbelt
    struct addrinfo hints, *res = 0;
144
    int error;
145
    char sport[16];
146 d607861c Nicolas George
    const char *node = 0, *service = "0";
147 7a91333f Hans Zandbelt
148
    if (port > 0) {
149 2fc8ea24 Michael Niedermayer
        snprintf(sport, sizeof(sport), "%d", port);
150 7a91333f Hans Zandbelt
        service = sport;
151
    }
152
    if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
153
        node = hostname;
154
    }
155 7d8576c2 Luca Abeni
    memset(&hints, 0, sizeof(hints));
156
    hints.ai_socktype = type;
157
    hints.ai_family   = family;
158
    hints.ai_flags = flags;
159
    if ((error = getaddrinfo(node, service, &hints, &res))) {
160 11a74f36 Reimar Döffinger
        res = NULL;
161 7c823d8b Martin Storsjö
        av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
162 7d8576c2 Luca Abeni
    }
163
164 7a91333f Hans Zandbelt
    return res;
165
}
166
167 64810754 Ronald S. Bultje
static int udp_set_url(struct sockaddr_storage *addr,
168
                       const char *hostname, int port)
169
{
170 7a91333f Hans Zandbelt
    struct addrinfo *res0;
171 d05cb726 Luca Abeni
    int addr_len;
172
173 7c823d8b Martin Storsjö
    res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
174 6f3e0b21 Panagiotis Issaris
    if (res0 == 0) return AVERROR(EIO);
175 d05cb726 Luca Abeni
    memcpy(addr, res0->ai_addr, res0->ai_addrlen);
176
    addr_len = res0->ai_addrlen;
177 7a91333f Hans Zandbelt
    freeaddrinfo(res0);
178 d05cb726 Luca Abeni
179
    return addr_len;
180 7a91333f Hans Zandbelt
}
181
182 64810754 Ronald S. Bultje
static int udp_socket_create(UDPContext *s,
183
                             struct sockaddr_storage *addr, int *addr_len)
184 aa519c47 Luca Abeni
{
185 7a91333f Hans Zandbelt
    int udp_fd = -1;
186 276358c1 Hans Zandbelt
    struct addrinfo *res0 = NULL, *res = NULL;
187 51844e6c Ronald S. Bultje
    int family = AF_UNSPEC;
188 115329f1 Diego Biurrun
189 51844e6c Ronald S. Bultje
    if (((struct sockaddr *) &s->dest_addr)->sa_family)
190
        family = ((struct sockaddr *) &s->dest_addr)->sa_family;
191 7c823d8b Martin Storsjö
    res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
192 7d8576c2 Luca Abeni
    if (res0 == 0)
193
        goto fail;
194
    for (res = res0; res; res=res->ai_next) {
195
        udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
196
        if (udp_fd > 0) break;
197 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
198 7d8576c2 Luca Abeni
    }
199 276358c1 Hans Zandbelt
200
    if (udp_fd < 0)
201 7a91333f Hans Zandbelt
        goto fail;
202 115329f1 Diego Biurrun
203 aa519c47 Luca Abeni
    memcpy(addr, res->ai_addr, res->ai_addrlen);
204
    *addr_len = res->ai_addrlen;
205 7a91333f Hans Zandbelt
206 aa519c47 Luca Abeni
    freeaddrinfo(res0);
207 115329f1 Diego Biurrun
208 7a91333f Hans Zandbelt
    return udp_fd;
209 115329f1 Diego Biurrun
210 7a91333f Hans Zandbelt
 fail:
211
    if (udp_fd >= 0)
212
        closesocket(udp_fd);
213 88730be6 Måns Rullgård
    if(res0)
214
        freeaddrinfo(res0);
215 7a91333f Hans Zandbelt
    return -1;
216
}
217
218 aa519c47 Luca Abeni
static int udp_port(struct sockaddr_storage *addr, int addr_len)
219
{
220 03c09e43 D Richard Felker III
    char sbuf[sizeof(int)*3+1];
221 aa519c47 Luca Abeni
222 03c09e43 D Richard Felker III
    if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0,  sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
223 086119b3 Luca Abeni
        av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
224 aa519c47 Luca Abeni
        return -1;
225
    }
226
227
    return strtol(sbuf, NULL, 10);
228
}
229
230 7a91333f Hans Zandbelt
231 2d225591 Fabrice Bellard
/**
232
 * If no filename is given to av_open_input_file because you want to
233
 * get the local port first, then you must call this function to set
234
 * the remote server address.
235
 *
236
 * url syntax: udp://host:port[?option=val...]
237 35b74c3d Luca Abeni
 * option: 'ttl=n'       : set the ttl value (for multicast only)
238 2d225591 Fabrice Bellard
 *         'localport=n' : set the local port
239 9899efb4 Max Krasnyansky
 *         'pkt_size=n'  : set max packet size
240 34ecc397 Thijs
 *         'reuse=1'     : enable reusing the socket
241 2d225591 Fabrice Bellard
 *
242 9a58234f Diego Biurrun
 * @param h media file context
243 2d225591 Fabrice Bellard
 * @param uri of the remote server
244
 * @return zero if no error.
245
 */
246
int udp_set_remote_url(URLContext *h, const char *uri)
247
{
248
    UDPContext *s = h->priv_data;
249 21a569f3 Martin Storsjö
    char hostname[256], buf[10];
250 2d225591 Fabrice Bellard
    int port;
251 21a569f3 Martin Storsjö
    const char *p;
252 115329f1 Diego Biurrun
253 f3bfe388 Måns Rullgård
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
254 de6d9b64 Fabrice Bellard
255 2d225591 Fabrice Bellard
    /* set the destination address */
256 d05cb726 Luca Abeni
    s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
257
    if (s->dest_addr_len < 0) {
258 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
259 d05cb726 Luca Abeni
    }
260 55b6bd1c Martin Storsjö
    s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
261 21a569f3 Martin Storsjö
    p = strchr(uri, '?');
262
    if (p) {
263 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
264 21a569f3 Martin Storsjö
            int was_connected = s->is_connected;
265
            s->is_connected = strtol(buf, NULL, 10);
266
            if (s->is_connected && !was_connected) {
267
                if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
268
                            s->dest_addr_len)) {
269
                    s->is_connected = 0;
270
                    av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
271
                    return AVERROR(EIO);
272
                }
273
            }
274
        }
275
    }
276 d05cb726 Luca Abeni
277 2d225591 Fabrice Bellard
    return 0;
278
}
279
280
/**
281 48e77473 Martin Storsjö
 * Return the local port used by the UDP connection
282 9a58234f Diego Biurrun
 * @param h media file context
283 2d225591 Fabrice Bellard
 * @return the local port number
284
 */
285
int udp_get_local_port(URLContext *h)
286
{
287
    UDPContext *s = h->priv_data;
288
    return s->local_port;
289
}
290
291
/**
292
 * Return the udp file handle for select() usage to wait for several RTP
293
 * streams at the same time.
294
 * @param h media file context
295
 */
296 8ef30ac1 Aurelien Jacobs
#if !FF_API_UDP_GET_FILE
297 f0a80394 Ronald S. Bultje
static
298
#endif
299 2d225591 Fabrice Bellard
int udp_get_file_handle(URLContext *h)
300
{
301
    UDPContext *s = h->priv_data;
302
    return s->udp_fd;
303
}
304
305
/* put it in UDP context */
306 de6d9b64 Fabrice Bellard
/* return non zero if error */
307
static int udp_open(URLContext *h, const char *uri, int flags)
308
{
309
    char hostname[1024];
310 36b53281 Jindřich Makovička
    int port, udp_fd = -1, tmp, bind_ret = -1;
311 2d225591 Fabrice Bellard
    UDPContext *s = NULL;
312 88730be6 Måns Rullgård
    int is_output;
313 2d225591 Fabrice Bellard
    const char *p;
314
    char buf[256];
315 aa519c47 Luca Abeni
    struct sockaddr_storage my_addr;
316
    int len;
317 00952be4 David Fries
    int reuse_specified = 0;
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 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
335 00952be4 David Fries
            const char *endptr=NULL;
336
            s->reuse_socket = strtol(buf, &endptr, 10);
337
            /* assume if no digits were found it is a request to enable it */
338
            if (buf == endptr)
339
                s->reuse_socket = 1;
340
            reuse_specified = 1;
341
        }
342 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
343 2d225591 Fabrice Bellard
            s->ttl = strtol(buf, NULL, 10);
344
        }
345 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
346 2d225591 Fabrice Bellard
            s->local_port = strtol(buf, NULL, 10);
347
        }
348 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
349 9899efb4 Max Krasnyansky
            h->max_packet_size = strtol(buf, NULL, 10);
350
        }
351 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
352 236bb1ab Jindřich Makovička
            s->buffer_size = strtol(buf, NULL, 10);
353
        }
354 ab0287fc Anton Khirnov
        if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
355 6766a354 Martin Storsjö
            s->is_connected = strtol(buf, NULL, 10);
356
        }
357 de6d9b64 Fabrice Bellard
    }
358 2d225591 Fabrice Bellard
359
    /* fill the dest addr */
360 f3bfe388 Måns Rullgård
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
361 115329f1 Diego Biurrun
362 f3bfe388 Måns Rullgård
    /* XXX: fix av_url_split */
363 2d225591 Fabrice Bellard
    if (hostname[0] == '\0' || hostname[0] == '?') {
364
        /* only accepts null hostname if input */
365 35b74c3d Luca Abeni
        if (flags & URL_WRONLY)
366 2d225591 Fabrice Bellard
            goto fail;
367
    } else {
368 82505611 Reimar Döffinger
        if (udp_set_remote_url(h, uri) < 0)
369
            goto fail;
370 2d225591 Fabrice Bellard
    }
371 de6d9b64 Fabrice Bellard
372 882d00f2 Luca Abeni
    if (s->is_multicast && !(h->flags & URL_WRONLY))
373
        s->local_port = port;
374 aa519c47 Luca Abeni
    udp_fd = udp_socket_create(s, &my_addr, &len);
375 2d225591 Fabrice Bellard
    if (udp_fd < 0)
376
        goto fail;
377
378 00952be4 David Fries
    /* Follow the requested reuse option, unless it's multicast in which
379
     * case enable reuse unless explicitely disabled.
380
     */
381
    if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
382
        s->reuse_socket = 1;
383 34ecc397 Thijs
        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
384
            goto fail;
385 00952be4 David Fries
    }
386 34ecc397 Thijs
387 de6d9b64 Fabrice Bellard
    /* the bind is needed to give a port to the socket now */
388 36b53281 Jindřich Makovička
    /* if multicast, try the multicast address bind first */
389
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
390
        bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
391
    }
392
    /* bind to the local address if not multicast or if the multicast
393
     * bind failed */
394
    if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
395 de6d9b64 Fabrice Bellard
        goto fail;
396
397 d4936869 Luca Abeni
    len = sizeof(my_addr);
398
    getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
399 aa519c47 Luca Abeni
    s->local_port = udp_port(&my_addr, len);
400
401 7a91333f Hans Zandbelt
    if (s->is_multicast) {
402
        if (h->flags & URL_WRONLY) {
403 a8bde059 Luca Abeni
            /* output */
404 9cdcb04f Luca Abeni
            if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
405 7a91333f Hans Zandbelt
                goto fail;
406
        } else {
407 a8bde059 Luca Abeni
            /* input */
408 9cdcb04f Luca Abeni
            if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
409 7a91333f Hans Zandbelt
                goto fail;
410
        }
411
    }
412 de6d9b64 Fabrice Bellard
413 2d225591 Fabrice Bellard
    if (is_output) {
414
        /* limit the tx buf size to limit latency */
415 236bb1ab Jindřich Makovička
        tmp = s->buffer_size;
416 2d225591 Fabrice Bellard
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
417 086119b3 Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
418 2d225591 Fabrice Bellard
            goto fail;
419
        }
420 f18cae4d Ramiro Polla
    } else {
421
        /* set udp recv buffer size to the largest possible udp packet size to
422
         * avoid losing data on OSes that set this too low by default. */
423 236bb1ab Jindřich Makovička
        tmp = s->buffer_size;
424
        if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
425
            av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
426
        }
427 ce09d560 Jindřich Makovička
        /* make the socket non-blocking */
428
        ff_socket_nonblock(udp_fd, 1);
429 2d225591 Fabrice Bellard
    }
430 6766a354 Martin Storsjö
    if (s->is_connected) {
431
        if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
432
            av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
433
            goto fail;
434
        }
435
    }
436 2d225591 Fabrice Bellard
437
    s->udp_fd = udp_fd;
438 de6d9b64 Fabrice Bellard
    return 0;
439
 fail:
440 2d225591 Fabrice Bellard
    if (udp_fd >= 0)
441 9ddd71fc François Revol
        closesocket(udp_fd);
442 2d225591 Fabrice Bellard
    av_free(s);
443 6f3e0b21 Panagiotis Issaris
    return AVERROR(EIO);
444 de6d9b64 Fabrice Bellard
}
445
446 0c1a9eda Zdenek Kabelac
static int udp_read(URLContext *h, uint8_t *buf, int size)
447 de6d9b64 Fabrice Bellard
{
448
    UDPContext *s = h->priv_data;
449 a8475bbd Luca Barbato
    struct pollfd p = {s->udp_fd, POLLIN, 0};
450 191e8ca7 Måns Rullgård
    int len;
451 7d084299 Jindřich Makovička
    int ret;
452 2d225591 Fabrice Bellard
453
    for(;;) {
454 7d084299 Jindřich Makovička
        if (url_interrupt_cb())
455 c76374c6 Nicolas George
            return AVERROR_EXIT;
456 a8475bbd Luca Barbato
        ret = poll(&p, 1, 100);
457 cae9a15c Martin Storsjö
        if (ret < 0) {
458 28c4741a Martin Storsjö
            if (ff_neterrno() == AVERROR(EINTR))
459 cae9a15c Martin Storsjö
                continue;
460 7d084299 Jindřich Makovička
            return AVERROR(EIO);
461 cae9a15c Martin Storsjö
        }
462 a8475bbd Luca Barbato
        if (!(ret == 1 && p.revents & POLLIN))
463 7d084299 Jindřich Makovička
            continue;
464 ce09d560 Jindřich Makovička
        len = recv(s->udp_fd, buf, size, 0);
465 2d225591 Fabrice Bellard
        if (len < 0) {
466 28c4741a Martin Storsjö
            if (ff_neterrno() != AVERROR(EAGAIN) &&
467
                ff_neterrno() != AVERROR(EINTR))
468 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
469 2d225591 Fabrice Bellard
        } else {
470
            break;
471
        }
472
    }
473
    return len;
474 de6d9b64 Fabrice Bellard
}
475
476 27241cbf Martin Storsjö
static int udp_write(URLContext *h, const uint8_t *buf, int size)
477 de6d9b64 Fabrice Bellard
{
478
    UDPContext *s = h->priv_data;
479 2d225591 Fabrice Bellard
    int ret;
480
481
    for(;;) {
482 6766a354 Martin Storsjö
        if (!s->is_connected) {
483 25968c39 Martin Storsjö
            ret = sendto (s->udp_fd, buf, size, 0,
484
                          (struct sockaddr *) &s->dest_addr,
485
                          s->dest_addr_len);
486 6766a354 Martin Storsjö
        } else
487
            ret = send(s->udp_fd, buf, size, 0);
488 2d225591 Fabrice Bellard
        if (ret < 0) {
489 28c4741a Martin Storsjö
            if (ff_neterrno() != AVERROR(EINTR) &&
490
                ff_neterrno() != AVERROR(EAGAIN))
491 25a2ebb9 Martin Storsjö
                return ff_neterrno();
492 2d225591 Fabrice Bellard
        } else {
493
            break;
494
        }
495 de6d9b64 Fabrice Bellard
    }
496 2d225591 Fabrice Bellard
    return size;
497
}
498
499
static int udp_close(URLContext *h)
500
{
501
    UDPContext *s = h->priv_data;
502
503 7a91333f Hans Zandbelt
    if (s->is_multicast && !(h->flags & URL_WRONLY))
504 9cdcb04f Luca Abeni
        udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
505 9ddd71fc François Revol
    closesocket(s->udp_fd);
506 2d225591 Fabrice Bellard
    av_free(s);
507
    return 0;
508 de6d9b64 Fabrice Bellard
}
509
510 c6610a21 Diego Elio Pettenò
URLProtocol ff_udp_protocol = {
511 de6d9b64 Fabrice Bellard
    "udp",
512
    udp_open,
513 2d225591 Fabrice Bellard
    udp_read,
514 de6d9b64 Fabrice Bellard
    udp_write,
515
    NULL, /* seek */
516
    udp_close,
517 f0a80394 Ronald S. Bultje
    .url_get_file_handle = udp_get_file_handle,
518 de6d9b64 Fabrice Bellard
};