Add last_data_sent timestamp to Connection.

Add a timestamp for last data sent in Connection.

Move calling of rtc::TimeMillis() to Connection and remove it from RateTracker::AddSamples.

This timestamp will be used to further improve fail over logic.

BUG=None

Change-Id: I4cbc7693a0e081277590b9cb13264dc2a998202e

No-Try: True
Change-Id: I4cbc7693a0e081277590b9cb13264dc2a998202e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/197421
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32831}
diff --git a/p2p/base/connection.cc b/p2p/base/connection.cc
index fe60421..8adfeb4 100644
--- a/p2p/base/connection.cc
+++ b/p2p/base/connection.cc
@@ -1372,13 +1372,15 @@
   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 88e930c..d48137d 100644
--- a/p2p/base/connection.h
+++ b/p2p/base/connection.h
@@ -237,6 +237,8 @@
   // 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 @@
   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 efbf62e..d4266bf 100644
--- a/p2p/base/tcp_port.cc
+++ b/p2p/base/tcp_port.cc
@@ -403,12 +403,14 @@
   static_cast<TCPPort*>(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 5c82792..e39dadb 100644
--- a/rtc_base/rate_tracker.cc
+++ b/rtc_base/rate_tracker.cc
@@ -108,14 +108,18 @@
 }
 
 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 @@
   // 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 e42d40f..3b3c235 100644
--- a/rtc_base/rate_tracker.h
+++ b/rtc_base/rate_tracker.h
@@ -47,6 +47,9 @@
   // 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;