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

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

Issue 2613543003: Fix BitrateProber to match the requested bitrate more precisely (Closed)
Patch Set: sync 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) {
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 }
41 } // namespace 32 } // namespace
42 33
43 BitrateProber::BitrateProber() 34 BitrateProber::BitrateProber()
44 : probing_state_(ProbingState::kDisabled), 35 : probing_state_(ProbingState::kDisabled),
45 probe_size_last_sent_(0), 36 next_probe_time_ms_(-1),
46 time_last_probe_sent_ms_(-1),
47 next_cluster_id_(0) { 37 next_cluster_id_(0) {
48 SetEnabled(true); 38 SetEnabled(true);
49 } 39 }
50 40
51 void BitrateProber::SetEnabled(bool enable) { 41 void BitrateProber::SetEnabled(bool enable) {
52 if (enable) { 42 if (enable) {
53 if (probing_state_ == ProbingState::kDisabled) { 43 if (probing_state_ == ProbingState::kDisabled) {
54 probing_state_ = ProbingState::kInactive; 44 probing_state_ = ProbingState::kInactive;
55 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; 45 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
56 } 46 }
57 } else { 47 } else {
58 probing_state_ = ProbingState::kDisabled; 48 probing_state_ = ProbingState::kDisabled;
59 LOG(LS_INFO) << "Bandwidth probing disabled"; 49 LOG(LS_INFO) << "Bandwidth probing disabled";
60 } 50 }
61 } 51 }
62 52
63 bool BitrateProber::IsProbing() const { 53 bool BitrateProber::IsProbing() const {
64 return probing_state_ == ProbingState::kActive; 54 return probing_state_ == ProbingState::kActive;
65 } 55 }
66 56
67 void BitrateProber::OnIncomingPacket(size_t packet_size) { 57 void BitrateProber::OnIncomingPacket(size_t packet_size) {
68 // Don't initialize probing unless we have something large enough to start 58 // Don't initialize probing unless we have something large enough to start
69 // probing. 59 // probing.
70 if (probing_state_ == ProbingState::kInactive && 60 if (probing_state_ == ProbingState::kInactive &&
71 !clusters_.empty() && 61 !clusters_.empty() &&
72 packet_size >= PacedSender::kMinProbePacketSize) { 62 packet_size >= PacedSender::kMinProbePacketSize) {
63 // Send next probe right away.
64 next_probe_time_ms_ = -1;
73 probing_state_ = ProbingState::kActive; 65 probing_state_ = ProbingState::kActive;
74 } 66 }
75 } 67 }
76 68
77 void BitrateProber::CreateProbeCluster(int bitrate_bps) { 69 void BitrateProber::CreateProbeCluster(int bitrate_bps) {
78 RTC_DCHECK(probing_state_ != ProbingState::kDisabled); 70 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
79 ProbeCluster cluster; 71 ProbeCluster cluster;
80 cluster.min_probes = kMinProbePacketsSent; 72 cluster.min_probes = kMinProbePacketsSent;
81 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000; 73 cluster.min_bytes = bitrate_bps * kMinProbeDurationMs / 8000;
82 cluster.bitrate_bps = bitrate_bps; 74 cluster.bitrate_bps = bitrate_bps;
83 cluster.id = next_cluster_id_++; 75 cluster.id = next_cluster_id_++;
84 clusters_.push(cluster); 76 clusters_.push(cluster);
85 77
86 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): (" 78 LOG(LS_INFO) << "Probe cluster (bitrate:min bytes:min packets): ("
87 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":" 79 << cluster.bitrate_bps << ":" << cluster.min_bytes << ":"
88 << cluster.min_probes << ")"; 80 << cluster.min_probes << ")";
89 // If we are already probing, continue to do so. Otherwise set it to 81 // If we are already probing, continue to do so. Otherwise set it to
90 // kInactive and wait for OnIncomingPacket to start the probing. 82 // kInactive and wait for OnIncomingPacket to start the probing.
91 if (probing_state_ != ProbingState::kActive) 83 if (probing_state_ != ProbingState::kActive)
92 probing_state_ = ProbingState::kInactive; 84 probing_state_ = ProbingState::kInactive;
93 } 85 }
94 86
95 void BitrateProber::ResetState() { 87 void BitrateProber::ResetState() {
96 time_last_probe_sent_ms_ = -1;
97 probe_size_last_sent_ = 0;
98
99 // Recreate all probing clusters. 88 // Recreate all probing clusters.
100 std::queue<ProbeCluster> clusters; 89 std::queue<ProbeCluster> clusters;
101 clusters.swap(clusters_); 90 clusters.swap(clusters_);
102 while (!clusters.empty()) { 91 while (!clusters.empty()) {
103 CreateProbeCluster(clusters.front().bitrate_bps); 92 CreateProbeCluster(clusters.front().bitrate_bps);
104 clusters.pop(); 93 clusters.pop();
105 } 94 }
106 // If its enabled, reset to inactive. 95 // If its enabled, reset to inactive.
107 if (probing_state_ != ProbingState::kDisabled) 96 if (probing_state_ != ProbingState::kDisabled)
108 probing_state_ = ProbingState::kInactive; 97 probing_state_ = ProbingState::kInactive;
109 } 98 }
110 99
111 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { 100 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
112 // Probing is not active or probing is already complete. 101 // Probing is not active or probing is already complete.
113 if (probing_state_ != ProbingState::kActive || clusters_.empty()) 102 if (probing_state_ != ProbingState::kActive || clusters_.empty())
114 return -1; 103 return -1;
115 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. 104
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; 105 int time_until_probe_ms = 0;
131 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { 106 if (next_probe_time_ms_ >= 0) {
132 int next_delta_ms = ComputeDeltaFromBitrate(probe_size_last_sent_, 107 time_until_probe_ms = next_probe_time_ms_ - now_ms;
133 clusters_.front().bitrate_bps); 108 // If we have waited more than 3 ms for a new packet we will restart probing
134 time_until_probe_ms = next_delta_ms - elapsed_time_ms; 109 // again later.
135 // If we have waited more than 3 ms for a new packet to probe with we will
136 // consider this probing session over.
137 const int kMaxProbeDelayMs = 3; 110 const int kMaxProbeDelayMs = 3;
138 if (next_delta_ms < kMinProbeDeltaMs || 111 if (time_until_probe_ms < -kMaxProbeDelayMs) {
139 time_until_probe_ms < -kMaxProbeDelayMs) { 112 ResetState();
140 probing_state_ = ProbingState::kSuspended; 113 return -1;
141 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
142 time_until_probe_ms = 0;
143 } 114 }
144 } 115 }
116
145 return std::max(time_until_probe_ms, 0); 117 return std::max(time_until_probe_ms, 0);
146 } 118 }
147 119
148 int BitrateProber::CurrentClusterId() const { 120 int BitrateProber::CurrentClusterId() const {
149 RTC_DCHECK(!clusters_.empty()); 121 RTC_DCHECK(!clusters_.empty());
150 RTC_DCHECK(ProbingState::kActive == probing_state_); 122 RTC_DCHECK(ProbingState::kActive == probing_state_);
151 return clusters_.front().id; 123 return clusters_.front().id;
152 } 124 }
153 125
154 // Probe size is recommended based on the probe bitrate required. We choose 126 // Probe size is recommended based on the probe bitrate required. We choose
155 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be 127 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
156 // feasible. 128 // feasible.
157 size_t BitrateProber::RecommendedMinProbeSize() const { 129 size_t BitrateProber::RecommendedMinProbeSize() const {
158 RTC_DCHECK(!clusters_.empty()); 130 RTC_DCHECK(!clusters_.empty());
159 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000); 131 return clusters_.front().bitrate_bps * 2 * kMinProbeDeltaMs / (8 * 1000);
160 } 132 }
161 133
162 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { 134 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
163 RTC_DCHECK(probing_state_ == ProbingState::kActive); 135 RTC_DCHECK(probing_state_ == ProbingState::kActive);
164 RTC_DCHECK_GT(bytes, 0); 136 RTC_DCHECK_GT(bytes, 0);
165 probe_size_last_sent_ = bytes; 137
166 time_last_probe_sent_ms_ = now_ms;
167 if (!clusters_.empty()) { 138 if (!clusters_.empty()) {
168 ProbeCluster* cluster = &clusters_.front(); 139 ProbeCluster* cluster = &clusters_.front();
169 cluster->sent_bytes += static_cast<int>(bytes); 140 if (cluster->sent_probes == 0) {
philipel 2017/01/09 09:34:34 This cast is needed for the win bots :). I realize
Sergey Ulanov 2017/01/09 18:37:16 Done.
141 RTC_DCHECK_EQ(cluster->time_started_ms, -1);
142 cluster->time_started_ms = now_ms;
143 }
144 cluster->sent_bytes += bytes;
170 cluster->sent_probes += 1; 145 cluster->sent_probes += 1;
146 next_probe_time_ms_ = GetNextProbeTime(clusters_.front());
171 if (cluster->sent_bytes >= cluster->min_bytes && 147 if (cluster->sent_bytes >= cluster->min_bytes &&
172 cluster->sent_probes >= cluster->min_probes) { 148 cluster->sent_probes >= cluster->min_probes) {
173 clusters_.pop(); 149 clusters_.pop();
174 } 150 }
175 if (clusters_.empty()) 151 if (clusters_.empty())
176 probing_state_ = ProbingState::kSuspended; 152 probing_state_ = ProbingState::kSuspended;
177 } 153 }
178 } 154 }
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
179 } // namespace webrtc 168 } // 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