time.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. struct timespec ts;
  55. clock_gettime(CLOCK_MONOTONIC, &ts);
  56. return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
  57. #else
  58. return av_gettime();
  59. #endif
  60. }
  61. int av_gettime_relative_is_monotonic(void)
  62. {
  63. #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
  64. return 1;
  65. #else
  66. return 0;
  67. #endif
  68. }
  69. int av_usleep(unsigned usec)
  70. {
  71. #if HAVE_NANOSLEEP
  72. struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
  73. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  74. return 0;
  75. #elif HAVE_USLEEP
  76. return usleep(usec);
  77. #elif HAVE_SLEEP
  78. Sleep(usec / 1000);
  79. return 0;
  80. #else
  81. return AVERROR(ENOSYS);
  82. #endif
  83. }