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
42
43 #if HAVE_UDPLITE_H
44 #include "udplite.h"
45 #else
46 /* On many Linux systems, udplite.h is missing but the kernel supports UDP-Lite.
47 * So, we provide a fallback here.
48 */
49 #define UDPLITE_SEND_CSCOV 10
50 #define UDPLITE_RECV_CSCOV 11
51 #endif
52
53 #ifndef IPPROTO_UDPLITE
54 #define IPPROTO_UDPLITE 136
55 #endif
56
57 #if HAVE_PTHREAD_CANCEL
58 #include <pthread.h>
59 #endif
60
61 #ifndef HAVE_PTHREAD_CANCEL
62 #define HAVE_PTHREAD_CANCEL 0
63 #endif
64
65 #ifndef IPV6_ADD_MEMBERSHIP
66 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
67 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
68 #endif
69
70 #define UDP_TX_BUF_SIZE 32768
71 #define UDP_MAX_PKT_SIZE 65536
72 #define UDP_HEADER_SIZE 8
73
88
89 /* Circular Buffer variables for use in UDP receive code */
93 #if HAVE_PTHREAD_CANCEL
97 int thread_started;
98 #endif
106
107 #define OFFSET(x) offsetof(UDPContext, x)
108 #define D AV_OPT_FLAG_DECODING_PARAM
109 #define E AV_OPT_FLAG_ENCODING_PARAM
111 {
"buffer_size",
"set packet buffer size in bytes",
OFFSET(buffer_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D|
E },
114 {
"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 },
115 {
"pkt_size",
"set size of UDP packets",
OFFSET(packet_size),
AV_OPT_TYPE_INT, {.i64 = 1472}, 0, INT_MAX,
D|
E },
116 {
"reuse",
"explicitly allow or disallow reusing UDP sockets",
OFFSET(reuse_socket),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
117 {
"broadcast",
"explicitly allow or disallow broadcast destination",
OFFSET(is_broadcast),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E },
118 {
"ttl",
"set the time to live value (for multicast only)",
OFFSET(ttl),
AV_OPT_TYPE_INT, {.i64 = 16}, 0, INT_MAX,
E },
119 {
"connect",
"set if connect() should be called on socket",
OFFSET(is_connected),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
120 /* TODO 'sources', 'block' option */
121 {
"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 },
122 {
"overrun_nonfatal",
"survive in case of UDP receiving circular buffer overrun",
OFFSET(overrun_nonfatal),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D },
123 {
"timeout",
"set raise error timeout (only in read mode)",
OFFSET(timeout),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
D },
125 };
126
132 };
133
139 };
140
142 {
143 char errbuf[100];
145 av_log(ctx, level,
"%s: %s\n", prefix, errbuf);
146 }
147
149 struct sockaddr *addr)
150 {
151 #ifdef IP_MULTICAST_TTL
152 if (addr->sa_family == AF_INET) {
153 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
155 return -1;
156 }
157 }
158 #endif
159 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
160 if (addr->sa_family == AF_INET6) {
161 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
163 return -1;
164 }
165 }
166 #endif
167 return 0;
168 }
169
171 {
172 #ifdef IP_ADD_MEMBERSHIP
173 if (addr->sa_family == AF_INET) {
174 struct ip_mreq mreq;
175
176 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
177 if (local_addr)
178 mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
179 else
180 mreq.imr_interface.s_addr= INADDR_ANY;
181 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
183 return -1;
184 }
185 }
186 #endif
187 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
188 if (addr->sa_family == AF_INET6) {
189 struct ipv6_mreq mreq6;
190
191 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
192 mreq6.ipv6mr_interface= 0;
195 return -1;
196 }
197 }
198 #endif
199 return 0;
200 }
201
203 {
204 #ifdef IP_DROP_MEMBERSHIP
205 if (addr->sa_family == AF_INET) {
206 struct ip_mreq mreq;
207
208 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
209 if (local_addr)
210 mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
211 else
212 mreq.imr_interface.s_addr= INADDR_ANY;
213 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
215 return -1;
216 }
217 }
218 #endif
219 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
220 if (addr->sa_family == AF_INET6) {
221 struct ipv6_mreq mreq6;
222
223 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
224 mreq6.ipv6mr_interface= 0;
227 return -1;
228 }
229 }
230 #endif
231 return 0;
232 }
233
236 {
237 struct addrinfo hints = { 0 }, *res = 0;
238 int error;
239 char sport[16];
240 const char *node = 0, *service = "0";
241
242 if (port > 0) {
243 snprintf(sport,
sizeof(sport),
"%d", port);
244 service = sport;
245 }
246 if ((hostname) && (hostname[0] != '0円') && (hostname[0] != '?')) {
247 node = hostname;
248 }
252 if ((error =
getaddrinfo(node, service, &hints, &res))) {
255 }
256
257 return res;
258 }
259
261 int addr_len, char **sources,
262 int nb_sources, int include)
263 {
264 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
265 /* These ones are available in the microsoft SDK, but don't seem to work
266 * as on linux, so just prefer the v4-only approach there for now. */
267 int i;
268 for (i = 0; i < nb_sources; i++) {
269 struct group_source_req mreqs;
270 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
272 SOCK_DGRAM, AF_UNSPEC,
273 0);
274 if (!sourceaddr)
276
277 mreqs.gsr_interface = 0;
278 memcpy(&mreqs.gsr_group, addr, addr_len);
281
282 if (setsockopt(sockfd, level,
283 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
284 (const void *)&mreqs, sizeof(mreqs)) < 0) {
285 if (include)
287 else
290 }
291 }
292 #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
293 int i;
294 if (addr->sa_family != AF_INET) {
296 "Setting multicast sources only supported for IPv4\n");
298 }
299 for (i = 0; i < nb_sources; i++) {
300 struct ip_mreq_source mreqs;
302 SOCK_DGRAM, AF_UNSPEC,
303 0);
304 if (!sourceaddr)
306 if (sourceaddr->
ai_addr->sa_family != AF_INET) {
309 sources[i]);
311 }
312
313 mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
314 mreqs.imr_interface.s_addr = INADDR_ANY;
315 mreqs.imr_sourceaddr.s_addr = ((
struct sockaddr_in *)sourceaddr->
ai_addr)->sin_addr.s_addr;
317
318 if (setsockopt(sockfd, IPPROTO_IP,
319 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
320 (const void *)&mreqs, sizeof(mreqs)) < 0) {
321 if (include)
323 else
326 }
327 }
328 #else
330 #endif
331 return 0;
332 }
334 const char *hostname, int port)
335 {
337 int addr_len;
338
340 if (!res0)
return AVERROR(EIO);
344
345 return addr_len;
346 }
347
349 socklen_t *addr_len, const char *localaddr)
350 {
351 int udp_fd = -1;
353 int family = AF_UNSPEC;
354
355 if (((
struct sockaddr *) &s->
dest_addr)->sa_family)
356 family = ((
struct sockaddr *) &s->
dest_addr)->sa_family;
359 if (!res0)
360 goto fail;
361 for (res = res0; res; res=res->
ai_next) {
364 else
366 if (udp_fd != -1) break;
368 }
369
370 if (udp_fd < 0)
371 goto fail;
372
375
377
378 return udp_fd;
379
380 fail:
381 if (udp_fd >= 0)
383 if(res0)
385 return -1;
386 }
387
389 {
390 char sbuf[sizeof(int)*3+1];
391 int error;
392
395 return -1;
396 }
397
398 return strtol(sbuf,
NULL, 10);
399 }
400
401
402 /**
403 * If no filename is given to av_open_input_file because you want to
404 * get the local port first, then you must call this function to set
405 * the remote server address.
406 *
407 * url syntax: udp://host:port[?option=val...]
408 * option: 'ttl=n' : set the ttl value (for multicast only)
409 * 'localport=n' : set the local port
410 * 'pkt_size=n' : set max packet size
411 * 'reuse=1' : enable reusing the socket
412 * 'overrun_nonfatal=1': survive in case of circular buffer overrun
413 *
414 * @param h media file context
415 * @param uri of the remote server
416 * @return zero if no error.
417 */
419 {
421 char hostname[256],
buf[10];
422 int port;
423 const char *p;
424
426
427 /* set the destination address */
431 }
433 p = strchr(uri, '?');
434 if (p) {
444 }
445 }
446 }
447 }
448
449 return 0;
450 }
451
452 /**
453 * Return the local port used by the UDP connection
454 * @param h media file context
455 * @return the local port number
456 */
458 {
461 }
462
463 /**
464 * Return the udp file handle for select() usage to wait for several RTP
465 * streams at the same time.
466 * @param h media file context
467 */
469 {
472 }
473
474 #if HAVE_PTHREAD_CANCEL
475 static void *circular_buffer_task( void *_URLContext)
476 {
479 int old_cancelstate;
480
481 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
487 }
488 while(1) {
490
492 /* Blocking operations are always cancellation points;
493 see "General Information" / "Thread Cancelation Overview"
494 in Single Unix. */
495 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
497 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
499 if (len < 0) {
503 }
504 continue;
505 }
507
509 /* No Space left */
512 "Surviving due to overrun_nonfatal option\n");
513 continue;
514 } else {
516 "To avoid, increase fifo_size URL option. "
517 "To survive in such case, use overrun_nonfatal option\n");
520 }
521 }
524 }
525
530 }
531 #endif
532
534 int max_sources)
535 {
536 char *source_start;
537
539 while (1) {
540 char *next = strchr(source_start, ',');
541 if (next)
542 *next = '0円';
543 sources[*num_sources] =
av_strdup(source_start);
544 if (!sources[*num_sources])
546 source_start = next + 1;
547 (*num_sources)++;
548 if (*num_sources >= max_sources || !next)
549 break;
550 }
551 return 0;
552 }
553
554 /* put it in UDP context */
555 /* return non zero if error */
557 {
558 char hostname[1024], localaddr[1024] = "";
559 int port, udp_fd = -1, tmp, bind_ret = -1, dscp = -1;
561 int is_output;
562 const char *p;
566 int reuse_specified = 0;
567 int i, num_include_sources = 0, num_exclude_sources = 0;
568 char *include_sources[32], *exclude_sources[32];
569
571
575
576 p = strchr(uri, '?');
577 if (p) {
581 /* assume if no digits were found it is a request to enable it */
582 if (buf == endptr)
584 reuse_specified = 1;
585 }
589 /* assume if no digits were found it is a request to enable it */
590 if (buf == endptr)
594 "'overrun_nonfatal' option was set but it is not supported "
595 "on this build (pthread support is required)\n");
596 }
598 s->
ttl = strtol(buf,
NULL, 10);
599 }
602 }
605 }
608 }
611 }
614 }
616 dscp = strtol(buf,
NULL, 10);
617 }
622 "'circular_buffer_size' option was set but it is not supported "
623 "on this build (pthread support is required)\n");
624 }
626 av_strlcpy(localaddr, buf,
sizeof(localaddr));
627 }
631 goto fail;
632 }
636 goto fail;
637 }
642 }
643 /* handling needed to support options picking from both AVOption and URL */
647 } else {
649 }
651
652 /* fill the dest addr */
654
655 /* XXX: fix av_url_split */
656 if (hostname[0] == '0円' || hostname[0] == '?') {
657 /* only accepts null hostname if input */
659 goto fail;
660 } else {
662 goto fail;
663 }
664
668 if (udp_fd < 0)
669 goto fail;
670
672
673 /* Follow the requested reuse option, unless it's multicast in which
674 * case enable reuse unless explicitly disabled.
675 */
679 goto fail;
680 }
681
683 #ifdef SO_BROADCAST
685 #endif
686 goto fail;
687 }
688
689 /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
690 * The receiver coverage has to be less than or equal to the sender coverage.
691 * Otherwise, the receiver will drop all packets.
692 */
696
699 }
700
701 if (dscp >= 0) {
702 dscp <<= 2;
703 if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
704 goto fail;
705 }
706
707 /* If multicast, try binding the multicast address first, to avoid
708 * receiving UDP packets from other sources aimed at the same UDP
709 * port. This fails on windows. This makes sending to the same address
710 * using sendto() fail, so only do it if we're opened in read-only mode. */
712 bind_ret = bind(udp_fd,(
struct sockaddr *)&s->
dest_addr, len);
713 }
714 /* bind to the local address if not multicast or if the multicast
715 * bind failed */
716 /* the bind is needed to give a port to the socket now */
717 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
719 goto fail;
720 }
721
722 len = sizeof(my_addr);
723 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
725
727 if (h->
flags & AVIO_FLAG_WRITE) {
728 /* output */
730 goto fail;
731 }
733 /* input */
734 if (num_include_sources && num_exclude_sources) {
735 av_log(h,
AV_LOG_ERROR,
"Simultaneously including and excluding multicast sources is not supported\n");
736 goto fail;
737 }
738 if (num_include_sources) {
740 goto fail;
741 } else {
743 goto fail;
744 }
745 if (num_exclude_sources) {
747 goto fail;
748 }
749 }
750 }
751
752 if (is_output) {
753 /* limit the tx buf size to limit latency */
755 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
757 goto fail;
758 }
759 } else {
760 /* set udp recv buffer size to the requested value (default 64K) */
762 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
764 }
765 len = sizeof(tmp);
766 if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
768 } else {
770 if(tmp < s->buffer_size)
772 }
773
774 /* make the socket non-blocking */
776 }
780 goto fail;
781 }
782 }
783
784 for (i = 0; i < num_include_sources; i++)
786 for (i = 0; i < num_exclude_sources; i++)
788
790
791 #if HAVE_PTHREAD_CANCEL
794
795 /* start the task going */
798 if (ret != 0) {
800 goto fail;
801 }
803 if (ret != 0) {
805 goto cond_fail;
806 }
808 if (ret != 0) {
810 goto thread_fail;
811 }
812 s->thread_started = 1;
813 }
814 #endif
815
816 return 0;
817 #if HAVE_PTHREAD_CANCEL
818 thread_fail:
820 cond_fail:
822 #endif
823 fail:
824 if (udp_fd >= 0)
827 for (i = 0; i < num_include_sources; i++)
829 for (i = 0; i < num_exclude_sources; i++)
832 }
833
835 {
837
838 // set default checksum coverage
840
842 }
843
845 {
848 #if HAVE_PTHREAD_CANCEL
850
853 do {
855 if (avail) { // >=size) {
857
860 if(avail > size){
863 }
864
868 return avail;
872 return err;
873 } else if(nonblock) {
876 }
877 else {
878 /* FIXME: using the monotonic clock would be better,
879 but it does not exist on all supported platforms. */
881 struct timespec tv = { .tv_sec = t / 1000000,
882 .tv_nsec = (t % 1000000) * 1000 };
883 if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
885 return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
886 }
887 nonblock = 1;
888 }
889 } while( 1);
890 }
891 #endif
892
895 if (ret < 0)
897 }
898 ret = recv(s->
udp_fd, buf, size, 0);
899
901 }
902
904 {
907
910 if (ret < 0)
912 }
913
915 ret = sendto (s->
udp_fd, buf, size, 0,
918 } else
919 ret = send(s->
udp_fd, buf, size, 0);
920
922 }
923
925 {
927
931 #if HAVE_PTHREAD_CANCEL
932 if (s->thread_started) {
934 pthread_cancel(s->circular_buffer_thread);
936 if (ret != 0)
940 }
941 #endif
943 return 0;
944 }
945
954 .priv_data_class = &udp_context_class,
956 };
957
966 .priv_data_class = &udplite_context_class,
968 };