From cdf37a929336e2ca1e0bcdd5e858ffc75b2b317e Mon Sep 17 00:00:00 2001 From: nisse Date: Tue, 13 Sep 2016 23:41:47 -0700 Subject: [PATCH] Delete Timing class, timing.h, and update all users. BUG=webrtc:6324 Review-Url: https://codereview.webrtc.org/2290203002 Cr-Commit-Position: refs/heads/master@{#14203} --- webrtc/api/statscollector.cc | 5 +- webrtc/base/BUILD.gn | 3 - webrtc/base/base.gyp | 2 - webrtc/base/base_tests.gyp | 1 - webrtc/base/test/faketiming.h | 33 -------- webrtc/base/timeutils.cc | 18 ++++ webrtc/base/timeutils.h | 12 +++ webrtc/base/timing.cc | 54 ------------ webrtc/base/timing.h | 41 ---------- webrtc/media/base/rtpdataengine.cc | 16 ++-- webrtc/media/base/rtpdataengine.h | 18 +--- webrtc/media/base/rtpdataengine_unittest.cc | 91 +-------------------- webrtc/stats/rtcstatscollector.cc | 4 +- webrtc/stats/rtcstatscollector_unittest.cc | 7 +- 14 files changed, 46 insertions(+), 259 deletions(-) delete mode 100644 webrtc/base/test/faketiming.h delete mode 100644 webrtc/base/timing.cc delete mode 100644 webrtc/base/timing.h diff --git a/webrtc/api/statscollector.cc b/webrtc/api/statscollector.cc index ffe241568b..d21fc3009a 100644 --- a/webrtc/api/statscollector.cc +++ b/webrtc/api/statscollector.cc @@ -17,7 +17,6 @@ #include "webrtc/api/peerconnection.h" #include "webrtc/base/base64.h" #include "webrtc/base/checks.h" -#include "webrtc/base/timing.h" #include "webrtc/pc/channel.h" namespace webrtc { @@ -375,8 +374,10 @@ StatsCollector::~StatsCollector() { RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent()); } +// Wallclock time in ms. double StatsCollector::GetTimeNow() { - return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec; + return rtc::TimeUTCMicros() / + static_cast(rtc::kNumMicrosecsPerMillisec); } // Adds a MediaStream with tracks that can be used as a |selector| in a call diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn index 2b1be29058..59cdf4c11f 100644 --- a/webrtc/base/BUILD.gn +++ b/webrtc/base/BUILD.gn @@ -420,8 +420,6 @@ rtc_static_library("rtc_base") { "taskrunner.h", "thread.cc", "thread.h", - "timing.cc", - "timing.h", "urlencode.cc", "urlencode.h", "worker.cc", @@ -715,7 +713,6 @@ if (rtc_include_tests) { "fakesslidentity.h", "faketaskrunner.h", "gunit.h", - "test/faketiming.h", "testbase64.h", "testechoserver.h", "testutils.h", diff --git a/webrtc/base/base.gyp b/webrtc/base/base.gyp index ed8cd59362..9fe2c6c2e0 100644 --- a/webrtc/base/base.gyp +++ b/webrtc/base/base.gyp @@ -363,8 +363,6 @@ 'taskrunner.h', 'thread.cc', 'thread.h', - 'timing.cc', - 'timing.h', 'urlencode.cc', 'urlencode.h', 'worker.cc', diff --git a/webrtc/base/base_tests.gyp b/webrtc/base/base_tests.gyp index 6940a66696..e1d5bc0496 100644 --- a/webrtc/base/base_tests.gyp +++ b/webrtc/base/base_tests.gyp @@ -21,7 +21,6 @@ 'fakesslidentity.h', 'faketaskrunner.h', 'gunit.h', - 'test/faketiming.h', 'testbase64.h', 'testechoserver.h', 'testutils.h', diff --git a/webrtc/base/test/faketiming.h b/webrtc/base/test/faketiming.h deleted file mode 100644 index 388a535d9c..0000000000 --- a/webrtc/base/test/faketiming.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef WEBRTC_BASE_TEST_FAKETIMING_H_ -#define WEBRTC_BASE_TEST_FAKETIMING_H_ - -#include "webrtc/base/timing.h" -#include "webrtc/base/timeutils.h" - -namespace rtc { - -class FakeTiming : public Timing { - public: - // Starts at Jan 1, 1983 (UTC). - FakeTiming() : now_(410227200.0) {} - double TimerNow() override { return now_; } - void AdvanceTimeSecs(double seconds) { now_ += seconds; } - void AdvanceTimeMillisecs(double ms) { now_ += (ms / kNumMillisecsPerSec); } - - private: - double now_; -}; - -} // namespace rtc - -#endif // WEBRTC_BASE_TEST_FAKETIMING_H_ diff --git a/webrtc/base/timeutils.cc b/webrtc/base/timeutils.cc index 1be368eb58..658d0796ad 100644 --- a/webrtc/base/timeutils.cc +++ b/webrtc/base/timeutils.cc @@ -23,6 +23,7 @@ #endif #include #include +#include #endif #include "webrtc/base/checks.h" @@ -185,4 +186,21 @@ int64_t TmToSeconds(const std::tm& tm) { (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; } +int64_t TimeUTCMicros() { +#if defined(WEBRTC_POSIX) + struct timeval time; + gettimeofday(&time, NULL); + // Convert from second (1.0) and microsecond (1e-6). + return (static_cast(time.tv_sec) * rtc::kNumMicrosecsPerSec + + time.tv_usec); + +#elif defined(WEBRTC_WIN) + struct _timeb time; + _ftime(&time); + // Convert from second (1.0) and milliseconds (1e-3). + return (static_cast(time.time) * rtc::kNumMicrosecsPerSec + + static_cast(time.millitm) * rtc::kNumMicrosecsPerMillisec); +#endif +} + } // namespace rtc diff --git a/webrtc/base/timeutils.h b/webrtc/base/timeutils.h index 25ef52105c..cc6deb47a9 100644 --- a/webrtc/base/timeutils.h +++ b/webrtc/base/timeutils.h @@ -108,6 +108,18 @@ class TimestampWrapAroundHandler { // is still 32 bits on many systems. int64_t TmToSeconds(const std::tm& tm); +// Return the number of microseconds since January 1, 1970, UTC. +// Useful mainly when producing logs to be correlated with other +// devices, and when the devices in question all have properly +// synchronized clocks. +// +// Note that this function obeys the system's idea about what the time +// is. It is not guaranteed to be monotonic; it will jump in case the +// system time is changed, e.g., by some other process calling +// settimeofday. Always use rtc::TimeMicros(), not this function, for +// measuring time intervals and timeouts. +int64_t TimeUTCMicros(); + } // namespace rtc #endif // WEBRTC_BASE_TIMEUTILS_H_ diff --git a/webrtc/base/timing.cc b/webrtc/base/timing.cc deleted file mode 100644 index 7fdcf6e933..0000000000 --- a/webrtc/base/timing.cc +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2008 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "webrtc/base/timing.h" -#include "webrtc/base/timeutils.h" - -#if defined(WEBRTC_POSIX) -#include -#include -#include -#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) -#include -#include -#endif -#elif defined(WEBRTC_WIN) -#include -#endif - -namespace rtc { - -Timing::Timing() {} - -Timing::~Timing() {} - -// static -double Timing::WallTimeNow() { -#if defined(WEBRTC_POSIX) - struct timeval time; - gettimeofday(&time, NULL); - // Convert from second (1.0) and microsecond (1e-6). - return (static_cast(time.tv_sec) + - static_cast(time.tv_usec) * 1.0e-6); - -#elif defined(WEBRTC_WIN) - struct _timeb time; - _ftime(&time); - // Convert from second (1.0) and milliseconds (1e-3). - return (static_cast(time.time) + - static_cast(time.millitm) * 1.0e-3); -#endif -} - -double Timing::TimerNow() { - return (static_cast(TimeNanos()) / kNumNanosecsPerSec); -} - -} // namespace rtc diff --git a/webrtc/base/timing.h b/webrtc/base/timing.h deleted file mode 100644 index a73c57d986..0000000000 --- a/webrtc/base/timing.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2008 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef WEBRTC_BASE_TIMING_H_ -#define WEBRTC_BASE_TIMING_H_ - -namespace rtc { - -// TODO(deadbeef): Remove this and use ClockInterface instead. -class Timing { - public: - Timing(); - virtual ~Timing(); - - // WallTimeNow() returns the current wall-clock time in seconds, - // within 10 milliseconds resolution. - // WallTimeNow is static and does not require a timer_handle_ on Windows. - static double WallTimeNow(); - - // TimerNow() is like WallTimeNow(), but is monotonically - // increasing. It returns seconds in resolution of 10 microseconds - // or better. Although timer and wall-clock time have the same - // timing unit, they do not necessarily correlate because wall-clock - // time may be adjusted backwards, hence not monotonic. - // Made virtual so we can make a fake one. - // TODO(tommi): The only place we use this (virtual) is in - // rtpdata_engine_unittest.cc. See if it doesn't make more sense to change - // that contract or test than to modify this generic class. - virtual double TimerNow(); -}; - -} // namespace rtc - -#endif // WEBRTC_BASE_TIMING_H_ diff --git a/webrtc/media/base/rtpdataengine.cc b/webrtc/media/base/rtpdataengine.cc index 4b6647c649..99aa3b14a5 100644 --- a/webrtc/media/base/rtpdataengine.cc +++ b/webrtc/media/base/rtpdataengine.cc @@ -14,7 +14,6 @@ #include "webrtc/base/helpers.h" #include "webrtc/base/logging.h" #include "webrtc/base/ratelimiter.h" -#include "webrtc/base/timing.h" #include "webrtc/media/base/codec.h" #include "webrtc/media/base/mediaconstants.h" #include "webrtc/media/base/rtputils.h" @@ -37,7 +36,6 @@ static const size_t kMaxSrtpHmacOverhead = 16; RtpDataEngine::RtpDataEngine() { data_codecs_.push_back( DataCodec(kGoogleRtpDataCodecId, kGoogleRtpDataCodecName)); - SetTiming(new rtc::Timing()); } DataMediaChannel* RtpDataEngine::CreateChannel( @@ -45,7 +43,7 @@ DataMediaChannel* RtpDataEngine::CreateChannel( if (data_channel_type != DCT_RTP) { return NULL; } - return new RtpDataMediaChannel(timing_.get()); + return new RtpDataMediaChannel(); } bool FindCodecByName(const std::vector& codecs, @@ -60,18 +58,13 @@ bool FindCodecByName(const std::vector& codecs, return false; } -RtpDataMediaChannel::RtpDataMediaChannel(rtc::Timing* timing) { - Construct(timing); -} - RtpDataMediaChannel::RtpDataMediaChannel() { - Construct(NULL); + Construct(); } -void RtpDataMediaChannel::Construct(rtc::Timing* timing) { +void RtpDataMediaChannel::Construct() { sending_ = false; receiving_ = false; - timing_ = timing; send_limiter_.reset(new rtc::RateLimiter(kDataMaxBandwidth / 8, 1.0)); } @@ -313,7 +306,8 @@ bool RtpDataMediaChannel::SendData( return false; } - double now = timing_->TimerNow(); + double now = + rtc::TimeMicros() / static_cast(rtc::kNumMicrosecsPerSec); if (!send_limiter_->CanUse(packet_len, now)) { LOG(LS_VERBOSE) << "Dropped data packet of len=" << packet_len diff --git a/webrtc/media/base/rtpdataengine.h b/webrtc/media/base/rtpdataengine.h index 0181e65759..98f90be332 100644 --- a/webrtc/media/base/rtpdataengine.h +++ b/webrtc/media/base/rtpdataengine.h @@ -15,7 +15,6 @@ #include #include -#include "webrtc/base/timing.h" #include "webrtc/media/base/mediachannel.h" #include "webrtc/media/base/mediaconstants.h" #include "webrtc/media/base/mediaengine.h" @@ -34,14 +33,8 @@ class RtpDataEngine : public DataEngineInterface { return data_codecs_; } - // Mostly for testing with a fake clock. Ownership is passed in. - void SetTiming(rtc::Timing* timing) { - timing_.reset(timing); - } - private: std::vector data_codecs_; - std::unique_ptr timing_; }; // Keep track of sequence number and timestamp of an RTP stream. The @@ -68,17 +61,9 @@ class RtpClock { class RtpDataMediaChannel : public DataMediaChannel { public: - // Timing* Used for the RtpClock - explicit RtpDataMediaChannel(rtc::Timing* timing); - // Sets Timing == NULL, so you'll need to call set_timer() before - // using it. This is needed by FakeMediaEngine. RtpDataMediaChannel(); virtual ~RtpDataMediaChannel(); - void set_timing(rtc::Timing* timing) { - timing_ = timing; - } - virtual bool SetSendParameters(const DataSendParameters& params); virtual bool SetRecvParameters(const DataRecvParameters& params); virtual bool AddSendStream(const StreamParams& sp); @@ -104,14 +89,13 @@ class RtpDataMediaChannel : public DataMediaChannel { SendDataResult* result); private: - void Construct(rtc::Timing* timing); + void Construct(); bool SetMaxSendBandwidth(int bps); bool SetSendCodecs(const std::vector& codecs); bool SetRecvCodecs(const std::vector& codecs); bool sending_; bool receiving_; - rtc::Timing* timing_; std::vector send_codecs_; std::vector recv_codecs_; std::vector send_streams_; diff --git a/webrtc/media/base/rtpdataengine_unittest.cc b/webrtc/media/base/rtpdataengine_unittest.cc index b5cfd32551..35daee5aeb 100644 --- a/webrtc/media/base/rtpdataengine_unittest.cc +++ b/webrtc/media/base/rtpdataengine_unittest.cc @@ -15,28 +15,11 @@ #include "webrtc/base/gunit.h" #include "webrtc/base/helpers.h" #include "webrtc/base/ssladapter.h" -#include "webrtc/base/timing.h" #include "webrtc/media/base/fakenetworkinterface.h" #include "webrtc/media/base/mediaconstants.h" #include "webrtc/media/base/rtpdataengine.h" #include "webrtc/media/base/rtputils.h" -class FakeTiming : public rtc::Timing { - public: - FakeTiming() : now_(0.0) {} - - virtual double TimerNow() { - return now_; - } - - void set_now(double now) { - now_ = now; - } - - private: - double now_; -}; - class FakeDataReceiver : public sigslot::has_slots<> { public: FakeDataReceiver() : has_received_data_(false) {} @@ -69,18 +52,16 @@ class RtpDataMediaChannelTest : public testing::Test { virtual void SetUp() { // Seed needed for each test to satisfy expectations. iface_.reset(new cricket::FakeNetworkInterface()); - timing_ = new FakeTiming(); - dme_.reset(CreateEngine(timing_)); + dme_.reset(CreateEngine()); receiver_.reset(new FakeDataReceiver()); } void SetNow(double now) { - timing_->set_now(now); + clock_.SetTimeNanos(now * rtc::kNumNanosecsPerSec); } - cricket::RtpDataEngine* CreateEngine(FakeTiming* timing) { + cricket::RtpDataEngine* CreateEngine() { cricket::RtpDataEngine* dme = new cricket::RtpDataEngine(); - dme->SetTiming(timing); return dme; } @@ -143,8 +124,7 @@ class RtpDataMediaChannelTest : public testing::Test { private: std::unique_ptr dme_; - // Timing passed into dme_. Owned by dme_; - FakeTiming* timing_; + rtc::ScopedFakeClock clock_; std::unique_ptr iface_; std::unique_ptr receiver_; }; @@ -290,69 +270,6 @@ TEST_F(RtpDataMediaChannelTest, SendData) { EXPECT_EQ(header0.timestamp + 180000, header1.timestamp); } -TEST_F(RtpDataMediaChannelTest, SendDataMultipleClocks) { - // Timings owned by RtpDataEngines. - FakeTiming* timing1 = new FakeTiming(); - std::unique_ptr dme1(CreateEngine(timing1)); - std::unique_ptr dmc1( - CreateChannel(dme1.get())); - FakeTiming* timing2 = new FakeTiming(); - std::unique_ptr dme2(CreateEngine(timing2)); - std::unique_ptr dmc2( - CreateChannel(dme2.get())); - - ASSERT_TRUE(dmc1->SetSend(true)); - ASSERT_TRUE(dmc2->SetSend(true)); - - cricket::StreamParams stream1; - stream1.add_ssrc(41); - ASSERT_TRUE(dmc1->AddSendStream(stream1)); - cricket::StreamParams stream2; - stream2.add_ssrc(42); - ASSERT_TRUE(dmc2->AddSendStream(stream2)); - - cricket::DataCodec codec; - codec.id = 103; - codec.name = cricket::kGoogleRtpDataCodecName; - cricket::DataSendParameters parameters; - parameters.codecs.push_back(codec); - ASSERT_TRUE(dmc1->SetSendParameters(parameters)); - ASSERT_TRUE(dmc2->SetSendParameters(parameters)); - - cricket::SendDataParams params1; - params1.ssrc = 41; - cricket::SendDataParams params2; - params2.ssrc = 42; - - unsigned char data[] = "foo"; - rtc::CopyOnWriteBuffer payload(data, 3); - cricket::SendDataResult result; - - EXPECT_TRUE(dmc1->SendData(params1, payload, &result)); - EXPECT_TRUE(dmc2->SendData(params2, payload, &result)); - - // Should bump timestamp by 90000 because the clock rate is 90khz. - timing1->set_now(1); - // Should bump timestamp by 180000 because the clock rate is 90khz. - timing2->set_now(2); - - EXPECT_TRUE(dmc1->SendData(params1, payload, &result)); - EXPECT_TRUE(dmc2->SendData(params2, payload, &result)); - - ASSERT_TRUE(HasSentData(3)); - cricket::RtpHeader header1a = GetSentDataHeader(0); - cricket::RtpHeader header2a = GetSentDataHeader(1); - cricket::RtpHeader header1b = GetSentDataHeader(2); - cricket::RtpHeader header2b = GetSentDataHeader(3); - - EXPECT_EQ(static_cast(header1a.seq_num + 1), - static_cast(header1b.seq_num)); - EXPECT_EQ(header1a.timestamp + 90000, header1b.timestamp); - EXPECT_EQ(static_cast(header2a.seq_num + 1), - static_cast(header2b.seq_num)); - EXPECT_EQ(header2a.timestamp + 180000, header2b.timestamp); -} - TEST_F(RtpDataMediaChannelTest, SendDataRate) { std::unique_ptr dmc(CreateChannel()); diff --git a/webrtc/stats/rtcstatscollector.cc b/webrtc/stats/rtcstatscollector.cc index 1cdafd6c91..3ff41558b3 100644 --- a/webrtc/stats/rtcstatscollector.cc +++ b/webrtc/stats/rtcstatscollector.cc @@ -16,7 +16,6 @@ #include "webrtc/api/peerconnection.h" #include "webrtc/base/checks.h" -#include "webrtc/base/timing.h" namespace webrtc { @@ -63,8 +62,7 @@ void RTCStatsCollector::GetStatsReport( // "Now" using a system clock, relative to the UNIX epoch (Jan 1, 1970, // UTC), in microseconds. The system clock could be modified and is not // necessarily monotonically increasing. - int64_t timestamp_us = static_cast( - rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec); + int64_t timestamp_us = rtc::TimeUTCMicros(); num_pending_partial_reports_ = 3; partial_report_timestamp_us_ = cache_now_us; diff --git a/webrtc/stats/rtcstatscollector_unittest.cc b/webrtc/stats/rtcstatscollector_unittest.cc index 1ead77b28d..f3d232ff07 100644 --- a/webrtc/stats/rtcstatscollector_unittest.cc +++ b/webrtc/stats/rtcstatscollector_unittest.cc @@ -27,7 +27,6 @@ #include "webrtc/base/thread_checker.h" #include "webrtc/base/timedelta.h" #include "webrtc/base/timeutils.h" -#include "webrtc/base/timing.h" #include "webrtc/media/base/fakemediaengine.h" using testing::Return; @@ -314,11 +313,9 @@ TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { } TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { - int64_t before = static_cast( - rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec); + int64_t before = rtc::TimeUTCMicros(); rtc::scoped_refptr report = GetStatsReport(); - int64_t after = static_cast( - rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec); + int64_t after = rtc::TimeUTCMicros(); EXPECT_EQ(report->GetStatsOfType().size(), static_cast(1)) << "Expecting 1 RTCPeerConnectionStats."; const RTCStats* stats = report->Get("RTCPeerConnection");