xref: /aosp_15_r20/external/webrtc/modules/pacing/bitrate_prober.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 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_PACING_BITRATE_PROBER_H_
12 #define MODULES_PACING_BITRATE_PROBER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <queue>
18 
19 #include "api/transport/field_trial_based_config.h"
20 #include "api/transport/network_types.h"
21 #include "rtc_base/experiments/field_trial_parser.h"
22 
23 namespace webrtc {
24 class RtcEventLog;
25 
26 struct BitrateProberConfig {
27   explicit BitrateProberConfig(const FieldTrialsView* key_value_config);
28   BitrateProberConfig(const BitrateProberConfig&) = default;
29   BitrateProberConfig& operator=(const BitrateProberConfig&) = default;
30   ~BitrateProberConfig() = default;
31 
32   // A minimum interval between probes to allow scheduling to be feasible.
33   FieldTrialParameter<TimeDelta> min_probe_delta;
34   // Maximum amount of time each probe can be delayed.
35   FieldTrialParameter<TimeDelta> max_probe_delay;
36   // This is used to start sending a probe after a large enough packet.
37   // The min packet size is scaled with the bitrate we're probing at.
38   // This defines the max min packet size, meaning that on high bitrates
39   // a packet of at least this size is needed to trigger sending a probe.
40   FieldTrialParameter<DataSize> min_packet_size;
41 };
42 
43 // Note that this class isn't thread-safe by itself and therefore relies
44 // on being protected by the caller.
45 class BitrateProber {
46  public:
47   explicit BitrateProber(const FieldTrialsView& field_trials);
48   ~BitrateProber();
49 
50   void SetEnabled(bool enable);
51 
52   // Returns true if the prober is in a probing session, i.e., it currently
53   // wants packets to be sent out according to the time returned by
54   // TimeUntilNextProbe().
is_probing()55   bool is_probing() const { return probing_state_ == ProbingState::kActive; }
56 
57   // Initializes a new probing session if the prober is allowed to probe. Does
58   // not initialize the prober unless the packet size is large enough to probe
59   // with.
60   void OnIncomingPacket(DataSize packet_size);
61 
62   // Create a cluster used to probe.
63   void CreateProbeCluster(const ProbeClusterConfig& cluster_config);
64   // Returns the time at which the next probe should be sent to get accurate
65   // probing. If probing is not desired at this time, Timestamp::PlusInfinity()
66   // will be returned.
67   // TODO(bugs.webrtc.org/11780): Remove `now` argument when old mode is gone.
68   Timestamp NextProbeTime(Timestamp now) const;
69 
70   // Information about the current probing cluster.
71   absl::optional<PacedPacketInfo> CurrentCluster(Timestamp now);
72 
73   // Returns the minimum number of bytes that the prober recommends for
74   // the next probe, or zero if not probing. A probe can consist of multiple
75   // packets that are sent back to back.
76   DataSize RecommendedMinProbeSize() const;
77 
78   // Called to report to the prober that a probe has been sent. In case of
79   // multiple packets per probe, this call would be made at the end of sending
80   // the last packet in probe. `size` is the total size of all packets in probe.
81   void ProbeSent(Timestamp now, DataSize size);
82 
83  private:
84   enum class ProbingState {
85     // Probing will not be triggered in this state at all times.
86     kDisabled,
87     // Probing is enabled and ready to trigger on the first packet arrival.
88     kInactive,
89     // Probe cluster is filled with the set of data rates to be probed and
90     // probes are being sent.
91     kActive,
92     // Probing is enabled, but currently suspended until an explicit trigger
93     // to start probing again.
94     kSuspended,
95   };
96 
97   // A probe cluster consists of a set of probes. Each probe in turn can be
98   // divided into a number of packets to accommodate the MTU on the network.
99   struct ProbeCluster {
100     PacedPacketInfo pace_info;
101 
102     int sent_probes = 0;
103     int sent_bytes = 0;
104     Timestamp requested_at = Timestamp::MinusInfinity();
105     Timestamp started_at = Timestamp::MinusInfinity();
106     int retries = 0;
107   };
108 
109   Timestamp CalculateNextProbeTime(const ProbeCluster& cluster) const;
110 
111   ProbingState probing_state_;
112 
113   // Probe bitrate per packet. These are used to compute the delta relative to
114   // the previous probe packet based on the size and time when that packet was
115   // sent.
116   std::queue<ProbeCluster> clusters_;
117 
118   // Time the next probe should be sent when in kActive state.
119   Timestamp next_probe_time_;
120 
121   int total_probe_count_;
122   int total_failed_probe_count_;
123 
124   BitrateProberConfig config_;
125 };
126 
127 }  // namespace webrtc
128 
129 #endif  // MODULES_PACING_BITRATE_PROBER_H_
130