helpers.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "helpers.hpp"
  19. #include <ctime>
  20. #ifdef _MSC_VER
  21. // taken from https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
  22. #include <windows.h>
  23. #include <winsock2.h> // for struct timeval
  24. struct timezone {
  25. int tz_minuteswest;
  26. int tz_dsttime;
  27. };
  28. int gettimeofday(struct timeval *tv, struct timezone *tz) {
  29. if (tv) {
  30. FILETIME filetime; /* 64-bit value representing the number of 100-nanosecond intervals since
  31. January 1, 1601 00:00 UTC */
  32. ULARGE_INTEGER x;
  33. ULONGLONG usec;
  34. static const ULONGLONG epoch_offset_us =
  35. 11644473600000000ULL; /* microseconds betweeen Jan 1,1601 and Jan 1,1970 */
  36. #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
  37. GetSystemTimePreciseAsFileTime(&filetime);
  38. #else
  39. GetSystemTimeAsFileTime(&filetime);
  40. #endif
  41. x.LowPart = filetime.dwLowDateTime;
  42. x.HighPart = filetime.dwHighDateTime;
  43. usec = x.QuadPart / 10 - epoch_offset_us;
  44. tv->tv_sec = time_t(usec / 1000000ULL);
  45. tv->tv_usec = long(usec % 1000000ULL);
  46. }
  47. if (tz) {
  48. TIME_ZONE_INFORMATION timezone;
  49. GetTimeZoneInformation(&timezone);
  50. tz->tz_minuteswest = timezone.Bias;
  51. tz->tz_dsttime = 0;
  52. }
  53. return 0;
  54. }
  55. #else
  56. #include <sys/time.h>
  57. #endif
  58. using namespace std;
  59. using namespace rtc;
  60. ClientTrackData::ClientTrackData(shared_ptr<Track> track, shared_ptr<RtcpSrReporter> sender) {
  61. this->track = track;
  62. this->sender = sender;
  63. }
  64. void Client::setState(State state) {
  65. std::unique_lock lock(_mutex);
  66. this->state = state;
  67. }
  68. Client::State Client::getState() {
  69. std::shared_lock lock(_mutex);
  70. return state;
  71. }
  72. ClientTrack::ClientTrack(string id, shared_ptr<ClientTrackData> trackData) {
  73. this->id = id;
  74. this->trackData = trackData;
  75. }
  76. uint64_t currentTimeInMicroSeconds() {
  77. struct timeval time;
  78. gettimeofday(&time, NULL);
  79. return uint64_t(time.tv_sec) * 1000 * 1000 + time.tv_usec;
  80. }