Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ 42756021

History | View | Annotate | Download (20.3 KB)

1
/*
2
 * UDP prototype streaming system
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * UDP protocol
25
 */
26

    
27
#define _BSD_SOURCE     /* Needed for using struct ip_mreq with recent glibc */
28

    
29
#include "avformat.h"
30
#include "avio_internal.h"
31
#include "libavutil/parseutils.h"
32
#include <unistd.h>
33
#include "internal.h"
34
#include "network.h"
35
#include "os_support.h"
36
#include "url.h"
37
#include <pthread.h>
38
#include <semaphore.h>
39
#include <sys/resource.h>
40
#include <sys/time.h>
41

    
42
#ifndef IPV6_ADD_MEMBERSHIP
43
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
44
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
45
#endif
46

    
47
typedef struct {
48
    int udp_fd;
49
    int ttl;
50
    int buffer_size;
51
    int is_multicast;
52
    int local_port;
53
    int reuse_socket;
54
    struct sockaddr_storage dest_addr;
55
    int dest_addr_len;
56
    int is_connected;
57

    
58
    /* Circular Buffer variables for use in UDP receive code */
59
    unsigned char *circular_buffer;
60
    int circular_buffer_head;
61
    int circular_buffer_size;
62
    int circular_buffer_tail;
63
    int circular_buffer_available;
64
    int circular_buffer_available_max;
65
    int circular_buffer_error;
66
    pthread_t circular_buffer_thread;
67
    sem_t circular_buffer_semaphore;
68
} UDPContext;
69

    
70
#define min(X,Y)                ((X)<(Y) ? (X):(Y))
71
#define max(X,Y)                ((X)>(Y) ? (X):(Y))
72

    
73
#define UDP_TX_BUF_SIZE 32768
74
#define UDP_MAX_PKT_SIZE 65536
75

    
76
static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
77
                                 struct sockaddr *addr)
78
{
79
#ifdef IP_MULTICAST_TTL
80
    if (addr->sa_family == AF_INET) {
81
        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
82
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
83
            return -1;
84
        }
85
    }
86
#endif
87
#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
88
    if (addr->sa_family == AF_INET6) {
89
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
90
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
91
            return -1;
92
        }
93
    }
94
#endif
95
    return 0;
96
}
97

    
98
static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
99
{
100
#ifdef IP_ADD_MEMBERSHIP
101
    if (addr->sa_family == AF_INET) {
102
        struct ip_mreq mreq;
103

    
104
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
105
        mreq.imr_interface.s_addr= INADDR_ANY;
106
        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
107
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
108
            return -1;
109
        }
110
    }
111
#endif
112
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
113
    if (addr->sa_family == AF_INET6) {
114
        struct ipv6_mreq mreq6;
115

    
116
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
117
        mreq6.ipv6mr_interface= 0;
118
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
119
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
120
            return -1;
121
        }
122
    }
123
#endif
124
    return 0;
125
}
126

    
127
static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
128
{
129
#ifdef IP_DROP_MEMBERSHIP
130
    if (addr->sa_family == AF_INET) {
131
        struct ip_mreq mreq;
132

    
133
        mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
134
        mreq.imr_interface.s_addr= INADDR_ANY;
135
        if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
136
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
137
            return -1;
138
        }
139
    }
140
#endif
141
#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
142
    if (addr->sa_family == AF_INET6) {
143
        struct ipv6_mreq mreq6;
144

    
145
        memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
146
        mreq6.ipv6mr_interface= 0;
147
        if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
148
            av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
149
            return -1;
150
        }
151
    }
152
#endif
153
    return 0;
154
}
155

    
156
static struct addrinfo* udp_resolve_host(const char *hostname, int port,
157
                                         int type, int family, int flags)
158
{
159
    struct addrinfo hints, *res = 0;
160
    int error;
161
    char sport[16];
162
    const char *node = 0, *service = "0";
163

    
164
    if (port > 0) {
165
        snprintf(sport, sizeof(sport), "%d", port);
166
        service = sport;
167
    }
168
    if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
169
        node = hostname;
170
    }
