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

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

Issue 2613543003: Fix BitrateProber to match the requested bitrate more precisely (Closed)
Patch Set: 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 int ComputeDeltaFromBitrate(size_t probe_size, uint32_t bitrate_bps) {
30 RTC_CHECK_GT(bitrate_bps, 0);
31 // Compute the time delta needed to send probe_size bytes at bitrate_bps
32 // bps. Result is in milliseconds.
33 return static_cast<int>((1000ll * probe_size * 8) / bitrate_bps);
34 }
35 } // namespace 26 } // namespace
36 27
37 BitrateProber::BitrateProber() 28 BitrateProber::BitrateProber()
38 : probing_state_(ProbingState::kDisabled), 29 : probing_state_(ProbingState::kDisabled),
39 probe_size_last_sent_(0), 30 next_probe_time_ms_(-1),
40 time_last_probe_sent_ms_(-1),
41 next_cluster_id_(0) { 31 next_cluster_id_(0) {
42 SetEnabled(true); 32 SetEnabled(true);
43 } 33 }
44 34
45 void BitrateProber::SetEnabled(bool enable) { 35 void BitrateProber::SetEnabled(bool enable) {
46 if (enable) { 36 if (enable) {
47 if (probing_state_ == ProbingState::kDisabled) { 37 if (probing_state_ == ProbingState::kDisabled) {
48 probing_state_ = ProbingState::kInactive; 38 probing_state_ = ProbingState::kInactive;
49 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive"; 39 LOG(LS_INFO) << "Bandwidth probing enabled, set to inactive";
50 } 40 }
51 } else { 41 } else {
52 probing_state_ = ProbingState::kDisabled; 42 probing_state_ = ProbingState::kDisabled;
53 LOG(LS_INFO) << "Bandwidth probing disabled"; 43 LOG(LS_INFO) << "Bandwidth probing disabled";
54 } 44 }
55 } 45 }
56 46
57 bool BitrateProber::IsProbing() const { 47 bool BitrateProber::IsProbing() const {
58 return probing_state_ == ProbingState::kActive; 48 return probing_state_ == ProbingState::kActive;
59 } 49 }
60 50
61 void BitrateProber::OnIncomingPacket(size_t packet_size) { 51 void BitrateProber::OnIncomingPacket(size_t packet_size) {
62 // Don't initialize probing unless we have something large enough to start 52 // Don't initialize probing unless we have something large enough to start
63 // probing. 53 // probing.
64 if (probing_state_ == ProbingState::kInactive && 54 if (probing_state_ == ProbingState::kInactive &&
65 !clusters_.empty() && 55 !clusters_.empty() &&
66 packet_size >= PacedSender::kMinProbePacketSize) { 56 packet_size >= PacedSender::kMinProbePacketSize) {
57 // Send next probe right away.
58 next_probe_time_ms_ = -1;
67 probing_state_ = ProbingState::kActive; 59 probing_state_ = ProbingState::kActive;
68 } 60 }
69 } 61 }
70 62
71 void BitrateProber::CreateProbeCluster(int bitrate_bps, int num_probes) { 63 void BitrateProber::CreateProbeCluster(int bitrate_bps, int num_probes) {
72 RTC_DCHECK(probing_state_ != ProbingState::kDisabled); 64 RTC_DCHECK(probing_state_ != ProbingState::kDisabled);
73 ProbeCluster cluster; 65 ProbeCluster cluster;
74 cluster.max_probes = num_probes; 66 cluster.max_probes = num_probes;
75 cluster.probe_bitrate_bps = bitrate_bps; 67 cluster.probe_bitrate_bps = bitrate_bps;
76 cluster.id = next_cluster_id_++; 68 cluster.id = next_cluster_id_++;
77 clusters_.push(cluster); 69 clusters_.push(cluster);
78 LOG(LS_INFO) << "Probe cluster (bitrate:probes): (" 70 LOG(LS_INFO) << "Probe cluster (bitrate:probes): ("
79 << cluster.probe_bitrate_bps << ":" << cluster.max_probes 71 << cluster.probe_bitrate_bps << ":" << cluster.max_probes
80 << ") "; 72 << ") ";
81 if (probing_state_ != ProbingState::kActive) 73 if (probing_state_ != ProbingState::kActive)
82 probing_state_ = ProbingState::kInactive; 74 probing_state_ = ProbingState::kInactive;
83 } 75 }
84 76
85 void BitrateProber::ResetState() { 77 void BitrateProber::ResetState() {
86 time_last_probe_sent_ms_ = -1;
87 probe_size_last_sent_ = 0;
88
89 // Recreate all probing clusters. 78 // Recreate all probing clusters.
90 std::queue<ProbeCluster> clusters; 79 std::queue<ProbeCluster> clusters;
91 clusters.swap(clusters_); 80 clusters.swap(clusters_);
92 while (!clusters.empty()) { 81 while (!clusters.empty()) {
93 CreateProbeCluster(clusters.front().probe_bitrate_bps, 82 CreateProbeCluster(clusters.front().probe_bitrate_bps,
94 clusters.front().max_probes); 83 clusters.front().max_probes);
95 clusters.pop(); 84 clusters.pop();
96 } 85 }
97 // If its enabled, reset to inactive. 86 // If its enabled, reset to inactive.
98 if (probing_state_ != ProbingState::kDisabled) 87 if (probing_state_ != ProbingState::kDisabled)
99 probing_state_ = ProbingState::kInactive; 88 probing_state_ = ProbingState::kInactive;
100 } 89 }
101 90
102 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { 91 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) {
103 // Probing is not active or probing is already complete. 92 // Probing is not active or probing is already complete.
104 if (probing_state_ != ProbingState::kActive || clusters_.empty()) 93 if (probing_state_ != ProbingState::kActive || clusters_.empty())
105 return -1; 94 return -1;
106 // time_last_probe_sent_ms_ of -1 indicates no probes have yet been sent. 95
107 int64_t elapsed_time_ms;
108 if (time_last_probe_sent_ms_ == -1) {
109 elapsed_time_ms = 0;
110 } else {
111 elapsed_time_ms = now_ms - time_last_probe_sent_ms_;
112 }
113 // If no probes have been sent for a while, abort current probing and
114 // reset.
115 if (elapsed_time_ms > kInactivityThresholdMs) {
Sergey Ulanov 2017/01/03 23:28:59 I couldn't see any case when this condition would
116 ResetState();
117 return -1;
118 }
119 // We will send the first probe packet immediately if no packet has been
120 // sent before.
121 int time_until_probe_ms = 0; 96 int time_until_probe_ms = 0;
122 if (probe_size_last_sent_ != 0 && probing_state_ == ProbingState::kActive) { 97 if (next_probe_time_ms_ >= 0) {
123 int next_delta_ms = ComputeDeltaFromBitrate( 98 time_until_probe_ms = next_probe_time_ms_ - now_ms;
124 probe_size_last_sent_, clusters_.front().probe_bitrate_bps); 99 // If we have waited more than 3 ms for a new packet we will restart probing
125 time_until_probe_ms = next_delta_ms - elapsed_time_ms; 100 // again later.
126 // If we have waited more than 3 ms for a new packet to probe with we will
127 // consider this probing session over.
128 const int kMaxProbeDelayMs = 3; 101 const int kMaxProbeDelayMs = 3;
129 if (next_delta_ms < kMinProbeDeltaMs || 102 if (time_until_probe_ms < -kMaxProbeDelayMs) {
130 time_until_probe_ms < -kMaxProbeDelayMs) { 103 ResetState();
131 probing_state_ = ProbingState::kSuspended; 104 return -1;
Sergey Ulanov 2017/01/03 23:28:59 This logic seems to be incorrect. It could suspend
132 LOG(LS_INFO) << "Delta too small or missed probing accurately, suspend";
133 time_until_probe_ms = 0;
134 } 105 }
135 } 106 }
107
136 return std::max(time_until_probe_ms, 0); 108 return std::max(time_until_probe_ms, 0);
137 } 109 }
138 110
139 int BitrateProber::CurrentClusterId() const { 111 int BitrateProber::CurrentClusterId() const {
140 RTC_DCHECK(!clusters_.empty()); 112 RTC_DCHECK(!clusters_.empty());
141 RTC_DCHECK(ProbingState::kActive == probing_state_); 113 RTC_DCHECK(ProbingState::kActive == probing_state_);
142 return clusters_.front().id; 114 return clusters_.front().id;
143 } 115 }
144 116
145 // Probe size is recommended based on the probe bitrate required. We choose 117 // Probe size is recommended based on the probe bitrate required. We choose
146 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be 118 // a minimum of twice |kMinProbeDeltaMs| interval to allow scheduling to be
147 // feasible. 119 // feasible.
148 size_t BitrateProber::RecommendedMinProbeSize() const { 120 size_t BitrateProber::RecommendedMinProbeSize() const {
149 RTC_DCHECK(!clusters_.empty()); 121 RTC_DCHECK(!clusters_.empty());
150 return clusters_.front().probe_bitrate_bps * 2 * kMinProbeDeltaMs / 122 return (clusters_.front().probe_bitrate_bps * 2 * kMinProbeDeltaMs) /
151 (8 * 1000); 123 (8 * 1000);
152 } 124 }
153 125
154 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) { 126 void BitrateProber::ProbeSent(int64_t now_ms, size_t bytes) {
155 RTC_DCHECK(probing_state_ == ProbingState::kActive); 127 RTC_DCHECK(probing_state_ == ProbingState::kActive);
156 RTC_DCHECK_GT(bytes, 0); 128 RTC_DCHECK_GT(bytes, 0);
157 probe_size_last_sent_ = bytes; 129
158 time_last_probe_sent_ms_ = now_ms;
159 if (!clusters_.empty()) { 130 if (!clusters_.empty()) {
160 ProbeCluster* cluster = &clusters_.front(); 131 ProbeCluster* cluster = &clusters_.front();
132 if (cluster->sent_probes == 0) {
133 RTC_DCHECK_EQ(cluster->time_started_ms, -1);
134 cluster->time_started_ms = now_ms;
135 }
161 ++cluster->sent_probes; 136 ++cluster->sent_probes;
137 cluster->bytes_sent += bytes;
138 next_probe_time_ms_ = GetNextProbeTime(clusters_.front());
162 if (cluster->sent_probes == cluster->max_probes) 139 if (cluster->sent_probes == cluster->max_probes)
163 clusters_.pop(); 140 clusters_.pop();
164 if (clusters_.empty()) 141 if (clusters_.empty())
philipel 2017/01/04 10:13:18 if (clusters_.empty()) { probing_state_ = Probin
Sergey Ulanov 2017/01/05 23:31:53 next_probe_time_ms_ is only used in kActive state
philipel 2017/01/09 09:34:34 Acknowledged.
165 probing_state_ = ProbingState::kSuspended; 142 probing_state_ = ProbingState::kSuspended;
166 } 143 }
167 } 144 }
145
146 int64_t BitrateProber::GetNextProbeTime(const ProbeCluster& cluster) {
147 RTC_CHECK_GT(cluster.probe_bitrate_bps, 0);
148 RTC_CHECK_GE(cluster.time_started_ms, 0);
149 // Compute the time delta needed to send the probe at the target bitrate.
150 // Result is in milliseconds.
151 return cluster.time_started_ms +
philipel 2017/01/04 10:13:18 Not super important, but can you break this calcul
Sergey Ulanov 2017/01/05 23:31:53 Will do.
152 static_cast<int>(
153 (1000ll * cluster.bytes_sent * 8 + cluster.probe_bitrate_bps / 2) /
154 cluster.probe_bitrate_bps);
155 }
156
157
168 } // namespace webrtc 158 } // 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