helpers.cpp 2.6 KB

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