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 #include <memory>
17 
18 #include "absl/types/optional.h"
19 #include "api/transport/webrtc_key_value_config.h"
20 #include "modules/pacing/interval_budget.h"
21 #include "rtc_base/experiments/alr_experiment.h"
22 #include "rtc_base/experiments/struct_parameters_parser.h"
23 
24 namespace webrtc {
25 
26 class RtcEventLog;
27 
28 struct AlrDetectorConfig {
29   // Sent traffic ratio as a function of network capacity used to determine
30   // application-limited region. ALR region start when bandwidth usage drops
31   // below kAlrStartUsageRatio and ends when it raises above
32   // kAlrEndUsageRatio. NOTE: This is intentionally conservative at the moment
33   // until BW adjustments of application limited region is fine tuned.
34   double bandwidth_usage_ratio = 0.65;
35   double start_budget_level_ratio = 0.80;
36   double stop_budget_level_ratio = 0.50;
37   std::unique_ptr<StructParametersParser> Parser();
38 };
39 // Application limited region detector is a class that utilizes signals of
40 // elapsed time and bytes sent to estimate whether network traffic is
41 // currently limited by the application's ability to generate traffic.
42 //
43 // AlrDetector provides a signal that can be utilized to adjust
44 // estimate bandwidth.
45 // Note: This class is not thread-safe.
46 class AlrDetector {
47  public:
48   AlrDetector(AlrDetectorConfig config, RtcEventLog* event_log);
49   explicit AlrDetector(const WebRtcKeyValueConfig* key_value_config);
50   AlrDetector(const WebRtcKeyValueConfig* key_value_config,
51               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   void UpdateBudgetWithElapsedTime(int64_t delta_time_ms);
64   void UpdateBudgetWithBytesSent(size_t bytes_sent);
65 
66  private:
67   friend class GoogCcStatePrinter;
68   const AlrDetectorConfig conf_;
69 
70   absl::optional<int64_t> last_send_time_ms_;
71 
72   IntervalBudget alr_budget_;
73   absl::optional<int64_t> alr_started_time_ms_;
74 
75   RtcEventLog* event_log_;
76 };
77 }  // namespace webrtc
78 
79 #endif  // MODULES_CONGESTION_CONTROLLER_GOOG_CC_ALR_DETECTOR_H_
80