diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc index b638f35dce..6d7d07d027 100644 --- a/webrtc/api/peerconnection.cc +++ b/webrtc/api/peerconnection.cc @@ -40,7 +40,6 @@ #include "webrtc/logging/rtc_event_log/rtc_event_log.h" #include "webrtc/media/sctp/sctpdataengine.h" #include "webrtc/pc/channelmanager.h" -#include "webrtc/system_wrappers/include/clock.h" #include "webrtc/system_wrappers/include/field_trial.h" namespace { @@ -590,7 +589,7 @@ PeerConnection::PeerConnection(PeerConnectionFactory* factory) signaling_state_(kStable), ice_connection_state_(kIceConnectionNew), ice_gathering_state_(kIceGatheringNew), - event_log_(RtcEventLog::Create(webrtc::Clock::GetRealTimeClock())), + event_log_(RtcEventLog::Create()), rtcp_cname_(GenerateRtcpCname()), local_streams_(StreamCollection::Create()), remote_streams_(StreamCollection::Create()) {} diff --git a/webrtc/base/fakeclock.h b/webrtc/base/fakeclock.h index 7fa101558f..eb78cf864d 100644 --- a/webrtc/base/fakeclock.h +++ b/webrtc/base/fakeclock.h @@ -32,9 +32,14 @@ class FakeClock : public ClockInterface { // Should only be used to set a time in the future. void SetTimeNanos(int64_t nanos); + void SetTimeMicros(int64_t micros) { + SetTimeNanos(kNumNanosecsPerMicrosec * micros); + } void AdvanceTime(TimeDelta delta); - + void AdvanceTimeMicros(int64_t micros) { + AdvanceTime(rtc::TimeDelta::FromMicroseconds(micros)); + } private: CriticalSection lock_; int64_t time_ GUARDED_BY(lock_) = 0; diff --git a/webrtc/logging/rtc_event_log/rtc_event_log.cc b/webrtc/logging/rtc_event_log/rtc_event_log.cc index b2d3b6eaa6..21ca5e127d 100644 --- a/webrtc/logging/rtc_event_log/rtc_event_log.cc +++ b/webrtc/logging/rtc_event_log/rtc_event_log.cc @@ -18,6 +18,7 @@ #include "webrtc/base/event.h" #include "webrtc/base/swap_queue.h" #include "webrtc/base/thread_checker.h" +#include "webrtc/base/timeutils.h" #include "webrtc/call/call.h" #include "webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h" #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" @@ -32,7 +33,6 @@ #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rtpfb.h" #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h" #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h" -#include "webrtc/system_wrappers/include/clock.h" #include "webrtc/system_wrappers/include/file_wrapper.h" #include "webrtc/system_wrappers/include/logging.h" @@ -51,7 +51,7 @@ namespace webrtc { class RtcEventLogImpl final : public RtcEventLog { public: - explicit RtcEventLogImpl(const Clock* clock); + RtcEventLogImpl(); ~RtcEventLogImpl() override; bool StartLogging(const std::string& file_name, @@ -87,12 +87,10 @@ class RtcEventLogImpl final : public RtcEventLog { // Message queue for passing events to the logging thread. SwapQueue > event_queue_; - const Clock* const clock_; - RtcEventLogHelperThread helper_thread_; rtc::ThreadChecker thread_checker_; - RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogImpl); + RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl); }; namespace { @@ -136,12 +134,11 @@ static const int kControlMessagesPerSecond = 10; } // namespace // RtcEventLogImpl member functions. -RtcEventLogImpl::RtcEventLogImpl(const Clock* clock) +RtcEventLogImpl::RtcEventLogImpl() // Allocate buffers for roughly one second of history. : message_queue_(kControlMessagesPerSecond), event_queue_(kEventsPerSecond), - clock_(clock), - helper_thread_(&message_queue_, &event_queue_, clock), + helper_thread_(&message_queue_, &event_queue_), thread_checker_() { thread_checker_.DetachFromThread(); } @@ -159,7 +156,7 @@ bool RtcEventLogImpl::StartLogging(const std::string& file_name, message.max_size_bytes = max_size_bytes <= 0 ? std::numeric_limits::max() : max_size_bytes; - message.start_time = clock_->TimeInMicroseconds(); + message.start_time = rtc::TimeMicros(); message.stop_time = std::numeric_limits::max(); message.file.reset(FileWrapper::Create()); if (!message.file->OpenFile(file_name.c_str(), false)) { @@ -183,7 +180,7 @@ bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, message.max_size_bytes = max_size_bytes <= 0 ? std::numeric_limits::max() : max_size_bytes; - message.start_time = clock_->TimeInMicroseconds(); + message.start_time = rtc::TimeMicros(); message.stop_time = std::numeric_limits::max(); message.file.reset(FileWrapper::Create()); FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); @@ -213,7 +210,7 @@ void RtcEventLogImpl::StopLogging() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); RtcEventLogHelperThread::ControlMessage message; message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE; - message.stop_time = clock_->TimeInMicroseconds(); + message.stop_time = rtc::TimeMicros(); while (!message_queue_.Insert(&message)) { // TODO(terelius): We would like to have a blocking Insert function in the // SwapQueue, but for the time being we will just clear any previous @@ -232,7 +229,7 @@ void RtcEventLogImpl::StopLogging() { void RtcEventLogImpl::LogVideoReceiveStreamConfig( const VideoReceiveStream::Config& config) { std::unique_ptr event(new rtclog::Event()); - event->set_timestamp_us(clock_->TimeInMicroseconds()); + event->set_timestamp_us(rtc::TimeMicros()); event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); rtclog::VideoReceiveConfig* receiver_config = @@ -268,7 +265,7 @@ void RtcEventLogImpl::LogVideoReceiveStreamConfig( void RtcEventLogImpl::LogVideoSendStreamConfig( const VideoSendStream::Config& config) { std::unique_ptr event(new rtclog::Event()); - event->set_timestamp_us(clock_->TimeInMicroseconds()); + event->set_timestamp_us(rtc::TimeMicros()); event->set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); rtclog::VideoSendConfig* sender_config = event->mutable_video_sender_config(); @@ -298,7 +295,7 @@ void RtcEventLogImpl::LogVideoSendStreamConfig( void RtcEventLogImpl::LogAudioReceiveStreamConfig( const AudioReceiveStream::Config& config) { std::unique_ptr event(new rtclog::Event()); - event->set_timestamp_us(clock_->TimeInMicroseconds()); + event->set_timestamp_us(rtc::TimeMicros()); event->set_type(rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT); rtclog::AudioReceiveConfig* receiver_config = @@ -318,7 +315,7 @@ void RtcEventLogImpl::LogAudioReceiveStreamConfig( void RtcEventLogImpl::LogAudioSendStreamConfig( const AudioSendStream::Config& config) { std::unique_ptr event(new rtclog::Event()); - event->set_timestamp_us(clock_->TimeInMicroseconds()); + event->set_timestamp_us(rtc::TimeMicros()); event->set_type(rtclog::Event::AUDIO_SENDER_CONFIG_EVENT); rtclog::AudioSendConfig* sender_config = event->mutable_audio_sender_config(); @@ -356,7 +353,7 @@ void RtcEventLogImpl::LogRtpHeader(PacketDirection direction, } std::unique_ptr rtp_event(new rtclog::Event()); - rtp_event->set_timestamp_us(clock_->TimeInMicroseconds()); + rtp_event->set_timestamp_us(rtc::TimeMicros()); rtp_event->set_type(rtclog::Event::RTP_EVENT); rtp_event->mutable_rtp_packet()->set_incoming(direction == kIncomingPacket); rtp_event->mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); @@ -370,7 +367,7 @@ void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, const uint8_t* packet, size_t length) { std::unique_ptr rtcp_event(new rtclog::Event()); - rtcp_event->set_timestamp_us(clock_->TimeInMicroseconds()); + rtcp_event->set_timestamp_us(rtc::TimeMicros()); rtcp_event->set_type(rtclog::Event::RTCP_EVENT); rtcp_event->mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket); rtcp_event->mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); @@ -417,7 +414,7 @@ void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { std::unique_ptr event(new rtclog::Event()); - event->set_timestamp_us(clock_->TimeInMicroseconds()); + event->set_timestamp_us(rtc::TimeMicros()); event->set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT); auto playout_event = event->mutable_audio_playout_event(); playout_event->set_local_ssrc(ssrc); @@ -428,7 +425,7 @@ void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate, uint8_t fraction_loss, int32_t total_packets) { std::unique_ptr event(new rtclog::Event()); - event->set_timestamp_us(clock_->TimeInMicroseconds()); + event->set_timestamp_us(rtc::TimeMicros()); event->set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT); auto bwe_event = event->mutable_bwe_packet_loss_event(); bwe_event->set_bitrate(bitrate); @@ -472,9 +469,9 @@ bool RtcEventLogNullImpl::StartLogging(rtc::PlatformFile platform_file, } // RtcEventLog member functions. -std::unique_ptr RtcEventLog::Create(const Clock* clock) { +std::unique_ptr RtcEventLog::Create() { #ifdef ENABLE_RTC_EVENT_LOG - return std::unique_ptr(new RtcEventLogImpl(clock)); + return std::unique_ptr(new RtcEventLogImpl()); #else return std::unique_ptr(new RtcEventLogNullImpl()); #endif // ENABLE_RTC_EVENT_LOG diff --git a/webrtc/logging/rtc_event_log/rtc_event_log.h b/webrtc/logging/rtc_event_log/rtc_event_log.h index 0800a43299..ccf60944e2 100644 --- a/webrtc/logging/rtc_event_log/rtc_event_log.h +++ b/webrtc/logging/rtc_event_log/rtc_event_log.h @@ -40,7 +40,13 @@ class RtcEventLog { virtual ~RtcEventLog() {} // Factory method to create an RtcEventLog object. - static std::unique_ptr Create(const Clock* clock); + static std::unique_ptr Create(); + // TODO(nisse): webrtc::Clock is deprecated. Delete this method and + // above forward declaration of Clock when + // webrtc/system_wrappers/include/clock.h is deleted. + static std::unique_ptr Create(const Clock* clock) { + return Create(); + } // Create an RtcEventLog object that does nothing. static std::unique_ptr CreateNull(); diff --git a/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.cc b/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.cc index 4d55da3542..e7edd9a93c 100644 --- a/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.cc +++ b/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.cc @@ -13,6 +13,7 @@ #include #include "webrtc/base/checks.h" +#include "webrtc/base/timeutils.h" #include "webrtc/system_wrappers/include/logging.h" #ifdef ENABLE_RTC_EVENT_LOG @@ -34,8 +35,7 @@ bool IsConfigEvent(const rtclog::Event& event) { // RtcEventLogImpl member functions. RtcEventLogHelperThread::RtcEventLogHelperThread( SwapQueue* message_queue, - SwapQueue>* event_queue, - const Clock* const clock) + SwapQueue>* event_queue) : message_queue_(message_queue), event_queue_(event_queue), history_(kEventsInHistory), @@ -51,18 +51,16 @@ RtcEventLogHelperThread::RtcEventLogHelperThread( output_string_(), wake_periodically_(false, false), wake_from_hibernation_(false, false), - file_finished_(false, false), - clock_(clock) { + file_finished_(false, false) { RTC_DCHECK(message_queue_); RTC_DCHECK(event_queue_); - RTC_DCHECK(clock_); thread_.Start(); } RtcEventLogHelperThread::~RtcEventLogHelperThread() { ControlMessage message; message.message_type = ControlMessage::TERMINATE_THREAD; - message.stop_time = clock_->TimeInMicroseconds(); + message.stop_time = rtc::TimeMicros(); while (!message_queue_->Insert(&message)) { // We can't destroy the event log until we have stopped the thread, // so clear the message queue and try again. Note that if we clear @@ -112,7 +110,7 @@ bool RtcEventLogHelperThread::LogToMemory() { // Process each event earlier than the current time and append it to the // appropriate history_. - int64_t current_time = clock_->TimeInMicroseconds(); + int64_t current_time = rtc::TimeMicros(); if (!has_recent_event_) { has_recent_event_ = event_queue_->Remove(&most_recent_event_); } @@ -180,7 +178,7 @@ bool RtcEventLogHelperThread::LogToFile() { // Append each event older than both the current time and the stop time // to the output_string_. - int64_t current_time = clock_->TimeInMicroseconds(); + int64_t current_time = rtc::TimeMicros(); int64_t time_limit = std::min(current_time, stop_time_); if (!has_recent_event_) { has_recent_event_ = event_queue_->Remove(&most_recent_event_); @@ -228,7 +226,7 @@ void RtcEventLogHelperThread::StopLogFile() { // or because we have reached the log file size limit. Therefore, use the // current time if we have not reached the time limit. end_event.set_timestamp_us( - std::min(stop_time_, clock_->TimeInMicroseconds())); + std::min(stop_time_, rtc::TimeMicros())); end_event.set_type(rtclog::Event::LOG_END); AppendEventToString(&end_event); diff --git a/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h b/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h index bda27f2f4f..7d41f60db2 100644 --- a/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h +++ b/webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h @@ -23,7 +23,6 @@ #include "webrtc/base/platform_thread.h" #include "webrtc/base/swap_queue.h" #include "webrtc/logging/rtc_event_log/ringbuffer.h" -#include "webrtc/system_wrappers/include/clock.h" #include "webrtc/system_wrappers/include/file_wrapper.h" #ifdef ENABLE_RTC_EVENT_LOG @@ -69,8 +68,7 @@ class RtcEventLogHelperThread final { RtcEventLogHelperThread( SwapQueue* message_queue, - SwapQueue>* event_queue, - const Clock* const clock); + SwapQueue>* event_queue); ~RtcEventLogHelperThread(); // This function MUST be called once a STOP_FILE message is added to the @@ -120,8 +118,6 @@ class RtcEventLogHelperThread final { rtc::Event wake_from_hibernation_; rtc::Event file_finished_; - const Clock* const clock_; - RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogHelperThread); }; diff --git a/webrtc/logging/rtc_event_log/rtc_event_log_unittest.cc b/webrtc/logging/rtc_event_log/rtc_event_log_unittest.cc index 258d0fd687..1540466785 100644 --- a/webrtc/logging/rtc_event_log/rtc_event_log_unittest.cc +++ b/webrtc/logging/rtc_event_log/rtc_event_log_unittest.cc @@ -16,6 +16,7 @@ #include "webrtc/base/buffer.h" #include "webrtc/base/checks.h" +#include "webrtc/base/fakeclock.h" #include "webrtc/base/random.h" #include "webrtc/call/call.h" #include "webrtc/logging/rtc_event_log/rtc_event_log.h" @@ -26,7 +27,6 @@ #include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h" #include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h" #include "webrtc/modules/rtp_rtcp/source/rtp_packet_to_send.h" -#include "webrtc/system_wrappers/include/clock.h" #include "webrtc/test/gtest.h" #include "webrtc/test/testsupport/fileutils.h" @@ -290,12 +290,13 @@ void LogSessionAndReadBack(size_t rtp_count, // When log_dumper goes out of scope, it causes the log file to be flushed // to disk. { - SimulatedClock fake_clock(prng.Rand()); - std::unique_ptr log_dumper(RtcEventLog::Create(&fake_clock)); + rtc::ScopedFakeClock fake_clock; + fake_clock.SetTimeMicros(prng.Rand()); + std::unique_ptr log_dumper(RtcEventLog::Create()); log_dumper->LogVideoReceiveStreamConfig(receiver_config); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); log_dumper->LogVideoSendStreamConfig(sender_config); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); size_t rtcp_index = 1; size_t playout_index = 1; size_t bwe_loss_index = 1; @@ -304,7 +305,7 @@ void LogSessionAndReadBack(size_t rtp_count, (i % 2 == 0) ? kIncomingPacket : kOutgoingPacket, (i % 3 == 0) ? MediaType::AUDIO : MediaType::VIDEO, rtp_packets[i - 1].data(), rtp_packets[i - 1].size()); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); if (i * rtcp_count >= rtcp_index * rtp_count) { log_dumper->LogRtcpPacket( (rtcp_index % 2 == 0) ? kIncomingPacket : kOutgoingPacket, @@ -312,23 +313,23 @@ void LogSessionAndReadBack(size_t rtp_count, rtcp_packets[rtcp_index - 1].data(), rtcp_packets[rtcp_index - 1].size()); rtcp_index++; - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); } if (i * playout_count >= playout_index * rtp_count) { log_dumper->LogAudioPlayout(playout_ssrcs[playout_index - 1]); playout_index++; - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); } if (i * bwe_loss_count >= bwe_loss_index * rtp_count) { log_dumper->LogBwePacketLossEvent( bwe_loss_updates[bwe_loss_index - 1].first, bwe_loss_updates[bwe_loss_index - 1].second, i); bwe_loss_index++; - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); } if (i == rtp_count / 2) { log_dumper->StartLogging(temp_filename, 10000000); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); } } log_dumper->StopLogging(); @@ -446,19 +447,20 @@ TEST(RtcEventLogTest, LogEventAndReadBack) { test::OutputPath() + test_info->test_case_name() + test_info->name(); // Add RTP, start logging, add RTCP and then stop logging - SimulatedClock fake_clock(prng.Rand()); - std::unique_ptr log_dumper(RtcEventLog::Create(&fake_clock)); + rtc::ScopedFakeClock fake_clock; + fake_clock.SetTimeMicros(prng.Rand()); + std::unique_ptr log_dumper(RtcEventLog::Create()); log_dumper->LogRtpHeader(kIncomingPacket, MediaType::VIDEO, rtp_packet.data(), rtp_packet.size()); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); log_dumper->StartLogging(temp_filename, 10000000); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); log_dumper->LogRtcpPacket(kOutgoingPacket, MediaType::VIDEO, rtcp_packet.data(), rtcp_packet.size()); - fake_clock.AdvanceTimeMicroseconds(prng.Rand(1, 1000)); + fake_clock.AdvanceTimeMicros(prng.Rand(1, 1000)); log_dumper->StopLogging(); @@ -507,8 +509,9 @@ class ConfigReadWriteTest { GenerateConfig(extensions_bitvector); // Log a single config event and stop logging. - SimulatedClock fake_clock(prng.Rand()); - std::unique_ptr log_dumper(RtcEventLog::Create(&fake_clock)); + rtc::ScopedFakeClock fake_clock; + fake_clock.SetTimeMicros(prng.Rand()); + std::unique_ptr log_dumper(RtcEventLog::Create()); log_dumper->StartLogging(temp_filename, 10000000); LogConfig(log_dumper.get());