You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/pacing/bitrate_prober.h"
  11. #include <algorithm>
  12. #include "absl/memory/memory.h"
  13. #include "api/rtc_event_log/rtc_event.h"
  14. #include "api/rtc_event_log/rtc_event_log.h"
  15. #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/logging.h"
  18. #include "system_wrappers/include/metrics.h"
  19. namespace webrtc {
  20. namespace {
  21. // The min probe packet size is scaled with the bitrate we're probing at.
  22. // This defines the max min probe packet size, meaning that on high bitrates
  23. // we have a min probe packet size of 200 bytes.
  24. constexpr size_t kMinProbePacketSize = 200;
  25. constexpr int64_t kProbeClusterTimeoutMs = 5000;
  26. } // namespace
  27. BitrateProberConfig::BitrateProberConfig(
  28. const WebRtcKeyValueConfig* key_value_config)
  29. : min_probe_packets_sent("min_probe_packets_sent", 5),
  30. min_probe_delta("min_probe_delta", TimeDelta::ms(1)),
  31. min_probe_duration("min_probe_duration", TimeDelta::ms(15)),
  32. max_probe_delay("max_probe_delay", TimeDelta::ms(3)) {
  33. ParseFieldTrial({&min_probe_packets_sent, &min_probe_delta,
  34. &min_probe_duration, &max_probe_delay},
  35. key_value_config->Lookup("WebRTC-Bwe-ProbingConfiguration"));
  36. ParseFieldTrial({&min_probe_packets_sent, &min_probe_delta,
  37. &min_probe_duration, &max_probe_delay},
  38. key_value_config->Lookup("WebRTC-Bwe-ProbingBehavior"));
  39. }
  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::BitrateProber(const WebRtcKeyValueConfig& field_trials)
  47. : probing_state_(ProbingState::kDisabled),
  48. next_probe_time_ms_(-1),
  49. total_probe_count_(0),
  50. total_failed_probe_count_(0),
  51. config_(&field_trials) {
  52. SetEnabled(true);
  53. }
  54. void BitrateProber::SetEnabled(bool enable) {
  55. if (enable) {
  56. if (probing_state_ == ProbingState::kDisabled) {
  57. probing_state_ = ProbingState::kInactive;
  58. RTC_LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
  59. }
  60. } else {
  61. probing_state_ = ProbingState::kDisabled;
  62. RTC_LOG(LS_INFO) << "Bandwidth probing disabled";
  63. }
  64. }
  65. bool BitrateProber::IsProbing() const {
  66. return probing_state_ == ProbingState::kActive;
  67. }
  68. void BitrateProber::OnIncomingPacket(size_t packet_size) {
  69. // Don't initialize probing unless we have something large enough to start
  70. // probing.
  71. if (probing_state_ == ProbingState::kInactive && !clusters_.empty() &&
  72. packet_size >=
  73. std::min<size_t>(RecommendedMinProbeSize(), kMinProbePacketSize)) {
  74. // Send next probe right away.
  75. next_probe_time_ms_ = -1;
  76. probing_state_ = ProbingState::kActive;
  77. }
  78. }
  79. void BitrateProber::CreateProbeCluster(int bitrate_bps,
  80. int64_t now_ms,
  81. int cluster_id) {
  82. RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
  83. RTC_DCHECK_GT(bitrate_bps, 0);
  84. total_probe_count_++;
  85. while (!clusters_.empty() &&
  86. now_ms - clusters_.front().time_created_ms > kProbeClusterTimeoutMs) {
  87. clusters_.pop();
  88. total_failed_probe_count_++;
  89. }
  90. ProbeCluster cluster;
  91. cluster.time_created_ms = now_ms;
  92. cluster.pace_info.probe_cluster_min_probes = config_.min_probe_packets_sent;
  93. cluster.pace_info.probe_cluster_min_bytes =
  94. static_cast<int32_t>(static_cast<int64_t>(bitrate_bps) *
  95. config_.min_probe_duration->ms() / 8000);
  96. RTC_DCHECK_GE(cluster.pace_info.probe_cluster_min_bytes, 0);
  97. cluster.pace_info.send_bitrate_bps = bitrate_bps;
  98. cluster.pace_info.probe_cluster_id = cluster_id;
  99. clusters_.push(cluster);
  100. RTC_LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
  101. << cluster.pace_info.send_bitrate_bps << ":"
  102. << cluster.pace_info.probe_cluster_min_bytes << ":"
  103. << cluster.pace_info.probe_cluster_min_probes << ")";
  104. // If we are already probing, continue to do so. Otherwise set it to
  105. // kInactive and wait for OnIncomingPacket to start the probing.
  106. if (probing_state_ != ProbingState::kActive)
  107. probing_state_ = ProbingState::kInactive;
  108. }
  109. int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
  110. // Probing is not active or probing is already complete.
  111. if (probing_state_ != ProbingState::kActive || clusters_.empty())
  112. return -1;
  113. int time_until_probe_ms = 0;
  114. if (next_probe_time_ms_ >= 0) {
  115. time_until_probe_ms = next_probe_time_ms_ - now_ms;
  116. if (time_until_probe_ms < -config_.max_probe_delay->ms()) {
  117. RTC_DLOG(LS_WARNING) << "Probe delay too high"
  118. << " (next_ms:" << next_probe_time_ms_
  119. << ", now_ms: " << now_ms << ")";
  120. return -1;
  121. }
  122. }
  123. return std::max(time_until_probe_ms, 0);
  124. }
  125. PacedPacketInfo BitrateProber::CurrentCluster() const {
  126. RTC_DCHECK(!clusters_.empty());
  127. RTC_DCHECK(probing_state_ == ProbingState::kActive);
  128. PacedPacketInfo info = clusters_.front().pace_info;
  129. info.probe_cluster_bytes_sent = clusters_.front().sent_bytes;
  130. return info;
  131. }
  132. // Probe size is recommended based on the probe bitrate required. We choose
  133. // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
  134. // feasible.
  135. size_t BitrateProber::RecommendedMinProbeSize() const {
  136. RTC_DCHECK(!clusters_.empty());
  137. return clusters_.front().pace_info.send_bitrate_bps * 2 *
  138. config_.min_probe_delta->ms() / (8 * 1000);
  139. }
  140. void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
  141. RTC_DCHECK(probing_state_ == ProbingState::kActive);
  142. RTC_DCHECK_GT(bytes, 0);
  143. if (!clusters_.empty()) {
  144. ProbeCluster* cluster = &clusters_.front();
  145. if (cluster->sent_probes == 0) {
  146. RTC_DCHECK_EQ(cluster->time_started_ms, -1);
  147. cluster->time_started_ms = now_ms;
  148. }
  149. cluster->sent_bytes += static_cast<int>(bytes);
  150. cluster->sent_probes += 1;
  151. next_probe_time_ms_ = GetNextProbeTime(*cluster);
  152. if (cluster->sent_bytes >= cluster->pace_info.probe_cluster_min_bytes &&
  153. cluster->sent_probes >= cluster->pace_info.probe_cluster_min_probes) {
  154. RTC_HISTOGRAM_COUNTS_100000("WebRTC.BWE.Probing.ProbeClusterSizeInBytes",
  155. cluster->sent_bytes);
  156. RTC_HISTOGRAM_COUNTS_100("WebRTC.BWE.Probing.ProbesPerCluster",
  157. cluster->sent_probes);
  158. RTC_HISTOGRAM_COUNTS_10000("WebRTC.BWE.Probing.TimePerProbeCluster",
  159. now_ms - cluster->time_started_ms);
  160. clusters_.pop();
  161. }
  162. if (clusters_.empty())
  163. probing_state_ = ProbingState::kSuspended;
  164. }
  165. }
  166. int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
  167. RTC_CHECK_GT(cluster.pace_info.send_bitrate_bps, 0);
  168. RTC_CHECK_GE(cluster.time_started_ms, 0);
  169. // Compute the time delta from the cluster start to ensure probe bitrate stays
  170. // close to the target bitrate. Result is in milliseconds.
  171. int64_t delta_ms =
  172. (8000ll * cluster.sent_bytes + cluster.pace_info.send_bitrate_bps / 2) /
  173. cluster.pace_info.send_bitrate_bps;
  174. return cluster.time_started_ms + delta_ms;
  175. }
  176. } // namespace webrtc