1 /* 2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_AUDIO_PROCESSING_AGC2_INPUT_VOLUME_STATS_REPORTER_H_ 12 #define MODULES_AUDIO_PROCESSING_AGC2_INPUT_VOLUME_STATS_REPORTER_H_ 13 14 #include "absl/types/optional.h" 15 #include "rtc_base/gtest_prod_util.h" 16 #include "system_wrappers/include/metrics.h" 17 18 namespace webrtc { 19 20 // Input volume statistics calculator. Computes aggregate stats based on the 21 // framewise input volume observed by `UpdateStatistics()`. Periodically logs 22 // the statistics into a histogram. 23 class InputVolumeStatsReporter { 24 public: 25 enum class InputVolumeType { 26 kApplied = 0, 27 kRecommended = 1, 28 }; 29 30 explicit InputVolumeStatsReporter(InputVolumeType input_volume_type); 31 InputVolumeStatsReporter(const InputVolumeStatsReporter&) = delete; 32 InputVolumeStatsReporter operator=(const InputVolumeStatsReporter&) = delete; 33 ~InputVolumeStatsReporter(); 34 35 // Updates the stats based on `input_volume`. Periodically logs the stats into 36 // a histogram. 37 void UpdateStatistics(int input_volume); 38 39 private: 40 FRIEND_TEST_ALL_PREFIXES(InputVolumeStatsReporterTest, 41 CheckVolumeUpdateStatsForEmptyStats); 42 FRIEND_TEST_ALL_PREFIXES(InputVolumeStatsReporterTest, 43 CheckVolumeUpdateStatsAfterNoVolumeChange); 44 FRIEND_TEST_ALL_PREFIXES(InputVolumeStatsReporterTest, 45 CheckVolumeUpdateStatsAfterVolumeIncrease); 46 FRIEND_TEST_ALL_PREFIXES(InputVolumeStatsReporterTest, 47 CheckVolumeUpdateStatsAfterVolumeDecrease); 48 FRIEND_TEST_ALL_PREFIXES(InputVolumeStatsReporterTest, 49 CheckVolumeUpdateStatsAfterReset); 50 51 // Stores input volume update stats to enable calculation of update rate and 52 // average update separately for volume increases and decreases. 53 struct VolumeUpdateStats { 54 int num_decreases = 0; 55 int num_increases = 0; 56 int sum_decreases = 0; 57 int sum_increases = 0; 58 } volume_update_stats_; 59 60 // Returns a copy of the stored statistics. Use only for testing. volume_update_stats()61 VolumeUpdateStats volume_update_stats() const { return volume_update_stats_; } 62 63 // Computes aggregate stat and logs them into a histogram. 64 void LogVolumeUpdateStats() const; 65 66 // Histograms. 67 struct Histograms { 68 metrics::Histogram* const decrease_rate; 69 metrics::Histogram* const decrease_average; 70 metrics::Histogram* const increase_rate; 71 metrics::Histogram* const increase_average; 72 metrics::Histogram* const update_rate; 73 metrics::Histogram* const update_average; AllPointersSetHistograms74 bool AllPointersSet() const { 75 return !!decrease_rate && !!decrease_average && !!increase_rate && 76 !!increase_average && !!update_rate && !!update_average; 77 } 78 } histograms_; 79 80 // True if the stats cannot be logged. 81 const bool cannot_log_stats_; 82 83 int log_volume_update_stats_counter_ = 0; 84 absl::optional<int> previous_input_volume_ = absl::nullopt; 85 }; 86 } // namespace webrtc 87 88 #endif // MODULES_AUDIO_PROCESSING_AGC2_INPUT_VOLUME_STATS_REPORTER_H_ 89