• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017 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 #include "modules/audio_processing/aec3/echo_remover_metrics.h"
12 
13 #include <math.h>
14 #include <stddef.h>
15 
16 #include <algorithm>
17 #include <cmath>
18 #include <numeric>
19 
20 #include "rtc_base/checks.h"
21 #include "rtc_base/numerics/safe_minmax.h"
22 #include "system_wrappers/include/metrics.h"
23 
24 namespace webrtc {
25 
26 namespace {
27 
28 constexpr float kOneByMetricsCollectionBlocks = 1.f / kMetricsCollectionBlocks;
29 
30 }  // namespace
31 
DbMetric()32 EchoRemoverMetrics::DbMetric::DbMetric() : DbMetric(0.f, 0.f, 0.f) {}
DbMetric(float sum_value,float floor_value,float ceil_value)33 EchoRemoverMetrics::DbMetric::DbMetric(float sum_value,
34                                        float floor_value,
35                                        float ceil_value)
36     : sum_value(sum_value), floor_value(floor_value), ceil_value(ceil_value) {}
37 
Update(float value)38 void EchoRemoverMetrics::DbMetric::Update(float value) {
39   sum_value += value;
40   floor_value = std::min(floor_value, value);
41   ceil_value = std::max(ceil_value, value);
42 }
43 
UpdateInstant(float value)44 void EchoRemoverMetrics::DbMetric::UpdateInstant(float value) {
45   sum_value = value;
46   floor_value = std::min(floor_value, value);
47   ceil_value = std::max(ceil_value, value);
48 }
49 
EchoRemoverMetrics()50 EchoRemoverMetrics::EchoRemoverMetrics() {
51   ResetMetrics();
52 }
53 
ResetMetrics()54 void EchoRemoverMetrics::ResetMetrics() {
55   erl_.fill(DbMetric(0.f, 10000.f, 0.000f));
56   erl_time_domain_ = DbMetric(0.f, 10000.f, 0.000f);
57   erle_.fill(DbMetric(0.f, 0.f, 1000.f));
58   erle_time_domain_ = DbMetric(0.f, 0.f, 1000.f);
59   active_render_count_ = 0;
60   saturated_capture_ = false;
61 }
62 
Update(const AecState & aec_state,const std::array<float,kFftLengthBy2Plus1> & comfort_noise_spectrum,const std::array<float,kFftLengthBy2Plus1> & suppressor_gain)63 void EchoRemoverMetrics::Update(
64     const AecState& aec_state,
65     const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
66     const std::array<float, kFftLengthBy2Plus1>& suppressor_gain) {
67   metrics_reported_ = false;
68   if (++block_counter_ <= kMetricsCollectionBlocks) {
69     aec3::UpdateDbMetric(aec_state.Erl(), &erl_);
70     erl_time_domain_.UpdateInstant(aec_state.ErlTimeDomain());
71     aec3::UpdateDbMetric(aec_state.Erle()[0], &erle_);
72     erle_time_domain_.UpdateInstant(aec_state.FullBandErleLog2());
73     active_render_count_ += (aec_state.ActiveRender() ? 1 : 0);
74     saturated_capture_ = saturated_capture_ || aec_state.SaturatedCapture();
75   } else {
76     // Report the metrics over several frames in order to lower the impact of
77     // the logarithms involved on the computational complexity.
78     constexpr int kMetricsCollectionBlocksBy2 = kMetricsCollectionBlocks / 2;
79     switch (block_counter_) {
80       case kMetricsCollectionBlocks + 1:
81         RTC_HISTOGRAM_COUNTS_LINEAR(
82             "WebRTC.Audio.EchoCanceller.ErleBand0.Average",
83             aec3::TransformDbMetricForReporting(true, 0.f, 19.f, 0.f,
84                                                 kOneByMetricsCollectionBlocks,
85                                                 erle_[0].sum_value),
86             0, 19, 20);
87         RTC_HISTOGRAM_COUNTS_LINEAR(
88             "WebRTC.Audio.EchoCanceller.ErleBand0.Max",
89             aec3::TransformDbMetricForReporting(true, 0.f, 19.f, 0.f, 1.f,
90                                                 erle_[0].ceil_value),
91             0, 19, 20);
92         RTC_HISTOGRAM_COUNTS_LINEAR(
93             "WebRTC.Audio.EchoCanceller.ErleBand0.Min",
94             aec3::TransformDbMetricForReporting(true, 0.f, 19.f, 0.f, 1.f,
95                                                 erle_[0].floor_value),
96             0, 19, 20);
97         break;
98       case kMetricsCollectionBlocks + 2:
99         RTC_HISTOGRAM_COUNTS_LINEAR(
100             "WebRTC.Audio.EchoCanceller.ErleBand1.Average",
101             aec3::TransformDbMetricForReporting(true, 0.f, 19.f, 0.f,
102                                                 kOneByMetricsCollectionBlocks,
103                                                 erle_[1].sum_value),
104             0, 19, 20);
105         RTC_HISTOGRAM_COUNTS_LINEAR(
106             "WebRTC.Audio.EchoCanceller.ErleBand1.Max",
107             aec3::TransformDbMetricForReporting(true, 0.f, 19.f, 0.f, 1.f,
108                                                 erle_[1].ceil_value),
109             0, 19, 20);
110         RTC_HISTOGRAM_COUNTS_LINEAR(
111             "WebRTC.Audio.EchoCanceller.ErleBand1.Min",
112             aec3::TransformDbMetricForReporting(true, 0.f, 19.f, 0.f, 1.f,
113                                                 erle_[1].floor_value),
114             0, 19, 20);
115         break;
116       case kMetricsCollectionBlocks + 3:
117         RTC_HISTOGRAM_COUNTS_LINEAR(
118             "WebRTC.Audio.EchoCanceller.ErlBand0.Average",
119             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f,
120                                                 kOneByMetricsCollectionBlocks,
121                                                 erl_[0].sum_value),
122             0, 59, 30);
123         RTC_HISTOGRAM_COUNTS_LINEAR(
124             "WebRTC.Audio.EchoCanceller.ErlBand0.Max",
125             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
126                                                 erl_[0].ceil_value),
127             0, 59, 30);
128         RTC_HISTOGRAM_COUNTS_LINEAR(
129             "WebRTC.Audio.EchoCanceller.ErlBand0.Min",
130             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
131                                                 erl_[0].floor_value),
132             0, 59, 30);
133         break;
134       case kMetricsCollectionBlocks + 4:
135         RTC_HISTOGRAM_COUNTS_LINEAR(
136             "WebRTC.Audio.EchoCanceller.ErlBand1.Average",
137             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f,
138                                                 kOneByMetricsCollectionBlocks,
139                                                 erl_[1].sum_value),
140             0, 59, 30);
141         RTC_HISTOGRAM_COUNTS_LINEAR(
142             "WebRTC.Audio.EchoCanceller.ErlBand1.Max",
143             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
144                                                 erl_[1].ceil_value),
145             0, 59, 30);
146         RTC_HISTOGRAM_COUNTS_LINEAR(
147             "WebRTC.Audio.EchoCanceller.ErlBand1.Min",
148             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
149                                                 erl_[1].floor_value),
150             0, 59, 30);
151         break;
152       case kMetricsCollectionBlocks + 5:
153         RTC_HISTOGRAM_BOOLEAN(
154             "WebRTC.Audio.EchoCanceller.UsableLinearEstimate",
155             static_cast<int>(aec_state.UsableLinearEstimate() ? 1 : 0));
156         RTC_HISTOGRAM_BOOLEAN(
157             "WebRTC.Audio.EchoCanceller.ActiveRender",
158             static_cast<int>(
159                 active_render_count_ > kMetricsCollectionBlocksBy2 ? 1 : 0));
160         RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.EchoCanceller.FilterDelay",
161                                     aec_state.MinDirectPathFilterDelay(), 0, 30,
162                                     31);
163         RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.EchoCanceller.CaptureSaturation",
164                               static_cast<int>(saturated_capture_ ? 1 : 0));
165         break;
166       case kMetricsCollectionBlocks + 6:
167         RTC_HISTOGRAM_COUNTS_LINEAR(
168             "WebRTC.Audio.EchoCanceller.Erl.Value",
169             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
170                                                 erl_time_domain_.sum_value),
171             0, 59, 30);
172         RTC_HISTOGRAM_COUNTS_LINEAR(
173             "WebRTC.Audio.EchoCanceller.Erl.Max",
174             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
175                                                 erl_time_domain_.ceil_value),
176             0, 59, 30);
177         RTC_HISTOGRAM_COUNTS_LINEAR(
178             "WebRTC.Audio.EchoCanceller.Erl.Min",
179             aec3::TransformDbMetricForReporting(true, 0.f, 59.f, 30.f, 1.f,
180                                                 erl_time_domain_.floor_value),
181             0, 59, 30);
182         break;
183       case kMetricsCollectionBlocks + 7:
184         RTC_HISTOGRAM_COUNTS_LINEAR(
185             "WebRTC.Audio.EchoCanceller.Erle.Value",
186             aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f,
187                                                 erle_time_domain_.sum_value),
188             0, 19, 20);
189         RTC_HISTOGRAM_COUNTS_LINEAR(
190             "WebRTC.Audio.EchoCanceller.Erle.Max",
191             aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f,
192                                                 erle_time_domain_.ceil_value),
193             0, 19, 20);
194         RTC_HISTOGRAM_COUNTS_LINEAR(
195             "WebRTC.Audio.EchoCanceller.Erle.Min",
196             aec3::TransformDbMetricForReporting(false, 0.f, 19.f, 0.f, 1.f,
197                                                 erle_time_domain_.floor_value),
198             0, 19, 20);
199         metrics_reported_ = true;
200         RTC_DCHECK_EQ(kMetricsReportingIntervalBlocks, block_counter_);
201         block_counter_ = 0;
202         ResetMetrics();
203         break;
204       default:
205         RTC_NOTREACHED();
206         break;
207     }
208   }
209 }
210 
211 namespace aec3 {
212 
UpdateDbMetric(const std::array<float,kFftLengthBy2Plus1> & value,std::array<EchoRemoverMetrics::DbMetric,2> * statistic)213 void UpdateDbMetric(const std::array<float, kFftLengthBy2Plus1>& value,
214                     std::array<EchoRemoverMetrics::DbMetric, 2>* statistic) {
215   RTC_DCHECK(statistic);
216   // Truncation is intended in the band width computation.
217   constexpr int kNumBands = 2;
218   constexpr int kBandWidth = 65 / kNumBands;
219   constexpr float kOneByBandWidth = 1.f / kBandWidth;
220   RTC_DCHECK_EQ(kNumBands, statistic->size());
221   RTC_DCHECK_EQ(65, value.size());
222   for (size_t k = 0; k < statistic->size(); ++k) {
223     float average_band =
224         std::accumulate(value.begin() + kBandWidth * k,
225                         value.begin() + kBandWidth * (k + 1), 0.f) *
226         kOneByBandWidth;
227     (*statistic)[k].Update(average_band);
228   }
229 }
230 
TransformDbMetricForReporting(bool negate,float min_value,float max_value,float offset,float scaling,float value)231 int TransformDbMetricForReporting(bool negate,
232                                   float min_value,
233                                   float max_value,
234                                   float offset,
235                                   float scaling,
236                                   float value) {
237   float new_value = 10.f * std::log10(value * scaling + 1e-10f) + offset;
238   if (negate) {
239     new_value = -new_value;
240   }
241   return static_cast<int>(rtc::SafeClamp(new_value, min_value, max_value));
242 }
243 
244 }  // namespace aec3
245 
246 }  // namespace webrtc
247