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 */
28 #define _DARWIN_C_SOURCE /* Needed for using IP_MULTICAST_TTL on OS X */
33 #include "os_support.h"
35 #include <sys/select.h>
39 #ifndef IPV6_ADD_MEMBERSHIP
40 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
41 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
51 struct sockaddr_storage dest_addr;
56 #define UDP_TX_BUF_SIZE 32768
57 #define UDP_MAX_PKT_SIZE 65536
59 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
60 struct sockaddr *addr)
62 #ifdef IP_MULTICAST_TTL
63 if (addr->sa_family == AF_INET) {
64 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
65 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
70 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
71 if (addr->sa_family == AF_INET6) {
72 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
73 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
81 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
83 #ifdef IP_ADD_MEMBERSHIP
84 if (addr->sa_family == AF_INET) {
87 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
88 mreq.imr_interface.s_addr= INADDR_ANY;
89 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
90 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
95 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
96 if (addr->sa_family == AF_INET6) {
97 struct ipv6_mreq mreq6;
99 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
100 mreq6.ipv6mr_interface= 0;
101 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
102 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
110 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
112 #ifdef IP_DROP_MEMBERSHIP
113 if (addr->sa_family == AF_INET) {
116 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
117 mreq.imr_interface.s_addr= INADDR_ANY;
118 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
119 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
124 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
125 if (addr->sa_family == AF_INET6) {
126 struct ipv6_mreq mreq6;
128 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
129 mreq6.ipv6mr_interface= 0;
130 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
131 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
139 static struct addrinfo* udp_resolve_host(const char *hostname, int port,
140 int type, int family, int flags)
142 struct addrinfo hints, *res = 0;
145 const char *node = 0, *service = "0";
148 snprintf(sport, sizeof(sport), "%d", port);
151 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
154 memset(&hints, 0, sizeof(hints));
155 hints.ai_socktype = type;
156 hints.ai_family = family;
157 hints.ai_flags = flags;
158 if ((error = getaddrinfo(node, service, &hints, &res))) {
160 av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
166 static int udp_set_url(struct sockaddr_storage *addr,
167 const char *hostname, int port)
169 struct addrinfo *res0;
172 res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
173 if (res0 == 0) return AVERROR(EIO);
174 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
175 addr_len = res0->ai_addrlen;
181 static int udp_socket_create(UDPContext *s,
182 struct sockaddr_storage *addr, int *addr_len)
185 struct addrinfo *res0 = NULL, *res = NULL;
186 int family = AF_UNSPEC;
188 if (((struct sockaddr *) &s->dest_addr)->sa_family)
189 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
190 res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
193 for (res = res0; res; res=res->ai_next) {
194 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
195 if (udp_fd > 0) break;
196 av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
202 memcpy(addr, res->ai_addr, res->ai_addrlen);
203 *addr_len = res->ai_addrlen;
217 static int udp_port(struct sockaddr_storage *addr, int addr_len)
219 char sbuf[sizeof(int)*3+1];
221 if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
222 av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
226 return strtol(sbuf, NULL, 10);
231 * If no filename is given to av_open_input_file because you want to
232 * get the local port first, then you must call this function to set
233 * the remote server address.
235 * url syntax: udp://host:port[?option=val...]
236 * option: 'ttl=n' : set the ttl value (for multicast only)
237 * 'localport=n' : set the local port
238 * 'pkt_size=n' : set max packet size
239 * 'reuse=1' : enable reusing the socket
241 * @param h media file context
242 * @param uri of the remote server
243 * @return zero if no error.
245 int udp_set_remote_url(URLContext *h, const char *uri)
247 UDPContext *s = h->priv_data;
251 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
253 /* set the destination address */
254 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
255 if (s->dest_addr_len < 0) {
258 s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
264 * Return the local port used by the UDP connection
265 * @param h media file context
266 * @return the local port number
268 int udp_get_local_port(URLContext *h)
270 UDPContext *s = h->priv_data;
271 return s->local_port;
275 * Return the udp file handle for select() usage to wait for several RTP
276 * streams at the same time.
277 * @param h media file context
279 #if !FF_API_UDP_GET_FILE
282 int udp_get_file_handle(URLContext *h)
284 UDPContext *s = h->priv_data;
288 /* put it in UDP context */
289 /* return non zero if error */
290 static int udp_open(URLContext *h, const char *uri, int flags)
293 int port, udp_fd = -1, tmp, bind_ret = -1;
294 UDPContext *s = NULL;
298 struct sockaddr_storage my_addr;
302 h->max_packet_size = 1472;
304 is_output = (flags & URL_WRONLY);
306 s = av_mallocz(sizeof(UDPContext));
308 return AVERROR(ENOMEM);
312 s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
314 p = strchr(uri, '?');
316 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
317 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
318 s->ttl = strtol(buf, NULL, 10);
320 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
321 s->local_port = strtol(buf, NULL, 10);
323 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
324 h->max_packet_size = strtol(buf, NULL, 10);
326 if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
327 s->buffer_size = strtol(buf, NULL, 10);
329 if (find_info_tag(buf, sizeof(buf), "connect", p)) {
330 s->is_connected = strtol(buf, NULL, 10);
334 /* fill the dest addr */
335 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
337 /* XXX: fix av_url_split */
338 if (hostname[0] == '\0' || hostname[0] == '?') {
339 /* only accepts null hostname if input */
340 if (flags & URL_WRONLY)
343 if (udp_set_remote_url(h, uri) < 0)
347 if (s->is_multicast && !(h->flags & URL_WRONLY))
348 s->local_port = port;
349 udp_fd = udp_socket_create(s, &my_addr, &len);
354 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
357 /* the bind is needed to give a port to the socket now */
358 /* if multicast, try the multicast address bind first */
359 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
360 bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
362 /* bind to the local address if not multicast or if the multicast
364 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
367 len = sizeof(my_addr);
368 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
369 s->local_port = udp_port(&my_addr, len);
371 if (s->is_multicast) {
372 if (h->flags & URL_WRONLY) {
374 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
378 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
384 /* limit the tx buf size to limit latency */
385 tmp = s->buffer_size;
386 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
387 av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
391 /* set udp recv buffer size to the largest possible udp packet size to
392 * avoid losing data on OSes that set this too low by default. */
393 tmp = s->buffer_size;
394 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
395 av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
397 /* make the socket non-blocking */
398 ff_socket_nonblock(udp_fd, 1);
400 if (s->is_connected) {
401 if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
402 av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
416 static int udp_read(URLContext *h, uint8_t *buf, int size)
418 UDPContext *s = h->priv_data;
425 if (url_interrupt_cb())
426 return AVERROR(EINTR);
428 FD_SET(s->udp_fd, &rfds);
430 tv.tv_usec = 100 * 1000;
431 ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
433 if (ff_neterrno() == FF_NETERROR(EINTR))
437 if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
439 len = recv(s->udp_fd, buf, size, 0);
441 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
442 ff_neterrno() != FF_NETERROR(EINTR))
451 static int udp_write(URLContext *h, const uint8_t *buf, int size)
453 UDPContext *s = h->priv_data;
457 if (!s->is_connected) {
458 ret = sendto (s->udp_fd, buf, size, 0,
459 (struct sockaddr *) &s->dest_addr,
462 ret = send(s->udp_fd, buf, size, 0);
464 if (ff_neterrno() != FF_NETERROR(EINTR) &&
465 ff_neterrno() != FF_NETERROR(EAGAIN))
466 return ff_neterrno();
474 static int udp_close(URLContext *h)
476 UDPContext *s = h->priv_data;
478 if (s->is_multicast && !(h->flags & URL_WRONLY))
479 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
480 closesocket(s->udp_fd);
485 URLProtocol udp_protocol = {
492 .url_get_file_handle = udp_get_file_handle,