diff --git a/p2p/base/connection.cc b/p2p/base/connection.cc index fe6042102c..8adfeb418d 100644 --- a/p2p/base/connection.cc +++ b/p2p/base/connection.cc @@ -1372,13 +1372,15 @@ int ProxyConnection::Send(const void* data, stats_.sent_total_packets++; int sent = port_->SendTo(data, size, remote_candidate_.address(), options, true); + int64_t now = rtc::TimeMillis(); if (sent <= 0) { RTC_DCHECK(sent < 0); error_ = port_->GetError(); stats_.sent_discarded_packets++; } else { - send_rate_tracker_.AddSamples(sent); + send_rate_tracker_.AddSamplesAtTime(now, sent); } + last_send_data_ = now; return sent; } diff --git a/p2p/base/connection.h b/p2p/base/connection.h index 88e930c216..d48137d01e 100644 --- a/p2p/base/connection.h +++ b/p2p/base/connection.h @@ -237,6 +237,8 @@ class Connection : public CandidatePairInterface, // that the remote peer has received, if it is indicated in the incoming // connectivity check from the peer. void HandlePiggybackCheckAcknowledgementIfAny(StunMessage* msg); + // Timestamp when data was last sent (or attempted to be sent). + int64_t last_send_data() const { return last_send_data_; } int64_t last_data_received() const { return last_data_received_; } // Debugging description of this connection @@ -378,6 +380,7 @@ class Connection : public CandidatePairInterface, ConnectionInfo stats_; rtc::RateTracker recv_rate_tracker_; rtc::RateTracker send_rate_tracker_; + int64_t last_send_data_ = 0; private: // Update the local candidate based on the mapped address attribute. diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc index efbf62e496..d4266bf0b6 100644 --- a/p2p/base/tcp_port.cc +++ b/p2p/base/tcp_port.cc @@ -403,12 +403,14 @@ int TCPConnection::Send(const void* data, static_cast(port_)->CopyPortInformationToPacketInfo( &modified_options.info_signaled_after_sent); int sent = socket_->Send(data, size, modified_options); + int64_t now = rtc::TimeMillis(); if (sent < 0) { stats_.sent_discarded_packets++; error_ = socket_->GetError(); } else { - send_rate_tracker_.AddSamples(sent); + send_rate_tracker_.AddSamplesAtTime(now, sent); } + last_send_data_ = now; return sent; } diff --git a/rtc_base/rate_tracker.cc b/rtc_base/rate_tracker.cc index 5c827927f6..e39dadb988 100644 --- a/rtc_base/rate_tracker.cc +++ b/rtc_base/rate_tracker.cc @@ -108,14 +108,18 @@ int64_t RateTracker::TotalSampleCount() const { } void RateTracker::AddSamples(int64_t sample_count) { + AddSamplesAtTime(Time(), sample_count); +} + +void RateTracker::AddSamplesAtTime(int64_t current_time_ms, + int64_t sample_count) { RTC_DCHECK_LE(0, sample_count); EnsureInitialized(); - int64_t current_time = Time(); // Advance the current bucket as needed for the current time, and reset // bucket counts as we advance. - for (size_t i = 0; - i <= bucket_count_ && - current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_; + for (size_t i = 0; i <= bucket_count_ && + current_time_ms >= + bucket_start_time_milliseconds_ + bucket_milliseconds_; ++i) { bucket_start_time_milliseconds_ += bucket_milliseconds_; current_bucket_ = NextBucketIndex(current_bucket_); @@ -125,7 +129,8 @@ void RateTracker::AddSamples(int64_t sample_count) { // the entire buffer of samples has been expired. bucket_start_time_milliseconds_ += bucket_milliseconds_ * - ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_); + ((current_time_ms - bucket_start_time_milliseconds_) / + bucket_milliseconds_); // Add all samples in the bucket that includes the current time. sample_buckets_[current_bucket_] += sample_count; total_sample_count_ += sample_count; diff --git a/rtc_base/rate_tracker.h b/rtc_base/rate_tracker.h index e42d40f14f..3b3c23538d 100644 --- a/rtc_base/rate_tracker.h +++ b/rtc_base/rate_tracker.h @@ -47,6 +47,9 @@ class RateTracker { // these samples, and increments the count for that bucket by sample_count. void AddSamples(int64_t sample_count); + // Increment count for bucket at |current_time_ms|. + void AddSamplesAtTime(int64_t current_time_ms, int64_t sample_count); + protected: // overrideable for tests virtual int64_t Time() const;