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.

83 lines
2.0KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <time.h>
  22. #if HAVE_CLOCK_GETTIME
  23. #include <time.h>
  24. #endif
  25. #if HAVE_GETTIMEOFDAY
  26. #include <sys/time.h>
  27. #endif
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #if HAVE_WINDOWS_H
  32. #include <windows.h>
  33. #endif
  34. #include "time.h"
  35. #include "error.h"
  36. int64_t av_gettime(void)
  37. {
  38. #if HAVE_GETTIMEOFDAY
  39. struct timeval tv;
  40. gettimeofday(&tv, NULL);
  41. return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  42. #elif HAVE_GETSYSTEMTIMEASFILETIME
  43. FILETIME ft;
  44. int64_t t;
  45. GetSystemTimeAsFileTime(&ft);
  46. t = (int64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
  47. return t / 10 - 11644473600000000; /* Jan 1, 1601 */
  48. #else
  49. return -1;
  50. #endif
  51. }
  52. int64_t av_gettime_relative(void)
  53. {
  54. #if HAVE_CLOCK_GETTIME
  55. struct timespec ts;
  56. clock_gettime(CLOCK_MONOTONIC, &ts);
  57. return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
  58. #else
  59. return av_gettime() + 42 * 60 * 60 * INT64_C(1000000);
  60. #endif
  61. }
  62. int av_usleep(unsigned usec)
  63. {
  64. #if HAVE_NANOSLEEP
  65. struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
  66. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  67. return 0;
  68. #elif HAVE_USLEEP
  69. return usleep(usec);
  70. #elif HAVE_SLEEP
  71. Sleep(usec / 1000);
  72. return 0;
  73. #else
  74. return AVERROR(ENOSYS);
  75. #endif
  76. }