blob: be0e0f12f21b49fcb70e30f0a4d7eaa6d2a3bcc9 [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"
gaetano.carlucci52a57032016-09-14 05:04:36 -070020#include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000022
23namespace webrtc {
24
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000025class BitrateControllerImpl::RtcpBandwidthObserverImpl
26 : public RtcpBandwidthObserver {
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000027 public:
28 explicit RtcpBandwidthObserverImpl(BitrateControllerImpl* owner)
29 : owner_(owner) {
30 }
31 virtual ~RtcpBandwidthObserverImpl() {
32 }
33 // Received RTCP REMB or TMMBR.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070035 owner_->OnReceiverEstimatedBitrate(bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000036 }
37 // Received RTCP receiver block.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000038 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
39 int64_t rtt,
40 int64_t now_ms) override {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000041 int fraction_lost_aggregate = 0;
42 int total_number_of_packets = 0;
43
44 // Compute the a weighted average of the fraction loss from all report
45 // blocks.
stefand48717b2016-08-22 08:50:31 -070046 for (const RTCPReportBlock& report_block : report_blocks) {
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000047 std::map<uint32_t, uint32_t>::iterator seq_num_it =
stefand48717b2016-08-22 08:50:31 -070048 ssrc_to_last_received_extended_high_seq_num_.find(
49 report_block.sourceSSRC);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000050
51 int number_of_packets = 0;
stefand48717b2016-08-22 08:50:31 -070052 if (seq_num_it != ssrc_to_last_received_extended_high_seq_num_.end()) {
53 number_of_packets =
54 report_block.extendedHighSeqNum - seq_num_it->second;
55 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000056
stefand48717b2016-08-22 08:50:31 -070057 fraction_lost_aggregate += number_of_packets * report_block.fractionLost;
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000058 total_number_of_packets += number_of_packets;
59
60 // Update last received for this SSRC.
stefand48717b2016-08-22 08:50:31 -070061 ssrc_to_last_received_extended_high_seq_num_[report_block.sourceSSRC] =
62 report_block.extendedHighSeqNum;
63 }
64 if (total_number_of_packets < 0) {
65 LOG(LS_WARNING) << "Received report block where extended high sequence "
66 "number goes backwards, ignoring.";
67 return;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000068 }
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000069 if (total_number_of_packets == 0)
70 fraction_lost_aggregate = 0;
71 else
72 fraction_lost_aggregate = (fraction_lost_aggregate +
73 total_number_of_packets / 2) / total_number_of_packets;
74 if (fraction_lost_aggregate > 255)
75 return;
76
stefand48717b2016-08-22 08:50:31 -070077 RTC_DCHECK_GE(total_number_of_packets, 0);
78
stefan@webrtc.org28a331e2013-09-17 07:49:56 +000079 owner_->OnReceivedRtcpReceiverReport(fraction_lost_aggregate, rtt,
80 total_number_of_packets, now_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000081 }
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000082
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000083 private:
84 std::map<uint32_t, uint32_t> ssrc_to_last_received_extended_high_seq_num_;
85 BitrateControllerImpl* owner_;
86};
87
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000088BitrateController* BitrateController::CreateBitrateController(
andresp@webrtc.org44caf012014-03-26 21:00:21 +000089 Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -070090 BitrateObserver* observer,
91 RtcEventLog* event_log) {
92 return new BitrateControllerImpl(clock, observer, event_log);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000093}
94
ivoc14d5dbe2016-07-04 07:06:55 -070095BitrateController* BitrateController::CreateBitrateController(
96 Clock* clock,
97 RtcEventLog* event_log) {
98 return CreateBitrateController(clock, nullptr, event_log);
perkjec81bcd2016-05-11 06:01:13 -070099}
100
sprang@webrtc.org9b791972014-12-18 11:53:59 +0000101BitrateControllerImpl::BitrateControllerImpl(Clock* clock,
ivoc14d5dbe2016-07-04 07:06:55 -0700102 BitrateObserver* observer,
103 RtcEventLog* event_log)
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000104 : clock_(clock),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000105 observer_(observer),
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000106 last_bitrate_update_ms_(clock_->TimeInMilliseconds()),
ivoc14d5dbe2016-07-04 07:06:55 -0700107 event_log_(event_log),
108 bandwidth_estimation_(event_log),
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000109 reserved_bitrate_bps_(0),
110 last_bitrate_bps_(0),
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000111 last_fraction_loss_(0),
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000112 last_rtt_ms_(0),
sprang@webrtc.org8bd2f402015-03-16 14:11:21 +0000113 last_reserved_bitrate_bps_(0) {
perkjec81bcd2016-05-11 06:01:13 -0700114 // This calls the observer_ if set, which means that the observer provided by
115 // the user must be ready to accept a bitrate update when it constructs the
Stefan Holmere5904162015-03-26 11:11:06 +0100116 // controller. We do this to avoid having to keep synchronized initial values
117 // in both the controller and the allocator.
118 MaybeTriggerOnNetworkChanged();
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000119}
120
121RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
122 return new RtcpBandwidthObserverImpl(this);
123}
124
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000125void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +0100126 {
sprang867fb522015-08-03 04:38:41 -0700127 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100128 bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
129 }
130 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000131}
132
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000133void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps,
134 int max_bitrate_bps) {
Stefan Holmere5904162015-03-26 11:11:06 +0100135 {
sprang867fb522015-08-03 04:38:41 -0700136 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100137 bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
138 }
139 MaybeTriggerOnNetworkChanged();
henrik.lundin@webrtc.org845862f2014-03-06 07:19:28 +0000140}
141
philipelc6957c72016-04-28 15:52:49 +0200142void BitrateControllerImpl::SetBitrates(int start_bitrate_bps,
143 int min_bitrate_bps,
144 int max_bitrate_bps) {
145 {
146 rtc::CritScope cs(&critsect_);
147 bandwidth_estimation_.SetBitrates(start_bitrate_bps,
148 min_bitrate_bps,
149 max_bitrate_bps);
150 }
151 MaybeTriggerOnNetworkChanged();
152}
153
honghaiz059e1832016-06-24 11:03:55 -0700154void BitrateControllerImpl::ResetBitrates(int bitrate_bps,
155 int min_bitrate_bps,
156 int max_bitrate_bps) {
157 {
158 rtc::CritScope cs(&critsect_);
ivoc14d5dbe2016-07-04 07:06:55 -0700159 bandwidth_estimation_ = SendSideBandwidthEstimation(event_log_);
honghaiz059e1832016-06-24 11:03:55 -0700160 bandwidth_estimation_.SetBitrates(bitrate_bps, min_bitrate_bps,
161 max_bitrate_bps);
162 }
163 MaybeTriggerOnNetworkChanged();
164}
165
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000166void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167 {
sprang867fb522015-08-03 04:38:41 -0700168 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169 reserved_bitrate_bps_ = reserved_bitrate_bps;
170 }
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000171 MaybeTriggerOnNetworkChanged();
172}
173
Irfan Sheriffb2540bb2016-09-12 12:28:54 -0700174// This is called upon reception of REMB or TMMBR.
175void BitrateControllerImpl::OnReceiverEstimatedBitrate(uint32_t bitrate) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000176 {
sprang867fb522015-08-03 04:38:41 -0700177 rtc::CritScope cs(&critsect_);
stefanb6b0b922015-09-04 03:04:56 -0700178 bandwidth_estimation_.UpdateReceiverEstimate(clock_->TimeInMilliseconds(),
179 bitrate);
gaetano.carlucci61050f62016-09-30 06:29:54 -0700180 BWE_TEST_LOGGING_PLOT(1, "REMB_kbps", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700181 bitrate / 1000);
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
Stefan Holmer280de9e2016-09-30 10:06:51 +0200186void BitrateControllerImpl::OnDelayBasedBweResult(
187 const DelayBasedBwe::Result& result) {
188 if (!result.updated)
189 return;
philipel0aa9d182016-08-24 02:45:35 -0700190 {
191 rtc::CritScope cs(&critsect_);
sergeyu26b67562016-11-14 10:52:58 -0800192 bandwidth_estimation_.UpdateDelayBasedEstimate(clock_->TimeInMilliseconds(),
193 result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200194 if (result.probe) {
195 bandwidth_estimation_.SetSendBitrate(result.target_bitrate_bps);
Stefan Holmer280de9e2016-09-30 10:06:51 +0200196 }
stefan32f81542016-01-20 07:13:58 -0800197 }
198 MaybeTriggerOnNetworkChanged();
199}
200
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000201int64_t BitrateControllerImpl::TimeUntilNextProcess() {
202 const int64_t kBitrateControllerUpdateIntervalMs = 25;
sprang867fb522015-08-03 04:38:41 -0700203 rtc::CritScope cs(&critsect_);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000204 int64_t time_since_update_ms =
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000205 clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000206 return std::max<int64_t>(
207 kBitrateControllerUpdateIntervalMs - time_since_update_ms, 0);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000208}
209
pbosa26ac922016-02-25 04:50:01 -0800210void BitrateControllerImpl::Process() {
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000211 if (TimeUntilNextProcess() > 0)
pbosa26ac922016-02-25 04:50:01 -0800212 return;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000213 {
sprang867fb522015-08-03 04:38:41 -0700214 rtc::CritScope cs(&critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000215 bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000216 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000217 MaybeTriggerOnNetworkChanged();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000218 last_bitrate_update_ms_ = clock_->TimeInMilliseconds();
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000219}
220
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000221void BitrateControllerImpl::OnReceivedRtcpReceiverReport(
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000222 uint8_t fraction_loss,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000223 int64_t rtt,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000224 int number_of_packets,
225 int64_t now_ms) {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000226 {
sprang867fb522015-08-03 04:38:41 -0700227 rtc::CritScope cs(&critsect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000228 bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt,
229 number_of_packets, now_ms);
230 }
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000231 MaybeTriggerOnNetworkChanged();
232}
233
234void BitrateControllerImpl::MaybeTriggerOnNetworkChanged() {
perkjec81bcd2016-05-11 06:01:13 -0700235 if (!observer_)
236 return;
237
238 uint32_t bitrate_bps;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000239 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000240 int64_t rtt;
perkjec81bcd2016-05-11 06:01:13 -0700241
242 if (GetNetworkParameters(&bitrate_bps, &fraction_loss, &rtt))
243 observer_->OnNetworkChanged(bitrate_bps, fraction_loss, rtt);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000244}
245
Stefan Holmere5904162015-03-26 11:11:06 +0100246bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
247 uint8_t* fraction_loss,
248 int64_t* rtt) {
sprang867fb522015-08-03 04:38:41 -0700249 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100250 int current_bitrate;
251 bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
252 *bitrate = current_bitrate;
253 *bitrate -= std::min(*bitrate, reserved_bitrate_bps_);
254 *bitrate =
255 std::max<uint32_t>(*bitrate, bandwidth_estimation_.GetMinBitrate());
256
257 bool new_bitrate = false;
258 if (*bitrate != last_bitrate_bps_ || *fraction_loss != last_fraction_loss_ ||
259 *rtt != last_rtt_ms_ ||
260 last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
261 last_bitrate_bps_ = *bitrate;
262 last_fraction_loss_ = *fraction_loss;
263 last_rtt_ms_ = *rtt;
264 last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
265 new_bitrate = true;
266 }
gaetano.carlucci52a57032016-09-14 05:04:36 -0700267
gaetano.carlucci61050f62016-09-30 06:29:54 -0700268 BWE_TEST_LOGGING_PLOT(1, "fraction_loss_%", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700269 (last_fraction_loss_ * 100) / 256);
gaetano.carlucci61050f62016-09-30 06:29:54 -0700270 BWE_TEST_LOGGING_PLOT(1, "rtt_ms", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700271 last_rtt_ms_);
gaetano.carlucci61050f62016-09-30 06:29:54 -0700272 BWE_TEST_LOGGING_PLOT(1, "Target_bitrate_kbps", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700273 last_bitrate_bps_ / 1000);
274
Stefan Holmere5904162015-03-26 11:11:06 +0100275 return new_bitrate;
276}
277
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000278bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
sprang867fb522015-08-03 04:38:41 -0700279 rtc::CritScope cs(&critsect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100280 int bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000281 uint8_t fraction_loss;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000282 int64_t rtt;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000283 bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
Stefan Holmere5904162015-03-26 11:11:06 +0100284 if (bitrate > 0) {
285 bitrate = bitrate - std::min<int>(bitrate, reserved_bitrate_bps_);
286 bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate());
287 *bandwidth = bitrate;
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000288 return true;
289 }
290 return false;
pwestin@webrtc.orga2cd7322012-04-23 08:32:47 +0000291}
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000292} // namespace webrtc