Apply Chromium C++ style to RemoteRateControl.

BUG=

Review URL: https://webrtc-codereview.appspot.com/1329004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3919 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
solenberg@webrtc.org
2013-04-30 08:33:46 +00:00
parent 15e32ccd30
commit 8efc623fc2
2 changed files with 342 additions and 474 deletions

View File

@@ -10,89 +10,52 @@
#include "modules/remote_bitrate_estimator/remote_rate_control.h" #include "modules/remote_bitrate_estimator/remote_rate_control.h"
#include <assert.h> #include <algorithm>
#include <math.h> #include <cassert>
#include <string.h> #include <cmath>
#if _WIN32 #include <cstring>
#include <windows.h>
#endif
#include "system_wrappers/interface/trace.h" #include "system_wrappers/interface/trace.h"
#ifdef MATLAB
extern MatlabEngine eng; // global variable defined elsewhere
#endif
namespace webrtc { namespace webrtc {
const unsigned int kDefaultRttMs = 200; const unsigned int kDefaultRttMs = 200;
RemoteRateControl::RemoteRateControl() RemoteRateControl::RemoteRateControl()
: : min_configured_bit_rate_(30000),
_minConfiguredBitRate(30000), max_configured_bit_rate_(30000000),
_maxConfiguredBitRate(30000000), current_bit_rate_(max_configured_bit_rate_),
_currentBitRate(_maxConfiguredBitRate), max_hold_rate_(0),
_maxHoldRate(0), avg_max_bit_rate_(-1.0f),
_avgMaxBitRate(-1.0f), var_max_bit_rate_(0.4f),
_varMaxBitRate(0.4f), rate_control_state_(kRcHold),
_rcState(kRcHold), came_from_state_(kRcDecrease),
_cameFromState(kRcDecrease), rate_control_region_(kRcMaxUnknown),
_rcRegion(kRcMaxUnknown), last_bit_rate_change_(-1),
_lastBitRateChange(-1), current_input_(kBwNormal, 0, 1.0),
_currentInput(kBwNormal, 0, 1.0), updated_(false),
_updated(false), time_first_incoming_estimate_(-1),
_timeFirstIncomingEstimate(-1), initialized_bit_rate_(false),
_initializedBitRate(false), avg_change_period_(1000.0f),
_avgChangePeriod(1000.0f), last_change_ms_(-1),
_lastChangeMs(-1), beta_(0.9f),
_beta(0.9f), rtt_(kDefaultRttMs)
_rtt(kDefaultRttMs)
#ifdef MATLAB
,_plot1(NULL),
_plot2(NULL)
#endif
{ {
} }
RemoteRateControl::~RemoteRateControl() void RemoteRateControl::Reset() {
{ *this = RemoteRateControl();
#ifdef MATLAB came_from_state_ = kRcHold;
eng.DeletePlot(_plot1);
eng.DeletePlot(_plot2);
#endif
}
void RemoteRateControl::Reset()
{
_minConfiguredBitRate = 30000;
_maxConfiguredBitRate = 30000000;
_currentBitRate = _maxConfiguredBitRate;
_maxHoldRate = 0;
_avgMaxBitRate = -1.0f;
_varMaxBitRate = 0.4f;
_rcState = kRcHold;
_cameFromState = kRcHold;
_rcRegion = kRcMaxUnknown;
_lastBitRateChange = -1;
_avgChangePeriod = 1000.0f;
_lastChangeMs = -1;
_beta = 0.9f;
_currentInput._bwState = kBwNormal;
_currentInput._incomingBitRate = 0;
_currentInput._noiseVar = 1.0;
_updated = false;
_timeFirstIncomingEstimate = -1;
_initializedBitRate = false;
} }
bool RemoteRateControl::ValidEstimate() const { bool RemoteRateControl::ValidEstimate() const {
return _initializedBitRate; return initialized_bit_rate_;
} }
bool RemoteRateControl::TimeToReduceFurther( bool RemoteRateControl::TimeToReduceFurther(int64_t time_now,
int64_t time_now, unsigned int incoming_bitrate) const { unsigned int incoming_bitrate) const {
const int bitrate_reduction_interval = BWE_MAX(BWE_MIN(_rtt, 200), 10); const int bitrate_reduction_interval = std::max(std::min(rtt_, 200u), 10u);
if (time_now - _lastBitRateChange >= bitrate_reduction_interval) { if (time_now - last_bit_rate_change_ >= bitrate_reduction_interval) {
return true; return true;
} }
if (ValidEstimate()) { if (ValidEstimate()) {
@@ -103,226 +66,173 @@ bool RemoteRateControl::TimeToReduceFurther(
return false; return false;
} }
int32_t RemoteRateControl::SetConfiguredBitRates( int32_t RemoteRateControl::SetConfiguredBitRates(uint32_t min_bit_rate_bps,
uint32_t minBitRateBps, uint32_t maxBitRateBps) uint32_t max_bit_rate_bps) {
{ if (min_bit_rate_bps > max_bit_rate_bps) {
if (minBitRateBps > maxBitRateBps)
{
return -1; return -1;
} }
_minConfiguredBitRate = minBitRateBps; min_configured_bit_rate_ = min_bit_rate_bps;
_maxConfiguredBitRate = maxBitRateBps; max_configured_bit_rate_ = max_bit_rate_bps;
_currentBitRate = BWE_MIN(BWE_MAX(minBitRateBps, _currentBitRate), current_bit_rate_ = std::min(std::max(min_bit_rate_bps, current_bit_rate_),
maxBitRateBps); max_bit_rate_bps);
return 0; return 0;
} }
uint32_t RemoteRateControl::LatestEstimate() const { uint32_t RemoteRateControl::LatestEstimate() const {
return _currentBitRate; return current_bit_rate_;
} }
uint32_t RemoteRateControl::UpdateBandwidthEstimate(int64_t nowMS) uint32_t RemoteRateControl::UpdateBandwidthEstimate(int64_t now_ms) {
{ current_bit_rate_ = ChangeBitRate(current_bit_rate_,
_currentBitRate = ChangeBitRate(_currentBitRate, current_input_._incomingBitRate,
_currentInput._incomingBitRate, current_input_._noiseVar,
_currentInput._noiseVar, now_ms);
nowMS); return current_bit_rate_;
return _currentBitRate;
} }
void RemoteRateControl::SetRtt(unsigned int rtt) { void RemoteRateControl::SetRtt(unsigned int rtt) {
_rtt = rtt; rtt_ = rtt;
} }
RateControlRegion RemoteRateControl::Update(const RateControlInput* input, RateControlRegion RemoteRateControl::Update(const RateControlInput* input,
int64_t nowMS) int64_t now_ms) {
{
assert(input); assert(input);
#ifdef MATLAB
// Create plots
if (_plot1 == NULL)
{
_plot1 = eng.NewPlot(new MatlabPlot());
_plot1->AddTimeLine(30, "b", "current");
_plot1->AddTimeLine(30, "r-", "avgMax");
_plot1->AddTimeLine(30, "r--", "pStdMax");
_plot1->AddTimeLine(30, "r--", "nStdMax");
_plot1->AddTimeLine(30, "r+", "max");
_plot1->AddTimeLine(30, "g", "incoming");
_plot1->AddTimeLine(30, "b+", "recovery");
}
if (_plot2 == NULL)
{
_plot2 = eng.NewPlot(new MatlabPlot());
_plot2->AddTimeLine(30, "b", "alpha");
}
#endif
// Set the initial bit rate value to what we're receiving the first half // Set the initial bit rate value to what we're receiving the first half
// second. // second.
if (!_initializedBitRate) if (!initialized_bit_rate_) {
{ if (time_first_incoming_estimate_ < 0) {
if (_timeFirstIncomingEstimate < 0) if (input->_incomingBitRate > 0) {
{ time_first_incoming_estimate_ = now_ms;
if (input->_incomingBitRate > 0)
{
_timeFirstIncomingEstimate = nowMS;
} }
} } else if (now_ms - time_first_incoming_estimate_ > 500 &&
else if (nowMS - _timeFirstIncomingEstimate > 500 && input->_incomingBitRate > 0) {
input->_incomingBitRate > 0) current_bit_rate_ = input->_incomingBitRate;
{ initialized_bit_rate_ = true;
_currentBitRate = input->_incomingBitRate;
_initializedBitRate = true;
} }
} }
if (_updated && _currentInput._bwState == kBwOverusing) if (updated_ && current_input_._bwState == kBwOverusing) {
{ // Only update delay factor and incoming bit rate. We always want to react
// Only update delay factor and incoming bit rate. We always want to react on an over-use. // on an over-use.
_currentInput._noiseVar = input->_noiseVar; current_input_._noiseVar = input->_noiseVar;
_currentInput._incomingBitRate = input->_incomingBitRate; current_input_._incomingBitRate = input->_incomingBitRate;
return _rcRegion; return rate_control_region_;
} }
_updated = true; updated_ = true;
_currentInput = *input; current_input_ = *input;
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, "BWE: Incoming rate = %u kbps", input->_incomingBitRate/1000); WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, "BWE: Incoming rate = %u kbps",
return _rcRegion; input->_incomingBitRate/1000);
return rate_control_region_;
} }
uint32_t RemoteRateControl::ChangeBitRate(uint32_t currentBitRate, uint32_t RemoteRateControl::ChangeBitRate(uint32_t current_bit_rate,
uint32_t incomingBitRate, uint32_t incoming_bit_rate,
double noiseVar, double noise_var,
int64_t nowMS) int64_t now_ms) {
{ if (!updated_) {
if (!_updated) return current_bit_rate_;
{
return _currentBitRate;
} }
_updated = false; updated_ = false;
UpdateChangePeriod(nowMS); UpdateChangePeriod(now_ms);
ChangeState(_currentInput, nowMS); ChangeState(current_input_, now_ms);
// calculated here because it's used in multiple places // calculated here because it's used in multiple places
const float incomingBitRateKbps = incomingBitRate / 1000.0f; const float incoming_bit_rate_kbps = incoming_bit_rate / 1000.0f;
// Calculate the max bit rate std dev given the normalized // Calculate the max bit rate std dev given the normalized
// variance and the current incoming bit rate. // variance and the current incoming bit rate.
const float stdMaxBitRate = sqrt(_varMaxBitRate * _avgMaxBitRate); const float std_max_bit_rate = sqrt(var_max_bit_rate_ * avg_max_bit_rate_);
bool recovery = false; bool recovery = false;
switch (_rcState) switch (rate_control_state_) {
{ case kRcHold: {
case kRcHold: max_hold_rate_ = std::max(max_hold_rate_, incoming_bit_rate);
{
_maxHoldRate = BWE_MAX(_maxHoldRate, incomingBitRate);
break; break;
} }
case kRcIncrease: case kRcIncrease: {
{ if (avg_max_bit_rate_ >= 0) {
if (_avgMaxBitRate >= 0) if (incoming_bit_rate_kbps > avg_max_bit_rate_ + 3 * std_max_bit_rate) {
{
if (incomingBitRateKbps > _avgMaxBitRate + 3 * stdMaxBitRate)
{
ChangeRegion(kRcMaxUnknown); ChangeRegion(kRcMaxUnknown);
_avgMaxBitRate = -1.0; avg_max_bit_rate_ = -1.0;
} } else if (incoming_bit_rate_kbps > avg_max_bit_rate_ + 2.5 *
else if (incomingBitRateKbps > _avgMaxBitRate + 2.5 * stdMaxBitRate) std_max_bit_rate) {
{
ChangeRegion(kRcAboveMax); ChangeRegion(kRcAboveMax);
} }
} }
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1,
"BWE: Response time: %f + %i + 10*33\n", "BWE: Response time: %f + %i + 10*33\n",
_avgChangePeriod, _rtt); avg_change_period_, rtt_);
const uint32_t responseTime = static_cast<uint32_t>(_avgChangePeriod + 0.5f) + _rtt + 300; const uint32_t response_time = static_cast<uint32_t>(avg_change_period_ +
double alpha = RateIncreaseFactor(nowMS, _lastBitRateChange, 0.5f) + rtt_ + 300;
responseTime, noiseVar); double alpha = RateIncreaseFactor(now_ms, last_bit_rate_change_,
response_time, noise_var);
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1,
"BWE: _avgChangePeriod = %f ms; RTT = %u ms", _avgChangePeriod, _rtt); "BWE: avg_change_period_ = %f ms; RTT = %u ms", avg_change_period_,
rtt_);
currentBitRate = static_cast<uint32_t>(currentBitRate * alpha) + 1000; current_bit_rate = static_cast<uint32_t>(current_bit_rate * alpha) + 1000;
if (_maxHoldRate > 0 && _beta * _maxHoldRate > currentBitRate) if (max_hold_rate_ > 0 && beta_ * max_hold_rate_ > current_bit_rate) {
{ current_bit_rate = static_cast<uint32_t>(beta_ * max_hold_rate_);
currentBitRate = static_cast<uint32_t>(_beta * _maxHoldRate); avg_max_bit_rate_ = beta_ * max_hold_rate_ / 1000.0f;
_avgMaxBitRate = _beta * _maxHoldRate / 1000.0f;
ChangeRegion(kRcNearMax); ChangeRegion(kRcNearMax);
recovery = true; recovery = true;
#ifdef MATLAB
_plot1->Append("recovery", _maxHoldRate/1000);
#endif
} }
_maxHoldRate = 0; max_hold_rate_ = 0;
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1,
"BWE: Increase rate to currentBitRate = %u kbps", currentBitRate/1000); "BWE: Increase rate to current_bit_rate = %u kbps",
_lastBitRateChange = nowMS; current_bit_rate / 1000);
last_bit_rate_change_ = now_ms;
break; break;
} }
case kRcDecrease: case kRcDecrease: {
{ if (incoming_bit_rate < min_configured_bit_rate_) {
if (incomingBitRate < _minConfiguredBitRate) current_bit_rate = min_configured_bit_rate_;
{ } else {
currentBitRate = _minConfiguredBitRate;
}
else
{
// Set bit rate to something slightly lower than max // Set bit rate to something slightly lower than max
// to get rid of any self-induced delay. // to get rid of any self-induced delay.
currentBitRate = static_cast<uint32_t>(_beta * incomingBitRate + 0.5); current_bit_rate = static_cast<uint32_t>(beta_ * incoming_bit_rate +
if (currentBitRate > _currentBitRate) 0.5);
{ if (current_bit_rate > current_bit_rate_) {
// Avoid increasing the rate when over-using. // Avoid increasing the rate when over-using.
if (_rcRegion != kRcMaxUnknown) if (rate_control_region_ != kRcMaxUnknown) {
{ current_bit_rate = static_cast<uint32_t>(beta_ * avg_max_bit_rate_ *
currentBitRate = static_cast<uint32_t>(_beta * _avgMaxBitRate * 1000 + 0.5f); 1000 + 0.5f);
} }
currentBitRate = BWE_MIN(currentBitRate, _currentBitRate); current_bit_rate = std::min(current_bit_rate, current_bit_rate_);
} }
ChangeRegion(kRcNearMax); ChangeRegion(kRcNearMax);
if (incomingBitRateKbps < _avgMaxBitRate - 3 * stdMaxBitRate) if (incoming_bit_rate_kbps < avg_max_bit_rate_ - 3 * std_max_bit_rate) {
{ avg_max_bit_rate_ = -1.0f;
_avgMaxBitRate = -1.0f;
} }
UpdateMaxBitRateEstimate(incomingBitRateKbps); UpdateMaxBitRateEstimate(incoming_bit_rate_kbps);
#ifdef MATLAB WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1,
_plot1->Append("max", incomingBitRateKbps); "BWE: Decrease rate to current_bit_rate = %u kbps",
#endif current_bit_rate / 1000);
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, "BWE: Decrease rate to currentBitRate = %u kbps", currentBitRate/1000);
} }
// Stay on hold until the pipes are cleared. // Stay on hold until the pipes are cleared.
ChangeState(kRcHold); ChangeState(kRcHold);
_lastBitRateChange = nowMS; last_bit_rate_change_ = now_ms;
break; break;
} }
default:
assert(false);
} }
if (!recovery && (incomingBitRate > 100000 || currentBitRate > 150000) && if (!recovery && (incoming_bit_rate > 100000 || current_bit_rate > 150000) &&
currentBitRate > 1.5 * incomingBitRate) current_bit_rate > 1.5 * incoming_bit_rate) {
{
// Allow changing the bit rate if we are operating at very low rates // Allow changing the bit rate if we are operating at very low rates
// Don't change the bit rate if the send side is too far off // Don't change the bit rate if the send side is too far off
currentBitRate = _currentBitRate; current_bit_rate = current_bit_rate_;
_lastBitRateChange = nowMS; last_bit_rate_change_ = now_ms;
} }
#ifdef MATLAB return current_bit_rate;
if (_avgMaxBitRate >= 0.0f)
{
_plot1->Append("avgMax", _avgMaxBitRate);
_plot1->Append("pStdMax", _avgMaxBitRate + 3*stdMaxBitRate);
_plot1->Append("nStdMax", _avgMaxBitRate - 3*stdMaxBitRate);
}
_plot1->Append("incoming", incomingBitRate/1000);
_plot1->Append("current", currentBitRate/1000);
_plot1->Plot();
#endif
return currentBitRate;
} }
double RemoteRateControl::RateIncreaseFactor(int64_t nowMs, int64_t lastMs, uint32_t reactionTimeMs, double noiseVar) const double RemoteRateControl::RateIncreaseFactor(int64_t now_ms,
{ int64_t last_ms,
uint32_t reaction_time_ms,
double noise_var) const {
// alpha = 1.02 + B ./ (1 + exp(b*(tr - (c1*s2 + c2)))) // alpha = 1.02 + B ./ (1 + exp(b*(tr - (c1*s2 + c2))))
// Parameters // Parameters
const double B = 0.0407; const double B = 0.0407;
@@ -331,151 +241,117 @@ double RemoteRateControl::RateIncreaseFactor(int64_t nowMs, int64_t lastMs, uint
const double c2 = 800.0; const double c2 = 800.0;
const double d = 0.85; const double d = 0.85;
double alpha = 1.005 + B / (1 + exp( b * (d * reactionTimeMs - (c1 * noiseVar + c2)))); double alpha = 1.005 + B / (1 + exp( b * (d * reaction_time_ms -
(c1 * noise_var + c2))));
if (alpha < 1.005) if (alpha < 1.005) {
{
alpha = 1.005; alpha = 1.005;
} } else if (alpha > 1.3) {
else if (alpha > 1.3)
{
alpha = 1.3; alpha = 1.3;
} }
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, "BWE: alpha = %f", alpha);
"BWE: alpha = %f", alpha);
#ifdef MATLAB
_plot2->Append("alpha", alpha);
_plot2->Plot();
#endif
if (lastMs > -1) if (last_ms > -1) {
{ alpha = pow(alpha, (now_ms - last_ms) / 1000.0);
alpha = pow(alpha, (nowMs - lastMs) / 1000.0);
} }
if (_rcRegion == kRcNearMax) if (rate_control_region_ == kRcNearMax) {
{
// We're close to our previous maximum. Try to stabilize the // We're close to our previous maximum. Try to stabilize the
// bit rate in this region, by increasing in smaller steps. // bit rate in this region, by increasing in smaller steps.
alpha = alpha - (alpha - 1.0) / 2.0; alpha = alpha - (alpha - 1.0) / 2.0;
} } else if (rate_control_region_ == kRcMaxUnknown) {
else if (_rcRegion == kRcMaxUnknown)
{
alpha = alpha + (alpha - 1.0) * 2.0; alpha = alpha + (alpha - 1.0) * 2.0;
} }
return alpha; return alpha;
} }
void RemoteRateControl::UpdateChangePeriod(int64_t nowMs) void RemoteRateControl::UpdateChangePeriod(int64_t now_ms) {
{
int64_t changePeriod = 0; int64_t changePeriod = 0;
if (_lastChangeMs > -1) if (last_change_ms_ > -1) {
{ changePeriod = now_ms - last_change_ms_;
changePeriod = nowMs - _lastChangeMs;
} }
_lastChangeMs = nowMs; last_change_ms_ = now_ms;
_avgChangePeriod = 0.9f * _avgChangePeriod + 0.1f * changePeriod; avg_change_period_ = 0.9f * avg_change_period_ + 0.1f * changePeriod;
} }
void RemoteRateControl::UpdateMaxBitRateEstimate(float incomingBitRateKbps) void RemoteRateControl::UpdateMaxBitRateEstimate(float incoming_bit_rate_kbps) {
{
const float alpha = 0.05f; const float alpha = 0.05f;
if (_avgMaxBitRate == -1.0f) if (avg_max_bit_rate_ == -1.0f) {
{ avg_max_bit_rate_ = incoming_bit_rate_kbps;
_avgMaxBitRate = incomingBitRateKbps; } else {
} avg_max_bit_rate_ = (1 - alpha) * avg_max_bit_rate_ +
else alpha * incoming_bit_rate_kbps;
{
_avgMaxBitRate = (1 - alpha) * _avgMaxBitRate +
alpha * incomingBitRateKbps;
} }
// Estimate the max bit rate variance and normalize the variance // Estimate the max bit rate variance and normalize the variance
// with the average max bit rate. // with the average max bit rate.
const float norm = BWE_MAX(_avgMaxBitRate, 1.0f); const float norm = std::max(avg_max_bit_rate_, 1.0f);
_varMaxBitRate = (1 - alpha) * _varMaxBitRate + var_max_bit_rate_ = (1 - alpha) * var_max_bit_rate_ +
alpha * (_avgMaxBitRate - incomingBitRateKbps) * alpha * (avg_max_bit_rate_ - incoming_bit_rate_kbps) *
(_avgMaxBitRate - incomingBitRateKbps) / (avg_max_bit_rate_ - incoming_bit_rate_kbps) / norm;
norm;
// 0.4 ~= 14 kbit/s at 500 kbit/s // 0.4 ~= 14 kbit/s at 500 kbit/s
if (_varMaxBitRate < 0.4f) if (var_max_bit_rate_ < 0.4f) {
{ var_max_bit_rate_ = 0.4f;
_varMaxBitRate = 0.4f;
} }
// 2.5f ~= 35 kbit/s at 500 kbit/s // 2.5f ~= 35 kbit/s at 500 kbit/s
if (_varMaxBitRate > 2.5f) if (var_max_bit_rate_ > 2.5f) {
{ var_max_bit_rate_ = 2.5f;
_varMaxBitRate = 2.5f;
} }
} }
void RemoteRateControl::ChangeState(const RateControlInput& input, int64_t nowMs) void RemoteRateControl::ChangeState(const RateControlInput& input,
{ int64_t now_ms) {
switch (_currentInput._bwState) switch (current_input_._bwState) {
{
case kBwNormal: case kBwNormal:
{ if (rate_control_state_ == kRcHold) {
if (_rcState == kRcHold) last_bit_rate_change_ = now_ms;
{
_lastBitRateChange = nowMs;
ChangeState(kRcIncrease); ChangeState(kRcIncrease);
} }
break; break;
}
case kBwOverusing: case kBwOverusing:
{ if (rate_control_state_ != kRcDecrease) {
if (_rcState != kRcDecrease)
{
ChangeState(kRcDecrease); ChangeState(kRcDecrease);
} }
break; break;
}
case kBwUnderusing: case kBwUnderusing:
{
ChangeState(kRcHold); ChangeState(kRcHold);
break; break;
} default:
assert(false);
} }
} }
void RemoteRateControl::ChangeRegion(RateControlRegion region) void RemoteRateControl::ChangeRegion(RateControlRegion region) {
{ rate_control_region_ = region;
_rcRegion = region; switch (rate_control_region_) {
switch (_rcRegion)
{
case kRcAboveMax: case kRcAboveMax:
case kRcMaxUnknown: case kRcMaxUnknown:
{ beta_ = 0.9f;
_beta = 0.9f;
break; break;
}
case kRcNearMax: case kRcNearMax:
{ beta_ = 0.95f;
_beta = 0.95f;
break; break;
} default:
assert(false);
} }
} }
void RemoteRateControl::ChangeState(RateControlState newState) void RemoteRateControl::ChangeState(RateControlState new_state) {
{ came_from_state_ = rate_control_state_;
_cameFromState = _rcState; rate_control_state_ = new_state;
_rcState = newState;
char state1[15]; char state1[15];
char state2[15]; char state2[15];
char state3[15]; char state3[15];
StateStr(_cameFromState, state1); StateStr(came_from_state_, state1);
StateStr(_rcState, state2); StateStr(rate_control_state_, state2);
StateStr(_currentInput._bwState, state3); StateStr(current_input_._bwState, state3);
WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1, WEBRTC_TRACE(kTraceStream, kTraceRtpRtcp, -1,
"\t%s => %s due to %s\n", state1, state2, state3); "\t%s => %s due to %s\n", state1, state2, state3);
} }
void RemoteRateControl::StateStr(RateControlState state, char* str) void RemoteRateControl::StateStr(RateControlState state, char* str) {
{ switch (state) {
switch (state)
{
case kRcDecrease: case kRcDecrease:
strncpy(str, "DECREASE", 9); strncpy(str, "DECREASE", 9);
break; break;
@@ -485,13 +361,13 @@ void RemoteRateControl::StateStr(RateControlState state, char* str)
case kRcIncrease: case kRcIncrease:
strncpy(str, "INCREASE", 9); strncpy(str, "INCREASE", 9);
break; break;
default:
assert(false);
} }
} }
void RemoteRateControl::StateStr(BandwidthUsage state, char* str) void RemoteRateControl::StateStr(BandwidthUsage state, char* str) {
{ switch (state) {
switch (state)
{
case kBwNormal: case kBwNormal:
strncpy(str, "NORMAL", 7); strncpy(str, "NORMAL", 7);
break; break;
@@ -501,7 +377,8 @@ void RemoteRateControl::StateStr(BandwidthUsage state, char* str)
case kBwUnderusing: case kBwUnderusing:
strncpy(str, "UNDER USING", 12); strncpy(str, "UNDER USING", 12);
break; break;
default:
assert(false);
} }
} }
} // namespace webrtc } // namespace webrtc

