Add support for field trials in peerconnection_client|server

Bug: webrtc:9935
Change-Id: Icb96123c5feb9dee309734d2a8ba88e23a467bef
Reviewed-on: https://webrtc-review.googlesource.com/c/108301
Commit-Queue: Björn Terelius <terelius@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25431}
diff --git a/examples/BUILD.gn b/examples/BUILD.gn
index 5192041..cd931b4 100644
--- a/examples/BUILD.gn
+++ b/examples/BUILD.gn
@@ -683,6 +683,8 @@
       "../rtc_base:checks",
       "../rtc_base:stringutils",
       "../rtc_base/third_party/sigslot",
+      "../system_wrappers:field_trial",
+      "../test:field_trial",
     ]
     if (is_win) {
       sources += [
@@ -745,6 +747,8 @@
       "../rtc_base:rtc_base_approved",
       "../rtc_base:stringutils",
       "../rtc_tools:command_line_parser",
+      "../system_wrappers:field_trial",
+      "../test:field_trial",
     ]
     if (!build_with_chromium && is_clang) {
       # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
diff --git a/examples/DEPS b/examples/DEPS
index 6a9a9f0..5b37845 100644
--- a/examples/DEPS
+++ b/examples/DEPS
@@ -8,5 +8,6 @@
   "+p2p",
   "+pc",
   "+sdk/objc",
+  "+system_wrappers/include",
   "+third_party/libyuv",
 ]
diff --git a/examples/peerconnection/client/flagdefs.h b/examples/peerconnection/client/flagdefs.h
index 7bbc383..51f9c9a 100644
--- a/examples/peerconnection/client/flagdefs.h
+++ b/examples/peerconnection/client/flagdefs.h
@@ -35,4 +35,13 @@
     "the server without user intervention.  Note: this flag should only be set "
     "to true on one of the two clients.");
 
+WEBRTC_DEFINE_string(
+    force_fieldtrials,
+    "",
+    "Field trials control experimental features. This flag specifies the field "
+    "trials in effect. E.g. running with "
+    "--force_fieldtrials=WebRTC-FooFeature/Enabled/ "
+    "will assign the group Enabled to field trial WebRTC-FooFeature. Multiple "
+    "trials are separated by \"/\"");
+
 #endif  // EXAMPLES_PEERCONNECTION_CLIENT_FLAGDEFS_H_
diff --git a/examples/peerconnection/client/linux/main.cc b/examples/peerconnection/client/linux/main.cc
index 50179c4..6c34683 100644
--- a/examples/peerconnection/client/linux/main.cc
+++ b/examples/peerconnection/client/linux/main.cc
@@ -17,6 +17,8 @@
 
 #include "rtc_base/ssladapter.h"
 #include "rtc_base/thread.h"
+#include "system_wrappers/include/field_trial.h"
+#include "test/field_trial.h"
 
 class CustomSocketServer : public rtc::PhysicalSocketServer {
  public:
@@ -75,6 +77,11 @@
     return 0;
   }
 
+  webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
+  // InitFieldTrialsFromString stores the char*, so the char array must outlive
+  // the application.
+  webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
+
   // Abort if the user specifies a port that is outside the allowed
   // range [1, 65535].
   if ((FLAG_port < 1) || (FLAG_port > 65535)) {
diff --git a/examples/peerconnection/client/main.cc b/examples/peerconnection/client/main.cc
index 25dde1e..f92e606 100644
--- a/examples/peerconnection/client/main.cc
+++ b/examples/peerconnection/client/main.cc
@@ -16,6 +16,8 @@
 #include "rtc_base/ssladapter.h"
 #include "rtc_base/win32socketinit.h"
 #include "rtc_base/win32socketserver.h"
+#include "system_wrappers/include/field_trial.h"
+#include "test/field_trial.h"
 
 int PASCAL wWinMain(HINSTANCE instance,
                     HINSTANCE prev_instance,
@@ -36,6 +38,11 @@
     return 0;
   }
 
+  webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
+  // InitFieldTrialsFromString stores the char*, so the char array must outlive
+  // the application.
+  webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
+
   // Abort if the user specifies a port that is outside the allowed
   // range [1, 65535].
   if ((FLAG_port < 1) || (FLAG_port > 65535)) {
diff --git a/examples/peerconnection/server/main.cc b/examples/peerconnection/server/main.cc
index 075b6ea..5214ca5 100644
--- a/examples/peerconnection/server/main.cc
+++ b/examples/peerconnection/server/main.cc
@@ -18,7 +18,19 @@
 #include "examples/peerconnection/server/data_socket.h"
 #include "examples/peerconnection/server/peer_channel.h"
 #include "examples/peerconnection/server/utils.h"
+#include "rtc_base/flags.h"
 #include "rtc_tools/simple_command_line_parser.h"
+#include "system_wrappers/include/field_trial.h"
+#include "test/field_trial.h"
+
+WEBRTC_DEFINE_string(
+    force_fieldtrials,
+    "",
+    "Field trials control experimental features. This flag specifies the field "
+    "trials in effect. E.g. running with "
+    "--force_fieldtrials=WebRTC-FooFeature/Enabled/ "
+    "will assign the group Enabled to field trial WebRTC-FooFeature. Multiple "
+    "trials are separated by \"/\"");
 
 static const size_t kMaxConnections = (FD_SETSIZE - 2);
 
@@ -62,6 +74,11 @@
     return 0;
   }
 
+  webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
+  // InitFieldTrialsFromString stores the char*, so the char array must outlive
+  // the application.
+  webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
+
   int port = strtol((parser.GetFlag("port")).c_str(), NULL, 10);
 
   // Abort if the user specifies a port that is outside the allowed