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. #if !defined(HAVE_LOCALTIME_R)
  59. struct tm *localtime_r(const time_t *t, struct tm *tp)
  60. {
  61. struct tm *l;
  62. l = localtime(t);
  63. if (!l)
  64. return 0;
  65. *tp = *l;
  66. return tp;
  67. }
  68. #endif /* !defined(HAVE_LOCALTIME_R) */
  69. #ifdef CONFIG_NETWORK
  70. #include "network.h"
  71. #if !defined(HAVE_INET_ATON)
  72. #include <stdlib.h>
  73. #include <strings.h>
  74. int inet_aton (const char * str, struct in_addr * add)
  75. {
  76. const char * pch = str;
  77. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  78. add1 = atoi(pch);
  79. pch = strpbrk(pch,".");
  80. if (pch == 0 || ++pch == 0) goto done;
  81. add2 = atoi(pch);
  82. pch = strpbrk(pch,".");
  83. if (pch == 0 || ++pch == 0) goto done;
  84. add3 = atoi(pch);
  85. pch = strpbrk(pch,".");
  86. if (pch == 0 || ++pch == 0) goto done;
  87. add4 = atoi(pch);
  88. done:
  89. add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
  90. return 1;
  91. }
  92. #endif /* !defined(HAVE_INET_ATON) */
  93. /* resolve host with also IP address parsing */
  94. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  95. {
  96. struct hostent *hp;
  97. if (!inet_aton(hostname, sin_addr)) {
  98. hp = gethostbyname(hostname);
  99. if (!hp)
  100. return -1;
  101. memcpy(sin_addr, hp->h_addr, sizeof(struct in_addr));
  102. }
  103. return 0;
  104. }
  105. int ff_socket_nonblock(int socket, int enable)
  106. {
  107. if (enable)
  108. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  109. else
  110. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  111. }
  112. #endif /* CONFIG_NETWORK */
  113. #ifdef CONFIG_FFSERVER
  114. #ifndef HAVE_SYS_POLL_H
  115. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  116. {
  117. fd_set read_set;
  118. fd_set write_set;
  119. fd_set exception_set;
  120. nfds_t i;
  121. int n;
  122. int rc;
  123. FD_ZERO(&read_set);
  124. FD_ZERO(&write_set);
  125. FD_ZERO(&exception_set);
  126. n = -1;
  127. for(i = 0; i < numfds; i++) {
  128. if (fds[i].fd < 0)
  129. continue;
  130. if (fds[i].fd >= FD_SETSIZE) {
  131. errno = EINVAL;
  132. return -1;
  133. }
  134. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  135. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  136. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  137. if (fds[i].fd > n)
  138. n = fds[i].fd;
  139. };
  140. if (n == -1)
  141. /* Hey!? Nothing to poll, in fact!!! */
  142. return 0;
  143. if (timeout < 0)
  144. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  145. else {
  146. struct timeval tv;
  147. tv.tv_sec = timeout / 1000;
  148. tv.tv_usec = 1000 * (timeout % 1000);
  149. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  150. };
  151. if (rc < 0)
  152. return rc;
  153. for(i = 0; i < (nfds_t) n; i++) {
  154. fds[i].revents = 0;
  155. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  156. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  157. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  158. };
  159. return rc;
  160. }
  161. #endif /* HAVE_SYS_POLL_H */
  162. #endif /* CONFIG_FFSERVER */