Delete NtpOffsetMs
and TimeMicrosToNtp
methods.
This consolidates the querying of the Ntp time in once place, the clock. Bug: webrtc:11327 Change-Id: I14b19c2380996571d8c67c2c186629c209787162 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/219794 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Paul Hallak <phallak@google.com> Cr-Commit-Position: refs/heads/master@{#34083}
This commit is contained in:

committed by
WebRTC LUCI CQ

parent
46fbefa302
commit
cab90db24a
@ -17,48 +17,6 @@
|
||||
#include "rtc_base/time_utils.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
|
||||
int64_t NtpOffsetMsCalledOnce() {
|
||||
constexpr int64_t kNtpJan1970Sec = 2208988800;
|
||||
int64_t clock_time = rtc::TimeMillis();
|
||||
int64_t utc_time = rtc::TimeUTCMillis();
|
||||
return utc_time - clock_time + kNtpJan1970Sec * rtc::kNumMillisecsPerSec;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int64_t NtpOffsetMs() {
|
||||
// Calculate the offset once.
|
||||
static int64_t ntp_offset_ms = NtpOffsetMsCalledOnce();
|
||||
return ntp_offset_ms;
|
||||
}
|
||||
|
||||
NtpTime TimeMicrosToNtp(int64_t time_us) {
|
||||
// Since this doesn't return a wallclock time, but only NTP representation
|
||||
// of rtc::TimeMillis() clock, the exact offset doesn't matter.
|
||||
// To simplify conversions between NTP and RTP time, this offset is
|
||||
// limited to milliseconds in resolution.
|
||||
int64_t time_ntp_us = time_us + NtpOffsetMs() * 1000;
|
||||
RTC_DCHECK_GE(time_ntp_us, 0); // Time before year 1900 is unsupported.
|
||||
|
||||
// TODO(danilchap): Convert both seconds and fraction together using int128
|
||||
// when that type is easily available.
|
||||
// Currently conversion is done separetly for seconds and fraction of a second
|
||||
// to avoid overflow.
|
||||
|
||||
// Convert seconds to uint32 through uint64 for well-defined cast.
|
||||
// Wrap around (will happen in 2036) is expected for ntp time.
|
||||
uint32_t ntp_seconds =
|
||||
static_cast<uint64_t>(time_ntp_us / rtc::kNumMicrosecsPerSec);
|
||||
|
||||
// Scale fractions of the second to ntp resolution.
|
||||
constexpr int64_t kNtpInSecond = 1LL << 32;
|
||||
int64_t us_fractions = time_ntp_us % rtc::kNumMicrosecsPerSec;
|
||||
uint32_t ntp_fractions =
|
||||
us_fractions * kNtpInSecond / rtc::kNumMicrosecsPerSec;
|
||||
return NtpTime(ntp_seconds, ntp_fractions);
|
||||
}
|
||||
|
||||
uint32_t SaturatedUsToCompactNtp(int64_t us) {
|
||||
constexpr uint32_t kMaxCompactNtp = 0xFFFFFFFF;
|
||||
|
@ -17,20 +17,6 @@
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Converts time obtained using rtc::TimeMicros to ntp format.
|
||||
// TimeMicrosToNtp guarantees difference of the returned values matches
|
||||
// difference of the passed values.
|
||||
// As a result TimeMicrosToNtp(rtc::TimeMicros()) doesn't guarantee to match
|
||||
// system time.
|
||||
// However, TimeMicrosToNtp Guarantees that returned NtpTime will be offsetted
|
||||
// from rtc::TimeMicros() by integral number of milliseconds.
|
||||
// Use NtpOffsetMs() to get that offset value.
|
||||
NtpTime TimeMicrosToNtp(int64_t time_us);
|
||||
|
||||
// Difference between Ntp time and local relative time returned by
|
||||
// rtc::TimeMicros()
|
||||
int64_t NtpOffsetMs();
|
||||
|
||||
// Helper function for compact ntp representation:
|
||||
// RFC 3550, Section 4. Time Format.
|
||||
// Wallclock time is represented using the timestamp format of
|
||||
|
@ -9,34 +9,10 @@
|
||||
*/
|
||||
#include "modules/rtp_rtcp/source/time_util.h"
|
||||
|
||||
#include "rtc_base/fake_clock.h"
|
||||
#include "rtc_base/time_utils.h"
|
||||
#include "system_wrappers/include/clock.h"
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
TEST(TimeUtilTest, TimeMicrosToNtpDoesntChangeBetweenRuns) {
|
||||
rtc::ScopedFakeClock clock;
|
||||
// TimeMicrosToNtp is not pure: it behave differently between different
|
||||
// execution of the program, but should behave same during same execution.
|
||||
const int64_t time_us = 12345;
|
||||
clock.SetTime(Timestamp::Micros(2));
|
||||
NtpTime time_ntp = TimeMicrosToNtp(time_us);
|
||||
clock.SetTime(Timestamp::Micros(time_us));
|
||||
EXPECT_EQ(TimeMicrosToNtp(time_us), time_ntp);
|
||||
clock.SetTime(Timestamp::Micros(1000000));
|
||||
EXPECT_EQ(TimeMicrosToNtp(time_us), time_ntp);
|
||||
}
|
||||
|
||||
TEST(TimeUtilTest, TimeMicrosToNtpKeepsIntervals) {
|
||||
rtc::ScopedFakeClock clock;
|
||||
NtpTime time_ntp1 = TimeMicrosToNtp(rtc::TimeMicros());
|
||||
clock.AdvanceTime(TimeDelta::Millis(20));
|
||||
NtpTime time_ntp2 = TimeMicrosToNtp(rtc::TimeMicros());
|
||||
EXPECT_EQ(time_ntp2.ToMs() - time_ntp1.ToMs(), 20);
|
||||
}
|
||||
|
||||
TEST(TimeUtilTest, CompactNtp) {
|
||||
const uint32_t kNtpSec = 0x12345678;
|
||||
const uint32_t kNtpFrac = 0x23456789;
|
||||
|
Reference in New Issue
Block a user