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" |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 20 | #include "webrtc/base/socket.h" |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 21 | #include "webrtc/base/thread_annotations.h" |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 22 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame^] | 23 | #include "webrtc/modules/congestion_controller/delay_based_bwe.h" |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 24 | #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
Erik Språng | 468e62a | 2015-07-06 10:50:47 +0200 | [diff] [blame] | 25 | #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h" |
| 26 | #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 27 | #include "webrtc/modules/utility/include/process_thread.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 28 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 29 | #include "webrtc/video/payload_router.h" |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 34 | static const uint32_t kTimeOffsetSwitchThreshold = 30; |
| 35 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 36 | class WrappingBitrateEstimator : public RemoteBitrateEstimator { |
| 37 | public: |
pbos | ef35f06 | 2015-07-27 08:37:06 -0700 | [diff] [blame] | 38 | WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock) |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 39 | : observer_(observer), |
| 40 | clock_(clock), |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 41 | crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 42 | rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)), |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 43 | using_absolute_send_time_(false), |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 44 | packets_since_absolute_send_time_(0), |
| 45 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {} |
andresp@webrtc.org | 1295dc6 | 2014-07-02 13:23:19 +0000 | [diff] [blame] | 46 | |
| 47 | virtual ~WrappingBitrateEstimator() {} |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 48 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 49 | void IncomingPacket(int64_t arrival_time_ms, |
| 50 | size_t payload_size, |
Stefan Holmer | ff4ea93 | 2015-06-18 16:01:33 +0200 | [diff] [blame] | 51 | const RTPHeader& header, |
| 52 | bool was_paced) override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 53 | CriticalSectionScoped cs(crit_sect_.get()); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 54 | PickEstimatorFromHeader(header); |
Stefan Holmer | ff4ea93 | 2015-06-18 16:01:33 +0200 | [diff] [blame] | 55 | rbe_->IncomingPacket(arrival_time_ms, payload_size, header, was_paced); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 56 | } |
| 57 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 58 | void Process() override { |
andresp@webrtc.org | 1295dc6 | 2014-07-02 13:23:19 +0000 | [diff] [blame] | 59 | CriticalSectionScoped cs(crit_sect_.get()); |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 60 | rbe_->Process(); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 61 | } |
| 62 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 63 | int64_t TimeUntilNextProcess() override { |
andresp@webrtc.org | 1295dc6 | 2014-07-02 13:23:19 +0000 | [diff] [blame] | 64 | CriticalSectionScoped cs(crit_sect_.get()); |
| 65 | return rbe_->TimeUntilNextProcess(); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 66 | } |
| 67 | |
stefan | 2328a94 | 2015-08-07 04:27:51 -0700 | [diff] [blame] | 68 | 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] | 69 | CriticalSectionScoped cs(crit_sect_.get()); |
stefan | 2328a94 | 2015-08-07 04:27:51 -0700 | [diff] [blame] | 70 | rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 71 | } |
| 72 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 73 | void RemoveStream(unsigned int ssrc) override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 74 | CriticalSectionScoped cs(crit_sect_.get()); |
| 75 | rbe_->RemoveStream(ssrc); |
| 76 | } |
| 77 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 78 | bool LatestEstimate(std::vector<unsigned int>* ssrcs, |
| 79 | unsigned int* bitrate_bps) const override { |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 80 | CriticalSectionScoped cs(crit_sect_.get()); |
| 81 | return rbe_->LatestEstimate(ssrcs, bitrate_bps); |
| 82 | } |
| 83 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 84 | void SetMinBitrate(int min_bitrate_bps) override { |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 85 | CriticalSectionScoped cs(crit_sect_.get()); |
| 86 | rbe_->SetMinBitrate(min_bitrate_bps); |
| 87 | min_bitrate_bps_ = min_bitrate_bps; |
| 88 | } |
| 89 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 90 | private: |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 91 | void PickEstimatorFromHeader(const RTPHeader& header) |
| 92 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) { |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 93 | if (header.extension.hasAbsoluteSendTime) { |
| 94 | // If we see AST in header, switch RBE strategy immediately. |
| 95 | if (!using_absolute_send_time_) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 96 | LOG(LS_INFO) << |
| 97 | "WrappingBitrateEstimator: Switching to absolute send time RBE."; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 98 | using_absolute_send_time_ = true; |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 99 | PickEstimator(); |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 100 | } |
| 101 | packets_since_absolute_send_time_ = 0; |
| 102 | } else { |
| 103 | // When we don't see AST, wait for a few packets before going back to TOF. |
| 104 | if (using_absolute_send_time_) { |
| 105 | ++packets_since_absolute_send_time_; |
| 106 | if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 107 | LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission " |
| 108 | << "time offset RBE."; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 109 | using_absolute_send_time_ = false; |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 110 | PickEstimator(); |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 116 | // Instantiate RBE for Time Offset or Absolute Send Time extensions. |
| 117 | void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) { |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 118 | if (using_absolute_send_time_) { |
stefan | 1112b2b | 2016-04-14 08:08:15 -0700 | [diff] [blame] | 119 | rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_)); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 120 | } else { |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 121 | rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_)); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 122 | } |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 123 | rbe_->SetMinBitrate(min_bitrate_bps_); |
stefan@webrtc.org | a16147c | 2014-03-25 10:37:31 +0000 | [diff] [blame] | 124 | } |
| 125 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 126 | RemoteBitrateObserver* observer_; |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 127 | Clock* const clock_; |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 128 | std::unique_ptr<CriticalSectionWrapper> crit_sect_; |
| 129 | std::unique_ptr<RemoteBitrateEstimator> rbe_; |
pbos@webrtc.org | 5ab7567 | 2013-12-16 12:24:44 +0000 | [diff] [blame] | 130 | bool using_absolute_send_time_; |
| 131 | uint32_t packets_since_absolute_send_time_; |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 132 | int min_bitrate_bps_; |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 133 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 134 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator); |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 135 | }; |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 136 | |
solenberg@webrtc.org | a6db54d | 2013-05-27 16:02:56 +0000 | [diff] [blame] | 137 | } // namespace |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 138 | |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 139 | CongestionController::CongestionController( |
| 140 | Clock* clock, |
perkj | 825eb58 | 2016-05-04 01:08:05 -0700 | [diff] [blame] | 141 | BitrateObserver* bitrate_observer, |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 142 | RemoteBitrateObserver* remote_bitrate_observer) |
| 143 | : clock_(clock), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 144 | observer_(nullptr), |
| 145 | packet_router_(new PacketRouter()), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 146 | pacer_(new PacedSender(clock_, packet_router_.get())), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 147 | remote_bitrate_estimator_( |
| 148 | new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
| 149 | bitrate_controller_( |
| 150 | BitrateController::CreateBitrateController(clock_, bitrate_observer)), |
| 151 | remote_estimator_proxy_(clock_, packet_router_.get()), |
| 152 | transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
| 153 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 154 | last_reported_bitrate_bps_(0), |
| 155 | last_reported_fraction_loss_(0), |
| 156 | last_reported_rtt_(0), |
| 157 | network_state_(kNetworkUp) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 158 | Init(); |
| 159 | } |
| 160 | |
| 161 | CongestionController::CongestionController( |
| 162 | Clock* clock, |
| 163 | Observer* observer, |
| 164 | RemoteBitrateObserver* remote_bitrate_observer) |
| 165 | : clock_(clock), |
| 166 | observer_(observer), |
| 167 | packet_router_(new PacketRouter()), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 168 | pacer_(new PacedSender(clock_, packet_router_.get())), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 169 | remote_bitrate_estimator_( |
| 170 | new WrappingBitrateEstimator(remote_bitrate_observer, clock_)), |
| 171 | bitrate_controller_(BitrateController::CreateBitrateController(clock_)), |
| 172 | remote_estimator_proxy_(clock_, packet_router_.get()), |
| 173 | transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
| 174 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 175 | last_reported_bitrate_bps_(0), |
| 176 | last_reported_fraction_loss_(0), |
| 177 | last_reported_rtt_(0), |
| 178 | network_state_(kNetworkUp) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 179 | Init(); |
| 180 | } |
| 181 | |
| 182 | CongestionController::CongestionController( |
| 183 | Clock* clock, |
| 184 | Observer* observer, |
| 185 | RemoteBitrateObserver* remote_bitrate_observer, |
| 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. |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 196 | bitrate_controller_(BitrateController::CreateBitrateController(clock_)), |
| 197 | remote_estimator_proxy_(clock_, packet_router_.get()), |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 198 | transport_feedback_adapter_(bitrate_controller_.get(), clock_), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 199 | min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps), |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 200 | last_reported_bitrate_bps_(0), |
| 201 | last_reported_fraction_loss_(0), |
| 202 | last_reported_rtt_(0), |
| 203 | network_state_(kNetworkUp) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 204 | Init(); |
| 205 | } |
| 206 | |
| 207 | CongestionController::~CongestionController() {} |
| 208 | |
| 209 | void CongestionController::Init() { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 210 | transport_feedback_adapter_.SetBitrateEstimator( |
philipel | 863a826 | 2016-06-17 09:21:34 -0700 | [diff] [blame^] | 211 | new DelayBasedBwe(&transport_feedback_adapter_)); |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 212 | transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
| 213 | min_bitrate_bps_); |
perkj | e30c272 | 2016-05-09 04:57:11 -0700 | [diff] [blame] | 214 | } |
| 215 | |
guidou | 72d41aa | 2016-06-02 23:24:51 -0700 | [diff] [blame] | 216 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 217 | void CongestionController::SetBweBitrates(int min_bitrate_bps, |
| 218 | int start_bitrate_bps, |
| 219 | int max_bitrate_bps) { |
guidou | 72d41aa | 2016-06-02 23:24:51 -0700 | [diff] [blame] | 220 | // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, |
| 221 | // and that we don't try to set the min bitrate to 0 from any applications. |
| 222 | // The congestion controller should allow a min bitrate of 0. |
| 223 | const int kMinBitrateBps = 10000; |
| 224 | if (min_bitrate_bps < kMinBitrateBps) |
| 225 | min_bitrate_bps = kMinBitrateBps; |
| 226 | if (max_bitrate_bps > 0) |
| 227 | max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); |
| 228 | if (start_bitrate_bps > 0) |
| 229 | start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps); |
| 230 | |
philipel | c6957c7 | 2016-04-28 15:52:49 +0200 | [diff] [blame] | 231 | bitrate_controller_->SetBitrates(start_bitrate_bps, |
| 232 | min_bitrate_bps, |
| 233 | max_bitrate_bps); |
| 234 | |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 235 | if (remote_bitrate_estimator_) |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 236 | remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps); |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 237 | min_bitrate_bps_ = min_bitrate_bps; |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 238 | transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate( |
| 239 | min_bitrate_bps_); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 240 | MaybeTriggerOnNetworkChanged(); |
stefan | 4fbd145 | 2015-09-28 03:57:14 -0700 | [diff] [blame] | 241 | } |
| 242 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 243 | BitrateController* CongestionController::GetBitrateController() const { |
bjornv@webrtc.org | cb89c6f | 2012-06-05 12:25:35 +0000 | [diff] [blame] | 244 | return bitrate_controller_.get(); |
stefan@webrtc.org | f728814 | 2012-06-05 10:44:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 247 | RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator( |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 248 | bool send_side_bwe) { |
| 249 | if (send_side_bwe) { |
| 250 | return &remote_estimator_proxy_; |
| 251 | } else { |
mflodman | a20de20 | 2015-10-18 22:08:19 -0700 | [diff] [blame] | 252 | return remote_bitrate_estimator_.get(); |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 253 | } |
stefan@webrtc.org | 9354cc9 | 2012-06-07 08:10:14 +0000 | [diff] [blame] | 254 | } |
| 255 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 256 | TransportFeedbackObserver* |
| 257 | CongestionController::GetTransportFeedbackObserver() { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 258 | return &transport_feedback_adapter_; |
mflodman | 949c2f0 | 2015-10-16 02:31:11 -0700 | [diff] [blame] | 259 | } |
| 260 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 261 | void CongestionController::SetAllocatedSendBitrateLimits( |
| 262 | int min_send_bitrate_bps, |
| 263 | int max_padding_bitrate_bps) { |
| 264 | pacer_->SetSendBitrateLimits(min_send_bitrate_bps, max_padding_bitrate_bps); |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 265 | } |
| 266 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 267 | int64_t CongestionController::GetPacerQueuingDelayMs() const { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 268 | return pacer_->QueueInMs(); |
| 269 | } |
| 270 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 271 | void CongestionController::SignalNetworkState(NetworkState state) { |
stefan | 457a61d | 2015-10-14 03:12:59 -0700 | [diff] [blame] | 272 | if (state == kNetworkUp) { |
| 273 | pacer_->Resume(); |
| 274 | } else { |
| 275 | pacer_->Pause(); |
| 276 | } |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 277 | { |
| 278 | rtc::CritScope cs(&critsect_); |
| 279 | network_state_ = state; |
| 280 | } |
| 281 | MaybeTriggerOnNetworkChanged(); |
stefan | 457a61d | 2015-10-14 03:12:59 -0700 | [diff] [blame] | 282 | } |
| 283 | |
mflodman | 0c478b3 | 2015-10-21 15:52:16 +0200 | [diff] [blame] | 284 | void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 285 | transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id, |
| 286 | sent_packet.send_time_ms); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 287 | } |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 288 | |
| 289 | void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
| 290 | remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 291 | transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms); |
| 292 | } |
| 293 | |
| 294 | int64_t CongestionController::TimeUntilNextProcess() { |
| 295 | return std::min(bitrate_controller_->TimeUntilNextProcess(), |
| 296 | remote_bitrate_estimator_->TimeUntilNextProcess()); |
| 297 | } |
| 298 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 299 | void CongestionController::Process() { |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 300 | bitrate_controller_->Process(); |
| 301 | remote_bitrate_estimator_->Process(); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 302 | MaybeTriggerOnNetworkChanged(); |
| 303 | } |
| 304 | |
| 305 | void CongestionController::MaybeTriggerOnNetworkChanged() { |
| 306 | // TODO(perkj): |observer_| can be nullptr if the ctor that accepts a |
| 307 | // BitrateObserver is used. Remove this check once the ctor is removed. |
| 308 | if (!observer_) |
| 309 | return; |
| 310 | |
| 311 | uint32_t bitrate_bps; |
| 312 | uint8_t fraction_loss; |
| 313 | int64_t rtt; |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 314 | bool estimate_changed = bitrate_controller_->GetNetworkParameters( |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 315 | &bitrate_bps, &fraction_loss, &rtt); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 316 | if (estimate_changed) |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 317 | pacer_->SetEstimatedBitrate(bitrate_bps); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 318 | |
| 319 | bitrate_bps = IsNetworkDown() || IsSendQueueFull() ? 0 : bitrate_bps; |
| 320 | |
| 321 | if (HasNetworkParametersToReportChanged(bitrate_bps, fraction_loss, rtt)) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 322 | observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt); |
| 323 | } |
| 324 | } |
| 325 | |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 326 | bool CongestionController::HasNetworkParametersToReportChanged( |
| 327 | uint32_t bitrate_bps, |
| 328 | uint8_t fraction_loss, |
| 329 | int64_t rtt) { |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 330 | rtc::CritScope cs(&critsect_); |
perkj | fea9309 | 2016-05-14 00:58:48 -0700 | [diff] [blame] | 331 | bool changed = |
| 332 | last_reported_bitrate_bps_ != bitrate_bps || |
| 333 | (bitrate_bps > 0 && (last_reported_fraction_loss_ != fraction_loss || |
| 334 | last_reported_rtt_ != rtt)); |
| 335 | last_reported_bitrate_bps_ = bitrate_bps; |
| 336 | last_reported_fraction_loss_ = fraction_loss; |
| 337 | last_reported_rtt_ = rtt; |
| 338 | return changed; |
| 339 | } |
| 340 | |
| 341 | bool CongestionController::IsSendQueueFull() const { |
| 342 | return pacer_->ExpectedQueueTimeMs() > PacedSender::kMaxQueueLengthMs; |
| 343 | } |
| 344 | |
| 345 | bool CongestionController::IsNetworkDown() const { |
| 346 | rtc::CritScope cs(&critsect_); |
| 347 | return network_state_ == kNetworkDown; |
Stefan Holmer | 789ba92 | 2016-02-17 15:52:17 +0100 | [diff] [blame] | 348 | } |
| 349 | |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 350 | } // namespace webrtc |