blob: c0ec3da0f9a42bca4b75d8e1a18d47c61659e08c [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"
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070025#include "webrtc/modules/congestion_controller/probe_controller.h"
sprang867fb522015-08-03 04:38:41 -070026#include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h"
Erik Språng468e62a2015-07-06 10:50:47 +020027#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
28#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010029#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010030#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Peter Boström7623ce42015-12-09 12:13:30 +010031#include "webrtc/video/payload_router.h"
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000032
33namespace webrtc {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000034namespace {
35
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000036static const uint32_t kTimeOffsetSwitchThreshold = 30;
sprangddba75c2016-08-04 09:52:21 -070037static const int64_t kRetransmitWindowSizeMs = 500;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000038
honghaiz059e1832016-06-24 11:03:55 -070039// Makes sure that the bitrate and the min, max values are in valid range.
40static void ClampBitrates(int* bitrate_bps,
41 int* min_bitrate_bps,
42 int* max_bitrate_bps) {
43 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
44 // and that we don't try to set the min bitrate to 0 from any applications.
45 // The congestion controller should allow a min bitrate of 0.
46 const int kMinBitrateBps = 10000;
47 if (*min_bitrate_bps < kMinBitrateBps)
48 *min_bitrate_bps = kMinBitrateBps;
49 if (*max_bitrate_bps > 0)
50 *max_bitrate_bps = std::max(*min_bitrate_bps, *max_bitrate_bps);
51 if (*bitrate_bps > 0)
52 *bitrate_bps = std::max(*min_bitrate_bps, *bitrate_bps);
53}
54
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000055class WrappingBitrateEstimator : public RemoteBitrateEstimator {
56 public:
pbosef35f062015-07-27 08:37:06 -070057 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000058 : observer_(observer),
59 clock_(clock),
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000060 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan4fbd1452015-09-28 03:57:14 -070061 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000062 using_absolute_send_time_(false),
stefan4fbd1452015-09-28 03:57:14 -070063 packets_since_absolute_send_time_(0),
64 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {}
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000065
66 virtual ~WrappingBitrateEstimator() {}
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000067
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 void IncomingPacket(int64_t arrival_time_ms,
69 size_t payload_size,
pbos2169d8b2016-06-20 11:53:02 -070070 const RTPHeader& header) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000071 CriticalSectionScoped cs(crit_sect_.get());
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000072 PickEstimatorFromHeader(header);
pbos2169d8b2016-06-20 11:53:02 -070073 rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000074 }
75
pbosa26ac922016-02-25 04:50:01 -080076 void Process() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000077 CriticalSectionScoped cs(crit_sect_.get());
pbosa26ac922016-02-25 04:50:01 -080078 rbe_->Process();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000079 }
80
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000081 int64_t TimeUntilNextProcess() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000082 CriticalSectionScoped cs(crit_sect_.get());
83 return rbe_->TimeUntilNextProcess();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000084 }
85
stefan2328a942015-08-07 04:27:51 -070086 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000087 CriticalSectionScoped cs(crit_sect_.get());
stefan2328a942015-08-07 04:27:51 -070088 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000089 }
90
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 void RemoveStream(unsigned int ssrc) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000092 CriticalSectionScoped cs(crit_sect_.get());
93 rbe_->RemoveStream(ssrc);
94 }
95
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 bool LatestEstimate(std::vector<unsigned int>* ssrcs,
97 unsigned int* bitrate_bps) const override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000098 CriticalSectionScoped cs(crit_sect_.get());
99 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
100 }
101
nisseef8b61e2016-04-29 06:09:15 -0700102 void SetMinBitrate(int min_bitrate_bps) override {
stefan4fbd1452015-09-28 03:57:14 -0700103 CriticalSectionScoped cs(crit_sect_.get());
104 rbe_->SetMinBitrate(min_bitrate_bps);
105 min_bitrate_bps_ = min_bitrate_bps;
106 }
107
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000108 private:
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000109 void PickEstimatorFromHeader(const RTPHeader& header)
110 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000111 if (header.extension.hasAbsoluteSendTime) {
112 // If we see AST in header, switch RBE strategy immediately.
113 if (!using_absolute_send_time_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000114 LOG(LS_INFO) <<
115 "WrappingBitrateEstimator: Switching to absolute send time RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000116 using_absolute_send_time_ = true;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000117 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000118 }
119 packets_since_absolute_send_time_ = 0;
120 } else {
121 // When we don't see AST, wait for a few packets before going back to TOF.
122 if (using_absolute_send_time_) {
123 ++packets_since_absolute_send_time_;
124 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000125 LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission "
126 << "time offset RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000127 using_absolute_send_time_ = false;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000128 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000129 }
130 }
131 }
132 }
133
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000134 // Instantiate RBE for Time Offset or Absolute Send Time extensions.
135 void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000136 if (using_absolute_send_time_) {
stefan5e12d362016-07-11 01:44:02 -0700137 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000138 } else {
stefan4fbd1452015-09-28 03:57:14 -0700139 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000140 }
stefan4fbd1452015-09-28 03:57:14 -0700141 rbe_->SetMinBitrate(min_bitrate_bps_);
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000142 }
143
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000144 RemoteBitrateObserver* observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100145 Clock* const clock_;
kwiberg22feaa32016-03-17 09:17:43 -0700146 std::unique_ptr<CriticalSectionWrapper> crit_sect_;
147 std::unique_ptr<RemoteBitrateEstimator> rbe_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000148 bool using_absolute_send_time_;
149 uint32_t packets_since_absolute_send_time_;
stefan4fbd1452015-09-28 03:57:14 -0700150 int min_bitrate_bps_;
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000151
henrikg3c089d72015-09-16 05:37:44 -0700152 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000153};
sprang867fb522015-08-03 04:38:41 -0700154
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000155} // namespace
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000156
Stefan Holmer58c664c2016-02-08 14:31:30 +0100157CongestionController::CongestionController(
158 Clock* clock,
perkjec81bcd2016-05-11 06:01:13 -0700159 Observer* observer,
ivoc14d5dbe2016-07-04 07:06:55 -0700160 RemoteBitrateObserver* remote_bitrate_observer,
161 RtcEventLog* event_log)
perkjec81bcd2016-05-11 06:01:13 -0700162 : clock_(clock),
163 observer_(observer),
164 packet_router_(new PacketRouter()),
perkjfea93092016-05-14 00:58:48 -0700165 pacer_(new PacedSender(clock_, packet_router_.get())),
perkjec81bcd2016-05-11 06:01:13 -0700166 remote_bitrate_estimator_(
167 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
ivoc14d5dbe2016-07-04 07:06:55 -0700168 bitrate_controller_(
169 BitrateController::CreateBitrateController(clock_, event_log)),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700170 probe_controller_(new ProbeController(pacer_.get(), clock_)),
sprangcd349d92016-07-13 09:11:28 -0700171 retransmission_rate_limiter_(
sprangddba75c2016-08-04 09:52:21 -0700172 new RateLimiter(clock, kRetransmitWindowSizeMs)),
perkjec81bcd2016-05-11 06:01:13 -0700173 remote_estimator_proxy_(clock_, packet_router_.get()),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700174 transport_feedback_adapter_(clock_),
perkjec81bcd2016-05-11 06:01:13 -0700175 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
philipeleb680ea2016-08-17 11:11:59 +0200176 max_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700177 last_reported_bitrate_bps_(0),
178 last_reported_fraction_loss_(0),
179 last_reported_rtt_(0),
180 network_state_(kNetworkUp) {
perkjec81bcd2016-05-11 06:01:13 -0700181 Init();
182}
183
184CongestionController::CongestionController(
185 Clock* clock,
186 Observer* observer,
187 RemoteBitrateObserver* remote_bitrate_observer,
ivoc14d5dbe2016-07-04 07:06:55 -0700188 RtcEventLog* event_log,
perkjec81bcd2016-05-11 06:01:13 -0700189 std::unique_ptr<PacketRouter> packet_router,
190 std::unique_ptr<PacedSender> pacer)
191 : clock_(clock),
192 observer_(observer),
193 packet_router_(std::move(packet_router)),
194 pacer_(std::move(pacer)),
Erik Språng6b8d3552015-09-24 15:06:57 +0200195 remote_bitrate_estimator_(
Stefan Holmer58c664c2016-02-08 14:31:30 +0100196 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
Stefan Holmere5904162015-03-26 11:11:06 +0100197 // Constructed last as this object calls the provided callback on
198 // construction.
ivoc14d5dbe2016-07-04 07:06:55 -0700199 bitrate_controller_(
200 BitrateController::CreateBitrateController(clock_, event_log)),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700201 probe_controller_(new ProbeController(pacer_.get(), clock_)),
sprangcd349d92016-07-13 09:11:28 -0700202 retransmission_rate_limiter_(
sprangddba75c2016-08-04 09:52:21 -0700203 new RateLimiter(clock, kRetransmitWindowSizeMs)),
perkjec81bcd2016-05-11 06:01:13 -0700204 remote_estimator_proxy_(clock_, packet_router_.get()),
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700205 transport_feedback_adapter_(clock_),
perkjec81bcd2016-05-11 06:01:13 -0700206 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps),
philipeleb680ea2016-08-17 11:11:59 +0200207 max_bitrate_bps_(0),
perkjfea93092016-05-14 00:58:48 -0700208 last_reported_bitrate_bps_(0),
209 last_reported_fraction_loss_(0),
210 last_reported_rtt_(0),
211 network_state_(kNetworkUp) {
perkjec81bcd2016-05-11 06:01:13 -0700212 Init();
213}
214
215CongestionController::~CongestionController() {}
216
217void CongestionController::Init() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100218 transport_feedback_adapter_.SetBitrateEstimator(
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700219 new DelayBasedBwe(bitrate_controller_.get(), clock_));
Stefan Holmer789ba922016-02-17 15:52:17 +0100220 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
221 min_bitrate_bps_);
perkje30c2722016-05-09 04:57:11 -0700222}
223
mflodman0c478b32015-10-21 15:52:16 +0200224void CongestionController::SetBweBitrates(int min_bitrate_bps,
225 int start_bitrate_bps,
226 int max_bitrate_bps) {
honghaiz059e1832016-06-24 11:03:55 -0700227 ClampBitrates(&start_bitrate_bps, &min_bitrate_bps, &max_bitrate_bps);
philipelc6957c72016-04-28 15:52:49 +0200228 bitrate_controller_->SetBitrates(start_bitrate_bps,
229 min_bitrate_bps,
230 max_bitrate_bps);
231
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700232 probe_controller_->SetBitrates(min_bitrate_bps, start_bitrate_bps,
233 max_bitrate_bps);
philipeleb680ea2016-08-17 11:11:59 +0200234 max_bitrate_bps_ = max_bitrate_bps;
235
Stefan Holmer789ba922016-02-17 15:52:17 +0100236 if (remote_bitrate_estimator_)
stefan4fbd1452015-09-28 03:57:14 -0700237 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
stefan4fbd1452015-09-28 03:57:14 -0700238 min_bitrate_bps_ = min_bitrate_bps;
Stefan Holmer789ba922016-02-17 15:52:17 +0100239 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
240 min_bitrate_bps_);
perkjec81bcd2016-05-11 06:01:13 -0700241 MaybeTriggerOnNetworkChanged();
stefan4fbd1452015-09-28 03:57:14 -0700242}
243
honghaiz059e1832016-06-24 11:03:55 -0700244void CongestionController::ResetBweAndBitrates(int bitrate_bps,
245 int min_bitrate_bps,
246 int max_bitrate_bps) {
247 ClampBitrates(&bitrate_bps, &min_bitrate_bps, &max_bitrate_bps);
248 // TODO(honghaiz): Recreate this object once the bitrate controller is
249 // no longer exposed outside CongestionController.
250 bitrate_controller_->ResetBitrates(bitrate_bps, min_bitrate_bps,
251 max_bitrate_bps);
252 min_bitrate_bps_ = min_bitrate_bps;
philipeleb680ea2016-08-17 11:11:59 +0200253 max_bitrate_bps_ = max_bitrate_bps;
honghaiz059e1832016-06-24 11:03:55 -0700254 // TODO(honghaiz): Recreate this object once the remote bitrate estimator is
255 // no longer exposed outside CongestionController.
256 if (remote_bitrate_estimator_)
257 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
258
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700259 RemoteBitrateEstimator* rbe =
260 new DelayBasedBwe(bitrate_controller_.get(), clock_);
honghaiz059e1832016-06-24 11:03:55 -0700261 transport_feedback_adapter_.SetBitrateEstimator(rbe);
262 rbe->SetMinBitrate(min_bitrate_bps);
263 // TODO(holmer): Trigger a new probe once mid-call probing is implemented.
264 MaybeTriggerOnNetworkChanged();
265}
266
mflodman0c478b32015-10-21 15:52:16 +0200267BitrateController* CongestionController::GetBitrateController() const {
bjornv@webrtc.orgcb89c6f2012-06-05 12:25:35 +0000268 return bitrate_controller_.get();
stefan@webrtc.orgf7288142012-06-05 10:44:00 +0000269}
270
mflodman0c478b32015-10-21 15:52:16 +0200271RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator(
Stefan Holmer789ba922016-02-17 15:52:17 +0100272 bool send_side_bwe) {
273 if (send_side_bwe) {
274 return &remote_estimator_proxy_;
275 } else {
mflodmana20de202015-10-18 22:08:19 -0700276 return remote_bitrate_estimator_.get();
Stefan Holmer789ba922016-02-17 15:52:17 +0100277 }
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000278}
279
mflodman0c478b32015-10-21 15:52:16 +0200280TransportFeedbackObserver*
281CongestionController::GetTransportFeedbackObserver() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100282 return &transport_feedback_adapter_;
mflodman949c2f02015-10-16 02:31:11 -0700283}
284
sprangcd349d92016-07-13 09:11:28 -0700285RateLimiter* CongestionController::GetRetransmissionRateLimiter() {
286 return retransmission_rate_limiter_.get();
287}
288
perkj71ee44c2016-06-15 00:47:53 -0700289void CongestionController::SetAllocatedSendBitrateLimits(
290 int min_send_bitrate_bps,
291 int max_padding_bitrate_bps) {
292 pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps);
mflodman0e7e2592015-11-12 21:02:42 -0800293}
294
mflodman0c478b32015-10-21 15:52:16 +0200295int64_t CongestionController::GetPacerQueuingDelayMs() const {
asapersson14f12502016-09-07 23:14:50 -0700296 return IsNetworkDown() ? 0 : pacer_->QueueInMs();
Stefan Holmere5904162015-03-26 11:11:06 +0100297}
298
mflodman0c478b32015-10-21 15:52:16 +0200299void CongestionController::SignalNetworkState(NetworkState state) {
perkj9b522f82016-07-07 00:36:28 -0700300 LOG(LS_INFO) << "SignalNetworkState "
301 << (state == kNetworkUp ? "Up" : "Down");
stefan457a61d2015-10-14 03:12:59 -0700302 if (state == kNetworkUp) {
303 pacer_->Resume();
304 } else {
305 pacer_->Pause();
306 }
perkjfea93092016-05-14 00:58:48 -0700307 {
308 rtc::CritScope cs(&critsect_);
309 network_state_ = state;
310 }
311 MaybeTriggerOnNetworkChanged();
stefan457a61d2015-10-14 03:12:59 -0700312}
313
mflodman0c478b32015-10-21 15:52:16 +0200314void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100315 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
Stefan Holmer13181032016-07-29 14:48:54 +0200316 sent_packet.send_time_ms);
stefanc1aeaf02015-10-15 07:26:07 -0700317}
Stefan Holmer789ba922016-02-17 15:52:17 +0100318
319void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
320 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
321 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
322}
323
324int64_t CongestionController::TimeUntilNextProcess() {
325 return std::min(bitrate_controller_->TimeUntilNextProcess(),
326 remote_bitrate_estimator_->TimeUntilNextProcess());
327}
328
pbosa26ac922016-02-25 04:50:01 -0800329void CongestionController::Process() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100330 bitrate_controller_->Process();
331 remote_bitrate_estimator_->Process();
perkjec81bcd2016-05-11 06:01:13 -0700332 MaybeTriggerOnNetworkChanged();
333}
334
335void CongestionController::MaybeTriggerOnNetworkChanged() {
336 // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a
337 // BitrateObserver is used. Remove this check once the ctor is removed.
338 if (!observer_)
339 return;
340
341 uint32_t bitrate_bps;
342 uint8_t fraction_loss;
343 int64_t rtt;
perkjfea93092016-05-14 00:58:48 -0700344 bool estimate_changed = bitrate_controller_->GetNetworkParameters(
perkjec81bcd2016-05-11 06:01:13 -0700345 &bitrate_bps, &fraction_loss, &rtt);
sprangcd349d92016-07-13 09:11:28 -0700346 if (estimate_changed) {
perkjec81bcd2016-05-11 06:01:13 -0700347 pacer_->SetEstimatedBitrate(bitrate_bps);
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700348 probe_controller_->SetEstimatedBitrate(bitrate_bps);
sprangcd349d92016-07-13 09:11:28 -0700349 retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
350 }
perkjfea93092016-05-14 00:58:48 -0700351
352 bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps;
353
354 if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) {
perkjec81bcd2016-05-11 06:01:13 -0700355 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
356 }
357}
358
perkjfea93092016-05-14 00:58:48 -0700359bool CongestionController::HasNetworkParametersToReportChanged(
360 uint32_t bitrate_bps,
361 uint8_t fraction_loss,
362 int64_t rtt) {
perkjec81bcd2016-05-11 06:01:13 -0700363 rtc::CritScope cs(&critsect_);
perkjfea93092016-05-14 00:58:48 -0700364 bool changed =
365 last_reported_bitrate_bps_ != bitrate_bps ||
366 (bitrate_bps > 0 && (last_reported_fraction_loss_ != fraction_loss ||
367 last_reported_rtt_ != rtt));
perkj9b522f82016-07-07 00:36:28 -0700368 if (changed && (last_reported_bitrate_bps_ == 0 || bitrate_bps == 0)) {
369 LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " << bitrate_bps
370 << " bps.";
371 }
perkjfea93092016-05-14 00:58:48 -0700372 last_reported_bitrate_bps_ = bitrate_bps;
373 last_reported_fraction_loss_ = fraction_loss;
374 last_reported_rtt_ = rtt;
375 return changed;
376}
377
378bool CongestionController::IsSendQueueFull() const {
379 return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs;
380}
381
382bool CongestionController::IsNetworkDown() const {
383 rtc::CritScope cs(&critsect_);
384 return network_state_ == kNetworkDown;
Stefan Holmer789ba922016-02-17 15:52:17 +0100385}
386
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000387} // namespace webrtc