time_util.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_
  11. #define MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_
  12. #include <stdint.h>
  13. #include "system_wrappers/include/ntp_time.h"
  14. namespace webrtc {
  15. // Converts time obtained using rtc::TimeMicros to ntp format.
  16. // TimeMicrosToNtp guarantees difference of the returned values matches
  17. // difference of the passed values.
  18. // As a result TimeMicrosToNtp(rtc::TimeMicros()) doesn't guarantee to match
  19. // system time.
  20. // However, TimeMicrosToNtp Guarantees that returned NtpTime will be offsetted
  21. // from rtc::TimeMicros() by integral number of milliseconds.
  22. // Use NtpOffsetMs() to get that offset value.
  23. NtpTime TimeMicrosToNtp(int64_t time_us);
  24. // Difference between Ntp time and local relative time returned by
  25. // rtc::TimeMicros()
  26. int64_t NtpOffsetMs();
  27. // Helper function for compact ntp representation:
  28. // RFC 3550, Section 4. Time Format.
  29. // Wallclock time is represented using the timestamp format of
  30. // the Network Time Protocol (NTP).
  31. // ...
  32. // In some fields where a more compact representation is
  33. // appropriate, only the middle 32 bits are used; that is, the low 16
  34. // bits of the integer part and the high 16 bits of the fractional part.
  35. inline uint32_t CompactNtp(NtpTime ntp) {
  36. return (ntp.seconds() << 16) | (ntp.fractions() >> 16);
  37. }
  38. // Converts interval in microseconds to compact ntp (1/2^16 seconds) resolution.
  39. // Negative values converted to 0, Overlarge values converted to max uint32_t.
  40. uint32_t SaturatedUsToCompactNtp(int64_t us);
  41. // Converts interval between compact ntp timestamps to milliseconds.
  42. // This interval can be up to ~9.1 hours (2^15 seconds).
  43. // Values close to 2^16 seconds consider negative and result in minimum rtt = 1.
  44. int64_t CompactNtpRttToMs(uint32_t compact_ntp_interval);
  45. } // namespace webrtc
  46. #endif // MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_