blob: 68c306ce10077a93bbbf8e6c792b8dd20121970d [file] [log] [blame]
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Stefan Holmer80e12072016-02-23 13:30:42 +010011#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000012
Stefan Holmer62a5ccd2016-02-16 17:07:21 +010013#include <algorithm>
kwiberg22feaa32016-03-17 09:17:43 -070014#include <memory>
Stefan Holmer58c664c2016-02-08 14:31:30 +010015#include <vector>
16
stefan@webrtc.orga50e6f02015-03-09 10:06:40 +000017#include "webrtc/base/checks.h"
kwiberg4485ffb2016-04-26 08:14:39 -070018#include "webrtc/base/constructormagic.h"
Peter Boström7c704b82015-12-04 16:13:05 +010019#include "webrtc/base/logging.h"
sprangcd349d92016-07-13 09:11:28 -070020#include "webrtc/base/rate_limiter.h"
Stefan Holmer58c664c2016-02-08 14:31:30 +010021#include "webrtc/base/socket.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000022#include "webrtc/base/thread_annotations.h"
mflodman0e7e2592015-11-12 21:02:42 -080023#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
philipel863a8262016-06-17 09:21:34 -070024#include "webrtc/modules/congestion_controller/delay_based_bwe.h"
sprang867fb522015-08-03 04:38:41 -070025#include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h"
Erik Språng468e62a2015-07-06 10:50:47 +020026#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
27#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010028#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Peter Boström7623ce42015-12-09 12:13:30 +010030#include "webrtc/video/payload_router.h"
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000031
32namespace webrtc {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000033namespace {
34
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000035static const uint32_t kTimeOffsetSwitchThreshold = 30;
sprangddba75c2016-08-04 09:52:21 -070036static const int64_t kRetransmitWindowSizeMs = 500;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000037
honghaiz059e1832016-06-24 11:03:55 -070038// Makes sure that the bitrate and the min, max values are in valid range.
39static void ClampBitrates(int* bitrate_bps,
40 int* min_bitrate_bps,
41 int* max_bitrate_bps) {
42 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
43 // and that we don't try to set the min bitrate to 0 from any applications.
44 // The congestion controller should allow a min bitrate of 0.
45 const int kMinBitrateBps = 10000;
46 if (*min_bitrate_bps < kMinBitrateBps)
47 *min_bitrate_bps = kMinBitrateBps;
48 if (*max_bitrate_bps > 0)
49 *max_bitrate_bps = std::max(*min_bitrate_bps, *max_bitrate_bps);
50 if (*bitrate_bps > 0)
51 *bitrate_bps = std::max(*min_bitrate_bps, *bitrate_bps);
52}
53
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000054class WrappingBitrateEstimator : public RemoteBitrateEstimator {
55 public:
pbosef35f062015-07-27 08:37:06 -070056 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000057 : observer_(observer),
58 clock_(clock),
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000059 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan4fbd1452015-09-28 03:57:14 -070060 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000061 using_absolute_send_time_(false),
stefan4fbd1452015-09-28 03:57:14 -070062 packets_since_absolute_send_time_(0),
63 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {}
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000064
65 virtual ~WrappingBitrateEstimator() {}
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000066
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 void IncomingPacket(int64_t arrival_time_ms,
68 size_t payload_size,
pbos2169d8b2016-06-20 11:53:02 -070069 const RTPHeader& header) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000070 CriticalSectionScoped cs(crit_sect_.get());
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000071 PickEstimatorFromHeader(header);
pbos2169d8b2016-06-20 11:53:02 -070072 rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000073 }
74
pbosa26ac922016-02-25 04:50:01 -080075 void Process() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000076 CriticalSectionScoped cs(crit_sect_.get());
pbosa26ac922016-02-25 04:50:01 -080077 rbe_->Process();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000078 }
79
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 int64_t TimeUntilNextProcess() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000081 CriticalSectionScoped cs(crit_sect_.get());
82 return rbe_->TimeUntilNextProcess();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000083 }
84
stefan2328a942015-08-07 04:27:51 -070085 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000086 CriticalSectionScoped cs(crit_sect_.get());
stefan2328a942015-08-07 04:27:51 -070087 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000088 }
89
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000090 void RemoveStream(unsigned int ssrc) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000091 CriticalSectionScoped cs(crit_sect_.get());
92 rbe_->RemoveStream(ssrc);
93 }
94
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000095 bool LatestEstimate(std::vector<unsigned int>* ssrcs,
96 unsigned int* bitrate_bps) const override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000097 CriticalSectionScoped cs(crit_sect_.get());
98 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
99 }
100
nisseef8b61e2016-04-29 06:09:15 -0700101 void SetMinBitrate(int min_bitrate_bps) override {
stefan4fbd1452015-09-28 03:57:14 -0700102 CriticalSectionScoped cs(crit_sect_.get());
103 rbe_->SetMinBitrate(min_bitrate_bps);
104 min_bitrate_bps_ = min_bitrate_bps;
105 }
106
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000107 private:
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000108 void PickEstimatorFromHeader(const RTPHeader& header)
109 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000110 if (header.extension.hasAbsoluteSendTime) {
111 // If we see AST in header, switch RBE strategy immediately.
112 if (!using_absolute_send_time_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000113 LOG(LS_INFO) <<
114 "WrappingBitrateEstimator: Switching to absolute send time RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000115 using_absolute_send_time_ = true;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000116 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000117 }
118 packets_since_absolute_send_time_ = 0;
119 } else {
120 // When we don't see AST, wait for a few packets before going back to TOF.
121 if (using_absolute_send_time_) {
122 ++packets_since_absolute_send_time_;
123 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000124 LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission "
125 << "time offset RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000126 using_absolute_send_time_ = false;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000127 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000128 }
129 }
130 }
131 }
132
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000133 // Instantiate RBE for Time Offset or Absolute Send Time extensions.
134 void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000135 if (using_absolute_send_time_) {
stefan5e12d362016-07-11 01:44:02 -0700136 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000137 } else {
stefan4fbd1452015-09-28 03:57:14 -0700138 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000139 }
stefan4fbd1452015-09-28 03:57:14 -0700140 rbe_->SetMinBitrate(min_bitrate_bps_);
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000141 }
142
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000143 RemoteBitrateObserver* observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100144 Clock* const clock_;
kwiberg22feaa32016-03-17 09:17:43 -0700145 std::unique_ptr<CriticalSectionWrapper> crit_sect_;
146 std::unique_ptr<RemoteBitrateEstimator> rbe_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000147 bool using_absolute_send_time_;
148 uint32_t packets_since_absolute_send_time_;
stefan4fbd1452015-09-28 03:57:14 -0700149 int min_bitrate_bps_;
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000150
henrikg3c089d72015-09-16 05:37:44 -0700151 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000152};
sprang867fb522015-08-03 04:38:41 -0700153
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000154} // namespace
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000155
Stefan Holmer58c664c2016-02-08 14:31:30 +0100156CongestionController::CongestionController(
157 Clock* clock,
perkjec81bcd2016-05-11 06:01:13 -0700158 Observer* observer,
ivoc14d5dbe2016-07-04 07:06:55 -0700159 RemoteBitrateObserver* remote_bitrate_observer,
160 RtcEventLog* event_log)
perkjec81bcd2016-05-11 06:01:13 -0700161 : clock_(clock),
162 observer_(observer),
163 packet_router_(new PacketRouter()),
perkjfea93092016-05-14 00:58:48 -0700164 pacer_(new PacedSender(clock_, packet_router_.get())),
perkjec81bcd2016-05-11 06:01:13 -0700165 remote_bitrate_estimator_(
166 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
ivoc14d5dbe2016-07-04 07:06:55 -0700167 bitrate_controller_(
168 BitrateController::CreateBitrateController(clock_, event_log)),
sprangcd349d92016-07-13 09:11:28 -0700169 retransmission_rate_limiter_(
sprangddba75c2016-08-04 09:52:21 -0700170 new RateLimiter(clock, kRetransmitWindowSizeMs)),
perkjec81bcd2016-05-11 06:01:13 -0700171 remote_estimator_proxy_(clock_, packet_router_.get()),
172 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
173 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
philipeleb680ea2016-08-17 11:11:59 +0200174 max_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700175 last_reported_bitrate_bps_(0),
176 last_reported_fraction_loss_(0),
177 last_reported_rtt_(0),
178 network_state_(kNetworkUp) {
perkjec81bcd2016-05-11 06:01:13 -0700179 Init();
180}
181
182CongestionController::CongestionController(
183 Clock* clock,
184 Observer* observer,
185 RemoteBitrateObserver* remote_bitrate_observer,
ivoc14d5dbe2016-07-04 07:06:55 -0700186 RtcEventLog* event_log,
perkjec81bcd2016-05-11 06:01:13 -0700187 std::unique_ptr<PacketRouter> packet_router,
188 std::unique_ptr<PacedSender> pacer)
189 : clock_(clock),
190 observer_(observer),
191 packet_router_(std::move(packet_router)),
192 pacer_(std::move(pacer)),
Erik Språng6b8d3552015-09-24 15:06:57 +0200193 remote_bitrate_estimator_(
Stefan Holmer58c664c2016-02-08 14:31:30 +0100194 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
Stefan Holmere5904162015-03-26 11:11:06 +0100195 // Constructed last as this object calls the provided callback on
196 // construction.
ivoc14d5dbe2016-07-04 07:06:55 -0700197 bitrate_controller_(
198 BitrateController::CreateBitrateController(clock_, event_log)),
sprangcd349d92016-07-13 09:11:28 -0700199 retransmission_rate_limiter_(
sprangddba75c2016-08-04 09:52:21 -0700200 new RateLimiter(clock, kRetransmitWindowSizeMs)),
perkjec81bcd2016-05-11 06:01:13 -0700201 remote_estimator_proxy_(clock_, packet_router_.get()),
Stefan Holmer789ba922016-02-17 15:52:17 +0100202 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
perkjec81bcd2016-05-11 06:01:13 -0700203 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
philipeleb680ea2016-08-17 11:11:59 +0200204 max_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700205 last_reported_bitrate_bps_(0),
206 last_reported_fraction_loss_(0),
207 last_reported_rtt_(0),
208 network_state_(kNetworkUp) {
perkjec81bcd2016-05-11 06:01:13 -0700209 Init();
210}
211
212CongestionController::~CongestionController() {}
213
214void CongestionController::Init() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100215 transport_feedback_adapter_.SetBitrateEstimator(
stefan5e12d362016-07-11 01:44:02 -0700216 new DelayBasedBwe(&transport_feedback_adapter_, clock_));
Stefan Holmer789ba922016-02-17 15:52:17 +0100217 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
218 min_bitrate_bps_);
philipeleb680ea2016-08-17 11:11:59 +0200219 pacer_->CreateProbeCluster(900000, 6);
220 pacer_->CreateProbeCluster(1800000, 5);
perkje30c2722016-05-09 04:57:11 -0700221}
222
mflodman0c478b32015-10-21 15:52:16 +0200223void CongestionController::SetBweBitrates(int min_bitrate_bps,
224 int start_bitrate_bps,
225 int max_bitrate_bps) {
honghaiz059e1832016-06-24 11:03:55 -0700226 ClampBitrates(&start_bitrate_bps, &min_bitrate_bps, &max_bitrate_bps);
philipelc6957c72016-04-28 15:52:49 +0200227 bitrate_controller_->SetBitrates(start_bitrate_bps,
228 min_bitrate_bps,
229 max_bitrate_bps);
230
philipeleb680ea2016-08-17 11:11:59 +0200231 {
232 // Only do probing if:
233 // - we are mid-call, which we consider to be if
234 // |last_reported_bitrate_bps_| != 0, and
235 // - the current bitrate is lower than the new |max_bitrate_bps|, and
236 // - we actually want to increase the |max_bitrate_bps_|.
237 rtc::CritScope cs(&critsect_);
238 if (last_reported_bitrate_bps_ != 0 &&
239 last_reported_bitrate_bps_ < static_cast<uint32_t>(max_bitrate_bps) &&
240 max_bitrate_bps > max_bitrate_bps_) {
241 pacer_->CreateProbeCluster(max_bitrate_bps, 5);
242 }
243 }
244 max_bitrate_bps_ = max_bitrate_bps;
245
Stefan Holmer789ba922016-02-17 15:52:17 +0100246 if (remote_bitrate_estimator_)
stefan4fbd1452015-09-28 03:57:14 -0700247 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
stefan4fbd1452015-09-28 03:57:14 -0700248 min_bitrate_bps_ = min_bitrate_bps;
Stefan Holmer789ba922016-02-17 15:52:17 +0100249 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
250 min_bitrate_bps_);
perkjec81bcd2016-05-11 06:01:13 -0700251 MaybeTriggerOnNetworkChanged();
stefan4fbd1452015-09-28 03:57:14 -0700252}
253
honghaiz059e1832016-06-24 11:03:55 -0700254void CongestionController::ResetBweAndBitrates(int bitrate_bps,
255 int min_bitrate_bps,
256 int max_bitrate_bps) {
257 ClampBitrates(&bitrate_bps, &min_bitrate_bps, &max_bitrate_bps);
258 // TODO(honghaiz): Recreate this object once the bitrate controller is
259 // no longer exposed outside CongestionController.
260 bitrate_controller_->ResetBitrates(bitrate_bps, min_bitrate_bps,
261 max_bitrate_bps);
262 min_bitrate_bps_ = min_bitrate_bps;
philipeleb680ea2016-08-17 11:11:59 +0200263 max_bitrate_bps_ = max_bitrate_bps;
honghaiz059e1832016-06-24 11:03:55 -0700264 // TODO(honghaiz): Recreate this object once the remote bitrate estimator is
265 // no longer exposed outside CongestionController.
266 if (remote_bitrate_estimator_)
267 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
268
Irfan Sheriffb578d6c2016-08-01 12:16:43 -0700269 RemoteBitrateEstimator* rbe = new DelayBasedBwe(
stefan5e12d362016-07-11 01:44:02 -0700270 &transport_feedback_adapter_, clock_);
honghaiz059e1832016-06-24 11:03:55 -0700271 transport_feedback_adapter_.SetBitrateEstimator(rbe);
272 rbe->SetMinBitrate(min_bitrate_bps);
273 // TODO(holmer): Trigger a new probe once mid-call probing is implemented.
274 MaybeTriggerOnNetworkChanged();
275}
276
mflodman0c478b32015-10-21 15:52:16 +0200277BitrateController* CongestionController::GetBitrateController() const {
bjornv@webrtc.orgcb89c6f2012-06-05 12:25:35 +0000278 return bitrate_controller_.get();
stefan@webrtc.orgf7288142012-06-05 10:44:00 +0000279}
280
mflodman0c478b32015-10-21 15:52:16 +0200281RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator(
Stefan Holmer789ba922016-02-17 15:52:17 +0100282 bool send_side_bwe) {
283 if (send_side_bwe) {
284 return &remote_estimator_proxy_;
285 } else {
mflodmana20de202015-10-18 22:08:19 -0700286 return remote_bitrate_estimator_.get();
Stefan Holmer789ba922016-02-17 15:52:17 +0100287 }
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000288}
289
mflodman0c478b32015-10-21 15:52:16 +0200290TransportFeedbackObserver*
291CongestionController::GetTransportFeedbackObserver() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100292 return &transport_feedback_adapter_;
mflodman949c2f02015-10-16 02:31:11 -0700293}
294
sprangcd349d92016-07-13 09:11:28 -0700295RateLimiter* CongestionController::GetRetransmissionRateLimiter() {
296 return retransmission_rate_limiter_.get();
297}
298
perkj71ee44c2016-06-15 00:47:53 -0700299void CongestionController::SetAllocatedSendBitrateLimits(
300 int min_send_bitrate_bps,
301 int max_padding_bitrate_bps) {
302 pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps);
mflodman0e7e2592015-11-12 21:02:42 -0800303}
304
mflodman0c478b32015-10-21 15:52:16 +0200305int64_t CongestionController::GetPacerQueuingDelayMs() const {
Stefan Holmere5904162015-03-26 11:11:06 +0100306 return pacer_->QueueInMs();
307}
308
mflodman0c478b32015-10-21 15:52:16 +0200309void CongestionController::SignalNetworkState(NetworkState state) {
perkj9b522f82016-07-07 00:36:28 -0700310 LOG(LS_INFO) << "SignalNetworkState "
311 << (state == kNetworkUp ? "Up" : "Down");
stefan457a61d2015-10-14 03:12:59 -0700312 if (state == kNetworkUp) {
313 pacer_->Resume();
314 } else {
315 pacer_->Pause();
316 }
perkjfea93092016-05-14 00:58:48 -0700317 {
318 rtc::CritScope cs(&critsect_);
319 network_state_ = state;
320 }
321 MaybeTriggerOnNetworkChanged();
stefan457a61d2015-10-14 03:12:59 -0700322}
323
mflodman0c478b32015-10-21 15:52:16 +0200324void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100325 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
Stefan Holmer13181032016-07-29 14:48:54 +0200326 sent_packet.send_time_ms);
stefanc1aeaf02015-10-15 07:26:07 -0700327}
Stefan Holmer789ba922016-02-17 15:52:17 +0100328
329void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
330 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
331 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
332}
333
334int64_t CongestionController::TimeUntilNextProcess() {
335 return std::min(bitrate_controller_->TimeUntilNextProcess(),
336 remote_bitrate_estimator_->TimeUntilNextProcess());
337}
338
pbosa26ac922016-02-25 04:50:01 -0800339void CongestionController::Process() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100340 bitrate_controller_->Process();
341 remote_bitrate_estimator_->Process();
perkjec81bcd2016-05-11 06:01:13 -0700342 MaybeTriggerOnNetworkChanged();
343}
344
345void CongestionController::MaybeTriggerOnNetworkChanged() {
346 // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a
347 // BitrateObserver is used. Remove this check once the ctor is removed.
348 if (!observer_)
349 return;
350
351 uint32_t bitrate_bps;
352 uint8_t fraction_loss;
353 int64_t rtt;
perkjfea93092016-05-14 00:58:48 -0700354 bool estimate_changed = bitrate_controller_->GetNetworkParameters(
perkjec81bcd2016-05-11 06:01:13 -0700355 &bitrate_bps, &fraction_loss, &rtt);
sprangcd349d92016-07-13 09:11:28 -0700356 if (estimate_changed) {
perkjec81bcd2016-05-11 06:01:13 -0700357 pacer_->SetEstimatedBitrate(bitrate_bps);
sprangcd349d92016-07-13 09:11:28 -0700358 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
359 }
perkjfea93092016-05-14 00:58:48 -0700360
361 bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps;
362
363 if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) {
perkjec81bcd2016-05-11 06:01:13 -0700364 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
365 }
366}
367
perkjfea93092016-05-14 00:58:48 -0700368bool CongestionController::HasNetworkParametersToReportChanged(
369 uint32_t bitrate_bps,
370 uint8_t fraction_loss,
371 int64_t rtt) {
perkjec81bcd2016-05-11 06:01:13 -0700372 rtc::CritScope cs(&critsect_);
perkjfea93092016-05-14 00:58:48 -0700373 bool changed =
374 last_reported_bitrate_bps_ != bitrate_bps ||
375 (bitrate_bps > 0 && (last_reported_fraction_loss_ != fraction_loss ||
376 last_reported_rtt_ != rtt));
perkj9b522f82016-07-07 00:36:28 -0700377 if (changed && (last_reported_bitrate_bps_ == 0 || bitrate_bps == 0)) {
378 LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " << bitrate_bps
379 << " bps.";
380 }
perkjfea93092016-05-14 00:58:48 -0700381 last_reported_bitrate_bps_ = bitrate_bps;
382 last_reported_fraction_loss_ = fraction_loss;
383 last_reported_rtt_ = rtt;
384 return changed;
385}
386
387bool CongestionController::IsSendQueueFull() const {
388 return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs;
389}
390
391bool CongestionController::IsNetworkDown() const {
392 rtc::CritScope cs(&critsect_);
393 return network_state_ == kNetworkDown;
Stefan Holmer789ba922016-02-17 15:52:17 +0100394}
395
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000396} // namespace webrtc