time.c 2.3 KB

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