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