blob: 14a73fe1cdccfb1a3bdaed8f6771f5fe1af3cc1f [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"
Stefan Holmer58c664c2016-02-08 14:31:30 +010020#include "webrtc/base/socket.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000021#include "webrtc/base/thread_annotations.h"
mflodman0e7e2592015-11-12 21:02:42 -080022#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
perkje30c2722016-05-09 04:57:11 -070023#include "webrtc/modules/pacing/paced_sender.h"
sprang867fb522015-08-03 04:38:41 -070024#include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h"
Erik Språng468e62a2015-07-06 10:50:47 +020025#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 Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Peter Boström7623ce42015-12-09 12:13:30 +010029#include "webrtc/video/payload_router.h"
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000030
31namespace webrtc {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000032namespace {
33
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000034static const uint32_t kTimeOffsetSwitchThreshold = 30;
35
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000036class WrappingBitrateEstimator : public RemoteBitrateEstimator {
37 public:
pbosef35f062015-07-27 08:37:06 -070038 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000039 : observer_(observer),
40 clock_(clock),
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000041 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan4fbd1452015-09-28 03:57:14 -070042 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000043 using_absolute_send_time_(false),
stefan4fbd1452015-09-28 03:57:14 -070044 packets_since_absolute_send_time_(0),
45 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {}
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000046
47 virtual ~WrappingBitrateEstimator() {}
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000048
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000049 void IncomingPacket(int64_t arrival_time_ms,
50 size_t payload_size,
Stefan Holmerff4ea932015-06-18 16:01:33 +020051 const RTPHeader& header,
52 bool was_paced) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000053 CriticalSectionScoped cs(crit_sect_.get());
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000054 PickEstimatorFromHeader(header);
Stefan Holmerff4ea932015-06-18 16:01:33 +020055 rbe_->IncomingPacket(arrival_time_ms, payload_size, header, was_paced);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000056 }
57
pbosa26ac922016-02-25 04:50:01 -080058 void Process() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000059 CriticalSectionScoped cs(crit_sect_.get());
pbosa26ac922016-02-25 04:50:01 -080060 rbe_->Process();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000061 }
62
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 int64_t TimeUntilNextProcess() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000064 CriticalSectionScoped cs(crit_sect_.get());
65 return rbe_->TimeUntilNextProcess();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000066 }
67
stefan2328a942015-08-07 04:27:51 -070068 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000069 CriticalSectionScoped cs(crit_sect_.get());
stefan2328a942015-08-07 04:27:51 -070070 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000071 }
72
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073 void RemoveStream(unsigned int ssrc) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000074 CriticalSectionScoped cs(crit_sect_.get());
75 rbe_->RemoveStream(ssrc);
76 }
77
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 bool LatestEstimate(std::vector<unsigned int>* ssrcs,
79 unsigned int* bitrate_bps) const override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000080 CriticalSectionScoped cs(crit_sect_.get());
81 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
82 }
83
nisseef8b61e2016-04-29 06:09:15 -070084 void SetMinBitrate(int min_bitrate_bps) override {
stefan4fbd1452015-09-28 03:57:14 -070085 CriticalSectionScoped cs(crit_sect_.get());
86 rbe_->SetMinBitrate(min_bitrate_bps);
87 min_bitrate_bps_ = min_bitrate_bps;
88 }
89
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000090 private:
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000091 void PickEstimatorFromHeader(const RTPHeader& header)
92 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000093 if (header.extension.hasAbsoluteSendTime) {
94 // If we see AST in header, switch RBE strategy immediately.
95 if (!using_absolute_send_time_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000096 LOG(LS_INFO) <<
97 "WrappingBitrateEstimator: Switching to absolute send time RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000098 using_absolute_send_time_ = true;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000099 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000100 }
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.org5574dac2014-04-07 10:56:31 +0000107 LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission "
108 << "time offset RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000109 using_absolute_send_time_ = false;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000110 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000111 }
112 }
113 }
114 }
115
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000116 // Instantiate RBE for Time Offset or Absolute Send Time extensions.
117 void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000118 if (using_absolute_send_time_) {
stefan1112b2b2016-04-14 08:08:15 -0700119 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000120 } else {
stefan4fbd1452015-09-28 03:57:14 -0700121 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000122 }
stefan4fbd1452015-09-28 03:57:14 -0700123 rbe_->SetMinBitrate(min_bitrate_bps_);
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000124 }
125
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000126 RemoteBitrateObserver* observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100127 Clock* const clock_;
kwiberg22feaa32016-03-17 09:17:43 -0700128 std::unique_ptr<CriticalSectionWrapper> crit_sect_;
129 std::unique_ptr<RemoteBitrateEstimator> rbe_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000130 bool using_absolute_send_time_;
131 uint32_t packets_since_absolute_send_time_;
stefan4fbd1452015-09-28 03:57:14 -0700132 int min_bitrate_bps_;
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000133
henrikg3c089d72015-09-16 05:37:44 -0700134 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000135};
sprang867fb522015-08-03 04:38:41 -0700136
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000137} // namespace
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000138
Stefan Holmer58c664c2016-02-08 14:31:30 +0100139CongestionController::CongestionController(
140 Clock* clock,
perkj825eb582016-05-04 01:08:05 -0700141 BitrateObserver* bitrate_observer,
Stefan Holmer58c664c2016-02-08 14:31:30 +0100142 RemoteBitrateObserver* remote_bitrate_observer)
143 : clock_(clock),
Stefan Holmer58c664c2016-02-08 14:31:30 +0100144 pacer_(new PacedSender(clock_,
perkje30c2722016-05-09 04:57:11 -0700145 &packet_router_,
146 BitrateController::kDefaultStartBitrateKbps,
147 PacedSender::kDefaultPaceMultiplier *
148 BitrateController::kDefaultStartBitrateKbps,
149 0)),
Erik Språng6b8d3552015-09-24 15:06:57 +0200150 remote_bitrate_estimator_(
Stefan Holmer58c664c2016-02-08 14:31:30 +0100151 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
Stefan Holmere5904162015-03-26 11:11:06 +0100152 // Constructed last as this object calls the provided callback on
153 // construction.
perkje30c2722016-05-09 04:57:11 -0700154 bitrate_controller_(
155 BitrateController::CreateBitrateController(clock_, bitrate_observer)),
156 remote_estimator_proxy_(clock_, &packet_router_),
Stefan Holmer789ba922016-02-17 15:52:17 +0100157 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
perkje30c2722016-05-09 04:57:11 -0700158 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100159 transport_feedback_adapter_.SetBitrateEstimator(
stefan1112b2b2016-04-14 08:08:15 -0700160 new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_));
Stefan Holmer789ba922016-02-17 15:52:17 +0100161 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
162 min_bitrate_bps_);
perkje30c2722016-05-09 04:57:11 -0700163}
164
165CongestionController::~CongestionController() {
stefan@webrtc.orga50e6f02015-03-09 10:06:40 +0000166}
167
stefan@webrtc.orga50e6f02015-03-09 10:06:40 +0000168
mflodman0c478b32015-10-21 15:52:16 +0200169void CongestionController::SetBweBitrates(int min_bitrate_bps,
170 int start_bitrate_bps,
171 int max_bitrate_bps) {
Stefan Holmer62a5ccd2016-02-16 17:07:21 +0100172 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
173 // and that we don't try to set the min bitrate to 0 from any applications.
174 // The congestion controller should allow a min bitrate of 0.
175 const int kMinBitrateBps = 10000;
176 if (min_bitrate_bps < kMinBitrateBps)
177 min_bitrate_bps = kMinBitrateBps;
178 if (max_bitrate_bps > 0)
179 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps);
philipelc6957c72016-04-28 15:52:49 +0200180 if (start_bitrate_bps > 0)
Stefan Holmer62a5ccd2016-02-16 17:07:21 +0100181 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps);
philipelc6957c72016-04-28 15:52:49 +0200182
183 bitrate_controller_->SetBitrates(start_bitrate_bps,
184 min_bitrate_bps,
185 max_bitrate_bps);
186
Stefan Holmer789ba922016-02-17 15:52:17 +0100187 if (remote_bitrate_estimator_)
stefan4fbd1452015-09-28 03:57:14 -0700188 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
stefan4fbd1452015-09-28 03:57:14 -0700189 min_bitrate_bps_ = min_bitrate_bps;
Stefan Holmer789ba922016-02-17 15:52:17 +0100190 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
191 min_bitrate_bps_);
stefan4fbd1452015-09-28 03:57:14 -0700192}
193
mflodman0c478b32015-10-21 15:52:16 +0200194BitrateController* CongestionController::GetBitrateController() const {
bjornv@webrtc.orgcb89c6f2012-06-05 12:25:35 +0000195 return bitrate_controller_.get();
stefan@webrtc.orgf7288142012-06-05 10:44:00 +0000196}
197
mflodman0c478b32015-10-21 15:52:16 +0200198RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator(
Stefan Holmer789ba922016-02-17 15:52:17 +0100199 bool send_side_bwe) {
200 if (send_side_bwe) {
201 return &remote_estimator_proxy_;
202 } else {
mflodmana20de202015-10-18 22:08:19 -0700203 return remote_bitrate_estimator_.get();
Stefan Holmer789ba922016-02-17 15:52:17 +0100204 }
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000205}
206
mflodman0c478b32015-10-21 15:52:16 +0200207TransportFeedbackObserver*
208CongestionController::GetTransportFeedbackObserver() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100209 return &transport_feedback_adapter_;
mflodman949c2f02015-10-16 02:31:11 -0700210}
211
perkje30c2722016-05-09 04:57:11 -0700212void CongestionController::UpdatePacerBitrate(int bitrate_kbps,
213 int max_bitrate_kbps,
214 int min_bitrate_kbps) {
215 pacer_->UpdateBitrate(bitrate_kbps, max_bitrate_kbps, min_bitrate_kbps);
mflodman0e7e2592015-11-12 21:02:42 -0800216}
217
mflodman0c478b32015-10-21 15:52:16 +0200218int64_t CongestionController::GetPacerQueuingDelayMs() const {
Stefan Holmere5904162015-03-26 11:11:06 +0100219 return pacer_->QueueInMs();
220}
221
mflodman0c478b32015-10-21 15:52:16 +0200222void CongestionController::SignalNetworkState(NetworkState state) {
stefan457a61d2015-10-14 03:12:59 -0700223 if (state == kNetworkUp) {
224 pacer_->Resume();
225 } else {
226 pacer_->Pause();
227 }
228}
229
mflodman0c478b32015-10-21 15:52:16 +0200230void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100231 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
232 sent_packet.send_time_ms);
stefanc1aeaf02015-10-15 07:26:07 -0700233}
Stefan Holmer789ba922016-02-17 15:52:17 +0100234
235void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
236 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
237 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
238}
239
240int64_t CongestionController::TimeUntilNextProcess() {
241 return std::min(bitrate_controller_->TimeUntilNextProcess(),
242 remote_bitrate_estimator_->TimeUntilNextProcess());
243}
244
pbosa26ac922016-02-25 04:50:01 -0800245void CongestionController::Process() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100246 bitrate_controller_->Process();
247 remote_bitrate_estimator_->Process();
Stefan Holmer789ba922016-02-17 15:52:17 +0100248}
249
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000250} // namespace webrtc