blob: e94baa7b6770db1903fc1ec51e20eaa72e0dae1a [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 {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070034 owner_->OnReceiverEstimatedBitrate(bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000035 }
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
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700176// This is called upon reception of REMB or TMMBR.
177void BitrateControllerImpl::OnReceiverEstimatedBitrate(uint32_t bitrate) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000178 {
sprang867fb522015-08-03 04:38:41 -0700179 rtc::CritScope cs(&critsect_);
stefanb6b0b922015-09-04 03:04:56 -0700180 bandwidth_estimation_.UpdateReceiverEstimate(clock_->TimeInMilliseconds(),
181 bitrate);
sprang@webrtc.org9b791972014-12-18 11:53:59 +0000182 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000183 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000184}
185
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700186void BitrateControllerImpl::OnProbeBitrate(uint32_t bitrate_bps) {
philipel0aa9d182016-08-24 02:45:35 -0700187 {
188 rtc::CritScope cs(&critsect_);
189 bandwidth_estimation_.SetSendBitrate(bitrate_bps);
190 }
191 MaybeTriggerOnNetworkChanged();
192}
193
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700194// TODO(isheriff): Perhaps need new interface for invocation from DelayBasedBwe.
195void BitrateControllerImpl::OnReceiveBitrateChanged(
196 const std::vector<uint32_t>& ssrcs,
197 uint32_t bitrate_bps) {
stefan32f81542016-01-20 07:13:58 -0800198 {
199 rtc::CritScope cs(&critsect_);
200 bandwidth_estimation_.UpdateDelayBasedEstimate(clock_->TimeInMilliseconds(),
201 bitrate_bps);
202 }
203 MaybeTriggerOnNetworkChanged();
204}
205
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000206int64_t BitrateControllerImpl::TimeUntilNextProcess() {
207 const int64_t kBitrateControllerUpdateIntervalMs = 25;
sprang867fb522015-08-03 04:38:41 -0700208 rtc::CritScope cs(&critsect_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000209 int64_t time_since_update_ms =
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000210 clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000211 return std::max<int64_t>(
212 kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000213}
214
pbosa26ac922016-02-25 04:50:01 -0800215void BitrateControllerImpl::Process() {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000216 if (TimeUntilNextProcess() > 0)
pbosa26ac922016-02-25 04:50:01 -0800217 return;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000218 {
sprang867fb522015-08-03 04:38:41 -0700219 rtc::CritScope cs(&critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000220 bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000221 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000222 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000223 last_bitrate_update_ms_ = clock_->TimeInMilliseconds();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000224}
225
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000226void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000227 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000228 int64_t rtt,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000229 int number_of_packets,
230 int64_t now_ms) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000231 {
sprang867fb522015-08-03 04:38:41 -0700232 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000233 bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt,
234 number_of_packets, now_ms);
235 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000236 MaybeTriggerOnNetworkChanged();
237}
238
239void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
perkjec81bcd2016-05-11 06:01:13 -0700240 if (!observer_)
241 return;
242
243 uint32_t bitrate_bps;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000244 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000245 int64_t rtt;
perkjec81bcd2016-05-11 06:01:13 -0700246
247 if (GetNetworkParameters(&bitrate_bps, &fraction_loss, &rtt))
248 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000249}
250
Stefan Holmere5904162015-03-26 11:11:06 +0100251bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
252 uint8_t* fraction_loss,
253 int64_t* rtt) {
sprang867fb522015-08-03 04:38:41 -0700254 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100255 int current_bitrate;
256 bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
257 *bitrate = current_bitrate;
258 *bitrate -= std::min(*bitrate, reserved_bitrate_bps_);
259 *bitrate =
260 std::max<uint32_t>(*bitrate, bandwidth_estimation_.GetMinBitrate());
261
262 bool new_bitrate = false;
263 if (*bitrate != last_bitrate_bps_ || *fraction_loss != last_fraction_loss_ ||
264 *rtt != last_rtt_ms_ ||
265 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
266 last_bitrate_bps_ = *bitrate;
267 last_fraction_loss_ = *fraction_loss;
268 last_rtt_ms_ = *rtt;
269 last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
270 new_bitrate = true;
271 }
272 return new_bitrate;
273}
274
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000275bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
sprang867fb522015-08-03 04:38:41 -0700276 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100277 int bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000278 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000279 int64_t rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000280 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
Stefan Holmere5904162015-03-26 11:11:06 +0100281 if (bitrate > 0) {
282 bitrate = bitrate - std::min<int>(bitrate, reserved_bitrate_bps_);
283 bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate());
284 *bandwidth = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000285 return true;
286 }
287 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000288}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000289} // namespace webrtc