time.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "libavutil/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. int av_usleep(unsigned usec)
  52. {
  53. #if HAVE_NANOSLEEP
  54. struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
  55. while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
  56. return 0;
  57. #elif HAVE_USLEEP
  58. return usleep(usec);
  59. #elif HAVE_SLEEP
  60. Sleep(usec / 1000);
  61. return 0;
  62. #else
  63. return AVERROR(ENOSYS);
  64. #endif
  65. }