00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define _BSD_SOURCE
00028
00029 #include "avformat.h"
00030 #include "avio_internal.h"
00031 #include "libavutil/parseutils.h"
00032 #include "libavutil/fifo.h"
00033 #include "libavutil/intreadwrite.h"
00034 #include "libavutil/avstring.h"
00035 #include <unistd.h>
00036 #include "internal.h"
00037 #include "network.h"
00038 #include "os_support.h"
00039 #include "url.h"
00040
00041 #if HAVE_PTHREADS
00042 #include <pthread.h>
00043 #endif
00044
00045 #include <sys/time.h>
00046
00047 #ifndef IPV6_ADD_MEMBERSHIP
00048 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
00049 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
00050 #endif
00051
00052 #define UDP_TX_BUF_SIZE 32768
00053 #define UDP_MAX_PKT_SIZE 65536
00054
00055 typedef struct {
00056 int udp_fd;
00057 int ttl;
00058 int buffer_size;
00059 int is_multicast;
00060 int local_port;
00061 int reuse_socket;
00062 struct sockaddr_storage dest_addr;
00063 int dest_addr_len;
00064 int is_connected;
00065
00066
00067 int circular_buffer_size;
00068 AVFifoBuffer *fifo;
00069 int circular_buffer_error;
00070 #if HAVE_PTHREADS
00071 pthread_t circular_buffer_thread;
00072 pthread_mutex_t mutex;
00073 pthread_cond_t cond;
00074 int thread_started;
00075 volatile int exit_thread;
00076 #endif
00077 uint8_t tmp[UDP_MAX_PKT_SIZE+4];
00078 int remaining_in_dg;
00079 } UDPContext;
00080
00081 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
00082 struct sockaddr *addr)
00083 {
00084 #ifdef IP_MULTICAST_TTL
00085 if (addr->sa_family == AF_INET) {
00086 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
00087 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
00088 return -1;
00089 }
00090 }
00091 #endif
00092 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
00093 if (addr->sa_family == AF_INET6) {
00094 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
00095 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
00096 return -1;
00097 }
00098 }
00099 #endif
00100 return 0;
00101 }
00102
00103 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
00104 {
00105 #ifdef IP_ADD_MEMBERSHIP
00106 if (addr->sa_family == AF_INET) {
00107 struct ip_mreq mreq;
00108
00109 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
00110 mreq.imr_interface.s_addr= INADDR_ANY;
00111 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
00112 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
00113 return -1;
00114 }
00115 }
00116 #endif
00117 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
00118 if (addr->sa_family == AF_INET6) {
00119 struct ipv6_mreq mreq6;
00120
00121 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
00122 mreq6.ipv6mr_interface= 0;
00123 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
00124 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
00125 return -1;
00126 }
00127 }
00128 #endif
00129 return 0;
00130 }
00131
00132 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
00133 {
00134 #ifdef IP_DROP_MEMBERSHIP
00135 if (addr->sa_family == AF_INET) {
00136 struct ip_mreq mreq;
00137
00138 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
00139 mreq.imr_interface.s_addr= INADDR_ANY;
00140 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
00141 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
00142 return -1;
00143 }
00144 }
00145 #endif
00146 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
00147 if (addr->sa_family == AF_INET6) {
00148 struct ipv6_mreq mreq6;
00149
00150 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
00151 mreq6.ipv6mr_interface= 0;
00152 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
00153 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
00154 return -1;
00155 }
00156 }
00157 #endif
00158 return 0;
00159 }
00160
00161 static struct addrinfo* udp_resolve_host(const char *hostname, int port,
00162 int type, int family, int flags)
00163 {
00164 struct addrinfo hints, *res = 0;
00165 int error;
00166 char sport[16];
00167 const char *node = 0, *service = "0";
00168
00169 if (port > 0) {
00170 snprintf(sport, sizeof(sport), "%d", port);
00171 service = sport;
00172 }
00173 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
00174 node = hostname;
00175 }
00176 memset(&hints, 0, sizeof(hints));
00177 hints.ai_socktype = type;
00178 hints.ai_family = family;
00179 hints.ai_flags = flags;
00180 if ((error = getaddrinfo(node, service, &hints, &res))) {
00181 res = NULL;
00182 av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
00183 }
00184
00185 return res;
00186 }
00187
00188 static int udp_set_url(struct sockaddr_storage *addr,
00189 const char *hostname, int port)
00190 {
00191 struct addrinfo *res0;
00192 int addr_len;
00193
00194 res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
00195 if (res0 == 0) return AVERROR(EIO);
00196 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
00197 addr_len = res0->ai_addrlen;
00198 freeaddrinfo(res0);
00199
00200 return addr_len;
00201 }
00202
00203 static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr,
00204 int *addr_len, const char *localaddr)
00205 {
00206 int udp_fd = -1;
00207 struct addrinfo *res0 = NULL, *res = NULL;
00208 int family = AF_UNSPEC;
00209
00210 if (((struct sockaddr *) &s->dest_addr)->sa_family)
00211 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
00212 res0 = udp_resolve_host(localaddr[0] ? localaddr : NULL, s->local_port,
00213 SOCK_DGRAM, family, AI_PASSIVE);
00214 if (res0 == 0)
00215 goto fail;
00216 for (res = res0; res; res=res->ai_next) {
00217 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
00218 if (udp_fd > 0) break;
00219 av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
00220 }
00221
00222 if (udp_fd < 0)
00223 goto fail;
00224
00225 memcpy(addr, res->ai_addr, res->ai_addrlen);
00226 *addr_len = res->ai_addrlen;
00227
00228 freeaddrinfo(res0);
00229
00230 return udp_fd;
00231
00232 fail:
00233 if (udp_fd >= 0)
00234 closesocket(udp_fd);
00235 if(res0)
00236 freeaddrinfo(res0);
00237 return -1;
00238 }
00239
00240 static int udp_port(struct sockaddr_storage *addr, int addr_len)
00241 {
00242 char sbuf[sizeof(int)*3+1];
00243
00244 if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
00245 av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
00246 return -1;
00247 }
00248
00249 return strtol(sbuf, NULL, 10);
00250 }
00251
00252
00268 int ff_udp_set_remote_url(URLContext *h, const char *uri)
00269 {
00270 UDPContext *s = h->priv_data;
00271 char hostname[256], buf[10];
00272 int port;
00273 const char *p;
00274
00275 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
00276
00277
00278 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
00279 if (s->dest_addr_len < 0) {
00280 return AVERROR(EIO);
00281 }
00282 s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
00283 p = strchr(uri, '?');
00284 if (p) {
00285 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
00286 int was_connected = s->is_connected;
00287 s->is_connected = strtol(buf, NULL, 10);
00288 if (s->is_connected && !was_connected) {
00289 if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
00290 s->dest_addr_len)) {
00291 s->is_connected = 0;
00292 av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
00293 return AVERROR(EIO);
00294 }
00295 }
00296 }
00297 }
00298
00299 return 0;
00300 }
00301
00307 int ff_udp_get_local_port(URLContext *h)
00308 {
00309 UDPContext *s = h->priv_data;
00310 return s->local_port;
00311 }
00312
00318 static int udp_get_file_handle(URLContext *h)
00319 {
00320 UDPContext *s = h->priv_data;
00321 return s->udp_fd;
00322 }
00323
00324 #if HAVE_PTHREADS
00325 static void *circular_buffer_task( void *_URLContext)
00326 {
00327 URLContext *h = _URLContext;
00328 UDPContext *s = h->priv_data;
00329 fd_set rfds;
00330 struct timeval tv;
00331
00332 while(!s->exit_thread) {
00333 int left;
00334 int ret;
00335 int len;
00336
00337 if (ff_check_interrupt(&h->interrupt_callback)) {
00338 s->circular_buffer_error = EINTR;
00339 goto end;
00340 }
00341
00342 FD_ZERO(&rfds);
00343 FD_SET(s->udp_fd, &rfds);
00344 tv.tv_sec = 1;
00345 tv.tv_usec = 0;
00346 ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
00347 if (ret < 0) {
00348 if (ff_neterrno() == AVERROR(EINTR))
00349 continue;
00350 s->circular_buffer_error = EIO;
00351 goto end;
00352 }
00353
00354 if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
00355 continue;
00356
00357
00358
00359 left = av_fifo_space(s->fifo);
00360
00361
00362 if(left < UDP_MAX_PKT_SIZE + 4) {
00363 av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
00364 s->circular_buffer_error = EIO;
00365 goto end;
00366 }
00367 left = FFMIN(left, s->fifo->end - s->fifo->wptr);
00368 len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
00369 if (len < 0) {
00370 if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
00371 s->circular_buffer_error = EIO;
00372 goto end;
00373 }
00374 continue;
00375 }
00376 AV_WL32(s->tmp, len);
00377 pthread_mutex_lock(&s->mutex);
00378 av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
00379 pthread_cond_signal(&s->cond);
00380 pthread_mutex_unlock(&s->mutex);
00381 }
00382
00383 end:
00384 pthread_mutex_lock(&s->mutex);
00385 pthread_cond_signal(&s->cond);
00386 pthread_mutex_unlock(&s->mutex);
00387 return NULL;
00388 }
00389 #endif
00390
00391
00392
00393 static int udp_open(URLContext *h, const char *uri, int flags)
00394 {
00395 char hostname[1024], localaddr[1024] = "";
00396 int port, udp_fd = -1, tmp, bind_ret = -1;
00397 UDPContext *s = h->priv_data;
00398 int is_output;
00399 const char *p;
00400 char buf[256];
00401 struct sockaddr_storage my_addr;
00402 int len;
00403 int reuse_specified = 0;
00404
00405 h->is_streamed = 1;
00406 h->max_packet_size = 1472;
00407
00408 is_output = !(flags & AVIO_FLAG_READ);
00409
00410 s->ttl = 16;
00411 s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
00412
00413 s->circular_buffer_size = 7*188*4096;
00414
00415 p = strchr(uri, '?');
00416 if (p) {
00417 if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
00418 char *endptr = NULL;
00419 s->reuse_socket = strtol(buf, &endptr, 10);
00420
00421 if (buf == endptr)
00422 s->reuse_socket = 1;
00423 reuse_specified = 1;
00424 }
00425 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
00426 s->ttl = strtol(buf, NULL, 10);
00427 }
00428 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
00429 s->local_port = strtol(buf, NULL, 10);
00430 }
00431 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
00432 h->max_packet_size = strtol(buf, NULL, 10);
00433 }
00434 if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
00435 s->buffer_size = strtol(buf, NULL, 10);
00436 }
00437 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
00438 s->is_connected = strtol(buf, NULL, 10);
00439 }
00440 if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
00441 s->circular_buffer_size = strtol(buf, NULL, 10)*188;
00442 }
00443 if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
00444 av_strlcpy(localaddr, buf, sizeof(localaddr));
00445 }
00446 }
00447
00448
00449 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
00450
00451
00452 if (hostname[0] == '\0' || hostname[0] == '?') {
00453
00454 if (!(flags & AVIO_FLAG_READ))
00455 goto fail;
00456 } else {
00457 if (ff_udp_set_remote_url(h, uri) < 0)
00458 goto fail;
00459 }
00460
00461 if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
00462 s->local_port = port;
00463 udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
00464 if (udp_fd < 0)
00465 goto fail;
00466
00467
00468
00469
00470 if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
00471 s->reuse_socket = 1;
00472 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
00473 goto fail;
00474 }
00475
00476
00477
00478 if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
00479 bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
00480 }
00481
00482
00483 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
00484 goto fail;
00485
00486 len = sizeof(my_addr);
00487 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
00488 s->local_port = udp_port(&my_addr, len);
00489
00490 if (s->is_multicast) {
00491 if (!(h->flags & AVIO_FLAG_READ)) {
00492
00493 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
00494 goto fail;
00495 } else {
00496
00497 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
00498 goto fail;
00499 }
00500 }
00501
00502 if (is_output) {
00503
00504 tmp = s->buffer_size;
00505 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
00506 av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
00507 goto fail;
00508 }
00509 } else {
00510
00511
00512 tmp = s->buffer_size;
00513 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
00514 av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
00515 }
00516
00517 ff_socket_nonblock(udp_fd, 1);
00518 }
00519 if (s->is_connected) {
00520 if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
00521 av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
00522 goto fail;
00523 }
00524 }
00525
00526 s->udp_fd = udp_fd;
00527
00528 #if HAVE_PTHREADS
00529 if (!is_output && s->circular_buffer_size) {
00530 int ret;
00531
00532
00533 s->fifo = av_fifo_alloc(s->circular_buffer_size);
00534 ret = pthread_mutex_init(&s->mutex, NULL);
00535 if (ret != 0) {
00536 av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
00537 goto fail;
00538 }
00539 ret = pthread_cond_init(&s->cond, NULL);
00540 if (ret != 0) {
00541 av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
00542 goto cond_fail;
00543 }
00544 ret = pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h);
00545 if (ret != 0) {
00546 av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
00547 goto thread_fail;
00548 }
00549 s->thread_started = 1;
00550 }
00551 #endif
00552
00553 return 0;
00554 #if HAVE_PTHREADS
00555 thread_fail:
00556 pthread_cond_destroy(&s->cond);
00557 cond_fail:
00558 pthread_mutex_destroy(&s->mutex);
00559 #endif
00560 fail:
00561 if (udp_fd >= 0)
00562 closesocket(udp_fd);
00563 av_fifo_free(s->fifo);
00564 return AVERROR(EIO);
00565 }
00566
00567 static int udp_read(URLContext *h, uint8_t *buf, int size)
00568 {
00569 UDPContext *s = h->priv_data;
00570 int ret;
00571 int avail;
00572
00573 #if HAVE_PTHREADS
00574 if (s->fifo) {
00575 pthread_mutex_lock(&s->mutex);
00576 do {
00577 avail = av_fifo_size(s->fifo);
00578 if (avail) {
00579 uint8_t tmp[4];
00580 pthread_mutex_unlock(&s->mutex);
00581
00582 av_fifo_generic_read(s->fifo, tmp, 4, NULL);
00583 avail= AV_RL32(tmp);
00584 if(avail > size){
00585 av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
00586 avail= size;
00587 }
00588
00589 av_fifo_generic_read(s->fifo, buf, avail, NULL);
00590 av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
00591 return avail;
00592 } else if(s->circular_buffer_error){
00593 pthread_mutex_unlock(&s->mutex);
00594 return s->circular_buffer_error;
00595 } else if(h->flags & AVIO_FLAG_NONBLOCK) {
00596 pthread_mutex_unlock(&s->mutex);
00597 return AVERROR(EAGAIN);
00598 }
00599 else {
00600 pthread_cond_wait(&s->cond, &s->mutex);
00601 }
00602 } while( 1);
00603 }
00604 #endif
00605
00606 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
00607 ret = ff_network_wait_fd(s->udp_fd, 0);
00608 if (ret < 0)
00609 return ret;
00610 }
00611 ret = recv(s->udp_fd, buf, size, 0);
00612
00613 return ret < 0 ? ff_neterrno() : ret;
00614 }
00615
00616 static int udp_write(URLContext *h, const uint8_t *buf, int size)
00617 {
00618 UDPContext *s = h->priv_data;
00619 int ret;
00620
00621 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
00622 ret = ff_network_wait_fd(s->udp_fd, 1);
00623 if (ret < 0)
00624 return ret;
00625 }
00626
00627 if (!s->is_connected) {
00628 ret = sendto (s->udp_fd, buf, size, 0,
00629 (struct sockaddr *) &s->dest_addr,
00630 s->dest_addr_len);
00631 } else
00632 ret = send(s->udp_fd, buf, size, 0);
00633
00634 return ret < 0 ? ff_neterrno() : ret;
00635 }
00636
00637 static int udp_close(URLContext *h)
00638 {
00639 UDPContext *s = h->priv_data;
00640 int ret;
00641
00642 if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
00643 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
00644 closesocket(s->udp_fd);
00645 av_fifo_free(s->fifo);
00646 #if HAVE_PTHREADS
00647 if (s->thread_started) {
00648 s->exit_thread = 1;
00649 ret = pthread_join(s->circular_buffer_thread, NULL);
00650 if (ret != 0)
00651 av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
00652 }
00653
00654 pthread_mutex_destroy(&s->mutex);
00655 pthread_cond_destroy(&s->cond);
00656 #endif
00657 return 0;
00658 }
00659
00660 URLProtocol ff_udp_protocol = {
00661 .name = "udp",
00662 .url_open = udp_open,
00663 .url_read = udp_read,
00664 .url_write = udp_write,
00665 .url_close = udp_close,
00666 .url_get_file_handle = udp_get_file_handle,
00667 .priv_data_size = sizeof(UDPContext),
00668 .flags = URL_PROTOCOL_FLAG_NETWORK,
00669 };