blob: 0dafdf65fd224f80d47cbf0ef5284534c979b4b3 [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"
Peter Boström7c704b82015-12-04 16:13:05 +010018#include "webrtc/base/logging.h"
sprangcd349d92016-07-13 09:11:28 -070019#include "webrtc/base/rate_limiter.h"
Stefan Holmer58c664c2016-02-08 14:31:30 +010020#include "webrtc/base/socket.h"
mflodman0e7e2592015-11-12 21:02:42 -080021#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070022#include "webrtc/modules/congestion_controller/probe_controller.h"
michaeltf082c2a2016-11-07 04:17:14 -080023#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
Erik Språng468e62a2015-07-06 10:50:47 +020024#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
25#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010026#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000027
28namespace webrtc {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000029namespace {
30
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000031static const uint32_t kTimeOffsetSwitchThreshold = 30;
sprangddba75c2016-08-04 09:52:21 -070032static const int64_t kRetransmitWindowSizeMs = 500;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000033
honghaiz059e1832016-06-24 11:03:55 -070034// Makes sure that the bitrate and the min, max values are in valid range.
35static void ClampBitrates(int* bitrate_bps,
36 int* min_bitrate_bps,
37 int* max_bitrate_bps) {
38 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
39 // and that we don't try to set the min bitrate to 0 from any applications.
40 // The congestion controller should allow a min bitrate of 0.
michaeltf082c2a2016-11-07 04:17:14 -080041 if (*min_bitrate_bps < congestion_controller::GetMinBitrateBps())
42 *min_bitrate_bps = congestion_controller::GetMinBitrateBps();
honghaiz059e1832016-06-24 11:03:55 -070043 if (*max_bitrate_bps > 0)
44 *max_bitrate_bps = std::max(*min_bitrate_bps, *max_bitrate_bps);
45 if (*bitrate_bps > 0)
46 *bitrate_bps = std::max(*min_bitrate_bps, *bitrate_bps);
47}
48
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000049class WrappingBitrateEstimator : public RemoteBitrateEstimator {
50 public:
pbosef35f062015-07-27 08:37:06 -070051 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000052 : observer_(observer),
53 clock_(clock),
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000054 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan4fbd1452015-09-28 03:57:14 -070055 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000056 using_absolute_send_time_(false),
stefan4fbd1452015-09-28 03:57:14 -070057 packets_since_absolute_send_time_(0),
michaeltf082c2a2016-11-07 04:17:14 -080058 min_bitrate_bps_(congestion_controller::GetMinBitrateBps()) {}
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000059
60 virtual ~WrappingBitrateEstimator() {}
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000061
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 void IncomingPacket(int64_t arrival_time_ms,
63 size_t payload_size,
pbos2169d8b2016-06-20 11:53:02 -070064 const RTPHeader& header) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000065 CriticalSectionScoped cs(crit_sect_.get());
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000066 PickEstimatorFromHeader(header);
pbos2169d8b2016-06-20 11:53:02 -070067 rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000068 }
69
pbosa26ac922016-02-25 04:50:01 -080070 void Process() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000071 CriticalSectionScoped cs(crit_sect_.get());
pbosa26ac922016-02-25 04:50:01 -080072 rbe_->Process();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000073 }
74
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 int64_t TimeUntilNextProcess() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000076 CriticalSectionScoped cs(crit_sect_.get());
77 return rbe_->TimeUntilNextProcess();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000078 }
79
stefan2328a942015-08-07 04:27:51 -070080 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000081 CriticalSectionScoped cs(crit_sect_.get());
stefan2328a942015-08-07 04:27:51 -070082 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000083 }
84
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000085 void RemoveStream(unsigned int ssrc) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000086 CriticalSectionScoped cs(crit_sect_.get());
87 rbe_->RemoveStream(ssrc);
88 }
89
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000090 bool LatestEstimate(std::vector<unsigned int>* ssrcs,
91 unsigned int* bitrate_bps) const override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000092 CriticalSectionScoped cs(crit_sect_.get());
93 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
94 }
95
nisseef8b61e2016-04-29 06:09:15 -070096 void SetMinBitrate(int min_bitrate_bps) override {
stefan4fbd1452015-09-28 03:57:14 -070097 CriticalSectionScoped cs(crit_sect_.get());
98 rbe_->SetMinBitrate(min_bitrate_bps);
99 min_bitrate_bps_ = min_bitrate_bps;
100 }
101
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000102 private:
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000103 void PickEstimatorFromHeader(const RTPHeader& header)
104 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000105 if (header.extension.hasAbsoluteSendTime) {
106 // If we see AST in header, switch RBE strategy immediately.
107 if (!using_absolute_send_time_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000108 LOG(LS_INFO) <<
109 "WrappingBitrateEstimator: Switching to absolute send time RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000110 using_absolute_send_time_ = true;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000111 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000112 }
113 packets_since_absolute_send_time_ = 0;
114 } else {
115 // When we don't see AST, wait for a few packets before going back to TOF.
116 if (using_absolute_send_time_) {
117 ++packets_since_absolute_send_time_;
118 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000119 LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission "
120 << "time offset RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000121 using_absolute_send_time_ = false;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000122 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000123 }
124 }
125 }
126 }
127
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000128 // Instantiate RBE for Time Offset or Absolute Send Time extensions.
129 void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000130 if (using_absolute_send_time_) {
stefan5e12d362016-07-11 01:44:02 -0700131 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000132 } else {
stefan4fbd1452015-09-28 03:57:14 -0700133 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000134 }
stefan4fbd1452015-09-28 03:57:14 -0700135 rbe_->SetMinBitrate(min_bitrate_bps_);
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000136 }
137
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000138 RemoteBitrateObserver* observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100139 Clock* const clock_;
kwiberg22feaa32016-03-17 09:17:43 -0700140 std::unique_ptr<CriticalSectionWrapper> crit_sect_;
141 std::unique_ptr<RemoteBitrateEstimator> rbe_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000142 bool using_absolute_send_time_;
143 uint32_t packets_since_absolute_send_time_;
stefan4fbd1452015-09-28 03:57:14 -0700144 int min_bitrate_bps_;
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000145
henrikg3c089d72015-09-16 05:37:44 -0700146 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000147};
sprang867fb522015-08-03 04:38:41 -0700148
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000149} // namespace
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000150
Stefan Holmer58c664c2016-02-08 14:31:30 +0100151CongestionController::CongestionController(
152 Clock* clock,
perkjec81bcd2016-05-11 06:01:13 -0700153 Observer* observer,
ivoc14d5dbe2016-07-04 07:06:55 -0700154 RemoteBitrateObserver* remote_bitrate_observer,
155 RtcEventLog* event_log)
perkjec81bcd2016-05-11 06:01:13 -0700156 : clock_(clock),
157 observer_(observer),
158 packet_router_(new PacketRouter()),
perkjfea93092016-05-14 00:58:48 -0700159 pacer_(new PacedSender(clock_, packet_router_.get())),
perkjec81bcd2016-05-11 06:01:13 -0700160 remote_bitrate_estimator_(
161 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
ivoc14d5dbe2016-07-04 07:06:55 -0700162 bitrate_controller_(
163 BitrateController::CreateBitrateController(clock_, event_log)),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700164 probe_controller_(new ProbeController(pacer_.get(), clock_)),
sprangcd349d92016-07-13 09:11:28 -0700165 retransmission_rate_limiter_(
sprangddba75c2016-08-04 09:52:21 -0700166 new RateLimiter(clock, kRetransmitWindowSizeMs)),
perkjec81bcd2016-05-11 06:01:13 -0700167 remote_estimator_proxy_(clock_, packet_router_.get()),
Stefan Holmer280de9e2016-09-30 10:06:51 +0200168 transport_feedback_adapter_(clock_, bitrate_controller_.get()),
michaeltf082c2a2016-11-07 04:17:14 -0800169 min_bitrate_bps_(congestion_controller::GetMinBitrateBps()),
philipeleb680ea2016-08-17 11:11:59 +0200170 max_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700171 last_reported_bitrate_bps_(0),
172 last_reported_fraction_loss_(0),
173 last_reported_rtt_(0),
174 network_state_(kNetworkUp) {
perkjec81bcd2016-05-11 06:01:13 -0700175 Init();
176}
177
178CongestionController::CongestionController(
179 Clock* clock,
180 Observer* observer,
181 RemoteBitrateObserver* remote_bitrate_observer,
ivoc14d5dbe2016-07-04 07:06:55 -0700182 RtcEventLog* event_log,
perkjec81bcd2016-05-11 06:01:13 -0700183 std::unique_ptr<PacketRouter> packet_router,
184 std::unique_ptr<PacedSender> pacer)
185 : clock_(clock),
186 observer_(observer),
187 packet_router_(std::move(packet_router)),
188 pacer_(std::move(pacer)),
Erik Språng6b8d3552015-09-24 15:06:57 +0200189 remote_bitrate_estimator_(
Stefan Holmer58c664c2016-02-08 14:31:30 +0100190 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
Stefan Holmere5904162015-03-26 11:11:06 +0100191 // Constructed last as this object calls the provided callback on
192 // construction.
ivoc14d5dbe2016-07-04 07:06:55 -0700193 bitrate_controller_(
194 BitrateController::CreateBitrateController(clock_, event_log)),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700195 probe_controller_(new ProbeController(pacer_.get(), clock_)),
sprangcd349d92016-07-13 09:11:28 -0700196 retransmission_rate_limiter_(
sprangddba75c2016-08-04 09:52:21 -0700197 new RateLimiter(clock, kRetransmitWindowSizeMs)),
perkjec81bcd2016-05-11 06:01:13 -0700198 remote_estimator_proxy_(clock_, packet_router_.get()),
Stefan Holmer280de9e2016-09-30 10:06:51 +0200199 transport_feedback_adapter_(clock_, bitrate_controller_.get()),
michaeltf082c2a2016-11-07 04:17:14 -0800200 min_bitrate_bps_(congestion_controller::GetMinBitrateBps()),
philipeleb680ea2016-08-17 11:11:59 +0200201 max_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700202 last_reported_bitrate_bps_(0),
203 last_reported_fraction_loss_(0),
204 last_reported_rtt_(0),
205 network_state_(kNetworkUp) {
perkjec81bcd2016-05-11 06:01:13 -0700206 Init();
207}
208
209CongestionController::~CongestionController() {}
210
211void CongestionController::Init() {
Stefan Holmer280de9e2016-09-30 10:06:51 +0200212 transport_feedback_adapter_.InitBwe();
213 transport_feedback_adapter_.SetMinBitrate(min_bitrate_bps_);
perkje30c2722016-05-09 04:57:11 -0700214}
215
mflodman0c478b32015-10-21 15:52:16 +0200216void CongestionController::SetBweBitrates(int min_bitrate_bps,
217 int start_bitrate_bps,
218 int max_bitrate_bps) {
honghaiz059e1832016-06-24 11:03:55 -0700219 ClampBitrates(&start_bitrate_bps, &min_bitrate_bps, &max_bitrate_bps);
philipelc6957c72016-04-28 15:52:49 +0200220 bitrate_controller_->SetBitrates(start_bitrate_bps,
221 min_bitrate_bps,
222 max_bitrate_bps);
223
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700224 probe_controller_->SetBitrates(min_bitrate_bps, start_bitrate_bps,
225 max_bitrate_bps);
philipeleb680ea2016-08-17 11:11:59 +0200226 max_bitrate_bps_ = max_bitrate_bps;
227
Stefan Holmer789ba922016-02-17 15:52:17 +0100228 if (remote_bitrate_estimator_)
stefan4fbd1452015-09-28 03:57:14 -0700229 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
stefan4fbd1452015-09-28 03:57:14 -0700230 min_bitrate_bps_ = min_bitrate_bps;
Stefan Holmer280de9e2016-09-30 10:06:51 +0200231 transport_feedback_adapter_.SetMinBitrate(min_bitrate_bps_);
perkjec81bcd2016-05-11 06:01:13 -0700232 MaybeTriggerOnNetworkChanged();
stefan4fbd1452015-09-28 03:57:14 -0700233}
234
honghaiz059e1832016-06-24 11:03:55 -0700235void CongestionController::ResetBweAndBitrates(int bitrate_bps,
236 int min_bitrate_bps,
237 int max_bitrate_bps) {
238 ClampBitrates(&bitrate_bps, &min_bitrate_bps, &max_bitrate_bps);
239 // TODO(honghaiz): Recreate this object once the bitrate controller is
240 // no longer exposed outside CongestionController.
241 bitrate_controller_->ResetBitrates(bitrate_bps, min_bitrate_bps,
242 max_bitrate_bps);
243 min_bitrate_bps_ = min_bitrate_bps;
philipeleb680ea2016-08-17 11:11:59 +0200244 max_bitrate_bps_ = max_bitrate_bps;
honghaiz059e1832016-06-24 11:03:55 -0700245 // TODO(honghaiz): Recreate this object once the remote bitrate estimator is
246 // no longer exposed outside CongestionController.
247 if (remote_bitrate_estimator_)
248 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
249
Stefan Holmer280de9e2016-09-30 10:06:51 +0200250 transport_feedback_adapter_.InitBwe();
251 transport_feedback_adapter_.SetMinBitrate(min_bitrate_bps);
honghaiz059e1832016-06-24 11:03:55 -0700252 // TODO(holmer): Trigger a new probe once mid-call probing is implemented.
253 MaybeTriggerOnNetworkChanged();
254}
255
mflodman0c478b32015-10-21 15:52:16 +0200256BitrateController* CongestionController::GetBitrateController() const {
bjornv@webrtc.orgcb89c6f2012-06-05 12:25:35 +0000257 return bitrate_controller_.get();
stefan@webrtc.orgf7288142012-06-05 10:44:00 +0000258}
259
mflodman0c478b32015-10-21 15:52:16 +0200260RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator(
Stefan Holmer789ba922016-02-17 15:52:17 +0100261 bool send_side_bwe) {
262 if (send_side_bwe) {
263 return &remote_estimator_proxy_;
264 } else {
mflodmana20de202015-10-18 22:08:19 -0700265 return remote_bitrate_estimator_.get();
Stefan Holmer789ba922016-02-17 15:52:17 +0100266 }
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000267}
268
mflodman0c478b32015-10-21 15:52:16 +0200269TransportFeedbackObserver*
270CongestionController::GetTransportFeedbackObserver() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100271 return &transport_feedback_adapter_;
mflodman949c2f02015-10-16 02:31:11 -0700272}
273
sprangcd349d92016-07-13 09:11:28 -0700274RateLimiter* CongestionController::GetRetransmissionRateLimiter() {
275 return retransmission_rate_limiter_.get();
276}
277
perkj71ee44c2016-06-15 00:47:53 -0700278void CongestionController::SetAllocatedSendBitrateLimits(
279 int min_send_bitrate_bps,
280 int max_padding_bitrate_bps) {
281 pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps);
mflodman0e7e2592015-11-12 21:02:42 -0800282}
283
mflodman0c478b32015-10-21 15:52:16 +0200284int64_t CongestionController::GetPacerQueuingDelayMs() const {
asapersson14f12502016-09-07 23:14:50 -0700285 return IsNetworkDown() ? 0 : pacer_->QueueInMs();
Stefan Holmere5904162015-03-26 11:11:06 +0100286}
287
mflodman0c478b32015-10-21 15:52:16 +0200288void CongestionController::SignalNetworkState(NetworkState state) {
perkj9b522f82016-07-07 00:36:28 -0700289 LOG(LS_INFO) << "SignalNetworkState "
290 << (state == kNetworkUp ? "Up" : "Down");
stefan457a61d2015-10-14 03:12:59 -0700291 if (state == kNetworkUp) {
292 pacer_->Resume();
293 } else {
294 pacer_->Pause();
295 }
perkjfea93092016-05-14 00:58:48 -0700296 {
297 rtc::CritScope cs(&critsect_);
298 network_state_ = state;
299 }
300 MaybeTriggerOnNetworkChanged();
stefan457a61d2015-10-14 03:12:59 -0700301}
302
mflodman0c478b32015-10-21 15:52:16 +0200303void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100304 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
Stefan Holmer13181032016-07-29 14:48:54 +0200305 sent_packet.send_time_ms);
stefanc1aeaf02015-10-15 07:26:07 -0700306}
Stefan Holmer789ba922016-02-17 15:52:17 +0100307
308void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
309 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
310 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
311}
312
313int64_t CongestionController::TimeUntilNextProcess() {
314 return std::min(bitrate_controller_->TimeUntilNextProcess(),
315 remote_bitrate_estimator_->TimeUntilNextProcess());
316}
317
pbosa26ac922016-02-25 04:50:01 -0800318void CongestionController::Process() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100319 bitrate_controller_->Process();
320 remote_bitrate_estimator_->Process();
perkjec81bcd2016-05-11 06:01:13 -0700321 MaybeTriggerOnNetworkChanged();
322}
323
324void CongestionController::MaybeTriggerOnNetworkChanged() {
325 // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a
326 // BitrateObserver is used. Remove this check once the ctor is removed.
327 if (!observer_)
328 return;
329
330 uint32_t bitrate_bps;
331 uint8_t fraction_loss;
332 int64_t rtt;
perkjfea93092016-05-14 00:58:48 -0700333 bool estimate_changed = bitrate_controller_->GetNetworkParameters(
perkjec81bcd2016-05-11 06:01:13 -0700334 &bitrate_bps, &fraction_loss, &rtt);
sprangcd349d92016-07-13 09:11:28 -0700335 if (estimate_changed) {
perkjec81bcd2016-05-11 06:01:13 -0700336 pacer_->SetEstimatedBitrate(bitrate_bps);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700337 probe_controller_->SetEstimatedBitrate(bitrate_bps);
sprangcd349d92016-07-13 09:11:28 -0700338 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
339 }
perkjfea93092016-05-14 00:58:48 -0700340
341 bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps;
342
343 if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) {
perkjec81bcd2016-05-11 06:01:13 -0700344 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
minyue8927b052016-11-07 07:51:20 -0800345 remote_estimator_proxy_.OnBitrateChanged(bitrate_bps);
perkjec81bcd2016-05-11 06:01:13 -0700346 }
347}
348
perkjfea93092016-05-14 00:58:48 -0700349bool CongestionController::HasNetworkParametersToReportChanged(
350 uint32_t bitrate_bps,
351 uint8_t fraction_loss,
352 int64_t rtt) {
perkjec81bcd2016-05-11 06:01:13 -0700353 rtc::CritScope cs(&critsect_);
perkjfea93092016-05-14 00:58:48 -0700354 bool changed =
355 last_reported_bitrate_bps_ != bitrate_bps ||
356 (bitrate_bps > 0 && (last_reported_fraction_loss_ != fraction_loss ||
357 last_reported_rtt_ != rtt));
perkj9b522f82016-07-07 00:36:28 -0700358 if (changed && (last_reported_bitrate_bps_ == 0 || bitrate_bps == 0)) {
359 LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " << bitrate_bps
360 << " bps.";
361 }
perkjfea93092016-05-14 00:58:48 -0700362 last_reported_bitrate_bps_ = bitrate_bps;
363 last_reported_fraction_loss_ = fraction_loss;
364 last_reported_rtt_ = rtt;
365 return changed;
366}
367
368bool CongestionController::IsSendQueueFull() const {
369 return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs;
370}
371
372bool CongestionController::IsNetworkDown() const {
373 rtc::CritScope cs(&critsect_);
374 return network_state_ == kNetworkDown;
Stefan Holmer789ba922016-02-17 15:52:17 +0100375}
376
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000377} // namespace webrtc