171
    memset(&hints, 0, sizeof(hints));
172
    hints.ai_socktype = type;
173
    hints.ai_family   = family;
174
    hints.ai_flags = flags;
175
    if ((error = getaddrinfo(node, service, &hints, &res))) {
176
        res = NULL;
177
        av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
178
    }
179

    
180
    return res;
181
}
182

    
183
static int udp_set_url(struct sockaddr_storage *addr,
184
                       const char *hostname, int port)
185
{
186
    struct addrinfo *res0;
187
    int addr_len;
188

    
189
    res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
190
    if (res0 == 0) return AVERROR(EIO);
191
    memcpy(addr, res0->ai_addr, res0->ai_addrlen);
192
    addr_len = res0->ai_addrlen;
193
    freeaddrinfo(res0);
194

    
195
    return addr_len;
196
}
197

    
198
static int udp_socket_create(UDPContext *s,
199
                             struct sockaddr_storage *addr, int *addr_len)
200
{
201
    int udp_fd = -1;
202
    struct addrinfo *res0 = NULL, *res = NULL;
203
    int family = AF_UNSPEC;
204

    
205
    if (((struct sockaddr *) &s->dest_addr)->sa_family)
206
        family = ((struct sockaddr *) &s->dest_addr)->sa_family;
207
    res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
208
    if (res0 == 0)
209
        goto fail;
210
    for (res = res0; res; res=res->ai_next) {
211
        udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
212
        if (udp_fd > 0) break;
213
        av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
214
    }
215

    
216
    if (udp_fd < 0)
217
        goto fail;
218

    
219
    memcpy(addr, res->ai_addr, res->ai_addrlen);
220
    *addr_len = res->ai_addrlen;
221

    
222
    freeaddrinfo(res0);
223

    
224
    return udp_fd;
225

    
226
 fail:
227
    if (udp_fd >= 0)
228
        closesocket(udp_fd);
229
    if(res0)
230
        freeaddrinfo(res0);
231
    return -1;
232
}
233

    
234
static int udp_port(struct sockaddr_storage *addr, int addr_len)
235
{
236
    char sbuf[sizeof(int)*3+1];
237

    
238
    if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0,  sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
239
        av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
240
        return -1;
241
    }
242

    
243
    return strtol(sbuf, NULL, 10);
244
}
245

    
246

    
247
/**
248
 * If no filename is given to av_open_input_file because you want to
249
 * get the local port first, then you must call this function to set
250
 * the remote server address.
251
 *
252
 * url syntax: udp://host:port[?option=val...]
253
 * option: 'ttl=n'       : set the ttl value (for multicast only)
254
 *         'localport=n' : set the local port
255
 *         'pkt_size=n'  : set max packet size
256
 *         'reuse=1'     : enable reusing the socket
257
 *
258
 * @param h media file context
259
 * @param uri of the remote server
260
 * @return zero if no error.
261
 */
262
int ff_udp_set_remote_url(URLContext *h, const char *uri)
263
{
264
    UDPContext *s = h->priv_data;
265
    char hostname[256], buf[10];
266
    int port;
267
    const char *p;
268

    
269
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
270

    
271
    /* set the destination address */
272
    s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
273
    if (s->dest_addr_len < 0) {
274
        return AVERROR(EIO);
275
    }
276
    s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
277
    p = strchr(uri, '?');
278
    if (p) {
279
        if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
280
            int was_connected = s->is_connected;
281
            s->is_connected = strtol(buf, NULL, 10);
282
            if (s->is_connected && !was_connected) {
283
                if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
284
                            s->dest_addr_len)) {
285
                    s->is_connected = 0;
286
                    av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
287
                    return AVERROR(EIO);
288
                }
289
            }
290
        }
291
    }
292

    
293
    return 0;
