1 /*
2  *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/experiments/cpu_speed_experiment.h"
12 
13 #include <stdio.h>
14 
15 #include <string>
16 
17 #include "rtc_base/logging.h"
18 #include "system_wrappers/include/field_trial.h"
19 
20 namespace webrtc {
21 namespace {
22 constexpr char kFieldTrial[] = "WebRTC-VP8-CpuSpeed-Arm";
23 constexpr int kMinSetting = -16;
24 constexpr int kMaxSetting = -1;
25 }  // namespace
26 
27 absl::optional<std::vector<CpuSpeedExperiment::Config>>
GetConfigs()28 CpuSpeedExperiment::GetConfigs() {
29   if (!webrtc::field_trial::IsEnabled(kFieldTrial))
30     return absl::nullopt;
31 
32   const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
33   if (group.empty())
34     return absl::nullopt;
35 
36   std::vector<Config> configs(3);
37   if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d", &(configs[0].pixels),
38              &(configs[0].cpu_speed), &(configs[1].pixels),
39              &(configs[1].cpu_speed), &(configs[2].pixels),
40              &(configs[2].cpu_speed)) != 6) {
41     RTC_LOG(LS_WARNING) << "Too few parameters provided.";
42     return absl::nullopt;
43   }
44 
45   for (const auto& config : configs) {
46     if (config.cpu_speed < kMinSetting || config.cpu_speed > kMaxSetting) {
47       RTC_LOG(LS_WARNING) << "Unsupported cpu speed setting, value ignored.";
48       return absl::nullopt;
49     }
50   }
51 
52   for (size_t i = 1; i < configs.size(); ++i) {
53     if (configs[i].pixels < configs[i - 1].pixels ||
54         configs[i].cpu_speed > configs[i - 1].cpu_speed) {
55       RTC_LOG(LS_WARNING) << "Invalid parameter value provided.";
56       return absl::nullopt;
57     }
58   }
59 
60   return absl::optional<std::vector<Config>>(configs);
61 }
62 
GetValue(int pixels,const std::vector<Config> & configs)63 int CpuSpeedExperiment::GetValue(int pixels,
64                                  const std::vector<Config>& configs) {
65   for (const auto& config : configs) {
66     if (pixels <= config.pixels)
67       return config.cpu_speed;
68   }
69   return kMinSetting;
70 }
71 
72 }  // namespace webrtc
73