| 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 "api/units/data_size.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
| 18 |
| 19 | namespace webrtc { |
| 20 |
| 21 | namespace { |
| 22 | constexpr TimeDelta kProbeClusterTimeout = TimeDelta::Seconds(5); |
| 23 | constexpr size_t kMaxPendingProbeClusters = 5; |
| 24 |
| 25 | } // namespace |
| 26 |
| 27 | BitrateProberConfig::BitrateProberConfig( |
| 28 | const FieldTrialsView* key_value_config) |
| 29 | : min_probe_delta("min_probe_delta", TimeDelta::Millis(2)), |
| 30 | max_probe_delay("max_probe_delay", TimeDelta::Millis(10)), |
| 31 | min_packet_size("min_packet_size", DataSize::Bytes(200)) { |
| 32 | ParseFieldTrial({&min_probe_delta, &max_probe_delay, &min_packet_size}, |
| 33 | key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior")); |
| 34 | } |
| 35 |
| 36 | BitrateProber::BitrateProber(const FieldTrialsView& field_trials) |
| 37 | : probing_state_(ProbingState::kDisabled), |
| 38 | next_probe_time_(Timestamp::PlusInfinity()), |
| 39 | config_(&field_trials) { |
| 40 | SetEnabled(true); |
| 41 | } |
| 42 |
| 43 | void BitrateProber::SetEnabled(bool enable) { |
| 44 | if (enable) { |
| 45 | if (probing_state_ == ProbingState::kDisabled) { |
| 46 | probing_state_ = ProbingState::kInactive; |
| 47 | RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; |
| 48 | } |
| 49 | } else { |
| 50 | probing_state_ = ProbingState::kDisabled; |
| 51 | RTC_LOG(LS_INFO) << "Bandwidth probing disabled"; |
| 52 | } |
| 53 | } |
| 54 |
| 55 | void BitrateProber::SetAllowProbeWithoutMediaPacket(bool allow) { |
| 56 | config_.allow_start_probing_immediately = allow; |
| 57 | MaybeSetActiveState(/*packet_size=*/DataSize::Zero()); |
| 58 | } |
| 59 |
| 60 | void BitrateProber::MaybeSetActiveState(DataSize packet_size) { |
| 61 | if (ReadyToSetActiveState(packet_size)) { |
| 62 | next_probe_time_ = Timestamp::MinusInfinity(); |
| 63 | probing_state_ = ProbingState::kActive; |
| 64 | } |
| 65 | } |
| 66 |
| 67 | bool BitrateProber::ReadyToSetActiveState(DataSize packet_size) const { |
| 68 | if (clusters_.empty()) { |
| 69 | RTC_DCHECK(probing_state_ == ProbingState::kDisabled || |
| 70 | probing_state_ == ProbingState::kInactive); |
| 71 | return false; |
| 72 | } |
| 73 | switch (probing_state_) { |
| 74 | case ProbingState::kDisabled: |
| 75 | case ProbingState::kActive: |
| 76 | return false; |
| 77 | case ProbingState::kInactive: |
| 78 | if (config_.allow_start_probing_immediately) { |
| 79 | return true; |
| 80 | } |
| 81 | // If config_.min_packet_size > 0, a "large enough" packet must be |
| 82 | // sent first, before a probe can be generated and sent. Otherwise, |
| 83 | // send the probe asap. |
| 84 | return packet_size >= |
| 85 | std::min(RecommendedMinProbeSize(), config_.min_packet_size.Get()); |
| 86 | } |
| 87 | } |
| 88 |
| 89 | void BitrateProber::OnIncomingPacket(DataSize packet_size) { |
| 90 | MaybeSetActiveState(packet_size); |
| 91 | } |
| 92 |
| 93 | void BitrateProber::CreateProbeCluster( |
| 94 | const ProbeClusterConfig& cluster_config) { |
| 95 | RTC_DCHECK(probing_state_ != ProbingState::kDisabled); |
| 96 |
| 97 | while (!clusters_.empty() && |
| 98 | (cluster_config.at_time - clusters_.front().requested_at > |
| 99 | kProbeClusterTimeout || |
| 100 | clusters_.size() > kMaxPendingProbeClusters)) { |
| 101 | clusters_.pop(); |
| 102 | } |
| 103 |
| 104 | ProbeCluster cluster; |
| 105 | cluster.requested_at = cluster_config.at_time; |
| 106 | cluster.pace_info.probe_cluster_min_probes = |
| 107 | cluster_config.target_probe_count; |
| 108 | cluster.pace_info.probe_cluster_min_bytes = |
| 109 | (cluster_config.target_data_rate * cluster_config.target_duration) |
| 110 | .bytes(); |
| 111 | RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0); |
| 112 | cluster.pace_info.send_bitrate = cluster_config.target_data_rate; |
| 113 | cluster.pace_info.probe_cluster_id = cluster_config.id; |
| 114 | clusters_.push(cluster); |
| 115 |
| 116 | MaybeSetActiveState(/*packet_size=*/DataSize::Zero()); |
| 117 |
| 118 | RTC_DCHECK(probing_state_ == ProbingState::kActive || |
| 119 | probing_state_ == ProbingState::kInactive); |
| 120 |
| 121 | RTC_LOG(LS_INFO) << "Probe cluster (bitrate_bps:min bytes:min packets): (" |
| 122 | << cluster.pace_info.send_bitrate << ":" |
| 123 | << cluster.pace_info.probe_cluster_min_bytes << ":" |
| 124 | << cluster.pace_info.probe_cluster_min_probes << ", " |
| 125 | << (probing_state_ == ProbingState::kInactive ? "Inactive" |
| 126 | : "Active") |
| 127 | << ")"; |
| 128 | } |
| 129 |
| 130 | Timestamp BitrateProber::NextProbeTime(Timestamp now) const { |
| 131 | // Probing is not active or probing is already complete. |
| 132 | if (probing_state_ != ProbingState::kActive || clusters_.empty()) { |
| 133 | return Timestamp::PlusInfinity(); |
| 134 | } |
| 135 |
| 136 | return next_probe_time_; |
| 137 | } |
| 138 |
| 139 | absl::optional<PacedPacketInfo> BitrateProber::CurrentCluster(Timestamp now) { |
| 140 | if (clusters_.empty() || probing_state_ != ProbingState::kActive) { |
| 141 | return absl::nullopt; |
| 142 | } |
| 143 |
| 144 | if (next_probe_time_.IsFinite() && |
| 145 | now - next_probe_time_ > config_.max_probe_delay.Get()) { |
| 146 | RTC_DLOG(LS_WARNING) << "Probe delay too high" |
| 147 | " (next_ms:" |
| 148 | << next_probe_time_.ms() << ", now_ms: " << now.ms() |
| 149 | << "), discarding probe cluster."; |
| 150 | clusters_.pop(); |
| 151 | if (clusters_.empty()) { |
| 152 | probing_state_ = ProbingState::kInactive; |
| 153 | return absl::nullopt; |
| 154 | } |
| 155 | } |
| 156 |
| 157 | PacedPacketInfo info = clusters_.front().pace_info; |
| 158 | info.probe_cluster_bytes_sent = clusters_.front().sent_bytes; |
| 159 | return info; |
| 160 | } |
| 161 |
| 162 | DataSize BitrateProber::RecommendedMinProbeSize() const { |
| 163 | if (clusters_.empty()) { |
| 164 | return DataSize::Zero(); |
| 165 | } |
| 166 | DataRate send_rate = clusters_.front().pace_info.send_bitrate; |
| 167 | return send_rate * config_.min_probe_delta; |
| 168 | } |
| 169 |
| 170 | void BitrateProber::ProbeSent(Timestamp now, DataSize size) { |
| 171 | RTC_DCHECK(probing_state_ == ProbingState::kActive); |
| 172 | RTC_DCHECK(!size.IsZero()); |
| 173 |
| 174 | if (!clusters_.empty()) { |
| 175 | ProbeCluster* cluster = &clusters_.front(); |
| 176 | if (cluster->sent_probes == 0) { |
| 177 | RTC_DCHECK(cluster->started_at.IsInfinite()); |
| 178 | cluster->started_at = now; |
| 179 | } |
| 180 | cluster->sent_bytes += size.bytes<int>(); |
| 181 | cluster->sent_probes += 1; |
| 182 | next_probe_time_ = CalculateNextProbeTime(*cluster); |
| 183 | if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes && |
| 184 | cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) { |
| 185 | clusters_.pop(); |
| 186 | } |
| 187 | if (clusters_.empty()) { |
| 188 | probing_state_ = ProbingState::kInactive; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 |
| 193 | Timestamp BitrateProber::CalculateNextProbeTime( |
| 194 | const ProbeCluster& cluster) const { |
| 195 | RTC_CHECK_GT(cluster.pace_info.send_bitrate.bps(), 0); |
| 196 | RTC_CHECK(cluster.started_at.IsFinite()); |
| 197 |
| 198 | // Compute the time delta from the cluster start to ensure probe bitrate stays |
| 199 | // close to the target bitrate. Result is in milliseconds. |
| 200 | DataSize sent_bytes = DataSize::Bytes(cluster.sent_bytes); |
| 201 | DataRate send_bitrate = cluster.pace_info.send_bitrate; |
| 202 |
| 203 | TimeDelta delta = sent_bytes / send_bitrate; |
| 204 | return cluster.started_at + delta; |
| 205 | } |
| 206 |
| 207 | } // namespace webrtc |
| 208 |