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.

189 lines
4.6KB

  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. #elif defined(CONFIG_OS2)
  28. #include <string.h>
  29. #include <sys/time.h>
  30. #else
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <sys/time.h>
  34. #endif
  35. #include <time.h>
  36. #ifndef HAVE_SYS_POLL_H
  37. #if defined(__MINGW32__)
  38. #include <winsock2.h>
  39. #else
  40. #include <sys/select.h>
  41. #endif
  42. #endif
  43. /**
  44. * gets the current time in micro seconds.
  45. */
  46. int64_t av_gettime(void)
  47. {
  48. #if defined(__MINGW32__)
  49. struct timeb tb;
  50. _ftime(&tb);
  51. return ((int64_t)tb.time * INT64_C(1000) + (int64_t)tb.millitm) * INT64_C(1000);
  52. #else
  53. struct timeval tv;
  54. gettimeofday(&tv,NULL);
  55. return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  56. #endif
  57. }
  58. #ifdef CONFIG_NETWORK
  59. #include "network.h"
  60. #if !defined(HAVE_INET_ATON)
  61. #include <stdlib.h>
  62. #include <strings.h>
  63. int inet_aton (const char * str, struct in_addr * add)
  64. {
  65. const char * pch = str;
  66. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  67. add1 = atoi(pch);
  68. pch = strpbrk(pch,".");
  69. if (pch == 0 || ++pch == 0) goto done;
  70. add2 = atoi(pch);
  71. pch = strpbrk(pch,".");
  72. if (pch == 0 || ++pch == 0) goto done;
  73. add3 = atoi(pch);
  74. pch = strpbrk(pch,".");
  75. if (pch == 0 || ++pch == 0) goto done;
  76. add4 = atoi(pch);
  77. done:
  78. add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
  79. return 1;
  80. }
  81. #endif /* !defined(HAVE_INET_ATON) */
  82. /* resolve host with also IP address parsing */
  83. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  84. {
  85. struct hostent *hp;
  86. if (!inet_aton(hostname, sin_addr)) {
  87. hp = gethostbyname(hostname);
  88. if (!hp)
  89. return -1;
  90. memcpy(sin_addr, hp->h_addr, sizeof(struct in_addr));
  91. }
  92. return 0;
  93. }
  94. int ff_socket_nonblock(int socket, int enable)
  95. {
  96. #ifdef __MINGW32__
  97. return ioctlsocket(socket, FIONBIO, &enable);
  98. #else
  99. if (enable)
  100. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  101. else
  102. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  103. #endif
  104. }
  105. #endif /* CONFIG_NETWORK */
  106. #ifdef CONFIG_FFSERVER
  107. #ifndef HAVE_SYS_POLL_H
  108. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  109. {
  110. fd_set read_set;
  111. fd_set write_set;
  112. fd_set exception_set;
  113. nfds_t i;
  114. int n;
  115. int rc;
  116. #ifdef __MINGW32__
  117. if (numfds >= FD_SETSIZE) {
  118. errno = EINVAL;
  119. return -1;
  120. }
  121. #endif
  122. FD_ZERO(&read_set);
  123. FD_ZERO(&write_set);
  124. FD_ZERO(&exception_set);
  125. n = -1;
  126. for(i = 0; i < numfds; i++) {
  127. if (fds[i].fd < 0)
  128. continue;
  129. #ifndef __MINGW32__
  130. if (fds[i].fd >= FD_SETSIZE) {
  131. errno = EINVAL;
  132. return -1;
  133. }
  134. #endif
  135. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  136. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  137. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  138. if (fds[i].fd > n)
  139. n = fds[i].fd;
  140. };
  141. if (n == -1)
  142. /* Hey!? Nothing to poll, in fact!!! */
  143. return 0;
  144. if (timeout < 0)
  145. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  146. else {
  147. struct timeval tv;
  148. tv.tv_sec = timeout / 1000;
  149. tv.tv_usec = 1000 * (timeout % 1000);
  150. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  151. };
  152. if (rc < 0)
  153. return rc;
  154. for(i = 0; i < (nfds_t) n; i++) {
  155. fds[i].revents = 0;
  156. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  157. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  158. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  159. };
  160. return rc;
  161. }
  162. #endif /* HAVE_SYS_POLL_H */
  163. #endif /* CONFIG_FFSERVER */