mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 21:30:45 +01:00
Update VirtualSocketServer to use more std::unique_ptr
And delete RecursiveCriticalSection delay_crit_. Bug: webrtc:11567 Change-Id: I70826fba3806e5d7525d6288be3d83eb43cc5fe6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206469 Reviewed-by: Tommi <tommi@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Commit-Queue: Niels Moller <nisse@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33228}
This commit is contained in:
parent
ed8abad192
commit
983627c88d
3 changed files with 34 additions and 41 deletions
|
@ -1019,13 +1019,7 @@ void PrintFunction(std::vector<std::pair<double, double> >* f) {
|
|||
#endif // <unused>
|
||||
|
||||
void VirtualSocketServer::UpdateDelayDistribution() {
|
||||
Function* dist =
|
||||
CreateDistribution(delay_mean_, delay_stddev_, delay_samples_);
|
||||
// We take a lock just to make sure we don't leak memory.
|
||||
{
|
||||
CritScope cs(&delay_crit_);
|
||||
delay_dist_.reset(dist);
|
||||
}
|
||||
delay_dist_ = CreateDistribution(delay_mean_, delay_stddev_, delay_samples_);
|
||||
}
|
||||
|
||||
static double PI = 4 * atan(1.0);
|
||||
|
@ -1044,11 +1038,11 @@ static double Pareto(double x, double min, double k) {
|
|||
}
|
||||
#endif
|
||||
|
||||
VirtualSocketServer::Function* VirtualSocketServer::CreateDistribution(
|
||||
uint32_t mean,
|
||||
uint32_t stddev,
|
||||
uint32_t samples) {
|
||||
Function* f = new Function();
|
||||
std::unique_ptr<VirtualSocketServer::Function>
|
||||
VirtualSocketServer::CreateDistribution(uint32_t mean,
|
||||
uint32_t stddev,
|
||||
uint32_t samples) {
|
||||
auto f = std::make_unique<Function>();
|
||||
|
||||
if (0 == stddev) {
|
||||
f->push_back(Point(mean, 1.0));
|
||||
|
@ -1064,7 +1058,7 @@ VirtualSocketServer::Function* VirtualSocketServer::CreateDistribution(
|
|||
f->push_back(Point(x, y));
|
||||
}
|
||||
}
|
||||
return Resample(Invert(Accumulate(f)), 0, 1, samples);
|
||||
return Resample(Invert(Accumulate(std::move(f))), 0, 1, samples);
|
||||
}
|
||||
|
||||
uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) {
|
||||
|
@ -1093,7 +1087,8 @@ struct FunctionDomainCmp {
|
|||
}
|
||||
};
|
||||
|
||||
VirtualSocketServer::Function* VirtualSocketServer::Accumulate(Function* f) {
|
||||
std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate(
|
||||
std::unique_ptr<Function> f) {
|
||||
RTC_DCHECK(f->size() >= 1);
|
||||
double v = 0;
|
||||
for (Function::size_type i = 0; i < f->size() - 1; ++i) {
|
||||
|
@ -1106,7 +1101,8 @@ VirtualSocketServer::Function* VirtualSocketServer::Accumulate(Function* f) {
|
|||
return f;
|
||||
}
|
||||
|
||||
VirtualSocketServer::Function* VirtualSocketServer::Invert(Function* f) {
|
||||
std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert(
|
||||
std::unique_ptr<Function> f) {
|
||||
for (Function::size_type i = 0; i < f->size(); ++i)
|
||||
std::swap((*f)[i].first, (*f)[i].second);
|
||||
|
||||
|
@ -1114,24 +1110,25 @@ VirtualSocketServer::Function* VirtualSocketServer::Invert(Function* f) {
|
|||
return f;
|
||||
}
|
||||
|
||||
VirtualSocketServer::Function* VirtualSocketServer::Resample(Function* f,
|
||||
double x1,
|
||||
double x2,
|
||||
uint32_t samples) {
|
||||
Function* g = new Function();
|
||||
std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample(
|
||||
std::unique_ptr<Function> f,
|
||||
double x1,
|
||||
double x2,
|
||||
uint32_t samples) {
|
||||
auto g = std::make_unique<Function>();
|
||||
|
||||
for (size_t i = 0; i < samples; i++) {
|
||||
double x = x1 + (x2 - x1) * i / (samples - 1);
|
||||
double y = Evaluate(f, x);
|
||||
double y = Evaluate(f.get(), x);
|
||||
g->push_back(Point(x, y));
|
||||
}
|
||||
|
||||
delete f;
|
||||
return g;
|
||||
}
|
||||
|
||||
double VirtualSocketServer::Evaluate(Function* f, double x) {
|
||||
Function::iterator iter = absl::c_lower_bound(*f, x, FunctionDomainCmp());
|
||||
double VirtualSocketServer::Evaluate(const Function* f, double x) {
|
||||
Function::const_iterator iter =
|
||||
absl::c_lower_bound(*f, x, FunctionDomainCmp());
|
||||
if (iter == f->begin()) {
|
||||
return (*f)[0].second;
|
||||
} else if (iter == f->end()) {
|
||||
|
|
|
@ -130,9 +130,9 @@ class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
|
|||
typedef std::pair<double, double> Point;
|
||||
typedef std::vector<Point> Function;
|
||||
|
||||
static Function* CreateDistribution(uint32_t mean,
|
||||
uint32_t stddev,
|
||||
uint32_t samples);
|
||||
static std::unique_ptr<Function> CreateDistribution(uint32_t mean,
|
||||
uint32_t stddev,
|
||||
uint32_t samples);
|
||||
|
||||
// Similar to Thread::ProcessMessages, but it only processes messages until
|
||||
// there are no immediate messages or pending network traffic. Returns false
|
||||
|
@ -221,15 +221,14 @@ class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
|
|||
// appropriate distribution.
|
||||
uint32_t GetTransitDelay(Socket* socket);
|
||||
|
||||
// Basic operations on functions. Those that return a function also take
|
||||
// ownership of the function given (and hence, may modify or delete it).
|
||||
static Function* Accumulate(Function* f);
|
||||
static Function* Invert(Function* f);
|
||||
static Function* Resample(Function* f,
|
||||
double x1,
|
||||
double x2,
|
||||
uint32_t samples);
|
||||
static double Evaluate(Function* f, double x);
|
||||
// Basic operations on functions.
|
||||
static std::unique_ptr<Function> Accumulate(std::unique_ptr<Function> f);
|
||||
static std::unique_ptr<Function> Invert(std::unique_ptr<Function> f);
|
||||
static std::unique_ptr<Function> Resample(std::unique_ptr<Function> f,
|
||||
double x1,
|
||||
double x2,
|
||||
uint32_t samples);
|
||||
static double Evaluate(const Function* f, double x);
|
||||
|
||||
// Null out our message queue if it goes away. Necessary in the case where
|
||||
// our lifetime is greater than that of the thread we are using, since we
|
||||
|
@ -295,8 +294,6 @@ class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> {
|
|||
std::map<rtc::IPAddress, rtc::IPAddress> alternative_address_mapping_;
|
||||
std::unique_ptr<Function> delay_dist_;
|
||||
|
||||
RecursiveCriticalSection delay_crit_;
|
||||
|
||||
double drop_prob_;
|
||||
bool sending_blocked_ = false;
|
||||
RTC_DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer);
|
||||
|
|
|
@ -1117,10 +1117,10 @@ TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) {
|
|||
ASSERT_LT(0u, kTestSamples[sidx]);
|
||||
const uint32_t kStdDev =
|
||||
static_cast<uint32_t>(kTestDev[didx] * kTestMean[midx]);
|
||||
VirtualSocketServer::Function* f =
|
||||
std::unique_ptr<VirtualSocketServer::Function> f =
|
||||
VirtualSocketServer::CreateDistribution(kTestMean[midx], kStdDev,
|
||||
kTestSamples[sidx]);
|
||||
ASSERT_TRUE(nullptr != f);
|
||||
ASSERT_TRUE(nullptr != f.get());
|
||||
ASSERT_EQ(kTestSamples[sidx], f->size());
|
||||
double sum = 0;
|
||||
for (uint32_t i = 0; i < f->size(); ++i) {
|
||||
|
@ -1139,7 +1139,6 @@ TEST_F(VirtualSocketServerTest, CreatesStandardDistribution) {
|
|||
EXPECT_NEAR(kStdDev, stddev, 0.1 * kStdDev)
|
||||
<< "M=" << kTestMean[midx] << " SD=" << kStdDev
|
||||
<< " N=" << kTestSamples[sidx];
|
||||
delete f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue