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. |
| 24 constexpr int kInactivityThresholdMs = 5000; |
| 25 |
23 // A minimum interval between probes to allow scheduling to be feasible. | 26 // A minimum interval between probes to allow scheduling to be feasible. |
24 constexpr int kMinProbeDeltaMs = 1; | 27 constexpr int kMinProbeDeltaMs = 1; |
25 | 28 |
26 // The minimum number probing packets used. | 29 // The minimum number probing packets used. |
27 constexpr int kMinProbePacketsSent = 5; | 30 constexpr int kMinProbePacketsSent = 5; |
28 | 31 |
29 // The minimum probing duration in ms. | 32 // The minimum probing duration in ms. |
30 constexpr int kMinProbeDurationMs = 15; | 33 constexpr int kMinProbeDurationMs = 15; |
31 | 34 |
| 35 int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) { |
| 36 RTC_CHECK_GT(bitrate_bps, 0); |
| 37 // Compute the time delta needed to send probe_size bytes at bitrate_bps |
| 38 // bps. Result is in milliseconds. |
| 39 return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps); |
| 40 } |
32 } // namespace | 41 } // namespace |
33 | 42 |
34 BitrateProber::BitrateProber() | 43 BitrateProber::BitrateProber() |
35 : probing_state_(ProbingState::kDisabled), | 44 : probing_state_(ProbingState::kDisabled), |
36 next_probe_time_ms_(-1), | 45 probe_size_last_sent_(0), |
| 46 time_last_probe_sent_ms_(-1), |
37 next_cluster_id_(0) { | 47 next_cluster_id_(0) { |
38 SetEnabled(true); | 48 SetEnabled(true); |
39 } | 49 } |
40 | 50 |
41 void BitrateProber::SetEnabled(bool enable) { | 51 void BitrateProber::SetEnabled(bool enable) { |
42 if (enable) { | 52 if (enable) { |
43 if (probing_state_ == ProbingState::kDisabled) { | 53 if (probing_state_ == ProbingState::kDisabled) { |
44 probing_state_ = ProbingState::kInactive; | 54 probing_state_ = ProbingState::kInactive; |
45 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; | 55 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; |
46 } | 56 } |
47 } else { | 57 } else { |
48 probing_state_ = ProbingState::kDisabled; | 58 probing_state_ = ProbingState::kDisabled; |
49 LOG(LS_INFO) << "Bandwidth probing disabled"; | 59 LOG(LS_INFO) << "Bandwidth probing disabled"; |
50 } | 60 } |
51 } | 61 } |
52 | 62 |
53 bool BitrateProber::IsProbing() const { | 63 bool BitrateProber::IsProbing() const { |
54 return probing_state_ == ProbingState::kActive; | 64 return probing_state_ == ProbingState::kActive; |
55 } | 65 } |
56 | 66 |
57 void BitrateProber::OnIncomingPacket(size_t packet_size) { | 67 void BitrateProber::OnIncomingPacket(size_t packet_size) { |
58 // 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 |
59 // probing. | 69 // probing. |
60 if (probing_state_ == ProbingState::kInactive && | 70 if (probing_state_ == ProbingState::kInactive && |
61 !clusters_.empty() && | 71 !clusters_.empty() && |
62 packet_size >= PacedSender::kMinProbePacketSize) { | 72 packet_size >= PacedSender::kMinProbePacketSize) { |
63 // Send next probe right away. | |
64 next_probe_time_ms_ = -1; | |
65 probing_state_ = ProbingState::kActive; | 73 probing_state_ = ProbingState::kActive; |
66 } | 74 } |
67 } | 75 } |
68 | 76 |
69 void BitrateProber::CreateProbeCluster(int bitrate_bps) { | 77 void BitrateProber::CreateProbeCluster(int bitrate_bps) { |
70 RTC_DCHECK(probing_state_ != ProbingState::kDisabled); | 78 RTC_DCHECK(probing_state_ != ProbingState::kDisabled); |
71 ProbeCluster cluster; | 79 ProbeCluster cluster; |
72 cluster.min_probes = kMinProbePacketsSent; | 80 cluster.min_probes = kMinProbePacketsSent; |
73 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000; | 81 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000; |
74 cluster.bitrate_bps = bitrate_bps; | 82 cluster.bitrate_bps = bitrate_bps; |
75 cluster.id = next_cluster_id_++; | 83 cluster.id = next_cluster_id_++; |
76 clusters_.push(cluster); | 84 clusters_.push(cluster); |
77 | 85 |
78 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" | 86 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" |
79 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":" | 87 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":" |
80 << cluster.min_probes << ")"; | 88 << cluster.min_probes << ")"; |
81 // If we are already probing, continue to do so. Otherwise set it to | 89 // If we are already probing, continue to do so. Otherwise set it to |
82 // kInactive and wait for OnIncomingPacket to start the probing. | 90 // kInactive and wait for OnIncomingPacket to start the probing. |
83 if (probing_state_ != ProbingState::kActive) | 91 if (probing_state_ != ProbingState::kActive) |
84 probing_state_ = ProbingState::kInactive; | 92 probing_state_ = ProbingState::kInactive; |
85 } | 93 } |
86 | 94 |
87 void BitrateProber::ResetState() { | 95 void BitrateProber::ResetState() { |
| 96 time_last_probe_sent_ms_ = -1; |
| 97 probe_size_last_sent_ = 0; |
| 98 |
88 // Recreate all probing clusters. | 99 // Recreate all probing clusters. |
89 std::queue<ProbeCluster> clusters; | 100 std::queue<ProbeCluster> clusters; |
90 clusters.swap(clusters_); | 101 clusters.swap(clusters_); |
91 while (!clusters.empty()) { | 102 while (!clusters.empty()) { |
92 CreateProbeCluster(clusters.front().bitrate_bps); | 103 CreateProbeCluster(clusters.front().bitrate_bps); |
93 clusters.pop(); | 104 clusters.pop(); |
94 } | 105 } |
95 // If its enabled, reset to inactive. | 106 // If its enabled, reset to inactive. |
96 if (probing_state_ != ProbingState::kDisabled) | 107 if (probing_state_ != ProbingState::kDisabled) |
97 probing_state_ = ProbingState::kInactive; | 108 probing_state_ = ProbingState::kInactive; |
98 } | 109 } |
99 | 110 |
100 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { | 111 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { |
101 // Probing is not active or probing is already complete. | 112 // Probing is not active or probing is already complete. |
102 if (probing_state_ != ProbingState::kActive || clusters_.empty()) | 113 if (probing_state_ != ProbingState::kActive || clusters_.empty()) |
103 return -1; | 114 return -1; |
104 | 115 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. |
| 116 int64_t elapsed_time_ms; |
| 117 if (time_last_probe_sent_ms_ == -1) { |
| 118 elapsed_time_ms = 0; |
| 119 } else { |
| 120 elapsed_time_ms = now_ms - time_last_probe_sent_ms_; |
| 121 } |
| 122 // If no probes have been sent for a while, abort current probing and |
| 123 // reset. |
| 124 if (elapsed_time_ms > kInactivityThresholdMs) { |
| 125 ResetState(); |
| 126 return -1; |
| 127 } |
| 128 // We will send the first probe packet immediately if no packet has been |
| 129 // sent before. |
105 int time_until_probe_ms = 0; | 130 int time_until_probe_ms = 0; |
106 if (next_probe_time_ms_ >= 0) { | 131 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { |
107 time_until_probe_ms = next_probe_time_ms_ - now_ms; | 132 int next_delta_ms = ComputeDeltaFromBitrate(probe_size_last_sent_, |
108 // If we have waited more than 3 ms for a new packet we will restart probing | 133 clusters_.front().bitrate_bps); |
109 // again later. | 134 time_until_probe_ms = next_delta_ms - elapsed_time_ms; |
| 135 // If we have waited more than 3 ms for a new packet to probe with we will |
| 136 // consider this probing session over. |
110 const int kMaxProbeDelayMs = 3; | 137 const int kMaxProbeDelayMs = 3; |
111 if (time_until_probe_ms < -kMaxProbeDelayMs) { | 138 if (next_delta_ms < kMinProbeDeltaMs || |
112 ResetState(); | 139 time_until_probe_ms < -kMaxProbeDelayMs) { |
113 return -1; | 140 probing_state_ = ProbingState::kSuspended; |
| 141 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend"; |
| 142 time_until_probe_ms = 0; |
114 } | 143 } |
115 } | 144 } |
116 | |
117 return std::max(time_until_probe_ms, 0); | 145 return std::max(time_until_probe_ms, 0); |
118 } | 146 } |
119 | 147 |
120 int BitrateProber::CurrentClusterId() const { | 148 int BitrateProber::CurrentClusterId() const { |
121 RTC_DCHECK(!clusters_.empty()); | 149 RTC_DCHECK(!clusters_.empty()); |
122 RTC_DCHECK(ProbingState::kActive == probing_state_); | 150 RTC_DCHECK(ProbingState::kActive == probing_state_); |
123 return clusters_.front().id; | 151 return clusters_.front().id; |
124 } | 152 } |
125 | 153 |
126 // 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 |
127 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be | 155 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be |
128 // feasible. | 156 // feasible. |
129 size_t BitrateProber::RecommendedMinProbeSize() const { | 157 size_t BitrateProber::RecommendedMinProbeSize() const { |
130 RTC_DCHECK(!clusters_.empty()); | 158 RTC_DCHECK(!clusters_.empty()); |
131 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000); | 159 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000); |
132 } | 160 } |
133 | 161 |
134 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { | 162 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { |
135 RTC_DCHECK(probing_state_ == ProbingState::kActive); | 163 RTC_DCHECK(probing_state_ == ProbingState::kActive); |
136 RTC_DCHECK_GT(bytes, 0); | 164 RTC_DCHECK_GT(bytes, 0); |
137 | 165 probe_size_last_sent_ = bytes; |
| 166 time_last_probe_sent_ms_ = now_ms; |
138 if (!clusters_.empty()) { | 167 if (!clusters_.empty()) { |
139 ProbeCluster* cluster = &clusters_.front(); | 168 ProbeCluster* cluster = &clusters_.front(); |
140 if (cluster->sent_probes == 0) { | |
141 RTC_DCHECK_EQ(cluster->time_started_ms, -1); | |
142 cluster->time_started_ms = now_ms; | |
143 } | |
144 cluster->sent_bytes += static_cast<int>(bytes); | 169 cluster->sent_bytes += static_cast<int>(bytes); |
145 cluster->sent_probes += 1; | 170 cluster->sent_probes += 1; |
146 next_probe_time_ms_ = GetNextProbeTime(clusters_.front()); | |
147 if (cluster->sent_bytes >= cluster->min_bytes && | 171 if (cluster->sent_bytes >= cluster->min_bytes && |
148 cluster->sent_probes >= cluster->min_probes) { | 172 cluster->sent_probes >= cluster->min_probes) { |
149 clusters_.pop(); | 173 clusters_.pop(); |
150 } | 174 } |
151 if (clusters_.empty()) | 175 if (clusters_.empty()) |
152 probing_state_ = ProbingState::kSuspended; | 176 probing_state_ = ProbingState::kSuspended; |
153 } | 177 } |
154 } | 178 } |
155 | |
156 int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) { | |
157 RTC_CHECK_GT(cluster.bitrate_bps, 0); | |
158 RTC_CHECK_GE(cluster.time_started_ms, 0); | |
159 | |
160 // Compute the time delta from the cluster start to ensure probe bitrate stays | |
161 // close to the target bitrate. Result is in milliseconds. | |
162 int64_t delta_ms = (8000ll * cluster.sent_bytes + cluster.bitrate_bps / 2) / | |
163 cluster.bitrate_bps; | |
164 return cluster.time_started_ms + delta_ms; | |
165 } | |
166 | |
167 | |
168 } // namespace webrtc | 179 } // namespace webrtc |
OLD | NEW |