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