• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 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 #ifndef MODULES_CONGESTION_CONTROLLER_GOOG_CC_PROBE_CONTROLLER_H_
12 #define MODULES_CONGESTION_CONTROLLER_GOOG_CC_PROBE_CONTROLLER_H_
13 
14 #include <stdint.h>
15 
16 #include <initializer_list>
17 #include <vector>
18 
19 #include "absl/base/attributes.h"
20 #include "absl/types/optional.h"
21 #include "api/field_trials_view.h"
22 #include "api/rtc_event_log/rtc_event_log.h"
23 #include "api/transport/network_control.h"
24 #include "api/transport/network_types.h"
25 #include "api/units/data_rate.h"
26 #include "api/units/timestamp.h"
27 #include "rtc_base/experiments/field_trial_parser.h"
28 
29 namespace webrtc {
30 
31 struct ProbeControllerConfig {
32   explicit ProbeControllerConfig(const FieldTrialsView* key_value_config);
33   ProbeControllerConfig(const ProbeControllerConfig&);
34   ProbeControllerConfig& operator=(const ProbeControllerConfig&) = default;
35   ~ProbeControllerConfig();
36 
37   // These parameters configure the initial probes. First we send one or two
38   // probes of sizes p1 * start_bitrate_ and p2 * start_bitrate_.
39   // Then whenever we get a bitrate estimate of at least further_probe_threshold
40   // times the size of the last sent probe we'll send another one of size
41   // step_size times the new estimate.
42   FieldTrialParameter<double> first_exponential_probe_scale;
43   FieldTrialOptional<double> second_exponential_probe_scale;
44   FieldTrialParameter<double> further_exponential_probe_scale;
45   FieldTrialParameter<double> further_probe_threshold;
46 
47   // Configures how often we send ALR probes and how big they are.
48   FieldTrialParameter<TimeDelta> alr_probing_interval;
49   FieldTrialParameter<double> alr_probe_scale;
50 
51   // Configures how often we send probes if NetworkStateEstimate is available.
52   FieldTrialParameter<TimeDelta> network_state_estimate_probing_interval;
53   // Periodically probe as long as the the ratio beteeen current estimate and
54   // NetworkStateEstimate is lower then this.
55   FieldTrialParameter<double>
56       probe_if_estimate_lower_than_network_state_estimate_ratio;
57   FieldTrialParameter<TimeDelta>
58       estimate_lower_than_network_state_estimate_probing_interval;
59   FieldTrialParameter<double> network_state_probe_scale;
60   // Overrides min_probe_duration if network_state_estimate_probing_interval
61   // is set and a network state estimate is known.
62   FieldTrialParameter<TimeDelta> network_state_probe_duration;
63 
64   // Configures the probes emitted by changed to the allocated bitrate.
65   FieldTrialParameter<bool> probe_on_max_allocated_bitrate_change;
66   FieldTrialOptional<double> first_allocation_probe_scale;
67   FieldTrialOptional<double> second_allocation_probe_scale;
68   FieldTrialFlag allocation_allow_further_probing;
69   FieldTrialParameter<DataRate> allocation_probe_max;
70 
71   // The minimum number probing packets used.
72   FieldTrialParameter<int> min_probe_packets_sent;
73   // The minimum probing duration.
74   FieldTrialParameter<TimeDelta> min_probe_duration;
75   // Periodically probe when bandwidth estimate is loss limited.
76   FieldTrialParameter<bool> limit_probe_target_rate_to_loss_bwe;
77   FieldTrialParameter<double> loss_limited_probe_scale;
78   // Dont send a probe if min(estimate, network state estimate) is larger than
79   // this fraction of the set max bitrate.
80   FieldTrialParameter<double> skip_if_estimate_larger_than_fraction_of_max;
81 };
82 
83 // Reason that bandwidth estimate is limited. Bandwidth estimate can be limited
84 // by either delay based bwe, or loss based bwe when it increases/decreases the
85 // estimate.
86 enum class BandwidthLimitedCause {
87   kLossLimitedBweIncreasing = 0,
88   kLossLimitedBweDecreasing = 1,
89   kDelayBasedLimited = 2
90 };
91 
92 // This class controls initiation of probing to estimate initial channel
93 // capacity. There is also support for probing during a session when max
94 // bitrate is adjusted by an application.
95 class ProbeController {
96  public:
97   explicit ProbeController(const FieldTrialsView* key_value_config,
98                            RtcEventLog* event_log);
99   ~ProbeController();
100 
101   ProbeController(const ProbeController&) = delete;
102   ProbeController& operator=(const ProbeController&) = delete;
103 
104   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> SetBitrates(
105       DataRate min_bitrate,
106       DataRate start_bitrate,
107       DataRate max_bitrate,
108       Timestamp at_time);
109 
110   // The total bitrate, as opposed to the max bitrate, is the sum of the
111   // configured bitrates for all active streams.
112   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig>
113   OnMaxTotalAllocatedBitrate(DataRate max_total_allocated_bitrate,
114                              Timestamp at_time);
115 
116   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> OnNetworkAvailability(
117       NetworkAvailability msg);
118 
119   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> SetEstimatedBitrate(
120       DataRate bitrate,
121       BandwidthLimitedCause bandwidth_limited_cause,
122       Timestamp at_time);
123 
124   void EnablePeriodicAlrProbing(bool enable);
125 
126   void SetAlrStartTimeMs(absl::optional<int64_t> alr_start_time);
127   void SetAlrEndedTimeMs(int64_t alr_end_time);
128 
129   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> RequestProbe(
130       Timestamp at_time);
131 
132   void SetNetworkStateEstimate(webrtc::NetworkStateEstimate estimate);
133 
134   // Resets the ProbeController to a state equivalent to as if it was just
135   // created EXCEPT for `enable_periodic_alr_probing_`.
136   void Reset(Timestamp at_time);
137 
138   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> Process(
139       Timestamp at_time);
140 
141  private:
142   enum class State {
143     // Initial state where no probing has been triggered yet.
144     kInit,
145     // Waiting for probing results to continue further probing.
146     kWaitingForProbingResult,
147     // Probing is complete.
148     kProbingComplete,
149   };
150 
151   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig>
152   InitiateExponentialProbing(Timestamp at_time);
153   ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> InitiateProbing(
154       Timestamp now,
155       std::vector<DataRate> bitrates_to_probe,
156       bool probe_further);
157   bool TimeForAlrProbe(Timestamp at_time) const;
158   bool TimeForNetworkStateProbe(Timestamp at_time) const;
159 
160   bool network_available_;
161   BandwidthLimitedCause bandwidth_limited_cause_ =
162       BandwidthLimitedCause::kDelayBasedLimited;
163   State state_;
164   DataRate min_bitrate_to_probe_further_ = DataRate::PlusInfinity();
165   Timestamp time_last_probing_initiated_ = Timestamp::MinusInfinity();
166   DataRate estimated_bitrate_ = DataRate::Zero();
167   absl::optional<webrtc::NetworkStateEstimate> network_estimate_;
168   DataRate start_bitrate_ = DataRate::Zero();
169   DataRate max_bitrate_ = DataRate::PlusInfinity();
170   Timestamp last_bwe_drop_probing_time_ = Timestamp::Zero();
171   absl::optional<Timestamp> alr_start_time_;
172   absl::optional<Timestamp> alr_end_time_;
173   bool enable_periodic_alr_probing_;
174   Timestamp time_of_last_large_drop_ = Timestamp::MinusInfinity();
175   DataRate bitrate_before_last_large_drop_ = DataRate::Zero();
176   DataRate max_total_allocated_bitrate_ = DataRate::Zero();
177 
178   const bool in_rapid_recovery_experiment_;
179   // For WebRTC.BWE.MidCallProbing.* metric.
180   bool mid_call_probing_waiting_for_result_;
181   DataRate mid_call_probing_bitrate_ = DataRate::Zero();
182   DataRate mid_call_probing_succcess_threshold_ = DataRate::Zero();
183   RtcEventLog* event_log_;
184 
185   int32_t next_probe_cluster_id_ = 1;
186 
187   ProbeControllerConfig config_;
188 };
189 
190 }  // namespace webrtc
191 
192 #endif  // MODULES_CONGESTION_CONTROLLER_GOOG_CC_PROBE_CONTROLLER_H_
193