blob: 0282e8148e9f5c8274ff88dfda56f6577d014b5f [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>
Stefan Holmer58c664c2016-02-08 14:31:30 +010014#include <vector>
15
stefan@webrtc.orga50e6f02015-03-09 10:06:40 +000016#include "webrtc/base/checks.h"
Peter Boström7c704b82015-12-04 16:13:05 +010017#include "webrtc/base/logging.h"
Stefan Holmer58c664c2016-02-08 14:31:30 +010018#include "webrtc/base/socket.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000019#include "webrtc/base/thread_annotations.h"
mflodman0e7e2592015-11-12 21:02:42 -080020#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010021#include "webrtc/modules/pacing/paced_sender.h"
sprang867fb522015-08-03 04:38:41 -070022#include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h"
Erik Språng468e62a2015-07-06 10:50:47 +020023#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
24#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010026#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
Peter Boström7623ce42015-12-09 12:13:30 +010027#include "webrtc/video/payload_router.h"
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000028
29namespace webrtc {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000030namespace {
31
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000032static const uint32_t kTimeOffsetSwitchThreshold = 30;
33
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000034class WrappingBitrateEstimator : public RemoteBitrateEstimator {
35 public:
pbosef35f062015-07-27 08:37:06 -070036 WrappingBitrateEstimator(RemoteBitrateObserver* observer, Clock* clock)
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000037 : observer_(observer),
38 clock_(clock),
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000039 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
stefan4fbd1452015-09-28 03:57:14 -070040 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000041 using_absolute_send_time_(false),
stefan4fbd1452015-09-28 03:57:14 -070042 packets_since_absolute_send_time_(0),
43 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {}
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000044
45 virtual ~WrappingBitrateEstimator() {}
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000046
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000047 void IncomingPacket(int64_t arrival_time_ms,
48 size_t payload_size,
Stefan Holmerff4ea932015-06-18 16:01:33 +020049 const RTPHeader& header,
50 bool was_paced) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000051 CriticalSectionScoped cs(crit_sect_.get());
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000052 PickEstimatorFromHeader(header);
Stefan Holmerff4ea932015-06-18 16:01:33 +020053 rbe_->IncomingPacket(arrival_time_ms, payload_size, header, was_paced);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000054 }
55
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000056 int32_t Process() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000057 CriticalSectionScoped cs(crit_sect_.get());
58 return rbe_->Process();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000059 }
60
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 int64_t TimeUntilNextProcess() override {
andresp@webrtc.org1295dc62014-07-02 13:23:19 +000062 CriticalSectionScoped cs(crit_sect_.get());
63 return rbe_->TimeUntilNextProcess();
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000064 }
65
stefan2328a942015-08-07 04:27:51 -070066 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000067 CriticalSectionScoped cs(crit_sect_.get());
stefan2328a942015-08-07 04:27:51 -070068 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000069 }
70
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 void RemoveStream(unsigned int ssrc) override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000072 CriticalSectionScoped cs(crit_sect_.get());
73 rbe_->RemoveStream(ssrc);
74 }
75
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000076 bool LatestEstimate(std::vector<unsigned int>* ssrcs,
77 unsigned int* bitrate_bps) const override {
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000078 CriticalSectionScoped cs(crit_sect_.get());
79 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
80 }
81
stefan4fbd1452015-09-28 03:57:14 -070082 void SetMinBitrate(int min_bitrate_bps) {
83 CriticalSectionScoped cs(crit_sect_.get());
84 rbe_->SetMinBitrate(min_bitrate_bps);
85 min_bitrate_bps_ = min_bitrate_bps;
86 }
87
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +000088 private:
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000089 void PickEstimatorFromHeader(const RTPHeader& header)
90 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000091 if (header.extension.hasAbsoluteSendTime) {
92 // If we see AST in header, switch RBE strategy immediately.
93 if (!using_absolute_send_time_) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000094 LOG(LS_INFO) <<
95 "WrappingBitrateEstimator: Switching to absolute send time RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000096 using_absolute_send_time_ = true;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +000097 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +000098 }
99 packets_since_absolute_send_time_ = 0;
100 } else {
101 // When we don't see AST, wait for a few packets before going back to TOF.
102 if (using_absolute_send_time_) {
103 ++packets_since_absolute_send_time_;
104 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000105 LOG(LS_INFO) << "WrappingBitrateEstimator: Switching to transmission "
106 << "time offset RBE.";
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000107 using_absolute_send_time_ = false;
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000108 PickEstimator();
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000109 }
110 }
111 }
112 }
113
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000114 // Instantiate RBE for Time Offset or Absolute Send Time extensions.
115 void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000116 if (using_absolute_send_time_) {
stefan4fbd1452015-09-28 03:57:14 -0700117 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000118 } else {
stefan4fbd1452015-09-28 03:57:14 -0700119 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000120 }
stefan4fbd1452015-09-28 03:57:14 -0700121 rbe_->SetMinBitrate(min_bitrate_bps_);
stefan@webrtc.orga16147c2014-03-25 10:37:31 +0000122 }
123
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000124 RemoteBitrateObserver* observer_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100125 Clock* const clock_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000126 rtc::scoped_ptr<CriticalSectionWrapper> crit_sect_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000127 rtc::scoped_ptr<RemoteBitrateEstimator> rbe_;
pbos@webrtc.org5ab75672013-12-16 12:24:44 +0000128 bool using_absolute_send_time_;
129 uint32_t packets_since_absolute_send_time_;
stefan4fbd1452015-09-28 03:57:14 -0700130 int min_bitrate_bps_;
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000131
henrikg3c089d72015-09-16 05:37:44 -0700132 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000133};
sprang867fb522015-08-03 04:38:41 -0700134
solenberg@webrtc.orga6db54d2013-05-27 16:02:56 +0000135} // namespace
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000136
Stefan Holmer58c664c2016-02-08 14:31:30 +0100137CongestionController::CongestionController(
138 Clock* clock,
Stefan Holmer58c664c2016-02-08 14:31:30 +0100139 BitrateObserver* bitrate_observer,
140 RemoteBitrateObserver* remote_bitrate_observer)
141 : clock_(clock),
Stefan Holmer58c664c2016-02-08 14:31:30 +0100142 pacer_(new PacedSender(clock_,
Stefan Holmer789ba922016-02-17 15:52:17 +0100143 &packet_router_,
144 BitrateController::kDefaultStartBitrateKbps,
145 PacedSender::kDefaultPaceMultiplier *
146 BitrateController::kDefaultStartBitrateKbps,
147 0)),
Erik Språng6b8d3552015-09-24 15:06:57 +0200148 remote_bitrate_estimator_(
Stefan Holmer58c664c2016-02-08 14:31:30 +0100149 new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
stefan847855b2015-09-11 09:52:15 -0700150 pacer_thread_(ProcessThread::Create("PacerThread")),
Stefan Holmere5904162015-03-26 11:11:06 +0100151 // Constructed last as this object calls the provided callback on
152 // construction.
153 bitrate_controller_(
Stefan Holmer58c664c2016-02-08 14:31:30 +0100154 BitrateController::CreateBitrateController(clock_, bitrate_observer)),
Stefan Holmer789ba922016-02-17 15:52:17 +0100155 remote_estimator_proxy_(clock_, &packet_router_),
156 transport_feedback_adapter_(bitrate_controller_.get(), clock_),
stefan4fbd1452015-09-28 03:57:14 -0700157 min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100158 transport_feedback_adapter_.SetBitrateEstimator(
159 new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_,
160 clock_));
161 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
162 min_bitrate_bps_);
Stefan Holmere5904162015-03-26 11:11:06 +0100163 pacer_thread_->RegisterModule(pacer_.get());
Stefan Holmer789ba922016-02-17 15:52:17 +0100164 pacer_thread_->RegisterModule(&remote_estimator_proxy_);
Stefan Holmere5904162015-03-26 11:11:06 +0100165 pacer_thread_->Start();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000166}
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000167
mflodman0c478b32015-10-21 15:52:16 +0200168CongestionController::~CongestionController() {
Stefan Holmere5904162015-03-26 11:11:06 +0100169 pacer_thread_->Stop();
170 pacer_thread_->DeRegisterModule(pacer_.get());
Stefan Holmer789ba922016-02-17 15:52:17 +0100171 pacer_thread_->DeRegisterModule(&remote_estimator_proxy_);
stefan@webrtc.orga50e6f02015-03-09 10:06:40 +0000172}
173
stefan@webrtc.orga50e6f02015-03-09 10:06:40 +0000174
mflodman0c478b32015-10-21 15:52:16 +0200175void CongestionController::SetBweBitrates(int min_bitrate_bps,
176 int start_bitrate_bps,
177 int max_bitrate_bps) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100178 RTC_DCHECK(config_thread_checker_.CalledOnValidThread());
Stefan Holmer62a5ccd2016-02-16 17:07:21 +0100179 // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
180 // and that we don't try to set the min bitrate to 0 from any applications.
181 // The congestion controller should allow a min bitrate of 0.
182 const int kMinBitrateBps = 10000;
183 if (min_bitrate_bps < kMinBitrateBps)
184 min_bitrate_bps = kMinBitrateBps;
185 if (max_bitrate_bps > 0)
186 max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps);
187 if (start_bitrate_bps > 0) {
188 start_bitrate_bps = std::max(min_bitrate_bps, start_bitrate_bps);
stefan4fbd1452015-09-28 03:57:14 -0700189 bitrate_controller_->SetStartBitrate(start_bitrate_bps);
Stefan Holmer62a5ccd2016-02-16 17:07:21 +0100190 }
stefan4fbd1452015-09-28 03:57:14 -0700191 bitrate_controller_->SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
Stefan Holmer789ba922016-02-17 15:52:17 +0100192 if (remote_bitrate_estimator_)
stefan4fbd1452015-09-28 03:57:14 -0700193 remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
stefan4fbd1452015-09-28 03:57:14 -0700194 min_bitrate_bps_ = min_bitrate_bps;
Stefan Holmer789ba922016-02-17 15:52:17 +0100195 transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
196 min_bitrate_bps_);
stefan4fbd1452015-09-28 03:57:14 -0700197}
198
mflodman0c478b32015-10-21 15:52:16 +0200199BitrateController* CongestionController::GetBitrateController() const {
bjornv@webrtc.orgcb89c6f2012-06-05 12:25:35 +0000200 return bitrate_controller_.get();
stefan@webrtc.orgf7288142012-06-05 10:44:00 +0000201}
202
mflodman0c478b32015-10-21 15:52:16 +0200203RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator(
Stefan Holmer789ba922016-02-17 15:52:17 +0100204 bool send_side_bwe) {
205 if (send_side_bwe) {
206 return &remote_estimator_proxy_;
207 } else {
mflodmana20de202015-10-18 22:08:19 -0700208 return remote_bitrate_estimator_.get();
Stefan Holmer789ba922016-02-17 15:52:17 +0100209 }
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000210}
211
mflodman0c478b32015-10-21 15:52:16 +0200212TransportFeedbackObserver*
213CongestionController::GetTransportFeedbackObserver() {
Stefan Holmer789ba922016-02-17 15:52:17 +0100214 RTC_DCHECK(config_thread_checker_.CalledOnValidThread());
215 return &transport_feedback_adapter_;
mflodman949c2f02015-10-16 02:31:11 -0700216}
217
mflodman0e7e2592015-11-12 21:02:42 -0800218void CongestionController::UpdatePacerBitrate(int bitrate_kbps,
219 int max_bitrate_kbps,
220 int min_bitrate_kbps) {
221 pacer_->UpdateBitrate(bitrate_kbps, max_bitrate_kbps, min_bitrate_kbps);
222}
223
mflodman0c478b32015-10-21 15:52:16 +0200224int64_t CongestionController::GetPacerQueuingDelayMs() const {
Stefan Holmere5904162015-03-26 11:11:06 +0100225 return pacer_->QueueInMs();
226}
227
mflodman0c478b32015-10-21 15:52:16 +0200228void CongestionController::SignalNetworkState(NetworkState state) {
stefan457a61d2015-10-14 03:12:59 -0700229 if (state == kNetworkUp) {
230 pacer_->Resume();
231 } else {
232 pacer_->Pause();
233 }
234}
235
mflodman0c478b32015-10-21 15:52:16 +0200236void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
Stefan Holmer789ba922016-02-17 15:52:17 +0100237 transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
238 sent_packet.send_time_ms);
stefanc1aeaf02015-10-15 07:26:07 -0700239}
Stefan Holmer789ba922016-02-17 15:52:17 +0100240
241void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
242 remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
243 transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
244}
245
246int64_t CongestionController::TimeUntilNextProcess() {
247 return std::min(bitrate_controller_->TimeUntilNextProcess(),
248 remote_bitrate_estimator_->TimeUntilNextProcess());
249}
250
251int32_t CongestionController::Process() {
252 bitrate_controller_->Process();
253 remote_bitrate_estimator_->Process();
254 return 0;
255}
256
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000257} // namespace webrtc