philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 "webrtc/modules/congestion_controller/delay_based_bwe.h" |
| 12 | |
| 13 | #include <math.h> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | |
| 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/base/constructormagic.h" |
| 19 | #include "webrtc/base/logging.h" |
| 20 | #include "webrtc/base/thread_annotations.h" |
| 21 | #include "webrtc/modules/pacing/paced_sender.h" |
| 22 | #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
| 23 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame^] | 24 | #include "webrtc/system_wrappers/include/metrics.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 25 | #include "webrtc/typedefs.h" |
| 26 | |
| 27 | namespace { |
| 28 | enum { |
| 29 | kTimestampGroupLengthMs = 5, |
| 30 | kAbsSendTimeFraction = 18, |
| 31 | kAbsSendTimeInterArrivalUpshift = 8, |
| 32 | kInterArrivalShift = kAbsSendTimeFraction + kAbsSendTimeInterArrivalUpshift, |
| 33 | kInitialProbingIntervalMs = 2000, |
| 34 | kMinClusterSize = 4, |
| 35 | kMaxProbePackets = 15, |
| 36 | kExpectedNumberOfProbes = 3 |
| 37 | }; |
| 38 | |
| 39 | static const double kTimestampToMs = |
| 40 | 1000.0 / static_cast<double>(1 << kInterArrivalShift); |
| 41 | |
| 42 | template <typename K, typename V> |
| 43 | std::vector<K> Keys(const std::map<K, V>& map) { |
| 44 | std::vector<K> keys; |
| 45 | keys.reserve(map.size()); |
| 46 | for (typename std::map<K, V>::const_iterator it = map.begin(); |
| 47 | it != map.end(); ++it) { |
| 48 | keys.push_back(it->first); |
| 49 | } |
| 50 | return keys; |
| 51 | } |
| 52 | |
| 53 | uint32_t ConvertMsTo24Bits(int64_t time_ms) { |
| 54 | uint32_t time_24_bits = |
| 55 | static_cast<uint32_t>( |
| 56 | ((static_cast<uint64_t>(time_ms) << kAbsSendTimeFraction) + 500) / |
| 57 | 1000) & |
| 58 | 0x00FFFFFF; |
| 59 | return time_24_bits; |
| 60 | } |
| 61 | } // namespace |
| 62 | |
| 63 | namespace webrtc { |
| 64 | |
| 65 | void DelayBasedBwe::AddCluster(std::list<Cluster>* clusters, Cluster* cluster) { |
| 66 | cluster->send_mean_ms /= static_cast<float>(cluster->count); |
| 67 | cluster->recv_mean_ms /= static_cast<float>(cluster->count); |
| 68 | cluster->mean_size /= cluster->count; |
| 69 | clusters->push_back(*cluster); |
| 70 | } |
| 71 | |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 72 | DelayBasedBwe::DelayBasedBwe(RemoteBitrateObserver* observer, Clock* clock) |
| 73 | : clock_(clock), |
| 74 | observer_(observer), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 75 | inter_arrival_(), |
| 76 | estimator_(), |
| 77 | detector_(OverUseDetectorOptions()), |
| 78 | incoming_bitrate_(kBitrateWindowMs, 8000), |
| 79 | total_probes_received_(0), |
| 80 | first_packet_time_ms_(-1), |
| 81 | last_update_ms_(-1), |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame^] | 82 | uma_recorded_(false) { |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 83 | RTC_DCHECK(observer_); |
| 84 | // NOTE! The BitrateEstimatorTest relies on this EXACT log line. |
| 85 | LOG(LS_INFO) << "RemoteBitrateEstimatorAbsSendTime: Instantiating."; |
| 86 | network_thread_.DetachFromThread(); |
| 87 | } |
| 88 | |
| 89 | void DelayBasedBwe::ComputeClusters(std::list<Cluster>* clusters) const { |
| 90 | Cluster current; |
| 91 | int64_t prev_send_time = -1; |
| 92 | int64_t prev_recv_time = -1; |
| 93 | int last_probe_cluster_id = -1; |
| 94 | for (std::list<Probe>::const_iterator it = probes_.begin(); |
| 95 | it != probes_.end(); ++it) { |
| 96 | if (last_probe_cluster_id == -1) |
| 97 | last_probe_cluster_id = it->cluster_id; |
| 98 | if (prev_send_time >= 0) { |
| 99 | int send_delta_ms = it->send_time_ms - prev_send_time; |
| 100 | int recv_delta_ms = it->recv_time_ms - prev_recv_time; |
| 101 | if (send_delta_ms >= 1 && recv_delta_ms >= 1) { |
| 102 | ++current.num_above_min_delta; |
| 103 | } |
| 104 | if (it->cluster_id != last_probe_cluster_id) { |
| 105 | if (current.count >= kMinClusterSize) |
| 106 | AddCluster(clusters, ¤t); |
| 107 | current = Cluster(); |
| 108 | } |
| 109 | current.send_mean_ms += send_delta_ms; |
| 110 | current.recv_mean_ms += recv_delta_ms; |
| 111 | current.mean_size += it->payload_size; |
| 112 | ++current.count; |
| 113 | last_probe_cluster_id = it->cluster_id; |
| 114 | } |
| 115 | prev_send_time = it->send_time_ms; |
| 116 | prev_recv_time = it->recv_time_ms; |
| 117 | } |
| 118 | if (current.count >= kMinClusterSize) |
| 119 | AddCluster(clusters, ¤t); |
| 120 | } |
| 121 | |
| 122 | std::list<DelayBasedBwe::Cluster>::const_iterator DelayBasedBwe::FindBestProbe( |
| 123 | const std::list<Cluster>& clusters) const { |
| 124 | int highest_probe_bitrate_bps = 0; |
| 125 | std::list<Cluster>::const_iterator best_it = clusters.end(); |
| 126 | for (std::list<Cluster>::const_iterator it = clusters.begin(); |
| 127 | it != clusters.end(); ++it) { |
| 128 | if (it->send_mean_ms == 0 || it->recv_mean_ms == 0) |
| 129 | continue; |
| 130 | int send_bitrate_bps = it->mean_size * 8 * 1000 / it->send_mean_ms; |
| 131 | int recv_bitrate_bps = it->mean_size * 8 * 1000 / it->recv_mean_ms; |
| 132 | if (it->num_above_min_delta > it->count / 2 && |
| 133 | (it->recv_mean_ms - it->send_mean_ms <= 2.0f && |
| 134 | it->send_mean_ms - it->recv_mean_ms <= 5.0f)) { |
| 135 | int probe_bitrate_bps = |
| 136 | std::min(it->GetSendBitrateBps(), it->GetRecvBitrateBps()); |
| 137 | if (probe_bitrate_bps > highest_probe_bitrate_bps) { |
| 138 | highest_probe_bitrate_bps = probe_bitrate_bps; |
| 139 | best_it = it; |
| 140 | } |
| 141 | } else { |
| 142 | LOG(LS_INFO) << "Probe failed, sent at " << send_bitrate_bps |
| 143 | << " bps, received at " << recv_bitrate_bps |
| 144 | << " bps. Mean send delta: " << it->send_mean_ms |
| 145 | << " ms, mean recv delta: " << it->recv_mean_ms |
| 146 | << " ms, num probes: " << it->count; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | return best_it; |
| 151 | } |
| 152 | |
| 153 | DelayBasedBwe::ProbeResult DelayBasedBwe::ProcessClusters(int64_t now_ms) { |
| 154 | std::list<Cluster> clusters; |
| 155 | ComputeClusters(&clusters); |
| 156 | if (clusters.empty()) { |
| 157 | // If we reach the max number of probe packets and still have no clusters, |
| 158 | // we will remove the oldest one. |
| 159 | if (probes_.size() >= kMaxProbePackets) |
| 160 | probes_.pop_front(); |
| 161 | return ProbeResult::kNoUpdate; |
| 162 | } |
| 163 | |
| 164 | std::list<Cluster>::const_iterator best_it = FindBestProbe(clusters); |
| 165 | if (best_it != clusters.end()) { |
| 166 | int probe_bitrate_bps = |
| 167 | std::min(best_it->GetSendBitrateBps(), best_it->GetRecvBitrateBps()); |
| 168 | // Make sure that a probe sent on a lower bitrate than our estimate can't |
| 169 | // reduce the estimate. |
| 170 | if (IsBitrateImproving(probe_bitrate_bps)) { |
| 171 | LOG(LS_INFO) << "Probe successful, sent at " |
| 172 | << best_it->GetSendBitrateBps() << " bps, received at " |
| 173 | << best_it->GetRecvBitrateBps() |
| 174 | << " bps. Mean send delta: " << best_it->send_mean_ms |
| 175 | << " ms, mean recv delta: " << best_it->recv_mean_ms |
| 176 | << " ms, num probes: " << best_it->count; |
| 177 | remote_rate_.SetEstimate(probe_bitrate_bps, now_ms); |
| 178 | return ProbeResult::kBitrateUpdated; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Not probing and received non-probe packet, or finished with current set |
| 183 | // of probes. |
| 184 | if (clusters.size() >= kExpectedNumberOfProbes) |
| 185 | probes_.clear(); |
| 186 | return ProbeResult::kNoUpdate; |
| 187 | } |
| 188 | |
| 189 | bool DelayBasedBwe::IsBitrateImproving(int new_bitrate_bps) const { |
| 190 | bool initial_probe = !remote_rate_.ValidEstimate() && new_bitrate_bps > 0; |
| 191 | bool bitrate_above_estimate = |
| 192 | remote_rate_.ValidEstimate() && |
| 193 | new_bitrate_bps > static_cast<int>(remote_rate_.LatestEstimate()); |
| 194 | return initial_probe || bitrate_above_estimate; |
| 195 | } |
| 196 | |
| 197 | void DelayBasedBwe::IncomingPacketFeedbackVector( |
| 198 | const std::vector<PacketInfo>& packet_feedback_vector) { |
| 199 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
stefan | 64636dd | 2016-08-03 00:29:03 -0700 | [diff] [blame^] | 200 | if (!uma_recorded_) { |
| 201 | RTC_LOGGED_HISTOGRAM_ENUMERATION(kBweTypeHistogram, |
| 202 | BweNames::kSendSideTransportSeqNum, |
| 203 | BweNames::kBweNamesMax); |
| 204 | uma_recorded_ = true; |
| 205 | } |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 206 | for (const auto& packet_info : packet_feedback_vector) { |
| 207 | IncomingPacketInfo(packet_info.arrival_time_ms, |
| 208 | ConvertMsTo24Bits(packet_info.send_time_ms), |
pbos | 2169d8b | 2016-06-20 11:53:02 -0700 | [diff] [blame] | 209 | packet_info.payload_size, 0, |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 210 | packet_info.probe_cluster_id); |
| 211 | } |
| 212 | } |
| 213 | |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 214 | void DelayBasedBwe::IncomingPacketInfo(int64_t arrival_time_ms, |
| 215 | uint32_t send_time_24bits, |
| 216 | size_t payload_size, |
| 217 | uint32_t ssrc, |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 218 | int probe_cluster_id) { |
| 219 | assert(send_time_24bits < (1ul << 24)); |
| 220 | // Shift up send time to use the full 32 bits that inter_arrival works with, |
| 221 | // so wrapping works properly. |
| 222 | uint32_t timestamp = send_time_24bits << kAbsSendTimeInterArrivalUpshift; |
| 223 | int64_t send_time_ms = static_cast<int64_t>(timestamp) * kTimestampToMs; |
| 224 | |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 225 | int64_t now_ms = clock_->TimeInMilliseconds(); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 226 | // TODO(holmer): SSRCs are only needed for REMB, should be broken out from |
| 227 | // here. |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 228 | incoming_bitrate_.Update(payload_size, arrival_time_ms); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 229 | |
| 230 | if (first_packet_time_ms_ == -1) |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 231 | first_packet_time_ms_ = now_ms; |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 232 | |
| 233 | uint32_t ts_delta = 0; |
| 234 | int64_t t_delta = 0; |
| 235 | int size_delta = 0; |
| 236 | |
| 237 | bool update_estimate = false; |
| 238 | uint32_t target_bitrate_bps = 0; |
| 239 | std::vector<uint32_t> ssrcs; |
| 240 | { |
| 241 | rtc::CritScope lock(&crit_); |
| 242 | |
| 243 | TimeoutStreams(now_ms); |
| 244 | RTC_DCHECK(inter_arrival_.get()); |
| 245 | RTC_DCHECK(estimator_.get()); |
| 246 | ssrcs_[ssrc] = now_ms; |
| 247 | |
| 248 | // For now only try to detect probes while we don't have a valid estimate, |
| 249 | // and make sure the packet was paced. We currently assume that only packets |
| 250 | // larger than 200 bytes are paced by the sender. |
| 251 | if (probe_cluster_id != PacketInfo::kNotAProbe && |
| 252 | payload_size > PacedSender::kMinProbePacketSize && |
| 253 | (!remote_rate_.ValidEstimate() || |
| 254 | now_ms - first_packet_time_ms_ < kInitialProbingIntervalMs)) { |
| 255 | // TODO(holmer): Use a map instead to get correct order? |
| 256 | if (total_probes_received_ < kMaxProbePackets) { |
| 257 | int send_delta_ms = -1; |
| 258 | int recv_delta_ms = -1; |
| 259 | if (!probes_.empty()) { |
| 260 | send_delta_ms = send_time_ms - probes_.back().send_time_ms; |
| 261 | recv_delta_ms = arrival_time_ms - probes_.back().recv_time_ms; |
| 262 | } |
| 263 | LOG(LS_INFO) << "Probe packet received: send time=" << send_time_ms |
| 264 | << " ms, recv time=" << arrival_time_ms |
| 265 | << " ms, send delta=" << send_delta_ms |
| 266 | << " ms, recv delta=" << recv_delta_ms << " ms."; |
| 267 | } |
| 268 | probes_.push_back( |
| 269 | Probe(send_time_ms, arrival_time_ms, payload_size, probe_cluster_id)); |
| 270 | ++total_probes_received_; |
| 271 | // Make sure that a probe which updated the bitrate immediately has an |
| 272 | // effect by calling the OnReceiveBitrateChanged callback. |
| 273 | if (ProcessClusters(now_ms) == ProbeResult::kBitrateUpdated) |
| 274 | update_estimate = true; |
| 275 | } |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 276 | if (inter_arrival_->ComputeDeltas(timestamp, arrival_time_ms, now_ms, |
| 277 | payload_size, &ts_delta, &t_delta, |
| 278 | &size_delta)) { |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 279 | double ts_delta_ms = (1000.0 * ts_delta) / (1 << kInterArrivalShift); |
| 280 | estimator_->Update(t_delta, ts_delta_ms, size_delta, detector_.State()); |
| 281 | detector_.Detect(estimator_->offset(), ts_delta_ms, |
| 282 | estimator_->num_of_deltas(), arrival_time_ms); |
| 283 | } |
| 284 | |
| 285 | if (!update_estimate) { |
| 286 | // Check if it's time for a periodic update or if we should update because |
| 287 | // of an over-use. |
| 288 | if (last_update_ms_ == -1 || |
| 289 | now_ms - last_update_ms_ > remote_rate_.GetFeedbackInterval()) { |
| 290 | update_estimate = true; |
| 291 | } else if (detector_.State() == kBwOverusing) { |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 292 | rtc::Optional<uint32_t> incoming_rate = |
| 293 | incoming_bitrate_.Rate(arrival_time_ms); |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 294 | if (incoming_rate && |
| 295 | remote_rate_.TimeToReduceFurther(now_ms, *incoming_rate)) { |
| 296 | update_estimate = true; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (update_estimate) { |
| 302 | // The first overuse should immediately trigger a new estimate. |
| 303 | // We also have to update the estimate immediately if we are overusing |
| 304 | // and the target bitrate is too high compared to what we are receiving. |
| 305 | const RateControlInput input(detector_.State(), |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 306 | incoming_bitrate_.Rate(arrival_time_ms), |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 307 | estimator_->var_noise()); |
| 308 | remote_rate_.Update(&input, now_ms); |
| 309 | target_bitrate_bps = remote_rate_.UpdateBandwidthEstimate(now_ms); |
| 310 | update_estimate = remote_rate_.ValidEstimate(); |
| 311 | ssrcs = Keys(ssrcs_); |
| 312 | } |
| 313 | } |
| 314 | if (update_estimate) { |
| 315 | last_update_ms_ = now_ms; |
| 316 | observer_->OnReceiveBitrateChanged(ssrcs, target_bitrate_bps); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void DelayBasedBwe::Process() {} |
| 321 | |
| 322 | int64_t DelayBasedBwe::TimeUntilNextProcess() { |
| 323 | const int64_t kDisabledModuleTime = 1000; |
| 324 | return kDisabledModuleTime; |
| 325 | } |
| 326 | |
| 327 | void DelayBasedBwe::TimeoutStreams(int64_t now_ms) { |
| 328 | for (Ssrcs::iterator it = ssrcs_.begin(); it != ssrcs_.end();) { |
| 329 | if ((now_ms - it->second) > kStreamTimeOutMs) { |
| 330 | ssrcs_.erase(it++); |
| 331 | } else { |
| 332 | ++it; |
| 333 | } |
| 334 | } |
| 335 | if (ssrcs_.empty()) { |
| 336 | // We can't update the estimate if we don't have any active streams. |
| 337 | inter_arrival_.reset( |
| 338 | new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, |
| 339 | kTimestampToMs, true)); |
| 340 | estimator_.reset(new OveruseEstimator(OverUseDetectorOptions())); |
| 341 | // We deliberately don't reset the first_packet_time_ms_ here for now since |
| 342 | // we only probe for bandwidth in the beginning of a call right now. |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void DelayBasedBwe::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
| 347 | rtc::CritScope lock(&crit_); |
| 348 | remote_rate_.SetRtt(avg_rtt_ms); |
| 349 | } |
| 350 | |
| 351 | void DelayBasedBwe::RemoveStream(uint32_t ssrc) { |
| 352 | rtc::CritScope lock(&crit_); |
| 353 | ssrcs_.erase(ssrc); |
| 354 | } |
| 355 | |
| 356 | bool DelayBasedBwe::LatestEstimate(std::vector<uint32_t>* ssrcs, |
| 357 | uint32_t* bitrate_bps) const { |
| 358 | // Currently accessed from both the process thread (see |
| 359 | // ModuleRtpRtcpImpl::Process()) and the configuration thread (see |
| 360 | // Call::GetStats()). Should in the future only be accessed from a single |
| 361 | // thread. |
| 362 | RTC_DCHECK(ssrcs); |
| 363 | RTC_DCHECK(bitrate_bps); |
| 364 | rtc::CritScope lock(&crit_); |
| 365 | if (!remote_rate_.ValidEstimate()) { |
| 366 | return false; |
| 367 | } |
| 368 | *ssrcs = Keys(ssrcs_); |
| 369 | if (ssrcs_.empty()) { |
| 370 | *bitrate_bps = 0; |
| 371 | } else { |
| 372 | *bitrate_bps = remote_rate_.LatestEstimate(); |
| 373 | } |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | void DelayBasedBwe::SetMinBitrate(int min_bitrate_bps) { |
| 378 | // Called from both the configuration thread and the network thread. Shouldn't |
| 379 | // be called from the network thread in the future. |
| 380 | rtc::CritScope lock(&crit_); |
| 381 | remote_rate_.SetMinBitrate(min_bitrate_bps); |
| 382 | } |
| 383 | } // namespace webrtc |