xref: /aosp_15_r20/external/webrtc/modules/congestion_controller/goog_cc/alr_detector.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_ALR_DETECTOR_H_
12 #define MODULES_CONGESTION_CONTROLLER_GOOG_CC_ALR_DETECTOR_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <memory>
18 
19 #include "absl/types/optional.h"
20 #include "api/field_trials_view.h"
21 #include "modules/pacing/interval_budget.h"
22 #include "rtc_base/experiments/alr_experiment.h"
23 #include "rtc_base/experiments/struct_parameters_parser.h"
24 
25 namespace webrtc {
26 
27 class RtcEventLog;
28 
29 struct AlrDetectorConfig {
30   // Sent traffic ratio as a function of network capacity used to determine
31   // application-limited region. ALR region start when bandwidth usage drops
32   // below kAlrStartUsageRatio and ends when it raises above
33   // kAlrEndUsageRatio. NOTE: This is intentionally conservative at the moment
34   // until BW adjustments of application limited region is fine tuned.
35   double bandwidth_usage_ratio = 0.65;
36   double start_budget_level_ratio = 0.80;
37   double stop_budget_level_ratio = 0.50;
38   std::unique_ptr<StructParametersParser> Parser();
39 };
40 // Application limited region detector is a class that utilizes signals of
41 // elapsed time and bytes sent to estimate whether network traffic is
42 // currently limited by the application's ability to generate traffic.
43 //
44 // AlrDetector provides a signal that can be utilized to adjust
45 // estimate bandwidth.
46 // Note: This class is not thread-safe.
47 class AlrDetector {
48  public:
49   AlrDetector(AlrDetectorConfig config, RtcEventLog* event_log);
50   explicit AlrDetector(const FieldTrialsView* key_value_config);
51   AlrDetector(const FieldTrialsView* key_value_config, RtcEventLog* event_log);
52   ~AlrDetector();
53 
54   void OnBytesSent(size_t bytes_sent, int64_t send_time_ms);
55 
56   // Set current estimated bandwidth.
57   void SetEstimatedBitrate(int bitrate_bps);
58 
59   // Returns time in milliseconds when the current application-limited region
60   // started or empty result if the sender is currently not application-limited.
61   absl::optional<int64_t> GetApplicationLimitedRegionStartTime() const;
62 
63  private:
64   friend class GoogCcStatePrinter;
65   const AlrDetectorConfig conf_;
66 
67   absl::optional<int64_t> last_send_time_ms_;
68 
69   IntervalBudget alr_budget_;
70   absl::optional<int64_t> alr_started_time_ms_;
71 
72   RtcEventLog* event_log_;
73 };
74 }  // namespace webrtc
75 
76 #endif  // MODULES_CONGESTION_CONTROLLER_GOOG_CC_ALR_DETECTOR_H_
77