]> git.sesse.net Git - ffmpeg/blob - libavformat/udp.c
Revert "udp: add multicast input interface selection"
[ffmpeg] / libavformat / udp.c
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 "libavutil/fifo.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/avstring.h"
35 #include <unistd.h>
36 #include "internal.h"
37 #include "network.h"
38 #include "os_support.h"
39 #include "url.h"
40
41 #if HAVE_PTHREADS
42 #include <pthread.h>
43 #endif
44
45 #include <sys/time.h>
46
47 #ifndef IPV6_ADD_MEMBERSHIP
48 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
49 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
50 #endif
51
52 #define UDP_TX_BUF_SIZE 32768
53 #define UDP_MAX_PKT_SIZE 65536
54
55 typedef struct {
56     int udp_fd;
57     int ttl;
58     int buffer_size;
59     int is_multicast;
60     int local_port;
61     int reuse_socket;
62     struct sockaddr_storage dest_addr;
63     int dest_addr_len;
64     int is_connected;
65
66     /* Circular Buffer variables for use in UDP receive code */
67     int circular_buffer_size;
68     AVFifoBuffer *fifo;
69     int circular_buffer_error;
70 #if HAVE_PTHREADS
71     pthread_t circular_buffer_thread;
72     pthread_mutex_t mutex;
73     pthread_cond_t cond;
74     int thread_started;
75     volatile int exit_thread;
76 #endif
77     uint8_t tmp[UDP_MAX_PKT_SIZE+4];
78     int remaining_in_dg;
79 } UDPContext;
80
81 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
82                                  struct sockaddr *addr)
83 {
84 #ifdef IP_MULTICAST_TTL
85     if (addr->sa_family == AF_INET) {
86         if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
87             av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
88             return -1;
89         }
90     }
91 #endif
92 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
93     if (addr->sa_family == AF_INET6) {
94         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
95             av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
96             return -1;
97         }
98     }
99 #endif
100     return 0;
101 }
102
103 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
104 {
105 #ifdef IP_ADD_MEMBERSHIP
106     if (addr->sa_family == AF_INET) {
107         struct ip_mreq mreq;
108
109         mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
110         mreq.imr_interface.s_addr= INADDR_ANY;
111         if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
112             av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
113             return -1;
114         }
115     }
116 #endif
117 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
118     if (addr->sa_family == AF_INET6) {
119         struct ipv6_mreq mreq6;
120
121         memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
122         mreq6.ipv6mr_interface= 0;
123         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
124             av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
125             return -1;
126         }
127     }
128 #endif
129     return 0;
130 }
131
132 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
133 {
134 #ifdef IP_DROP_MEMBERSHIP
135     if (addr->sa_family == AF_INET) {
136         struct ip_mreq mreq;
137
138         mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
139         mreq.imr_interface.s_addr= INADDR_ANY;
140         if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
141             av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
142             return -1;
143         }
144     }
145 #endif
146 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
147     if (addr->sa_family == AF_INET6) {
148         struct ipv6_mreq mreq6;
149
150         memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
151         mreq6.ipv6mr_interface= 0;
152         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
153             av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
154             return -1;
155         }
156     }
157 #endif
158     return 0;
159 }
160
161 static struct addrinfo* udp_resolve_host(const char *hostname, int port,
162                                          int type, int family, int flags)
163 {
164     struct addrinfo hints, *res = 0;
165     int error;
166     char sport[16];
167     const char *node = 0, *service = "0";
168
169     if (port > 0) {
170         snprintf(sport, sizeof(sport), "%d", port);
171         service = sport;
172     }
173     if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
174         node = hostname;
175     }
176     memset(&hints, 0, sizeof(hints));
177     hints.ai_socktype = type;
178     hints.ai_family   = family;
179     hints.ai_flags = flags;
180     if ((error = getaddrinfo(node, service, &hints, &res))) {
181         res = NULL;
182         av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
183     }
184
185     return res;
186 }
187
188 static int udp_set_url(struct sockaddr_storage *addr,
189                        const char *hostname, int port)
190 {
191     struct addrinfo *res0;
192     int addr_len;
193
194     res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
195     if (res0 == 0) return AVERROR(EIO);
196     memcpy(addr, res0->ai_addr, res0->ai_addrlen);
197     addr_len = res0->ai_addrlen;
198     freeaddrinfo(res0);
199
200     return addr_len;
201 }
202
203 static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr,
204                              int *addr_len, const char *localaddr)
205 {
206     int udp_fd = -1;
207     struct addrinfo *res0 = NULL, *res = NULL;
208     int family = AF_UNSPEC;
209
210     if (((struct sockaddr *) &s->dest_addr)->sa_family)
211         family = ((struct sockaddr *) &s->dest_addr)->sa_family;
212     res0 = udp_resolve_host(localaddr[0] ? localaddr : NULL, s->local_port,
213                             SOCK_DGRAM, family, AI_PASSIVE);
214     if (res0 == 0)
215         goto fail;
216     for (res = res0; res; res=res->ai_next) {
217         udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
218         if (udp_fd > 0) break;
219         av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
220     }
221
222     if (udp_fd < 0)
223         goto fail;
224
225     memcpy(addr, res->ai_addr, res->ai_addrlen);
226     *addr_len = res->ai_addrlen;
227
228     freeaddrinfo(res0);
229
230     return udp_fd;
231
232  fail:
233     if (udp_fd >= 0)
234         closesocket(udp_fd);
235     if(res0)
236         freeaddrinfo(res0);
237     return -1;
238 }
239
240 static int udp_port(struct sockaddr_storage *addr, int addr_len)
241 {
242     char sbuf[sizeof(int)*3+1];
243
244     if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0,  sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
245         av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
246         return -1;
247     }
248
249     return strtol(sbuf, NULL, 10);
250 }
251
252
253 /**
254  * If no filename is given to av_open_input_file because you want to
255  * get the local port first, then you must call this function to set
256  * the remote server address.
257  *
258  * url syntax: udp://host:port[?option=val...]
259  * option: 'ttl=n'       : set the ttl value (for multicast only)
260  *         'localport=n' : set the local port
261  *         'pkt_size=n'  : set max packet size
262  *         'reuse=1'     : enable reusing the socket
263  *
264  * @param h media file context
265  * @param uri of the remote server
266  * @return zero if no error.
267  */
268 int ff_udp_set_remote_url(URLContext *h, const char *uri)
269 {
270     UDPContext *s = h->priv_data;
271     char hostname[256], buf[10];
272     int port;
273     const char *p;
274
275     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
276
277     /* set the destination address */
278     s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
279     if (s->dest_addr_len < 0) {
280         return AVERROR(EIO);
281     }
282     s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
283     p = strchr(uri, '?');
284     if (p) {
285         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
286             int was_connected = s->is_connected;
287             s->is_connected = strtol(buf, NULL, 10);
288             if (s->is_connected && !was_connected) {
289                 if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
290                             s->dest_addr_len)) {
291                     s->is_connected = 0;
292                     av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
293                     return AVERROR(EIO);
294                 }
295             }
296         }
297     }
298
299     return 0;
300 }
301
302 /**
303  * Return the local port used by the UDP connection
304  * @param h media file context
305  * @return the local port number
306  */
307 int ff_udp_get_local_port(URLContext *h)
308 {
309     UDPContext *s = h->priv_data;
310     return s->local_port;
311 }
312
313 /**
314  * Return the udp file handle for select() usage to wait for several RTP
315  * streams at the same time.
316  * @param h media file context
317  */
318 static int udp_get_file_handle(URLContext *h)
319 {
320     UDPContext *s = h->priv_data;
321     return s->udp_fd;
322 }
323
324 #if HAVE_PTHREADS
325 static void *circular_buffer_task( void *_URLContext)
326 {
327     URLContext *h = _URLContext;
328     UDPContext *s = h->priv_data;
329     fd_set rfds;
330     struct timeval tv;
331
332     while(!s->exit_thread) {
333         int left;
334         int ret;
335         int len;
336
337         if (ff_check_interrupt(&h->interrupt_callback)) {
338             s->circular_buffer_error = AVERROR(EINTR);
339             goto end;
340         }
341
342         FD_ZERO(&rfds);
343         FD_SET(s->udp_fd, &rfds);
344         tv.tv_sec = 1;
345         tv.tv_usec = 0;
346         ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
347         if (ret < 0) {
348             if (ff_neterrno() == AVERROR(EINTR))
349                 continue;
350             s->circular_buffer_error = AVERROR(EIO);
351             goto end;
352         }
353
354         if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
355             continue;
356
357         /* How much do we have left to the end of the buffer */
358         /* Whats the minimum we can read so that we dont comletely fill the buffer */
359         left = av_fifo_space(s->fifo);
360
361         /* No Space left, error, what do we do now */
362         if(left < UDP_MAX_PKT_SIZE + 4) {
363             av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
364             s->circular_buffer_error = AVERROR(EIO);
365             goto end;
366         }
367         left = FFMIN(left, s->fifo->end - s->fifo->wptr);
368         len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
369         if (len < 0) {
370             if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
371                 s->circular_buffer_error = AVERROR(EIO);
372                 goto end;
373             }
374             continue;
375         }
376         AV_WL32(s->tmp, len);
377         pthread_mutex_lock(&s->mutex);
378         av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
379         pthread_cond_signal(&s->cond);
380         pthread_mutex_unlock(&s->mutex);
381     }
382
383 end:
384     pthread_mutex_lock(&s->mutex);
385     pthread_cond_signal(&s->cond);
386     pthread_mutex_unlock(&s->mutex);
387     return NULL;
388 }
389 #endif
390
391 /* put it in UDP context */
392 /* return non zero if error */
393 static int udp_open(URLContext *h, const char *uri, int flags)
394 {
395     char hostname[1024], localaddr[1024] = "";
396     int port, udp_fd = -1, tmp, bind_ret = -1;
397     UDPContext *s = h->priv_data;
398     int is_output;
399     const char *p;
400     char buf[256];
401     struct sockaddr_storage my_addr;
402     int len;
403     int reuse_specified = 0;
404
405     h->is_streamed = 1;
406     h->max_packet_size = 1472;
407
408     is_output = !(flags & AVIO_FLAG_READ);
409
410     s->ttl = 16;
411     s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
412
413     s->circular_buffer_size = 7*188*4096;
414
415     p = strchr(uri, '?');
416     if (p) {
417         if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
418             char *endptr = NULL;
419             s->reuse_socket = strtol(buf, &endptr, 10);
420             /* assume if no digits were found it is a request to enable it */
421             if (buf == endptr)
422                 s->reuse_socket = 1;
423             reuse_specified = 1;
424         }
425         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
426             s->ttl = strtol(buf, NULL, 10);
427         }
428         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
429             s->local_port = strtol(buf, NULL, 10);
430         }
431         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
432             h->max_packet_size = strtol(buf, NULL, 10);
433         }
434         if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
435             s->buffer_size = strtol(buf, NULL, 10);
436         }
437         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
438             s->is_connected = strtol(buf, NULL, 10);
439         }
440         if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
441             s->circular_buffer_size = strtol(buf, NULL, 10)*188;
442         }
443         if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
444             av_strlcpy(localaddr, buf, sizeof(localaddr));
445         }
446     }
447
448     /* fill the dest addr */
449     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
450
451     /* XXX: fix av_url_split */
452     if (hostname[0] == '\0' || hostname[0] == '?') {
453         /* only accepts null hostname if input */
454         if (!(flags & AVIO_FLAG_READ))
455             goto fail;
456     } else {
457         if (ff_udp_set_remote_url(h, uri) < 0)
458             goto fail;
459     }
460
461     if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
462         s->local_port = port;
463     udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
464     if (udp_fd < 0)
465         goto fail;
466
467     /* Follow the requested reuse option, unless it's multicast in which
468      * case enable reuse unless explicitly disabled.
469      */
470     if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
471         s->reuse_socket = 1;
472         if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
473             goto fail;
474     }
475
476     /* If multicast, try binding the multicast address first, to avoid
477      * receiving UDP packets from other sources aimed at the same UDP
478      * port. This fails on windows. This makes sending to the same address
479      * using sendto() fail, so only do it if we're opened in read-only mode. */
480     if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
481         bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
482     }
483     /* bind to the local address if not multicast or if the multicast
484      * bind failed */
485     /* the bind is needed to give a port to the socket now */
486     if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
487         av_log(h, AV_LOG_ERROR, "bind failed: %s\n", strerror(errno));
488         goto fail;
489     }
490
491     len = sizeof(my_addr);
492     getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
493     s->local_port = udp_port(&my_addr, len);
494
495     if (s->is_multicast) {
496         if (h->flags & AVIO_FLAG_WRITE) {
497             /* output */
498             if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
499                 goto fail;
500         }
501         if (h->flags & AVIO_FLAG_READ) {
502             /* input */
503             if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
504                 goto fail;
505         }
506     }
507
508     if (is_output) {
509         /* limit the tx buf size to limit latency */
510         tmp = s->buffer_size;
511         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
512             av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
513             goto fail;
514         }
515     } else {
516         /* set udp recv buffer size to the largest possible udp packet size to
517          * avoid losing data on OSes that set this too low by default. */
518         tmp = s->buffer_size;
519         if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
520             av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
521         }
522         /* make the socket non-blocking */
523         ff_socket_nonblock(udp_fd, 1);
524     }
525     if (s->is_connected) {
526         if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
527             av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
528             goto fail;
529         }
530     }
531
532     s->udp_fd = udp_fd;
533
534 #if HAVE_PTHREADS
535     if (!is_output && s->circular_buffer_size) {
536         int ret;
537
538         /* start the task going */
539         s->fifo = av_fifo_alloc(s->circular_buffer_size);
540         ret = pthread_mutex_init(&s->mutex, NULL);
541         if (ret != 0) {
542             av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
543             goto fail;
544         }
545         ret = pthread_cond_init(&s->cond, NULL);
546         if (ret != 0) {
547             av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
548             goto cond_fail;
549         }
550         ret = pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h);
551         if (ret != 0) {
552             av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
553             goto thread_fail;
554         }
555         s->thread_started = 1;
556     }
557 #endif
558
559     return 0;
560 #if HAVE_PTHREADS
561  thread_fail:
562     pthread_cond_destroy(&s->cond);
563  cond_fail:
564     pthread_mutex_destroy(&s->mutex);
565 #endif
566  fail:
567     if (udp_fd >= 0)
568         closesocket(udp_fd);
569     av_fifo_free(s->fifo);
570     return AVERROR(EIO);
571 }
572
573 static int udp_read(URLContext *h, uint8_t *buf, int size)
574 {
575     UDPContext *s = h->priv_data;
576     int ret;
577     int avail;
578
579 #if HAVE_PTHREADS
580     if (s->fifo) {
581         pthread_mutex_lock(&s->mutex);
582         do {
583             avail = av_fifo_size(s->fifo);
584             if (avail) { // >=size) {
585                 uint8_t tmp[4];
586                 pthread_mutex_unlock(&s->mutex);
587
588                 av_fifo_generic_read(s->fifo, tmp, 4, NULL);
589                 avail= AV_RL32(tmp);
590                 if(avail > size){
591                     av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
592                     avail= size;
593                 }
594
595                 av_fifo_generic_read(s->fifo, buf, avail, NULL);
596                 av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
597                 return avail;
598             } else if(s->circular_buffer_error){
599                 pthread_mutex_unlock(&s->mutex);
600                 return s->circular_buffer_error;
601             } else if(h->flags & AVIO_FLAG_NONBLOCK) {
602                 pthread_mutex_unlock(&s->mutex);
603                 return AVERROR(EAGAIN);
604             }
605             else {
606                 pthread_cond_wait(&s->cond, &s->mutex);
607             }
608         } while( 1);
609     }
610 #endif
611
612     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
613         ret = ff_network_wait_fd(s->udp_fd, 0);
614         if (ret < 0)
615             return ret;
616     }
617     ret = recv(s->udp_fd, buf, size, 0);
618
619     return ret < 0 ? ff_neterrno() : ret;
620 }
621
622 static int udp_write(URLContext *h, const uint8_t *buf, int size)
623 {
624     UDPContext *s = h->priv_data;
625     int ret;
626
627     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
628         ret = ff_network_wait_fd(s->udp_fd, 1);
629         if (ret < 0)
630             return ret;
631     }
632
633     if (!s->is_connected) {
634         ret = sendto (s->udp_fd, buf, size, 0,
635                       (struct sockaddr *) &s->dest_addr,
636                       s->dest_addr_len);
637     } else
638         ret = send(s->udp_fd, buf, size, 0);
639
640     return ret < 0 ? ff_neterrno() : ret;
641 }
642
643 static int udp_close(URLContext *h)
644 {
645     UDPContext *s = h->priv_data;
646     int ret;
647
648     if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
649         udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
650     closesocket(s->udp_fd);
651     av_fifo_free(s->fifo);
652 #if HAVE_PTHREADS
653     if (s->thread_started) {
654         s->exit_thread = 1;
655         ret = pthread_join(s->circular_buffer_thread, NULL);
656         if (ret != 0)
657             av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
658     }
659
660     pthread_mutex_destroy(&s->mutex);
661     pthread_cond_destroy(&s->cond);
662 #endif
663     return 0;
664 }
665
666 URLProtocol ff_udp_protocol = {
667     .name                = "udp",
668     .url_open            = udp_open,
669     .url_read            = udp_read,
670     .url_write           = udp_write,
671     .url_close           = udp_close,
672     .url_get_file_handle = udp_get_file_handle,
673     .priv_data_size      = sizeof(UDPContext),
674     .flags               = URL_PROTOCOL_FLAG_NETWORK,
675 };