FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/udp.c
Date: 2026-10-29 07:45:57
Exec Total Coverage
Lines: 0 523 0.0%
Functions: 0 19 0.0%
Branches: 0 345 0.0%

Line Branch Exec Source
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 _DEFAULT_SOURCE
28 #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
29
30 #include "avformat.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/fifo.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/log.h"
38 #include "libavutil/time.h"
39 #include "internal.h"
40 #include "network.h"
41 #include "os_support.h"
42 #include "url.h"
43 #include "ip.h"
44
45 #ifdef __APPLE__
46 #include "TargetConditionals.h"
47 #endif
48
49 #if HAVE_UDPLITE_H
50 #include "udplite.h"
51 #else
52 /* On many Linux systems, udplite.h is missing but the kernel supports UDP-Lite.
53 * So, we provide a fallback here.
54 */
55 #define UDPLITE_SEND_CSCOV 10
56 #define UDPLITE_RECV_CSCOV 11
57 #endif
58
59 #ifndef IPPROTO_UDPLITE
60 #define IPPROTO_UDPLITE 136
61 #endif
62
63 #if HAVE_THREADS
64 #include "libavutil/thread.h"
65 #endif
66
67 #ifndef IPV6_ADD_MEMBERSHIP
68 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
69 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
70 #endif
71
72 #define UDP_TX_BUF_SIZE 32768
73 #define UDP_RX_BUF_SIZE 393216
74 #define UDP_MAX_PKT_SIZE 65536
75 #define UDP_HEADER_SIZE 8
76 /* Bounds how long udp_close() waits when signaling the wakeup channel
77 * failed and no datagram arrives to release the rx thread.
78 */
79 #define UDP_RX_WAIT_TIMEOUT_MS 1000
80
81 typedef struct UDPQueuedPacketHeader {
82 int pkt_size;
83 struct sockaddr_storage addr;
84 socklen_t addr_len;
85 } UDPQueuedPacketHeader;
86
87 typedef struct UDPContext {
88 const AVClass *class;
89 int udp_fd;
90 int ttl;
91 int udplite_coverage;
92 int buffer_size;
93 int pkt_size;
94 int is_multicast;
95 int is_broadcast;
96 int local_port;
97 int reuse_socket;
98 int overrun_nonfatal;
99 struct sockaddr_storage dest_addr;
100 int dest_addr_len;
101 int is_connected;
102
103 /* Circular Buffer variables for use in UDP receive code */
104 int circular_buffer_size;
105 AVFifo *rx_fifo;
106 AVFifo *tx_fifo;
107 int circular_buffer_error;
108 int64_t bitrate; /* number of bits to send per second */
109 int64_t burst_bits;
110 int close_req;
111 #if HAVE_THREADS
112 pthread_t circular_buffer_thread;
113 pthread_mutex_t mutex;
114 pthread_cond_t cond;
115 FFNetWakeup wakeup; /* ends the rx thread's socket wait on close */
116 int thread_started;
117 #endif
118 uint8_t tmp[UDP_MAX_PKT_SIZE + sizeof(UDPQueuedPacketHeader)];
119 int remaining_in_dg;
120 char *localaddr;
121 int timeout;
122 int dscp;
123 struct sockaddr_storage local_addr_storage;
124 char *sources;
125 char *block;
126 IPSourceFilters filters;
127 struct sockaddr_storage last_recv_addr;
128 socklen_t last_recv_addr_len;
129 } UDPContext;
130
131 #define OFFSET(x) offsetof(UDPContext, x)
132 #define D AV_OPT_FLAG_DECODING_PARAM
133 #define E AV_OPT_FLAG_ENCODING_PARAM
134 static const AVOption options[] = {
135 { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
136 { "bitrate", "Bits to send per second", OFFSET(bitrate), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
137 { "burst_bits", "Max length of bursts in bits (when using bitrate)", OFFSET(burst_bits), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = E },
138 { "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E },
139 { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
140 { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
141 { "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
142 { "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E },
143 { "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D|E },
144 { "reuse_socket", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags = D|E },
145 { "broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
146 { "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 255, E },
147 { "dscp", "DSCP class for outgoing packets", OFFSET(dscp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 63, E },
148 { "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
149 { "fifo_size", "set the UDP circular buffer size (in 188-byte packets)", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = HAVE_THREADS ? 7*4096 : 0}, 0, INT_MAX, D },
150 { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D },
151 { "timeout", "set raise error timeout, in microseconds (only in read mode)",OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D },
152 { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
153 { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
154 { NULL }
155 };
156
157 static const AVClass udp_class = {
158 .class_name = "udp",
159 .item_name = av_default_item_name,
160 .option = options,
161 .version = LIBAVUTIL_VERSION_INT,
162 };
163
164 static const AVClass udplite_context_class = {
165 .class_name = "udplite",
166 .item_name = av_default_item_name,
167 .option = options,
168 .version = LIBAVUTIL_VERSION_INT,
169 };
170
171 ✗ static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
172 struct sockaddr *addr,
173 void *logctx)
174 {
175 int protocol, cmd;
176
177 /* There is some confusion in the world whether IP_MULTICAST_TTL
178 * takes a byte or an int as an argument.
179 * BSD seems to indicate byte so we are going with that and use
180 * int and fall back to byte to be safe */
181 ✗ switch (addr->sa_family) {
182 #ifdef IP_MULTICAST_TTL
183 ✗ case AF_INET:
184 ✗ protocol = IPPROTO_IP;
185 ✗ cmd = IP_MULTICAST_TTL;
186 ✗ break;
187 #endif
188 #ifdef IPV6_MULTICAST_HOPS
189 ✗ case AF_INET6:
190 ✗ protocol = IPPROTO_IPV6;
191 ✗ cmd = IPV6_MULTICAST_HOPS;
192 ✗ break;
193 #endif
194 ✗ default:
195 ✗ return 0;
196 }
197
198 ✗ if (setsockopt(sockfd, protocol, cmd, &mcastTTL, sizeof(mcastTTL)) < 0) {
199 /* BSD compatibility */
200 ✗ unsigned char ttl = (unsigned char) mcastTTL;
201
202 ✗ ff_log_net_error(logctx, AV_LOG_DEBUG, "setsockopt(IPV4/IPV6 MULTICAST TTL)");
203 ✗ if (setsockopt(sockfd, protocol, cmd, &ttl, sizeof(ttl)) < 0) {
204 ✗ ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV4/IPV6 MULTICAST TTL)");
205 ✗ return ff_neterrno();
206 }
207 }
208
209 ✗ return 0;
210 }
211
212 ✗ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,
213 struct sockaddr *local_addr, void *logctx)
214 {
215 #ifdef IP_ADD_MEMBERSHIP
216 ✗ if (addr->sa_family == AF_INET) {
217 struct ip_mreq mreq;
218
219 ✗ mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
220 ✗ if (local_addr)
221 ✗ mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
222 else
223 ✗ mreq.imr_interface.s_addr = INADDR_ANY;
224 ✗ if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
225 ✗ ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
226 ✗ return ff_neterrno();
227 }
228 }
229 #endif
230 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
231 ✗ if (addr->sa_family == AF_INET6) {
232 struct ipv6_mreq mreq6;
233
234 ✗ memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
235 //TODO: Interface index should be looked up from local_addr
236 ✗ mreq6.ipv6mr_interface = 0;
237 ✗ if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
238 ✗ ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
239 ✗ return ff_neterrno();
240 }
241 }
242 #endif
243 ✗ return 0;
244 }
245
246 ✗ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,
247 struct sockaddr *local_addr, void *logctx)
248 {
249 #ifdef IP_DROP_MEMBERSHIP
250 ✗ if (addr->sa_family == AF_INET) {
251 struct ip_mreq mreq;
252
253 ✗ mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
254 ✗ if (local_addr)
255 ✗ mreq.imr_interface = ((struct sockaddr_in *)local_addr)->sin_addr;
256 else
257 ✗ mreq.imr_interface.s_addr = INADDR_ANY;
258 ✗ if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
259 ✗ ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
260 ✗ return -1;
261 }
262 }
263 #endif
264 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
265 ✗ if (addr->sa_family == AF_INET6) {
266 struct ipv6_mreq mreq6;
267
268 ✗ memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
269 //TODO: Interface index should be looked up from local_addr
270 ✗ mreq6.ipv6mr_interface = 0;
271 ✗ if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
272 ✗ ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
273 ✗ return -1;
274 }
275 }
276 #endif
277 ✗ return 0;
278 }
279
280 ✗ static int udp_set_multicast_sources(URLContext *h,
281 int sockfd, struct sockaddr *addr,
282 int addr_len, struct sockaddr_storage *local_addr,
283 struct sockaddr_storage *sources,
284 int nb_sources, int include)
285 {
286 ✗ if (addr->sa_family != AF_INET) {
287 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE)
288 /* For IPv4 prefer the old approach, as that alone works reliably on
289 * Windows and it also supports supplying the interface based on its
290 * address. */
291 ✗ for (int i = 0; i < nb_sources; i++) {
292 struct group_source_req mreqs;
293 ✗ int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
294
295 //TODO: Interface index should be looked up from local_addr
296 ✗ mreqs.gsr_interface = 0;
297 ✗ memcpy(&mreqs.gsr_group, addr, addr_len);
298 ✗ memcpy(&mreqs.gsr_source, &sources[i], sizeof(*sources));
299
300 ✗ if (setsockopt(sockfd, level,
301 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
302 (const void *)&mreqs, sizeof(mreqs)) < 0) {
303 ✗ if (include)
304 ✗ ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
305 else
306 ✗ ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
307 ✗ return ff_neterrno();
308 }
309 }
310 ✗ return 0;
311 #else
312 av_log(h, AV_LOG_ERROR,
313 "Setting multicast sources only supported for IPv4\n");
314 return AVERROR(EINVAL);
315 #endif
316 }
317 #if HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
318 ✗ for (int i = 0; i < nb_sources; i++) {
319 struct ip_mreq_source mreqs;
320 ✗ if (sources[i].ss_family != AF_INET) {
321 ✗ av_log(h, AV_LOG_ERROR, "Source/block address %d is of incorrect protocol family\n", i + 1);
322 ✗ return AVERROR(EINVAL);
323 }
324
325 ✗ mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
326 ✗ if (local_addr)
327 ✗ mreqs.imr_interface = ((struct sockaddr_in *)local_addr)->sin_addr;
328 else
329 ✗ mreqs.imr_interface.s_addr = INADDR_ANY;
330 ✗ mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)&sources[i])->sin_addr.s_addr;
331
332 ✗ if (setsockopt(sockfd, IPPROTO_IP,
333 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
334 (const void *)&mreqs, sizeof(mreqs)) < 0) {
335 ✗ if (include)
336 ✗ ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
337 else
338 ✗ ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
339 ✗ return ff_neterrno();
340 }
341 }
342 #else
343 return AVERROR(ENOSYS);
344 #endif
345 ✗ return 0;
346 }
347 ✗ static int udp_set_url(URLContext *h,
348 struct sockaddr_storage *addr,
349 const char *hostname, int port)
350 {
351 struct addrinfo *res0;
352 int addr_len;
353
354 ✗ res0 = ff_ip_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
355 ✗ if (!res0) return AVERROR(EIO);
356 ✗ memcpy(addr, res0->ai_addr, res0->ai_addrlen);
357 ✗ addr_len = res0->ai_addrlen;
358 ✗ freeaddrinfo(res0);
359
360 ✗ return addr_len;
361 }
362
363 ✗ static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
364 socklen_t *addr_len, const char *localaddr)
365 {
366 ✗ UDPContext *s = h->priv_data;
367 ✗ int udp_fd = -1;
368 struct addrinfo *res0, *res;
369 ✗ int family = AF_UNSPEC;
370
371 ✗ if (((struct sockaddr *) &s->dest_addr)->sa_family)
372 ✗ family = ((struct sockaddr *) &s->dest_addr)->sa_family;
373 ✗ res0 = ff_ip_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
374 s->local_port,
375 SOCK_DGRAM, family, AI_PASSIVE);
376 ✗ if (!res0)
377 ✗ goto fail;
378 ✗ for (res = res0; res; res=res->ai_next) {
379 ✗ if (s->udplite_coverage)
380 ✗ udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, IPPROTO_UDPLITE, h);
381 else
382 ✗ udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0, h);
383 ✗ if (udp_fd != -1) break;
384 ✗ ff_log_net_error(h, AV_LOG_ERROR, "socket");
385 }
386
387 ✗ if (udp_fd < 0)
388 ✗ goto fail;
389
390 ✗ memcpy(addr, res->ai_addr, res->ai_addrlen);
391 ✗ *addr_len = res->ai_addrlen;
392
393 ✗ freeaddrinfo(res0);
394
395 ✗ return udp_fd;
396
397 ✗ fail:
398 ✗ if (udp_fd >= 0)
399 ✗ closesocket(udp_fd);
400 ✗ if(res0)
401 ✗ freeaddrinfo(res0);
402 ✗ return -1;
403 }
404
405 ✗ static int udp_port(struct sockaddr_storage *addr, int addr_len)
406 {
407 char sbuf[sizeof(int)*3+1];
408 int error;
409
410 ✗ if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
411 ✗ av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
412 ✗ return -1;
413 }
414
415 ✗ return strtol(sbuf, NULL, 10);
416 }
417
418
419 /**
420 * If no filename is given to av_open_input_file because you want to
421 * get the local port first, then you must call this function to set
422 * the remote server address.
423 *
424 * url syntax: udp://host:port[?option=val...]
425 * option: 'ttl=n' : set the ttl value (for multicast only)
426 * 'localport=n' : set the local port
427 * 'pkt_size=n' : set max packet size
428 * 'reuse=1' : enable reusing the socket
429 * 'overrun_nonfatal=1': survive in case of circular buffer overrun
430 *
431 * @param h media file context
432 * @param uri of the remote server
433 * @return zero if no error.
434 */
435 ✗ int ff_udp_set_remote_url(URLContext *h, const char *uri)
436 {
437 ✗ UDPContext *s = h->priv_data;
438 char hostname[256], buf[10];
439 int port;
440 const char *p;
441
442 ✗ av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
443
444 /* set the destination address */
445 ✗ s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
446 ✗ if (s->dest_addr_len < 0) {
447 ✗ return AVERROR(EIO);
448 }
449 ✗ s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
450 ✗ p = strchr(uri, '?');
451 ✗ if (p) {
452 ✗ if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
453 ✗ int was_connected = s->is_connected;
454 ✗ s->is_connected = strtol(buf, NULL, 10);
455 ✗ if (s->is_connected && !was_connected) {
456 ✗ if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
457 ✗ s->dest_addr_len)) {
458 ✗ s->is_connected = 0;
459 ✗ ff_log_net_error(h, AV_LOG_ERROR, "connect");
460 ✗ return AVERROR(EIO);
461 }
462 }
463 }
464 }
465
466 ✗ return 0;
467 }
468
469 /**
470 * This function is identical to ff_udp_set_remote_url, except that it takes a sockaddr directly.
471 */
472 ✗ int ff_udp_set_remote_addr(URLContext *h, const struct sockaddr *dest_addr, socklen_t dest_addr_len, int do_connect)
473 {
474 ✗ UDPContext *s = h->priv_data;
475
476 /* set the destination address */
477 ✗ if ((size_t)dest_addr_len > sizeof(s->dest_addr))
478 ✗ return AVERROR(EIO);
479 ✗ s->dest_addr_len = dest_addr_len;
480 ✗ memcpy(&s->dest_addr, dest_addr, dest_addr_len);
481
482 ✗ s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
483 ✗ if (do_connect >= 0) {
484 ✗ int was_connected = s->is_connected;
485 ✗ s->is_connected = do_connect;
486 ✗ if (s->is_connected && !was_connected) {
487 ✗ if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
488 ✗ s->dest_addr_len)) {
489 ✗ s->is_connected = 0;
490 ✗ ff_log_net_error(h, AV_LOG_ERROR, "connect");
491 ✗ return AVERROR(EIO);
492 }
493 }
494 }
495
496 ✗ return 0;
497 }
498
499 /**
500 * Return the local port used by the UDP connection
501 * @param h media file context
502 * @return the local port number
503 */
504 ✗ int ff_udp_get_local_port(URLContext *h)
505 {
506 ✗ UDPContext *s = h->priv_data;
507 ✗ return s->local_port;
508 }
509
510 ✗ void ff_udp_get_last_recv_addr(URLContext *h, struct sockaddr_storage *addr, socklen_t *addr_len)
511 {
512 ✗ UDPContext *s = h->priv_data;
513 ✗ *addr = s->last_recv_addr;
514 ✗ *addr_len = s->last_recv_addr_len;
515 ✗ }
516
517 /**
518 * Return the udp file handle for select() usage to wait for several RTP
519 * streams at the same time.
520 * @param h media file context
521 */
522 ✗ static int udp_get_file_handle(URLContext *h)
523 {
524 ✗ UDPContext *s = h->priv_data;
525 ✗ return s->udp_fd;
526 }
527
528 #if HAVE_THREADS
529 ✗ static void *circular_buffer_task_rx( void *_URLContext)
530 {
531 ✗ URLContext *h = _URLContext;
532 ✗ UDPContext *s = h->priv_data;
533
534 ✗ ff_thread_setname("udp-rx");
535
536 ✗ pthread_mutex_lock(&s->mutex);
537 ✗ while(1) {
538 UDPQueuedPacketHeader pkt_header;
539 ✗ int err = 0;
540
541 ✗ if (s->close_req)
542 ✗ goto end;
543
544 ✗ pkt_header.addr_len = sizeof(pkt_header.addr);
545
546 ✗ pthread_mutex_unlock(&s->mutex);
547 ✗ pkt_header.pkt_size = recvfrom(s->udp_fd, s->tmp + sizeof(pkt_header), sizeof(s->tmp) - sizeof(pkt_header), 0, (struct sockaddr *)&pkt_header.addr, &pkt_header.addr_len);
548 ✗ if (pkt_header.pkt_size < 0) {
549 ✗ err = ff_neterrno();
550 ✗ if (err == AVERROR(EAGAIN))
551 ✗ err = ff_network_wait_fd_wakeup(s->udp_fd, 0,
552 UDP_RX_WAIT_TIMEOUT_MS,
553 ✗ &s->wakeup);
554 }
555 ✗ pthread_mutex_lock(&s->mutex);
556 ✗ if (pkt_header.pkt_size < 0) {
557 // ff_network_wakeup_signal to exit
558 ✗ if (err == AVERROR_EXIT)
559 ✗ goto end;
560
561 ✗ if (err < 0 && err != AVERROR(EAGAIN) && err != AVERROR(EINTR)) {
562 ✗ s->circular_buffer_error = err;
563 ✗ goto end;
564 }
565 ✗ continue;
566 }
567 ✗ if (ff_ip_check_source_lists(&pkt_header.addr, &s->filters))
568 ✗ continue;
569 ✗ memcpy(s->tmp, &pkt_header, sizeof(pkt_header));
570
571 ✗ if (av_fifo_can_write(s->rx_fifo) < pkt_header.pkt_size + sizeof(pkt_header)) {
572 /* No Space left */
573 ✗ if (s->overrun_nonfatal) {
574 ✗ av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
575 "Surviving due to overrun_nonfatal option\n");
576 ✗ continue;
577 } else {
578 ✗ av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "
579 "To avoid, increase fifo_size URL option. "
580 "To survive in such case, use overrun_nonfatal option\n");
581 ✗ s->circular_buffer_error = AVERROR(EIO);
582 ✗ goto end;
583 }
584 }
585 ✗ av_fifo_write(s->rx_fifo, s->tmp, pkt_header.pkt_size + sizeof(pkt_header));
586 ✗ pthread_cond_signal(&s->cond);
587 }
588
589 ✗ end:
590 ✗ pthread_cond_signal(&s->cond);
591 ✗ pthread_mutex_unlock(&s->mutex);
592 ✗ return NULL;
593 }
594
595 ✗ static void *circular_buffer_task_tx( void *_URLContext)
596 {
597 ✗ URLContext *h = _URLContext;
598 ✗ UDPContext *s = h->priv_data;
599 ✗ int64_t target_timestamp = av_gettime_relative();
600 ✗ int64_t start_timestamp = av_gettime_relative();
601 ✗ int64_t sent_bits = 0;
602 ✗ int64_t burst_interval = s->bitrate ? (s->burst_bits * 1000000 / s->bitrate) : 0;
603 ✗ int64_t max_delay = s->bitrate ? ((int64_t)h->max_packet_size * 8 * 1000000 / s->bitrate + 1) : 0;
604
605 ✗ ff_thread_setname("udp-tx");
606
607 ✗ pthread_mutex_lock(&s->mutex);
608
609 ✗ if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
610 ✗ av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
611 ✗ s->circular_buffer_error = AVERROR(EIO);
612 ✗ goto end;
613 }
614
615 ✗ for(;;) {
616 int len;
617 const uint8_t *p;
618 uint8_t tmp[4];
619 int64_t timestamp;
620
621 ✗ len = av_fifo_can_read(s->tx_fifo);
622
623 ✗ while (len<4) {
624 ✗ if (s->close_req)
625 ✗ goto end;
626 ✗ pthread_cond_wait(&s->cond, &s->mutex);
627 ✗ len = av_fifo_can_read(s->tx_fifo);
628 }
629
630 ✗ av_fifo_read(s->tx_fifo, tmp, 4);
631 ✗ len = AV_RL32(tmp);
632
633 ✗ av_assert0(len >= 0);
634 ✗ av_assert0(len <= sizeof(s->tmp));
635
636 ✗ av_fifo_read(s->tx_fifo, s->tmp, len);
637
638 ✗ pthread_mutex_unlock(&s->mutex);
639
640 ✗ if (s->bitrate) {
641 ✗ timestamp = av_gettime_relative();
642 ✗ if (timestamp < target_timestamp) {
643 ✗ int64_t delay = target_timestamp - timestamp;
644 ✗ if (delay > max_delay) {
645 ✗ delay = max_delay;
646 ✗ start_timestamp = timestamp + delay;
647 ✗ sent_bits = 0;
648 }
649 ✗ av_usleep(delay);
650 } else {
651 ✗ if (timestamp - burst_interval > target_timestamp) {
652 ✗ start_timestamp = timestamp - burst_interval;
653 ✗ sent_bits = 0;
654 }
655 }
656 ✗ sent_bits += len * 8;
657 ✗ target_timestamp = start_timestamp + sent_bits * 1000000 / s->bitrate;
658 }
659
660 ✗ p = s->tmp;
661 ✗ while (len) {
662 int ret;
663 ✗ av_assert0(len > 0);
664 ✗ if (!s->is_connected) {
665 ✗ ret = sendto (s->udp_fd, p, len, 0,
666 ✗ (struct sockaddr *) &s->dest_addr,
667 ✗ s->dest_addr_len);
668 } else
669 ✗ ret = send(s->udp_fd, p, len, 0);
670 ✗ if (ret >= 0) {
671 ✗ len -= ret;
672 ✗ p += ret;
673 } else {
674 ✗ ret = ff_neterrno();
675 ✗ if (ret != AVERROR(EAGAIN) && ret != AVERROR(EINTR)) {
676 ✗ pthread_mutex_lock(&s->mutex);
677 ✗ s->circular_buffer_error = ret;
678 ✗ pthread_mutex_unlock(&s->mutex);
679 ✗ return NULL;
680 }
681 }
682 }
683
684 ✗ pthread_mutex_lock(&s->mutex);
685 }
686
687 ✗ end:
688 ✗ pthread_mutex_unlock(&s->mutex);
689 ✗ return NULL;
690 }
691
692
693 #endif
694
695 /* put it in UDP context */
696 /* return non zero if error */
697 ✗ static int udp_open(URLContext *h, const char *uri, int flags)
698 {
699 char hostname[1024];
700 ✗ int port, udp_fd = -1, tmp, bind_ret = -1;
701 ✗ UDPContext *s = h->priv_data;
702 int is_output;
703 const char *p;
704 struct sockaddr_storage my_addr;
705 socklen_t len;
706 int ret;
707 ✗ const char *bind_addr = NULL;
708
709 ✗ h->is_streamed = 1;
710
711 ✗ is_output = !(flags & AVIO_FLAG_READ);
712 ✗ if (s->buffer_size < 0)
713 ✗ s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_RX_BUF_SIZE;
714
715 ✗ p = strchr(uri, '?');
716 ✗ if (p) {
717 ✗ ret = ff_parse_opts_from_query_string(s, p, 1);
718 ✗ if (ret < 0)
719 ✗ goto fail;
720 }
721 if (!HAVE_THREADS) {
722 int64_t optvals[] = {s->overrun_nonfatal, s->bitrate, s->circular_buffer_size};
723 const char* optnames[] = { "overrun_nonfatal", "bitrate", "fifo_size"};
724 for (unsigned i = 0; i < FF_ARRAY_ELEMS(optvals); i++) {
725 if (optvals[i])
726 av_log(h, AV_LOG_WARNING,
727 "'%s' option was set but it is not supported "
728 "on this build (threading support is required)\n", optnames[i]);
729 }
730 }
731 ✗ if (s->sources) {
732 ✗ if ((ret = ff_ip_parse_sources(h, s->sources, &s->filters)) < 0)
733 ✗ goto fail;
734 }
735 ✗ if (s->block) {
736 ✗ if ((ret = ff_ip_parse_blocks(h, s->block, &s->filters)) < 0)
737 ✗ goto fail;
738 }
739
740 /* handling needed to support options picking from both AVOption and URL */
741 ✗ s->circular_buffer_size *= 188;
742 ✗ if (flags & AVIO_FLAG_WRITE) {
743 ✗ h->max_packet_size = s->pkt_size;
744 } else {
745 ✗ h->max_packet_size = UDP_MAX_PKT_SIZE;
746 }
747 ✗ h->rw_timeout = s->timeout;
748
749 /* fill the dest addr */
750 ✗ av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
751
752 /* XXX: fix av_url_split */
753 ✗ if (hostname[0] == '\0' || hostname[0] == '?') {
754 /* only accepts null hostname if input */
755 ✗ if (!(flags & AVIO_FLAG_READ)) {
756 ✗ ret = AVERROR(EINVAL);
757 ✗ goto fail;
758 }
759 } else {
760 ✗ if ((ret = ff_udp_set_remote_url(h, uri)) < 0)
761 ✗ goto fail;
762 }
763
764 ✗ if ((s->is_multicast || s->local_port < 0) && (h->flags & AVIO_FLAG_READ))
765 ✗ s->local_port = port;
766
767 ✗ if (!s->is_multicast && (s->localaddr == NULL || s->localaddr[0] == '\0') && h->flags == AVIO_FLAG_READ)
768 ✗ bind_addr = hostname;
769 else
770 ✗ bind_addr = s->localaddr;
771
772 ✗ udp_fd = udp_socket_create(h, &my_addr, &len, bind_addr);
773 ✗ if (udp_fd < 0) {
774 ✗ ret = AVERROR(EIO);
775 ✗ goto fail;
776 }
777
778 ✗ s->local_addr_storage=my_addr; //store for future multicast join
779
780 /* Follow the requested reuse option, unless it's multicast in which
781 * case enable reuse unless explicitly disabled.
782 */
783 ✗ if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
784 ✗ s->reuse_socket = 1;
785 ✗ if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0) {
786 ✗ ret = ff_neterrno();
787 ✗ goto fail;
788 }
789 }
790
791 ✗ if (s->is_broadcast) {
792 #ifdef SO_BROADCAST
793 ✗ if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0) {
794 ✗ ret = ff_neterrno();
795 ✗ goto fail;
796 }
797 #else
798 ret = AVERROR(ENOSYS);
799 goto fail;
800 #endif
801 }
802
803 /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
804 * The receiver coverage has to be less than or equal to the sender coverage.
805 * Otherwise, the receiver will drop all packets.
806 */
807 ✗ if (s->udplite_coverage) {
808 ✗ if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
809 ✗ av_log(h, AV_LOG_WARNING, "socket option UDPLITE_SEND_CSCOV not available");
810
811 ✗ if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
812 ✗ av_log(h, AV_LOG_WARNING, "socket option UDPLITE_RECV_CSCOV not available");
813 }
814
815 ✗ if (s->dscp >= 0) {
816 ✗ int dscp = s->dscp << 2;
817 ✗ if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0) {
818 ✗ ret = ff_neterrno();
819 ✗ goto fail;
820 }
821 }
822
823 /* If multicast, try binding the multicast address first, to avoid
824 * receiving UDP packets from other sources aimed at the same UDP
825 * port. This fails on windows. This makes sending to the same address
826 * using sendto() fail, so only do it if we're opened in read-only mode. */
827 ✗ if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
828 ✗ bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
829 }
830 /* bind to the local address if not multicast or if the multicast
831 * bind failed */
832 /* the bind is needed to give a port to the socket now */
833 ✗ if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
834 ✗ ff_log_net_error(h, AV_LOG_ERROR, "bind failed");
835 ✗ ret = ff_neterrno();
836 ✗ goto fail;
837 }
838
839 ✗ len = sizeof(my_addr);
840 ✗ getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
841 ✗ s->local_port = udp_port(&my_addr, len);
842
843 ✗ if (s->is_multicast) {
844 ✗ if (h->flags & AVIO_FLAG_WRITE) {
845 /* output */
846 ✗ if ((ret = udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr, h)) < 0)
847 ✗ goto fail;
848 }
849 ✗ if (h->flags & AVIO_FLAG_READ) {
850 /* input */
851 ✗ if (s->filters.nb_include_addrs) {
852 ✗ if ((ret = udp_set_multicast_sources(h, udp_fd,
853 ✗ (struct sockaddr *)&s->dest_addr,
854 s->dest_addr_len, &s->local_addr_storage,
855 s->filters.include_addrs,
856 s->filters.nb_include_addrs, 1)) < 0)
857 ✗ goto fail;
858 } else {
859 ✗ if ((ret = udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,
860 ✗ (struct sockaddr *)&s->local_addr_storage, h)) < 0)
861 ✗ goto fail;
862 }
863 ✗ if (s->filters.nb_exclude_addrs) {
864 ✗ if ((ret = udp_set_multicast_sources(h, udp_fd,
865 ✗ (struct sockaddr *)&s->dest_addr,
866 s->dest_addr_len, &s->local_addr_storage,
867 s->filters.exclude_addrs,
868 s->filters.nb_exclude_addrs, 0)) < 0)
869 ✗ goto fail;
870 }
871 }
872 }
873
874 ✗ if (is_output) {
875 /* limit the tx buf size to limit latency */
876 ✗ tmp = s->buffer_size;
877 ✗ if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
878 ✗ ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
879 ✗ ret = ff_neterrno();
880 ✗ goto fail;
881 }
882 } else {
883 /* set udp recv buffer size to the requested value (default UDP_RX_BUF_SIZE) */
884 ✗ tmp = s->buffer_size;
885 ✗ if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
886 ✗ ff_log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
887 }
888 ✗ len = sizeof(tmp);
889 ✗ if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
890 ✗ ff_log_net_error(h, AV_LOG_WARNING, "getsockopt(SO_RCVBUF)");
891 } else {
892 ✗ av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp);
893 ✗ if(tmp < s->buffer_size)
894 ✗ av_log(h, AV_LOG_WARNING, "attempted to set receive buffer to size %d but it only ended up set as %d\n", s->buffer_size, tmp);
895 }
896
897 /* make the socket non-blocking */
898 ✗ if (ff_socket_nonblock(udp_fd, 1) < 0) {
899 ✗ ret = ff_neterrno();
900 ✗ goto fail;
901 }
902 }
903 ✗ if (s->is_connected) {
904 ✗ if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
905 ✗ ff_log_net_error(h, AV_LOG_ERROR, "connect");
906 ✗ ret = ff_neterrno();
907 ✗ goto fail;
908 }
909 }
910
911 ✗ s->udp_fd = udp_fd;
912
913 #if HAVE_THREADS
914 /*
915 Create thread in case of:
916 1. Input and circular_buffer_size is set
917 2. Output and bitrate and circular_buffer_size is set
918 */
919
920 ✗ if (is_output && s->bitrate && !s->circular_buffer_size) {
921 /* Warn user in case of 'circular_buffer_size' is not set */
922 ✗ av_log(h, AV_LOG_WARNING,"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
923 }
924
925 ✗ if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
926 /* start the task going */
927 ✗ AVFifo *fifo = av_fifo_alloc2(s->circular_buffer_size, 1, 0);
928 ✗ if (!fifo) {
929 ✗ ret = AVERROR(ENOMEM);
930 ✗ goto fail;
931 }
932 ✗ if (is_output)
933 ✗ s->tx_fifo = fifo;
934 else
935 ✗ s->rx_fifo = fifo;
936 ✗ ret = pthread_mutex_init(&s->mutex, NULL);
937 ✗ if (ret != 0) {
938 ✗ av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
939 ✗ ret = AVERROR(ret);
940 ✗ goto fail;
941 }
942 ✗ ret = pthread_cond_init(&s->cond, NULL);
943 ✗ if (ret != 0) {
944 ✗ av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
945 ✗ ret = AVERROR(ret);
946 ✗ goto cond_fail;
947 }
948 ✗ if (!is_output) {
949 ✗ ret = ff_network_wakeup_open(&s->wakeup);
950 ✗ if (ret < 0) {
951 ✗ av_log(h, AV_LOG_ERROR, "Failed to create the wakeup channel\n");
952 ✗ goto thread_fail;
953 }
954 }
955 ✗ ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
956 ✗ if (ret != 0) {
957 ✗ av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
958 ✗ ret = AVERROR(ret);
959 ✗ goto create_fail;
960 }
961 ✗ s->thread_started = 1;
962 }
963 #endif
964
965 ✗ return 0;
966 #if HAVE_THREADS
967 ✗ create_fail:
968 ✗ if (!is_output)
969 ✗ ff_network_wakeup_close(&s->wakeup);
970 ✗ thread_fail:
971 ✗ pthread_cond_destroy(&s->cond);
972 ✗ cond_fail:
973 ✗ pthread_mutex_destroy(&s->mutex);
974 #endif
975 ✗ fail:
976 ✗ if (udp_fd >= 0)
977 ✗ closesocket(udp_fd);
978 ✗ av_fifo_freep2(&s->rx_fifo);
979 ✗ av_fifo_freep2(&s->tx_fifo);
980 ✗ ff_ip_reset_filters(&s->filters);
981 ✗ return ret;
982 }
983
984 ✗ static int udplite_open(URLContext *h, const char *uri, int flags)
985 {
986 ✗ UDPContext *s = h->priv_data;
987
988 // set default checksum coverage
989 ✗ s->udplite_coverage = UDP_HEADER_SIZE;
990
991 ✗ return udp_open(h, uri, flags);
992 }
993
994 ✗ static int udp_read(URLContext *h, uint8_t *buf, int size)
995 {
996 ✗ UDPContext *s = h->priv_data;
997 int ret;
998 #if HAVE_THREADS
999 ✗ int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
1000
1001 ✗ if (s->rx_fifo) {
1002 ✗ pthread_mutex_lock(&s->mutex);
1003 do {
1004 ✗ avail = av_fifo_can_read(s->rx_fifo);
1005 ✗ if (avail) { // >=size) {
1006 UDPQueuedPacketHeader header;
1007
1008 ✗ av_fifo_read(s->rx_fifo, &header, sizeof(header));
1009
1010 ✗ s->last_recv_addr = header.addr;
1011 ✗ s->last_recv_addr_len = header.addr_len;
1012
1013 ✗ avail = header.pkt_size;
1014 ✗ if(avail > size){
1015 ✗ av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
1016 ✗ avail = size;
1017 }
1018
1019 ✗ av_fifo_read(s->rx_fifo, buf, avail);
1020 ✗ av_fifo_drain2(s->rx_fifo, header.pkt_size - avail);
1021 ✗ pthread_mutex_unlock(&s->mutex);
1022 ✗ return avail;
1023 ✗ } else if(s->circular_buffer_error){
1024 ✗ int err = s->circular_buffer_error;
1025 ✗ pthread_mutex_unlock(&s->mutex);
1026 ✗ return err;
1027 ✗ } else if(nonblock) {
1028 ✗ pthread_mutex_unlock(&s->mutex);
1029 ✗ return AVERROR(EAGAIN);
1030 } else {
1031 /* FIXME: using the monotonic clock would be better,
1032 but it does not exist on all supported platforms. */
1033 ✗ int64_t t = av_gettime() + 100000;
1034 ✗ struct timespec tv = { .tv_sec = t / 1000000,
1035 ✗ .tv_nsec = (t % 1000000) * 1000 };
1036 ✗ int err = pthread_cond_timedwait(&s->cond, &s->mutex, &tv);
1037 ✗ if (err) {
1038 ✗ pthread_mutex_unlock(&s->mutex);
1039 ✗ return AVERROR(err == ETIMEDOUT ? EAGAIN : err);
1040 }
1041 ✗ nonblock = 1;
1042 }
1043 } while(1);
1044 }
1045 #endif
1046
1047 ✗ if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
1048 ✗ ret = ff_network_wait_fd(s->udp_fd, 0);
1049 ✗ if (ret < 0)
1050 ✗ return ret;
1051 }
1052 ✗ s->last_recv_addr_len = sizeof(s->last_recv_addr);
1053 ✗ ret = recvfrom(s->udp_fd, buf, size, 0, (struct sockaddr *)&s->last_recv_addr, &s->last_recv_addr_len);
1054 ✗ if (ret < 0)
1055 ✗ return ff_neterrno();
1056 ✗ if (ff_ip_check_source_lists(&s->last_recv_addr, &s->filters))
1057 ✗ return AVERROR(EINTR);
1058 ✗ return ret;
1059 }
1060
1061 ✗ static int udp_write(URLContext *h, const uint8_t *buf, int size)
1062 {
1063 ✗ UDPContext *s = h->priv_data;
1064 int ret;
1065
1066 #if HAVE_THREADS
1067 ✗ if (s->tx_fifo) {
1068 uint8_t tmp[4];
1069
1070 ✗ pthread_mutex_lock(&s->mutex);
1071
1072 /*
1073 Return error if last tx failed.
1074 Here we can't know on which packet error was, but it needs to know that error exists.
1075 */
1076 ✗ if (s->circular_buffer_error<0) {
1077 ✗ int err = s->circular_buffer_error;
1078 ✗ pthread_mutex_unlock(&s->mutex);
1079 ✗ return err;
1080 }
1081
1082 ✗ if (av_fifo_can_write(s->tx_fifo) < size + 4) {
1083 /* What about a partial packet tx ? */
1084 ✗ pthread_mutex_unlock(&s->mutex);
1085 ✗ return AVERROR(ENOMEM);
1086 }
1087 ✗ AV_WL32(tmp, size);
1088 ✗ av_fifo_write(s->tx_fifo, tmp, 4); /* size of packet */
1089 ✗ av_fifo_write(s->tx_fifo, buf, size); /* the data */
1090 ✗ pthread_cond_signal(&s->cond);
1091 ✗ pthread_mutex_unlock(&s->mutex);
1092 ✗ return size;
1093 }
1094 #endif
1095 ✗ if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
1096 ✗ ret = ff_network_wait_fd(s->udp_fd, 1);
1097 ✗ if (ret < 0)
1098 ✗ return ret;
1099 }
1100
1101 ✗ if (!s->is_connected) {
1102 ✗ ret = sendto (s->udp_fd, buf, size, 0,
1103 ✗ (struct sockaddr *) &s->dest_addr,
1104 ✗ s->dest_addr_len);
1105 } else
1106 ✗ ret = send(s->udp_fd, buf, size, 0);
1107
1108 ✗ return ret < 0 ? ff_neterrno() : ret;
1109 }
1110
1111 ✗ static int udp_close(URLContext *h)
1112 {
1113 ✗ UDPContext *s = h->priv_data;
1114
1115 #if HAVE_THREADS
1116 ✗ if (s->thread_started) {
1117 ✗ pthread_mutex_lock(&s->mutex);
1118 ✗ s->close_req = 1;
1119 ✗ pthread_cond_signal(&s->cond);
1120 ✗ pthread_mutex_unlock(&s->mutex);
1121 ✗ if (s->rx_fifo)
1122 ✗ ff_network_wakeup_signal(&s->wakeup);
1123 }
1124 #endif
1125
1126 ✗ if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
1127 ✗ udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,
1128 ✗ (struct sockaddr *)&s->local_addr_storage, h);
1129 #if HAVE_THREADS
1130 ✗ if (s->thread_started) {
1131 int ret;
1132 ✗ ret = pthread_join(s->circular_buffer_thread, NULL);
1133 ✗ if (ret != 0)
1134 ✗ av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
1135 ✗ if (s->rx_fifo)
1136 ✗ ff_network_wakeup_close(&s->wakeup);
1137 ✗ pthread_mutex_destroy(&s->mutex);
1138 ✗ pthread_cond_destroy(&s->cond);
1139 }
1140 #endif
1141 ✗ closesocket(s->udp_fd);
1142 ✗ av_fifo_freep2(&s->rx_fifo);
1143 ✗ av_fifo_freep2(&s->tx_fifo);
1144 ✗ ff_ip_reset_filters(&s->filters);
1145 ✗ return 0;
1146 }
1147
1148 const URLProtocol ff_udp_protocol = {
1149 .name = "udp",
1150 .url_open = udp_open,
1151 .url_read = udp_read,
1152 .url_write = udp_write,
1153 .url_close = udp_close,
1154 .url_get_file_handle = udp_get_file_handle,
1155 .priv_data_size = sizeof(UDPContext),
1156 .priv_data_class = &udp_class,
1157 .flags = URL_PROTOCOL_FLAG_NETWORK,
1158 };
1159
1160 const URLProtocol ff_udplite_protocol = {
1161 .name = "udplite",
1162 .url_open = udplite_open,
1163 .url_read = udp_read,
1164 .url_write = udp_write,
1165 .url_close = udp_close,
1166 .url_get_file_handle = udp_get_file_handle,
1167 .priv_data_size = sizeof(UDPContext),
1168 .priv_data_class = &udplite_context_class,
1169 .flags = URL_PROTOCOL_FLAG_NETWORK,
1170 };
1171