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