Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 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 | #include "rtc_base/experiments/field_trial_units.h" |
| 11 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 12 | #include <stdio.h> |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 13 | #include <limits> |
| 14 | #include <string> |
| 15 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 17 | |
| 18 | // Large enough to fit "seconds", the longest supported unit name. |
| 19 | #define RTC_TRIAL_UNIT_LENGTH_STR "7" |
| 20 | #define RTC_TRIAL_UNIT_SIZE 8 |
| 21 | |
| 22 | namespace webrtc { |
| 23 | namespace { |
| 24 | |
| 25 | struct ValueWithUnit { |
| 26 | double value; |
| 27 | std::string unit; |
| 28 | }; |
| 29 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 30 | absl::optional<ValueWithUnit> ParseValueWithUnit(std::string str) { |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 31 | if (str == "inf") { |
| 32 | return ValueWithUnit{std::numeric_limits<double>::infinity(), ""}; |
| 33 | } else if (str == "-inf") { |
| 34 | return ValueWithUnit{-std::numeric_limits<double>::infinity(), ""}; |
| 35 | } else { |
| 36 | double double_val; |
| 37 | char unit_char[RTC_TRIAL_UNIT_SIZE]; |
| 38 | unit_char[0] = 0; |
| 39 | if (sscanf(str.c_str(), "%lf%" RTC_TRIAL_UNIT_LENGTH_STR "s", &double_val, |
| 40 | unit_char) >= 1) { |
| 41 | return ValueWithUnit{double_val, unit_char}; |
| 42 | } |
| 43 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 44 | return absl::nullopt; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 45 | } |
| 46 | } // namespace |
| 47 | |
| 48 | template <> |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 49 | absl::optional<DataRate> ParseTypedParameter<DataRate>(std::string str) { |
| 50 | absl::optional<ValueWithUnit> result = ParseValueWithUnit(str); |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 51 | if (result) { |
| 52 | if (result->unit.empty() || result->unit == "kbps") { |
| 53 | return DataRate::kbps(result->value); |
| 54 | } else if (result->unit == "bps") { |
| 55 | return DataRate::bps(result->value); |
| 56 | } |
| 57 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 58 | return absl::nullopt; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | template <> |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 62 | absl::optional<DataSize> ParseTypedParameter<DataSize>(std::string str) { |
| 63 | absl::optional<ValueWithUnit> result = ParseValueWithUnit(str); |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 64 | if (result) { |
| 65 | if (result->unit.empty() || result->unit == "bytes") |
| 66 | return DataSize::bytes(result->value); |
| 67 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 68 | return absl::nullopt; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | template <> |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 72 | absl::optional<TimeDelta> ParseTypedParameter<TimeDelta>(std::string str) { |
| 73 | absl::optional<ValueWithUnit> result = ParseValueWithUnit(str); |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 74 | if (result) { |
| 75 | if (result->unit == "s" || result->unit == "seconds") { |
| 76 | return TimeDelta::seconds(result->value); |
| 77 | } else if (result->unit == "us") { |
| 78 | return TimeDelta::us(result->value); |
| 79 | } else if (result->unit.empty() || result->unit == "ms") { |
| 80 | return TimeDelta::ms(result->value); |
| 81 | } |
| 82 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 83 | return absl::nullopt; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | template class FieldTrialParameter<DataRate>; |
| 87 | template class FieldTrialParameter<DataSize>; |
| 88 | template class FieldTrialParameter<TimeDelta>; |
| 89 | |
| 90 | template class FieldTrialOptional<DataRate>; |
| 91 | template class FieldTrialOptional<DataSize>; |
| 92 | template class FieldTrialOptional<TimeDelta>; |
| 93 | } // namespace webrtc |