2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
27 #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
30 #include "avio_internal.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/fifo.h"
33 #include "libavutil/intreadwrite.h"
37 #include "os_support.h"
46 #ifndef IPV6_ADD_MEMBERSHIP
47 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
48 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
51 #define UDP_TX_BUF_SIZE 32768
52 #define UDP_MAX_PKT_SIZE 65536
61 struct sockaddr_storage dest_addr;
65 /* Circular Buffer variables for use in UDP receive code */
66 int circular_buffer_size;
68 int circular_buffer_error;
70 pthread_t circular_buffer_thread;
72 uint8_t tmp[UDP_MAX_PKT_SIZE+4];
76 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
77 struct sockaddr *addr)
79 #ifdef IP_MULTICAST_TTL
80 if (addr->sa_family == AF_INET) {
81 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
82 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
87 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
88 if (addr->sa_family == AF_INET6) {
89 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
90 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
98 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
100 #ifdef IP_ADD_MEMBERSHIP
101 if (addr->sa_family == AF_INET) {
104 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
105 mreq.imr_interface.s_addr= INADDR_ANY;
106 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
107 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
112 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
113 if (addr->sa_family == AF_INET6) {
114 struct ipv6_mreq mreq6;
116 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
117 mreq6.ipv6mr_interface= 0;
118 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
119 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
127 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
129 #ifdef IP_DROP_MEMBERSHIP
130 if (addr->sa_family == AF_INET) {
133 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
134 mreq.imr_interface.s_addr= INADDR_ANY;
135 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
136 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
141 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
142 if (addr->sa_family == AF_INET6) {
143 struct ipv6_mreq mreq6;
145 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
146 mreq6.ipv6mr_interface= 0;
147 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
148 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
156 static struct addrinfo* udp_resolve_host(const char *hostname, int port,
157 int type, int family, int flags)
159 struct addrinfo hints, *res = 0;
162 const char *node = 0, *service = "0";
165 snprintf(sport, sizeof(sport), "%d", port);
168 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
171 memset(&hints, 0, sizeof(hints));
172 hints.ai_socktype = type;
173 hints.ai_family = family;
174 hints.ai_flags = flags;
175 if ((error = getaddrinfo(node, service, &hints, &res))) {
177 av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
183 static int udp_set_url(struct sockaddr_storage *addr,
184 const char *hostname, int port)
186 struct addrinfo *res0;
189 res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
190 if (res0 == 0) return AVERROR(EIO);
191 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
192 addr_len = res0->ai_addrlen;
198 static int udp_socket_create(UDPContext *s,
199 struct sockaddr_storage *addr, int *addr_len)
202 struct addrinfo *res0 = NULL, *res = NULL;
203 int family = AF_UNSPEC;
205 if (((struct sockaddr *) &s->dest_addr)->sa_family)
206 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
207 res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
210 for (res = res0; res; res=res->ai_next) {
211 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
212 if (udp_fd > 0) break;
213 av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
219 memcpy(addr, res->ai_addr, res->ai_addrlen);
220 *addr_len = res->ai_addrlen;
234 static int udp_port(struct sockaddr_storage *addr, int addr_len)
236 char sbuf[sizeof(int)*3+1];
238 if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
239 av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
243 return strtol(sbuf, NULL, 10);
248 * If no filename is given to av_open_input_file because you want to
249 * get the local port first, then you must call this function to set
250 * the remote server address.
252 * url syntax: udp://host:port[?option=val...]
253 * option: 'ttl=n' : set the ttl value (for multicast only)
254 * 'localport=n' : set the local port
255 * 'pkt_size=n' : set max packet size
256 * 'reuse=1' : enable reusing the socket
258 * @param h media file context
259 * @param uri of the remote server
260 * @return zero if no error.
262 int ff_udp_set_remote_url(URLContext *h, const char *uri)
264 UDPContext *s = h->priv_data;
265 char hostname[256], buf[10];
269 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
271 /* set the destination address */
272 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
273 if (s->dest_addr_len < 0) {
276 s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
277 p = strchr(uri, '?');
279 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
280 int was_connected = s->is_connected;
281 s->is_connected = strtol(buf, NULL, 10);
282 if (s->is_connected && !was_connected) {
283 if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
286 av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
297 * Return the local port used by the UDP connection
298 * @param h media file context
299 * @return the local port number
301 int ff_udp_get_local_port(URLContext *h)
303 UDPContext *s = h->priv_data;
304 return s->local_port;
308 * Return the udp file handle for select() usage to wait for several RTP
309 * streams at the same time.
310 * @param h media file context
312 static int udp_get_file_handle(URLContext *h)
314 UDPContext *s = h->priv_data;
318 static void *circular_buffer_task( void *_URLContext)
320 URLContext *h = _URLContext;
321 UDPContext *s = h->priv_data;
330 if (url_interrupt_cb()) {
331 s->circular_buffer_error = EINTR;
336 FD_SET(s->udp_fd, &rfds);
339 ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
341 if (ff_neterrno() == AVERROR(EINTR))
343 s->circular_buffer_error = EIO;
347 if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
350 /* How much do we have left to the end of the buffer */
351 /* Whats the minimum we can read so that we dont comletely fill the buffer */
352 left = av_fifo_space(s->fifo);
354 /* No Space left, error, what do we do now */
355 if(left < UDP_MAX_PKT_SIZE + 4) {
356 av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
357 s->circular_buffer_error = EIO;
360 left = FFMIN(left, s->fifo->end - s->fifo->wptr);
361 len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
363 if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
364 s->circular_buffer_error = EIO;
369 AV_WL32(s->tmp, len);
370 av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
376 /* put it in UDP context */
377 /* return non zero if error */
378 static int udp_open(URLContext *h, const char *uri, int flags)
381 int port, udp_fd = -1, tmp, bind_ret = -1;
382 UDPContext *s = NULL;
386 struct sockaddr_storage my_addr;
388 int reuse_specified = 0;
391 h->max_packet_size = 1472;
393 is_output = !(flags & AVIO_FLAG_READ);
395 s = av_mallocz(sizeof(UDPContext));
397 return AVERROR(ENOMEM);
401 s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
403 s->circular_buffer_size = 7*188*4096;
405 p = strchr(uri, '?');
407 if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
409 s->reuse_socket = strtol(buf, &endptr, 10);
410 /* assume if no digits were found it is a request to enable it */
415 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
416 s->ttl = strtol(buf, NULL, 10);
418 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
419 s->local_port = strtol(buf, NULL, 10);
421 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
422 h->max_packet_size = strtol(buf, NULL, 10);
424 if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
425 s->buffer_size = strtol(buf, NULL, 10);
427 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
428 s->is_connected = strtol(buf, NULL, 10);
430 if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
431 s->circular_buffer_size = strtol(buf, NULL, 10)*188;
435 /* fill the dest addr */
436 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
438 /* XXX: fix av_url_split */
439 if (hostname[0] == '\0' || hostname[0] == '?') {
440 /* only accepts null hostname if input */
441 if (!(flags & AVIO_FLAG_READ))
444 if (ff_udp_set_remote_url(h, uri) < 0)
448 if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
449 s->local_port = port;
450 udp_fd = udp_socket_create(s, &my_addr, &len);
454 /* Follow the requested reuse option, unless it's multicast in which
455 * case enable reuse unless explicitely disabled.
457 if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
459 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
463 /* the bind is needed to give a port to the socket now */
464 /* if multicast, try the multicast address bind first */
465 if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
466 bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
468 /* bind to the local address if not multicast or if the multicast
470 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
473 len = sizeof(my_addr);
474 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
475 s->local_port = udp_port(&my_addr, len);
477 if (s->is_multicast) {
478 if (!(h->flags & AVIO_FLAG_READ)) {
480 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
484 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
490 /* limit the tx buf size to limit latency */
491 tmp = s->buffer_size;
492 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
493 av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
497 /* set udp recv buffer size to the largest possible udp packet size to
498 * avoid losing data on OSes that set this too low by default. */
499 tmp = s->buffer_size;
500 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
501 av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
503 /* make the socket non-blocking */
504 ff_socket_nonblock(udp_fd, 1);
506 if (s->is_connected) {
507 if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
508 av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
516 if (!is_output && s->circular_buffer_size) {
517 /* start the task going */
518 s->fifo = av_fifo_alloc(s->circular_buffer_size);
519 if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) {
520 av_log(h, AV_LOG_ERROR, "pthread_create failed\n");
530 av_fifo_free(s->fifo);
535 static int udp_read(URLContext *h, uint8_t *buf, int size)
537 UDPContext *s = h->priv_data;
546 avail = av_fifo_size(s->fifo);
547 if (avail) { // >=size) {
550 av_fifo_generic_read(s->fifo, tmp, 4, NULL);
553 av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
557 av_fifo_generic_read(s->fifo, buf, avail, NULL);
562 FD_SET(s->udp_fd, &rfds);
565 ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
572 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
573 ret = ff_network_wait_fd(s->udp_fd, 0);
577 ret = recv(s->udp_fd, buf, size, 0);
579 return ret < 0 ? ff_neterrno() : ret;
582 static int udp_write(URLContext *h, const uint8_t *buf, int size)
584 UDPContext *s = h->priv_data;
587 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
588 ret = ff_network_wait_fd(s->udp_fd, 1);
593 if (!s->is_connected) {
594 ret = sendto (s->udp_fd, buf, size, 0,
595 (struct sockaddr *) &s->dest_addr,
598 ret = send(s->udp_fd, buf, size, 0);
600 return ret < 0 ? ff_neterrno() : ret;
603 static int udp_close(URLContext *h)
605 UDPContext *s = h->priv_data;
607 if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
608 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
609 closesocket(s->udp_fd);
610 av_fifo_free(s->fifo);
615 URLProtocol ff_udp_protocol = {
617 .url_open = udp_open,
618 .url_read = udp_read,
619 .url_write = udp_write,
620 .url_close = udp_close,
621 .url_get_file_handle = udp_get_file_handle,