blob: 45600f05b6ad71f2444ab34a14b61172fa9baca2 [file] [log] [blame]
minyue0de1c132016-03-17 02:39:30 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/test/debug_dump_replayer.h"
minyue0de1c132016-03-17 02:39:30 -070012
sazabe490b22018-10-03 17:03:13 +020013#include "modules/audio_processing/echo_cancellation_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_processing/test/protobuf_utils.h"
Alex Loiko62347222018-09-10 10:18:07 +020015#include "modules/audio_processing/test/runtime_setting_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
minyue0de1c132016-03-17 02:39:30 -070017
18namespace webrtc {
19namespace test {
20
21namespace {
22
23void 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()) {
Yves Gerey665174f2018-06-19 15:03:05 +020028 buffer_ref.reset(
29 new ChannelBuffer<float>(config.num_frames(), config.num_channels()));
minyue0de1c132016-03-17 02:39:30 -070030 }
31}
32
33} // namespace
34
35DebugDumpReplayer::DebugDumpReplayer()
36 : input_(nullptr), // will be created upon usage.
37 reverse_(nullptr),
38 output_(nullptr),
39 apm_(nullptr),
40 debug_file_(nullptr) {}
41
42DebugDumpReplayer::~DebugDumpReplayer() {
43 if (debug_file_)
44 fclose(debug_file_);
45}
46
47bool 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.
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020054absl::optional<audioproc::Event> DebugDumpReplayer::GetNextEvent() const {
minyue0de1c132016-03-17 02:39:30 -070055 if (!has_next_event_)
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020056 return absl::nullopt;
minyue0de1c132016-03-17 02:39:30 -070057 else
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010058 return next_event_;
minyue0de1c132016-03-17 02:39:30 -070059}
60
61// Run the next event. Returns the event type.
62bool 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;
Alex Loiko62347222018-09-10 10:18:07 +020078 case audioproc::Event::RUNTIME_SETTING:
79 OnRuntimeSettingEvent(next_event_.runtime_setting());
80 break;
minyue0de1c132016-03-17 02:39:30 -070081 case audioproc::Event::UNKNOWN_EVENT:
82 // We do not expect to receive UNKNOWN event.
Alex Loiko62347222018-09-10 10:18:07 +020083 RTC_CHECK(false);
minyue0de1c132016-03-17 02:39:30 -070084 return false;
85 }
86 LoadNextMessage();
87 return true;
88}
89
90const ChannelBuffer<float>* DebugDumpReplayer::GetOutput() const {
91 return output_.get();
92}
93
94StreamConfig DebugDumpReplayer::GetOutputConfig() const {
95 return output_config_;
96}
97
98// OnInitEvent reset the input/output/reserve channel format.
99void 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.
118void DebugDumpReplayer::OnStreamEvent(const audioproc::Stream& msg) {
119 // APM should have been created.
120 RTC_CHECK(apm_.get());
121
Sam Zackrisson41478c72019-10-15 10:10:26 +0200122 apm_->set_stream_analog_level(msg.level());
minyue0de1c132016-03-17 02:39:30 -0700123 RTC_CHECK_EQ(AudioProcessing::kNoError,
124 apm_->set_stream_delay_ms(msg.delay()));
125
minyue0de1c132016-03-17 02:39:30 -0700126 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
147void 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
169void DebugDumpReplayer::OnConfigEvent(const audioproc::Config& msg) {
170 MaybeRecreateApm(msg);
171 ConfigureApm(msg);
172}
173
Alex Loiko62347222018-09-10 10:18:07 +0200174void DebugDumpReplayer::OnRuntimeSettingEvent(
175 const audioproc::RuntimeSetting& msg) {
176 RTC_CHECK(apm_.get());
177 ReplayRuntimeSetting(apm_.get(), msg);
178}
179
minyue0de1c132016-03-17 02:39:30 -0700180void 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 config.Set<DelayAgnostic>(
185 new DelayAgnostic(msg.aec_delay_agnostic_enabled()));
186
187 RTC_CHECK(msg.has_noise_robust_agc_enabled());
188 config.Set<ExperimentalAgc>(
189 new ExperimentalAgc(msg.noise_robust_agc_enabled()));
190
191 RTC_CHECK(msg.has_transient_suppression_enabled());
192 config.Set<ExperimentalNs>(
193 new ExperimentalNs(msg.transient_suppression_enabled()));
194
195 RTC_CHECK(msg.has_aec_extended_filter_enabled());
196 config.Set<ExtendedFilter>(
197 new ExtendedFilter(msg.aec_extended_filter_enabled()));
198
minyue0de1c132016-03-17 02:39:30 -0700199 // We only create APM once, since changes on these fields should not
200 // happen in current implementation.
201 if (!apm_.get()) {
Ivo Creusen62337e52018-01-09 14:17:33 +0100202 apm_.reset(AudioProcessingBuilder().Create(config));
minyue0de1c132016-03-17 02:39:30 -0700203 }
204}
205
206void DebugDumpReplayer::ConfigureApm(const audioproc::Config& msg) {
peah8271d042016-11-22 07:24:52 -0800207 AudioProcessing::Config apm_config;
208
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200209 // AEC2/AECM configs.
minyue0de1c132016-03-17 02:39:30 -0700210 RTC_CHECK(msg.has_aec_enabled());
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200211 RTC_CHECK(msg.has_aecm_enabled());
212 apm_config.echo_canceller.enabled = msg.aec_enabled() || msg.aecm_enabled();
213 apm_config.echo_canceller.mobile_mode = msg.aecm_enabled();
minyue0de1c132016-03-17 02:39:30 -0700214
minyue0de1c132016-03-17 02:39:30 -0700215 RTC_CHECK(msg.has_aec_suppression_level());
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200216 apm_config.echo_canceller.legacy_moderate_suppression_level =
sazabe490b22018-10-03 17:03:13 +0200217 static_cast<EchoCancellationImpl::SuppressionLevel>(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200218 msg.aec_suppression_level()) ==
sazabe490b22018-10-03 17:03:13 +0200219 EchoCancellationImpl::SuppressionLevel::kModerateSuppression;
minyue0de1c132016-03-17 02:39:30 -0700220
Sam Zackrisson23513132019-01-11 15:10:32 +0100221 // HPF configs.
222 RTC_CHECK(msg.has_hpf_enabled());
223 apm_config.high_pass_filter.enabled = msg.hpf_enabled();
224
225 // Preamp configs.
226 RTC_CHECK(msg.has_pre_amplifier_enabled());
227 apm_config.pre_amplifier.enabled = msg.pre_amplifier_enabled();
228 apm_config.pre_amplifier.fixed_gain_factor =
229 msg.pre_amplifier_fixed_gain_factor();
230
231 // NS configs.
232 RTC_CHECK(msg.has_ns_enabled());
233 RTC_CHECK(msg.has_ns_level());
234 apm_config.noise_suppression.enabled = msg.ns_enabled();
235 apm_config.noise_suppression.level =
236 static_cast<AudioProcessing::Config::NoiseSuppression::Level>(
237 msg.ns_level());
238
minyue0de1c132016-03-17 02:39:30 -0700239 // AGC configs.
240 RTC_CHECK(msg.has_agc_enabled());
minyue0de1c132016-03-17 02:39:30 -0700241 RTC_CHECK(msg.has_agc_mode());
minyue0de1c132016-03-17 02:39:30 -0700242 RTC_CHECK(msg.has_agc_limiter_enabled());
Sam Zackrisson41478c72019-10-15 10:10:26 +0200243 apm_config.gain_controller1.enabled = msg.agc_enabled();
244 apm_config.gain_controller1.mode =
245 static_cast<AudioProcessing::Config::GainController1::Mode>(
246 msg.agc_mode());
247 apm_config.gain_controller1.enable_limiter = msg.agc_limiter_enabled();
248
249 apm_->ApplyConfig(apm_config);
minyue0de1c132016-03-17 02:39:30 -0700250}
251
252void DebugDumpReplayer::LoadNextMessage() {
253 has_next_event_ =
254 debug_file_ && ReadMessageFromFile(debug_file_, &next_event_);
255}
256
257} // namespace test
258} // namespace webrtc