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
25 #ifndef IPV6_ADD_MEMBERSHIP
26 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
27 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
37 struct sockaddr_in dest_addr;
39 struct sockaddr_storage dest_addr;
44 #define UDP_TX_BUF_SIZE 32768
45 #define UDP_MAX_PKT_SIZE 65536
47 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
48 #ifdef IP_MULTICAST_TTL
49 if (addr->sa_family == AF_INET) {
50 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
51 perror("setsockopt(IP_MULTICAST_TTL)");
57 if (addr->sa_family == AF_INET6) {
58 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
59 perror("setsockopt(IPV6_MULTICAST_HOPS)");
67 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) {
68 #ifdef IP_ADD_MEMBERSHIP
69 if (addr->sa_family == AF_INET) {
72 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
73 mreq.imr_interface.s_addr= INADDR_ANY;
74 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
75 perror("setsockopt(IP_ADD_MEMBERSHIP)");
81 if (addr->sa_family == AF_INET6) {
82 struct ipv6_mreq mreq6;
84 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
85 mreq6.ipv6mr_interface= 0;
86 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
87 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
95 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) {
96 #ifdef IP_DROP_MEMBERSHIP
97 if (addr->sa_family == AF_INET) {
100 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
101 mreq.imr_interface.s_addr= INADDR_ANY;
102 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
103 perror("setsockopt(IP_DROP_MEMBERSHIP)");
109 if (addr->sa_family == AF_INET6) {
110 struct ipv6_mreq mreq6;
112 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
113 mreq6.ipv6mr_interface= 0;
114 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
115 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
124 static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
125 struct addrinfo hints, *res = 0;
128 const char *node = 0, *service = "0";
131 snprintf(sport, sizeof(sport), "%d", port);
134 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
137 memset(&hints, 0, sizeof(hints));
138 hints.ai_socktype = type;
139 hints.ai_family = family;
140 hints.ai_flags = flags;
141 if ((error = getaddrinfo(node, service, &hints, &res))) {
142 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
148 static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
149 UDPContext *s = h->priv_data;
152 struct addrinfo *res0;
153 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
154 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
155 if (res0 == 0) return AVERROR(EIO);
156 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
157 s->dest_addr_len = res0->ai_addrlen;
162 static int udp_ipv6_set_local(URLContext *h) {
163 UDPContext *s = h->priv_data;
165 struct sockaddr_storage clientaddr;
167 char sbuf[NI_MAXSERV];
168 char hbuf[NI_MAXHOST];
169 struct addrinfo *res0 = NULL, *res = NULL;
170 int family = AF_UNSPEC;
172 if (((struct sockaddr *) &s->dest_addr)->sa_family)
173 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
174 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
177 for (res = res0; res; res=res->ai_next) {
178 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
179 if (udp_fd > 0) break;
186 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
193 addrlen = sizeof(clientaddr);
194 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
195 perror("getsockname");
199 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
200 perror("getnameinfo");
204 s->local_port = strtol(sbuf, NULL, 10);
216 #endif /* CONFIG_IPV6 */
220 * If no filename is given to av_open_input_file because you want to
221 * get the local port first, then you must call this function to set
222 * the remote server address.
224 * url syntax: udp://host:port[?option=val...]
225 * option: 'multicast=1' : enable multicast
226 * 'ttl=n' : set the ttl value (for multicast only)
227 * 'localport=n' : set the local port
228 * 'pkt_size=n' : set max packet size
229 * 'reuse=1' : enable reusing the socket
231 * @param s1 media file context
232 * @param uri of the remote server
233 * @return zero if no error.
235 int udp_set_remote_url(URLContext *h, const char *uri)
238 return udp_ipv6_set_remote_url(h, uri);
240 UDPContext *s = h->priv_data;
244 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
246 /* set the destination address */
247 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
249 s->dest_addr.sin_family = AF_INET;
250 s->dest_addr.sin_port = htons(port);
251 s->dest_addr_len = sizeof(s->dest_addr);
257 * Return the local port used by the UDP connexion
258 * @param s1 media file context
259 * @return the local port number
261 int udp_get_local_port(URLContext *h)
263 UDPContext *s = h->priv_data;
264 return s->local_port;
268 * Return the udp file handle for select() usage to wait for several RTP
269 * streams at the same time.
270 * @param h media file context
272 int udp_get_file_handle(URLContext *h)
274 UDPContext *s = h->priv_data;
278 /* put it in UDP context */
279 /* return non zero if error */
280 static int udp_open(URLContext *h, const char *uri, int flags)
283 int port, udp_fd = -1, tmp;
284 UDPContext *s = NULL;
289 struct sockaddr_in my_addr, my_addr1;
294 h->max_packet_size = 1472;
296 is_output = (flags & URL_WRONLY);
298 s = av_mallocz(sizeof(UDPContext));
300 return AVERROR(ENOMEM);
307 p = strchr(uri, '?');
309 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
310 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
311 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
312 s->ttl = strtol(buf, NULL, 10);
314 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
315 s->local_port = strtol(buf, NULL, 10);
317 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
318 h->max_packet_size = strtol(buf, NULL, 10);
322 /* fill the dest addr */
323 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
325 /* XXX: fix url_split */
326 if (hostname[0] == '\0' || hostname[0] == '?') {
327 /* only accepts null hostname if input */
328 if (s->is_multicast || (flags & URL_WRONLY))
331 udp_set_remote_url(h, uri);
334 if(!ff_network_init())
338 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
342 my_addr.sin_family = AF_INET;
343 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
344 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
345 /* special case: the bind must be done on the multicast address port */
346 my_addr.sin_port = s->dest_addr.sin_port;
348 my_addr.sin_port = htons(s->local_port);
352 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
355 /* the bind is needed to give a port to the socket now */
356 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
359 len = sizeof(my_addr1);
360 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
361 s->local_port = ntohs(my_addr1.sin_port);
363 if (s->is_multicast && !(h->flags & URL_WRONLY))
364 s->local_port = port;
365 udp_fd = udp_ipv6_set_local(h);
368 #endif /* CONFIG_IPV6 */
369 if (s->is_multicast) {
370 if (h->flags & URL_WRONLY) {
372 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
376 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
382 /* limit the tx buf size to limit latency */
383 tmp = UDP_TX_BUF_SIZE;
384 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
385 perror("setsockopt sndbuf");
389 /* set udp recv buffer size to the largest possible udp packet size to
390 * avoid losing data on OSes that set this too low by default. */
391 tmp = UDP_MAX_PKT_SIZE;
392 setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
404 static int udp_read(URLContext *h, uint8_t *buf, int size)
406 UDPContext *s = h->priv_data;
410 len = recv(s->udp_fd, buf, size, 0);
412 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
413 ff_neterrno() != FF_NETERROR(EINTR))
422 static int udp_write(URLContext *h, uint8_t *buf, int size)
424 UDPContext *s = h->priv_data;
428 ret = sendto (s->udp_fd, buf, size, 0,
429 (struct sockaddr *) &s->dest_addr,
432 if (ff_neterrno() != FF_NETERROR(EINTR) &&
433 ff_neterrno() != FF_NETERROR(EAGAIN))
442 static int udp_close(URLContext *h)
444 UDPContext *s = h->priv_data;
446 if (s->is_multicast && !(h->flags & URL_WRONLY))
447 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
448 closesocket(s->udp_fd);
454 URLProtocol udp_protocol = {