Skip to content

Commit

Permalink
Move SampleStatsCounter to public API
Browse files Browse the repository at this point in the history
Bug: None
Change-Id: I8956f6febbb1caf71e951d212d57746fe1ec5eb2
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/184506
Commit-Queue: Artem Titov <[email protected]>
Reviewed-by: Karl Wiberg <[email protected]>
Cr-Commit-Position: refs/heads/master@{#32142}
  • Loading branch information
Artem Titov authored and Commit Bot committed Sep 18, 2020
1 parent 979c13f commit 9d77762
Show file tree
Hide file tree
Showing 25 changed files with 126 additions and 56 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ if (rtc_include_tests) {
"api:rtc_api_unittests",
"api/audio/test:audio_api_unittests",
"api/audio_codecs/test:audio_codecs_api_unittests",
"api/numerics:numerics_unittests",
"api/transport:stun_unittest",
"api/video/test:rtc_api_video_unittests",
"api/video_codecs/test:video_codecs_api_unittests",
Expand Down
41 changes: 41 additions & 0 deletions api/numerics/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2020 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.

import("../../webrtc.gni")

rtc_library("numerics") {
visibility = [ "*" ]

sources = [
"samples_stats_counter.cc",
"samples_stats_counter.h",
]
deps = [
"..:array_view",
"../../rtc_base:checks",
"../../rtc_base:rtc_numerics",
"../../rtc_base:timeutils",
"../units:timestamp",
]
absl_deps = [ "//third_party/abseil-cpp/absl/algorithm:container" ]
}

if (rtc_include_tests) {
rtc_library("numerics_unittests") {
visibility = [ "*" ]
testonly = true

sources = [ "samples_stats_counter_unittest.cc" ]

deps = [
":numerics",
"../../test:test_support",
]
absl_deps = [ "//third_party/abseil-cpp/absl/algorithm:container" ]
}
}
6 changes: 6 additions & 0 deletions api/numerics/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
specific_include_rules = {
# Some internal headers are allowed even in API headers:
"samples_stats_counter\.h": [
"+rtc_base/numerics/running_statistics.h",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#include "rtc_base/numerics/samples_stats_counter.h"
#include "api/numerics/samples_stats_counter.h"

#include <algorithm>
#include <cmath>

#include "absl/algorithm/container.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#ifndef RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_
#define RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_
#ifndef API_NUMERICS_SAMPLES_STATS_COUNTER_H_
#define API_NUMERICS_SAMPLES_STATS_COUNTER_H_

#include <vector>

Expand Down Expand Up @@ -98,7 +98,7 @@ class SamplesStatsCounter {
}

private:
RunningStatistics<double> stats_;
webrtc_impl::RunningStatistics<double> stats_;
std::vector<StatsSample> samples_;
bool sorted_ = false;
};
Expand All @@ -116,4 +116,4 @@ SamplesStatsCounter operator/(const SamplesStatsCounter& counter, double value);

} // namespace webrtc

#endif // RTC_BASE_NUMERICS_SAMPLES_STATS_COUNTER_H_
#endif // API_NUMERICS_SAMPLES_STATS_COUNTER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#include "rtc_base/numerics/samples_stats_counter.h"
#include "api/numerics/samples_stats_counter.h"

#include <math.h>

Expand Down
1 change: 1 addition & 0 deletions modules/video_coding/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ if (rtc_include_tests) {
]
deps = [
"../../api:videocodec_test_fixture_api",
"../../api/numerics",
"../../rtc_base:checks",
"../../rtc_base:rtc_numerics",
"../../rtc_base:stringutils",
Expand Down
30 changes: 15 additions & 15 deletions modules/video_coding/codecs/test/videocodec_test_stats_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,20 @@ VideoStatistics VideoCodecTestStatsImpl::SliceAndCalcVideoStatistic(
VideoStatistics video_stat;

float buffer_level_bits = 0.0f;
RunningStatistics<float> buffer_level_sec;
webrtc_impl::RunningStatistics<float> buffer_level_sec;

RunningStatistics<size_t> key_frame_size_bytes;
RunningStatistics<size_t> delta_frame_size_bytes;
webrtc_impl::RunningStatistics<size_t> key_frame_size_bytes;
webrtc_impl::RunningStatistics<size_t> delta_frame_size_bytes;

RunningStatistics<size_t> frame_encoding_time_us;
RunningStatistics<size_t> frame_decoding_time_us;
webrtc_impl::RunningStatistics<size_t> frame_encoding_time_us;
webrtc_impl::RunningStatistics<size_t> frame_decoding_time_us;

RunningStatistics<float> psnr_y;
RunningStatistics<float> psnr_u;
RunningStatistics<float> psnr_v;
RunningStatistics<float> psnr;
RunningStatistics<float> ssim;
RunningStatistics<int> qp;
webrtc_impl::RunningStatistics<float> psnr_y;
webrtc_impl::RunningStatistics<float> psnr_u;
webrtc_impl::RunningStatistics<float> psnr_v;
webrtc_impl::RunningStatistics<float> psnr;
webrtc_impl::RunningStatistics<float> ssim;
webrtc_impl::RunningStatistics<int> qp;

size_t rtp_timestamp_first_frame = 0;
size_t rtp_timestamp_prev_frame = 0;
Expand Down Expand Up @@ -329,10 +329,10 @@ VideoStatistics VideoCodecTestStatsImpl::SliceAndCalcVideoStatistic(
? 1000000.0f / mean_decode_time_us
: std::numeric_limits<float>::max();

auto MaxDelaySec =
[target_bitrate_kbps](const RunningStatistics<size_t>& stats) {
return 8 * stats.GetMax().value_or(0) / 1000 / target_bitrate_kbps;
};
auto MaxDelaySec = [target_bitrate_kbps](
const webrtc_impl::RunningStatistics<size_t>& stats) {
return 8 * stats.GetMax().value_or(0) / 1000 / target_bitrate_kbps;
};

video_stat.avg_delay_sec = buffer_level_sec.GetMean().value_or(0);
video_stat.max_key_frame_delay_sec = MaxDelaySec(key_frame_size_bytes);
Expand Down
34 changes: 19 additions & 15 deletions rtc_base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ rtc_library("platform_thread") {
visibility = [
":rtc_base_approved",
":rtc_task_queue_libevent",
":rtc_task_queue_win",
":rtc_task_queue_stdlib",
":rtc_task_queue_win",
"synchronization:mutex",
"synchronization:sequence_checker",
]
Expand Down Expand Up @@ -587,8 +587,6 @@ rtc_library("rtc_numerics") {
sources = [
"numerics/event_based_exponential_moving_average.cc",
"numerics/event_based_exponential_moving_average.h",
"numerics/event_rate_counter.cc",
"numerics/event_rate_counter.h",
"numerics/exp_filter.cc",
"numerics/exp_filter.h",
"numerics/math_utils.h",
Expand All @@ -597,25 +595,30 @@ rtc_library("rtc_numerics") {
"numerics/moving_median_filter.h",
"numerics/percentile_filter.h",
"numerics/running_statistics.h",
"numerics/sample_stats.cc",
"numerics/sample_stats.h",
"numerics/samples_stats_counter.cc",
"numerics/samples_stats_counter.h",
"numerics/sequence_number_util.h",
]
deps = [
":checks",
":macromagic",
":rtc_base_approved",
":safe_compare",
"../api:array_view",
]
absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
}

rtc_library("rtc_stats_counters") {
sources = [
"numerics/event_rate_counter.cc",
"numerics/event_rate_counter.h",
"numerics/sample_stats.cc",
"numerics/sample_stats.h",
]
deps = [
"../api/numerics",
"../api/units:data_rate",
"../api/units:time_delta",
"../api/units:timestamp",
]
absl_deps = [
"//third_party/abseil-cpp/absl/algorithm:container",
"//third_party/abseil-cpp/absl/types:optional",
]
absl_deps = []
}

config("rtc_json_suppressions") {
Expand Down Expand Up @@ -804,6 +807,7 @@ rtc_library("rtc_base") {
"../api:array_view",
"../api:function_view",
"../api:scoped_refptr",
"../api/numerics",
"../api/task_queue",
"../system_wrappers:field_trial",
"network:sent_packet",
Expand Down Expand Up @@ -942,7 +946,6 @@ rtc_library("rtc_base") {
"callback.h",
"log_sinks.cc",
"log_sinks.h",
"numerics/math_utils.h",
"rolling_accumulator.h",
"ssl_roots.h",
]
Expand Down Expand Up @@ -1270,6 +1273,7 @@ if (rtc_include_tests) {
":rtc_base",
":rtc_base_approved",
":rtc_base_tests_utils",
":rtc_numerics",
":rtc_task_queue",
":safe_compare",
":safe_minmax",
Expand All @@ -1278,6 +1282,7 @@ if (rtc_include_tests) {
":testclient",
"../api:array_view",
"../api:scoped_refptr",
"../api/numerics",
"../api/units:time_delta",
"../system_wrappers",
"../test:fileutils",
Expand Down Expand Up @@ -1351,7 +1356,6 @@ if (rtc_include_tests) {
"numerics/moving_median_filter_unittest.cc",
"numerics/percentile_filter_unittest.cc",
"numerics/running_statistics_unittest.cc",
"numerics/samples_stats_counter_unittest.cc",
"numerics/sequence_number_util_unittest.cc",
]
deps = [
Expand Down
11 changes: 8 additions & 3 deletions rtc_base/numerics/math_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#ifndef RTC_BASE_NUMERICS_MATH_UTILS_H_
#define RTC_BASE_NUMERICS_MATH_UTILS_H_
#ifndef API_NUMERICS_MATH_UTILS_H_
#define API_NUMERICS_MATH_UTILS_H_

#include <limits>
#include <type_traits>

#include "rtc_base/checks.h"

namespace webrtc {
namespace webrtc_impl {
// Given two numbers |x| and |y| such that x >= y, computes the difference
// x - y without causing undefined behavior due to signed overflow.
template <typename T>
Expand Down Expand Up @@ -67,4 +69,7 @@ constexpr T minus_infinity_or_min() {
return std::numeric_limits<T>::min();
}

#endif // RTC_BASE_NUMERICS_MATH_UTILS_H_
} // namespace webrtc_impl
} // namespace webrtc

#endif // API_NUMERICS_MATH_UTILS_H_
8 changes: 5 additions & 3 deletions rtc_base/numerics/running_statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree.
*/

#ifndef RTC_BASE_NUMERICS_RUNNING_STATISTICS_H_
#define RTC_BASE_NUMERICS_RUNNING_STATISTICS_H_
#ifndef API_NUMERICS_RUNNING_STATISTICS_H_
#define API_NUMERICS_RUNNING_STATISTICS_H_

#include <algorithm>
#include <cmath>
Expand All @@ -20,6 +20,7 @@
#include "rtc_base/numerics/math_utils.h"

namespace webrtc {
namespace webrtc_impl {

// tl;dr: Robust and efficient online computation of statistics,
// using Welford's method for variance. [1]
Expand Down Expand Up @@ -154,6 +155,7 @@ class RunningStatistics {
double cumul_ = 0; // Variance * size_, sometimes noted m2.
};

} // namespace webrtc_impl
} // namespace webrtc

#endif // RTC_BASE_NUMERICS_RUNNING_STATISTICS_H_
#endif // API_NUMERICS_RUNNING_STATISTICS_H_
5 changes: 3 additions & 2 deletions rtc_base/numerics/running_statistics_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// Tests were copied from samples_stats_counter_unittest.cc.

namespace webrtc {
namespace webrtc_impl {
namespace {

RunningStatistics<double> CreateStatsFilledWithIntsFrom1ToN(int n) {
Expand Down Expand Up @@ -55,8 +56,6 @@ class RunningStatisticsTest : public ::testing::TestWithParam<int> {};

constexpr int SIZE_FOR_MERGE = 5;

} // namespace

TEST(RunningStatistics, FullSimpleTest) {
auto stats = CreateStatsFilledWithIntsFrom1ToN(100);

Expand Down Expand Up @@ -192,4 +191,6 @@ INSTANTIATE_TEST_SUITE_P(RunningStatisticsTests,
RunningStatisticsTest,
::testing::Range(0, SIZE_FOR_MERGE + 1));

} // namespace
} // namespace webrtc_impl
} // namespace webrtc
2 changes: 1 addition & 1 deletion rtc_base/numerics/sample_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#ifndef RTC_BASE_NUMERICS_SAMPLE_STATS_H_
#define RTC_BASE_NUMERICS_SAMPLE_STATS_H_

#include "api/numerics/samples_stats_counter.h"
#include "api/units/data_rate.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "rtc_base/numerics/samples_stats_counter.h"

namespace webrtc {
template <typename T>
Expand Down
5 changes: 3 additions & 2 deletions rtc_base/random_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void BucketTestSignedInterval(unsigned int bucket_count,

ASSERT_GE(high, low);
ASSERT_GE(bucket_count, 2u);
uint32_t interval = unsigned_difference<int32_t>(high, low) + 1;
uint32_t interval = webrtc_impl::unsigned_difference<int32_t>(high, low) + 1;
uint32_t numbers_per_bucket;
if (interval == 0) {
// The computation high - low + 1 should be 2^32 but overflowed
Expand All @@ -136,7 +136,8 @@ void BucketTestSignedInterval(unsigned int bucket_count,
int32_t sample = prng->Rand(low, high);
EXPECT_LE(low, sample);
EXPECT_GE(high, sample);
buckets[unsigned_difference<int32_t>(sample, low) / numbers_per_bucket]++;
buckets[webrtc_impl::unsigned_difference<int32_t>(sample, low) /
numbers_per_bucket]++;
}

for (unsigned int i = 0; i < bucket_count; i++) {
Expand Down
4 changes: 2 additions & 2 deletions rtc_base/rolling_accumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RollingAccumulator {
size_t count() const { return static_cast<size_t>(stats_.Size()); }

void Reset() {
stats_ = webrtc::RunningStatistics<T>();
stats_ = webrtc::webrtc_impl::RunningStatistics<T>();
next_index_ = 0U;
max_ = T();
max_stale_ = false;
Expand Down Expand Up @@ -129,7 +129,7 @@ class RollingAccumulator {
double ComputeVariance() const { return stats_.GetVariance().value_or(0); }

private:
webrtc::RunningStatistics<T> stats_;
webrtc::webrtc_impl::RunningStatistics<T> stats_;
size_t next_index_;
mutable T max_;
mutable bool max_stale_;
Expand Down
Loading

0 comments on commit 9d77762

Please sign in to comment.