294
}
295

    
296
/**
297
 * Return the local port used by the UDP connection
298
 * @param h media file context
299
 * @return the local port number
300
 */
301
int ff_udp_get_local_port(URLContext *h)
302
{
303
    UDPContext *s = h->priv_data;
304
    return s->local_port;
305
}
306

    
307
/**
308
 * Return the udp file handle for select() usage to wait for several RTP
309
 * streams at the same time.
310
 * @param h media file context
311
 */
312
static int udp_get_file_handle(URLContext *h)
313
{
314
    UDPContext *s = h->priv_data;
315
    return s->udp_fd;
316
}
317

    
318
static void *circular_buffer_task( void *_URLContext)
319
{
320
    URLContext *h = _URLContext;
321
    UDPContext *s = h->priv_data;
322
    fd_set rfds;
323
    struct timeval tv;
324

    
325
    for(;;) {
326
        int left;
327
        int ret;
328
        int len;
329

    
330
        if (url_interrupt_cb()) {
331
            s->circular_buffer_error = EINTR;
332
            return NULL;
333
        }
334

    
335
        FD_ZERO(&rfds);
336
        FD_SET(s->udp_fd, &rfds);
337
        tv.tv_sec = 1;
338
        tv.tv_usec = 0;
339
        ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
340
        if (ret < 0) {
341
            if (ff_neterrno() == AVERROR(EINTR))
342
                continue;
343
            s->circular_buffer_error = EIO;
344
            return NULL;
345
        }
346

    
347
        if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
348
            continue;
349

    
350
        /* How much do we have left to the end of the buffer */
351
        left = s->circular_buffer_size-s->circular_buffer_head;
352
        /* Whats the minimum we can read so that we dont comletely fill the buffer */
353
        sem_wait( &s->circular_buffer_semaphore);
354
        left = min( left, s->circular_buffer_size-s->circular_buffer_available);
355
        sem_post( &s->circular_buffer_semaphore );
356
        /* No Space left, error, what do we do now */
357
        if( !left) {
358
            av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
359
            s->circular_buffer_error = EIO;
360
            return NULL;
361
        }
362
        len = recv(s->udp_fd, s->circular_buffer+s->circular_buffer_head, left, 0);
363
        if (len < 0) {
364
            if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
365
                s->circular_buffer_error = EIO;
366
                return NULL;
367
            }
368
        }
369
        s->circular_buffer_head += len;
370
        sem_wait( &s->circular_buffer_semaphore);
371
        s->circular_buffer_available += len;
372
        s->circular_buffer_available_max = max( s->circular_buffer_available_max, s->circular_buffer_available);
373
        sem_post( &s->circular_buffer_semaphore );
374
        if( s->circular_buffer_head>=s->circular_buffer_size)
375
            s->circular_buffer_head -= s->circular_buffer_size;
376
    }
377

    
378
    return NULL;
379
}
380

    
381
/* put it in UDP context */
382
/* return non zero if error */
383
static int udp_open(URLContext *h, const char *uri, int flags)
384
{
385
    char hostname[1024];
386
    int port, udp_fd = -1, tmp, bind_ret = -1;
387
    UDPContext *s = NULL;
388
    int is_output;
389
    const char *p;
390
    char buf[256];
391
    struct sockaddr_storage my_addr;
392
    int len;
393
    int reuse_specified = 0;
394

    
395
    h->is_streamed = 1;
396
    h->max_packet_size = 1472;
397

    
398
    is_output = !(flags & AVIO_FLAG_READ);
399

    
400
    s = av_mallocz(sizeof(UDPContext));
401
    if (!s)
402
        return AVERROR(ENOMEM);
403

    
404
    h->priv_data = s;
405
    s->ttl = 16;
406
    s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
407

    
408
    s->circular_buffer_size = 7*188*4096;
409

    
410
    p = strchr(uri, '?');
411
    if (p) {
412
        if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
413
            char *endptr=NULL;
414
            s->reuse_socket = strtol(buf, &endptr, 10);
415
            /* assume if no digits were found it is a request to enable it */
416
            if (buf == endptr)
417
                s->reuse_socket = 1;
418
            reuse_specified = 1;
419
        }
420
        if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
421
            s->ttl = strtol(buf, NULL, 10);
422
        }
