blob: 11e66efbc2d51cea8c565b3868633a4020a28624 [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
34void ReceiveSideCongestionController::WrappingBitrateEstimator::IncomingPacket(
35 int64_t arrival_time_ms,
36 size_t payload_size,
37 const RTPHeader& header) {
38 rtc::CritScope cs(&crit_sect_);
39 PickEstimatorFromHeader(header);
40 rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
41}
42
43void ReceiveSideCongestionController::WrappingBitrateEstimator::Process() {
44 rtc::CritScope cs(&crit_sect_);
45 rbe_->Process();
46}
47
48int64_t ReceiveSideCongestionController::WrappingBitrateEstimator::
49 TimeUntilNextProcess() {
50 rtc::CritScope cs(&crit_sect_);
51 return rbe_->TimeUntilNextProcess();
52}
53
54void ReceiveSideCongestionController::WrappingBitrateEstimator::OnRttUpdate(
55 int64_t avg_rtt_ms,
56 int64_t max_rtt_ms) {
57 rtc::CritScope cs(&crit_sect_);
58 rbe_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
59}
60
61void ReceiveSideCongestionController::WrappingBitrateEstimator::RemoveStream(
62 unsigned int ssrc) {
63 rtc::CritScope cs(&crit_sect_);
64 rbe_->RemoveStream(ssrc);
65}
66
67bool ReceiveSideCongestionController::WrappingBitrateEstimator::LatestEstimate(
68 std::vector<unsigned int>* ssrcs,
69 unsigned int* bitrate_bps) const {
70 rtc::CritScope cs(&crit_sect_);
71 return rbe_->LatestEstimate(ssrcs, bitrate_bps);
72}
73
74void ReceiveSideCongestionController::WrappingBitrateEstimator::SetMinBitrate(
75 int min_bitrate_bps) {
76 rtc::CritScope cs(&crit_sect_);
77 rbe_->SetMinBitrate(min_bitrate_bps);
78 min_bitrate_bps_ = min_bitrate_bps;
79}
80
81void ReceiveSideCongestionController::WrappingBitrateEstimator::
82 PickEstimatorFromHeader(const RTPHeader& header) {
83 if (header.extension.hasAbsoluteSendTime) {
84 // If we see AST in header, switch RBE strategy immediately.
85 if (!using_absolute_send_time_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010086 RTC_LOG(LS_INFO)
nisse559af382017-03-21 06:41:12 -070087 << "WrappingBitrateEstimator: Switching to absolute send time RBE.";
88 using_absolute_send_time_ = true;
89 PickEstimator();
90 }
91 packets_since_absolute_send_time_ = 0;
92 } else {
93 // When we don't see AST, wait for a few packets before going back to TOF.
94 if (using_absolute_send_time_) {
95 ++packets_since_absolute_send_time_;
96 if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010097 RTC_LOG(LS_INFO)
98 << "WrappingBitrateEstimator: Switching to transmission "
99 << "time offset RBE.";
nisse559af382017-03-21 06:41:12 -0700100 using_absolute_send_time_ = false;
101 PickEstimator();
102 }
103 }
104 }
105}
106
107// Instantiate RBE for Time Offset or Absolute Send Time extensions.
108void ReceiveSideCongestionController::WrappingBitrateEstimator::
109 PickEstimator() {
110 if (using_absolute_send_time_) {
111 rbe_.reset(new RemoteBitrateEstimatorAbsSendTime(observer_, clock_));
112 } else {
113 rbe_.reset(new RemoteBitrateEstimatorSingleStream(observer_, clock_));
114 }
115 rbe_->SetMinBitrate(min_bitrate_bps_);
116}
117
118ReceiveSideCongestionController::ReceiveSideCongestionController(
119 const Clock* clock,
nisse559af382017-03-21 06:41:12 -0700120 PacketRouter* packet_router)
nisse05843312017-04-18 23:38:35 -0700121 : remote_bitrate_estimator_(packet_router, clock),
nisse559af382017-03-21 06:41:12 -0700122 remote_estimator_proxy_(clock, packet_router) {}
123
124void ReceiveSideCongestionController::OnReceivedPacket(
125 int64_t arrival_time_ms,
126 size_t payload_size,
127 const RTPHeader& header) {
128 // Send-side BWE.
129 if (header.extension.hasTransportSequenceNumber) {
130 remote_estimator_proxy_.IncomingPacket(arrival_time_ms, payload_size,
131 header);
132 } else {
133 // Receive-side BWE.
134 remote_bitrate_estimator_.IncomingPacket(arrival_time_ms, payload_size,
135 header);
136 }
137}
138
139RemoteBitrateEstimator*
140ReceiveSideCongestionController::GetRemoteBitrateEstimator(bool send_side_bwe) {
141 if (send_side_bwe) {
142 return &remote_estimator_proxy_;
143 } else {
144 return &remote_bitrate_estimator_;
145 }
146}
147
148const RemoteBitrateEstimator*
149ReceiveSideCongestionController::GetRemoteBitrateEstimator(
150 bool send_side_bwe) const {
151 if (send_side_bwe) {
152 return &remote_estimator_proxy_;
153 } else {
154 return &remote_bitrate_estimator_;
155 }
156}
157
158void ReceiveSideCongestionController::OnRttUpdate(int64_t avg_rtt_ms,
159 int64_t max_rtt_ms) {
160 remote_bitrate_estimator_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
161}
162
163void ReceiveSideCongestionController::OnBitrateChanged(int bitrate_bps) {
164 remote_estimator_proxy_.OnBitrateChanged(bitrate_bps);
165}
166
167int64_t ReceiveSideCongestionController::TimeUntilNextProcess() {
168 return remote_bitrate_estimator_.TimeUntilNextProcess();
169}
170
171void ReceiveSideCongestionController::Process() {
172 remote_bitrate_estimator_.Process();
173}
174
175} // namespace webrtc