Add field trial to reduce max STUN check retransmissions from 8 to 6 in accordance with RFC 5389.

Bug: webrtc:10282
Change-Id: I19ac690e3388e2015792695cf7836cb727a4314b
Reviewed-on: https://webrtc-review.googlesource.com/c/120960
Reviewed-by: Christoffer Rodbro <crodbro@webrtc.org>
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Commit-Queue: Björn Terelius <terelius@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26524}
diff --git a/p2p/base/stun_request.cc b/p2p/base/stun_request.cc
index 088cbc8..afd276f 100644
--- a/p2p/base/stun_request.cc
+++ b/p2p/base/stun_request.cc
@@ -19,6 +19,7 @@
 #include "rtc_base/helpers.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/time_utils.h"  // For TimeMillis
+#include "system_wrappers/include/field_trial.h"
 
 namespace cricket {
 
@@ -34,6 +35,7 @@
 // RFC 5389 says SHOULD retransmit 7 times.
 // This has been 8 for years (not sure why).
 const int STUN_MAX_RETRANSMISSIONS = 8;  // Total sends: 9
+const int STUN_MAX_RETRANSMISSIONS_RFC_5389 = 6;  // Total sends: 7
 
 // We also cap the doubling, even though the standard doesn't say to.
 // This has been 1.6 seconds for years, but for networks that
@@ -41,6 +43,10 @@
 // work well.
 const int STUN_MAX_RTO = 8000;  // milliseconds, or 5 doublings
 
+namespace {
+const char kRfc5389StunRetransmissions[] = "WebRTC-Rfc5389StunRetransmissions";
+}  // namespace
+
 StunRequestManager::StunRequestManager(rtc::Thread* thread) : thread_(thread) {}
 
 StunRequestManager::~StunRequestManager() {
@@ -169,12 +175,20 @@
       timeout_(false),
       manager_(0),
       msg_(new StunMessage()),
-      tstamp_(0) {
+      tstamp_(0),
+      in_rfc5389_retransmission_experiment_(
+          webrtc::field_trial::IsEnabled(kRfc5389StunRetransmissions)) {
   msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
 }
 
 StunRequest::StunRequest(StunMessage* request)
-    : count_(0), timeout_(false), manager_(0), msg_(request), tstamp_(0) {
+    : count_(0),
+      timeout_(false),
+      manager_(0),
+      msg_(request),
+      tstamp_(0),
+      in_rfc5389_retransmission_experiment_(
+          webrtc::field_trial::IsEnabled(kRfc5389StunRetransmissions)) {
   msg_->SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength));
 }
 
@@ -244,7 +258,9 @@
 void StunRequest::OnSent() {
   count_ += 1;
   int retransmissions = (count_ - 1);
-  if (retransmissions >= STUN_MAX_RETRANSMISSIONS) {
+  if (retransmissions >= STUN_MAX_RETRANSMISSIONS ||
+      (in_rfc5389_retransmission_experiment_ &&
+       retransmissions >= STUN_MAX_RETRANSMISSIONS_RFC_5389)) {
     timeout_ = true;
   }
   RTC_LOG(LS_VERBOSE) << "Sent STUN request " << count_
diff --git a/p2p/base/stun_request.h b/p2p/base/stun_request.h
index 7994fb6..571abe1 100644
--- a/p2p/base/stun_request.h
+++ b/p2p/base/stun_request.h
@@ -148,6 +148,7 @@
   StunRequestManager* manager_;
   StunMessage* msg_;
   int64_t tstamp_;
+  bool in_rfc5389_retransmission_experiment_;
 
   friend class StunRequestManager;
 };