mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 1 | /* |
| 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 Holmer | 80e1207 | 2016-02-23 13:30:42 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 12 | |
Stefan Holmer | 62a5ccd | 2016-02-16 17:07:21 +0100 | [diff] [blame] | 13 | #include <algorithm> |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 14 | #include <memory> |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
stefan@webrtc.org | a50e6f0 | 2015-03-09 10:06:40 +0000 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 18 | #include "webrtc/base/constructormagic.h" |
Peter Boström | 7c704b8 | 2015-12-04 16:13:05 +0100 | [diff] [blame] | 19 | #include "webrtc/base/logging.h" |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 20 | #include "webrtc/base/rate_limiter.h" |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 21 | #include "webrtc/base/socket.h" |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 22 | #include "webrtc/base/thread_annotations.h" |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 23 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame] | 24 | #include "webrtc/modules/congestion_controller/delay_based_bwe.h" |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 25 | #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
Erik Språng | 468e62a | 2015-07-06 10:50:47 +0200 | [diff] [blame] | 26 | #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 Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 28 | #include "webrtc/modules/utility/include/process_thread.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 29 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 30 | #include "webrtc/video/payload_router.h" |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 35 | static const uint32_t kTimeOffsetSwitchThreshold = 30; |
sprang | ddba75c | 2016-08-04 09:52:21 -0700 | [diff] [blame] | 36 | static const int64_t kRetransmitWindowSizeMs = 500; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 37 | |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 38 | // Makes sure that the bitrate and the min, max values are in valid range. |
| 39 | static 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.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 54 | class WrappingBitrateEstimator : public RemoteBitrateEstimator { |
| 55 | public: |
pbos | ef35f06 | 2015-07-27 08:37:06 -0700 | [diff] [blame] | 56 | WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 57 | : observer_(observer), |
| 58 | clock_(clock), |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 59 | crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 60 | rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)), |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 61 | using_absolute_send_time_(false), |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 62 | packets_since_absolute_send_time_(0), |
| 63 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {} |
andresp@webrtc.org | 1295dc6 | 2014-07-02 13:23:19 +0000 | [diff] [blame] | 64 | |
| 65 | virtual ~WrappingBitrateEstimator() {} |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 66 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 67 | void IncomingPacket(int64_t arrival_time_ms, |
| 68 | size_t payload_size, |
pbos | 2169d8b | 2016-06-20 11:53:02 -0700 | [diff] [blame] | 69 | const RTPHeader& header) override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 70 | CriticalSectionScoped cs(crit_sect_.get()); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 71 | PickEstimatorFromHeader(header); |
pbos | 2169d8b | 2016-06-20 11:53:02 -0700 | [diff] [blame] | 72 | rbe_->IncomingPacket(arrival_time_ms, payload_size, header); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 73 | } |
| 74 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 75 | void Process() override { |
andresp@webrtc.org | 1295dc6 | 2014-07-02 13:23:19 +0000 | [diff] [blame] | 76 | CriticalSectionScoped cs(crit_sect_.get()); |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 77 | rbe_->Process(); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 78 | } |
| 79 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 80 | int64_t TimeUntilNextProcess() override { |
andresp@webrtc.org | 1295dc6 | 2014-07-02 13:23:19 +0000 | [diff] [blame] | 81 | CriticalSectionScoped cs(crit_sect_.get()); |
| 82 | return rbe_->TimeUntilNextProcess(); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 83 | } |
| 84 | |
stefan | 2328a94 | 2015-08-07 04:27:51 -0700 | [diff] [blame] | 85 | void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 86 | CriticalSectionScoped cs(crit_sect_.get()); |
stefan | 2328a94 | 2015-08-07 04:27:51 -0700 | [diff] [blame] | 87 | rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 88 | } |
| 89 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 90 | void RemoveStream(unsigned int ssrc) override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 91 | CriticalSectionScoped cs(crit_sect_.get()); |
| 92 | rbe_->RemoveStream(ssrc); |
| 93 | } |
| 94 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 95 | bool LatestEstimate(std::vector<unsigned int>* ssrcs, |
| 96 | unsigned int* bitrate_bps) const override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 97 | CriticalSectionScoped cs(crit_sect_.get()); |
| 98 | return rbe_->LatestEstimate(ssrcs, bitrate_bps); |
| 99 | } |
| 100 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 101 | void SetMinBitrate(int min_bitrate_bps) override { |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 102 | CriticalSectionScoped cs(crit_sect_.get()); |
| 103 | rbe_->SetMinBitrate(min_bitrate_bps); |
| 104 | min_bitrate_bps_ = min_bitrate_bps; |
| 105 | } |
| 106 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 107 | private: |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 108 | void PickEstimatorFromHeader(const RTPHeader& header) |
| 109 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) { |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 110 | if (header.extension.hasAbsoluteSendTime) { |
| 111 | // If we see AST in header, switch RBE strategy immediately. |
| 112 | if (!using_absolute_send_time_) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 113 | LOG(LS_INFO) << |
| 114 | "WrappingBitrateEstimator: Switching to absolute send time RBE."; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 115 | using_absolute_send_time_ = true; |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 116 | PickEstimator(); |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 117 | } |
| 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.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 124 | LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission " |
| 125 | << "time offset RBE."; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 126 | using_absolute_send_time_ = false; |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 127 | PickEstimator(); |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 133 | // Instantiate RBE for Time Offset or Absolute Send Time extensions. |
| 134 | void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) { |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 135 | if (using_absolute_send_time_) { |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 136 | rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_)); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 137 | } else { |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 138 | rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_)); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 139 | } |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 140 | rbe_->SetMinBitrate(min_bitrate_bps_); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 141 | } |
| 142 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 143 | RemoteBitrateObserver* observer_; |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 144 | Clock* const clock_; |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 145 | std::unique_ptr<CriticalSectionWrapper> crit_sect_; |
| 146 | std::unique_ptr<RemoteBitrateEstimator> rbe_; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 147 | bool using_absolute_send_time_; |
| 148 | uint32_t packets_since_absolute_send_time_; |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 149 | int min_bitrate_bps_; |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 150 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 151 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 152 | }; |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 153 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 154 | } // namespace |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 155 | |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 156 | CongestionController::CongestionController( |
| 157 | Clock* clock, |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 158 | Observer* observer, |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 159 | RemoteBitrateObserver* remote_bitrate_observer, |
| 160 | RtcEventLog* event_log) |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 161 | : clock_(clock), |
| 162 | observer_(observer), |
| 163 | packet_router_(new PacketRouter()), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 164 | pacer_(new PacedSender(clock_, packet_router_.get())), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 165 | remote_bitrate_estimator_( |
| 166 | new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 167 | bitrate_controller_( |
| 168 | BitrateController::CreateBitrateController(clock_, event_log)), |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 169 | retransmission_rate_limiter_( |
sprang | ddba75c | 2016-08-04 09:52:21 -0700 | [diff] [blame] | 170 | new RateLimiter(clock, kRetransmitWindowSizeMs)), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 171 | remote_estimator_proxy_(clock_, packet_router_.get()), |
| 172 | transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
| 173 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 174 | last_reported_bitrate_bps_(0), |
| 175 | last_reported_fraction_loss_(0), |
| 176 | last_reported_rtt_(0), |
| 177 | network_state_(kNetworkUp) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 178 | Init(); |
| 179 | } |
| 180 | |
| 181 | CongestionController::CongestionController( |
| 182 | Clock* clock, |
| 183 | Observer* observer, |
| 184 | RemoteBitrateObserver* remote_bitrate_observer, |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 185 | RtcEventLog* event_log, |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 186 | std::unique_ptr<PacketRouter> packet_router, |
| 187 | std::unique_ptr<PacedSender> pacer) |
| 188 | : clock_(clock), |
| 189 | observer_(observer), |
| 190 | packet_router_(std::move(packet_router)), |
| 191 | pacer_(std::move(pacer)), |
Erik Språng | 6b8d355 | 2015-09-24 15:06:57 +0200 | [diff] [blame] | 192 | remote_bitrate_estimator_( |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 193 | new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 194 | // Constructed last as this object calls the provided callback on |
| 195 | // construction. |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 196 | bitrate_controller_( |
| 197 | BitrateController::CreateBitrateController(clock_, event_log)), |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 198 | retransmission_rate_limiter_( |
sprang | ddba75c | 2016-08-04 09:52:21 -0700 | [diff] [blame] | 199 | new RateLimiter(clock, kRetransmitWindowSizeMs)), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 200 | remote_estimator_proxy_(clock_, packet_router_.get()), |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 201 | transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 202 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 203 | last_reported_bitrate_bps_(0), |
| 204 | last_reported_fraction_loss_(0), |
| 205 | last_reported_rtt_(0), |
| 206 | network_state_(kNetworkUp) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 207 | Init(); |
| 208 | } |
| 209 | |
| 210 | CongestionController::~CongestionController() {} |
| 211 | |
| 212 | void CongestionController::Init() { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 213 | transport_feedback_adapter_.SetBitrateEstimator( |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 214 | new DelayBasedBwe(&transport_feedback_adapter_, clock_)); |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 215 | transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
| 216 | min_bitrate_bps_); |
perkj | e30c272 | 2016-05-09 04:57:11 -0700 | [diff] [blame] | 217 | } |
| 218 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 219 | void CongestionController::SetBweBitrates(int min_bitrate_bps, |
| 220 | int start_bitrate_bps, |
| 221 | int max_bitrate_bps) { |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 222 | ClampBitrates(&start_bitrate_bps, &min_bitrate_bps, &max_bitrate_bps); |
philipel | c6957c7 | 2016-04-28 15:52:49 +0200 | [diff] [blame] | 223 | bitrate_controller_->SetBitrates(start_bitrate_bps, |
| 224 | min_bitrate_bps, |
| 225 | max_bitrate_bps); |
| 226 | |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 227 | if (remote_bitrate_estimator_) |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 228 | remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 229 | min_bitrate_bps_ = min_bitrate_bps; |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 230 | transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
| 231 | min_bitrate_bps_); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 232 | MaybeTriggerOnNetworkChanged(); |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 233 | } |
| 234 | |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 235 | void 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; |
| 244 | // TODO(honghaiz): Recreate this object once the remote bitrate estimator is |
| 245 | // no longer exposed outside CongestionController. |
| 246 | if (remote_bitrate_estimator_) |
| 247 | remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); |
| 248 | |
Irfan Sheriff | b578d6c | 2016-08-01 12:16:43 -0700 | [diff] [blame] | 249 | RemoteBitrateEstimator* rbe = new DelayBasedBwe( |
stefan | 5e12d36 | 2016-07-11 01:44:02 -0700 | [diff] [blame] | 250 | &transport_feedback_adapter_, clock_); |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 251 | transport_feedback_adapter_.SetBitrateEstimator(rbe); |
| 252 | rbe->SetMinBitrate(min_bitrate_bps); |
| 253 | // TODO(holmer): Trigger a new probe once mid-call probing is implemented. |
| 254 | MaybeTriggerOnNetworkChanged(); |
| 255 | } |
| 256 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 257 | BitrateController* CongestionController::GetBitrateController() const { |
bjornv@webrtc.org | cb89c6f | 2012-06-05 12:25:35 +0000 | [diff] [blame] | 258 | return bitrate_controller_.get(); |
stefan@webrtc.org | f728814 | 2012-06-05 10:44:00 +0000 | [diff] [blame] | 259 | } |
| 260 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 261 | RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 262 | bool send_side_bwe) { |
| 263 | if (send_side_bwe) { |
| 264 | return &remote_estimator_proxy_; |
| 265 | } else { |
mflodman | a20de20 | 2015-10-18 22:08:19 -0700 | [diff] [blame] | 266 | return remote_bitrate_estimator_.get(); |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 267 | } |
stefan@webrtc.org | 9354cc9 | 2012-06-07 08:10:14 +0000 | [diff] [blame] | 268 | } |
| 269 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 270 | TransportFeedbackObserver* |
| 271 | CongestionController::GetTransportFeedbackObserver() { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 272 | return &transport_feedback_adapter_; |
mflodman | 949c2f0 | 2015-10-16 02:31:11 -0700 | [diff] [blame] | 273 | } |
| 274 | |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 275 | RateLimiter* CongestionController::GetRetransmissionRateLimiter() { |
| 276 | return retransmission_rate_limiter_.get(); |
| 277 | } |
| 278 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 279 | void CongestionController::SetAllocatedSendBitrateLimits( |
| 280 | int min_send_bitrate_bps, |
| 281 | int max_padding_bitrate_bps) { |
| 282 | pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps); |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 283 | } |
| 284 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 285 | int64_t CongestionController::GetPacerQueuingDelayMs() const { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 286 | return pacer_->QueueInMs(); |
| 287 | } |
| 288 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 289 | void CongestionController::SignalNetworkState(NetworkState state) { |
perkj | 9b522f8 | 2016-07-07 00:36:28 -0700 | [diff] [blame] | 290 | LOG(LS_INFO) << "SignalNetworkState " |
| 291 | << (state == kNetworkUp ? "Up" : "Down"); |
stefan | 457a61d | 2015-10-14 03:12:59 -0700 | [diff] [blame] | 292 | if (state == kNetworkUp) { |
| 293 | pacer_->Resume(); |
| 294 | } else { |
| 295 | pacer_->Pause(); |
| 296 | } |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 297 | { |
| 298 | rtc::CritScope cs(&critsect_); |
| 299 | network_state_ = state; |
| 300 | } |
| 301 | MaybeTriggerOnNetworkChanged(); |
stefan | 457a61d | 2015-10-14 03:12:59 -0700 | [diff] [blame] | 302 | } |
| 303 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 304 | void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 305 | transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id, |
Stefan Holmer | 1318103 | 2016-07-29 14:48:54 +0200 | [diff] [blame] | 306 | sent_packet.send_time_ms); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 307 | } |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 308 | |
| 309 | void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
| 310 | remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 311 | transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 312 | } |
| 313 | |
| 314 | int64_t CongestionController::TimeUntilNextProcess() { |
| 315 | return std::min(bitrate_controller_->TimeUntilNextProcess(), |
| 316 | remote_bitrate_estimator_->TimeUntilNextProcess()); |
| 317 | } |
| 318 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 319 | void CongestionController::Process() { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 320 | bitrate_controller_->Process(); |
| 321 | remote_bitrate_estimator_->Process(); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 322 | MaybeTriggerOnNetworkChanged(); |
| 323 | } |
| 324 | |
| 325 | void CongestionController::MaybeTriggerOnNetworkChanged() { |
| 326 | // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a |
| 327 | // BitrateObserver is used. Remove this check once the ctor is removed. |
| 328 | if (!observer_) |
| 329 | return; |
| 330 | |
| 331 | uint32_t bitrate_bps; |
| 332 | uint8_t fraction_loss; |
| 333 | int64_t rtt; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 334 | bool estimate_changed = bitrate_controller_->GetNetworkParameters( |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 335 | &bitrate_bps, &fraction_loss, &rtt); |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 336 | if (estimate_changed) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 337 | pacer_->SetEstimatedBitrate(bitrate_bps); |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 338 | retransmission_rate_limiter_->SetMaxRate(bitrate_bps); |
| 339 | } |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 340 | |
| 341 | bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps; |
| 342 | |
| 343 | if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 344 | observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt); |
| 345 | } |
| 346 | } |
| 347 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 348 | bool CongestionController::HasNetworkParametersToReportChanged( |
| 349 | uint32_t bitrate_bps, |
| 350 | uint8_t fraction_loss, |
| 351 | int64_t rtt) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 352 | rtc::CritScope cs(&critsect_); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 353 | bool changed = |
| 354 | last_reported_bitrate_bps_ != bitrate_bps || |
| 355 | (bitrate_bps > 0 && (last_reported_fraction_loss_ != fraction_loss || |
| 356 | last_reported_rtt_ != rtt)); |
perkj | 9b522f8 | 2016-07-07 00:36:28 -0700 | [diff] [blame] | 357 | if (changed && (last_reported_bitrate_bps_ == 0 || bitrate_bps == 0)) { |
| 358 | LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " << bitrate_bps |
| 359 | << " bps."; |
| 360 | } |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 361 | last_reported_bitrate_bps_ = bitrate_bps; |
| 362 | last_reported_fraction_loss_ = fraction_loss; |
| 363 | last_reported_rtt_ = rtt; |
| 364 | return changed; |
| 365 | } |
| 366 | |
| 367 | bool CongestionController::IsSendQueueFull() const { |
| 368 | return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs; |
| 369 | } |
| 370 | |
| 371 | bool CongestionController::IsNetworkDown() const { |
| 372 | rtc::CritScope cs(&critsect_); |
| 373 | return network_state_ == kNetworkDown; |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 374 | } |
| 375 | |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 376 | } // namespace webrtc |