423
        if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
424
            s->local_port = strtol(buf, NULL, 10);
425
        }
426
        if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
427
            h->max_packet_size = strtol(buf, NULL, 10);
428
        }
429
        if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
430
            s->buffer_size = strtol(buf, NULL, 10);
431
        }
432
        if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
433
            s->is_connected = strtol(buf, NULL, 10);
434
        }
435
        if (av_find_info_tag(buf, sizeof(buf), "buf_size", p)) {
436
            s->circular_buffer_size = strtol(buf, NULL, 10)*188;
437
        }
438
    }
439

    
440
    /* fill the dest addr */
441
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
442

    
443
    /* XXX: fix av_url_split */
444
    if (hostname[0] == '\0' || hostname[0] == '?') {
445
        /* only accepts null hostname if input */
446
        if (!(flags & AVIO_FLAG_READ))
447
            goto fail;
448
    } else {
449
        if (ff_udp_set_remote_url(h, uri) < 0)
450
            goto fail;
451
    }
452

    
453
    if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
454
        s->local_port = port;
455
    udp_fd = udp_socket_create(s, &my_addr, &len);
456
    if (udp_fd < 0)
457
        goto fail;
458

    
459
    /* Follow the requested reuse option, unless it's multicast in which
460
     * case enable reuse unless explicitely disabled.
461
     */
462
    if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
463
        s->reuse_socket = 1;
464
        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
465
            goto fail;
466
    }
467

    
468
    /* the bind is needed to give a port to the socket now */
469
    /* if multicast, try the multicast address bind first */
470
    if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
471
        bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
472
    }
473
    /* bind to the local address if not multicast or if the multicast
474
     * bind failed */
475
    if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
476
        goto fail;
477

    
478
    len = sizeof(my_addr);
479
    getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
480
    s->local_port = udp_port(&my_addr, len);
481

    
482
    if (s->is_multicast) {
483
        if (!(h->flags & AVIO_FLAG_READ)) {
484
            /* output */
485
            if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
486
                goto fail;
487
        } else {
488
            /* input */
489
            if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
490
                goto fail;
491
        }
492
    }
493

    
494
    if (is_output) {
495
        /* limit the tx buf size to limit latency */
496
        tmp = s->buffer_size;
497
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
498
            av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
499
            goto fail;
500
        }
501
    } else {
502
        /* set udp recv buffer size to the largest possible udp packet size to
503
         * avoid losing data on OSes that set this too low by default. */
504
        tmp = s->buffer_size;
505
        if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
506
            av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
507
        }
508
        /* make the socket non-blocking */
509
        ff_socket_nonblock(udp_fd, 1);
510
    }
511
    if (s->is_connected) {
512
        if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
513
            av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
514
            goto fail;
515
        }
516
    }
517

    
518
    s->udp_fd = udp_fd;
519

    
520
    if (!is_output && s->circular_buffer_size) {
521
        /* start the task going */
522
        s->circular_buffer = av_malloc( s->circular_buffer_size);
523
        if (sem_init( &s->circular_buffer_semaphore, PTHREAD_PROCESS_PRIVATE, 1 )) {
524
            av_log(h, AV_LOG_ERROR, "sem_init failed\n");
525
            goto fail;
526
        }
527
        if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) {
528
            av_log(h, AV_LOG_ERROR, "pthread_create failed\n");
529
            goto fail;
530
        }
531
    }
532

    
533
    return 0;
534
 fail:
535
    if (udp_fd >= 0)
536
        closesocket(udp_fd);
