helpers.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 _WIN32
  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. {
  30. if (tv) {
  31. FILETIME filetime; /* 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 00:00 UTC */
  32. ULARGE_INTEGER x;
  33. ULONGLONG usec;
  34. static const ULONGLONG epoch_offset_us = 11644473600000000ULL; /* microseconds betweeen Jan 1,1601 and Jan 1,1970 */
  35. #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
  36. GetSystemTimePreciseAsFileTime(&filetime);
  37. #else
  38. GetSystemTimeAsFileTime(&filetime);
  39. #endif
  40. x.LowPart = filetime.dwLowDateTime;
  41. x.HighPart = filetime.dwHighDateTime;
  42. usec = x.QuadPart / 10 - epoch_offset_us;
  43. tv->tv_sec = time_t(usec / 1000000ULL);
  44. tv->tv_usec = long(usec % 1000000ULL);
  45. }
  46. if (tz) {
  47. TIME_ZONE_INFORMATION timezone;
  48. GetTimeZoneInformation(&timezone);
  49. tz->tz_minuteswest = timezone.Bias;
  50. tz->tz_dsttime = 0;
  51. }
  52. return 0;
  53. }
  54. #else
  55. #include <sys/time.h>
  56. #endif
  57. using namespace std;
  58. using namespace rtc;
  59. ClientTrackData::ClientTrackData(shared_ptr<Track> track, shared_ptr<RtcpSrReporter> sender) {
  60. this->track = track;
  61. this->sender = sender;
  62. }
  63. void Client::setState(State state) {
  64. std::unique_lock lock(_mutex);
  65. this->state = state;
  66. }
  67. Client::State Client::getState() {
  68. std::shared_lock lock(_mutex);
  69. return state;
  70. }
  71. ClientTrack::ClientTrack(string id, shared_ptr<ClientTrackData> trackData) {
  72. this->id = id;
  73. this->trackData = trackData;
  74. }
  75. uint64_t currentTimeInMicroSeconds() {
  76. struct timeval time;
  77. gettimeofday(&time, NULL);
  78. return uint64_t(time.tv_sec) * 1000 * 1000 + time.tv_usec;
  79. }