xref: /aosp_15_r20/external/webrtc/modules/audio_processing/test/debug_dump_replayer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2016 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/test/debug_dump_replayer.h"
12 
13 #include <string>
14 
15 #include "absl/strings/string_view.h"
16 #include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
17 #include "modules/audio_processing/test/protobuf_utils.h"
18 #include "modules/audio_processing/test/runtime_setting_util.h"
19 #include "rtc_base/checks.h"
20 
21 namespace webrtc {
22 namespace test {
23 
24 namespace {
25 
MaybeResetBuffer(std::unique_ptr<ChannelBuffer<float>> * buffer,const StreamConfig & config)26 void MaybeResetBuffer(std::unique_ptr<ChannelBuffer<float>>* buffer,
27                       const StreamConfig& config) {
28   auto& buffer_ref = *buffer;
29   if (!buffer_ref.get() || buffer_ref->num_frames() != config.num_frames() ||
30       buffer_ref->num_channels() != config.num_channels()) {
31     buffer_ref.reset(
32         new ChannelBuffer<float>(config.num_frames(), config.num_channels()));
33   }
34 }
35 
36 }  // namespace
37 
DebugDumpReplayer()38 DebugDumpReplayer::DebugDumpReplayer()
39     : input_(nullptr),  // will be created upon usage.
40       reverse_(nullptr),
41       output_(nullptr),
42       apm_(nullptr),
43       debug_file_(nullptr) {}
44 
~DebugDumpReplayer()45 DebugDumpReplayer::~DebugDumpReplayer() {
46   if (debug_file_)
47     fclose(debug_file_);
48 }
49 
SetDumpFile(absl::string_view filename)50 bool DebugDumpReplayer::SetDumpFile(absl::string_view filename) {
51   debug_file_ = fopen(std::string(filename).c_str(), "rb");
52   LoadNextMessage();
53   return debug_file_;
54 }
55 
56 // Get next event that has not run.
GetNextEvent() const57 absl::optional<audioproc::Event> DebugDumpReplayer::GetNextEvent() const {
58   if (!has_next_event_)
59     return absl::nullopt;
60   else
61     return next_event_;
62 }
63 
64 // Run the next event. Returns the event type.
RunNextEvent()65 bool DebugDumpReplayer::RunNextEvent() {
66   if (!has_next_event_)
67     return false;
68   switch (next_event_.type()) {
69     case audioproc::Event::INIT:
70       OnInitEvent(next_event_.init());
71       break;
72     case audioproc::Event::STREAM:
73       OnStreamEvent(next_event_.stream());
74       break;
75     case audioproc::Event::REVERSE_STREAM:
76       OnReverseStreamEvent(next_event_.reverse_stream());
77       break;
78     case audioproc::Event::CONFIG:
79       OnConfigEvent(next_event_.config());
80       break;
81     case audioproc::Event::RUNTIME_SETTING:
82       OnRuntimeSettingEvent(next_event_.runtime_setting());
83       break;
84     case audioproc::Event::UNKNOWN_EVENT:
85       // We do not expect to receive UNKNOWN event.
86       RTC_CHECK_NOTREACHED();
87   }
88   LoadNextMessage();
89   return true;
90 }
91 
GetOutput() const92 const ChannelBuffer<float>* DebugDumpReplayer::GetOutput() const {
93   return output_.get();
94 }
95 
GetOutputConfig() const96 StreamConfig DebugDumpReplayer::GetOutputConfig() const {
97   return output_config_;
98 }
99 
100 // OnInitEvent reset the input/output/reserve channel format.
OnInitEvent(const audioproc::Init & msg)101 void DebugDumpReplayer::OnInitEvent(const audioproc::Init& msg) {
102   RTC_CHECK(msg.has_num_input_channels());
103   RTC_CHECK(msg.has_output_sample_rate());
104   RTC_CHECK(msg.has_num_output_channels());
105   RTC_CHECK(msg.has_reverse_sample_rate());
106   RTC_CHECK(msg.has_num_reverse_channels());
107 
108   input_config_ = StreamConfig(msg.sample_rate(), msg.num_input_channels());
109   output_config_ =
110       StreamConfig(msg.output_sample_rate(), msg.num_output_channels());
111   reverse_config_ =
112       StreamConfig(msg.reverse_sample_rate(), msg.num_reverse_channels());
113 
114   MaybeResetBuffer(&input_, input_config_);
115   MaybeResetBuffer(&output_, output_config_);
116   MaybeResetBuffer(&reverse_, reverse_config_);
117 }
118 
119 // OnStreamEvent replays an input signal and verifies the output.
OnStreamEvent(const audioproc::Stream & msg)120 void DebugDumpReplayer::OnStreamEvent(const audioproc::Stream& msg) {
121   // APM should have been created.
122   RTC_CHECK(apm_.get());
123 
124   if (msg.has_applied_input_volume()) {
125     apm_->set_stream_analog_level(msg.applied_input_volume());
126   }
127   RTC_CHECK_EQ(AudioProcessing::kNoError,
128                apm_->set_stream_delay_ms(msg.delay()));
129 
130   if (msg.has_keypress()) {
131     apm_->set_stream_key_pressed(msg.keypress());
132   } else {
133     apm_->set_stream_key_pressed(true);
134   }
135 
136   RTC_CHECK_EQ(input_config_.num_channels(),
137                static_cast<size_t>(msg.input_channel_size()));
138   RTC_CHECK_EQ(input_config_.num_frames() * sizeof(float),
139                msg.input_channel(0).size());
140 
141   for (int i = 0; i < msg.input_channel_size(); ++i) {
142     memcpy(input_->channels()[i], msg.input_channel(i).data(),
143            msg.input_channel(i).size());
144   }
145 
146   RTC_CHECK_EQ(AudioProcessing::kNoError,
147                apm_->ProcessStream(input_->channels(), input_config_,
148                                    output_config_, output_->channels()));
149 }
150 
OnReverseStreamEvent(const audioproc::ReverseStream & msg)151 void DebugDumpReplayer::OnReverseStreamEvent(
152     const audioproc::ReverseStream& msg) {
153   // APM should have been created.
154   RTC_CHECK(apm_.get());
155 
156   RTC_CHECK_GT(msg.channel_size(), 0);
157   RTC_CHECK_EQ(reverse_config_.num_channels(),
158                static_cast<size_t>(msg.channel_size()));
159   RTC_CHECK_EQ(reverse_config_.num_frames() * sizeof(float),
160                msg.channel(0).size());
161 
162   for (int i = 0; i < msg.channel_size(); ++i) {
163     memcpy(reverse_->channels()[i], msg.channel(i).data(),
164            msg.channel(i).size());
165   }
166 
167   RTC_CHECK_EQ(
168       AudioProcessing::kNoError,
169       apm_->ProcessReverseStream(reverse_->channels(), reverse_config_,
170                                  reverse_config_, reverse_->channels()));
171 }
172 
OnConfigEvent(const audioproc::Config & msg)173 void DebugDumpReplayer::OnConfigEvent(const audioproc::Config& msg) {
174   MaybeRecreateApm(msg);
175   ConfigureApm(msg);
176 }
177 
OnRuntimeSettingEvent(const audioproc::RuntimeSetting & msg)178 void DebugDumpReplayer::OnRuntimeSettingEvent(
179     const audioproc::RuntimeSetting& msg) {
180   RTC_CHECK(apm_.get());
181   ReplayRuntimeSetting(apm_.get(), msg);
182 }
183 
MaybeRecreateApm(const audioproc::Config & msg)184 void DebugDumpReplayer::MaybeRecreateApm(const audioproc::Config& msg) {
185   // These configurations cannot be changed on the fly.
186   RTC_CHECK(msg.has_aec_delay_agnostic_enabled());
187   RTC_CHECK(msg.has_aec_extended_filter_enabled());
188 
189   // We only create APM once, since changes on these fields should not
190   // happen in current implementation.
191   if (!apm_.get()) {
192     apm_ = AudioProcessingBuilderForTesting().Create();
193   }
194 }
195 
ConfigureApm(const audioproc::Config & msg)196 void DebugDumpReplayer::ConfigureApm(const audioproc::Config& msg) {
197   AudioProcessing::Config apm_config;
198 
199   // AEC2/AECM configs.
200   RTC_CHECK(msg.has_aec_enabled());
201   RTC_CHECK(msg.has_aecm_enabled());
202   apm_config.echo_canceller.enabled = msg.aec_enabled() || msg.aecm_enabled();
203   apm_config.echo_canceller.mobile_mode = msg.aecm_enabled();
204 
205   // HPF configs.
206   RTC_CHECK(msg.has_hpf_enabled());
207   apm_config.high_pass_filter.enabled = msg.hpf_enabled();
208 
209   // Preamp configs.
210   RTC_CHECK(msg.has_pre_amplifier_enabled());
211   apm_config.pre_amplifier.enabled = msg.pre_amplifier_enabled();
212   apm_config.pre_amplifier.fixed_gain_factor =
213       msg.pre_amplifier_fixed_gain_factor();
214 
215   // NS configs.
216   RTC_CHECK(msg.has_ns_enabled());
217   RTC_CHECK(msg.has_ns_level());
218   apm_config.noise_suppression.enabled = msg.ns_enabled();
219   apm_config.noise_suppression.level =
220       static_cast<AudioProcessing::Config::NoiseSuppression::Level>(
221           msg.ns_level());
222 
223   // TS configs.
224   RTC_CHECK(msg.has_transient_suppression_enabled());
225   apm_config.transient_suppression.enabled =
226       msg.transient_suppression_enabled();
227 
228   // AGC configs.
229   RTC_CHECK(msg.has_agc_enabled());
230   RTC_CHECK(msg.has_agc_mode());
231   RTC_CHECK(msg.has_agc_limiter_enabled());
232   apm_config.gain_controller1.enabled = msg.agc_enabled();
233   apm_config.gain_controller1.mode =
234       static_cast<AudioProcessing::Config::GainController1::Mode>(
235           msg.agc_mode());
236   apm_config.gain_controller1.enable_limiter = msg.agc_limiter_enabled();
237   RTC_CHECK(msg.has_noise_robust_agc_enabled());
238   apm_config.gain_controller1.analog_gain_controller.enabled =
239       msg.noise_robust_agc_enabled();
240 
241   apm_->ApplyConfig(apm_config);
242 }
243 
LoadNextMessage()244 void DebugDumpReplayer::LoadNextMessage() {
245   has_next_event_ =
246       debug_file_ && ReadMessageFromFile(debug_file_, &next_event_);
247 }
248 
249 }  // namespace test
250 }  // namespace webrtc
251