1 /*
2  *  Copyright (c) 2019 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 "modules/congestion_controller/goog_cc/acknowledged_bitrate_estimator_interface.h"
12 
13 #include <algorithm>
14 
15 #include "api/units/time_delta.h"
16 #include "modules/congestion_controller/goog_cc/acknowledged_bitrate_estimator.h"
17 #include "modules/congestion_controller/goog_cc/robust_throughput_estimator.h"
18 #include "rtc_base/logging.h"
19 
20 namespace webrtc {
21 
22 constexpr char RobustThroughputEstimatorSettings::kKey[];
23 
RobustThroughputEstimatorSettings(const FieldTrialsView * key_value_config)24 RobustThroughputEstimatorSettings::RobustThroughputEstimatorSettings(
25     const FieldTrialsView* key_value_config) {
26   Parser()->Parse(
27       key_value_config->Lookup(RobustThroughputEstimatorSettings::kKey));
28   if (window_packets < 10 || 1000 < window_packets) {
29     RTC_LOG(LS_WARNING) << "Window size must be between 10 and 1000 packets";
30     window_packets = 20;
31   }
32   if (max_window_packets < 10 || 1000 < max_window_packets) {
33     RTC_LOG(LS_WARNING)
34         << "Max window size must be between 10 and 1000 packets";
35     max_window_packets = 500;
36   }
37   max_window_packets = std::max(max_window_packets, window_packets);
38 
39   if (required_packets < 10 || 1000 < required_packets) {
40     RTC_LOG(LS_WARNING) << "Required number of initial packets must be between "
41                            "10 and 1000 packets";
42     required_packets = 10;
43   }
44   required_packets = std::min(required_packets, window_packets);
45 
46   if (min_window_duration < TimeDelta::Millis(100) ||
47       TimeDelta::Millis(3000) < min_window_duration) {
48     RTC_LOG(LS_WARNING) << "Window duration must be between 100 and 3000 ms";
49     min_window_duration = TimeDelta::Millis(750);
50   }
51   if (max_window_duration < TimeDelta::Seconds(1) ||
52       TimeDelta::Seconds(15) < max_window_duration) {
53     RTC_LOG(LS_WARNING) << "Max window duration must be between 1 and 15 s";
54     max_window_duration = TimeDelta::Seconds(5);
55   }
56   min_window_duration = std::min(min_window_duration, max_window_duration);
57 
58   if (unacked_weight < 0.0 || 1.0 < unacked_weight) {
59     RTC_LOG(LS_WARNING)
60         << "Weight for prior unacked size must be between 0 and 1.";
61     unacked_weight = 1.0;
62   }
63 }
64 
65 std::unique_ptr<StructParametersParser>
Parser()66 RobustThroughputEstimatorSettings::Parser() {
67   return StructParametersParser::Create(
68       "enabled", &enabled,                          //
69       "window_packets", &window_packets,            //
70       "max_window_packets", &max_window_packets,    //
71       "window_duration", &min_window_duration,      //
72       "max_window_duration", &max_window_duration,  //
73       "required_packets", &required_packets,        //
74       "unacked_weight", &unacked_weight);
75 }
76 
77 AcknowledgedBitrateEstimatorInterface::
~AcknowledgedBitrateEstimatorInterface()78     ~AcknowledgedBitrateEstimatorInterface() {}
79 
80 std::unique_ptr<AcknowledgedBitrateEstimatorInterface>
Create(const FieldTrialsView * key_value_config)81 AcknowledgedBitrateEstimatorInterface::Create(
82     const FieldTrialsView* key_value_config) {
83   RobustThroughputEstimatorSettings simplified_estimator_settings(
84       key_value_config);
85   if (simplified_estimator_settings.enabled) {
86     return std::make_unique<RobustThroughputEstimator>(
87         simplified_estimator_settings);
88   }
89   return std::make_unique<AcknowledgedBitrateEstimator>(key_value_config);
90 }
91 
92 }  // namespace webrtc
93