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