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