Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1270)

Side by Side Diff: webrtc/modules/pacing/bitrate_prober.cc

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

Powered by Google App Engine
This is Rietveld 408576698