blob: 1a51dece92069ab59b2f7ad894ebc418d218778e [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "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>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000015#include <utility>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
18#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.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 }
Danil Chapovalov38018ba2017-06-12 16:29:45 +020030 ~RtcpBandwidthObserverImpl() override = default;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000031 // Received RTCP REMB or TMMBR.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000032 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
Danil Chapovalov38018ba2017-06-12 16:29:45 +020033 owner_->OnReceivedEstimatedBitrate(bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000034 }
35 // Received RTCP receiver block.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000036 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
37 int64_t rtt,
38 int64_t now_ms) override {
Danil Chapovalov38018ba2017-06-12 16:29:45 +020039 owner_->OnReceivedRtcpReceiverReport(report_blocks, rtt, now_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000040 }
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000041
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000042 private:
Danil Chapovalov38018ba2017-06-12 16:29:45 +020043 BitrateControllerImpl* const owner_;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000044};
45
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000046BitrateController* BitrateController::CreateBitrateController(
elad.alon61ce37e2017-03-09 07:09:31 -080047 const Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -070048 BitrateObserver* observer,
49 RtcEventLog* event_log) {
50 return new BitrateControllerImpl(clock, observer, event_log);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000051}
52
ivoc14d5dbe2016-07-04 07:06:55 -070053BitrateController* BitrateController::CreateBitrateController(
elad.alon61ce37e2017-03-09 07:09:31 -080054 const Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -070055 RtcEventLog* event_log) {
56 return CreateBitrateController(clock, nullptr, event_log);
perkjec81bcd2016-05-11 06:01:13 -070057}
58
elad.alon61ce37e2017-03-09 07:09:31 -080059BitrateControllerImpl::BitrateControllerImpl(const Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -070060 BitrateObserver* observer,
61 RtcEventLog* event_log)
andresp@webrtc.org44caf012014-03-26 21:00:21 +000062 : clock_(clock),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000063 observer_(observer),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000064 last_bitrate_update_ms_(clock_->TimeInMilliseconds()),
ivoc14d5dbe2016-07-04 07:06:55 -070065 event_log_(event_log),
66 bandwidth_estimation_(event_log),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000067 reserved_bitrate_bps_(0),
68 last_bitrate_bps_(0),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000069 last_fraction_loss_(0),
andresp@webrtc.org44caf012014-03-26 21:00:21 +000070 last_rtt_ms_(0),
sprang@webrtc.org8bd2f402015-03-16 14:11:21 +000071 last_reserved_bitrate_bps_(0) {
perkjec81bcd2016-05-11 06:01:13 -070072 // This calls the observer_ if set, which means that the observer provided by
73 // the user must be ready to accept a bitrate update when it constructs the
Stefan Holmere5904162015-03-26 11:11:06 +010074 // controller. We do this to avoid having to keep synchronized initial values
75 // in both the controller and the allocator.
76 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000077}
78
79RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
80 return new RtcpBandwidthObserverImpl(this);
81}
82
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000083void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +010084 {
sprang867fb522015-08-03 04:38:41 -070085 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +010086 bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
87 }
88 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000089}
90
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000091void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps,
92 int max_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +010093 {
sprang867fb522015-08-03 04:38:41 -070094 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +010095 bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
96 }
97 MaybeTriggerOnNetworkChanged();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +000098}
99
philipelc6957c72016-04-28 15:52:49 +0200100void BitrateControllerImpl::SetBitrates(int start_bitrate_bps,
101 int min_bitrate_bps,
102 int max_bitrate_bps) {
103 {
104 rtc::CritScope cs(&critsect_);
105 bandwidth_estimation_.SetBitrates(start_bitrate_bps,
106 min_bitrate_bps,
107 max_bitrate_bps);
108 }
109 MaybeTriggerOnNetworkChanged();
110}
111
honghaiz059e1832016-06-24 11:03:55 -0700112void BitrateControllerImpl::ResetBitrates(int bitrate_bps,
113 int min_bitrate_bps,
114 int max_bitrate_bps) {
115 {
116 rtc::CritScope cs(&critsect_);
ivoc14d5dbe2016-07-04 07:06:55 -0700117 bandwidth_estimation_ = SendSideBandwidthEstimation(event_log_);
honghaiz059e1832016-06-24 11:03:55 -0700118 bandwidth_estimation_.SetBitrates(bitrate_bps, min_bitrate_bps,
119 max_bitrate_bps);
120 }
121 MaybeTriggerOnNetworkChanged();
122}
123
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000124void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000125 {
sprang867fb522015-08-03 04:38:41 -0700126 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127 reserved_bitrate_bps_ = reserved_bitrate_bps;
128 }
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000129 MaybeTriggerOnNetworkChanged();
130}
131
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700132// This is called upon reception of REMB or TMMBR.
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200133void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000134 {
sprang867fb522015-08-03 04:38:41 -0700135 rtc::CritScope cs(&critsect_);
stefanb6b0b922015-09-04 03:04:56 -0700136 bandwidth_estimation_.UpdateReceiverEstimate(clock_->TimeInMilliseconds(),
137 bitrate);
gaetano.carlucci61050f62016-09-30 06:29:54 -0700138 BWE_TEST_LOGGING_PLOT(1, "REMB_kbps", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700139 bitrate / 1000);
sprang@webrtc.org9b791972014-12-18 11:53:59 +0000140 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000141 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000142}
143
Stefan Holmer280de9e2016-09-30 10:06:51 +0200144void BitrateControllerImpl::OnDelayBasedBweResult(
145 const DelayBasedBwe::Result& result) {
146 if (!result.updated)
147 return;
philipel0aa9d182016-08-24 02:45:35 -0700148 {
149 rtc::CritScope cs(&critsect_);
sergeyu26b67562016-11-14 10:52:58 -0800150 bandwidth_estimation_.UpdateDelayBasedEstimate(clock_->TimeInMilliseconds(),
151 result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200152 if (result.probe) {
153 bandwidth_estimation_.SetSendBitrate(result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200154 }
stefan32f81542016-01-20 07:13:58 -0800155 }
156 MaybeTriggerOnNetworkChanged();
157}
158
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000159int64_t BitrateControllerImpl::TimeUntilNextProcess() {
160 const int64_t kBitrateControllerUpdateIntervalMs = 25;
sprang867fb522015-08-03 04:38:41 -0700161 rtc::CritScope cs(&critsect_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000162 int64_t time_since_update_ms =
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000163 clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000164 return std::max<int64_t>(
165 kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000166}
167
pbosa26ac922016-02-25 04:50:01 -0800168void BitrateControllerImpl::Process() {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000169 {
sprang867fb522015-08-03 04:38:41 -0700170 rtc::CritScope cs(&critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000171 bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000172 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000173 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000174 last_bitrate_update_ms_ = clock_->TimeInMilliseconds();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000175}
176
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000177void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200178 const ReportBlockList& report_blocks,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000179 int64_t rtt,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000180 int64_t now_ms) {
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200181 if (report_blocks.empty())
182 return;
183
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000184 {
sprang867fb522015-08-03 04:38:41 -0700185 rtc::CritScope cs(&critsect_);
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200186 int fraction_lost_aggregate = 0;
187 int total_number_of_packets = 0;
188
189 // Compute the a weighted average of the fraction loss from all report
190 // blocks.
191 for (const RTCPReportBlock& report_block : report_blocks) {
192 std::map<uint32_t, uint32_t>::iterator seq_num_it =
193 ssrc_to_last_received_extended_high_seq_num_.find(
srte3e69e5c2017-08-09 06:13:45 -0700194 report_block.source_ssrc);
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200195
196 int number_of_packets = 0;
197 if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end()) {
198 number_of_packets =
srte3e69e5c2017-08-09 06:13:45 -0700199 report_block.extended_highest_sequence_number - seq_num_it->second;
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200200 }
201
srte3e69e5c2017-08-09 06:13:45 -0700202 fraction_lost_aggregate += number_of_packets * report_block.fraction_lost;
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200203 total_number_of_packets += number_of_packets;
204
205 // Update last received for this SSRC.
srte3e69e5c2017-08-09 06:13:45 -0700206 ssrc_to_last_received_extended_high_seq_num_[report_block.source_ssrc] =
207 report_block.extended_highest_sequence_number;
Danil Chapovalov38018ba2017-06-12 16:29:45 +0200208 }
209 if (total_number_of_packets < 0) {
210 LOG(LS_WARNING) << "Received report block where extended high sequence "
211 "number goes backwards, ignoring.";
212 return;
213 }
214 if (total_number_of_packets == 0)
215 fraction_lost_aggregate = 0;
216 else
217 fraction_lost_aggregate =
218 (fraction_lost_aggregate + total_number_of_packets / 2) /
219 total_number_of_packets;
220 if (fraction_lost_aggregate > 255)
221 return;
222
223 RTC_DCHECK_GE(total_number_of_packets, 0);
224
225 bandwidth_estimation_.UpdateReceiverBlock(fraction_lost_aggregate, rtt,
226 total_number_of_packets, now_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000227 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000228 MaybeTriggerOnNetworkChanged();
229}
230
231void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
perkjec81bcd2016-05-11 06:01:13 -0700232 if (!observer_)
233 return;
234
235 uint32_t bitrate_bps;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000236 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000237 int64_t rtt;
perkjec81bcd2016-05-11 06:01:13 -0700238
239 if (GetNetworkParameters(&bitrate_bps, &fraction_loss, &rtt))
240 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000241}
242
Stefan Holmere5904162015-03-26 11:11:06 +0100243bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
244 uint8_t* fraction_loss,
245 int64_t* rtt) {
sprang867fb522015-08-03 04:38:41 -0700246 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100247 int current_bitrate;
248 bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
249 *bitrate = current_bitrate;
250 *bitrate -= std::min(*bitrate, reserved_bitrate_bps_);
251 *bitrate =
252 std::max<uint32_t>(*bitrate, bandwidth_estimation_.GetMinBitrate());
253
254 bool new_bitrate = false;
255 if (*bitrate != last_bitrate_bps_ || *fraction_loss != last_fraction_loss_ ||
256 *rtt != last_rtt_ms_ ||
257 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
258 last_bitrate_bps_ = *bitrate;
259 last_fraction_loss_ = *fraction_loss;
260 last_rtt_ms_ = *rtt;
261 last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
262 new_bitrate = true;
263 }
gaetano.carlucci52a57032016-09-14 05:04:36 -0700264
gaetano.carlucci61050f62016-09-30 06:29:54 -0700265 BWE_TEST_LOGGING_PLOT(1, "fraction_loss_%", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700266 (last_fraction_loss_ * 100) / 256);
gaetano.carlucci61050f62016-09-30 06:29:54 -0700267 BWE_TEST_LOGGING_PLOT(1, "rtt_ms", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700268 last_rtt_ms_);
gaetano.carlucci61050f62016-09-30 06:29:54 -0700269 BWE_TEST_LOGGING_PLOT(1, "Target_bitrate_kbps", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700270 last_bitrate_bps_ / 1000);
271
Stefan Holmere5904162015-03-26 11:11:06 +0100272 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