You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.5KB

  1. /*
  2. * Various utilities for ffmpeg system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * copyright (c) 2002 Francois Revol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "config.h"
  23. #include "avformat.h"
  24. #if defined(__MINGW32__)
  25. #include <sys/types.h>
  26. #include <sys/timeb.h>
  27. #else
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/time.h>
  31. #endif
  32. #include <time.h>
  33. #ifndef HAVE_SYS_POLL_H
  34. #if defined(__MINGW32__)
  35. #include <winsock2.h>
  36. #else
  37. #include <sys/select.h>
  38. #endif
  39. #endif
  40. /**
  41. * gets the current time in micro seconds.
  42. */
  43. int64_t av_gettime(void)
  44. {
  45. #if defined(__MINGW32__)
  46. struct timeb tb;
  47. _ftime(&tb);
  48. return ((int64_t)tb.time * INT64_C(1000) + (int64_t)tb.millitm) * INT64_C(1000);
  49. #else
  50. struct timeval tv;
  51. gettimeofday(&tv,NULL);
  52. return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  53. #endif
  54. }
  55. #ifdef CONFIG_NETWORK
  56. #include "network.h"
  57. #if !defined(HAVE_INET_ATON)
  58. #include <stdlib.h>
  59. #include <strings.h>
  60. int inet_aton (const char * str, struct in_addr * add)
  61. {
  62. const char * pch = str;
  63. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  64. add1 = atoi(pch);
  65. pch = strpbrk(pch,".");
  66. if (pch == 0 || ++pch == 0) goto done;
  67. add2 = atoi(pch);
  68. pch = strpbrk(pch,".");
  69. if (pch == 0 || ++pch == 0) goto done;
  70. add3 = atoi(pch);
  71. pch = strpbrk(pch,".");
  72. if (pch == 0 || ++pch == 0) goto done;
  73. add4 = atoi(pch);
  74. done:
  75. add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
  76. return 1;
  77. }
  78. #endif /* !defined(HAVE_INET_ATON) */
  79. /* resolve host with also IP address parsing */
  80. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  81. {
  82. struct hostent *hp;
  83. if (!inet_aton(hostname, sin_addr)) {
  84. hp = gethostbyname(hostname);
  85. if (!hp)
  86. return -1;
  87. memcpy(sin_addr, hp->h_addr, sizeof(struct in_addr));
  88. }
  89. return 0;
  90. }
  91. int ff_socket_nonblock(int socket, int enable)
  92. {
  93. #ifdef __MINGW32__
  94. return ioctlsocket(socket, FIONBIO, &enable);
  95. #else
  96. if (enable)
  97. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  98. else
  99. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  100. #endif
  101. }
  102. #endif /* CONFIG_NETWORK */
  103. #ifdef CONFIG_FFSERVER
  104. #ifndef HAVE_SYS_POLL_H
  105. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  106. {
  107. fd_set read_set;
  108. fd_set write_set;
  109. fd_set exception_set;
  110. nfds_t i;
  111. int n;
  112. int rc;
  113. #ifdef __MINGW32__
  114. if (numfds >= FD_SETSIZE) {
  115. errno = EINVAL;
  116. return -1;
  117. }
  118. #endif
  119. FD_ZERO(&read_set);
  120. FD_ZERO(&write_set);
  121. FD_ZERO(&exception_set);
  122. n = -1;
  123. for(i = 0; i < numfds; i++) {
  124. if (fds[i].fd < 0)
  125. continue;
  126. #ifndef __MINGW32__
  127. if (fds[i].fd >= FD_SETSIZE) {
  128. errno = EINVAL;
  129. return -1;
  130. }
  131. #endif
  132. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  133. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  134. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  135. if (fds[i].fd > n)
  136. n = fds[i].fd;
  137. };
  138. if (n == -1)
  139. /* Hey!? Nothing to poll, in fact!!! */
  140. return 0;
  141. if (timeout < 0)
  142. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  143. else {
  144. struct timeval tv;
  145. tv.tv_sec = timeout / 1000;
  146. tv.tv_usec = 1000 * (timeout % 1000);
  147. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  148. };
  149. if (rc < 0)
  150. return rc;
  151. for(i = 0; i < (nfds_t) n; i++) {
  152. fds[i].revents = 0;
  153. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  154. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  155. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  156. };
  157. return rc;
  158. }
  159. #endif /* HAVE_SYS_POLL_H */
  160. #endif /* CONFIG_FFSERVER */