View File

@@ -12,30 +12,20 @@
#define WEBRTC_MODULES_RTP_RTCP_SOURCE_REMOTE_RATE_CONTROL_H_ #define WEBRTC_MODULES_RTP_RTCP_SOURCE_REMOTE_RATE_CONTROL_H_
#include "modules/remote_bitrate_estimator/include/bwe_defines.h" #include "modules/remote_bitrate_estimator/include/bwe_defines.h"
#include "typedefs.h"
#ifdef MATLAB
#include "../test/BWEStandAlone/MatlabPlot.h"
#endif
namespace webrtc { namespace webrtc {
class RemoteRateControl
{ class RemoteRateControl {
public: public:
RemoteRateControl(); RemoteRateControl();
~RemoteRateControl(); ~RemoteRateControl() {}
int32_t SetConfiguredBitRates(uint32_t minBitRate,
uint32_t maxBitRate);
uint32_t LatestEstimate() const;
uint32_t UpdateBandwidthEstimate(int64_t nowMS);
void SetRtt(unsigned int rtt);
RateControlRegion Update(const RateControlInput* input,
int64_t nowMS);
void Reset(); void Reset();
// Returns true if there is a valid estimate of the incoming bitrate, false // Returns true if there is a valid estimate of the incoming bitrate, false
// otherwise. // otherwise.
bool ValidEstimate() const; bool ValidEstimate() const;
// Returns true if the bitrate estimate hasn't been changed for more than // Returns true if the bitrate estimate hasn't been changed for more than
// an RTT, or if the incoming_bitrate is more than 5% above the current // an RTT, or if the incoming_bitrate is more than 5% above the current
// estimate. Should be used to decide if we should reduce the rate further // estimate. Should be used to decide if we should reduce the rate further
@@ -43,46 +33,47 @@ public:
bool TimeToReduceFurther(int64_t time_now, bool TimeToReduceFurther(int64_t time_now,
unsigned int incoming_bitrate) const; unsigned int incoming_bitrate) const;
private: int32_t SetConfiguredBitRates(uint32_t min_bit_rate, uint32_t max_bit_rate);
uint32_t ChangeBitRate(uint32_t currentBitRate, uint32_t LatestEstimate() const;
uint32_t incomingBitRate, uint32_t UpdateBandwidthEstimate(int64_t now_ms);
double delayFactor, void SetRtt(unsigned int rtt);
int64_t nowMS); RateControlRegion Update(const RateControlInput* input, int64_t now_ms);
double RateIncreaseFactor(int64_t nowMs,
int64_t lastMs, private:
uint32_t reactionTimeMs, uint32_t ChangeBitRate(uint32_t current_bit_rate,
double noiseVar) const; uint32_t incoming_bit_rate,
void UpdateChangePeriod(int64_t nowMs); double delay_factor,
void UpdateMaxBitRateEstimate(float incomingBitRateKbps); int64_t now_ms);
void ChangeState(const RateControlInput& input, int64_t nowMs); double RateIncreaseFactor(int64_t now_ms,
void ChangeState(RateControlState newState); int64_t last_ms,
uint32_t reaction_time_ms,
double noise_var) const;
void UpdateChangePeriod(int64_t now_ms);
void UpdateMaxBitRateEstimate(float incoming_bit_rate_kbps);
void ChangeState(const RateControlInput& input, int64_t now_ms);
void ChangeState(RateControlState new_state);
void ChangeRegion(RateControlRegion region); void ChangeRegion(RateControlRegion region);
static void StateStr(RateControlState state, char* str); static void StateStr(RateControlState state, char* str);
static void StateStr(BandwidthUsage state, char* str); static void StateStr(BandwidthUsage state, char* str);
uint32_t _minConfiguredBitRate; uint32_t min_configured_bit_rate_;
uint32_t _maxConfiguredBitRate; uint32_t max_configured_bit_rate_;
uint32_t _currentBitRate; uint32_t current_bit_rate_;
uint32_t _maxHoldRate; uint32_t max_hold_rate_;
float _avgMaxBitRate; float avg_max_bit_rate_;
float _varMaxBitRate; float var_max_bit_rate_;
RateControlState _rcState; RateControlState rate_control_state_;
RateControlState _cameFromState; RateControlState came_from_state_;
RateControlRegion _rcRegion; RateControlRegion rate_control_region_;
int64_t _lastBitRateChange; int64_t last_bit_rate_change_;
RateControlInput _currentInput; RateControlInput current_input_;
bool _updated; bool updated_;
int64_t _timeFirstIncomingEstimate; int64_t time_first_incoming_estimate_;
bool _initializedBitRate; bool initialized_bit_rate_;
float avg_change_period_;
float _avgChangePeriod; int64_t last_change_ms_;
int64_t _lastChangeMs; float beta_;
float _beta; unsigned int rtt_;
unsigned int _rtt;
#ifdef MATLAB
MatlabPlot *_plot1;
MatlabPlot *_plot2;
#endif
}; };
} // namespace webrtc } // namespace webrtc