| 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 | // The min probe packet size is scaled with the bitrate we're probing at. |
| 27 | // This defines the max min probe packet size, meaning that on high bitrates |
| 28 | // we have a min probe packet size of 200 bytes. |
| 29 | constexpr DataSize kMinProbePacketSize = DataSize::Bytes(200); |
| 30 |
| 31 | constexpr TimeDelta kProbeClusterTimeout = TimeDelta::Seconds(5); |
| 32 |
| 33 | } // namespace |
| 34 |
| 35 | BitrateProberConfig::BitrateProberConfig( |
| 36 | const WebRtcKeyValueConfig* key_value_config) |
| 37 | : min_probe_packets_sent("min_probe_packets_sent", 5), |
| 38 | min_probe_delta("min_probe_delta", TimeDelta::Millis(1)), |
| 39 | min_probe_duration("min_probe_duration", TimeDelta::Millis(15)), |
| 40 | max_probe_delay("max_probe_delay", TimeDelta::Millis(10)), |
| 41 | abort_delayed_probes("abort_delayed_probes", true) { |
| 42 | ParseFieldTrial( |
| 43 | {&min_probe_packets_sent, &min_probe_delta, &min_probe_duration, |
| 44 | &max_probe_delay, &abort_delayed_probes}, |
| 45 | key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration")); |
| 46 | ParseFieldTrial( |
| 47 | {&min_probe_packets_sent, &min_probe_delta, &min_probe_duration, |
| 48 | &max_probe_delay, &abort_delayed_probes}, |
| 49 | key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior")); |
| 50 | } |
| 51 |
| 52 | BitrateProber::~BitrateProber() { |
| 53 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalProbeClustersRequested", |
| 54 | total_probe_count_); |
| 55 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.BWE.Probing.TotalFailedProbeClusters", |
| 56 | total_failed_probe_count_); |
| 57 | } |
| 58 |
| 59 | BitrateProber::BitrateProber(const WebRtcKeyValueConfig& field_trials) |
| 60 | : probing_state_(ProbingState::kDisabled), |
| 61 | next_probe_time_(Timestamp::PlusInfinity()), |
| 62 | total_probe_count_(0), |
| 63 | total_failed_probe_count_(0), |
| 64 | config_(&field_trials) { |
| 65 | SetEnabled(true); |
| 66 | } |
| 67 |
| 68 | void BitrateProber::SetEnabled(bool enable) { |
| 69 | if (enable) { |
| 70 | if (probing_state_ == ProbingState::kDisabled) { |
| 71 | probing_state_ = ProbingState::kInactive; |
| 72 | RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; |
| 73 | } |
| 74 | } else { |
| 75 | probing_state_ = ProbingState::kDisabled; |
| 76 | RTC_LOG(LS_INFO) << "Bandwidth probing disabled"; |
| 77 | } |
| 78 | } |
| 79 |
| 80 | void BitrateProber::OnIncomingPacket(DataSize packet_size) { |
| 81 | // Don't initialize probing unless we have something large enough to start |
| 82 | // probing. |
| 83 | if (probing_state_ == ProbingState::kInactive && !clusters_.empty() && |
| 84 | packet_size >= std::min(RecommendedMinProbeSize(), kMinProbePacketSize)) { |
| 85 | // Send next probe right away. |
| 86 | next_probe_time_ = Timestamp::MinusInfinity(); |
| 87 | probing_state_ = ProbingState::kActive; |
| 88 | } |
| 89 | } |
| 90 |
| 91 | void BitrateProber::CreateProbeCluster(DataRate bitrate, |
| 92 | Timestamp now, |
| 93 | int cluster_id) { |
| 94 | RTC_DCHECK(probing_state_ != ProbingState::kDisabled); |
| 95 | RTC_DCHECK_GT(bitrate, DataRate::Zero()); |
| 96 |
| 97 | total_probe_count_++; |
| 98 | while (!clusters_.empty() && |
| 99 | now - clusters_.front().created_at > kProbeClusterTimeout) { |
| 100 | clusters_.pop(); |
| 101 | total_failed_probe_count_++; |
| 102 | } |
| 103 |
| 104 | ProbeCluster cluster; |
| 105 | cluster.created_at = now; |
| 106 | cluster.pace_info.probe_cluster_min_probes = config_.min_probe_packets_sent; |
| 107 | cluster.pace_info.probe_cluster_min_bytes = |
| 108 | (bitrate * config_.min_probe_duration.Get()).bytes(); |
| 109 | RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0); |
| 110 | cluster.pace_info.send_bitrate_bps = bitrate.bps(); |
| 111 | cluster.pace_info.probe_cluster_id = cluster_id; |
| 112 | clusters_.push(cluster); |
| 113 |
| 114 | RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" |
| 115 | << cluster.pace_info.send_bitrate_bps << ":" |
| 116 | << cluster.pace_info.probe_cluster_min_bytes << ":" |
| 117 | << cluster.pace_info.probe_cluster_min_probes << ")"; |
| 118 | // If we are already probing, continue to do so. Otherwise set it to |
| 119 | // kInactive and wait for OnIncomingPacket to start the probing. |
| 120 | if (probing_state_ != ProbingState::kActive) |
| 121 | probing_state_ = ProbingState::kInactive; |
| 122 | } |
| 123 |
| 124 | Timestamp BitrateProber::NextProbeTime(Timestamp now) const { |
| 125 | // Probing is not active or probing is already complete. |
| 126 | if (probing_state_ != ProbingState::kActive || clusters_.empty()) { |
| 127 | return Timestamp::PlusInfinity(); |
| 128 | } |
| 129 |
| 130 | // Legacy behavior, just warn about late probe and return as if not probing. |
| 131 | if (!config_.abort_delayed_probes && next_probe_time_.IsFinite() && |
| 132 | now - next_probe_time_ > config_.max_probe_delay.Get()) { |
| 133 | RTC_DLOG(LS_WARNING) << "Probe delay too high" |
| 134 | " (next_ms:" |
| 135 | << next_probe_time_.ms() << ", now_ms: " << now.ms() |
| 136 | << ")"; |
| 137 | return Timestamp::PlusInfinity(); |
| 138 | } |
| 139 |
| 140 | return next_probe_time_; |
| 141 | } |
| 142 |
| 143 | absl::optional<PacedPacketInfo> BitrateProber::CurrentCluster(Timestamp now) { |
| 144 | if (clusters_.empty() || probing_state_ != ProbingState::kActive) { |
| 145 | return absl::nullopt; |
| 146 | } |
| 147 |
| 148 | if (config_.abort_delayed_probes && next_probe_time_.IsFinite() && |
| 149 | now - next_probe_time_ > config_.max_probe_delay.Get()) { |
| 150 | RTC_DLOG(LS_WARNING) << "Probe delay too high" |
| 151 | " (next_ms:" |
| 152 | << next_probe_time_.ms() << ", now_ms: " << now.ms() |
| 153 | << "), discarding probe cluster."; |
| 154 | clusters_.pop(); |
| 155 | if (clusters_.empty()) { |
| 156 | probing_state_ = ProbingState::kSuspended; |
| 157 | return absl::nullopt; |
| 158 | } |
| 159 | } |
| 160 |
| 161 | PacedPacketInfo info = clusters_.front().pace_info; |
| 162 | info.probe_cluster_bytes_sent = clusters_.front().sent_bytes; |
| 163 | return info; |
| 164 | } |
| 165 |
| 166 | // Probe size is recommended based on the probe bitrate required. We choose |
| 167 | // a minimum of twice `kMinProbeDeltaMs` interval to allow scheduling to be |
| 168 | // feasible. |
| 169 | DataSize BitrateProber::RecommendedMinProbeSize() const { |
| 170 | if (clusters_.empty()) { |
| 171 | return DataSize::Zero(); |
| 172 | } |
| 173 | DataRate send_rate = |
| 174 | DataRate::BitsPerSec(clusters_.front().pace_info.send_bitrate_bps); |
| 175 | return 2 * send_rate * config_.min_probe_delta; |
| 176 | } |
| 177 |
| 178 | void BitrateProber::ProbeSent(Timestamp now, DataSize size) { |
| 179 | RTC_DCHECK(probing_state_ == ProbingState::kActive); |
| 180 | RTC_DCHECK(!size.IsZero()); |
| 181 |
| 182 | if (!clusters_.empty()) { |
| 183 | ProbeCluster* cluster = &clusters_.front(); |
| 184 | if (cluster->sent_probes == 0) { |
| 185 | RTC_DCHECK(cluster->started_at.IsInfinite()); |
| 186 | cluster->started_at = now; |
| 187 | } |
| 188 | cluster->sent_bytes += size.bytes<int>(); |
| 189 | cluster->sent_probes += 1; |
| 190 | next_probe_time_ = CalculateNextProbeTime(*cluster); |
| 191 | if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes && |
| 192 | cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) { |
| 193 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.BWE.Probing.ProbeClusterSizeInBytes", |
| 194 | cluster->sent_bytes); |
| 195 | RTC_HISTOGRAM_COUNTS_100("WebRTC.BWE.Probing.ProbesPerCluster", |
| 196 | cluster->sent_probes); |
| 197 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.Probing.TimePerProbeCluster", |
| 198 | (now - cluster->started_at).ms()); |
| 199 |
| 200 | clusters_.pop(); |
| 201 | } |
| 202 | if (clusters_.empty()) { |
| 203 | probing_state_ = ProbingState::kSuspended; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 |
| 208 | Timestamp BitrateProber::CalculateNextProbeTime( |
| 209 | const ProbeCluster& cluster) const { |
| 210 | RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0); |
| 211 | RTC_CHECK(cluster.started_at.IsFinite()); |
| 212 |
| 213 | // Compute the time delta from the cluster start to ensure probe bitrate stays |
| 214 | // close to the target bitrate. Result is in milliseconds. |
| 215 | DataSize sent_bytes = DataSize::Bytes(cluster.sent_bytes); |
| 216 | DataRate send_bitrate = |
| 217 | DataRate::BitsPerSec(cluster.pace_info.send_bitrate_bps); |
| 218 | TimeDelta delta = sent_bytes / send_bitrate; |
| 219 | return cluster.started_at + delta; |
| 220 | } |
| 221 |
| 222 | } // namespace webrtc |
| 223 |