replace NtpTime->Clock with Clock->NtpTime dependency

BUG=None

Review-Url: https://codereview.webrtc.org/2393723004
Cr-Commit-Position: refs/heads/master@{#16519}
This commit is contained in:
danilchap
2017-02-09 11:15:25 -08:00
committed by Commit bot
parent 1e1c84db10
commit 3795376ba1
9 changed files with 35 additions and 47 deletions

View File

@ -17,6 +17,7 @@
#include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h" #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
#include "webrtc/modules/rtp_rtcp/source/time_util.h" #include "webrtc/modules/rtp_rtcp/source/time_util.h"
#include "webrtc/system_wrappers/include/clock.h"
namespace webrtc { namespace webrtc {
@ -78,7 +79,7 @@ void StreamStatisticianImpl::UpdateCounters(const RTPHeader& header,
// are received, 4 will be ignored. // are received, 4 will be ignored.
if (in_order) { if (in_order) {
// Current time in samples. // Current time in samples.
NtpTime receive_time(*clock_); NtpTime receive_time = clock_->CurrentNtpTime();
// Wrong if we use RetransmitOfOldPacket. // Wrong if we use RetransmitOfOldPacket.
if (receive_counters_.transmitted.packets > 1 && if (receive_counters_.transmitted.packets > 1 &&

View File

@ -260,7 +260,7 @@ bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
// Get the delay since last received report (RFC 3611). // Get the delay since last received report (RFC 3611).
uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_); uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
uint32_t now_ntp = CompactNtp(NtpTime(*clock_)); uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
info->delay_since_last_rr = now_ntp - receive_time_ntp; info->delay_since_last_rr = now_ntp - receive_time_ntp;
return true; return true;
@ -423,7 +423,7 @@ void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
remote_sender_info_.sendPacketCount = sender_report.sender_packet_count(); remote_sender_info_.sendPacketCount = sender_report.sender_packet_count();
remote_sender_info_.sendOctetCount = sender_report.sender_octet_count(); remote_sender_info_.sendOctetCount = sender_report.sender_octet_count();
last_received_sr_ntp_.SetCurrent(*clock_); last_received_sr_ntp_ = clock_->CurrentNtpTime();
} else { } else {
// We will only store the send report from one source, but // We will only store the send report from one source, but
// we will store all the receive blocks. // we will store all the receive blocks.
@ -504,7 +504,7 @@ void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
if (!receiver_only_ && send_time_ntp != 0) { if (!receiver_only_ && send_time_ntp != 0) {
uint32_t delay_ntp = report_block.delay_since_last_sr(); uint32_t delay_ntp = report_block.delay_since_last_sr();
// Local NTP time. // Local NTP time.
uint32_t receive_time_ntp = CompactNtp(NtpTime(*clock_)); uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
// RTT in 1/(2^16) seconds. // RTT in 1/(2^16) seconds.
uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp; uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
@ -709,7 +709,7 @@ void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
const rtcp::Rrtr& rrtr) { const rtcp::Rrtr& rrtr) {
remote_time_info_.ssrc = sender_ssrc; remote_time_info_.ssrc = sender_ssrc;
remote_time_info_.last_rr = CompactNtp(rrtr.ntp()); remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
last_received_xr_ntp_.SetCurrent(*clock_); last_received_xr_ntp_ = clock_->CurrentNtpTime();
} }
void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) { void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
@ -728,7 +728,7 @@ void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
return; return;
uint32_t delay_ntp = rti.delay_since_last_rr; uint32_t delay_ntp = rti.delay_since_last_rr;
uint32_t now_ntp = CompactNtp(NtpTime(*clock_)); uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp; uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp); xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);

View File

@ -271,7 +271,7 @@ TEST_F(RtcpReceiverTest, InjectSrPacketCalculatesRTT) {
EXPECT_EQ( EXPECT_EQ(
-1, rtcp_receiver_.RTT(kSenderSsrc, &rtt_ms, nullptr, nullptr, nullptr)); -1, rtcp_receiver_.RTT(kSenderSsrc, &rtt_ms, nullptr, nullptr, nullptr));
uint32_t sent_ntp = CompactNtp(NtpTime(system_clock_)); uint32_t sent_ntp = CompactNtp(system_clock_.CurrentNtpTime());
system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs); system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
rtcp::SenderReport sr; rtcp::SenderReport sr;
@ -301,7 +301,7 @@ TEST_F(RtcpReceiverTest, InjectSrPacketCalculatesNegativeRTTAsOne) {
EXPECT_EQ( EXPECT_EQ(
-1, rtcp_receiver_.RTT(kSenderSsrc, &rtt_ms, nullptr, nullptr, nullptr)); -1, rtcp_receiver_.RTT(kSenderSsrc, &rtt_ms, nullptr, nullptr, nullptr));
uint32_t sent_ntp = CompactNtp(NtpTime(system_clock_)); uint32_t sent_ntp = CompactNtp(system_clock_.CurrentNtpTime());
system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs); system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
rtcp::SenderReport sr; rtcp::SenderReport sr;
@ -774,7 +774,7 @@ TEST_F(RtcpReceiverTest, InjectExtendedReportsDlrrPacketWithSubBlock) {
InjectRtcpPacket(xr); InjectRtcpPacket(xr);
uint32_t compact_ntp_now = CompactNtp(NtpTime(system_clock_)); uint32_t compact_ntp_now = CompactNtp(system_clock_.CurrentNtpTime());
EXPECT_TRUE(rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)); EXPECT_TRUE(rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms));
uint32_t rtt_ntp = compact_ntp_now - kDelay - kLastRR; uint32_t rtt_ntp = compact_ntp_now - kDelay - kLastRR;
EXPECT_NEAR(CompactNtpRttToMs(rtt_ntp), rtt_ms, 1); EXPECT_NEAR(CompactNtpRttToMs(rtt_ntp), rtt_ms, 1);
@ -793,7 +793,7 @@ TEST_F(RtcpReceiverTest, InjectExtendedReportsDlrrPacketWithMultipleSubBlocks) {
InjectRtcpPacket(xr); InjectRtcpPacket(xr);
uint32_t compact_ntp_now = CompactNtp(NtpTime(system_clock_)); uint32_t compact_ntp_now = CompactNtp(system_clock_.CurrentNtpTime());
int64_t rtt_ms = 0; int64_t rtt_ms = 0;
EXPECT_TRUE(rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms)); EXPECT_TRUE(rtcp_receiver_.GetAndResetXrRrRtt(&rtt_ms));
uint32_t rtt_ntp = compact_ntp_now - kDelay - kLastRR; uint32_t rtt_ntp = compact_ntp_now - kDelay - kLastRR;
@ -859,7 +859,7 @@ TEST_F(RtcpReceiverTest, RttCalculatedAfterExtendedReportsDlrr) {
const uint32_t kDelayNtp = rand.Rand(0, 0x7fffffff); const uint32_t kDelayNtp = rand.Rand(0, 0x7fffffff);
const int64_t kDelayMs = CompactNtpRttToMs(kDelayNtp); const int64_t kDelayMs = CompactNtpRttToMs(kDelayNtp);
rtcp_receiver_.SetRtcpXrRrtrStatus(true); rtcp_receiver_.SetRtcpXrRrtrStatus(true);
NtpTime now(system_clock_); NtpTime now = system_clock_.CurrentNtpTime();
uint32_t sent_ntp = CompactNtp(now); uint32_t sent_ntp = CompactNtp(now);
system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs); system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
@ -879,7 +879,7 @@ TEST_F(RtcpReceiverTest, XrDlrrCalculatesNegativeRttAsOne) {
const int64_t kRttMs = rand.Rand(-3600 * 1000, -1); const int64_t kRttMs = rand.Rand(-3600 * 1000, -1);
const uint32_t kDelayNtp = rand.Rand(0, 0x7fffffff); const uint32_t kDelayNtp = rand.Rand(0, 0x7fffffff);
const int64_t kDelayMs = CompactNtpRttToMs(kDelayNtp); const int64_t kDelayMs = CompactNtpRttToMs(kDelayNtp);
NtpTime now(system_clock_); NtpTime now = system_clock_.CurrentNtpTime();
uint32_t sent_ntp = CompactNtp(now); uint32_t sent_ntp = CompactNtp(now);
system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs); system_clock_.AdvanceTimeMilliseconds(kRttMs + kDelayMs);
rtcp_receiver_.SetRtcpXrRrtrStatus(true); rtcp_receiver_.SetRtcpXrRrtrStatus(true);

View File

@ -779,7 +779,7 @@ int32_t RTCPSender::SendCompoundRTCP(
// We need to send our NTP even if we haven't received any reports. // We need to send our NTP even if we haven't received any reports.
RtcpContext context(feedback_state, nack_size, nack_list, pictureID, RtcpContext context(feedback_state, nack_size, nack_list, pictureID,
NtpTime(*clock_)); clock_->CurrentNtpTime());
PrepareReport(feedback_state); PrepareReport(feedback_state);

View File

@ -22,11 +22,6 @@ inline uint32_t NtpToRtp(NtpTime ntp, uint32_t freq) {
uint32_t tmp = (static_cast<uint64_t>(ntp.fractions()) * freq) >> 32; uint32_t tmp = (static_cast<uint64_t>(ntp.fractions()) * freq) >> 32;
return ntp.seconds() * freq + tmp; return ntp.seconds() * freq + tmp;
} }
// Return the current RTP timestamp from the NTP timestamp
// returned by the specified clock.
inline uint32_t CurrentRtp(const Clock& clock, uint32_t freq) {
return NtpToRtp(NtpTime(clock), freq);
}
// Helper function for compact ntp representation: // Helper function for compact ntp representation:
// RFC 3550, Section 4. Time Format. // RFC 3550, Section 4. Time Format.

View File

@ -13,6 +13,7 @@
#include <memory> #include <memory>
#include "webrtc/system_wrappers/include/ntp_time.h"
#include "webrtc/system_wrappers/include/rw_lock_wrapper.h" #include "webrtc/system_wrappers/include/rw_lock_wrapper.h"
#include "webrtc/typedefs.h" #include "webrtc/typedefs.h"
@ -43,8 +44,14 @@ class Clock {
// Retrieve an NTP absolute timestamp in milliseconds. // Retrieve an NTP absolute timestamp in milliseconds.
virtual int64_t CurrentNtpInMilliseconds() const = 0; virtual int64_t CurrentNtpInMilliseconds() const = 0;
// TODO(danilchap): Make pure virtual once implemented in derived classed
// replacing CurrentNtp function.
virtual NtpTime CurrentNtpTime() const;
// Converts an NTP timestamp to a millisecond timestamp. // Converts an NTP timestamp to a millisecond timestamp.
static int64_t NtpToMs(uint32_t seconds, uint32_t fractions); static int64_t NtpToMs(uint32_t seconds, uint32_t fractions) {
return NtpTime(seconds, fractions).ToMs();
}
// Returns an instance of the real-time system clock implementation. // Returns an instance of the real-time system clock implementation.
static Clock* GetRealTimeClock(); static Clock* GetRealTimeClock();

View File

@ -12,25 +12,17 @@
#include <stdint.h> #include <stdint.h>
#include "webrtc/system_wrappers/include/clock.h"
namespace webrtc { namespace webrtc {
class NtpTime { class NtpTime {
public: public:
NtpTime() : seconds_(0), fractions_(0) {} NtpTime() : seconds_(0), fractions_(0) {}
explicit NtpTime(const Clock& clock) {
clock.CurrentNtp(seconds_, fractions_);
}
NtpTime(uint32_t seconds, uint32_t fractions) NtpTime(uint32_t seconds, uint32_t fractions)
: seconds_(seconds), fractions_(fractions) {} : seconds_(seconds), fractions_(fractions) {}
NtpTime(const NtpTime&) = default; NtpTime(const NtpTime&) = default;
NtpTime& operator=(const NtpTime&) = default; NtpTime& operator=(const NtpTime&) = default;
void SetCurrent(const Clock& clock) {
clock.CurrentNtp(seconds_, fractions_);
}
void Set(uint32_t seconds, uint32_t fractions) { void Set(uint32_t seconds, uint32_t fractions) {
seconds_ = seconds; seconds_ = seconds;
fractions_ = fractions; fractions_ = fractions;
@ -40,8 +32,12 @@ class NtpTime {
fractions_ = 0; fractions_ = 0;
} }
int64_t ToMs() const { return Clock::NtpToMs(seconds_, fractions_); } int64_t ToMs() const {
static constexpr double kNtpFracPerMs = 4.294967296E6; // 2^32 / 1000.
const double frac_ms = static_cast<double>(fractions_) / kNtpFracPerMs;
return 1000 * static_cast<int64_t>(seconds_) +
static_cast<int64_t>(frac_ms + 0.5);
}
// NTP standard (RFC1305, section 3.1) explicitly state value 0/0 is invalid. // NTP standard (RFC1305, section 3.1) explicitly state value 0/0 is invalid.
bool Valid() const { return !(seconds_ == 0 && fractions_ == 0); } bool Valid() const { return !(seconds_ == 0 && fractions_ == 0); }

View File

@ -25,12 +25,11 @@
namespace webrtc { namespace webrtc {
const double kNtpFracPerMs = 4.294967296E6; NtpTime Clock::CurrentNtpTime() const {
uint32_t seconds;
int64_t Clock::NtpToMs(uint32_t ntp_secs, uint32_t ntp_frac) { uint32_t fractions;
const double ntp_frac_ms = static_cast<double>(ntp_frac) / kNtpFracPerMs; CurrentNtp(seconds, fractions);
return 1000 * static_cast<int64_t>(ntp_secs) + return NtpTime(seconds, fractions);
static_cast<int64_t>(ntp_frac_ms + 0.5);
} }
class RealTimeClock : public Clock { class RealTimeClock : public Clock {

View File

@ -8,6 +8,7 @@
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include "webrtc/system_wrappers/include/clock.h"
#include "webrtc/system_wrappers/include/ntp_time.h" #include "webrtc/system_wrappers/include/ntp_time.h"
#include "webrtc/test/gtest.h" #include "webrtc/test/gtest.h"
@ -45,21 +46,10 @@ TEST(NtpTimeTest, SetIsSameAs2ParameterConstructor) {
EXPECT_EQ(ntp1, ntp2); EXPECT_EQ(ntp1, ntp2);
} }
TEST(NtpTimeTest, SetCurrentIsSameAs1ParameterConstructor) {
SimulatedClock clock(0x0123456789abcdef);
NtpTime ntp1(clock);
NtpTime ntp2;
EXPECT_NE(ntp1, ntp2);
ntp2.SetCurrent(clock);
EXPECT_EQ(ntp1, ntp2);
}
TEST(NtpTimeTest, ToMsMeansToNtpMilliseconds) { TEST(NtpTimeTest, ToMsMeansToNtpMilliseconds) {
SimulatedClock clock(0x123456789abc); SimulatedClock clock(0x123456789abc);
NtpTime ntp(clock); NtpTime ntp = clock.CurrentNtpTime();
EXPECT_EQ(ntp.ToMs(), Clock::NtpToMs(ntp.seconds(), ntp.fractions())); EXPECT_EQ(ntp.ToMs(), Clock::NtpToMs(ntp.seconds(), ntp.fractions()));
EXPECT_EQ(ntp.ToMs(), clock.CurrentNtpInMilliseconds()); EXPECT_EQ(ntp.ToMs(), clock.CurrentNtpInMilliseconds());
} }