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