xref: /aosp_15_r20/external/webrtc/modules/pacing/bitrate_prober.cc (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 #include "modules/pacing/bitrate_prober.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include "absl/memory/memory.h"
16*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_event_log/rtc_event.h"
17*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_event_log/rtc_event_log.h"
18*d9f75844SAndroid Build Coastguard Worker #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
20*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
21*d9f75844SAndroid Build Coastguard Worker #include "system_wrappers/include/metrics.h"
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker namespace {
26*d9f75844SAndroid Build Coastguard Worker constexpr TimeDelta kProbeClusterTimeout = TimeDelta::Seconds(5);
27*d9f75844SAndroid Build Coastguard Worker constexpr size_t kMaxPendingProbeClusters = 5;
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker }  // namespace
30*d9f75844SAndroid Build Coastguard Worker 
BitrateProberConfig(const FieldTrialsView * key_value_config)31*d9f75844SAndroid Build Coastguard Worker BitrateProberConfig::BitrateProberConfig(
32*d9f75844SAndroid Build Coastguard Worker     const FieldTrialsView* key_value_config)
33*d9f75844SAndroid Build Coastguard Worker     : min_probe_delta("min_probe_delta", TimeDelta::Millis(2)),
34*d9f75844SAndroid Build Coastguard Worker       max_probe_delay("max_probe_delay", TimeDelta::Millis(10)),
35*d9f75844SAndroid Build Coastguard Worker       min_packet_size("min_packet_size", DataSize::Bytes(200)) {
36*d9f75844SAndroid Build Coastguard Worker   ParseFieldTrial({&min_probe_delta, &max_probe_delay, &min_packet_size},
37*d9f75844SAndroid Build Coastguard Worker                   key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior"));
38*d9f75844SAndroid Build Coastguard Worker }
39*d9f75844SAndroid Build Coastguard Worker 
~BitrateProber()40*d9f75844SAndroid Build Coastguard Worker BitrateProber::~BitrateProber() {
41*d9f75844SAndroid Build Coastguard Worker   RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalProbeClustersRequested",
42*d9f75844SAndroid Build Coastguard Worker                             total_probe_count_);
43*d9f75844SAndroid Build Coastguard Worker   RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalFailedProbeClusters",
44*d9f75844SAndroid Build Coastguard Worker                             total_failed_probe_count_);
45*d9f75844SAndroid Build Coastguard Worker }
46*d9f75844SAndroid Build Coastguard Worker 
BitrateProber(const FieldTrialsView & field_trials)47*d9f75844SAndroid Build Coastguard Worker BitrateProber::BitrateProber(const FieldTrialsView& field_trials)
48*d9f75844SAndroid Build Coastguard Worker     : probing_state_(ProbingState::kDisabled),
49*d9f75844SAndroid Build Coastguard Worker       next_probe_time_(Timestamp::PlusInfinity()),
50*d9f75844SAndroid Build Coastguard Worker       total_probe_count_(0),
51*d9f75844SAndroid Build Coastguard Worker       total_failed_probe_count_(0),
52*d9f75844SAndroid Build Coastguard Worker       config_(&field_trials) {
53*d9f75844SAndroid Build Coastguard Worker   SetEnabled(true);
54*d9f75844SAndroid Build Coastguard Worker }
55*d9f75844SAndroid Build Coastguard Worker 
SetEnabled(bool enable)56*d9f75844SAndroid Build Coastguard Worker void BitrateProber::SetEnabled(bool enable) {
57*d9f75844SAndroid Build Coastguard Worker   if (enable) {
58*d9f75844SAndroid Build Coastguard Worker     if (probing_state_ == ProbingState::kDisabled) {
59*d9f75844SAndroid Build Coastguard Worker       probing_state_ = ProbingState::kInactive;
60*d9f75844SAndroid Build Coastguard Worker       RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
61*d9f75844SAndroid Build Coastguard Worker     }
62*d9f75844SAndroid Build Coastguard Worker   } else {
63*d9f75844SAndroid Build Coastguard Worker     probing_state_ = ProbingState::kDisabled;
64*d9f75844SAndroid Build Coastguard Worker     RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
65*d9f75844SAndroid Build Coastguard Worker   }
66*d9f75844SAndroid Build Coastguard Worker }
67*d9f75844SAndroid Build Coastguard Worker 
OnIncomingPacket(DataSize packet_size)68*d9f75844SAndroid Build Coastguard Worker void BitrateProber::OnIncomingPacket(DataSize packet_size) {
69*d9f75844SAndroid Build Coastguard Worker   // Don't initialize probing unless we have something large enough to start
70*d9f75844SAndroid Build Coastguard Worker   // probing.
71*d9f75844SAndroid Build Coastguard Worker   // Note that the pacer can send several packets at once when sending a probe,
72*d9f75844SAndroid Build Coastguard Worker   // and thus, packets can be smaller than needed for a probe.
73*d9f75844SAndroid Build Coastguard Worker   if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
74*d9f75844SAndroid Build Coastguard Worker       packet_size >=
75*d9f75844SAndroid Build Coastguard Worker           std::min(RecommendedMinProbeSize(), config_.min_packet_size.Get())) {
76*d9f75844SAndroid Build Coastguard Worker     // Send next probe right away.
77*d9f75844SAndroid Build Coastguard Worker     next_probe_time_ = Timestamp::MinusInfinity();
78*d9f75844SAndroid Build Coastguard Worker     probing_state_ = ProbingState::kActive;
79*d9f75844SAndroid Build Coastguard Worker   }
80*d9f75844SAndroid Build Coastguard Worker }
81*d9f75844SAndroid Build Coastguard Worker 
CreateProbeCluster(const ProbeClusterConfig & cluster_config)82*d9f75844SAndroid Build Coastguard Worker void BitrateProber::CreateProbeCluster(
83*d9f75844SAndroid Build Coastguard Worker     const ProbeClusterConfig& cluster_config) {
84*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker   total_probe_count_++;
87*d9f75844SAndroid Build Coastguard Worker   while (!clusters_.empty() &&
88*d9f75844SAndroid Build Coastguard Worker          (cluster_config.at_time - clusters_.front().requested_at >
89*d9f75844SAndroid Build Coastguard Worker               kProbeClusterTimeout ||
90*d9f75844SAndroid Build Coastguard Worker           clusters_.size() > kMaxPendingProbeClusters)) {
91*d9f75844SAndroid Build Coastguard Worker     clusters_.pop();
92*d9f75844SAndroid Build Coastguard Worker     total_failed_probe_count_++;
93*d9f75844SAndroid Build Coastguard Worker   }
94*d9f75844SAndroid Build Coastguard Worker 
95*d9f75844SAndroid Build Coastguard Worker   ProbeCluster cluster;
96*d9f75844SAndroid Build Coastguard Worker   cluster.requested_at = cluster_config.at_time;
97*d9f75844SAndroid Build Coastguard Worker   cluster.pace_info.probe_cluster_min_probes =
98*d9f75844SAndroid Build Coastguard Worker       cluster_config.target_probe_count;
99*d9f75844SAndroid Build Coastguard Worker   cluster.pace_info.probe_cluster_min_bytes =
100*d9f75844SAndroid Build Coastguard Worker       (cluster_config.target_data_rate * cluster_config.target_duration)
101*d9f75844SAndroid Build Coastguard Worker           .bytes();
102*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0);
103*d9f75844SAndroid Build Coastguard Worker   cluster.pace_info.send_bitrate_bps = cluster_config.target_data_rate.bps();
104*d9f75844SAndroid Build Coastguard Worker   cluster.pace_info.probe_cluster_id = cluster_config.id;
105*d9f75844SAndroid Build Coastguard Worker   clusters_.push(cluster);
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker   RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
108*d9f75844SAndroid Build Coastguard Worker                    << cluster.pace_info.send_bitrate_bps << ":"
109*d9f75844SAndroid Build Coastguard Worker                    << cluster.pace_info.probe_cluster_min_bytes << ":"
110*d9f75844SAndroid Build Coastguard Worker                    << cluster.pace_info.probe_cluster_min_probes << ")";
111*d9f75844SAndroid Build Coastguard Worker 
112*d9f75844SAndroid Build Coastguard Worker   // If we are already probing, continue to do so. Otherwise set it to
113*d9f75844SAndroid Build Coastguard Worker   // kInactive and wait for OnIncomingPacket to start the probing.
114*d9f75844SAndroid Build Coastguard Worker   if (probing_state_ != ProbingState::kActive)
115*d9f75844SAndroid Build Coastguard Worker     probing_state_ = ProbingState::kInactive;
116*d9f75844SAndroid Build Coastguard Worker }
117*d9f75844SAndroid Build Coastguard Worker 
NextProbeTime(Timestamp now) const118*d9f75844SAndroid Build Coastguard Worker Timestamp BitrateProber::NextProbeTime(Timestamp now) const {
119*d9f75844SAndroid Build Coastguard Worker   // Probing is not active or probing is already complete.
120*d9f75844SAndroid Build Coastguard Worker   if (probing_state_ != ProbingState::kActive || clusters_.empty()) {
121*d9f75844SAndroid Build Coastguard Worker     return Timestamp::PlusInfinity();
122*d9f75844SAndroid Build Coastguard Worker   }
123*d9f75844SAndroid Build Coastguard Worker 
124*d9f75844SAndroid Build Coastguard Worker   return next_probe_time_;
125*d9f75844SAndroid Build Coastguard Worker }
126*d9f75844SAndroid Build Coastguard Worker 
CurrentCluster(Timestamp now)127*d9f75844SAndroid Build Coastguard Worker absl::optional<PacedPacketInfo> BitrateProber::CurrentCluster(Timestamp now) {
128*d9f75844SAndroid Build Coastguard Worker   if (clusters_.empty() || probing_state_ != ProbingState::kActive) {
129*d9f75844SAndroid Build Coastguard Worker     return absl::nullopt;
130*d9f75844SAndroid Build Coastguard Worker   }
131*d9f75844SAndroid Build Coastguard Worker 
132*d9f75844SAndroid Build Coastguard Worker   if (next_probe_time_.IsFinite() &&
133*d9f75844SAndroid Build Coastguard Worker       now - next_probe_time_ > config_.max_probe_delay.Get()) {
134*d9f75844SAndroid Build Coastguard Worker     RTC_DLOG(LS_WARNING) << "Probe delay too high"
135*d9f75844SAndroid Build Coastguard Worker                             " (next_ms:"
136*d9f75844SAndroid Build Coastguard Worker                          << next_probe_time_.ms() << ", now_ms: " << now.ms()
137*d9f75844SAndroid Build Coastguard Worker                          << "), discarding probe cluster.";
138*d9f75844SAndroid Build Coastguard Worker     clusters_.pop();
139*d9f75844SAndroid Build Coastguard Worker     if (clusters_.empty()) {
140*d9f75844SAndroid Build Coastguard Worker       probing_state_ = ProbingState::kSuspended;
141*d9f75844SAndroid Build Coastguard Worker       return absl::nullopt;
142*d9f75844SAndroid Build Coastguard Worker     }
143*d9f75844SAndroid Build Coastguard Worker   }
144*d9f75844SAndroid Build Coastguard Worker 
145*d9f75844SAndroid Build Coastguard Worker   PacedPacketInfo info = clusters_.front().pace_info;
146*d9f75844SAndroid Build Coastguard Worker   info.probe_cluster_bytes_sent = clusters_.front().sent_bytes;
147*d9f75844SAndroid Build Coastguard Worker   return info;
148*d9f75844SAndroid Build Coastguard Worker }
149*d9f75844SAndroid Build Coastguard Worker 
RecommendedMinProbeSize() const150*d9f75844SAndroid Build Coastguard Worker DataSize BitrateProber::RecommendedMinProbeSize() const {
151*d9f75844SAndroid Build Coastguard Worker   if (clusters_.empty()) {
152*d9f75844SAndroid Build Coastguard Worker     return DataSize::Zero();
153*d9f75844SAndroid Build Coastguard Worker   }
154*d9f75844SAndroid Build Coastguard Worker   DataRate send_rate =
155*d9f75844SAndroid Build Coastguard Worker       DataRate::BitsPerSec(clusters_.front().pace_info.send_bitrate_bps);
156*d9f75844SAndroid Build Coastguard Worker   return send_rate * config_.min_probe_delta;
157*d9f75844SAndroid Build Coastguard Worker }
158*d9f75844SAndroid Build Coastguard Worker 
ProbeSent(Timestamp now,DataSize size)159*d9f75844SAndroid Build Coastguard Worker void BitrateProber::ProbeSent(Timestamp now, DataSize size) {
160*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(probing_state_ == ProbingState::kActive);
161*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(!size.IsZero());
162*d9f75844SAndroid Build Coastguard Worker 
163*d9f75844SAndroid Build Coastguard Worker   if (!clusters_.empty()) {
164*d9f75844SAndroid Build Coastguard Worker     ProbeCluster* cluster = &clusters_.front();
165*d9f75844SAndroid Build Coastguard Worker     if (cluster->sent_probes == 0) {
166*d9f75844SAndroid Build Coastguard Worker       RTC_DCHECK(cluster->started_at.IsInfinite());
167*d9f75844SAndroid Build Coastguard Worker       cluster->started_at = now;
168*d9f75844SAndroid Build Coastguard Worker     }
169*d9f75844SAndroid Build Coastguard Worker     cluster->sent_bytes += size.bytes<int>();
170*d9f75844SAndroid Build Coastguard Worker     cluster->sent_probes += 1;
171*d9f75844SAndroid Build Coastguard Worker     next_probe_time_ = CalculateNextProbeTime(*cluster);
172*d9f75844SAndroid Build Coastguard Worker     if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
173*d9f75844SAndroid Build Coastguard Worker         cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
174*d9f75844SAndroid Build Coastguard Worker       RTC_HISTOGRAM_COUNTS_100000("WebRTC.BWE.Probing.ProbeClusterSizeInBytes",
175*d9f75844SAndroid Build Coastguard Worker                                   cluster->sent_bytes);
176*d9f75844SAndroid Build Coastguard Worker       RTC_HISTOGRAM_COUNTS_100("WebRTC.BWE.Probing.ProbesPerCluster",
177*d9f75844SAndroid Build Coastguard Worker                                cluster->sent_probes);
178*d9f75844SAndroid Build Coastguard Worker       RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.Probing.TimePerProbeCluster",
179*d9f75844SAndroid Build Coastguard Worker                                  (now - cluster->started_at).ms());
180*d9f75844SAndroid Build Coastguard Worker 
181*d9f75844SAndroid Build Coastguard Worker       clusters_.pop();
182*d9f75844SAndroid Build Coastguard Worker     }
183*d9f75844SAndroid Build Coastguard Worker     if (clusters_.empty()) {
184*d9f75844SAndroid Build Coastguard Worker       probing_state_ = ProbingState::kSuspended;
185*d9f75844SAndroid Build Coastguard Worker     }
186*d9f75844SAndroid Build Coastguard Worker   }
187*d9f75844SAndroid Build Coastguard Worker }
188*d9f75844SAndroid Build Coastguard Worker 
CalculateNextProbeTime(const ProbeCluster & cluster) const189*d9f75844SAndroid Build Coastguard Worker Timestamp BitrateProber::CalculateNextProbeTime(
190*d9f75844SAndroid Build Coastguard Worker     const ProbeCluster& cluster) const {
191*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
192*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(cluster.started_at.IsFinite());
193*d9f75844SAndroid Build Coastguard Worker 
194*d9f75844SAndroid Build Coastguard Worker   // Compute the time delta from the cluster start to ensure probe bitrate stays
195*d9f75844SAndroid Build Coastguard Worker   // close to the target bitrate. Result is in milliseconds.
196*d9f75844SAndroid Build Coastguard Worker   DataSize sent_bytes = DataSize::Bytes(cluster.sent_bytes);
197*d9f75844SAndroid Build Coastguard Worker   DataRate send_bitrate =
198*d9f75844SAndroid Build Coastguard Worker       DataRate::BitsPerSec(cluster.pace_info.send_bitrate_bps);
199*d9f75844SAndroid Build Coastguard Worker 
200*d9f75844SAndroid Build Coastguard Worker   TimeDelta delta = sent_bytes / send_bitrate;
201*d9f75844SAndroid Build Coastguard Worker   return cluster.started_at + delta;
202*d9f75844SAndroid Build Coastguard Worker }
203*d9f75844SAndroid Build Coastguard Worker 
204*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
205