Add field trial for vp8 cpu speed configuration for arm.

Bug: none
Change-Id: I90c2475a229a1f10016e2ad84029e19b5a4f9966
Reviewed-on: https://webrtc-review.googlesource.com/c/107340
Commit-Queue: Åsa Persson <asapersson@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25484}
diff --git a/rtc_base/experiments/BUILD.gn b/rtc_base/experiments/BUILD.gn
index 924b490..b2e1302 100644
--- a/rtc_base/experiments/BUILD.gn
+++ b/rtc_base/experiments/BUILD.gn
@@ -75,6 +75,18 @@
   ]
 }
 
+rtc_static_library("cpu_speed_experiment") {
+  sources = [
+    "cpu_speed_experiment.cc",
+    "cpu_speed_experiment.h",
+  ]
+  deps = [
+    "../:rtc_base_approved",
+    "../../system_wrappers:field_trial",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+}
+
 rtc_static_library("rtt_mult_experiment") {
   sources = [
     "rtt_mult_experiment.cc",
@@ -104,6 +116,7 @@
 
     sources = [
       "congestion_controller_experiment_unittest.cc",
+      "cpu_speed_experiment_unittest.cc",
       "field_trial_parser_unittest.cc",
       "field_trial_units_unittest.cc",
       "normalize_simulcast_size_experiment_unittest.cc",
@@ -112,6 +125,7 @@
     ]
     deps = [
       ":congestion_controller_experiment",
+      ":cpu_speed_experiment",
       ":field_trial_parser",
       ":normalize_simulcast_size_experiment",
       ":quality_scaling_experiment",
@@ -120,6 +134,7 @@
       "../:rtc_base_tests_utils",
       "../../system_wrappers:field_trial",
       "../../test:field_trial",
+      "../../test:test_support",
     ]
   }
 }
diff --git a/rtc_base/experiments/cpu_speed_experiment.cc b/rtc_base/experiments/cpu_speed_experiment.cc
new file mode 100644
index 0000000..f39540c
--- /dev/null
+++ b/rtc_base/experiments/cpu_speed_experiment.cc
@@ -0,0 +1,70 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rtc_base/experiments/cpu_speed_experiment.h"
+
+#include <string>
+
+#include "rtc_base/logging.h"
+#include "system_wrappers/include/field_trial.h"
+
+namespace webrtc {
+namespace {
+constexpr char kFieldTrial[] = "WebRTC-VP8-CpuSpeed-Arm";
+constexpr int kMinSetting = -16;
+constexpr int kMaxSetting = -1;
+}  // namespace
+
+absl::optional<std::vector<CpuSpeedExperiment::Config>>
+CpuSpeedExperiment::GetConfigs() {
+  if (!webrtc::field_trial::IsEnabled(kFieldTrial))
+    return absl::nullopt;
+
+  const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
+  if (group.empty())
+    return absl::nullopt;
+
+  std::vector<Config> configs(3);
+  if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d", &(configs[0].pixels),
+             &(configs[0].cpu_speed), &(configs[1].pixels),
+             &(configs[1].cpu_speed), &(configs[2].pixels),
+             &(configs[2].cpu_speed)) != 6) {
+    RTC_LOG(LS_WARNING) << "Too few parameters provided.";
+    return absl::nullopt;
+  }
+
+  for (const auto& config : configs) {
+    if (config.cpu_speed < kMinSetting || config.cpu_speed > kMaxSetting) {
+      RTC_LOG(LS_WARNING) << "Unsupported cpu speed setting, value ignored.";
+      return absl::nullopt;
+    }
+  }
+
+  for (size_t i = 1; i < configs.size(); ++i) {
+    if (configs[i].pixels < configs[i - 1].pixels ||
+        configs[i].cpu_speed > configs[i - 1].cpu_speed) {
+      RTC_LOG(LS_WARNING) << "Invalid parameter value provided.";
+      return absl::nullopt;
+    }
+  }
+
+  return absl::optional<std::vector<Config>>(configs);
+}
+
+int CpuSpeedExperiment::GetValue(int pixels,
+                                 const std::vector<Config>& configs) {
+  for (const auto& config : configs) {
+    if (pixels <= config.pixels)
+      return config.cpu_speed;
+  }
+  return kMinSetting;
+}
+
+}  // namespace webrtc
diff --git a/rtc_base/experiments/cpu_speed_experiment.h b/rtc_base/experiments/cpu_speed_experiment.h
new file mode 100644
index 0000000..e6c8340
--- /dev/null
+++ b/rtc_base/experiments/cpu_speed_experiment.h
@@ -0,0 +1,41 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef RTC_BASE_EXPERIMENTS_CPU_SPEED_EXPERIMENT_H_
+#define RTC_BASE_EXPERIMENTS_CPU_SPEED_EXPERIMENT_H_
+
+#include <vector>
+
+#include "absl/types/optional.h"
+
+namespace webrtc {
+
+class CpuSpeedExperiment {
+ public:
+  struct Config {
+    bool operator==(const Config& o) const {
+      return pixels == o.pixels && cpu_speed == o.cpu_speed;
+    }
+
+    int pixels;     // The video frame size.
+    int cpu_speed;  // The |cpu_speed| to be used if the frame size is less
+                    // than or equal to |pixels|.
+  };
+
+  // Returns the configurations from field trial on success.
+  static absl::optional<std::vector<Config>> GetConfigs();
+
+  // Gets the cpu speed from the |configs| based on |pixels|.
+  static int GetValue(int pixels, const std::vector<Config>& configs);
+};
+
+}  // namespace webrtc
+
+#endif  // RTC_BASE_EXPERIMENTS_CPU_SPEED_EXPERIMENT_H_
diff --git a/rtc_base/experiments/cpu_speed_experiment_unittest.cc b/rtc_base/experiments/cpu_speed_experiment_unittest.cc
new file mode 100644
index 0000000..edc782c
--- /dev/null
+++ b/rtc_base/experiments/cpu_speed_experiment_unittest.cc
@@ -0,0 +1,85 @@
+/*
+ *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rtc_base/experiments/cpu_speed_experiment.h"
+
+#include "rtc_base/gunit.h"
+#include "test/field_trial.h"
+#include "test/gmock.h"
+
+namespace webrtc {
+
+TEST(CpuSpeedExperimentTest, GetConfigsFailsIfNotEnabled) {
+  EXPECT_FALSE(CpuSpeedExperiment::GetConfigs());
+}
+
+TEST(CpuSpeedExperimentTest, GetConfigsFailsForTooFewParameters) {
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,-1,2000,-10,3000/");
+  EXPECT_FALSE(CpuSpeedExperiment::GetConfigs());
+}
+
+TEST(CpuSpeedExperimentTest, GetConfigs) {
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,-1,2000,-10,3000,-16/");
+
+  const absl::optional<std::vector<CpuSpeedExperiment::Config>> kConfigs =
+      CpuSpeedExperiment::GetConfigs();
+  ASSERT_TRUE(kConfigs);
+  EXPECT_THAT(*kConfigs,
+              ::testing::ElementsAre(CpuSpeedExperiment::Config{1000, -1},
+                                     CpuSpeedExperiment::Config{2000, -10},
+                                     CpuSpeedExperiment::Config{3000, -16}));
+}
+
+TEST(CpuSpeedExperimentTest, GetValue) {
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,-5,2000,-10,3000,-12/");
+
+  const absl::optional<std::vector<CpuSpeedExperiment::Config>> kConfigs =
+      CpuSpeedExperiment::GetConfigs();
+  ASSERT_TRUE(kConfigs);
+  ASSERT_EQ(3u, (*kConfigs).size());
+  EXPECT_EQ(-5, CpuSpeedExperiment::GetValue(1, *kConfigs));
+  EXPECT_EQ(-5, CpuSpeedExperiment::GetValue(1000, *kConfigs));
+  EXPECT_EQ(-10, CpuSpeedExperiment::GetValue(1000 + 1, *kConfigs));
+  EXPECT_EQ(-10, CpuSpeedExperiment::GetValue(2000, *kConfigs));
+  EXPECT_EQ(-12, CpuSpeedExperiment::GetValue(2000 + 1, *kConfigs));
+  EXPECT_EQ(-12, CpuSpeedExperiment::GetValue(3000, *kConfigs));
+  EXPECT_EQ(-16, CpuSpeedExperiment::GetValue(3000 + 1, *kConfigs));
+}
+
+TEST(CpuSpeedExperimentTest, GetConfigsFailsForTooSmallValue) {
+  // Supported range: [-16, -1].
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,-1,2000,-10,3000,-17/");
+  EXPECT_FALSE(CpuSpeedExperiment::GetConfigs());
+}
+
+TEST(CpuSpeedExperimentTest, GetConfigsFailsForTooLargeValue) {
+  // Supported range: [-16, -1].
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,0,2000,-10,3000,-16/");
+  EXPECT_FALSE(CpuSpeedExperiment::GetConfigs());
+}
+
+TEST(CpuSpeedExperimentTest, GetConfigsFailsIfPixelsDecreasing) {
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,-5,999,-10,3000,-16/");
+  EXPECT_FALSE(CpuSpeedExperiment::GetConfigs());
+}
+
+TEST(CpuSpeedExperimentTest, GetConfigsFailsIfCpuSpeedIncreasing) {
+  webrtc::test::ScopedFieldTrials field_trials(
+      "WebRTC-VP8-CpuSpeed-Arm/Enabled-1000,-5,2000,-4,3000,-16/");
+  EXPECT_FALSE(CpuSpeedExperiment::GetConfigs());
+}
+
+}  // namespace webrtc