blob: 09c7ca5e58112223268c3f4b2a0ab25c2a0695e3 [file] [log] [blame]
nisse559af382017-03-21 06:41:12 -07001/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/congestion_controller/include/receive_side_congestion_controller.h"
nisse559af382017-03-21 06:41:12 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/pacing/packet_router.h"
14#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
15#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
16#include "rtc_base/logging.h"
nisse559af382017-03-21 06:41:12 -070017
18namespace webrtc {
19
20namespace {
21static const uint32_t kTimeOffsetSwitchThreshold = 30;
22} // namespace
23
24ReceiveSideCongestionController::WrappingBitrateEstimator::
25 WrappingBitrateEstimator(RemoteBitrateObserver* observer,
26 const Clock* clock)
27 : observer_(observer),
28 clock_(clock),
29 rbe_(new RemoteBitrateEstimatorSingleStream(observer_, clock_)),
30 using_absolute_send_time_(false),
31 packets_since_absolute_send_time_(0),
32 min_bitrate_bps_(congestion_controller::GetMinBitrateBps()) {}
33
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020034ReceiveSideCongestionController::WrappingBitrateEstimator::
35 ~WrappingBitrateEstimator() = default;
36
nisse559af382017-03-21 06:41:12 -070037void ReceiveSideCongestionController::WrappingBitrateEstimator::IncomingPacket(
38 int64_t arrival_time_ms,
39 size_t payload_size,
40 const RTPHeader& header) {
41 rtc::CritScope cs(&crit_sect_);
42 PickEstimatorFromHeader(header);
43 rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
44}
45
46void ReceiveSideCongestionController::WrappingBitrateEstimator::Process() {
47 rtc::CritScope cs(&crit_sect_);
48 rbe_->Process();
49}
50
51int64_t ReceiveSideCongestionController::WrappingBitrateEstimator::
52 TimeUntilNextProcess() {
53 rtc::CritScope cs(&crit_sect_);
54 return rbe_->TimeUntilNextProcess();
55}
56
57void ReceiveSideCongestionController::WrappingBitrateEstimator::OnRttUpdate(
58 int64_t avg_rtt_ms,
59 int64_t max_rtt_ms) {
60 rtc::CritScope cs(&crit_sect_);
61 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
62}
63
64void ReceiveSideCongestionController::WrappingBitrateEstimator::RemoveStream(
65 unsigned int ssrc) {
66 rtc::CritScope cs(&crit_sect_);
67 rbe_->RemoveStream(ssrc);
68}
69
70bool ReceiveSideCongestionController::WrappingBitrateEstimator::LatestEstimate(
71 std::vector<unsigned int>* ssrcs,
72 unsigned int* bitrate_bps) const {
73 rtc::CritScope cs(&crit_sect_);
74 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
75}
76
77void ReceiveSideCongestionController::WrappingBitrateEstimator::SetMinBitrate(
78 int min_bitrate_bps) {
79 rtc::CritScope cs(&crit_sect_);
80 rbe_->SetMinBitrate(min_bitrate_bps);
81 min_bitrate_bps_ = min_bitrate_bps;
82}
83
84void ReceiveSideCongestionController::WrappingBitrateEstimator::
85 PickEstimatorFromHeader(const RTPHeader& header) {
86 if (header.extension.hasAbsoluteSendTime) {
87 // If we see AST in header, switch RBE strategy immediately.
88 if (!using_absolute_send_time_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010089 RTC_LOG(LS_INFO)
nisse559af382017-03-21 06:41:12 -070090 << "WrappingBitrateEstimator: Switching to absolute send time RBE.";
91 using_absolute_send_time_ = true;
92 PickEstimator();
93 }
94 packets_since_absolute_send_time_ = 0;
95 } else {
96 // When we don't see AST, wait for a few packets before going back to TOF.
97 if (using_absolute_send_time_) {
98 ++packets_since_absolute_send_time_;
99 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100100 RTC_LOG(LS_INFO)
101 << "WrappingBitrateEstimator: Switching to transmission "
102 << "time offset RBE.";
nisse559af382017-03-21 06:41:12 -0700103 using_absolute_send_time_ = false;
104 PickEstimator();
105 }
106 }
107 }
108}
109
110// Instantiate RBE for Time Offset or Absolute Send Time extensions.
111void ReceiveSideCongestionController::WrappingBitrateEstimator::
112 PickEstimator() {
113 if (using_absolute_send_time_) {
114 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
115 } else {
116 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
117 }
118 rbe_->SetMinBitrate(min_bitrate_bps_);
119}
120
121ReceiveSideCongestionController::ReceiveSideCongestionController(
122 const Clock* clock,
nisse559af382017-03-21 06:41:12 -0700123 PacketRouter* packet_router)
nisse05843312017-04-18 23:38:35 -0700124 : remote_bitrate_estimator_(packet_router, clock),
nisse559af382017-03-21 06:41:12 -0700125 remote_estimator_proxy_(clock, packet_router) {}
126
127void ReceiveSideCongestionController::OnReceivedPacket(
128 int64_t arrival_time_ms,
129 size_t payload_size,
130 const RTPHeader& header) {
131 // Send-side BWE.
132 if (header.extension.hasTransportSequenceNumber) {
133 remote_estimator_proxy_.IncomingPacket(arrival_time_ms, payload_size,
134 header);
135 } else {
136 // Receive-side BWE.
137 remote_bitrate_estimator_.IncomingPacket(arrival_time_ms, payload_size,
138 header);
139 }
140}
141
142RemoteBitrateEstimator*
143ReceiveSideCongestionController::GetRemoteBitrateEstimator(bool send_side_bwe) {
144 if (send_side_bwe) {
145 return &remote_estimator_proxy_;
146 } else {
147 return &remote_bitrate_estimator_;
148 }
149}
150
151const RemoteBitrateEstimator*
152ReceiveSideCongestionController::GetRemoteBitrateEstimator(
153 bool send_side_bwe) const {
154 if (send_side_bwe) {
155 return &remote_estimator_proxy_;
156 } else {
157 return &remote_bitrate_estimator_;
158 }
159}
160
161void ReceiveSideCongestionController::OnRttUpdate(int64_t avg_rtt_ms,
162 int64_t max_rtt_ms) {
163 remote_bitrate_estimator_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
164}
165
166void ReceiveSideCongestionController::OnBitrateChanged(int bitrate_bps) {
167 remote_estimator_proxy_.OnBitrateChanged(bitrate_bps);
168}
169
170int64_t ReceiveSideCongestionController::TimeUntilNextProcess() {
171 return remote_bitrate_estimator_.TimeUntilNextProcess();
172}
173
174void ReceiveSideCongestionController::Process() {
175 remote_bitrate_estimator_.Process();
176}
177
178} // namespace webrtc