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
38 struct sockaddr_in dest_addr;
40 struct sockaddr_storage dest_addr;
45 #define UDP_TX_BUF_SIZE 32768
46 #define UDP_MAX_PKT_SIZE 65536
48 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
49 #ifdef IP_MULTICAST_TTL
50 if (addr->sa_family == AF_INET) {
51 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
52 perror("setsockopt(IP_MULTICAST_TTL)");
58 if (addr->sa_family == AF_INET6) {
59 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
60 perror("setsockopt(IPV6_MULTICAST_HOPS)");
68 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) {
69 #ifdef IP_ADD_MEMBERSHIP
70 if (addr->sa_family == AF_INET) {
73 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
74 mreq.imr_interface.s_addr= INADDR_ANY;
75 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
76 perror("setsockopt(IP_ADD_MEMBERSHIP)");
82 if (addr->sa_family == AF_INET6) {
83 struct ipv6_mreq mreq6;
85 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
86 mreq6.ipv6mr_interface= 0;
87 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
88 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
96 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) {
97 #ifdef IP_DROP_MEMBERSHIP
98 if (addr->sa_family == AF_INET) {
101 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
102 mreq.imr_interface.s_addr= INADDR_ANY;
103 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
104 perror("setsockopt(IP_DROP_MEMBERSHIP)");
110 if (addr->sa_family == AF_INET6) {
111 struct ipv6_mreq mreq6;
113 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
114 mreq6.ipv6mr_interface= 0;
115 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
116 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
125 static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
126 struct addrinfo hints, *res = 0;
129 const char *node = 0, *service = "0";
132 snprintf(sport, sizeof(sport), "%d", port);
135 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
138 memset(&hints, 0, sizeof(hints));
139 hints.ai_socktype = type;
140 hints.ai_family = family;
141 hints.ai_flags = flags;
142 if ((error = getaddrinfo(node, service, &hints, &res))) {
143 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
149 static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
150 UDPContext *s = h->priv_data;
153 struct addrinfo *res0;
154 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
155 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
156 if (res0 == 0) return AVERROR(EIO);
157 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
158 s->dest_addr_len = res0->ai_addrlen;
163 static int udp_ipv6_set_local(URLContext *h) {
164 UDPContext *s = h->priv_data;
166 struct sockaddr_storage clientaddr;
168 char sbuf[NI_MAXSERV];
169 char hbuf[NI_MAXHOST];
170 struct addrinfo *res0 = NULL, *res = NULL;
171 int family = AF_UNSPEC;
173 if (((struct sockaddr *) &s->dest_addr)->sa_family)
174 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
175 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
178 for (res = res0; res; res=res->ai_next) {
179 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
180 if (udp_fd > 0) break;
187 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
194 addrlen = sizeof(clientaddr);
195 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
196 perror("getsockname");
200 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
201 perror("getnameinfo");
205 s->local_port = strtol(sbuf, NULL, 10);
217 #endif /* CONFIG_IPV6 */
221 * If no filename is given to av_open_input_file because you want to
222 * get the local port first, then you must call this function to set
223 * the remote server address.
225 * url syntax: udp://host:port[?option=val...]
226 * option: 'multicast=1' : enable multicast
227 * 'ttl=n' : set the ttl value (for multicast only)
228 * 'localport=n' : set the local port
229 * 'pkt_size=n' : set max packet size
230 * 'reuse=1' : enable reusing the socket
232 * @param s1 media file context
233 * @param uri of the remote server
234 * @return zero if no error.
236 int udp_set_remote_url(URLContext *h, const char *uri)
239 return udp_ipv6_set_remote_url(h, uri);
241 UDPContext *s = h->priv_data;
245 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
247 /* set the destination address */
248 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
250 s->dest_addr.sin_family = AF_INET;
251 s->dest_addr.sin_port = htons(port);
252 s->dest_addr_len = sizeof(s->dest_addr);
258 * Return the local port used by the UDP connexion
259 * @param s1 media file context
260 * @return the local port number
262 int udp_get_local_port(URLContext *h)
264 UDPContext *s = h->priv_data;
265 return s->local_port;
269 * Return the udp file handle for select() usage to wait for several RTP
270 * streams at the same time.
271 * @param h media file context
273 int udp_get_file_handle(URLContext *h)
275 UDPContext *s = h->priv_data;
279 /* put it in UDP context */
280 /* return non zero if error */
281 static int udp_open(URLContext *h, const char *uri, int flags)
284 int port, udp_fd = -1, tmp;
285 UDPContext *s = NULL;
290 struct sockaddr_in my_addr, my_addr1;
295 h->max_packet_size = 1472;
297 is_output = (flags & URL_WRONLY);
299 s = av_mallocz(sizeof(UDPContext));
301 return AVERROR(ENOMEM);
308 p = strchr(uri, '?');
310 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
311 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
312 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
313 s->ttl = strtol(buf, NULL, 10);
315 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
316 s->local_port = strtol(buf, NULL, 10);
318 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
319 h->max_packet_size = strtol(buf, NULL, 10);
323 /* fill the dest addr */
324 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
326 /* XXX: fix url_split */
327 if (hostname[0] == '\0' || hostname[0] == '?') {
328 /* only accepts null hostname if input */
329 if (s->is_multicast || (flags & URL_WRONLY))
332 udp_set_remote_url(h, uri);
335 if(!ff_network_init())
339 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
343 my_addr.sin_family = AF_INET;
344 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
345 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
346 /* special case: the bind must be done on the multicast address port */
347 my_addr.sin_port = s->dest_addr.sin_port;
349 my_addr.sin_port = htons(s->local_port);
353 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
356 /* the bind is needed to give a port to the socket now */
357 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
360 len = sizeof(my_addr1);
361 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
362 s->local_port = ntohs(my_addr1.sin_port);
364 if (s->is_multicast && !(h->flags & URL_WRONLY))
365 s->local_port = port;
366 udp_fd = udp_ipv6_set_local(h);
369 #endif /* CONFIG_IPV6 */
370 if (s->is_multicast) {
371 if (h->flags & URL_WRONLY) {
373 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
377 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
383 /* limit the tx buf size to limit latency */
384 tmp = UDP_TX_BUF_SIZE;
385 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
386 perror("setsockopt sndbuf");
390 /* set udp recv buffer size to the largest possible udp packet size to
391 * avoid losing data on OSes that set this too low by default. */
392 tmp = UDP_MAX_PKT_SIZE;
393 setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
405 static int udp_read(URLContext *h, uint8_t *buf, int size)
407 UDPContext *s = h->priv_data;
409 struct sockaddr_in from;
411 struct sockaddr_storage from;
417 from_len = sizeof(from);
418 len = recvfrom (s->udp_fd, buf, size, 0,
419 (struct sockaddr *)&from, &from_len);
421 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
422 ff_neterrno() != FF_NETERROR(EINTR))
431 static int udp_write(URLContext *h, uint8_t *buf, int size)
433 UDPContext *s = h->priv_data;
437 ret = sendto (s->udp_fd, buf, size, 0,
438 (struct sockaddr *) &s->dest_addr,
441 if (ff_neterrno() != FF_NETERROR(EINTR) &&
442 ff_neterrno() != FF_NETERROR(EAGAIN))
451 static int udp_close(URLContext *h)
453 UDPContext *s = h->priv_data;
455 if (s->is_multicast && !(h->flags & URL_WRONLY))
456 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
457 closesocket(s->udp_fd);
463 URLProtocol udp_protocol = {