Reland "Move webrtc/{base => rtc_base}" (https://codereview.webrtc.org/2877023002)

Reland the base->rtc_base without adding stub headers (will be
done in follow-up CL). This preserves git blame history of all files.

BUG=webrtc:7634
NOTRY=True
TBR=kwiberg@webrtc.org

Change-Id: Iea3bb6f3f67b8374c96337b63e8f5aa3e6181012
Reviewed-on: https://chromium-review.googlesource.com/554611
Reviewed-by: Henrik Kjellander <kjellander@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#18821}
This commit is contained in:
Henrik Kjellander
2017-06-29 08:03:04 +02:00
parent ec78f1cebc
commit c03627683f
383 changed files with 1607 additions and 1467 deletions

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2011 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.
*/
#include "webrtc/base/numerics/exp_filter.h"
#include <math.h>
namespace rtc {
const float ExpFilter::kValueUndefined = -1.0f;
void ExpFilter::Reset(float alpha) {
alpha_ = alpha;
filtered_ = kValueUndefined;
}
float ExpFilter::Apply(float exp, float sample) {
if (filtered_ == kValueUndefined) {
// Initialize filtered value.
filtered_ = sample;
} else if (exp == 1.0) {
filtered_ = alpha_ * filtered_ + (1 - alpha_) * sample;
} else {
float alpha = pow(alpha_, exp);
filtered_ = alpha * filtered_ + (1 - alpha) * sample;
}
if (max_ != kValueUndefined && filtered_ > max_) {
filtered_ = max_;
}
return filtered_;
}
void ExpFilter::UpdateBase(float alpha) {
alpha_ = alpha;
}
} // namespace rtc

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2011 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.
*/
#ifndef WEBRTC_RTC_BASE_NUMERICS_EXP_FILTER_H_
#define WEBRTC_RTC_BASE_NUMERICS_EXP_FILTER_H_
namespace rtc {
// This class can be used, for example, for smoothing the result of bandwidth
// estimation and packet loss estimation.
class ExpFilter {
public:
static const float kValueUndefined;
explicit ExpFilter(float alpha, float max = kValueUndefined) : max_(max) {
Reset(alpha);
}
// Resets the filter to its initial state, and resets filter factor base to
// the given value |alpha|.
void Reset(float alpha);
// Applies the filter with a given exponent on the provided sample:
// y(k) = min(alpha_^ exp * y(k-1) + (1 - alpha_^ exp) * sample, max_).
float Apply(float exp, float sample);
// Returns current filtered value.
float filtered() const { return filtered_; }
// Changes the filter factor base to the given value |alpha|.
void UpdateBase(float alpha);
private:
float alpha_; // Filter factor base.
float filtered_; // Current filter output.
const float max_;
};
} // namespace rtc
#endif // WEBRTC_RTC_BASE_NUMERICS_EXP_FILTER_H_

View File

@ -0,0 +1,71 @@
/*
* Copyright 2014 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.
*/
#include <math.h>
#include "webrtc/base/numerics/exp_filter.h"
#include "webrtc/test/gtest.h"
namespace rtc {
TEST(ExpFilterTest, FirstTimeOutputEqualInput) {
// No max value defined.
ExpFilter filter = ExpFilter(0.9f);
filter.Apply(100.0f, 10.0f);
// First time, first argument no effect.
double value = 10.0f;
EXPECT_FLOAT_EQ(value, filter.filtered());
}
TEST(ExpFilterTest, SecondTime) {
double value;
ExpFilter filter = ExpFilter(0.9f);
filter.Apply(100.0f, 10.0f);
// First time, first argument no effect.
value = 10.0f;
filter.Apply(10.0f, 20.0f);
double alpha = pow(0.9f, 10.0f);
value = alpha * value + (1.0f - alpha) * 20.0f;
EXPECT_FLOAT_EQ(value, filter.filtered());
}
TEST(ExpFilterTest, Reset) {
ExpFilter filter = ExpFilter(0.9f);
filter.Apply(100.0f, 10.0f);
filter.Reset(0.8f);
filter.Apply(100.0f, 1.0f);
// Become first time after a reset.
double value = 1.0f;
EXPECT_FLOAT_EQ(value, filter.filtered());
}
TEST(ExpfilterTest, OutputLimitedByMax) {
double value;
// Max value defined.
ExpFilter filter = ExpFilter(0.9f, 1.0f);
filter.Apply(100.0f, 10.0f);
// Limited to max value.
value = 1.0f;
EXPECT_EQ(value, filter.filtered());
filter.Apply(1.0f, 0.0f);
value = 0.9f * value;
EXPECT_FLOAT_EQ(value, filter.filtered());
}
} // namespace rtc

View File

@ -0,0 +1,115 @@
/*
* Copyright (c) 2016 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.
*/
#ifndef WEBRTC_RTC_BASE_NUMERICS_PERCENTILE_FILTER_H_
#define WEBRTC_RTC_BASE_NUMERICS_PERCENTILE_FILTER_H_
#include <stdint.h>
#include <iterator>
#include <set>
#include "webrtc/base/checks.h"
namespace webrtc {
// Class to efficiently get the percentile value from a group of observations.
// The percentile is the value below which a given percentage of the
// observations fall.
template <typename T>
class PercentileFilter {
public:
// Construct filter. |percentile| should be between 0 and 1.
explicit PercentileFilter(float percentile);
// Insert one observation. The complexity of this operation is logarithmic in
// the size of the container.
void Insert(const T& value);
// Remove one observation or return false if |value| doesn't exist in the
// container. The complexity of this operation is logarithmic in the size of
// the container.
bool Erase(const T& value);
// Get the percentile value. The complexity of this operation is constant.
T GetPercentileValue() const;
private:
// Update iterator and index to point at target percentile value.
void UpdatePercentileIterator();
const float percentile_;
std::multiset<T> set_;
// Maintain iterator and index of current target percentile value.
typename std::multiset<T>::iterator percentile_it_;
int64_t percentile_index_;
};
template <typename T>
PercentileFilter<T>::PercentileFilter(float percentile)
: percentile_(percentile),
percentile_it_(set_.begin()),
percentile_index_(0) {
RTC_CHECK_GE(percentile, 0.0f);
RTC_CHECK_LE(percentile, 1.0f);
}
template <typename T>
void PercentileFilter<T>::Insert(const T& value) {
// Insert element at the upper bound.
set_.insert(value);
if (set_.size() == 1u) {
// First element inserted - initialize percentile iterator and index.
percentile_it_ = set_.begin();
percentile_index_ = 0;
} else if (value < *percentile_it_) {
// If new element is before us, increment |percentile_index_|.
++percentile_index_;
}
UpdatePercentileIterator();
}
template <typename T>
bool PercentileFilter<T>::Erase(const T& value) {
typename std::multiset<T>::const_iterator it = set_.lower_bound(value);
// Ignore erase operation if the element is not present in the current set.
if (it == set_.end() || *it != value)
return false;
if (it == percentile_it_) {
// If same iterator, update to the following element. Index is not
// affected.
percentile_it_ = set_.erase(it);
} else {
set_.erase(it);
// If erased element was before us, decrement |percentile_index_|.
if (value <= *percentile_it_)
--percentile_index_;
}
UpdatePercentileIterator();
return true;
}
template <typename T>
void PercentileFilter<T>::UpdatePercentileIterator() {
if (set_.empty())
return;
const int64_t index = static_cast<int64_t>(percentile_ * (set_.size() - 1));
std::advance(percentile_it_, index - percentile_index_);
percentile_index_ = index;
}
template <typename T>
T PercentileFilter<T>::GetPercentileValue() const {
return set_.empty() ? 0 : *percentile_it_;
}
} // namespace webrtc
#endif // WEBRTC_RTC_BASE_NUMERICS_PERCENTILE_FILTER_H_

View File

@ -0,0 +1,138 @@
/*
* Copyright 2016 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.
*/
#include <algorithm>
#include <climits>
#include "webrtc/base/constructormagic.h"
#include "webrtc/base/numerics/percentile_filter.h"
#include "webrtc/test/gtest.h"
namespace webrtc {
class PercentileFilterTest : public ::testing::TestWithParam<float> {
public:
PercentileFilterTest() : filter_(GetParam()) {
// Make sure the tests are deterministic by seeding with a constant.
srand(42);
}
protected:
PercentileFilter<int64_t> filter_;
private:
RTC_DISALLOW_COPY_AND_ASSIGN(PercentileFilterTest);
};
INSTANTIATE_TEST_CASE_P(PercentileFilterTests,
PercentileFilterTest,
::testing::Values(0.0f, 0.1f, 0.5f, 0.9f, 1.0f));
TEST(PercentileFilterTest, MinFilter) {
PercentileFilter<int64_t> filter(0.0f);
filter.Insert(4);
EXPECT_EQ(4, filter.GetPercentileValue());
filter.Insert(3);
EXPECT_EQ(3, filter.GetPercentileValue());
}
TEST(PercentileFilterTest, MaxFilter) {
PercentileFilter<int64_t> filter(1.0f);
filter.Insert(3);
EXPECT_EQ(3, filter.GetPercentileValue());
filter.Insert(4);
EXPECT_EQ(4, filter.GetPercentileValue());
}
TEST(PercentileFilterTest, MedianFilterDouble) {
PercentileFilter<double> filter(0.5f);
filter.Insert(2.71828);
filter.Insert(3.14159);
filter.Insert(1.41421);
EXPECT_EQ(2.71828, filter.GetPercentileValue());
}
TEST(PercentileFilterTest, MedianFilterInt) {
PercentileFilter<int> filter(0.5f);
filter.Insert(INT_MIN);
filter.Insert(1);
filter.Insert(2);
EXPECT_EQ(1, filter.GetPercentileValue());
filter.Insert(INT_MAX);
filter.Erase(INT_MIN);
EXPECT_EQ(2, filter.GetPercentileValue());
}
TEST(PercentileFilterTest, MedianFilterUnsigned) {
PercentileFilter<unsigned> filter(0.5f);
filter.Insert(UINT_MAX);
filter.Insert(2u);
filter.Insert(1u);
EXPECT_EQ(2u, filter.GetPercentileValue());
filter.Insert(0u);
filter.Erase(UINT_MAX);
EXPECT_EQ(1u, filter.GetPercentileValue());
}
TEST_P(PercentileFilterTest, EmptyFilter) {
EXPECT_EQ(0, filter_.GetPercentileValue());
filter_.Insert(3);
bool success = filter_.Erase(3);
EXPECT_TRUE(success);
EXPECT_EQ(0, filter_.GetPercentileValue());
}
TEST_P(PercentileFilterTest, EraseNonExistingElement) {
bool success = filter_.Erase(3);
EXPECT_FALSE(success);
EXPECT_EQ(0, filter_.GetPercentileValue());
filter_.Insert(4);
success = filter_.Erase(3);
EXPECT_FALSE(success);
EXPECT_EQ(4, filter_.GetPercentileValue());
}
TEST_P(PercentileFilterTest, DuplicateElements) {
filter_.Insert(3);
filter_.Insert(3);
filter_.Erase(3);
EXPECT_EQ(3, filter_.GetPercentileValue());
}
TEST_P(PercentileFilterTest, InsertAndEraseTenValuesInRandomOrder) {
int64_t zero_to_nine[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// The percentile value of the ten values above.
const int64_t expected_value = static_cast<int64_t>(GetParam() * 9);
// Insert two sets of |zero_to_nine| in random order.
for (int i = 0; i < 2; ++i) {
std::random_shuffle(zero_to_nine, zero_to_nine + 10);
for (int64_t value : zero_to_nine)
filter_.Insert(value);
// After inserting a full set of |zero_to_nine|, the percentile should
// stay constant.
EXPECT_EQ(expected_value, filter_.GetPercentileValue());
}
// Insert and erase sets of |zero_to_nine| in random order a few times.
for (int i = 0; i < 3; ++i) {
std::random_shuffle(zero_to_nine, zero_to_nine + 10);
for (int64_t value : zero_to_nine)
filter_.Erase(value);
EXPECT_EQ(expected_value, filter_.GetPercentileValue());
std::random_shuffle(zero_to_nine, zero_to_nine + 10);
for (int64_t value : zero_to_nine)
filter_.Insert(value);
EXPECT_EQ(expected_value, filter_.GetPercentileValue());
}
}
} // namespace webrtc