peerconnection: add was-ever-connected boolean flag

and report some metrics only on the first connection state
change to connected

BUG=webrtc:12383

Change-Id: I32908e23c51aa40730be8e534793829268d4e25e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/208583
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Philipp Hancke <philipp.hancke@googlemail.com>
Cr-Commit-Position: refs/heads/master@{#33333}
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index 0411ab2..a340f98 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -1797,9 +1797,11 @@
   connection_state_ = new_state;
   Observer()->OnConnectionChange(new_state);
 
-  // The connection state change to connected usually happens once per
-  // connection which makes it a good point to report metrics.
-  if (new_state == PeerConnectionState::kConnected) {
+  if (new_state == PeerConnectionState::kConnected && !was_ever_connected_) {
+    was_ever_connected_ = true;
+
+    // The first connection state change to connected happens once per
+    // connection which makes it a good point to report metrics.
     // Record bundle-policy from configuration. Done here from
     // connectionStateChange to limit to actually established connections.
     BundlePolicyUsage policy = kBundlePolicyUsageMax;
diff --git a/pc/peer_connection.h b/pc/peer_connection.h
index d81f3c9..98c5519 100644
--- a/pc/peer_connection.h
+++ b/pc/peer_connection.h
@@ -707,6 +707,10 @@
   std::unique_ptr<RtpTransmissionManager> rtp_manager_;
 
   rtc::WeakPtrFactory<PeerConnection> weak_factory_;
+
+  // Did the connectionState ever change to `connected`?
+  // Used to gather metrics only the first such state change.
+  bool was_ever_connected_ RTC_GUARDED_BY(signaling_thread()) = false;
 };
 
 }  // namespace webrtc