blob: d283796953b7fc2cf6e171afeb009f09c0d4ff54 [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +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 */
11
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000012#include "webrtc/modules/bitrate_controller/bitrate_controller_impl.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000013
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000014#include <algorithm>
jbauchf91e6d02016-01-24 23:05:21 -080015#include <map>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000016#include <utility>
17
stefand48717b2016-08-22 08:50:31 -070018#include "webrtc/base/checks.h"
19#include "webrtc/base/logging.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000021
22namespace webrtc {
23
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000024class BitrateControllerImpl::RtcpBandwidthObserverImpl
25 : public RtcpBandwidthObserver {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000026 public:
27 explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner)
28 : owner_(owner) {
29 }
30 virtual ~RtcpBandwidthObserverImpl() {
31 }
32 // Received RTCP REMB or TMMBR.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000033 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000034 owner_->OnReceivedEstimatedBitrate(bitrate);
35 }
36 // Received RTCP receiver block.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000037 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
38 int64_t rtt,
39 int64_t now_ms) override {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000040 if (report_blocks.empty())
41 return;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000042
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000043 int fraction_lost_aggregate = 0;
44 int total_number_of_packets = 0;
45
46 // Compute the a weighted average of the fraction loss from all report
47 // blocks.
stefand48717b2016-08-22 08:50:31 -070048 for (const RTCPReportBlock& report_block : report_blocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000049 std::map<uint32_t, uint32_t>::iterator seq_num_it =
stefand48717b2016-08-22 08:50:31 -070050 ssrc_to_last_received_extended_high_seq_num_.find(
51 report_block.sourceSSRC);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000052
53 int number_of_packets = 0;
stefand48717b2016-08-22 08:50:31 -070054 if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end()) {
55 number_of_packets =
56 report_block.extendedHighSeqNum - seq_num_it->second;
57 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000058
stefand48717b2016-08-22 08:50:31 -070059 fraction_lost_aggregate += number_of_packets * report_block.fractionLost;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000060 total_number_of_packets += number_of_packets;
61
62 // Update last received for this SSRC.
stefand48717b2016-08-22 08:50:31 -070063 ssrc_to_last_received_extended_high_seq_num_[report_block.sourceSSRC] =
64 report_block.extendedHighSeqNum;
65 }
66 if (total_number_of_packets < 0) {
67 LOG(LS_WARNING) << "Received report block where extended high sequence "
68 "number goes backwards, ignoring.";
69 return;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000070 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000071 if (total_number_of_packets == 0)
72 fraction_lost_aggregate = 0;
73 else
74 fraction_lost_aggregate = (fraction_lost_aggregate +
75 total_number_of_packets / 2) / total_number_of_packets;
76 if (fraction_lost_aggregate > 255)
77 return;
78
stefand48717b2016-08-22 08:50:31 -070079 RTC_DCHECK_GE(total_number_of_packets, 0);
80
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000081 owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt,
82 total_number_of_packets, now_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000083 }
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000084
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000085 private:
86 std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_;
87 BitrateControllerImpl* owner_;
88};
89
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000090BitrateController* BitrateController::CreateBitrateController(
andresp@webrtc.org44caf012014-03-26 21:00:21 +000091 Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -070092 BitrateObserver* observer,
93 RtcEventLog* event_log) {
94 return new BitrateControllerImpl(clock, observer, event_log);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000095}
96
ivoc14d5dbe2016-07-04 07:06:55 -070097BitrateController* BitrateController::CreateBitrateController(
98 Clock* clock,
99 RtcEventLog* event_log) {
100 return CreateBitrateController(clock, nullptr, event_log);
perkjec81bcd2016-05-11 06:01:13 -0700101}
102
sprang@webrtc.org9b791972014-12-18 11:53:59 +0000103BitrateControllerImpl::BitrateControllerImpl(Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -0700104 BitrateObserver* observer,
105 RtcEventLog* event_log)
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000106 : clock_(clock),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000107 observer_(observer),
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000108 last_bitrate_update_ms_(clock_->TimeInMilliseconds()),
ivoc14d5dbe2016-07-04 07:06:55 -0700109 event_log_(event_log),
110 bandwidth_estimation_(event_log),
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000111 reserved_bitrate_bps_(0),
112 last_bitrate_bps_(0),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000113 last_fraction_loss_(0),
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000114 last_rtt_ms_(0),
sprang@webrtc.org8bd2f402015-03-16 14:11:21 +0000115 last_reserved_bitrate_bps_(0) {
perkjec81bcd2016-05-11 06:01:13 -0700116 // This calls the observer_ if set, which means that the observer provided by
117 // the user must be ready to accept a bitrate update when it constructs the
Stefan Holmere5904162015-03-26 11:11:06 +0100118 // controller. We do this to avoid having to keep synchronized initial values
119 // in both the controller and the allocator.
120 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000121}
122
123RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
124 return new RtcpBandwidthObserverImpl(this);
125}
126
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +0100128 {
sprang867fb522015-08-03 04:38:41 -0700129 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100130 bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
131 }
132 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000133}
134
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000135void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps,
136 int max_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +0100137 {
sprang867fb522015-08-03 04:38:41 -0700138 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100139 bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
140 }
141 MaybeTriggerOnNetworkChanged();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000142}
143
philipelc6957c72016-04-28 15:52:49 +0200144void BitrateControllerImpl::SetBitrates(int start_bitrate_bps,
145 int min_bitrate_bps,
146 int max_bitrate_bps) {
147 {
148 rtc::CritScope cs(&critsect_);
149 bandwidth_estimation_.SetBitrates(start_bitrate_bps,
150 min_bitrate_bps,
151 max_bitrate_bps);
152 }
153 MaybeTriggerOnNetworkChanged();
154}
155
honghaiz059e1832016-06-24 11:03:55 -0700156void BitrateControllerImpl::ResetBitrates(int bitrate_bps,
157 int min_bitrate_bps,
158 int max_bitrate_bps) {
159 {
160 rtc::CritScope cs(&critsect_);
ivoc14d5dbe2016-07-04 07:06:55 -0700161 bandwidth_estimation_ = SendSideBandwidthEstimation(event_log_);
honghaiz059e1832016-06-24 11:03:55 -0700162 bandwidth_estimation_.SetBitrates(bitrate_bps, min_bitrate_bps,
163 max_bitrate_bps);
164 }
165 MaybeTriggerOnNetworkChanged();
166}
167
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000168void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169 {
sprang867fb522015-08-03 04:38:41 -0700170 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000171 reserved_bitrate_bps_ = reserved_bitrate_bps;
172 }
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000173 MaybeTriggerOnNetworkChanged();
174}
175
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000176void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000177 {
sprang867fb522015-08-03 04:38:41 -0700178 rtc::CritScope cs(&critsect_);
stefanb6b0b922015-09-04 03:04:56 -0700179 bandwidth_estimation_.UpdateReceiverEstimate(clock_->TimeInMilliseconds(),
180 bitrate);
sprang@webrtc.org9b791972014-12-18 11:53:59 +0000181 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000182 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000183}
184
stefan32f81542016-01-20 07:13:58 -0800185void BitrateControllerImpl::UpdateDelayBasedEstimate(uint32_t bitrate_bps) {
186 {
187 rtc::CritScope cs(&critsect_);
188 bandwidth_estimation_.UpdateDelayBasedEstimate(clock_->TimeInMilliseconds(),
189 bitrate_bps);
190 }
191 MaybeTriggerOnNetworkChanged();
192}
193
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000194int64_t BitrateControllerImpl::TimeUntilNextProcess() {
195 const int64_t kBitrateControllerUpdateIntervalMs = 25;
sprang867fb522015-08-03 04:38:41 -0700196 rtc::CritScope cs(&critsect_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000197 int64_t time_since_update_ms =
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000198 clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000199 return std::max<int64_t>(
200 kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000201}
202
pbosa26ac922016-02-25 04:50:01 -0800203void BitrateControllerImpl::Process() {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000204 if (TimeUntilNextProcess() > 0)
pbosa26ac922016-02-25 04:50:01 -0800205 return;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000206 {
sprang867fb522015-08-03 04:38:41 -0700207 rtc::CritScope cs(&critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000208 bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000209 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000210 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000211 last_bitrate_update_ms_ = clock_->TimeInMilliseconds();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000212}
213
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000214void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000215 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000216 int64_t rtt,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000217 int number_of_packets,
218 int64_t now_ms) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000219 {
sprang867fb522015-08-03 04:38:41 -0700220 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000221 bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt,
222 number_of_packets, now_ms);
223 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000224 MaybeTriggerOnNetworkChanged();
225}
226
227void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
perkjec81bcd2016-05-11 06:01:13 -0700228 if (!observer_)
229 return;
230
231 uint32_t bitrate_bps;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000232 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000233 int64_t rtt;
perkjec81bcd2016-05-11 06:01:13 -0700234
235 if (GetNetworkParameters(&bitrate_bps, &fraction_loss, &rtt))
236 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000237}
238
Stefan Holmere5904162015-03-26 11:11:06 +0100239bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
240 uint8_t* fraction_loss,
241 int64_t* rtt) {
sprang867fb522015-08-03 04:38:41 -0700242 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100243 int current_bitrate;
244 bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
245 *bitrate = current_bitrate;
246 *bitrate -= std::min(*bitrate, reserved_bitrate_bps_);
247 *bitrate =
248 std::max<uint32_t>(*bitrate, bandwidth_estimation_.GetMinBitrate());
249
250 bool new_bitrate = false;
251 if (*bitrate != last_bitrate_bps_ || *fraction_loss != last_fraction_loss_ ||
252 *rtt != last_rtt_ms_ ||
253 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
254 last_bitrate_bps_ = *bitrate;
255 last_fraction_loss_ = *fraction_loss;
256 last_rtt_ms_ = *rtt;
257 last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
258 new_bitrate = true;
259 }
260 return new_bitrate;
261}
262
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000263bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
sprang867fb522015-08-03 04:38:41 -0700264 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100265 int bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000266 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000267 int64_t rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000268 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
Stefan Holmere5904162015-03-26 11:11:06 +0100269 if (bitrate > 0) {
270 bitrate = bitrate - std::min<int>(bitrate, reserved_bitrate_bps_);
271 bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate());
272 *bandwidth = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000273 return true;
274 }
275 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000276}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000277} // namespace webrtc