537
        if (s->circular_buffer) {
538
            sem_destroy( &s->circular_buffer_semaphore);
539
            av_free( s->circular_buffer);
540
        }
541
    av_free(s);
542
    return AVERROR(EIO);
543
}
544

    
545
static int udp_read(URLContext *h, uint8_t *buf, int size)
546
{
547
    UDPContext *s = h->priv_data;
548
    int ret;
549
    int avail;
550
    int left;
551
    fd_set rfds;
552
    struct timeval tv;
553

    
554
    if (s->circular_buffer_thread) {
555

    
556
        do {
557
            sem_wait( &s->circular_buffer_semaphore );
558
            avail = s->circular_buffer_available;
559
            sem_post( &s->circular_buffer_semaphore );
560
            if (avail) { // >=size) {
561

    
562
                // Maximum amount available
563
                size = min( avail, size);
564
                // Whats left till the end of the circular buffer
565
                left = s->circular_buffer_size-s->circular_buffer_tail;
566
                // How much do we need, all?
567
                left = min( left, size);
568
                // Get the first block
569
                memcpy( buf, s->circular_buffer+s->circular_buffer_tail, left);
570
                // Have we any more, this will be from the start of the buffer
571
                if (size-left)
572
                    memcpy( buf+left, s->circular_buffer, size-left);
573
                // Check for the tail wrapping around
574
                s->circular_buffer_tail += size;
575
                if( s->circular_buffer_tail>=s->circular_buffer_size)
576
                    s->circular_buffer_tail -= s->circular_buffer_size;
577
                // Update the available amount
578
                sem_wait( &s->circular_buffer_semaphore );
579
                s->circular_buffer_available -= size;
580
                sem_post( &s->circular_buffer_semaphore );
581
                return size;
582
            }
583
            else {
584
                FD_ZERO(&rfds);
585
                FD_SET(s->udp_fd, &rfds);
586
                tv.tv_sec = 1;
587
                tv.tv_usec = 0;
588
                ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
589
                if (ret<0)
590
                    return ret;
591
            }
592
        } while( 1);
593
    }
594

    
595
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
596
        ret = ff_network_wait_fd(s->udp_fd, 0);
597
        if (ret < 0)
598
            return ret;
599
    }
600
    ret = recv(s->udp_fd, buf, size, 0);
601

    
602
    return ret < 0 ? ff_neterrno() : ret;
603
}
604

    
605
static int udp_write(URLContext *h, const uint8_t *buf, int size)
606
{
607
    UDPContext *s = h->priv_data;
608
    int ret;
609

    
610
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
611
        ret = ff_network_wait_fd(s->udp_fd, 1);
612
        if (ret < 0)
613
            return ret;
614
    }
615

    
616
    if (!s->is_connected) {
617
        ret = sendto (s->udp_fd, buf, size, 0,
618
                      (struct sockaddr *) &s->dest_addr,
619
                      s->dest_addr_len);
620
    } else
621
        ret = send(s->udp_fd, buf, size, 0);
622

    
623
    return ret < 0 ? ff_neterrno() : ret;
624
}
625

    
626
static int udp_close(URLContext *h)
627
{
628
    UDPContext *s = h->priv_data;
629

    
630
    if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
631
        udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
632
    closesocket(s->udp_fd);
633
    if (s->circular_buffer) {
634
        sem_destroy( &s->circular_buffer_semaphore);
635
        av_free( s->circular_buffer);
636
        av_log( h, AV_LOG_INFO, "circular_buffer_info max:%d%%\r\n", (s->circular_buffer_available_max*100)/s->circular_buffer_size);
637
    }
638
    av_free(s);
639
    return 0;
640
}
641

    
642
URLProtocol ff_udp_protocol = {
643
    .name                = "udp",
644
    .url_open            = udp_open,
645
    .url_read            = udp_read,
646
    .url_write           = udp_write,
647
    .url_close           = udp_close,
648
    .url_get_file_handle = udp_get_file_handle,
649
};