2012-04-19 12:13:52 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-07-16 12:54:53 +00:00
|
|
|
#include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h"
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2016-01-24 23:05:21 -08:00
|
|
|
#include <algorithm>
|
2014-03-21 16:51:01 +00:00
|
|
|
#include <cmath>
|
2017-03-31 15:53:27 +02:00
|
|
|
#include <limits>
|
|
|
|
|
#include <string>
|
2012-04-19 12:13:52 +00:00
|
|
|
|
Moved RtcEventLog files from call/ to logging/
The RtcEventLog headers need to be accessible from any place which needs
logging, and the implementation needs access to data structures that are
logged.
After a discussion in the code review, we all agreed to move the RtcEventLog implementation into its own top level directory - which I called "logging/" in expectation that other types of logging may have similar requirements. The directory contains two main build targets - "rtc_event_log_api", which is just rtc_event_log.h, that has no external dependencies and can be used from anywhere, and "rtc_event_log_impl" which contains the rest of the implementation and has many dependencies (more in the future).
The "api" target can be referenced from anywhere, while the "impl" target is only needed at the place of instantiation (currently Call, soon to be moved to PeerConnection by https://codereview.webrtc.org/2353033005/).
This change allows using RtcEventLog in the p2p/ directory, so that we
can log STUN pings and ICE state transitions.
BUG=webrtc:6393
R=kjellander@webrtc.org, kwiberg@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, terelius@webrtc.org
Review URL: https://codereview.webrtc.org/2380683005 .
Cr-Commit-Position: refs/heads/master@{#14485}
2016-10-03 18:31:22 -07:00
|
|
|
#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
|
2016-11-07 04:17:14 -08:00
|
|
|
#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
|
2017-06-30 14:02:00 -07:00
|
|
|
#include "webrtc/rtc_base/checks.h"
|
|
|
|
|
#include "webrtc/rtc_base/logging.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/field_trial.h"
|
|
|
|
|
#include "webrtc/system_wrappers/include/metrics.h"
|
2012-04-19 12:13:52 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2014-03-21 16:51:01 +00:00
|
|
|
namespace {
|
2015-03-26 11:11:06 +01:00
|
|
|
const int64_t kBweIncreaseIntervalMs = 1000;
|
|
|
|
|
const int64_t kBweDecreaseIntervalMs = 300;
|
|
|
|
|
const int64_t kStartPhaseMs = 2000;
|
|
|
|
|
const int64_t kBweConverganceTimeMs = 20000;
|
|
|
|
|
const int kLimitNumPackets = 20;
|
|
|
|
|
const int kDefaultMaxBitrateBps = 1000000000;
|
2015-09-04 03:04:56 -07:00
|
|
|
const int64_t kLowBitrateLogPeriodMs = 10000;
|
2016-09-04 05:07:26 -07:00
|
|
|
const int64_t kRtcEventLogPeriodMs = 5000;
|
2016-09-20 14:14:23 +02:00
|
|
|
// Expecting that RTCP feedback is sent uniformly within [0.5, 1.5]s intervals.
|
2017-06-16 07:47:00 -07:00
|
|
|
const int64_t kFeedbackIntervalMs = 5000;
|
2016-09-20 14:14:23 +02:00
|
|
|
const int64_t kFeedbackTimeoutIntervals = 3;
|
|
|
|
|
const int64_t kTimeoutIntervalMs = 1000;
|
2014-03-21 16:51:01 +00:00
|
|
|
|
2017-03-31 15:53:27 +02:00
|
|
|
const float kDefaultLowLossThreshold = 0.02f;
|
|
|
|
|
const float kDefaultHighLossThreshold = 0.1f;
|
|
|
|
|
const int kDefaultBitrateThresholdKbps = 0;
|
|
|
|
|
|
2015-01-19 15:44:47 +00:00
|
|
|
struct UmaRampUpMetric {
|
|
|
|
|
const char* metric_name;
|
|
|
|
|
int bitrate_kbps;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const UmaRampUpMetric kUmaRampupMetrics[] = {
|
|
|
|
|
{"WebRTC.BWE.RampUpTimeTo500kbpsInMs", 500},
|
|
|
|
|
{"WebRTC.BWE.RampUpTimeTo1000kbpsInMs", 1000},
|
|
|
|
|
{"WebRTC.BWE.RampUpTimeTo2000kbpsInMs", 2000}};
|
|
|
|
|
const size_t kNumUmaRampupMetrics =
|
|
|
|
|
sizeof(kUmaRampupMetrics) / sizeof(kUmaRampupMetrics[0]);
|
|
|
|
|
|
2017-03-31 15:53:27 +02:00
|
|
|
const char kBweLosExperiment[] = "WebRTC-BweLossExperiment";
|
|
|
|
|
|
|
|
|
|
bool BweLossExperimentIsEnabled() {
|
|
|
|
|
std::string experiment_string =
|
|
|
|
|
webrtc::field_trial::FindFullName(kBweLosExperiment);
|
|
|
|
|
// The experiment is enabled iff the field trial string begins with "Enabled".
|
|
|
|
|
return experiment_string.find("Enabled") == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ReadBweLossExperimentParameters(float* low_loss_threshold,
|
|
|
|
|
float* high_loss_threshold,
|
|
|
|
|
uint32_t* bitrate_threshold_kbps) {
|
|
|
|
|
RTC_DCHECK(low_loss_threshold);
|
|
|
|
|
RTC_DCHECK(high_loss_threshold);
|
|
|
|
|
RTC_DCHECK(bitrate_threshold_kbps);
|
|
|
|
|
std::string experiment_string =
|
|
|
|
|
webrtc::field_trial::FindFullName(kBweLosExperiment);
|
|
|
|
|
int parsed_values =
|
|
|
|
|
sscanf(experiment_string.c_str(), "Enabled-%f,%f,%u", low_loss_threshold,
|
|
|
|
|
high_loss_threshold, bitrate_threshold_kbps);
|
|
|
|
|
if (parsed_values == 3) {
|
|
|
|
|
RTC_CHECK_GT(*low_loss_threshold, 0.0f)
|
|
|
|
|
<< "Loss threshold must be greater than 0.";
|
|
|
|
|
RTC_CHECK_LE(*low_loss_threshold, 1.0f)
|
|
|
|
|
<< "Loss threshold must be less than or equal to 1.";
|
|
|
|
|
RTC_CHECK_GT(*high_loss_threshold, 0.0f)
|
|
|
|
|
<< "Loss threshold must be greater than 0.";
|
|
|
|
|
RTC_CHECK_LE(*high_loss_threshold, 1.0f)
|
|
|
|
|
<< "Loss threshold must be less than or equal to 1.";
|
|
|
|
|
RTC_CHECK_LE(*low_loss_threshold, *high_loss_threshold)
|
|
|
|
|
<< "The low loss threshold must be less than or equal to the high loss "
|
|
|
|
|
"threshold.";
|
|
|
|
|
RTC_CHECK_GE(*bitrate_threshold_kbps, 0)
|
|
|
|
|
<< "Bitrate threshold can't be negative.";
|
|
|
|
|
RTC_CHECK_LT(*bitrate_threshold_kbps,
|
|
|
|
|
std::numeric_limits<int>::max() / 1000)
|
|
|
|
|
<< "Bitrate must be smaller enough to avoid overflows.";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
LOG(LS_WARNING) << "Failed to parse parameters for BweLossExperiment "
|
|
|
|
|
"experiment from field trial string. Using default.";
|
|
|
|
|
*low_loss_threshold = kDefaultLowLossThreshold;
|
|
|
|
|
*high_loss_threshold = kDefaultHighLossThreshold;
|
|
|
|
|
*bitrate_threshold_kbps = kDefaultBitrateThresholdKbps;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-01-24 23:05:21 -08:00
|
|
|
} // namespace
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2016-07-04 07:06:55 -07:00
|
|
|
SendSideBandwidthEstimation::SendSideBandwidthEstimation(RtcEventLog* event_log)
|
2015-10-22 08:52:20 -07:00
|
|
|
: lost_packets_since_last_loss_update_Q8_(0),
|
|
|
|
|
expected_packets_since_last_loss_update_(0),
|
2017-04-18 06:55:32 -07:00
|
|
|
current_bitrate_bps_(0),
|
2016-11-07 04:17:14 -08:00
|
|
|
min_bitrate_configured_(congestion_controller::GetMinBitrateBps()),
|
2015-03-26 11:11:06 +01:00
|
|
|
max_bitrate_configured_(kDefaultMaxBitrateBps),
|
2015-09-04 03:04:56 -07:00
|
|
|
last_low_bitrate_log_ms_(-1),
|
2015-10-22 08:52:20 -07:00
|
|
|
has_decreased_since_last_fraction_loss_(false),
|
2016-09-20 14:14:23 +02:00
|
|
|
last_feedback_ms_(-1),
|
|
|
|
|
last_packet_report_ms_(-1),
|
|
|
|
|
last_timeout_ms_(-1),
|
2012-04-19 12:13:52 +00:00
|
|
|
last_fraction_loss_(0),
|
2016-09-04 05:07:26 -07:00
|
|
|
last_logged_fraction_loss_(0),
|
2014-03-26 21:00:21 +00:00
|
|
|
last_round_trip_time_ms_(0),
|
2012-04-19 12:13:52 +00:00
|
|
|
bwe_incoming_(0),
|
2016-01-20 07:13:58 -08:00
|
|
|
delay_based_bitrate_bps_(0),
|
2014-10-23 11:57:05 +00:00
|
|
|
time_last_decrease_ms_(0),
|
2014-11-03 14:42:43 +00:00
|
|
|
first_report_time_ms_(-1),
|
|
|
|
|
initially_lost_packets_(0),
|
2014-11-04 19:32:10 +00:00
|
|
|
bitrate_at_2_seconds_kbps_(0),
|
2015-01-19 15:44:47 +00:00
|
|
|
uma_update_state_(kNoUpdate),
|
2015-11-05 12:02:15 -08:00
|
|
|
rampup_uma_stats_updated_(kNumUmaRampupMetrics, false),
|
2016-09-04 05:07:26 -07:00
|
|
|
event_log_(event_log),
|
2016-09-20 14:14:23 +02:00
|
|
|
last_rtc_event_log_ms_(-1),
|
2017-02-28 08:50:47 -08:00
|
|
|
in_timeout_experiment_(
|
2017-03-31 15:53:27 +02:00
|
|
|
webrtc::field_trial::IsEnabled("WebRTC-FeedbackTimeout")),
|
|
|
|
|
low_loss_threshold_(kDefaultLowLossThreshold),
|
|
|
|
|
high_loss_threshold_(kDefaultHighLossThreshold),
|
|
|
|
|
bitrate_threshold_bps_(1000 * kDefaultBitrateThresholdKbps) {
|
2016-07-04 07:06:55 -07:00
|
|
|
RTC_DCHECK(event_log);
|
2017-03-31 15:53:27 +02:00
|
|
|
if (BweLossExperimentIsEnabled()) {
|
|
|
|
|
uint32_t bitrate_threshold_kbps;
|
|
|
|
|
if (ReadBweLossExperimentParameters(&low_loss_threshold_,
|
|
|
|
|
&high_loss_threshold_,
|
|
|
|
|
&bitrate_threshold_kbps)) {
|
|
|
|
|
LOG(LS_INFO) << "Enabled BweLossExperiment with parameters "
|
|
|
|
|
<< low_loss_threshold_ << ", " << high_loss_threshold_
|
|
|
|
|
<< ", " << bitrate_threshold_kbps;
|
|
|
|
|
bitrate_threshold_bps_ = bitrate_threshold_kbps * 1000;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-04 07:06:55 -07:00
|
|
|
}
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2014-03-21 14:00:51 +00:00
|
|
|
SendSideBandwidthEstimation::~SendSideBandwidthEstimation() {}
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2016-04-28 15:52:49 +02:00
|
|
|
void SendSideBandwidthEstimation::SetBitrates(int send_bitrate,
|
|
|
|
|
int min_bitrate,
|
|
|
|
|
int max_bitrate) {
|
2017-04-18 06:55:32 -07:00
|
|
|
SetMinMaxBitrate(min_bitrate, max_bitrate);
|
2016-04-28 15:52:49 +02:00
|
|
|
if (send_bitrate > 0)
|
|
|
|
|
SetSendBitrate(send_bitrate);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
void SendSideBandwidthEstimation::SetSendBitrate(int bitrate) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK_GT(bitrate, 0);
|
2017-04-18 06:55:32 -07:00
|
|
|
CapBitrateToThresholds(Clock::GetRealTimeClock()->TimeInMilliseconds(),
|
|
|
|
|
bitrate);
|
2014-03-26 21:00:21 +00:00
|
|
|
// Clear last sent bitrate history so the new value can be used directly
|
|
|
|
|
// and not capped.
|
|
|
|
|
min_bitrate_history_.clear();
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
void SendSideBandwidthEstimation::SetMinMaxBitrate(int min_bitrate,
|
|
|
|
|
int max_bitrate) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK_GE(min_bitrate, 0);
|
2016-11-07 04:17:14 -08:00
|
|
|
min_bitrate_configured_ =
|
|
|
|
|
std::max(min_bitrate, congestion_controller::GetMinBitrateBps());
|
2015-03-26 11:11:06 +01:00
|
|
|
if (max_bitrate > 0) {
|
|
|
|
|
max_bitrate_configured_ =
|
|
|
|
|
std::max<uint32_t>(min_bitrate_configured_, max_bitrate);
|
|
|
|
|
} else {
|
|
|
|
|
max_bitrate_configured_ = kDefaultMaxBitrateBps;
|
|
|
|
|
}
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
int SendSideBandwidthEstimation::GetMinBitrate() const {
|
2015-03-04 12:24:26 +00:00
|
|
|
return min_bitrate_configured_;
|
2014-03-06 07:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:11:06 +01:00
|
|
|
void SendSideBandwidthEstimation::CurrentEstimate(int* bitrate,
|
2014-03-21 16:51:01 +00:00
|
|
|
uint8_t* loss,
|
2015-01-12 21:51:21 +00:00
|
|
|
int64_t* rtt) const {
|
2017-04-18 06:55:32 -07:00
|
|
|
*bitrate = current_bitrate_bps_;
|
2014-03-21 16:51:01 +00:00
|
|
|
*loss = last_fraction_loss_;
|
2014-03-26 21:00:21 +00:00
|
|
|
*rtt = last_round_trip_time_ms_;
|
2014-03-21 16:51:01 +00:00
|
|
|
}
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2015-09-04 03:04:56 -07:00
|
|
|
void SendSideBandwidthEstimation::UpdateReceiverEstimate(
|
|
|
|
|
int64_t now_ms, uint32_t bandwidth) {
|
2014-03-21 16:51:01 +00:00
|
|
|
bwe_incoming_ = bandwidth;
|
2017-04-18 06:55:32 -07:00
|
|
|
CapBitrateToThresholds(now_ms, current_bitrate_bps_);
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-20 07:13:58 -08:00
|
|
|
void SendSideBandwidthEstimation::UpdateDelayBasedEstimate(
|
|
|
|
|
int64_t now_ms,
|
|
|
|
|
uint32_t bitrate_bps) {
|
|
|
|
|
delay_based_bitrate_bps_ = bitrate_bps;
|
2017-04-18 06:55:32 -07:00
|
|
|
CapBitrateToThresholds(now_ms, current_bitrate_bps_);
|
2016-01-20 07:13:58 -08:00
|
|
|
}
|
|
|
|
|
|
2014-03-21 16:51:01 +00:00
|
|
|
void SendSideBandwidthEstimation::UpdateReceiverBlock(uint8_t fraction_loss,
|
2015-01-12 21:51:21 +00:00
|
|
|
int64_t rtt,
|
2014-03-21 16:51:01 +00:00
|
|
|
int number_of_packets,
|
2014-12-08 19:46:23 +00:00
|
|
|
int64_t now_ms) {
|
2016-09-20 14:14:23 +02:00
|
|
|
last_feedback_ms_ = now_ms;
|
2014-11-10 13:55:16 +00:00
|
|
|
if (first_report_time_ms_ == -1)
|
|
|
|
|
first_report_time_ms_ = now_ms;
|
|
|
|
|
|
2012-04-19 12:13:52 +00:00
|
|
|
// Update RTT.
|
2014-03-26 21:00:21 +00:00
|
|
|
last_round_trip_time_ms_ = rtt;
|
2012-04-19 12:13:52 +00:00
|
|
|
|
|
|
|
|
// Check sequence number diff and weight loss report
|
|
|
|
|
if (number_of_packets > 0) {
|
|
|
|
|
// Calculate number of lost packets.
|
2014-03-21 16:51:01 +00:00
|
|
|
const int num_lost_packets_Q8 = fraction_loss * number_of_packets;
|
2012-04-19 12:13:52 +00:00
|
|
|
// Accumulate reports.
|
2015-10-22 08:52:20 -07:00
|
|
|
lost_packets_since_last_loss_update_Q8_ += num_lost_packets_Q8;
|
|
|
|
|
expected_packets_since_last_loss_update_ += number_of_packets;
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2015-10-22 08:52:20 -07:00
|
|
|
// Don't generate a loss rate until it can be based on enough packets.
|
|
|
|
|
if (expected_packets_since_last_loss_update_ < kLimitNumPackets)
|
2014-03-21 16:51:01 +00:00
|
|
|
return;
|
2015-10-22 08:52:20 -07:00
|
|
|
|
|
|
|
|
has_decreased_since_last_fraction_loss_ = false;
|
|
|
|
|
last_fraction_loss_ = lost_packets_since_last_loss_update_Q8_ /
|
|
|
|
|
expected_packets_since_last_loss_update_;
|
|
|
|
|
|
|
|
|
|
// Reset accumulators.
|
|
|
|
|
lost_packets_since_last_loss_update_Q8_ = 0;
|
|
|
|
|
expected_packets_since_last_loss_update_ = 0;
|
2016-09-20 14:14:23 +02:00
|
|
|
last_packet_report_ms_ = now_ms;
|
|
|
|
|
UpdateEstimate(now_ms);
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
2014-11-10 13:55:16 +00:00
|
|
|
UpdateUmaStats(now_ms, rtt, (fraction_loss * number_of_packets) >> 8);
|
2014-11-04 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendSideBandwidthEstimation::UpdateUmaStats(int64_t now_ms,
|
2015-01-12 21:51:21 +00:00
|
|
|
int64_t rtt,
|
2014-11-04 19:32:10 +00:00
|
|
|
int lost_packets) {
|
2017-04-18 06:55:32 -07:00
|
|
|
int bitrate_kbps = static_cast<int>((current_bitrate_bps_ + 500) / 1000);
|
2015-01-19 15:44:47 +00:00
|
|
|
for (size_t i = 0; i < kNumUmaRampupMetrics; ++i) {
|
|
|
|
|
if (!rampup_uma_stats_updated_[i] &&
|
|
|
|
|
bitrate_kbps >= kUmaRampupMetrics[i].bitrate_kbps) {
|
2016-09-09 22:40:25 -07:00
|
|
|
RTC_HISTOGRAMS_COUNTS_100000(i, kUmaRampupMetrics[i].metric_name,
|
|
|
|
|
now_ms - first_report_time_ms_);
|
2015-01-19 15:44:47 +00:00
|
|
|
rampup_uma_stats_updated_[i] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-04 19:32:10 +00:00
|
|
|
if (IsInStartPhase(now_ms)) {
|
|
|
|
|
initially_lost_packets_ += lost_packets;
|
|
|
|
|
} else if (uma_update_state_ == kNoUpdate) {
|
|
|
|
|
uma_update_state_ = kFirstDone;
|
2015-01-19 15:44:47 +00:00
|
|
|
bitrate_at_2_seconds_kbps_ = bitrate_kbps;
|
2016-09-09 22:40:25 -07:00
|
|
|
RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitiallyLostPackets",
|
|
|
|
|
initially_lost_packets_, 0, 100, 50);
|
|
|
|
|
RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialRtt", static_cast<int>(rtt), 0,
|
|
|
|
|
2000, 50);
|
|
|
|
|
RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialBandwidthEstimate",
|
|
|
|
|
bitrate_at_2_seconds_kbps_, 0, 2000, 50);
|
2014-11-04 19:32:10 +00:00
|
|
|
} else if (uma_update_state_ == kFirstDone &&
|
|
|
|
|
now_ms - first_report_time_ms_ >= kBweConverganceTimeMs) {
|
|
|
|
|
uma_update_state_ = kDone;
|
2015-01-19 15:44:47 +00:00
|
|
|
int bitrate_diff_kbps =
|
|
|
|
|
std::max(bitrate_at_2_seconds_kbps_ - bitrate_kbps, 0);
|
2016-09-09 22:40:25 -07:00
|
|
|
RTC_HISTOGRAM_COUNTS("WebRTC.BWE.InitialVsConvergedDiff", bitrate_diff_kbps,
|
|
|
|
|
0, 2000, 50);
|
2014-11-03 14:42:43 +00:00
|
|
|
}
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-08 19:46:23 +00:00
|
|
|
void SendSideBandwidthEstimation::UpdateEstimate(int64_t now_ms) {
|
2017-04-18 06:55:32 -07:00
|
|
|
uint32_t new_bitrate = current_bitrate_bps_;
|
2016-01-21 08:55:03 -08:00
|
|
|
// We trust the REMB and/or delay-based estimate during the first 2 seconds if
|
|
|
|
|
// we haven't had any packet loss reported, to allow startup bitrate probing.
|
|
|
|
|
if (last_fraction_loss_ == 0 && IsInStartPhase(now_ms)) {
|
2017-04-18 06:55:32 -07:00
|
|
|
new_bitrate = std::max(bwe_incoming_, new_bitrate);
|
|
|
|
|
new_bitrate = std::max(delay_based_bitrate_bps_, new_bitrate);
|
|
|
|
|
|
|
|
|
|
if (new_bitrate != current_bitrate_bps_) {
|
2016-01-21 08:55:03 -08:00
|
|
|
min_bitrate_history_.clear();
|
2017-04-18 06:55:32 -07:00
|
|
|
min_bitrate_history_.push_back(
|
|
|
|
|
std::make_pair(now_ms, current_bitrate_bps_));
|
|
|
|
|
CapBitrateToThresholds(now_ms, new_bitrate);
|
2016-01-21 08:55:03 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2014-10-23 11:57:05 +00:00
|
|
|
}
|
2014-03-26 21:00:21 +00:00
|
|
|
UpdateMinHistory(now_ms);
|
2016-09-20 14:14:23 +02:00
|
|
|
if (last_packet_report_ms_ == -1) {
|
|
|
|
|
// No feedback received.
|
2017-04-18 06:55:32 -07:00
|
|
|
CapBitrateToThresholds(now_ms, current_bitrate_bps_);
|
2016-09-20 14:14:23 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int64_t time_since_packet_report_ms = now_ms - last_packet_report_ms_;
|
|
|
|
|
int64_t time_since_feedback_ms = now_ms - last_feedback_ms_;
|
|
|
|
|
if (time_since_packet_report_ms < 1.2 * kFeedbackIntervalMs) {
|
2017-03-31 15:53:27 +02:00
|
|
|
// We only care about loss above a given bitrate threshold.
|
|
|
|
|
float loss = last_fraction_loss_ / 256.0f;
|
|
|
|
|
// We only make decisions based on loss when the bitrate is above a
|
|
|
|
|
// threshold. This is a crude way of handling loss which is uncorrelated
|
|
|
|
|
// to congestion.
|
2017-04-18 06:55:32 -07:00
|
|
|
if (current_bitrate_bps_ < bitrate_threshold_bps_ ||
|
|
|
|
|
loss <= low_loss_threshold_) {
|
2014-03-26 21:00:21 +00:00
|
|
|
// Loss < 2%: Increase rate by 8% of the min bitrate in the last
|
|
|
|
|
// kBweIncreaseIntervalMs.
|
|
|
|
|
// Note that by remembering the bitrate over the last second one can
|
|
|
|
|
// rampup up one second faster than if only allowed to start ramping
|
|
|
|
|
// at 8% per second rate now. E.g.:
|
|
|
|
|
// If sending a constant 100kbps it can rampup immediatly to 108kbps
|
|
|
|
|
// whenever a receiver report is received with lower packet loss.
|
2017-04-18 06:55:32 -07:00
|
|
|
// If instead one would do: current_bitrate_bps_ *= 1.08^(delta time),
|
|
|
|
|
// it would take over one second since the lower packet loss to achieve
|
2016-09-20 14:14:23 +02:00
|
|
|
// 108kbps.
|
2017-04-18 06:55:32 -07:00
|
|
|
new_bitrate = static_cast<uint32_t>(
|
2014-03-26 21:00:21 +00:00
|
|
|
min_bitrate_history_.front().second * 1.08 + 0.5);
|
2012-04-19 12:13:52 +00:00
|
|
|
|
2014-03-21 16:51:01 +00:00
|
|
|
// Add 1 kbps extra, just to make sure that we do not get stuck
|
|
|
|
|
// (gives a little extra increase at low rates, negligible at higher
|
|
|
|
|
// rates).
|
2017-04-18 06:55:32 -07:00
|
|
|
new_bitrate += 1000;
|
|
|
|
|
} else if (current_bitrate_bps_ > bitrate_threshold_bps_) {
|
2017-03-31 15:53:27 +02:00
|
|
|
if (loss <= high_loss_threshold_) {
|
|
|
|
|
// Loss between 2% - 10%: Do nothing.
|
|
|
|
|
} else {
|
|
|
|
|
// Loss > 10%: Limit the rate decreases to once a kBweDecreaseIntervalMs
|
|
|
|
|
// + rtt.
|
|
|
|
|
if (!has_decreased_since_last_fraction_loss_ &&
|
|
|
|
|
(now_ms - time_last_decrease_ms_) >=
|
|
|
|
|
(kBweDecreaseIntervalMs + last_round_trip_time_ms_)) {
|
|
|
|
|
time_last_decrease_ms_ = now_ms;
|
2014-03-26 21:00:21 +00:00
|
|
|
|
2017-03-31 15:53:27 +02:00
|
|
|
// Reduce rate:
|
|
|
|
|
// newRate = rate * (1 - 0.5*lossRate);
|
|
|
|
|
// where packetLoss = 256*lossRate;
|
2017-04-18 06:55:32 -07:00
|
|
|
new_bitrate = static_cast<uint32_t>(
|
|
|
|
|
(current_bitrate_bps_ *
|
|
|
|
|
static_cast<double>(512 - last_fraction_loss_)) /
|
2017-03-31 15:53:27 +02:00
|
|
|
512.0);
|
|
|
|
|
has_decreased_since_last_fraction_loss_ = true;
|
|
|
|
|
}
|
2014-03-26 21:00:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-20 14:14:23 +02:00
|
|
|
} else if (time_since_feedback_ms >
|
|
|
|
|
kFeedbackTimeoutIntervals * kFeedbackIntervalMs &&
|
|
|
|
|
(last_timeout_ms_ == -1 ||
|
|
|
|
|
now_ms - last_timeout_ms_ > kTimeoutIntervalMs)) {
|
|
|
|
|
if (in_timeout_experiment_) {
|
|
|
|
|
LOG(LS_WARNING) << "Feedback timed out (" << time_since_feedback_ms
|
|
|
|
|
<< " ms), reducing bitrate.";
|
2017-04-18 06:55:32 -07:00
|
|
|
new_bitrate *= 0.8;
|
2016-09-20 14:14:23 +02:00
|
|
|
// Reset accumulators since we've already acted on missing feedback and
|
|
|
|
|
// shouldn't to act again on these old lost packets.
|
|
|
|
|
lost_packets_since_last_loss_update_Q8_ = 0;
|
|
|
|
|
expected_packets_since_last_loss_update_ = 0;
|
|
|
|
|
last_timeout_ms_ = now_ms;
|
|
|
|
|
}
|
2014-03-26 21:00:21 +00:00
|
|
|
}
|
2017-04-18 06:55:32 -07:00
|
|
|
|
|
|
|
|
CapBitrateToThresholds(now_ms, new_bitrate);
|
2014-03-26 21:00:21 +00:00
|
|
|
}
|
2014-03-21 16:51:01 +00:00
|
|
|
|
2014-11-03 14:42:43 +00:00
|
|
|
bool SendSideBandwidthEstimation::IsInStartPhase(int64_t now_ms) const {
|
|
|
|
|
return first_report_time_ms_ == -1 ||
|
|
|
|
|
now_ms - first_report_time_ms_ < kStartPhaseMs;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-08 19:46:23 +00:00
|
|
|
void SendSideBandwidthEstimation::UpdateMinHistory(int64_t now_ms) {
|
2014-03-26 21:00:21 +00:00
|
|
|
// Remove old data points from history.
|
|
|
|
|
// Since history precision is in ms, add one so it is able to increase
|
|
|
|
|
// bitrate if it is off by as little as 0.5ms.
|
|
|
|
|
while (!min_bitrate_history_.empty() &&
|
|
|
|
|
now_ms - min_bitrate_history_.front().first + 1 >
|
|
|
|
|
kBweIncreaseIntervalMs) {
|
|
|
|
|
min_bitrate_history_.pop_front();
|
|
|
|
|
}
|
2014-03-21 16:51:01 +00:00
|
|
|
|
2014-03-26 21:00:21 +00:00
|
|
|
// Typical minimum sliding-window algorithm: Pop values higher than current
|
|
|
|
|
// bitrate before pushing it.
|
|
|
|
|
while (!min_bitrate_history_.empty() &&
|
2017-04-18 06:55:32 -07:00
|
|
|
current_bitrate_bps_ <= min_bitrate_history_.back().second) {
|
2014-03-26 21:00:21 +00:00
|
|
|
min_bitrate_history_.pop_back();
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-18 06:55:32 -07:00
|
|
|
min_bitrate_history_.push_back(std::make_pair(now_ms, current_bitrate_bps_));
|
2014-03-17 17:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
2017-04-18 06:55:32 -07:00
|
|
|
void SendSideBandwidthEstimation::CapBitrateToThresholds(int64_t now_ms,
|
|
|
|
|
uint32_t bitrate_bps) {
|
|
|
|
|
if (bwe_incoming_ > 0 && bitrate_bps > bwe_incoming_) {
|
|
|
|
|
bitrate_bps = bwe_incoming_;
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
2017-04-18 06:55:32 -07:00
|
|
|
if (delay_based_bitrate_bps_ > 0 && bitrate_bps > delay_based_bitrate_bps_) {
|
|
|
|
|
bitrate_bps = delay_based_bitrate_bps_;
|
2016-01-20 07:13:58 -08:00
|
|
|
}
|
2017-04-18 06:55:32 -07:00
|
|
|
if (bitrate_bps > max_bitrate_configured_) {
|
|
|
|
|
bitrate_bps = max_bitrate_configured_;
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
2017-04-18 06:55:32 -07:00
|
|
|
if (bitrate_bps < min_bitrate_configured_) {
|
2015-09-04 03:04:56 -07:00
|
|
|
if (last_low_bitrate_log_ms_ == -1 ||
|
|
|
|
|
now_ms - last_low_bitrate_log_ms_ > kLowBitrateLogPeriodMs) {
|
2017-04-18 06:55:32 -07:00
|
|
|
LOG(LS_WARNING) << "Estimated available bandwidth " << bitrate_bps / 1000
|
2015-09-04 03:04:56 -07:00
|
|
|
<< " kbps is below configured min bitrate "
|
|
|
|
|
<< min_bitrate_configured_ / 1000 << " kbps.";
|
|
|
|
|
last_low_bitrate_log_ms_ = now_ms;
|
|
|
|
|
}
|
2017-04-18 06:55:32 -07:00
|
|
|
bitrate_bps = min_bitrate_configured_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bitrate_bps != current_bitrate_bps_ ||
|
|
|
|
|
last_fraction_loss_ != last_logged_fraction_loss_ ||
|
|
|
|
|
now_ms - last_rtc_event_log_ms_ > kRtcEventLogPeriodMs) {
|
|
|
|
|
event_log_->LogLossBasedBweUpdate(bitrate_bps, last_fraction_loss_,
|
|
|
|
|
expected_packets_since_last_loss_update_);
|
|
|
|
|
last_logged_fraction_loss_ = last_fraction_loss_;
|
|
|
|
|
last_rtc_event_log_ms_ = now_ms;
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
2017-04-18 06:55:32 -07:00
|
|
|
current_bitrate_bps_ = bitrate_bps;
|
2012-04-19 12:13:52 +00:00
|
|
|
}
|
|
|
|
|
} // namespace webrtc
|