helpers.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #include "helpers.hpp"
  10. #include <ctime>
  11. #ifdef _MSC_VER
  12. // taken from https://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
  13. #include <windows.h>
  14. #include <winsock2.h> // for struct timeval
  15. struct timezone {
  16. int tz_minuteswest;
  17. int tz_dsttime;
  18. };
  19. int gettimeofday(struct timeval *tv, struct timezone *tz) {
  20. if (tv) {
  21. FILETIME filetime; /* 64-bit value representing the number of 100-nanosecond intervals since
  22. January 1, 1601 00:00 UTC */
  23. ULARGE_INTEGER x;
  24. ULONGLONG usec;
  25. static const ULONGLONG epoch_offset_us =
  26. 11644473600000000ULL; /* microseconds betweeen Jan 1,1601 and Jan 1,1970 */
  27. #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
  28. GetSystemTimePreciseAsFileTime(&filetime);
  29. #else
  30. GetSystemTimeAsFileTime(&filetime);
  31. #endif
  32. x.LowPart = filetime.dwLowDateTime;
  33. x.HighPart = filetime.dwHighDateTime;
  34. usec = x.QuadPart / 10 - epoch_offset_us;
  35. tv->tv_sec = long(usec / 1000000ULL);
  36. tv->tv_usec = long(usec % 1000000ULL);
  37. }
  38. if (tz) {
  39. TIME_ZONE_INFORMATION timezone;
  40. GetTimeZoneInformation(&timezone);
  41. tz->tz_minuteswest = timezone.Bias;
  42. tz->tz_dsttime = 0;
  43. }
  44. return 0;
  45. }
  46. #else
  47. #include <sys/time.h>
  48. #endif
  49. using namespace std;
  50. using namespace rtc;
  51. ClientTrackData::ClientTrackData(shared_ptr<Track> track, shared_ptr<RtcpSrReporter> sender) {
  52. this->track = track;
  53. this->sender = sender;
  54. }
  55. void Client::setState(State state) {
  56. std::unique_lock lock(_mutex);
  57. this->state = state;
  58. }
  59. Client::State Client::getState() {
  60. std::shared_lock lock(_mutex);
  61. return state;
  62. }
  63. ClientTrack::ClientTrack(string id, shared_ptr<ClientTrackData> trackData) {
  64. this->id = id;
  65. this->trackData = trackData;
  66. }
  67. uint64_t currentTimeInMicroSeconds() {
  68. struct timeval time;
  69. gettimeofday(&time, NULL);
  70. return uint64_t(time.tv_sec) * 1000 * 1000 + time.tv_usec;
  71. }