| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/pacing/bitrate_prober.h" | 11 #include "webrtc/modules/pacing/bitrate_prober.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/logging.h" | 16 #include "webrtc/base/logging.h" |
| 17 #include "webrtc/modules/pacing/paced_sender.h" | 17 #include "webrtc/modules/pacing/paced_sender.h" |
| 18 | 18 |
| 19 namespace webrtc { | 19 namespace webrtc { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Inactivity threshold above which probing is restarted. | 23 // Inactivity threshold above which probing is restarted. |
| 24 constexpr int kInactivityThresholdMs = 5000; | 24 constexpr int kInactivityThresholdMs = 5000; |
| 25 | 25 |
| 26 // A minimum interval between probes to allow scheduling to be feasible. | 26 // A minimum interval between probes to allow scheduling to be feasible. |
| 27 constexpr int kMinProbeDeltaMs = 1; | 27 constexpr int kMinProbeDeltaMs = 1; |
| 28 | 28 |
| 29 // The minimum number probing packets used. |
| 30 constexpr int kMinProbePacketsSent = 5; |
| 31 |
| 32 // The minimum probing duration in ms. |
| 33 constexpr int kMinProbeDurationMs = 15; |
| 34 |
| 29 int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) { | 35 int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) { |
| 30 RTC_CHECK_GT(bitrate_bps, 0); | 36 RTC_CHECK_GT(bitrate_bps, 0); |
| 31 // Compute the time delta needed to send probe_size bytes at bitrate_bps | 37 // Compute the time delta needed to send probe_size bytes at bitrate_bps |
| 32 // bps. Result is in milliseconds. | 38 // bps. Result is in milliseconds. |
| 33 return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps); | 39 return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps); |
| 34 } | 40 } |
| 35 } // namespace | 41 } // namespace |
| 36 | 42 |
| 37 BitrateProber::BitrateProber() | 43 BitrateProber::BitrateProber() |
| 38 : probing_state_(ProbingState::kDisabled), | 44 : probing_state_(ProbingState::kDisabled), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 61 void BitrateProber::OnIncomingPacket(size_t packet_size) { | 67 void BitrateProber::OnIncomingPacket(size_t packet_size) { |
| 62 // Don't initialize probing unless we have something large enough to start | 68 // Don't initialize probing unless we have something large enough to start |
| 63 // probing. | 69 // probing. |
| 64 if (probing_state_ == ProbingState::kInactive && | 70 if (probing_state_ == ProbingState::kInactive && |
| 65 !clusters_.empty() && | 71 !clusters_.empty() && |
| 66 packet_size >= PacedSender::kMinProbePacketSize) { | 72 packet_size >= PacedSender::kMinProbePacketSize) { |
| 67 probing_state_ = ProbingState::kActive; | 73 probing_state_ = ProbingState::kActive; |
| 68 } | 74 } |
| 69 } | 75 } |
| 70 | 76 |
| 71 void BitrateProber::CreateProbeCluster(int bitrate_bps, int num_probes) { | 77 void BitrateProber::CreateProbeCluster(int bitrate_bps) { |
| 72 RTC_DCHECK(probing_state_ != ProbingState::kDisabled); | 78 RTC_DCHECK(probing_state_ != ProbingState::kDisabled); |
| 73 ProbeCluster cluster; | 79 ProbeCluster cluster; |
| 74 cluster.max_probes = num_probes; | 80 cluster.min_probes = kMinProbePacketsSent; |
| 75 cluster.probe_bitrate_bps = bitrate_bps; | 81 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000; |
| 82 cluster.bitrate_bps = bitrate_bps; |
| 76 cluster.id = next_cluster_id_++; | 83 cluster.id = next_cluster_id_++; |
| 77 clusters_.push(cluster); | 84 clusters_.push(cluster); |
| 78 LOG(LS_INFO) << "Probe cluster (bitrate:probes): (" | 85 |
| 79 << cluster.probe_bitrate_bps << ":" << cluster.max_probes | 86 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" |
| 80 << ") "; | 87 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":" |
| 88 << cluster.min_probes << ")"; |
| 89 // If we are already probing, continue to do so. Otherwise set it to |
| 90 // kInactive and wait for OnIncomingPacket to start the probing. |
| 81 if (probing_state_ != ProbingState::kActive) | 91 if (probing_state_ != ProbingState::kActive) |
| 82 probing_state_ = ProbingState::kInactive; | 92 probing_state_ = ProbingState::kInactive; |
| 83 } | 93 } |
| 84 | 94 |
| 85 void BitrateProber::ResetState() { | 95 void BitrateProber::ResetState() { |
| 86 time_last_probe_sent_ms_ = -1; | 96 time_last_probe_sent_ms_ = -1; |
| 87 probe_size_last_sent_ = 0; | 97 probe_size_last_sent_ = 0; |
| 88 | 98 |
| 89 // Recreate all probing clusters. | 99 // Recreate all probing clusters. |
| 90 std::queue<ProbeCluster> clusters; | 100 std::queue<ProbeCluster> clusters; |
| 91 clusters.swap(clusters_); | 101 clusters.swap(clusters_); |
| 92 while (!clusters.empty()) { | 102 while (!clusters.empty()) { |
| 93 CreateProbeCluster(clusters.front().probe_bitrate_bps, | 103 CreateProbeCluster(clusters.front().bitrate_bps); |
| 94 clusters.front().max_probes); | |
| 95 clusters.pop(); | 104 clusters.pop(); |
| 96 } | 105 } |
| 97 // If its enabled, reset to inactive. | 106 // If its enabled, reset to inactive. |
| 98 if (probing_state_ != ProbingState::kDisabled) | 107 if (probing_state_ != ProbingState::kDisabled) |
| 99 probing_state_ = ProbingState::kInactive; | 108 probing_state_ = ProbingState::kInactive; |
| 100 } | 109 } |
| 101 | 110 |
| 102 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { | 111 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { |
| 103 // Probing is not active or probing is already complete. | 112 // Probing is not active or probing is already complete. |
| 104 if (probing_state_ != ProbingState::kActive || clusters_.empty()) | 113 if (probing_state_ != ProbingState::kActive || clusters_.empty()) |
| 105 return -1; | 114 return -1; |
| 106 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. | 115 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. |
| 107 int64_t elapsed_time_ms; | 116 int64_t elapsed_time_ms; |
| 108 if (time_last_probe_sent_ms_ == -1) { | 117 if (time_last_probe_sent_ms_ == -1) { |
| 109 elapsed_time_ms = 0; | 118 elapsed_time_ms = 0; |
| 110 } else { | 119 } else { |
| 111 elapsed_time_ms = now_ms - time_last_probe_sent_ms_; | 120 elapsed_time_ms = now_ms - time_last_probe_sent_ms_; |
| 112 } | 121 } |
| 113 // If no probes have been sent for a while, abort current probing and | 122 // If no probes have been sent for a while, abort current probing and |
| 114 // reset. | 123 // reset. |
| 115 if (elapsed_time_ms > kInactivityThresholdMs) { | 124 if (elapsed_time_ms > kInactivityThresholdMs) { |
| 116 ResetState(); | 125 ResetState(); |
| 117 return -1; | 126 return -1; |
| 118 } | 127 } |
| 119 // We will send the first probe packet immediately if no packet has been | 128 // We will send the first probe packet immediately if no packet has been |
| 120 // sent before. | 129 // sent before. |
| 121 int time_until_probe_ms = 0; | 130 int time_until_probe_ms = 0; |
| 122 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { | 131 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { |
| 123 int next_delta_ms = ComputeDeltaFromBitrate( | 132 int next_delta_ms = ComputeDeltaFromBitrate(probe_size_last_sent_, |
| 124 probe_size_last_sent_, clusters_.front().probe_bitrate_bps); | 133 clusters_.front().bitrate_bps); |
| 125 time_until_probe_ms = next_delta_ms - elapsed_time_ms; | 134 time_until_probe_ms = next_delta_ms - elapsed_time_ms; |
| 126 // If we have waited more than 3 ms for a new packet to probe with we will | 135 // If we have waited more than 3 ms for a new packet to probe with we will |
| 127 // consider this probing session over. | 136 // consider this probing session over. |
| 128 const int kMaxProbeDelayMs = 3; | 137 const int kMaxProbeDelayMs = 3; |
| 129 if (next_delta_ms < kMinProbeDeltaMs || | 138 if (next_delta_ms < kMinProbeDeltaMs || |
| 130 time_until_probe_ms < -kMaxProbeDelayMs) { | 139 time_until_probe_ms < -kMaxProbeDelayMs) { |
| 131 probing_state_ = ProbingState::kSuspended; | 140 probing_state_ = ProbingState::kSuspended; |
| 132 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend"; | 141 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend"; |
| 133 time_until_probe_ms = 0; | 142 time_until_probe_ms = 0; |
| 134 } | 143 } |
| 135 } | 144 } |
| 136 return std::max(time_until_probe_ms, 0); | 145 return std::max(time_until_probe_ms, 0); |
| 137 } | 146 } |
| 138 | 147 |
| 139 int BitrateProber::CurrentClusterId() const { | 148 int BitrateProber::CurrentClusterId() const { |
| 140 RTC_DCHECK(!clusters_.empty()); | 149 RTC_DCHECK(!clusters_.empty()); |
| 141 RTC_DCHECK(ProbingState::kActive == probing_state_); | 150 RTC_DCHECK(ProbingState::kActive == probing_state_); |
| 142 return clusters_.front().id; | 151 return clusters_.front().id; |
| 143 } | 152 } |
| 144 | 153 |
| 145 // Probe size is recommended based on the probe bitrate required. We choose | 154 // Probe size is recommended based on the probe bitrate required. We choose |
| 146 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be | 155 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be |
| 147 // feasible. | 156 // feasible. |
| 148 size_t BitrateProber::RecommendedMinProbeSize() const { | 157 size_t BitrateProber::RecommendedMinProbeSize() const { |
| 149 RTC_DCHECK(!clusters_.empty()); | 158 RTC_DCHECK(!clusters_.empty()); |
| 150 return clusters_.front().probe_bitrate_bps * 2 * kMinProbeDeltaMs / | 159 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000); |
| 151 (8 * 1000); | |
| 152 } | 160 } |
| 153 | 161 |
| 154 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { | 162 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { |
| 155 RTC_DCHECK(probing_state_ == ProbingState::kActive); | 163 RTC_DCHECK(probing_state_ == ProbingState::kActive); |
| 156 RTC_DCHECK_GT(bytes, 0); | 164 RTC_DCHECK_GT(bytes, 0); |
| 157 probe_size_last_sent_ = bytes; | 165 probe_size_last_sent_ = bytes; |
| 158 time_last_probe_sent_ms_ = now_ms; | 166 time_last_probe_sent_ms_ = now_ms; |
| 159 if (!clusters_.empty()) { | 167 if (!clusters_.empty()) { |
| 160 ProbeCluster* cluster = &clusters_.front(); | 168 ProbeCluster* cluster = &clusters_.front(); |
| 161 ++cluster->sent_probes; | 169 cluster->sent_bytes += bytes; |
| 162 if (cluster->sent_probes == cluster->max_probes) | 170 cluster->sent_probes += 1; |
| 171 if (cluster->sent_bytes >= cluster->min_bytes && |
| 172 cluster->sent_probes >= cluster->min_probes) { |
| 163 clusters_.pop(); | 173 clusters_.pop(); |
| 174 } |
| 164 if (clusters_.empty()) | 175 if (clusters_.empty()) |
| 165 probing_state_ = ProbingState::kSuspended; | 176 probing_state_ = ProbingState::kSuspended; |
| 166 } | 177 } |
| 167 } | 178 } |
| 168 } // namespace webrtc | 179 } // namespace webrtc |
| OLD | NEW |